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 
     17 * File: pidns02.c
     18 *
     19 * Description:
     20 *	The pidns02.c testcase builds into the ltp framework to verify
     21 *	the basic functionality of PID Namespace.
     22 *
     23 * Verify that:
     24 * 1. When parent clone a process with flag CLONE_NEWPID, the session ID of
     25 * child should be always one.
     26 *
     27 * 2. When parent clone a process with flag CLONE_NEWPID, the parent process group ID
     28 * should be always one.
     29 *
     30 * Total Tests
     31 *
     32 * Test Name: pidns02
     33 *
     34 * Test Assertion & Strategy:
     35 *
     36 * From main() clone a new child process with passing the clone_flag as CLONE_NEWPID,
     37 * Call the setid() inside container.
     38 * Inside the cloned pid check for the getsid(0) and getpgid(0)
     39 * Verify with global macro defined value for parent pid & child pid.
     40 *
     41 * Usage: <for command-line>
     42 * pidns02
     43 */
     44 
     45 #define _GNU_SOURCE
     46 #include <sys/wait.h>
     47 #include <assert.h>
     48 #include <stdio.h>
     49 #include <stdlib.h>
     50 #include <unistd.h>
     51 #include <string.h>
     52 #include <errno.h>
     53 #include "test.h"
     54 #include "libclone.h"
     55 #include "pidns_helper.h"
     56 
     57 char *TCID = "pidns02";
     58 int TST_TOTAL = 1;
     59 
     60 #define PGID	1
     61 #define SID	1
     62 
     63 /*
     64  * child_fn1() - Inside container
     65  */
     66 int child_fn1(void *vtest)
     67 {
     68 	pid_t pgid, sid;
     69 
     70 	setsid();
     71 
     72 	pgid = getpgid(0);
     73 	sid = getsid(0);
     74 
     75 	printf("Checking session id & group id inside container\n");
     76 	if (pgid == PGID && sid == SID) {
     77 		printf("Success: Got Group ID = %d & Session ID = %d\n",
     78 		       pgid, sid);
     79 		exit(0);
     80 	} else {
     81 		printf("Got unexpected result of Group ID = %d & Session ID = "
     82 		       "%d\n", pgid, sid);
     83 		exit(1);
     84 	}
     85 }
     86 
     87 static void setup(void)
     88 {
     89 	tst_require_root();
     90 	check_newpid();
     91 }
     92 
     93 int main(int argc, char *argv[])
     94 {
     95 	int status;
     96 
     97 	setup();
     98 
     99 	TEST(do_clone_unshare_test(T_CLONE, CLONE_NEWPID, child_fn1, NULL));
    100 	if (TEST_RETURN == -1) {
    101 		tst_brkm(TFAIL | TTERRNO, NULL, "clone failed");
    102 	} else if ((wait(&status)) == -1) {
    103 		tst_brkm(TFAIL | TERRNO, NULL, "wait failed");
    104 	}
    105 
    106 	if (WIFEXITED(status) && WEXITSTATUS(status) != 0) {
    107 		tst_resm(TFAIL | TERRNO, "child exited abnormally");
    108 	} else if (WIFSIGNALED(status)) {
    109 		tst_resm(TFAIL | TERRNO, "child exited with signal %d",
    110 			 WTERMSIG(status));
    111 	}
    112 
    113 	tst_exit();
    114 
    115 }
    116