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