1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines Corp., 2007
4f08c3bdfSopenharmony_ci *               13/11/08  Gowrishankar M  <gowrishankar.m@in.ibm.com>
5f08c3bdfSopenharmony_ci * Copyright (C) 2022 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, block SIGUSR1 signal before sending
12f08c3bdfSopenharmony_ci * it from parent and check if it's received once SIGUSR1 signal is unblocked.
13f08c3bdfSopenharmony_ci */
14f08c3bdfSopenharmony_ci
15f08c3bdfSopenharmony_ci#define _GNU_SOURCE 1
16f08c3bdfSopenharmony_ci#include <signal.h>
17f08c3bdfSopenharmony_ci#include "tst_test.h"
18f08c3bdfSopenharmony_ci#include "lapi/sched.h"
19f08c3bdfSopenharmony_ci
20f08c3bdfSopenharmony_cistatic volatile int signals;
21f08c3bdfSopenharmony_cistatic volatile int last_signo;
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	last_signo = si->si_signo;
26f08c3bdfSopenharmony_ci	signals++;
27f08c3bdfSopenharmony_ci}
28f08c3bdfSopenharmony_ci
29f08c3bdfSopenharmony_cistatic void child_func(void)
30f08c3bdfSopenharmony_ci{
31f08c3bdfSopenharmony_ci	struct sigaction sa;
32f08c3bdfSopenharmony_ci	sigset_t newset;
33f08c3bdfSopenharmony_ci	pid_t cpid, ppid;
34f08c3bdfSopenharmony_ci
35f08c3bdfSopenharmony_ci	cpid = tst_getpid();
36f08c3bdfSopenharmony_ci	ppid = getppid();
37f08c3bdfSopenharmony_ci
38f08c3bdfSopenharmony_ci	if (cpid != 1 || ppid != 0) {
39f08c3bdfSopenharmony_ci		tst_res(TFAIL, "Got unexpected result of cpid=%d ppid=%d", cpid, ppid);
40f08c3bdfSopenharmony_ci		return;
41f08c3bdfSopenharmony_ci	}
42f08c3bdfSopenharmony_ci
43f08c3bdfSopenharmony_ci	SAFE_SIGEMPTYSET(&newset);
44f08c3bdfSopenharmony_ci	SAFE_SIGADDSET(&newset, SIGUSR1);
45f08c3bdfSopenharmony_ci	SAFE_SIGPROCMASK(SIG_BLOCK, &newset, 0);
46f08c3bdfSopenharmony_ci
47f08c3bdfSopenharmony_ci	TST_CHECKPOINT_WAKE_AND_WAIT(0);
48f08c3bdfSopenharmony_ci
49f08c3bdfSopenharmony_ci	sa.sa_flags = SA_SIGINFO;
50f08c3bdfSopenharmony_ci	SAFE_SIGFILLSET(&sa.sa_mask);
51f08c3bdfSopenharmony_ci	sa.sa_sigaction = child_signal_handler;
52f08c3bdfSopenharmony_ci
53f08c3bdfSopenharmony_ci	SAFE_SIGACTION(SIGUSR1, &sa, NULL);
54f08c3bdfSopenharmony_ci
55f08c3bdfSopenharmony_ci	SAFE_SIGPROCMASK(SIG_UNBLOCK, &newset, 0);
56f08c3bdfSopenharmony_ci
57f08c3bdfSopenharmony_ci	if (signals != 1) {
58f08c3bdfSopenharmony_ci		tst_res(TFAIL, "Received %d signals", signals);
59f08c3bdfSopenharmony_ci		return;
60f08c3bdfSopenharmony_ci	}
61f08c3bdfSopenharmony_ci
62f08c3bdfSopenharmony_ci	if (last_signo != SIGUSR1) {
63f08c3bdfSopenharmony_ci		tst_res(TFAIL, "Received %s signal", tst_strsig(last_signo));
64f08c3bdfSopenharmony_ci		return;
65f08c3bdfSopenharmony_ci	}
66f08c3bdfSopenharmony_ci
67f08c3bdfSopenharmony_ci	tst_res(TPASS, "Received SIGUSR1 signal after unblock");
68f08c3bdfSopenharmony_ci}
69f08c3bdfSopenharmony_ci
70f08c3bdfSopenharmony_cistatic void run(void)
71f08c3bdfSopenharmony_ci{
72f08c3bdfSopenharmony_ci	const struct tst_clone_args args = {
73f08c3bdfSopenharmony_ci		.flags = CLONE_NEWPID,
74f08c3bdfSopenharmony_ci		.exit_signal = SIGCHLD,
75f08c3bdfSopenharmony_ci	};
76f08c3bdfSopenharmony_ci	int pid;
77f08c3bdfSopenharmony_ci
78f08c3bdfSopenharmony_ci	pid = SAFE_CLONE(&args);
79f08c3bdfSopenharmony_ci	if (!pid) {
80f08c3bdfSopenharmony_ci		child_func();
81f08c3bdfSopenharmony_ci		return;
82f08c3bdfSopenharmony_ci	}
83f08c3bdfSopenharmony_ci
84f08c3bdfSopenharmony_ci	TST_CHECKPOINT_WAIT(0);
85f08c3bdfSopenharmony_ci
86f08c3bdfSopenharmony_ci	SAFE_KILL(pid, SIGUSR1);
87f08c3bdfSopenharmony_ci
88f08c3bdfSopenharmony_ci	TST_CHECKPOINT_WAKE(0);
89f08c3bdfSopenharmony_ci}
90f08c3bdfSopenharmony_ci
91f08c3bdfSopenharmony_cistatic struct tst_test test = {
92f08c3bdfSopenharmony_ci	.test_all = run,
93f08c3bdfSopenharmony_ci	.needs_root = 1,
94f08c3bdfSopenharmony_ci	.forks_child = 1,
95f08c3bdfSopenharmony_ci	.needs_checkpoints = 1,
96f08c3bdfSopenharmony_ci	.needs_kconfigs = (const char *[]) {
97f08c3bdfSopenharmony_ci		"CONFIG_PID_NS",
98f08c3bdfSopenharmony_ci		NULL,
99f08c3bdfSopenharmony_ci	},
100f08c3bdfSopenharmony_ci};
101