Skip to contentSkip to conversation

Refactoring setpgid03.c test using new LTP API

PATCH Superseded Andrea Cervesato 2 participants 2 messages Feb 16, 2022

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 Refactoring setpgid03.c test using new LTP API

Removed TST_CHECKPOINT_INIT and replaced it with the .needs_checkpoints LTP test feature. Simplified source code.

---
In v2 we have more LTP API style test

 testcases/kernel/syscalls/setpgid/setpgid03.c | 165 ++++--------------
 .../kernel/syscalls/setpgid/setpgid03_child.c |  28 +--
 2 files changed, 42 insertions(+), 151 deletions(-)
testcases/kernel/syscalls/setpgid/setpgid03.c +36 -129
index 51e0eeb24..2ffa34088 100644
@@ -1,24 +1,14 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
 /*
  * Copyright (c) International Business Machines  Corp., 2001
  * Copyright (c) 2013 Oracle and/or its affiliates. All Rights Reserved.
  * Copyright (c) 2014 Cyril Hrubis <[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) 2021 SUSE LLC Andrea Cervesato <[email protected]>
  */
 
-/*
+/*\
+ * [Description]
+ *
  * Test to check the error and trivial conditions in setpgid system call
  *
  * EPERM   -  The calling process, process specified by pid and the target
@@ -28,136 +18,53 @@
  *            has performed exec()
  */
 
-#include <sys/wait.h>
-#include <limits.h>
-#include <signal.h>
-#include <errno.h>
-#include <sys/param.h>
-#include <sys/types.h>
-#include <sys/stat.h>
 #include <unistd.h>
-#include "test.h"
+#include <sys/wait.h>
+#include "tst_test.h"
 
 #define TEST_APP "setpgid03_child"
 
-char *TCID = "setpgid03";
-int TST_TOTAL = 1;
-
-static void do_child(void);
-static void setup(void);
-static void cleanup(void);
-
-int main(int ac, char **av)
+static void do_child(void)
 {
-	int child_pid;
-	int status;
-	int rval;
-	int lc;
-
-	tst_parse_opts(ac, av, NULL, NULL);
-#ifdef UCLINUX
-	maybe_run_child(&do_child, "");
-#endif
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-
-		tst_count = 0;
-
-		/* Child is in new session we are not alowed to change pgid */
-		if ((child_pid = FORK_OR_VFORK()) == -1)
-			tst_brkm(TBROK, cleanup, "fork() failed");
-
-		if (child_pid == 0) {
-#ifdef UCLINUX
-			if (self_exec(av[0], "") < 0)
-				tst_brkm(TBROK, cleanup, "self_exec failed");
-#else
-			do_child();
-#endif
-		}
-
-		TST_SAFE_CHECKPOINT_WAIT(cleanup, 0);
-		rval = setpgid(child_pid, getppid());
-		if (rval == -1 && errno == EPERM) {
-			tst_resm(TPASS, "setpgid failed with EPERM");
-		} else {
-			tst_resm(TFAIL,
-				"retval %d, errno %d, expected errno %d",
-				rval, errno, EPERM);
-		}
-		TST_SAFE_CHECKPOINT_WAKE(cleanup, 0);
-
-		if (wait(&status) < 0)
-			tst_resm(TFAIL | TERRNO, "wait() for child 1 failed");
-
-		if (!(WIFEXITED(status)) || (WEXITSTATUS(status) != 0))
-			tst_resm(TFAIL, "child 1 failed with status %d",
-				WEXITSTATUS(status));
-
-		/* Child after exec() we are no longer allowed to set pgid */
-		if ((child_pid = FORK_OR_VFORK()) == -1)
-			tst_resm(TFAIL, "Fork failed");
-
-		if (child_pid == 0) {
-			if (execlp(TEST_APP, TEST_APP, NULL) < 0)
-				perror("exec failed");
-
-			exit(127);
-		}
-
-		TST_SAFE_CHECKPOINT_WAIT(cleanup, 0);
-		rval = setpgid(child_pid, getppid());
-		if (rval == -1 && errno == EACCES) {
-			tst_resm(TPASS, "setpgid failed with EACCES");
-		} else {
-			tst_resm(TFAIL,
-				"retval %d, errno %d, expected errno %d",
-				rval, errno, EACCES);
-		}
-		TST_SAFE_CHECKPOINT_WAKE(cleanup, 0);
-
-		if (wait(&status) < 0)
-			tst_resm(TFAIL | TERRNO, "wait() for child 2 failed");
-
-		if (!(WIFEXITED(status)) || (WEXITSTATUS(status) != 0))
-			tst_resm(TFAIL, "child 2 failed with status %d",
-				WEXITSTATUS(status));
-	}
-
-	cleanup();
-	tst_exit();
+	SAFE_SETSID();
+	TST_CHECKPOINT_WAKE_AND_WAIT(0);
 }
 
