]>
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 | ||
ff218c25 | 24 | #include "../config.h" |
0ad19a3f | 25 | #include <stdio.h> |
26 | #undef _GNU_SOURCE | |
27 | #include <string.h> | |
28 | #include <stdlib.h> | |
29 | #include <dirent.h> | |
30 | #include <errno.h> | |
31 | #include <unistd.h> | |
32 | #include <signal.h> | |
b0a33c1e | 33 | #include <fcntl.h> |
34 | #include <termios.h> | |
50e98013 | 35 | #include <namespace.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" |
36eb9bde CLG |
128 | |
129 | lxc_log_define(lxc_start, lxc); | |
130 | ||
0ad19a3f | 131 | LXC_TTY_HANDLER(SIGINT); |
132 | LXC_TTY_HANDLER(SIGQUIT); | |
133 | ||
80090207 CLG |
134 | static int match_fd(int fd) |
135 | { | |
136 | return (fd == 0 || fd == 1 || fd == 2); | |
137 | } | |
138 | ||
f2faa8fa | 139 | int lxc_check_inherited(int fd_to_ignore) |
80090207 CLG |
140 | { |
141 | struct dirent dirent, *direntp; | |
142 | int fd, fddir; | |
143 | DIR *dir; | |
144 | int ret = 0; | |
145 | ||
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)) { | |
155 | char procpath[64]; | |
156 | char path[PATH_MAX]; | |
157 | ||
158 | if (!direntp) | |
159 | break; | |
160 | ||
161 | if (!strcmp(direntp->d_name, ".")) | |
162 | continue; | |
163 | ||
164 | if (!strcmp(direntp->d_name, "..")) | |
165 | continue; | |
166 | ||
167 | fd = atoi(direntp->d_name); | |
168 | ||
f2faa8fa | 169 | if (fd == fddir || fd == lxc_log_fd || fd == fd_to_ignore) |
80090207 CLG |
170 | continue; |
171 | ||
172 | if (match_fd(fd)) | |
173 | continue; | |
174 | /* | |
175 | * found inherited fd | |
176 | */ | |
177 | ret = -1; | |
178 | ||
179 | snprintf(procpath, sizeof(procpath), "/proc/self/fd/%d", fd); | |
180 | ||
181 | if (readlink(procpath, path, sizeof(path)) == -1) | |
182 | ERROR("readlink(%s) failed : %m", procpath); | |
183 | else | |
184 | ERROR("inherited fd %d on %s", fd, path); | |
185 | } | |
186 | ||
187 | if (closedir(dir)) | |
188 | ERROR("failed to close directory"); | |
189 | return ret; | |
190 | } | |
191 | ||
83ee7875 | 192 | static int setup_signal_fd(sigset_t *oldmask) |
b0a33c1e | 193 | { |
194 | sigset_t mask; | |
195 | int fd; | |
196 | ||
f3304a29 FW |
197 | /* Block everything except serious error signals */ |
198 | if (sigfillset(&mask) || | |
199 | sigdelset(&mask, SIGILL) || | |
200 | sigdelset(&mask, SIGSEGV) || | |
201 | sigdelset(&mask, SIGBUS) || | |
202 | sigprocmask(SIG_BLOCK, &mask, oldmask)) { | |
203 | SYSERROR("failed to set signal mask"); | |
b0a33c1e | 204 | return -1; |
205 | } | |
206 | ||
207 | fd = signalfd(-1, &mask, 0); | |
208 | if (fd < 0) { | |
36eb9bde | 209 | SYSERROR("failed to create the signal fd"); |
b0a33c1e | 210 | return -1; |
211 | } | |
212 | ||
213 | if (fcntl(fd, F_SETFD, FD_CLOEXEC)) { | |
36eb9bde | 214 | SYSERROR("failed to set sigfd to close-on-exec"); |
b0a33c1e | 215 | close(fd); |
216 | return -1; | |
217 | } | |
218 | ||
1ac470c0 DL |
219 | DEBUG("sigchild handler set"); |
220 | ||
b0a33c1e | 221 | return fd; |
222 | } | |
223 | ||
83ee7875 | 224 | static int signal_handler(int fd, void *data, |
b0a33c1e | 225 | struct lxc_epoll_descr *descr) |
226 | { | |
15cd25fd DL |
227 | struct signalfd_siginfo siginfo; |
228 | int ret; | |
82d89dce | 229 | pid_t *pid = data; |
15cd25fd DL |
230 | |
231 | ret = read(fd, &siginfo, sizeof(siginfo)); | |
232 | if (ret < 0) { | |
f3304a29 | 233 | ERROR("failed to read signal info"); |
15cd25fd DL |
234 | return -1; |
235 | } | |
236 | ||
237 | if (ret != sizeof(siginfo)) { | |
238 | ERROR("unexpected siginfo size"); | |
239 | return -1; | |
240 | } | |
241 | ||
f3304a29 FW |
242 | if (siginfo.ssi_signo != SIGCHLD) { |
243 | kill(*pid, siginfo.ssi_signo); | |
244 | INFO("forwarded signal %d to pid %d", siginfo.ssi_signo, *pid); | |
245 | return 0; | |
246 | } | |
247 | ||
15cd25fd DL |
248 | if (siginfo.ssi_code == CLD_STOPPED || |
249 | siginfo.ssi_code == CLD_CONTINUED) { | |
250 | INFO("container init process was stopped/continued"); | |
251 | return 0; | |
252 | } | |
1ac470c0 | 253 | |
82d89dce DL |
254 | /* more robustness, protect ourself from a SIGCHLD sent |
255 | * by a process different from the container init | |
256 | */ | |
257 | if (siginfo.ssi_pid != *pid) { | |
258 | WARN("invalid pid for SIGCHLD"); | |
259 | return 0; | |
260 | } | |
261 | ||
15cd25fd | 262 | DEBUG("container init process exited"); |
b0a33c1e | 263 | return 1; |
264 | } | |
265 | ||
0a3ec350 DL |
266 | int lxc_pid_callback(int fd, struct lxc_request *request, |
267 | struct lxc_handler *handler) | |
81c75799 DL |
268 | { |
269 | struct lxc_answer answer; | |
270 | int ret; | |
271 | ||
272 | answer.pid = handler->pid; | |
273 | answer.ret = 0; | |
274 | ||
275 | ret = send(fd, &answer, sizeof(answer), 0); | |
276 | if (ret < 0) { | |
277 | WARN("failed to send answer to the peer"); | |
278 | return -1; | |
279 | } | |
280 | ||
281 | if (ret != sizeof(answer)) { | |
282 | ERROR("partial answer sent"); | |
283 | return -1; | |
284 | } | |
285 | ||
286 | return 0; | |
287 | } | |
288 | ||
25c2aca5 | 289 | int lxc_set_state(const char *name, struct lxc_handler *handler, lxc_state_t state) |
66aeffc7 DL |
290 | { |
291 | handler->state = state; | |
292 | lxc_monitor_send_state(name, state); | |
293 | return 0; | |
294 | } | |
295 | ||
1bc5cc8c | 296 | int lxc_poll(const char *name, struct lxc_handler *handler) |
b0a33c1e | 297 | { |
ca5f7926 DL |
298 | int sigfd = handler->sigfd; |
299 | int pid = handler->pid; | |
b0a33c1e | 300 | struct lxc_epoll_descr descr; |
301 | ||
a9e61274 | 302 | if (lxc_mainloop_open(&descr)) { |
36eb9bde | 303 | ERROR("failed to create mainloop"); |
50c8bf05 | 304 | goto out_sigfd; |
b0a33c1e | 305 | } |
306 | ||
83ee7875 | 307 | if (lxc_mainloop_add_handler(&descr, sigfd, signal_handler, &pid)) { |
36eb9bde | 308 | ERROR("failed to add handler for the signal"); |
b0a33c1e | 309 | goto out_mainloop_open; |
310 | } | |
311 | ||
63376d7d DL |
312 | if (lxc_console_mainloop_add(&descr, handler)) { |
313 | ERROR("failed to add console handler to mainloop"); | |
314 | goto out_mainloop_open; | |
315 | } | |
316 | ||
563f2f2c DL |
317 | if (lxc_command_mainloop_add(name, &descr, handler)) { |
318 | ERROR("failed to add command handler to mainloop"); | |
96fa1ff0 | 319 | goto out_mainloop_open; |
563f2f2c DL |
320 | } |
321 | ||
322 | if (lxc_utmp_mainloop_add(&descr, handler)) { | |
323 | ERROR("failed to add utmp handler to mainloop"); | |
324 | goto out_mainloop_open; | |
325 | } | |
b0a33c1e | 326 | |
c3e13372 | 327 | return lxc_mainloop(&descr); |
b0a33c1e | 328 | |
329 | out_mainloop_open: | |
330 | lxc_mainloop_close(&descr); | |
b0a33c1e | 331 | out_sigfd: |
332 | close(sigfd); | |
c3e13372 | 333 | return -1; |
b0a33c1e | 334 | } |
335 | ||
fae349da | 336 | struct lxc_handler *lxc_init(const char *name, struct lxc_conf *conf) |
59eb99ba | 337 | { |
3a0f472d DL |
338 | struct lxc_handler *handler; |
339 | ||
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 | ||
ffe1e01a | 421 | static int do_start(void *data) |
50e98013 | 422 | { |
23c53af9 | 423 | struct lxc_handler *handler = data; |
50e98013 DL |
424 | |
425 | if (sigprocmask(SIG_SETMASK, &handler->oldmask, NULL)) { | |
426 | SYSERROR("failed to set sigprocmask"); | |
9d7f9e52 | 427 | return -1; |
50e98013 DL |
428 | } |
429 | ||
743ecd2e DL |
430 | /* This prctl must be before the synchro, so if the parent |
431 | * dies before we set the parent death signal, we will detect | |
432 | * its death with the synchro right after, otherwise we have | |
433 | * a window where the parent can exit before we set the pdeath | |
434 | * signal leading to a unsupervized container. | |
435 | */ | |
436 | if (prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0)) { | |
437 | SYSERROR("failed to set pdeath signal"); | |
438 | return -1; | |
439 | } | |
440 | ||
3c22086f | 441 | lxc_sync_fini_parent(handler); |
50e98013 | 442 | |
3c22086f CLG |
443 | /* Tell the parent task it can begin to configure the |
444 | * container and wait for it to finish | |
445 | */ | |
446 | if (lxc_sync_barrier_parent(handler, LXC_SYNC_CONFIGURE)) | |
9d7f9e52 | 447 | return -1; |
50e98013 DL |
448 | |
449 | /* Setup the container, ip, names, utsname, ... */ | |
ffe1e01a | 450 | if (lxc_setup(handler->name, handler->conf)) { |
50e98013 DL |
451 | ERROR("failed to setup the container"); |
452 | goto out_warn_father; | |
453 | } | |
454 | ||
455 | if (prctl(PR_CAPBSET_DROP, CAP_SYS_BOOT, 0, 0, 0)) { | |
456 | SYSERROR("failed to remove CAP_SYS_BOOT capability"); | |
9d7f9e52 | 457 | return -1; |
50e98013 DL |
458 | } |
459 | ||
fc25b815 MN |
460 | close(handler->sigfd); |
461 | ||
e6126dbe MN |
462 | /* after this call, we are in error because this |
463 | * ops should not return as it execs */ | |
23c53af9 | 464 | if (handler->ops->start(handler, handler->data)) |
e6126dbe | 465 | return -1; |
50e98013 DL |
466 | |
467 | out_warn_father: | |
3c22086f | 468 | lxc_sync_wake_parent(handler, LXC_SYNC_POST_CONFIGURE); |
50e98013 DL |
469 | return -1; |
470 | } | |
471 | ||
23c53af9 | 472 | int lxc_spawn(struct lxc_handler *handler) |
59eb99ba | 473 | { |
59eb99ba | 474 | int clone_flags; |
99a6af52 | 475 | int failed_before_rename = 0; |
ffe1e01a | 476 | const char *name = handler->name; |
50e98013 | 477 | |
3c22086f | 478 | if (lxc_sync_init(handler)) |
9d7f9e52 | 479 | return -1; |
0ad19a3f | 480 | |
1ea6db29 | 481 | clone_flags = CLONE_NEWUTS|CLONE_NEWPID|CLONE_NEWIPC|CLONE_NEWNS; |
fae349da | 482 | if (!lxc_list_empty(&handler->conf->network)) { |
82d5ae15 | 483 | |
0ad19a3f | 484 | clone_flags |= CLONE_NEWNET; |
485 | ||
82d5ae15 DL |
486 | /* that should be done before the clone because we will |
487 | * fill the netdev index and use them in the child | |
488 | */ | |
e3b4c4c4 | 489 | if (lxc_create_network(handler)) { |
82d5ae15 | 490 | ERROR("failed to create the network"); |
3c22086f | 491 | lxc_sync_fini(handler); |
32e1c760 | 492 | return -1; |
82d5ae15 DL |
493 | } |
494 | } | |
495 | ||
ffe1e01a | 496 | |
0ad19a3f | 497 | /* Create a process in a new set of namespaces */ |
23c53af9 | 498 | handler->pid = lxc_clone(do_start, handler, clone_flags); |
59eb99ba | 499 | if (handler->pid < 0) { |
36eb9bde | 500 | SYSERROR("failed to fork into a new namespace"); |
7fef7a06 | 501 | goto out_delete_net; |
0ad19a3f | 502 | } |
503 | ||
3c22086f CLG |
504 | lxc_sync_fini_child(handler); |
505 | ||
506 | if (lxc_sync_wait_child(handler, LXC_SYNC_CONFIGURE)) | |
99a6af52 | 507 | failed_before_rename = 1; |
0ad19a3f | 508 | |
36b86299 | 509 | if (lxc_cgroup_create(name, handler->pid)) |
7fef7a06 | 510 | goto out_delete_net; |
218d4250 | 511 | |
99a6af52 MN |
512 | if (failed_before_rename) |
513 | goto out_delete_net; | |
514 | ||
0ad19a3f | 515 | /* Create the network configuration */ |
82d5ae15 | 516 | if (clone_flags & CLONE_NEWNET) { |
fae349da | 517 | if (lxc_assign_network(&handler->conf->network, handler->pid)) { |
82d5ae15 | 518 | ERROR("failed to create the configured network"); |
7fef7a06 | 519 | goto out_delete_net; |
82d5ae15 | 520 | } |
0ad19a3f | 521 | } |
522 | ||
3c22086f CLG |
523 | /* Tell the child to continue its initialization and wait for |
524 | * it to exec or return an error | |
525 | */ | |
526 | if (lxc_sync_barrier_child(handler, LXC_SYNC_POST_CONFIGURE)) | |
527 | return -1; | |
0ad19a3f | 528 | |
23c53af9 | 529 | if (handler->ops->post_start(handler, handler->data)) |
e6126dbe MN |
530 | goto out_abort; |
531 | ||
25c2aca5 | 532 | if (lxc_set_state(name, handler, RUNNING)) { |
59eb99ba DL |
533 | ERROR("failed to set state to %s", |
534 | lxc_state2str(RUNNING)); | |
535 | goto out_abort; | |
3f21c114 | 536 | } |
22ebac19 | 537 | |
3c22086f | 538 | lxc_sync_fini(handler); |
e6126dbe | 539 | return 0; |
1ac470c0 | 540 | |
7fef7a06 DL |
541 | out_delete_net: |
542 | if (clone_flags & CLONE_NEWNET) | |
543 | lxc_delete_network(&handler->conf->network); | |
59eb99ba DL |
544 | out_abort: |
545 | lxc_abort(name, handler); | |
3c22086f | 546 | lxc_sync_fini(handler); |
b79fcd86 | 547 | return -1; |
59eb99ba | 548 | } |
0ad19a3f | 549 | |
ee70bf78 CLG |
550 | int __lxc_start(const char *name, struct lxc_conf *conf, |
551 | struct lxc_operations* ops, void *data) | |
59eb99ba | 552 | { |
3a0f472d | 553 | struct lxc_handler *handler; |
e043236e | 554 | int err = -1; |
59eb99ba | 555 | int status; |
80090207 | 556 | |
fae349da | 557 | handler = lxc_init(name, conf); |
3a0f472d | 558 | if (!handler) { |
59eb99ba | 559 | ERROR("failed to initialize the container"); |
66aeffc7 | 560 | return -1; |
0ad19a3f | 561 | } |
ee70bf78 CLG |
562 | handler->ops = ops; |
563 | handler->data = data; | |
e6126dbe | 564 | |
23c53af9 | 565 | err = lxc_spawn(handler); |
59eb99ba | 566 | if (err) { |
ee70bf78 | 567 | ERROR("failed to spawn '%s'", name); |
9d7f9e52 | 568 | goto out_fini; |
0ad19a3f | 569 | } |
570 | ||
69ffa311 MN |
571 | /* Avoid signals from terminal */ |
572 | LXC_TTY_ADD_HANDLER(SIGINT); | |
573 | LXC_TTY_ADD_HANDLER(SIGQUIT); | |
574 | ||
3a0f472d | 575 | err = lxc_poll(name, handler); |
e043236e | 576 | if (err) { |
59eb99ba DL |
577 | ERROR("mainloop exited with an error"); |
578 | goto out_abort; | |
579 | } | |
0ad19a3f | 580 | |
3a0f472d | 581 | while (waitpid(handler->pid, &status, 0) < 0 && errno == EINTR) |
1bc5cc8c | 582 | continue; |
e043236e | 583 | |
3a0f472d | 584 | err = lxc_error_set_and_log(handler->pid, status); |
9d7f9e52 | 585 | out_fini: |
69ffa311 MN |
586 | LXC_TTY_DEL_HANDLER(SIGQUIT); |
587 | LXC_TTY_DEL_HANDLER(SIGINT); | |
36b86299 | 588 | lxc_cgroup_destroy(name); |
3a0f472d | 589 | lxc_fini(name, handler); |
0ad19a3f | 590 | return err; |
591 | ||
59eb99ba | 592 | out_abort: |
3a0f472d | 593 | lxc_abort(name, handler); |
9d7f9e52 | 594 | goto out_fini; |
0ad19a3f | 595 | } |
ee70bf78 CLG |
596 | |
597 | struct start_args { | |
598 | char *const *argv; | |
599 | }; | |
600 | ||
601 | static int start(struct lxc_handler *handler, void* data) | |
602 | { | |
603 | struct start_args *arg = data; | |
604 | ||
605 | NOTICE("exec'ing '%s'", arg->argv[0]); | |
606 | ||
607 | execvp(arg->argv[0], arg->argv); | |
608 | SYSERROR("failed to exec %s", arg->argv[0]); | |
609 | return 0; | |
610 | } | |
611 | ||
612 | static int post_start(struct lxc_handler *handler, void* data) | |
613 | { | |
614 | struct start_args *arg = data; | |
615 | ||
616 | NOTICE("'%s' started with pid '%d'", arg->argv[0], handler->pid); | |
617 | return 0; | |
618 | } | |
619 | ||
620 | static struct lxc_operations start_ops = { | |
621 | .start = start, | |
622 | .post_start = post_start | |
623 | }; | |
624 | ||
625 | int lxc_start(const char *name, char *const argv[], struct lxc_conf *conf) | |
626 | { | |
627 | struct start_args start_arg = { | |
628 | .argv = argv, | |
629 | }; | |
630 | ||
631 | if (lxc_check_inherited(-1)) | |
632 | return -1; | |
633 | ||
634 | return __lxc_start(name, conf, &start_ops, &start_arg); | |
635 | } |