Skip to contentSkip to conversation

Refactor tests which are verifying fork/vfork functionalities

PATCH Superseded Andrea Cervesato 4 participants 9 messages Jan 27 – Mar 4

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover Refactor tests which are verifying fork/vfork functionalities

---
Andrea Cervesato (3):
syscalls: refactor fork09 using new API
syscalls: refactor vfork01 using new API
syscalls: refactor vfork02 using new API

 testcases/kernel/syscalls/fork/fork09.c   | 218 ++++++-----------
 testcases/kernel/syscalls/vfork/vfork01.c | 374 ++++--------------------------
 testcases/kernel/syscalls/vfork/vfork02.c | 230 ++----------------
 3 files changed, 137 insertions(+), 685 deletions(-)

---
base-commit: c47ab95d5fe077133e598ac7762387450a87cb2b
change-id: 20260126-fork_refactoring-8067f844f8b6

Best regards,

Signature · 5 lines
Andrea Cervesato <[email protected]>


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

PATCH 1/3 syscalls: refactor fork09 using new API

From: Andrea Cervesato <[email protected]>

---
 testcases/kernel/syscalls/fork/fork09.c | 218 ++++++++++----------------------
 1 file changed, 69 insertions(+), 149 deletions(-)
testcases/kernel/syscalls/fork/fork09.c +69 -149
index 32bad89b35575f1597bed1a44ef3b984bd1db3bd..7ce64b9a1188fe81e1d44d8ea5cb33a2504b8cfd 100644
@@ -1,172 +1,92 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
 /*
- *   Copyright (c) International Business Machines  Corp., 2001
- *
- *   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
- *
- * NAME
- *	fork09.c
- *
- * DESCRIPTION
- *	Check that child has access to a full set of files.
- *
- * ALGORITHM
- *	Parent opens a maximum number of files
- *	Child closes one and attempts to open another, it should be
- *	available
- *
- * USAGE
- *	fork09
- *
- * HISTORY
- *	07/2001 Ported by Wayne Boyer
- *
- *	10/2008 Suzuki K P <[email protected]>
- *		Fix maximum number of files open logic.
- *
- * RESTRICTIONS
- *	None
+ * Copyright (C) 2026 SUSE LLC Andrea Cervesato <[email protected]>
  */
 
-#include <sys/types.h>
-#include <sys/wait.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <stdio.h>
-#include <errno.h>
-#include <unistd.h>		/* for _SC_OPEN_MAX */
-#include "test.h"
-#include "safe_macros.h"
-
-char *TCID = "fork09";
-int TST_TOTAL = 1;
+/*\
+ * Verify that a forked child can close all the files which have been open by
+ * the parent process.
+ */
 
-static void setup(void);
-static void cleanup(void);
+#include "tst_test.h"
+#include "tst_safe_stdio.h"
 
-static char filname[40], childfile[40];
-static int first;
-static FILE **fildeses;		/* file streams */
-static int mypid, nfiles;
+#define FILE_PREFIX "ltp_file"
 
-#define OPEN_MAX (sysconf(_SC_OPEN_MAX))
+static FILE **open_files;
+static long file_open_max;
 
