1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) International Business Machines Corp., 2007
4 * 13/11/08 Gowrishankar M <[email protected]>
5 * Copyright (C) 2022 SUSE LLC Andrea Cervesato <[email protected]>
6 */
7
8 /*\
9 * [Description]
10 *
11 * Clone a process with CLONE_NEWPID flag and verifies that siginfo->si_pid is
12 * set to 0 if sender (parent process) is not in the receiver's namespace.
13 */
14
15 #include "tst_test_macros.h"
16 #define _GNU_SOURCE 1
17 #include <signal.h>
18 #include "tst_test.h"
19 #include "lapi/sched.h"
20
21 static volatile pid_t sig_pid = -1;
22
child_signal_handler(LTP_ATTRIBUTE_UNUSED int sig,siginfo_t * si,LTP_ATTRIBUTE_UNUSED void * unused)23 static void child_signal_handler(LTP_ATTRIBUTE_UNUSED int sig, siginfo_t *si, LTP_ATTRIBUTE_UNUSED void *unused)
24 {
25 sig_pid = si->si_pid;
26 }
27
child_func(void)28 static void child_func(void)
29 {
30 struct sigaction sa;
31
32 TST_EXP_EQ_LI(tst_getpid(), 1);
33 TST_EXP_EQ_LI(getppid(), 0);
34
35 sa.sa_flags = SA_SIGINFO;
36 SAFE_SIGFILLSET(&sa.sa_mask);
37 sa.sa_sigaction = child_signal_handler;
38
39 SAFE_SIGACTION(SIGUSR1, &sa, NULL);
40
41 TST_CHECKPOINT_WAKE_AND_WAIT(0);
42
43 TST_EXP_EQ_LI(sig_pid, 0);
44 }
45
run(void)46 static void run(void)
47 {
48 const struct tst_clone_args args = {
49 .flags = CLONE_NEWPID,
50 .exit_signal = SIGCHLD,
51 };
52 int pid;
53
54 pid = SAFE_CLONE(&args);
55 if (!pid) {
56 child_func();
57 return;
58 }
59
60 TST_CHECKPOINT_WAIT(0);
61
62 SAFE_KILL(pid, SIGUSR1);
63
64 TST_CHECKPOINT_WAKE(0);
65 }
66
67 static struct tst_test test = {
68 .test_all = run,
69 .needs_root = 1,
70 .forks_child = 1,
71 .needs_checkpoints = 1,
72 .needs_kconfigs = (const char *[]) {
73 "CONFIG_PID_NS",
74 NULL,
75 },
76 };
77