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