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
28 #include "sys_clone.h"
36 #include <sys/types.h>
41 #define ENSC_WRAPPERS_PREFIX "new-namespace: "
42 #define ENSC_WRAPPERS_WAIT 1
46 # define CLONE_NEWNS 0x00020000
49 int wrapper_exit_code = 255;
52 showHelp(int fd, char const *cmd, int exit_code)
54 VSERVER_DECLARE_CMD(cmd);
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[])
83 if (argc==1) showHelp(2, argv[0], 255);
84 if (!strcmp(argv[1], "--help")) showHelp(1, argv[0], 0);
85 if (!strcmp(argv[1], "--version")) showVersion();
86 if (!strcmp(argv[1], "--")) ++argv;
90 pid = sys_clone(CLONE_NEWNS|CLONE_VFORK|SIGCHLD, 0);
92 pid = sys_clone(CLONE_NEWNS|SIGCHLD, 0);
96 perror("new-namespace: clone()");
97 exit(wrapper_exit_code);
99 execvp(argv[1], argv+1);
100 perror("new-namespace: execvp()");
101 exit(wrapper_exit_code);
103 exitLikeProcess(pid);