1 /*
2 *
3 * Copyright (c) International Business Machines Corp., 2001
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 /*
21 * NAME
22 * ioctl02.c
23 *
24 * DESCRIPTION
25 * Testcase to test the TCGETA, and TCSETA ioctl implementations for
26 * the tty driver
27 *
28 * ALGORITHM
29 * In this test, the parent and child open the parentty and the childtty
30 * respectively. After opening the childtty the child flushes the stream
31 * and sends a SIGUSR1 to the parent (thereby asking it to continue its
32 * testing). The parent, which was waiting for this signal to arrive, now
33 * starts the testing. It issues a TCGETA ioctl to get all the tty
34 * parameters. It then changes them to known values by issuing a TCSETA
35 * ioctl. Then the parent issues a TCGETA ioctl again and compares the
36 * received values with what it had set earlier. The test fails if TCGETA
37 * or TCSETA fails, or if the received values don't match those that were
38 * set. The parent does all the testing, the requirement of the child
39 * process is to moniter the testing done by the parent, and hence the
40 * child just waits for the parent.
41 *
42 * USAGE: <for command-line>
43 * ioctl02 -D /dev/tty[0-9] [-c n] [-f] [-i n] [-I x] [-P x] [-t]
44 * where, -c n : Run n copies concurrently.
45 * -f : Turn off functionality Testing.
46 * -i n : Execute test n times.
47 * -I x : Execute test for x seconds.
48 * -P x : Pause for x seconds between iterations.
49 * -t : Turn on syscall timing.
50 *
51 * HISTORY
52 * 07/2001 Ported by Wayne Boyer
53 *
54 * RESTRICTIONS
55 * test must be run with the -D option
56 * test may have to be run as root depending on the tty permissions
57 */
58
59 #include <stdio.h>
60 #include <termio.h>
61 #include <fcntl.h>
62 #include <signal.h>
63 #include <errno.h>
64 #include <sys/wait.h>
65 #include <sys/types.h>
66 #include <sys/stat.h>
67 #include <sys/ioctl.h>
68 #include <sys/termios.h>
69 #include "test.h"
70 #include "safe_macros.h"
71
72 #define CNUL 0
73
74 char *TCID = "ioctl02";
75 int TST_TOTAL = 1;
76
77 static struct termio termio, save_io;
78
79 static char *parenttty, *childtty;
80 static int parentfd, childfd;
81 static int parentpid, childpid;
82 static volatile int sigterm, sigusr1, sigusr2;
83 static int closed = 1;
84
85 static int do_child_setup(void);
86 static int do_parent_setup(void);
87 static int run_ptest(void);
88 static int run_ctest(void);
89 static int chk_tty_parms();
90 static void setup(void);
91 static void cleanup(void);
92 static void help(void);
93 static void do_child(void);
94 void do_child_uclinux(void);
95 static void sigterm_handler(void);
96
97 static int Devflag;
98 static char *devname;
99
100 static option_t options[] = {
101 {"D:", &Devflag, &devname},
102 {NULL, NULL, NULL}
103 };
104
main(int ac,char ** av)105 int main(int ac, char **av)
106 {
107 int lc;
108 int rval;
109
110 tst_parse_opts(ac, av, options, &help);
111
112 #ifdef UCLINUX
113 maybe_run_child(&do_child_uclinux, "dS", &parentpid, &childtty);
114 #endif
115
116 if (!Devflag)
117 tst_brkm(TBROK, NULL, "You must specify a tty device with "
118 "the -D option.");
119
120 tst_require_root();
121
122 setup();
123
124 for (lc = 0; TEST_LOOPING(lc); lc++) {
125
126 tst_count = 0;
127
128 parenttty = devname;
129 childtty = devname;
130
131 parentpid = getpid();
132
133 childpid = FORK_OR_VFORK();
134 if (childpid < 0)
135 tst_brkm(TBROK, cleanup, "fork failed");
136
137 if (childpid == 0) { /* child */
138 #ifdef UCLINUX
139 if (self_exec(av[0], "dS", parentpid, childtty) < 0)
140 tst_brkm(TBROK, cleanup, "self_exec failed");
141 #else
142 do_child();
143 #endif
144 }
145
146 while (!sigusr1)
147 sleep(1);
148
149 sigusr1 = 0;
150
151 parentfd = do_parent_setup();
152 if (parentfd < 0) {
153 kill(childpid, SIGTERM);
154 waitpid(childpid, NULL, 0);
155 cleanup();
156 }
157
158 /* run the parent test */
159 rval = run_ptest();
160 if (rval == -1) {
161 /*
162 * Parent cannot set/get ioctl parameters.
163 * SIGTERM the child and cleanup.
164 */
165 kill(childpid, SIGTERM);
166 waitpid(childpid, NULL, 0);
167 cleanup();
168 }
169
170 if (rval != 0)
171 tst_resm(TFAIL, "TCGETA/TCSETA tests FAILED with "
172 "%d %s", rval, rval > 1 ? "errors" : "error");
173 else
174 tst_resm(TPASS, "TCGETA/TCSETA tests SUCCEEDED");
175
176 /* FIXME: check return codes. */
177 (void)kill(childpid, SIGTERM);
178 (void)waitpid(childpid, NULL, 0);
179
180 /*
181 * Clean up things from the parent by restoring the
182 * tty device information that was saved in setup()
183 * and closing the tty file descriptor.
184 */
185 if (ioctl(parentfd, TCSETA, &save_io) == -1)
186 tst_resm(TINFO, "ioctl restore failed in main");
187 SAFE_CLOSE(cleanup, parentfd);
188
189 closed = 1;
190 }
191 cleanup();
192
193 tst_exit();
194 }
195
do_child(void)196 static void do_child(void)
197 {
198 childfd = do_child_setup();
199 if (childfd < 0)
200 _exit(1);
201 run_ctest();
202 _exit(0);
203 }
204
do_child_uclinux(void)205 void do_child_uclinux(void)
206 {
207 struct sigaction act;
208
209 /* Set up the signal handlers again */
210 act.sa_handler = (void *)sigterm_handler;
211 act.sa_flags = 0;
212 sigemptyset(&act.sa_mask);
213 (void)sigaction(SIGTERM, &act, 0);
214
215 /* Run the normal child */
216 do_child();
217 }
218
219 /*
220 * run_ptest() - setup the various termio structure values and issue
221 * the TCSETA ioctl call with the TEST macro.
222 */
run_ptest(void)223 static int run_ptest(void)
224 {
225 int i, rval;
226
227 /* Use "old" line discipline */
228 termio.c_line = 0;
229
230 /* Set control modes */
231 termio.c_cflag = B50 | CS7 | CREAD | PARENB | PARODD | CLOCAL;
232
233 /* Set control chars. */
234 for (i = 0; i < NCC; i++) {
235 if (i == VEOL2)
236 continue;
237 termio.c_cc[i] = CSTART;
238 }
239
240 /* Set local modes. */
241 termio.c_lflag =
242 ((unsigned short)(ISIG | ICANON | XCASE | ECHO | ECHOE | NOFLSH));
243
244 /* Set input modes. */
245 termio.c_iflag =
246 BRKINT | IGNPAR | INPCK | ISTRIP | ICRNL | IUCLC | IXON | IXANY |
247 IXOFF;
248
249 /* Set output modes. */
250 termio.c_oflag = OPOST | OLCUC | ONLCR | ONOCR;
251
252 TEST(ioctl(parentfd, TCSETA, &termio));
253
254 if (TEST_RETURN < 0) {
255 tst_resm(TFAIL, "ioctl TCSETA failed : "
256 "errno = %d", TEST_ERRNO);
257 return -1;
258 }
259
260 /* Get termio and see if all parameters actually got set */
261 rval = ioctl(parentfd, TCGETA, &termio);
262 if (rval < 0) {
263 tst_resm(TFAIL, "ioctl TCGETA failed. Ending test.");
264 return -1;
265 }
266
267 return chk_tty_parms();
268 }
269
run_ctest(void)270 static int run_ctest(void)
271 {
272 /*
273 * Wait till the parent has finished testing.
274 */
275 while (!sigterm)
276 sleep(1);
277
278 sigterm = 0;
279
280 tst_resm(TINFO, "child: Got SIGTERM from parent.");
281
282 if (close(childfd) == -1)
283 tst_resm(TINFO, "close() in run_ctest() failed");
284 return 0;
285 }
286
chk_tty_parms(void)287 static int chk_tty_parms(void)
288 {
289 int i, flag = 0;
290
291 if (termio.c_line != 0) {
292 tst_resm(TINFO, "line discipline has incorrect value %o",
293 termio.c_line);
294 flag++;
295 }
296 /*
297 * The following Code Sniffet is disabled to check the value of c_cflag
298 * as it seems that due to some changes from 2.6.24 onwards, this
299 * setting is not done properly for either of (B50|CS7|CREAD|PARENB|
300 * PARODD|CLOCAL|(CREAD|HUPCL|CLOCAL).
301 * However, it has been observed that other flags are properly set.
302 */
303 #if 0
304 if (termio.c_cflag != (B50 | CS7 | CREAD | PARENB | PARODD | CLOCAL)) {
305 tst_resm(TINFO, "cflag has incorrect value. %o",
306 termio.c_cflag);
307 flag++;
308 }
309 #endif
310
311 for (i = 0; i < NCC; i++) {
312 if (i == VEOL2) {
313 if (termio.c_cc[VEOL2] == CNUL) {
314 continue;
315 } else {
316 tst_resm(TINFO, "control char %d has "
317 "incorrect value %d %d", i,
318 termio.c_cc[i], CNUL);
319 flag++;
320 continue;
321 }
322 }
323
324 if (termio.c_cc[i] != CSTART) {
325 tst_resm(TINFO, "control char %d has incorrect "
326 "value %d.", i, termio.c_cc[i]);
327 flag++;
328 }
329 }
330
331 if (!
332 (termio.c_lflag
333 && (ISIG | ICANON | XCASE | ECHO | ECHOE | NOFLSH))) {
334 tst_resm(TINFO, "lflag has incorrect value. %o",
335 termio.c_lflag);
336 flag++;
337 }
338
339 if (!
340 (termio.c_iflag
341 && (BRKINT | IGNPAR | INPCK | ISTRIP | ICRNL | IUCLC | IXON | IXANY
342 | IXOFF))) {
343 tst_resm(TINFO, "iflag has incorrect value. %o",
344 termio.c_iflag);
345 flag++;
346 }
347
348 if (!(termio.c_oflag && (OPOST | OLCUC | ONLCR | ONOCR))) {
349 tst_resm(TINFO, "oflag has incorrect value. %o",
350 termio.c_oflag);
351 flag++;
352 }
353
354 if (!flag)
355 tst_resm(TINFO, "termio values are set as expected");
356
357 return flag;
358 }
359
do_parent_setup(void)360 static int do_parent_setup(void)
361 {
362 int pfd;
363
364 pfd = SAFE_OPEN(cleanup, parenttty, O_RDWR, 0777);
365
366 /* unset the closed flag */
367 closed = 0;
368
369 /* flush tty queues to remove old output */
370 SAFE_IOCTL(cleanup, pfd, TCFLSH, 2);
371 return pfd;
372 }
373
do_child_setup(void)374 static int do_child_setup(void)
375 {
376 int cfd;
377
378 cfd = open(childtty, O_RDWR, 0777);
379 if (cfd < 0) {
380 tst_resm(TINFO, "Could not open %s in do_child_setup(), errno "
381 "= %d", childtty, errno);
382 /* signal the parent so we don't hang the test */
383 kill(parentpid, SIGUSR1);
384 return -1;
385 }
386
387 /* flush tty queues to remove old output */
388 if (ioctl(cfd, TCFLSH, 2) < 0) {
389 tst_resm(TINFO, "ioctl TCFLSH failed. : errno = %d", errno);
390 /* signal the parent so we don't hang the test */
391 kill(parentpid, SIGUSR1);
392 return -1;
393 }
394
395 /* tell the parent that we're done */
396 kill(parentpid, SIGUSR1);
397
398 return cfd;
399 }
400
401 /*
402 * Define the signals handlers here.
403 */
sigterm_handler(void)404 static void sigterm_handler(void)
405 {
406 sigterm = 1;
407 }
408
sigusr1_handler(void)409 static void sigusr1_handler(void)
410 {
411 sigusr1 = 1;
412 }
413
sigusr2_handler(void)414 static void sigusr2_handler(void)
415 {
416 sigusr2 = 1;
417 }
418
help(void)419 static void help(void)
420 {
421 printf(" -D <tty device> : for example, /dev/tty[0-9]\n");
422 }
423
setup(void)424 static void setup(void)
425 {
426 int fd;
427 struct sigaction act;
428
429 /* XXX: TERRNO required all over the place */
430 fd = SAFE_OPEN(NULL, devname, O_RDWR, 0777);
431
432 /* Save the current device information - to be restored in cleanup() */
433 SAFE_IOCTL(cleanup, fd, TCGETA, &save_io);
434
435 /* Close the device */
436 SAFE_CLOSE(cleanup, fd);
437
438 /* Set up the signal handlers */
439 act.sa_handler = (void *)sigterm_handler;
440 act.sa_flags = 0;
441 sigemptyset(&act.sa_mask);
442 (void)sigaction(SIGTERM, &act, 0);
443
444 act.sa_handler = (void *)sigusr1_handler;
445 act.sa_flags = 0;
446 (void)sigaction(SIGUSR1, &act, 0);
447
448 act.sa_handler = (void *)sigusr2_handler;
449 act.sa_flags = 0;
450 (void)sigaction(SIGUSR2, &act, 0);
451
452 act.sa_handler = SIG_IGN;
453 act.sa_flags = 0;
454 (void)sigaction(SIGTTOU, &act, 0);
455
456 sigterm = sigusr1 = sigusr2 = 0;
457
458 TEST_PAUSE;
459 }
460
cleanup(void)461 static void cleanup(void)
462 {
463 if (!closed) {
464 if (ioctl(parentfd, TCSETA, &save_io) == -1)
465 tst_resm(TINFO, "ioctl restore failed in cleanup()");
466 if (close(parentfd) == -1)
467 tst_resm(TINFO, "close() failed in cleanup()");
468 }
469 }
470