1 /*
2 * Copyright (c) International Business Machines Corp., 2007
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
10 * the GNU General Public License for more details.
11 * You should have received a copy of the GNU General Public License
12 * along with this program; if not, write to the Free Software
13 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
14 *
15 ***************************************************************************
16
17 * * Test Assertion.
18 * *----------------
19 * * kill -USR1 container_init
20 * * - from the parent process and also inside a container
21 * * - Where init has defined a custom handler for USR1
22 * * - Should call the handler and
23 * * - Verify whether the signal handler is called from the proper process.
24 * *
25 * * Description:
26 * * Create PID namespace container.
27 * * Container init defines the handler for SIGUSR1 and waits indefinetly.
28 * * Parent sends SIGUSR1 to container init.
29 * * The signal handler is handled and the cont-init resumes normally.
30 * * From the container, again the signal SIGUSR1 is sent.
31 * * In the sig-handler check if it's invoked from correct pid(parent/container)
32 * * If cont-init wakes up properly -
33 * * it will return expected value at exit which is verified at the end.
34 * *
35 * * History:
36 * * DATE NAME DESCRIPTION
37 * * 04/11/08 Veerendra C <vechandr@in.ibm.com> Verifying cont init kill -USR1
38 *
39 *******************************************************************************/
40 #include "config.h"
41
42 #define _GNU_SOURCE 1
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <sys/wait.h>
46 #include <sys/types.h>
47 #include <signal.h>
48 #include <unistd.h>
49 #include "test.h"
50 #include <libclone.h>
51 #include "pidns_helper.h"
52
53 #define CHILD_PID 1
54 #define PARENT_PID 0
55
56 char *TCID = "pidns16";
57 int TST_TOTAL = 3;
58
child_signal_handler(int sig,siginfo_t * si,void * unused)59 void child_signal_handler(int sig, siginfo_t * si, void *unused)
60 {
61 static int c = 1;
62 pid_t expected_pid;
63
64 /* Verifying from which process the signal handler is signalled */
65
66 switch (c) {
67 case 1:
68 expected_pid = PARENT_PID;
69 break;
70 case 2:
71 expected_pid = CHILD_PID;
72 break;
73 default:
74 tst_resm(TBROK, "child should NOT be signalled 3+ times");
75 return;
76 }
77
78 if (si->si_pid == expected_pid)
79 tst_resm(TPASS, "child is signalled from expected pid %d",
80 expected_pid);
81 else
82 tst_resm(TFAIL, "child is signalled from unexpected pid %d,"
83 " expecting pid %d", si->si_pid, expected_pid);
84
85 c++;
86 }
87
88 /*
89 * child_fn() - Inside container
90 */
child_fn(void * ttype)91 int child_fn(void *ttype)
92 {
93 struct sigaction sa;
94 pid_t pid, ppid;
95
96 /* Set process id and parent pid */
97 pid = getpid();
98 ppid = getppid();
99
100 if ((pid != CHILD_PID) || (ppid != PARENT_PID))
101 tst_resm(TBROK, "pidns is not created.");
102
103 /* Set signal handler for SIGUSR1, also mask other signals */
104 sa.sa_flags = SA_SIGINFO;
105 sigemptyset(&sa.sa_mask);
106 sa.sa_sigaction = child_signal_handler;
107 if (sigaction(SIGUSR1, &sa, NULL) == -1)
108 tst_resm(TBROK, "%d: sigaction() failed", pid);
109
110 pause();
111 tst_resm(TINFO, "Container: Resumed after receiving SIGUSR1 "
112 "from parentNS ");
113 if (kill(pid, SIGUSR1) != 0) {
114 tst_resm(TFAIL, "kill(SIGUSR1) fails.");
115 }
116 tst_resm(TINFO, "Container: Resumed after sending SIGUSR1 "
117 "from container itself");
118 _exit(10);
119 }
120
setup(void)121 static void setup(void)
122 {
123 tst_require_root();
124 check_newpid();
125 }
126
127 /***********************************************************************
128 * M A I N
129 ***********************************************************************/
main(int argc,char * argv[])130 int main(int argc, char *argv[])
131 {
132 int status;
133 pid_t cpid;
134
135 setup();
136
137 cpid = ltp_clone_quick(CLONE_NEWPID | SIGCHLD, child_fn, NULL);
138
139 if (cpid < 0) {
140 tst_resm(TBROK, "clone() failed.");
141 }
142
143 sleep(1);
144 if (kill(cpid, SIGUSR1) != 0) {
145 tst_resm(TFAIL, "kill(SIGUSR1) fails.");
146 }
147 sleep(1);
148 if (waitpid(cpid, &status, 0) < 0)
149 tst_resm(TWARN, "waitpid() failed.");
150
151 if ((WIFEXITED(status)) && (WEXITSTATUS(status) == 10))
152 tst_resm(TPASS, "container init continued successfuly, "
153 "after handling signal -USR1");
154 else
155 tst_resm(TFAIL, "c-init failed to continue after "
156 "passing kill -USR1");
157 tst_exit();
158 }
159