]>
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 | ||
25c2aca5 | 282 | int lxc_set_state(const char *name, struct lxc_handler *handler, lxc_state_t state) |
66aeffc7 DL |
283 | { |
284 | handler->state = state; | |
285 | lxc_monitor_send_state(name, state); | |
286 | return 0; | |
287 | } | |
288 | ||
1bc5cc8c | 289 | int lxc_poll(const char *name, struct lxc_handler *handler) |
b0a33c1e | 290 | { |
ca5f7926 DL |
291 | int sigfd = handler->sigfd; |
292 | int pid = handler->pid; | |
b0a33c1e | 293 | struct lxc_epoll_descr descr; |
294 | ||
a9e61274 | 295 | if (lxc_mainloop_open(&descr)) { |
36eb9bde | 296 | ERROR("failed to create mainloop"); |
50c8bf05 | 297 | goto out_sigfd; |
b0a33c1e | 298 | } |
299 | ||
83ee7875 | 300 | if (lxc_mainloop_add_handler(&descr, sigfd, signal_handler, &pid)) { |
36eb9bde | 301 | ERROR("failed to add handler for the signal"); |
b0a33c1e | 302 | goto out_mainloop_open; |
303 | } | |
304 | ||
63376d7d DL |
305 | if (lxc_console_mainloop_add(&descr, handler)) { |
306 | ERROR("failed to add console handler to mainloop"); | |
307 | goto out_mainloop_open; | |
308 | } | |
309 | ||
563f2f2c DL |
310 | if (lxc_command_mainloop_add(name, &descr, handler)) { |
311 | ERROR("failed to add command handler to mainloop"); | |
96fa1ff0 | 312 | goto out_mainloop_open; |
563f2f2c DL |
313 | } |
314 | ||
828695d9 SH |
315 | if (handler->conf->need_utmp_watch) { |
316 | if (lxc_utmp_mainloop_add(&descr, handler)) { | |
317 | ERROR("failed to add utmp handler to mainloop"); | |
318 | goto out_mainloop_open; | |
319 | } | |
563f2f2c | 320 | } |
b0a33c1e | 321 | |
c3e13372 | 322 | return lxc_mainloop(&descr); |
b0a33c1e | 323 | |
324 | out_mainloop_open: | |
325 | lxc_mainloop_close(&descr); | |
b0a33c1e | 326 | out_sigfd: |
327 | close(sigfd); | |
c3e13372 | 328 | return -1; |
b0a33c1e | 329 | } |
330 | ||
4a2ca8b2 SH |
331 | extern int lxc_caps_check(void); |
332 | ||
fae349da | 333 | struct lxc_handler *lxc_init(const char *name, struct lxc_conf *conf) |
59eb99ba | 334 | { |
3a0f472d DL |
335 | struct lxc_handler *handler; |
336 | ||
4a2ca8b2 SH |
337 | if (!lxc_caps_check()) { |
338 | ERROR("Not running with sufficient privilege"); | |
339 | return NULL; | |
340 | } | |
341 | ||
3a0f472d DL |
342 | handler = malloc(sizeof(*handler)); |
343 | if (!handler) | |
344 | return NULL; | |
59eb99ba DL |
345 | |
346 | memset(handler, 0, sizeof(*handler)); | |
347 | ||
fae349da DL |
348 | handler->conf = conf; |
349 | ||
e075f5d9 | 350 | apparmor_handler_init(handler); |
3bdf52d7 DL |
351 | handler->name = strdup(name); |
352 | if (!handler->name) { | |
353 | ERROR("failed to allocate memory"); | |
354 | goto out_free; | |
355 | } | |
356 | ||
8f2c3a70 SH |
357 | if (lxc_read_seccomp_config(conf) != 0) { |
358 | ERROR("failed loading seccomp policy"); | |
359 | goto out_free_name; | |
360 | } | |
361 | ||
0ad19a3f | 362 | /* Begin the set the state to STARTING*/ |
25c2aca5 | 363 | if (lxc_set_state(name, handler, STARTING)) { |
59eb99ba | 364 | ERROR("failed to set state '%s'", lxc_state2str(STARTING)); |
3bdf52d7 | 365 | goto out_free_name; |
0ad19a3f | 366 | } |
367 | ||
773fb9ca SH |
368 | if (run_lxc_hooks(name, "pre-start", conf)) { |
369 | ERROR("failed to run pre-start hooks for container '%s'.", name); | |
370 | goto out_aborting; | |
371 | } | |
26ddeedd | 372 | |
fae349da | 373 | if (lxc_create_tty(name, conf)) { |
36eb9bde | 374 | ERROR("failed to create the ttys"); |
59eb99ba | 375 | goto out_aborting; |
b0a33c1e | 376 | } |
377 | ||
1560f6c9 | 378 | if (lxc_create_console(conf)) { |
63376d7d DL |
379 | ERROR("failed to create console"); |
380 | goto out_delete_tty; | |
381 | } | |
382 | ||
b0a33c1e | 383 | /* the signal fd has to be created before forking otherwise |
384 | * if the child process exits before we setup the signal fd, | |
385 | * the event will be lost and the command will be stuck */ | |
83ee7875 | 386 | handler->sigfd = setup_signal_fd(&handler->oldmask); |
59eb99ba | 387 | if (handler->sigfd < 0) { |
36eb9bde | 388 | ERROR("failed to set sigchild fd handler"); |
63376d7d | 389 | goto out_delete_console; |
b0a33c1e | 390 | } |
391 | ||
c3e13372 | 392 | INFO("'%s' is initialized", name); |
3a0f472d | 393 | return handler; |
59eb99ba | 394 | |
63376d7d DL |
395 | out_delete_console: |
396 | lxc_delete_console(&conf->console); | |
59eb99ba | 397 | out_delete_tty: |
fae349da | 398 | lxc_delete_tty(&conf->tty_info); |
59eb99ba | 399 | out_aborting: |
25c2aca5 | 400 | lxc_set_state(name, handler, ABORTING); |
3bdf52d7 DL |
401 | out_free_name: |
402 | free(handler->name); | |
403 | handler->name = NULL; | |
3a0f472d DL |
404 | out_free: |
405 | free(handler); | |
c3e13372 | 406 | return NULL; |
59eb99ba DL |
407 | } |
408 | ||
1bc5cc8c | 409 | void lxc_fini(const char *name, struct lxc_handler *handler) |
59eb99ba DL |
410 | { |
411 | /* The STOPPING state is there for future cleanup code | |
412 | * which can take awhile | |
413 | */ | |
25c2aca5 MN |
414 | lxc_set_state(name, handler, STOPPING); |
415 | lxc_set_state(name, handler, STOPPED); | |
59eb99ba | 416 | |
773fb9ca SH |
417 | if (run_lxc_hooks(name, "post-stop", handler->conf)) |
418 | ERROR("failed to run post-stop hooks for container '%s'.", name); | |
26ddeedd | 419 | |
83ee7875 | 420 | /* reset mask set by setup_signal_fd */ |
8f64a3f6 MN |
421 | if (sigprocmask(SIG_SETMASK, &handler->oldmask, NULL)) |
422 | WARN("failed to restore sigprocmask"); | |
423 | ||
63376d7d | 424 | lxc_delete_console(&handler->conf->console); |
b2431939 | 425 | lxc_delete_tty(&handler->conf->tty_info); |
3bdf52d7 | 426 | free(handler->name); |
b2431939 | 427 | free(handler); |
59eb99ba DL |
428 | } |
429 | ||
1bc5cc8c | 430 | void lxc_abort(const char *name, struct lxc_handler *handler) |
59eb99ba | 431 | { |
25c2aca5 | 432 | lxc_set_state(name, handler, ABORTING); |
7d9fb3e9 DL |
433 | if (handler->pid > 0) |
434 | kill(handler->pid, SIGKILL); | |
59eb99ba DL |
435 | } |
436 | ||
828695d9 SH |
437 | #include <sys/reboot.h> |
438 | #include <linux/reboot.h> | |
439 | ||
e2fa1520 SH |
440 | /* |
441 | * reboot(LINUX_REBOOT_CMD_CAD_ON) will return -EINVAL | |
442 | * in a child pid namespace if container reboot support exists. | |
443 | * Otherwise, it will either succeed or return -EPERM. | |
444 | */ | |
445 | static int container_reboot_supported(void *arg) | |
828695d9 | 446 | { |
e2fa1520 | 447 | int *cmd = arg; |
828695d9 | 448 | int ret; |
828695d9 | 449 | |
e2fa1520 SH |
450 | ret = reboot(*cmd); |
451 | if (ret == -1 && errno == EINVAL) | |
452 | return 1; | |
453 | return 0; | |
454 | } | |
455 | ||
456 | static int must_drop_cap_sys_boot(void) | |
457 | { | |
458 | FILE *f = fopen("/proc/sys/kernel/ctrl-alt-del", "r"); | |
459 | int ret, cmd, v; | |
460 | long stack_size = 4096; | |
461 | void *stack = alloca(stack_size) + stack_size; | |
462 | int status; | |
463 | pid_t pid; | |
464 | ||
465 | if (!f) { | |
466 | DEBUG("failed to open /proc/sys/kernel/ctrl-alt-del"); | |
828695d9 | 467 | return 1; |
e2fa1520 | 468 | } |
828695d9 SH |
469 | |
470 | ret = fscanf(f, "%d", &v); | |
471 | fclose(f); | |
e2fa1520 SH |
472 | if (ret != 1) { |
473 | DEBUG("Failed to read /proc/sys/kernel/ctrl-alt-del"); | |
828695d9 | 474 | return 1; |
e2fa1520 SH |
475 | } |
476 | cmd = v ? LINUX_REBOOT_CMD_CAD_ON : LINUX_REBOOT_CMD_CAD_OFF; | |
477 | ||
478 | pid = clone(container_reboot_supported, stack, CLONE_NEWPID | SIGCHLD, &cmd); | |
479 | if (pid < 0) { | |
480 | SYSERROR("failed to clone\n"); | |
481 | return -1; | |
482 | } | |
483 | if (wait(&status) < 0) { | |
484 | SYSERROR("unexpected wait error: %m\n"); | |
485 | return -1; | |
486 | } | |
487 | ||
488 | if (WEXITSTATUS(status) != 1) | |
828695d9 | 489 | return 1; |
e2fa1520 | 490 | |
828695d9 SH |
491 | return 0; |
492 | } | |
493 | ||
ffe1e01a | 494 | static int do_start(void *data) |
50e98013 | 495 | { |
23c53af9 | 496 | struct lxc_handler *handler = data; |
50e98013 DL |
497 | |
498 | if (sigprocmask(SIG_SETMASK, &handler->oldmask, NULL)) { | |
499 | SYSERROR("failed to set sigprocmask"); | |
9d7f9e52 | 500 | return -1; |
50e98013 DL |
501 | } |
502 | ||
743ecd2e DL |
503 | /* This prctl must be before the synchro, so if the parent |
504 | * dies before we set the parent death signal, we will detect | |
505 | * its death with the synchro right after, otherwise we have | |
506 | * a window where the parent can exit before we set the pdeath | |
507 | * signal leading to a unsupervized container. | |
508 | */ | |
509 | if (prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0)) { | |
510 | SYSERROR("failed to set pdeath signal"); | |
511 | return -1; | |
512 | } | |
513 | ||
3c22086f | 514 | lxc_sync_fini_parent(handler); |
50e98013 | 515 | |
3c22086f CLG |
516 | /* Tell the parent task it can begin to configure the |
517 | * container and wait for it to finish | |
518 | */ | |
519 | if (lxc_sync_barrier_parent(handler, LXC_SYNC_CONFIGURE)) | |
9d7f9e52 | 520 | return -1; |
50e98013 | 521 | |
69182a31 | 522 | if (handler->conf->need_utmp_watch) { |
828695d9 SH |
523 | if (prctl(PR_CAPBSET_DROP, CAP_SYS_BOOT, 0, 0, 0)) { |
524 | SYSERROR("failed to remove CAP_SYS_BOOT capability"); | |
525 | return -1; | |
526 | } | |
e2fa1520 | 527 | DEBUG("Dropped cap_sys_boot\n"); |
e2fa1520 SH |
528 | } |
529 | ||
530 | /* Setup the container, ip, names, utsname, ... */ | |
531 | if (lxc_setup(handler->name, handler->conf)) { | |
532 | ERROR("failed to setup the container"); | |
533 | goto out_warn_father; | |
534 | } | |
50e98013 | 535 | |
e075f5d9 SH |
536 | if (apparmor_load(handler) < 0) |
537 | goto out_warn_father; | |
538 | ||
8f2c3a70 SH |
539 | if (lxc_seccomp_load(handler->conf) != 0) |
540 | goto out_warn_father; | |
541 | ||
773fb9ca SH |
542 | if (run_lxc_hooks(handler->name, "start", handler->conf)) { |
543 | ERROR("failed to run start hooks for container '%s'.", handler->name); | |
544 | goto out_warn_father; | |
545 | } | |
fc25b815 | 546 | |
773fb9ca | 547 | close(handler->sigfd); |
26ddeedd | 548 | |
e6126dbe MN |
549 | /* after this call, we are in error because this |
550 | * ops should not return as it execs */ | |
23c53af9 | 551 | if (handler->ops->start(handler, handler->data)) |
e6126dbe | 552 | return -1; |
50e98013 DL |
553 | |
554 | out_warn_father: | |
3c22086f | 555 | lxc_sync_wake_parent(handler, LXC_SYNC_POST_CONFIGURE); |
50e98013 DL |
556 | return -1; |
557 | } | |
558 | ||
23c53af9 | 559 | int lxc_spawn(struct lxc_handler *handler) |
59eb99ba | 560 | { |
59eb99ba | 561 | int clone_flags; |
99a6af52 | 562 | int failed_before_rename = 0; |
ffe1e01a | 563 | const char *name = handler->name; |
0c547523 | 564 | int pinfd; |
50e98013 | 565 | |
3c22086f | 566 | if (lxc_sync_init(handler)) |
9d7f9e52 | 567 | return -1; |
0ad19a3f | 568 | |
1ea6db29 | 569 | clone_flags = CLONE_NEWUTS|CLONE_NEWPID|CLONE_NEWIPC|CLONE_NEWNS; |
fae349da | 570 | if (!lxc_list_empty(&handler->conf->network)) { |
82d5ae15 | 571 | |
0ad19a3f | 572 | clone_flags |= CLONE_NEWNET; |
573 | ||
19a26f82 MK |
574 | /* Find gateway addresses from the link device, which is |
575 | * no longer accessible inside the container. Do this | |
576 | * before creating network interfaces, since goto | |
577 | * out_delete_net does not work before lxc_clone. */ | |
578 | if (lxc_find_gateway_addresses(handler)) { | |
579 | ERROR("failed to find gateway addresses"); | |
580 | lxc_sync_fini(handler); | |
581 | return -1; | |
582 | } | |
583 | ||
82d5ae15 DL |
584 | /* that should be done before the clone because we will |
585 | * fill the netdev index and use them in the child | |
586 | */ | |
e3b4c4c4 | 587 | if (lxc_create_network(handler)) { |
82d5ae15 | 588 | ERROR("failed to create the network"); |
3c22086f | 589 | lxc_sync_fini(handler); |
32e1c760 | 590 | return -1; |
82d5ae15 DL |
591 | } |
592 | } | |
593 | ||
0c547523 SH |
594 | /* |
595 | * if the rootfs is not a blockdev, prevent the container from | |
596 | * marking it readonly. | |
597 | */ | |
598 | ||
599 | pinfd = pin_rootfs(handler->conf->rootfs.path); | |
600 | if (pinfd == -1) { | |
601 | ERROR("failed to pin the container's rootfs"); | |
602 | goto out_abort; | |
603 | } | |
604 | ||
0ad19a3f | 605 | /* Create a process in a new set of namespaces */ |
23c53af9 | 606 | handler->pid = lxc_clone(do_start, handler, clone_flags); |
59eb99ba | 607 | if (handler->pid < 0) { |
36eb9bde | 608 | SYSERROR("failed to fork into a new namespace"); |
7fef7a06 | 609 | goto out_delete_net; |
0ad19a3f | 610 | } |
611 | ||
3c22086f CLG |
612 | lxc_sync_fini_child(handler); |
613 | ||
614 | if (lxc_sync_wait_child(handler, LXC_SYNC_CONFIGURE)) | |
99a6af52 | 615 | failed_before_rename = 1; |
0ad19a3f | 616 | |
36b86299 | 617 | if (lxc_cgroup_create(name, handler->pid)) |
7fef7a06 | 618 | goto out_delete_net; |
218d4250 | 619 | |
99a6af52 MN |
620 | if (failed_before_rename) |
621 | goto out_delete_net; | |
622 | ||
0ad19a3f | 623 | /* Create the network configuration */ |
82d5ae15 | 624 | if (clone_flags & CLONE_NEWNET) { |
fae349da | 625 | if (lxc_assign_network(&handler->conf->network, handler->pid)) { |
82d5ae15 | 626 | ERROR("failed to create the configured network"); |
7fef7a06 | 627 | goto out_delete_net; |
82d5ae15 | 628 | } |
0ad19a3f | 629 | } |
630 | ||
3c22086f CLG |
631 | /* Tell the child to continue its initialization and wait for |
632 | * it to exec or return an error | |
633 | */ | |
634 | if (lxc_sync_barrier_child(handler, LXC_SYNC_POST_CONFIGURE)) | |
635 | return -1; | |
0ad19a3f | 636 | |
23c53af9 | 637 | if (handler->ops->post_start(handler, handler->data)) |
e6126dbe MN |
638 | goto out_abort; |
639 | ||
25c2aca5 | 640 | if (lxc_set_state(name, handler, RUNNING)) { |
59eb99ba DL |
641 | ERROR("failed to set state to %s", |
642 | lxc_state2str(RUNNING)); | |
643 | goto out_abort; | |
3f21c114 | 644 | } |
22ebac19 | 645 | |
3c22086f | 646 | lxc_sync_fini(handler); |
0c547523 SH |
647 | |
648 | if (pinfd >= 0) | |
649 | close(pinfd); | |
650 | ||
e6126dbe | 651 | return 0; |
1ac470c0 | 652 | |
7fef7a06 DL |
653 | out_delete_net: |
654 | if (clone_flags & CLONE_NEWNET) | |
655 | lxc_delete_network(&handler->conf->network); | |
59eb99ba DL |
656 | out_abort: |
657 | lxc_abort(name, handler); | |
3c22086f | 658 | lxc_sync_fini(handler); |
b79fcd86 | 659 | return -1; |
59eb99ba | 660 | } |
0ad19a3f | 661 | |
ee70bf78 CLG |
662 | int __lxc_start(const char *name, struct lxc_conf *conf, |
663 | struct lxc_operations* ops, void *data) | |
59eb99ba | 664 | { |
3a0f472d | 665 | struct lxc_handler *handler; |
e043236e | 666 | int err = -1; |
59eb99ba | 667 | int status; |
80090207 | 668 | |
fae349da | 669 | handler = lxc_init(name, conf); |
3a0f472d | 670 | if (!handler) { |
59eb99ba | 671 | ERROR("failed to initialize the container"); |
66aeffc7 | 672 | return -1; |
0ad19a3f | 673 | } |
ee70bf78 CLG |
674 | handler->ops = ops; |
675 | handler->data = data; | |
e6126dbe | 676 | |
69182a31 | 677 | if (must_drop_cap_sys_boot()) { |
f51db2b3 | 678 | DEBUG("Dropping cap_sys_boot\n"); |
69182a31 SH |
679 | } else { |
680 | DEBUG("Not dropping cap_sys_boot or watching utmp\n"); | |
681 | handler->conf->need_utmp_watch = 0; | |
682 | } | |
683 | ||
23c53af9 | 684 | err = lxc_spawn(handler); |
59eb99ba | 685 | if (err) { |
ee70bf78 | 686 | ERROR("failed to spawn '%s'", name); |
9d7f9e52 | 687 | goto out_fini; |
0ad19a3f | 688 | } |
689 | ||
3a0f472d | 690 | err = lxc_poll(name, handler); |
e043236e | 691 | if (err) { |
59eb99ba DL |
692 | ERROR("mainloop exited with an error"); |
693 | goto out_abort; | |
694 | } | |
0ad19a3f | 695 | |
3a0f472d | 696 | while (waitpid(handler->pid, &status, 0) < 0 && errno == EINTR) |
1bc5cc8c | 697 | continue; |
e043236e | 698 | |
8b004f07 SH |
699 | /* |
700 | * If the child process exited but was not signaled, | |
701 | * it didn't call reboot. This should mean it was an | |
702 | * lxc-execute which simply exited. In any case, treat | |
703 | * it as a 'halt' | |
704 | */ | |
705 | if (WIFSIGNALED(status)) { | |
706 | switch(WTERMSIG(status)) { | |
707 | case SIGINT: /* halt */ | |
708 | DEBUG("Container halting"); | |
709 | break; | |
710 | case SIGHUP: /* reboot */ | |
711 | DEBUG("Container rebooting"); | |
712 | handler->conf->reboot = 1; | |
713 | break; | |
714 | default: | |
715 | DEBUG("unknown exit status for init: %d\n", WTERMSIG(status)); | |
716 | break; | |
717 | } | |
828695d9 SH |
718 | } |
719 | ||
3a0f472d | 720 | err = lxc_error_set_and_log(handler->pid, status); |
9d7f9e52 | 721 | out_fini: |
36b86299 | 722 | lxc_cgroup_destroy(name); |
3a0f472d | 723 | lxc_fini(name, handler); |
0ad19a3f | 724 | return err; |
725 | ||
59eb99ba | 726 | out_abort: |
3a0f472d | 727 | lxc_abort(name, handler); |
9d7f9e52 | 728 | goto out_fini; |
0ad19a3f | 729 | } |
ee70bf78 CLG |
730 | |
731 | struct start_args { | |
732 | char *const *argv; | |
733 | }; | |
734 | ||
735 | static int start(struct lxc_handler *handler, void* data) | |
736 | { | |
737 | struct start_args *arg = data; | |
738 | ||
739 | NOTICE("exec'ing '%s'", arg->argv[0]); | |
740 | ||
741 | execvp(arg->argv[0], arg->argv); | |
742 | SYSERROR("failed to exec %s", arg->argv[0]); | |
743 | return 0; | |
744 | } | |
745 | ||
746 | static int post_start(struct lxc_handler *handler, void* data) | |
747 | { | |
748 | struct start_args *arg = data; | |
749 | ||
750 | NOTICE("'%s' started with pid '%d'", arg->argv[0], handler->pid); | |
751 | return 0; | |
752 | } | |
753 | ||
754 | static struct lxc_operations start_ops = { | |
755 | .start = start, | |
756 | .post_start = post_start | |
757 | }; | |
758 | ||
759 | int lxc_start(const char *name, char *const argv[], struct lxc_conf *conf) | |
760 | { | |
761 | struct start_args start_arg = { | |
762 | .argv = argv, | |
763 | }; | |
764 | ||
b119f362 | 765 | if (lxc_check_inherited(conf, -1)) |
ee70bf78 CLG |
766 | return -1; |
767 | ||
828695d9 | 768 | conf->need_utmp_watch = 1; |
ee70bf78 CLG |
769 | return __lxc_start(name, conf, &start_ops, &start_arg); |
770 | } |