3 // Copyright (C) 2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
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.
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.
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.
23 #include "vserver-start.h"
25 #include "pathconfig.h"
26 #include "interface.h"
27 #include "configuration.h"
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"
40 #include <sys/socket.h>
42 #define ENSC_WRAPPERS_VSERVER 1
43 #define ENSC_WRAPPERS_SOCKET 1
44 #define ENSC_WRAPPERS_FCNTL 1
45 #define ENSC_WRAPPERS_STDLIB 1
46 #include <ensc_wrappers/wrappers.h>
49 struct Configuration cfg;
50 int wrapper_exit_code;
53 env2Str(char const **var, char const *env, bool req)
55 char const * tmp = getenv(env);
57 WRITE_MSG(2, "vserver.start: required environment variable $");
59 WRITE_STR(2, " not set; aborting...\n");
68 env2Bool(bool *var, char const *env, bool req)
71 env2Str(&tmp, env, req);
72 *var = !(tmp==0 || atoi(tmp)==0);
78 env2Str (&opts.VSERVER_DIR, "VSERVER_DIR", true);
79 env2Str (&opts.VSERVER_NAME, "VSERVER_NAME", true);
80 env2Bool(&opts.OPTION_DEBUG, "OPTION_DEBUG", false);
81 env2Bool(&opts.OPTION_DEFAULTTTY, "OPTION_DEFAULTTTY", false);
87 size_t l = strlen(opts.VSERVER_DIR);
88 char tmp[sizeof(LOCKDIR "/vserver..startup") + l];
90 struct ErrorInformation err = { .app = 0 };
93 ptr = Xmemcpy(ptr, LOCKDIR "/vserver.", sizeof(LOCKDIR "/vserver.")-1);
94 ((char *)(Xmemcpy(ptr, opts.VSERVER_DIR, l)))[0] = '\0';
95 ptr += canonifyVserverName(ptr);
96 ptr = Xmemcpy(ptr, ".startup", sizeof(".startup"));
99 if (!lockfile(&fd, tmp, LOCK_EX, 30, &err)) {
100 WRITE_MSG(2, "vserver.start: failed to lock '");
103 ErrInfo_writeErrno(&err);
113 struct vc_vx_info info;
115 xid = vc_getVserverCtx(opts.VSERVER_DIR, vcCFG_RECENT_FULL,
118 if (xid!=VC_NOCTX && vc_get_vx_info(xid, &info)!=-1) {
119 WRITE_MSG(2, "vserver.start: vserver '");
120 WRITE_STR(2, opts.VSERVER_NAME);
121 WRITE_MSG(2, "' already running; aborting...\n");
125 Vshelper_doSanityCheck();
128 int main(int argc, char *argv[])
139 char buf[sizeof(xid)*3 + 2];
140 PathInfo cfgdir = { .d = opts.VSERVER_DIR, .l = strlen(opts.VSERVER_DIR) };
142 Esocketpair(AF_UNIX, SOCK_STREAM, 0, sync_fd);
143 Efcntl(sync_fd[0], F_SETFD, FD_CLOEXEC);
144 Efcntl(sync_fd[1], F_SETFD, FD_CLOEXEC);
146 getConfiguration(&cfg, &cfgdir);
147 pid_t pid = sys_clone(CLONE_NEWNS|SIGCHLD, 0);
148 FatalErrnoError(pid==-1, "sys_clone()");
153 execScriptlets(&cfgdir, opts.VSERVER_NAME, "prepre-start");
154 activateInterfaces();
156 xid = Evc_ctx_create(cfg.xid);
158 // prepareInit(&cfg, &cfgdir);
160 Esend(sync_fd[0], &xid, sizeof xid, MSG_NOSIGNAL);
161 Erecv(sync_fd[0], &c, 1, 0);
162 execScriptlets(&cfgdir, opts.VSERVER_NAME, "pre-start");
168 Erecv(sync_fd[1], &xid, sizeof xid, 0);
169 utilvserver_fmt_uint(buf, xid);
170 Esetenv("CHILD_XID", buf, 1);
172 execScriptlets(&cfgdir, opts.VSERVER_NAME, "pre-start.parent");
173 Esend(sync_fd[1], ".", 1, MSG_NOSIGNAL);