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