3 // Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2, or (at your option)
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 // Executes a program in a new namespace
20 // Based on http://www.win.tue.nl/~aeb/linux/lk/lk-6.html
35 #include <sys/types.h>
40 # define CLONE_NEWNS 0x00020000
44 childFunc(void *argv_v)
46 char ** argv = argv_v;
48 execvp(argv[0], argv);
54 usage(int fd, char const *cmd, int exit_code)
56 WRITE_MSG(fd, "Usage: ");
59 " [--help] [--version] <cmd> <args*>\n"
61 "Executes <cmd> in a new namespace.\n"
63 "Report bugs to " PACKAGE_BUGREPORT "\n");
72 "new-namespace " VERSION " -- executes programs in a new namespace\n"
73 "This program is part of " PACKAGE_STRING "\n"
74 "Copyright (C) 2003 Enrico Scholz\n"
75 VERSION_COPYRIGHT_DISCLAIMER);
79 int main(int argc, char *argv[])
85 if (argc==1) usage(2, argv[0], 255);
86 if (!strcmp(argv[1], "--help")) usage(1, argv[0], 0);
87 if (!strcmp(argv[1], "--version")) showVersion();
88 if (!strcmp(argv[1], "--")) ++argv;
90 pid = clone(childFunc, buf+sizeof(buf)/2, CLONE_NEWNS|CLONE_VFORK|SIGCHLD, argv+1);
96 p = wait4(pid, &status, 0,0);
102 if (WIFEXITED(status)) return WEXITSTATUS(status);