Home | History | Annotate | Download | only in pidns
      1 /*
      2  * Copyright (c) Huawei Technologies Co., Ltd., 2015
      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  *
      8  * This program is distributed in the hope that it will be useful,
      9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
     11  * the GNU General Public License for more details.
     12  */
     13 
     14 /*
     15  * Verify that:
     16  * The kernel imposes a limit of 32 nested levels of pid namespaces.
     17  */
     18 
     19 #define _GNU_SOURCE
     20 #include <sys/wait.h>
     21 #include <assert.h>
     22 #include <stdio.h>
     23 #include <stdlib.h>
     24 #include <unistd.h>
     25 #include <string.h>
     26 #include <errno.h>
     27 #include "pidns_helper.h"
     28 #include "test.h"
     29 
     30 #define MAXNEST 32
     31 
     32 char *TCID = "pidns32";
     33 int TST_TOTAL = 1;
     34 
     35 static void setup(void)
     36 {
     37 	tst_require_root();
     38 	check_newpid();
     39 	tst_tmpdir();
     40 }
     41 
     42 static void cleanup(void)
     43 {
     44 	tst_rmdir();
     45 }
     46 
     47 static int child_fn1(void *arg)
     48 {
     49 	pid_t cpid1;
     50 	long level = (long)arg;
     51 	int status;
     52 
     53 	if (level == MAXNEST)
     54 		return 0;
     55 	cpid1 = ltp_clone_quick(CLONE_NEWPID | SIGCHLD,
     56 		(void *)child_fn1, (void *)(level + 1));
     57 	if (cpid1 < 0) {
     58 		printf("level %ld:unexpected error: (%d) %s\n",
     59 			level, errno, strerror(errno));
     60 		return 1;
     61 	}
     62 	if (waitpid(cpid1, &status, 0) == -1)
     63 		return 1;
     64 
     65 	if (WIFEXITED(status) && WEXITSTATUS(status) != 0) {
     66 		printf("child exited abnormally\n");
     67 		return 1;
     68 	} else if (WIFSIGNALED(status)) {
     69 		printf("child was killed with signal = %d", WTERMSIG(status));
     70 		return 1;
     71 	}
     72 	return 0;
     73 }
     74 
     75 static void test_max_nest(void)
     76 {
     77 	pid_t cpid1;
     78 
     79 	cpid1 = ltp_clone_quick(CLONE_NEWPID | SIGCHLD,
     80 		(void *)child_fn1, (void *)1);
     81 	if (cpid1 < 0)
     82 		tst_brkm(TBROK | TERRNO, cleanup, "clone failed");
     83 
     84 	tst_record_childstatus(cleanup, cpid1);
     85 }
     86 
     87 int main(int argc, char *argv[])
     88 {
     89 	int lc;
     90 
     91 	setup();
     92 	tst_parse_opts(argc, argv, NULL, NULL);
     93 
     94 	for (lc = 0; TEST_LOOPING(lc); lc++) {
     95 		tst_count = 0;
     96 		test_max_nest();
     97 	}
     98 
     99 	cleanup();
    100 	tst_exit();
    101 }
    102