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