-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathnsexec.c
187 lines (149 loc) · 5.13 KB
/
nsexec.c
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/* ns_exec.c
Create a child process that executes a shell command in new
namespace(s);
Copyright (C) 2017 Vilibald Wanča
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
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, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#define _GNU_SOURCE
#include <sched.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <sys/mount.h>
#include <signal.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <errno.h>
#include <getopt.h>
#ifndef FALSE
# define FALSE 0
#endif
#ifndef TRUE
# define TRUE 1
#endif
/* A simple error-handling function: print an error message based
on the value in 'errno' and terminate the calling process */
#define die(msg) \
do { \
perror(msg); \
exit(EXIT_FAILURE); \
} while (0)
/* Quick logging macro to allow logging iff verbose output */
#define LOG(x...) { if (verbose) { fprintf(stdout, x); fprintf(stdout, "\n"); } }
struct child_args {
char *hostname;
char **argv; /* Command to be executed by child, with args */
int pipe_fd[2]; /* Pipe used to synchronize parent and child */
};
struct clone_stack {
char stack[4096] __attribute__ ((aligned(16))); /* Stack for the clone call */
char ptr[0];
};
static int verbose;
static void
usage(char *name)
{
printf("Create a child process that executes a shell command in new namespace(s),\n");
printf("Usage: %s [OPTIONS] <CMD>\n\n", name);
printf("\
-h, --help print this help\n");
printf("\
-n, --net new network namespace\n");
printf("\
-p, --pid new PID namespace\n");
printf("\
-u, --uts HOSTNAME new UTS namespace\n");
printf("\
-v, --verbose more verbose output\n\n");
printf("\
<CMD> command to be executed\n");
exit(EXIT_FAILURE);
}
static int
childFunc(void *arg)
{
struct child_args *args = (struct child_args *) arg;
char ch;
int err = 0;
close(args->pipe_fd[1]); /* Close our descriptor for the write
end of the pipe so that we see EOF
when parent closes its descriptor */
if (read(args->pipe_fd[0], &ch, 1) != 0) {
die("Failure in child: read from pipe returned != 0\n");
}
if (mount("/", "/", "none", MS_PRIVATE | MS_REC, NULL) < 0 ) {
die("mount private");
}
if (umount2("/proc", MNT_DETACH) < 0) die("unmount proc");
if (mount("proc", "/proc", "proc", 0, NULL) < 0) die("mount proc");
LOG("/proc mounted");
/* Set hostname if present */
if (args->hostname) {
sethostname(args->hostname,strlen(args->hostname));
}
/* Execute a shell command */
LOG("Executing %s\n", args->argv[0]);
err = execvp(args->argv[0], args->argv);
if (0 != err) {
printf("execvp error: %s\n", strerror(errno));
}
return err;
}
int
main(int argc, char *argv[])
{
int flags = SIGCHLD | CLONE_NEWNS;
int long_index = 0;
int opt;
pid_t child_pid;
struct child_args args;
struct clone_stack stack;
verbose = 0;
args.hostname = NULL;
/*COMMAND LINE ARGS*/
static const struct option long_opts[] = {
{ "help", no_argument, NULL, 'h' },
{ "net", no_argument, NULL, 'n' },
{ "pid", no_argument, NULL, 'p' },
{ "uts", required_argument, NULL, 'u' },
{ "verbose", no_argument, NULL, 'v' },
{ NULL, 0, NULL, 0 }
};
while ((opt = getopt_long(argc, argv,"+inpu:v",
long_opts, &long_index )) != -1) {
switch (opt) {
case 'n': flags |= CLONE_NEWNET; break;
case 'p': flags |= CLONE_NEWPID; break;
case 'u':
flags |= CLONE_NEWUTS;
args.hostname = optarg;
break;
case 'v': verbose = TRUE; break;
default: usage(argv[0]);
}
}
args.argv = &argv[optind];
if (pipe(args.pipe_fd) == -1) die("pipe");
child_pid = clone(childFunc, stack.ptr, flags, &args);
if (child_pid == -1) die("clone");
LOG("%s: child created with PID %ld\n",
argv[0], (long) child_pid);
/* Close the pipe to signal the child to proceeed forward */
close(args.pipe_fd[1]);
LOG("waiting for child to terminate\n");
if (waitpid(child_pid, NULL, 0) == -1) die("waitpid");
LOG("%s: terminating\n", argv[0]);
exit(EXIT_SUCCESS);
}