-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathns_exec.c
327 lines (278 loc) · 6.57 KB
/
ns_exec.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*
* Copyright 2008 IBM Corp.
*/
#include <stdio.h>
#include <stdlib.h>
#include <sched.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/syscall.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <errno.h>
#include <libgen.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "clone.h"
extern pid_t getpgid(pid_t pid);
extern pid_t getsid(pid_t pid);
static const char* procname;
static void usage(const char *name)
{
printf("usage: %s [-h] [-c] [-mnuUip] [-P <pid-file>]"
"[command [arg ..]]\n", name);
printf("\n");
printf(" -h this message\n");
printf("\n");
printf(" -c use 'clone' rather than 'unshare' system call\n");
printf(" -g launch in new cgroup\n");
printf(" -m mount namespace\n");
printf(" -n network namespace\n");
printf(" -u utsname namespace\n");
printf(" -U userid namespace\n");
printf(" -i ipc namespace\n");
printf(" -P <pid-file> File in which to write global pid of cinit\n");
printf(" -p pid namespace\n");
printf(" -f <flag> extra clone flags\n");
printf("\n");
printf("(C) Copyright IBM Corp. 2006\n");
printf("\n");
exit(1);
}
#if 0
// gcc complains about this method not being used
static void print_my_info(const char *procname, char *ttyname)
{
printf("procname %s, ttyname %s, pid %d, ppid %d, pgid %d, sid %d\n",
procname, ttyname, getpid(), getppid(), getpgid(0),
getsid(0));
}
#endif
static int string_to_ul(const char *str, unsigned long int *res)
{
char *tail;
long long int r;
if (!*str)
return -1;
errno = 0;
r = strtol(str, &tail, 16);
/*
* according to strtol(3), if errno is set or tail does no point
* to the ending '\0', the conversion failed.
*/
if (errno || *tail)
return -1;
*res = r;
return 0;
}
/*
* Copied following opentty() from Fedora's util-linux rpm
* I just changed the "FATAL" message below from syslog()
* to printf
*/
static void
opentty(const char * tty) {
int i, fd, flags;
fd = open(tty, O_RDWR | O_NONBLOCK);
if (fd == -1) {
printf("FATAL: can't reopen tty: %s", strerror(errno));
sleep(1);
exit(1);
}
flags = fcntl(fd, F_GETFL);
flags &= ~O_NONBLOCK;
fcntl(fd, F_SETFL, flags);
for (i = 0; i < fd; i++)
close(i);
for (i = 0; i < 3; i++)
if (fd != i)
dup2(fd, i);
if (fd >= 3)
close(fd);
}
// Code copy end
int do_newcgrp = 0;
int load_cgroup_dir(char *dest, int len)
{
FILE *f = fopen("/proc/mounts", "r");
char buf[200];
char *name, *path, *fsname, *options, *p1=NULL, *p2=NULL, *s;
if (!f)
return 0;
while (fgets(buf, 200, f)) {
name = strtok_r(buf, " ", &p1);
path = strtok_r(NULL, " ", &p1);
fsname = strtok_r(NULL, " ", &p1);
options = strtok_r(NULL, " ", &p1);
if (strcmp(fsname, "cgroup") != 0)
continue;
/* make sure the freezer is composed */
s = strtok_r(options, ",", &p2);
while (s && strcmp(s, "freezer") != 0)
s = strtok_r(NULL, ",", &p2);
if (!s)
continue;
strncpy(dest, path, len);
fclose(f);
return 1;
}
fclose(f);
printf("Freezer not mounted\n");
return 0;
}
int move_to_new_cgroup(int newcgroup)
{
char cgroupname[150], cgroupbase[100], tasksfname[200];
FILE *fout;
int ret;
if (!load_cgroup_dir(cgroupbase, 100))
return 0;
snprintf(cgroupname, 150, "%s/%d", cgroupbase, newcgroup);
ret = mkdir(cgroupname, 0755);
if (ret)
return 0;
snprintf(tasksfname, 200, "%s/tasks", cgroupname);
fout = fopen(tasksfname, "w");
if (!fout)
return 0;
fprintf(fout, "%d\n", getpid());
fclose(fout);
return 1;
}
int pipefd[2];
/* gah. opentty will close the pipefd */
int check_newcgrp(void)
{
int ret, newgroup;
char buf[20];
if (!do_newcgrp)
return 0;
close(pipefd[1]);
ret = read(pipefd[0], buf, 20);
close(pipefd[0]);
if (ret == -1) {
perror("read");
return 1;
}
newgroup = atoi(buf);
if (!move_to_new_cgroup(newgroup))
return 1;
do_newcgrp = 0;
return 0;
}
int do_child(void *vargv)
{
char **argv = (char **)vargv;
if (check_newcgrp())
return 1;
execve(argv[0], argv, __environ);
perror("execve");
return 1;
}
void write_pid(char *pid_file, int pid)
{
FILE *fp;
if (!pid_file)
return;
fp = fopen(pid_file, "w");
if (!fp) {
perror("fopen, pid_file");
exit(1);
}
fprintf(fp, "%d", pid);
fflush(fp);
fclose(fp);
}
int main(int argc, char *argv[])
{
int c;
unsigned long flags = 0, eflags = 0;
char ttyname[256];
int status;
int ret, use_clone = 0;
int pid;
char *pid_file = NULL;
procname = basename(argv[0]);
memset(ttyname, '\0', sizeof(ttyname));
if(readlink("/proc/self/fd/0", ttyname, sizeof(ttyname))<0) {
perror("readlink /proc/self/fd/0");
exit(1);
}
while ((c = getopt(argc, argv, "+mguUiphcnf:P:")) != EOF) {
switch (c) {
case 'g': do_newcgrp = getpid(); break;
case 'm': flags |= CLONE_NEWNS; break;
case 'c': use_clone = 1; break;
case 'P': pid_file = optarg; break;
case 'u': flags |= CLONE_NEWUTS; break;
case 'i': flags |= CLONE_NEWIPC; break;
case 'U': flags |= CLONE_NEWUSER; break;
case 'n': flags |= CLONE_NEWNET; break;
case 'p': flags |= CLONE_NEWNS|CLONE_NEWPID; break;
case 'f': if (!string_to_ul(optarg, &eflags)) {
flags |= eflags;
break;
}
case 'h':
default:
usage(procname);
}
};
argv = &argv[optind];
argc = argc - optind;
if (do_newcgrp) {
ret = pipe(pipefd);
if (ret) {
perror("pipe");
return -1;
}
do_newcgrp = pipefd[0];
}
if (use_clone) {
int stacksize = 4*getpagesize();
void *childstack, *stack = malloc(stacksize);
if (!stack) {
perror("malloc");
return -1;
}
childstack = stack + stacksize - 1;
printf("about to clone with %lx\n", flags);
flags |= SIGCHLD;
pid = clone(do_child, childstack, flags, (void *)argv);
if (pid == -1) {
perror("clone");
return -1;
}
} else {
if ((pid = fork()) == 0) {
// Child.
//print_my_info(procname, ttyname);
if (check_newcgrp())
return 1;
opentty(ttyname);
printf("about to unshare with %lx\n", flags);
ret = unshare(flags);
if (ret < 0) {
perror("unshare");
return 1;
}
return do_child((void*)argv);
}
}
if (pid != -1 && do_newcgrp) {
char buf[20];
snprintf(buf, 20, "%d", pid);
close(pipefd[0]);
if(write(pipefd[1], buf, strlen(buf)+1)<0) {
perror("write to pipe");
exit(1);
}
close(pipefd[1]);
}
write_pid(pid_file, pid);
if ((ret = waitpid(pid, &status, __WALL)) < 0)
printf("waitpid() returns %d, errno %d\n", ret, errno);
exit(0);
}