• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 * File: pidns10.c
17 * *
18 * * Description:
19 * *  The pidns10.c testcase verifies inside the container, if kill(-1, signal)
20 * *  fails with ESRCH when there are no processes in container.
21 * *
22 * * Test Assertion & Strategy:
23 * *  Create a PID namespace container.
24 * *  Invoke kill(-1, SIGUSR1) inside container and check return code and error.
25 * *  kill() should have failed;except swapper & init, no process is inside.
26 * *
27 * * Usage: <for command-line>
28 * *  pidns10
29 * *
30 * * History:
31 * *  DATE      NAME                             DESCRIPTION
32 * *  13/11/08  Gowrishankar M 			Creation of this test.
33 * *            <gowrishankar.m@in.ibm.com>
34 *
35 ******************************************************************************/
36 #define _GNU_SOURCE 1
37 #include <sys/wait.h>
38 #include <sys/types.h>
39 #include <string.h>
40 #include <stdlib.h>
41 #include <unistd.h>
42 #include <stdio.h>
43 #include <errno.h>
44 #include "test.h"
45 #include "libclone.h"
46 #include "pidns_helper.h"
47 
48 char *TCID = "pidns10";
49 int TST_TOTAL = 1;
50 int errno;
51 
52 int child_fn(void *);
53 
54 #define CHILD_PID       1
55 #define PARENT_PID      0
56 
57 /*
58  * child_fn() - Inside container
59  */
child_fn(void * arg)60 int child_fn(void *arg)
61 {
62 	int exit_val, ret;
63 	pid_t pid, ppid;
64 
65 	/* Set process id and parent pid */
66 	pid = getpid();
67 	ppid = getppid();
68 	if (pid != CHILD_PID || ppid != PARENT_PID) {
69 		printf("cinit: pidns was not created.\n");
70 		return 1;
71 	}
72 
73 	if ((ret = kill(-1, SIGUSR1)) == -1 && errno == ESRCH) {
74 		printf("cinit: kill(-1, sig) failed with -1 / ESRCH as "
75 		       "expected\n");
76 		exit_val = 0;
77 	} else {
78 		printf("cinit: kill(-1, sig) didn't fail with -1 / ESRCH "
79 		       "(%d); failed with %d / %d instead", ESRCH, ret, errno);
80 		exit_val = 1;
81 	}
82 	exit(exit_val);
83 }
84 
setup(void)85 static void setup(void)
86 {
87 	tst_require_root();
88 	check_newpid();
89 }
90 
main(int argc,char * argv[])91 int main(int argc, char *argv[])
92 {
93 	int status;
94 	pid_t pid;
95 
96 	setup();
97 
98 	pid = getpid();
99 
100 	/* Container creation on PID namespace */
101 	TEST(do_clone_unshare_test(T_CLONE, CLONE_NEWPID, child_fn, NULL));
102 	if (TEST_RETURN == -1) {
103 		tst_brkm(TBROK | TTERRNO, NULL, "clone failed");
104 	}
105 
106 	sleep(1);
107 	if (wait(&status) < 0)
108 		tst_resm(TWARN, "parent: waitpid() failed.");
109 
110 	if (WIFEXITED(status) && WEXITSTATUS(status) != 0)
111 		tst_resm(TBROK, "container was terminated abnormally");
112 
113 	tst_exit();
114 }
115