]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * lxc: linux Container library | |
3 | * | |
4 | * (C) Copyright IBM Corp. 2007, 2008 | |
5 | * | |
6 | * Authors: | |
7 | * Daniel Lezcano <daniel.lezcano at free.fr> | |
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
22 | */ | |
23 | ||
24 | #include "config.h" | |
25 | ||
26 | #include <stdio.h> | |
27 | #undef _GNU_SOURCE | |
28 | #include <string.h> | |
29 | #include <stdlib.h> | |
30 | #include <dirent.h> | |
31 | #include <errno.h> | |
32 | #include <unistd.h> | |
33 | #include <signal.h> | |
34 | #include <fcntl.h> | |
35 | #include <termios.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/socket.h> | |
42 | #include <sys/prctl.h> | |
43 | #include <sys/types.h> | |
44 | #include <sys/wait.h> | |
45 | #include <sys/un.h> | |
46 | #include <sys/poll.h> | |
47 | #include <sys/syscall.h> | |
48 | ||
49 | #if HAVE_SYS_CAPABILITY_H | |
50 | #include <sys/capability.h> | |
51 | #endif | |
52 | ||
53 | #if !HAVE_DECL_PR_CAPBSET_DROP | |
54 | #define PR_CAPBSET_DROP 24 | |
55 | #endif | |
56 | ||
57 | #include "start.h" | |
58 | #include "conf.h" | |
59 | #include "log.h" | |
60 | #include "cgroup.h" | |
61 | #include "error.h" | |
62 | #include "af_unix.h" | |
63 | #include "mainloop.h" | |
64 | #include "utils.h" | |
65 | #include "lxcutmp.h" | |
66 | #include "monitor.h" | |
67 | #include "commands.h" | |
68 | #include "console.h" | |
69 | #include "sync.h" | |
70 | #include "namespace.h" | |
71 | #include "lxcseccomp.h" | |
72 | #include "caps.h" | |
73 | #include "lxclock.h" | |
74 | #include "lsm/lsm.h" | |
75 | ||
76 | lxc_log_define(lxc_start, lxc); | |
77 | ||
78 | static int match_fd(int fd) | |
79 | { | |
80 | return (fd == 0 || fd == 1 || fd == 2); | |
81 | } | |
82 | ||
83 | int lxc_check_inherited(struct lxc_conf *conf, int fd_to_ignore) | |
84 | { | |
85 | struct dirent dirent, *direntp; | |
86 | int fd, fddir; | |
87 | DIR *dir; | |
88 | ||
89 | restart: | |
90 | process_lock(); | |
91 | dir = opendir("/proc/self/fd"); | |
92 | process_unlock(); | |
93 | if (!dir) { | |
94 | WARN("failed to open directory: %m"); | |
95 | return -1; | |
96 | } | |
97 | ||
98 | fddir = dirfd(dir); | |
99 | ||
100 | while (!readdir_r(dir, &dirent, &direntp)) { | |
101 | if (!direntp) | |
102 | break; | |
103 | ||
104 | if (!strcmp(direntp->d_name, ".")) | |
105 | continue; | |
106 | ||
107 | if (!strcmp(direntp->d_name, "..")) | |
108 | continue; | |
109 | ||
110 | fd = atoi(direntp->d_name); | |
111 | ||
112 | if (fd == fddir || fd == lxc_log_fd || fd == fd_to_ignore) | |
113 | continue; | |
114 | ||
115 | if (match_fd(fd)) | |
116 | continue; | |
117 | ||
118 | if (conf->close_all_fds) { | |
119 | process_lock(); | |
120 | close(fd); | |
121 | closedir(dir); | |
122 | process_unlock(); | |
123 | INFO("closed inherited fd %d", fd); | |
124 | goto restart; | |
125 | } | |
126 | WARN("inherited fd %d", fd); | |
127 | } | |
128 | ||
129 | process_lock(); | |
130 | closedir(dir); /* cannot fail */ | |
131 | process_unlock(); | |
132 | return 0; | |
133 | } | |
134 | ||
135 | static int setup_signal_fd(sigset_t *oldmask) | |
136 | { | |
137 | sigset_t mask; | |
138 | int fd; | |
139 | ||
140 | /* Block everything except serious error signals */ | |
141 | if (sigfillset(&mask) || | |
142 | sigdelset(&mask, SIGILL) || | |
143 | sigdelset(&mask, SIGSEGV) || | |
144 | sigdelset(&mask, SIGBUS) || | |
145 | sigdelset(&mask, SIGWINCH) || | |
146 | sigprocmask(SIG_BLOCK, &mask, oldmask)) { | |
147 | SYSERROR("failed to set signal mask"); | |
148 | return -1; | |
149 | } | |
150 | ||
151 | fd = signalfd(-1, &mask, 0); | |
152 | if (fd < 0) { | |
153 | SYSERROR("failed to create the signal fd"); | |
154 | return -1; | |
155 | } | |
156 | ||
157 | if (fcntl(fd, F_SETFD, FD_CLOEXEC)) { | |
158 | SYSERROR("failed to set sigfd to close-on-exec"); | |
159 | close(fd); | |
160 | return -1; | |
161 | } | |
162 | ||
163 | DEBUG("sigchild handler set"); | |
164 | ||
165 | return fd; | |
166 | } | |
167 | ||
168 | static int signal_handler(int fd, void *data, | |
169 | struct lxc_epoll_descr *descr) | |
170 | { | |
171 | struct signalfd_siginfo siginfo; | |
172 | siginfo_t info; | |
173 | int ret; | |
174 | pid_t *pid = data; | |
175 | bool init_died = false; | |
176 | ||
177 | ret = read(fd, &siginfo, sizeof(siginfo)); | |
178 | if (ret < 0) { | |
179 | ERROR("failed to read signal info"); | |
180 | return -1; | |
181 | } | |
182 | ||
183 | if (ret != sizeof(siginfo)) { | |
184 | ERROR("unexpected siginfo size"); | |
185 | return -1; | |
186 | } | |
187 | ||
188 | // check whether init is running | |
189 | info.si_pid = 0; | |
190 | ret = waitid(P_PID, *pid, &info, WEXITED | WNOWAIT | WNOHANG); | |
191 | if (ret == 0 && info.si_pid == *pid) { | |
192 | init_died = true; | |
193 | } | |
194 | ||
195 | if (siginfo.ssi_signo != SIGCHLD) { | |
196 | kill(*pid, siginfo.ssi_signo); | |
197 | INFO("forwarded signal %d to pid %d", siginfo.ssi_signo, *pid); | |
198 | return init_died ? 1 : 0; | |
199 | } | |
200 | ||
201 | if (siginfo.ssi_code == CLD_STOPPED || | |
202 | siginfo.ssi_code == CLD_CONTINUED) { | |
203 | INFO("container init process was stopped/continued"); | |
204 | return init_died ? 1 : 0; | |
205 | } | |
206 | ||
207 | /* more robustness, protect ourself from a SIGCHLD sent | |
208 | * by a process different from the container init | |
209 | */ | |
210 | if (siginfo.ssi_pid != *pid) { | |
211 | WARN("invalid pid for SIGCHLD"); | |
212 | return init_died ? 1 : 0; | |
213 | } | |
214 | ||
215 | DEBUG("container init process exited"); | |
216 | return 1; | |
217 | } | |
218 | ||
219 | int lxc_set_state(const char *name, struct lxc_handler *handler, lxc_state_t state) | |
220 | { | |
221 | handler->state = state; | |
222 | lxc_monitor_send_state(name, state, handler->lxcpath); | |
223 | return 0; | |
224 | } | |
225 | ||
226 | int lxc_poll(const char *name, struct lxc_handler *handler) | |
227 | { | |
228 | int sigfd = handler->sigfd; | |
229 | int pid = handler->pid; | |
230 | struct lxc_epoll_descr descr; | |
231 | ||
232 | if (lxc_mainloop_open(&descr)) { | |
233 | ERROR("failed to create mainloop"); | |
234 | goto out_sigfd; | |
235 | } | |
236 | ||
237 | if (lxc_mainloop_add_handler(&descr, sigfd, signal_handler, &pid)) { | |
238 | ERROR("failed to add handler for the signal"); | |
239 | goto out_mainloop_open; | |
240 | } | |
241 | ||
242 | if (lxc_console_mainloop_add(&descr, handler)) { | |
243 | ERROR("failed to add console handler to mainloop"); | |
244 | goto out_mainloop_open; | |
245 | } | |
246 | ||
247 | if (lxc_cmd_mainloop_add(name, &descr, handler)) { | |
248 | ERROR("failed to add command handler to mainloop"); | |
249 | goto out_mainloop_open; | |
250 | } | |
251 | ||
252 | if (handler->conf->need_utmp_watch) { | |
253 | #if HAVE_SYS_CAPABILITY_H | |
254 | if (lxc_utmp_mainloop_add(&descr, handler)) { | |
255 | ERROR("failed to add utmp handler to mainloop"); | |
256 | goto out_mainloop_open; | |
257 | } | |
258 | #else | |
259 | DEBUG("not starting utmp handler as cap_sys_boot cannot be dropped without capabilities support\n"); | |
260 | #endif | |
261 | } | |
262 | ||
263 | return lxc_mainloop(&descr, -1); | |
264 | ||
265 | out_mainloop_open: | |
266 | lxc_mainloop_close(&descr); | |
267 | out_sigfd: | |
268 | process_lock(); | |
269 | close(sigfd); | |
270 | process_unlock(); | |
271 | return -1; | |
272 | } | |
273 | ||
274 | struct lxc_handler *lxc_init(const char *name, struct lxc_conf *conf, const char *lxcpath) | |
275 | { | |
276 | struct lxc_handler *handler; | |
277 | ||
278 | handler = malloc(sizeof(*handler)); | |
279 | if (!handler) | |
280 | return NULL; | |
281 | ||
282 | memset(handler, 0, sizeof(*handler)); | |
283 | ||
284 | handler->conf = conf; | |
285 | handler->lxcpath = lxcpath; | |
286 | handler->pinfd = -1; | |
287 | ||
288 | lsm_init(); | |
289 | ||
290 | handler->name = strdup(name); | |
291 | if (!handler->name) { | |
292 | ERROR("failed to allocate memory"); | |
293 | goto out_free; | |
294 | } | |
295 | ||
296 | if (lxc_cmd_init(name, handler, lxcpath)) | |
297 | goto out_free_name; | |
298 | ||
299 | if (lxc_read_seccomp_config(conf) != 0) { | |
300 | ERROR("failed loading seccomp policy"); | |
301 | goto out_close_maincmd_fd; | |
302 | } | |
303 | ||
304 | /* Begin by setting the state to STARTING */ | |
305 | if (lxc_set_state(name, handler, STARTING)) { | |
306 | ERROR("failed to set state '%s'", lxc_state2str(STARTING)); | |
307 | goto out_close_maincmd_fd; | |
308 | } | |
309 | ||
310 | /* Start of environment variable setup for hooks */ | |
311 | if (setenv("LXC_NAME", name, 1)) { | |
312 | SYSERROR("failed to set environment variable for container name"); | |
313 | } | |
314 | if (setenv("LXC_CONFIG_FILE", conf->rcfile, 1)) { | |
315 | SYSERROR("failed to set environment variable for config path"); | |
316 | } | |
317 | if (setenv("LXC_ROOTFS_MOUNT", conf->rootfs.mount, 1)) { | |
318 | SYSERROR("failed to set environment variable for rootfs mount"); | |
319 | } | |
320 | if (setenv("LXC_ROOTFS_PATH", conf->rootfs.path, 1)) { | |
321 | SYSERROR("failed to set environment variable for rootfs mount"); | |
322 | } | |
323 | if (conf->console.path && setenv("LXC_CONSOLE", conf->console.path, 1)) { | |
324 | SYSERROR("failed to set environment variable for console path"); | |
325 | } | |
326 | if (conf->console.log_path && setenv("LXC_CONSOLE_LOGPATH", conf->console.log_path, 1)) { | |
327 | SYSERROR("failed to set environment variable for console log"); | |
328 | } | |
329 | /* End of environment variable setup for hooks */ | |
330 | ||
331 | if (run_lxc_hooks(name, "pre-start", conf, handler->lxcpath, NULL)) { | |
332 | ERROR("failed to run pre-start hooks for container '%s'.", name); | |
333 | goto out_aborting; | |
334 | } | |
335 | ||
336 | if (lxc_create_tty(name, conf)) { | |
337 | ERROR("failed to create the ttys"); | |
338 | goto out_aborting; | |
339 | } | |
340 | ||
341 | /* the signal fd has to be created before forking otherwise | |
342 | * if the child process exits before we setup the signal fd, | |
343 | * the event will be lost and the command will be stuck */ | |
344 | handler->sigfd = setup_signal_fd(&handler->oldmask); | |
345 | if (handler->sigfd < 0) { | |
346 | ERROR("failed to set sigchild fd handler"); | |
347 | goto out_delete_tty; | |
348 | } | |
349 | ||
350 | /* do this after setting up signals since it might unblock SIGWINCH */ | |
351 | if (lxc_console_create(conf)) { | |
352 | ERROR("failed to create console"); | |
353 | goto out_restore_sigmask; | |
354 | } | |
355 | ||
356 | if (ttys_shift_ids(conf) < 0) { | |
357 | ERROR("Failed to shift tty into container"); | |
358 | goto out_restore_sigmask; | |
359 | } | |
360 | ||
361 | INFO("'%s' is initialized", name); | |
362 | return handler; | |
363 | ||
364 | out_restore_sigmask: | |
365 | sigprocmask(SIG_SETMASK, &handler->oldmask, NULL); | |
366 | out_delete_tty: | |
367 | lxc_delete_tty(&conf->tty_info); | |
368 | out_aborting: | |
369 | lxc_set_state(name, handler, ABORTING); | |
370 | out_close_maincmd_fd: | |
371 | process_lock(); | |
372 | close(conf->maincmd_fd); | |
373 | process_unlock(); | |
374 | conf->maincmd_fd = -1; | |
375 | out_free_name: | |
376 | free(handler->name); | |
377 | handler->name = NULL; | |
378 | out_free: | |
379 | free(handler); | |
380 | return NULL; | |
381 | } | |
382 | ||
383 | static void lxc_fini(const char *name, struct lxc_handler *handler) | |
384 | { | |
385 | /* The STOPPING state is there for future cleanup code | |
386 | * which can take awhile | |
387 | */ | |
388 | lxc_set_state(name, handler, STOPPING); | |
389 | lxc_set_state(name, handler, STOPPED); | |
390 | ||
391 | if (run_lxc_hooks(name, "post-stop", handler->conf, handler->lxcpath, NULL)) | |
392 | ERROR("failed to run post-stop hooks for container '%s'.", name); | |
393 | ||
394 | /* reset mask set by setup_signal_fd */ | |
395 | if (sigprocmask(SIG_SETMASK, &handler->oldmask, NULL)) | |
396 | WARN("failed to restore sigprocmask"); | |
397 | ||
398 | lxc_console_delete(&handler->conf->console); | |
399 | lxc_delete_tty(&handler->conf->tty_info); | |
400 | process_lock(); | |
401 | close(handler->conf->maincmd_fd); | |
402 | process_unlock(); | |
403 | handler->conf->maincmd_fd = -1; | |
404 | free(handler->name); | |
405 | if (handler->cgroup) { | |
406 | lxc_cgroup_process_info_free_and_remove(handler->cgroup); | |
407 | handler->cgroup = NULL; | |
408 | } | |
409 | free(handler); | |
410 | } | |
411 | ||
412 | void lxc_abort(const char *name, struct lxc_handler *handler) | |
413 | { | |
414 | int ret, status; | |
415 | ||
416 | lxc_set_state(name, handler, ABORTING); | |
417 | if (handler->pid > 0) | |
418 | kill(handler->pid, SIGKILL); | |
419 | while ((ret = waitpid(-1, &status, 0)) > 0) ; | |
420 | } | |
421 | ||
422 | #include <sys/reboot.h> | |
423 | #include <linux/reboot.h> | |
424 | ||
425 | /* | |
426 | * reboot(LINUX_REBOOT_CMD_CAD_ON) will return -EINVAL | |
427 | * in a child pid namespace if container reboot support exists. | |
428 | * Otherwise, it will either succeed or return -EPERM. | |
429 | */ | |
430 | static int container_reboot_supported(void *arg) | |
431 | { | |
432 | int *cmd = arg; | |
433 | int ret; | |
434 | ||
435 | ret = reboot(*cmd); | |
436 | if (ret == -1 && errno == EINVAL) | |
437 | return 1; | |
438 | return 0; | |
439 | } | |
440 | ||
441 | static int must_drop_cap_sys_boot(struct lxc_conf *conf) | |
442 | { | |
443 | FILE *f; | |
444 | int ret, cmd, v, flags; | |
445 | long stack_size = 4096; | |
446 | void *stack = alloca(stack_size); | |
447 | int status; | |
448 | pid_t pid; | |
449 | ||
450 | process_lock(); | |
451 | f = fopen("/proc/sys/kernel/ctrl-alt-del", "r"); | |
452 | process_unlock(); | |
453 | if (!f) { | |
454 | DEBUG("failed to open /proc/sys/kernel/ctrl-alt-del"); | |
455 | return 1; | |
456 | } | |
457 | ||
458 | ret = fscanf(f, "%d", &v); | |
459 | process_lock(); | |
460 | fclose(f); | |
461 | process_unlock(); | |
462 | if (ret != 1) { | |
463 | DEBUG("Failed to read /proc/sys/kernel/ctrl-alt-del"); | |
464 | return 1; | |
465 | } | |
466 | cmd = v ? LINUX_REBOOT_CMD_CAD_ON : LINUX_REBOOT_CMD_CAD_OFF; | |
467 | ||
468 | flags = CLONE_NEWPID | SIGCHLD; | |
469 | if (!lxc_list_empty(&conf->id_map)) | |
470 | flags |= CLONE_NEWUSER; | |
471 | ||
472 | #ifdef __ia64__ | |
473 | pid = __clone2(container_reboot_supported, stack, stack_size, flags, &cmd); | |
474 | #else | |
475 | stack += stack_size; | |
476 | pid = clone(container_reboot_supported, stack, flags, &cmd); | |
477 | #endif | |
478 | if (pid < 0) { | |
479 | SYSERROR("failed to clone\n"); | |
480 | return -1; | |
481 | } | |
482 | if (wait(&status) < 0) { | |
483 | SYSERROR("unexpected wait error: %m\n"); | |
484 | return -1; | |
485 | } | |
486 | ||
487 | if (WEXITSTATUS(status) != 1) | |
488 | return 1; | |
489 | ||
490 | return 0; | |
491 | } | |
492 | ||
493 | static int do_start(void *data) | |
494 | { | |
495 | struct lxc_handler *handler = data; | |
496 | const char *lsm_label = NULL; | |
497 | ||
498 | if (sigprocmask(SIG_SETMASK, &handler->oldmask, NULL)) { | |
499 | SYSERROR("failed to set sigprocmask"); | |
500 | return -1; | |
501 | } | |
502 | ||
503 | /* This prctl must be before the synchro, so if the parent | |
504 | * dies before we set the parent death signal, we will detect | |
505 | * its death with the synchro right after, otherwise we have | |
506 | * a window where the parent can exit before we set the pdeath | |
507 | * signal leading to a unsupervized container. | |
508 | */ | |
509 | if (prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0)) { | |
510 | SYSERROR("failed to set pdeath signal"); | |
511 | return -1; | |
512 | } | |
513 | ||
514 | lxc_sync_fini_parent(handler); | |
515 | ||
516 | /* don't leak the pinfd to the container */ | |
517 | if (handler->pinfd >= 0) { | |
518 | process_lock(); | |
519 | close(handler->pinfd); | |
520 | process_unlock(); | |
521 | } | |
522 | ||
523 | /* Tell the parent task it can begin to configure the | |
524 | * container and wait for it to finish | |
525 | */ | |
526 | if (lxc_sync_barrier_parent(handler, LXC_SYNC_CONFIGURE)) | |
527 | return -1; | |
528 | ||
529 | /* | |
530 | * if we are in a new user namespace, become root there to have | |
531 | * privilege over our namespace | |
532 | */ | |
533 | if (!lxc_list_empty(&handler->conf->id_map)) { | |
534 | NOTICE("switching to gid/uid 0 in new user namespace"); | |
535 | if (setgid(0)) { | |
536 | SYSERROR("setgid"); | |
537 | goto out_warn_father; | |
538 | } | |
539 | if (setuid(0)) { | |
540 | SYSERROR("setuid"); | |
541 | goto out_warn_father; | |
542 | } | |
543 | } | |
544 | ||
545 | #if HAVE_SYS_CAPABILITY_H | |
546 | if (handler->conf->need_utmp_watch) { | |
547 | if (prctl(PR_CAPBSET_DROP, CAP_SYS_BOOT, 0, 0, 0)) { | |
548 | SYSERROR("failed to remove CAP_SYS_BOOT capability"); | |
549 | goto out_warn_father; | |
550 | } | |
551 | DEBUG("Dropped cap_sys_boot\n"); | |
552 | } | |
553 | #endif | |
554 | ||
555 | /* Setup the container, ip, names, utsname, ... */ | |
556 | if (lxc_setup(handler->name, handler->conf, handler->lxcpath, handler->cgroup)) { | |
557 | ERROR("failed to setup the container"); | |
558 | goto out_warn_father; | |
559 | } | |
560 | ||
561 | /* ask father to setup cgroups and wait for him to finish */ | |
562 | if (lxc_sync_barrier_parent(handler, LXC_SYNC_CGROUP)) | |
563 | return -1; | |
564 | ||
565 | /* Set the label to change to when we exec(2) the container's init */ | |
566 | if (!strcmp(lsm_name(), "AppArmor")) | |
567 | lsm_label = handler->conf->lsm_aa_profile; | |
568 | else if (!strcmp(lsm_name(), "SELinux")) | |
569 | lsm_label = handler->conf->lsm_se_context; | |
570 | if (lsm_process_label_set(lsm_label, 1, 1) < 0) | |
571 | goto out_warn_father; | |
572 | lsm_proc_unmount(handler->conf); | |
573 | ||
574 | if (lxc_seccomp_load(handler->conf) != 0) | |
575 | goto out_warn_father; | |
576 | ||
577 | if (run_lxc_hooks(handler->name, "start", handler->conf, handler->lxcpath, NULL)) { | |
578 | ERROR("failed to run start hooks for container '%s'.", handler->name); | |
579 | goto out_warn_father; | |
580 | } | |
581 | ||
582 | /* The clearenv() and putenv() calls have been moved here | |
583 | * to allow us to use enviroment variables passed to the various | |
584 | * hooks, such as the start hook above. Not all of the | |
585 | * variables like CONFIG_PATH or ROOTFS are valid in this | |
586 | * context but others are. */ | |
587 | if (clearenv()) { | |
588 | SYSERROR("failed to clear environment"); | |
589 | /* don't error out though */ | |
590 | } | |
591 | ||
592 | if (putenv("container=lxc")) { | |
593 | SYSERROR("failed to set environment variable"); | |
594 | goto out_warn_father; | |
595 | } | |
596 | ||
597 | process_lock(); | |
598 | close(handler->sigfd); | |
599 | process_unlock(); | |
600 | ||
601 | /* after this call, we are in error because this | |
602 | * ops should not return as it execs */ | |
603 | handler->ops->start(handler, handler->data); | |
604 | ||
605 | out_warn_father: | |
606 | /* we want the parent to know something went wrong, so any | |
607 | * value other than what it expects is ok. */ | |
608 | lxc_sync_wake_parent(handler, LXC_SYNC_POST_CONFIGURE); | |
609 | return -1; | |
610 | } | |
611 | ||
612 | int save_phys_nics(struct lxc_conf *conf) | |
613 | { | |
614 | struct lxc_list *iterator; | |
615 | ||
616 | lxc_list_for_each(iterator, &conf->network) { | |
617 | struct lxc_netdev *netdev = iterator->elem; | |
618 | ||
619 | if (netdev->type != LXC_NET_PHYS) | |
620 | continue; | |
621 | conf->saved_nics = realloc(conf->saved_nics, | |
622 | (conf->num_savednics+1)*sizeof(struct saved_nic)); | |
623 | if (!conf->saved_nics) { | |
624 | SYSERROR("failed to allocate memory"); | |
625 | return -1; | |
626 | } | |
627 | conf->saved_nics[conf->num_savednics].ifindex = netdev->ifindex; | |
628 | conf->saved_nics[conf->num_savednics].orig_name = strdup(netdev->link); | |
629 | if (!conf->saved_nics[conf->num_savednics].orig_name) { | |
630 | SYSERROR("failed to allocate memory"); | |
631 | return -1; | |
632 | } | |
633 | INFO("stored saved_nic #%d idx %d name %s\n", conf->num_savednics, | |
634 | conf->saved_nics[conf->num_savednics].ifindex, | |
635 | conf->saved_nics[conf->num_savednics].orig_name); | |
636 | conf->num_savednics++; | |
637 | } | |
638 | ||
639 | return 0; | |
640 | } | |
641 | ||
642 | int lxc_spawn(struct lxc_handler *handler) | |
643 | { | |
644 | int failed_before_rename = 0; | |
645 | const char *name = handler->name; | |
646 | struct cgroup_meta_data *cgroup_meta = NULL; | |
647 | const char *cgroup_pattern = NULL; | |
648 | ||
649 | if (lxc_sync_init(handler)) | |
650 | return -1; | |
651 | ||
652 | handler->clone_flags = CLONE_NEWUTS|CLONE_NEWPID|CLONE_NEWIPC|CLONE_NEWNS; | |
653 | if (!lxc_list_empty(&handler->conf->id_map)) { | |
654 | INFO("Cloning a new user namespace"); | |
655 | handler->clone_flags |= CLONE_NEWUSER; | |
656 | } | |
657 | if (!lxc_list_empty(&handler->conf->network)) { | |
658 | ||
659 | handler->clone_flags |= CLONE_NEWNET; | |
660 | ||
661 | /* Find gateway addresses from the link device, which is | |
662 | * no longer accessible inside the container. Do this | |
663 | * before creating network interfaces, since goto | |
664 | * out_delete_net does not work before lxc_clone. */ | |
665 | if (lxc_find_gateway_addresses(handler)) { | |
666 | ERROR("failed to find gateway addresses"); | |
667 | lxc_sync_fini(handler); | |
668 | return -1; | |
669 | } | |
670 | ||
671 | /* that should be done before the clone because we will | |
672 | * fill the netdev index and use them in the child | |
673 | */ | |
674 | if (lxc_create_network(handler)) { | |
675 | ERROR("failed to create the network"); | |
676 | lxc_sync_fini(handler); | |
677 | return -1; | |
678 | } | |
679 | } | |
680 | ||
681 | if (save_phys_nics(handler->conf)) { | |
682 | ERROR("failed to save physical nic info"); | |
683 | goto out_abort; | |
684 | } | |
685 | ||
686 | cgroup_meta = lxc_cgroup_load_meta(); | |
687 | if (!cgroup_meta) { | |
688 | ERROR("failed to detect cgroup metadata"); | |
689 | goto out_delete_net; | |
690 | } | |
691 | ||
692 | /* if we are running as root, use system cgroup pattern, otherwise | |
693 | * just create a cgroup under the current one. But also fall back to | |
694 | * that if for some reason reading the configuration fails and no | |
695 | * default value is available | |
696 | */ | |
697 | if (getuid() == 0) | |
698 | cgroup_pattern = default_cgroup_pattern(); | |
699 | if (!cgroup_pattern) | |
700 | cgroup_pattern = "%n"; | |
701 | ||
702 | /* Create cgroup before doing clone(), so the child will know from | |
703 | * handler which cgroup it is going to be put in later. | |
704 | */ | |
705 | if ((handler->cgroup = lxc_cgroup_create(name, cgroup_pattern, cgroup_meta, NULL)) == NULL) { | |
706 | ERROR("failed to create cgroups for '%s'", name); | |
707 | goto out_delete_net; | |
708 | } | |
709 | ||
710 | /* | |
711 | * if the rootfs is not a blockdev, prevent the container from | |
712 | * marking it readonly. | |
713 | */ | |
714 | ||
715 | handler->pinfd = pin_rootfs(handler->conf->rootfs.path); | |
716 | if (handler->pinfd == -1) | |
717 | INFO("failed to pin the container's rootfs"); | |
718 | ||
719 | /* Create a process in a new set of namespaces */ | |
720 | handler->pid = lxc_clone(do_start, handler, handler->clone_flags); | |
721 | if (handler->pid < 0) { | |
722 | SYSERROR("failed to fork into a new namespace"); | |
723 | goto out_delete_net; | |
724 | } | |
725 | ||
726 | lxc_sync_fini_child(handler); | |
727 | ||
728 | if (lxc_sync_wait_child(handler, LXC_SYNC_CONFIGURE)) | |
729 | failed_before_rename = 1; | |
730 | ||
731 | /* In case there is still legacy ns cgroup support in the kernel. | |
732 | * Should be removed at some later point in time. | |
733 | */ | |
734 | if (lxc_cgroup_create_legacy(handler->cgroup, name, handler->pid) < 0) { | |
735 | ERROR("failed to create legacy ns cgroups for '%s'", name); | |
736 | goto out_delete_net; | |
737 | } | |
738 | ||
739 | if (lxc_setup_cgroup_without_devices(handler, &handler->conf->cgroup)) { | |
740 | ERROR("failed to setup the cgroups for '%s'", name); | |
741 | goto out_delete_net; | |
742 | } | |
743 | ||
744 | if (lxc_cgroup_enter(handler->cgroup, handler->pid, false) < 0) | |
745 | goto out_delete_net; | |
746 | ||
747 | if (failed_before_rename) | |
748 | goto out_delete_net; | |
749 | ||
750 | /* Create the network configuration */ | |
751 | if (handler->clone_flags & CLONE_NEWNET) { | |
752 | if (lxc_assign_network(&handler->conf->network, handler->pid)) { | |
753 | ERROR("failed to create the configured network"); | |
754 | goto out_delete_net; | |
755 | } | |
756 | } | |
757 | ||
758 | /* map the container uids - the container became an invalid | |
759 | * userid the moment it was cloned with CLONE_NEWUSER - this | |
760 | * call doesn't change anything immediately, but allows the | |
761 | * container to setuid(0) (0 being mapped to something else on | |
762 | * the host) later to become a valid uid again */ | |
763 | if (lxc_map_ids(&handler->conf->id_map, handler->pid)) { | |
764 | ERROR("failed to set up id mapping"); | |
765 | goto out_delete_net; | |
766 | } | |
767 | ||
768 | /* Tell the child to continue its initialization. we'll get | |
769 | * LXC_SYNC_CGROUP when it is ready for us to setup cgroups | |
770 | */ | |
771 | if (lxc_sync_barrier_child(handler, LXC_SYNC_POST_CONFIGURE)) | |
772 | goto out_delete_net; | |
773 | ||
774 | if (lxc_setup_cgroup_devices(handler, &handler->conf->cgroup)) { | |
775 | ERROR("failed to setup the devices cgroup for '%s'", name); | |
776 | goto out_delete_net; | |
777 | } | |
778 | ||
779 | /* Tell the child to complete its initialization and wait for | |
780 | * it to exec or return an error. (the child will never | |
781 | * return LXC_SYNC_POST_CGROUP+1. It will either close the | |
782 | * sync pipe, causing lxc_sync_barrier_child to return | |
783 | * success, or return a different value, causing us to error | |
784 | * out). | |
785 | */ | |
786 | if (lxc_sync_barrier_child(handler, LXC_SYNC_POST_CGROUP)) | |
787 | return -1; | |
788 | ||
789 | if (detect_shared_rootfs()) | |
790 | umount2(handler->conf->rootfs.mount, MNT_DETACH); | |
791 | ||
792 | if (handler->ops->post_start(handler, handler->data)) | |
793 | goto out_abort; | |
794 | ||
795 | if (lxc_set_state(name, handler, RUNNING)) { | |
796 | ERROR("failed to set state to %s", | |
797 | lxc_state2str(RUNNING)); | |
798 | goto out_abort; | |
799 | } | |
800 | ||
801 | lxc_cgroup_put_meta(cgroup_meta); | |
802 | lxc_sync_fini(handler); | |
803 | ||
804 | return 0; | |
805 | ||
806 | out_delete_net: | |
807 | if (handler->clone_flags & CLONE_NEWNET) | |
808 | lxc_delete_network(handler); | |
809 | out_abort: | |
810 | lxc_cgroup_put_meta(cgroup_meta); | |
811 | lxc_abort(name, handler); | |
812 | lxc_sync_fini(handler); | |
813 | if (handler->pinfd >= 0) { | |
814 | process_lock(); | |
815 | close(handler->pinfd); | |
816 | process_unlock(); | |
817 | handler->pinfd = -1; | |
818 | } | |
819 | ||
820 | return -1; | |
821 | } | |
822 | ||
823 | int __lxc_start(const char *name, struct lxc_conf *conf, | |
824 | struct lxc_operations* ops, void *data, const char *lxcpath) | |
825 | { | |
826 | struct lxc_handler *handler; | |
827 | int err = -1; | |
828 | int status; | |
829 | ||
830 | handler = lxc_init(name, conf, lxcpath); | |
831 | if (!handler) { | |
832 | ERROR("failed to initialize the container"); | |
833 | return -1; | |
834 | } | |
835 | handler->ops = ops; | |
836 | handler->data = data; | |
837 | ||
838 | if (must_drop_cap_sys_boot(handler->conf)) { | |
839 | #if HAVE_SYS_CAPABILITY_H | |
840 | DEBUG("Dropping cap_sys_boot\n"); | |
841 | #else | |
842 | DEBUG("Can't drop cap_sys_boot as capabilities aren't supported\n"); | |
843 | #endif | |
844 | } else { | |
845 | DEBUG("Not dropping cap_sys_boot or watching utmp\n"); | |
846 | handler->conf->need_utmp_watch = 0; | |
847 | } | |
848 | ||
849 | err = lxc_spawn(handler); | |
850 | if (err) { | |
851 | ERROR("failed to spawn '%s'", name); | |
852 | goto out_fini_nonet; | |
853 | } | |
854 | ||
855 | err = lxc_poll(name, handler); | |
856 | if (err) { | |
857 | ERROR("mainloop exited with an error"); | |
858 | goto out_abort; | |
859 | } | |
860 | ||
861 | while (waitpid(handler->pid, &status, 0) < 0 && errno == EINTR) | |
862 | continue; | |
863 | ||
864 | /* | |
865 | * If the child process exited but was not signaled, | |
866 | * it didn't call reboot. This should mean it was an | |
867 | * lxc-execute which simply exited. In any case, treat | |
868 | * it as a 'halt' | |
869 | */ | |
870 | if (WIFSIGNALED(status)) { | |
871 | switch(WTERMSIG(status)) { | |
872 | case SIGINT: /* halt */ | |
873 | DEBUG("Container halting"); | |
874 | break; | |
875 | case SIGHUP: /* reboot */ | |
876 | DEBUG("Container rebooting"); | |
877 | handler->conf->reboot = 1; | |
878 | break; | |
879 | default: | |
880 | DEBUG("unknown exit status for init: %d\n", WTERMSIG(status)); | |
881 | break; | |
882 | } | |
883 | } | |
884 | ||
885 | lxc_rename_phys_nics_on_shutdown(handler->conf); | |
886 | ||
887 | if (handler->pinfd >= 0) { | |
888 | process_lock(); | |
889 | close(handler->pinfd); | |
890 | process_unlock(); | |
891 | handler->pinfd = -1; | |
892 | } | |
893 | ||
894 | err = lxc_error_set_and_log(handler->pid, status); | |
895 | out_fini: | |
896 | lxc_delete_network(handler); | |
897 | ||
898 | out_fini_nonet: | |
899 | lxc_fini(name, handler); | |
900 | return err; | |
901 | ||
902 | out_abort: | |
903 | lxc_abort(name, handler); | |
904 | goto out_fini; | |
905 | } | |
906 | ||
907 | struct start_args { | |
908 | char *const *argv; | |
909 | }; | |
910 | ||
911 | static int start(struct lxc_handler *handler, void* data) | |
912 | { | |
913 | struct start_args *arg = data; | |
914 | ||
915 | NOTICE("exec'ing '%s'", arg->argv[0]); | |
916 | ||
917 | execvp(arg->argv[0], arg->argv); | |
918 | SYSERROR("failed to exec %s", arg->argv[0]); | |
919 | return 0; | |
920 | } | |
921 | ||
922 | static int post_start(struct lxc_handler *handler, void* data) | |
923 | { | |
924 | struct start_args *arg = data; | |
925 | ||
926 | NOTICE("'%s' started with pid '%d'", arg->argv[0], handler->pid); | |
927 | return 0; | |
928 | } | |
929 | ||
930 | static struct lxc_operations start_ops = { | |
931 | .start = start, | |
932 | .post_start = post_start | |
933 | }; | |
934 | ||
935 | int lxc_start(const char *name, char *const argv[], struct lxc_conf *conf, | |
936 | const char *lxcpath) | |
937 | { | |
938 | struct start_args start_arg = { | |
939 | .argv = argv, | |
940 | }; | |
941 | ||
942 | if (lxc_check_inherited(conf, -1)) | |
943 | return -1; | |
944 | ||
945 | conf->need_utmp_watch = 1; | |
946 | return __lxc_start(name, conf, &start_ops, &start_arg, lxcpath); | |
947 | } |