Skip to contentSkip to conversation

Convert ptrace01.c to new library

PATCH New Jorik Cronenberg 3 participants 4 messages Oct 1, 2019 – Oct 8, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Convert ptrace01.c to new library

Hi,

This is just a port of ptrace01.c. I wanted to make sure what I'm doing is right before porting the rest. I tried to make as little changes as possible so the test should be pretty much the same.

---
 testcases/kernel/syscalls/ptrace/ptrace01.c | 296 +++++++---------------------
 1 file changed, 72 insertions(+), 224 deletions(-)
testcases/kernel/syscalls/ptrace/ptrace01.c +72 -224
index 5a7ed5ffd..ab9ef35ee 100644
@@ -1,36 +1,13 @@
-/*
+// SPDX-License-Identifier: GPL-2.0-or-later
+/* 
  * Copyright (c) Wipro Technologies Ltd, 2002.  All Rights Reserved.
  *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of version 2 of the GNU General Public License as
- * published by the Free Software Foundation.
+ * Author:	Saji Kumar.V.R <[email protected]>
  *
- * This program is distributed in the hope that it would be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ * Ported to new library:
+ * 10/2019	Jorik Cronenberg <[email protected]>
  *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- */
-/**********************************************************
- *
- *    TEST IDENTIFIER	: ptrace01
- *
- *    EXECUTED BY	: anyone
- *
- *    TEST TITLE	: functionality test for ptrace(2)
- *
- *    TEST CASE TOTAL	: 2
- *
- *    AUTHOR		: Saji Kumar.V.R <[email protected]>
- *
- *    SIGNALS
- * 	Uses SIGUSR1 to pause before test if option set.
- * 	(See the parse_opts(3) man page).
- *
- *    DESCRIPTION
+ * DESCRIPTION
  *	This test case tests the functionality of ptrace() for
  *	PTRACE_TRACEME & PTRACE_KILL requests.
  *	Here, we fork a child & the child does ptrace(PTRACE_TRACEME, ...).
@@ -44,243 +21,114 @@
  * 		In both cases, child should stop & notify parent on reception
  * 		of SIGUSR2
  *
- * 	Setup:
- * 	  Setup signal handling.
- *	  Pause for SIGUSR1 if option specified.
- *
- * 	Test:
- *	 Loop if the proper options are given.
- *	 setup signal handler for SIGUSR2 signal
- *	 fork a child
- *
- *	 CHILD:
- *		setup signal handler for SIGUSR2 signal
- *		call ptrace() with PTRACE_TRACEME request
- *		send SIGUSR2 signal to self
- *	 PARENT:
- *		wait() for child.
- *		if parent is notified when child gets a signal through wait(),
- *		then
- *			do  ptrace(PTRACE_KILL, ..) on child
- * 			wait() for child to finish,
- * 			if child exited abnormaly,
- *				TEST passed
- * 			else
- * 				TEST failed
- *		else
- *			TEST failed
- *
- * 	Cleanup:
- * 	  Print errno log and/or timing stats if options given
- *
- * USAGE:  <for command-line>
- *  ptrace01 [-c n] [-e] [-i n] [-I x] [-P x] [-t] [-h] [-f] [-p]
- *			where,  -c n : Run n copies concurrently.
- *				-e   : Turn on errno logging.
- *				-h   : Show help screen
- *				-f   : Turn off functional testing
- *				-i n : Execute test n times.
- *				-I x : Execute test for x seconds.
- *				-p   : Pause for SIGUSR1 before starting
- *				-P x : Pause for x seconds between iterations.
- *				-t   : Turn on syscall timing.
- *
- ****************************************************************/
+ */
 
+#include <stdlib.h>
 #include <errno.h>
 #include <signal.h>
 #include <sys/wait.h>
-
 #include <config.h>
 #include "ptrace.h"
+#include "tst_test.h"
 
-#include "test.h"
-
-static void do_child(void);
-static void setup(void);
-static void cleanup(void);
-static void child_handler();
-static void parent_handler();
+static int got_signal;
 
