blob: 9f4427d78c76e42010b40f1bab32bac1bd5a0d48 [file] [log] [blame]
subrata_modak910f93a2008-12-16 12:34:49 +00001/*
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
Wanlong Gao4548c6c2012-10-19 18:03:36 +080013* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
subrata_modak910f93a2008-12-16 12:34:49 +000014*
15***************************************************************************
16* File: pidns12.c
17* *
18* * Description:
19* * The pidns12.c testcase verifies that siginfo->si_pid is set to 0
20* * if sender (parent process) is not in receiver's namespace.
21* *
22* * Test Assertion & Strategy:
23* * Create a PID namespace container.
24* * Initialise signal handler for SIGUSR1 in container.
25* * Let parent send SIGUSR1 to container.
26* * Check if sender pid is set to 0 from signal info.
27* *
28* * Usage: <for command-line>
29* * pidns12
30* *
31* * History:
32* * DATE NAME DESCRIPTION
33* * 13/11/08 Gowrishankar M Creation of this test.
34* * <gowrishankar.m@in.ibm.com>
35*
36******************************************************************************/
37#define _GNU_SOURCE 1
38#include <sys/wait.h>
39#include <sys/types.h>
40#include <signal.h>
41#include <string.h>
42#include <stdlib.h>
43#include <unistd.h>
44#include <stdio.h>
Garrett Coopere8530df2010-12-21 11:37:57 -080045#include "test.h"
subrata_modak910f93a2008-12-16 12:34:49 +000046#include <libclone.h>
Jan Stanceked991ae2014-07-29 10:50:27 +020047#include "pidns_helper.h"
subrata_modak910f93a2008-12-16 12:34:49 +000048
49char *TCID = "pidns12";
50int TST_TOTAL = 1;
51int errno;
52int pipefd[2];
53
54#define CHILD_PID 1
55#define PARENT_PID 0
56
57/*
58 * cleanup() - performs all ONE TIME cleanup for this test at
59 * completion or premature exit.
60 */
61void cleanup()
62{
subrata_modak910f93a2008-12-16 12:34:49 +000063
subrata_modak910f93a2008-12-16 12:34:49 +000064}
65
66/*
67 * child_signal_handler() - dummy function for sigaction()
68 */
Wanlong Gao354ebb42012-12-07 10:10:04 +080069static void child_signal_handler(int sig, siginfo_t * si, void *unused)
subrata_modak910f93a2008-12-16 12:34:49 +000070{
subrata_modakc6ca86c2009-05-26 07:26:51 +000071 /* Recieved SIGUSR1. Check sender pid */
72 if (si->si_pid == 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +080073 tst_resm(TPASS, "cinit: signalling PID (from other namespace)"
74 " is 0 as expected");
subrata_modakc6ca86c2009-05-26 07:26:51 +000075 else
Wanlong Gao354ebb42012-12-07 10:10:04 +080076 tst_resm(TFAIL, "cinit: signalling PID (from other namespace)"
77 " is not 0, but %d.", si->si_pid);
subrata_modak910f93a2008-12-16 12:34:49 +000078}
79
80/*
81 * child_fn() - Inside container
82 */
83int child_fn(void *arg)
84{
85 struct sigaction sa;
subrata_modak910f93a2008-12-16 12:34:49 +000086 pid_t pid, ppid;
87
88 /* Set process id and parent pid */
89 pid = getpid();
90 ppid = getppid();
91 if (pid != CHILD_PID || ppid != PARENT_PID) {
92 tst_resm(TBROK, "cinit: pidns is not created.");
93 cleanup();
94 }
95
96 /* Close read end of pipe */
97 close(pipefd[0]);
98
99 /* Set signal handler for SIGUSR1 */
100 sa.sa_flags = SA_SIGINFO;
101 sigfillset(&sa.sa_mask);
102 sa.sa_sigaction = child_signal_handler;
103 if (sigaction(SIGUSR1, &sa, NULL) == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800104 tst_resm(TBROK, "cinit: sigaction() failed(%s).",
105 strerror(errno));
subrata_modak910f93a2008-12-16 12:34:49 +0000106 cleanup();
107 }
108
subrata_modak910f93a2008-12-16 12:34:49 +0000109 /* Let parent to signal SIGUSR1 */
110 if (write(pipefd[1], "c:go\0", 5) != 5) {
111 tst_resm(TBROK, "cinit: pipe is broken to write");
112 cleanup();
113 }
114
subrata_modakc6ca86c2009-05-26 07:26:51 +0000115 sleep(3);
subrata_modak910f93a2008-12-16 12:34:49 +0000116
117 /* cleanup and exit */
118 close(pipefd[1]);
119 cleanup();
120
121 /* Control won't reach below */
122 exit(0);
123}
124
Jan Stanceked991ae2014-07-29 10:50:27 +0200125static void setup(void)
126{
127 tst_require_root(NULL);
128 check_newpid();
129}
130
subrata_modak910f93a2008-12-16 12:34:49 +0000131/***********************************************************************
132* M A I N
133***********************************************************************/
134
135int main(int argc, char *argv[])
136{
subrata_modakce23f152009-01-19 09:23:02 +0000137 int status;
subrata_modak910f93a2008-12-16 12:34:49 +0000138 pid_t pid, cpid;
139 char buf[5];
140
Jan Stanceked991ae2014-07-29 10:50:27 +0200141 setup();
142
subrata_modak910f93a2008-12-16 12:34:49 +0000143 pid = getpid();
144 tst_resm(TINFO, "parent: PID is %d", pid);
145
146 /* Create pipe for intercommunication */
147 if (pipe(pipefd) == -1) {
148 tst_resm(TBROK, "parent: pipe() failed. aborting!");
149 cleanup();
150 }
151
Wanlong Gao354ebb42012-12-07 10:10:04 +0800152 cpid = ltp_clone_quick(CLONE_NEWPID | SIGCHLD, child_fn, NULL);
subrata_modak910f93a2008-12-16 12:34:49 +0000153 if (cpid < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800154 tst_resm(TBROK, "parent: clone() failed(%s).", strerror(errno));
subrata_modak910f93a2008-12-16 12:34:49 +0000155 cleanup();
156 }
157
158 /* Close write end of pipe */
159 close(pipefd[1]);
160
161 /* Check if container is ready */
162 read(pipefd[0], buf, 5);
163 if (strcmp(buf, "c:go") != 0) {
164 tst_resm(TBROK, "parent: container did not respond!");
165 cleanup();
166 }
167
168 /* Send SIGUSR1 to container init */
169 if (kill(cpid, SIGUSR1) == -1) {
170 tst_resm(TBROK, "parent: kill() failed(%s).", strerror(errno));
171 cleanup();
172 }
173
174 if (waitpid(cpid, &status, 0) < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800175 tst_resm(TWARN, "parent: waitpid() failed(%s).",
176 strerror(errno));
subrata_modak910f93a2008-12-16 12:34:49 +0000177
178 if (WIFSIGNALED(status) && WTERMSIG(status))
Wanlong Gao354ebb42012-12-07 10:10:04 +0800179 tst_resm(TBROK, "child is terminated by signal(%s)",
180 strsignal(WTERMSIG(status)));
subrata_modak910f93a2008-12-16 12:34:49 +0000181
182 /* Cleanup and exit */
183 close(pipefd[0]);
184 cleanup();
185
186 /* Control won't reach below */
187 exit(0);
188
Chris Dearmanec6edca2012-10-17 19:54:01 -0700189}