-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpidns.c
59 lines (54 loc) · 1.1 KB
/
pidns.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
#define _GNU_SOURCE
#include <sched.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/mount.h>
#include <unistd.h>
int main(int argc, char **argv)
{
pid_t pid;
int status;
/*
* pidns is used to avoid conflicts
* mntns is used to mount /proc
* net is used to avoid conflicts of parasite sockets
*/
if (unshare(CLONE_NEWNS | CLONE_NEWPID))
return 1;
pid = fork();
if (pid == 0) {
int ret = 0, p;
if (mount(NULL, "/", NULL, MS_REC | MS_SLAVE, NULL)) {
fprintf(stderr, "mount(/, S_REC | MS_SLAVE)): %m");
return 1;
}
umount2("/proc", MNT_DETACH);
if (mount("zdtm_proc", "/proc", "proc", 0, NULL)) {
fprintf(stderr, "mount(/proc): %m");
return 1;
}
pid = fork();
if (pid < 0)
return 1;
if (pid == 0) {
execvp(argv[1], argv + 1);
fprintf(stderr, "execve: %m");
return 1;
}
while (1) {
p = waitpid(-1, &status, 0);
if (p == pid)
ret = status != 0;
if (p < 0)
break;
}
return ret;
}
if (waitpid(pid, &status, 0) != pid) {
fprintf(stderr, "waitpid: %m");
return 1;
}
return status != 0;
}