plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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 |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 60 | #include <termio.h> |
| 61 | #include <fcntl.h> |
| 62 | #include <signal.h> |
| 63 | #include <errno.h> |
robbiew | 0f24d85 | 2003-03-26 21:07:16 +0000 | [diff] [blame] | 64 | #include <wait.h> |
| 65 | #include <sys/types.h> |
| 66 | #include <sys/stat.h> |
| 67 | #include <sys/ioctl.h> |
| 68 | #include <sys/termios.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 69 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 70 | |
| 71 | #define CNUL 0 |
| 72 | |
| 73 | char *TCID = "ioctl02"; |
| 74 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 75 | |
Wanlong Gao | 6633ce9 | 2012-11-05 11:19:57 +0800 | [diff] [blame] | 76 | static struct termio termio, save_io; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 77 | |
Wanlong Gao | 6633ce9 | 2012-11-05 11:19:57 +0800 | [diff] [blame] | 78 | static char *parenttty, *childtty; |
| 79 | static int parentfd, childfd; |
| 80 | static int parentpid, childpid; |
| 81 | static volatile int sigterm, sigusr1, sigusr2; |
| 82 | static int closed = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 83 | |
Wanlong Gao | 6633ce9 | 2012-11-05 11:19:57 +0800 | [diff] [blame] | 84 | static int do_child_setup(void); |
| 85 | static int do_parent_setup(void); |
| 86 | static int run_ptest(void); |
| 87 | static int run_ctest(void); |
| 88 | static int chk_tty_parms(); |
| 89 | static void setup(void); |
| 90 | static void cleanup(void); |
| 91 | static void help(void); |
| 92 | static void do_child(void); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 93 | void do_child_uclinux(void); |
Wanlong Gao | 6633ce9 | 2012-11-05 11:19:57 +0800 | [diff] [blame] | 94 | static void sigterm_handler(void); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 95 | |
Wanlong Gao | 6633ce9 | 2012-11-05 11:19:57 +0800 | [diff] [blame] | 96 | static int Devflag; |
| 97 | static char *devname; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 98 | |
| 99 | /* for test specific parse_opts options - in this case "-D" */ |
Wanlong Gao | 6633ce9 | 2012-11-05 11:19:57 +0800 | [diff] [blame] | 100 | static option_t options[] = { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 101 | {"D:", &Devflag, &devname}, |
| 102 | {NULL, NULL, NULL} |
| 103 | }; |
| 104 | |
robbiew | 0f24d85 | 2003-03-26 21:07:16 +0000 | [diff] [blame] | 105 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 106 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 107 | int lc; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 108 | int rval; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 109 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 110 | |
Wanlong Gao | 6633ce9 | 2012-11-05 11:19:57 +0800 | [diff] [blame] | 111 | msg = parse_opts(ac, av, options, &help); |
| 112 | if (msg != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 113 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 114 | #ifdef UCLINUX |
| 115 | maybe_run_child(&do_child_uclinux, "dS", &parentpid, &childtty); |
| 116 | #endif |
| 117 | |
Garrett Cooper | 2b6b1ee | 2010-12-19 05:07:50 -0800 | [diff] [blame] | 118 | if (!Devflag) |
| 119 | tst_brkm(TBROK, NULL, "You must specify a tty device with " |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 120 | "the -D option."); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 121 | |
Garrett Cooper | ff29dd1 | 2010-12-19 05:05:40 -0800 | [diff] [blame] | 122 | tst_require_root(NULL); |
subrata_modak | ed21684 | 2008-09-24 15:58:12 +0000 | [diff] [blame] | 123 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 124 | setup(); |
| 125 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 126 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 127 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 128 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 129 | |
| 130 | parenttty = devname; |
| 131 | childtty = devname; |
| 132 | |
| 133 | parentpid = getpid(); |
| 134 | |
Wanlong Gao | 6633ce9 | 2012-11-05 11:19:57 +0800 | [diff] [blame] | 135 | childpid = FORK_OR_VFORK(); |
| 136 | if (childpid < 0) |
Garrett Cooper | ff29dd1 | 2010-12-19 05:05:40 -0800 | [diff] [blame] | 137 | tst_brkm(TBROK, cleanup, "fork failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 138 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 139 | if (childpid == 0) { /* child */ |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 140 | #ifdef UCLINUX |
Garrett Cooper | ff29dd1 | 2010-12-19 05:05:40 -0800 | [diff] [blame] | 141 | if (self_exec(av[0], "dS", parentpid, childtty) < 0) |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 142 | tst_brkm(TBROK, cleanup, "self_exec failed"); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 143 | #else |
| 144 | do_child(); |
| 145 | #endif |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Garrett Cooper | ff29dd1 | 2010-12-19 05:05:40 -0800 | [diff] [blame] | 148 | while (!sigusr1) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 149 | sleep(1); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 150 | |
| 151 | sigusr1 = 0; |
| 152 | |
Wanlong Gao | 6633ce9 | 2012-11-05 11:19:57 +0800 | [diff] [blame] | 153 | parentfd = do_parent_setup(); |
| 154 | if (parentfd < 0) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 155 | kill(childpid, SIGTERM); |
| 156 | waitpid(childpid, NULL, 0); |
| 157 | cleanup(); |
Garrett Cooper | ff29dd1 | 2010-12-19 05:05:40 -0800 | [diff] [blame] | 158 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 159 | |
| 160 | /* run the parent test */ |
Wanlong Gao | 6633ce9 | 2012-11-05 11:19:57 +0800 | [diff] [blame] | 161 | rval = run_ptest(); |
| 162 | if (rval == -1) { |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 163 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 164 | * Parent cannot set/get ioctl parameters. |
| 165 | * SIGTERM the child and cleanup. |
| 166 | */ |
| 167 | kill(childpid, SIGTERM); |
| 168 | waitpid(childpid, NULL, 0); |
| 169 | cleanup(); |
| 170 | } |
| 171 | |
Garrett Cooper | ff29dd1 | 2010-12-19 05:05:40 -0800 | [diff] [blame] | 172 | if (rval != 0) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 173 | tst_resm(TFAIL, "TCGETA/TCSETA tests FAILED with " |
| 174 | "%d %s", rval, rval > 1 ? "errors" : "error"); |
Garrett Cooper | ff29dd1 | 2010-12-19 05:05:40 -0800 | [diff] [blame] | 175 | else |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 176 | tst_resm(TPASS, "TCGETA/TCSETA tests SUCCEEDED"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 177 | |
Garrett Cooper | ff29dd1 | 2010-12-19 05:05:40 -0800 | [diff] [blame] | 178 | /* FIXME: check return codes. */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 179 | (void)kill(childpid, SIGTERM); |
| 180 | (void)waitpid(childpid, NULL, 0); |
| 181 | |
| 182 | /* |
| 183 | * Clean up things from the parent by restoring the |
| 184 | * tty device information that was saved in setup() |
| 185 | * and closing the tty file descriptor. |
| 186 | */ |
Garrett Cooper | ff29dd1 | 2010-12-19 05:05:40 -0800 | [diff] [blame] | 187 | if (ioctl(parentfd, TCSETA, &save_io) == -1) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 188 | tst_resm(TINFO, "ioctl restore failed in main"); |
Garrett Cooper | ff29dd1 | 2010-12-19 05:05:40 -0800 | [diff] [blame] | 189 | if (close(parentfd) == -1) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 190 | tst_brkm(TBROK, cleanup, "close() failed in main"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 191 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 192 | closed = 1; |
| 193 | } |
| 194 | cleanup(); |
| 195 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 196 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Wanlong Gao | 6633ce9 | 2012-11-05 11:19:57 +0800 | [diff] [blame] | 199 | static void do_child(void) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 200 | { |
Wanlong Gao | 6633ce9 | 2012-11-05 11:19:57 +0800 | [diff] [blame] | 201 | childfd = do_child_setup(); |
| 202 | if (childfd < 0) |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 203 | _exit(1); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 204 | run_ctest(); |
| 205 | _exit(0); |
| 206 | } |
| 207 | |
Wanlong Gao | 6633ce9 | 2012-11-05 11:19:57 +0800 | [diff] [blame] | 208 | void do_child_uclinux(void) |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 209 | { |
| 210 | struct sigaction act; |
| 211 | |
| 212 | /* Set up the signal handlers again */ |
| 213 | act.sa_handler = (void *)sigterm_handler; |
| 214 | act.sa_flags = 0; |
Jan Stancek | c5532e4 | 2013-05-14 11:59:33 +0200 | [diff] [blame] | 215 | sigemptyset(&act.sa_mask); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 216 | (void)sigaction(SIGTERM, &act, 0); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 217 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 218 | /* Run the normal child */ |
| 219 | do_child(); |
| 220 | } |
| 221 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 222 | /* |
| 223 | * run_ptest() - setup the various termio structure values and issue |
| 224 | * the TCSETA ioctl call with the TEST macro. |
| 225 | */ |
Wanlong Gao | 6633ce9 | 2012-11-05 11:19:57 +0800 | [diff] [blame] | 226 | static int run_ptest(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 227 | { |
| 228 | int i, rval; |
| 229 | |
| 230 | /* Use "old" line discipline */ |
| 231 | termio.c_line = 0; |
| 232 | |
| 233 | /* Set control modes */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 234 | termio.c_cflag = B50 | CS7 | CREAD | PARENB | PARODD | CLOCAL; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 235 | |
| 236 | /* Set control chars. */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 237 | for (i = 0; i < NCC; i++) { |
Wanlong Gao | 6633ce9 | 2012-11-05 11:19:57 +0800 | [diff] [blame] | 238 | if (i == VEOL2) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 239 | continue; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 240 | termio.c_cc[i] = CSTART; |
| 241 | } |
| 242 | |
| 243 | /* Set local modes. */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 244 | termio.c_lflag = |
| 245 | ((unsigned short)(ISIG | ICANON | XCASE | ECHO | ECHOE | NOFLSH)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 246 | |
| 247 | /* Set input modes. */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 248 | termio.c_iflag = |
| 249 | BRKINT | IGNPAR | INPCK | ISTRIP | ICRNL | IUCLC | IXON | IXANY | |
| 250 | IXOFF; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 251 | |
| 252 | /* Set output modes. */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 253 | termio.c_oflag = OPOST | OLCUC | ONLCR | ONOCR; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 254 | |
| 255 | TEST(ioctl(parentfd, TCSETA, &termio)); |
| 256 | |
| 257 | if (TEST_RETURN < 0) { |
| 258 | tst_resm(TFAIL, "ioctl TCSETA failed : " |
| 259 | "errno = %d", TEST_ERRNO); |
Wanlong Gao | 6633ce9 | 2012-11-05 11:19:57 +0800 | [diff] [blame] | 260 | return -1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 263 | /* Get termio and see if all parameters actually got set */ |
| 264 | rval = ioctl(parentfd, TCGETA, &termio); |
| 265 | if (rval < 0) { |
| 266 | tst_resm(TFAIL, "ioctl TCGETA failed. Ending test."); |
| 267 | return -1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 268 | } |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 269 | |
| 270 | return chk_tty_parms(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 271 | } |
| 272 | |
Wanlong Gao | 6633ce9 | 2012-11-05 11:19:57 +0800 | [diff] [blame] | 273 | static int run_ctest(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 274 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 275 | /* |
| 276 | * Wait till the parent has finished testing. |
| 277 | */ |
Wanlong Gao | 6633ce9 | 2012-11-05 11:19:57 +0800 | [diff] [blame] | 278 | while (!sigterm) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 279 | sleep(1); |
Wanlong Gao | 6633ce9 | 2012-11-05 11:19:57 +0800 | [diff] [blame] | 280 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 281 | sigterm = 0; |
| 282 | |
| 283 | tst_resm(TINFO, "child: Got SIGTERM from parent."); |
| 284 | |
Wanlong Gao | 6633ce9 | 2012-11-05 11:19:57 +0800 | [diff] [blame] | 285 | if (close(childfd) == -1) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 286 | tst_resm(TINFO, "close() in run_ctest() failed"); |
subrata_modak | 43337a3 | 2009-02-26 11:43:51 +0000 | [diff] [blame] | 287 | return 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Wanlong Gao | 6633ce9 | 2012-11-05 11:19:57 +0800 | [diff] [blame] | 290 | static int chk_tty_parms(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 291 | { |
| 292 | int i, flag = 0; |
| 293 | |
| 294 | if (termio.c_line != 0) { |
| 295 | tst_resm(TINFO, "line discipline has incorrect value %o", |
| 296 | termio.c_line); |
| 297 | flag++; |
| 298 | } |
Wanlong Gao | 6633ce9 | 2012-11-05 11:19:57 +0800 | [diff] [blame] | 299 | /* |
| 300 | * The following Code Sniffet is disabled to check the value of c_cflag |
| 301 | * as it seems that due to some changes from 2.6.24 onwards, this |
| 302 | * setting is not done properly for either of (B50|CS7|CREAD|PARENB| |
| 303 | * PARODD|CLOCAL|(CREAD|HUPCL|CLOCAL). |
| 304 | * However, it has been observed that other flags are properly set. |
| 305 | */ |
| 306 | #if 0 |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 307 | if (termio.c_cflag != (B50 | CS7 | CREAD | PARENB | PARODD | CLOCAL)) { |
Wanlong Gao | 6633ce9 | 2012-11-05 11:19:57 +0800 | [diff] [blame] | 308 | tst_resm(TINFO, "cflag has incorrect value. %o", |
| 309 | termio.c_cflag); |
| 310 | flag++; |
| 311 | } |
| 312 | #endif |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 313 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 314 | for (i = 0; i < NCC; i++) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 315 | if (i == VEOL2) { |
| 316 | if (termio.c_cc[VEOL2] == CNUL) { |
| 317 | continue; |
| 318 | } else { |
| 319 | tst_resm(TINFO, "control char %d has " |
| 320 | "incorrect value %d %d", i, |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 321 | termio.c_cc[i], CNUL); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 322 | flag++; |
| 323 | continue; |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | if (termio.c_cc[i] != CSTART) { |
| 328 | tst_resm(TINFO, "control char %d has incorrect " |
| 329 | "value %d.", i, termio.c_cc[i]); |
| 330 | flag++; |
| 331 | } |
| 332 | } |
| 333 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 334 | if (! |
| 335 | (termio.c_lflag |
| 336 | && (ISIG | ICANON | XCASE | ECHO | ECHOE | NOFLSH))) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 337 | tst_resm(TINFO, "lflag has incorrect value. %o", |
| 338 | termio.c_lflag); |
| 339 | flag++; |
| 340 | } |
| 341 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 342 | if (! |
| 343 | (termio.c_iflag |
| 344 | && (BRKINT | IGNPAR | INPCK | ISTRIP | ICRNL | IUCLC | IXON | IXANY |
| 345 | | IXOFF))) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 346 | tst_resm(TINFO, "iflag has incorrect value. %o", |
| 347 | termio.c_iflag); |
| 348 | flag++; |
| 349 | } |
| 350 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 351 | if (!(termio.c_oflag && (OPOST | OLCUC | ONLCR | ONOCR))) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 352 | tst_resm(TINFO, "oflag has incorrect value. %o", |
| 353 | termio.c_oflag); |
| 354 | flag++; |
| 355 | } |
| 356 | |
Wanlong Gao | 6633ce9 | 2012-11-05 11:19:57 +0800 | [diff] [blame] | 357 | if (!flag) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 358 | tst_resm(TINFO, "termio values are set as expected"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 359 | |
Wanlong Gao | 6633ce9 | 2012-11-05 11:19:57 +0800 | [diff] [blame] | 360 | return flag; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 361 | } |
| 362 | |
Wanlong Gao | 6633ce9 | 2012-11-05 11:19:57 +0800 | [diff] [blame] | 363 | static int do_parent_setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 364 | { |
| 365 | int pfd; |
| 366 | |
Wanlong Gao | 6633ce9 | 2012-11-05 11:19:57 +0800 | [diff] [blame] | 367 | pfd = open(parenttty, O_RDWR, 0777); |
| 368 | if (pfd < 0) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 369 | tst_brkm(TBROK, cleanup, "Could not open %s in " |
| 370 | "do_parent_setup(), errno = %d", parenttty, errno); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 371 | |
| 372 | /* unset the closed flag */ |
| 373 | closed = 0; |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 374 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 375 | /* flush tty queues to remove old output */ |
Garrett Cooper | ff29dd1 | 2010-12-19 05:05:40 -0800 | [diff] [blame] | 376 | if (ioctl(pfd, TCFLSH, 2) < 0) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 377 | tst_brkm(TBROK, cleanup, "ioctl TCFLSH failed : " |
| 378 | "errno = %d", errno); |
Wanlong Gao | 6633ce9 | 2012-11-05 11:19:57 +0800 | [diff] [blame] | 379 | return pfd; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 380 | } |
| 381 | |
Wanlong Gao | 6633ce9 | 2012-11-05 11:19:57 +0800 | [diff] [blame] | 382 | static int do_child_setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 383 | { |
| 384 | int cfd; |
| 385 | |
Wanlong Gao | 6633ce9 | 2012-11-05 11:19:57 +0800 | [diff] [blame] | 386 | cfd = open(childtty, O_RDWR, 0777); |
| 387 | if (cfd < 0) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 388 | tst_resm(TINFO, "Could not open %s in do_child_setup(), errno " |
| 389 | "= %d", childtty, errno); |
| 390 | /* signal the parent so we don't hang the test */ |
| 391 | kill(parentpid, SIGUSR1); |
Wanlong Gao | 6633ce9 | 2012-11-05 11:19:57 +0800 | [diff] [blame] | 392 | return -1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | /* flush tty queues to remove old output */ |
| 396 | if (ioctl(cfd, TCFLSH, 2) < 0) { |
| 397 | tst_resm(TINFO, "ioctl TCFLSH failed. : errno = %d", errno); |
| 398 | /* signal the parent so we don't hang the test */ |
| 399 | kill(parentpid, SIGUSR1); |
Wanlong Gao | 6633ce9 | 2012-11-05 11:19:57 +0800 | [diff] [blame] | 400 | return -1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | /* tell the parent that we're done */ |
| 404 | kill(parentpid, SIGUSR1); |
| 405 | |
Wanlong Gao | 6633ce9 | 2012-11-05 11:19:57 +0800 | [diff] [blame] | 406 | return cfd; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | /* |
| 410 | * Define the signals handlers here. |
| 411 | */ |
Wanlong Gao | 6633ce9 | 2012-11-05 11:19:57 +0800 | [diff] [blame] | 412 | static void sigterm_handler(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 413 | { |
| 414 | sigterm = 1; |
| 415 | } |
| 416 | |
Wanlong Gao | 6633ce9 | 2012-11-05 11:19:57 +0800 | [diff] [blame] | 417 | static void sigusr1_handler(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 418 | { |
| 419 | sigusr1 = 1; |
| 420 | } |
| 421 | |
Wanlong Gao | 6633ce9 | 2012-11-05 11:19:57 +0800 | [diff] [blame] | 422 | static void sigusr2_handler(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 423 | { |
| 424 | sigusr2 = 1; |
| 425 | } |
| 426 | |
Wanlong Gao | 6633ce9 | 2012-11-05 11:19:57 +0800 | [diff] [blame] | 427 | static void help(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 428 | { |
| 429 | printf(" -D <tty device> : for example, /dev/tty[0-9]\n"); |
| 430 | } |
| 431 | |
Wanlong Gao | 6633ce9 | 2012-11-05 11:19:57 +0800 | [diff] [blame] | 432 | static void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 433 | { |
| 434 | int fd; |
| 435 | struct sigaction act; |
| 436 | |
Garrett Cooper | ff29dd1 | 2010-12-19 05:05:40 -0800 | [diff] [blame] | 437 | /* XXX: TERRNO required all over the place */ |
Wanlong Gao | 6633ce9 | 2012-11-05 11:19:57 +0800 | [diff] [blame] | 438 | fd = open(devname, O_RDWR, 0777); |
| 439 | if (fd < 0) |
Garrett Cooper | ff29dd1 | 2010-12-19 05:05:40 -0800 | [diff] [blame] | 440 | tst_brkm(TBROK, NULL, "Could not open %s in " |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 441 | "setup(), errno = %d", devname, errno); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 442 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 443 | /* Save the current device information - to be restored in cleanup() */ |
Garrett Cooper | ff29dd1 | 2010-12-19 05:05:40 -0800 | [diff] [blame] | 444 | if (ioctl(fd, TCGETA, &save_io) < 0) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 445 | tst_brkm(TBROK, cleanup, "TCGETA ioctl failed in " |
Garrett Cooper | ff29dd1 | 2010-12-19 05:05:40 -0800 | [diff] [blame] | 446 | "do_parent_setup"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 447 | |
| 448 | /* Close the device */ |
Garrett Cooper | ff29dd1 | 2010-12-19 05:05:40 -0800 | [diff] [blame] | 449 | if (close(fd) == -1) |
| 450 | tst_brkm(TBROK, cleanup, "close failed in setup"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 451 | |
| 452 | /* Set up the signal handlers */ |
| 453 | act.sa_handler = (void *)sigterm_handler; |
| 454 | act.sa_flags = 0; |
Jan Stancek | c5532e4 | 2013-05-14 11:59:33 +0200 | [diff] [blame] | 455 | sigemptyset(&act.sa_mask); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 456 | (void)sigaction(SIGTERM, &act, 0); |
| 457 | |
| 458 | act.sa_handler = (void *)sigusr1_handler; |
| 459 | act.sa_flags = 0; |
| 460 | (void)sigaction(SIGUSR1, &act, 0); |
| 461 | |
| 462 | act.sa_handler = (void *)sigusr2_handler; |
| 463 | act.sa_flags = 0; |
| 464 | (void)sigaction(SIGUSR2, &act, 0); |
| 465 | |
plars | d93a8e7 | 2001-11-30 20:38:48 +0000 | [diff] [blame] | 466 | act.sa_handler = SIG_IGN; |
| 467 | act.sa_flags = 0; |
| 468 | (void)sigaction(SIGTTOU, &act, 0); |
| 469 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 470 | sigterm = sigusr1 = sigusr2 = 0; |
| 471 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 472 | TEST_PAUSE; |
| 473 | } |
| 474 | |
Wanlong Gao | 6633ce9 | 2012-11-05 11:19:57 +0800 | [diff] [blame] | 475 | static void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 476 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 477 | if (!closed) { |
Garrett Cooper | ff29dd1 | 2010-12-19 05:05:40 -0800 | [diff] [blame] | 478 | if (ioctl(parentfd, TCSETA, &save_io) == -1) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 479 | tst_resm(TINFO, "ioctl restore failed in cleanup()"); |
Garrett Cooper | ff29dd1 | 2010-12-19 05:05:40 -0800 | [diff] [blame] | 480 | if (close(parentfd) == -1) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 481 | tst_resm(TINFO, "close() failed in cleanup()"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 482 | } |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 483 | } |