Skip to contentSkip to conversation

Improve ioctl02.c

PATCH Superseded Marius Kittler 2 participants 6 messages Sep 7, 2023 – Sep 15, 2023

v3 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

v3 cover Improve ioctl02.c

Improve ioctl02.c

Marius Kittler (4):
Refactor ioctl02.c to use the new test API
Use correct binary operand in ioctl02.c
Make checks for termio flags more strict
Simplify ioctl02.c further

 testcases/kernel/syscalls/ioctl/ioctl02.c | 396 ++++++----------------
 1 file changed, 106 insertions(+), 290 deletions(-)
Signature · 5 lines
2.42.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#1 lore raw

PATCH v3 1/4 Refactor ioctl02.c to use the new test API

This test code itself has been kept unchanged as much as possible (although it probably still has room for improvement).

See https://github.com/linux-test-project/ltp/issues/637 for related discussion.

---
 testcases/kernel/syscalls/ioctl/ioctl02.c | 280 +++++++---------------
 1 file changed, 90 insertions(+), 190 deletions(-)
testcases/kernel/syscalls/ioctl/ioctl02.c +90 -190
index b4d4a3594..77283e510 100644
@@ -1,31 +1,16 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
 /*
- *   Copyright (c) International Business Machines  Corp., 2001
- *   Copyright (c) 2020 Petr Vorel <[email protected]>
- *
- *   This program is free software;  you can redistribute it and/or modify
- *   it under the terms of the GNU General Public License as published by
- *   the Free Software Foundation; either version 2 of the License, or
- *   (at your option) any later version.
- *
- *   This program is distributed in the hope that it will be useful,
- *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
- *   the GNU General Public License for more details.
- *
- *   You should have received a copy of the GNU General Public License
- *   along with this program;  if not, write to the Free Software
- *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ * Copyright (c) International Business Machines  Corp., 2001
+ * Copyright (c) 2020 Petr Vorel <[email protected]>
+ * Copyright (c) 2023 Marius Kittler <[email protected]>
  */
 
-/*
- * NAME
- *	ioctl02.c
+/*\
+ * [Description]
  *
- * DESCRIPTION
  *	Testcase to test the TCGETA, and TCSETA ioctl implementations for
  *	the tty driver
  *
- * ALGORITHM
  *	In this test, the parent and child open the parentty and the childtty
  *	respectively.  After opening the childtty the child flushes the stream
  *	and sends a SIGUSR1 to the parent (thereby asking it to continue its
@@ -38,22 +23,6 @@
  *	set. The parent does all the testing, the requirement of the child
  *	process is to moniter the testing done by the parent, and hence the
  *	child just waits for the parent.
- *
- * USAGE:  <for command-line>
- *  ioctl02 -D /dev/tty[0-9] [-c n] [-f] [-i n] [-I x] [-P x] [-t]
- *     where,  -c n : Run n copies concurrently.
- *             -f   : Turn off functionality Testing.
- *             -i n : Execute test n times.
- *             -I x : Execute test for x seconds.
- *             -P x : Pause for x seconds between iterations.
- *             -t   : Turn on syscall timing.
- *
- * HISTORY
- *	07/2001 Ported by Wayne Boyer
- *
- * RESTRICTIONS
- *	test must be run with the -D option
- *	test may have to be run as root depending on the tty permissions
  */
 
 #include <stdio.h>
@@ -64,22 +33,17 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <termios.h>
-#include "test.h"
-#include "safe_macros.h"
-#include "lapi/ioctl.h"
+#include "tst_test.h"
+#include "tst_safe_macros.h"
 
 #define	CNUL	0
 
-char *TCID = "ioctl02";
-int TST_TOTAL = 1;
-
 static struct termio termio, save_io;
 
 static char *parenttty, *childtty;
-static int parentfd, childfd;
+static int parentfd = -1, childfd = -1;
 static int parentpid, childpid;
 static volatile int sigterm, sigusr1, sigusr2;
-static int closed = 1;
 
 static int do_child_setup(void);
 static int do_parent_setup(void);
@@ -88,108 +52,63 @@ static int run_ctest(void);
 static int chk_tty_parms();
 static void setup(void);
 static void cleanup(void);
-static void help(void);
 static void do_child(void);
-void do_child_uclinux(void);
 static void sigterm_handler(void);
 
-static int Devflag;
-static char *devname;
+static char *device;
 
