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