1 /* $NetBSD: t_ttypty.c,v 1.3 2019/02/15 18:57:15 mgorny Exp $ */
2 
3 /*-
4  * Copyright (c) 2002, 2008, 2019 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Luke Mewburn and Jaromir Dolecek.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __COPYRIGHT("@(#) Copyright (c) 2008, 2019\
34  The NetBSD Foundation, inc. All rights reserved.");
35 __RCSID("$NetBSD: t_ttypty.c,v 1.3 2019/02/15 18:57:15 mgorny Exp $");
36 
37 #include <sys/event.h>
38 #include <sys/time.h>
39 #include <sys/wait.h>
40 
41 #include <poll.h>
42 #include <stdio.h>
43 #include <termios.h>
44 #include <unistd.h>
45 #include <util.h>
46 
47 #include <atf-c.h>
48 
49 #include "h_macros.h"
50 
51 static void
h_check(bool check_master)52 h_check(bool check_master)
53 {
54           char slavetty[1024];
55           char buffer[128];
56           struct kevent event[1];
57           pid_t child;
58           int amaster, aslave, acurrent;
59           int kq, n, status;
60 #if 0
61           int fl;
62 #endif
63           struct pollfd pfd;
64           struct termios tio;
65 
66           RL(openpty(&amaster, &aslave, slavetty, NULL, NULL));
67 
68           (void)printf("tty: openpty master %d slave %d tty '%s'\n",
69                     amaster, aslave, slavetty);
70           acurrent = check_master ? amaster : aslave;
71 
72           RL(child = fork());
73           if (child == 0) {
74                     sleep(1);
75 
76                     (void)printf("tty: child writing 'f00\\n'\n");
77                     (void)write(check_master ? aslave : amaster, "f00\n", 4);
78 
79                     _exit(0);
80           }
81 
82           /* switch ONLCR off, to not get confused by newline translation */
83           RL(tcgetattr(acurrent, &tio));
84           tio.c_oflag &= ~ONLCR;
85           RL(tcsetattr(acurrent, TCSADRAIN, &tio));
86 
87           pfd.fd = acurrent;
88           pfd.events = POLLIN;
89           (void)printf("tty: polling ...\n");
90           RL(poll(&pfd, 1, INFTIM));
91           (void)printf("tty: returned from poll - %d\n", pfd.revents);
92 
93 #if 0
94           fl = 1;
95           if (ioctl(acurrent, TIOCPKT, &fl) < 0)
96                     err(1, "ioctl");
97 #endif
98 
99           RL(kq = kqueue());
100 
101           EV_SET(&event[0], acurrent, EVFILT_READ, EV_ADD|EV_ENABLE, 0, 0, 0);
102           RL(kevent(kq, event, 1, NULL, 0, NULL));
103 
104           RL(n = kevent(kq, NULL, 0, event, 1, NULL));
105 
106           (void)printf("kevent num %d filt %d flags: %#x, fflags: %#x, "
107               "data: %" PRId64 "\n", n, event[0].filter, event[0].flags,
108               event[0].fflags, event[0].data);
109 
110           ATF_REQUIRE_EQ(event[0].filter, EVFILT_READ);
111 
112           RL(n = read(acurrent, buffer, 128));
113           (void)printf("tty: read '%.*s' (n=%d)\n", n, buffer, n);
114 
115           (void)waitpid(child, &status, 0);
116           (void)printf("tty: successful end\n");
117 }
118 
119 ATF_TC(master);
ATF_TC_HEAD(master,tc)120 ATF_TC_HEAD(master, tc)
121 {
122           atf_tc_set_md_var(tc, "descr", "Checks EVFILT_READ for master tty");
123 }
ATF_TC_BODY(master,tc)124 ATF_TC_BODY(master, tc)
125 {
126           h_check(true);
127 }
128 
129 ATF_TC(slave);
ATF_TC_HEAD(slave,tc)130 ATF_TC_HEAD(slave, tc)
131 {
132           atf_tc_set_md_var(tc, "descr", "Checks EVFILT_READ for slave tty");
133 }
ATF_TC_BODY(slave,tc)134 ATF_TC_BODY(slave, tc)
135 {
136           h_check(false);
137 }
138 
139 ATF_TC(closed_slave);
ATF_TC_HEAD(closed_slave,tc)140 ATF_TC_HEAD(closed_slave, tc)
141 {
142           atf_tc_set_md_var(tc, "descr",
143                     "Checks EVFILT_READ reporting for slave tty being closed");
144 }
ATF_TC_BODY(closed_slave,tc)145 ATF_TC_BODY(closed_slave, tc)
146 {
147           char slavetty[1024];
148           struct kevent event[1];
149           int amaster, aslave;
150           int kq, n;
151           struct timespec timeout = {5, 0};
152 
153           RL(openpty(&amaster, &aslave, slavetty, NULL, NULL));
154 
155           (void)printf("tty: openpty master %d slave %d tty '%s'\n",
156                     amaster, aslave, slavetty);
157 
158           RL(kq = kqueue());
159 
160           EV_SET(&event[0], amaster, EVFILT_READ, EV_ADD|EV_ENABLE, 0, 0, 0);
161           RL(kevent(kq, event, 1, NULL, 0, NULL));
162 
163           RL(close(aslave));
164 
165           RL(n = kevent(kq, NULL, 0, event, 1, &timeout));
166 
167           (void)printf("kevent num %d filt %d flags: %#x, fflags: %#x, "
168               "data: %" PRId64 "\n", n, event[0].filter, event[0].flags,
169               event[0].fflags, event[0].data);
170 
171           ATF_REQUIRE_EQ(n, 1);
172           ATF_REQUIRE_EQ(event[0].filter, EVFILT_READ);
173           ATF_REQUIRE_EQ(event[0].flags & EV_EOF, EV_EOF);
174 }
175 
ATF_TP_ADD_TCS(tp)176 ATF_TP_ADD_TCS(tp)
177 {
178           ATF_TP_ADD_TC(tp, master);
179           ATF_TP_ADD_TC(tp, slave);
180           ATF_TP_ADD_TC(tp, closed_slave);
181 
182           return atf_no_error();
183 }
184