-static option_t options[] = {
-	{"D:", &Devflag, &devname},
-	{NULL, NULL, NULL}
-};
-
-int main(int ac, char **av)
+static void verify_ioctl(void)
 {
-	int lc;
-	int rval;
-
-	tst_parse_opts(ac, av, options, &help);
-
-#ifdef UCLINUX
-	maybe_run_child(&do_child_uclinux, "dS", &parentpid, &childtty);
-#endif
-
-	if (!Devflag)
-		tst_brkm(TBROK, NULL, "You must specify a tty device with "
-			 "the -D option.");
-
-	tst_require_root();
-
-	setup();
+	parenttty = device;
+	childtty = device;
 
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
+	parentpid = getpid();
+	childpid = SAFE_FORK();
+	if (childpid == 0)
+		do_child();
 
-		tst_count = 0;
-
-		parenttty = devname;
-		childtty = devname;
-
-		parentpid = getpid();
-
-		childpid = FORK_OR_VFORK();
-		if (childpid < 0)
-			tst_brkm(TBROK, cleanup, "fork failed");
-
-		if (childpid == 0) {	/* child */
-#ifdef UCLINUX
-			if (self_exec(av[0], "dS", parentpid, childtty) < 0)
-				tst_brkm(TBROK, cleanup, "self_exec failed");
-#else
-			do_child();
-#endif
-		}
-
-		while (!sigusr1)
-			sleep(1);
-
-		sigusr1 = 0;
-
-		parentfd = do_parent_setup();
-		if (parentfd < 0) {
-			kill(childpid, SIGTERM);
-			waitpid(childpid, NULL, 0);
-			cleanup();
-		}
-
-		/* run the parent test */
-		rval = run_ptest();
-		if (rval == -1) {
-			/*
-			 * Parent cannot set/get ioctl parameters.
-			 * SIGTERM the child and cleanup.
-			 */
-			kill(childpid, SIGTERM);
-			waitpid(childpid, NULL, 0);
-			cleanup();
-		}
+	while (!sigusr1)
+		sleep(1);
 
-		if (rval != 0)
-			tst_resm(TFAIL, "TCGETA/TCSETA tests FAILED with "
-				 "%d %s", rval, rval > 1 ? "errors" : "error");
-		else
-			tst_resm(TPASS, "TCGETA/TCSETA tests SUCCEEDED");
+	sigusr1 = 0;
 
-		/* FIXME: check return codes. */
-		(void)kill(childpid, SIGTERM);
-		(void)waitpid(childpid, NULL, 0);
+	parentfd = do_parent_setup();
+	if (parentfd < 0) {
+		kill(childpid, SIGTERM);
+		waitpid(childpid, NULL, 0);
+		cleanup();
+	}
 
+	/* run the parent test */
+	int rval = run_ptest();
+	if (rval == -1) {
 		/*
-		 * Clean up things from the parent by restoring the
-		 * tty device information that was saved in setup()
-		 * and closing the tty file descriptor.
+		 * Parent cannot set/get ioctl parameters.
+		 * SIGTERM the child and cleanup.
 		 */
-		if (ioctl(parentfd, TCSETA, &save_io) == -1)
-			tst_resm(TINFO, "ioctl restore failed in main");
-		SAFE_CLOSE(cleanup, parentfd);
-
-		closed = 1;
+		kill(childpid, SIGTERM);
+		waitpid(childpid, NULL, 0);
+		cleanup();
 	}
-	cleanup();
 
-	tst_exit();
+	if (rval != 0)
+		tst_res(TFAIL, "TCGETA/TCSETA tests FAILED with "
+				"%d %s", rval, rval > 1 ? "errors" : "error");
+	else
+		tst_res(TPASS, "TCGETA/TCSETA tests SUCCEEDED");
+
+	/* FIXME: check return codes. */
+	(void)kill(childpid, SIGTERM);
+	(void)waitpid(childpid, NULL, 0);
+
+	/*
+	 * Clean up things from the parent by restoring the
+	 * tty device information that was saved in setup()
+	 * and closing the tty file descriptor.
+	 */
+	if (ioctl(parentfd, TCSETA, &save_io) == -1)
+		tst_res(TINFO, "ioctl restore failed in main");
+	SAFE_CLOSE(parentfd);
 }
 
 static void do_child(void)
@@ -201,28 +120,12 @@ static void do_child(void)
 	_exit(0);
 }
 