-static void do_child(void)
+static void run(void)
 {
-	if (setsid() < 0) {
-		printf("CHILD: setsid() failed, errno: %d\n", errno);
-		exit(2);
+	pid_t child_pid;
+	int status;
+
+	child_pid = SAFE_FORK();
+	if (!child_pid) {
+		do_child();
+		return;
 	}
 
-	TST_SAFE_CHECKPOINT_WAKE(NULL, 0);
+	TST_CHECKPOINT_WAIT(0);
 
-	TST_SAFE_CHECKPOINT_WAIT(NULL, 0);
+	TST_EXP_FAIL(setpgid(child_pid, getppid()), EPERM);
 
-	exit(0);
-}
+	TST_CHECKPOINT_WAKE(0);
 
-static void setup(void)
-{
-	tst_sig(FORK, DEF_HANDLER, cleanup);
+	SAFE_WAIT(&status);
 
-	tst_tmpdir();
+	/* child after exec() we are no longer allowed to set pgid */
+	child_pid = SAFE_FORK();
+	if (!child_pid)
+		SAFE_EXECLP(TEST_APP, TEST_APP, NULL);
 
-	TST_CHECKPOINT_INIT(tst_rmdir);
+	TST_CHECKPOINT_WAIT(0);
 
-	umask(0);
+	TST_EXP_FAIL(setpgid(child_pid, getppid()), EACCES);
 
-	TEST_PAUSE;
-}
+	TST_CHECKPOINT_WAKE(0);
 
-static void cleanup(void)
-{
-	tst_rmdir();
+	SAFE_WAIT(&status);
 }
+
+static struct tst_test test = {
+	.test_all = run,
+	.forks_child = 1,
+	.needs_checkpoints = 1,
+};
testcases/kernel/syscalls/setpgid/setpgid03_child.c +6 -22
index 2657422a6..fdb22f24d 100644
@@ -1,32 +1,16 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
 /*
  * Copyright (c) 2013 Oracle and/or its affiliates. All Rights Reserved.
- *
- * 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 would 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 the Free Software Foundation,
- * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ * Copyright (C) 2021 SUSE LLC Andrea Cervesato <[email protected]>
  */
 
-#include "test.h"
-
-char *TCID = "setpgid03_child";
-
+#define TST_NO_DEFAULT_MAIN
+#include "tst_test.h"
 
 int main(void)
 {
-	TST_CHECKPOINT_INIT(NULL);
-
-	TST_SAFE_CHECKPOINT_WAKE(NULL, 0);
-	TST_SAFE_CHECKPOINT_WAIT(NULL, 0);
+	tst_reinit();
+	TST_CHECKPOINT_WAKE_AND_WAIT(0);
 
 	return 0;
 }
Signature · 5 lines
2.35.1


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

Hi,

I sent this patch by accident. Please, don't consider it

Quoted 256 lines · Andrea Cervesato

Removed TST_CHECKPOINT_INIT and replaced it with the .needs_checkpoints LTP test feature. Simplified source code.

---
In v2 we have more LTP API style test

  testcases/kernel/syscalls/setpgid/setpgid03.c | 165 ++++--------------
  .../kernel/syscalls/setpgid/setpgid03_child.c |  28 +--
  2 files changed, 42 insertions(+), 151 deletions(-)
testcases/kernel/syscalls/setpgid/setpgid03.c +36 -129
index 51e0eeb24..2ffa34088 100644
@@ -1,24 +1,14 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
  /*
   * Copyright (c) International Business Machines  Corp., 2001
   * Copyright (c) 2013 Oracle and/or its affiliates. All Rights Reserved.
   * Copyright (c) 2014 Cyril Hrubis<[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) 2021 SUSE LLC Andrea Cervesato<[email protected]>
   */
 
-/*
+/*\
+ * [Description]
+ *
   * Test to check the error and trivial conditions in setpgid system call
   *
   * EPERM   -  The calling process, process specified by pid and the target
@@ -28,136 +18,53 @@
   *            has performed exec()
   */
 
-#include <sys/wait.h>
-#include <limits.h>
-#include <signal.h>
-#include <errno.h>
-#include <sys/param.h>
-#include <sys/types.h>
-#include <sys/stat.h>
  #include <unistd.h>
-#include "test.h"
+#include <sys/wait.h>
+#include "tst_test.h"
 
  #define TEST_APP "setpgid03_child"
 
-char *TCID = "setpgid03";
-int TST_TOTAL = 1;
-
-static void do_child(void);
-static void setup(void);
-static void cleanup(void);
-
-int main(int ac, char **av)
+static void do_child(void)
  {
-	int child_pid;
-	int status;
-	int rval;
-	int lc;
-
-	tst_parse_opts(ac, av, NULL, NULL);
-#ifdef UCLINUX
-	maybe_run_child(&do_child, "");
-#endif
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-
-		tst_count = 0;
-
-		/* Child is in new session we are not alowed to change pgid */
-		if ((child_pid = FORK_OR_VFORK()) == -1)
-			tst_brkm(TBROK, cleanup, "fork() failed");
-
-		if (child_pid == 0) {
-#ifdef UCLINUX
-			if (self_exec(av[0], "") < 0)
-				tst_brkm(TBROK, cleanup, "self_exec failed");
-#else
-			do_child();
-#endif
-		}
-
-		TST_SAFE_CHECKPOINT_WAIT(cleanup, 0);
-		rval = setpgid(child_pid, getppid());
-		if (rval == -1 && errno == EPERM) {
-			tst_resm(TPASS, "setpgid failed with EPERM");
-		} else {
-			tst_resm(TFAIL,
-				"retval %d, errno %d, expected errno %d",
-				rval, errno, EPERM);
-		}
-		TST_SAFE_CHECKPOINT_WAKE(cleanup, 0);
-
-		if (wait(&status) < 0)
-			tst_resm(TFAIL | TERRNO, "wait() for child 1 failed");
-
-		if (!(WIFEXITED(status)) || (WEXITSTATUS(status) != 0))
-			tst_resm(TFAIL, "child 1 failed with status %d",
-				WEXITSTATUS(status));
-
-		/* Child after exec() we are no longer allowed to set pgid */
-		if ((child_pid = FORK_OR_VFORK()) == -1)
-			tst_resm(TFAIL, "Fork failed");
-
-		if (child_pid == 0) {
-			if (execlp(TEST_APP, TEST_APP, NULL) < 0)
-				perror("exec failed");
-
-			exit(127);
-		}
-
-		TST_SAFE_CHECKPOINT_WAIT(cleanup, 0);
-		rval = setpgid(child_pid, getppid());
-		if (rval == -1 && errno == EACCES) {
-			tst_resm(TPASS, "setpgid failed with EACCES");
-		} else {
-			tst_resm(TFAIL,
-				"retval %d, errno %d, expected errno %d",
-				rval, errno, EACCES);
-		}
-		TST_SAFE_CHECKPOINT_WAKE(cleanup, 0);
-
-		if (wait(&status) < 0)
-			tst_resm(TFAIL | TERRNO, "wait() for child 2 failed");
-
-		if (!(WIFEXITED(status)) || (WEXITSTATUS(status) != 0))
-			tst_resm(TFAIL, "child 2 failed with status %d",
-				WEXITSTATUS(status));
-	}
-
-	cleanup();
-	tst_exit();
+	SAFE_SETSID();
+	TST_CHECKPOINT_WAKE_AND_WAIT(0);
  }
 
