1 /* SPDX-License-Identifier: GPL-2.0 */
11 #include <linux/sched.h>
12 #include <linux/types.h>
13 #include <sys/syscall.h>
16 #include "../kselftest.h"
17 #include "clone3_selftests.h"
19 #ifndef CLONE_CLEAR_SIGHAND
20 #define CLONE_CLEAR_SIGHAND 0x100000000ULL
23 static void nop_handler(int signo)
27 static int wait_for_pid(pid_t pid)
32 ret = waitpid(pid, &status, 0);
40 if (!WIFEXITED(status))
43 return WEXITSTATUS(status);
46 static void test_clone3_clear_sighand(void)
50 struct __clone_args args = {};
54 * Check that CLONE_CLEAR_SIGHAND and CLONE_SIGHAND are mutually
57 args.flags |= CLONE_CLEAR_SIGHAND | CLONE_SIGHAND;
58 args.exit_signal = SIGCHLD;
59 pid = sys_clone3(&args, sizeof(args));
62 "clone3(CLONE_CLEAR_SIGHAND | CLONE_SIGHAND) succeeded\n");
64 act.sa_handler = nop_handler;
65 ret = sigemptyset(&act.sa_mask);
67 ksft_exit_fail_msg("%s - sigemptyset() failed\n",
72 /* Register signal handler for SIGUSR1 */
73 ret = sigaction(SIGUSR1, &act, NULL);
76 "%s - sigaction(SIGUSR1, &act, NULL) failed\n",
79 /* Register signal handler for SIGUSR2 */
80 ret = sigaction(SIGUSR2, &act, NULL);
83 "%s - sigaction(SIGUSR2, &act, NULL) failed\n",
86 /* Check that CLONE_CLEAR_SIGHAND works. */
87 args.flags = CLONE_CLEAR_SIGHAND;
88 pid = sys_clone3(&args, sizeof(args));
90 ksft_exit_fail_msg("%s - clone3(CLONE_CLEAR_SIGHAND) failed\n",
94 ret = sigaction(SIGUSR1, NULL, &act);
98 if (act.sa_handler != SIG_DFL)
101 ret = sigaction(SIGUSR2, NULL, &act);
105 if (act.sa_handler != SIG_DFL)
111 ret = wait_for_pid(pid);
114 "Failed to clear signal handler for child process\n");
116 ksft_test_result_pass("Cleared signal handlers for child process\n");
119 int main(int argc, char **argv)
123 test_clone3_supported();
125 test_clone3_clear_sighand();
127 return ksft_exit_pass();