-void do_child_uclinux(void)
-{
-	struct sigaction act;
-
-	/* Set up the signal handlers again */
-	act.sa_handler = (void *)sigterm_handler;
-	act.sa_flags = 0;
-	sigemptyset(&act.sa_mask);
-	(void)sigaction(SIGTERM, &act, 0);
-
-	/* Run the normal child */
-	do_child();
-}
-
 /*
  * run_ptest() - setup the various termio structure values and issue
  *		 the TCSETA ioctl call with the TEST macro.
  */
 static int run_ptest(void)
 {
-	int i, rval;
-
 	/* Use "old" line discipline */
 	termio.c_line = 0;
 
@@ -230,7 +133,7 @@ static int run_ptest(void)
 	termio.c_cflag = B50 | CS7 | CREAD | PARENB | PARODD | CLOCAL;
 
 	/* Set control chars. */
-	for (i = 0; i < NCC; i++) {
+	for (int i = 0; i < NCC; i++) {
 		if (i == VEOL2)
 			continue;
 		termio.c_cc[i] = CSTART;
@@ -250,16 +153,10 @@ static int run_ptest(void)
 
 	TEST(ioctl(parentfd, TCSETA, &termio));
 
-	if (TEST_RETURN < 0) {
-		tst_resm(TFAIL, "ioctl TCSETA failed : "
-			 "errno = %d", TEST_ERRNO);
-		return -1;
-	}
-
 	/* Get termio and see if all parameters actually got set */
-	rval = ioctl(parentfd, TCGETA, &termio);
+	int rval = ioctl(parentfd, TCGETA, &termio);
 	if (rval < 0) {
-		tst_resm(TFAIL, "ioctl TCGETA failed.  Ending test.");
+		tst_res(TFAIL, "ioctl TCGETA failed.  Ending test.");
 		return -1;
 	}
 
@@ -276,10 +173,10 @@ static int run_ctest(void)
 
 	sigterm = 0;
 
-	tst_resm(TINFO, "child: Got SIGTERM from parent.");
+	tst_res(TINFO, "child: Got SIGTERM from parent.");
 
 	if (close(childfd) == -1)
-		tst_resm(TINFO, "close() in run_ctest() failed");
+		tst_res(TINFO, "close() in run_ctest() failed");
 	return 0;
 }
 
@@ -288,7 +185,7 @@ static int chk_tty_parms(void)
 	int i, flag = 0;
 
 	if (termio.c_line != 0) {
-		tst_resm(TINFO, "line discipline has incorrect value %o",
+		tst_res(TINFO, "line discipline has incorrect value %o",
 			 termio.c_line);
 		flag++;
 	}
@@ -301,7 +198,7 @@ static int chk_tty_parms(void)
 	 */
 #if 0
 	if (termio.c_cflag != (B50 | CS7 | CREAD | PARENB | PARODD | CLOCAL)) {
-		tst_resm(TINFO, "cflag has incorrect value. %o",
+		tst_res(TINFO, "cflag has incorrect value. %o",
 			 termio.c_cflag);
 		flag++;
 	}
@@ -312,7 +209,7 @@ static int chk_tty_parms(void)
 			if (termio.c_cc[VEOL2] == CNUL) {
 				continue;
 			} else {
-				tst_resm(TINFO, "control char %d has "
+				tst_res(TINFO, "control char %d has "
 					 "incorrect value %d %d", i,
 					 termio.c_cc[i], CNUL);
 				flag++;
@@ -321,7 +218,7 @@ static int chk_tty_parms(void)
 		}
 
 		if (termio.c_cc[i] != CSTART) {
-			tst_resm(TINFO, "control char %d has incorrect "
+			tst_res(TINFO, "control char %d has incorrect "
 				 "value %d.", i, termio.c_cc[i]);
 			flag++;
 		}
@@ -330,7 +227,7 @@ static int chk_tty_parms(void)
 	if (!
 	    (termio.c_lflag
 	     && (ISIG | ICANON | XCASE | ECHO | ECHOE | NOFLSH))) {
-		tst_resm(TINFO, "lflag has incorrect value. %o",
+		tst_res(TINFO, "lflag has incorrect value. %o",
 			 termio.c_lflag);
 		flag++;
 	}
@@ -339,34 +236,29 @@ static int chk_tty_parms(void)
 	    (termio.c_iflag
 	     && (BRKINT | IGNPAR | INPCK | ISTRIP | ICRNL | IUCLC | IXON | IXANY
 		 | IXOFF))) {
-		tst_resm(TINFO, "iflag has incorrect value. %o",
+		tst_res(TINFO, "iflag has incorrect value. %o",
 			 termio.c_iflag);
 		flag++;
 	}
 
 	if (!(termio.c_oflag && (OPOST | OLCUC | ONLCR | ONOCR))) {
-		tst_resm(TINFO, "oflag has incorrect value. %o",
+		tst_res(TINFO, "oflag has incorrect value. %o",
 			 termio.c_oflag);
 		flag++;
 	}
 
 	if (!flag)
-		tst_resm(TINFO, "termio values are set as expected");
+		tst_res(TINFO, "termio values are set as expected");
 
 	return flag;
 }
 
 static int do_parent_setup(void)
 {
-	int pfd;
-
-	pfd = SAFE_OPEN(cleanup, parenttty, O_RDWR, 0777);
-
-	/* unset the closed flag */
-	closed = 0;
+	int pfd = SAFE_OPEN(parenttty, O_RDWR, 0777);
 
 	/* flush tty queues to remove old output */
-	SAFE_IOCTL(cleanup, pfd, TCFLSH, 2);
+	SAFE_IOCTL(pfd, TCFLSH, 2);
 	return pfd;
 }
 
@@ -376,7 +268,7 @@ static int do_child_setup(void)
 
 	cfd = open(childtty, O_RDWR, 0777);
 	if (cfd < 0) {
-		tst_resm(TINFO, "Could not open %s in do_child_setup(), errno "
+		tst_res(TINFO, "Could not open %s in do_child_setup(), errno "
 			 "= %d", childtty, errno);
 		/* signal the parent so we don't hang the test */
 		kill(parentpid, SIGUSR1);
@@ -385,7 +277,7 @@ static int do_child_setup(void)
 
 	/* flush tty queues to remove old output */
 	if (ioctl(cfd, TCFLSH, 2) < 0) {
-		tst_resm(TINFO, "ioctl TCFLSH failed. : errno = %d", errno);
+		tst_res(TINFO, "ioctl TCFLSH failed. : errno = %d", errno);
 		/* signal the parent so we don't hang the test */
 		kill(parentpid, SIGUSR1);
 		return -1;
@@ -415,24 +307,22 @@ static void sigusr2_handler(void)
 	sigusr2 = 1;
 }
 
-static void help(void)
-{
-	printf("  -D <tty device> : for example, /dev/tty[0-9]\n");
-}
-
 static void setup(void)
 {
+	if (!device)
+		tst_brk(TBROK, "You must specify a tty device with -D option");
+
 	int fd;
 	struct sigaction act;
 
 	/* XXX: TERRNO required all over the place */
-	fd = SAFE_OPEN(NULL, devname, O_RDWR, 0777);
+	fd = SAFE_OPEN(device, O_RDWR, 0777);
 
 	/* Save the current device information - to be restored in cleanup() */
-	SAFE_IOCTL(cleanup, fd, TCGETA, &save_io);
+	SAFE_IOCTL(fd, TCGETA, &save_io);
 
 	/* Close the device */
-	SAFE_CLOSE(cleanup, fd);
+	SAFE_CLOSE(fd);
 
 	/* Set up the signal handlers */
 	act.sa_handler = (void *)sigterm_handler;
@@ -453,16 +343,26 @@ static void setup(void)
 	(void)sigaction(SIGTTOU, &act, 0);
 
 	sigterm = sigusr1 = sigusr2 = 0;
-
-	TEST_PAUSE;
 }
 
 static void cleanup(void)
 {
-	if (!closed) {
+	if (parentfd != -1) {
 		if (ioctl(parentfd, TCSETA, &save_io) == -1)
-			tst_resm(TINFO, "ioctl restore failed in cleanup()");
+			tst_res(TINFO, "ioctl restore failed in cleanup()");
 		if (close(parentfd) == -1)
-			tst_resm(TINFO, "close() failed in cleanup()");
+			tst_res(TINFO, "close() failed in cleanup()");
 	}
 }
+
+static struct tst_test test = {
+	.needs_root = 1,
+	.forks_child = 1,
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = verify_ioctl,
+	.options = (struct tst_option[]) {
+		{"D:", &device, "Tty device. For example, /dev/tty[0-9]"},
+		{}
+	}
+};
\ No newline at end of file
Signature · 5 lines
2.42.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#2 lore raw

PATCH v3 2/4 Use correct binary operand in ioctl02.c

In reply to Marius Kittler
---
 testcases/kernel/syscalls/ioctl/ioctl02.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
testcases/kernel/syscalls/ioctl/ioctl02.c +3 -3
index 77283e510..72a446f99 100644
@@ -226,7 +226,7 @@ static int chk_tty_parms(void)
 
 	if (!
 	    (termio.c_lflag
-	     && (ISIG | ICANON | XCASE | ECHO | ECHOE | NOFLSH))) {
+	     & (ISIG | ICANON | XCASE | ECHO | ECHOE | NOFLSH))) {
 		tst_res(TINFO, "lflag has incorrect value. %o",
 			 termio.c_lflag);
 		flag++;
@@ -234,14 +234,14 @@ static int chk_tty_parms(void)
 
 	if (!
 	    (termio.c_iflag
-	     && (BRKINT | IGNPAR | INPCK | ISTRIP | ICRNL | IUCLC | IXON | IXANY
+	     & (BRKINT | IGNPAR | INPCK | ISTRIP | ICRNL | IUCLC | IXON | IXANY
 		 | IXOFF))) {
 		tst_res(TINFO, "iflag has incorrect value. %o",
 			 termio.c_iflag);
 		flag++;
 	}
 
-	if (!(termio.c_oflag && (OPOST | OLCUC | ONLCR | ONOCR))) {
+	if (!(termio.c_oflag & (OPOST | OLCUC | ONLCR | ONOCR))) {
 		tst_res(TINFO, "oflag has incorrect value. %o",
 			 termio.c_oflag);
 		flag++;
Signature · 5 lines
2.42.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#3 lore raw

PATCH v3 3/4 Make checks for termio flags more strict

In reply to Marius Kittler

The checks for termio flags can actually use a != check to also fail when any unexpected flags are present.

---
 testcases/kernel/syscalls/ioctl/ioctl02.c | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)
testcases/kernel/syscalls/ioctl/ioctl02.c +5 -8
index 72a446f99..6b0924c65 100644
@@ -224,24 +224,21 @@ static int chk_tty_parms(void)
 		}
 	}
 
-	if (!
-	    (termio.c_lflag
-	     & (ISIG | ICANON | XCASE | ECHO | ECHOE | NOFLSH))) {
+	if (termio.c_lflag != (ISIG | ICANON | XCASE | ECHO | ECHOE
+		 | NOFLSH)) {
 		tst_res(TINFO, "lflag has incorrect value. %o",
 			 termio.c_lflag);
 		flag++;
 	}
 
-	if (!
-	    (termio.c_iflag
-	     & (BRKINT | IGNPAR | INPCK | ISTRIP | ICRNL | IUCLC | IXON | IXANY
-		 | IXOFF))) {
+	if (termio.c_iflag != (BRKINT | IGNPAR | INPCK | ISTRIP
+		 | ICRNL | IUCLC | IXON | IXANY | IXOFF)) {
 		tst_res(TINFO, "iflag has incorrect value. %o",
 			 termio.c_iflag);
 		flag++;
 	}
 
-	if (!(termio.c_oflag & (OPOST | OLCUC | ONLCR | ONOCR))) {
+	if (termio.c_oflag != (OPOST | OLCUC | ONLCR | ONOCR)) {
 		tst_res(TINFO, "oflag has incorrect value. %o",
 			 termio.c_oflag);
 		flag++;
Signature · 5 lines
2.42.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#4 lore raw

PATCH v3 4/4 Simplify ioctl02.c further

In reply to Marius Kittler

* Use checkpoint API instead of signals
* Use SAFE_…-macros instead of manual error handling

---
 testcases/kernel/syscalls/ioctl/ioctl02.c | 167 ++++++----------------
 1 file changed, 43 insertions(+), 124 deletions(-)
testcases/kernel/syscalls/ioctl/ioctl02.c +43 -124
index 6b0924c65..30c647ca3 100644
@@ -13,50 +13,49 @@
  *
  *	In this test, the parent and child open the parentty and the childtty
  *	respectively.  After opening the childtty the child flushes the stream
- *	and sends a SIGUSR1 to the parent (thereby asking it to continue its
- *	testing). The parent, which was waiting for this signal to arrive, now
- *	starts the testing. It issues a TCGETA ioctl to get all the tty
- *	parameters. It then changes them to known values by issuing a TCSETA
- *	ioctl.  Then the parent issues a TCGETA ioctl again and compares the
- *	received values with what it had set earlier. The test fails if TCGETA
- *	or TCSETA fails, or if the received values don't match those that were
- *	set. The parent does all the testing, the requirement of the child
- *	process is to moniter the testing done by the parent, and hence the
- *	child just waits for the parent.
+ *	and sends a wakes the parent (thereby asking it to continue its
+ *	testing). The parent, then starts the testing. It issues a TCGETA
+ *	ioctl to get all the tty parameters. It then changes them to known
+ *	values by issuing a TCSETA ioctl. Then the parent issues a TCGETA
+ *	ioctl again and compares the received values with what it had set
+ *	earlier. The test fails if TCGETA or TCSETA fails, or if the received
+ *	values don't match those that were set. The parent does all the
+ *	testing, the requirement of the child process is to moniter the
+ *	testing done by the parent, and hence the child just waits for the
+ *	parent.
  */
 
 #include <stdio.h>
 #include <fcntl.h>
-#include <signal.h>
 #include <errno.h>
 #include <sys/wait.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <termios.h>
+
+#include "tst_checkpoint.h"
 #include "tst_test.h"
 #include "tst_safe_macros.h"
 
-#define	CNUL	0
-
 static struct termio termio, save_io;
 
 static char *parenttty, *childtty;
 static int parentfd = -1, childfd = -1;
 static int parentpid, childpid;
-static volatile int sigterm, sigusr1, sigusr2;
 
 static int do_child_setup(void);
-static int do_parent_setup(void);
 static int run_ptest(void);
 static int run_ctest(void);
 static int chk_tty_parms();
 static void setup(void);
 static void cleanup(void);
 static void do_child(void);
-static void sigterm_handler(void);
 
 static char *device;
 
+static const int checkpoint_child_ready = 1;
+static const int checkpoint_parent_done_testing = 2;
+
 static void verify_ioctl(void)
 {
 	parenttty = device;
@@ -67,28 +66,21 @@ static void verify_ioctl(void)
 	if (childpid == 0)
 		do_child();
 
-	while (!sigusr1)
-		sleep(1);
+	TST_CHECKPOINT_WAIT(checkpoint_child_ready);
 
-	sigusr1 = 0;
-
-	parentfd = do_parent_setup();
-	if (parentfd < 0) {
-		kill(childpid, SIGTERM);
-		waitpid(childpid, NULL, 0);
-		cleanup();
-	}
+	parentfd = SAFE_OPEN(parenttty, O_RDWR, 0777);
+	/* flush tty queues to remove old output */
+	SAFE_IOCTL(parentfd, TCFLSH, 2);
 
 	/* run the parent test */
 	int rval = run_ptest();
 	if (rval == -1) {
 		/*
 		 * Parent cannot set/get ioctl parameters.
-		 * SIGTERM the child and cleanup.
+		 * wake the child and cleanup.
 		 */
-		kill(childpid, SIGTERM);
+		TST_CHECKPOINT_WAKE(checkpoint_parent_done_testing);
 		waitpid(childpid, NULL, 0);
-		cleanup();
 	}
 
 	if (rval != 0)
@@ -97,17 +89,15 @@ static void verify_ioctl(void)
 	else
 		tst_res(TPASS, "TCGETA/TCSETA tests SUCCEEDED");
 
-	/* FIXME: check return codes. */
-	(void)kill(childpid, SIGTERM);
-	(void)waitpid(childpid, NULL, 0);
+	TST_CHECKPOINT_WAKE(checkpoint_parent_done_testing);
+	waitpid(childpid, NULL, 0);
 
 	/*
 	 * Clean up things from the parent by restoring the
 	 * tty device information that was saved in setup()
 	 * and closing the tty file descriptor.
 	 */
-	if (ioctl(parentfd, TCSETA, &save_io) == -1)
-		tst_res(TINFO, "ioctl restore failed in main");
+	SAFE_IOCTL(parentfd, TCSETA, &save_io);
 	SAFE_CLOSE(parentfd);
 }
 
@@ -151,32 +141,18 @@ static int run_ptest(void)
 	/* Set output modes. */
 	termio.c_oflag = OPOST | OLCUC | ONLCR | ONOCR;
 
-	TEST(ioctl(parentfd, TCSETA, &termio));
+	SAFE_IOCTL(parentfd, TCSETA, &termio);
 
 	/* Get termio and see if all parameters actually got set */
-	int rval = ioctl(parentfd, TCGETA, &termio);
-	if (rval < 0) {
-		tst_res(TFAIL, "ioctl TCGETA failed.  Ending test.");
-		return -1;
-	}
-
+	SAFE_IOCTL(parentfd, TCGETA, &termio);
 	return chk_tty_parms();
 }
 
 static int run_ctest(void)
 {
-	/*
-	 * Wait till the parent has finished testing.
-	 */
-	while (!sigterm)
-		sleep(1);
-
-	sigterm = 0;
-
-	tst_res(TINFO, "child: Got SIGTERM from parent.");
-
-	if (close(childfd) == -1)
-		tst_res(TINFO, "close() in run_ctest() failed");
+	TST_CHECKPOINT_WAIT(checkpoint_parent_done_testing);
+	tst_res(TINFO, "child: parent has finished testing");
+	SAFE_CLOSE(childfd);
 	return 0;
 }
 
@@ -206,12 +182,11 @@ static int chk_tty_parms(void)
 
 	for (i = 0; i < NCC; i++) {
 		if (i == VEOL2) {
-			if (termio.c_cc[VEOL2] == CNUL) {
+			if (!termio.c_cc[VEOL2]) {
 				continue;
 			} else {
 				tst_res(TINFO, "control char %d has "
-					 "incorrect value %d %d", i,
-					 termio.c_cc[i], CNUL);
+					 "incorrect value %d", i, termio.c_cc[i]);
 				flag++;
 				continue;
 			}
@@ -250,110 +225,54 @@ static int chk_tty_parms(void)
 	return flag;
 }
 
-static int do_parent_setup(void)
-{
-	int pfd = SAFE_OPEN(parenttty, O_RDWR, 0777);
-
-	/* flush tty queues to remove old output */
-	SAFE_IOCTL(pfd, TCFLSH, 2);
-	return pfd;
-}
-
 static int do_child_setup(void)
 {
-	int cfd;
-
-	cfd = open(childtty, O_RDWR, 0777);
+	int cfd = SAFE_OPEN(childtty, O_RDWR, 0777);
 	if (cfd < 0) {
-		tst_res(TINFO, "Could not open %s in do_child_setup(), errno "
-			 "= %d", childtty, errno);
-		/* signal the parent so we don't hang the test */
-		kill(parentpid, SIGUSR1);
+		/* wake the parent so we don't hang the test */
+		TST_CHECKPOINT_WAKE(checkpoint_child_ready);
 		return -1;
 	}
 
 	/* flush tty queues to remove old output */
-	if (ioctl(cfd, TCFLSH, 2) < 0) {
-		tst_res(TINFO, "ioctl TCFLSH failed. : errno = %d", errno);
-		/* signal the parent so we don't hang the test */
-		kill(parentpid, SIGUSR1);
+	if (SAFE_IOCTL(cfd, TCFLSH, 2) < 0) {
+		/* wake the parent so we don't hang the test */
+		TST_CHECKPOINT_WAKE(checkpoint_child_ready);
 		return -1;
 	}
 
 	/* tell the parent that we're done */
-	kill(parentpid, SIGUSR1);
+	TST_CHECKPOINT_WAKE(checkpoint_child_ready);
 
 	return cfd;
 }
 
-/*
- * Define the signals handlers here.
- */
-static void sigterm_handler(void)
-{
-	sigterm = 1;
-}
-
-static void sigusr1_handler(void)
-{
-	sigusr1 = 1;
-}
-
-static void sigusr2_handler(void)
-{
-	sigusr2 = 1;
-}
-
 static void setup(void)
 {
 	if (!device)
 		tst_brk(TBROK, "You must specify a tty device with -D option");
 
-	int fd;
-	struct sigaction act;
-
 	/* XXX: TERRNO required all over the place */
-	fd = SAFE_OPEN(device, O_RDWR, 0777);
+	int fd = SAFE_OPEN(device, O_RDWR, 0777);
 
 	/* Save the current device information - to be restored in cleanup() */
 	SAFE_IOCTL(fd, TCGETA, &save_io);
 
 	/* Close the device */
 	SAFE_CLOSE(fd);
-
-	/* Set up the signal handlers */
-	act.sa_handler = (void *)sigterm_handler;
-	act.sa_flags = 0;
-	sigemptyset(&act.sa_mask);
-	(void)sigaction(SIGTERM, &act, 0);
-
-	act.sa_handler = (void *)sigusr1_handler;
-	act.sa_flags = 0;
-	(void)sigaction(SIGUSR1, &act, 0);
-
-	act.sa_handler = (void *)sigusr2_handler;
-	act.sa_flags = 0;
-	(void)sigaction(SIGUSR2, &act, 0);
-
-	act.sa_handler = SIG_IGN;
-	act.sa_flags = 0;
-	(void)sigaction(SIGTTOU, &act, 0);
-
-	sigterm = sigusr1 = sigusr2 = 0;
 }
 
 static void cleanup(void)
 {
-	if (parentfd != -1) {
-		if (ioctl(parentfd, TCSETA, &save_io) == -1)
-			tst_res(TINFO, "ioctl restore failed in cleanup()");
-		if (close(parentfd) == -1)
-			tst_res(TINFO, "close() failed in cleanup()");
+	if (parentfd >= 0) {
+		SAFE_IOCTL(parentfd, TCSETA, &save_io);
+		SAFE_CLOSE(parentfd);
 	}
 }
 
 static struct tst_test test = {
 	.needs_root = 1,
+	.needs_checkpoints = 1,
 	.forks_child = 1,
 	.setup = setup,
 	.cleanup = cleanup,
Signature · 5 lines
2.42.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#5 lore raw

Re: [LTP] [PATCH v3 4/4] Simplify ioctl02.c further

Hi,
please merge this patch with patch 1 and also merge patch 2 with patch 3.

Quoted 91 lines · Marius Kittler

* Use checkpoint API instead of signals
* Use SAFE_…-macros instead of manual error handling

---
  testcases/kernel/syscalls/ioctl/ioctl02.c | 167 ++++++----------------
  1 file changed, 43 insertions(+), 124 deletions(-)
testcases/kernel/syscalls/ioctl/ioctl02.c +16 -23
index 6b0924c65..30c647ca3 100644
@@ -13,50 +13,49 @@
   *
   *	In this test, the parent and child open the parentty and the childtty
   *	respectively.  After opening the childtty the child flushes the stream
- *	and sends a SIGUSR1 to the parent (thereby asking it to continue its
- *	testing). The parent, which was waiting for this signal to arrive, now
- *	starts the testing. It issues a TCGETA ioctl to get all the tty
- *	parameters. It then changes them to known values by issuing a TCSETA
- *	ioctl.  Then the parent issues a TCGETA ioctl again and compares the
- *	received values with what it had set earlier. The test fails if TCGETA
- *	or TCSETA fails, or if the received values don't match those that were
- *	set. The parent does all the testing, the requirement of the child
- *	process is to moniter the testing done by the parent, and hence the
- *	child just waits for the parent.
+ *	and sends a wakes the parent (thereby asking it to continue its
+ *	testing). The parent, then starts the testing. It issues a TCGETA
+ *	ioctl to get all the tty parameters. It then changes them to known
+ *	values by issuing a TCSETA ioctl. Then the parent issues a TCGETA
+ *	ioctl again and compares the received values with what it had set
+ *	earlier. The test fails if TCGETA or TCSETA fails, or if the received
+ *	values don't match those that were set. The parent does all the
+ *	testing, the requirement of the child process is to moniter the
+ *	testing done by the parent, and hence the child just waits for the
+ *	parent.
   */
 
  #include <stdio.h>
  #include <fcntl.h>
-#include <signal.h>
  #include <errno.h>
  #include <sys/wait.h>
  #include <sys/types.h>
  #include <sys/stat.h>
  #include <termios.h>
+
+#include "tst_checkpoint.h"
  #include "tst_test.h"
  #include "tst_safe_macros.h"
 
-#define	CNUL	0
-
  static struct termio termio, save_io;
 
  static char *parenttty, *childtty;
  static int parentfd = -1, childfd = -1;
  static int parentpid, childpid;
-static volatile int sigterm, sigusr1, sigusr2;
 
  static int do_child_setup(void);
-static int do_parent_setup(void);
  static int run_ptest(void);
  static int run_ctest(void);
  static int chk_tty_parms();
  static void setup(void);
  static void cleanup(void);
  static void do_child(void);
-static void sigterm_handler(void);
 
  static char *device;
 
+static const int checkpoint_child_ready = 1;
+static const int checkpoint_parent_done_testing = 2;
+
  static void verify_ioctl(void)
  {
  	parenttty = device;
@@ -67,28 +66,21 @@ static void verify_ioctl(void)
  	if (childpid == 0)
  		do_child();
 
-	while (!sigusr1)
-		sleep(1);
+	TST_CHECKPOINT_WAIT(checkpoint_child_ready);
 
-	sigusr1 = 0;
-
-	parentfd = do_parent_setup();
-	if (parentfd < 0) {
-		kill(childpid, SIGTERM);

testcases/kernel/syscalls/ioctl/ioctl02.c static void verify_ioctl(void) · line 77

Quoted 10 lines
testcases/kernel/syscalls/ioctl/ioctl02.c +3 -3
-		waitpid(childpid, NULL, 0);
-		cleanup();
-	}
+	parentfd = SAFE_OPEN(parenttty, O_RDWR, 0777);
+	/* flush tty queues to remove old output */
+	SAFE_IOCTL(parentfd, TCFLSH, 2);
 
  	/* run the parent test */
  	int rval = run_ptest();
  	if (rval == -1) {

This error handling branch is now identical to normal child wakeup so it can be removed.

Quoted 13 lines
testcases/kernel/syscalls/ioctl/ioctl02.c +2 -3
  		/*
  		 * Parent cannot set/get ioctl parameters.
-		 * SIGTERM the child and cleanup.
+		 * wake the child and cleanup.
  		 */
-		kill(childpid, SIGTERM);
+		TST_CHECKPOINT_WAKE(checkpoint_parent_done_testing);
  		waitpid(childpid, NULL, 0);
-		cleanup();
  	}
 
  	if (rval != 0)

testcases/kernel/syscalls/ioctl/ioctl02.c static void verify_ioctl(void) · line 93

Quoted 8 lines
testcases/kernel/syscalls/ioctl/ioctl02.c +2 -3
@@ -97,17 +89,15 @@ static void verify_ioctl(void)
  	else
  		tst_res(TPASS, "TCGETA/TCSETA tests SUCCEEDED");
 
-	/* FIXME: check return codes. */
-	(void)kill(childpid, SIGTERM);
-	(void)waitpid(childpid, NULL, 0);
+	TST_CHECKPOINT_WAKE(checkpoint_parent_done_testing);
+	waitpid(childpid, NULL, 0);

waitpid() is unnecessary unless you want to check child exit status. The child process will be reaped by the LTP library automatically.

testcases/kernel/syscalls/ioctl/ioctl02.c static void verify_ioctl(void) · line 101

Quoted 10 lines
testcases/kernel/syscalls/ioctl/ioctl02.c +1 -2
  	/*
  	 * Clean up things from the parent by restoring the
  	 * tty device information that was saved in setup()
  	 * and closing the tty file descriptor.
  	 */
-	if (ioctl(parentfd, TCSETA, &save_io) == -1)
-		tst_res(TINFO, "ioctl restore failed in main");
+	SAFE_IOCTL(parentfd, TCSETA, &save_io);
  	SAFE_CLOSE(parentfd);

This will be done by cleanup() automatically.

Quoted 71 lines
testcases/kernel/syscalls/ioctl/ioctl02.c +7 -32
  }
 
@@ -151,32 +141,18 @@ static int run_ptest(void)
  	/* Set output modes. */
  	termio.c_oflag = OPOST | OLCUC | ONLCR | ONOCR;
 
-	TEST(ioctl(parentfd, TCSETA, &termio));
+	SAFE_IOCTL(parentfd, TCSETA, &termio);
 
  	/* Get termio and see if all parameters actually got set */
-	int rval = ioctl(parentfd, TCGETA, &termio);
-	if (rval < 0) {
-		tst_res(TFAIL, "ioctl TCGETA failed.  Ending test.");
-		return -1;
-	}
-
+	SAFE_IOCTL(parentfd, TCGETA, &termio);
  	return chk_tty_parms();
  }
 
  static int run_ctest(void)
  {
-	/*
-	 * Wait till the parent has finished testing.
-	 */
-	while (!sigterm)
-		sleep(1);
-
-	sigterm = 0;
-
-	tst_res(TINFO, "child: Got SIGTERM from parent.");
-
-	if (close(childfd) == -1)
-		tst_res(TINFO, "close() in run_ctest() failed");
+	TST_CHECKPOINT_WAIT(checkpoint_parent_done_testing);
+	tst_res(TINFO, "child: parent has finished testing");
+	SAFE_CLOSE(childfd);
  	return 0;
  }
 
@@ -206,12 +182,11 @@ static int chk_tty_parms(void)
 
  	for (i = 0; i < NCC; i++) {
  		if (i == VEOL2) {
-			if (termio.c_cc[VEOL2] == CNUL) {
+			if (!termio.c_cc[VEOL2]) {
  				continue;
  			} else {
  				tst_res(TINFO, "control char %d has "
-					 "incorrect value %d %d", i,
-					 termio.c_cc[i], CNUL);
+					 "incorrect value %d", i, termio.c_cc[i]);
  				flag++;
  				continue;
  			}
@@ -250,110 +225,54 @@ static int chk_tty_parms(void)
  	return flag;
  }
 
-static int do_parent_setup(void)
-{
-	int pfd = SAFE_OPEN(parenttty, O_RDWR, 0777);
-
-	/* flush tty queues to remove old output */
-	SAFE_IOCTL(pfd, TCFLSH, 2);
-	return pfd;
-}
-
  static int do_child_setup(void)
  {
-	int cfd;

testcases/kernel/syscalls/ioctl/ioctl02.c static int chk_tty_parms(void) · line 233

Quoted 10 lines
testcases/kernel/syscalls/ioctl/ioctl02.c +3 -6
-
-	cfd = open(childtty, O_RDWR, 0777);
+	int cfd = SAFE_OPEN(childtty, O_RDWR, 0777);
  	if (cfd < 0) {
-		tst_res(TINFO, "Could not open %s in do_child_setup(), errno "
-			 "= %d", childtty, errno);
-		/* signal the parent so we don't hang the test */
-		kill(parentpid, SIGUSR1);
+		/* wake the parent so we don't hang the test */
+		TST_CHECKPOINT_WAKE(checkpoint_child_ready);

This wake call is unreachable. The parent should use wait with timeout in case the child fails to open the TTY.

testcases/kernel/syscalls/ioctl/ioctl02.c static int chk_tty_parms(void) · line 240

Quoted 11 lines
testcases/kernel/syscalls/ioctl/ioctl02.c +3 -4
  		return -1;
  	}
 
  	/* flush tty queues to remove old output */
-	if (ioctl(cfd, TCFLSH, 2) < 0) {
-		tst_res(TINFO, "ioctl TCFLSH failed. : errno = %d", errno);
-		/* signal the parent so we don't hang the test */
-		kill(parentpid, SIGUSR1);
+	if (SAFE_IOCTL(cfd, TCFLSH, 2) < 0) {
+		/* wake the parent so we don't hang the test */
+		TST_CHECKPOINT_WAKE(checkpoint_child_ready);

Again, unreachable wake call.

Quoted 86 lines
testcases/kernel/syscalls/ioctl/ioctl02.c +6 -48
  		return -1;
  	}
 
  	/* tell the parent that we're done */
-	kill(parentpid, SIGUSR1);
+	TST_CHECKPOINT_WAKE(checkpoint_child_ready);
 
  	return cfd;
  }
 
-/*
- * Define the signals handlers here.
- */
-static void sigterm_handler(void)
-{
-	sigterm = 1;
-}
-
-static void sigusr1_handler(void)
-{
-	sigusr1 = 1;
-}
-
-static void sigusr2_handler(void)
-{
-	sigusr2 = 1;
-}
-
  static void setup(void)
  {
  	if (!device)
  		tst_brk(TBROK, "You must specify a tty device with -D option");
 
-	int fd;
-	struct sigaction act;
-
  	/* XXX: TERRNO required all over the place */
-	fd = SAFE_OPEN(device, O_RDWR, 0777);
+	int fd = SAFE_OPEN(device, O_RDWR, 0777);
 
  	/* Save the current device information - to be restored in cleanup() */
  	SAFE_IOCTL(fd, TCGETA, &save_io);
 
  	/* Close the device */
  	SAFE_CLOSE(fd);
-
-	/* Set up the signal handlers */
-	act.sa_handler = (void *)sigterm_handler;
-	act.sa_flags = 0;
-	sigemptyset(&act.sa_mask);
-	(void)sigaction(SIGTERM, &act, 0);
-
-	act.sa_handler = (void *)sigusr1_handler;
-	act.sa_flags = 0;
-	(void)sigaction(SIGUSR1, &act, 0);
-
-	act.sa_handler = (void *)sigusr2_handler;
-	act.sa_flags = 0;
-	(void)sigaction(SIGUSR2, &act, 0);
-
-	act.sa_handler = SIG_IGN;
-	act.sa_flags = 0;
-	(void)sigaction(SIGTTOU, &act, 0);
-
-	sigterm = sigusr1 = sigusr2 = 0;
  }
 
  static void cleanup(void)
  {
-	if (parentfd != -1) {
-		if (ioctl(parentfd, TCSETA, &save_io) == -1)
-			tst_res(TINFO, "ioctl restore failed in cleanup()");
-		if (close(parentfd) == -1)
-			tst_res(TINFO, "close() failed in cleanup()");
+	if (parentfd >= 0) {
+		SAFE_IOCTL(parentfd, TCSETA, &save_io);
+		SAFE_CLOSE(parentfd);
  	}
  }
 
  static struct tst_test test = {
  	.needs_root = 1,
+	.needs_checkpoints = 1,
  	.forks_child = 1,
  	.setup = setup,
  	.cleanup = cleanup,
Signature · 11 lines
Martin Doucha   [email protected]
SW Quality Engineer
SUSE LINUX, s.r.o.
CORSO IIa
Krizikova 148/34
186 00 Prague 8
Czech Republic


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp