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