]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/start.c
conditional use of new capabilities
[mirror_lxc.git] / src / lxc / start.c
CommitLineData
0ad19a3f 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
ff218c25 24#include "../config.h"
0ad19a3f 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>
b0a33c1e 33#include <fcntl.h>
34#include <termios.h>
50e98013 35#include <namespace.h>
0ad19a3f 36#include <sys/param.h>
37#include <sys/file.h>
f4d507d5 38#include <sys/mount.h>
b4f8660e 39#include <sys/stat.h>
0ad19a3f 40#include <sys/types.h>
0ad19a3f 41#include <sys/prctl.h>
ddceb1f9 42#include <sys/types.h>
42ff343d 43#include <sys/capability.h>
0ad19a3f 44#include <sys/wait.h>
b0a33c1e 45#include <sys/un.h>
46#include <sys/poll.h>
ff218c25 47
15cd25fd 48#ifdef HAVE_SYS_SIGNALFD_H
8ca61733 49# include <sys/signalfd.h>
ff218c25 50#else
15cd25fd
DL
51/* assume kernel headers are too old */
52struct signalfd_siginfo
53{
da2aef7f
DL
54 uint32_t ssi_signo;
55 int32_t ssi_errno;
56 int32_t ssi_code;
57 uint32_t ssi_pid;
58 uint32_t ssi_uid;
59 int32_t ssi_fd;
60 uint32_t ssi_tid;
61 uint32_t ssi_band;
62 uint32_t ssi_overrun;
63 uint32_t ssi_trapno;
64 int32_t ssi_status;
65 int32_t ssi_int;
66 uint64_t ssi_ptr;
67 uint64_t ssi_utime;
68 uint64_t ssi_stime;
69 uint64_t ssi_addr;
70 uint8_t __pad[48];
15cd25fd
DL
71};
72
8ca61733
MJ
73# ifndef __NR_signalfd4
74/* assume kernel headers are too old */
75# if __i386__
76# define __NR_signalfd4 327
77# elif __x86_64__
78# define __NR_signalfd4 289
bfa38025
MH
79# elif __powerpc__
80# define __NR_signalfd4 313
47f38330
SH
81# elif __s390x__
82# define __NR_signalfd4 322
8ca61733
MJ
83# endif
84#endif
85
86# ifndef __NR_signalfd
87/* assume kernel headers are too old */
88# if __i386__
89# define __NR_signalfd 321
90# elif __x86_64__
91# define __NR_signalfd 282
bfa38025
MH
92# elif __powerpc__
93# define __NR_signalfd 305
47f38330
SH
94# elif __s390x__
95# define __NR_signalfd 316
8ca61733
MJ
96# endif
97#endif
98
99int signalfd(int fd, const sigset_t *mask, int flags)
100{
101 int retval;
102
103 retval = syscall (__NR_signalfd4, fd, mask, _NSIG / 8, flags);
104 if (errno == ENOSYS && flags == 0)
105 retval = syscall (__NR_signalfd, fd, mask, _NSIG / 8);
106 return retval;
107}
ff218c25 108#endif
0ad19a3f 109
656994bb
MH
110#if !HAVE_DECL_PR_CAPBSET_DROP
111#define PR_CAPBSET_DROP 24
112#endif
113
63376d7d
DL
114#include "start.h"
115#include "conf.h"
6a3111b8 116#include "cgroup.h"
63376d7d 117#include "log.h"
563f2f2c 118#include "cgroup.h"
e2bcd7db 119#include "error.h"
b0a33c1e 120#include "af_unix.h"
121#include "mainloop.h"
63376d7d 122#include "utils.h"
563f2f2c 123#include "utmp.h"
63376d7d 124#include "monitor.h"
96fa1ff0 125#include "commands.h"
63376d7d 126#include "console.h"
3c22086f 127#include "sync.h"
36eb9bde
CLG
128
129lxc_log_define(lxc_start, lxc);
130
0ad19a3f 131LXC_TTY_HANDLER(SIGINT);
132LXC_TTY_HANDLER(SIGQUIT);
133
80090207
CLG
134static int match_fd(int fd)
135{
136 return (fd == 0 || fd == 1 || fd == 2);
137}
138
f2faa8fa 139int lxc_check_inherited(int fd_to_ignore)
80090207
CLG
140{
141 struct dirent dirent, *direntp;
142 int fd, fddir;
143 DIR *dir;
144 int ret = 0;
145
146 dir = opendir("/proc/self/fd");
147 if (!dir) {
148 WARN("failed to open directory: %m");
149 return -1;
150 }
151
152 fddir = dirfd(dir);
153
154 while (!readdir_r(dir, &dirent, &direntp)) {
155 char procpath[64];
156 char path[PATH_MAX];
157
158 if (!direntp)
159 break;
160
161 if (!strcmp(direntp->d_name, "."))
162 continue;
163
164 if (!strcmp(direntp->d_name, ".."))
165 continue;
166
167 fd = atoi(direntp->d_name);
168
f2faa8fa 169 if (fd == fddir || fd == lxc_log_fd || fd == fd_to_ignore)
80090207
CLG
170 continue;
171
172 if (match_fd(fd))
173 continue;
174 /*
175 * found inherited fd
176 */
177 ret = -1;
178
179 snprintf(procpath, sizeof(procpath), "/proc/self/fd/%d", fd);
180
181 if (readlink(procpath, path, sizeof(path)) == -1)
182 ERROR("readlink(%s) failed : %m", procpath);
183 else
184 ERROR("inherited fd %d on %s", fd, path);
185 }
186
187 if (closedir(dir))
188 ERROR("failed to close directory");
189 return ret;
190}
191
b0a33c1e 192static int setup_sigchld_fd(sigset_t *oldmask)
193{
194 sigset_t mask;
195 int fd;
196
197 if (sigprocmask(SIG_BLOCK, NULL, &mask)) {
36eb9bde 198 SYSERROR("failed to get mask signal");
b0a33c1e 199 return -1;
200 }
201
202 if (sigaddset(&mask, SIGCHLD) || sigprocmask(SIG_BLOCK, &mask, oldmask)) {
36eb9bde 203 SYSERROR("failed to set mask signal");
b0a33c1e 204 return -1;
205 }
206
207 fd = signalfd(-1, &mask, 0);
208 if (fd < 0) {
36eb9bde 209 SYSERROR("failed to create the signal fd");
b0a33c1e 210 return -1;
211 }
212
213 if (fcntl(fd, F_SETFD, FD_CLOEXEC)) {
36eb9bde 214 SYSERROR("failed to set sigfd to close-on-exec");
b0a33c1e 215 close(fd);
216 return -1;
217 }
218
1ac470c0
DL
219 DEBUG("sigchild handler set");
220
b0a33c1e 221 return fd;
222}
223
15cd25fd 224static int sigchld_handler(int fd, void *data,
b0a33c1e 225 struct lxc_epoll_descr *descr)
226{
15cd25fd
DL
227 struct signalfd_siginfo siginfo;
228 int ret;
82d89dce 229 pid_t *pid = data;
15cd25fd
DL
230
231 ret = read(fd, &siginfo, sizeof(siginfo));
232 if (ret < 0) {
233 ERROR("failed to read sigchld info");
234 return -1;
235 }
236
237 if (ret != sizeof(siginfo)) {
238 ERROR("unexpected siginfo size");
239 return -1;
240 }
241
242 if (siginfo.ssi_code == CLD_STOPPED ||
243 siginfo.ssi_code == CLD_CONTINUED) {
244 INFO("container init process was stopped/continued");
245 return 0;
246 }
1ac470c0 247
82d89dce
DL
248 /* more robustness, protect ourself from a SIGCHLD sent
249 * by a process different from the container init
250 */
251 if (siginfo.ssi_pid != *pid) {
252 WARN("invalid pid for SIGCHLD");
253 return 0;
254 }
255
15cd25fd 256 DEBUG("container init process exited");
b0a33c1e 257 return 1;
258}
259
0a3ec350
DL
260int lxc_pid_callback(int fd, struct lxc_request *request,
261 struct lxc_handler *handler)
81c75799
DL
262{
263 struct lxc_answer answer;
264 int ret;
265
266 answer.pid = handler->pid;
267 answer.ret = 0;
268
269 ret = send(fd, &answer, sizeof(answer), 0);
270 if (ret < 0) {
271 WARN("failed to send answer to the peer");
272 return -1;
273 }
274
275 if (ret != sizeof(answer)) {
276 ERROR("partial answer sent");
277 return -1;
278 }
279
280 return 0;
281}
282
25c2aca5 283int lxc_set_state(const char *name, struct lxc_handler *handler, lxc_state_t state)
66aeffc7
DL
284{
285 handler->state = state;
286 lxc_monitor_send_state(name, state);
287 return 0;
288}
289
1bc5cc8c 290int lxc_poll(const char *name, struct lxc_handler *handler)
b0a33c1e 291{
ca5f7926
DL
292 int sigfd = handler->sigfd;
293 int pid = handler->pid;
b0a33c1e 294 struct lxc_epoll_descr descr;
295
a9e61274 296 if (lxc_mainloop_open(&descr)) {
36eb9bde 297 ERROR("failed to create mainloop");
50c8bf05 298 goto out_sigfd;
b0a33c1e 299 }
300
301 if (lxc_mainloop_add_handler(&descr, sigfd, sigchld_handler, &pid)) {
36eb9bde 302 ERROR("failed to add handler for the signal");
b0a33c1e 303 goto out_mainloop_open;
304 }
305
63376d7d
DL
306 if (lxc_console_mainloop_add(&descr, handler)) {
307 ERROR("failed to add console handler to mainloop");
308 goto out_mainloop_open;
309 }
310
563f2f2c
DL
311 if (lxc_command_mainloop_add(name, &descr, handler)) {
312 ERROR("failed to add command handler to mainloop");
96fa1ff0 313 goto out_mainloop_open;
563f2f2c
DL
314 }
315
316 if (lxc_utmp_mainloop_add(&descr, handler)) {
317 ERROR("failed to add utmp handler to mainloop");
318 goto out_mainloop_open;
319 }
b0a33c1e 320
c3e13372 321 return lxc_mainloop(&descr);
b0a33c1e 322
323out_mainloop_open:
324 lxc_mainloop_close(&descr);
b0a33c1e 325out_sigfd:
326 close(sigfd);
c3e13372 327 return -1;
b0a33c1e 328}
329
fae349da 330struct lxc_handler *lxc_init(const char *name, struct lxc_conf *conf)
59eb99ba 331{
3a0f472d
DL
332 struct lxc_handler *handler;
333
334 handler = malloc(sizeof(*handler));
335 if (!handler)
336 return NULL;
59eb99ba
DL
337
338 memset(handler, 0, sizeof(*handler));
339
fae349da
DL
340 handler->conf = conf;
341
3bdf52d7
DL
342 handler->name = strdup(name);
343 if (!handler->name) {
344 ERROR("failed to allocate memory");
345 goto out_free;
346 }
347
0ad19a3f 348 /* Begin the set the state to STARTING*/
25c2aca5 349 if (lxc_set_state(name, handler, STARTING)) {
59eb99ba 350 ERROR("failed to set state '%s'", lxc_state2str(STARTING));
3bdf52d7 351 goto out_free_name;
0ad19a3f 352 }
353
fae349da 354 if (lxc_create_tty(name, conf)) {
36eb9bde 355 ERROR("failed to create the ttys");
59eb99ba 356 goto out_aborting;
b0a33c1e 357 }
358
1560f6c9 359 if (lxc_create_console(conf)) {
63376d7d
DL
360 ERROR("failed to create console");
361 goto out_delete_tty;
362 }
363
b0a33c1e 364 /* the signal fd has to be created before forking otherwise
365 * if the child process exits before we setup the signal fd,
366 * the event will be lost and the command will be stuck */
59eb99ba
DL
367 handler->sigfd = setup_sigchld_fd(&handler->oldmask);
368 if (handler->sigfd < 0) {
36eb9bde 369 ERROR("failed to set sigchild fd handler");
63376d7d 370 goto out_delete_console;
b0a33c1e 371 }
372
c3e13372 373 INFO("'%s' is initialized", name);
3a0f472d 374 return handler;
59eb99ba 375
63376d7d
DL
376out_delete_console:
377 lxc_delete_console(&conf->console);
59eb99ba 378out_delete_tty:
fae349da 379 lxc_delete_tty(&conf->tty_info);
59eb99ba 380out_aborting:
25c2aca5 381 lxc_set_state(name, handler, ABORTING);
3bdf52d7
DL
382out_free_name:
383 free(handler->name);
384 handler->name = NULL;
3a0f472d
DL
385out_free:
386 free(handler);
c3e13372 387 return NULL;
59eb99ba
DL
388}
389
1bc5cc8c 390void lxc_fini(const char *name, struct lxc_handler *handler)
59eb99ba
DL
391{
392 /* The STOPPING state is there for future cleanup code
393 * which can take awhile
394 */
25c2aca5
MN
395 lxc_set_state(name, handler, STOPPING);
396 lxc_set_state(name, handler, STOPPED);
59eb99ba 397
8f64a3f6
MN
398 /* reset mask set by setup_sigchld_fd */
399 if (sigprocmask(SIG_SETMASK, &handler->oldmask, NULL))
400 WARN("failed to restore sigprocmask");
401
63376d7d 402 lxc_delete_console(&handler->conf->console);
b2431939 403 lxc_delete_tty(&handler->conf->tty_info);
3bdf52d7 404 free(handler->name);
b2431939 405 free(handler);
59eb99ba
DL
406}
407
1bc5cc8c 408void lxc_abort(const char *name, struct lxc_handler *handler)
59eb99ba 409{
25c2aca5 410 lxc_set_state(name, handler, ABORTING);
7d9fb3e9
DL
411 if (handler->pid > 0)
412 kill(handler->pid, SIGKILL);
59eb99ba
DL
413}
414
ffe1e01a 415static int do_start(void *data)
50e98013 416{
23c53af9 417 struct lxc_handler *handler = data;
50e98013
DL
418
419 if (sigprocmask(SIG_SETMASK, &handler->oldmask, NULL)) {
420 SYSERROR("failed to set sigprocmask");
9d7f9e52 421 return -1;
50e98013
DL
422 }
423
3c22086f 424 lxc_sync_fini_parent(handler);
50e98013 425
3c22086f
CLG
426 /* Tell the parent task it can begin to configure the
427 * container and wait for it to finish
428 */
429 if (lxc_sync_barrier_parent(handler, LXC_SYNC_CONFIGURE))
9d7f9e52 430 return -1;
50e98013
DL
431
432 /* Setup the container, ip, names, utsname, ... */
ffe1e01a 433 if (lxc_setup(handler->name, handler->conf)) {
50e98013
DL
434 ERROR("failed to setup the container");
435 goto out_warn_father;
436 }
437
438 if (prctl(PR_CAPBSET_DROP, CAP_SYS_BOOT, 0, 0, 0)) {
439 SYSERROR("failed to remove CAP_SYS_BOOT capability");
9d7f9e52 440 return -1;
50e98013
DL
441 }
442
6a6ad7af
DL
443 if (prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0)) {
444 SYSERROR("failed to set pdeath signal");
9d7f9e52 445 return -1;
6a6ad7af
DL
446 }
447
fc25b815
MN
448 close(handler->sigfd);
449
e6126dbe
MN
450 /* after this call, we are in error because this
451 * ops should not return as it execs */
23c53af9 452 if (handler->ops->start(handler, handler->data))
e6126dbe 453 return -1;
50e98013
DL
454
455out_warn_father:
3c22086f 456 lxc_sync_wake_parent(handler, LXC_SYNC_POST_CONFIGURE);
50e98013
DL
457 return -1;
458}
459
23c53af9 460int lxc_spawn(struct lxc_handler *handler)
59eb99ba 461{
59eb99ba 462 int clone_flags;
99a6af52 463 int failed_before_rename = 0;
ffe1e01a 464 const char *name = handler->name;
50e98013 465
3c22086f 466 if (lxc_sync_init(handler))
9d7f9e52 467 return -1;
0ad19a3f 468
1ea6db29 469 clone_flags = CLONE_NEWUTS|CLONE_NEWPID|CLONE_NEWIPC|CLONE_NEWNS;
fae349da 470 if (!lxc_list_empty(&handler->conf->network)) {
82d5ae15 471
0ad19a3f 472 clone_flags |= CLONE_NEWNET;
473
82d5ae15
DL
474 /* that should be done before the clone because we will
475 * fill the netdev index and use them in the child
476 */
fae349da 477 if (lxc_create_network(&handler->conf->network)) {
82d5ae15 478 ERROR("failed to create the network");
3c22086f 479 lxc_sync_fini(handler);
32e1c760 480 return -1;
82d5ae15
DL
481 }
482 }
483
ffe1e01a 484
0ad19a3f 485 /* Create a process in a new set of namespaces */
23c53af9 486 handler->pid = lxc_clone(do_start, handler, clone_flags);
59eb99ba 487 if (handler->pid < 0) {
36eb9bde 488 SYSERROR("failed to fork into a new namespace");
7fef7a06 489 goto out_delete_net;
0ad19a3f 490 }
491
3c22086f
CLG
492 lxc_sync_fini_child(handler);
493
494 if (lxc_sync_wait_child(handler, LXC_SYNC_CONFIGURE))
99a6af52 495 failed_before_rename = 1;
0ad19a3f 496
9f44c578 497 if (lxc_rename_nsgroup(name, handler))
7fef7a06 498 goto out_delete_net;
218d4250 499
99a6af52
MN
500 if (failed_before_rename)
501 goto out_delete_net;
502
0ad19a3f 503 /* Create the network configuration */
82d5ae15 504 if (clone_flags & CLONE_NEWNET) {
fae349da 505 if (lxc_assign_network(&handler->conf->network, handler->pid)) {
82d5ae15 506 ERROR("failed to create the configured network");
7fef7a06 507 goto out_delete_net;
82d5ae15 508 }
0ad19a3f 509 }
510
3c22086f
CLG
511 /* Tell the child to continue its initialization and wait for
512 * it to exec or return an error
513 */
514 if (lxc_sync_barrier_child(handler, LXC_SYNC_POST_CONFIGURE))
515 return -1;
0ad19a3f 516
23c53af9 517 if (handler->ops->post_start(handler, handler->data))
e6126dbe
MN
518 goto out_abort;
519
25c2aca5 520 if (lxc_set_state(name, handler, RUNNING)) {
59eb99ba
DL
521 ERROR("failed to set state to %s",
522 lxc_state2str(RUNNING));
523 goto out_abort;
3f21c114 524 }
22ebac19 525
3c22086f 526 lxc_sync_fini(handler);
e6126dbe 527 return 0;
1ac470c0 528
7fef7a06
DL
529out_delete_net:
530 if (clone_flags & CLONE_NEWNET)
531 lxc_delete_network(&handler->conf->network);
59eb99ba
DL
532out_abort:
533 lxc_abort(name, handler);
3c22086f 534 lxc_sync_fini(handler);
b79fcd86 535 return -1;
59eb99ba 536}
0ad19a3f 537
ee70bf78
CLG
538int __lxc_start(const char *name, struct lxc_conf *conf,
539 struct lxc_operations* ops, void *data)
59eb99ba 540{
3a0f472d 541 struct lxc_handler *handler;
e043236e 542 int err = -1;
59eb99ba 543 int status;
80090207 544
fae349da 545 handler = lxc_init(name, conf);
3a0f472d 546 if (!handler) {
59eb99ba 547 ERROR("failed to initialize the container");
66aeffc7 548 return -1;
0ad19a3f 549 }
ee70bf78
CLG
550 handler->ops = ops;
551 handler->data = data;
e6126dbe 552
23c53af9 553 err = lxc_spawn(handler);
59eb99ba 554 if (err) {
ee70bf78 555 ERROR("failed to spawn '%s'", name);
9d7f9e52 556 goto out_fini;
0ad19a3f 557 }
558
69ffa311
MN
559 /* Avoid signals from terminal */
560 LXC_TTY_ADD_HANDLER(SIGINT);
561 LXC_TTY_ADD_HANDLER(SIGQUIT);
562
3a0f472d 563 err = lxc_poll(name, handler);
e043236e 564 if (err) {
59eb99ba
DL
565 ERROR("mainloop exited with an error");
566 goto out_abort;
567 }
0ad19a3f 568
3a0f472d 569 while (waitpid(handler->pid, &status, 0) < 0 && errno == EINTR)
1bc5cc8c 570 continue;
e043236e 571
3a0f472d 572 err = lxc_error_set_and_log(handler->pid, status);
9d7f9e52 573out_fini:
69ffa311
MN
574 LXC_TTY_DEL_HANDLER(SIGQUIT);
575 LXC_TTY_DEL_HANDLER(SIGINT);
4d2e2ec6 576 lxc_unlink_nsgroup(name);
3a0f472d 577 lxc_fini(name, handler);
0ad19a3f 578 return err;
579
59eb99ba 580out_abort:
3a0f472d 581 lxc_abort(name, handler);
9d7f9e52 582 goto out_fini;
0ad19a3f 583}
ee70bf78
CLG
584
585struct start_args {
586 char *const *argv;
587};
588
589static int start(struct lxc_handler *handler, void* data)
590{
591 struct start_args *arg = data;
592
593 NOTICE("exec'ing '%s'", arg->argv[0]);
594
595 execvp(arg->argv[0], arg->argv);
596 SYSERROR("failed to exec %s", arg->argv[0]);
597 return 0;
598}
599
600static int post_start(struct lxc_handler *handler, void* data)
601{
602 struct start_args *arg = data;
603
604 NOTICE("'%s' started with pid '%d'", arg->argv[0], handler->pid);
605 return 0;
606}
607
608static struct lxc_operations start_ops = {
609 .start = start,
610 .post_start = post_start
611};
612
613int lxc_start(const char *name, char *const argv[], struct lxc_conf *conf)
614{
615 struct start_args start_arg = {
616 .argv = argv,
617 };
618
619 if (lxc_check_inherited(-1))
620 return -1;
621
622 return __lxc_start(name, conf, &start_ops, &start_arg);
623}