modified showVersion() to show current version instead of an hardcoded
[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
29 #include <stdio.h>
30 #include <unistd.h>
31 #include <stdlib.h>
32 #include <signal.h>
33 #include <sched.h>
34 #include <string.h>
35 #include <sys/types.h>
36 #include <sys/wait.h>
37 #include <sched.h>
38
39 #ifndef CLONE_NEWNS
40 #  define CLONE_NEWNS 0x00020000
41 #endif
42
43 static int
44 childFunc(void *argv_v)
45 {
46   char **       argv = argv_v;
47   
48   execvp(argv[0], argv);
49   perror("execvp()");
50   exit(255);
51 }
52
53 static void
54 usage(int fd, char const *cmd, int exit_code)
55 {
56   WRITE_MSG(fd, "Usage:  ");
57   WRITE_STR(fd, cmd);
58   WRITE_MSG(fd,
59             " [--help] [--version] <cmd> <args*>\n"
60             "\n"
61             "Executes <cmd> in a new namespace.\n"
62             "\n"
63             "Report bugs to " PACKAGE_BUGREPORT "\n");
64
65   exit(exit_code);
66 }
67
68 static void
69 showVersion()
70 {
71   WRITE_MSG(1,
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);
76   exit(0);
77 }
78   
79 int main(int argc, char *argv[])
80 {
81   char                  buf[128];
82   int                   status;
83   pid_t                 pid, p;
84
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;
89
90   pid = clone(childFunc, buf+sizeof(buf)/2, CLONE_NEWNS|CLONE_VFORK|SIGCHLD, argv+1);
91   if (pid==-1) {
92     perror("clone()");
93     exit(255);
94   }
95
96   p   = wait4(pid, &status, 0,0);
97   if (p==-1) {
98     perror("waitpid()");
99     exit(255);
100   }
101
102   if (WIFEXITED(status)) return WEXITSTATUS(status);
103   else                   return 255;
104 }