]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/start.c
shutdown the container when powering off the container
[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/stat.h>
40 #include <sys/types.h>
41 #include <sys/prctl.h>
42 #include <sys/types.h>
43 #include <sys/capability.h>
44 #include <sys/wait.h>
45 #include <sys/un.h>
46 #include <sys/poll.h>
47
48 #ifdef HAVE_SYS_SIGNALFD_H
49 # include <sys/signalfd.h>
50 #else
51 # ifndef __NR_signalfd4
52 /* assume kernel headers are too old */
53 # if __i386__
54 # define __NR_signalfd4 327
55 # elif __x86_64__
56 # define __NR_signalfd4 289
57 # elif __powerpc__
58 # define __NR_signalfd4 313
59 # elif __s390x__
60 # define __NR_signalfd4 322
61 # endif
62 #endif
63
64 # ifndef __NR_signalfd
65 /* assume kernel headers are too old */
66 # if __i386__
67 # define __NR_signalfd 321
68 # elif __x86_64__
69 # define __NR_signalfd 282
70 # elif __powerpc__
71 # define __NR_signalfd 305
72 # elif __s390x__
73 # define __NR_signalfd 316
74 # endif
75 #endif
76
77 int signalfd(int fd, const sigset_t *mask, int flags)
78 {
79 int retval;
80
81 retval = syscall (__NR_signalfd4, fd, mask, _NSIG / 8, flags);
82 if (errno == ENOSYS && flags == 0)
83 retval = syscall (__NR_signalfd, fd, mask, _NSIG / 8);
84 return retval;
85 }
86 #endif
87
88 #if !HAVE_DECL_PR_CAPBSET_DROP
89 #define PR_CAPBSET_DROP 24
90 #endif
91
92 #include "start.h"
93 #include "conf.h"
94 #include "cgroup.h"
95 #include "log.h"
96 #include "cgroup.h"
97 #include "error.h"
98 #include "af_unix.h"
99 #include "mainloop.h"
100 #include "utils.h"
101 #include "utmp.h"
102 #include "monitor.h"
103 #include "commands.h"
104 #include "console.h"
105
106 lxc_log_define(lxc_start, lxc);
107
108 LXC_TTY_HANDLER(SIGINT);
109 LXC_TTY_HANDLER(SIGQUIT);
110
111 static int match_fd(int fd)
112 {
113 return (fd == 0 || fd == 1 || fd == 2);
114 }
115
116 int lxc_check_inherited(void)
117 {
118 struct dirent dirent, *direntp;
119 int fd, fddir;
120 DIR *dir;
121 int ret = 0;
122
123 dir = opendir("/proc/self/fd");
124 if (!dir) {
125 WARN("failed to open directory: %m");
126 return -1;
127 }
128
129 fddir = dirfd(dir);
130
131 while (!readdir_r(dir, &dirent, &direntp)) {
132 char procpath[64];
133 char path[PATH_MAX];
134
135 if (!direntp)
136 break;
137
138 if (!strcmp(direntp->d_name, "."))
139 continue;
140
141 if (!strcmp(direntp->d_name, ".."))
142 continue;
143
144 fd = atoi(direntp->d_name);
145
146 if (fd == fddir || fd == lxc_log_fd)
147 continue;
148
149 if (match_fd(fd))
150 continue;
151 /*
152 * found inherited fd
153 */
154 ret = -1;
155
156 snprintf(procpath, sizeof(procpath), "/proc/self/fd/%d", fd);
157
158 if (readlink(procpath, path, sizeof(path)) == -1)
159 ERROR("readlink(%s) failed : %m", procpath);
160 else
161 ERROR("inherited fd %d on %s", fd, path);
162 }
163
164 if (closedir(dir))
165 ERROR("failed to close directory");
166 return ret;
167 }
168
169 static int setup_sigchld_fd(sigset_t *oldmask)
170 {
171 sigset_t mask;
172 int fd;
173
174 if (sigprocmask(SIG_BLOCK, NULL, &mask)) {
175 SYSERROR("failed to get mask signal");
176 return -1;
177 }
178
179 if (sigaddset(&mask, SIGCHLD) || sigprocmask(SIG_BLOCK, &mask, oldmask)) {
180 SYSERROR("failed to set mask signal");
181 return -1;
182 }
183
184 fd = signalfd(-1, &mask, 0);
185 if (fd < 0) {
186 SYSERROR("failed to create the signal fd");
187 return -1;
188 }
189
190 if (fcntl(fd, F_SETFD, FD_CLOEXEC)) {
191 SYSERROR("failed to set sigfd to close-on-exec");
192 close(fd);
193 return -1;
194 }
195
196 DEBUG("sigchild handler set");
197
198 return fd;
199 }
200
201 static int sigchld_handler(int fd, void *data,
202 struct lxc_epoll_descr *descr)
203 {
204 DEBUG("child exited");
205
206 return 1;
207 }
208
209 int lxc_pid_callback(int fd, struct lxc_request *request,
210 struct lxc_handler *handler)
211 {
212 struct lxc_answer answer;
213 int ret;
214
215 answer.pid = handler->pid;
216 answer.ret = 0;
217
218 ret = send(fd, &answer, sizeof(answer), 0);
219 if (ret < 0) {
220 WARN("failed to send answer to the peer");
221 return -1;
222 }
223
224 if (ret != sizeof(answer)) {
225 ERROR("partial answer sent");
226 return -1;
227 }
228
229 return 0;
230 }
231
232 int lxc_set_state(const char *name, struct lxc_handler *handler, lxc_state_t state)
233 {
234 handler->state = state;
235 lxc_monitor_send_state(name, state);
236 return 0;
237 }
238
239 int lxc_poll(const char *name, struct lxc_handler *handler)
240 {
241 int sigfd = handler->sigfd;
242 int pid = handler->pid;
243 struct lxc_epoll_descr descr;
244
245 if (lxc_mainloop_open(&descr)) {
246 ERROR("failed to create mainloop");
247 goto out_sigfd;
248 }
249
250 if (lxc_mainloop_add_handler(&descr, sigfd, sigchld_handler, &pid)) {
251 ERROR("failed to add handler for the signal");
252 goto out_mainloop_open;
253 }
254
255 if (lxc_console_mainloop_add(&descr, handler)) {
256 ERROR("failed to add console handler to mainloop");
257 goto out_mainloop_open;
258 }
259
260 if (lxc_command_mainloop_add(name, &descr, handler)) {
261 ERROR("failed to add command handler to mainloop");
262 goto out_mainloop_open;
263 }
264
265 if (lxc_utmp_mainloop_add(&descr, handler)) {
266 ERROR("failed to add utmp handler to mainloop");
267 goto out_mainloop_open;
268 }
269
270 return lxc_mainloop(&descr);
271
272 out_mainloop_open:
273 lxc_mainloop_close(&descr);
274 out_sigfd:
275 close(sigfd);
276 return -1;
277 }
278
279 struct lxc_handler *lxc_init(const char *name, struct lxc_conf *conf)
280 {
281 struct lxc_handler *handler;
282
283 handler = malloc(sizeof(*handler));
284 if (!handler)
285 return NULL;
286
287 memset(handler, 0, sizeof(*handler));
288
289 handler->conf = conf;
290
291 handler->name = strdup(name);
292 if (!handler->name) {
293 ERROR("failed to allocate memory");
294 goto out_free;
295 }
296
297 /* Begin the set the state to STARTING*/
298 if (lxc_set_state(name, handler, STARTING)) {
299 ERROR("failed to set state '%s'", lxc_state2str(STARTING));
300 goto out_free_name;
301 }
302
303 if (lxc_create_tty(name, conf)) {
304 ERROR("failed to create the ttys");
305 goto out_aborting;
306 }
307
308 if (lxc_create_console(conf)) {
309 ERROR("failed to create console");
310 goto out_delete_tty;
311 }
312
313 /* the signal fd has to be created before forking otherwise
314 * if the child process exits before we setup the signal fd,
315 * the event will be lost and the command will be stuck */
316 handler->sigfd = setup_sigchld_fd(&handler->oldmask);
317 if (handler->sigfd < 0) {
318 ERROR("failed to set sigchild fd handler");
319 goto out_delete_console;
320 }
321
322 /* Avoid signals from terminal */
323 LXC_TTY_ADD_HANDLER(SIGINT);
324 LXC_TTY_ADD_HANDLER(SIGQUIT);
325
326 INFO("'%s' is initialized", name);
327 return handler;
328
329 out_delete_console:
330 lxc_delete_console(&conf->console);
331 out_delete_tty:
332 lxc_delete_tty(&conf->tty_info);
333 out_aborting:
334 lxc_set_state(name, handler, ABORTING);
335 out_free_name:
336 free(handler->name);
337 handler->name = NULL;
338 out_free:
339 free(handler);
340 return NULL;
341 }
342
343 void lxc_fini(const char *name, struct lxc_handler *handler)
344 {
345 /* The STOPPING state is there for future cleanup code
346 * which can take awhile
347 */
348 lxc_set_state(name, handler, STOPPING);
349 lxc_set_state(name, handler, STOPPED);
350 lxc_unlink_nsgroup(name);
351
352 lxc_delete_console(&handler->conf->console);
353 lxc_delete_tty(&handler->conf->tty_info);
354 free(handler->name);
355 free(handler);
356
357 LXC_TTY_DEL_HANDLER(SIGQUIT);
358 LXC_TTY_DEL_HANDLER(SIGINT);
359 }
360
361 void lxc_abort(const char *name, struct lxc_handler *handler)
362 {
363 lxc_set_state(name, handler, ABORTING);
364 if (handler->pid > 0)
365 kill(handler->pid, SIGKILL);
366 }
367
368 struct start_arg {
369 const char *name;
370 char *const *argv;
371 struct lxc_handler *handler;
372 int *sv;
373 };
374
375 static int do_start(void *arg)
376 {
377 struct start_arg *start_arg = arg;
378 struct lxc_handler *handler = start_arg->handler;
379 const char *name = start_arg->name;
380 char *const *argv = start_arg->argv;
381 int *sv = start_arg->sv;
382 int err = -1, sync;
383
384 if (sigprocmask(SIG_SETMASK, &handler->oldmask, NULL)) {
385 SYSERROR("failed to set sigprocmask");
386 return -1;
387 }
388
389 close(sv[1]);
390
391 /* Be sure we don't inherit this after the exec */
392 fcntl(sv[0], F_SETFD, FD_CLOEXEC);
393
394 /* Tell our father he can begin to configure the container */
395 if (write(sv[0], &sync, sizeof(sync)) < 0) {
396 SYSERROR("failed to write socket");
397 return -1;
398 }
399
400 /* Wait for the father to finish the configuration */
401 if (read(sv[0], &sync, sizeof(sync)) < 0) {
402 SYSERROR("failed to read socket");
403 return -1;
404 }
405
406 /* Setup the container, ip, names, utsname, ... */
407 if (lxc_setup(name, handler->conf)) {
408 ERROR("failed to setup the container");
409 goto out_warn_father;
410 }
411
412 if (prctl(PR_CAPBSET_DROP, CAP_SYS_BOOT, 0, 0, 0)) {
413 SYSERROR("failed to remove CAP_SYS_BOOT capability");
414 return -1;
415 }
416
417 if (prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0)) {
418 SYSERROR("failed to set pdeath signal");
419 return -1;
420 }
421
422 NOTICE("exec'ing '%s'", argv[0]);
423
424 execvp(argv[0], argv);
425 SYSERROR("failed to exec %s", argv[0]);
426
427 out_warn_father:
428 /* If the exec fails, tell that to our father */
429 if (write(sv[0], &err, sizeof(err)) < 0)
430 SYSERROR("failed to write the socket");
431 return -1;
432 }
433
434 int lxc_spawn(const char *name, struct lxc_handler *handler, char *const argv[])
435 {
436 int sv[2];
437 int clone_flags;
438 int err = -1, sync;
439
440 struct start_arg start_arg = {
441 .name = name,
442 .argv = argv,
443 .handler = handler,
444 .sv = sv,
445 };
446
447 /* Synchro socketpair */
448 if (socketpair(AF_LOCAL, SOCK_STREAM, 0, sv)) {
449 SYSERROR("failed to create communication socketpair");
450 return -1;
451 }
452
453 clone_flags = CLONE_NEWUTS|CLONE_NEWPID|CLONE_NEWIPC|CLONE_NEWNS;
454 if (!lxc_list_empty(&handler->conf->network)) {
455
456 clone_flags |= CLONE_NEWNET;
457
458 /* that should be done before the clone because we will
459 * fill the netdev index and use them in the child
460 */
461 if (lxc_create_network(&handler->conf->network)) {
462 ERROR("failed to create the network");
463 goto out_close;
464 }
465 }
466
467 /* Create a process in a new set of namespaces */
468 handler->pid = lxc_clone(do_start, &start_arg, clone_flags);
469 if (handler->pid < 0) {
470 SYSERROR("failed to fork into a new namespace");
471 goto out_delete_net;
472 }
473
474 close(sv[0]);
475
476 /* Wait for the child to be ready */
477 if (read(sv[1], &sync, sizeof(sync)) < 0) {
478 SYSERROR("failed to read the socket");
479 goto out_delete_net;
480 }
481
482 if (lxc_rename_nsgroup(name, handler))
483 goto out_delete_net;
484
485 /* Create the network configuration */
486 if (clone_flags & CLONE_NEWNET) {
487 if (lxc_assign_network(&handler->conf->network, handler->pid)) {
488 ERROR("failed to create the configured network");
489 goto out_delete_net;
490 }
491 }
492
493 /* Tell the child to continue its initialization */
494 if (write(sv[1], &sync, sizeof(sync)) < 0) {
495 SYSERROR("failed to write the socket");
496 goto out_abort;
497 }
498
499 /* Wait for the child to exec or returning an error */
500 if (read(sv[1], &sync, sizeof(sync)) < 0) {
501 ERROR("failed to read the socket");
502 goto out_abort;
503 }
504
505 if (lxc_set_state(name, handler, RUNNING)) {
506 ERROR("failed to set state to %s",
507 lxc_state2str(RUNNING));
508 goto out_abort;
509 }
510
511 err = 0;
512
513 NOTICE("'%s' started with pid '%d'", argv[0], handler->pid);
514
515 out_close:
516 close(sv[0]);
517 close(sv[1]);
518 return err;
519
520 out_delete_net:
521 if (clone_flags & CLONE_NEWNET)
522 lxc_delete_network(&handler->conf->network);
523 out_abort:
524 lxc_abort(name, handler);
525 close(sv[1]);
526 return -1;
527 }
528
529 int lxc_start(const char *name, char *const argv[], struct lxc_conf *conf)
530 {
531 struct lxc_handler *handler;
532 int err = -1;
533 int status;
534
535 if (lxc_check_inherited())
536 return -1;
537
538 handler = lxc_init(name, conf);
539 if (!handler) {
540 ERROR("failed to initialize the container");
541 return -1;
542 }
543
544 err = lxc_spawn(name, handler, argv);
545 if (err) {
546 ERROR("failed to spawn '%s'", argv[0]);
547 goto out_fini;
548 }
549
550 err = lxc_poll(name, handler);
551 if (err) {
552 ERROR("mainloop exited with an error");
553 goto out_abort;
554 }
555
556 while (waitpid(handler->pid, &status, 0) < 0 && errno == EINTR)
557 continue;
558
559 err = lxc_error_set_and_log(handler->pid, status);
560 out_fini:
561 lxc_fini(name, handler);
562 return err;
563
564 out_abort:
565 lxc_abort(name, handler);
566 goto out_fini;
567 }