]>
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> |
0ad19a3f | 39 | #include <sys/types.h> |
0ad19a3f | 40 | #include <sys/prctl.h> |
ddceb1f9 | 41 | #include <sys/types.h> |
42ff343d | 42 | #include <sys/capability.h> |
0ad19a3f | 43 | #include <sys/wait.h> |
b0a33c1e | 44 | #include <sys/un.h> |
45 | #include <sys/poll.h> | |
ff218c25 | 46 | |
47 | #ifdef HAVE_SYS_SIGNALFD_H | |
8ca61733 | 48 | # include <sys/signalfd.h> |
ff218c25 | 49 | #else |
8ca61733 MJ |
50 | # ifndef __NR_signalfd4 |
51 | /* assume kernel headers are too old */ | |
52 | # if __i386__ | |
53 | # define __NR_signalfd4 327 | |
54 | # elif __x86_64__ | |
55 | # define __NR_signalfd4 289 | |
bfa38025 MH |
56 | # elif __powerpc__ |
57 | # define __NR_signalfd4 313 | |
47f38330 SH |
58 | # elif __s390x__ |
59 | # define __NR_signalfd4 322 | |
8ca61733 MJ |
60 | # endif |
61 | #endif | |
62 | ||
63 | # ifndef __NR_signalfd | |
64 | /* assume kernel headers are too old */ | |
65 | # if __i386__ | |
66 | # define __NR_signalfd 321 | |
67 | # elif __x86_64__ | |
68 | # define __NR_signalfd 282 | |
bfa38025 MH |
69 | # elif __powerpc__ |
70 | # define __NR_signalfd 305 | |
47f38330 SH |
71 | # elif __s390x__ |
72 | # define __NR_signalfd 316 | |
8ca61733 MJ |
73 | # endif |
74 | #endif | |
75 | ||
76 | int signalfd(int fd, const sigset_t *mask, int flags) | |
77 | { | |
78 | int retval; | |
79 | ||
80 | retval = syscall (__NR_signalfd4, fd, mask, _NSIG / 8, flags); | |
81 | if (errno == ENOSYS && flags == 0) | |
82 | retval = syscall (__NR_signalfd, fd, mask, _NSIG / 8); | |
83 | return retval; | |
84 | } | |
ff218c25 | 85 | #endif |
0ad19a3f | 86 | |
656994bb MH |
87 | #if !HAVE_DECL_PR_CAPBSET_DROP |
88 | #define PR_CAPBSET_DROP 24 | |
89 | #endif | |
90 | ||
e2bcd7db | 91 | #include "error.h" |
b0a33c1e | 92 | #include "af_unix.h" |
93 | #include "mainloop.h" | |
e2bcd7db | 94 | |
b113348e | 95 | #include <lxc/lxc.h> |
36eb9bde CLG |
96 | #include <lxc/log.h> |
97 | ||
98 | lxc_log_define(lxc_start, lxc); | |
99 | ||
0ad19a3f | 100 | LXC_TTY_HANDLER(SIGINT); |
101 | LXC_TTY_HANDLER(SIGQUIT); | |
102 | ||
3a0f472d DL |
103 | struct lxc_handler { |
104 | int sigfd; | |
105 | int lock; | |
106 | pid_t pid; | |
107 | char tty[MAXPATHLEN]; | |
108 | sigset_t oldmask; | |
109 | struct lxc_tty_info tty_info; | |
110 | }; | |
111 | ||
b0a33c1e | 112 | static int setup_sigchld_fd(sigset_t *oldmask) |
113 | { | |
114 | sigset_t mask; | |
115 | int fd; | |
116 | ||
117 | if (sigprocmask(SIG_BLOCK, NULL, &mask)) { | |
36eb9bde | 118 | SYSERROR("failed to get mask signal"); |
b0a33c1e | 119 | return -1; |
120 | } | |
121 | ||
122 | if (sigaddset(&mask, SIGCHLD) || sigprocmask(SIG_BLOCK, &mask, oldmask)) { | |
36eb9bde | 123 | SYSERROR("failed to set mask signal"); |
b0a33c1e | 124 | return -1; |
125 | } | |
126 | ||
127 | fd = signalfd(-1, &mask, 0); | |
128 | if (fd < 0) { | |
36eb9bde | 129 | SYSERROR("failed to create the signal fd"); |
b0a33c1e | 130 | return -1; |
131 | } | |
132 | ||
133 | if (fcntl(fd, F_SETFD, FD_CLOEXEC)) { | |
36eb9bde | 134 | SYSERROR("failed to set sigfd to close-on-exec"); |
b0a33c1e | 135 | close(fd); |
136 | return -1; | |
137 | } | |
138 | ||
139 | return fd; | |
140 | } | |
141 | ||
142 | static int setup_tty_service(const char *name, int *ttyfd) | |
143 | { | |
144 | int fd; | |
145 | struct sockaddr_un addr = { 0 }; | |
146 | char *offset = &addr.sun_path[1]; | |
147 | ||
148 | strcpy(offset, name); | |
149 | addr.sun_path[0] = '\0'; | |
150 | ||
151 | fd = lxc_af_unix_open(addr.sun_path, SOCK_STREAM, 0); | |
152 | if (fd < 0) | |
153 | return -1; | |
154 | ||
155 | if (fcntl(fd, F_SETFD, FD_CLOEXEC)) { | |
36eb9bde | 156 | SYSERROR("failed to close-on-exec flag"); |
b0a33c1e | 157 | close(fd); |
158 | return -1; | |
159 | } | |
160 | ||
161 | *ttyfd = fd; | |
162 | ||
163 | return 0; | |
164 | } | |
165 | ||
166 | static int sigchld_handler(int fd, void *data, | |
167 | struct lxc_epoll_descr *descr) | |
168 | { | |
b0a33c1e | 169 | return 1; |
170 | } | |
171 | ||
172 | static int ttyclient_handler(int fd, void *data, | |
173 | struct lxc_epoll_descr *descr) | |
174 | { | |
175 | int i; | |
176 | struct lxc_tty_info *tty_info = data; | |
177 | ||
178 | for (i = 0; i < tty_info->nbtty; i++) { | |
179 | ||
180 | if (tty_info->pty_info[i].busy != fd) | |
181 | continue; | |
182 | ||
183 | lxc_mainloop_del_handler(descr, fd); | |
184 | tty_info->pty_info[i].busy = 0; | |
185 | close(fd); | |
186 | } | |
187 | ||
188 | return 0; | |
189 | } | |
190 | ||
191 | static int ttyservice_handler(int fd, void *data, | |
192 | struct lxc_epoll_descr *descr) | |
193 | { | |
194 | int conn, ttynum, val = 1, ret = -1; | |
195 | struct lxc_tty_info *tty_info = data; | |
196 | ||
197 | conn = accept(fd, NULL, 0); | |
198 | if (conn < 0) { | |
36eb9bde | 199 | SYSERROR("failed to accept tty client"); |
b0a33c1e | 200 | return -1; |
201 | } | |
202 | ||
203 | if (setsockopt(conn, SOL_SOCKET, SO_PASSCRED, &val, sizeof(val))) { | |
36eb9bde | 204 | SYSERROR("failed to enable credential on socket"); |
b0a33c1e | 205 | goto out_close; |
206 | } | |
207 | ||
208 | if (lxc_af_unix_rcv_credential(conn, &ttynum, sizeof(ttynum))) | |
209 | goto out_close; | |
210 | ||
be43f17e DL |
211 | if (ttynum > 0) { |
212 | if (ttynum > tty_info->nbtty) | |
213 | goto out_close; | |
214 | ||
215 | if (tty_info->pty_info[ttynum - 1].busy) | |
216 | goto out_close; | |
b0a33c1e | 217 | |
be43f17e DL |
218 | goto out_send; |
219 | } | |
b0a33c1e | 220 | |
be43f17e DL |
221 | /* fixup index tty1 => [0] */ |
222 | for (ttynum = 1; | |
223 | ttynum <= tty_info->nbtty && tty_info->pty_info[ttynum - 1].busy; | |
224 | ttynum++); | |
225 | ||
226 | /* we didn't find any available slot for tty */ | |
227 | if (ttynum > tty_info->nbtty) | |
b0a33c1e | 228 | goto out_close; |
229 | ||
be43f17e DL |
230 | out_send: |
231 | if (lxc_af_unix_send_fd(conn, tty_info->pty_info[ttynum - 1].master, | |
232 | &ttynum, sizeof(ttynum)) < 0) { | |
36eb9bde | 233 | ERROR("failed to send tty to client"); |
b0a33c1e | 234 | goto out_close; |
235 | } | |
236 | ||
be43f17e | 237 | if (lxc_mainloop_add_handler(descr, conn, |
b0a33c1e | 238 | ttyclient_handler, tty_info)) { |
36eb9bde | 239 | ERROR("failed to add tty client handler"); |
b0a33c1e | 240 | goto out_close; |
241 | } | |
242 | ||
be43f17e | 243 | tty_info->pty_info[ttynum - 1].busy = conn; |
b0a33c1e | 244 | |
245 | ret = 0; | |
b0a33c1e | 246 | out: |
247 | return ret; | |
248 | out_close: | |
249 | close(conn); | |
250 | goto out; | |
251 | } | |
252 | ||
1bc5cc8c | 253 | int lxc_poll(const char *name, struct lxc_handler *handler) |
b0a33c1e | 254 | { |
ca5f7926 DL |
255 | int sigfd = handler->sigfd; |
256 | int pid = handler->pid; | |
257 | const struct lxc_tty_info *tty_info = &handler->tty_info; | |
258 | ||
b0a33c1e | 259 | int nfds, ttyfd = -1, ret = -1; |
260 | struct lxc_epoll_descr descr; | |
261 | ||
262 | if (tty_info->nbtty && setup_tty_service(name, &ttyfd)) { | |
36eb9bde | 263 | ERROR("failed to create the tty service point"); |
b0a33c1e | 264 | goto out_sigfd; |
265 | } | |
266 | ||
267 | /* sigfd + nb tty + tty service | |
268 | * if tty is enabled */ | |
269 | nfds = tty_info->nbtty + 1 + tty_info->nbtty ? 1 : 0; | |
270 | ||
271 | if (lxc_mainloop_open(nfds, &descr)) { | |
36eb9bde | 272 | ERROR("failed to create mainloop"); |
b0a33c1e | 273 | goto out_ttyfd; |
274 | } | |
275 | ||
276 | if (lxc_mainloop_add_handler(&descr, sigfd, sigchld_handler, &pid)) { | |
36eb9bde | 277 | ERROR("failed to add handler for the signal"); |
b0a33c1e | 278 | goto out_mainloop_open; |
279 | } | |
280 | ||
281 | if (tty_info->nbtty) { | |
282 | if (lxc_mainloop_add_handler(&descr, ttyfd, | |
283 | ttyservice_handler, | |
284 | (void *)tty_info)) { | |
36eb9bde | 285 | ERROR("failed to add handler for the tty"); |
b0a33c1e | 286 | goto out_mainloop_open; |
287 | } | |
288 | } | |
289 | ||
290 | ret = lxc_mainloop(&descr); | |
291 | ||
292 | out: | |
293 | return ret; | |
294 | ||
295 | out_mainloop_open: | |
296 | lxc_mainloop_close(&descr); | |
297 | out_ttyfd: | |
298 | close(ttyfd); | |
299 | out_sigfd: | |
300 | close(sigfd); | |
301 | goto out; | |
302 | } | |
303 | ||
59eb99ba | 304 | static int save_init_pid(const char *name, pid_t pid) |
0ad19a3f | 305 | { |
22ebac19 | 306 | char init[MAXPATHLEN]; |
59eb99ba DL |
307 | char *val; |
308 | int fd, err = -1; | |
309 | ||
310 | snprintf(init, MAXPATHLEN, LXCPATH "/%s/init", name); | |
311 | ||
312 | if (!asprintf(&val, "%d\n", pid)) { | |
313 | SYSERROR("failed to allocate memory"); | |
314 | goto out; | |
315 | } | |
316 | ||
317 | fd = open(init, O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR); | |
318 | if (fd < 0) { | |
319 | SYSERROR("failed to open '%s'", init); | |
320 | goto out_free; | |
321 | } | |
322 | ||
323 | if (write(fd, val, strlen(val)) < 0) { | |
324 | SYSERROR("failed to write the init pid"); | |
325 | goto out_close; | |
326 | } | |
f4d507d5 | 327 | |
59eb99ba DL |
328 | err = 0; |
329 | ||
330 | out_close: | |
331 | close(fd); | |
332 | out_free: | |
333 | free(val); | |
334 | out: | |
335 | return err; | |
336 | } | |
337 | ||
338 | static void remove_init_pid(const char *name, pid_t pid) | |
339 | { | |
340 | char init[MAXPATHLEN]; | |
341 | ||
342 | snprintf(init, MAXPATHLEN, LXCPATH "/%s/init", name); | |
343 | unlink(init); | |
344 | } | |
345 | ||
b8f57738 DL |
346 | static int fdname(int fd, char *name, size_t size) |
347 | { | |
348 | char path[MAXPATHLEN]; | |
acc86941 | 349 | ssize_t len; |
b8f57738 DL |
350 | |
351 | snprintf(path, MAXPATHLEN, "/proc/self/fd/%d", fd); | |
352 | ||
acc86941 MN |
353 | len = readlink(path, name, size); |
354 | if (len > 0) | |
355 | path[len] = '\0'; | |
356 | ||
357 | return (len <= 0) ? -1 : 0; | |
b8f57738 DL |
358 | } |
359 | ||
360 | static int console_init(char *console, size_t size) | |
361 | { | |
362 | struct stat stat; | |
363 | int i; | |
364 | ||
365 | for (i = 0; i < 3; i++) { | |
366 | if (!isatty(i)) | |
367 | continue; | |
368 | ||
369 | if (ttyname_r(i, console, size)) { | |
370 | SYSERROR("failed to retrieve tty name"); | |
371 | return -1; | |
372 | } | |
af795875 | 373 | |
b8f57738 DL |
374 | return 0; |
375 | } | |
376 | ||
377 | if (!fstat(0, &stat)) { | |
378 | if (S_ISREG(stat.st_mode) || S_ISCHR(stat.st_mode) || | |
379 | S_ISFIFO(stat.st_mode) || S_ISLNK(stat.st_mode)) | |
380 | return fdname(0, console, size); | |
381 | } | |
382 | ||
383 | console[0] = '\0'; | |
384 | return 0; | |
385 | } | |
386 | ||
3a0f472d | 387 | struct lxc_handler *lxc_init(const char *name) |
59eb99ba | 388 | { |
3a0f472d DL |
389 | struct lxc_handler *handler; |
390 | ||
391 | handler = malloc(sizeof(*handler)); | |
392 | if (!handler) | |
393 | return NULL; | |
59eb99ba DL |
394 | |
395 | memset(handler, 0, sizeof(*handler)); | |
396 | ||
397 | handler->lock = lxc_get_lock(name); | |
398 | if (handler->lock < 0) | |
3a0f472d | 399 | goto out_free; |
0ad19a3f | 400 | |
0ad19a3f | 401 | /* Begin the set the state to STARTING*/ |
402 | if (lxc_setstate(name, STARTING)) { | |
59eb99ba DL |
403 | ERROR("failed to set state '%s'", lxc_state2str(STARTING)); |
404 | goto out_put_lock; | |
0ad19a3f | 405 | } |
406 | ||
b8f57738 DL |
407 | if (console_init(handler->tty, sizeof(handler->tty))) { |
408 | ERROR("failed to initialize the console"); | |
409 | goto out_aborting; | |
410 | } | |
939229eb | 411 | |
59eb99ba | 412 | if (lxc_create_tty(name, &handler->tty_info)) { |
36eb9bde | 413 | ERROR("failed to create the ttys"); |
59eb99ba | 414 | goto out_aborting; |
b0a33c1e | 415 | } |
416 | ||
417 | /* the signal fd has to be created before forking otherwise | |
418 | * if the child process exits before we setup the signal fd, | |
419 | * the event will be lost and the command will be stuck */ | |
59eb99ba DL |
420 | handler->sigfd = setup_sigchld_fd(&handler->oldmask); |
421 | if (handler->sigfd < 0) { | |
36eb9bde | 422 | ERROR("failed to set sigchild fd handler"); |
59eb99ba | 423 | goto out_delete_tty; |
b0a33c1e | 424 | } |
425 | ||
59eb99ba DL |
426 | /* Avoid signals from terminal */ |
427 | LXC_TTY_ADD_HANDLER(SIGINT); | |
428 | LXC_TTY_ADD_HANDLER(SIGQUIT); | |
429 | ||
59eb99ba | 430 | out: |
3a0f472d | 431 | return handler; |
59eb99ba DL |
432 | |
433 | out_delete_tty: | |
434 | lxc_delete_tty(&handler->tty_info); | |
435 | out_aborting: | |
436 | lxc_setstate(name, ABORTING); | |
437 | out_put_lock: | |
438 | lxc_put_lock(handler->lock); | |
3a0f472d DL |
439 | out_free: |
440 | free(handler); | |
441 | handler = NULL; | |
59eb99ba DL |
442 | goto out; |
443 | } | |
444 | ||
1bc5cc8c | 445 | void lxc_fini(const char *name, struct lxc_handler *handler) |
59eb99ba DL |
446 | { |
447 | /* The STOPPING state is there for future cleanup code | |
448 | * which can take awhile | |
449 | */ | |
450 | lxc_setstate(name, STOPPING); | |
59eb99ba | 451 | lxc_setstate(name, STOPPED); |
59eb99ba DL |
452 | lxc_unlink_nsgroup(name); |
453 | ||
3a0f472d DL |
454 | if (handler) { |
455 | remove_init_pid(name, handler->pid); | |
456 | lxc_delete_tty(&handler->tty_info); | |
457 | lxc_put_lock(handler->lock); | |
458 | free(handler); | |
459 | } | |
59eb99ba DL |
460 | |
461 | LXC_TTY_DEL_HANDLER(SIGQUIT); | |
462 | LXC_TTY_DEL_HANDLER(SIGINT); | |
463 | } | |
464 | ||
1bc5cc8c | 465 | void lxc_abort(const char *name, struct lxc_handler *handler) |
59eb99ba DL |
466 | { |
467 | lxc_setstate(name, ABORTING); | |
468 | kill(handler->pid, SIGKILL); | |
469 | } | |
470 | ||
50e98013 DL |
471 | struct start_arg { |
472 | const char *name; | |
473 | char *const *argv; | |
474 | struct lxc_handler *handler; | |
475 | int *sv; | |
476 | }; | |
477 | ||
478 | static int do_start(void *arg) | |
479 | { | |
480 | struct start_arg *start_arg = arg; | |
481 | struct lxc_handler *handler = start_arg->handler; | |
482 | const char *name = start_arg->name; | |
483 | char *const *argv = start_arg->argv; | |
484 | int *sv = start_arg->sv; | |
485 | int err = -1, sync; | |
486 | ||
487 | if (sigprocmask(SIG_SETMASK, &handler->oldmask, NULL)) { | |
488 | SYSERROR("failed to set sigprocmask"); | |
489 | goto out_child; | |
490 | } | |
491 | ||
492 | close(sv[1]); | |
493 | ||
494 | /* Be sure we don't inherit this after the exec */ | |
495 | fcntl(sv[0], F_SETFD, FD_CLOEXEC); | |
496 | ||
497 | /* Tell our father he can begin to configure the container */ | |
498 | if (write(sv[0], &sync, sizeof(sync)) < 0) { | |
499 | SYSERROR("failed to write socket"); | |
500 | goto out_child; | |
501 | } | |
502 | ||
503 | /* Wait for the father to finish the configuration */ | |
504 | if (read(sv[0], &sync, sizeof(sync)) < 0) { | |
505 | SYSERROR("failed to read socket"); | |
506 | goto out_child; | |
507 | } | |
508 | ||
509 | /* Setup the container, ip, names, utsname, ... */ | |
510 | if (lxc_setup(name, handler->tty, &handler->tty_info)) { | |
511 | ERROR("failed to setup the container"); | |
512 | goto out_warn_father; | |
513 | } | |
514 | ||
515 | if (prctl(PR_CAPBSET_DROP, CAP_SYS_BOOT, 0, 0, 0)) { | |
516 | SYSERROR("failed to remove CAP_SYS_BOOT capability"); | |
517 | goto out_child; | |
518 | } | |
519 | ||
520 | execvp(argv[0], argv); | |
521 | SYSERROR("failed to exec %s", argv[0]); | |
522 | ||
523 | out_warn_father: | |
524 | /* If the exec fails, tell that to our father */ | |
525 | if (write(sv[0], &err, sizeof(err)) < 0) | |
526 | SYSERROR("failed to write the socket"); | |
527 | out_child: | |
528 | return -1; | |
529 | } | |
530 | ||
9618063c | 531 | int lxc_spawn(const char *name, struct lxc_handler *handler, char *const argv[]) |
59eb99ba DL |
532 | { |
533 | int sv[2]; | |
534 | int clone_flags; | |
535 | int err = -1, sync; | |
536 | ||
50e98013 DL |
537 | struct start_arg start_arg = { |
538 | .name = name, | |
539 | .argv = argv, | |
540 | .handler = handler, | |
541 | .sv = sv, | |
542 | }; | |
543 | ||
0ad19a3f | 544 | /* Synchro socketpair */ |
545 | if (socketpair(AF_LOCAL, SOCK_STREAM, 0, sv)) { | |
36eb9bde | 546 | SYSERROR("failed to create communication socketpair"); |
f4d507d5 | 547 | goto out; |
0ad19a3f | 548 | } |
549 | ||
1ea6db29 | 550 | clone_flags = CLONE_NEWUTS|CLONE_NEWPID|CLONE_NEWIPC|CLONE_NEWNS; |
0ad19a3f | 551 | if (conf_has_network(name)) |
552 | clone_flags |= CLONE_NEWNET; | |
553 | ||
554 | /* Create a process in a new set of namespaces */ | |
50e98013 | 555 | handler->pid = lxc_clone(do_start, &start_arg, clone_flags); |
59eb99ba | 556 | if (handler->pid < 0) { |
36eb9bde | 557 | SYSERROR("failed to fork into a new namespace"); |
59eb99ba | 558 | goto out_close; |
0ad19a3f | 559 | } |
560 | ||
0ad19a3f | 561 | close(sv[0]); |
562 | ||
563 | /* Wait for the child to be ready */ | |
564 | if (read(sv[1], &sync, sizeof(sync)) < 0) { | |
36eb9bde | 565 | SYSERROR("failed to read the socket"); |
59eb99ba | 566 | goto out_abort; |
0ad19a3f | 567 | } |
568 | ||
6203de18 | 569 | if (lxc_rename_nsgroup(name, handler->pid) || lxc_link_nsgroup(name)) |
2b31f553 | 570 | goto out_abort; |
218d4250 | 571 | |
0ad19a3f | 572 | /* Create the network configuration */ |
6203de18 DL |
573 | if (clone_flags & CLONE_NEWNET && |
574 | conf_create_network(name, handler->pid)) { | |
36eb9bde | 575 | ERROR("failed to create the configured network"); |
59eb99ba | 576 | goto out_abort; |
0ad19a3f | 577 | } |
578 | ||
579 | /* Tell the child to continue its initialization */ | |
580 | if (write(sv[1], &sync, sizeof(sync)) < 0) { | |
36eb9bde | 581 | SYSERROR("failed to write the socket"); |
59eb99ba | 582 | goto out_abort; |
0ad19a3f | 583 | } |
584 | ||
585 | /* Wait for the child to exec or returning an error */ | |
e043236e | 586 | if (read(sv[1], &sync, sizeof(sync)) < 0) { |
36eb9bde | 587 | ERROR("failed to read the socket"); |
59eb99ba | 588 | goto out_abort; |
0ad19a3f | 589 | } |
590 | ||
59eb99ba DL |
591 | if (save_init_pid(name, handler->pid)) { |
592 | ERROR("failed to save the init pid info"); | |
593 | goto out_abort; | |
0ad19a3f | 594 | } |
595 | ||
59eb99ba DL |
596 | if (lxc_setstate(name, RUNNING)) { |
597 | ERROR("failed to set state to %s", | |
598 | lxc_state2str(RUNNING)); | |
599 | goto out_abort; | |
3f21c114 | 600 | } |
22ebac19 | 601 | |
59eb99ba | 602 | err = 0; |
22ebac19 | 603 | |
59eb99ba DL |
604 | out_close: |
605 | close(sv[0]); | |
606 | close(sv[1]); | |
607 | out: | |
608 | return err; | |
0ad19a3f | 609 | |
59eb99ba DL |
610 | out_abort: |
611 | lxc_abort(name, handler); | |
612 | goto out_close; | |
613 | } | |
0ad19a3f | 614 | |
9618063c | 615 | int lxc_start(const char *name, char *const argv[]) |
59eb99ba | 616 | { |
3a0f472d | 617 | struct lxc_handler *handler; |
e043236e | 618 | int err = -1; |
59eb99ba DL |
619 | int status; |
620 | ||
3a0f472d DL |
621 | handler = lxc_init(name); |
622 | if (!handler) { | |
59eb99ba DL |
623 | ERROR("failed to initialize the container"); |
624 | goto out; | |
0ad19a3f | 625 | } |
626 | ||
3a0f472d | 627 | err = lxc_spawn(name, handler, argv); |
59eb99ba DL |
628 | if (err) { |
629 | ERROR("failed to spawn '%s'", argv[0]); | |
630 | goto out; | |
0ad19a3f | 631 | } |
632 | ||
af795875 | 633 | err = lxc_close_all_inherited_fd(); |
d983b93c MN |
634 | if (err) { |
635 | ERROR("unable to close inherited fds"); | |
636 | goto out_abort; | |
637 | } | |
638 | ||
3a0f472d | 639 | err = lxc_poll(name, handler); |
e043236e | 640 | if (err) { |
59eb99ba DL |
641 | ERROR("mainloop exited with an error"); |
642 | goto out_abort; | |
643 | } | |
0ad19a3f | 644 | |
3a0f472d | 645 | while (waitpid(handler->pid, &status, 0) < 0 && errno == EINTR) |
1bc5cc8c | 646 | continue; |
e043236e | 647 | |
3a0f472d | 648 | err = lxc_error_set_and_log(handler->pid, status); |
0ad19a3f | 649 | out: |
3a0f472d | 650 | lxc_fini(name, handler); |
0ad19a3f | 651 | return err; |
652 | ||
59eb99ba | 653 | out_abort: |
3a0f472d | 654 | lxc_abort(name, handler); |
0ad19a3f | 655 | goto out; |
656 | } |