-static int got_signal = 0;
-
-char *TCID = "ptrace01";
-static int i;			/* loop test case counter, shared with do_child */
-
-int TST_TOTAL = 2;
-
-int main(int ac, char **av)
+void child_handler(void)
 {
-
-	int lc;
-	pid_t child_pid;
-	int status;
-	struct sigaction parent_act;
-
-	tst_parse_opts(ac, av, NULL, NULL);
-#ifdef UCLINUX
-	maybe_run_child(&do_child, "d", &i);
-#endif
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-
-		tst_count = 0;
-
-		for (i = 0; i < TST_TOTAL; ++i) {
-			got_signal = 0;
-
-			/* Setup signal handler for parent */
-			if (i == 1) {
-				parent_act.sa_handler = parent_handler;
-				parent_act.sa_flags = SA_RESTART;
-				sigemptyset(&parent_act.sa_mask);
-
-				if ((sigaction(SIGUSR2, &parent_act, NULL))
-				    == -1) {
-					tst_resm(TWARN, "sigaction() failed"
-						 " in parent");
-					continue;
-				}
-			}
-
-			switch (child_pid = FORK_OR_VFORK()) {
-
-			case -1:
-				/* fork() failed */
-				tst_resm(TFAIL, "fork() failed");
-				continue;
-
-			case 0:
-				/* Child */
-#ifdef UCLINUX
-				if (self_exec(av[0], "d", i) < 0) {
-					tst_resm(TFAIL, "self_exec failed");
-					continue;
-				}
-#else
-				do_child();
-#endif
-
-			default:
-				/* Parent */
-				if ((waitpid(child_pid, &status, 0)) < 0) {
-					tst_resm(TFAIL, "waitpid() failed");
-					continue;
-				}
-
-				/*
-				 * Check the exit status of child. If (it exits
-				 * normally with exit value 1) OR (child came
-				 * through signal handler), Test Failed
-				 */
-
-				if (((WIFEXITED(status)) &&
-				     (WEXITSTATUS(status))) ||
-				    (got_signal == 1)) {
-					tst_resm(TFAIL, "Test Failed");
-					continue;
-				} else {
-					/* Kill child */
-					if ((ptrace(PTRACE_KILL, child_pid,
-						    0, 0)) == -1) {
-						tst_resm(TFAIL, "Test Failed:"
-							 " Parent was not able to kill"
-							 " child");
-						continue;
-					}
-				}
-
-				if ((waitpid(child_pid, &status, 0)) < 0) {
-					tst_resm(TFAIL, "waitpid() failed");
-					continue;
-				}
-
-				if (WIFEXITED(status)) {
-					/* Child exits normally */
-					tst_resm(TFAIL, "Test failed");
-				} else {
-					tst_resm(TPASS, "Test Passed");
-				}
-
-			}
-		}
+	if ((kill(getppid(), SIGUSR2)) == -1) {
+		tst_res(TWARN, "kill() failed in child_handler()");
+		exit(1);
 	}
+}
 
-	/* cleanup and exit */
-	cleanup();
-	tst_exit();
-
+void parent_handler(void)
+{
+	got_signal = 1;
 }
 
-/* do_child() */
-void do_child(void)
+void do_child(unsigned int i)
 {
 	struct sigaction child_act;
 
-	/* Setup signal handler for child */
-	if (i == 0) {
+	if (i == 0)
 		child_act.sa_handler = SIG_IGN;
-	} else {
+	else
 		child_act.sa_handler = child_handler;
-	}
+
 	child_act.sa_flags = SA_RESTART;
 	sigemptyset(&child_act.sa_mask);
 
 	if ((sigaction(SIGUSR2, &child_act, NULL)) == -1) {
-		tst_resm(TWARN, "sigaction() failed in child");
+		tst_res(TWARN, "sigaction() failed in child");
 		exit(1);
 	}
 
 	if ((ptrace(PTRACE_TRACEME, 0, 0, 0)) == -1) {
-		tst_resm(TWARN, "ptrace() failed in child");
+		tst_res(TWARN, "ptrace() failed in child");
 		exit(1);
 	}
-	/* ensure that child bypasses signal handler */
 	if ((kill(getpid(), SIGUSR2)) == -1) {
-		tst_resm(TWARN, "kill() failed in child");
+		tst_res(TWARN, "kill() failed in child");
 		exit(1);
 	}
 	exit(1);
 }
 
-/* setup() - performs all ONE TIME setup for this test */
-void setup(void)
+static void run(unsigned int i)
 {
 
-	tst_sig(FORK, DEF_HANDLER, cleanup);
+	pid_t child_pid;
+	int status;
+	struct sigaction parent_act;
 
-	TEST_PAUSE;
+#ifdef UCLINUX
+	maybe_run_child(&do_child, "d", &i);
+#endif
 
-}
+	got_signal = 0;
 
-/*
- *cleanup() -  performs all ONE TIME cleanup for this test at
- *		completion or premature exit.
- */
-void cleanup(void)
-{
+	if (i == 1) {
+		parent_act.sa_handler = parent_handler;
+		parent_act.sa_flags = SA_RESTART;
+		sigemptyset(&parent_act.sa_mask);
 
-}
+		if ((sigaction(SIGUSR2, &parent_act, NULL))
+		    == -1) {
+			tst_res(TWARN, "sigaction() failed in parent");
+		}
+	}
 
-/*
- * child_handler() - Signal handler for child
- */
-void child_handler(void)
-{
+	child_pid = SAFE_FORK();
 
-	if ((kill(getppid(), SIGUSR2)) == -1) {
-		tst_resm(TWARN, "kill() failed in child_handler()");
-		exit(1);
-	}
-}
+	if (child_pid != 0) {
 
-/*
- * parent_handler() - Signal handler for parent
- */
-void parent_handler(void)
-{
+		SAFE_WAITPID(child_pid, &status, 0);
 
-	got_signal = 1;
+		if (((WIFEXITED(status)) &&
+		     (WEXITSTATUS(status))) ||
+		    (got_signal == 1)) {
+			tst_res(TFAIL, "Test Failed");
+		} else {
+			if ((ptrace(PTRACE_KILL, child_pid,
+				    0, 0)) == -1) {
+				tst_res(TFAIL,
+						"Test Failed: Parent was not able to kill child");
+			}
+		}
+
+		SAFE_WAITPID(child_pid, &status, 0);
+
+		if (WIFEXITED(status))
+			tst_res(TFAIL, "Test failed");
+		else
+			tst_res(TPASS, "Test Passed");
+
+	} else
+		do_child(i);
 }
+
+static struct tst_test test = {
+	.test = run,
+	.tcnt = 2,
+	.forks_child = 1,
+};
Signature · 1 line
2.16.4
#1 lore raw

PATCH 1/1 Convert ptrace01.c to new library

Quoted 102 lines

-----Original Message-----
From: Jorik Cronenberg on Monday, September 30, 2019 9:01 PM
To: [email protected]
Subject: [LTP] [PATCH] Convert ptrace01.c to new library

Hi,

This is just a port of ptrace01.c. I wanted to make sure what I'm doing is right before porting the rest. I tried to make as little changes as possible so the test should be pretty much the same.

---
 testcases/kernel/syscalls/ptrace/ptrace01.c | 296 +++++++---------------------
 1 file changed, 72 insertions(+), 224 deletions(-)
testcases/kernel/syscalls/ptrace/ptrace01.c +6 -29
index 5a7ed5ffd..ab9ef35ee 100644
@@ -1,36 +1,13 @@
-/*
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
  * Copyright (c) Wipro Technologies Ltd, 2002.  All Rights Reserved.
  *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of version 2 of the GNU General Public License as
- * published by the Free Software Foundation.
+ * Author:	Saji Kumar.V.R <[email protected]>
  *
- * This program is distributed in the hope that it would be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ * Ported to new library:
+ * 10/2019	Jorik Cronenberg <[email protected]>
  *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- */
-
 /**********************************************************
- *
- *    TEST IDENTIFIER	: ptrace01
- *
- *    EXECUTED BY	: anyone
- *
- *    TEST TITLE	: functionality test for ptrace(2)
- *
- *    TEST CASE TOTAL	: 2
- *
- *    AUTHOR		: Saji Kumar.V.R <[email protected]>
- *
- *    SIGNALS
- * 	Uses SIGUSR1 to pause before test if option set.
- * 	(See the parse_opts(3) man page).
- *
- *    DESCRIPTION
+ * DESCRIPTION
  *	This test case tests the functionality of ptrace() for
  *	PTRACE_TRACEME & PTRACE_KILL requests.

* Here, we fork a child & the child does ptrace(PTRACE_TRACEME, ...).

testcases/kernel/syscalls/ptrace/ptrace01.c +0 -35
@@ -44,243 +21,114 @@
  * 		In both cases, child should stop & notify parent on reception
  * 		of SIGUSR2
  *
- * 	Setup:
- * 	  Setup signal handling.
- *	  Pause for SIGUSR1 if option specified.
- *
- * 	Test:
- *	 Loop if the proper options are given.
- *	 setup signal handler for SIGUSR2 signal
- *	 fork a child
- *
- *	 CHILD:
- *		setup signal handler for SIGUSR2 signal
- *		call ptrace() with PTRACE_TRACEME request
- *		send SIGUSR2 signal to self
- *	 PARENT:
- *		wait() for child.
- *		if parent is notified when child gets a signal through wait(),
- *		then
- *			do  ptrace(PTRACE_KILL, ..) on child
- * 			wait() for child to finish,
- * 			if child exited abnormaly,
- *				TEST passed
- * 			else
- * 				TEST failed
- *		else
- *			TEST failed
- *
- * 	Cleanup:
- * 	  Print errno log and/or timing stats if options given
- *
- * USAGE:  <for command-line>
- *  ptrace01 [-c n] [-e] [-i n] [-I x] [-P x] [-t] [-h] [-f] [-p]
- *			where,  -c n : Run n copies concurrently.
- *				-e   : Turn on errno logging.
- *				-h   : Show help screen
- *				-f   : Turn off functional testing

testcases/kernel/syscalls/ptrace/ptrace01.c line 26

Quoted 10 lines
testcases/kernel/syscalls/ptrace/ptrace01.c +1 -7
- *				-i n : Execute test n times.
- *				-I x : Execute test for x seconds.
- *				-p   : Pause for SIGUSR1 before starting
- *				-P x : Pause for x seconds between iterations.
- *				-t   : Turn on syscall timing.
- *
-
 **********************************************************
 ******/
+ */

It looks like the old comment had some nicely structured meta-data. I'm not familiar with the process for converting to the new library. Is it intentional to eliminate this meta-data? (This is more of a question for the LTP leaders than for Jorik).

I know that Cyril is working on a new meta-data system. If this meta-data is eliminated now, does it make adding it back into the new system harder?

Personally, I find it useful to see the pseudo-code outline when I'm debugging a test. So I hate to see those be eliminated. But maybe those are hard to trust as they tend to not be maintained along with the code?
-- Tim

Quoted 272 lines
testcases/kernel/syscalls/ptrace/ptrace01.c +3 -12
+#include <stdlib.h>
 #include <errno.h>
 #include <signal.h>
 #include <sys/wait.h>
-
 #include <config.h>
 #include "ptrace.h"
+#include "tst_test.h"
 
-#include "test.h"
-
-static void do_child(void);
-static void setup(void);
-static void cleanup(void);
-static void child_handler();
-static void parent_handler();
+static int got_signal;
 
-static int got_signal = 0;
-
-char *TCID = "ptrace01";
-static int i;			/* loop test case counter, shared with

do_child */

testcases/kernel/syscalls/ptrace/ptrace01.c +1 -80
-
-int TST_TOTAL = 2;
-
-int main(int ac, char **av)
+void child_handler(void)
 {
-
-	int lc;
-	pid_t child_pid;
-	int status;
-	struct sigaction parent_act;
-
-	tst_parse_opts(ac, av, NULL, NULL);
-#ifdef UCLINUX
-	maybe_run_child(&do_child, "d", &i);
-#endif
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-
-		tst_count = 0;
-
-		for (i = 0; i < TST_TOTAL; ++i) {
-			got_signal = 0;
-
-			/* Setup signal handler for parent */
-			if (i == 1) {
-				parent_act.sa_handler = parent_handler;
-				parent_act.sa_flags = SA_RESTART;
-				sigemptyset(&parent_act.sa_mask);
-
-				if ((sigaction(SIGUSR2, &parent_act, NULL))
-				    == -1) {
-					tst_resm(TWARN, "sigaction() failed"
-						 " in parent");
-					continue;
-				}
-			}
-
-			switch (child_pid = FORK_OR_VFORK()) {
-
-			case -1:
-				/* fork() failed */
-				tst_resm(TFAIL, "fork() failed");
-				continue;
-
-			case 0:
-				/* Child */
-#ifdef UCLINUX
-				if (self_exec(av[0], "d", i) < 0) {
-					tst_resm(TFAIL, "self_exec failed");
-					continue;
-				}
-#else
-				do_child();
-#endif
-
-			default:
-				/* Parent */
-				if ((waitpid(child_pid, &status, 0)) < 0) {
-					tst_resm(TFAIL, "waitpid() failed");
-					continue;
-				}
-
-				/*
-				 * Check the exit status of child. If (it exits
-				 * normally with exit value 1) OR (child came
-				 * through signal handler), Test Failed
-				 */
-
-				if (((WIFEXITED(status)) &&
-				     (WEXITSTATUS(status))) ||
-				    (got_signal == 1)) {
-					tst_resm(TFAIL, "Test Failed");
-					continue;
-				} else {
-					/* Kill child */
-					if ((ptrace(PTRACE_KILL, child_pid,
-						    0, 0)) == -1) {
-						tst_resm(TFAIL, "Test Failed:"
-							 " Parent was not able

to kill"

testcases/kernel/syscalls/ptrace/ptrace01.c +43 -61
-							 " child");
-						continue;
-					}
-				}
-
-				if ((waitpid(child_pid, &status, 0)) < 0) {
-					tst_resm(TFAIL, "waitpid() failed");
-					continue;
-				}
-
-				if (WIFEXITED(status)) {
-					/* Child exits normally */
-					tst_resm(TFAIL, "Test failed");
-				} else {
-					tst_resm(TPASS, "Test Passed");
-				}
-
-			}
-		}
+	if ((kill(getppid(), SIGUSR2)) == -1) {
+		tst_res(TWARN, "kill() failed in child_handler()");
+		exit(1);
 	}
+}
 
-	/* cleanup and exit */
-	cleanup();
-	tst_exit();
-
+void parent_handler(void)
+{
+	got_signal = 1;
 }
 
-/* do_child() */
-void do_child(void)
+void do_child(unsigned int i)
 {
 	struct sigaction child_act;
 
-	/* Setup signal handler for child */
-	if (i == 0) {
+	if (i == 0)
 		child_act.sa_handler = SIG_IGN;
-	} else {
+	else
 		child_act.sa_handler = child_handler;
-	}
+
 	child_act.sa_flags = SA_RESTART;
 	sigemptyset(&child_act.sa_mask);
 
 	if ((sigaction(SIGUSR2, &child_act, NULL)) == -1) {
-		tst_resm(TWARN, "sigaction() failed in child");
+		tst_res(TWARN, "sigaction() failed in child");
 		exit(1);
 	}
 
 	if ((ptrace(PTRACE_TRACEME, 0, 0, 0)) == -1) {
-		tst_resm(TWARN, "ptrace() failed in child");
+		tst_res(TWARN, "ptrace() failed in child");
 		exit(1);
 	}
-	/* ensure that child bypasses signal handler */
 	if ((kill(getpid(), SIGUSR2)) == -1) {
-		tst_resm(TWARN, "kill() failed in child");
+		tst_res(TWARN, "kill() failed in child");
 		exit(1);
 	}
 	exit(1);
 }
 
-/* setup() - performs all ONE TIME setup for this test */
-void setup(void)
+static void run(unsigned int i)
 {
 
-	tst_sig(FORK, DEF_HANDLER, cleanup);
+	pid_t child_pid;
+	int status;
+	struct sigaction parent_act;
 
-	TEST_PAUSE;
+#ifdef UCLINUX
+	maybe_run_child(&do_child, "d", &i);
+#endif
 
-}
+	got_signal = 0;
 
