Merge remote-tracking branch 'origin/trellis-old' into trellis
[util-vserver.git] / src / exec-remount.c
1 // $Id: exec-remount.c 2776 2008-08-25 22:57:09Z dhozac $    --*- c -*--
2
3 // Copyright (C) 2008 Daniel Hokka Zakrisson
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; version 2 of the License.
8 //  
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //  
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
18
19 #ifdef HAVE_CONFIG_H
20 #  include <config.h>
21 #endif
22
23 #include "util.h"
24
25 #include <vserver.h>
26
27 #include <getopt.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <sys/mount.h>
32 #include <errno.h>
33
34 // Make this version of util-vserver build in one of PL's builds with an old mount.h
35 #if defined(_SYS_MOUNT_H) && !defined(MNT_DETACH)
36 #define MNT_DETACH          2
37 #endif
38
39 #define ENSC_WRAPPERS_PREFIX    "exec-remount: "
40 #define ENSC_WRAPPERS_MOUNT     1
41 #define ENSC_WRAPPERS_UNISTD    1
42 #include <wrappers.h>
43
44 #define CMD_HELP                0x1000
45 #define CMD_VERSION             0x1001
46 #define CMD_MTAB                0x2001
47
48 static struct option const
49 CMDLINE_OPTIONS[] = {
50   { "help",     no_argument,       0, CMD_HELP },
51   { "version",  no_argument,       0, CMD_VERSION },
52   { "mtab",     required_argument, 0, CMD_MTAB },
53   { NULL, 0, 0, 0 }
54 };
55
56 int             wrapper_exit_code  =  255;
57
58 static void
59 showHelp(int fd, char const *cmd, int res)
60 {
61   WRITE_MSG(fd, "Usage:\n    ");
62   WRITE_STR(fd, cmd);
63   WRITE_MSG(fd, " [--mtab <file>] <mount points>* -- <command> <args>*\n");
64   WRITE_MSG(fd, "\n"
65                 "Please report bugs to " PACKAGE_BUGREPORT "\n");
66   exit(res);
67 }
68
69 static void
70 showVersion(void)
71 {
72   WRITE_MSG(1,
73         "exec-remount " VERSION " -- remounts specified mount points and executes a program\n"
74         "This program is part of " PACKAGE_STRING "\n\n"
75         "Copyright (c) 2008 Daniel Hokka Zakrisson\n"
76         VERSION_COPYRIGHT_DISCLAIMER);
77   exit(0);
78 }
79
80 static void
81 do_remount(char const *mount)
82 {
83   /* FIXME: Read this from mtab */
84   if (strcmp(mount, "/proc") == 0)
85     Emount("proc", "proc", "proc", 0, NULL);
86   else if (strcmp(mount, "/sys") == 0)
87     Emount("sysfs", "sys", "sysfs", 0, NULL);
88   else {
89     WRITE_MSG(2, ENSC_WRAPPERS_PREFIX "unknown mount point: ");
90     WRITE_STR(2, mount);
91     WRITE_MSG(2, "\n");
92   }
93 }
94
95 int main(int argc, char *argv[])
96 {
97   int i;
98   char const *mtab = "/etc/mtab";
99
100   while (1) {
101     int c = getopt_long(argc, argv, "+", CMDLINE_OPTIONS, 0);
102     if (c==-1) break;
103
104     switch (c) {
105       case CMD_HELP     :  showHelp(1, argv[0], 0);
106       case CMD_VERSION  :  showVersion();
107       case CMD_MTAB     :  mtab = optarg; break;
108       default           :  showHelp(2, argv[0], 1);
109     }
110   }
111
112   for (i = optind; argv[i] != NULL && strcmp(argv[i], "--") != 0; i++) {
113     if (vc_isSupported(vcFEATURE_PIDSPACE)) {
114       /* + 1 to strip the leading / */
115       if (umount2(argv[i] + 1, MNT_DETACH) == 0)
116         do_remount(argv[i]);
117     }
118   }
119
120   if (argv[i] == NULL)
121     showHelp(2, argv[0], 1);
122
123   i++;
124   EexecvpD(argv[i], argv+i);
125   /* NOTREACHED */
126   return 1;
127 }