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