1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines Corp., 2007
4f08c3bdfSopenharmony_ci *               04/11/08  Veerendra C  <vechandr@in.ibm.com>
5f08c3bdfSopenharmony_ci * Copyright (C) 2023 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
6f08c3bdfSopenharmony_ci */
7f08c3bdfSopenharmony_ci
8f08c3bdfSopenharmony_ci/*\
9f08c3bdfSopenharmony_ci * [Description]
10f08c3bdfSopenharmony_ci *
11f08c3bdfSopenharmony_ci * Clone a process with CLONE_NEWPID flag and verifies that siginfo->si_pid is
12f08c3bdfSopenharmony_ci * set to 0 if sender (parent process) sent the signal. Then send signal from
13f08c3bdfSopenharmony_ci * container itself and check if siginfo->si_pid is set to 1.
14f08c3bdfSopenharmony_ci */
15f08c3bdfSopenharmony_ci
16f08c3bdfSopenharmony_ci#define _GNU_SOURCE 1
17f08c3bdfSopenharmony_ci#include <signal.h>
18f08c3bdfSopenharmony_ci#include "tst_test.h"
19f08c3bdfSopenharmony_ci#include "lapi/sched.h"
20f08c3bdfSopenharmony_ci
21f08c3bdfSopenharmony_cistatic volatile int signal_pid;
22f08c3bdfSopenharmony_ci
23f08c3bdfSopenharmony_cistatic void child_signal_handler(LTP_ATTRIBUTE_UNUSED int sig, siginfo_t *si, LTP_ATTRIBUTE_UNUSED void *unused)
24f08c3bdfSopenharmony_ci{
25f08c3bdfSopenharmony_ci	signal_pid = si->si_pid;
26f08c3bdfSopenharmony_ci}
27f08c3bdfSopenharmony_ci
28f08c3bdfSopenharmony_cistatic void child_func(void)
29f08c3bdfSopenharmony_ci{
30f08c3bdfSopenharmony_ci	struct sigaction sa;
31f08c3bdfSopenharmony_ci	pid_t cpid, ppid;
32f08c3bdfSopenharmony_ci
33f08c3bdfSopenharmony_ci	cpid = tst_getpid();
34f08c3bdfSopenharmony_ci	ppid = getppid();
35f08c3bdfSopenharmony_ci
36f08c3bdfSopenharmony_ci	TST_EXP_EQ_LI(cpid, 1);
37f08c3bdfSopenharmony_ci	TST_EXP_EQ_LI(ppid, 0);
38f08c3bdfSopenharmony_ci
39f08c3bdfSopenharmony_ci	tst_res(TINFO, "Catching SIGUSR1 signal");
40f08c3bdfSopenharmony_ci
41f08c3bdfSopenharmony_ci	sa.sa_flags = SA_SIGINFO;
42f08c3bdfSopenharmony_ci	SAFE_SIGFILLSET(&sa.sa_mask);
43f08c3bdfSopenharmony_ci	sa.sa_sigaction = child_signal_handler;
44f08c3bdfSopenharmony_ci	SAFE_SIGACTION(SIGUSR1, &sa, NULL);
45f08c3bdfSopenharmony_ci
46f08c3bdfSopenharmony_ci	TST_CHECKPOINT_WAKE_AND_WAIT(0);
47f08c3bdfSopenharmony_ci
48f08c3bdfSopenharmony_ci	TST_EXP_EQ_LI(signal_pid, 0);
49f08c3bdfSopenharmony_ci
50f08c3bdfSopenharmony_ci	tst_res(TINFO, "Sending SIGUSR1 from container itself");
51f08c3bdfSopenharmony_ci
52f08c3bdfSopenharmony_ci	SAFE_KILL(cpid, SIGUSR1);
53f08c3bdfSopenharmony_ci
54f08c3bdfSopenharmony_ci	TST_EXP_EQ_LI(signal_pid, 1);
55f08c3bdfSopenharmony_ci}
56f08c3bdfSopenharmony_ci
57f08c3bdfSopenharmony_cistatic void run(void)
58f08c3bdfSopenharmony_ci{
59f08c3bdfSopenharmony_ci	const struct tst_clone_args args = {
60f08c3bdfSopenharmony_ci		.flags = CLONE_NEWPID,
61f08c3bdfSopenharmony_ci		.exit_signal = SIGCHLD,
62f08c3bdfSopenharmony_ci	};
63f08c3bdfSopenharmony_ci	pid_t pid;
64f08c3bdfSopenharmony_ci
65f08c3bdfSopenharmony_ci	signal_pid = -1;
66f08c3bdfSopenharmony_ci
67f08c3bdfSopenharmony_ci	pid = SAFE_CLONE(&args);
68f08c3bdfSopenharmony_ci	if (!pid) {
69f08c3bdfSopenharmony_ci		child_func();
70f08c3bdfSopenharmony_ci		return;
71f08c3bdfSopenharmony_ci	}
72f08c3bdfSopenharmony_ci
73f08c3bdfSopenharmony_ci	TST_CHECKPOINT_WAIT(0);
74f08c3bdfSopenharmony_ci
75f08c3bdfSopenharmony_ci	tst_res(TINFO, "Sending SIGUSR1 from parent");
76f08c3bdfSopenharmony_ci
77f08c3bdfSopenharmony_ci	SAFE_KILL(pid, SIGUSR1);
78f08c3bdfSopenharmony_ci
79f08c3bdfSopenharmony_ci	TST_CHECKPOINT_WAKE(0);
80f08c3bdfSopenharmony_ci}
81f08c3bdfSopenharmony_ci
82f08c3bdfSopenharmony_cistatic struct tst_test test = {
83f08c3bdfSopenharmony_ci	.test_all = run,
84f08c3bdfSopenharmony_ci	.needs_root = 1,
85f08c3bdfSopenharmony_ci	.needs_checkpoints = 1,
86f08c3bdfSopenharmony_ci	.forks_child = 1,
87f08c3bdfSopenharmony_ci};
88