202
212
if (lxc_af_unix_rcv_credential(conn, &ttynum, sizeof(ttynum)))
205
if (ttynum <= 0 || ttynum > tty_info->nbtty)
208
/* fixup index array (eg. tty1 is index 0) */
211
if (tty_info->pty_info[ttynum].busy)
214
if (lxc_af_unix_send_fd(conn, tty_info->pty_info[ttynum].master,
216
if (ttynum > tty_info->nbtty)
219
if (tty_info->pty_info[ttynum - 1].busy)
225
/* fixup index tty1 => [0] */
227
ttynum <= tty_info->nbtty && tty_info->pty_info[ttynum - 1].busy;
230
/* we didn't find any available slot for tty */
231
if (ttynum > tty_info->nbtty)
235
if (lxc_af_unix_send_fd(conn, tty_info->pty_info[ttynum - 1].master,
236
&ttynum, sizeof(ttynum)) < 0) {
216
237
ERROR("failed to send tty to client");
220
if (lxc_mainloop_add_handler(descr, conn,
241
if (lxc_mainloop_add_handler(descr, conn,
221
242
ttyclient_handler, tty_info)) {
222
243
ERROR("failed to add tty client handler");
226
tty_info->pty_info[ttynum].busy = conn;
247
tty_info->pty_info[ttynum - 1].busy = conn;
285
int lxc_start(const char *name, char *argv[])
287
struct lxc_tty_info tty_info = { 0 };
289
char init[MAXPATHLEN];
290
char tty[MAXPATHLEN];
292
int fd, sigfd, lock, sv[2], sync = 0, err = -LXC_ERROR_INTERNAL;
296
lock = lxc_get_lock(name);
308
static int save_init_pid(const char *name, pid_t pid)
310
char init[MAXPATHLEN];
314
snprintf(init, MAXPATHLEN, LXCPATH "/%s/init", name);
316
if (!asprintf(&val, "%d\n", pid)) {
317
SYSERROR("failed to allocate memory");
321
fd = open(init, O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR);
323
SYSERROR("failed to open '%s'", init);
327
if (write(fd, val, strlen(val)) < 0) {
328
SYSERROR("failed to write the init pid");
342
static void remove_init_pid(const char *name, pid_t pid)
344
char init[MAXPATHLEN];
346
snprintf(init, MAXPATHLEN, LXCPATH "/%s/init", name);
350
static int fdname(int fd, char *name, size_t size)
352
char path[MAXPATHLEN];
355
snprintf(path, MAXPATHLEN, "/proc/self/fd/%d", fd);
357
len = readlink(path, name, size);
361
return (len <= 0) ? -1 : 0;
364
static int console_init(char *console, size_t size)
369
for (i = 0; i < 3; i++) {
373
if (ttyname_r(i, console, size)) {
374
SYSERROR("failed to retrieve tty name");
381
if (!fstat(0, &stat)) {
382
if (S_ISREG(stat.st_mode) || S_ISCHR(stat.st_mode) ||
383
S_ISFIFO(stat.st_mode) || S_ISLNK(stat.st_mode))
384
return fdname(0, console, size);
389
DEBUG("console initialized");
394
struct lxc_handler *lxc_init(const char *name)
396
struct lxc_handler *handler;
398
handler = malloc(sizeof(*handler));
402
memset(handler, 0, sizeof(*handler));
404
handler->lock = lxc_get_lock(name);
405
if (handler->lock < 0)
300
408
/* Begin the set the state to STARTING*/
301
409
if (lxc_setstate(name, STARTING)) {
302
ERROR("failed to set state '%s'",
303
lxc_state2str(STARTING));
307
/* If we are not attached to a tty, disable it */
308
if (ttyname_r(0, tty, sizeof(tty)))
311
if (lxc_create_tty(name, &tty_info)) {
410
ERROR("failed to set state '%s'", lxc_state2str(STARTING));
414
if (console_init(handler->tty, sizeof(handler->tty))) {
415
ERROR("failed to initialize the console");
419
if (lxc_create_tty(name, &handler->tty_info)) {
312
420
ERROR("failed to create the ttys");
316
424
/* the signal fd has to be created before forking otherwise
317
425
* if the child process exits before we setup the signal fd,
318
426
* the event will be lost and the command will be stuck */
319
sigfd = setup_sigchld_fd(&oldmask);
427
handler->sigfd = setup_sigchld_fd(&handler->oldmask);
428
if (handler->sigfd < 0) {
321
429
ERROR("failed to set sigchild fd handler");
433
/* Avoid signals from terminal */
434
LXC_TTY_ADD_HANDLER(SIGINT);
435
LXC_TTY_ADD_HANDLER(SIGQUIT);
439
INFO("'%s' is initialized", name);
444
lxc_delete_tty(&handler->tty_info);
446
lxc_setstate(name, ABORTING);
448
lxc_put_lock(handler->lock);
455
void lxc_fini(const char *name, struct lxc_handler *handler)
457
/* The STOPPING state is there for future cleanup code
458
* which can take awhile
460
lxc_setstate(name, STOPPING);
461
lxc_setstate(name, STOPPED);
462
lxc_unlink_nsgroup(name);
465
remove_init_pid(name, handler->pid);
466
lxc_delete_tty(&handler->tty_info);
467
lxc_put_lock(handler->lock);
471
LXC_TTY_DEL_HANDLER(SIGQUIT);
472
LXC_TTY_DEL_HANDLER(SIGINT);
475
void lxc_abort(const char *name, struct lxc_handler *handler)
477
lxc_setstate(name, ABORTING);
478
kill(handler->pid, SIGKILL);
484
struct lxc_handler *handler;
488
static int do_start(void *arg)
490
struct start_arg *start_arg = arg;
491
struct lxc_handler *handler = start_arg->handler;
492
const char *name = start_arg->name;
493
char *const *argv = start_arg->argv;
494
int *sv = start_arg->sv;
497
if (sigprocmask(SIG_SETMASK, &handler->oldmask, NULL)) {
498
SYSERROR("failed to set sigprocmask");
504
/* Be sure we don't inherit this after the exec */
505
fcntl(sv[0], F_SETFD, FD_CLOEXEC);
507
/* Tell our father he can begin to configure the container */
508
if (write(sv[0], &sync, sizeof(sync)) < 0) {
509
SYSERROR("failed to write socket");
513
/* Wait for the father to finish the configuration */
514
if (read(sv[0], &sync, sizeof(sync)) < 0) {
515
SYSERROR("failed to read socket");
519
/* Setup the container, ip, names, utsname, ... */
520
if (lxc_setup(name, handler->tty, &handler->tty_info)) {
521
ERROR("failed to setup the container");
522
goto out_warn_father;
525
if (prctl(PR_CAPBSET_DROP, CAP_SYS_BOOT, 0, 0, 0)) {
526
SYSERROR("failed to remove CAP_SYS_BOOT capability");
530
NOTICE("exec'ing '%s'", argv[0]);
532
execvp(argv[0], argv);
533
SYSERROR("failed to exec %s", argv[0]);
536
/* If the exec fails, tell that to our father */
537
if (write(sv[0], &err, sizeof(err)) < 0)
538
SYSERROR("failed to write the socket");
543
int lxc_spawn(const char *name, struct lxc_handler *handler, char *const argv[])
549
struct start_arg start_arg = {
325
556
/* Synchro socketpair */
326
557
if (socketpair(AF_LOCAL, SOCK_STREAM, 0, sv)) {
331
/* Avoid signals from terminal */
332
LXC_TTY_ADD_HANDLER(SIGINT);
333
LXC_TTY_ADD_HANDLER(SIGQUIT);
335
clone_flags = CLONE_NEWPID|CLONE_NEWIPC|CLONE_NEWNS;
336
if (conf_has_utsname(name))
337
clone_flags |= CLONE_NEWUTS;
562
clone_flags = CLONE_NEWUTS|CLONE_NEWPID|CLONE_NEWIPC|CLONE_NEWNS;
338
563
if (conf_has_network(name))
339
564
clone_flags |= CLONE_NEWNET;
341
566
/* Create a process in a new set of namespaces */
342
pid = fork_ns(clone_flags);
567
handler->pid = lxc_clone(do_start, &start_arg, clone_flags);
568
if (handler->pid < 0) {
344
569
SYSERROR("failed to fork into a new namespace");
350
if (sigprocmask(SIG_SETMASK, &oldmask, NULL)) {
351
SYSERROR("failed to set sigprocmask");
357
/* Be sure we don't inherit this after the exec */
358
fcntl(sv[0], F_SETFD, FD_CLOEXEC);
360
/* Tell our father he can begin to configure the container */
361
if (write(sv[0], &sync, sizeof(sync)) < 0) {
362
SYSERROR("failed to write socket");
366
/* Wait for the father to finish the configuration */
367
if (read(sv[0], &sync, sizeof(sync)) < 0) {
368
SYSERROR("failed to read socket");
372
/* Setup the container, ip, names, utsname, ... */
373
err = lxc_setup(name, tty, &tty_info);
375
ERROR("failed to setup the container");
376
if (write(sv[0], &err, sizeof(err)) < 0)
377
SYSERROR("failed to write the socket");
381
if (prctl(PR_CAPBSET_DROP, CAP_SYS_BOOT, 0, 0, 0)) {
382
SYSERROR("failed to remove CAP_SYS_BOOT capability");
386
execvp(argv[0], argv);
387
SYSERROR("failed to exec %s", argv[0]);
389
err = LXC_ERROR_WRONG_COMMAND;
390
/* If the exec fails, tell that to our father */
391
if (write(sv[0], &err, sizeof(err)) < 0)
392
SYSERROR("failed to write the socket");
400
575
/* Wait for the child to be ready */
401
576
if (read(sv[1], &sync, sizeof(sync)) < 0) {
402
577
SYSERROR("failed to read the socket");
406
if (lxc_link_nsgroup(name, pid))
407
WARN("cgroupfs not found: cgroup disabled");
581
if (lxc_rename_nsgroup(name, handler->pid) || lxc_link_nsgroup(name))
409
584
/* Create the network configuration */
410
if (clone_flags & CLONE_NEWNET && conf_create_network(name, pid)) {
585
if (clone_flags & CLONE_NEWNET &&
586
conf_create_network(name, handler->pid)) {
411
587
ERROR("failed to create the configured network");
412
goto err_create_network;
415
591
/* Tell the child to continue its initialization */
416
592
if (write(sv[1], &sync, sizeof(sync)) < 0) {
417
593
SYSERROR("failed to write the socket");
421
597
/* Wait for the child to exec or returning an error */
422
err = read(sv[1], &sync, sizeof(sync));
598
if (read(sv[1], &sync, sizeof(sync)) < 0) {
424
599
ERROR("failed to read the socket");
430
waitpid(pid, NULL, 0);
431
goto err_child_failed;
434
if (!asprintf(&val, "%d\n", pid)) {
435
SYSERROR("failed to allocate memory");
436
goto err_child_failed;
439
snprintf(init, MAXPATHLEN, LXCPATH "/%s/init", name);
441
fd = open(init, O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR);
443
SYSERROR("failed to open '%s'", init);
447
if (write(fd, val, strlen(val)) < 0) {
448
SYSERROR("failed to write the init pid");
603
if (save_init_pid(name, handler->pid)) {
604
ERROR("failed to save the init pid info");
454
608
if (lxc_setstate(name, RUNNING)) {
455
609
ERROR("failed to set state to %s",
456
610
lxc_state2str(RUNNING));
457
goto err_state_failed;
460
if (mainloop(name, pid, sigfd, &tty_info)) {
616
NOTICE("'%s' started with pid '%d'", argv[0], handler->pid);
625
lxc_abort(name, handler);
629
int lxc_start(const char *name, char *const argv[])
631
struct lxc_handler *handler;
635
handler = lxc_init(name);
637
ERROR("failed to initialize the container");
641
err = lxc_spawn(name, handler, argv);
643
ERROR("failed to spawn '%s'", argv[0]);
647
err = lxc_close_all_inherited_fd();
649
ERROR("unable to close inherited fds");
653
err = lxc_poll(name, handler);
461
655
ERROR("mainloop exited with an error");
462
goto err_mailoop_failed;
465
if (lxc_setstate(name, STOPPING))
466
ERROR("failed to set state %s", lxc_state2str(STOPPING));
468
if (clone_flags & CLONE_NEWNET && conf_destroy_network(name))
469
ERROR("failed to destroy the network");
659
while (waitpid(handler->pid, &status, 0) < 0 && errno == EINTR)
662
err = lxc_error_set_and_log(handler->pid, status);
473
if (lxc_setstate(name, STOPPED))
474
ERROR("failed to set state %s", lxc_state2str(STOPPED));
476
lxc_delete_tty(&tty_info);
477
lxc_unlink_nsgroup(name);
481
LXC_TTY_DEL_HANDLER(SIGQUIT);
482
LXC_TTY_DEL_HANDLER(SIGINT);
664
lxc_fini(name, handler);
493
if (clone_flags & CLONE_NEWNET)
494
conf_destroy_network(name);
498
if (lxc_setstate(name, ABORTING))
499
ERROR("failed to set state %s", lxc_state2str(STOPPED));
668
lxc_abort(name, handler);