do not use CLONE_VFORK in debug mode; lockups with 'strace' are too annoying
[util-vserver.git] / util-vserver / src / new-namespace.c
1 // $Id$    --*- c++ -*--
2
3 // Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 //  
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)
8 // any later version.
9 //  
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.
14 //  
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.
18
19 // Executes a program in a new namespace
20 // Based on http://www.win.tue.nl/~aeb/linux/lk/lk-6.html
21
22
23 #ifdef HAVE_CONFIG_H
24 #  include <config.h>
25 #endif
26
27 #include "util.h"
28 #include "wrappers.h"
29 #include "stack-start.h"
30
31 #include <stdio.h>
32 #include <unistd.h>
33 #include <stdlib.h>
34 #include <signal.h>
35 #include <sched.h>
36 #include <string.h>
37 #include <sys/types.h>
38 #include <sys/wait.h>
39 #include <sched.h>
40
41 #ifndef CLONE_NEWNS
42 #  define CLONE_NEWNS 0x00020000
43 #endif
44
45 int     wrapper_exit_code = 255;
46
47 static int
48 childFunc(void *argv_v)
49 {
50   char **       argv = argv_v;
51   
52   execvp(argv[0], argv);
53   perror("execvp()");
54   exit(255);
55 }
56
57 static void
58 usage(int fd, char const *cmd, int exit_code)
59 {
60   WRITE_MSG(fd, "Usage:  ");
61   WRITE_STR(fd, cmd);
62   WRITE_MSG(fd,
63             " [--help] [--version] <cmd> <args*>\n"
64             "\n"
65             "Executes <cmd> in a new namespace.\n"
66             "\n"
67             "Report bugs to " PACKAGE_BUGREPORT "\n");
68
69   exit(exit_code);
70 }
71
72 static void
73 showVersion()
74 {
75   WRITE_MSG(1,
76             "new-namespace " VERSION " -- executes programs in a new namespace\n"
77             "This program is part of " PACKAGE_STRING "\n"
78             "Copyright (C) 2003 Enrico Scholz\n"
79             VERSION_COPYRIGHT_DISCLAIMER);
80   exit(0);
81 }
82   
83 int main(int argc, char *argv[])
84 {
85   char                  buf[128];
86   int                   status;
87   pid_t                 pid, p;
88
89   if (argc==1) usage(2, argv[0], 255);
90   if (!strcmp(argv[1], "--help"))    usage(1, argv[0], 0);
91   if (!strcmp(argv[1], "--version")) showVersion();
92   if (!strcmp(argv[1], "--"))        ++argv;
93
94 #ifdef NDEBUG    
95   pid = Eclone(childFunc, STACK_START(buf), CLONE_NEWNS|CLONE_VFORK|SIGCHLD, argv+1);
96 #else
97   pid = Eclone(childFunc, STACK_START(buf), CLONE_NEWNS|SIGCHLD, argv+1);
98 #endif
99   p   = Ewait4(pid, &status, 0,0);
100
101   if (WIFEXITED(status)) return WEXITSTATUS(status);
102   else                   return 255;
103 }