]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/start.c
ttyclient_handler is only cleanup on disconnect
[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>
0ad19a3f 39#include <sys/types.h>
0ad19a3f 40#include <sys/prctl.h>
ddceb1f9 41#include <sys/types.h>
42ff343d 42#include <sys/capability.h>
0ad19a3f 43#include <sys/wait.h>
b0a33c1e 44#include <sys/un.h>
45#include <sys/poll.h>
ff218c25 46
47#ifdef HAVE_SYS_SIGNALFD_H
8ca61733 48# include <sys/signalfd.h>
ff218c25 49#else
8ca61733
MJ
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
bfa38025
MH
56# elif __powerpc__
57# define __NR_signalfd4 313
47f38330
SH
58# elif __s390x__
59# define __NR_signalfd4 322
8ca61733
MJ
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
bfa38025
MH
69# elif __powerpc__
70# define __NR_signalfd 305
47f38330
SH
71# elif __s390x__
72# define __NR_signalfd 316
8ca61733
MJ
73# endif
74#endif
75
76int 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}
ff218c25 85#endif
0ad19a3f 86
656994bb
MH
87#if !HAVE_DECL_PR_CAPBSET_DROP
88#define PR_CAPBSET_DROP 24
89#endif
90
e2bcd7db 91#include "error.h"
b0a33c1e 92#include "af_unix.h"
93#include "mainloop.h"
e2bcd7db 94
b113348e 95#include <lxc/lxc.h>
36eb9bde
CLG
96#include <lxc/log.h>
97
98lxc_log_define(lxc_start, lxc);
99
0ad19a3f 100LXC_TTY_HANDLER(SIGINT);
101LXC_TTY_HANDLER(SIGQUIT);
102
b0a33c1e 103static int setup_sigchld_fd(sigset_t *oldmask)
104{
105 sigset_t mask;
106 int fd;
107
108 if (sigprocmask(SIG_BLOCK, NULL, &mask)) {
36eb9bde 109 SYSERROR("failed to get mask signal");
b0a33c1e 110 return -1;
111 }
112
113 if (sigaddset(&mask, SIGCHLD) || sigprocmask(SIG_BLOCK, &mask, oldmask)) {
36eb9bde 114 SYSERROR("failed to set mask signal");
b0a33c1e 115 return -1;
116 }
117
118 fd = signalfd(-1, &mask, 0);
119 if (fd < 0) {
36eb9bde 120 SYSERROR("failed to create the signal fd");
b0a33c1e 121 return -1;
122 }
123
124 if (fcntl(fd, F_SETFD, FD_CLOEXEC)) {
36eb9bde 125 SYSERROR("failed to set sigfd to close-on-exec");
b0a33c1e 126 close(fd);
127 return -1;
128 }
129
1ac470c0
DL
130 DEBUG("sigchild handler set");
131
b0a33c1e 132 return fd;
133}
134
50c8bf05
MN
135static int ttyservice_handler(int fd, void *data,
136 struct lxc_epoll_descr *descr);
137
138static int tty_mainloop_add(const char *name, struct lxc_epoll_descr *descr,
139 struct lxc_handler *handler)
b0a33c1e 140{
50c8bf05 141 int ret, fd;
b0a33c1e 142 struct sockaddr_un addr = { 0 };
143 char *offset = &addr.sun_path[1];
50c8bf05 144
b0a33c1e 145 strcpy(offset, name);
146 addr.sun_path[0] = '\0';
147
148 fd = lxc_af_unix_open(addr.sun_path, SOCK_STREAM, 0);
50c8bf05
MN
149 if (fd < 0) {
150 ERROR("failed to create the tty service point");
b0a33c1e 151 return -1;
50c8bf05 152 }
b0a33c1e 153
154 if (fcntl(fd, F_SETFD, FD_CLOEXEC)) {
36eb9bde 155 SYSERROR("failed to close-on-exec flag");
b0a33c1e 156 close(fd);
157 return -1;
158 }
159
50c8bf05
MN
160 ret = lxc_mainloop_add_handler(descr, fd, ttyservice_handler,
161 handler);
162 if (ret) {
163 ERROR("failed to add handler for command socket");
164 close(fd);
165 }
b0a33c1e 166
50c8bf05 167 return ret;
b0a33c1e 168}
169
170static int sigchld_handler(int fd, void *data,
171 struct lxc_epoll_descr *descr)
172{
1ac470c0
DL
173 DEBUG("child exited");
174
b0a33c1e 175 return 1;
176}
177
35be16d7 178static void ttyinfo_remove_fd(int fd, struct lxc_tty_info *tty_info)
b0a33c1e 179{
180 int i;
b0a33c1e 181
182 for (i = 0; i < tty_info->nbtty; i++) {
183
184 if (tty_info->pty_info[i].busy != fd)
185 continue;
186
b0a33c1e 187 tty_info->pty_info[i].busy = 0;
b0a33c1e 188 }
189
35be16d7
MN
190 return;
191}
192
193static void command_fd_cleanup(int fd, struct lxc_tty_info *tty_info,
194 struct lxc_epoll_descr *descr)
195{
196 ttyinfo_remove_fd(fd, tty_info);
197 lxc_mainloop_del_handler(descr, fd);
198 close(fd);
199}
200
201static int ttyclient_handler(int fd, void *data,
202 struct lxc_epoll_descr *descr)
203{
204 struct lxc_tty_info *tty_info = data;
205
206 command_fd_cleanup(fd, tty_info, descr);
b0a33c1e 207}
208
209static int ttyservice_handler(int fd, void *data,
210 struct lxc_epoll_descr *descr)
211{
212 int conn, ttynum, val = 1, ret = -1;
50c8bf05
MN
213 struct lxc_handler *handler = data;
214 struct lxc_tty_info *tty_info = &handler->tty_info;
b0a33c1e 215
216 conn = accept(fd, NULL, 0);
217 if (conn < 0) {
36eb9bde 218 SYSERROR("failed to accept tty client");
b0a33c1e 219 return -1;
220 }
221
222 if (setsockopt(conn, SOL_SOCKET, SO_PASSCRED, &val, sizeof(val))) {
36eb9bde 223 SYSERROR("failed to enable credential on socket");
b0a33c1e 224 goto out_close;
225 }
226
2dcb28a9
MN
227 ret = lxc_af_unix_rcv_credential(conn, &ttynum, sizeof(ttynum));
228 if (ret < 0) {
229 SYSERROR("failed to receive data on tty socket");
b0a33c1e 230 goto out_close;
2dcb28a9
MN
231 }
232
233 if (!ret) {
234 DEBUG("peer has disconnected");
235 goto out_close;
236 }
237
238 if (ret != sizeof(ttynum)) {
239 WARN("partial request, ignored");
240 goto out_close;
241 }
b0a33c1e 242
be43f17e
DL
243 if (ttynum > 0) {
244 if (ttynum > tty_info->nbtty)
245 goto out_close;
246
247 if (tty_info->pty_info[ttynum - 1].busy)
248 goto out_close;
b0a33c1e 249
be43f17e
DL
250 goto out_send;
251 }
b0a33c1e 252
be43f17e
DL
253 /* fixup index tty1 => [0] */
254 for (ttynum = 1;
255 ttynum <= tty_info->nbtty && tty_info->pty_info[ttynum - 1].busy;
256 ttynum++);
257
258 /* we didn't find any available slot for tty */
259 if (ttynum > tty_info->nbtty)
b0a33c1e 260 goto out_close;
261
be43f17e
DL
262out_send:
263 if (lxc_af_unix_send_fd(conn, tty_info->pty_info[ttynum - 1].master,
264 &ttynum, sizeof(ttynum)) < 0) {
36eb9bde 265 ERROR("failed to send tty to client");
b0a33c1e 266 goto out_close;
267 }
268
be43f17e 269 if (lxc_mainloop_add_handler(descr, conn,
b0a33c1e 270 ttyclient_handler, tty_info)) {
36eb9bde 271 ERROR("failed to add tty client handler");
b0a33c1e 272 goto out_close;
273 }
274
be43f17e 275 tty_info->pty_info[ttynum - 1].busy = conn;
b0a33c1e 276
277 ret = 0;
b0a33c1e 278out:
279 return ret;
280out_close:
281 close(conn);
282 goto out;
283}
284
1bc5cc8c 285int lxc_poll(const char *name, struct lxc_handler *handler)
b0a33c1e 286{
ca5f7926
DL
287 int sigfd = handler->sigfd;
288 int pid = handler->pid;
289 const struct lxc_tty_info *tty_info = &handler->tty_info;
290
50c8bf05 291 int nfds, ret = -1;
b0a33c1e 292 struct lxc_epoll_descr descr;
293
b0a33c1e 294 /* sigfd + nb tty + tty service
295 * if tty is enabled */
296 nfds = tty_info->nbtty + 1 + tty_info->nbtty ? 1 : 0;
297
298 if (lxc_mainloop_open(nfds, &descr)) {
36eb9bde 299 ERROR("failed to create mainloop");
50c8bf05 300 goto out_sigfd;
b0a33c1e 301 }
302
303 if (lxc_mainloop_add_handler(&descr, sigfd, sigchld_handler, &pid)) {
36eb9bde 304 ERROR("failed to add handler for the signal");
b0a33c1e 305 goto out_mainloop_open;
306 }
307
308 if (tty_info->nbtty) {
50c8bf05 309 if (tty_mainloop_add(name, &descr, handler))
b0a33c1e 310 goto out_mainloop_open;
b0a33c1e 311 }
312
313 ret = lxc_mainloop(&descr);
314
315out:
316 return ret;
317
318out_mainloop_open:
319 lxc_mainloop_close(&descr);
b0a33c1e 320out_sigfd:
321 close(sigfd);
322 goto out;
323}
324
59eb99ba 325static int save_init_pid(const char *name, pid_t pid)
0ad19a3f 326{
22ebac19 327 char init[MAXPATHLEN];
59eb99ba
DL
328 char *val;
329 int fd, err = -1;
330
331 snprintf(init, MAXPATHLEN, LXCPATH "/%s/init", name);
332
333 if (!asprintf(&val, "%d\n", pid)) {
334 SYSERROR("failed to allocate memory");
335 goto out;
336 }
337
338 fd = open(init, O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR);
339 if (fd < 0) {
340 SYSERROR("failed to open '%s'", init);
341 goto out_free;
342 }
343
344 if (write(fd, val, strlen(val)) < 0) {
345 SYSERROR("failed to write the init pid");
346 goto out_close;
347 }
f4d507d5 348
59eb99ba
DL
349 err = 0;
350
351out_close:
352 close(fd);
353out_free:
354 free(val);
355out:
356 return err;
357}
358
359static void remove_init_pid(const char *name, pid_t pid)
360{
361 char init[MAXPATHLEN];
362
363 snprintf(init, MAXPATHLEN, LXCPATH "/%s/init", name);
364 unlink(init);
365}
366
b8f57738
DL
367static int fdname(int fd, char *name, size_t size)
368{
369 char path[MAXPATHLEN];
acc86941 370 ssize_t len;
b8f57738
DL
371
372 snprintf(path, MAXPATHLEN, "/proc/self/fd/%d", fd);
373
acc86941
MN
374 len = readlink(path, name, size);
375 if (len > 0)
376 path[len] = '\0';
377
378 return (len <= 0) ? -1 : 0;
b8f57738
DL
379}
380
381static int console_init(char *console, size_t size)
382{
383 struct stat stat;
384 int i;
385
386 for (i = 0; i < 3; i++) {
387 if (!isatty(i))
388 continue;
389
390 if (ttyname_r(i, console, size)) {
391 SYSERROR("failed to retrieve tty name");
392 return -1;
393 }
af795875 394
b8f57738
DL
395 return 0;
396 }
397
398 if (!fstat(0, &stat)) {
399 if (S_ISREG(stat.st_mode) || S_ISCHR(stat.st_mode) ||
400 S_ISFIFO(stat.st_mode) || S_ISLNK(stat.st_mode))
401 return fdname(0, console, size);
402 }
403
404 console[0] = '\0';
1ac470c0
DL
405
406 DEBUG("console initialized");
407
b8f57738
DL
408 return 0;
409}
410
3a0f472d 411struct lxc_handler *lxc_init(const char *name)
59eb99ba 412{
3a0f472d
DL
413 struct lxc_handler *handler;
414
415 handler = malloc(sizeof(*handler));
416 if (!handler)
417 return NULL;
59eb99ba
DL
418
419 memset(handler, 0, sizeof(*handler));
420
421 handler->lock = lxc_get_lock(name);
422 if (handler->lock < 0)
3a0f472d 423 goto out_free;
0ad19a3f 424
0ad19a3f 425 /* Begin the set the state to STARTING*/
426 if (lxc_setstate(name, STARTING)) {
59eb99ba
DL
427 ERROR("failed to set state '%s'", lxc_state2str(STARTING));
428 goto out_put_lock;
0ad19a3f 429 }
430
b8f57738
DL
431 if (console_init(handler->tty, sizeof(handler->tty))) {
432 ERROR("failed to initialize the console");
433 goto out_aborting;
434 }
939229eb 435
59eb99ba 436 if (lxc_create_tty(name, &handler->tty_info)) {
36eb9bde 437 ERROR("failed to create the ttys");
59eb99ba 438 goto out_aborting;
b0a33c1e 439 }
440
441 /* the signal fd has to be created before forking otherwise
442 * if the child process exits before we setup the signal fd,
443 * the event will be lost and the command will be stuck */
59eb99ba
DL
444 handler->sigfd = setup_sigchld_fd(&handler->oldmask);
445 if (handler->sigfd < 0) {
36eb9bde 446 ERROR("failed to set sigchild fd handler");
59eb99ba 447 goto out_delete_tty;
b0a33c1e 448 }
449
59eb99ba
DL
450 /* Avoid signals from terminal */
451 LXC_TTY_ADD_HANDLER(SIGINT);
452 LXC_TTY_ADD_HANDLER(SIGQUIT);
453
59eb99ba 454out:
1ac470c0
DL
455 if (handler)
456 INFO("'%s' is initialized", name);
457
3a0f472d 458 return handler;
59eb99ba
DL
459
460out_delete_tty:
461 lxc_delete_tty(&handler->tty_info);
462out_aborting:
463 lxc_setstate(name, ABORTING);
464out_put_lock:
465 lxc_put_lock(handler->lock);
3a0f472d
DL
466out_free:
467 free(handler);
468 handler = NULL;
59eb99ba
DL
469 goto out;
470}
471
1bc5cc8c 472void lxc_fini(const char *name, struct lxc_handler *handler)
59eb99ba
DL
473{
474 /* The STOPPING state is there for future cleanup code
475 * which can take awhile
476 */
477 lxc_setstate(name, STOPPING);
59eb99ba 478 lxc_setstate(name, STOPPED);
59eb99ba
DL
479 lxc_unlink_nsgroup(name);
480
3a0f472d
DL
481 if (handler) {
482 remove_init_pid(name, handler->pid);
483 lxc_delete_tty(&handler->tty_info);
484 lxc_put_lock(handler->lock);
485 free(handler);
486 }
59eb99ba
DL
487
488 LXC_TTY_DEL_HANDLER(SIGQUIT);
489 LXC_TTY_DEL_HANDLER(SIGINT);
490}
491
1bc5cc8c 492void lxc_abort(const char *name, struct lxc_handler *handler)
59eb99ba
DL
493{
494 lxc_setstate(name, ABORTING);
495 kill(handler->pid, SIGKILL);
496}
497
50e98013
DL
498struct start_arg {
499 const char *name;
500 char *const *argv;
501 struct lxc_handler *handler;
502 int *sv;
503};
504
505static int do_start(void *arg)
506{
507 struct start_arg *start_arg = arg;
508 struct lxc_handler *handler = start_arg->handler;
509 const char *name = start_arg->name;
510 char *const *argv = start_arg->argv;
511 int *sv = start_arg->sv;
512 int err = -1, sync;
513
514 if (sigprocmask(SIG_SETMASK, &handler->oldmask, NULL)) {
515 SYSERROR("failed to set sigprocmask");
516 goto out_child;
517 }
518
519 close(sv[1]);
520
521 /* Be sure we don't inherit this after the exec */
522 fcntl(sv[0], F_SETFD, FD_CLOEXEC);
523
524 /* Tell our father he can begin to configure the container */
525 if (write(sv[0], &sync, sizeof(sync)) < 0) {
526 SYSERROR("failed to write socket");
527 goto out_child;
528 }
529
530 /* Wait for the father to finish the configuration */
531 if (read(sv[0], &sync, sizeof(sync)) < 0) {
532 SYSERROR("failed to read socket");
533 goto out_child;
534 }
535
536 /* Setup the container, ip, names, utsname, ... */
537 if (lxc_setup(name, handler->tty, &handler->tty_info)) {
538 ERROR("failed to setup the container");
539 goto out_warn_father;
540 }
541
542 if (prctl(PR_CAPBSET_DROP, CAP_SYS_BOOT, 0, 0, 0)) {
543 SYSERROR("failed to remove CAP_SYS_BOOT capability");
544 goto out_child;
545 }
546
1ac470c0
DL
547 NOTICE("exec'ing '%s'", argv[0]);
548
50e98013
DL
549 execvp(argv[0], argv);
550 SYSERROR("failed to exec %s", argv[0]);
551
552out_warn_father:
553 /* If the exec fails, tell that to our father */
554 if (write(sv[0], &err, sizeof(err)) < 0)
555 SYSERROR("failed to write the socket");
556out_child:
557 return -1;
558}
559
9618063c 560int lxc_spawn(const char *name, struct lxc_handler *handler, char *const argv[])
59eb99ba
DL
561{
562 int sv[2];
563 int clone_flags;
564 int err = -1, sync;
565
50e98013
DL
566 struct start_arg start_arg = {
567 .name = name,
568 .argv = argv,
569 .handler = handler,
570 .sv = sv,
571 };
572
0ad19a3f 573 /* Synchro socketpair */
574 if (socketpair(AF_LOCAL, SOCK_STREAM, 0, sv)) {
36eb9bde 575 SYSERROR("failed to create communication socketpair");
f4d507d5 576 goto out;
0ad19a3f 577 }
578
1ea6db29 579 clone_flags = CLONE_NEWUTS|CLONE_NEWPID|CLONE_NEWIPC|CLONE_NEWNS;
0ad19a3f 580 if (conf_has_network(name))
581 clone_flags |= CLONE_NEWNET;
582
583 /* Create a process in a new set of namespaces */
50e98013 584 handler->pid = lxc_clone(do_start, &start_arg, clone_flags);
59eb99ba 585 if (handler->pid < 0) {
36eb9bde 586 SYSERROR("failed to fork into a new namespace");
59eb99ba 587 goto out_close;
0ad19a3f 588 }
589
0ad19a3f 590 close(sv[0]);
591
592 /* Wait for the child to be ready */
593 if (read(sv[1], &sync, sizeof(sync)) < 0) {
36eb9bde 594 SYSERROR("failed to read the socket");
59eb99ba 595 goto out_abort;
0ad19a3f 596 }
597
6203de18 598 if (lxc_rename_nsgroup(name, handler->pid) || lxc_link_nsgroup(name))
2b31f553 599 goto out_abort;
218d4250 600
0ad19a3f 601 /* Create the network configuration */
6203de18
DL
602 if (clone_flags & CLONE_NEWNET &&
603 conf_create_network(name, handler->pid)) {
36eb9bde 604 ERROR("failed to create the configured network");
59eb99ba 605 goto out_abort;
0ad19a3f 606 }
607
608 /* Tell the child to continue its initialization */
609 if (write(sv[1], &sync, sizeof(sync)) < 0) {
36eb9bde 610 SYSERROR("failed to write the socket");
59eb99ba 611 goto out_abort;
0ad19a3f 612 }
613
614 /* Wait for the child to exec or returning an error */
e043236e 615 if (read(sv[1], &sync, sizeof(sync)) < 0) {
36eb9bde 616 ERROR("failed to read the socket");
59eb99ba 617 goto out_abort;
0ad19a3f 618 }
619
59eb99ba
DL
620 if (save_init_pid(name, handler->pid)) {
621 ERROR("failed to save the init pid info");
622 goto out_abort;
0ad19a3f 623 }
624
59eb99ba
DL
625 if (lxc_setstate(name, RUNNING)) {
626 ERROR("failed to set state to %s",
627 lxc_state2str(RUNNING));
628 goto out_abort;
3f21c114 629 }
22ebac19 630
59eb99ba 631 err = 0;
22ebac19 632
1ac470c0
DL
633 NOTICE("'%s' started with pid '%d'", argv[0], handler->pid);
634
59eb99ba
DL
635out_close:
636 close(sv[0]);
637 close(sv[1]);
638out:
639 return err;
0ad19a3f 640
59eb99ba
DL
641out_abort:
642 lxc_abort(name, handler);
643 goto out_close;
644}
0ad19a3f 645
9618063c 646int lxc_start(const char *name, char *const argv[])
59eb99ba 647{
3a0f472d 648 struct lxc_handler *handler;
e043236e 649 int err = -1;
59eb99ba
DL
650 int status;
651
3a0f472d
DL
652 handler = lxc_init(name);
653 if (!handler) {
59eb99ba
DL
654 ERROR("failed to initialize the container");
655 goto out;
0ad19a3f 656 }
657
3a0f472d 658 err = lxc_spawn(name, handler, argv);
59eb99ba
DL
659 if (err) {
660 ERROR("failed to spawn '%s'", argv[0]);
661 goto out;
0ad19a3f 662 }
663
af795875 664 err = lxc_close_all_inherited_fd();
d983b93c
MN
665 if (err) {
666 ERROR("unable to close inherited fds");
667 goto out_abort;
668 }
669
3a0f472d 670 err = lxc_poll(name, handler);
e043236e 671 if (err) {
59eb99ba
DL
672 ERROR("mainloop exited with an error");
673 goto out_abort;
674 }
0ad19a3f 675
3a0f472d 676 while (waitpid(handler->pid, &status, 0) < 0 && errno == EINTR)
1bc5cc8c 677 continue;
e043236e 678
3a0f472d 679 err = lxc_error_set_and_log(handler->pid, status);
0ad19a3f 680out:
3a0f472d 681 lxc_fini(name, handler);
0ad19a3f 682 return err;
683
59eb99ba 684out_abort:
3a0f472d 685 lxc_abort(name, handler);
0ad19a3f 686 goto out;
687}