-int main(int ac, char **av)
+static void run(void)
 {
-	int pid, status, nf;
-
-	int lc;
-
-	tst_parse_opts(ac, av, NULL, NULL);
-
-	setup();
-
-	fildeses = malloc((OPEN_MAX + 10) * sizeof(FILE *));
-	if (fildeses == NULL)
-		tst_brkm(TBROK, cleanup, "malloc failed");
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
-		mypid = getpid();
-
-		tst_resm(TINFO, "OPEN_MAX is %ld", OPEN_MAX);
-
-		/* establish first free file */
-		sprintf(filname, "fork09.%d", mypid);
-		first = SAFE_CREAT(cleanup, filname, 0660);
-		close(first);
-
-		tst_resm(TINFO, "first file descriptor is %d ", first);
-
-		SAFE_UNLINK(cleanup, filname);
-
-		/*
-		 * now open all the files for the test
-		 */
-		for (nfiles = first; nfiles < OPEN_MAX; nfiles++) {
-			sprintf(filname, "file%d.%d", nfiles, mypid);
-			fildeses[nfiles] = fopen(filname, "a");
-			if (fildeses[nfiles] == NULL) {
-				/* Did we already reach OPEN_MAX ? */
-				if (errno == EMFILE)
-					break;
-				tst_brkm(TBROK, cleanup, "Parent: cannot open "
-					 "file %d %s errno = %d", nfiles,
-					 filname, errno);
-			}
-#ifdef DEBUG
-			tst_resm(TINFO, "filname: %s", filname);
-#endif
-		}
+	FILE *f;
+	long nfiles;
+	long totfiles;
+	char name[PATH_MAX];
 
-		tst_resm(TINFO, "Parent reporting %d files open", nfiles - 1);
-
-		pid = fork();
-		if (pid == -1)
-			tst_brkm(TBROK, cleanup, "Fork failed");
-
-		if (pid == 0) {	/* child */
-			nfiles--;
-			if (fclose(fildeses[nfiles]) == -1) {
-				tst_resm(TINFO, "Child could not close file "
-					 "#%d, errno = %d", nfiles, errno);
-				exit(1);
-			} else {
-				sprintf(childfile, "cfile.%d", getpid());
-				fildeses[nfiles] = fopen(childfile, "a");
-				if (fildeses[nfiles] == NULL) {
-					tst_resm(TINFO, "Child could not open "
-						 "file %s, errno = %d",
-						 childfile, errno);
-					exit(1);
-				} else {
-					tst_resm(TINFO, "Child opened new "
-						 "file #%d", nfiles);
-					unlink(childfile);
-					exit(0);
-				}
-			}
-		} else {	/* parent */
-			wait(&status);
-			if (status >> 8 != 0)
-				tst_resm(TFAIL, "test 1 FAILED");
-			else
-				tst_resm(TPASS, "test 1 PASSED");
-		}
+	tst_res(TINFO, "Opening files from parent");
+
+	for (nfiles = 0; nfiles < file_open_max; nfiles++) {
+		memset(name, 0, PATH_MAX);
+		snprintf(name, PATH_MAX, "%s%lu", FILE_PREFIX, nfiles);
 
-		/* clean up things in case we are looping */
-		for (nf = first; nf < nfiles; nf++) {
-			fclose(fildeses[nf]);
-			sprintf(filname, "file%d.%d", nf, mypid);
-			unlink(filname);
+		f = fopen(name, "a");
+		if (!f) {
+			if (errno == EMFILE)
+				break;
+
+			tst_brk(TBROK | TERRNO, "fopen() error");
 		}
+
+		open_files[nfiles] = f;
+	}
+
+	totfiles = nfiles;
+
+	if (!totfiles)
+		tst_brk(TBROK, "Parent couldn't open any file");
+
+	tst_res(TINFO, "Closing %lu files from child", totfiles);
+
+	if (!SAFE_FORK()) {
+		for (nfiles = nfiles - 1; nfiles >= 0; nfiles--)
+			SAFE_FCLOSE(open_files[nfiles]);
+
+		exit(0);
 	}
 
-	cleanup();
-	tst_exit();
+	tst_reap_children();
+
+	tst_res(TPASS, "Child closed all parent's files");
+
+	for (nfiles = 0; nfiles < totfiles; nfiles++) {
+		memset(name, 0, PATH_MAX);
+		snprintf(name, PATH_MAX, "%s%lu", FILE_PREFIX, nfiles);
+
+		SAFE_FCLOSE(open_files[nfiles]);
+		SAFE_UNLINK(name);
+	}
 }
 
 static void setup(void)
 {
-	tst_sig(FORK, DEF_HANDLER, cleanup);
-	umask(0);
+	file_open_max = sysconf(_SC_OPEN_MAX);
 
-	TEST_PAUSE;
-	tst_tmpdir();
+	open_files = SAFE_MMAP(NULL, sizeof(FILE *) * file_open_max,
+			PROT_READ | PROT_WRITE,
+			MAP_ANONYMOUS | MAP_SHARED,
+			-1, 0);
 }
 
 static void cleanup(void)
 {
-	tst_rmdir();
+	if (open_files)
+		SAFE_MUNMAP(open_files, sizeof(FILE *) * file_open_max);
 }
+
+static struct tst_test test = {
+	.test_all = run,
+	.setup = setup,
+	.cleanup = cleanup,
+	.forks_child = 1,
+	.needs_tmpdir = 1,
+};
Signature · 5 lines
2.51.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by on v1 1/3
#2 lore raw

Re: [LTP] [PATCH 1/3] syscalls: refactor fork09 using new API

Hi Andrea,

Generally LGTM.

testcases/kernel/syscalls/fork/fork09.c line 33

Quoted 13 lines
testcases/kernel/syscalls/fork/fork09.c +7 -5
+	tst_res(TINFO, "Opening files from parent");
+
+	for (nfiles = 0; nfiles < file_open_max; nfiles++) {
+		memset(name, 0, PATH_MAX);
+		snprintf(name, PATH_MAX, "%s%lu", FILE_PREFIX, nfiles);
 
-		/* clean up things in case we are looping */
-		for (nf = first; nf < nfiles; nf++) {
-			fclose(fildeses[nf]);
-			sprintf(filname, "file%d.%d", nf, mypid);
-			unlink(filname);
+		f = fopen(name, "a");
+		if (!f) {

nit: Although EMFILE meaning is quite obvious I found previous comment about OPEN_MAX useful.

testcases/kernel/syscalls/fork/fork09.c line 37

Quoted 4 lines
testcases/kernel/syscalls/fork/fork09.c +4 -0
+			if (errno == EMFILE)
+				break;
+
+			tst_brk(TBROK | TERRNO, "fopen() error");

I suppose we are ok to not close these open files even if it fails close to OPEN_MAX...

Quoted 13 lines
testcases/kernel/syscalls/fork/fork09.c +12 -0
 		}
+
+		open_files[nfiles] = f;
+	}
+
+	totfiles = nfiles;
+
+	if (!totfiles)
+		tst_brk(TBROK, "Parent couldn't open any file");
+
+	tst_res(TINFO, "Closing %lu files from child", totfiles);
+
+	if (!SAFE_FORK()) {

testcases/kernel/syscalls/fork/fork09.c line 60

Quoted 10 lines
testcases/kernel/syscalls/fork/fork09.c +8 -0
+		for (nfiles = nfiles - 1; nfiles >= 0; nfiles--)
+			SAFE_FCLOSE(open_files[nfiles]);
+
+		exit(0);
 	}
 
+	tst_reap_children();
+
+	tst_res(TPASS, "Child closed all parent's files");
+

... because this is not in a cleanup.

testcases/kernel/syscalls/fork/fork09.c line 67

Quoted 7 lines
testcases/kernel/syscalls/fork/fork09.c +7 -0
+	for (nfiles = 0; nfiles < totfiles; nfiles++) {
+		memset(name, 0, PATH_MAX);
+		snprintf(name, PATH_MAX, "%s%lu", FILE_PREFIX, nfiles);
+
+		SAFE_FCLOSE(open_files[nfiles]);
+		SAFE_UNLINK(name);
+	}

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#3 lore raw

Re: [LTP] [PATCH 1/3] syscalls: refactor fork09 using new API

In reply to Andrea Cervesato

Hi!

Quoted 3 lines

+// SPDX-License-Identifier: GPL-2.0-or-later
/*
- * Copyright (c) International Business Machines Corp., 2001

Unless it's a rewritten from scratch this copyright should stay.

Quoted 211 lines
- *   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

- *
- * NAME
- * fork09.c
- *
- * DESCRIPTION
- * Check that child has access to a full set of files.
- *
- * ALGORITHM
- * Parent opens a maximum number of files
- * Child closes one and attempts to open another, it should be
- * available
- *
- * USAGE
- * fork09
- *
- * HISTORY
- * 07/2001 Ported by Wayne Boyer
- *
- * 10/2008 Suzuki K P <[email protected]>
- * Fix maximum number of files open logic.
- *
- * RESTRICTIONS
- * None
+ * Copyright (C) 2026 SUSE LLC Andrea Cervesato <[email protected]> */

testcases/kernel/syscalls/fork/fork09.c +52 -106
-#include <sys/types.h>
-#include <sys/wait.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <stdio.h>
-#include <errno.h>
-#include <unistd.h>		/* for _SC_OPEN_MAX */
-#include "test.h"
-#include "safe_macros.h"
-
-char *TCID = "fork09";
-int TST_TOTAL = 1;
+/*\
+ * Verify that a forked child can close all the files which have been open by
+ * the parent process.
+ */
 
-static void setup(void);
-static void cleanup(void);
+#include "tst_test.h"
+#include "tst_safe_stdio.h"
 
-static char filname[40], childfile[40];
-static int first;
-static FILE **fildeses;		/* file streams */
-static int mypid, nfiles;
+#define FILE_PREFIX "ltp_file"
 
-#define OPEN_MAX (sysconf(_SC_OPEN_MAX))
+static FILE **open_files;
+static long file_open_max;
 
-int main(int ac, char **av)
+static void run(void)
 {
-	int pid, status, nf;
-
-	int lc;
-
-	tst_parse_opts(ac, av, NULL, NULL);
-
-	setup();
-
-	fildeses = malloc((OPEN_MAX + 10) * sizeof(FILE *));
-	if (fildeses == NULL)
-		tst_brkm(TBROK, cleanup, "malloc failed");
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
-		mypid = getpid();
-
-		tst_resm(TINFO, "OPEN_MAX is %ld", OPEN_MAX);
-
-		/* establish first free file */
-		sprintf(filname, "fork09.%d", mypid);
-		first = SAFE_CREAT(cleanup, filname, 0660);
-		close(first);
-
-		tst_resm(TINFO, "first file descriptor is %d ", first);
-
-		SAFE_UNLINK(cleanup, filname);
-
-		/*
-		 * now open all the files for the test
-		 */
-		for (nfiles = first; nfiles < OPEN_MAX; nfiles++) {
-			sprintf(filname, "file%d.%d", nfiles, mypid);
-			fildeses[nfiles] = fopen(filname, "a");
-			if (fildeses[nfiles] == NULL) {
-				/* Did we already reach OPEN_MAX ? */
-				if (errno == EMFILE)
-					break;
-				tst_brkm(TBROK, cleanup, "Parent: cannot open "
-					 "file %d %s errno = %d", nfiles,
-					 filname, errno);
-			}
-#ifdef DEBUG
-			tst_resm(TINFO, "filname: %s", filname);
-#endif
-		}
+	FILE *f;
+	long nfiles;
+	long totfiles;
+	char name[PATH_MAX];
 
-		tst_resm(TINFO, "Parent reporting %d files open", nfiles - 1);
-
-		pid = fork();
-		if (pid == -1)
-			tst_brkm(TBROK, cleanup, "Fork failed");
-
-		if (pid == 0) {	/* child */
-			nfiles--;
-			if (fclose(fildeses[nfiles]) == -1) {
-				tst_resm(TINFO, "Child could not close file "
-					 "#%d, errno = %d", nfiles, errno);
-				exit(1);
-			} else {
-				sprintf(childfile, "cfile.%d", getpid());
-				fildeses[nfiles] = fopen(childfile, "a");
-				if (fildeses[nfiles] == NULL) {
-					tst_resm(TINFO, "Child could not open "
-						 "file %s, errno = %d",
-						 childfile, errno);
-					exit(1);
-				} else {
-					tst_resm(TINFO, "Child opened new "
-						 "file #%d", nfiles);
-					unlink(childfile);
-					exit(0);
-				}
-			}
-		} else {	/* parent */
-			wait(&status);
-			if (status >> 8 != 0)
-				tst_resm(TFAIL, "test 1 FAILED");
-			else
-				tst_resm(TPASS, "test 1 PASSED");
-		}
+	tst_res(TINFO, "Opening files from parent");
+
+	for (nfiles = 0; nfiles < file_open_max; nfiles++) {
+		memset(name, 0, PATH_MAX);
+		snprintf(name, PATH_MAX, "%s%lu", FILE_PREFIX, nfiles);
 
-		/* clean up things in case we are looping */
-		for (nf = first; nf < nfiles; nf++) {
-			fclose(fildeses[nf]);
-			sprintf(filname, "file%d.%d", nf, mypid);
-			unlink(filname);
+		f = fopen(name, "a");
+		if (!f) {
+			if (errno == EMFILE)
+				break;
+
+			tst_brk(TBROK | TERRNO, "fopen() error");
 		}
+
+		open_files[nfiles] = f;
+	}
+
+	totfiles = nfiles;
+
+	if (!totfiles)
+		tst_brk(TBROK, "Parent couldn't open any file");
+
+	tst_res(TINFO, "Closing %lu files from child", totfiles);
+
+	if (!SAFE_FORK()) {
+		for (nfiles = nfiles - 1; nfiles >= 0; nfiles--)
+			SAFE_FCLOSE(open_files[nfiles]);
+
+		exit(0);
 	}
 
-	cleanup();
-	tst_exit();
+	tst_reap_children();
+
+	tst_res(TPASS, "Child closed all parent's files");
+
+	for (nfiles = 0; nfiles < totfiles; nfiles++) {
+		memset(name, 0, PATH_MAX);
+		snprintf(name, PATH_MAX, "%s%lu", FILE_PREFIX, nfiles);
+
+		SAFE_FCLOSE(open_files[nfiles]);
+		SAFE_UNLINK(name);
+	}
 }
 
 static void setup(void)
 {

testcases/kernel/syscalls/fork/fork09.c line 77

Quoted 10 lines
testcases/kernel/syscalls/fork/fork09.c +5 -4
-	tst_sig(FORK, DEF_HANDLER, cleanup);
-	umask(0);
+	file_open_max = sysconf(_SC_OPEN_MAX);
 
-	TEST_PAUSE;
-	tst_tmpdir();
+	open_files = SAFE_MMAP(NULL, sizeof(FILE *) * file_open_max,
+			PROT_READ | PROT_WRITE,
+			MAP_ANONYMOUS | MAP_SHARED,
+			-1, 0);

Do we need to put this into a shared memory? We do not modify that in the child process so there is no reason for changes to be propagated to the parent...

Signature · 5 lines
Cyril Hrubis
[email protected]

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

PATCH 2/3 syscalls: refactor vfork01 using new API

In reply to Andrea Cervesato

From: Andrea Cervesato <[email protected]>

---
 testcases/kernel/syscalls/vfork/vfork01.c | 374 ++++--------------------------
 1 file changed, 45 insertions(+), 329 deletions(-)
testcases/kernel/syscalls/vfork/vfork01.c +45 -329
index b050776b7982e568f59b31547a6ebf72a1dba880..a681abe37b8cac7b6fdee4e2387db12cd548c698 100644
@@ -1,352 +1,68 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
 /*
- *
- *   Copyright (c) International Business Machines  Corp., 2001
- *
- *   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) 2026 SUSE LLC Andrea Cervesato <[email protected]>
  */
 
-/*
- * Name: vfork01
- *
- * Test Description:
- *  Fork a process using vfork() and verify that, the attribute values like
- *  euid, ruid, suid, egid, rgid, sgid, umask, inode and device number of
- *  root and current working directories are same as that of the parent
- *  process.
- * $
- * Expected Result:
- *  The attribute values like euid, ruid, suid, egid, rgid, sgid, umask, inode
- *  and device number of root and current working directory of the parent and
- *  child processes should be equal.
- *
- * Algorithm:
- *  Setup:
- *   Setup signal handling.
- *   Pause for SIGUSR1 if option specified.
- *
- *  Test:
- *   Loop if the proper options are given.
- *   Execute system call
- *   Check return code, if system call failed (return=-1)
- *   	Log the errno and Issue a FAIL message.
- *   Otherwise,
- *   	Verify the Functionality of system call
- *      if successful,
- *      	Issue Functionality-Pass message.
- *      Otherwise,
- *		Issue Functionality-Fail message.
- *  Cleanup:
- *   Print errno log and/or timing stats if options given
- *
- * Usage:  <for command-line>
- *  vfork01 [-c n] [-e] [-f] [-i n] [-I x] [-p x] [-t]
- *	where,	-c n : Run n copies concurrently.
- *		-e   : Turn on errno logging.
- *		-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 John George
- *		-Ported
- *
- * Restrictions:
- *  None.
- *
+/*\
+ * Fork a process using `vfork()` and verify that the attribute values like
+ * euid, ruid, suid, egid, rgid, sgid, umask, inode and device number of
+ * root and current working directories are the same of the parent
+ * process ones.
  */
 
-#define _GNU_SOURCE 1
-#include <stdio.h>
-#include <sys/types.h>
-#include <errno.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <string.h>
-#include <signal.h>
-#include <unistd.h>
-#include <sys/stat.h>
-#include <sys/wait.h>
-
-#include "test.h"
-
-char *TCID = "vfork01";
-int TST_TOTAL = 1;
-
-/* Variables to hold parent/child eff/real/saved uid/gid values */
-uid_t Peuid, Ceuid, Csuid, Psuid, Pruid, Cruid;
-gid_t Pegid, Cegid, Psgid, Csgid, Prgid, Crgid;
-mode_t Pumask, Cumask;
-
-char *Pcwd, *Ccwd;		/*
-				 * pathname of working directory of
-				 * child/parent process.
-				 */
-/* stat structure to hold directory/inode information for parent/child */
-struct stat StatPbuf;
-struct stat StatCbuf;
-struct stat Stat_cwd_Pbuf;
-struct stat Stat_cwd_Cbuf;
+#include "tst_test.h"
+#include "tst_uid.h"
 
-void setup();			/* Main setup function of test */
-void cleanup();			/* cleanup function for the test */
-
-int main(int ac, char **av)
+static void run(void)
 {
-	int lc;
-	pid_t cpid;		/* process id of the child process */
-	int exit_status;	/* exit status of child process */
-
-	tst_parse_opts(ac, av, NULL, NULL);
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
+	char *p_cwd, *c_cwd;
+	mode_t p_mask, c_mask;
+	static uid_t p_ruid, p_euid, p_suid;
+	static gid_t p_rgid, p_egid, p_sgid;
+	struct stat p_cwd_stat, c_cwd_stat;
+	struct stat p_root_stat, c_root_stat;
 
-		tst_count = 0;
+	p_mask = umask(0);
+	umask(p_mask);
 
-		/*
-		 * Call vfork(2) to create a child process without
-		 * fully copying the address space of parent.
-		 */
-		TEST(vfork());
+	p_cwd = getcwd(NULL, BUFSIZ);
 
-		if ((cpid = TEST_RETURN) == -1) {
-			tst_resm(TFAIL, "vfork() Failed, errno=%d : %s",
-				 TEST_ERRNO, strerror(TEST_ERRNO));
-		} else if (cpid == 0) {	/* Child process */
-			/*
-			 * Get the euid, ruid, egid, rgid, umask value
-			 * and the current working directory of the
-			 * child process
-			 */
-			if (getresuid(&Cruid, &Ceuid, &Csuid) < 0) {
-				tst_resm(TFAIL, "getresuid() fails to "
-					 "get real/eff./saved uid of "
-					 "child process");
-				_exit(1);
-			}
+	SAFE_GETRESUID(&p_ruid, &p_euid, &p_suid);
+	SAFE_GETRESGID(&p_rgid, &p_egid, &p_sgid);
 
-			if (getresgid(&Crgid, &Cegid, &Csgid) < 0) {
-				tst_resm(TFAIL, "getresgid() fails to "
-					 "get real/eff./saved gid of "
-					 "child process");
-				_exit(1);
-			}
+	SAFE_STAT(p_cwd, &p_cwd_stat);
+	SAFE_STAT("/", &p_root_stat);
 
-			/*
-			 * Get the file mode creation mask value of
-			 * child process by setting value zero and
-			 * restore the previous mask value.
-			 */
-			Cumask = umask(0);
+	if (!vfork()) {
+		c_mask = umask(0);
+		umask(c_mask);
 
-			/*
-			 * Restore the process mask of child to
-			 * previous value.
-			 */
-			umask(Cumask);
+		TST_EXP_EQ_LI(p_mask, c_mask);
 
-			/*
-			 * Get the pathname of current working
-			 * directory for the child process.
-			 */
-			if ((Ccwd = (char *)getcwd(NULL,
-						   BUFSIZ)) == NULL) {
-				tst_resm(TFAIL, "getcwd failed for the "
-					 "child process");
-				_exit(1);
-			}
+		c_cwd = getcwd(NULL, BUFSIZ);
+		SAFE_STAT(c_cwd, &c_cwd_stat);
 
-			/*
-			 * Get the device number and the inode
-			 * number of "/" directory for the child
-			 * process.
-			 */
-			if (stat("/", &StatCbuf) < 0) {
-				tst_resm(TFAIL, "stat(2) failed to get "
-					 "info. of'/' in the child "
-					 "process");
-				_exit(1);
-			}
+		TST_EXP_EQ_STR(p_cwd, c_cwd);
+		TST_EXP_EQ_LI(p_cwd_stat.st_ino, c_cwd_stat.st_ino);
+		TST_EXP_EQ_LI(p_cwd_stat.st_dev, c_cwd_stat.st_dev);
 
-			/*
-			 * Get the device/inode number of "."
-			 * (working directory) for the child process.
-			 */
-			if (stat(Ccwd, &Stat_cwd_Cbuf) < 0) {
-				tst_resm(TFAIL, "stat(2) failed to get "
-					 "info. of working irectory in "
-					 "the child");
-				_exit(1);
-			}
+		SAFE_STAT("/", &c_root_stat);
 
-			/* Now, do the actual comparision */
-			if (Peuid != Ceuid || Pegid != Cegid ||
-			    Psuid != Csuid || Psgid != Csgid ||
-			    Pruid != Cruid || Prgid != Crgid ||
-			    Pumask != Cumask) {
-				tst_resm(TFAIL, "Attribute values of "
-					 "parent and child don't match");
-				_exit(1);
-			} else {
-				tst_resm(TINFO, "Attribute values of "
-					 "parent and child match");
-			}
+		TST_EXP_EQ_LI(p_root_stat.st_ino, c_root_stat.st_ino);
+		TST_EXP_EQ_LI(p_root_stat.st_dev, c_root_stat.st_dev);
 
-			/* Check for the same working directories */
-			if (strcmp(Pcwd, Ccwd) != 0) {
-				tst_resm(TFAIL, "Working directories "
-					 "of parent and child don't "
-					 "match");
-				_exit(1);
-			} else {
-				tst_resm(TINFO, "Working directories "
-					 "of parent and child match");
-			}
+		if (tst_check_resuid("resuid()", p_ruid, p_euid, p_suid))
+			tst_res(TPASS, "Parent and child UID are matching");
 
-			/*
-			 * Check for the same device/inode number of
-			 * '/' directory.
-			 */
-			if ((StatPbuf.st_ino != StatCbuf.st_ino) ||
-			    (StatPbuf.st_dev != StatCbuf.st_dev)) {
-				tst_resm(TFAIL, "Device/inode number "
-					 "of parent and childs '/' "
-					 " don't match");
-				_exit(1);
-			} else {
-				tst_resm(TINFO, "Device/inode number "
-					 "of parent and childs '/' "
-					 "match");
-			}
+		if (tst_check_resgid("resgid()", p_rgid, p_egid, p_sgid))
+			tst_res(TPASS, "Parent and child GID are matching");
 
-			/*
-			 * Check for the same device and inode number
-			 *  of "." (current working directory.
-			 */
-			if ((Stat_cwd_Pbuf.st_ino !=
-			     Stat_cwd_Cbuf.st_ino) ||
-			    (Stat_cwd_Pbuf.st_dev !=
-			     Stat_cwd_Cbuf.st_dev)) {
-				tst_resm(TFAIL, "Device/inode number "
-					 "of parent and childs '.' "
-					 "don't match");
-				_exit(1);
-			} else {
-				tst_resm(TINFO, "Device/inode number "
-					 "of parent and childs '.' "
-					 "don't match");
-			}
-
-			/*
-			 * Exit with normal exit code if everything
-			 * fine
-			 */
-			_exit(0);
-
-		} else {	/* parent process */
-			/*
-			 * Let the parent process wait till child completes
-			 * its execution.
-			 */
-			wait(&exit_status);
-
-			/* Check for the exit status of child process */
-			if (WEXITSTATUS(exit_status) == 0) {
-				tst_resm(TPASS, "Call of vfork() successful");
-			} else if (WEXITSTATUS(exit_status) == 1) {
-				tst_resm(TFAIL,
-					 "Child process exited abnormally");
-			}
-		}
-		tst_count++;	/* incr. TEST_LOOP counter */
+		exit(0);
 	}
-
-	cleanup();
-	tst_exit();
 }
 
-/*
- * void
- * setup() - performs all ONE TIME setup for this test.
- *  This function gets real/effective/saved uid/gid, umask, the device/inode
- *  number of '/' and current working directory for the parent process.
- */
-void setup(void)
-{
-
-	tst_sig(FORK, DEF_HANDLER, cleanup);
-
-	TEST_PAUSE;
-
-	/*
-	 * Get the euid, ruid, egid, rgid, umask value
-	 * and the current working directory of the parent process.
-	 */
-	if (getresuid(&Pruid, &Peuid, &Psuid) < 0) {
-		tst_brkm(TFAIL, cleanup, "getresuid() fails to get "
-			 "real/eff./saved uid of parent");
-	}
-
-	if (getresgid(&Prgid, &Pegid, &Psgid) < 0) {
-		tst_brkm(TFAIL, cleanup, "getresgid() fails to get "
-			 "real/eff./saved gid of parent");
-	}
-
-	/* Get the process file mode creation mask by setting value 0 */
-	Pumask = umask(0);
-	umask(Pumask);		/*
-				 * Restore the mask value of the
-				 * process.
-				 */
-	/*
-	 * Get the pathname of current working directory of the parent
-	 * process.
-	 */
-	if ((Pcwd = (char *)getcwd(NULL, BUFSIZ)) == NULL) {
-		tst_brkm(TFAIL, cleanup,
-			 "getcwd failed for the parent process");
-	}
-
-	/*
-	 * Get the device and inode number of root directory for the
-	 * parent process.
-	 */
-	if (stat("/", &StatPbuf) == -1) {
-		tst_brkm(TFAIL, cleanup, "stat(2) failed to get info. of '/' "
-			 "in parent process");
-	}
-
-	/*
-	 * Get the device number and the inode number of "." (current-
-	 * working directory) for the parent process.
-	 */
-	if (stat(Pcwd, &Stat_cwd_Pbuf) < 0) {
-		tst_brkm(TFAIL, cleanup, "stat(2) failed to get info. of "
-			 "working directory in parent process");
-	}
-}
-
-/*
- * void
- * cleanup() - performs all ONE TIME cleanup for this test at
- *             completion or premature exit.
- */
-void cleanup(void)
-{
-
-}
+static struct tst_test test = {
+	.test_all = run,
+	.forks_child = 1,
+};
Signature · 5 lines
2.51.0


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

PATCH 3/3 syscalls: refactor vfork02 using new API

In reply to Andrea Cervesato

From: Andrea Cervesato <[email protected]>

Replace the deprecated sigrelse/sighold usage and replace them with sigaction.

---
 testcases/kernel/syscalls/vfork/vfork02.c | 230 +++---------------------------
 1 file changed, 23 insertions(+), 207 deletions(-)
testcases/kernel/syscalls/vfork/vfork02.c +23 -207
index efa70d0bef325a77e345448fc88d3fb721a8d39d..507666c5f2c59f2d74f2efcd4c0e088a9818ed05 100644
@@ -1,229 +1,45 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
 /*
- *
- *   Copyright (c) International Business Machines  Corp., 2001
- *
- *   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) 2026 SUSE LLC Andrea Cervesato <[email protected]>
  */
 
-/*
- * Test Name: vfork02
- *
- * Test Description:
+/*\
  *  Fork a process using vfork() and verify that, the pending signals in
  *  the parent are not pending in the child process.
- * $
- * Expected Result:
- *  The signal which is pending in the parent should not be pending in the
- *  child process.
- *
- * Algorithm:
- *  Setup:
- *   Setup signal handling.
- *   Pause for SIGUSR1 if option specified.
- *
- *  Test:
- *   Loop if the proper options are given.
- *   Execute system call
- *   Check return code, if system call failed (return=-1)
- *   	Log the errno and Issue a FAIL message.
- *   Otherwise,
- *   	Verify the Functionality of system call
- *      if successful,
- *      	Issue Functionality-Pass message.
- *      Otherwise,
- *		Issue Functionality-Fail message.
- *  Cleanup:
- *   Print errno log and/or timing stats if options given
- *
- * Usage:  <for command-line>
- *  vfork02 [-c n] [-e] [-f] [-i n] [-I x] [-p x] [-t]
- *     where,  -c n : Run n copies concurrently.
- *             -e   : Turn on errno logging.
- *             -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 John George
- *		-Ported
- *
- * Restrictions:
- *  None.
- *
  */
-#define _GNU_SOURCE 1
-
-#include <stdio.h>
-#include <sys/types.h>
-#include <errno.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <string.h>
-#include <signal.h>
-#include <sys/stat.h>
-#include <sys/wait.h>
-
-#include "test.h"
-#include "safe_macros.h"
-
-char *TCID = "vfork02";
-int TST_TOTAL = 1;
-
-void setup();			/* Main setup function of test */
-void cleanup();			/* cleanup function for the test */
-void sig_handler();		/* signal catching function */
-
-int main(int ac, char **av)
-{
-	int lc;
-	pid_t cpid;		/* process id of the child process */
-	int exit_status;	/* exit status of child process */
-	sigset_t PendSig;	/* variable to hold pending signal */
-
-	tst_parse_opts(ac, av, NULL, NULL);
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-
-		tst_count = 0;
-
-		/*
-		 * Call vfork(2) to create a child process without
-		 * fully copying the address space of parent.
-		 */
-		TEST(vfork());
-
-		if ((cpid = TEST_RETURN) == -1) {
-			tst_resm(TFAIL, "vfork() Failed, errno=%d : %s",
-				 TEST_ERRNO, strerror(TEST_ERRNO));
-		} else if (cpid == 0) {	/* Child process */
-			/*
-			 * Check whether the pending signal SIGUSR1
-			 * in the parent is also pending in the child
-			 * process by storing it in a variable.
-			 */
-			if (sigpending(&PendSig) == -1) {
-				tst_resm(TFAIL, "sigpending function "
-					 "failed in child");
-				_exit(1);
-			}
-
-			/* Check if SIGUSR1 is pending in child */
-			if (sigismember(&PendSig, SIGUSR1) != 0) {
-				tst_resm(TFAIL, "SIGUSR1 also pending "
-					 "in child process");
-				_exit(1);
-			}
-
-			/*
-			 * Exit with normal exit code if everything
-			 * fine
-			 */
-			_exit(0);
-		} else {	/* parent process */
-			/*
-			 * Let the parent process wait till child completes
-			 * its execution.
-			 */
-			wait(&exit_status);
-
-			/* Check for the exit status of child process */
-			if (WEXITSTATUS(exit_status) == 0) {
-				tst_resm(TPASS, "Call to vfork() "
-					 "successful");
-			} else if (WEXITSTATUS(exit_status) == 1) {
-				tst_resm(TFAIL,
-					 "Child process exited abnormally");
-			}
-		}
-		tst_count++;	/* incr. TEST_LOOP counter */
-	}
 
-	cleanup();
-	tst_exit();
+#include "tst_test.h"
 
-}
-
-/*
- * void
- * setup() - performs all ONE TIME setup for this test.
- *   This function installs signal handler for SIGUSR1, puts signal SIGUSR1
- *   on hold and then sends the signal SIGUSR1 to itself so that it is in
- *   pending state.
- */
-void setup(void)
+static void run(void)
 {
-	sigset_t PendSig;	/* variable to hold pending signal */
-
-	tst_sig(FORK, DEF_HANDLER, cleanup);
+	if (!vfork()) {
+		sigset_t signal;
 
-	TEST_PAUSE;
-
-	/* Install the signal handler */
-	if (signal(SIGUSR1, sig_handler) == SIG_ERR) {
-		tst_brkm(TBROK, cleanup, "Fails to catch the signal SIGUSR1");
-	}
-
-	/* Hold the signal SIGUSR1 */
-	if (sighold(SIGUSR1) == -1) {
-		tst_brkm(TBROK, cleanup,
-			 "sighold failed to hold the signal SIGUSR1");
-	}
+		if (sigpending(&signal) == -1)
+			tst_brk(TBROK | TERRNO, "sigpending() error");
 
-	/* Send the signal SIGUSR1 to itself so that SIGUSR1 is pending */
-	SAFE_KILL(cleanup, getpid(), SIGUSR1);
+		TST_EXP_EQ_LI(sigismember(&signal, SIGUSR1), 0);
 
-	/* If SIGUSR1 is not pending in the parent, fail */
-	if (sigpending(&PendSig) == -1) {
-		tst_brkm(TBROK, cleanup,
-			 "sigpending function failed in parent");
-	}
-
-	/* Check if SIGUSR1 is pending in parent */
-	if (sigismember(&PendSig, SIGUSR1) != 1) {
-		tst_brkm(TBROK, cleanup,
-			 "SIGUSR1 signal is not pending in parent");
+		exit(0);
 	}
 }
 
-/*
- * void
- * sig_handler() - signal catching function for 'SIGUSR1' signal.
- *		 $
- *   This is a null function and used only to catch the above signal
- *   generated in parent process.
- */
-void sig_handler(void)
+static void sig_handler(LTP_ATTRIBUTE_UNUSED int signo)
 {
 }
 
-/*
- * void
- * cleanup() - performs all ONE TIME cleanup for this test at
- *             completion or premature exit.
- *  Release the signal 'SIGUSR1'  if still in pending state.
- */
-void cleanup(void)
+static void setup(void)
 {
+	struct sigaction action;
 
-	/* Release the signal 'SIGUSR1' if in pending state */
-	if (sigrelse(SIGUSR1) == -1) {
-		tst_brkm(TBROK, NULL, "Failed to release 'SIGUSR1' in cleanup");
-	}
+	memset(&action, 0, sizeof(action));
+	action.sa_handler = sig_handler;
 
+	SAFE_SIGACTION(SIGUSR1, &action, NULL);
 }
+
+static struct tst_test test = {
+	.test_all = run,
+	.setup = setup,
+};
Signature · 5 lines
2.51.0


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

Re: [LTP] [PATCH 3/3] syscalls: refactor vfork02 using new API

Hi!
This is actually too fleshed out that it no longer does the test.

The original test actually set the SIGUSR1 to hold and then send the signal to itself and also checked that it's pending in the test setup. That is the part that is missing after the rewrite.

Signature · 5 lines
Cyril Hrubis
[email protected]

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

Re: [LTP] [PATCH 3/3] syscalls: refactor vfork02 using new API

Quoted 6 lines

Hi!
This is actually too fleshed out that it no longer does the test.

The original test actually set the SIGUSR1 to hold and then send the signal to itself and also checked that it's pending in the test setup. That is the part that is missing after the rewrite.

Sorry, I think I sent this patch unfinished..I'm gonna send a v2 for the whole patch-set.

--
Andrea Cervesato
SUSE QE Automation Engineer Linux
[email protected]

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp