linux-boot-probe: mounted/40grub2 add tests for handling escaped $ " \
[os-prober.git] / newns.c
1 #define _GNU_SOURCE
2 #include <errno.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <stdio.h>
6 #include <sched.h>
7
8 int main(int argc, char **argv)
9 {
10         if (argc < 2) {
11                 fprintf(stderr, "Usage: newns PROGRAM [ARGUMENTS ...]\n");
12                 exit(1);
13         }
14
15         /* This is best-effort; if the kernel is too old (Linux << 2.6.16),
16          * or indeed if the kernel isn't Linux so we don't have
17          * unshare(CLONE_NEWNS), don't worry about it.
18          */
19 #ifdef __linux__
20         if (unshare(CLONE_NEWNS) < 0 && errno != ENOSYS)
21                 perror("unshare failed");
22                 /* ... but continue anyway */
23 #endif /* __linux__ */
24         setenv("OS_PROBER_NEWNS", "1", 1);
25         execvp(argv[1], argv + 1);
26
27         perror("execvp failed");
28         _exit(127);
29 }