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

Andrea Cervesato
2026-01-27T12:50:50Z

From: Andrea Cervesato <andrea.cervesato@suse.com>

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

Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
---
testcases/kernel/syscalls/vfork/vfork02.c | 230 +++---------------------------
1 file changed, 23 insertions(+), 207 deletions(-)

diff --git a/testcases/kernel/syscalls/vfork/vfork02.c b/testcases/kernel/syscalls/vfork/vfork02.c
index efa70d0bef325a77e345448fc88d3fb721a8d39d..507666c5f2c59f2d74f2efcd4c0e088a9818ed05 100644
--- a/testcases/kernel/syscalls/vfork/vfork02.c
+++ b/testcases/kernel/syscalls/vfork/vfork02.c
@@ -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 <andrea.cervesato@suse.com>
*/

-/*
- * 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,
+};

--
2.51.0