-static void do_child(void)
+static void run(void)
  {
-	if (setsid() < 0) {
-		printf("CHILD: setsid() failed, errno: %d\n", errno);
-		exit(2);
+	pid_t child_pid;
+	int status;
+
+	child_pid = SAFE_FORK();
+	if (!child_pid) {
+		do_child();
+		return;
  	}
 
-	TST_SAFE_CHECKPOINT_WAKE(NULL, 0);
+	TST_CHECKPOINT_WAIT(0);
 
-	TST_SAFE_CHECKPOINT_WAIT(NULL, 0);
+	TST_EXP_FAIL(setpgid(child_pid, getppid()), EPERM);
 
-	exit(0);
-}
+	TST_CHECKPOINT_WAKE(0);
 
-static void setup(void)
-{
-	tst_sig(FORK, DEF_HANDLER, cleanup);
+	SAFE_WAIT(&status);
 
-	tst_tmpdir();
+	/* child after exec() we are no longer allowed to set pgid */
+	child_pid = SAFE_FORK();
+	if (!child_pid)
+		SAFE_EXECLP(TEST_APP, TEST_APP, NULL);
 
-	TST_CHECKPOINT_INIT(tst_rmdir);
+	TST_CHECKPOINT_WAIT(0);
 
-	umask(0);
+	TST_EXP_FAIL(setpgid(child_pid, getppid()), EACCES);
 
-	TEST_PAUSE;
-}
+	TST_CHECKPOINT_WAKE(0);
 
-static void cleanup(void)
-{
-	tst_rmdir();
+	SAFE_WAIT(&status);
  }
+
+static struct tst_test test = {
+	.test_all = run,
+	.forks_child = 1,
+	.needs_checkpoints = 1,
+};
testcases/kernel/syscalls/setpgid/setpgid03_child.c +6 -22
index 2657422a6..fdb22f24d 100644
@@ -1,32 +1,16 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
  /*
   * Copyright (c) 2013 Oracle and/or its affiliates. All Rights Reserved.
- *
- * 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 would 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 the Free Software Foundation,
- * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ * Copyright (C) 2021 SUSE LLC Andrea Cervesato<[email protected]>
   */
 
-#include "test.h"
-
-char *TCID = "setpgid03_child";
-
+#define TST_NO_DEFAULT_MAIN
+#include "tst_test.h"
 
  int main(void)
  {
-	TST_CHECKPOINT_INIT(NULL);
-
-	TST_SAFE_CHECKPOINT_WAKE(NULL, 0);
-	TST_SAFE_CHECKPOINT_WAIT(NULL, 0);
+	tst_reinit();
+	TST_CHECKPOINT_WAKE_AND_WAIT(0);
 
  	return 0;
  }
Signature · 2 lines
-- 
Mailing list info: https://lists.linux.it/listinfo/ltp