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