]>
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 | |
250b1eec | 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
0ad19a3f | 22 | */ |
23 | ||
f549edcc GK |
24 | #include "config.h" |
25 | ||
0ad19a3f | 26 | #include <stdio.h> |
0ad19a3f | 27 | #include <string.h> |
28 | #include <stdlib.h> | |
29 | #include <dirent.h> | |
30 | #include <errno.h> | |
31 | #include <unistd.h> | |
32 | #include <signal.h> | |
b0a33c1e | 33 | #include <fcntl.h> |
c476bdce | 34 | #include <grp.h> |
37515ebd | 35 | #include <poll.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> |
8173e600 | 46 | #include <sys/syscall.h> |
ff218c25 | 47 | |
495d2046 SG |
48 | #if HAVE_SYS_CAPABILITY_H |
49 | #include <sys/capability.h> | |
50 | #endif | |
51 | ||
656994bb MH |
52 | #if !HAVE_DECL_PR_CAPBSET_DROP |
53 | #define PR_CAPBSET_DROP 24 | |
54 | #endif | |
55 | ||
63376d7d DL |
56 | #include "start.h" |
57 | #include "conf.h" | |
58 | #include "log.h" | |
563f2f2c | 59 | #include "cgroup.h" |
e2bcd7db | 60 | #include "error.h" |
b0a33c1e | 61 | #include "af_unix.h" |
62 | #include "mainloop.h" | |
63376d7d | 63 | #include "utils.h" |
565c2d76 | 64 | #include "lxcutmp.h" |
63376d7d | 65 | #include "monitor.h" |
96fa1ff0 | 66 | #include "commands.h" |
63376d7d | 67 | #include "console.h" |
3c22086f | 68 | #include "sync.h" |
f549edcc | 69 | #include "namespace.h" |
0d0527a9 | 70 | #include "lxcseccomp.h" |
8173e600 | 71 | #include "caps.h" |
76a26f55 | 72 | #include "bdev.h" |
fe4de9a6 | 73 | #include "lsm/lsm.h" |
36eb9bde CLG |
74 | |
75 | lxc_log_define(lxc_start, lxc); | |
76 | ||
9f30a190 MM |
77 | const struct ns_info ns_info[LXC_NS_MAX] = { |
78 | [LXC_NS_MNT] = {"mnt", CLONE_NEWNS}, | |
79 | [LXC_NS_PID] = {"pid", CLONE_NEWPID}, | |
80 | [LXC_NS_UTS] = {"uts", CLONE_NEWUTS}, | |
81 | [LXC_NS_IPC] = {"ipc", CLONE_NEWIPC}, | |
82 | [LXC_NS_USER] = {"user", CLONE_NEWUSER}, | |
83 | [LXC_NS_NET] = {"net", CLONE_NEWNET} | |
84 | }; | |
85 | ||
c8154066 SH |
86 | static void print_top_failing_dir(const char *path) |
87 | { | |
88 | size_t len = strlen(path); | |
89 | char *copy = alloca(len+1), *p, *e, saved; | |
90 | strcpy(copy, path); | |
91 | ||
92 | p = copy; | |
93 | e = copy + len; | |
94 | while (p < e) { | |
95 | while (p < e && *p == '/') p++; | |
96 | while (p < e && *p != '/') p++; | |
c8154066 SH |
97 | saved = *p; |
98 | *p = '\0'; | |
99 | if (access(copy, X_OK)) { | |
100 | SYSERROR("could not access %s. Please grant it 'x' " \ | |
101 | "access, or add an ACL for the container root.", | |
102 | copy); | |
103 | return; | |
104 | } | |
105 | *p = saved; | |
106 | } | |
107 | } | |
108 | ||
9f30a190 MM |
109 | static void close_ns(int ns_fd[LXC_NS_MAX]) { |
110 | int i; | |
111 | ||
9f30a190 MM |
112 | for (i = 0; i < LXC_NS_MAX; i++) { |
113 | if (ns_fd[i] > -1) { | |
114 | close(ns_fd[i]); | |
115 | ns_fd[i] = -1; | |
116 | } | |
117 | } | |
9f30a190 MM |
118 | } |
119 | ||
120 | static int preserve_ns(int ns_fd[LXC_NS_MAX], int clone_flags) { | |
121 | int i, saved_errno; | |
122 | char path[MAXPATHLEN]; | |
123 | ||
9f30a190 MM |
124 | for (i = 0; i < LXC_NS_MAX; i++) |
125 | ns_fd[i] = -1; | |
126 | ||
cd43d2d1 SH |
127 | if (access("/proc/self/ns", X_OK)) { |
128 | WARN("Kernel does not support attach; preserve_ns ignored"); | |
129 | return 0; | |
130 | } | |
131 | ||
9f30a190 MM |
132 | for (i = 0; i < LXC_NS_MAX; i++) { |
133 | if ((clone_flags & ns_info[i].clone_flag) == 0) | |
134 | continue; | |
135 | snprintf(path, MAXPATHLEN, "/proc/self/ns/%s", ns_info[i].proc_name); | |
9f30a190 | 136 | ns_fd[i] = open(path, O_RDONLY | O_CLOEXEC); |
9f30a190 MM |
137 | if (ns_fd[i] < 0) |
138 | goto error; | |
139 | } | |
140 | ||
141 | return 0; | |
142 | ||
143 | error: | |
144 | saved_errno = errno; | |
145 | close_ns(ns_fd); | |
146 | errno = saved_errno; | |
147 | SYSERROR("failed to open '%s'", path); | |
148 | return -1; | |
149 | } | |
150 | ||
151 | static int attach_ns(const int ns_fd[LXC_NS_MAX]) { | |
152 | int i; | |
153 | ||
154 | for (i = 0; i < LXC_NS_MAX; i++) { | |
155 | if (ns_fd[i] < 0) | |
156 | continue; | |
157 | ||
158 | if (setns(ns_fd[i], 0) != 0) | |
159 | goto error; | |
160 | } | |
161 | return 0; | |
162 | ||
163 | error: | |
164 | SYSERROR("failed to set namespace '%s'", ns_info[i].proc_name); | |
165 | return -1; | |
166 | } | |
167 | ||
80090207 CLG |
168 | static int match_fd(int fd) |
169 | { | |
170 | return (fd == 0 || fd == 1 || fd == 2); | |
171 | } | |
172 | ||
d2cf4c37 SH |
173 | /* |
174 | * Check for any fds we need to close | |
175 | * * if fd_to_ignore != -1, then if we find that fd open we will ignore it. | |
176 | * * By default we warn about open fds we find. | |
177 | * * If closeall is true, we will close open fds. | |
178 | * * If lxc-start was passed "-C", then conf->close_all_fds will be true, | |
179 | * in which case we also close all open fds. | |
180 | * * A daemonized container will always pass closeall=true. | |
181 | */ | |
182 | int lxc_check_inherited(struct lxc_conf *conf, bool closeall, int fd_to_ignore) | |
80090207 CLG |
183 | { |
184 | struct dirent dirent, *direntp; | |
185 | int fd, fddir; | |
186 | DIR *dir; | |
80090207 | 187 | |
d2cf4c37 SH |
188 | if (conf && conf->close_all_fds) |
189 | closeall = true; | |
190 | ||
b119f362 | 191 | restart: |
80090207 CLG |
192 | dir = opendir("/proc/self/fd"); |
193 | if (!dir) { | |
194 | WARN("failed to open directory: %m"); | |
195 | return -1; | |
196 | } | |
197 | ||
198 | fddir = dirfd(dir); | |
199 | ||
200 | while (!readdir_r(dir, &dirent, &direntp)) { | |
80090207 CLG |
201 | if (!direntp) |
202 | break; | |
203 | ||
204 | if (!strcmp(direntp->d_name, ".")) | |
205 | continue; | |
206 | ||
207 | if (!strcmp(direntp->d_name, "..")) | |
208 | continue; | |
209 | ||
210 | fd = atoi(direntp->d_name); | |
211 | ||
f2faa8fa | 212 | if (fd == fddir || fd == lxc_log_fd || fd == fd_to_ignore) |
80090207 CLG |
213 | continue; |
214 | ||
215 | if (match_fd(fd)) | |
216 | continue; | |
80090207 | 217 | |
d2cf4c37 | 218 | if (closeall) { |
b119f362 SH |
219 | close(fd); |
220 | closedir(dir); | |
221 | INFO("closed inherited fd %d", fd); | |
222 | goto restart; | |
223 | } | |
92c7f629 | 224 | WARN("inherited fd %d", fd); |
80090207 CLG |
225 | } |
226 | ||
92c7f629 GK |
227 | closedir(dir); /* cannot fail */ |
228 | return 0; | |
80090207 CLG |
229 | } |
230 | ||
83ee7875 | 231 | static int setup_signal_fd(sigset_t *oldmask) |
b0a33c1e | 232 | { |
233 | sigset_t mask; | |
234 | int fd; | |
235 | ||
f3304a29 FW |
236 | /* Block everything except serious error signals */ |
237 | if (sigfillset(&mask) || | |
238 | sigdelset(&mask, SIGILL) || | |
239 | sigdelset(&mask, SIGSEGV) || | |
240 | sigdelset(&mask, SIGBUS) || | |
b5159817 | 241 | sigdelset(&mask, SIGWINCH) || |
f3304a29 FW |
242 | sigprocmask(SIG_BLOCK, &mask, oldmask)) { |
243 | SYSERROR("failed to set signal mask"); | |
b0a33c1e | 244 | return -1; |
245 | } | |
246 | ||
247 | fd = signalfd(-1, &mask, 0); | |
248 | if (fd < 0) { | |
36eb9bde | 249 | SYSERROR("failed to create the signal fd"); |
b0a33c1e | 250 | return -1; |
251 | } | |
252 | ||
253 | if (fcntl(fd, F_SETFD, FD_CLOEXEC)) { | |
36eb9bde | 254 | SYSERROR("failed to set sigfd to close-on-exec"); |
b0a33c1e | 255 | close(fd); |
256 | return -1; | |
257 | } | |
258 | ||
1ac470c0 DL |
259 | DEBUG("sigchild handler set"); |
260 | ||
b0a33c1e | 261 | return fd; |
262 | } | |
263 | ||
84c92abd | 264 | static int signal_handler(int fd, uint32_t events, void *data, |
b0a33c1e | 265 | struct lxc_epoll_descr *descr) |
266 | { | |
15cd25fd | 267 | struct signalfd_siginfo siginfo; |
80507ee8 | 268 | siginfo_t info; |
15cd25fd | 269 | int ret; |
82d89dce | 270 | pid_t *pid = data; |
80507ee8 | 271 | bool init_died = false; |
15cd25fd DL |
272 | |
273 | ret = read(fd, &siginfo, sizeof(siginfo)); | |
274 | if (ret < 0) { | |
f3304a29 | 275 | ERROR("failed to read signal info"); |
15cd25fd DL |
276 | return -1; |
277 | } | |
278 | ||
279 | if (ret != sizeof(siginfo)) { | |
280 | ERROR("unexpected siginfo size"); | |
281 | return -1; | |
282 | } | |
283 | ||
80507ee8 SH |
284 | // check whether init is running |
285 | info.si_pid = 0; | |
286 | ret = waitid(P_PID, *pid, &info, WEXITED | WNOWAIT | WNOHANG); | |
287 | if (ret == 0 && info.si_pid == *pid) { | |
288 | init_died = true; | |
289 | } | |
290 | ||
f3304a29 FW |
291 | if (siginfo.ssi_signo != SIGCHLD) { |
292 | kill(*pid, siginfo.ssi_signo); | |
293 | INFO("forwarded signal %d to pid %d", siginfo.ssi_signo, *pid); | |
80507ee8 | 294 | return init_died ? 1 : 0; |
f3304a29 FW |
295 | } |
296 | ||
15cd25fd DL |
297 | if (siginfo.ssi_code == CLD_STOPPED || |
298 | siginfo.ssi_code == CLD_CONTINUED) { | |
299 | INFO("container init process was stopped/continued"); | |
80507ee8 | 300 | return init_died ? 1 : 0; |
15cd25fd | 301 | } |
1ac470c0 | 302 | |
82d89dce DL |
303 | /* more robustness, protect ourself from a SIGCHLD sent |
304 | * by a process different from the container init | |
305 | */ | |
306 | if (siginfo.ssi_pid != *pid) { | |
307 | WARN("invalid pid for SIGCHLD"); | |
80507ee8 | 308 | return init_died ? 1 : 0; |
82d89dce DL |
309 | } |
310 | ||
15cd25fd | 311 | DEBUG("container init process exited"); |
b0a33c1e | 312 | return 1; |
313 | } | |
314 | ||
735f2c6e | 315 | int lxc_set_state(const char *name, struct lxc_handler *handler, lxc_state_t state) |
66aeffc7 DL |
316 | { |
317 | handler->state = state; | |
9123e471 | 318 | lxc_monitor_send_state(name, state, handler->lxcpath); |
66aeffc7 DL |
319 | return 0; |
320 | } | |
321 | ||
735f2c6e | 322 | int lxc_poll(const char *name, struct lxc_handler *handler) |
b0a33c1e | 323 | { |
ca5f7926 DL |
324 | int sigfd = handler->sigfd; |
325 | int pid = handler->pid; | |
b0a33c1e | 326 | struct lxc_epoll_descr descr; |
327 | ||
a9e61274 | 328 | if (lxc_mainloop_open(&descr)) { |
36eb9bde | 329 | ERROR("failed to create mainloop"); |
50c8bf05 | 330 | goto out_sigfd; |
b0a33c1e | 331 | } |
332 | ||
83ee7875 | 333 | if (lxc_mainloop_add_handler(&descr, sigfd, signal_handler, &pid)) { |
36eb9bde | 334 | ERROR("failed to add handler for the signal"); |
b0a33c1e | 335 | goto out_mainloop_open; |
336 | } | |
337 | ||
63376d7d DL |
338 | if (lxc_console_mainloop_add(&descr, handler)) { |
339 | ERROR("failed to add console handler to mainloop"); | |
340 | goto out_mainloop_open; | |
341 | } | |
342 | ||
ef6e34ee | 343 | if (lxc_cmd_mainloop_add(name, &descr, handler)) { |
563f2f2c | 344 | ERROR("failed to add command handler to mainloop"); |
96fa1ff0 | 345 | goto out_mainloop_open; |
563f2f2c DL |
346 | } |
347 | ||
828695d9 | 348 | if (handler->conf->need_utmp_watch) { |
495d2046 | 349 | #if HAVE_SYS_CAPABILITY_H |
828695d9 SH |
350 | if (lxc_utmp_mainloop_add(&descr, handler)) { |
351 | ERROR("failed to add utmp handler to mainloop"); | |
352 | goto out_mainloop_open; | |
353 | } | |
495d2046 | 354 | #else |
959aee9c | 355 | DEBUG("not starting utmp handler as cap_sys_boot cannot be dropped without capabilities support"); |
495d2046 | 356 | #endif |
563f2f2c | 357 | } |
b0a33c1e | 358 | |
e51d4895 | 359 | return lxc_mainloop(&descr, -1); |
b0a33c1e | 360 | |
361 | out_mainloop_open: | |
362 | lxc_mainloop_close(&descr); | |
b0a33c1e | 363 | out_sigfd: |
364 | close(sigfd); | |
c3e13372 | 365 | return -1; |
b0a33c1e | 366 | } |
367 | ||
13f5be62 | 368 | struct lxc_handler *lxc_init(const char *name, struct lxc_conf *conf, const char *lxcpath) |
59eb99ba | 369 | { |
3a0f472d DL |
370 | struct lxc_handler *handler; |
371 | ||
372 | handler = malloc(sizeof(*handler)); | |
373 | if (!handler) | |
374 | return NULL; | |
59eb99ba DL |
375 | |
376 | memset(handler, 0, sizeof(*handler)); | |
377 | ||
e8bd4e43 | 378 | handler->ttysock[0] = handler->ttysock[1] = -1; |
fae349da | 379 | handler->conf = conf; |
9123e471 | 380 | handler->lxcpath = lxcpath; |
5c068da9 | 381 | handler->pinfd = -1; |
fae349da | 382 | |
fe4de9a6 DE |
383 | lsm_init(); |
384 | ||
3bdf52d7 DL |
385 | handler->name = strdup(name); |
386 | if (!handler->name) { | |
387 | ERROR("failed to allocate memory"); | |
388 | goto out_free; | |
389 | } | |
390 | ||
ef6e34ee | 391 | if (lxc_cmd_init(name, handler, lxcpath)) |
d2e30e99 DE |
392 | goto out_free_name; |
393 | ||
8f2c3a70 SH |
394 | if (lxc_read_seccomp_config(conf) != 0) { |
395 | ERROR("failed loading seccomp policy"); | |
d2e30e99 | 396 | goto out_close_maincmd_fd; |
8f2c3a70 SH |
397 | } |
398 | ||
051151de | 399 | /* Begin by setting the state to STARTING */ |
25c2aca5 | 400 | if (lxc_set_state(name, handler, STARTING)) { |
59eb99ba | 401 | ERROR("failed to set state '%s'", lxc_state2str(STARTING)); |
051151de | 402 | goto out_close_maincmd_fd; |
0ad19a3f | 403 | } |
404 | ||
f7bee6c6 MW |
405 | /* Start of environment variable setup for hooks */ |
406 | if (setenv("LXC_NAME", name, 1)) { | |
407 | SYSERROR("failed to set environment variable for container name"); | |
408 | } | |
409 | if (setenv("LXC_CONFIG_FILE", conf->rcfile, 1)) { | |
410 | SYSERROR("failed to set environment variable for config path"); | |
411 | } | |
412 | if (setenv("LXC_ROOTFS_MOUNT", conf->rootfs.mount, 1)) { | |
413 | SYSERROR("failed to set environment variable for rootfs mount"); | |
414 | } | |
415 | if (setenv("LXC_ROOTFS_PATH", conf->rootfs.path, 1)) { | |
416 | SYSERROR("failed to set environment variable for rootfs mount"); | |
417 | } | |
418 | if (conf->console.path && setenv("LXC_CONSOLE", conf->console.path, 1)) { | |
419 | SYSERROR("failed to set environment variable for console path"); | |
420 | } | |
421 | if (conf->console.log_path && setenv("LXC_CONSOLE_LOGPATH", conf->console.log_path, 1)) { | |
422 | SYSERROR("failed to set environment variable for console log"); | |
423 | } | |
424 | /* End of environment variable setup for hooks */ | |
425 | ||
283678ed | 426 | if (run_lxc_hooks(name, "pre-start", conf, handler->lxcpath, NULL)) { |
773fb9ca SH |
427 | ERROR("failed to run pre-start hooks for container '%s'.", name); |
428 | goto out_aborting; | |
429 | } | |
26ddeedd | 430 | |
b0a33c1e | 431 | /* the signal fd has to be created before forking otherwise |
432 | * if the child process exits before we setup the signal fd, | |
433 | * the event will be lost and the command will be stuck */ | |
83ee7875 | 434 | handler->sigfd = setup_signal_fd(&handler->oldmask); |
59eb99ba | 435 | if (handler->sigfd < 0) { |
36eb9bde | 436 | ERROR("failed to set sigchild fd handler"); |
b5159817 DE |
437 | goto out_delete_tty; |
438 | } | |
439 | ||
440 | /* do this after setting up signals since it might unblock SIGWINCH */ | |
441 | if (lxc_console_create(conf)) { | |
442 | ERROR("failed to create console"); | |
443 | goto out_restore_sigmask; | |
b0a33c1e | 444 | } |
445 | ||
c4d10a05 SH |
446 | if (ttys_shift_ids(conf) < 0) { |
447 | ERROR("Failed to shift tty into container"); | |
448 | goto out_restore_sigmask; | |
449 | } | |
450 | ||
c3e13372 | 451 | INFO("'%s' is initialized", name); |
3a0f472d | 452 | return handler; |
59eb99ba | 453 | |
b5159817 DE |
454 | out_restore_sigmask: |
455 | sigprocmask(SIG_SETMASK, &handler->oldmask, NULL); | |
59eb99ba | 456 | out_delete_tty: |
fae349da | 457 | lxc_delete_tty(&conf->tty_info); |
59eb99ba | 458 | out_aborting: |
25c2aca5 | 459 | lxc_set_state(name, handler, ABORTING); |
d2e30e99 DE |
460 | out_close_maincmd_fd: |
461 | close(conf->maincmd_fd); | |
462 | conf->maincmd_fd = -1; | |
3bdf52d7 DL |
463 | out_free_name: |
464 | free(handler->name); | |
465 | handler->name = NULL; | |
3a0f472d DL |
466 | out_free: |
467 | free(handler); | |
c3e13372 | 468 | return NULL; |
59eb99ba DL |
469 | } |
470 | ||
3b72c4a0 | 471 | void lxc_fini(const char *name, struct lxc_handler *handler) |
59eb99ba DL |
472 | { |
473 | /* The STOPPING state is there for future cleanup code | |
474 | * which can take awhile | |
475 | */ | |
25c2aca5 MN |
476 | lxc_set_state(name, handler, STOPPING); |
477 | lxc_set_state(name, handler, STOPPED); | |
59eb99ba | 478 | |
283678ed | 479 | if (run_lxc_hooks(name, "post-stop", handler->conf, handler->lxcpath, NULL)) |
773fb9ca | 480 | ERROR("failed to run post-stop hooks for container '%s'.", name); |
26ddeedd | 481 | |
83ee7875 | 482 | /* reset mask set by setup_signal_fd */ |
8f64a3f6 MN |
483 | if (sigprocmask(SIG_SETMASK, &handler->oldmask, NULL)) |
484 | WARN("failed to restore sigprocmask"); | |
485 | ||
b5159817 | 486 | lxc_console_delete(&handler->conf->console); |
b2431939 | 487 | lxc_delete_tty(&handler->conf->tty_info); |
d2e30e99 DE |
488 | close(handler->conf->maincmd_fd); |
489 | handler->conf->maincmd_fd = -1; | |
3bdf52d7 | 490 | free(handler->name); |
e8bd4e43 SH |
491 | if (handler->ttysock[0] != -1) { |
492 | close(handler->ttysock[0]); | |
493 | close(handler->ttysock[1]); | |
494 | } | |
d4ef7c50 | 495 | cgroup_destroy(handler); |
b2431939 | 496 | free(handler); |
59eb99ba DL |
497 | } |
498 | ||
735f2c6e | 499 | void lxc_abort(const char *name, struct lxc_handler *handler) |
59eb99ba | 500 | { |
73e608b2 SH |
501 | int ret, status; |
502 | ||
25c2aca5 | 503 | lxc_set_state(name, handler, ABORTING); |
7d9fb3e9 DL |
504 | if (handler->pid > 0) |
505 | kill(handler->pid, SIGKILL); | |
73e608b2 | 506 | while ((ret = waitpid(-1, &status, 0)) > 0) ; |
59eb99ba DL |
507 | } |
508 | ||
828695d9 SH |
509 | #include <sys/reboot.h> |
510 | #include <linux/reboot.h> | |
511 | ||
e2fa1520 SH |
512 | /* |
513 | * reboot(LINUX_REBOOT_CMD_CAD_ON) will return -EINVAL | |
514 | * in a child pid namespace if container reboot support exists. | |
515 | * Otherwise, it will either succeed or return -EPERM. | |
516 | */ | |
517 | static int container_reboot_supported(void *arg) | |
828695d9 | 518 | { |
e2fa1520 | 519 | int *cmd = arg; |
828695d9 | 520 | int ret; |
828695d9 | 521 | |
e2fa1520 SH |
522 | ret = reboot(*cmd); |
523 | if (ret == -1 && errno == EINVAL) | |
524 | return 1; | |
525 | return 0; | |
526 | } | |
527 | ||
b60ed720 | 528 | static int must_drop_cap_sys_boot(struct lxc_conf *conf) |
e2fa1520 | 529 | { |
025ed0f3 | 530 | FILE *f; |
b60ed720 | 531 | int ret, cmd, v, flags; |
e2fa1520 | 532 | long stack_size = 4096; |
7f145a6d | 533 | void *stack = alloca(stack_size); |
e2fa1520 SH |
534 | int status; |
535 | pid_t pid; | |
536 | ||
025ed0f3 | 537 | f = fopen("/proc/sys/kernel/ctrl-alt-del", "r"); |
e2fa1520 SH |
538 | if (!f) { |
539 | DEBUG("failed to open /proc/sys/kernel/ctrl-alt-del"); | |
828695d9 | 540 | return 1; |
e2fa1520 | 541 | } |
828695d9 SH |
542 | |
543 | ret = fscanf(f, "%d", &v); | |
544 | fclose(f); | |
e2fa1520 SH |
545 | if (ret != 1) { |
546 | DEBUG("Failed to read /proc/sys/kernel/ctrl-alt-del"); | |
828695d9 | 547 | return 1; |
e2fa1520 SH |
548 | } |
549 | cmd = v ? LINUX_REBOOT_CMD_CAD_ON : LINUX_REBOOT_CMD_CAD_OFF; | |
550 | ||
b60ed720 SH |
551 | flags = CLONE_NEWPID | SIGCHLD; |
552 | if (!lxc_list_empty(&conf->id_map)) | |
553 | flags |= CLONE_NEWUSER; | |
554 | ||
7f145a6d | 555 | #ifdef __ia64__ |
959aee9c | 556 | pid = __clone2(container_reboot_supported, stack, stack_size, flags, &cmd); |
7f145a6d | 557 | #else |
959aee9c SG |
558 | stack += stack_size; |
559 | pid = clone(container_reboot_supported, stack, flags, &cmd); | |
7f145a6d | 560 | #endif |
959aee9c | 561 | if (pid < 0) { |
c08220e9 | 562 | if (flags & CLONE_NEWUSER) |
4a7e5f4f | 563 | ERROR("failed to clone (%#x): %s (includes CLONE_NEWUSER)", flags, strerror(errno)); |
c08220e9 | 564 | else |
4a7e5f4f | 565 | ERROR("failed to clone (%#x): %s", flags, strerror(errno)); |
959aee9c SG |
566 | return -1; |
567 | } | |
568 | if (wait(&status) < 0) { | |
569 | SYSERROR("unexpected wait error: %m"); | |
570 | return -1; | |
571 | } | |
e2fa1520 SH |
572 | |
573 | if (WEXITSTATUS(status) != 1) | |
828695d9 | 574 | return 1; |
e2fa1520 | 575 | |
828695d9 SH |
576 | return 0; |
577 | } | |
578 | ||
658979c5 SH |
579 | /* |
580 | * netpipe is used in the unprivileged case to transfer the ifindexes | |
581 | * from parent to child | |
582 | */ | |
583 | static int netpipe = -1; | |
584 | ||
585 | static inline int count_veths(struct lxc_list *network) | |
586 | { | |
587 | struct lxc_list *iterator; | |
588 | struct lxc_netdev *netdev; | |
589 | int count = 0; | |
590 | ||
591 | lxc_list_for_each(iterator, network) { | |
592 | netdev = iterator->elem; | |
593 | if (netdev->type != LXC_NET_VETH) | |
594 | continue; | |
595 | count++; | |
596 | } | |
597 | return count; | |
598 | } | |
599 | ||
600 | static int read_unpriv_netifindex(struct lxc_list *network) | |
601 | { | |
602 | struct lxc_list *iterator; | |
603 | struct lxc_netdev *netdev; | |
604 | ||
605 | if (netpipe == -1) | |
606 | return 0; | |
607 | lxc_list_for_each(iterator, network) { | |
608 | netdev = iterator->elem; | |
609 | if (netdev->type != LXC_NET_VETH) | |
610 | continue; | |
611 | if (!(netdev->name = malloc(IFNAMSIZ))) { | |
612 | ERROR("Out of memory"); | |
613 | close(netpipe); | |
614 | return -1; | |
615 | } | |
616 | if (read(netpipe, netdev->name, IFNAMSIZ) != IFNAMSIZ) { | |
617 | close(netpipe); | |
618 | return -1; | |
619 | } | |
620 | } | |
621 | close(netpipe); | |
622 | return 0; | |
623 | } | |
624 | ||
ffe1e01a | 625 | static int do_start(void *data) |
50e98013 | 626 | { |
7c661726 | 627 | struct lxc_list *iterator; |
23c53af9 | 628 | struct lxc_handler *handler = data; |
50e98013 DL |
629 | |
630 | if (sigprocmask(SIG_SETMASK, &handler->oldmask, NULL)) { | |
631 | SYSERROR("failed to set sigprocmask"); | |
9d7f9e52 | 632 | return -1; |
50e98013 DL |
633 | } |
634 | ||
743ecd2e DL |
635 | /* This prctl must be before the synchro, so if the parent |
636 | * dies before we set the parent death signal, we will detect | |
637 | * its death with the synchro right after, otherwise we have | |
638 | * a window where the parent can exit before we set the pdeath | |
639 | * signal leading to a unsupervized container. | |
640 | */ | |
641 | if (prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0)) { | |
642 | SYSERROR("failed to set pdeath signal"); | |
643 | return -1; | |
644 | } | |
645 | ||
3c22086f | 646 | lxc_sync_fini_parent(handler); |
50e98013 | 647 | |
2b0e17e4 | 648 | /* don't leak the pinfd to the container */ |
025ed0f3 | 649 | if (handler->pinfd >= 0) { |
0d03360a | 650 | close(handler->pinfd); |
025ed0f3 | 651 | } |
2b0e17e4 | 652 | |
3c22086f CLG |
653 | /* Tell the parent task it can begin to configure the |
654 | * container and wait for it to finish | |
655 | */ | |
656 | if (lxc_sync_barrier_parent(handler, LXC_SYNC_CONFIGURE)) | |
9d7f9e52 | 657 | return -1; |
50e98013 | 658 | |
658979c5 SH |
659 | if (read_unpriv_netifindex(&handler->conf->network) < 0) |
660 | goto out_warn_father; | |
661 | ||
f6d3e3e4 SH |
662 | /* |
663 | * if we are in a new user namespace, become root there to have | |
664 | * privilege over our namespace | |
665 | */ | |
666 | if (!lxc_list_empty(&handler->conf->id_map)) { | |
667 | NOTICE("switching to gid/uid 0 in new user namespace"); | |
668 | if (setgid(0)) { | |
669 | SYSERROR("setgid"); | |
670 | goto out_warn_father; | |
671 | } | |
672 | if (setuid(0)) { | |
673 | SYSERROR("setuid"); | |
674 | goto out_warn_father; | |
675 | } | |
c476bdce SH |
676 | if (setgroups(0, NULL)) { |
677 | SYSERROR("setgroups"); | |
678 | goto out_warn_father; | |
679 | } | |
f6d3e3e4 SH |
680 | } |
681 | ||
99b71824 | 682 | if (access(handler->lxcpath, X_OK)) { |
c8154066 SH |
683 | print_top_failing_dir(handler->lxcpath); |
684 | goto out_warn_father; | |
685 | } | |
686 | ||
495d2046 | 687 | #if HAVE_SYS_CAPABILITY_H |
69182a31 | 688 | if (handler->conf->need_utmp_watch) { |
828695d9 SH |
689 | if (prctl(PR_CAPBSET_DROP, CAP_SYS_BOOT, 0, 0, 0)) { |
690 | SYSERROR("failed to remove CAP_SYS_BOOT capability"); | |
c4ea60df | 691 | goto out_warn_father; |
828695d9 | 692 | } |
959aee9c | 693 | DEBUG("Dropped cap_sys_boot"); |
e2fa1520 | 694 | } |
495d2046 | 695 | #endif |
e2fa1520 SH |
696 | |
697 | /* Setup the container, ip, names, utsname, ... */ | |
d4ef7c50 | 698 | if (lxc_setup(handler)) { |
e2fa1520 SH |
699 | ERROR("failed to setup the container"); |
700 | goto out_warn_father; | |
701 | } | |
50e98013 | 702 | |
544a48a0 SH |
703 | /* ask father to setup cgroups and wait for him to finish */ |
704 | if (lxc_sync_barrier_parent(handler, LXC_SYNC_CGROUP)) | |
705 | return -1; | |
706 | ||
72863294 | 707 | /* Set the label to change to when we exec(2) the container's init */ |
7aff4f43 | 708 | if (lsm_process_label_set(NULL, handler->conf, 1, 1) < 0) |
e075f5d9 | 709 | goto out_warn_father; |
5112cd70 | 710 | |
0d9acb99 DE |
711 | /* Some init's such as busybox will set sane tty settings on stdin, |
712 | * stdout, stderr which it thinks is the console. We already set them | |
713 | * the way we wanted on the real terminal, and we want init to do its | |
714 | * setup on its console ie. the pty allocated in lxc_console_create() | |
715 | * so make sure that that pty is stdin,stdout,stderr. | |
716 | */ | |
717 | if (lxc_console_set_stdfds(handler) < 0) | |
718 | goto out_warn_father; | |
719 | ||
5112cd70 SH |
720 | /* If we mounted a temporary proc, then unmount it now */ |
721 | tmp_proc_unmount(handler->conf); | |
e075f5d9 | 722 | |
8f2c3a70 SH |
723 | if (lxc_seccomp_load(handler->conf) != 0) |
724 | goto out_warn_father; | |
725 | ||
283678ed | 726 | if (run_lxc_hooks(handler->name, "start", handler->conf, handler->lxcpath, NULL)) { |
773fb9ca SH |
727 | ERROR("failed to run start hooks for container '%s'.", handler->name); |
728 | goto out_warn_father; | |
729 | } | |
fc25b815 | 730 | |
f7bee6c6 | 731 | /* The clearenv() and putenv() calls have been moved here |
ec64264d | 732 | * to allow us to use environment variables passed to the various |
f7bee6c6 MW |
733 | * hooks, such as the start hook above. Not all of the |
734 | * variables like CONFIG_PATH or ROOTFS are valid in this | |
735 | * context but others are. */ | |
736 | if (clearenv()) { | |
737 | SYSERROR("failed to clear environment"); | |
738 | /* don't error out though */ | |
739 | } | |
740 | ||
7c661726 MP |
741 | lxc_list_for_each(iterator, &handler->conf->environment) { |
742 | if (putenv((char *)iterator->elem)) { | |
743 | SYSERROR("failed to set environment variable '%s'", (char *)iterator->elem); | |
744 | goto out_warn_father; | |
745 | } | |
746 | } | |
747 | ||
f7bee6c6 | 748 | if (putenv("container=lxc")) { |
7c661726 | 749 | SYSERROR("failed to set environment variable 'container=lxc'"); |
c4ea60df | 750 | goto out_warn_father; |
f7bee6c6 MW |
751 | } |
752 | ||
393903d1 SH |
753 | if (handler->conf->pty_names) { |
754 | if (putenv(handler->conf->pty_names)) { | |
755 | SYSERROR("failed to set environment variable for container ptys"); | |
756 | goto out_warn_father; | |
757 | } | |
758 | } | |
759 | ||
773fb9ca | 760 | close(handler->sigfd); |
26ddeedd | 761 | |
e6126dbe MN |
762 | /* after this call, we are in error because this |
763 | * ops should not return as it execs */ | |
c4ea60df | 764 | handler->ops->start(handler, handler->data); |
50e98013 DL |
765 | |
766 | out_warn_father: | |
544a48a0 SH |
767 | /* we want the parent to know something went wrong, so any |
768 | * value other than what it expects is ok. */ | |
3c22086f | 769 | lxc_sync_wake_parent(handler, LXC_SYNC_POST_CONFIGURE); |
50e98013 DL |
770 | return -1; |
771 | } | |
772 | ||
74a3920a | 773 | static int save_phys_nics(struct lxc_conf *conf) |
7b35f3d6 SH |
774 | { |
775 | struct lxc_list *iterator; | |
776 | ||
777 | lxc_list_for_each(iterator, &conf->network) { | |
778 | struct lxc_netdev *netdev = iterator->elem; | |
779 | ||
780 | if (netdev->type != LXC_NET_PHYS) | |
781 | continue; | |
782 | conf->saved_nics = realloc(conf->saved_nics, | |
783 | (conf->num_savednics+1)*sizeof(struct saved_nic)); | |
784 | if (!conf->saved_nics) { | |
785 | SYSERROR("failed to allocate memory"); | |
786 | return -1; | |
787 | } | |
788 | conf->saved_nics[conf->num_savednics].ifindex = netdev->ifindex; | |
789 | conf->saved_nics[conf->num_savednics].orig_name = strdup(netdev->link); | |
790 | if (!conf->saved_nics[conf->num_savednics].orig_name) { | |
791 | SYSERROR("failed to allocate memory"); | |
792 | return -1; | |
793 | } | |
959aee9c | 794 | INFO("stored saved_nic #%d idx %d name %s", conf->num_savednics, |
7b35f3d6 SH |
795 | conf->saved_nics[conf->num_savednics].ifindex, |
796 | conf->saved_nics[conf->num_savednics].orig_name); | |
797 | conf->num_savednics++; | |
798 | } | |
799 | ||
800 | return 0; | |
801 | } | |
802 | ||
e8bd4e43 SH |
803 | static int recv_fd(int sock, int *fd) |
804 | { | |
805 | if (lxc_abstract_unix_recv_fd(sock, fd, NULL, 0) < 0) { | |
806 | SYSERROR("Error receiving tty fd from child"); | |
807 | return -1; | |
808 | } | |
809 | if (*fd == -1) | |
810 | return -1; | |
811 | return 0; | |
812 | } | |
813 | ||
814 | static int recv_ttys_from_child(struct lxc_handler *handler) | |
815 | { | |
816 | struct lxc_conf *conf = handler->conf; | |
817 | int i, sock = handler->ttysock[1]; | |
818 | struct lxc_tty_info *tty_info = &conf->tty_info; | |
819 | ||
820 | if (!conf->tty) | |
821 | return 0; | |
822 | ||
823 | tty_info->pty_info = malloc(sizeof(*tty_info->pty_info)*conf->tty); | |
824 | if (!tty_info->pty_info) { | |
825 | SYSERROR("failed to allocate pty_info"); | |
826 | return -1; | |
827 | } | |
828 | ||
829 | for (i = 0; i < conf->tty; i++) { | |
830 | struct lxc_pty_info *pty_info = &tty_info->pty_info[i]; | |
831 | pty_info->busy = 0; | |
832 | if (recv_fd(sock, &pty_info->slave) < 0 || | |
833 | recv_fd(sock, &pty_info->master) < 0) { | |
834 | ERROR("Error receiving tty info from child"); | |
835 | return -1; | |
836 | } | |
837 | } | |
838 | tty_info->nbtty = conf->tty; | |
839 | ||
840 | return 0; | |
841 | } | |
842 | ||
f813849c TA |
843 | void resolve_clone_flags(struct lxc_handler *handler) |
844 | { | |
845 | handler->clone_flags = CLONE_NEWPID | CLONE_NEWNS; | |
846 | ||
847 | if (!lxc_list_empty(&handler->conf->id_map)) { | |
848 | INFO("Cloning a new user namespace"); | |
849 | handler->clone_flags |= CLONE_NEWUSER; | |
850 | } | |
851 | ||
852 | if (handler->conf->inherit_ns_fd[LXC_NS_NET] == -1) { | |
853 | if (!lxc_requests_empty_network(handler)) | |
854 | handler->clone_flags |= CLONE_NEWNET; | |
855 | } else { | |
856 | INFO("Inheriting a net namespace"); | |
857 | } | |
858 | ||
859 | if (handler->conf->inherit_ns_fd[LXC_NS_IPC] == -1) { | |
860 | handler->clone_flags |= CLONE_NEWIPC; | |
861 | } else { | |
862 | INFO("Inheriting an IPC namespace"); | |
863 | } | |
864 | ||
865 | if (handler->conf->inherit_ns_fd[LXC_NS_UTS] == -1) { | |
866 | handler->clone_flags |= CLONE_NEWUTS; | |
867 | } else { | |
868 | INFO("Inheriting a UTS namespace"); | |
869 | } | |
870 | } | |
871 | ||
74a3920a | 872 | static int lxc_spawn(struct lxc_handler *handler) |
59eb99ba | 873 | { |
b98f7d6e | 874 | int failed_before_rename = 0; |
ffe1e01a | 875 | const char *name = handler->name; |
7e4dfe0b | 876 | bool cgroups_connected = false; |
9f30a190 MM |
877 | int saved_ns_fd[LXC_NS_MAX]; |
878 | int preserve_mask = 0, i; | |
658979c5 | 879 | int netpipepair[2], nveths; |
9f30a190 | 880 | |
2f2623ec SH |
881 | netpipe = -1; |
882 | ||
9f30a190 | 883 | for (i = 0; i < LXC_NS_MAX; i++) |
6c544cb3 | 884 | if (handler->conf->inherit_ns_fd[i] != -1) |
9f30a190 | 885 | preserve_mask |= ns_info[i].clone_flag; |
50e98013 | 886 | |
3c22086f | 887 | if (lxc_sync_init(handler)) |
9d7f9e52 | 888 | return -1; |
0ad19a3f | 889 | |
e8bd4e43 SH |
890 | if (socketpair(AF_UNIX, SOCK_DGRAM, 0, handler->ttysock) < 0) { |
891 | lxc_sync_fini(handler); | |
892 | return -1; | |
893 | } | |
894 | ||
f813849c | 895 | resolve_clone_flags(handler); |
9f30a190 | 896 | |
f813849c | 897 | if (handler->clone_flags & CLONE_NEWNET) { |
26b797f3 SH |
898 | if (!lxc_list_empty(&handler->conf->network)) { |
899 | ||
9f30a190 MM |
900 | /* Find gateway addresses from the link device, which is |
901 | * no longer accessible inside the container. Do this | |
902 | * before creating network interfaces, since goto | |
903 | * out_delete_net does not work before lxc_clone. */ | |
904 | if (lxc_find_gateway_addresses(handler)) { | |
905 | ERROR("failed to find gateway addresses"); | |
906 | lxc_sync_fini(handler); | |
907 | return -1; | |
908 | } | |
909 | ||
910 | /* that should be done before the clone because we will | |
911 | * fill the netdev index and use them in the child | |
912 | */ | |
913 | if (lxc_create_network(handler)) { | |
38521a3b | 914 | ERROR("failed to create the network"); |
9f30a190 MM |
915 | lxc_sync_fini(handler); |
916 | return -1; | |
917 | } | |
19a26f82 MK |
918 | } |
919 | ||
9f30a190 MM |
920 | if (save_phys_nics(handler->conf)) { |
921 | ERROR("failed to save physical nic info"); | |
922 | goto out_abort; | |
82d5ae15 DL |
923 | } |
924 | } | |
925 | ||
d4ef7c50 SH |
926 | if (!cgroup_init(handler)) { |
927 | ERROR("failed initializing cgroup support"); | |
33ad9f1a CS |
928 | goto out_delete_net; |
929 | } | |
930 | ||
7e4dfe0b SH |
931 | cgroups_connected = true; |
932 | ||
d4ef7c50 SH |
933 | if (!cgroup_create(handler)) { |
934 | ERROR("failed creating cgroups"); | |
47d8fb3b CS |
935 | goto out_delete_net; |
936 | } | |
937 | ||
0c547523 SH |
938 | /* |
939 | * if the rootfs is not a blockdev, prevent the container from | |
940 | * marking it readonly. | |
5e32a990 ÇO |
941 | * |
942 | * if the container is unprivileged then skip rootfs pinning | |
0c547523 | 943 | */ |
5e32a990 ÇO |
944 | if (lxc_list_empty(&handler->conf->id_map)) { |
945 | handler->pinfd = pin_rootfs(handler->conf->rootfs.path); | |
946 | if (handler->pinfd == -1) | |
947 | INFO("failed to pin the container's rootfs"); | |
948 | } | |
0c547523 | 949 | |
cd43d2d1 SH |
950 | if (preserve_ns(saved_ns_fd, preserve_mask) < 0) |
951 | goto out_delete_net; | |
952 | if (attach_ns(handler->conf->inherit_ns_fd) < 0) | |
953 | goto out_delete_net; | |
9f30a190 | 954 | |
658979c5 SH |
955 | if (am_unpriv() && (nveths = count_veths(&handler->conf->network))) { |
956 | if (pipe(netpipepair) < 0) { | |
957 | SYSERROR("Error creating pipe"); | |
958 | goto out_delete_net; | |
959 | } | |
960 | /* store netpipe in the global var for do_start's use */ | |
961 | netpipe = netpipepair[0]; | |
962 | } | |
963 | ||
0ad19a3f | 964 | /* Create a process in a new set of namespaces */ |
d5088cf2 | 965 | handler->pid = lxc_clone(do_start, handler, handler->clone_flags); |
59eb99ba | 966 | if (handler->pid < 0) { |
36eb9bde | 967 | SYSERROR("failed to fork into a new namespace"); |
7fef7a06 | 968 | goto out_delete_net; |
0ad19a3f | 969 | } |
970 | ||
cd43d2d1 SH |
971 | if (attach_ns(saved_ns_fd)) |
972 | WARN("failed to restore saved namespaces"); | |
9f30a190 | 973 | |
3c22086f CLG |
974 | lxc_sync_fini_child(handler); |
975 | ||
976 | if (lxc_sync_wait_child(handler, LXC_SYNC_CONFIGURE)) | |
99a6af52 | 977 | failed_before_rename = 1; |
0ad19a3f | 978 | |
d4ef7c50 SH |
979 | if (!cgroup_create_legacy(handler)) { |
980 | ERROR("failed to setup the legacy cgroups for %s", name); | |
ae5c8b8e | 981 | goto out_delete_net; |
33ad9f1a | 982 | } |
9daf6f5d SH |
983 | if (!cgroup_setup_limits(handler, false)) { |
984 | ERROR("failed to setup the cgroup limits for '%s'", name); | |
6031a6e5 DE |
985 | goto out_delete_net; |
986 | } | |
987 | ||
d4ef7c50 | 988 | if (!cgroup_enter(handler)) |
7fef7a06 | 989 | goto out_delete_net; |
218d4250 | 990 | |
0996e18a SH |
991 | if (!cgroup_chown(handler)) |
992 | goto out_delete_net; | |
993 | ||
99a6af52 MN |
994 | if (failed_before_rename) |
995 | goto out_delete_net; | |
996 | ||
0ad19a3f | 997 | /* Create the network configuration */ |
d5088cf2 | 998 | if (handler->clone_flags & CLONE_NEWNET) { |
fae349da | 999 | if (lxc_assign_network(&handler->conf->network, handler->pid)) { |
82d5ae15 | 1000 | ERROR("failed to create the configured network"); |
7fef7a06 | 1001 | goto out_delete_net; |
82d5ae15 | 1002 | } |
0ad19a3f | 1003 | } |
1004 | ||
658979c5 SH |
1005 | if (netpipe != -1) { |
1006 | struct lxc_list *iterator; | |
1007 | struct lxc_netdev *netdev; | |
1008 | ||
1009 | close(netpipe); | |
1010 | lxc_list_for_each(iterator, &handler->conf->network) { | |
1011 | netdev = iterator->elem; | |
1012 | if (netdev->type != LXC_NET_VETH) | |
1013 | continue; | |
1014 | if (write(netpipepair[1], netdev->name, IFNAMSIZ) != IFNAMSIZ) { | |
1015 | ERROR("Error writing veth name to container"); | |
1016 | goto out_delete_net; | |
1017 | } | |
1018 | } | |
1019 | close(netpipepair[1]); | |
1020 | } | |
1021 | ||
f6d3e3e4 SH |
1022 | /* map the container uids - the container became an invalid |
1023 | * userid the moment it was cloned with CLONE_NEWUSER - this | |
1024 | * call doesn't change anything immediately, but allows the | |
1025 | * container to setuid(0) (0 being mapped to something else on | |
1026 | * the host) later to become a valid uid again */ | |
1027 | if (lxc_map_ids(&handler->conf->id_map, handler->pid)) { | |
1028 | ERROR("failed to set up id mapping"); | |
1029 | goto out_delete_net; | |
1030 | } | |
1031 | ||
544a48a0 SH |
1032 | /* Tell the child to continue its initialization. we'll get |
1033 | * LXC_SYNC_CGROUP when it is ready for us to setup cgroups | |
3c22086f CLG |
1034 | */ |
1035 | if (lxc_sync_barrier_child(handler, LXC_SYNC_POST_CONFIGURE)) | |
544a48a0 SH |
1036 | goto out_delete_net; |
1037 | ||
9daf6f5d | 1038 | if (!cgroup_setup_limits(handler, true)) { |
b98f7d6e SH |
1039 | ERROR("failed to setup the devices cgroup for '%s'", name); |
1040 | goto out_delete_net; | |
544a48a0 SH |
1041 | } |
1042 | ||
73d28d42 | 1043 | cgroup_disconnect(); |
7e4dfe0b | 1044 | cgroups_connected = false; |
73d28d42 | 1045 | |
e8bd4e43 SH |
1046 | /* read tty fds allocated by child */ |
1047 | if (recv_ttys_from_child(handler) < 0) { | |
1048 | ERROR("failed to receive tty info from child"); | |
1049 | goto out_delete_net; | |
1050 | } | |
1051 | ||
544a48a0 SH |
1052 | /* Tell the child to complete its initialization and wait for |
1053 | * it to exec or return an error. (the child will never | |
1054 | * return LXC_SYNC_POST_CGROUP+1. It will either close the | |
1055 | * sync pipe, causing lxc_sync_barrier_child to return | |
1056 | * success, or return a different value, causing us to error | |
1057 | * out). | |
1058 | */ | |
1059 | if (lxc_sync_barrier_child(handler, LXC_SYNC_POST_CGROUP)) | |
3c22086f | 1060 | return -1; |
0ad19a3f | 1061 | |
cc28d0b0 SH |
1062 | if (detect_shared_rootfs()) |
1063 | umount2(handler->conf->rootfs.mount, MNT_DETACH); | |
1064 | ||
23c53af9 | 1065 | if (handler->ops->post_start(handler, handler->data)) |
e6126dbe MN |
1066 | goto out_abort; |
1067 | ||
25c2aca5 | 1068 | if (lxc_set_state(name, handler, RUNNING)) { |
59eb99ba DL |
1069 | ERROR("failed to set state to %s", |
1070 | lxc_state2str(RUNNING)); | |
1071 | goto out_abort; | |
3f21c114 | 1072 | } |
22ebac19 | 1073 | |
3c22086f | 1074 | lxc_sync_fini(handler); |
0c547523 | 1075 | |
e6126dbe | 1076 | return 0; |
1ac470c0 | 1077 | |
7fef7a06 | 1078 | out_delete_net: |
7e4dfe0b SH |
1079 | if (cgroups_connected) |
1080 | cgroup_disconnect(); | |
d5088cf2 | 1081 | if (handler->clone_flags & CLONE_NEWNET) |
74a2b586 | 1082 | lxc_delete_network(handler); |
59eb99ba DL |
1083 | out_abort: |
1084 | lxc_abort(name, handler); | |
3c22086f | 1085 | lxc_sync_fini(handler); |
5c068da9 SH |
1086 | if (handler->pinfd >= 0) { |
1087 | close(handler->pinfd); | |
1088 | handler->pinfd = -1; | |
1089 | } | |
1090 | ||
b79fcd86 | 1091 | return -1; |
59eb99ba | 1092 | } |
0ad19a3f | 1093 | |
2af6bd1b SH |
1094 | int get_netns_fd(int pid) |
1095 | { | |
1096 | char path[MAXPATHLEN]; | |
1097 | int ret, fd; | |
1098 | ||
1099 | ret = snprintf(path, MAXPATHLEN, "/proc/%d/ns/net", pid); | |
1100 | if (ret < 0 || ret >= MAXPATHLEN) { | |
1101 | WARN("Failed to pin netns file for pid %d", pid); | |
1102 | return -1; | |
1103 | } | |
1104 | ||
1105 | fd = open(path, O_RDONLY); | |
1106 | if (fd < 0) { | |
1107 | WARN("Failed to pin netns file %s for pid %d: %s", | |
1108 | path, pid, strerror(errno)); | |
1109 | return -1; | |
1110 | } | |
1111 | return fd; | |
1112 | } | |
1113 | ||
ee70bf78 | 1114 | int __lxc_start(const char *name, struct lxc_conf *conf, |
13f5be62 | 1115 | struct lxc_operations* ops, void *data, const char *lxcpath) |
59eb99ba | 1116 | { |
3a0f472d | 1117 | struct lxc_handler *handler; |
e043236e | 1118 | int err = -1; |
59eb99ba | 1119 | int status; |
2af6bd1b | 1120 | int netnsfd = -1; |
80090207 | 1121 | |
13f5be62 | 1122 | handler = lxc_init(name, conf, lxcpath); |
3a0f472d | 1123 | if (!handler) { |
59eb99ba | 1124 | ERROR("failed to initialize the container"); |
66aeffc7 | 1125 | return -1; |
0ad19a3f | 1126 | } |
ee70bf78 CLG |
1127 | handler->ops = ops; |
1128 | handler->data = data; | |
e6126dbe | 1129 | |
b60ed720 | 1130 | if (must_drop_cap_sys_boot(handler->conf)) { |
495d2046 | 1131 | #if HAVE_SYS_CAPABILITY_H |
959aee9c | 1132 | DEBUG("Dropping cap_sys_boot"); |
495d2046 | 1133 | #else |
959aee9c | 1134 | DEBUG("Can't drop cap_sys_boot as capabilities aren't supported"); |
495d2046 | 1135 | #endif |
69182a31 | 1136 | } else { |
959aee9c | 1137 | DEBUG("Not dropping cap_sys_boot or watching utmp"); |
69182a31 SH |
1138 | handler->conf->need_utmp_watch = 0; |
1139 | } | |
1140 | ||
76a26f55 SH |
1141 | if (!attach_block_device(handler->conf)) { |
1142 | ERROR("Failure attaching block device"); | |
1143 | goto out_fini_nonet; | |
1144 | } | |
1145 | ||
35120d9c SH |
1146 | if (geteuid() == 0 && !lxc_list_empty(&conf->id_map)) { |
1147 | /* if the backing store is a device, mount it here and now */ | |
1148 | if (rootfs_is_blockdev(conf)) { | |
1149 | if (unshare(CLONE_NEWNS) < 0) { | |
1150 | ERROR("Error unsharing mounts"); | |
1151 | goto out_fini_nonet; | |
1152 | } | |
6a0c909a | 1153 | remount_all_slave(); |
35120d9c SH |
1154 | if (do_rootfs_setup(conf, name, lxcpath) < 0) { |
1155 | ERROR("Error setting up rootfs mount as root before spawn"); | |
1156 | goto out_fini_nonet; | |
1157 | } | |
1158 | INFO("Set up container rootfs as host root"); | |
1159 | } | |
1160 | } | |
1161 | ||
23c53af9 | 1162 | err = lxc_spawn(handler); |
59eb99ba | 1163 | if (err) { |
ee70bf78 | 1164 | ERROR("failed to spawn '%s'", name); |
76a26f55 | 1165 | goto out_detach_blockdev; |
0ad19a3f | 1166 | } |
1167 | ||
2af6bd1b SH |
1168 | netnsfd = get_netns_fd(handler->pid); |
1169 | ||
3a0f472d | 1170 | err = lxc_poll(name, handler); |
e043236e | 1171 | if (err) { |
59eb99ba | 1172 | ERROR("mainloop exited with an error"); |
4b2f98f5 SH |
1173 | if (netnsfd >= 0) |
1174 | close(netnsfd); | |
59eb99ba DL |
1175 | goto out_abort; |
1176 | } | |
0ad19a3f | 1177 | |
3a0f472d | 1178 | while (waitpid(handler->pid, &status, 0) < 0 && errno == EINTR) |
1bc5cc8c | 1179 | continue; |
e043236e | 1180 | |
8b004f07 SH |
1181 | /* |
1182 | * If the child process exited but was not signaled, | |
1183 | * it didn't call reboot. This should mean it was an | |
1184 | * lxc-execute which simply exited. In any case, treat | |
1185 | * it as a 'halt' | |
1186 | */ | |
1187 | if (WIFSIGNALED(status)) { | |
1188 | switch(WTERMSIG(status)) { | |
1189 | case SIGINT: /* halt */ | |
1190 | DEBUG("Container halting"); | |
1191 | break; | |
1192 | case SIGHUP: /* reboot */ | |
1193 | DEBUG("Container rebooting"); | |
1194 | handler->conf->reboot = 1; | |
1195 | break; | |
c2b9bd9e SH |
1196 | case SIGSYS: /* seccomp */ |
1197 | DEBUG("Container violated its seccomp policy"); | |
1198 | break; | |
8b004f07 | 1199 | default: |
959aee9c | 1200 | DEBUG("unknown exit status for init: %d", WTERMSIG(status)); |
8b004f07 SH |
1201 | break; |
1202 | } | |
828695d9 SH |
1203 | } |
1204 | ||
2af6bd1b | 1205 | lxc_rename_phys_nics_on_shutdown(netnsfd, handler->conf); |
4b2f98f5 SH |
1206 | if (netnsfd >= 0) |
1207 | close(netnsfd); | |
7b35f3d6 | 1208 | |
5c068da9 SH |
1209 | if (handler->pinfd >= 0) { |
1210 | close(handler->pinfd); | |
1211 | handler->pinfd = -1; | |
1212 | } | |
1213 | ||
1787abca | 1214 | lxc_monitor_send_exit_code(name, status, handler->lxcpath); |
3a0f472d | 1215 | err = lxc_error_set_and_log(handler->pid, status); |
9d7f9e52 | 1216 | out_fini: |
74a2b586 JK |
1217 | lxc_delete_network(handler); |
1218 | ||
76a26f55 SH |
1219 | out_detach_blockdev: |
1220 | detach_block_device(handler->conf); | |
1221 | ||
74a2b586 | 1222 | out_fini_nonet: |
3a0f472d | 1223 | lxc_fini(name, handler); |
0ad19a3f | 1224 | return err; |
1225 | ||
59eb99ba | 1226 | out_abort: |
3a0f472d | 1227 | lxc_abort(name, handler); |
9d7f9e52 | 1228 | goto out_fini; |
0ad19a3f | 1229 | } |
ee70bf78 CLG |
1230 | |
1231 | struct start_args { | |
1232 | char *const *argv; | |
1233 | }; | |
1234 | ||
1235 | static int start(struct lxc_handler *handler, void* data) | |
1236 | { | |
1237 | struct start_args *arg = data; | |
1238 | ||
1239 | NOTICE("exec'ing '%s'", arg->argv[0]); | |
1240 | ||
1241 | execvp(arg->argv[0], arg->argv); | |
1242 | SYSERROR("failed to exec %s", arg->argv[0]); | |
1243 | return 0; | |
1244 | } | |
1245 | ||
1246 | static int post_start(struct lxc_handler *handler, void* data) | |
1247 | { | |
1248 | struct start_args *arg = data; | |
1249 | ||
1250 | NOTICE("'%s' started with pid '%d'", arg->argv[0], handler->pid); | |
1251 | return 0; | |
1252 | } | |
1253 | ||
1254 | static struct lxc_operations start_ops = { | |
1255 | .start = start, | |
1256 | .post_start = post_start | |
1257 | }; | |
1258 | ||
13f5be62 SH |
1259 | int lxc_start(const char *name, char *const argv[], struct lxc_conf *conf, |
1260 | const char *lxcpath) | |
ee70bf78 CLG |
1261 | { |
1262 | struct start_args start_arg = { | |
1263 | .argv = argv, | |
1264 | }; | |
1265 | ||
828695d9 | 1266 | conf->need_utmp_watch = 1; |
13f5be62 | 1267 | return __lxc_start(name, conf, &start_ops, &start_arg, lxcpath); |
ee70bf78 | 1268 | } |