Imported Upstream version 20081219
[ltp-debian.git] / testcases / kernel / containers / pidns / pidns10.c
blobab2a81664fc9adf49d5402bd9e8fac42fa664893
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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>
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 <usctest.h>
45 #include <test.h>
46 #include <libclone.h>
48 char *TCID = "pidns10";
49 int TST_TOTAL = 1;
50 int errno;
52 int child_fn(void *);
53 void cleanup(void);
55 #define CHILD_PID 1
56 #define PARENT_PID 0
59 * child_fn() - Inside container
61 int child_fn(void *arg)
63 pid_t pid, ppid;
65 /* Set process id and parent pid */
66 pid = getpid();
67 ppid = getppid();
68 if (pid != CHILD_PID || ppid != PARENT_PID) {
69 tst_resm(TBROK, "cinit: pidns is not created.");
70 cleanup();
73 if (kill(-1, SIGUSR1) != -1) {
74 tst_resm(TFAIL, "cinit: kill(-1, sig) should have failed");
75 cleanup();
78 if (errno == ESRCH)
79 tst_resm(TPASS, "cinit: expected kill(-1, sig) failure.");
80 else
81 tst_resm(TFAIL, "cinit: kill(-1, sig) failure is not ESRCH, "
82 "but %s", strerror(errno));
84 /* cleanup and exit */
85 cleanup();
86 exit(0);
89 /***********************************************************************
90 * M A I N
91 ***********************************************************************/
93 int main(int argc, char *argv[])
95 int status, ret;
96 pid_t pid;
98 pid = getpid();
100 /* Container creation on PID namespace */
101 ret = do_clone_unshare_test(T_CLONE,\
102 CLONE_NEWPID, child_fn, NULL);
103 if (ret != 0) {
104 tst_resm(TBROK, "parent: clone() failed. rc=%d(%s)",\
105 ret, strerror(errno));
106 /* Cleanup & continue with next test case */
107 cleanup();
110 sleep(1);
111 if (wait(&status) < 0)
112 tst_resm(TWARN, "parent: waitpid() failed.");
114 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
115 tst_resm(TBROK, "parent: container was terminated by %s",\
116 strsignal(WTERMSIG(status)));
118 cleanup();
119 exit(0);
120 } /* End main */
123 * cleanup() - performs all ONE TIME cleanup for this test at
124 * completion or premature exit.
126 void cleanup()
128 /* Clean the test testcase as LTP wants*/
129 TEST_CLEANUP;
131 /* exit with return code appropriate for results */
132 tst_exit();