use new ensc_wrappers/ headers
[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 "stack-start.h"
29
30 #include <stdio.h>
31 #include <unistd.h>
32 #include <stdlib.h>
33 #include <signal.h>
34 #include <sched.h>
35 #include <string.h>
36 #include <sys/types.h>
37 #include <sys/wait.h>
38 #include <sched.h>
39
40 #define ENSC_WRAPPERS_CLONE     1
41 #define ENSC_WRAPPERS_WAIT      1
42 #include <wrappers.h>
43
44 #ifndef CLONE_NEWNS
45 #  define CLONE_NEWNS 0x00020000
46 #endif
47
48 int     wrapper_exit_code = 255;
49
50 static int
51 childFunc(void *argv_v)
52 {
53   char **       argv = argv_v;
54   
55   execvp(argv[0], argv);
56   perror("execvp()");
57   exit(255);
58 }
59
60 static void
61 usage(int fd, char const *cmd, int exit_code)
62 {
63   WRITE_MSG(fd, "Usage:  ");
64   WRITE_STR(fd, cmd);
65   WRITE_MSG(fd,
66             " [--help] [--version] <cmd> <args*>\n"
67             "\n"
68             "Executes <cmd> in a new namespace.\n"
69             "\n"
70             "Report bugs to " PACKAGE_BUGREPORT "\n");
71
72   exit(exit_code);
73 }
74
75 static void
76 showVersion()
77 {
78   WRITE_MSG(1,
79             "new-namespace " VERSION " -- executes programs in a new namespace\n"
80             "This program is part of " PACKAGE_STRING "\n"
81             "Copyright (C) 2003 Enrico Scholz\n"
82             VERSION_COPYRIGHT_DISCLAIMER);
83   exit(0);
84 }
85   
86 int main(int argc, char *argv[])
87 {
88   char                  buf[128];
89   int                   status;
90   pid_t                 pid, p;
91
92   if (argc==1) usage(2, argv[0], 255);
93   if (!strcmp(argv[1], "--help"))    usage(1, argv[0], 0);
94   if (!strcmp(argv[1], "--version")) showVersion();
95   if (!strcmp(argv[1], "--"))        ++argv;
96
97 #ifdef NDEBUG    
98   pid = Eclone(childFunc, STACK_START(buf), CLONE_NEWNS|CLONE_VFORK|SIGCHLD, argv+1);
99 #else
100   pid = Eclone(childFunc, STACK_START(buf), CLONE_NEWNS|SIGCHLD, argv+1);
101 #endif
102   p   = Ewait4(pid, &status, 0,0);
103
104   if (WIFEXITED(status)) return WEXITSTATUS(status);
105   else                   return 255;
106 }