blob: c5b40486ee02cf56313ce6f78750dfc937ffa029 [file] [log] [blame]
subrata_modak66e08502008-11-07 09:06:01 +00001/*
2* Copyright (c) International Business Machines Corp., 2008
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_modak66e08502008-11-07 09:06:01 +000014
15*************************************************************************
16* Description:
17* Testcase tries killing of the parent namespace pid by the container-init.
18* It also tries killing of non-existent PID, by the container-init.
19* Returns Success if Unable to kill, and proper error number is set.
20* else Returns Failure
21*
22* Steps:
23* 1. Parent process clone a process with flag CLONE_NEWPID
24* 2. The pid of the parent namespace is passed to the container.
25* 3. Container receieves the PID and passes SIGKILL to this PID.
26* 4. If kill() is unsuccessful and the errno is set to 'No Such process'
27* then sets PASS
28* else,
29* sets FAIL
30* 5. It also verifies by passing SIGKILL to FAKE_PID
31* 6. If kill() is unsuccessful and the errno is set to 'No Such process'
32* then sets PASS
33* else,
34* sets FAIL
35*
subrata_modak66e08502008-11-07 09:06:01 +000036*******************************************************************************/
37#define _GNU_SOURCE 1
38#include <stdio.h>
39#include <stdlib.h>
40#include <sys/wait.h>
41#include <assert.h>
42#include <unistd.h>
43#include <errno.h>
Garrett Coopere8530df2010-12-21 11:37:57 -080044#include "test.h"
subrata_modak66e08502008-11-07 09:06:01 +000045#include <libclone.h>
46#include <signal.h>
Jan Stanceked991ae2014-07-29 10:50:27 +020047#include "pidns_helper.h"
subrata_modak66e08502008-11-07 09:06:01 +000048
49#define CINIT_PID 1
50#define PARENT_PID 0
Garrett Cooperad14e902010-12-16 10:03:44 -080051#define FAKE_PID -1
subrata_modak66e08502008-11-07 09:06:01 +000052
53char *TCID = "pidns06";
54int TST_TOTAL = 1;
55
subrata_modak66e08502008-11-07 09:06:01 +000056void cleanup()
57{
subrata_modak66e08502008-11-07 09:06:01 +000058}
59
60/*
61 * kill_pid_in_childfun()
62 * Cont-init tries to kill the parent-process using parent's global Pid.
63 * Also checks passing SIGKILL to non existent PID in the container.
64 */
65static int kill_pid_in_childfun(void *vtest)
66{
67 int cpid, ppid, *par_pid;
68 int ret = 0;
69 cpid = getpid();
70 ppid = getppid();
71 par_pid = (int *)vtest;
72
73 /* Checking the values to make sure pidns is created correctly */
Garrett Cooperad14e902010-12-16 10:03:44 -080074 if (cpid != CINIT_PID || ppid != PARENT_PID) {
75 printf("Unexpected result for Container: init "
Wanlong Gao354ebb42012-12-07 10:10:04 +080076 "pid=%d ppid=%d\n", cpid, ppid);
Garrett Cooperad14e902010-12-16 10:03:44 -080077 exit(1);
subrata_modak66e08502008-11-07 09:06:01 +000078 }
79
80 /*
Garrett Cooperad14e902010-12-16 10:03:44 -080081 * While trying kill() of the pid of the parent namespace..
82 * Check to see if the errno was set to the expected, value of 3 : ESRCH
83 */
subrata_modak66e08502008-11-07 09:06:01 +000084 ret = kill(*par_pid, SIGKILL);
85 if (ret == -1 && errno == ESRCH) {
Garrett Cooperad14e902010-12-16 10:03:44 -080086 printf("Container: killing parent pid=%d failed as expected "
Wanlong Gao354ebb42012-12-07 10:10:04 +080087 "with ESRCH\n", *par_pid);
subrata_modak66e08502008-11-07 09:06:01 +000088 } else {
Garrett Cooperad14e902010-12-16 10:03:44 -080089 printf("Container: killing parent pid=%d, didn't fail as "
Wanlong Gao354ebb42012-12-07 10:10:04 +080090 "expected with ESRCH (%d) and a return value of -1. Got "
91 "%d (\"%s\") and a return value of %d instead.\n",
92 *par_pid, ESRCH, errno, strerror(errno), ret);
Garrett Cooperad14e902010-12-16 10:03:44 -080093 exit(1);
subrata_modak66e08502008-11-07 09:06:01 +000094 }
95 /*
Garrett Cooperad14e902010-12-16 10:03:44 -080096 * While killing non-existent pid in the container,
97 * Check to see if the errno was set to the expected, value of 3 : ESRCH
98 */
subrata_modak66e08502008-11-07 09:06:01 +000099 ret = kill(FAKE_PID, SIGKILL);
100 if (ret == -1 && errno == ESRCH) {
Garrett Cooperad14e902010-12-16 10:03:44 -0800101 printf("Container: killing non-existent pid failed as expected "
Wanlong Gao354ebb42012-12-07 10:10:04 +0800102 "with ESRCH\n");
subrata_modak66e08502008-11-07 09:06:01 +0000103 } else {
Garrett Cooperad14e902010-12-16 10:03:44 -0800104 printf("Container: killing non-existent pid, didn't fail as "
Wanlong Gao354ebb42012-12-07 10:10:04 +0800105 "expected with ESRCH (%d) and a return value of -1. Got "
106 "%d (\"%s\") and a return value of %d instead.\n",
107 ESRCH, errno, strerror(errno), ret);
Garrett Cooperad14e902010-12-16 10:03:44 -0800108 exit(1);
subrata_modak66e08502008-11-07 09:06:01 +0000109 }
110
Garrett Cooperad14e902010-12-16 10:03:44 -0800111 exit(0);
subrata_modak66e08502008-11-07 09:06:01 +0000112}
113
Jan Stanceked991ae2014-07-29 10:50:27 +0200114static void setup(void)
115{
116 tst_require_root(NULL);
117 check_newpid();
118}
119
subrata_modak66e08502008-11-07 09:06:01 +0000120int main()
121{
Garrett Cooperad14e902010-12-16 10:03:44 -0800122 int status;
Jan Stanceked991ae2014-07-29 10:50:27 +0200123
124 setup();
125
subrata_modak66e08502008-11-07 09:06:01 +0000126 pid_t pid = getpid();
127
128 tst_resm(TINFO, "Parent: Passing the pid of the process %d", pid);
Garrett Cooperad14e902010-12-16 10:03:44 -0800129 TEST(do_clone_unshare_test(T_CLONE, CLONE_NEWPID, kill_pid_in_childfun,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800130 (void *)&pid));
Garrett Cooperad14e902010-12-16 10:03:44 -0800131 if (TEST_RETURN == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800132 tst_brkm(TFAIL | TERRNO, cleanup, "clone failed");
Garrett Cooperad14e902010-12-16 10:03:44 -0800133 } else if (wait(&status) == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800134 tst_brkm(TFAIL | TERRNO, cleanup, "wait failed");
subrata_modak66e08502008-11-07 09:06:01 +0000135 }
136
subrata_modak66e08502008-11-07 09:06:01 +0000137 cleanup();
Garrett Cooper2c282152010-12-16 00:55:50 -0800138 tst_exit();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700139}