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