Home | History | Annotate | Download | only in pidns
      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 (at) 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 "pidns_helper.h"
     45 #include "test.h"
     46 
     47 char *TCID = "pidns10";
     48 int TST_TOTAL = 1;
     49 
     50 int child_fn(void *);
     51 
     52 #define CHILD_PID       1
     53 #define PARENT_PID      0
     54 
     55 /*
     56  * child_fn() - Inside container
     57  */
     58 int child_fn(void *arg)
     59 {
     60 	int exit_val, ret;
     61 	pid_t pid, ppid;
     62 
     63 	/* Set process id and parent pid */
     64 	pid = getpid();
     65 	ppid = getppid();
     66 	if (pid != CHILD_PID || ppid != PARENT_PID) {
     67 		printf("cinit: pidns was not created.\n");
     68 		return 1;
     69 	}
     70 
     71 	if ((ret = kill(-1, SIGUSR1)) == -1 && errno == ESRCH) {
     72 		printf("cinit: kill(-1, sig) failed with -1 / ESRCH as "
     73 		       "expected\n");
     74 		exit_val = 0;
     75 	} else {
     76 		printf("cinit: kill(-1, sig) didn't fail with -1 / ESRCH "
     77 		       "(%d); failed with %d / %d instead", ESRCH, ret, errno);
     78 		exit_val = 1;
     79 	}
     80 	exit(exit_val);
     81 }
     82 
     83 static void setup(void)
     84 {
     85 	tst_require_root();
     86 	check_newpid();
     87 }
     88 
     89 int main(int argc, char *argv[])
     90 {
     91 	int status;
     92 	pid_t pid;
     93 
     94 	setup();
     95 
     96 	pid = getpid();
     97 
     98 	/* Container creation on PID namespace */
     99 	TEST(do_clone_unshare_test(T_CLONE, CLONE_NEWPID, child_fn, NULL));
    100 	if (TEST_RETURN == -1) {
    101 		tst_brkm(TBROK | TTERRNO, NULL, "clone failed");
    102 	}
    103 
    104 	sleep(1);
    105 	if (wait(&status) < 0)
    106 		tst_resm(TWARN, "parent: waitpid() failed.");
    107 
    108 	if (WIFEXITED(status) && WEXITSTATUS(status) != 0)
    109 		tst_resm(TBROK, "container was terminated abnormally");
    110 
    111 	tst_exit();
    112 }
    113