merge with 0.30.213
[util-vserver.git] / vserver-start / main.c
1 // $Id: main.c 1939 2005-03-19 02:07:40Z ensc $    --*- c -*--
2
3 // Copyright (C) 2004 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; 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 "vserver-start.h"
24 #include "vshelper.h"
25 #include "pathconfig.h"
26 #include "interface.h"
27 #include "configuration.h"
28 #include "mount.h"
29 #include "undo.h"
30
31 #include "lib_internal/util.h"
32 #include "lib_internal/errinfo.h"
33 #include "lib_internal/sys_clone.h"
34 #include "lib/vserver.h"
35 #include "lib/internal.h"
36
37 #include <sys/file.h>
38 #include <sched.h>
39 #include <signal.h>
40 #include <unistd.h>
41 #include <sys/socket.h>
42
43 #define ENSC_WRAPPERS_VSERVER   1
44 #define ENSC_WRAPPERS_SOCKET    1
45 #define ENSC_WRAPPERS_UNISTD    1
46 #define ENSC_WRAPPERS_FCNTL     1
47 #define ENSC_WRAPPERS_STDLIB    1
48 #include <ensc_wrappers/wrappers.h>
49
50 struct Options                  opts;
51 struct Configuration            cfg;
52 int                             wrapper_exit_code;
53
54 static void
55 env2Str(char const **var, char const *env, bool req)
56 {
57   char const *          tmp = getenv(env);
58   if (req && tmp==0) {
59     WRITE_MSG(2, "vserver.start: required environment variable $");
60     WRITE_STR(2, env);
61     WRITE_STR(2, " not set; aborting...\n");
62     exit(1);
63   }
64
65   *var = tmp;
66   unsetenv(env);
67 }
68
69 static void
70 env2Bool(bool *var, char const *env, bool req)
71 {
72   char const *  tmp;
73   env2Str(&tmp, env, req);
74   *var = !(tmp==0 || atoi(tmp)==0);
75 }
76
77 static void
78 initGlobals()
79 {
80   env2Str (&opts.VSERVER_DIR,       "VSERVER_DIR",       true);
81   env2Str (&opts.VSERVER_NAME,      "VSERVER_NAME",      true);
82   env2Bool(&opts.OPTION_DEBUG,      "OPTION_DEBUG",      false);
83   env2Bool(&opts.OPTION_DEFAULTTTY, "OPTION_DEFAULTTTY", false);  
84 }
85
86 static void
87 initLock()
88 {
89   size_t                        l   = strlen(opts.VSERVER_DIR);
90   char                          tmp[sizeof(LOCKDIR "/vserver..startup") + l];
91   char *                        ptr = tmp;
92   struct ErrorInformation       err = { .app = 0 };
93   int                           fd;
94
95   ptr  = Xmemcpy(ptr, LOCKDIR "/vserver.", sizeof(LOCKDIR "/vserver.")-1);
96   ((char *)(Xmemcpy(ptr, opts.VSERVER_DIR, l)))[0] = '\0';
97   ptr += canonifyVserverName(ptr);
98   ptr  = Xmemcpy(ptr, ".startup",  sizeof(".startup"));
99   *ptr = '\0';
100
101   if (!lockfile(&fd, tmp, LOCK_EX, 30, &err)) {
102     WRITE_MSG(2, "vserver.start: failed to lock '");
103     WRITE_STR(2, tmp);
104     WRITE_MSG(2, "': ");
105     ErrInfo_writeErrno(&err);
106     exit(1);
107   }
108 }
109
110 static void
111 checkConstraints()
112 {
113   xid_t                 xid;
114   bool                  is_running;
115   struct vc_vx_info     info;
116
117   xid = vc_getVserverCtx(opts.VSERVER_DIR, vcCFG_RECENT_FULL,
118                          true, &is_running);
119
120   if (xid!=VC_NOCTX && vc_get_vx_info(xid, &info)!=-1) {
121     WRITE_MSG(2, "vserver.start: vserver '");
122     WRITE_STR(2, opts.VSERVER_NAME);
123     WRITE_MSG(2, "' already running; aborting...\n");
124     exit(1);
125   }
126
127   Vshelper_doSanityCheck();
128 }
129
130 static void
131 setCFlag(xid_t xid, uint_least64_t value)
132 {
133   struct vc_ctx_flags   flags = {
134     .flagword = value,
135     .mask     = value
136   };
137
138   Evc_set_cflags(xid, &flags);
139 }
140
141 int main(int UNUSED argc, char UNUSED *argv[])
142 {
143   Cfg_init(&cfg);
144   
145   initGlobals();
146   initLock();
147   checkConstraints();
148
149   int           sync_fd[2];
150   char          c;
151   xid_t         xid;
152   char          buf[sizeof(xid)*3 + 2];
153   PathInfo      cfgdir = { .d = opts.VSERVER_DIR, .l = strlen(opts.VSERVER_DIR) };
154
155   Esocketpair(AF_UNIX, SOCK_STREAM, 0, sync_fd);
156   Efcntl(sync_fd[0], F_SETFD, FD_CLOEXEC);
157   Efcntl(sync_fd[1], F_SETFD, FD_CLOEXEC);
158   
159   getConfiguration(&cfg, &cfgdir);
160   pid_t         pid    = sys_clone(CLONE_NEWNS|SIGCHLD, 0);
161   FatalErrnoError(pid==-1, "sys_clone()");
162   
163   switch (pid) {
164     case 0      :
165       Undo_init();
166       execScriptlets(&cfgdir, opts.VSERVER_NAME, "prepre-start");
167       activateInterfaces(&cfg.interfaces);
168       
169       xid = Evc_ctx_create(cfg.xid);
170       setCFlag(xid, VC_VXF_INFO_NAMESPACE);
171       
172       mountVserver(&cfg);
173         //      prepareInit(&cfg, &cfgdir);
174
175       Esend(sync_fd[0], &xid, sizeof xid, MSG_NOSIGNAL);
176         // 'pre-start.parent' will be executed now in the parent-context
177       Erecv(sync_fd[0], &c, 1, 0);
178       execScriptlets(&cfgdir, opts.VSERVER_NAME, "pre-start");
179
180       if (cfg.nice)
181         Enice(atoi(cfg.nice));
182       if (opts.OPTION_DEFAULTTTY)
183         setDefaultTTY(&cfgdir, 0);
184
185       
186
187       Undo_detach();
188       break;
189
190     default     :
191       Erecv(sync_fd[1], &xid, sizeof xid, 0);
192       utilvserver_fmt_uint(buf, xid);
193       Esetenv("CHILD_XID", buf, 1);
194       
195       execScriptlets(&cfgdir, opts.VSERVER_NAME, "pre-start.parent");
196       Esend(sync_fd[1], ".", 1, MSG_NOSIGNAL);
197       
198       break;
199   }
200 }