]>
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 DL |
51 | /* assume kernel headers are too old */ |
52 | struct signalfd_siginfo | |
53 | { | |
da2aef7f DL |
54 | uint32_t ssi_signo; |
55 | int32_t ssi_errno; | |
56 | int32_t ssi_code; | |
57 | uint32_t ssi_pid; | |
58 | uint32_t ssi_uid; | |
59 | int32_t ssi_fd; | |
60 | uint32_t ssi_tid; | |
61 | uint32_t ssi_band; | |
62 | uint32_t ssi_overrun; | |
63 | uint32_t ssi_trapno; | |
64 | int32_t ssi_status; | |
65 | int32_t ssi_int; | |
66 | uint64_t ssi_ptr; | |
67 | uint64_t ssi_utime; | |
68 | uint64_t ssi_stime; | |
69 | uint64_t ssi_addr; | |
70 | uint8_t __pad[48]; | |
15cd25fd DL |
71 | }; |
72 | ||
8ca61733 MJ |
73 | # ifndef __NR_signalfd4 |
74 | /* assume kernel headers are too old */ | |
75 | # if __i386__ | |
76 | # define __NR_signalfd4 327 | |
77 | # elif __x86_64__ | |
78 | # define __NR_signalfd4 289 | |
bfa38025 MH |
79 | # elif __powerpc__ |
80 | # define __NR_signalfd4 313 | |
47f38330 SH |
81 | # elif __s390x__ |
82 | # define __NR_signalfd4 322 | |
8ca61733 MJ |
83 | # endif |
84 | #endif | |
85 | ||
86 | # ifndef __NR_signalfd | |
87 | /* assume kernel headers are too old */ | |
88 | # if __i386__ | |
89 | # define __NR_signalfd 321 | |
90 | # elif __x86_64__ | |
91 | # define __NR_signalfd 282 | |
bfa38025 MH |
92 | # elif __powerpc__ |
93 | # define __NR_signalfd 305 | |
47f38330 SH |
94 | # elif __s390x__ |
95 | # define __NR_signalfd 316 | |
8ca61733 MJ |
96 | # endif |
97 | #endif | |
98 | ||
99 | int signalfd(int fd, const sigset_t *mask, int flags) | |
100 | { | |
101 | int retval; | |
102 | ||
103 | retval = syscall (__NR_signalfd4, fd, mask, _NSIG / 8, flags); | |
104 | if (errno == ENOSYS && flags == 0) | |
105 | retval = syscall (__NR_signalfd, fd, mask, _NSIG / 8); | |
106 | return retval; | |
107 | } | |
ff218c25 | 108 | #endif |
0ad19a3f | 109 | |
656994bb MH |
110 | #if !HAVE_DECL_PR_CAPBSET_DROP |
111 | #define PR_CAPBSET_DROP 24 | |
112 | #endif | |
113 | ||
63376d7d DL |
114 | #include "start.h" |
115 | #include "conf.h" | |
6a3111b8 | 116 | #include "cgroup.h" |
63376d7d | 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" |
36eb9bde CLG |
127 | |
128 | lxc_log_define(lxc_start, lxc); | |
129 | ||
0ad19a3f | 130 | LXC_TTY_HANDLER(SIGINT); |
131 | LXC_TTY_HANDLER(SIGQUIT); | |
132 | ||
80090207 CLG |
133 | static int match_fd(int fd) |
134 | { | |
135 | return (fd == 0 || fd == 1 || fd == 2); | |
136 | } | |
137 | ||
f2faa8fa | 138 | int lxc_check_inherited(int fd_to_ignore) |
80090207 CLG |
139 | { |
140 | struct dirent dirent, *direntp; | |
141 | int fd, fddir; | |
142 | DIR *dir; | |
143 | int ret = 0; | |
144 | ||
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)) { | |
154 | char procpath[64]; | |
155 | char path[PATH_MAX]; | |
156 | ||
157 | if (!direntp) | |
158 | break; | |
159 | ||
160 | if (!strcmp(direntp->d_name, ".")) | |
161 | continue; | |
162 | ||
163 | if (!strcmp(direntp->d_name, "..")) | |
164 | continue; | |
165 | ||
166 | fd = atoi(direntp->d_name); | |
167 | ||
f2faa8fa | 168 | if (fd == fddir || fd == lxc_log_fd || fd == fd_to_ignore) |
80090207 CLG |
169 | continue; |
170 | ||
171 | if (match_fd(fd)) | |
172 | continue; | |
173 | /* | |
174 | * found inherited fd | |
175 | */ | |
176 | ret = -1; | |
177 | ||
178 | snprintf(procpath, sizeof(procpath), "/proc/self/fd/%d", fd); | |
179 | ||
180 | if (readlink(procpath, path, sizeof(path)) == -1) | |
181 | ERROR("readlink(%s) failed : %m", procpath); | |
182 | else | |
183 | ERROR("inherited fd %d on %s", fd, path); | |
184 | } | |
185 | ||
186 | if (closedir(dir)) | |
187 | ERROR("failed to close directory"); | |
188 | return ret; | |
189 | } | |
190 | ||
b0a33c1e | 191 | static int setup_sigchld_fd(sigset_t *oldmask) |
192 | { | |
193 | sigset_t mask; | |
194 | int fd; | |
195 | ||
196 | if (sigprocmask(SIG_BLOCK, NULL, &mask)) { | |
36eb9bde | 197 | SYSERROR("failed to get mask signal"); |
b0a33c1e | 198 | return -1; |
199 | } | |
200 | ||
201 | if (sigaddset(&mask, SIGCHLD) || sigprocmask(SIG_BLOCK, &mask, oldmask)) { | |
36eb9bde | 202 | SYSERROR("failed to set mask signal"); |
b0a33c1e | 203 | return -1; |
204 | } | |
205 | ||
206 | fd = signalfd(-1, &mask, 0); | |
207 | if (fd < 0) { | |
36eb9bde | 208 | SYSERROR("failed to create the signal fd"); |
b0a33c1e | 209 | return -1; |
210 | } | |
211 | ||
212 | if (fcntl(fd, F_SETFD, FD_CLOEXEC)) { | |
36eb9bde | 213 | SYSERROR("failed to set sigfd to close-on-exec"); |
b0a33c1e | 214 | close(fd); |
215 | return -1; | |
216 | } | |
217 | ||
1ac470c0 DL |
218 | DEBUG("sigchild handler set"); |
219 | ||
b0a33c1e | 220 | return fd; |
221 | } | |
222 | ||
15cd25fd | 223 | static int sigchld_handler(int fd, void *data, |
b0a33c1e | 224 | struct lxc_epoll_descr *descr) |
225 | { | |
15cd25fd DL |
226 | struct signalfd_siginfo siginfo; |
227 | int ret; | |
82d89dce | 228 | pid_t *pid = data; |
15cd25fd DL |
229 | |
230 | ret = read(fd, &siginfo, sizeof(siginfo)); | |
231 | if (ret < 0) { | |
232 | ERROR("failed to read sigchld info"); | |
233 | return -1; | |
234 | } | |
235 | ||
236 | if (ret != sizeof(siginfo)) { | |
237 | ERROR("unexpected siginfo size"); | |
238 | return -1; | |
239 | } | |
240 | ||
241 | if (siginfo.ssi_code == CLD_STOPPED || | |
242 | siginfo.ssi_code == CLD_CONTINUED) { | |
243 | INFO("container init process was stopped/continued"); | |
244 | return 0; | |
245 | } | |
1ac470c0 | 246 | |
82d89dce DL |
247 | /* more robustness, protect ourself from a SIGCHLD sent |
248 | * by a process different from the container init | |
249 | */ | |
250 | if (siginfo.ssi_pid != *pid) { | |
251 | WARN("invalid pid for SIGCHLD"); | |
252 | return 0; | |
253 | } | |
254 | ||
15cd25fd | 255 | DEBUG("container init process exited"); |
b0a33c1e | 256 | return 1; |
257 | } | |
258 | ||
0a3ec350 DL |
259 | int lxc_pid_callback(int fd, struct lxc_request *request, |
260 | struct lxc_handler *handler) | |
81c75799 DL |
261 | { |
262 | struct lxc_answer answer; | |
263 | int ret; | |
264 | ||
265 | answer.pid = handler->pid; | |
266 | answer.ret = 0; | |
267 | ||
268 | ret = send(fd, &answer, sizeof(answer), 0); | |
269 | if (ret < 0) { | |
270 | WARN("failed to send answer to the peer"); | |
271 | return -1; | |
272 | } | |
273 | ||
274 | if (ret != sizeof(answer)) { | |
275 | ERROR("partial answer sent"); | |
276 | return -1; | |
277 | } | |
278 | ||
279 | return 0; | |
280 | } | |
281 | ||
25c2aca5 | 282 | int lxc_set_state(const char *name, struct lxc_handler *handler, lxc_state_t state) |
66aeffc7 DL |
283 | { |
284 | handler->state = state; | |
285 | lxc_monitor_send_state(name, state); | |
286 | return 0; | |
287 | } | |
288 | ||
1bc5cc8c | 289 | int lxc_poll(const char *name, struct lxc_handler *handler) |
b0a33c1e | 290 | { |
ca5f7926 DL |
291 | int sigfd = handler->sigfd; |
292 | int pid = handler->pid; | |
b0a33c1e | 293 | struct lxc_epoll_descr descr; |
294 | ||
a9e61274 | 295 | if (lxc_mainloop_open(&descr)) { |
36eb9bde | 296 | ERROR("failed to create mainloop"); |
50c8bf05 | 297 | goto out_sigfd; |
b0a33c1e | 298 | } |
299 | ||
300 | if (lxc_mainloop_add_handler(&descr, sigfd, sigchld_handler, &pid)) { | |
36eb9bde | 301 | ERROR("failed to add handler for the signal"); |
b0a33c1e | 302 | goto out_mainloop_open; |
303 | } | |
304 | ||
63376d7d DL |
305 | if (lxc_console_mainloop_add(&descr, handler)) { |
306 | ERROR("failed to add console handler to mainloop"); | |
307 | goto out_mainloop_open; | |
308 | } | |
309 | ||
563f2f2c DL |
310 | if (lxc_command_mainloop_add(name, &descr, handler)) { |
311 | ERROR("failed to add command handler to mainloop"); | |
96fa1ff0 | 312 | goto out_mainloop_open; |
563f2f2c DL |
313 | } |
314 | ||
315 | if (lxc_utmp_mainloop_add(&descr, handler)) { | |
316 | ERROR("failed to add utmp handler to mainloop"); | |
317 | goto out_mainloop_open; | |
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 | ||
fae349da | 329 | struct lxc_handler *lxc_init(const char *name, struct lxc_conf *conf) |
59eb99ba | 330 | { |
3a0f472d DL |
331 | struct lxc_handler *handler; |
332 | ||
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 */ | |
59eb99ba DL |
366 | handler->sigfd = setup_sigchld_fd(&handler->oldmask); |
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 | |
8f64a3f6 MN |
397 | /* reset mask set by setup_sigchld_fd */ |
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 | ||
ffe1e01a | 414 | static int do_start(void *data) |
50e98013 | 415 | { |
23c53af9 | 416 | struct lxc_handler *handler = data; |
50e98013 DL |
417 | int err = -1, sync; |
418 | ||
419 | if (sigprocmask(SIG_SETMASK, &handler->oldmask, NULL)) { | |
420 | SYSERROR("failed to set sigprocmask"); | |
9d7f9e52 | 421 | return -1; |
50e98013 DL |
422 | } |
423 | ||
23c53af9 | 424 | close(handler->sv[1]); |
50e98013 DL |
425 | |
426 | /* Be sure we don't inherit this after the exec */ | |
23c53af9 | 427 | fcntl(handler->sv[0], F_SETFD, FD_CLOEXEC); |
50e98013 DL |
428 | |
429 | /* Tell our father he can begin to configure the container */ | |
23c53af9 | 430 | if (write(handler->sv[0], &sync, sizeof(sync)) < 0) { |
50e98013 | 431 | SYSERROR("failed to write socket"); |
9d7f9e52 | 432 | return -1; |
50e98013 DL |
433 | } |
434 | ||
435 | /* Wait for the father to finish the configuration */ | |
23c53af9 | 436 | if (read(handler->sv[0], &sync, sizeof(sync)) < 0) { |
50e98013 | 437 | SYSERROR("failed to read socket"); |
9d7f9e52 | 438 | return -1; |
50e98013 DL |
439 | } |
440 | ||
441 | /* Setup the container, ip, names, utsname, ... */ | |
ffe1e01a | 442 | if (lxc_setup(handler->name, handler->conf)) { |
50e98013 DL |
443 | ERROR("failed to setup the container"); |
444 | goto out_warn_father; | |
445 | } | |
446 | ||
447 | if (prctl(PR_CAPBSET_DROP, CAP_SYS_BOOT, 0, 0, 0)) { | |
448 | SYSERROR("failed to remove CAP_SYS_BOOT capability"); | |
9d7f9e52 | 449 | return -1; |
50e98013 DL |
450 | } |
451 | ||
6a6ad7af DL |
452 | if (prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0)) { |
453 | SYSERROR("failed to set pdeath signal"); | |
9d7f9e52 | 454 | return -1; |
6a6ad7af DL |
455 | } |
456 | ||
fc25b815 MN |
457 | close(handler->sigfd); |
458 | ||
e6126dbe MN |
459 | /* after this call, we are in error because this |
460 | * ops should not return as it execs */ | |
23c53af9 | 461 | if (handler->ops->start(handler, handler->data)) |
e6126dbe | 462 | return -1; |
50e98013 DL |
463 | |
464 | out_warn_father: | |
465 | /* If the exec fails, tell that to our father */ | |
23c53af9 | 466 | if (write(handler->sv[0], &err, sizeof(err)) < 0) |
50e98013 | 467 | SYSERROR("failed to write the socket"); |
50e98013 DL |
468 | return -1; |
469 | } | |
470 | ||
23c53af9 | 471 | int lxc_spawn(struct lxc_handler *handler) |
59eb99ba | 472 | { |
59eb99ba | 473 | int clone_flags; |
e6126dbe | 474 | int sync; |
99a6af52 | 475 | int failed_before_rename = 0; |
ffe1e01a | 476 | const char *name = handler->name; |
50e98013 | 477 | |
0ad19a3f | 478 | /* Synchro socketpair */ |
23c53af9 | 479 | if (socketpair(AF_LOCAL, SOCK_STREAM, 0, handler->sv)) { |
36eb9bde | 480 | SYSERROR("failed to create communication socketpair"); |
9d7f9e52 | 481 | return -1; |
0ad19a3f | 482 | } |
483 | ||
1ea6db29 | 484 | clone_flags = CLONE_NEWUTS|CLONE_NEWPID|CLONE_NEWIPC|CLONE_NEWNS; |
fae349da | 485 | if (!lxc_list_empty(&handler->conf->network)) { |
82d5ae15 | 486 | |
0ad19a3f | 487 | clone_flags |= CLONE_NEWNET; |
488 | ||
82d5ae15 DL |
489 | /* that should be done before the clone because we will |
490 | * fill the netdev index and use them in the child | |
491 | */ | |
fae349da | 492 | if (lxc_create_network(&handler->conf->network)) { |
82d5ae15 | 493 | ERROR("failed to create the network"); |
23c53af9 CLG |
494 | close(handler->sv[0]); |
495 | close(handler->sv[1]); | |
32e1c760 | 496 | return -1; |
82d5ae15 DL |
497 | } |
498 | } | |
499 | ||
ffe1e01a | 500 | |
0ad19a3f | 501 | /* Create a process in a new set of namespaces */ |
23c53af9 | 502 | handler->pid = lxc_clone(do_start, handler, clone_flags); |
59eb99ba | 503 | if (handler->pid < 0) { |
36eb9bde | 504 | SYSERROR("failed to fork into a new namespace"); |
7fef7a06 | 505 | goto out_delete_net; |
0ad19a3f | 506 | } |
507 | ||
23c53af9 | 508 | close(handler->sv[0]); |
0ad19a3f | 509 | |
510 | /* Wait for the child to be ready */ | |
23c53af9 | 511 | if (read(handler->sv[1], &sync, sizeof(sync)) <= 0) { |
99a6af52 MN |
512 | ERROR("sync read failure : %m"); |
513 | failed_before_rename = 1; | |
0ad19a3f | 514 | } |
515 | ||
9f44c578 | 516 | if (lxc_rename_nsgroup(name, handler)) |
7fef7a06 | 517 | goto out_delete_net; |
218d4250 | 518 | |
99a6af52 MN |
519 | if (failed_before_rename) |
520 | goto out_delete_net; | |
521 | ||
0ad19a3f | 522 | /* Create the network configuration */ |
82d5ae15 | 523 | if (clone_flags & CLONE_NEWNET) { |
fae349da | 524 | if (lxc_assign_network(&handler->conf->network, handler->pid)) { |
82d5ae15 | 525 | ERROR("failed to create the configured network"); |
7fef7a06 | 526 | goto out_delete_net; |
82d5ae15 | 527 | } |
0ad19a3f | 528 | } |
529 | ||
530 | /* Tell the child to continue its initialization */ | |
23c53af9 | 531 | if (write(handler->sv[1], &sync, sizeof(sync)) < 0) { |
36eb9bde | 532 | SYSERROR("failed to write the socket"); |
59eb99ba | 533 | goto out_abort; |
0ad19a3f | 534 | } |
535 | ||
536 | /* Wait for the child to exec or returning an error */ | |
23c53af9 | 537 | if (read(handler->sv[1], &sync, sizeof(sync)) < 0) { |
36eb9bde | 538 | ERROR("failed to read the socket"); |
59eb99ba | 539 | goto out_abort; |
0ad19a3f | 540 | } |
541 | ||
23c53af9 | 542 | if (handler->ops->post_start(handler, handler->data)) |
e6126dbe MN |
543 | goto out_abort; |
544 | ||
25c2aca5 | 545 | if (lxc_set_state(name, handler, RUNNING)) { |
59eb99ba DL |
546 | ERROR("failed to set state to %s", |
547 | lxc_state2str(RUNNING)); | |
548 | goto out_abort; | |
3f21c114 | 549 | } |
22ebac19 | 550 | |
23c53af9 | 551 | close(handler->sv[1]); |
e6126dbe | 552 | return 0; |
1ac470c0 | 553 | |
7fef7a06 DL |
554 | out_delete_net: |
555 | if (clone_flags & CLONE_NEWNET) | |
556 | lxc_delete_network(&handler->conf->network); | |
59eb99ba DL |
557 | out_abort: |
558 | lxc_abort(name, handler); | |
23c53af9 | 559 | close(handler->sv[1]); |
b79fcd86 | 560 | return -1; |
59eb99ba | 561 | } |
0ad19a3f | 562 | |
23c53af9 CLG |
563 | struct start_arg { |
564 | char *const *argv; | |
565 | }; | |
566 | ||
567 | static int start(struct lxc_handler *handler, void* data) | |
e6126dbe | 568 | { |
23c53af9 CLG |
569 | struct start_arg *arg = data; |
570 | ||
e6126dbe MN |
571 | NOTICE("exec'ing '%s'", arg->argv[0]); |
572 | ||
573 | execvp(arg->argv[0], arg->argv); | |
574 | SYSERROR("failed to exec %s", arg->argv[0]); | |
575 | return 0; | |
576 | } | |
577 | ||
23c53af9 | 578 | static int post_start(struct lxc_handler *handler, void* data) |
e6126dbe | 579 | { |
23c53af9 CLG |
580 | struct start_arg *arg = data; |
581 | ||
e6126dbe MN |
582 | NOTICE("'%s' started with pid '%d'", arg->argv[0], handler->pid); |
583 | return 0; | |
584 | } | |
585 | ||
586 | static struct lxc_operations start_ops = { | |
587 | .start = start, | |
588 | .post_start = post_start | |
589 | }; | |
590 | ||
fae349da | 591 | int lxc_start(const char *name, char *const argv[], struct lxc_conf *conf) |
59eb99ba | 592 | { |
3a0f472d | 593 | struct lxc_handler *handler; |
e043236e | 594 | int err = -1; |
59eb99ba | 595 | int status; |
23c53af9 CLG |
596 | struct start_arg start_arg = { |
597 | .argv = argv, | |
598 | }; | |
59eb99ba | 599 | |
f2faa8fa | 600 | if (lxc_check_inherited(-1)) |
80090207 CLG |
601 | return -1; |
602 | ||
fae349da | 603 | handler = lxc_init(name, conf); |
3a0f472d | 604 | if (!handler) { |
59eb99ba | 605 | ERROR("failed to initialize the container"); |
66aeffc7 | 606 | return -1; |
0ad19a3f | 607 | } |
e6126dbe | 608 | handler->ops = &start_ops; |
23c53af9 | 609 | handler->data = &start_arg; |
e6126dbe | 610 | |
23c53af9 | 611 | err = lxc_spawn(handler); |
59eb99ba DL |
612 | if (err) { |
613 | ERROR("failed to spawn '%s'", argv[0]); | |
9d7f9e52 | 614 | goto out_fini; |
0ad19a3f | 615 | } |
616 | ||
69ffa311 MN |
617 | /* Avoid signals from terminal */ |
618 | LXC_TTY_ADD_HANDLER(SIGINT); | |
619 | LXC_TTY_ADD_HANDLER(SIGQUIT); | |
620 | ||
3a0f472d | 621 | err = lxc_poll(name, handler); |
e043236e | 622 | if (err) { |
59eb99ba DL |
623 | ERROR("mainloop exited with an error"); |
624 | goto out_abort; | |
625 | } | |
0ad19a3f | 626 | |
3a0f472d | 627 | while (waitpid(handler->pid, &status, 0) < 0 && errno == EINTR) |
1bc5cc8c | 628 | continue; |
e043236e | 629 | |
3a0f472d | 630 | err = lxc_error_set_and_log(handler->pid, status); |
9d7f9e52 | 631 | out_fini: |
69ffa311 MN |
632 | LXC_TTY_DEL_HANDLER(SIGQUIT); |
633 | LXC_TTY_DEL_HANDLER(SIGINT); | |
4d2e2ec6 | 634 | lxc_unlink_nsgroup(name); |
3a0f472d | 635 | lxc_fini(name, handler); |
0ad19a3f | 636 | return err; |
637 | ||
59eb99ba | 638 | out_abort: |
3a0f472d | 639 | lxc_abort(name, handler); |
9d7f9e52 | 640 | goto out_fini; |
0ad19a3f | 641 | } |