aboutsummaryrefslogtreecommitdiff
path: root/testcases/kernel/containers/pidns/pidns12.c
blob: 5fb13d2a6ac9ec31b708090841e2ca4f904f877e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
* Copyright (c) International Business Machines Corp., 2007
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
* the GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
***************************************************************************
* File: pidns12.c
* *
* * Description:
* *  The pidns12.c testcase verifies that siginfo->si_pid is set to 0
* *  if sender (parent process) is not in receiver's namespace.
* *
* * Test Assertion & Strategy:
* *  Create a PID namespace container.
* *  Initialise signal handler for SIGUSR1 in container.
* *  Let parent send SIGUSR1 to container.
* *  Check if sender pid is set to 0 from signal info.
* *
* * Usage: <for command-line>
* *  pidns12
* *
* * History:
* *  DATE      NAME                             DESCRIPTION
* *  13/11/08  Gowrishankar M 			Creation of this test.
* *            <gowrishankar.m@in.ibm.com>
*
******************************************************************************/
#define _GNU_SOURCE 1
#include <sys/wait.h>
#include <sys/types.h>
#include <signal.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include "pidns_helper.h"
#include "test.h"

char *TCID = "pidns12";
int TST_TOTAL = 1;
int pipefd[2];

#define CHILD_PID       1
#define PARENT_PID      0

/*
 * child_signal_handler() - dummy function for sigaction()
 */
static void child_signal_handler(int sig, siginfo_t * si, void *unused)
{
	/* Recieved SIGUSR1. Check sender pid */
	if (si->si_pid == 0)
		tst_resm(TPASS, "cinit: signalling PID (from other namespace)"
			 " is 0 as expected");
	else
		tst_resm(TFAIL, "cinit: signalling PID (from other namespace)"
			 " is not 0, but %d.", si->si_pid);
}

/*
 * child_fn() - Inside container
 */
int child_fn(void *arg)
{
	struct sigaction sa;
	pid_t pid, ppid;

	/* Set process id and parent pid */
	pid = getpid();
	ppid = getppid();
	if (pid != CHILD_PID || ppid != PARENT_PID) {
		tst_resm(TBROK, "cinit: pidns is not created.");
	}

	/* Close read end of pipe */
	close(pipefd[0]);

	/* Set signal handler for SIGUSR1 */
	sa.sa_flags = SA_SIGINFO;
	sigfillset(&sa.sa_mask);
	sa.sa_sigaction = child_signal_handler;
	if (sigaction(SIGUSR1, &sa, NULL) == -1) {
		tst_resm(TBROK, "cinit: sigaction() failed(%s).",
			 strerror(errno));
	}

	/* Let parent to signal SIGUSR1 */
	if (write(pipefd[1], "c:go\0", 5) != 5) {
		tst_resm(TBROK, "cinit: pipe is broken to write");
	}

	sleep(3);

	/* cleanup and exit */
	close(pipefd[1]);

	/* Control won't reach below */
	exit(0);
}

static void setup(void)
{
	tst_require_root();
	check_newpid();
}

/***********************************************************************
*   M A I N
***********************************************************************/

int main(void)
{
	int status;
	pid_t pid, cpid;
	char buf[5];

	setup();

	pid = getpid();
	tst_resm(TINFO, "parent: PID is %d", pid);

	/* Create pipe for intercommunication */
	if (pipe(pipefd) == -1) {
		tst_resm(TBROK, "parent: pipe() failed. aborting!");
	}

	cpid = ltp_clone_quick(CLONE_NEWPID | SIGCHLD, child_fn, NULL);
	if (cpid < 0) {
		tst_resm(TBROK, "parent: clone() failed(%s).", strerror(errno));
	}

	/* Close write end of pipe */
	close(pipefd[1]);

	/* Check if container is ready */
	read(pipefd[0], buf, 5);
	if (strcmp(buf, "c:go") != 0) {
		tst_resm(TBROK, "parent: container did not respond!");
	}

	/* Send SIGUSR1 to container init */
	if (kill(cpid, SIGUSR1) == -1) {
		tst_resm(TBROK, "parent: kill() failed(%s).", strerror(errno));
	}

	if (waitpid(cpid, &status, 0) < 0)
		tst_resm(TWARN, "parent: waitpid() failed(%s).",
			 strerror(errno));

	if (WIFSIGNALED(status) && WTERMSIG(status))
		tst_resm(TBROK, "child is terminated by signal(%s)",
			 strsignal(WTERMSIG(status)));

	/* Cleanup and exit */
	close(pipefd[0]);

	/* Control won't reach below */
	exit(0);

}