]>
Commit | Line | Data |
---|---|---|
0ad19a3f | 1 | /* |
2 | * lxc: linux Container library | |
3 | * | |
4 | * (C) Copyright IBM Corp. 2007, 2008 | |
5 | * | |
6 | * Authors: | |
7 | * Daniel Lezcano <dlezcano at fr.ibm.com> | |
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 | |
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
22 | */ | |
23 | ||
f549edcc GK |
24 | #include "config.h" |
25 | ||
0ad19a3f | 26 | #include <stdio.h> |
27 | #undef _GNU_SOURCE | |
28 | #include <string.h> | |
29 | #include <stdlib.h> | |
30 | #include <dirent.h> | |
31 | #include <errno.h> | |
32 | #include <unistd.h> | |
33 | #include <signal.h> | |
b0a33c1e | 34 | #include <fcntl.h> |
35 | #include <termios.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> |
0ad19a3f | 41 | #include <sys/prctl.h> |
ddceb1f9 | 42 | #include <sys/types.h> |
42ff343d | 43 | #include <sys/capability.h> |
0ad19a3f | 44 | #include <sys/wait.h> |
b0a33c1e | 45 | #include <sys/un.h> |
46 | #include <sys/poll.h> | |
ff218c25 | 47 | |
15cd25fd | 48 | #ifdef HAVE_SYS_SIGNALFD_H |
8ca61733 | 49 | # include <sys/signalfd.h> |
ff218c25 | 50 | #else |
15cd25fd | 51 | /* assume kernel headers are too old */ |
09d1bd23 | 52 | #include <stdint.h> |
15cd25fd DL |
53 | struct signalfd_siginfo |
54 | { | |
da2aef7f DL |
55 | uint32_t ssi_signo; |
56 | int32_t ssi_errno; | |
57 | int32_t ssi_code; | |
58 | uint32_t ssi_pid; | |
59 | uint32_t ssi_uid; | |
60 | int32_t ssi_fd; | |
61 | uint32_t ssi_tid; | |
62 | uint32_t ssi_band; | |
63 | uint32_t ssi_overrun; | |
64 | uint32_t ssi_trapno; | |
65 | int32_t ssi_status; | |
66 | int32_t ssi_int; | |
67 | uint64_t ssi_ptr; | |
68 | uint64_t ssi_utime; | |
69 | uint64_t ssi_stime; | |
70 | uint64_t ssi_addr; | |
71 | uint8_t __pad[48]; | |
15cd25fd DL |
72 | }; |
73 | ||
8ca61733 MJ |
74 | # ifndef __NR_signalfd4 |
75 | /* assume kernel headers are too old */ | |
76 | # if __i386__ | |
77 | # define __NR_signalfd4 327 | |
78 | # elif __x86_64__ | |
79 | # define __NR_signalfd4 289 | |
bfa38025 MH |
80 | # elif __powerpc__ |
81 | # define __NR_signalfd4 313 | |
47f38330 SH |
82 | # elif __s390x__ |
83 | # define __NR_signalfd4 322 | |
8ca61733 MJ |
84 | # endif |
85 | #endif | |
86 | ||
87 | # ifndef __NR_signalfd | |
88 | /* assume kernel headers are too old */ | |
89 | # if __i386__ | |
90 | # define __NR_signalfd 321 | |
91 | # elif __x86_64__ | |
92 | # define __NR_signalfd 282 | |
bfa38025 MH |
93 | # elif __powerpc__ |
94 | # define __NR_signalfd 305 | |
47f38330 SH |
95 | # elif __s390x__ |
96 | # define __NR_signalfd 316 | |
8ca61733 MJ |
97 | # endif |
98 | #endif | |
99 | ||
100 | int signalfd(int fd, const sigset_t *mask, int flags) | |
101 | { | |
102 | int retval; | |
103 | ||
104 | retval = syscall (__NR_signalfd4, fd, mask, _NSIG / 8, flags); | |
105 | if (errno == ENOSYS && flags == 0) | |
106 | retval = syscall (__NR_signalfd, fd, mask, _NSIG / 8); | |
107 | return retval; | |
108 | } | |
ff218c25 | 109 | #endif |
0ad19a3f | 110 | |
656994bb MH |
111 | #if !HAVE_DECL_PR_CAPBSET_DROP |
112 | #define PR_CAPBSET_DROP 24 | |
113 | #endif | |
114 | ||
63376d7d DL |
115 | #include "start.h" |
116 | #include "conf.h" | |
117 | #include "log.h" | |
563f2f2c | 118 | #include "cgroup.h" |
e2bcd7db | 119 | #include "error.h" |
b0a33c1e | 120 | #include "af_unix.h" |
121 | #include "mainloop.h" | |
63376d7d | 122 | #include "utils.h" |
563f2f2c | 123 | #include "utmp.h" |
63376d7d | 124 | #include "monitor.h" |
96fa1ff0 | 125 | #include "commands.h" |
63376d7d | 126 | #include "console.h" |
3c22086f | 127 | #include "sync.h" |
f549edcc | 128 | #include "namespace.h" |
e075f5d9 | 129 | #include "apparmor.h" |
0d0527a9 | 130 | #include "lxcseccomp.h" |
36eb9bde CLG |
131 | |
132 | lxc_log_define(lxc_start, lxc); | |
133 | ||
80090207 CLG |
134 | static int match_fd(int fd) |
135 | { | |
136 | return (fd == 0 || fd == 1 || fd == 2); | |
137 | } | |
138 | ||
b119f362 | 139 | int lxc_check_inherited(struct lxc_conf *conf, int fd_to_ignore) |
80090207 CLG |
140 | { |
141 | struct dirent dirent, *direntp; | |
142 | int fd, fddir; | |
143 | DIR *dir; | |
80090207 | 144 | |
b119f362 | 145 | restart: |
80090207 CLG |
146 | dir = opendir("/proc/self/fd"); |
147 | if (!dir) { | |
148 | WARN("failed to open directory: %m"); | |
149 | return -1; | |
150 | } | |
151 | ||
152 | fddir = dirfd(dir); | |
153 | ||
154 | while (!readdir_r(dir, &dirent, &direntp)) { | |
80090207 CLG |
155 | if (!direntp) |
156 | break; | |
157 | ||
158 | if (!strcmp(direntp->d_name, ".")) | |
159 | continue; | |
160 | ||
161 | if (!strcmp(direntp->d_name, "..")) | |
162 | continue; | |
163 | ||
164 | fd = atoi(direntp->d_name); | |
165 | ||
f2faa8fa | 166 | if (fd == fddir || fd == lxc_log_fd || fd == fd_to_ignore) |
80090207 CLG |
167 | continue; |
168 | ||
169 | if (match_fd(fd)) | |
170 | continue; | |
80090207 | 171 | |
b119f362 SH |
172 | if (conf->close_all_fds) { |
173 | close(fd); | |
174 | closedir(dir); | |
175 | INFO("closed inherited fd %d", fd); | |
176 | goto restart; | |
177 | } | |
92c7f629 | 178 | WARN("inherited fd %d", fd); |
80090207 CLG |
179 | } |
180 | ||
92c7f629 GK |
181 | closedir(dir); /* cannot fail */ |
182 | return 0; | |
80090207 CLG |
183 | } |
184 | ||
83ee7875 | 185 | static int setup_signal_fd(sigset_t *oldmask) |
b0a33c1e | 186 | { |
187 | sigset_t mask; | |
188 | int fd; | |
189 | ||
f3304a29 FW |
190 | /* Block everything except serious error signals */ |
191 | if (sigfillset(&mask) || | |
192 | sigdelset(&mask, SIGILL) || | |
193 | sigdelset(&mask, SIGSEGV) || | |
194 | sigdelset(&mask, SIGBUS) || | |
195 | sigprocmask(SIG_BLOCK, &mask, oldmask)) { | |
196 | SYSERROR("failed to set signal mask"); | |
b0a33c1e | 197 | return -1; |
198 | } | |
199 | ||
200 | fd = signalfd(-1, &mask, 0); | |
201 | if (fd < 0) { | |
36eb9bde | 202 | SYSERROR("failed to create the signal fd"); |
b0a33c1e | 203 | return -1; |
204 | } | |
205 | ||
206 | if (fcntl(fd, F_SETFD, FD_CLOEXEC)) { | |
36eb9bde | 207 | SYSERROR("failed to set sigfd to close-on-exec"); |
b0a33c1e | 208 | close(fd); |
209 | return -1; | |
210 | } | |
211 | ||
1ac470c0 DL |
212 | DEBUG("sigchild handler set"); |
213 | ||
b0a33c1e | 214 | return fd; |
215 | } | |
216 | ||
83ee7875 | 217 | static int signal_handler(int fd, void *data, |
b0a33c1e | 218 | struct lxc_epoll_descr *descr) |
219 | { | |
15cd25fd DL |
220 | struct signalfd_siginfo siginfo; |
221 | int ret; | |
82d89dce | 222 | pid_t *pid = data; |
15cd25fd DL |
223 | |
224 | ret = read(fd, &siginfo, sizeof(siginfo)); | |
225 | if (ret < 0) { | |
f3304a29 | 226 | ERROR("failed to read signal info"); |
15cd25fd DL |
227 | return -1; |
228 | } | |
229 | ||
230 | if (ret != sizeof(siginfo)) { | |
231 | ERROR("unexpected siginfo size"); | |
232 | return -1; | |
233 | } | |
234 | ||
f3304a29 FW |
235 | if (siginfo.ssi_signo != SIGCHLD) { |
236 | kill(*pid, siginfo.ssi_signo); | |
237 | INFO("forwarded signal %d to pid %d", siginfo.ssi_signo, *pid); | |
238 | return 0; | |
239 | } | |
240 | ||
15cd25fd DL |
241 | if (siginfo.ssi_code == CLD_STOPPED || |
242 | siginfo.ssi_code == CLD_CONTINUED) { | |
243 | INFO("container init process was stopped/continued"); | |
244 | return 0; | |
245 | } | |
1ac470c0 | 246 | |
82d89dce DL |
247 | /* more robustness, protect ourself from a SIGCHLD sent |
248 | * by a process different from the container init | |
249 | */ | |
250 | if (siginfo.ssi_pid != *pid) { | |
251 | WARN("invalid pid for SIGCHLD"); | |
252 | return 0; | |
253 | } | |
254 | ||
15cd25fd | 255 | DEBUG("container init process exited"); |
b0a33c1e | 256 | return 1; |
257 | } | |
258 | ||
0a3ec350 DL |
259 | int lxc_pid_callback(int fd, struct lxc_request *request, |
260 | struct lxc_handler *handler) | |
81c75799 DL |
261 | { |
262 | struct lxc_answer answer; | |
263 | int ret; | |
264 | ||
265 | answer.pid = handler->pid; | |
266 | answer.ret = 0; | |
267 | ||
268 | ret = send(fd, &answer, sizeof(answer), 0); | |
269 | if (ret < 0) { | |
270 | WARN("failed to send answer to the peer"); | |
271 | return -1; | |
272 | } | |
273 | ||
274 | if (ret != sizeof(answer)) { | |
275 | ERROR("partial answer sent"); | |
276 | return -1; | |
277 | } | |
278 | ||
279 | return 0; | |
280 | } | |
281 | ||
d5088cf2 CS |
282 | int lxc_clone_flags_callback(int fd, struct lxc_request *request, |
283 | struct lxc_handler *handler) | |
284 | { | |
285 | struct lxc_answer answer; | |
286 | int ret; | |
287 | ||
288 | answer.pid = 0; | |
289 | answer.ret = handler->clone_flags; | |
290 | ||
291 | ret = send(fd, &answer, sizeof(answer), 0); | |
292 | if (ret < 0) { | |
293 | WARN("failed to send answer to the peer"); | |
294 | return -1; | |
295 | } | |
296 | ||
297 | if (ret != sizeof(answer)) { | |
298 | ERROR("partial answer sent"); | |
299 | return -1; | |
300 | } | |
301 | ||
302 | return 0; | |
303 | } | |
304 | ||
25c2aca5 | 305 | int lxc_set_state(const char *name, struct lxc_handler *handler, lxc_state_t state) |
66aeffc7 DL |
306 | { |
307 | handler->state = state; | |
308 | lxc_monitor_send_state(name, state); | |
309 | return 0; | |
310 | } | |
311 | ||
1bc5cc8c | 312 | int lxc_poll(const char *name, struct lxc_handler *handler) |
b0a33c1e | 313 | { |
ca5f7926 DL |
314 | int sigfd = handler->sigfd; |
315 | int pid = handler->pid; | |
b0a33c1e | 316 | struct lxc_epoll_descr descr; |
317 | ||
a9e61274 | 318 | if (lxc_mainloop_open(&descr)) { |
36eb9bde | 319 | ERROR("failed to create mainloop"); |
50c8bf05 | 320 | goto out_sigfd; |
b0a33c1e | 321 | } |
322 | ||
83ee7875 | 323 | if (lxc_mainloop_add_handler(&descr, sigfd, signal_handler, &pid)) { |
36eb9bde | 324 | ERROR("failed to add handler for the signal"); |
b0a33c1e | 325 | goto out_mainloop_open; |
326 | } | |
327 | ||
63376d7d DL |
328 | if (lxc_console_mainloop_add(&descr, handler)) { |
329 | ERROR("failed to add console handler to mainloop"); | |
330 | goto out_mainloop_open; | |
331 | } | |
332 | ||
563f2f2c DL |
333 | if (lxc_command_mainloop_add(name, &descr, handler)) { |
334 | ERROR("failed to add command handler to mainloop"); | |
96fa1ff0 | 335 | goto out_mainloop_open; |
563f2f2c DL |
336 | } |
337 | ||
828695d9 SH |
338 | if (handler->conf->need_utmp_watch) { |
339 | if (lxc_utmp_mainloop_add(&descr, handler)) { | |
340 | ERROR("failed to add utmp handler to mainloop"); | |
341 | goto out_mainloop_open; | |
342 | } | |
563f2f2c | 343 | } |
b0a33c1e | 344 | |
c3e13372 | 345 | return lxc_mainloop(&descr); |
b0a33c1e | 346 | |
347 | out_mainloop_open: | |
348 | lxc_mainloop_close(&descr); | |
b0a33c1e | 349 | out_sigfd: |
350 | close(sigfd); | |
c3e13372 | 351 | return -1; |
b0a33c1e | 352 | } |
353 | ||
4a2ca8b2 SH |
354 | extern int lxc_caps_check(void); |
355 | ||
fae349da | 356 | struct lxc_handler *lxc_init(const char *name, struct lxc_conf *conf) |
59eb99ba | 357 | { |
3a0f472d DL |
358 | struct lxc_handler *handler; |
359 | ||
4a2ca8b2 SH |
360 | if (!lxc_caps_check()) { |
361 | ERROR("Not running with sufficient privilege"); | |
362 | return NULL; | |
363 | } | |
364 | ||
3a0f472d DL |
365 | handler = malloc(sizeof(*handler)); |
366 | if (!handler) | |
367 | return NULL; | |
59eb99ba DL |
368 | |
369 | memset(handler, 0, sizeof(*handler)); | |
370 | ||
fae349da DL |
371 | handler->conf = conf; |
372 | ||
e075f5d9 | 373 | apparmor_handler_init(handler); |
3bdf52d7 DL |
374 | handler->name = strdup(name); |
375 | if (!handler->name) { | |
376 | ERROR("failed to allocate memory"); | |
377 | goto out_free; | |
378 | } | |
379 | ||
8f2c3a70 SH |
380 | if (lxc_read_seccomp_config(conf) != 0) { |
381 | ERROR("failed loading seccomp policy"); | |
382 | goto out_free_name; | |
383 | } | |
384 | ||
0ad19a3f | 385 | /* Begin the set the state to STARTING*/ |
25c2aca5 | 386 | if (lxc_set_state(name, handler, STARTING)) { |
59eb99ba | 387 | ERROR("failed to set state '%s'", lxc_state2str(STARTING)); |
3bdf52d7 | 388 | goto out_free_name; |
0ad19a3f | 389 | } |
390 | ||
773fb9ca SH |
391 | if (run_lxc_hooks(name, "pre-start", conf)) { |
392 | ERROR("failed to run pre-start hooks for container '%s'.", name); | |
393 | goto out_aborting; | |
394 | } | |
26ddeedd | 395 | |
fae349da | 396 | if (lxc_create_tty(name, conf)) { |
36eb9bde | 397 | ERROR("failed to create the ttys"); |
59eb99ba | 398 | goto out_aborting; |
b0a33c1e | 399 | } |
400 | ||
1560f6c9 | 401 | if (lxc_create_console(conf)) { |
63376d7d DL |
402 | ERROR("failed to create console"); |
403 | goto out_delete_tty; | |
404 | } | |
405 | ||
b0a33c1e | 406 | /* the signal fd has to be created before forking otherwise |
407 | * if the child process exits before we setup the signal fd, | |
408 | * the event will be lost and the command will be stuck */ | |
83ee7875 | 409 | handler->sigfd = setup_signal_fd(&handler->oldmask); |
59eb99ba | 410 | if (handler->sigfd < 0) { |
36eb9bde | 411 | ERROR("failed to set sigchild fd handler"); |
63376d7d | 412 | goto out_delete_console; |
b0a33c1e | 413 | } |
414 | ||
c3e13372 | 415 | INFO("'%s' is initialized", name); |
3a0f472d | 416 | return handler; |
59eb99ba | 417 | |
63376d7d DL |
418 | out_delete_console: |
419 | lxc_delete_console(&conf->console); | |
59eb99ba | 420 | out_delete_tty: |
fae349da | 421 | lxc_delete_tty(&conf->tty_info); |
59eb99ba | 422 | out_aborting: |
25c2aca5 | 423 | lxc_set_state(name, handler, ABORTING); |
3bdf52d7 DL |
424 | out_free_name: |
425 | free(handler->name); | |
426 | handler->name = NULL; | |
3a0f472d DL |
427 | out_free: |
428 | free(handler); | |
c3e13372 | 429 | return NULL; |
59eb99ba DL |
430 | } |
431 | ||
1bc5cc8c | 432 | void lxc_fini(const char *name, struct lxc_handler *handler) |
59eb99ba DL |
433 | { |
434 | /* The STOPPING state is there for future cleanup code | |
435 | * which can take awhile | |
436 | */ | |
25c2aca5 MN |
437 | lxc_set_state(name, handler, STOPPING); |
438 | lxc_set_state(name, handler, STOPPED); | |
59eb99ba | 439 | |
773fb9ca SH |
440 | if (run_lxc_hooks(name, "post-stop", handler->conf)) |
441 | ERROR("failed to run post-stop hooks for container '%s'.", name); | |
26ddeedd | 442 | |
83ee7875 | 443 | /* reset mask set by setup_signal_fd */ |
8f64a3f6 MN |
444 | if (sigprocmask(SIG_SETMASK, &handler->oldmask, NULL)) |
445 | WARN("failed to restore sigprocmask"); | |
446 | ||
63376d7d | 447 | lxc_delete_console(&handler->conf->console); |
b2431939 | 448 | lxc_delete_tty(&handler->conf->tty_info); |
3bdf52d7 | 449 | free(handler->name); |
b2431939 | 450 | free(handler); |
59eb99ba DL |
451 | } |
452 | ||
1bc5cc8c | 453 | void lxc_abort(const char *name, struct lxc_handler *handler) |
59eb99ba | 454 | { |
25c2aca5 | 455 | lxc_set_state(name, handler, ABORTING); |
7d9fb3e9 DL |
456 | if (handler->pid > 0) |
457 | kill(handler->pid, SIGKILL); | |
59eb99ba DL |
458 | } |
459 | ||
828695d9 SH |
460 | #include <sys/reboot.h> |
461 | #include <linux/reboot.h> | |
462 | ||
e2fa1520 SH |
463 | /* |
464 | * reboot(LINUX_REBOOT_CMD_CAD_ON) will return -EINVAL | |
465 | * in a child pid namespace if container reboot support exists. | |
466 | * Otherwise, it will either succeed or return -EPERM. | |
467 | */ | |
468 | static int container_reboot_supported(void *arg) | |
828695d9 | 469 | { |
e2fa1520 | 470 | int *cmd = arg; |
828695d9 | 471 | int ret; |
828695d9 | 472 | |
e2fa1520 SH |
473 | ret = reboot(*cmd); |
474 | if (ret == -1 && errno == EINVAL) | |
475 | return 1; | |
476 | return 0; | |
477 | } | |
478 | ||
479 | static int must_drop_cap_sys_boot(void) | |
480 | { | |
481 | FILE *f = fopen("/proc/sys/kernel/ctrl-alt-del", "r"); | |
482 | int ret, cmd, v; | |
483 | long stack_size = 4096; | |
484 | void *stack = alloca(stack_size) + stack_size; | |
485 | int status; | |
486 | pid_t pid; | |
487 | ||
488 | if (!f) { | |
489 | DEBUG("failed to open /proc/sys/kernel/ctrl-alt-del"); | |
828695d9 | 490 | return 1; |
e2fa1520 | 491 | } |
828695d9 SH |
492 | |
493 | ret = fscanf(f, "%d", &v); | |
494 | fclose(f); | |
e2fa1520 SH |
495 | if (ret != 1) { |
496 | DEBUG("Failed to read /proc/sys/kernel/ctrl-alt-del"); | |
828695d9 | 497 | return 1; |
e2fa1520 SH |
498 | } |
499 | cmd = v ? LINUX_REBOOT_CMD_CAD_ON : LINUX_REBOOT_CMD_CAD_OFF; | |
500 | ||
501 | pid = clone(container_reboot_supported, stack, CLONE_NEWPID | SIGCHLD, &cmd); | |
502 | if (pid < 0) { | |
503 | SYSERROR("failed to clone\n"); | |
504 | return -1; | |
505 | } | |
506 | if (wait(&status) < 0) { | |
507 | SYSERROR("unexpected wait error: %m\n"); | |
508 | return -1; | |
509 | } | |
510 | ||
511 | if (WEXITSTATUS(status) != 1) | |
828695d9 | 512 | return 1; |
e2fa1520 | 513 | |
828695d9 SH |
514 | return 0; |
515 | } | |
516 | ||
ffe1e01a | 517 | static int do_start(void *data) |
50e98013 | 518 | { |
23c53af9 | 519 | struct lxc_handler *handler = data; |
50e98013 DL |
520 | |
521 | if (sigprocmask(SIG_SETMASK, &handler->oldmask, NULL)) { | |
522 | SYSERROR("failed to set sigprocmask"); | |
9d7f9e52 | 523 | return -1; |
50e98013 DL |
524 | } |
525 | ||
743ecd2e DL |
526 | /* This prctl must be before the synchro, so if the parent |
527 | * dies before we set the parent death signal, we will detect | |
528 | * its death with the synchro right after, otherwise we have | |
529 | * a window where the parent can exit before we set the pdeath | |
530 | * signal leading to a unsupervized container. | |
531 | */ | |
532 | if (prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0)) { | |
533 | SYSERROR("failed to set pdeath signal"); | |
534 | return -1; | |
535 | } | |
536 | ||
3c22086f | 537 | lxc_sync_fini_parent(handler); |
50e98013 | 538 | |
3c22086f CLG |
539 | /* Tell the parent task it can begin to configure the |
540 | * container and wait for it to finish | |
541 | */ | |
542 | if (lxc_sync_barrier_parent(handler, LXC_SYNC_CONFIGURE)) | |
9d7f9e52 | 543 | return -1; |
50e98013 | 544 | |
69182a31 | 545 | if (handler->conf->need_utmp_watch) { |
828695d9 SH |
546 | if (prctl(PR_CAPBSET_DROP, CAP_SYS_BOOT, 0, 0, 0)) { |
547 | SYSERROR("failed to remove CAP_SYS_BOOT capability"); | |
548 | return -1; | |
549 | } | |
e2fa1520 | 550 | DEBUG("Dropped cap_sys_boot\n"); |
e2fa1520 SH |
551 | } |
552 | ||
553 | /* Setup the container, ip, names, utsname, ... */ | |
554 | if (lxc_setup(handler->name, handler->conf)) { | |
555 | ERROR("failed to setup the container"); | |
556 | goto out_warn_father; | |
557 | } | |
50e98013 | 558 | |
e075f5d9 SH |
559 | if (apparmor_load(handler) < 0) |
560 | goto out_warn_father; | |
561 | ||
8f2c3a70 SH |
562 | if (lxc_seccomp_load(handler->conf) != 0) |
563 | goto out_warn_father; | |
564 | ||
773fb9ca SH |
565 | if (run_lxc_hooks(handler->name, "start", handler->conf)) { |
566 | ERROR("failed to run start hooks for container '%s'.", handler->name); | |
567 | goto out_warn_father; | |
568 | } | |
fc25b815 | 569 | |
773fb9ca | 570 | close(handler->sigfd); |
26ddeedd | 571 | |
e6126dbe MN |
572 | /* after this call, we are in error because this |
573 | * ops should not return as it execs */ | |
23c53af9 | 574 | if (handler->ops->start(handler, handler->data)) |
e6126dbe | 575 | return -1; |
50e98013 DL |
576 | |
577 | out_warn_father: | |
3c22086f | 578 | lxc_sync_wake_parent(handler, LXC_SYNC_POST_CONFIGURE); |
50e98013 DL |
579 | return -1; |
580 | } | |
581 | ||
7b35f3d6 SH |
582 | int save_phys_nics(struct lxc_conf *conf) |
583 | { | |
584 | struct lxc_list *iterator; | |
585 | ||
586 | lxc_list_for_each(iterator, &conf->network) { | |
587 | struct lxc_netdev *netdev = iterator->elem; | |
588 | ||
589 | if (netdev->type != LXC_NET_PHYS) | |
590 | continue; | |
591 | conf->saved_nics = realloc(conf->saved_nics, | |
592 | (conf->num_savednics+1)*sizeof(struct saved_nic)); | |
593 | if (!conf->saved_nics) { | |
594 | SYSERROR("failed to allocate memory"); | |
595 | return -1; | |
596 | } | |
597 | conf->saved_nics[conf->num_savednics].ifindex = netdev->ifindex; | |
598 | conf->saved_nics[conf->num_savednics].orig_name = strdup(netdev->link); | |
599 | if (!conf->saved_nics[conf->num_savednics].orig_name) { | |
600 | SYSERROR("failed to allocate memory"); | |
601 | return -1; | |
602 | } | |
603 | INFO("stored saved_nic #%d idx %d name %s\n", conf->num_savednics, | |
604 | conf->saved_nics[conf->num_savednics].ifindex, | |
605 | conf->saved_nics[conf->num_savednics].orig_name); | |
606 | conf->num_savednics++; | |
607 | } | |
608 | ||
609 | return 0; | |
610 | } | |
611 | ||
612 | ||
23c53af9 | 613 | int lxc_spawn(struct lxc_handler *handler) |
59eb99ba | 614 | { |
99a6af52 | 615 | int failed_before_rename = 0; |
ffe1e01a | 616 | const char *name = handler->name; |
0c547523 | 617 | int pinfd; |
50e98013 | 618 | |
3c22086f | 619 | if (lxc_sync_init(handler)) |
9d7f9e52 | 620 | return -1; |
0ad19a3f | 621 | |
d5088cf2 | 622 | handler->clone_flags = CLONE_NEWUTS|CLONE_NEWPID|CLONE_NEWIPC|CLONE_NEWNS; |
fae349da | 623 | if (!lxc_list_empty(&handler->conf->network)) { |
82d5ae15 | 624 | |
d5088cf2 | 625 | handler->clone_flags |= CLONE_NEWNET; |
0ad19a3f | 626 | |
19a26f82 MK |
627 | /* Find gateway addresses from the link device, which is |
628 | * no longer accessible inside the container. Do this | |
629 | * before creating network interfaces, since goto | |
630 | * out_delete_net does not work before lxc_clone. */ | |
631 | if (lxc_find_gateway_addresses(handler)) { | |
632 | ERROR("failed to find gateway addresses"); | |
633 | lxc_sync_fini(handler); | |
634 | return -1; | |
635 | } | |
636 | ||
82d5ae15 DL |
637 | /* that should be done before the clone because we will |
638 | * fill the netdev index and use them in the child | |
639 | */ | |
e3b4c4c4 | 640 | if (lxc_create_network(handler)) { |
82d5ae15 | 641 | ERROR("failed to create the network"); |
3c22086f | 642 | lxc_sync_fini(handler); |
32e1c760 | 643 | return -1; |
82d5ae15 DL |
644 | } |
645 | } | |
646 | ||
7b35f3d6 SH |
647 | if (save_phys_nics(handler->conf)) { |
648 | ERROR("failed to save physical nic info"); | |
649 | goto out_abort; | |
650 | } | |
651 | ||
0c547523 SH |
652 | /* |
653 | * if the rootfs is not a blockdev, prevent the container from | |
654 | * marking it readonly. | |
655 | */ | |
656 | ||
657 | pinfd = pin_rootfs(handler->conf->rootfs.path); | |
658 | if (pinfd == -1) { | |
659 | ERROR("failed to pin the container's rootfs"); | |
660 | goto out_abort; | |
661 | } | |
662 | ||
0ad19a3f | 663 | /* Create a process in a new set of namespaces */ |
d5088cf2 | 664 | handler->pid = lxc_clone(do_start, handler, handler->clone_flags); |
59eb99ba | 665 | if (handler->pid < 0) { |
36eb9bde | 666 | SYSERROR("failed to fork into a new namespace"); |
7fef7a06 | 667 | goto out_delete_net; |
0ad19a3f | 668 | } |
669 | ||
3c22086f CLG |
670 | lxc_sync_fini_child(handler); |
671 | ||
672 | if (lxc_sync_wait_child(handler, LXC_SYNC_CONFIGURE)) | |
99a6af52 | 673 | failed_before_rename = 1; |
0ad19a3f | 674 | |
36b86299 | 675 | if (lxc_cgroup_create(name, handler->pid)) |
7fef7a06 | 676 | goto out_delete_net; |
218d4250 | 677 | |
99a6af52 MN |
678 | if (failed_before_rename) |
679 | goto out_delete_net; | |
680 | ||
0ad19a3f | 681 | /* Create the network configuration */ |
d5088cf2 | 682 | if (handler->clone_flags & CLONE_NEWNET) { |
fae349da | 683 | if (lxc_assign_network(&handler->conf->network, handler->pid)) { |
82d5ae15 | 684 | ERROR("failed to create the configured network"); |
7fef7a06 | 685 | goto out_delete_net; |
82d5ae15 | 686 | } |
0ad19a3f | 687 | } |
688 | ||
3c22086f CLG |
689 | /* Tell the child to continue its initialization and wait for |
690 | * it to exec or return an error | |
691 | */ | |
692 | if (lxc_sync_barrier_child(handler, LXC_SYNC_POST_CONFIGURE)) | |
693 | return -1; | |
0ad19a3f | 694 | |
23c53af9 | 695 | if (handler->ops->post_start(handler, handler->data)) |
e6126dbe MN |
696 | goto out_abort; |
697 | ||
25c2aca5 | 698 | if (lxc_set_state(name, handler, RUNNING)) { |
59eb99ba DL |
699 | ERROR("failed to set state to %s", |
700 | lxc_state2str(RUNNING)); | |
701 | goto out_abort; | |
3f21c114 | 702 | } |
22ebac19 | 703 | |
3c22086f | 704 | lxc_sync_fini(handler); |
0c547523 SH |
705 | |
706 | if (pinfd >= 0) | |
707 | close(pinfd); | |
708 | ||
e6126dbe | 709 | return 0; |
1ac470c0 | 710 | |
7fef7a06 | 711 | out_delete_net: |
d5088cf2 | 712 | if (handler->clone_flags & CLONE_NEWNET) |
74a2b586 | 713 | lxc_delete_network(handler); |
59eb99ba DL |
714 | out_abort: |
715 | lxc_abort(name, handler); | |
3c22086f | 716 | lxc_sync_fini(handler); |
b79fcd86 | 717 | return -1; |
59eb99ba | 718 | } |
0ad19a3f | 719 | |
ee70bf78 CLG |
720 | int __lxc_start(const char *name, struct lxc_conf *conf, |
721 | struct lxc_operations* ops, void *data) | |
59eb99ba | 722 | { |
3a0f472d | 723 | struct lxc_handler *handler; |
e043236e | 724 | int err = -1; |
59eb99ba | 725 | int status; |
80090207 | 726 | |
fae349da | 727 | handler = lxc_init(name, conf); |
3a0f472d | 728 | if (!handler) { |
59eb99ba | 729 | ERROR("failed to initialize the container"); |
66aeffc7 | 730 | return -1; |
0ad19a3f | 731 | } |
ee70bf78 CLG |
732 | handler->ops = ops; |
733 | handler->data = data; | |
e6126dbe | 734 | |
69182a31 | 735 | if (must_drop_cap_sys_boot()) { |
f51db2b3 | 736 | DEBUG("Dropping cap_sys_boot\n"); |
69182a31 SH |
737 | } else { |
738 | DEBUG("Not dropping cap_sys_boot or watching utmp\n"); | |
739 | handler->conf->need_utmp_watch = 0; | |
740 | } | |
741 | ||
23c53af9 | 742 | err = lxc_spawn(handler); |
59eb99ba | 743 | if (err) { |
ee70bf78 | 744 | ERROR("failed to spawn '%s'", name); |
74a2b586 | 745 | goto out_fini_nonet; |
0ad19a3f | 746 | } |
747 | ||
3a0f472d | 748 | err = lxc_poll(name, handler); |
e043236e | 749 | if (err) { |
59eb99ba DL |
750 | ERROR("mainloop exited with an error"); |
751 | goto out_abort; | |
752 | } | |
0ad19a3f | 753 | |
3a0f472d | 754 | while (waitpid(handler->pid, &status, 0) < 0 && errno == EINTR) |
1bc5cc8c | 755 | continue; |
e043236e | 756 | |
8b004f07 SH |
757 | /* |
758 | * If the child process exited but was not signaled, | |
759 | * it didn't call reboot. This should mean it was an | |
760 | * lxc-execute which simply exited. In any case, treat | |
761 | * it as a 'halt' | |
762 | */ | |
763 | if (WIFSIGNALED(status)) { | |
764 | switch(WTERMSIG(status)) { | |
765 | case SIGINT: /* halt */ | |
766 | DEBUG("Container halting"); | |
767 | break; | |
768 | case SIGHUP: /* reboot */ | |
769 | DEBUG("Container rebooting"); | |
770 | handler->conf->reboot = 1; | |
771 | break; | |
772 | default: | |
773 | DEBUG("unknown exit status for init: %d\n", WTERMSIG(status)); | |
774 | break; | |
775 | } | |
828695d9 SH |
776 | } |
777 | ||
7b35f3d6 SH |
778 | lxc_rename_phys_nics_on_shutdown(handler->conf); |
779 | ||
3a0f472d | 780 | err = lxc_error_set_and_log(handler->pid, status); |
9d7f9e52 | 781 | out_fini: |
74a2b586 JK |
782 | lxc_delete_network(handler); |
783 | ||
784 | out_fini_nonet: | |
36b86299 | 785 | lxc_cgroup_destroy(name); |
3a0f472d | 786 | lxc_fini(name, handler); |
0ad19a3f | 787 | return err; |
788 | ||
59eb99ba | 789 | out_abort: |
3a0f472d | 790 | lxc_abort(name, handler); |
9d7f9e52 | 791 | goto out_fini; |
0ad19a3f | 792 | } |
ee70bf78 CLG |
793 | |
794 | struct start_args { | |
795 | char *const *argv; | |
796 | }; | |
797 | ||
798 | static int start(struct lxc_handler *handler, void* data) | |
799 | { | |
800 | struct start_args *arg = data; | |
801 | ||
802 | NOTICE("exec'ing '%s'", arg->argv[0]); | |
803 | ||
804 | execvp(arg->argv[0], arg->argv); | |
805 | SYSERROR("failed to exec %s", arg->argv[0]); | |
806 | return 0; | |
807 | } | |
808 | ||
809 | static int post_start(struct lxc_handler *handler, void* data) | |
810 | { | |
811 | struct start_args *arg = data; | |
812 | ||
813 | NOTICE("'%s' started with pid '%d'", arg->argv[0], handler->pid); | |
814 | return 0; | |
815 | } | |
816 | ||
817 | static struct lxc_operations start_ops = { | |
818 | .start = start, | |
819 | .post_start = post_start | |
820 | }; | |
821 | ||
822 | int lxc_start(const char *name, char *const argv[], struct lxc_conf *conf) | |
823 | { | |
824 | struct start_args start_arg = { | |
825 | .argv = argv, | |
826 | }; | |
827 | ||
b119f362 | 828 | if (lxc_check_inherited(conf, -1)) |
ee70bf78 CLG |
829 | return -1; |
830 | ||
828695d9 | 831 | conf->need_utmp_watch = 1; |
ee70bf78 CLG |
832 | return __lxc_start(name, conf, &start_ops, &start_arg); |
833 | } |