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