xref: /aosp_15_r20/external/ltp/testcases/kernel/pty/pty02.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
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) 2018 Google, Inc.
4*49cdfc7eSAndroid Build Coastguard Worker  */
5*49cdfc7eSAndroid Build Coastguard Worker 
6*49cdfc7eSAndroid Build Coastguard Worker /*
7*49cdfc7eSAndroid Build Coastguard Worker  * Regression test for commit 966031f340185 ("n_tty: fix EXTPROC vs ICANON
8*49cdfc7eSAndroid Build Coastguard Worker  * interaction with TIOCINQ (aka FIONREAD)").  The test reproduces a hang
9*49cdfc7eSAndroid Build Coastguard Worker  * (infinite loop in the kernel) after a pseudoterminal is put in both canonical
10*49cdfc7eSAndroid Build Coastguard Worker  * (ICANON) and external processing (EXTPROC) mode, some data is written to the
11*49cdfc7eSAndroid Build Coastguard Worker  * master and read from the slave, and the FIONREAD ioctl is called on the
12*49cdfc7eSAndroid Build Coastguard Worker  * slave.  This is simplified from a syzkaller-generated reproducer.
13*49cdfc7eSAndroid Build Coastguard Worker  */
14*49cdfc7eSAndroid Build Coastguard Worker 
15*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE
16*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
17*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
18*49cdfc7eSAndroid Build Coastguard Worker #include <sys/ioctl.h>
19*49cdfc7eSAndroid Build Coastguard Worker #include <termios.h>
20*49cdfc7eSAndroid Build Coastguard Worker 
21*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
22*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/termbits.h"
23*49cdfc7eSAndroid Build Coastguard Worker 
do_test(void)24*49cdfc7eSAndroid Build Coastguard Worker static void do_test(void)
25*49cdfc7eSAndroid Build Coastguard Worker {
26*49cdfc7eSAndroid Build Coastguard Worker 	struct termios io;
27*49cdfc7eSAndroid Build Coastguard Worker 	int ptmx, pts;
28*49cdfc7eSAndroid Build Coastguard Worker 	char c = 'A';
29*49cdfc7eSAndroid Build Coastguard Worker 	int nbytes;
30*49cdfc7eSAndroid Build Coastguard Worker 
31*49cdfc7eSAndroid Build Coastguard Worker 	ptmx = SAFE_OPEN("/dev/ptmx", O_WRONLY);
32*49cdfc7eSAndroid Build Coastguard Worker 
33*49cdfc7eSAndroid Build Coastguard Worker 	if (tcgetattr(ptmx, &io) != 0)
34*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK | TERRNO, "tcgetattr() failed");
35*49cdfc7eSAndroid Build Coastguard Worker 
36*49cdfc7eSAndroid Build Coastguard Worker 	io.c_lflag = EXTPROC | ICANON;
37*49cdfc7eSAndroid Build Coastguard Worker 
38*49cdfc7eSAndroid Build Coastguard Worker 	TEST(tcsetattr(ptmx, TCSANOW, &io));
39*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET == -1) {
40*49cdfc7eSAndroid Build Coastguard Worker 		if (TST_ERR == EINVAL)
41*49cdfc7eSAndroid Build Coastguard Worker 			tst_brk(TCONF, "tcsetattr(, , EXTPROC | ICANON) is not supported");
42*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK | TTERRNO, "tcsetattr() failed");
43*49cdfc7eSAndroid Build Coastguard Worker 	}
44*49cdfc7eSAndroid Build Coastguard Worker 
45*49cdfc7eSAndroid Build Coastguard Worker 	if (unlockpt(ptmx) != 0)
46*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK | TERRNO, "unlockpt() failed");
47*49cdfc7eSAndroid Build Coastguard Worker 
48*49cdfc7eSAndroid Build Coastguard Worker 	pts = SAFE_OPEN(ptsname(ptmx), O_RDONLY);
49*49cdfc7eSAndroid Build Coastguard Worker 	/* write newline to ptmx to avoid read() on pts to block */
50*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_WRITE(SAFE_WRITE_ALL, ptmx, "A\n", 2);
51*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_READ(1, pts, &c, 1);
52*49cdfc7eSAndroid Build Coastguard Worker 
53*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "Calling FIONREAD, this will hang in n_tty_ioctl() if the bug is present...");
54*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_IOCTL(pts, FIONREAD, &nbytes);
55*49cdfc7eSAndroid Build Coastguard Worker 
56*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(ptmx);
57*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(pts);
58*49cdfc7eSAndroid Build Coastguard Worker 
59*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TPASS, "Got to the end without hanging");
60*49cdfc7eSAndroid Build Coastguard Worker }
61*49cdfc7eSAndroid Build Coastguard Worker 
62*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
63*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = do_test,
64*49cdfc7eSAndroid Build Coastguard Worker 	.tags = (const struct tst_tag[]) {
65*49cdfc7eSAndroid Build Coastguard Worker 		{"linux-git", "966031f34018"},
66*49cdfc7eSAndroid Build Coastguard Worker 		{}
67*49cdfc7eSAndroid Build Coastguard Worker 	}
68*49cdfc7eSAndroid Build Coastguard Worker };
69