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) Bull S.A.S. 2008
4*49cdfc7eSAndroid Build Coastguard Worker * 01/12/08 Nadia Derbey <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker * Copyright (C) 2023 SUSE LLC Andrea Cervesato <[email protected]>
6*49cdfc7eSAndroid Build Coastguard Worker */
7*49cdfc7eSAndroid Build Coastguard Worker
8*49cdfc7eSAndroid Build Coastguard Worker /*\
9*49cdfc7eSAndroid Build Coastguard Worker * [Description]
10*49cdfc7eSAndroid Build Coastguard Worker *
11*49cdfc7eSAndroid Build Coastguard Worker * Clone a process with CLONE_NEWPID flag, register notification on a posix
12*49cdfc7eSAndroid Build Coastguard Worker * mqueue and send a mqueue message from the parent. Then check if signal
13*49cdfc7eSAndroid Build Coastguard Worker * notification contains si_pid of the parent.
14*49cdfc7eSAndroid Build Coastguard Worker */
15*49cdfc7eSAndroid Build Coastguard Worker
16*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE
17*49cdfc7eSAndroid Build Coastguard Worker #include <signal.h>
18*49cdfc7eSAndroid Build Coastguard Worker #include <mqueue.h>
19*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
20*49cdfc7eSAndroid Build Coastguard Worker #include "tst_safe_posix_ipc.h"
21*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/sched.h"
22*49cdfc7eSAndroid Build Coastguard Worker
23*49cdfc7eSAndroid Build Coastguard Worker #define MQNAME "/LTP_PIDNS30_MQ"
24*49cdfc7eSAndroid Build Coastguard Worker
25*49cdfc7eSAndroid Build Coastguard Worker static mqd_t mqd = -1;
26*49cdfc7eSAndroid Build Coastguard Worker static siginfo_t info;
27*49cdfc7eSAndroid Build Coastguard Worker static volatile int received;
28*49cdfc7eSAndroid Build Coastguard Worker
remove_mqueue(mqd_t mqd)29*49cdfc7eSAndroid Build Coastguard Worker static void remove_mqueue(mqd_t mqd)
30*49cdfc7eSAndroid Build Coastguard Worker {
31*49cdfc7eSAndroid Build Coastguard Worker if (mqd != -1)
32*49cdfc7eSAndroid Build Coastguard Worker SAFE_MQ_CLOSE(mqd);
33*49cdfc7eSAndroid Build Coastguard Worker
34*49cdfc7eSAndroid Build Coastguard Worker mq_unlink(MQNAME);
35*49cdfc7eSAndroid Build Coastguard Worker }
36*49cdfc7eSAndroid Build Coastguard Worker
child_signal_handler(LTP_ATTRIBUTE_UNUSED int sig,siginfo_t * si,LTP_ATTRIBUTE_UNUSED void * unused)37*49cdfc7eSAndroid Build Coastguard Worker static void child_signal_handler(LTP_ATTRIBUTE_UNUSED int sig, siginfo_t *si, LTP_ATTRIBUTE_UNUSED void *unused)
38*49cdfc7eSAndroid Build Coastguard Worker {
39*49cdfc7eSAndroid Build Coastguard Worker received = 1;
40*49cdfc7eSAndroid Build Coastguard Worker memcpy(&info, si, sizeof(info));
41*49cdfc7eSAndroid Build Coastguard Worker }
42*49cdfc7eSAndroid Build Coastguard Worker
child_func(void)43*49cdfc7eSAndroid Build Coastguard Worker static void child_func(void)
44*49cdfc7eSAndroid Build Coastguard Worker {
45*49cdfc7eSAndroid Build Coastguard Worker pid_t cpid, ppid;
46*49cdfc7eSAndroid Build Coastguard Worker struct sigaction sa;
47*49cdfc7eSAndroid Build Coastguard Worker struct sigevent notif;
48*49cdfc7eSAndroid Build Coastguard Worker mqd_t mqd_child;
49*49cdfc7eSAndroid Build Coastguard Worker
50*49cdfc7eSAndroid Build Coastguard Worker cpid = tst_getpid();
51*49cdfc7eSAndroid Build Coastguard Worker ppid = getppid();
52*49cdfc7eSAndroid Build Coastguard Worker
53*49cdfc7eSAndroid Build Coastguard Worker TST_EXP_EQ_LI(cpid, 1);
54*49cdfc7eSAndroid Build Coastguard Worker TST_EXP_EQ_LI(ppid, 0);
55*49cdfc7eSAndroid Build Coastguard Worker
56*49cdfc7eSAndroid Build Coastguard Worker TST_CHECKPOINT_WAIT(0);
57*49cdfc7eSAndroid Build Coastguard Worker
58*49cdfc7eSAndroid Build Coastguard Worker tst_res(TINFO, "Register notification on posix mqueue");
59*49cdfc7eSAndroid Build Coastguard Worker
60*49cdfc7eSAndroid Build Coastguard Worker mqd_child = SAFE_MQ_OPEN(MQNAME, O_RDONLY, 0, NULL);
61*49cdfc7eSAndroid Build Coastguard Worker notif.sigev_notify = SIGEV_SIGNAL;
62*49cdfc7eSAndroid Build Coastguard Worker notif.sigev_signo = SIGUSR1;
63*49cdfc7eSAndroid Build Coastguard Worker notif.sigev_value.sival_int = mqd_child;
64*49cdfc7eSAndroid Build Coastguard Worker
65*49cdfc7eSAndroid Build Coastguard Worker SAFE_MQ_NOTIFY(mqd_child, ¬if);
66*49cdfc7eSAndroid Build Coastguard Worker
67*49cdfc7eSAndroid Build Coastguard Worker sa.sa_flags = SA_SIGINFO;
68*49cdfc7eSAndroid Build Coastguard Worker SAFE_SIGEMPTYSET(&sa.sa_mask);
69*49cdfc7eSAndroid Build Coastguard Worker sa.sa_sigaction = child_signal_handler;
70*49cdfc7eSAndroid Build Coastguard Worker SAFE_SIGACTION(SIGUSR1, &sa, NULL);
71*49cdfc7eSAndroid Build Coastguard Worker
72*49cdfc7eSAndroid Build Coastguard Worker TST_CHECKPOINT_WAKE_AND_WAIT(0);
73*49cdfc7eSAndroid Build Coastguard Worker
74*49cdfc7eSAndroid Build Coastguard Worker if (received)
75*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "Signal notification has been received");
76*49cdfc7eSAndroid Build Coastguard Worker else
77*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "Signal notification has not been received");
78*49cdfc7eSAndroid Build Coastguard Worker
79*49cdfc7eSAndroid Build Coastguard Worker TST_EXP_EQ_LI(info.si_signo, SIGUSR1);
80*49cdfc7eSAndroid Build Coastguard Worker TST_EXP_EQ_LI(info.si_code, SI_MESGQ);
81*49cdfc7eSAndroid Build Coastguard Worker TST_EXP_EQ_LI(info.si_pid, 0);
82*49cdfc7eSAndroid Build Coastguard Worker }
83*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)84*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
85*49cdfc7eSAndroid Build Coastguard Worker {
86*49cdfc7eSAndroid Build Coastguard Worker remove_mqueue(mqd);
87*49cdfc7eSAndroid Build Coastguard Worker }
88*49cdfc7eSAndroid Build Coastguard Worker
run(void)89*49cdfc7eSAndroid Build Coastguard Worker static void run(void)
90*49cdfc7eSAndroid Build Coastguard Worker {
91*49cdfc7eSAndroid Build Coastguard Worker const struct tst_clone_args args = {
92*49cdfc7eSAndroid Build Coastguard Worker .flags = CLONE_NEWPID,
93*49cdfc7eSAndroid Build Coastguard Worker .exit_signal = SIGCHLD,
94*49cdfc7eSAndroid Build Coastguard Worker };
95*49cdfc7eSAndroid Build Coastguard Worker
96*49cdfc7eSAndroid Build Coastguard Worker remove_mqueue(mqd);
97*49cdfc7eSAndroid Build Coastguard Worker received = 0;
98*49cdfc7eSAndroid Build Coastguard Worker
99*49cdfc7eSAndroid Build Coastguard Worker if (!SAFE_CLONE(&args)) {
100*49cdfc7eSAndroid Build Coastguard Worker child_func();
101*49cdfc7eSAndroid Build Coastguard Worker return;
102*49cdfc7eSAndroid Build Coastguard Worker }
103*49cdfc7eSAndroid Build Coastguard Worker
104*49cdfc7eSAndroid Build Coastguard Worker mqd = SAFE_MQ_OPEN(MQNAME, O_RDWR | O_CREAT | O_EXCL, 0777, 0);
105*49cdfc7eSAndroid Build Coastguard Worker
106*49cdfc7eSAndroid Build Coastguard Worker TST_CHECKPOINT_WAKE_AND_WAIT(0);
107*49cdfc7eSAndroid Build Coastguard Worker
108*49cdfc7eSAndroid Build Coastguard Worker tst_res(TINFO, "Send mqueue message");
109*49cdfc7eSAndroid Build Coastguard Worker
110*49cdfc7eSAndroid Build Coastguard Worker SAFE_MQ_SEND(mqd, "pippo", 5, 1);
111*49cdfc7eSAndroid Build Coastguard Worker
112*49cdfc7eSAndroid Build Coastguard Worker TST_CHECKPOINT_WAKE(0);
113*49cdfc7eSAndroid Build Coastguard Worker }
114*49cdfc7eSAndroid Build Coastguard Worker
115*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
116*49cdfc7eSAndroid Build Coastguard Worker .test_all = run,
117*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup,
118*49cdfc7eSAndroid Build Coastguard Worker .forks_child = 1,
119*49cdfc7eSAndroid Build Coastguard Worker .needs_root = 1,
120*49cdfc7eSAndroid Build Coastguard Worker .needs_checkpoints = 1,
121*49cdfc7eSAndroid Build Coastguard Worker };
122