]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/start.c
allocate a console to be proxied
[mirror_lxc.git] / src / lxc / start.c
1 /*
2 * lxc: linux Container library
3 *
4 * (C) Copyright IBM Corp. 2007, 2008
5 *
6 * Authors:
7 * Daniel Lezcano <dlezcano at fr.ibm.com>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23
24 #include "../config.h"
25 #include <stdio.h>
26 #undef _GNU_SOURCE
27 #include <string.h>
28 #include <stdlib.h>
29 #include <dirent.h>
30 #include <errno.h>
31 #include <unistd.h>
32 #include <signal.h>
33 #include <fcntl.h>
34 #include <termios.h>
35 #include <namespace.h>
36 #include <sys/param.h>
37 #include <sys/file.h>
38 #include <sys/mount.h>
39 #include <sys/types.h>
40 #include <sys/prctl.h>
41 #include <sys/types.h>
42 #include <sys/capability.h>
43 #include <sys/wait.h>
44 #include <sys/un.h>
45 #include <sys/poll.h>
46
47 #ifdef HAVE_SYS_SIGNALFD_H
48 # include <sys/signalfd.h>
49 #else
50 # ifndef __NR_signalfd4
51 /* assume kernel headers are too old */
52 # if __i386__
53 # define __NR_signalfd4 327
54 # elif __x86_64__
55 # define __NR_signalfd4 289
56 # elif __powerpc__
57 # define __NR_signalfd4 313
58 # elif __s390x__
59 # define __NR_signalfd4 322
60 # endif
61 #endif
62
63 # ifndef __NR_signalfd
64 /* assume kernel headers are too old */
65 # if __i386__
66 # define __NR_signalfd 321
67 # elif __x86_64__
68 # define __NR_signalfd 282
69 # elif __powerpc__
70 # define __NR_signalfd 305
71 # elif __s390x__
72 # define __NR_signalfd 316
73 # endif
74 #endif
75
76 int signalfd(int fd, const sigset_t *mask, int flags)
77 {
78 int retval;
79
80 retval = syscall (__NR_signalfd4, fd, mask, _NSIG / 8, flags);
81 if (errno == ENOSYS && flags == 0)
82 retval = syscall (__NR_signalfd, fd, mask, _NSIG / 8);
83 return retval;
84 }
85 #endif
86
87 #if !HAVE_DECL_PR_CAPBSET_DROP
88 #define PR_CAPBSET_DROP 24
89 #endif
90
91 #include "start.h"
92 #include "conf.h"
93 #include "log.h"
94 #include "error.h"
95 #include "af_unix.h"
96 #include "mainloop.h"
97 #include "utils.h"
98 #include "monitor.h"
99 #include "commands.h"
100 #include "console.h"
101
102 lxc_log_define(lxc_start, lxc);
103
104 LXC_TTY_HANDLER(SIGINT);
105 LXC_TTY_HANDLER(SIGQUIT);
106
107 static int setup_sigchld_fd(sigset_t *oldmask)
108 {
109 sigset_t mask;
110 int fd;
111
112 if (sigprocmask(SIG_BLOCK, NULL, &mask)) {
113 SYSERROR("failed to get mask signal");
114 return -1;
115 }
116
117 if (sigaddset(&mask, SIGCHLD) || sigprocmask(SIG_BLOCK, &mask, oldmask)) {
118 SYSERROR("failed to set mask signal");
119 return -1;
120 }
121
122 fd = signalfd(-1, &mask, 0);
123 if (fd < 0) {
124 SYSERROR("failed to create the signal fd");
125 return -1;
126 }
127
128 if (fcntl(fd, F_SETFD, FD_CLOEXEC)) {
129 SYSERROR("failed to set sigfd to close-on-exec");
130 close(fd);
131 return -1;
132 }
133
134 DEBUG("sigchild handler set");
135
136 return fd;
137 }
138
139 static int sigchld_handler(int fd, void *data,
140 struct lxc_epoll_descr *descr)
141 {
142 DEBUG("child exited");
143
144 return 1;
145 }
146
147 int lxc_set_state(const char *name, struct lxc_handler *handler, lxc_state_t state)
148 {
149 handler->state = state;
150 lxc_monitor_send_state(name, state);
151 return 0;
152 }
153
154 int lxc_poll(const char *name, struct lxc_handler *handler)
155 {
156 int sigfd = handler->sigfd;
157 int pid = handler->pid;
158 struct lxc_epoll_descr descr;
159
160 if (lxc_mainloop_open(&descr)) {
161 ERROR("failed to create mainloop");
162 goto out_sigfd;
163 }
164
165 if (lxc_mainloop_add_handler(&descr, sigfd, sigchld_handler, &pid)) {
166 ERROR("failed to add handler for the signal");
167 goto out_mainloop_open;
168 }
169
170 if (lxc_console_mainloop_add(&descr, handler)) {
171 ERROR("failed to add console handler to mainloop");
172 goto out_mainloop_open;
173 }
174
175 if (lxc_command_mainloop_add(name, &descr, handler))
176 goto out_mainloop_open;
177
178 return lxc_mainloop(&descr);
179
180 out_mainloop_open:
181 lxc_mainloop_close(&descr);
182 out_sigfd:
183 close(sigfd);
184 return -1;
185 }
186
187 struct lxc_handler *lxc_init(const char *name, struct lxc_conf *conf)
188 {
189 struct lxc_handler *handler;
190
191 handler = malloc(sizeof(*handler));
192 if (!handler)
193 return NULL;
194
195 memset(handler, 0, sizeof(*handler));
196
197 handler->conf = conf;
198
199 /* Begin the set the state to STARTING*/
200 if (lxc_set_state(name, handler, STARTING)) {
201 ERROR("failed to set state '%s'", lxc_state2str(STARTING));
202 goto out_free;
203 }
204
205 if (lxc_create_tty(name, conf)) {
206 ERROR("failed to create the ttys");
207 goto out_aborting;
208 }
209
210 if (lxc_create_console(&conf->console)) {
211 ERROR("failed to create console");
212 goto out_delete_tty;
213 }
214
215 /* the signal fd has to be created before forking otherwise
216 * if the child process exits before we setup the signal fd,
217 * the event will be lost and the command will be stuck */
218 handler->sigfd = setup_sigchld_fd(&handler->oldmask);
219 if (handler->sigfd < 0) {
220 ERROR("failed to set sigchild fd handler");
221 goto out_delete_console;
222 }
223
224 /* Avoid signals from terminal */
225 LXC_TTY_ADD_HANDLER(SIGINT);
226 LXC_TTY_ADD_HANDLER(SIGQUIT);
227
228 INFO("'%s' is initialized", name);
229 return handler;
230
231 out_delete_console:
232 lxc_delete_console(&conf->console);
233 out_delete_tty:
234 lxc_delete_tty(&conf->tty_info);
235 out_aborting:
236 lxc_set_state(name, handler, ABORTING);
237 out_free:
238 free(handler);
239 return NULL;
240 }
241
242 void lxc_fini(const char *name, struct lxc_handler *handler)
243 {
244 /* The STOPPING state is there for future cleanup code
245 * which can take awhile
246 */
247 lxc_set_state(name, handler, STOPPING);
248 lxc_set_state(name, handler, STOPPED);
249 lxc_unlink_nsgroup(name);
250
251 lxc_delete_console(&handler->conf->console);
252 lxc_delete_tty(&handler->conf->tty_info);
253 free(handler);
254
255 LXC_TTY_DEL_HANDLER(SIGQUIT);
256 LXC_TTY_DEL_HANDLER(SIGINT);
257 }
258
259 void lxc_abort(const char *name, struct lxc_handler *handler)
260 {
261 lxc_set_state(name, handler, ABORTING);
262 kill(handler->pid, SIGKILL);
263 }
264
265 struct start_arg {
266 const char *name;
267 char *const *argv;
268 struct lxc_handler *handler;
269 int *sv;
270 };
271
272 static int do_start(void *arg)
273 {
274 struct start_arg *start_arg = arg;
275 struct lxc_handler *handler = start_arg->handler;
276 const char *name = start_arg->name;
277 char *const *argv = start_arg->argv;
278 int *sv = start_arg->sv;
279 int err = -1, sync;
280
281 if (sigprocmask(SIG_SETMASK, &handler->oldmask, NULL)) {
282 SYSERROR("failed to set sigprocmask");
283 return -1;
284 }
285
286 close(sv[1]);
287
288 /* Be sure we don't inherit this after the exec */
289 fcntl(sv[0], F_SETFD, FD_CLOEXEC);
290
291 /* Tell our father he can begin to configure the container */
292 if (write(sv[0], &sync, sizeof(sync)) < 0) {
293 SYSERROR("failed to write socket");
294 return -1;
295 }
296
297 /* Wait for the father to finish the configuration */
298 if (read(sv[0], &sync, sizeof(sync)) < 0) {
299 SYSERROR("failed to read socket");
300 return -1;
301 }
302
303 /* Setup the container, ip, names, utsname, ... */
304 if (lxc_setup(name, handler->conf)) {
305 ERROR("failed to setup the container");
306 goto out_warn_father;
307 }
308
309 if (prctl(PR_CAPBSET_DROP, CAP_SYS_BOOT, 0, 0, 0)) {
310 SYSERROR("failed to remove CAP_SYS_BOOT capability");
311 return -1;
312 }
313
314 if (prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0)) {
315 SYSERROR("failed to set pdeath signal");
316 return -1;
317 }
318
319 NOTICE("exec'ing '%s'", argv[0]);
320
321 execvp(argv[0], argv);
322 SYSERROR("failed to exec %s", argv[0]);
323
324 out_warn_father:
325 /* If the exec fails, tell that to our father */
326 if (write(sv[0], &err, sizeof(err)) < 0)
327 SYSERROR("failed to write the socket");
328 return -1;
329 }
330
331 int lxc_spawn(const char *name, struct lxc_handler *handler, char *const argv[])
332 {
333 int sv[2];
334 int clone_flags;
335 int err = -1, sync;
336
337 struct start_arg start_arg = {
338 .name = name,
339 .argv = argv,
340 .handler = handler,
341 .sv = sv,
342 };
343
344 /* Synchro socketpair */
345 if (socketpair(AF_LOCAL, SOCK_STREAM, 0, sv)) {
346 SYSERROR("failed to create communication socketpair");
347 return -1;
348 }
349
350 clone_flags = CLONE_NEWUTS|CLONE_NEWPID|CLONE_NEWIPC|CLONE_NEWNS;
351 if (!lxc_list_empty(&handler->conf->network)) {
352
353 clone_flags |= CLONE_NEWNET;
354
355 /* that should be done before the clone because we will
356 * fill the netdev index and use them in the child
357 */
358 if (lxc_create_network(&handler->conf->network)) {
359 ERROR("failed to create the network");
360 goto out_close;
361 }
362 }
363
364 /* Create a process in a new set of namespaces */
365 handler->pid = lxc_clone(do_start, &start_arg, clone_flags);
366 if (handler->pid < 0) {
367 SYSERROR("failed to fork into a new namespace");
368 goto out_close;
369 }
370
371 close(sv[0]);
372
373 /* Wait for the child to be ready */
374 if (read(sv[1], &sync, sizeof(sync)) < 0) {
375 SYSERROR("failed to read the socket");
376 goto out_abort;
377 }
378
379 if (lxc_rename_nsgroup(name, handler))
380 goto out_abort;
381
382 /* Create the network configuration */
383 if (clone_flags & CLONE_NEWNET) {
384 if (lxc_assign_network(&handler->conf->network, handler->pid)) {
385 ERROR("failed to create the configured network");
386 goto out_abort;
387 }
388 }
389
390 /* Tell the child to continue its initialization */
391 if (write(sv[1], &sync, sizeof(sync)) < 0) {
392 SYSERROR("failed to write the socket");
393 goto out_abort;
394 }
395
396 /* Wait for the child to exec or returning an error */
397 if (read(sv[1], &sync, sizeof(sync)) < 0) {
398 ERROR("failed to read the socket");
399 goto out_abort;
400 }
401
402 if (lxc_set_state(name, handler, RUNNING)) {
403 ERROR("failed to set state to %s",
404 lxc_state2str(RUNNING));
405 goto out_abort;
406 }
407
408 err = 0;
409
410 NOTICE("'%s' started with pid '%d'", argv[0], handler->pid);
411
412 out_close:
413 close(sv[0]);
414 close(sv[1]);
415 return err;
416
417 out_abort:
418 lxc_abort(name, handler);
419 close(sv[1]);
420 return -1;
421 }
422
423 int lxc_start(const char *name, char *const argv[], struct lxc_conf *conf)
424 {
425 struct lxc_handler *handler;
426 int err = -1;
427 int status;
428
429 handler = lxc_init(name, conf);
430 if (!handler) {
431 ERROR("failed to initialize the container");
432 return -1;
433 }
434
435 err = lxc_spawn(name, handler, argv);
436 if (err) {
437 ERROR("failed to spawn '%s'", argv[0]);
438 goto out_fini;
439 }
440
441 err = lxc_close_all_inherited_fd();
442 if (err) {
443 ERROR("unable to close inherited fds");
444 goto out_abort;
445 }
446
447 err = lxc_poll(name, handler);
448 if (err) {
449 ERROR("mainloop exited with an error");
450 goto out_abort;
451 }
452
453 while (waitpid(handler->pid, &status, 0) < 0 && errno == EINTR)
454 continue;
455
456 err = lxc_error_set_and_log(handler->pid, status);
457 out_fini:
458 lxc_fini(name, handler);
459 return err;
460
461 out_abort:
462 lxc_abort(name, handler);
463 goto out_fini;
464 }