blob: 1928d205f4b1ba53de5370a523e013292e9a9610 [file] [log] [blame]
subrata_modak698b9aa2008-12-15 13:13:16 +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_modak698b9aa2008-12-15 13:13:16 +000014*
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>
Garrett Coopere8530df2010-12-21 11:37:57 -080044#include "test.h"
yaberauneya7f140ab2009-12-06 20:55:15 +000045#define CLEANUP cleanup
46#include "libclone.h"
Jan Stanceked991ae2014-07-29 10:50:27 +020047#include "pidns_helper.h"
subrata_modak698b9aa2008-12-15 13:13:16 +000048
49char *TCID = "pidns10";
50int TST_TOTAL = 1;
51int errno;
52
53int child_fn(void *);
subrata_modak698b9aa2008-12-15 13:13:16 +000054
55#define CHILD_PID 1
56#define PARENT_PID 0
57
58/*
59 * child_fn() - Inside container
60 */
61int child_fn(void *arg)
62{
Garrett Cooperad14e902010-12-16 10:03:44 -080063 int exit_val, ret;
subrata_modak698b9aa2008-12-15 13:13:16 +000064 pid_t pid, ppid;
65
66 /* Set process id and parent pid */
67 pid = getpid();
68 ppid = getppid();
69 if (pid != CHILD_PID || ppid != PARENT_PID) {
Garrett Cooperad14e902010-12-16 10:03:44 -080070 printf("cinit: pidns was not created.\n");
71 return 1;
subrata_modak698b9aa2008-12-15 13:13:16 +000072 }
73
Garrett Cooperad14e902010-12-16 10:03:44 -080074 if ((ret = kill(-1, SIGUSR1)) == -1 && errno == ESRCH) {
75 printf("cinit: kill(-1, sig) failed with -1 / ESRCH as "
Wanlong Gao354ebb42012-12-07 10:10:04 +080076 "expected\n");
Garrett Cooperad14e902010-12-16 10:03:44 -080077 exit_val = 0;
78 } else {
79 printf("cinit: kill(-1, sig) didn't fail with -1 / ESRCH "
Wanlong Gao354ebb42012-12-07 10:10:04 +080080 "(%d); failed with %d / %d instead", ESRCH, ret, errno);
Garrett Cooperad14e902010-12-16 10:03:44 -080081 exit_val = 1;
subrata_modak698b9aa2008-12-15 13:13:16 +000082 }
Garrett Cooperad14e902010-12-16 10:03:44 -080083 exit(exit_val);
subrata_modak698b9aa2008-12-15 13:13:16 +000084}
85
Jan Stanceked991ae2014-07-29 10:50:27 +020086static void setup(void)
87{
88 tst_require_root(NULL);
89 check_newpid();
90}
91
subrata_modak698b9aa2008-12-15 13:13:16 +000092int main(int argc, char *argv[])
93{
Garrett Cooperad14e902010-12-16 10:03:44 -080094 int status;
subrata_modak698b9aa2008-12-15 13:13:16 +000095 pid_t pid;
96
Jan Stanceked991ae2014-07-29 10:50:27 +020097 setup();
98
subrata_modak698b9aa2008-12-15 13:13:16 +000099 pid = getpid();
100
101 /* Container creation on PID namespace */
Garrett Cooperad14e902010-12-16 10:03:44 -0800102 TEST(do_clone_unshare_test(T_CLONE, CLONE_NEWPID, child_fn, NULL));
103 if (TEST_RETURN == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800104 tst_brkm(TBROK | TTERRNO, CLEANUP, "clone failed");
subrata_modak698b9aa2008-12-15 13:13:16 +0000105 }
106
107 sleep(1);
108 if (wait(&status) < 0)
109 tst_resm(TWARN, "parent: waitpid() failed.");
110
Garrett Cooperad14e902010-12-16 10:03:44 -0800111 if (WIFEXITED(status) && WEXITSTATUS(status) != 0)
112 tst_resm(TBROK, "container was terminated abnormally");
subrata_modak698b9aa2008-12-15 13:13:16 +0000113
yaberauneya7f140ab2009-12-06 20:55:15 +0000114 CLEANUP();
Garrett Cooperad14e902010-12-16 10:03:44 -0800115 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800116}
subrata_modak698b9aa2008-12-15 13:13:16 +0000117
118/*
yaberauneya7f140ab2009-12-06 20:55:15 +0000119 * cleanup() - performs all ONE TIME CLEANUP for this test at
subrata_modak698b9aa2008-12-15 13:13:16 +0000120 * completion or premature exit.
121 */
yaberauneya7f140ab2009-12-06 20:55:15 +0000122static void cleanup()
subrata_modak698b9aa2008-12-15 13:13:16 +0000123{
subrata_modak698b9aa2008-12-15 13:13:16 +0000124
Chris Dearmanec6edca2012-10-17 19:54:01 -0700125}