88
112
#define PR_CAPBSET_DROP 24
93
#include <lxc/confile.h>
94
#include <lxc/start.h>
95
#include <lxc/utils.h>
96
#include <lxc/cgroup.h>
97
#include <lxc/monitor.h>
99
120
#include "error.h"
100
121
#include "af_unix.h"
101
122
#include "mainloop.h"
102
126
#include "commands.h"
105
130
lxc_log_define(lxc_start, lxc);
107
132
LXC_TTY_HANDLER(SIGINT);
108
133
LXC_TTY_HANDLER(SIGQUIT);
135
static int match_fd(int fd)
137
return (fd == 0 || fd == 1 || fd == 2);
140
int lxc_check_inherited(int fd_to_ignore)
142
struct dirent dirent, *direntp;
147
dir = opendir("/proc/self/fd");
149
WARN("failed to open directory: %m");
155
while (!readdir_r(dir, &dirent, &direntp)) {
162
if (!strcmp(direntp->d_name, "."))
165
if (!strcmp(direntp->d_name, ".."))
168
fd = atoi(direntp->d_name);
170
if (fd == fddir || fd == lxc_log_fd || fd == fd_to_ignore)
180
snprintf(procpath, sizeof(procpath), "/proc/self/fd/%d", fd);
182
if (readlink(procpath, path, sizeof(path)) == -1)
183
ERROR("readlink(%s) failed : %m", procpath);
185
ERROR("inherited fd %d on %s", fd, path);
189
ERROR("failed to close directory");
110
193
static int setup_sigchld_fd(sigset_t *oldmask)
142
static int sigchld_handler(int fd, void *data,
225
static int sigchld_handler(int fd, void *data,
143
226
struct lxc_epoll_descr *descr)
145
DEBUG("child exited");
228
struct signalfd_siginfo siginfo;
232
ret = read(fd, &siginfo, sizeof(siginfo));
234
ERROR("failed to read sigchld info");
238
if (ret != sizeof(siginfo)) {
239
ERROR("unexpected siginfo size");
243
if (siginfo.ssi_code == CLD_STOPPED ||
244
siginfo.ssi_code == CLD_CONTINUED) {
245
INFO("container init process was stopped/continued");
249
/* more robustness, protect ourself from a SIGCHLD sent
250
* by a process different from the container init
252
if (siginfo.ssi_pid != *pid) {
253
WARN("invalid pid for SIGCHLD");
257
DEBUG("container init process exited");
261
int lxc_pid_callback(int fd, struct lxc_request *request,
262
struct lxc_handler *handler)
264
struct lxc_answer answer;
267
answer.pid = handler->pid;
270
ret = send(fd, &answer, sizeof(answer), 0);
272
WARN("failed to send answer to the peer");
276
if (ret != sizeof(answer)) {
277
ERROR("partial answer sent");
150
284
int lxc_set_state(const char *name, struct lxc_handler *handler, lxc_state_t state)
152
286
handler->state = state;
170
304
goto out_mainloop_open;
173
if (lxc_command_mainloop_add(name, &descr, handler))
174
goto out_mainloop_open;
307
if (lxc_console_mainloop_add(&descr, handler)) {
308
ERROR("failed to add console handler to mainloop");
309
goto out_mainloop_open;
312
if (lxc_command_mainloop_add(name, &descr, handler)) {
313
ERROR("failed to add command handler to mainloop");
314
goto out_mainloop_open;
317
if (lxc_utmp_mainloop_add(&descr, handler)) {
318
ERROR("failed to add utmp handler to mainloop");
319
goto out_mainloop_open;
176
322
return lxc_mainloop(&descr);
185
static int fdname(int fd, char *name, size_t size)
187
char path[MAXPATHLEN];
190
snprintf(path, MAXPATHLEN, "/proc/self/fd/%d", fd);
192
len = readlink(path, name, size);
196
return (len <= 0) ? -1 : 0;
199
static int console_init(char *console, size_t size)
204
for (i = 0; i < 3; i++) {
208
if (ttyname_r(i, console, size)) {
209
SYSERROR("failed to retrieve tty name");
216
if (!fstat(0, &stat)) {
217
if (S_ISREG(stat.st_mode) || S_ISCHR(stat.st_mode) ||
218
S_ISFIFO(stat.st_mode) || S_ISLNK(stat.st_mode))
219
return fdname(0, console, size);
224
DEBUG("console initialized");
229
331
struct lxc_handler *lxc_init(const char *name, struct lxc_conf *conf)
231
333
struct lxc_handler *handler;
239
341
handler->conf = conf;
343
handler->name = strdup(name);
344
if (!handler->name) {
345
ERROR("failed to allocate memory");
241
349
/* Begin the set the state to STARTING*/
242
350
if (lxc_set_state(name, handler, STARTING)) {
243
351
ERROR("failed to set state '%s'", lxc_state2str(STARTING));
247
if (console_init(conf->console, sizeof(conf->console))) {
248
ERROR("failed to initialize the console");
252
355
if (lxc_create_tty(name, conf)) {
254
357
goto out_aborting;
360
if (lxc_create_console(conf)) {
361
ERROR("failed to create console");
257
365
/* the signal fd has to be created before forking otherwise
258
366
* if the child process exits before we setup the signal fd,
259
367
* the event will be lost and the command will be stuck */
260
368
handler->sigfd = setup_sigchld_fd(&handler->oldmask);
261
369
if (handler->sigfd < 0) {
262
370
ERROR("failed to set sigchild fd handler");
371
goto out_delete_console;
266
/* Avoid signals from terminal */
267
LXC_TTY_ADD_HANDLER(SIGINT);
268
LXC_TTY_ADD_HANDLER(SIGQUIT);
270
374
INFO("'%s' is initialized", name);
378
lxc_delete_console(&conf->console);
274
380
lxc_delete_tty(&conf->tty_info);
276
382
lxc_set_state(name, handler, ABORTING);
385
handler->name = NULL;
287
396
lxc_set_state(name, handler, STOPPING);
288
397
lxc_set_state(name, handler, STOPPED);
289
lxc_unlink_nsgroup(name);
399
/* reset mask set by setup_sigchld_fd */
400
if (sigprocmask(SIG_SETMASK, &handler->oldmask, NULL))
401
WARN("failed to restore sigprocmask");
403
lxc_delete_console(&handler->conf->console);
291
404
lxc_delete_tty(&handler->conf->tty_info);
294
LXC_TTY_DEL_HANDLER(SIGQUIT);
295
LXC_TTY_DEL_HANDLER(SIGINT);
298
409
void lxc_abort(const char *name, struct lxc_handler *handler)
300
411
lxc_set_state(name, handler, ABORTING);
301
kill(handler->pid, SIGKILL);
412
if (handler->pid > 0)
413
kill(handler->pid, SIGKILL);
307
struct lxc_handler *handler;
311
static int do_start(void *arg)
416
static int do_start(void *data)
313
struct start_arg *start_arg = arg;
314
struct lxc_handler *handler = start_arg->handler;
315
const char *name = start_arg->name;
316
char *const *argv = start_arg->argv;
317
int *sv = start_arg->sv;
418
struct lxc_handler *handler = data;
320
420
if (sigprocmask(SIG_SETMASK, &handler->oldmask, NULL)) {
321
421
SYSERROR("failed to set sigprocmask");
327
/* Be sure we don't inherit this after the exec */
328
fcntl(sv[0], F_SETFD, FD_CLOEXEC);
330
/* Tell our father he can begin to configure the container */
331
if (write(sv[0], &sync, sizeof(sync)) < 0) {
332
SYSERROR("failed to write socket");
336
/* Wait for the father to finish the configuration */
337
if (read(sv[0], &sync, sizeof(sync)) < 0) {
338
SYSERROR("failed to read socket");
425
lxc_sync_fini_parent(handler);
427
/* Tell the parent task it can begin to configure the
428
* container and wait for it to finish
430
if (lxc_sync_barrier_parent(handler, LXC_SYNC_CONFIGURE))
342
433
/* Setup the container, ip, names, utsname, ... */
343
if (lxc_setup(name, handler->conf)) {
434
if (lxc_setup(handler->name, handler->conf)) {
344
435
ERROR("failed to setup the container");
345
436
goto out_warn_father;
358
NOTICE("exec'ing '%s'", argv[0]);
449
close(handler->sigfd);
360
execvp(argv[0], argv);
361
SYSERROR("failed to exec %s", argv[0]);
451
/* after this call, we are in error because this
452
* ops should not return as it execs */
453
if (handler->ops->start(handler, handler->data))
364
/* If the exec fails, tell that to our father */
365
if (write(sv[0], &err, sizeof(err)) < 0)
366
SYSERROR("failed to write the socket");
457
lxc_sync_wake_parent(handler, LXC_SYNC_POST_CONFIGURE);
370
int lxc_spawn(const char *name, struct lxc_handler *handler, char *const argv[])
461
int lxc_spawn(struct lxc_handler *handler)
376
struct start_arg start_arg = {
383
/* Synchro socketpair */
384
if (socketpair(AF_LOCAL, SOCK_STREAM, 0, sv)) {
385
SYSERROR("failed to create communication socketpair");
464
int failed_before_rename = 0;
465
const char *name = handler->name;
467
if (lxc_sync_init(handler))
389
470
clone_flags = CLONE_NEWUTS|CLONE_NEWPID|CLONE_NEWIPC|CLONE_NEWNS;
390
471
if (!lxc_list_empty(&handler->conf->network)) {
397
478
if (lxc_create_network(&handler->conf->network)) {
398
479
ERROR("failed to create the network");
480
lxc_sync_fini(handler);
403
486
/* Create a process in a new set of namespaces */
404
handler->pid = lxc_clone(do_start, &start_arg, clone_flags);
487
handler->pid = lxc_clone(do_start, handler, clone_flags);
405
488
if (handler->pid < 0) {
406
489
SYSERROR("failed to fork into a new namespace");
412
/* Wait for the child to be ready */
413
if (read(sv[1], &sync, sizeof(sync)) < 0) {
414
SYSERROR("failed to read the socket");
493
lxc_sync_fini_child(handler);
495
if (lxc_sync_wait_child(handler, LXC_SYNC_CONFIGURE))
496
failed_before_rename = 1;
418
498
if (lxc_rename_nsgroup(name, handler))
501
if (failed_before_rename)
421
504
/* Create the network configuration */
422
505
if (clone_flags & CLONE_NEWNET) {
423
506
if (lxc_assign_network(&handler->conf->network, handler->pid)) {
424
507
ERROR("failed to create the configured network");
429
/* Tell the child to continue its initialization */
430
if (write(sv[1], &sync, sizeof(sync)) < 0) {
431
SYSERROR("failed to write the socket");
512
/* Tell the child to continue its initialization and wait for
513
* it to exec or return an error
515
if (lxc_sync_barrier_child(handler, LXC_SYNC_POST_CONFIGURE))
435
/* Wait for the child to exec or returning an error */
436
if (read(sv[1], &sync, sizeof(sync)) < 0) {
437
ERROR("failed to read the socket");
518
if (handler->ops->post_start(handler, handler->data))
441
521
if (lxc_set_state(name, handler, RUNNING)) {
442
522
ERROR("failed to set state to %s",
449
NOTICE("'%s' started with pid '%d'", argv[0], handler->pid);
527
lxc_sync_fini(handler);
531
if (clone_flags & CLONE_NEWNET)
532
lxc_delete_network(&handler->conf->network);
457
534
lxc_abort(name, handler);
535
lxc_sync_fini(handler);
462
int lxc_start(const char *name, char *const argv[], struct lxc_conf *conf)
539
int __lxc_start(const char *name, struct lxc_conf *conf,
540
struct lxc_operations* ops, void *data)
464
542
struct lxc_handler *handler;
470
548
ERROR("failed to initialize the container");
552
handler->data = data;
474
err = lxc_spawn(name, handler, argv);
554
err = lxc_spawn(handler);
476
ERROR("failed to spawn '%s'", argv[0]);
556
ERROR("failed to spawn '%s'", name);
480
err = lxc_close_all_inherited_fd();
482
ERROR("unable to close inherited fds");
560
/* Avoid signals from terminal */
561
LXC_TTY_ADD_HANDLER(SIGINT);
562
LXC_TTY_ADD_HANDLER(SIGQUIT);
486
564
err = lxc_poll(name, handler);
501
582
lxc_abort(name, handler);
590
static int start(struct lxc_handler *handler, void* data)
592
struct start_args *arg = data;
594
NOTICE("exec'ing '%s'", arg->argv[0]);
596
execvp(arg->argv[0], arg->argv);
597
SYSERROR("failed to exec %s", arg->argv[0]);
601
static int post_start(struct lxc_handler *handler, void* data)
603
struct start_args *arg = data;
605
NOTICE("'%s' started with pid '%d'", arg->argv[0], handler->pid);
609
static struct lxc_operations start_ops = {
611
.post_start = post_start
614
int lxc_start(const char *name, char *const argv[], struct lxc_conf *conf)
616
struct start_args start_arg = {
620
if (lxc_check_inherited(-1))
623
return __lxc_start(name, conf, &start_ops, &start_arg);