1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) 2020 Viresh Kumar <[email protected]>
4*49cdfc7eSAndroid Build Coastguard Worker */
5*49cdfc7eSAndroid Build Coastguard Worker
6*49cdfc7eSAndroid Build Coastguard Worker /*\
7*49cdfc7eSAndroid Build Coastguard Worker * [Description]
8*49cdfc7eSAndroid Build Coastguard Worker *
9*49cdfc7eSAndroid Build Coastguard Worker * Basic clone3() test.
10*49cdfc7eSAndroid Build Coastguard Worker */
11*49cdfc7eSAndroid Build Coastguard Worker
12*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE
13*49cdfc7eSAndroid Build Coastguard Worker
14*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
15*49cdfc7eSAndroid Build Coastguard Worker #include <sys/wait.h>
16*49cdfc7eSAndroid Build Coastguard Worker
17*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
18*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/sched.h"
19*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/pidfd.h"
20*49cdfc7eSAndroid Build Coastguard Worker
21*49cdfc7eSAndroid Build Coastguard Worker #define CHILD_SIGNAL SIGUSR1
22*49cdfc7eSAndroid Build Coastguard Worker #define DATA 777
23*49cdfc7eSAndroid Build Coastguard Worker
24*49cdfc7eSAndroid Build Coastguard Worker static int pidfd, child_tid, parent_tid, parent_received_signal;
25*49cdfc7eSAndroid Build Coastguard Worker static volatile int child_received_signal, child_data;
26*49cdfc7eSAndroid Build Coastguard Worker static struct clone_args *args;
27*49cdfc7eSAndroid Build Coastguard Worker
28*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
29*49cdfc7eSAndroid Build Coastguard Worker uint64_t flags;
30*49cdfc7eSAndroid Build Coastguard Worker int exit_signal;
31*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
32*49cdfc7eSAndroid Build Coastguard Worker {0, SIGCHLD},
33*49cdfc7eSAndroid Build Coastguard Worker {0, SIGUSR2},
34*49cdfc7eSAndroid Build Coastguard Worker {CLONE_FS, SIGCHLD},
35*49cdfc7eSAndroid Build Coastguard Worker {CLONE_NEWPID, SIGCHLD},
36*49cdfc7eSAndroid Build Coastguard Worker {CLONE_PARENT_SETTID | CLONE_CHILD_SETTID | CLONE_PIDFD, SIGCHLD},
37*49cdfc7eSAndroid Build Coastguard Worker };
38*49cdfc7eSAndroid Build Coastguard Worker
parent_rx_signal(int sig)39*49cdfc7eSAndroid Build Coastguard Worker static void parent_rx_signal(int sig)
40*49cdfc7eSAndroid Build Coastguard Worker {
41*49cdfc7eSAndroid Build Coastguard Worker parent_received_signal = sig;
42*49cdfc7eSAndroid Build Coastguard Worker }
43*49cdfc7eSAndroid Build Coastguard Worker
child_rx_signal(int sig,siginfo_t * info,void * ucontext)44*49cdfc7eSAndroid Build Coastguard Worker static void child_rx_signal(int sig, siginfo_t *info, void *ucontext)
45*49cdfc7eSAndroid Build Coastguard Worker {
46*49cdfc7eSAndroid Build Coastguard Worker (void) ucontext;
47*49cdfc7eSAndroid Build Coastguard Worker
48*49cdfc7eSAndroid Build Coastguard Worker child_received_signal = sig;
49*49cdfc7eSAndroid Build Coastguard Worker child_data = info->si_value.sival_int;
50*49cdfc7eSAndroid Build Coastguard Worker }
51*49cdfc7eSAndroid Build Coastguard Worker
52*49cdfc7eSAndroid Build Coastguard Worker static struct sigaction psig_action = {
53*49cdfc7eSAndroid Build Coastguard Worker .sa_handler = parent_rx_signal,
54*49cdfc7eSAndroid Build Coastguard Worker };
55*49cdfc7eSAndroid Build Coastguard Worker
56*49cdfc7eSAndroid Build Coastguard Worker static struct sigaction csig_action = {
57*49cdfc7eSAndroid Build Coastguard Worker .sa_sigaction = child_rx_signal,
58*49cdfc7eSAndroid Build Coastguard Worker .sa_flags = SA_SIGINFO,
59*49cdfc7eSAndroid Build Coastguard Worker };
60*49cdfc7eSAndroid Build Coastguard Worker
61*49cdfc7eSAndroid Build Coastguard Worker static siginfo_t uinfo = {
62*49cdfc7eSAndroid Build Coastguard Worker .si_signo = CHILD_SIGNAL,
63*49cdfc7eSAndroid Build Coastguard Worker .si_code = SI_QUEUE,
64*49cdfc7eSAndroid Build Coastguard Worker .si_value.sival_int = DATA,
65*49cdfc7eSAndroid Build Coastguard Worker };
66*49cdfc7eSAndroid Build Coastguard Worker
67*49cdfc7eSAndroid Build Coastguard Worker
do_child(int clone_pidfd)68*49cdfc7eSAndroid Build Coastguard Worker static void do_child(int clone_pidfd)
69*49cdfc7eSAndroid Build Coastguard Worker {
70*49cdfc7eSAndroid Build Coastguard Worker int count = 1000;
71*49cdfc7eSAndroid Build Coastguard Worker
72*49cdfc7eSAndroid Build Coastguard Worker if (clone_pidfd) {
73*49cdfc7eSAndroid Build Coastguard Worker child_received_signal = 0;
74*49cdfc7eSAndroid Build Coastguard Worker child_data = 0;
75*49cdfc7eSAndroid Build Coastguard Worker
76*49cdfc7eSAndroid Build Coastguard Worker SAFE_SIGACTION(CHILD_SIGNAL, &csig_action, NULL);
77*49cdfc7eSAndroid Build Coastguard Worker
78*49cdfc7eSAndroid Build Coastguard Worker TST_CHECKPOINT_WAKE(0);
79*49cdfc7eSAndroid Build Coastguard Worker
80*49cdfc7eSAndroid Build Coastguard Worker while (child_received_signal != CHILD_SIGNAL && --count)
81*49cdfc7eSAndroid Build Coastguard Worker usleep(100);
82*49cdfc7eSAndroid Build Coastguard Worker
83*49cdfc7eSAndroid Build Coastguard Worker if (!child_received_signal) {
84*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "Child haven't got signal");
85*49cdfc7eSAndroid Build Coastguard Worker exit(0);
86*49cdfc7eSAndroid Build Coastguard Worker }
87*49cdfc7eSAndroid Build Coastguard Worker
88*49cdfc7eSAndroid Build Coastguard Worker if (child_received_signal != CHILD_SIGNAL) {
89*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "Child got %s (%i) signal expected %s",
90*49cdfc7eSAndroid Build Coastguard Worker tst_strsig(child_received_signal),
91*49cdfc7eSAndroid Build Coastguard Worker child_received_signal,
92*49cdfc7eSAndroid Build Coastguard Worker tst_strsig(CHILD_SIGNAL));
93*49cdfc7eSAndroid Build Coastguard Worker exit(0);
94*49cdfc7eSAndroid Build Coastguard Worker }
95*49cdfc7eSAndroid Build Coastguard Worker
96*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "Child got correct signal %s",
97*49cdfc7eSAndroid Build Coastguard Worker tst_strsig(CHILD_SIGNAL));
98*49cdfc7eSAndroid Build Coastguard Worker
99*49cdfc7eSAndroid Build Coastguard Worker if (child_data != DATA) {
100*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "Child got wrong si_value=%i expected %i",
101*49cdfc7eSAndroid Build Coastguard Worker child_data, DATA);
102*49cdfc7eSAndroid Build Coastguard Worker } else {
103*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "Child got correct si_value");
104*49cdfc7eSAndroid Build Coastguard Worker }
105*49cdfc7eSAndroid Build Coastguard Worker }
106*49cdfc7eSAndroid Build Coastguard Worker
107*49cdfc7eSAndroid Build Coastguard Worker exit(0);
108*49cdfc7eSAndroid Build Coastguard Worker }
109*49cdfc7eSAndroid Build Coastguard Worker
run(unsigned int n)110*49cdfc7eSAndroid Build Coastguard Worker static void run(unsigned int n)
111*49cdfc7eSAndroid Build Coastguard Worker {
112*49cdfc7eSAndroid Build Coastguard Worker struct tcase *tc = &tcases[n];
113*49cdfc7eSAndroid Build Coastguard Worker int status, clone_pidfd = tc->flags & CLONE_PIDFD;
114*49cdfc7eSAndroid Build Coastguard Worker pid_t pid;
115*49cdfc7eSAndroid Build Coastguard Worker
116*49cdfc7eSAndroid Build Coastguard Worker args->flags = tc->flags;
117*49cdfc7eSAndroid Build Coastguard Worker args->pidfd = (uint64_t)(&pidfd);
118*49cdfc7eSAndroid Build Coastguard Worker args->child_tid = (uint64_t)(&child_tid);
119*49cdfc7eSAndroid Build Coastguard Worker args->parent_tid = (uint64_t)(&parent_tid);
120*49cdfc7eSAndroid Build Coastguard Worker args->exit_signal = tc->exit_signal;
121*49cdfc7eSAndroid Build Coastguard Worker args->stack = 0;
122*49cdfc7eSAndroid Build Coastguard Worker args->stack_size = 0;
123*49cdfc7eSAndroid Build Coastguard Worker args->tls = 0;
124*49cdfc7eSAndroid Build Coastguard Worker
125*49cdfc7eSAndroid Build Coastguard Worker parent_received_signal = 0;
126*49cdfc7eSAndroid Build Coastguard Worker SAFE_SIGACTION(tc->exit_signal, &psig_action, NULL);
127*49cdfc7eSAndroid Build Coastguard Worker
128*49cdfc7eSAndroid Build Coastguard Worker TEST(pid = clone3(args, sizeof(*args)));
129*49cdfc7eSAndroid Build Coastguard Worker if (pid < 0) {
130*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL | TTERRNO, "clone3() failed (%d)", n);
131*49cdfc7eSAndroid Build Coastguard Worker return;
132*49cdfc7eSAndroid Build Coastguard Worker }
133*49cdfc7eSAndroid Build Coastguard Worker
134*49cdfc7eSAndroid Build Coastguard Worker if (!pid)
135*49cdfc7eSAndroid Build Coastguard Worker do_child(clone_pidfd);
136*49cdfc7eSAndroid Build Coastguard Worker
137*49cdfc7eSAndroid Build Coastguard Worker /* Need to send signal to child process */
138*49cdfc7eSAndroid Build Coastguard Worker if (clone_pidfd) {
139*49cdfc7eSAndroid Build Coastguard Worker TST_CHECKPOINT_WAIT(0);
140*49cdfc7eSAndroid Build Coastguard Worker
141*49cdfc7eSAndroid Build Coastguard Worker TEST(pidfd_send_signal(pidfd, CHILD_SIGNAL, &uinfo, 0));
142*49cdfc7eSAndroid Build Coastguard Worker if (TST_RET != 0) {
143*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL | TTERRNO, "pidfd_send_signal() failed");
144*49cdfc7eSAndroid Build Coastguard Worker return;
145*49cdfc7eSAndroid Build Coastguard Worker }
146*49cdfc7eSAndroid Build Coastguard Worker }
147*49cdfc7eSAndroid Build Coastguard Worker
148*49cdfc7eSAndroid Build Coastguard Worker SAFE_WAITPID(pid, &status, __WALL);
149*49cdfc7eSAndroid Build Coastguard Worker
150*49cdfc7eSAndroid Build Coastguard Worker if (!parent_received_signal) {
151*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "Parent haven't got signal");
152*49cdfc7eSAndroid Build Coastguard Worker return;
153*49cdfc7eSAndroid Build Coastguard Worker }
154*49cdfc7eSAndroid Build Coastguard Worker
155*49cdfc7eSAndroid Build Coastguard Worker if (parent_received_signal != tc->exit_signal) {
156*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "Parent got %s (%i) signal expected %s",
157*49cdfc7eSAndroid Build Coastguard Worker tst_strsig(parent_received_signal),
158*49cdfc7eSAndroid Build Coastguard Worker parent_received_signal,
159*49cdfc7eSAndroid Build Coastguard Worker tst_strsig(tc->exit_signal));
160*49cdfc7eSAndroid Build Coastguard Worker return;
161*49cdfc7eSAndroid Build Coastguard Worker }
162*49cdfc7eSAndroid Build Coastguard Worker
163*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "Parent got correct signal %s",
164*49cdfc7eSAndroid Build Coastguard Worker tst_strsig(parent_received_signal));
165*49cdfc7eSAndroid Build Coastguard Worker }
166*49cdfc7eSAndroid Build Coastguard Worker
setup(void)167*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
168*49cdfc7eSAndroid Build Coastguard Worker {
169*49cdfc7eSAndroid Build Coastguard Worker clone3_supported_by_kernel();
170*49cdfc7eSAndroid Build Coastguard Worker
171*49cdfc7eSAndroid Build Coastguard Worker uinfo.si_pid = getpid();
172*49cdfc7eSAndroid Build Coastguard Worker uinfo.si_uid = getuid();
173*49cdfc7eSAndroid Build Coastguard Worker }
174*49cdfc7eSAndroid Build Coastguard Worker
175*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
176*49cdfc7eSAndroid Build Coastguard Worker .tcnt = ARRAY_SIZE(tcases),
177*49cdfc7eSAndroid Build Coastguard Worker .test = run,
178*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
179*49cdfc7eSAndroid Build Coastguard Worker .needs_root = 1,
180*49cdfc7eSAndroid Build Coastguard Worker .needs_checkpoints = 1,
181*49cdfc7eSAndroid Build Coastguard Worker .bufs = (struct tst_buffers []) {
182*49cdfc7eSAndroid Build Coastguard Worker {&args, .size = sizeof(*args)},
183*49cdfc7eSAndroid Build Coastguard Worker {},
184*49cdfc7eSAndroid Build Coastguard Worker }
185*49cdfc7eSAndroid Build Coastguard Worker };
186