Repository navigation
Expand file tree
/
Copy pathmnexec.c
More file actions
79 lines (73 loc) · 2.05 KB
/
Copy pathmnexec.c
File metadata and controls
79 lines (73 loc) · 2.05 KB
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
8E22
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
/* mnexec: execution utility for mininet
*
* Starts up programs and does things that are slow or
* difficult in Python, including:
*
* - closing all file descriptors except stdin/out/error
* - detaching from a controlling tty using setsid
* - running in a network namespace
* - printing out the pid of a process so we can identify it later
*
* Partially based on public domain setsid(1)
*/
#include <stdio.h>
#include <linux/sched.h>
#include <unistd.h>
void usage(char *name)
{
printf("Execution utility for Mininet.\n"
"usage: %s [-cdnp]\n"
"-c: close all file descriptors except stdin/out/error\n"
"-d: detach from tty by calling setsid()\n"
"-n: run in new network namespace\n"
"-p: print ^A + pid\n", name);
}
int main(int argc, char *argv[])
{
char c;
int fd;
while ((c = getopt(argc, argv, "+cdnp")) != -1)
switch(c) {
case 'c':
/* close file descriptors except stdin/out/error */
for (fd = getdtablesize(); fd > 2; fd--)
close(fd);
break;
case 'd':
/* detach from tty */
if (getpgrp() == getpid()) {
switch(fork()) {
case -1:
perror("fork");
return 1;
case 0: /* child */
break;
default: /* parent */
return 0;
}
}
setsid();
break;
case 'n':
/* run in network namespace */
if (unshare(CLONE_NEWNET) == -1) {
perror("unshare");
return 1;
}
break;
case 'p':
/* print pid */
printf("\001%d\n", getpid());
fflush(stdout);
break;
default:
usage(argv[0]);
break;
}
if (optind < argc) {
execvp(argv[optind], &argv[optind]);
perror(argv[optind]);
return 1;
}
usage(argv[0]);
}