]>
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" | |
6a3111b8 | 117 | #include "cgroup.h" |
63376d7d | 118 | #include "log.h" |
563f2f2c | 119 | #include "cgroup.h" |
e2bcd7db | 120 | #include "error.h" |
b0a33c1e | 121 | #include "af_unix.h" |
122 | #include "mainloop.h" | |
63376d7d | 123 | #include "utils.h" |
563f2f2c | 124 | #include "utmp.h" |
63376d7d | 125 | #include "monitor.h" |
96fa1ff0 | 126 | #include "commands.h" |
63376d7d | 127 | #include "console.h" |
3c22086f | 128 | #include "sync.h" |
36eb9bde CLG |
129 | |
130 | lxc_log_define(lxc_start, lxc); | |
131 | ||
0ad19a3f | 132 | LXC_TTY_HANDLER(SIGINT); |
133 | LXC_TTY_HANDLER(SIGQUIT); | |
134 | ||
80090207 CLG |
135 | static int match_fd(int fd) |
136 | { | |
137 | return (fd == 0 || fd == 1 || fd == 2); | |
138 | } | |
139 | ||
f2faa8fa | 140 | int lxc_check_inherited(int fd_to_ignore) |
80090207 CLG |
141 | { |
142 | struct dirent dirent, *direntp; | |
143 | int fd, fddir; | |
144 | DIR *dir; | |
145 | int ret = 0; | |
146 | ||
147 | dir = opendir("/proc/self/fd"); | |
148 | if (!dir) { | |
149 | WARN("failed to open directory: %m"); | |
150 | return -1; | |
151 | } | |
152 | ||
153 | fddir = dirfd(dir); | |
154 | ||
155 | while (!readdir_r(dir, &dirent, &direntp)) { | |
156 | char procpath[64]; | |
157 | char path[PATH_MAX]; | |
158 | ||
159 | if (!direntp) | |
160 | break; | |
161 | ||
162 | if (!strcmp(direntp->d_name, ".")) | |
163 | continue; | |
164 | ||
165 | if (!strcmp(direntp->d_name, "..")) | |
166 | continue; | |
167 | ||
168 | fd = atoi(direntp->d_name); | |
169 | ||
f2faa8fa | 170 | if (fd == fddir || fd == lxc_log_fd || fd == fd_to_ignore) |
80090207 CLG |
171 | continue; |
172 | ||
173 | if (match_fd(fd)) | |
174 | continue; | |
175 | /* | |
176 | * found inherited fd | |
177 | */ | |
178 | ret = -1; | |
179 | ||
180 | snprintf(procpath, sizeof(procpath), "/proc/self/fd/%d", fd); | |
181 | ||
182 | if (readlink(procpath, path, sizeof(path)) == -1) | |
183 | ERROR("readlink(%s) failed : %m", procpath); | |
184 | else | |
185 | ERROR("inherited fd %d on %s", fd, path); | |
186 | } | |
187 | ||
188 | if (closedir(dir)) | |
189 | ERROR("failed to close directory"); | |
190 | return ret; | |
191 | } | |
192 | ||
b0a33c1e | 193 | static int setup_sigchld_fd(sigset_t *oldmask) |
194 | { | |
195 | sigset_t mask; | |
196 | int fd; | |
197 | ||
198 | if (sigprocmask(SIG_BLOCK, NULL, &mask)) { | |
36eb9bde | 199 | SYSERROR("failed to get mask signal"); |
b0a33c1e | 200 | return -1; |
201 | } | |
202 | ||
203 | if (sigaddset(&mask, SIGCHLD) || sigprocmask(SIG_BLOCK, &mask, oldmask)) { | |
36eb9bde | 204 | SYSERROR("failed to set mask signal"); |
b0a33c1e | 205 | return -1; |
206 | } | |
207 | ||
208 | fd = signalfd(-1, &mask, 0); | |
209 | if (fd < 0) { | |
36eb9bde | 210 | SYSERROR("failed to create the signal fd"); |
b0a33c1e | 211 | return -1; |
212 | } | |
213 | ||
214 | if (fcntl(fd, F_SETFD, FD_CLOEXEC)) { | |
36eb9bde | 215 | SYSERROR("failed to set sigfd to close-on-exec"); |
b0a33c1e | 216 | close(fd); |
217 | return -1; | |
218 | } | |
219 | ||
1ac470c0 DL |
220 | DEBUG("sigchild handler set"); |
221 | ||
b0a33c1e | 222 | return fd; |
223 | } | |
224 | ||
15cd25fd | 225 | static int sigchld_handler(int fd, void *data, |
b0a33c1e | 226 | struct lxc_epoll_descr *descr) |
227 | { | |
15cd25fd DL |
228 | struct signalfd_siginfo siginfo; |
229 | int ret; | |
82d89dce | 230 | pid_t *pid = data; |
15cd25fd DL |
231 | |
232 | ret = read(fd, &siginfo, sizeof(siginfo)); | |
233 | if (ret < 0) { | |
234 | ERROR("failed to read sigchld info"); | |
235 | return -1; | |
236 | } | |
237 | ||
238 | if (ret != sizeof(siginfo)) { | |
239 | ERROR("unexpected siginfo size"); | |
240 | return -1; | |
241 | } | |
242 | ||
243 | if (siginfo.ssi_code == CLD_STOPPED || | |
244 | siginfo.ssi_code == CLD_CONTINUED) { | |
245 | INFO("container init process was stopped/continued"); | |
246 | return 0; | |
247 | } | |
1ac470c0 | 248 | |
82d89dce DL |
249 | /* more robustness, protect ourself from a SIGCHLD sent |
250 | * by a process different from the container init | |
251 | */ | |
252 | if (siginfo.ssi_pid != *pid) { | |
253 | WARN("invalid pid for SIGCHLD"); | |
254 | return 0; | |
255 | } | |
256 | ||
15cd25fd | 257 | DEBUG("container init process exited"); |
b0a33c1e | 258 | return 1; |
259 | } | |
260 | ||
0a3ec350 DL |
261 | int lxc_pid_callback(int fd, struct lxc_request *request, |
262 | struct lxc_handler *handler) | |
81c75799 DL |
263 | { |
264 | struct lxc_answer answer; | |
265 | int ret; | |
266 | ||
267 | answer.pid = handler->pid; | |
268 | answer.ret = 0; | |
269 | ||
270 | ret = send(fd, &answer, sizeof(answer), 0); | |
271 | if (ret < 0) { | |
272 | WARN("failed to send answer to the peer"); | |
273 | return -1; | |
274 | } | |
275 | ||
276 | if (ret != sizeof(answer)) { | |
277 | ERROR("partial answer sent"); | |
278 | return -1; | |
279 | } | |
280 | ||
281 | return 0; | |
282 | } | |
283 | ||
25c2aca5 | 284 | int lxc_set_state(const char *name, struct lxc_handler *handler, lxc_state_t state) |
66aeffc7 DL |
285 | { |
286 | handler->state = state; | |
287 | lxc_monitor_send_state(name, state); | |
288 | return 0; | |
289 | } | |
290 | ||
1bc5cc8c | 291 | int lxc_poll(const char *name, struct lxc_handler *handler) |
b0a33c1e | 292 | { |
ca5f7926 DL |
293 | int sigfd = handler->sigfd; |
294 | int pid = handler->pid; | |
b0a33c1e | 295 | struct lxc_epoll_descr descr; |
296 | ||
a9e61274 | 297 | if (lxc_mainloop_open(&descr)) { |
36eb9bde | 298 | ERROR("failed to create mainloop"); |
50c8bf05 | 299 | goto out_sigfd; |
b0a33c1e | 300 | } |
301 | ||
302 | if (lxc_mainloop_add_handler(&descr, sigfd, sigchld_handler, &pid)) { | |
36eb9bde | 303 | ERROR("failed to add handler for the signal"); |
b0a33c1e | 304 | goto out_mainloop_open; |
305 | } | |
306 | ||
63376d7d DL |
307 | if (lxc_console_mainloop_add(&descr, handler)) { |
308 | ERROR("failed to add console handler to mainloop"); | |
309 | goto out_mainloop_open; | |
310 | } | |
311 | ||
563f2f2c DL |
312 | if (lxc_command_mainloop_add(name, &descr, handler)) { |
313 | ERROR("failed to add command handler to mainloop"); | |
96fa1ff0 | 314 | goto out_mainloop_open; |
563f2f2c DL |
315 | } |
316 | ||
317 | if (lxc_utmp_mainloop_add(&descr, handler)) { | |
318 | ERROR("failed to add utmp handler to mainloop"); | |
319 | goto out_mainloop_open; | |
320 | } | |
b0a33c1e | 321 | |
c3e13372 | 322 | return lxc_mainloop(&descr); |
b0a33c1e | 323 | |
324 | out_mainloop_open: | |
325 | lxc_mainloop_close(&descr); | |
b0a33c1e | 326 | out_sigfd: |
327 | close(sigfd); | |
c3e13372 | 328 | return -1; |
b0a33c1e | 329 | } |
330 | ||
fae349da | 331 | struct lxc_handler *lxc_init(const char *name, struct lxc_conf *conf) |
59eb99ba | 332 | { |
3a0f472d DL |
333 | struct lxc_handler *handler; |
334 | ||
335 | handler = malloc(sizeof(*handler)); | |
336 | if (!handler) | |
337 | return NULL; | |
59eb99ba DL |
338 | |
339 | memset(handler, 0, sizeof(*handler)); | |
340 | ||
fae349da DL |
341 | handler->conf = conf; |
342 | ||
3bdf52d7 DL |
343 | handler->name = strdup(name); |
344 | if (!handler->name) { | |
345 | ERROR("failed to allocate memory"); | |
346 | goto out_free; | |
347 | } | |
348 | ||
0ad19a3f | 349 | /* Begin the set the state to STARTING*/ |
25c2aca5 | 350 | if (lxc_set_state(name, handler, STARTING)) { |
59eb99ba | 351 | ERROR("failed to set state '%s'", lxc_state2str(STARTING)); |
3bdf52d7 | 352 | goto out_free_name; |
0ad19a3f | 353 | } |
354 | ||
fae349da | 355 | if (lxc_create_tty(name, conf)) { |
36eb9bde | 356 | ERROR("failed to create the ttys"); |
59eb99ba | 357 | goto out_aborting; |
b0a33c1e | 358 | } |
359 | ||
1560f6c9 | 360 | if (lxc_create_console(conf)) { |
63376d7d DL |
361 | ERROR("failed to create console"); |
362 | goto out_delete_tty; | |
363 | } | |
364 | ||
b0a33c1e | 365 | /* the signal fd has to be created before forking otherwise |
366 | * if the child process exits before we setup the signal fd, | |
367 | * the event will be lost and the command will be stuck */ | |
59eb99ba DL |
368 | handler->sigfd = setup_sigchld_fd(&handler->oldmask); |
369 | if (handler->sigfd < 0) { | |
36eb9bde | 370 | ERROR("failed to set sigchild fd handler"); |
63376d7d | 371 | goto out_delete_console; |
b0a33c1e | 372 | } |
373 | ||
c3e13372 | 374 | INFO("'%s' is initialized", name); |
3a0f472d | 375 | return handler; |
59eb99ba | 376 | |
63376d7d DL |
377 | out_delete_console: |
378 | lxc_delete_console(&conf->console); | |
59eb99ba | 379 | out_delete_tty: |
fae349da | 380 | lxc_delete_tty(&conf->tty_info); |
59eb99ba | 381 | out_aborting: |
25c2aca5 | 382 | lxc_set_state(name, handler, ABORTING); |
3bdf52d7 DL |
383 | out_free_name: |
384 | free(handler->name); | |
385 | handler->name = NULL; | |
3a0f472d DL |
386 | out_free: |
387 | free(handler); | |
c3e13372 | 388 | return NULL; |
59eb99ba DL |
389 | } |
390 | ||
1bc5cc8c | 391 | void lxc_fini(const char *name, struct lxc_handler *handler) |
59eb99ba DL |
392 | { |
393 | /* The STOPPING state is there for future cleanup code | |
394 | * which can take awhile | |
395 | */ | |
25c2aca5 MN |
396 | lxc_set_state(name, handler, STOPPING); |
397 | lxc_set_state(name, handler, STOPPED); | |
59eb99ba | 398 | |
8f64a3f6 MN |
399 | /* reset mask set by setup_sigchld_fd */ |
400 | if (sigprocmask(SIG_SETMASK, &handler->oldmask, NULL)) | |
401 | WARN("failed to restore sigprocmask"); | |
402 | ||
63376d7d | 403 | lxc_delete_console(&handler->conf->console); |
b2431939 | 404 | lxc_delete_tty(&handler->conf->tty_info); |
3bdf52d7 | 405 | free(handler->name); |
b2431939 | 406 | free(handler); |
59eb99ba DL |
407 | } |
408 | ||
1bc5cc8c | 409 | void lxc_abort(const char *name, struct lxc_handler *handler) |
59eb99ba | 410 | { |
25c2aca5 | 411 | lxc_set_state(name, handler, ABORTING); |
7d9fb3e9 DL |
412 | if (handler->pid > 0) |
413 | kill(handler->pid, SIGKILL); | |
59eb99ba DL |
414 | } |
415 | ||
ffe1e01a | 416 | static int do_start(void *data) |
50e98013 | 417 | { |
23c53af9 | 418 | struct lxc_handler *handler = data; |
50e98013 DL |
419 | |
420 | if (sigprocmask(SIG_SETMASK, &handler->oldmask, NULL)) { | |
421 | SYSERROR("failed to set sigprocmask"); | |
9d7f9e52 | 422 | return -1; |
50e98013 DL |
423 | } |
424 | ||
3c22086f | 425 | lxc_sync_fini_parent(handler); |
50e98013 | 426 | |
3c22086f CLG |
427 | /* Tell the parent task it can begin to configure the |
428 | * container and wait for it to finish | |
429 | */ | |
430 | if (lxc_sync_barrier_parent(handler, LXC_SYNC_CONFIGURE)) | |
9d7f9e52 | 431 | return -1; |
50e98013 DL |
432 | |
433 | /* Setup the container, ip, names, utsname, ... */ | |
ffe1e01a | 434 | if (lxc_setup(handler->name, handler->conf)) { |
50e98013 DL |
435 | ERROR("failed to setup the container"); |
436 | goto out_warn_father; | |
437 | } | |
438 | ||
439 | if (prctl(PR_CAPBSET_DROP, CAP_SYS_BOOT, 0, 0, 0)) { | |
440 | SYSERROR("failed to remove CAP_SYS_BOOT capability"); | |
9d7f9e52 | 441 | return -1; |
50e98013 DL |
442 | } |
443 | ||
6a6ad7af DL |
444 | if (prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0)) { |
445 | SYSERROR("failed to set pdeath signal"); | |
9d7f9e52 | 446 | return -1; |
6a6ad7af DL |
447 | } |
448 | ||
fc25b815 MN |
449 | close(handler->sigfd); |
450 | ||
e6126dbe MN |
451 | /* after this call, we are in error because this |
452 | * ops should not return as it execs */ | |
23c53af9 | 453 | if (handler->ops->start(handler, handler->data)) |
e6126dbe | 454 | return -1; |
50e98013 DL |
455 | |
456 | out_warn_father: | |
3c22086f | 457 | lxc_sync_wake_parent(handler, LXC_SYNC_POST_CONFIGURE); |
50e98013 DL |
458 | return -1; |
459 | } | |
460 | ||
23c53af9 | 461 | int lxc_spawn(struct lxc_handler *handler) |
59eb99ba | 462 | { |
59eb99ba | 463 | int clone_flags; |
99a6af52 | 464 | int failed_before_rename = 0; |
ffe1e01a | 465 | const char *name = handler->name; |
50e98013 | 466 | |
3c22086f | 467 | if (lxc_sync_init(handler)) |
9d7f9e52 | 468 | return -1; |
0ad19a3f | 469 | |
1ea6db29 | 470 | clone_flags = CLONE_NEWUTS|CLONE_NEWPID|CLONE_NEWIPC|CLONE_NEWNS; |
fae349da | 471 | if (!lxc_list_empty(&handler->conf->network)) { |
82d5ae15 | 472 | |
0ad19a3f | 473 | clone_flags |= CLONE_NEWNET; |
474 | ||
82d5ae15 DL |
475 | /* that should be done before the clone because we will |
476 | * fill the netdev index and use them in the child | |
477 | */ | |
fae349da | 478 | if (lxc_create_network(&handler->conf->network)) { |
82d5ae15 | 479 | ERROR("failed to create the network"); |
3c22086f | 480 | lxc_sync_fini(handler); |
32e1c760 | 481 | return -1; |
82d5ae15 DL |
482 | } |
483 | } | |
484 | ||
ffe1e01a | 485 | |
0ad19a3f | 486 | /* Create a process in a new set of namespaces */ |
23c53af9 | 487 | handler->pid = lxc_clone(do_start, handler, clone_flags); |
59eb99ba | 488 | if (handler->pid < 0) { |
36eb9bde | 489 | SYSERROR("failed to fork into a new namespace"); |
7fef7a06 | 490 | goto out_delete_net; |
0ad19a3f | 491 | } |
492 | ||
3c22086f CLG |
493 | lxc_sync_fini_child(handler); |
494 | ||
495 | if (lxc_sync_wait_child(handler, LXC_SYNC_CONFIGURE)) | |
99a6af52 | 496 | failed_before_rename = 1; |
0ad19a3f | 497 | |
9f44c578 | 498 | if (lxc_rename_nsgroup(name, handler)) |
7fef7a06 | 499 | goto out_delete_net; |
218d4250 | 500 | |
99a6af52 MN |
501 | if (failed_before_rename) |
502 | goto out_delete_net; | |
503 | ||
0ad19a3f | 504 | /* Create the network configuration */ |
82d5ae15 | 505 | if (clone_flags & CLONE_NEWNET) { |
fae349da | 506 | if (lxc_assign_network(&handler->conf->network, handler->pid)) { |
82d5ae15 | 507 | ERROR("failed to create the configured network"); |
7fef7a06 | 508 | goto out_delete_net; |
82d5ae15 | 509 | } |
0ad19a3f | 510 | } |
511 | ||
3c22086f CLG |
512 | /* Tell the child to continue its initialization and wait for |
513 | * it to exec or return an error | |
514 | */ | |
515 | if (lxc_sync_barrier_child(handler, LXC_SYNC_POST_CONFIGURE)) | |
516 | return -1; | |
0ad19a3f | 517 | |
23c53af9 | 518 | if (handler->ops->post_start(handler, handler->data)) |
e6126dbe MN |
519 | goto out_abort; |
520 | ||
25c2aca5 | 521 | if (lxc_set_state(name, handler, RUNNING)) { |
59eb99ba DL |
522 | ERROR("failed to set state to %s", |
523 | lxc_state2str(RUNNING)); | |
524 | goto out_abort; | |
3f21c114 | 525 | } |
22ebac19 | 526 | |
3c22086f | 527 | lxc_sync_fini(handler); |
e6126dbe | 528 | return 0; |
1ac470c0 | 529 | |
7fef7a06 DL |
530 | out_delete_net: |
531 | if (clone_flags & CLONE_NEWNET) | |
532 | lxc_delete_network(&handler->conf->network); | |
59eb99ba DL |
533 | out_abort: |
534 | lxc_abort(name, handler); | |
3c22086f | 535 | lxc_sync_fini(handler); |
b79fcd86 | 536 | return -1; |
59eb99ba | 537 | } |
0ad19a3f | 538 | |
ee70bf78 CLG |
539 | int __lxc_start(const char *name, struct lxc_conf *conf, |
540 | struct lxc_operations* ops, void *data) | |
59eb99ba | 541 | { |
3a0f472d | 542 | struct lxc_handler *handler; |
e043236e | 543 | int err = -1; |
59eb99ba | 544 | int status; |
80090207 | 545 | |
fae349da | 546 | handler = lxc_init(name, conf); |
3a0f472d | 547 | if (!handler) { |
59eb99ba | 548 | ERROR("failed to initialize the container"); |
66aeffc7 | 549 | return -1; |
0ad19a3f | 550 | } |
ee70bf78 CLG |
551 | handler->ops = ops; |
552 | handler->data = data; | |
e6126dbe | 553 | |
23c53af9 | 554 | err = lxc_spawn(handler); |
59eb99ba | 555 | if (err) { |
ee70bf78 | 556 | ERROR("failed to spawn '%s'", name); |
9d7f9e52 | 557 | goto out_fini; |
0ad19a3f | 558 | } |
559 | ||
69ffa311 MN |
560 | /* Avoid signals from terminal */ |
561 | LXC_TTY_ADD_HANDLER(SIGINT); | |
562 | LXC_TTY_ADD_HANDLER(SIGQUIT); | |
563 | ||
3a0f472d | 564 | err = lxc_poll(name, handler); |
e043236e | 565 | if (err) { |
59eb99ba DL |
566 | ERROR("mainloop exited with an error"); |
567 | goto out_abort; | |
568 | } | |
0ad19a3f | 569 | |
3a0f472d | 570 | while (waitpid(handler->pid, &status, 0) < 0 && errno == EINTR) |
1bc5cc8c | 571 | continue; |
e043236e | 572 | |
3a0f472d | 573 | err = lxc_error_set_and_log(handler->pid, status); |
9d7f9e52 | 574 | out_fini: |
69ffa311 MN |
575 | LXC_TTY_DEL_HANDLER(SIGQUIT); |
576 | LXC_TTY_DEL_HANDLER(SIGINT); | |
4d2e2ec6 | 577 | lxc_unlink_nsgroup(name); |
3a0f472d | 578 | lxc_fini(name, handler); |
0ad19a3f | 579 | return err; |
580 | ||
59eb99ba | 581 | out_abort: |
3a0f472d | 582 | lxc_abort(name, handler); |
9d7f9e52 | 583 | goto out_fini; |
0ad19a3f | 584 | } |
ee70bf78 CLG |
585 | |
586 | struct start_args { | |
587 | char *const *argv; | |
588 | }; | |
589 | ||
590 | static int start(struct lxc_handler *handler, void* data) | |
591 | { | |
592 | struct start_args *arg = data; | |
593 | ||
594 | NOTICE("exec'ing '%s'", arg->argv[0]); | |
595 | ||
596 | execvp(arg->argv[0], arg->argv); | |
597 | SYSERROR("failed to exec %s", arg->argv[0]); | |
598 | return 0; | |
599 | } | |
600 | ||
601 | static int post_start(struct lxc_handler *handler, void* data) | |
602 | { | |
603 | struct start_args *arg = data; | |
604 | ||
605 | NOTICE("'%s' started with pid '%d'", arg->argv[0], handler->pid); | |
606 | return 0; | |
607 | } | |
608 | ||
609 | static struct lxc_operations start_ops = { | |
610 | .start = start, | |
611 | .post_start = post_start | |
612 | }; | |
613 | ||
614 | int lxc_start(const char *name, char *const argv[], struct lxc_conf *conf) | |
615 | { | |
616 | struct start_args start_arg = { | |
617 | .argv = argv, | |
618 | }; | |
619 | ||
620 | if (lxc_check_inherited(-1)) | |
621 | return -1; | |
622 | ||
623 | return __lxc_start(name, conf, &start_ops, &start_arg); | |
624 | } |