1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) International Business Machines Corp., 2001
4*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) 2020 Petr Vorel <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) 2023 Marius Kittler <[email protected]>
6*49cdfc7eSAndroid Build Coastguard Worker */
7*49cdfc7eSAndroid Build Coastguard Worker
8*49cdfc7eSAndroid Build Coastguard Worker /*\
9*49cdfc7eSAndroid Build Coastguard Worker * [Description]
10*49cdfc7eSAndroid Build Coastguard Worker *
11*49cdfc7eSAndroid Build Coastguard Worker * Test TCGETA/TCGETS and TCSETA/TCSETS ioctl implementations for tty driver.
12*49cdfc7eSAndroid Build Coastguard Worker *
13*49cdfc7eSAndroid Build Coastguard Worker * In this test, the parent and child open the parentty and the childtty
14*49cdfc7eSAndroid Build Coastguard Worker * respectively. After opening the childtty the child flushes the stream
15*49cdfc7eSAndroid Build Coastguard Worker * and wakes the parent (thereby asking it to continue its testing). The
16*49cdfc7eSAndroid Build Coastguard Worker * parent, then starts the testing. It issues a TCGETA/TCGETS ioctl to
17*49cdfc7eSAndroid Build Coastguard Worker * get all the tty parameters. It then changes them to known values by
18*49cdfc7eSAndroid Build Coastguard Worker * issuing a TCSETA/TCSETS ioctl. Then the parent issues a TCSETA/TCGETS
19*49cdfc7eSAndroid Build Coastguard Worker * ioctl again and compares the received values with what it had set
20*49cdfc7eSAndroid Build Coastguard Worker * earlier. The test fails if TCGETA/TCGETS or TCSETA/TCSETS fails, or if
21*49cdfc7eSAndroid Build Coastguard Worker * the received values don't match those that were set. The parent does
22*49cdfc7eSAndroid Build Coastguard Worker * all the testing, the requirement of the child process is to moniter
23*49cdfc7eSAndroid Build Coastguard Worker * the testing done by the parent, and hence the child just waits for the
24*49cdfc7eSAndroid Build Coastguard Worker * parent.
25*49cdfc7eSAndroid Build Coastguard Worker */
26*49cdfc7eSAndroid Build Coastguard Worker
27*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
28*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
29*49cdfc7eSAndroid Build Coastguard Worker #include <asm/termbits.h>
30*49cdfc7eSAndroid Build Coastguard Worker
31*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/ioctl.h"
32*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
33*49cdfc7eSAndroid Build Coastguard Worker
34*49cdfc7eSAndroid Build Coastguard Worker static struct termio termio, termio_exp;
35*49cdfc7eSAndroid Build Coastguard Worker static struct termios termios, termios_exp, termios_bak;
36*49cdfc7eSAndroid Build Coastguard Worker
37*49cdfc7eSAndroid Build Coastguard Worker static char *parenttty, *childtty;
38*49cdfc7eSAndroid Build Coastguard Worker static int parentfd = -1;
39*49cdfc7eSAndroid Build Coastguard Worker static int parentpid, childpid;
40*49cdfc7eSAndroid Build Coastguard Worker
41*49cdfc7eSAndroid Build Coastguard Worker static void do_child(void);
42*49cdfc7eSAndroid Build Coastguard Worker static void prepare_termio(void);
43*49cdfc7eSAndroid Build Coastguard Worker static void run_ptest(void);
44*49cdfc7eSAndroid Build Coastguard Worker static void chk_tty_parms_termio(void);
45*49cdfc7eSAndroid Build Coastguard Worker static void chk_tty_parms_termios(void);
46*49cdfc7eSAndroid Build Coastguard Worker static void setup(void);
47*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void);
48*49cdfc7eSAndroid Build Coastguard Worker
49*49cdfc7eSAndroid Build Coastguard Worker static char *device;
50*49cdfc7eSAndroid Build Coastguard Worker
51*49cdfc7eSAndroid Build Coastguard Worker static struct variant {
52*49cdfc7eSAndroid Build Coastguard Worker const char *name;
53*49cdfc7eSAndroid Build Coastguard Worker void *termio, *termio_exp, *termio_bak;
54*49cdfc7eSAndroid Build Coastguard Worker size_t termio_size;
55*49cdfc7eSAndroid Build Coastguard Worker void (*check)(void);
56*49cdfc7eSAndroid Build Coastguard Worker int tcget, tcset;
57*49cdfc7eSAndroid Build Coastguard Worker } variants[] = {
58*49cdfc7eSAndroid Build Coastguard Worker {
59*49cdfc7eSAndroid Build Coastguard Worker .name = "termio",
60*49cdfc7eSAndroid Build Coastguard Worker .termio = &termio,
61*49cdfc7eSAndroid Build Coastguard Worker .termio_exp = &termio_exp,
62*49cdfc7eSAndroid Build Coastguard Worker .termio_size = sizeof(termio),
63*49cdfc7eSAndroid Build Coastguard Worker .check = &chk_tty_parms_termio,
64*49cdfc7eSAndroid Build Coastguard Worker .tcget = TCGETA,
65*49cdfc7eSAndroid Build Coastguard Worker .tcset = TCSETA,
66*49cdfc7eSAndroid Build Coastguard Worker },
67*49cdfc7eSAndroid Build Coastguard Worker {
68*49cdfc7eSAndroid Build Coastguard Worker .name = "termios",
69*49cdfc7eSAndroid Build Coastguard Worker .termio = &termios,
70*49cdfc7eSAndroid Build Coastguard Worker .termio_exp = &termios_exp,
71*49cdfc7eSAndroid Build Coastguard Worker .termio_size = sizeof(termios),
72*49cdfc7eSAndroid Build Coastguard Worker .check = &chk_tty_parms_termios,
73*49cdfc7eSAndroid Build Coastguard Worker .tcget = TCGETS,
74*49cdfc7eSAndroid Build Coastguard Worker .tcset = TCSETS,
75*49cdfc7eSAndroid Build Coastguard Worker },
76*49cdfc7eSAndroid Build Coastguard Worker };
77*49cdfc7eSAndroid Build Coastguard Worker
verify_ioctl(void)78*49cdfc7eSAndroid Build Coastguard Worker static void verify_ioctl(void)
79*49cdfc7eSAndroid Build Coastguard Worker {
80*49cdfc7eSAndroid Build Coastguard Worker tst_res(TINFO, "Testing %s variant", variants[tst_variant].name);
81*49cdfc7eSAndroid Build Coastguard Worker
82*49cdfc7eSAndroid Build Coastguard Worker parenttty = device;
83*49cdfc7eSAndroid Build Coastguard Worker childtty = device;
84*49cdfc7eSAndroid Build Coastguard Worker
85*49cdfc7eSAndroid Build Coastguard Worker parentpid = getpid();
86*49cdfc7eSAndroid Build Coastguard Worker childpid = SAFE_FORK();
87*49cdfc7eSAndroid Build Coastguard Worker if (!childpid) {
88*49cdfc7eSAndroid Build Coastguard Worker do_child();
89*49cdfc7eSAndroid Build Coastguard Worker exit(EXIT_SUCCESS);
90*49cdfc7eSAndroid Build Coastguard Worker }
91*49cdfc7eSAndroid Build Coastguard Worker
92*49cdfc7eSAndroid Build Coastguard Worker TST_CHECKPOINT_WAIT(0);
93*49cdfc7eSAndroid Build Coastguard Worker
94*49cdfc7eSAndroid Build Coastguard Worker parentfd = SAFE_OPEN(parenttty, O_RDWR, 0777);
95*49cdfc7eSAndroid Build Coastguard Worker SAFE_IOCTL(parentfd, TCFLSH, TCIOFLUSH);
96*49cdfc7eSAndroid Build Coastguard Worker
97*49cdfc7eSAndroid Build Coastguard Worker run_ptest();
98*49cdfc7eSAndroid Build Coastguard Worker
99*49cdfc7eSAndroid Build Coastguard Worker TST_CHECKPOINT_WAKE(0);
100*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(parentfd);
101*49cdfc7eSAndroid Build Coastguard Worker }
102*49cdfc7eSAndroid Build Coastguard Worker
prepare_termio(void)103*49cdfc7eSAndroid Build Coastguard Worker static void prepare_termio(void)
104*49cdfc7eSAndroid Build Coastguard Worker {
105*49cdfc7eSAndroid Build Coastguard Worker /* Use "old" line discipline */
106*49cdfc7eSAndroid Build Coastguard Worker termios_exp.c_line = termio_exp.c_line = 0;
107*49cdfc7eSAndroid Build Coastguard Worker
108*49cdfc7eSAndroid Build Coastguard Worker /* Set control modes */
109*49cdfc7eSAndroid Build Coastguard Worker termios_exp.c_cflag = termio_exp.c_cflag = B50 | CS7 | CREAD | PARENB | PARODD | CLOCAL;
110*49cdfc7eSAndroid Build Coastguard Worker
111*49cdfc7eSAndroid Build Coastguard Worker /* Set control chars. */
112*49cdfc7eSAndroid Build Coastguard Worker for (int i = 0; i < NCC; i++)
113*49cdfc7eSAndroid Build Coastguard Worker termio_exp.c_cc[i] = CSTART;
114*49cdfc7eSAndroid Build Coastguard Worker for (int i = 0; i < VEOL2; i++)
115*49cdfc7eSAndroid Build Coastguard Worker termios_exp.c_cc[i] = CSTART;
116*49cdfc7eSAndroid Build Coastguard Worker
117*49cdfc7eSAndroid Build Coastguard Worker /* Set local modes. */
118*49cdfc7eSAndroid Build Coastguard Worker termios_exp.c_lflag = termio_exp.c_lflag =
119*49cdfc7eSAndroid Build Coastguard Worker ((unsigned short)(ISIG | ICANON | XCASE | ECHO | ECHOE | NOFLSH));
120*49cdfc7eSAndroid Build Coastguard Worker
121*49cdfc7eSAndroid Build Coastguard Worker /* Set input modes. */
122*49cdfc7eSAndroid Build Coastguard Worker termios_exp.c_iflag = termio_exp.c_iflag =
123*49cdfc7eSAndroid Build Coastguard Worker BRKINT | IGNPAR | INPCK | ISTRIP | ICRNL | IUCLC | IXON | IXANY |
124*49cdfc7eSAndroid Build Coastguard Worker IXOFF;
125*49cdfc7eSAndroid Build Coastguard Worker
126*49cdfc7eSAndroid Build Coastguard Worker /* Set output modes. */
127*49cdfc7eSAndroid Build Coastguard Worker termios_exp.c_oflag = termio_exp.c_oflag = OPOST | OLCUC | ONLCR | ONOCR;
128*49cdfc7eSAndroid Build Coastguard Worker }
129*49cdfc7eSAndroid Build Coastguard Worker
130*49cdfc7eSAndroid Build Coastguard Worker /*
131*49cdfc7eSAndroid Build Coastguard Worker * run_ptest() - setup the various termio/termios structure values and issue
132*49cdfc7eSAndroid Build Coastguard Worker * the TCSETA/TCSETS ioctl call with the TEST macro.
133*49cdfc7eSAndroid Build Coastguard Worker */
run_ptest(void)134*49cdfc7eSAndroid Build Coastguard Worker static void run_ptest(void)
135*49cdfc7eSAndroid Build Coastguard Worker {
136*49cdfc7eSAndroid Build Coastguard Worker struct variant *v = &variants[tst_variant];
137*49cdfc7eSAndroid Build Coastguard Worker
138*49cdfc7eSAndroid Build Coastguard Worker /* Init termio/termios structures used to check if all params got set */
139*49cdfc7eSAndroid Build Coastguard Worker memset(v->termio, 0, v->termio_size);
140*49cdfc7eSAndroid Build Coastguard Worker
141*49cdfc7eSAndroid Build Coastguard Worker SAFE_IOCTL(parentfd, v->tcset, v->termio_exp);
142*49cdfc7eSAndroid Build Coastguard Worker
143*49cdfc7eSAndroid Build Coastguard Worker /* Get termio and see if all parameters actually got set */
144*49cdfc7eSAndroid Build Coastguard Worker SAFE_IOCTL(parentfd, v->tcget, v->termio);
145*49cdfc7eSAndroid Build Coastguard Worker v->check();
146*49cdfc7eSAndroid Build Coastguard Worker }
147*49cdfc7eSAndroid Build Coastguard Worker
cmp_attr(unsigned long long exp,unsigned long long act,const char * attr)148*49cdfc7eSAndroid Build Coastguard Worker static int cmp_attr(unsigned long long exp, unsigned long long act, const char *attr)
149*49cdfc7eSAndroid Build Coastguard Worker {
150*49cdfc7eSAndroid Build Coastguard Worker if (act == exp)
151*49cdfc7eSAndroid Build Coastguard Worker return 0;
152*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "%s has incorrect value %llu, expected %llu",
153*49cdfc7eSAndroid Build Coastguard Worker attr, act, exp);
154*49cdfc7eSAndroid Build Coastguard Worker return 1;
155*49cdfc7eSAndroid Build Coastguard Worker }
156*49cdfc7eSAndroid Build Coastguard Worker
cmp_c_cc(unsigned char * exp_c_cc,unsigned char * act_c_cc,int ncc)157*49cdfc7eSAndroid Build Coastguard Worker static int cmp_c_cc(unsigned char *exp_c_cc, unsigned char *act_c_cc, int ncc)
158*49cdfc7eSAndroid Build Coastguard Worker {
159*49cdfc7eSAndroid Build Coastguard Worker int i, fails = 0;
160*49cdfc7eSAndroid Build Coastguard Worker char what[32];
161*49cdfc7eSAndroid Build Coastguard Worker
162*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < ncc; ++i) {
163*49cdfc7eSAndroid Build Coastguard Worker sprintf(what, "control char %d", i);
164*49cdfc7eSAndroid Build Coastguard Worker fails += cmp_attr(exp_c_cc[i], act_c_cc[i], what);
165*49cdfc7eSAndroid Build Coastguard Worker }
166*49cdfc7eSAndroid Build Coastguard Worker return fails;
167*49cdfc7eSAndroid Build Coastguard Worker }
168*49cdfc7eSAndroid Build Coastguard Worker
169*49cdfc7eSAndroid Build Coastguard Worker #define CMP_ATTR(term_exp, term, attr, flag) \
170*49cdfc7eSAndroid Build Coastguard Worker ({ \
171*49cdfc7eSAndroid Build Coastguard Worker flag += cmp_attr((term_exp).attr, (term).attr, #attr); \
172*49cdfc7eSAndroid Build Coastguard Worker flag; \
173*49cdfc7eSAndroid Build Coastguard Worker })
174*49cdfc7eSAndroid Build Coastguard Worker
175*49cdfc7eSAndroid Build Coastguard Worker #define CMP_C_CC(term_exp, term, flag) \
176*49cdfc7eSAndroid Build Coastguard Worker ({ \
177*49cdfc7eSAndroid Build Coastguard Worker flag += cmp_c_cc(term_exp.c_cc, term.c_cc, sizeof(term.c_cc)); \
178*49cdfc7eSAndroid Build Coastguard Worker flag; \
179*49cdfc7eSAndroid Build Coastguard Worker })
180*49cdfc7eSAndroid Build Coastguard Worker
chk_tty_parms_termio(void)181*49cdfc7eSAndroid Build Coastguard Worker static void chk_tty_parms_termio(void)
182*49cdfc7eSAndroid Build Coastguard Worker {
183*49cdfc7eSAndroid Build Coastguard Worker int flag = 0;
184*49cdfc7eSAndroid Build Coastguard Worker
185*49cdfc7eSAndroid Build Coastguard Worker flag = CMP_ATTR(termio_exp, termio, c_line, flag);
186*49cdfc7eSAndroid Build Coastguard Worker flag = CMP_C_CC(termio_exp, termio, flag);
187*49cdfc7eSAndroid Build Coastguard Worker flag = CMP_ATTR(termio_exp, termio, c_lflag, flag);
188*49cdfc7eSAndroid Build Coastguard Worker flag = CMP_ATTR(termio_exp, termio, c_iflag, flag);
189*49cdfc7eSAndroid Build Coastguard Worker flag = CMP_ATTR(termio_exp, termio, c_oflag, flag);
190*49cdfc7eSAndroid Build Coastguard Worker
191*49cdfc7eSAndroid Build Coastguard Worker if (!flag)
192*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "TCGETA/TCSETA tests");
193*49cdfc7eSAndroid Build Coastguard Worker }
194*49cdfc7eSAndroid Build Coastguard Worker
chk_tty_parms_termios(void)195*49cdfc7eSAndroid Build Coastguard Worker static void chk_tty_parms_termios(void)
196*49cdfc7eSAndroid Build Coastguard Worker {
197*49cdfc7eSAndroid Build Coastguard Worker int flag = 0;
198*49cdfc7eSAndroid Build Coastguard Worker
199*49cdfc7eSAndroid Build Coastguard Worker flag = CMP_ATTR(termios_exp, termios, c_line, flag);
200*49cdfc7eSAndroid Build Coastguard Worker flag = CMP_C_CC(termios_exp, termios, flag);
201*49cdfc7eSAndroid Build Coastguard Worker flag = CMP_ATTR(termios_exp, termios, c_lflag, flag);
202*49cdfc7eSAndroid Build Coastguard Worker flag = CMP_ATTR(termios_exp, termios, c_iflag, flag);
203*49cdfc7eSAndroid Build Coastguard Worker flag = CMP_ATTR(termios_exp, termios, c_oflag, flag);
204*49cdfc7eSAndroid Build Coastguard Worker
205*49cdfc7eSAndroid Build Coastguard Worker if (!flag)
206*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "TCGETS/TCSETS tests");
207*49cdfc7eSAndroid Build Coastguard Worker }
208*49cdfc7eSAndroid Build Coastguard Worker
do_child(void)209*49cdfc7eSAndroid Build Coastguard Worker static void do_child(void)
210*49cdfc7eSAndroid Build Coastguard Worker {
211*49cdfc7eSAndroid Build Coastguard Worker int cfd = SAFE_OPEN(childtty, O_RDWR, 0777);
212*49cdfc7eSAndroid Build Coastguard Worker
213*49cdfc7eSAndroid Build Coastguard Worker SAFE_IOCTL(cfd, TCFLSH, TCIOFLUSH);
214*49cdfc7eSAndroid Build Coastguard Worker
215*49cdfc7eSAndroid Build Coastguard Worker /* tell the parent that we're done */
216*49cdfc7eSAndroid Build Coastguard Worker TST_CHECKPOINT_WAKE(0);
217*49cdfc7eSAndroid Build Coastguard Worker
218*49cdfc7eSAndroid Build Coastguard Worker TST_CHECKPOINT_WAIT(0);
219*49cdfc7eSAndroid Build Coastguard Worker tst_res(TINFO, "child: parent has finished testing");
220*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(cfd);
221*49cdfc7eSAndroid Build Coastguard Worker }
222*49cdfc7eSAndroid Build Coastguard Worker
setup(void)223*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
224*49cdfc7eSAndroid Build Coastguard Worker {
225*49cdfc7eSAndroid Build Coastguard Worker if (!device)
226*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TBROK, "You must specify a tty device with -d option");
227*49cdfc7eSAndroid Build Coastguard Worker
228*49cdfc7eSAndroid Build Coastguard Worker int fd = SAFE_OPEN(device, O_RDWR, 0777);
229*49cdfc7eSAndroid Build Coastguard Worker
230*49cdfc7eSAndroid Build Coastguard Worker SAFE_IOCTL(fd, TCGETS, &termios_bak);
231*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd);
232*49cdfc7eSAndroid Build Coastguard Worker
233*49cdfc7eSAndroid Build Coastguard Worker prepare_termio();
234*49cdfc7eSAndroid Build Coastguard Worker }
235*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)236*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
237*49cdfc7eSAndroid Build Coastguard Worker {
238*49cdfc7eSAndroid Build Coastguard Worker if (parentfd >= 0) {
239*49cdfc7eSAndroid Build Coastguard Worker SAFE_IOCTL(parentfd, TCSETS, &termios_bak);
240*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(parentfd);
241*49cdfc7eSAndroid Build Coastguard Worker }
242*49cdfc7eSAndroid Build Coastguard Worker }
243*49cdfc7eSAndroid Build Coastguard Worker
244*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
245*49cdfc7eSAndroid Build Coastguard Worker .needs_root = 1,
246*49cdfc7eSAndroid Build Coastguard Worker .needs_checkpoints = 1,
247*49cdfc7eSAndroid Build Coastguard Worker .forks_child = 1,
248*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
249*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup,
250*49cdfc7eSAndroid Build Coastguard Worker .test_all = verify_ioctl,
251*49cdfc7eSAndroid Build Coastguard Worker .test_variants = 2,
252*49cdfc7eSAndroid Build Coastguard Worker .options = (struct tst_option[]) {
253*49cdfc7eSAndroid Build Coastguard Worker {"d:", &device, "Tty device. For example, /dev/tty[0-9]"},
254*49cdfc7eSAndroid Build Coastguard Worker {}
255*49cdfc7eSAndroid Build Coastguard Worker }
256*49cdfc7eSAndroid Build Coastguard Worker };
257