]>
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 | ||
f3787121 | 24 | #define _GNU_SOURCE |
f549edcc GK |
25 | #include "config.h" |
26 | ||
f3787121 | 27 | #include <alloca.h> |
0ad19a3f | 28 | #include <dirent.h> |
29 | #include <errno.h> | |
b0a33c1e | 30 | #include <fcntl.h> |
c476bdce | 31 | #include <grp.h> |
37515ebd | 32 | #include <poll.h> |
f3787121 CB |
33 | #include <signal.h> |
34 | #include <stdio.h> | |
35 | #include <stdlib.h> | |
36 | #include <string.h> | |
37 | #include <unistd.h> | |
0ad19a3f | 38 | #include <sys/file.h> |
f4d507d5 | 39 | #include <sys/mount.h> |
f3787121 | 40 | #include <sys/param.h> |
0ad19a3f | 41 | #include <sys/prctl.h> |
f3787121 CB |
42 | #include <sys/socket.h> |
43 | #include <sys/stat.h> | |
44 | #include <sys/syscall.h> | |
ddceb1f9 | 45 | #include <sys/types.h> |
b0a33c1e | 46 | #include <sys/un.h> |
f3787121 | 47 | #include <sys/wait.h> |
ff218c25 | 48 | |
e37dda71 | 49 | #if HAVE_LIBCAP |
495d2046 SG |
50 | #include <sys/capability.h> |
51 | #endif | |
52 | ||
955e2a02 | 53 | #ifndef HAVE_DECL_PR_CAPBSET_DROP |
656994bb MH |
54 | #define PR_CAPBSET_DROP 24 |
55 | #endif | |
56 | ||
955e2a02 CB |
57 | #ifndef HAVE_DECL_PR_SET_NO_NEW_PRIVS |
58 | #define PR_SET_NO_NEW_PRIVS 38 | |
59 | #endif | |
60 | ||
61 | #ifndef HAVE_DECL_PR_GET_NO_NEW_PRIVS | |
62 | #define PR_GET_NO_NEW_PRIVS 39 | |
63 | #endif | |
64 | ||
f3787121 | 65 | #include "af_unix.h" |
d8e48992 | 66 | #include "bdev.h" |
f3787121 | 67 | #include "caps.h" |
563f2f2c | 68 | #include "cgroup.h" |
f3787121 CB |
69 | #include "commands.h" |
70 | #include "conf.h" | |
71 | #include "console.h" | |
e2bcd7db | 72 | #include "error.h" |
f3787121 CB |
73 | #include "log.h" |
74 | #include "lxclock.h" | |
75 | #include "lxcseccomp.h" | |
565c2d76 | 76 | #include "lxcutmp.h" |
f3787121 | 77 | #include "mainloop.h" |
63376d7d | 78 | #include "monitor.h" |
f549edcc | 79 | #include "namespace.h" |
f3787121 CB |
80 | #include "start.h" |
81 | #include "sync.h" | |
82 | #include "utils.h" | |
fe4de9a6 | 83 | #include "lsm/lsm.h" |
36eb9bde CLG |
84 | |
85 | lxc_log_define(lxc_start, lxc); | |
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); | |
f3787121 | 96 | char *copy = alloca(len + 1), *p, *e, saved; |
c8154066 SH |
97 | strcpy(copy, path); |
98 | ||
99 | p = copy; | |
100 | e = copy + len; | |
101 | while (p < e) { | |
f3787121 CB |
102 | while (p < e && *p == '/') |
103 | p++; | |
104 | while (p < e && *p != '/') | |
105 | p++; | |
c8154066 SH |
106 | saved = *p; |
107 | *p = '\0'; | |
108 | if (access(copy, X_OK)) { | |
408da065 CB |
109 | SYSERROR("Could not access %s. Please grant it x " |
110 | "access, or add an ACL for the container " | |
111 | "root.", copy); | |
c8154066 SH |
112 | return; |
113 | } | |
114 | *p = saved; | |
115 | } | |
116 | } | |
117 | ||
408da065 CB |
118 | static void close_ns(int ns_fd[LXC_NS_MAX]) |
119 | { | |
9f30a190 MM |
120 | int i; |
121 | ||
9f30a190 MM |
122 | for (i = 0; i < LXC_NS_MAX; i++) { |
123 | if (ns_fd[i] > -1) { | |
124 | close(ns_fd[i]); | |
125 | ns_fd[i] = -1; | |
126 | } | |
127 | } | |
9f30a190 MM |
128 | } |
129 | ||
4d8ac866 | 130 | /* preserve_ns: open /proc/@pid/ns/@ns for each namespace specified |
62d05d9b | 131 | * in clone_flags. |
4d8ac866 | 132 | * Return true on success, false on failure. |
62d05d9b | 133 | */ |
4d8ac866 | 134 | static bool preserve_ns(int ns_fd[LXC_NS_MAX], int clone_flags, pid_t pid) |
f3787121 | 135 | { |
62d05d9b | 136 | int i, ret; |
9f30a190 | 137 | |
9f30a190 MM |
138 | for (i = 0; i < LXC_NS_MAX; i++) |
139 | ns_fd[i] = -1; | |
140 | ||
4d8ac866 CB |
141 | ret = lxc_preserve_ns(pid, ""); |
142 | if (ret < 0) { | |
143 | SYSERROR("Kernel does not support attaching to namespaces."); | |
62d05d9b | 144 | return false; |
4d8ac866 CB |
145 | } else { |
146 | close(ret); | |
cd43d2d1 SH |
147 | } |
148 | ||
9f30a190 MM |
149 | for (i = 0; i < LXC_NS_MAX; i++) { |
150 | if ((clone_flags & ns_info[i].clone_flag) == 0) | |
151 | continue; | |
4d8ac866 | 152 | ns_fd[i] = lxc_preserve_ns(pid, ns_info[i].proc_name); |
9f30a190 MM |
153 | if (ns_fd[i] < 0) |
154 | goto error; | |
155 | } | |
156 | ||
62d05d9b | 157 | return true; |
9f30a190 MM |
158 | |
159 | error: | |
4d8ac866 CB |
160 | if (errno == ENOENT) |
161 | SYSERROR("Kernel does not support attaching to %s namespaces.", ns_info[i].proc_name); | |
162 | else | |
163 | SYSERROR("Failed to open file descriptor for %s namespace: %s.", ns_info[i].proc_name, strerror(errno)); | |
9f30a190 | 164 | close_ns(ns_fd); |
62d05d9b | 165 | return false; |
9f30a190 MM |
166 | } |
167 | ||
168 | static int attach_ns(const int ns_fd[LXC_NS_MAX]) { | |
169 | int i; | |
170 | ||
171 | for (i = 0; i < LXC_NS_MAX; i++) { | |
172 | if (ns_fd[i] < 0) | |
173 | continue; | |
174 | ||
175 | if (setns(ns_fd[i], 0) != 0) | |
176 | goto error; | |
177 | } | |
178 | return 0; | |
179 | ||
180 | error: | |
408da065 | 181 | SYSERROR("Failed to attach %s namespace.", ns_info[i].proc_name); |
9f30a190 MM |
182 | return -1; |
183 | } | |
184 | ||
80090207 CLG |
185 | static int match_fd(int fd) |
186 | { | |
187 | return (fd == 0 || fd == 1 || fd == 2); | |
188 | } | |
189 | ||
408da065 CB |
190 | /* Check for any fds we need to close. |
191 | * - If fd_to_ignore != -1, then if we find that fd open we will ignore it. | |
192 | * - By default we warn about open fds we find. | |
193 | * - If closeall is true, we will close open fds. | |
194 | * - If lxc-start was passed "-C", then conf->close_all_fds will be true, in | |
195 | * which case we also close all open fds. | |
196 | * - A daemonized container will always pass closeall=true. | |
d2cf4c37 SH |
197 | */ |
198 | int lxc_check_inherited(struct lxc_conf *conf, bool closeall, int fd_to_ignore) | |
80090207 | 199 | { |
74f96976 | 200 | struct dirent *direntp; |
80090207 CLG |
201 | int fd, fddir; |
202 | DIR *dir; | |
80090207 | 203 | |
d2cf4c37 SH |
204 | if (conf && conf->close_all_fds) |
205 | closeall = true; | |
206 | ||
b119f362 | 207 | restart: |
80090207 CLG |
208 | dir = opendir("/proc/self/fd"); |
209 | if (!dir) { | |
408da065 | 210 | WARN("Failed to open directory: %m."); |
80090207 CLG |
211 | return -1; |
212 | } | |
213 | ||
214 | fddir = dirfd(dir); | |
215 | ||
74f96976 | 216 | while ((direntp = readdir(dir))) { |
80090207 CLG |
217 | if (!direntp) |
218 | break; | |
219 | ||
220 | if (!strcmp(direntp->d_name, ".")) | |
221 | continue; | |
222 | ||
223 | if (!strcmp(direntp->d_name, "..")) | |
224 | continue; | |
225 | ||
d4cff0d2 CB |
226 | if (lxc_safe_int(direntp->d_name, &fd) < 0) { |
227 | INFO("Could not parse file descriptor for: %s", direntp->d_name); | |
228 | continue; | |
229 | } | |
80090207 | 230 | |
f2faa8fa | 231 | if (fd == fddir || fd == lxc_log_fd || fd == fd_to_ignore) |
80090207 CLG |
232 | continue; |
233 | ||
858377e4 SH |
234 | if (current_config && fd == current_config->logfd) |
235 | continue; | |
236 | ||
80090207 CLG |
237 | if (match_fd(fd)) |
238 | continue; | |
80090207 | 239 | |
d2cf4c37 | 240 | if (closeall) { |
b119f362 SH |
241 | close(fd); |
242 | closedir(dir); | |
408da065 | 243 | INFO("Closed inherited fd: %d.", fd); |
b119f362 SH |
244 | goto restart; |
245 | } | |
408da065 | 246 | WARN("Inherited fd: %d.", fd); |
80090207 CLG |
247 | } |
248 | ||
408da065 | 249 | /* Only enable syslog at this point to avoid the above logging function |
64c57ea1 BD |
250 | * to open a new fd and make the check_inherited function enter an |
251 | * infinite loop. | |
252 | */ | |
253 | lxc_log_enable_syslog(); | |
254 | ||
92c7f629 GK |
255 | closedir(dir); /* cannot fail */ |
256 | return 0; | |
80090207 CLG |
257 | } |
258 | ||
83ee7875 | 259 | static int setup_signal_fd(sigset_t *oldmask) |
b0a33c1e | 260 | { |
261 | sigset_t mask; | |
262 | int fd; | |
263 | ||
408da065 | 264 | /* Block everything except serious error signals. */ |
f3304a29 FW |
265 | if (sigfillset(&mask) || |
266 | sigdelset(&mask, SIGILL) || | |
267 | sigdelset(&mask, SIGSEGV) || | |
268 | sigdelset(&mask, SIGBUS) || | |
b5159817 | 269 | sigdelset(&mask, SIGWINCH) || |
f3304a29 | 270 | sigprocmask(SIG_BLOCK, &mask, oldmask)) { |
408da065 | 271 | SYSERROR("Failed to set signal mask."); |
b0a33c1e | 272 | return -1; |
273 | } | |
274 | ||
275 | fd = signalfd(-1, &mask, 0); | |
276 | if (fd < 0) { | |
408da065 | 277 | SYSERROR("Failed to create signal file descriptor."); |
b0a33c1e | 278 | return -1; |
279 | } | |
280 | ||
281 | if (fcntl(fd, F_SETFD, FD_CLOEXEC)) { | |
408da065 | 282 | SYSERROR("Failed to set FD_CLOEXEC on the signal file descriptor: %d.", fd); |
b0a33c1e | 283 | close(fd); |
284 | return -1; | |
285 | } | |
286 | ||
408da065 | 287 | DEBUG("Set SIGCHLD handler with file descriptor: %d.", fd); |
1ac470c0 | 288 | |
b0a33c1e | 289 | return fd; |
290 | } | |
291 | ||
84c92abd | 292 | static int signal_handler(int fd, uint32_t events, void *data, |
f3787121 | 293 | struct lxc_epoll_descr *descr) |
b0a33c1e | 294 | { |
15cd25fd | 295 | struct signalfd_siginfo siginfo; |
80507ee8 | 296 | siginfo_t info; |
15cd25fd | 297 | int ret; |
82d89dce | 298 | pid_t *pid = data; |
80507ee8 | 299 | bool init_died = false; |
15cd25fd DL |
300 | |
301 | ret = read(fd, &siginfo, sizeof(siginfo)); | |
302 | if (ret < 0) { | |
408da065 | 303 | ERROR("Failed to read signal info from signal file descriptor: %d.", fd); |
15cd25fd DL |
304 | return -1; |
305 | } | |
306 | ||
307 | if (ret != sizeof(siginfo)) { | |
408da065 | 308 | ERROR("Unexpected size for siginfo struct."); |
15cd25fd DL |
309 | return -1; |
310 | } | |
311 | ||
408da065 | 312 | /* Check whether init is running. */ |
80507ee8 SH |
313 | info.si_pid = 0; |
314 | ret = waitid(P_PID, *pid, &info, WEXITED | WNOWAIT | WNOHANG); | |
408da065 | 315 | if (ret == 0 && info.si_pid == *pid) |
80507ee8 | 316 | init_died = true; |
80507ee8 | 317 | |
f3304a29 FW |
318 | if (siginfo.ssi_signo != SIGCHLD) { |
319 | kill(*pid, siginfo.ssi_signo); | |
408da065 | 320 | INFO("Forwarded signal %d to pid %d.", siginfo.ssi_signo, *pid); |
80507ee8 | 321 | return init_died ? 1 : 0; |
f3304a29 FW |
322 | } |
323 | ||
408da065 CB |
324 | if (siginfo.ssi_code == CLD_STOPPED) { |
325 | INFO("Container init process was stopped."); | |
326 | return init_died ? 1 : 0; | |
327 | } else if (siginfo.ssi_code == CLD_CONTINUED) { | |
328 | INFO("Container init process was continued."); | |
80507ee8 | 329 | return init_died ? 1 : 0; |
15cd25fd | 330 | } |
1ac470c0 | 331 | |
408da065 CB |
332 | /* More robustness, protect ourself from a SIGCHLD sent |
333 | * by a process different from the container init. | |
82d89dce DL |
334 | */ |
335 | if (siginfo.ssi_pid != *pid) { | |
af4c0f05 | 336 | NOTICE("Received SIGCHLD from pid %d instead of container init %d.", siginfo.ssi_pid, *pid); |
80507ee8 | 337 | return init_died ? 1 : 0; |
82d89dce DL |
338 | } |
339 | ||
408da065 | 340 | DEBUG("Container init process %d exited.", *pid); |
b0a33c1e | 341 | return 1; |
342 | } | |
343 | ||
735f2c6e | 344 | int lxc_set_state(const char *name, struct lxc_handler *handler, lxc_state_t state) |
66aeffc7 DL |
345 | { |
346 | handler->state = state; | |
9123e471 | 347 | lxc_monitor_send_state(name, state, handler->lxcpath); |
66aeffc7 DL |
348 | return 0; |
349 | } | |
350 | ||
735f2c6e | 351 | int lxc_poll(const char *name, struct lxc_handler *handler) |
b0a33c1e | 352 | { |
ca5f7926 DL |
353 | int sigfd = handler->sigfd; |
354 | int pid = handler->pid; | |
b0a33c1e | 355 | struct lxc_epoll_descr descr; |
356 | ||
a9e61274 | 357 | if (lxc_mainloop_open(&descr)) { |
408da065 | 358 | ERROR("Failed to create LXC mainloop."); |
50c8bf05 | 359 | goto out_sigfd; |
b0a33c1e | 360 | } |
361 | ||
83ee7875 | 362 | if (lxc_mainloop_add_handler(&descr, sigfd, signal_handler, &pid)) { |
408da065 | 363 | ERROR("Failed to add signal handler with file descriptor %d to LXC mainloop.", sigfd); |
b0a33c1e | 364 | goto out_mainloop_open; |
365 | } | |
366 | ||
da41561c | 367 | if (lxc_console_mainloop_add(&descr, handler->conf)) { |
408da065 | 368 | ERROR("Failed to add console handler to LXC mainloop."); |
63376d7d DL |
369 | goto out_mainloop_open; |
370 | } | |
371 | ||
ef6e34ee | 372 | if (lxc_cmd_mainloop_add(name, &descr, handler)) { |
408da065 | 373 | ERROR("Failed to add command handler to LXC mainloop."); |
96fa1ff0 | 374 | goto out_mainloop_open; |
563f2f2c DL |
375 | } |
376 | ||
828695d9 | 377 | if (handler->conf->need_utmp_watch) { |
e37dda71 | 378 | #if HAVE_LIBCAP |
828695d9 | 379 | if (lxc_utmp_mainloop_add(&descr, handler)) { |
408da065 | 380 | ERROR("Failed to add utmp handler to LXC mainloop."); |
828695d9 SH |
381 | goto out_mainloop_open; |
382 | } | |
495d2046 | 383 | #else |
408da065 | 384 | DEBUG("Not starting utmp handler as CAP_SYS_BOOT cannot be dropped without capabilities support."); |
495d2046 | 385 | #endif |
563f2f2c | 386 | } |
b0a33c1e | 387 | |
e51d4895 | 388 | return lxc_mainloop(&descr, -1); |
b0a33c1e | 389 | |
390 | out_mainloop_open: | |
391 | lxc_mainloop_close(&descr); | |
408da065 | 392 | |
b0a33c1e | 393 | out_sigfd: |
394 | close(sigfd); | |
408da065 | 395 | |
c3e13372 | 396 | return -1; |
b0a33c1e | 397 | } |
398 | ||
13f5be62 | 399 | struct lxc_handler *lxc_init(const char *name, struct lxc_conf *conf, const char *lxcpath) |
59eb99ba | 400 | { |
b6b2b194 | 401 | int i; |
3a0f472d DL |
402 | struct lxc_handler *handler; |
403 | ||
404 | handler = malloc(sizeof(*handler)); | |
405 | if (!handler) | |
406 | return NULL; | |
59eb99ba DL |
407 | |
408 | memset(handler, 0, sizeof(*handler)); | |
409 | ||
e8bd4e43 | 410 | handler->ttysock[0] = handler->ttysock[1] = -1; |
fae349da | 411 | handler->conf = conf; |
9123e471 | 412 | handler->lxcpath = lxcpath; |
5c068da9 | 413 | handler->pinfd = -1; |
fae349da | 414 | |
b6b2b194 WB |
415 | for (i = 0; i < LXC_NS_MAX; i++) |
416 | handler->nsfd[i] = -1; | |
417 | ||
fe4de9a6 DE |
418 | lsm_init(); |
419 | ||
3bdf52d7 DL |
420 | handler->name = strdup(name); |
421 | if (!handler->name) { | |
408da065 | 422 | ERROR("Failed to allocate memory."); |
3bdf52d7 DL |
423 | goto out_free; |
424 | } | |
425 | ||
ef6e34ee | 426 | if (lxc_cmd_init(name, handler, lxcpath)) |
d2e30e99 DE |
427 | goto out_free_name; |
428 | ||
8f2c3a70 | 429 | if (lxc_read_seccomp_config(conf) != 0) { |
408da065 | 430 | ERROR("Failed loading seccomp policy."); |
d2e30e99 | 431 | goto out_close_maincmd_fd; |
8f2c3a70 SH |
432 | } |
433 | ||
408da065 | 434 | /* Begin by setting the state to STARTING. */ |
25c2aca5 | 435 | if (lxc_set_state(name, handler, STARTING)) { |
408da065 | 436 | ERROR("Failed to set state for container \"%s\" to \"%s\".", name, lxc_state2str(STARTING)); |
051151de | 437 | goto out_close_maincmd_fd; |
0ad19a3f | 438 | } |
439 | ||
408da065 CB |
440 | /* Start of environment variable setup for hooks. */ |
441 | if (name && setenv("LXC_NAME", name, 1)) | |
442 | SYSERROR("Failed to set environment variable: LXC_NAME=%s.", name); | |
443 | ||
444 | if (conf->rcfile && setenv("LXC_CONFIG_FILE", conf->rcfile, 1)) | |
445 | SYSERROR("Failed to set environment variable: LXC_CONFIG_FILE=%s.", conf->rcfile); | |
446 | ||
447 | if (conf->rootfs.mount && setenv("LXC_ROOTFS_MOUNT", conf->rootfs.mount, 1)) | |
448 | SYSERROR("Failed to set environment variable: LXC_ROOTFS_MOUNT=%s.", conf->rootfs.mount); | |
449 | ||
450 | if (conf->rootfs.path && setenv("LXC_ROOTFS_PATH", conf->rootfs.path, 1)) | |
451 | SYSERROR("Failed to set environment variable: LXC_ROOTFS_PATH=%s.", conf->rootfs.path); | |
452 | ||
453 | if (conf->console.path && setenv("LXC_CONSOLE", conf->console.path, 1)) | |
454 | SYSERROR("Failed to set environment variable: LXC_CONSOLE=%s.", conf->console.path); | |
455 | ||
456 | if (conf->console.log_path && setenv("LXC_CONSOLE_LOGPATH", conf->console.log_path, 1)) | |
457 | SYSERROR("Failed to set environment variable: LXC_CONSOLE_LOGPATH=%s.", conf->console.log_path); | |
458 | ||
459 | if (setenv("LXC_CGNS_AWARE", "1", 1)) | |
460 | SYSERROR("Failed to set environment variable LXC_CGNS_AWARE=1."); | |
461 | /* End of environment variable setup for hooks. */ | |
f7bee6c6 | 462 | |
283678ed | 463 | if (run_lxc_hooks(name, "pre-start", conf, handler->lxcpath, NULL)) { |
408da065 | 464 | ERROR("Failed to run lxc.hook.pre-start for container \"%s\".", name); |
773fb9ca SH |
465 | goto out_aborting; |
466 | } | |
26ddeedd | 467 | |
408da065 CB |
468 | /* The signal fd has to be created before forking otherwise if the child |
469 | * process exits before we setup the signal fd, the event will be lost | |
470 | * and the command will be stuck. | |
471 | */ | |
83ee7875 | 472 | handler->sigfd = setup_signal_fd(&handler->oldmask); |
59eb99ba | 473 | if (handler->sigfd < 0) { |
408da065 | 474 | ERROR("Failed to setup SIGCHLD fd handler."); |
b5159817 DE |
475 | goto out_delete_tty; |
476 | } | |
477 | ||
408da065 | 478 | /* Do this after setting up signals since it might unblock SIGWINCH. */ |
b5159817 | 479 | if (lxc_console_create(conf)) { |
408da065 | 480 | ERROR("Failed to create console for container \"%s\".", name); |
b5159817 | 481 | goto out_restore_sigmask; |
b0a33c1e | 482 | } |
483 | ||
54117de5 | 484 | if (lxc_ttys_shift_ids(conf) < 0) { |
408da065 | 485 | ERROR("Failed to shift tty into container."); |
c4d10a05 SH |
486 | goto out_restore_sigmask; |
487 | } | |
488 | ||
408da065 | 489 | INFO("Container \"%s\" is initialized.", name); |
3a0f472d | 490 | return handler; |
59eb99ba | 491 | |
b5159817 DE |
492 | out_restore_sigmask: |
493 | sigprocmask(SIG_SETMASK, &handler->oldmask, NULL); | |
59eb99ba | 494 | out_delete_tty: |
fae349da | 495 | lxc_delete_tty(&conf->tty_info); |
59eb99ba | 496 | out_aborting: |
25c2aca5 | 497 | lxc_set_state(name, handler, ABORTING); |
d2e30e99 DE |
498 | out_close_maincmd_fd: |
499 | close(conf->maincmd_fd); | |
500 | conf->maincmd_fd = -1; | |
3bdf52d7 DL |
501 | out_free_name: |
502 | free(handler->name); | |
503 | handler->name = NULL; | |
3a0f472d DL |
504 | out_free: |
505 | free(handler); | |
c3e13372 | 506 | return NULL; |
59eb99ba DL |
507 | } |
508 | ||
3b72c4a0 | 509 | void lxc_fini(const char *name, struct lxc_handler *handler) |
59eb99ba | 510 | { |
b3286b62 WB |
511 | int i, rc; |
512 | pid_t self = getpid(); | |
513 | char *namespaces[LXC_NS_MAX+1]; | |
514 | size_t namespace_count = 0; | |
b6b2b194 | 515 | |
408da065 CB |
516 | /* The STOPPING state is there for future cleanup code which can take |
517 | * awhile. | |
59eb99ba | 518 | */ |
25c2aca5 | 519 | lxc_set_state(name, handler, STOPPING); |
b6b2b194 | 520 | |
b3286b62 WB |
521 | for (i = 0; i < LXC_NS_MAX; i++) { |
522 | if (handler->nsfd[i] != -1) { | |
523 | rc = asprintf(&namespaces[namespace_count], "%s:/proc/%d/fd/%d", | |
524 | ns_info[i].proc_name, self, handler->nsfd[i]); | |
525 | if (rc == -1) { | |
408da065 | 526 | SYSERROR("Failed to allocate memory."); |
b3286b62 WB |
527 | break; |
528 | } | |
529 | ++namespace_count; | |
530 | } | |
531 | } | |
532 | namespaces[namespace_count] = NULL; | |
c154af98 | 533 | |
408da065 CB |
534 | if (handler->conf->reboot && setenv("LXC_TARGET", "reboot", 1)) |
535 | SYSERROR("Failed to set environment variable: LXC_TARGET=reboot."); | |
536 | ||
537 | if (!handler->conf->reboot && setenv("LXC_TARGET", "stop", 1)) | |
538 | SYSERROR("Failed to set environment variable: LXC_TARGET=stop."); | |
c154af98 | 539 | |
b3286b62 | 540 | if (run_lxc_hooks(name, "stop", handler->conf, handler->lxcpath, namespaces)) |
408da065 | 541 | ERROR("Failed to run lxc.hook.stop for container \"%s\".", name); |
c154af98 | 542 | |
b3286b62 WB |
543 | while (namespace_count--) |
544 | free(namespaces[namespace_count]); | |
b6b2b194 WB |
545 | for (i = 0; i < LXC_NS_MAX; i++) { |
546 | if (handler->nsfd[i] != -1) { | |
547 | close(handler->nsfd[i]); | |
548 | handler->nsfd[i] = -1; | |
549 | } | |
550 | } | |
738d0deb CB |
551 | |
552 | if (handler->netnsfd >= 0) { | |
553 | close(handler->netnsfd); | |
554 | handler->netnsfd = -1; | |
555 | } | |
556 | ||
25c2aca5 | 557 | lxc_set_state(name, handler, STOPPED); |
59eb99ba | 558 | |
f3787121 | 559 | if (run_lxc_hooks(name, "post-stop", handler->conf, handler->lxcpath, NULL)) { |
408da065 | 560 | ERROR("Failed to run lxc.hook.post-stop for container \"%s\".", name); |
f3787121 CB |
561 | if (handler->conf->reboot) { |
562 | WARN("Container will be stopped instead of rebooted."); | |
563 | handler->conf->reboot = 0; | |
408da065 CB |
564 | if (setenv("LXC_TARGET", "stop", 1)) |
565 | WARN("Failed to set environment variable: LXC_TARGET=stop."); | |
f3787121 CB |
566 | } |
567 | } | |
26ddeedd | 568 | |
408da065 | 569 | /* Reset mask set by setup_signal_fd. */ |
8f64a3f6 | 570 | if (sigprocmask(SIG_SETMASK, &handler->oldmask, NULL)) |
408da065 | 571 | WARN("Failed to restore signal mask."); |
8f64a3f6 | 572 | |
b5159817 | 573 | lxc_console_delete(&handler->conf->console); |
b2431939 | 574 | lxc_delete_tty(&handler->conf->tty_info); |
d2e30e99 DE |
575 | close(handler->conf->maincmd_fd); |
576 | handler->conf->maincmd_fd = -1; | |
3bdf52d7 | 577 | free(handler->name); |
e8bd4e43 SH |
578 | if (handler->ttysock[0] != -1) { |
579 | close(handler->ttysock[0]); | |
580 | close(handler->ttysock[1]); | |
581 | } | |
2c5f2ede CB |
582 | |
583 | if (handler->conf->ephemeral == 1 && handler->conf->reboot != 1) | |
28272964 | 584 | lxc_destroy_container_on_signal(handler, name); |
2c5f2ede | 585 | |
d4ef7c50 | 586 | cgroup_destroy(handler); |
b2431939 | 587 | free(handler); |
59eb99ba DL |
588 | } |
589 | ||
735f2c6e | 590 | void lxc_abort(const char *name, struct lxc_handler *handler) |
59eb99ba | 591 | { |
73e608b2 SH |
592 | int ret, status; |
593 | ||
25c2aca5 | 594 | lxc_set_state(name, handler, ABORTING); |
7d9fb3e9 DL |
595 | if (handler->pid > 0) |
596 | kill(handler->pid, SIGKILL); | |
408da065 CB |
597 | while ((ret = waitpid(-1, &status, 0)) > 0) { |
598 | ; | |
599 | } | |
59eb99ba DL |
600 | } |
601 | ||
828695d9 SH |
602 | #include <sys/reboot.h> |
603 | #include <linux/reboot.h> | |
604 | ||
408da065 CB |
605 | /* reboot(LINUX_REBOOT_CMD_CAD_ON) will return -EINVAL in a child pid namespace |
606 | * if container reboot support exists. Otherwise, it will either succeed or | |
607 | * return -EPERM. | |
e2fa1520 SH |
608 | */ |
609 | static int container_reboot_supported(void *arg) | |
828695d9 | 610 | { |
d028235d | 611 | int *cmd = arg; |
828695d9 | 612 | int ret; |
828695d9 | 613 | |
d028235d | 614 | ret = reboot(*cmd); |
e2fa1520 SH |
615 | if (ret == -1 && errno == EINVAL) |
616 | return 1; | |
617 | return 0; | |
618 | } | |
619 | ||
b60ed720 | 620 | static int must_drop_cap_sys_boot(struct lxc_conf *conf) |
e2fa1520 | 621 | { |
025ed0f3 | 622 | FILE *f; |
b60ed720 | 623 | int ret, cmd, v, flags; |
d028235d SG |
624 | long stack_size = 4096; |
625 | void *stack = alloca(stack_size); | |
626 | int status; | |
627 | pid_t pid; | |
e2fa1520 | 628 | |
025ed0f3 | 629 | f = fopen("/proc/sys/kernel/ctrl-alt-del", "r"); |
e2fa1520 SH |
630 | if (!f) { |
631 | DEBUG("failed to open /proc/sys/kernel/ctrl-alt-del"); | |
828695d9 | 632 | return 1; |
e2fa1520 | 633 | } |
828695d9 SH |
634 | |
635 | ret = fscanf(f, "%d", &v); | |
636 | fclose(f); | |
e2fa1520 | 637 | if (ret != 1) { |
408da065 | 638 | DEBUG("Failed to read /proc/sys/kernel/ctrl-alt-del."); |
828695d9 | 639 | return 1; |
e2fa1520 SH |
640 | } |
641 | cmd = v ? LINUX_REBOOT_CMD_CAD_ON : LINUX_REBOOT_CMD_CAD_OFF; | |
642 | ||
b60ed720 SH |
643 | flags = CLONE_NEWPID | SIGCHLD; |
644 | if (!lxc_list_empty(&conf->id_map)) | |
645 | flags |= CLONE_NEWUSER; | |
646 | ||
7f145a6d | 647 | #ifdef __ia64__ |
959aee9c | 648 | pid = __clone2(container_reboot_supported, stack, stack_size, flags, &cmd); |
7f145a6d | 649 | #else |
959aee9c SG |
650 | stack += stack_size; |
651 | pid = clone(container_reboot_supported, stack, flags, &cmd); | |
7f145a6d | 652 | #endif |
959aee9c | 653 | if (pid < 0) { |
c08220e9 | 654 | if (flags & CLONE_NEWUSER) |
408da065 | 655 | ERROR("Failed to clone (%#x): %s (includes CLONE_NEWUSER).", flags, strerror(errno)); |
c08220e9 | 656 | else |
408da065 | 657 | ERROR("Failed to clone (%#x): %s.", flags, strerror(errno)); |
959aee9c SG |
658 | return -1; |
659 | } | |
660 | if (wait(&status) < 0) { | |
408da065 | 661 | SYSERROR("Unexpected wait error: %m."); |
959aee9c SG |
662 | return -1; |
663 | } | |
e2fa1520 SH |
664 | |
665 | if (WEXITSTATUS(status) != 1) | |
828695d9 | 666 | return 1; |
e2fa1520 | 667 | |
828695d9 SH |
668 | return 0; |
669 | } | |
670 | ||
408da065 CB |
671 | /* netpipe is used in the unprivileged case to transfer the ifindexes from |
672 | * parent to child | |
658979c5 SH |
673 | */ |
674 | static int netpipe = -1; | |
675 | ||
676 | static inline int count_veths(struct lxc_list *network) | |
677 | { | |
678 | struct lxc_list *iterator; | |
679 | struct lxc_netdev *netdev; | |
680 | int count = 0; | |
681 | ||
682 | lxc_list_for_each(iterator, network) { | |
683 | netdev = iterator->elem; | |
684 | if (netdev->type != LXC_NET_VETH) | |
685 | continue; | |
686 | count++; | |
687 | } | |
688 | return count; | |
689 | } | |
690 | ||
691 | static int read_unpriv_netifindex(struct lxc_list *network) | |
692 | { | |
693 | struct lxc_list *iterator; | |
694 | struct lxc_netdev *netdev; | |
695 | ||
696 | if (netpipe == -1) | |
697 | return 0; | |
698 | lxc_list_for_each(iterator, network) { | |
699 | netdev = iterator->elem; | |
700 | if (netdev->type != LXC_NET_VETH) | |
701 | continue; | |
702 | if (!(netdev->name = malloc(IFNAMSIZ))) { | |
408da065 | 703 | ERROR("Out of memory."); |
658979c5 SH |
704 | close(netpipe); |
705 | return -1; | |
706 | } | |
707 | if (read(netpipe, netdev->name, IFNAMSIZ) != IFNAMSIZ) { | |
708 | close(netpipe); | |
709 | return -1; | |
710 | } | |
711 | } | |
712 | close(netpipe); | |
713 | return 0; | |
714 | } | |
715 | ||
ffe1e01a | 716 | static int do_start(void *data) |
50e98013 | 717 | { |
7c661726 | 718 | struct lxc_list *iterator; |
23c53af9 | 719 | struct lxc_handler *handler = data; |
7a55c157 TA |
720 | int devnull_fd = -1, ret; |
721 | char path[PATH_MAX]; | |
50e98013 DL |
722 | |
723 | if (sigprocmask(SIG_SETMASK, &handler->oldmask, NULL)) { | |
408da065 | 724 | SYSERROR("Failed to set signal mask."); |
9d7f9e52 | 725 | return -1; |
50e98013 DL |
726 | } |
727 | ||
408da065 CB |
728 | /* This prctl must be before the synchro, so if the parent dies before |
729 | * we set the parent death signal, we will detect its death with the | |
730 | * synchro right after, otherwise we have a window where the parent can | |
731 | * exit before we set the pdeath signal leading to a unsupervized | |
732 | * container. | |
743ecd2e DL |
733 | */ |
734 | if (prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0)) { | |
408da065 | 735 | SYSERROR("Failed to set PR_SET_PDEATHSIG to SIGKILL."); |
743ecd2e DL |
736 | return -1; |
737 | } | |
738 | ||
3c22086f | 739 | lxc_sync_fini_parent(handler); |
50e98013 | 740 | |
408da065 | 741 | /* Don't leak the pinfd to the container. */ |
025ed0f3 | 742 | if (handler->pinfd >= 0) { |
0d03360a | 743 | close(handler->pinfd); |
025ed0f3 | 744 | } |
2b0e17e4 | 745 | |
5b1e83cb SH |
746 | if (lxc_sync_wait_parent(handler, LXC_SYNC_STARTUP)) |
747 | return -1; | |
748 | ||
408da065 CB |
749 | /* Unshare CLONE_NEWNET after CLONE_NEWUSER. See |
750 | * https://github.com/lxc/lxd/issues/1978. | |
751 | */ | |
5b1e83cb | 752 | if ((handler->clone_flags & (CLONE_NEWNET | CLONE_NEWUSER)) == |
408da065 | 753 | (CLONE_NEWNET | CLONE_NEWUSER)) { |
5b1e83cb SH |
754 | ret = unshare(CLONE_NEWNET); |
755 | if (ret < 0) { | |
408da065 | 756 | SYSERROR("Failed to unshare CLONE_NEWNET."); |
5b1e83cb SH |
757 | goto out_warn_father; |
758 | } | |
408da065 | 759 | INFO("Unshared CLONE_NEWNET."); |
5b1e83cb SH |
760 | } |
761 | ||
408da065 CB |
762 | /* Tell the parent task it can begin to configure the container and wait |
763 | * for it to finish. | |
3c22086f CLG |
764 | */ |
765 | if (lxc_sync_barrier_parent(handler, LXC_SYNC_CONFIGURE)) | |
9d7f9e52 | 766 | return -1; |
50e98013 | 767 | |
658979c5 SH |
768 | if (read_unpriv_netifindex(&handler->conf->network) < 0) |
769 | goto out_warn_father; | |
770 | ||
408da065 | 771 | /* If we are in a new user namespace, become root there to have |
d08f8d2f | 772 | * privilege over our namespace. |
f6d3e3e4 SH |
773 | */ |
774 | if (!lxc_list_empty(&handler->conf->id_map)) { | |
d08f8d2f | 775 | if (lxc_switch_uid_gid(0, 0) < 0) |
f6d3e3e4 | 776 | goto out_warn_father; |
d08f8d2f CB |
777 | |
778 | /* Drop groups only after we switched to a valid gid in the new | |
779 | * user namespace. | |
780 | */ | |
781 | if (lxc_setgroups(0, NULL) < 0) | |
c476bdce | 782 | goto out_warn_father; |
f6d3e3e4 SH |
783 | } |
784 | ||
99b71824 | 785 | if (access(handler->lxcpath, X_OK)) { |
c8154066 SH |
786 | print_top_failing_dir(handler->lxcpath); |
787 | goto out_warn_father; | |
788 | } | |
789 | ||
e37dda71 | 790 | #if HAVE_LIBCAP |
69182a31 | 791 | if (handler->conf->need_utmp_watch) { |
828695d9 | 792 | if (prctl(PR_CAPBSET_DROP, CAP_SYS_BOOT, 0, 0, 0)) { |
408da065 | 793 | SYSERROR("Failed to remove the CAP_SYS_BOOT capability."); |
c4ea60df | 794 | goto out_warn_father; |
828695d9 | 795 | } |
408da065 | 796 | DEBUG("Dropped the CAP_SYS_BOOT capability."); |
e2fa1520 | 797 | } |
495d2046 | 798 | #endif |
e2fa1520 | 799 | |
a91cde21 | 800 | ret = snprintf(path, sizeof(path), "%s/dev/null", handler->conf->rootfs.mount); |
408da065 | 801 | if (ret < 0 || ret >= sizeof(path)) |
7a55c157 | 802 | goto out_warn_father; |
7a55c157 TA |
803 | |
804 | /* In order to checkpoint restore, we need to have everything in the | |
805 | * same mount namespace. However, some containers may not have a | |
806 | * reasonable /dev (in particular, they may not have /dev/null), so we | |
807 | * can't set init's std fds to /dev/null by opening it from inside the | |
808 | * container. | |
809 | * | |
810 | * If that's the case, fall back to using the host's /dev/null. This | |
811 | * means that migration won't work, but at least we won't spew output | |
812 | * where it isn't wanted. | |
813 | */ | |
814 | if (handler->backgrounded && !handler->conf->autodev && access(path, F_OK) < 0) { | |
c44de748 AM |
815 | devnull_fd = open_devnull(); |
816 | ||
817 | if (devnull_fd < 0) | |
818 | goto out_warn_father; | |
408da065 CB |
819 | WARN("Using /dev/null from the host for container init's " |
820 | "standard file descriptors. Migration will not work."); | |
c44de748 AM |
821 | } |
822 | ||
e2fa1520 | 823 | /* Setup the container, ip, names, utsname, ... */ |
d4ef7c50 | 824 | if (lxc_setup(handler)) { |
408da065 | 825 | ERROR("Failed to setup container \"%s\".", handler->name); |
e2fa1520 SH |
826 | goto out_warn_father; |
827 | } | |
50e98013 | 828 | |
408da065 | 829 | /* Ask father to setup cgroups and wait for him to finish. */ |
544a48a0 | 830 | if (lxc_sync_barrier_parent(handler, LXC_SYNC_CGROUP)) |
c44de748 | 831 | goto out_error; |
544a48a0 | 832 | |
deefdf8a CB |
833 | /* Unshare cgroup namespace after we have setup our cgroups. If we do it |
834 | * earlier we end up with a wrong view of /proc/self/cgroup. For | |
835 | * example, assume we unshare(CLONE_NEWCGROUP) first, and then create | |
836 | * the cgroup for the container, say /sys/fs/cgroup/cpuset/lxc/c, then | |
837 | * /proc/self/cgroup would show us: | |
838 | * | |
839 | * 8:cpuset:/lxc/c | |
840 | * | |
841 | * whereas it should actually show | |
842 | * | |
843 | * 8:cpuset:/ | |
844 | */ | |
845 | if (cgns_supported()) { | |
846 | if (unshare(CLONE_NEWCGROUP) < 0) { | |
847 | INFO("Failed to unshare CLONE_NEWCGROUP."); | |
848 | goto out_warn_father; | |
849 | } | |
850 | INFO("Unshared CLONE_NEWCGROUP."); | |
851 | } | |
852 | ||
408da065 | 853 | /* Set the label to change to when we exec(2) the container's init. */ |
7aff4f43 | 854 | if (lsm_process_label_set(NULL, handler->conf, 1, 1) < 0) |
e075f5d9 | 855 | goto out_warn_father; |
5112cd70 | 856 | |
029cdff5 | 857 | /* Set PR_SET_NO_NEW_PRIVS after we changed the lsm label. If we do it |
408da065 CB |
858 | * before we aren't allowed anymore. |
859 | */ | |
029cdff5 CB |
860 | if (handler->conf->no_new_privs) { |
861 | if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) < 0) { | |
862 | SYSERROR("Could not set PR_SET_NO_NEW_PRIVS to block execve() gainable privileges."); | |
863 | goto out_warn_father; | |
864 | } | |
865 | DEBUG("Set PR_SET_NO_NEW_PRIVS to block execve() gainable privileges."); | |
866 | } | |
867 | ||
0d9acb99 DE |
868 | /* Some init's such as busybox will set sane tty settings on stdin, |
869 | * stdout, stderr which it thinks is the console. We already set them | |
870 | * the way we wanted on the real terminal, and we want init to do its | |
408da065 CB |
871 | * setup on its console ie. the pty allocated in lxc_console_create() so |
872 | * make sure that that pty is stdin,stdout,stderr. | |
0d9acb99 | 873 | */ |
39a78bbe | 874 | if (lxc_console_set_stdfds(handler->conf->console.slave) < 0) |
0d9acb99 DE |
875 | goto out_warn_father; |
876 | ||
408da065 | 877 | /* If we mounted a temporary proc, then unmount it now. */ |
5112cd70 | 878 | tmp_proc_unmount(handler->conf); |
e075f5d9 | 879 | |
8f2c3a70 SH |
880 | if (lxc_seccomp_load(handler->conf) != 0) |
881 | goto out_warn_father; | |
882 | ||
283678ed | 883 | if (run_lxc_hooks(handler->name, "start", handler->conf, handler->lxcpath, NULL)) { |
408da065 | 884 | ERROR("Failed to run lxc.hook.start for container \"%s\".", handler->name); |
773fb9ca SH |
885 | goto out_warn_father; |
886 | } | |
fc25b815 | 887 | |
d08f8d2f CB |
888 | /* The container has been setup. We can now switch to an unprivileged |
889 | * uid/gid. | |
890 | */ | |
891 | if (handler->conf->is_execute) { | |
87bf0db0 CB |
892 | bool have_cap_setgid; |
893 | uid_t new_uid = handler->conf->init_uid; | |
894 | gid_t new_gid = handler->conf->init_gid; | |
d08f8d2f CB |
895 | |
896 | /* If we are in a new user namespace we already dropped all | |
897 | * groups when we switched to root in the new user namespace | |
898 | * further above. Only drop groups if we can, so ensure that we | |
899 | * have necessary privilege. | |
900 | */ | |
e37dda71 | 901 | #if HAVE_LIBCAP |
207c4c71 | 902 | have_cap_setgid = lxc_proc_cap_is_set(CAP_SETGID, CAP_EFFECTIVE); |
df11e022 BN |
903 | #else |
904 | have_cap_setgid = false; | |
905 | #endif | |
87bf0db0 | 906 | if (lxc_list_empty(&handler->conf->id_map) && have_cap_setgid) { |
d08f8d2f CB |
907 | if (lxc_setgroups(0, NULL) < 0) |
908 | goto out_warn_father; | |
909 | } | |
910 | ||
911 | if (lxc_switch_uid_gid(new_uid, new_gid) < 0) | |
912 | goto out_warn_father; | |
913 | } | |
914 | ||
408da065 CB |
915 | /* The clearenv() and putenv() calls have been moved here to allow us to |
916 | * use environment variables passed to the various hooks, such as the | |
917 | * start hook above. Not all of the variables like CONFIG_PATH or ROOTFS | |
918 | * are valid in this context but others are. | |
919 | */ | |
f7bee6c6 | 920 | if (clearenv()) { |
408da065 CB |
921 | SYSERROR("Failed to clear environment."); |
922 | /* Don't error out though. */ | |
f7bee6c6 MW |
923 | } |
924 | ||
7c661726 MP |
925 | lxc_list_for_each(iterator, &handler->conf->environment) { |
926 | if (putenv((char *)iterator->elem)) { | |
408da065 | 927 | SYSERROR("Failed to set environment variable: %s.", (char *)iterator->elem); |
7c661726 MP |
928 | goto out_warn_father; |
929 | } | |
930 | } | |
931 | ||
f7bee6c6 | 932 | if (putenv("container=lxc")) { |
408da065 | 933 | SYSERROR("Failed to set environment variable: container=lxc."); |
c4ea60df | 934 | goto out_warn_father; |
f7bee6c6 MW |
935 | } |
936 | ||
393903d1 SH |
937 | if (handler->conf->pty_names) { |
938 | if (putenv(handler->conf->pty_names)) { | |
408da065 | 939 | SYSERROR("Failed to set environment variable for container ptys."); |
393903d1 SH |
940 | goto out_warn_father; |
941 | } | |
942 | } | |
943 | ||
773fb9ca | 944 | close(handler->sigfd); |
26ddeedd | 945 | |
7a55c157 TA |
946 | if (devnull_fd < 0) { |
947 | devnull_fd = open_devnull(); | |
948 | ||
949 | if (devnull_fd < 0) | |
950 | goto out_warn_father; | |
951 | } | |
952 | ||
c44de748 | 953 | if (handler->backgrounded && set_stdfds(devnull_fd)) |
69aeabac | 954 | goto out_warn_father; |
507cee36 | 955 | |
c44de748 AM |
956 | if (devnull_fd >= 0) { |
957 | close(devnull_fd); | |
958 | devnull_fd = -1; | |
959 | } | |
960 | ||
8c9a7665 TA |
961 | setsid(); |
962 | ||
408da065 CB |
963 | /* After this call, we are in error because this ops should not return |
964 | * as it execs. | |
965 | */ | |
c4ea60df | 966 | handler->ops->start(handler, handler->data); |
50e98013 DL |
967 | |
968 | out_warn_father: | |
408da065 CB |
969 | /* We want the parent to know something went wrong, so we return a |
970 | * special error code. | |
971 | */ | |
d1ccb562 | 972 | lxc_sync_wake_parent(handler, LXC_SYNC_ERROR); |
c44de748 AM |
973 | |
974 | out_error: | |
975 | if (devnull_fd >= 0) | |
976 | close(devnull_fd); | |
977 | ||
50e98013 DL |
978 | return -1; |
979 | } | |
980 | ||
74a3920a | 981 | static int save_phys_nics(struct lxc_conf *conf) |
7b35f3d6 SH |
982 | { |
983 | struct lxc_list *iterator; | |
40f2f8a2 | 984 | int am_root = (getuid() == 0); |
7b35f3d6 | 985 | |
40f2f8a2 LQ |
986 | if (!am_root) |
987 | return 0; | |
2c5f2ede | 988 | |
7b35f3d6 SH |
989 | lxc_list_for_each(iterator, &conf->network) { |
990 | struct lxc_netdev *netdev = iterator->elem; | |
991 | ||
992 | if (netdev->type != LXC_NET_PHYS) | |
993 | continue; | |
994 | conf->saved_nics = realloc(conf->saved_nics, | |
995 | (conf->num_savednics+1)*sizeof(struct saved_nic)); | |
408da065 | 996 | if (!conf->saved_nics) |
7b35f3d6 | 997 | return -1; |
7b35f3d6 SH |
998 | conf->saved_nics[conf->num_savednics].ifindex = netdev->ifindex; |
999 | conf->saved_nics[conf->num_savednics].orig_name = strdup(netdev->link); | |
408da065 | 1000 | if (!conf->saved_nics[conf->num_savednics].orig_name) |
7b35f3d6 | 1001 | return -1; |
408da065 | 1002 | INFO("Stored saved_nic #%d idx %d name %s.", conf->num_savednics, |
7b35f3d6 SH |
1003 | conf->saved_nics[conf->num_savednics].ifindex, |
1004 | conf->saved_nics[conf->num_savednics].orig_name); | |
1005 | conf->num_savednics++; | |
1006 | } | |
1007 | ||
1008 | return 0; | |
1009 | } | |
1010 | ||
ae467c54 | 1011 | static int lxc_recv_ttys_from_child(struct lxc_handler *handler) |
e8bd4e43 | 1012 | { |
ae467c54 CB |
1013 | int i; |
1014 | int *ttyfds; | |
1015 | struct lxc_pty_info *pty_info; | |
1016 | int ret = -1; | |
f07fa8df | 1017 | int sock = handler->ttysock[1]; |
e8bd4e43 | 1018 | struct lxc_conf *conf = handler->conf; |
e8bd4e43 | 1019 | struct lxc_tty_info *tty_info = &conf->tty_info; |
ae467c54 | 1020 | size_t num_ttyfds = (2 * conf->tty); |
e8bd4e43 SH |
1021 | |
1022 | if (!conf->tty) | |
1023 | return 0; | |
1024 | ||
408da065 CB |
1025 | tty_info->pty_info = malloc(sizeof(*tty_info->pty_info) * conf->tty); |
1026 | if (!tty_info->pty_info) | |
e8bd4e43 | 1027 | return -1; |
e8bd4e43 | 1028 | |
ae467c54 CB |
1029 | ttyfds = malloc(num_ttyfds * sizeof(int)); |
1030 | if (!ttyfds) | |
1031 | return -1; | |
1032 | ||
1033 | ret = lxc_abstract_unix_recv_fds(sock, ttyfds, num_ttyfds, NULL, 0); | |
1034 | for (i = 0; (ret >= 0 && *ttyfds != -1) && (i < num_ttyfds); i++) { | |
1035 | pty_info = &tty_info->pty_info[i / 2]; | |
e8bd4e43 | 1036 | pty_info->busy = 0; |
ae467c54 CB |
1037 | pty_info->slave = ttyfds[i++]; |
1038 | pty_info->master = ttyfds[i]; | |
1039 | TRACE("received pty with master fd %d and slave fd %d from " | |
1040 | "parent", pty_info->master, pty_info->slave); | |
e8bd4e43 | 1041 | } |
ae467c54 | 1042 | |
e8bd4e43 SH |
1043 | tty_info->nbtty = conf->tty; |
1044 | ||
ae467c54 CB |
1045 | free(ttyfds); |
1046 | ||
1047 | if (ret < 0) | |
1048 | ERROR("failed to receive %d ttys from child: %s", conf->tty, | |
1049 | strerror(errno)); | |
1050 | else | |
1051 | TRACE("received %d ttys from child", conf->tty); | |
1052 | ||
1053 | return ret; | |
e8bd4e43 SH |
1054 | } |
1055 | ||
f813849c TA |
1056 | void resolve_clone_flags(struct lxc_handler *handler) |
1057 | { | |
1058 | handler->clone_flags = CLONE_NEWPID | CLONE_NEWNS; | |
1059 | ||
408da065 | 1060 | if (!lxc_list_empty(&handler->conf->id_map)) |
f813849c | 1061 | handler->clone_flags |= CLONE_NEWUSER; |
f813849c TA |
1062 | |
1063 | if (handler->conf->inherit_ns_fd[LXC_NS_NET] == -1) { | |
408da065 | 1064 | if (!lxc_requests_empty_network(handler)) |
f813849c TA |
1065 | handler->clone_flags |= CLONE_NEWNET; |
1066 | } else { | |
198cbbaa | 1067 | INFO("Inheriting a NET namespace."); |
f813849c TA |
1068 | } |
1069 | ||
408da065 | 1070 | if (handler->conf->inherit_ns_fd[LXC_NS_IPC] == -1) |
f813849c | 1071 | handler->clone_flags |= CLONE_NEWIPC; |
408da065 | 1072 | else |
198cbbaa | 1073 | INFO("Inheriting an IPC namespace."); |
f813849c | 1074 | |
408da065 | 1075 | if (handler->conf->inherit_ns_fd[LXC_NS_UTS] == -1) |
f813849c | 1076 | handler->clone_flags |= CLONE_NEWUTS; |
408da065 | 1077 | else |
198cbbaa | 1078 | INFO("Inheriting a UTS namespace."); |
f813849c TA |
1079 | } |
1080 | ||
480588e6 CB |
1081 | /* lxc_spawn() performs crucial setup tasks and clone()s the new process which |
1082 | * exec()s the requested container binary. | |
1083 | * Note that lxc_spawn() runs in the parent namespaces. Any operations performed | |
1084 | * right here should be double checked if they'd pose a security risk. (For | |
1085 | * example, any {u}mount() operations performed here will be reflected on the | |
1086 | * host!) | |
1087 | */ | |
74a3920a | 1088 | static int lxc_spawn(struct lxc_handler *handler) |
59eb99ba | 1089 | { |
b98f7d6e | 1090 | int failed_before_rename = 0; |
ffe1e01a | 1091 | const char *name = handler->name; |
7e4dfe0b | 1092 | bool cgroups_connected = false; |
9f30a190 | 1093 | int saved_ns_fd[LXC_NS_MAX]; |
5b1e83cb | 1094 | int preserve_mask = 0, i, flags; |
658979c5 | 1095 | int netpipepair[2], nveths; |
57927bf2 CB |
1096 | bool wants_to_map_ids; |
1097 | struct lxc_list *id_map; | |
9f30a190 | 1098 | |
2f2623ec | 1099 | netpipe = -1; |
57927bf2 CB |
1100 | id_map = &handler->conf->id_map; |
1101 | wants_to_map_ids = !lxc_list_empty(id_map); | |
2f2623ec | 1102 | |
9f30a190 | 1103 | for (i = 0; i < LXC_NS_MAX; i++) |
6c544cb3 | 1104 | if (handler->conf->inherit_ns_fd[i] != -1) |
9f30a190 | 1105 | preserve_mask |= ns_info[i].clone_flag; |
50e98013 | 1106 | |
3c22086f | 1107 | if (lxc_sync_init(handler)) |
9d7f9e52 | 1108 | return -1; |
0ad19a3f | 1109 | |
e8bd4e43 SH |
1110 | if (socketpair(AF_UNIX, SOCK_DGRAM, 0, handler->ttysock) < 0) { |
1111 | lxc_sync_fini(handler); | |
1112 | return -1; | |
1113 | } | |
1114 | ||
f813849c | 1115 | resolve_clone_flags(handler); |
9f30a190 | 1116 | |
f813849c | 1117 | if (handler->clone_flags & CLONE_NEWNET) { |
26b797f3 SH |
1118 | if (!lxc_list_empty(&handler->conf->network)) { |
1119 | ||
9f30a190 MM |
1120 | /* Find gateway addresses from the link device, which is |
1121 | * no longer accessible inside the container. Do this | |
1122 | * before creating network interfaces, since goto | |
408da065 CB |
1123 | * out_delete_net does not work before lxc_clone. |
1124 | */ | |
9f30a190 | 1125 | if (lxc_find_gateway_addresses(handler)) { |
408da065 | 1126 | ERROR("Failed to find gateway addresses."); |
9f30a190 MM |
1127 | lxc_sync_fini(handler); |
1128 | return -1; | |
1129 | } | |
1130 | ||
408da065 CB |
1131 | /* That should be done before the clone because we will |
1132 | * fill the netdev index and use them in the child. | |
9f30a190 MM |
1133 | */ |
1134 | if (lxc_create_network(handler)) { | |
408da065 | 1135 | ERROR("Failed to create the network."); |
9f30a190 MM |
1136 | lxc_sync_fini(handler); |
1137 | return -1; | |
1138 | } | |
19a26f82 MK |
1139 | } |
1140 | ||
9f30a190 | 1141 | if (save_phys_nics(handler->conf)) { |
408da065 | 1142 | ERROR("Failed to save physical nic info."); |
9f30a190 | 1143 | goto out_abort; |
82d5ae15 DL |
1144 | } |
1145 | } | |
1146 | ||
d4ef7c50 | 1147 | if (!cgroup_init(handler)) { |
408da065 | 1148 | ERROR("Failed initializing cgroup support."); |
33ad9f1a CS |
1149 | goto out_delete_net; |
1150 | } | |
1151 | ||
7e4dfe0b SH |
1152 | cgroups_connected = true; |
1153 | ||
d4ef7c50 | 1154 | if (!cgroup_create(handler)) { |
408da065 | 1155 | ERROR("Failed creating cgroups."); |
47d8fb3b CS |
1156 | goto out_delete_net; |
1157 | } | |
1158 | ||
408da065 CB |
1159 | /* If the rootfs is not a blockdev, prevent the container from marking |
1160 | * it readonly. | |
1161 | * If the container is unprivileged then skip rootfs pinning. | |
0c547523 | 1162 | */ |
0ee35059 | 1163 | if (!wants_to_map_ids) { |
5e32a990 ÇO |
1164 | handler->pinfd = pin_rootfs(handler->conf->rootfs.path); |
1165 | if (handler->pinfd == -1) | |
408da065 | 1166 | INFO("Failed to pin the rootfs for container \"%s\".", handler->name); |
5e32a990 | 1167 | } |
0c547523 | 1168 | |
4d8ac866 | 1169 | if (!preserve_ns(saved_ns_fd, preserve_mask, getpid())) |
cd43d2d1 | 1170 | goto out_delete_net; |
4d8ac866 | 1171 | |
cd43d2d1 SH |
1172 | if (attach_ns(handler->conf->inherit_ns_fd) < 0) |
1173 | goto out_delete_net; | |
9f30a190 | 1174 | |
658979c5 SH |
1175 | if (am_unpriv() && (nveths = count_veths(&handler->conf->network))) { |
1176 | if (pipe(netpipepair) < 0) { | |
408da065 | 1177 | SYSERROR("Failed to create pipe."); |
658979c5 SH |
1178 | goto out_delete_net; |
1179 | } | |
408da065 | 1180 | /* Store netpipe in the global var for do_start's use. */ |
658979c5 SH |
1181 | netpipe = netpipepair[0]; |
1182 | } | |
1183 | ||
408da065 | 1184 | /* Create a process in a new set of namespaces. */ |
5b1e83cb | 1185 | flags = handler->clone_flags; |
408da065 CB |
1186 | if (handler->clone_flags & CLONE_NEWUSER) { |
1187 | /* If CLONE_NEWUSER and CLONE_NEWNET was requested, we need to | |
1188 | * clone a new user namespace first and only later unshare our | |
1189 | * network namespace to ensure that network devices ownership is | |
1190 | * set up correctly. | |
1191 | */ | |
5b1e83cb | 1192 | flags &= ~CLONE_NEWNET; |
408da065 | 1193 | } |
9fac8fbb | 1194 | handler->pid = lxc_clone(do_start, handler, flags); |
59eb99ba | 1195 | if (handler->pid < 0) { |
408da065 | 1196 | SYSERROR("Failed to clone a new set of namespaces."); |
7fef7a06 | 1197 | goto out_delete_net; |
0ad19a3f | 1198 | } |
9662e444 CB |
1199 | for (i = 0; i < LXC_NS_MAX; i++) |
1200 | if (flags & ns_info[i].clone_flag) | |
1201 | INFO("Cloned %s.", ns_info[i].flag_name); | |
0ad19a3f | 1202 | |
4d8ac866 CB |
1203 | if (!preserve_ns(handler->nsfd, handler->clone_flags | preserve_mask, handler->pid)) |
1204 | INFO("Failed to preserve namespace for lxc.hook.stop."); | |
b6b2b194 | 1205 | |
cd43d2d1 | 1206 | if (attach_ns(saved_ns_fd)) |
408da065 | 1207 | WARN("Failed to restore saved namespaces."); |
9f30a190 | 1208 | |
3c22086f CLG |
1209 | lxc_sync_fini_child(handler); |
1210 | ||
408da065 CB |
1211 | /* Map the container uids. The container became an invalid userid the |
1212 | * moment it was cloned with CLONE_NEWUSER. This call doesn't change | |
1213 | * anything immediately, but allows the container to setuid(0) (0 being | |
1214 | * mapped to something else on the host.) later to become a valid uid | |
1215 | * again. | |
1216 | */ | |
57927bf2 | 1217 | if (wants_to_map_ids && lxc_map_ids(id_map, handler->pid)) { |
408da065 | 1218 | ERROR("Failed to set up id mapping."); |
5b1e83cb SH |
1219 | goto out_delete_net; |
1220 | } | |
1221 | ||
1222 | if (lxc_sync_wake_child(handler, LXC_SYNC_STARTUP)) { | |
99a6af52 | 1223 | failed_before_rename = 1; |
5b1e83cb SH |
1224 | goto out_delete_net; |
1225 | } | |
1226 | ||
1227 | if (lxc_sync_wait_child(handler, LXC_SYNC_CONFIGURE)) { | |
1228 | failed_before_rename = 1; | |
1229 | goto out_delete_net; | |
1230 | } | |
0ad19a3f | 1231 | |
d4ef7c50 | 1232 | if (!cgroup_create_legacy(handler)) { |
408da065 | 1233 | ERROR("Failed to setup legacy cgroups for container \"%s\".", name); |
ae5c8b8e | 1234 | goto out_delete_net; |
33ad9f1a | 1235 | } |
9daf6f5d | 1236 | if (!cgroup_setup_limits(handler, false)) { |
408da065 | 1237 | ERROR("Failed to setup cgroup limits for container \"%s\".", name); |
6031a6e5 DE |
1238 | goto out_delete_net; |
1239 | } | |
1240 | ||
d4ef7c50 | 1241 | if (!cgroup_enter(handler)) |
7fef7a06 | 1242 | goto out_delete_net; |
218d4250 | 1243 | |
0996e18a SH |
1244 | if (!cgroup_chown(handler)) |
1245 | goto out_delete_net; | |
1246 | ||
99a6af52 MN |
1247 | if (failed_before_rename) |
1248 | goto out_delete_net; | |
1249 | ||
408da065 | 1250 | /* Create the network configuration. */ |
d5088cf2 | 1251 | if (handler->clone_flags & CLONE_NEWNET) { |
c43cbc04 | 1252 | if (lxc_assign_network(handler->lxcpath, handler->name, |
408da065 CB |
1253 | &handler->conf->network, handler->pid)) { |
1254 | ERROR("Failed to create the configured network."); | |
7fef7a06 | 1255 | goto out_delete_net; |
82d5ae15 | 1256 | } |
0ad19a3f | 1257 | } |
1258 | ||
658979c5 SH |
1259 | if (netpipe != -1) { |
1260 | struct lxc_list *iterator; | |
1261 | struct lxc_netdev *netdev; | |
1262 | ||
1263 | close(netpipe); | |
1264 | lxc_list_for_each(iterator, &handler->conf->network) { | |
1265 | netdev = iterator->elem; | |
1266 | if (netdev->type != LXC_NET_VETH) | |
1267 | continue; | |
1268 | if (write(netpipepair[1], netdev->name, IFNAMSIZ) != IFNAMSIZ) { | |
408da065 | 1269 | ERROR("Error writing veth name to container."); |
658979c5 SH |
1270 | goto out_delete_net; |
1271 | } | |
1272 | } | |
1273 | close(netpipepair[1]); | |
1274 | } | |
1275 | ||
408da065 CB |
1276 | /* Tell the child to continue its initialization. We'll get |
1277 | * LXC_SYNC_CGROUP when it is ready for us to setup cgroups. | |
3c22086f CLG |
1278 | */ |
1279 | if (lxc_sync_barrier_child(handler, LXC_SYNC_POST_CONFIGURE)) | |
544a48a0 SH |
1280 | goto out_delete_net; |
1281 | ||
c6d09e15 WB |
1282 | if (!lxc_list_empty(&handler->conf->limits) && setup_resource_limits(&handler->conf->limits, handler->pid)) { |
1283 | ERROR("failed to setup resource limits for '%s'", name); | |
84ff3af7 | 1284 | goto out_delete_net; |
c6d09e15 WB |
1285 | } |
1286 | ||
9daf6f5d | 1287 | if (!cgroup_setup_limits(handler, true)) { |
408da065 | 1288 | ERROR("Failed to setup the devices cgroup for container \"%s\".", name); |
b98f7d6e | 1289 | goto out_delete_net; |
544a48a0 SH |
1290 | } |
1291 | ||
73d28d42 | 1292 | cgroup_disconnect(); |
7e4dfe0b | 1293 | cgroups_connected = false; |
73d28d42 | 1294 | |
408da065 | 1295 | /* Read tty fds allocated by child. */ |
ae467c54 | 1296 | if (lxc_recv_ttys_from_child(handler) < 0) { |
408da065 | 1297 | ERROR("Failed to receive tty info from child process."); |
e8bd4e43 SH |
1298 | goto out_delete_net; |
1299 | } | |
1300 | ||
408da065 CB |
1301 | /* Tell the child to complete its initialization and wait for it to exec |
1302 | * or return an error. (The child will never return | |
1303 | * LXC_SYNC_POST_CGROUP+1. It will either close the sync pipe, causing | |
1304 | * lxc_sync_barrier_child to return success, or return a different | |
1305 | * value, causing us to error out). | |
544a48a0 SH |
1306 | */ |
1307 | if (lxc_sync_barrier_child(handler, LXC_SYNC_POST_CGROUP)) | |
3c22086f | 1308 | return -1; |
0ad19a3f | 1309 | |
23c53af9 | 1310 | if (handler->ops->post_start(handler, handler->data)) |
e6126dbe MN |
1311 | goto out_abort; |
1312 | ||
25c2aca5 | 1313 | if (lxc_set_state(name, handler, RUNNING)) { |
408da065 CB |
1314 | ERROR("Failed to set state for container \"%s\" to \"%s\".", name, |
1315 | lxc_state2str(RUNNING)); | |
59eb99ba | 1316 | goto out_abort; |
3f21c114 | 1317 | } |
22ebac19 | 1318 | |
3c22086f | 1319 | lxc_sync_fini(handler); |
738d0deb | 1320 | handler->netnsfd = lxc_preserve_ns(handler->pid, "net"); |
0c547523 | 1321 | |
e6126dbe | 1322 | return 0; |
1ac470c0 | 1323 | |
7fef7a06 | 1324 | out_delete_net: |
7e4dfe0b SH |
1325 | if (cgroups_connected) |
1326 | cgroup_disconnect(); | |
d5088cf2 | 1327 | if (handler->clone_flags & CLONE_NEWNET) |
74a2b586 | 1328 | lxc_delete_network(handler); |
59eb99ba DL |
1329 | out_abort: |
1330 | lxc_abort(name, handler); | |
3c22086f | 1331 | lxc_sync_fini(handler); |
5c068da9 SH |
1332 | if (handler->pinfd >= 0) { |
1333 | close(handler->pinfd); | |
1334 | handler->pinfd = -1; | |
1335 | } | |
1336 | ||
b79fcd86 | 1337 | return -1; |
59eb99ba | 1338 | } |
0ad19a3f | 1339 | |
ee70bf78 | 1340 | int __lxc_start(const char *name, struct lxc_conf *conf, |
507cee36 TA |
1341 | struct lxc_operations* ops, void *data, const char *lxcpath, |
1342 | bool backgrounded) | |
59eb99ba | 1343 | { |
3a0f472d | 1344 | struct lxc_handler *handler; |
e043236e | 1345 | int err = -1; |
59eb99ba | 1346 | int status; |
358daf49 | 1347 | bool removed_all_netdevs = true; |
80090207 | 1348 | |
13f5be62 | 1349 | handler = lxc_init(name, conf, lxcpath); |
3a0f472d | 1350 | if (!handler) { |
408da065 | 1351 | ERROR("Failed to initialize container \"%s\".", name); |
66aeffc7 | 1352 | return -1; |
0ad19a3f | 1353 | } |
ee70bf78 CLG |
1354 | handler->ops = ops; |
1355 | handler->data = data; | |
507cee36 | 1356 | handler->backgrounded = backgrounded; |
738d0deb | 1357 | handler->netnsfd = -1; |
e6126dbe | 1358 | |
b60ed720 | 1359 | if (must_drop_cap_sys_boot(handler->conf)) { |
e37dda71 | 1360 | #if HAVE_LIBCAP |
408da065 | 1361 | DEBUG("Dropping CAP_SYS_BOOT capability."); |
495d2046 | 1362 | #else |
408da065 | 1363 | DEBUG("Not dropping CAP_SYS_BOOT capability as capabilities aren't supported."); |
495d2046 | 1364 | #endif |
69182a31 | 1365 | } else { |
408da065 | 1366 | DEBUG("Not dropping CAP_SYS_BOOT or watching utmp."); |
69182a31 SH |
1367 | handler->conf->need_utmp_watch = 0; |
1368 | } | |
1369 | ||
76a26f55 | 1370 | if (!attach_block_device(handler->conf)) { |
408da065 | 1371 | ERROR("Failed to attach block device."); |
76a26f55 SH |
1372 | goto out_fini_nonet; |
1373 | } | |
1374 | ||
35120d9c | 1375 | if (geteuid() == 0 && !lxc_list_empty(&conf->id_map)) { |
408da065 | 1376 | /* If the backing store is a device, mount it here and now. */ |
35120d9c SH |
1377 | if (rootfs_is_blockdev(conf)) { |
1378 | if (unshare(CLONE_NEWNS) < 0) { | |
408da065 | 1379 | ERROR("Failed to unshare CLONE_NEWNS."); |
35120d9c SH |
1380 | goto out_fini_nonet; |
1381 | } | |
408da065 CB |
1382 | INFO("Unshared CLONE_NEWNS."); |
1383 | ||
6a0c909a | 1384 | remount_all_slave(); |
35120d9c | 1385 | if (do_rootfs_setup(conf, name, lxcpath) < 0) { |
408da065 | 1386 | ERROR("Error setting up rootfs mount as root before spawn."); |
35120d9c SH |
1387 | goto out_fini_nonet; |
1388 | } | |
408da065 | 1389 | INFO("Set up container rootfs as host root."); |
35120d9c SH |
1390 | } |
1391 | } | |
1392 | ||
23c53af9 | 1393 | err = lxc_spawn(handler); |
59eb99ba | 1394 | if (err) { |
408da065 | 1395 | ERROR("Failed to spawn container \"%s\".", name); |
76a26f55 | 1396 | goto out_detach_blockdev; |
0ad19a3f | 1397 | } |
1398 | ||
8bee8851 WB |
1399 | handler->conf->reboot = 0; |
1400 | ||
3a0f472d | 1401 | err = lxc_poll(name, handler); |
e043236e | 1402 | if (err) { |
408da065 | 1403 | ERROR("LXC mainloop exited with error: %d.", err); |
738d0deb CB |
1404 | if (handler->netnsfd >= 0) { |
1405 | close(handler->netnsfd); | |
1406 | handler->netnsfd = -1; | |
1407 | } | |
59eb99ba DL |
1408 | goto out_abort; |
1409 | } | |
0ad19a3f | 1410 | |
3a0f472d | 1411 | while (waitpid(handler->pid, &status, 0) < 0 && errno == EINTR) |
1bc5cc8c | 1412 | continue; |
e043236e | 1413 | |
408da065 CB |
1414 | /* If the child process exited but was not signaled, it didn't call |
1415 | * reboot. This should mean it was an lxc-execute which simply exited. | |
1416 | * In any case, treat it as a 'halt'. | |
8b004f07 | 1417 | */ |
d028235d | 1418 | if (WIFSIGNALED(status)) { |
8b004f07 SH |
1419 | switch(WTERMSIG(status)) { |
1420 | case SIGINT: /* halt */ | |
408da065 | 1421 | DEBUG("Container \"%s\" is halting.", name); |
8b004f07 SH |
1422 | break; |
1423 | case SIGHUP: /* reboot */ | |
408da065 | 1424 | DEBUG("Container \"%s\" is rebooting.", name); |
8b004f07 SH |
1425 | handler->conf->reboot = 1; |
1426 | break; | |
c2b9bd9e | 1427 | case SIGSYS: /* seccomp */ |
408da065 | 1428 | DEBUG("Container \"%s\" violated its seccomp policy.", name); |
c2b9bd9e | 1429 | break; |
8b004f07 | 1430 | default: |
408da065 | 1431 | DEBUG("Unknown exit status for container \"%s\" init %d.", name, WTERMSIG(status)); |
8b004f07 SH |
1432 | break; |
1433 | } | |
d028235d | 1434 | } |
828695d9 | 1435 | |
ce5782df | 1436 | DEBUG("Pushing physical nics back to host namespace"); |
738d0deb | 1437 | lxc_restore_phys_nics_to_netns(handler->netnsfd, handler->conf); |
ce5782df | 1438 | |
408da065 | 1439 | DEBUG("Tearing down virtual network devices used by container \"%s\".", name); |
358daf49 | 1440 | removed_all_netdevs = lxc_delete_network(handler); |
ce5782df | 1441 | |
5c068da9 SH |
1442 | if (handler->pinfd >= 0) { |
1443 | close(handler->pinfd); | |
1444 | handler->pinfd = -1; | |
1445 | } | |
1446 | ||
1787abca | 1447 | lxc_monitor_send_exit_code(name, status, handler->lxcpath); |
3a0f472d | 1448 | err = lxc_error_set_and_log(handler->pid, status); |
9d7f9e52 | 1449 | out_fini: |
358daf49 | 1450 | if (!removed_all_netdevs) { |
408da065 | 1451 | DEBUG("Failed tearing down network devices used by container. Trying again!"); |
358daf49 CB |
1452 | removed_all_netdevs = lxc_delete_network(handler); |
1453 | if (!removed_all_netdevs) | |
1454 | DEBUG("Failed tearing down network devices used by container. Not trying again!"); | |
1455 | } | |
74a2b586 | 1456 | |
76a26f55 SH |
1457 | out_detach_blockdev: |
1458 | detach_block_device(handler->conf); | |
1459 | ||
74a2b586 | 1460 | out_fini_nonet: |
3a0f472d | 1461 | lxc_fini(name, handler); |
0ad19a3f | 1462 | return err; |
1463 | ||
59eb99ba | 1464 | out_abort: |
3a0f472d | 1465 | lxc_abort(name, handler); |
9d7f9e52 | 1466 | goto out_fini; |
0ad19a3f | 1467 | } |
ee70bf78 CLG |
1468 | |
1469 | struct start_args { | |
1470 | char *const *argv; | |
1471 | }; | |
1472 | ||
1473 | static int start(struct lxc_handler *handler, void* data) | |
1474 | { | |
1475 | struct start_args *arg = data; | |
1476 | ||
408da065 | 1477 | NOTICE("Exec'ing \"%s\".", arg->argv[0]); |
ee70bf78 CLG |
1478 | |
1479 | execvp(arg->argv[0], arg->argv); | |
408da065 | 1480 | SYSERROR("Failed to exec \"%s\".", arg->argv[0]); |
ee70bf78 CLG |
1481 | return 0; |
1482 | } | |
1483 | ||
1484 | static int post_start(struct lxc_handler *handler, void* data) | |
1485 | { | |
1486 | struct start_args *arg = data; | |
1487 | ||
408da065 | 1488 | NOTICE("Started \"%s\" with pid \"%d\".", arg->argv[0], handler->pid); |
ee70bf78 CLG |
1489 | return 0; |
1490 | } | |
1491 | ||
1492 | static struct lxc_operations start_ops = { | |
1493 | .start = start, | |
1494 | .post_start = post_start | |
1495 | }; | |
1496 | ||
13f5be62 | 1497 | int lxc_start(const char *name, char *const argv[], struct lxc_conf *conf, |
507cee36 | 1498 | const char *lxcpath, bool backgrounded) |
ee70bf78 CLG |
1499 | { |
1500 | struct start_args start_arg = { | |
1501 | .argv = argv, | |
1502 | }; | |
1503 | ||
828695d9 | 1504 | conf->need_utmp_watch = 1; |
507cee36 | 1505 | return __lxc_start(name, conf, &start_ops, &start_arg, lxcpath, backgrounded); |
ee70bf78 | 1506 | } |
28272964 CB |
1507 | |
1508 | static void lxc_destroy_container_on_signal(struct lxc_handler *handler, | |
1509 | const char *name) | |
1510 | { | |
1511 | char destroy[MAXPATHLEN]; | |
1512 | bool bret = true; | |
1513 | int ret = 0; | |
f01f7975 | 1514 | struct lxc_container *c; |
df31363a | 1515 | if (handler->conf->rootfs.path && handler->conf->rootfs.mount) { |
28272964 CB |
1516 | bret = do_destroy_container(handler->conf); |
1517 | if (!bret) { | |
408da065 | 1518 | ERROR("Error destroying rootfs for container \"%s\".", name); |
28272964 CB |
1519 | return; |
1520 | } | |
1521 | } | |
408da065 | 1522 | INFO("Destroyed rootfs for container \"%s\".", name); |
28272964 CB |
1523 | |
1524 | ret = snprintf(destroy, MAXPATHLEN, "%s/%s", handler->lxcpath, name); | |
1525 | if (ret < 0 || ret >= MAXPATHLEN) { | |
408da065 | 1526 | ERROR("Error destroying directory for container \"%s\".", name); |
28272964 CB |
1527 | return; |
1528 | } | |
1529 | ||
f01f7975 CB |
1530 | c = lxc_container_new(name, handler->lxcpath); |
1531 | if (c) { | |
1532 | if (container_disk_lock(c)) { | |
408da065 | 1533 | INFO("Could not update lxc_snapshots file."); |
f01f7975 CB |
1534 | lxc_container_put(c); |
1535 | } else { | |
1536 | mod_all_rdeps(c, false); | |
1537 | container_disk_unlock(c); | |
1538 | lxc_container_put(c); | |
1539 | } | |
1540 | } | |
1541 | ||
28272964 | 1542 | if (am_unpriv()) |
c9b7c33e CB |
1543 | ret = userns_exec_1(handler->conf, lxc_rmdir_onedev_wrapper, |
1544 | destroy, "lxc_rmdir_onedev_wrapper"); | |
28272964 CB |
1545 | else |
1546 | ret = lxc_rmdir_onedev(destroy, NULL); | |
1547 | ||
1548 | if (ret < 0) { | |
408da065 | 1549 | ERROR("Error destroying directory for container \"%s\".", name); |
28272964 CB |
1550 | return; |
1551 | } | |
408da065 | 1552 | INFO("Destroyed directory for container \"%s\".", name); |
28272964 CB |
1553 | } |
1554 | ||
1555 | static int lxc_rmdir_onedev_wrapper(void *data) | |
1556 | { | |
1557 | char *arg = (char *) data; | |
1558 | return lxc_rmdir_onedev(arg, NULL); | |
1559 | } | |
1560 | ||
1561 | static bool do_destroy_container(struct lxc_conf *conf) { | |
d028235d | 1562 | if (am_unpriv()) { |
c9b7c33e CB |
1563 | if (userns_exec_1(conf, bdev_destroy_wrapper, conf, |
1564 | "bdev_destroy_wrapper") < 0) | |
d028235d SG |
1565 | return false; |
1566 | return true; | |
1567 | } | |
1568 | return bdev_destroy(conf); | |
28272964 | 1569 | } |