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