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