liyujie
2025-08-28 b3810562527858a3b3d98ffa6e9c9c5b0f4a9a8e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/* Copyright (c) 2015 Red Hat, Inc.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of version 2 the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Written by Matus Marhefka <mmarhefk@redhat.com>
 *
 ***********************************************************************
 * Creates a child process in the new specified namespace(s), child is then
 * daemonized and is running in the background. PID of the daemonized child
 * process is printed on the stdout. As the new namespace(s) is(are) maintained
 * by the daemonized child process it(they) can be removed by killing this
 * process.
 *
 */
 
#define _GNU_SOURCE
#include <sched.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <strings.h>
#include <errno.h>
#include "test.h"
#include "lapi/namespaces_constants.h"
#include "ns_common.h"
 
char *TCID = "ns_create";
 
 
void print_help(void)
{
   int i;
 
   printf("usage: ns_create <%s", params[0].name);
 
   for (i = 1; params[i].name; i++)
       printf("|,%s", params[i].name);
   printf(">\nThe only argument is a comma separated list "
          "of namespaces to create.\nExample: ns_create net,ipc\n");
}
 
static int child_fn(void *arg LTP_ATTRIBUTE_UNUSED)
{
   int i;
 
   if (setsid() == -1) {
       tst_resm(TINFO | TERRNO, "setsid");
       exit(1);
   }
 
   if (chdir("/") == -1) {
       tst_resm(TINFO | TERRNO, "chdir");
       exit(1);
   }
 
   /* close all inherrited file descriptors */
   for (i = 0; i < sysconf(_SC_OPEN_MAX); i++)
       close(i);
 
   pause();
   return 0;
}
 
/*
 * ./ns_create <ipc,mnt,net,pid,user,uts>
 */
int main(int argc, char *argv[])
{
   int pid, flags;
   char *token;
 
   if (argc < 2) {
       print_help();
       return 1;
   }
 
   flags = 0;
   while ((token = strsep(&argv[1], ","))) {
       struct param *p = get_param(token);
 
       if (!p) {
           tst_resm(TINFO, "Unknown namespace: %s", token);
           print_help();
           return 1;
       }
 
       flags |= p->flag;
   }
 
   pid = ltp_clone_quick(flags | SIGCHLD, child_fn, NULL);
   if (pid == -1) {
       tst_resm(TINFO | TERRNO, "ltp_clone_quick");
       return 1;
   }
 
   printf("%d", pid);
   return 0;
}