-/*
- *cleanup() -  performs all ONE TIME cleanup for this test at
- *		completion or premature exit.
- */
-void cleanup(void)
-{
+	if (i == 1) {
+		parent_act.sa_handler = parent_handler;
+		parent_act.sa_flags = SA_RESTART;
+		sigemptyset(&parent_act.sa_mask);
 
-}
+		if ((sigaction(SIGUSR2, &parent_act, NULL))
+		    == -1) {
+			tst_res(TWARN, "sigaction() failed in parent");
+		}
+	}
 
-/*
- * child_handler() - Signal handler for child
- */
-void child_handler(void)
-{
+	child_pid = SAFE_FORK();
 
-	if ((kill(getppid(), SIGUSR2)) == -1) {
-		tst_resm(TWARN, "kill() failed in child_handler()");
-		exit(1);
-	}
-}
+	if (child_pid != 0) {
 
-/*
- * parent_handler() - Signal handler for parent
- */
-void parent_handler(void)
-{
+		SAFE_WAITPID(child_pid, &status, 0);
 
-	got_signal = 1;
+		if (((WIFEXITED(status)) &&
+		     (WEXITSTATUS(status))) ||
+		    (got_signal == 1)) {
+			tst_res(TFAIL, "Test Failed");
+		} else {
+			if ((ptrace(PTRACE_KILL, child_pid,
+				    0, 0)) == -1) {
+				tst_res(TFAIL,
+						"Test Failed: Parent was not

able to kill child");

testcases/kernel/syscalls/ptrace/ptrace01.c +18 -1
+			}
+		}
+
+		SAFE_WAITPID(child_pid, &status, 0);
+
+		if (WIFEXITED(status))
+			tst_res(TFAIL, "Test failed");
+		else
+			tst_res(TPASS, "Test Passed");
+
+	} else
+		do_child(i);
 }
+
+static struct tst_test test = {
+	.test = run,
+	.tcnt = 2,
+	.forks_child = 1,
+};
--

2.16.4

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

#2 lore raw

PATCH 1/1 Convert ptrace01.c to new library

Hi!

Quoted 67 lines
Quoted 67 lines

/**********************************************************
- *
- * TEST IDENTIFIER : ptrace01
- *
- * EXECUTED BY : anyone
- *
- * TEST TITLE : functionality test for ptrace(2)
- *
- * TEST CASE TOTAL : 2
- *
- * AUTHOR : Saji Kumar.V.R <[email protected]>
- *
- * SIGNALS
- * Uses SIGUSR1 to pause before test if option set.
- * (See the parse_opts(3) man page).
- *
- * DESCRIPTION
+ * DESCRIPTION
* This test case tests the functionality of ptrace() for
* PTRACE_TRACEME & PTRACE_KILL requests.
* Here, we fork a child & the child does ptrace(PTRACE_TRACEME, ...).

testcases/kernel/syscalls/ptrace/ptrace01.c +0 -42
@@ -44,243 +21,114 @@
  * 		In both cases, child should stop & notify parent on reception
  * 		of SIGUSR2
  *
- * 	Setup:
- * 	  Setup signal handling.
- *	  Pause for SIGUSR1 if option specified.
- *
- * 	Test:
- *	 Loop if the proper options are given.
- *	 setup signal handler for SIGUSR2 signal
- *	 fork a child
- *
- *	 CHILD:
- *		setup signal handler for SIGUSR2 signal
- *		call ptrace() with PTRACE_TRACEME request
- *		send SIGUSR2 signal to self
- *	 PARENT:
- *		wait() for child.
- *		if parent is notified when child gets a signal through wait(),
- *		then
- *			do  ptrace(PTRACE_KILL, ..) on child
- * 			wait() for child to finish,
- * 			if child exited abnormaly,
- *				TEST passed
- * 			else
- * 				TEST failed
- *		else
- *			TEST failed
- *
- * 	Cleanup:
- * 	  Print errno log and/or timing stats if options given
- *
- * USAGE:  <for command-line>
- *  ptrace01 [-c n] [-e] [-i n] [-I x] [-P x] [-t] [-h] [-f] [-p]
- *			where,  -c n : Run n copies concurrently.
- *				-e   : Turn on errno logging.
- *				-h   : Show help screen
- *				-f   : Turn off functional testing
- *				-i n : Execute test n times.
- *				-I x : Execute test for x seconds.
- *				-p   : Pause for SIGUSR1 before starting
- *				-P x : Pause for x seconds between iterations.
- *				-t   : Turn on syscall timing.
- *
-
Quoted 8 lines

Review line 26

Quoted 3 lines
(quoted diff) +1 -0
 **********************************************************
 ******/
+ */

It looks like the old comment had some nicely structured meta-data. I'm not familiar with the process for converting to the new library. Is it intentional to eliminate this meta-data? (This is more of a question for the LTP leaders than for Jorik).

Well most of the old data is unfortunatelly garbage, for instance it lists the USAGE lists flags implemented in the test library, repeating these in each test is actually doing a harm. So we tend to keep carefuly selected subset.

I suspect that there was some kind of silly template that had to be filled up in order to get test accepted in the old days, which resulted in long and mostly useless comments on the top of the testcases.

Quoted 6 lines

I know that Cyril is working on a new meta-data system. If this meta-data is eliminated now, does it make adding it back into the new system harder?

Personally, I find it useful to see the pseudo-code outline when I'm debugging a test. So I hate to see those be eliminated. But maybe those are hard to trust as they tend to not be maintained along with the code?

Well in this case the patch actually keeps the test description, which is the same as the pseudo-code but in a paragraph of a text, so as far as I can tell it's fine.

Signature · 2 lines
Cyril Hrubis
[email protected]
#3 lore raw

PATCH 1/1 Convert ptrace01.c to new library

In reply to Jorik Cronenberg

Hi!

Quoted 1 line

This is just a port of ptrace01.c. I wanted to make sure what I'm doing is right before porting the rest. I tried to make as little changes as possible so the test should be pretty much the same.

Generally looks good, a few comments below.

Also the part before the --- should look like:

Ported ptrace01 to new library.

Quoted 1 line

---

And the informal part about making minimal changes should be here, after the ---, which is the part that is not include in the final commit.

Quoted 8 lines
 testcases/kernel/syscalls/ptrace/ptrace01.c | 296 +++++++---------------------
 1 file changed, 72 insertions(+), 224 deletions(-)
testcases/kernel/syscalls/ptrace/ptrace01.c +0 -0
index 5a7ed5ffd..ab9ef35ee 100644

testcases/kernel/syscalls/ptrace/ptrace01.c line 6

Quoted 8 lines
testcases/kernel/syscalls/ptrace/ptrace01.c +2 -4
@@ -1,36 +1,13 @@
-/*
+// SPDX-License-Identifier: GPL-2.0-or-later
+/* 
  * Copyright (c) Wipro Technologies Ltd, 2002.  All Rights Reserved.
  *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of version 2 of the GNU General Public License as
- * published by the Free Software Foundation.

Being pedantic about the licences this is GPL-2.0 only and we cannot change the original licence without consent from the author, so we have to keep the code under GPL-2.0 and not the -or-later variant.

Quoted 89 lines
testcases/kernel/syscalls/ptrace/ptrace01.c +6 -68
+ * Author:	Saji Kumar.V.R <[email protected]>
  *
- * This program is distributed in the hope that it would be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ * Ported to new library:
+ * 10/2019	Jorik Cronenberg <[email protected]>
  *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- */
-/**********************************************************
- *
- *    TEST IDENTIFIER	: ptrace01
- *
- *    EXECUTED BY	: anyone
- *
- *    TEST TITLE	: functionality test for ptrace(2)
- *
- *    TEST CASE TOTAL	: 2
- *
- *    AUTHOR		: Saji Kumar.V.R <[email protected]>
- *
- *    SIGNALS
- * 	Uses SIGUSR1 to pause before test if option set.
- * 	(See the parse_opts(3) man page).
- *
- *    DESCRIPTION
+ * DESCRIPTION
  *	This test case tests the functionality of ptrace() for
  *	PTRACE_TRACEME & PTRACE_KILL requests.
  *	Here, we fork a child & the child does ptrace(PTRACE_TRACEME, ...).
@@ -44,243 +21,114 @@
  * 		In both cases, child should stop & notify parent on reception
  * 		of SIGUSR2
  *
- * 	Setup:
- * 	  Setup signal handling.
- *	  Pause for SIGUSR1 if option specified.
- *
- * 	Test:
- *	 Loop if the proper options are given.
- *	 setup signal handler for SIGUSR2 signal
- *	 fork a child
- *
- *	 CHILD:
- *		setup signal handler for SIGUSR2 signal
- *		call ptrace() with PTRACE_TRACEME request
- *		send SIGUSR2 signal to self
- *	 PARENT:
- *		wait() for child.
- *		if parent is notified when child gets a signal through wait(),
- *		then
- *			do  ptrace(PTRACE_KILL, ..) on child
- * 			wait() for child to finish,
- * 			if child exited abnormaly,
- *				TEST passed
- * 			else
- * 				TEST failed
- *		else
- *			TEST failed
- *
- * 	Cleanup:
- * 	  Print errno log and/or timing stats if options given
- *
- * USAGE:  <for command-line>
- *  ptrace01 [-c n] [-e] [-i n] [-I x] [-P x] [-t] [-h] [-f] [-p]
- *			where,  -c n : Run n copies concurrently.
- *				-e   : Turn on errno logging.
- *				-h   : Show help screen
- *				-f   : Turn off functional testing
- *				-i n : Execute test n times.
- *				-I x : Execute test for x seconds.
- *				-p   : Pause for SIGUSR1 before starting
- *				-P x : Pause for x seconds between iterations.
- *				-t   : Turn on syscall timing.
- *
- ****************************************************************/
+ */
 
+#include <stdlib.h>
 #include <errno.h>
 #include <signal.h>
 #include <sys/wait.h>
-
 #include <config.h>
 #include "ptrace.h"

testcases/kernel/syscalls/ptrace/ptrace01.c line 34

Quoted 10 lines
testcases/kernel/syscalls/ptrace/ptrace01.c +2 -7
+#include "tst_test.h"
 
-#include "test.h"
-
-static void do_child(void);
-static void setup(void);
-static void cleanup(void);
-static void child_handler();
-static void parent_handler();
+static int got_signal;

Variables changed from signal handlers has to be declared volatile as well, otherwise the compiler may mis-optimize access to such variables.

Quoted 100 lines
testcases/kernel/syscalls/ptrace/ptrace01.c +1 -98
-static int got_signal = 0;
-
-char *TCID = "ptrace01";
-static int i;			/* loop test case counter, shared with do_child */
-
-int TST_TOTAL = 2;
-
-int main(int ac, char **av)
+void child_handler(void)
 {
-
-	int lc;
-	pid_t child_pid;
-	int status;
-	struct sigaction parent_act;
-
-	tst_parse_opts(ac, av, NULL, NULL);
-#ifdef UCLINUX
-	maybe_run_child(&do_child, "d", &i);
-#endif
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-
-		tst_count = 0;
-
-		for (i = 0; i < TST_TOTAL; ++i) {
-			got_signal = 0;
-
-			/* Setup signal handler for parent */
-			if (i == 1) {
-				parent_act.sa_handler = parent_handler;
-				parent_act.sa_flags = SA_RESTART;
-				sigemptyset(&parent_act.sa_mask);
-
-				if ((sigaction(SIGUSR2, &parent_act, NULL))
-				    == -1) {
-					tst_resm(TWARN, "sigaction() failed"
-						 " in parent");
-					continue;
-				}
-			}
-
-			switch (child_pid = FORK_OR_VFORK()) {
-
-			case -1:
-				/* fork() failed */
-				tst_resm(TFAIL, "fork() failed");
-				continue;
-
-			case 0:
-				/* Child */
-#ifdef UCLINUX
-				if (self_exec(av[0], "d", i) < 0) {
-					tst_resm(TFAIL, "self_exec failed");
-					continue;
-				}
-#else
-				do_child();
-#endif
-
-			default:
-				/* Parent */
-				if ((waitpid(child_pid, &status, 0)) < 0) {
-					tst_resm(TFAIL, "waitpid() failed");
-					continue;
-				}
-
-				/*
-				 * Check the exit status of child. If (it exits
-				 * normally with exit value 1) OR (child came
-				 * through signal handler), Test Failed
-				 */
-
-				if (((WIFEXITED(status)) &&
-				     (WEXITSTATUS(status))) ||
-				    (got_signal == 1)) {
-					tst_resm(TFAIL, "Test Failed");
-					continue;
-				} else {
-					/* Kill child */
-					if ((ptrace(PTRACE_KILL, child_pid,
-						    0, 0)) == -1) {
-						tst_resm(TFAIL, "Test Failed:"
-							 " Parent was not able to kill"
-							 " child");
-						continue;
-					}
-				}
-
-				if ((waitpid(child_pid, &status, 0)) < 0) {
-					tst_resm(TFAIL, "waitpid() failed");
-					continue;
-				}
-
-				if (WIFEXITED(status)) {
-					/* Child exits normally */
-					tst_resm(TFAIL, "Test failed");
-				} else {

testcases/kernel/syscalls/ptrace/ptrace01.c line 42

Quoted 10 lines
testcases/kernel/syscalls/ptrace/ptrace01.c +4 -5
-					tst_resm(TPASS, "Test Passed");
-				}
-
-			}
-		}
+	if ((kill(getppid(), SIGUSR2)) == -1) {
+		tst_res(TWARN, "kill() failed in child_handler()");
+		exit(1);
 	}
+}

Can we please use SAFE_MACROS() here?

E.g. SAFE_SIGACTION(), SAFE_KILL(), ...

Since with these we don't have to check the return value, which simplifies the code greatly.

testcases/kernel/syscalls/ptrace/ptrace01.c line 47

Quoted 8 lines
testcases/kernel/syscalls/ptrace/ptrace01.c +3 -4
-	/* cleanup and exit */
-	cleanup();
-	tst_exit();
-
+void parent_handler(void)
+{
+	got_signal = 1;
 }

Can we please change all the functions to static as well? I.e. the child_handler, parent_handler as well as do_child?

Quoted 42 lines
testcases/kernel/syscalls/ptrace/ptrace01.c +8 -12
-/* do_child() */
-void do_child(void)
+void do_child(unsigned int i)
 {
 	struct sigaction child_act;
 
-	/* Setup signal handler for child */
-	if (i == 0) {
+	if (i == 0)
 		child_act.sa_handler = SIG_IGN;
-	} else {
+	else
 		child_act.sa_handler = child_handler;
-	}
+
 	child_act.sa_flags = SA_RESTART;
 	sigemptyset(&child_act.sa_mask);
 
 	if ((sigaction(SIGUSR2, &child_act, NULL)) == -1) {
-		tst_resm(TWARN, "sigaction() failed in child");
+		tst_res(TWARN, "sigaction() failed in child");
 		exit(1);
 	}
 
 	if ((ptrace(PTRACE_TRACEME, 0, 0, 0)) == -1) {
-		tst_resm(TWARN, "ptrace() failed in child");
+		tst_res(TWARN, "ptrace() failed in child");
 		exit(1);
 	}
-	/* ensure that child bypasses signal handler */
 	if ((kill(getpid(), SIGUSR2)) == -1) {
-		tst_resm(TWARN, "kill() failed in child");
+		tst_res(TWARN, "kill() failed in child");
 		exit(1);
 	}
 	exit(1);
 }
 
-/* setup() - performs all ONE TIME setup for this test */
-void setup(void)
+static void run(unsigned int i)
 {

testcases/kernel/syscalls/ptrace/ptrace01.c line 86

Quoted 10 lines
testcases/kernel/syscalls/ptrace/ptrace01.c +6 -2
 
-	tst_sig(FORK, DEF_HANDLER, cleanup);
+	pid_t child_pid;
+	int status;
+	struct sigaction parent_act;
 
-	TEST_PAUSE;
+#ifdef UCLINUX
+	maybe_run_child(&do_child, "d", &i);
+#endif

We are staging out the uClinux support, feel free to delete everyting between #ifdef UCLINUX and #endif.

Quoted 7 lines
testcases/kernel/syscalls/ptrace/ptrace01.c +1 -5
-}
+	got_signal = 0;
 
-/*
- *cleanup() -  performs all ONE TIME cleanup for this test at
- *		completion or premature exit.
- */

testcases/kernel/syscalls/ptrace/ptrace01.c line 96

Quoted 10 lines
testcases/kernel/syscalls/ptrace/ptrace01.c +6 -3
-void cleanup(void)
-{
+	if (i == 1) {
+		parent_act.sa_handler = parent_handler;
+		parent_act.sa_flags = SA_RESTART;
+		sigemptyset(&parent_act.sa_mask);
 
-}
+		if ((sigaction(SIGUSR2, &parent_act, NULL))
+		    == -1) {

I guess that the original code was over 80 lines which resulted into such ugly line wrapping. Can we please keep conditions on a single line unless they are over 80 lines?

Quoted 27 lines
testcases/kernel/syscalls/ptrace/ptrace01.c +7 -16
+			tst_res(TWARN, "sigaction() failed in parent");
+		}
+	}
 
-/*
- * child_handler() - Signal handler for child
- */
-void child_handler(void)
-{
+	child_pid = SAFE_FORK();
 
-	if ((kill(getppid(), SIGUSR2)) == -1) {
-		tst_resm(TWARN, "kill() failed in child_handler()");
-		exit(1);
-	}
-}
+	if (child_pid != 0) {
 
-/*
- * parent_handler() - Signal handler for parent
- */
-void parent_handler(void)
-{
+		SAFE_WAITPID(child_pid, &status, 0);
 
-	got_signal = 1;
+		if (((WIFEXITED(status)) &&

testcases/kernel/syscalls/ptrace/ptrace01.c line 117

Quoted 10 lines
testcases/kernel/syscalls/ptrace/ptrace01.c +10 -0
+		     (WEXITSTATUS(status))) ||
+		    (got_signal == 1)) {
+			tst_res(TFAIL, "Test Failed");
+		} else {
+			if ((ptrace(PTRACE_KILL, child_pid,
+				    0, 0)) == -1) {
+				tst_res(TFAIL,
+						"Test Failed: Parent was not able to kill child");
+			}
+		}

Can with print errno here if the ptrace has failed? Hint: Use the TERRNO flag for tst_res().

Also the message could be shorter, we don't have to include the "Test Failed" in it since the test library already prints that because of the TFAIL flag, and we may as well be shorter and to the point with something as "ptrace(PTRACE_KILL, child_pid, ...) failed", which carries the same information.

testcases/kernel/syscalls/ptrace/ptrace01.c line 124

Quoted 6 lines
testcases/kernel/syscalls/ptrace/ptrace01.c +6 -0
+		SAFE_WAITPID(child_pid, &status, 0);
+
+		if (WIFEXITED(status))
+			tst_res(TFAIL, "Test failed");
+		else
+			tst_res(TPASS, "Test Passed");

We can do better in these messages, "Test failed" is nearly meaningless since that information is carried in the TFAIL flag already.

Also we should be more pedantic and pass the test only and only if it was killed. Looking at manual pages the PTRACE_KILL should send SIGKILL signal to the process being traced, so we should pass the test only and only if it was killed. Also we do have a tst_strstatus() in the test library, so that we can print what happened to the child process in case that it wasn't killed by SIGKILL.

testcases/kernel/syscalls/ptrace/ptrace01.c line 127

Quoted 2 lines
testcases/kernel/syscalls/ptrace/ptrace01.c +2 -0
+	} else
+		do_child(i);

This part would be more elegant as:

	if (!child_pid) {
		do_child();
		exit(1);
	}

	SAFE_WAITPID(...);

	...
Quoted 13 lines
testcases/kernel/syscalls/ptrace/ptrace01.c +6 -0
 }
+
+static struct tst_test test = {
+	.test = run,
+	.tcnt = 2,
+	.forks_child = 1,
+};
Signature · 5 lines
2.16.4


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 2 lines
Cyril Hrubis
[email protected]