]>
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 | |
48 | #ifdef HAVE_SYS_SIGNALFD_H | |
8ca61733 | 49 | # include <sys/signalfd.h> |
ff218c25 | 50 | #else |
8ca61733 MJ |
51 | # ifndef __NR_signalfd4 |
52 | /* assume kernel headers are too old */ | |
53 | # if __i386__ | |
54 | # define __NR_signalfd4 327 | |
55 | # elif __x86_64__ | |
56 | # define __NR_signalfd4 289 | |
bfa38025 MH |
57 | # elif __powerpc__ |
58 | # define __NR_signalfd4 313 | |
47f38330 SH |
59 | # elif __s390x__ |
60 | # define __NR_signalfd4 322 | |
8ca61733 MJ |
61 | # endif |
62 | #endif | |
63 | ||
64 | # ifndef __NR_signalfd | |
65 | /* assume kernel headers are too old */ | |
66 | # if __i386__ | |
67 | # define __NR_signalfd 321 | |
68 | # elif __x86_64__ | |
69 | # define __NR_signalfd 282 | |
bfa38025 MH |
70 | # elif __powerpc__ |
71 | # define __NR_signalfd 305 | |
47f38330 SH |
72 | # elif __s390x__ |
73 | # define __NR_signalfd 316 | |
8ca61733 MJ |
74 | # endif |
75 | #endif | |
76 | ||
77 | int signalfd(int fd, const sigset_t *mask, int flags) | |
78 | { | |
79 | int retval; | |
80 | ||
81 | retval = syscall (__NR_signalfd4, fd, mask, _NSIG / 8, flags); | |
82 | if (errno == ENOSYS && flags == 0) | |
83 | retval = syscall (__NR_signalfd, fd, mask, _NSIG / 8); | |
84 | return retval; | |
85 | } | |
ff218c25 | 86 | #endif |
0ad19a3f | 87 | |
656994bb MH |
88 | #if !HAVE_DECL_PR_CAPBSET_DROP |
89 | #define PR_CAPBSET_DROP 24 | |
90 | #endif | |
91 | ||
63376d7d DL |
92 | #include "start.h" |
93 | #include "conf.h" | |
94 | #include "log.h" | |
e2bcd7db | 95 | #include "error.h" |
b0a33c1e | 96 | #include "af_unix.h" |
97 | #include "mainloop.h" | |
63376d7d DL |
98 | #include "utils.h" |
99 | #include "monitor.h" | |
96fa1ff0 | 100 | #include "commands.h" |
63376d7d | 101 | #include "console.h" |
36eb9bde CLG |
102 | |
103 | lxc_log_define(lxc_start, lxc); | |
104 | ||
0ad19a3f | 105 | LXC_TTY_HANDLER(SIGINT); |
106 | LXC_TTY_HANDLER(SIGQUIT); | |
107 | ||
b0a33c1e | 108 | static int setup_sigchld_fd(sigset_t *oldmask) |
109 | { | |
110 | sigset_t mask; | |
111 | int fd; | |
112 | ||
113 | if (sigprocmask(SIG_BLOCK, NULL, &mask)) { | |
36eb9bde | 114 | SYSERROR("failed to get mask signal"); |
b0a33c1e | 115 | return -1; |
116 | } | |
117 | ||
118 | if (sigaddset(&mask, SIGCHLD) || sigprocmask(SIG_BLOCK, &mask, oldmask)) { | |
36eb9bde | 119 | SYSERROR("failed to set mask signal"); |
b0a33c1e | 120 | return -1; |
121 | } | |
122 | ||
123 | fd = signalfd(-1, &mask, 0); | |
124 | if (fd < 0) { | |
36eb9bde | 125 | SYSERROR("failed to create the signal fd"); |
b0a33c1e | 126 | return -1; |
127 | } | |
128 | ||
129 | if (fcntl(fd, F_SETFD, FD_CLOEXEC)) { | |
36eb9bde | 130 | SYSERROR("failed to set sigfd to close-on-exec"); |
b0a33c1e | 131 | close(fd); |
132 | return -1; | |
133 | } | |
134 | ||
1ac470c0 DL |
135 | DEBUG("sigchild handler set"); |
136 | ||
b0a33c1e | 137 | return fd; |
138 | } | |
139 | ||
b0a33c1e | 140 | static int sigchld_handler(int fd, void *data, |
141 | struct lxc_epoll_descr *descr) | |
142 | { | |
1ac470c0 DL |
143 | DEBUG("child exited"); |
144 | ||
b0a33c1e | 145 | return 1; |
146 | } | |
147 | ||
25c2aca5 | 148 | int lxc_set_state(const char *name, struct lxc_handler *handler, lxc_state_t state) |
66aeffc7 DL |
149 | { |
150 | handler->state = state; | |
151 | lxc_monitor_send_state(name, state); | |
152 | return 0; | |
153 | } | |
154 | ||
1bc5cc8c | 155 | int lxc_poll(const char *name, struct lxc_handler *handler) |
b0a33c1e | 156 | { |
ca5f7926 DL |
157 | int sigfd = handler->sigfd; |
158 | int pid = handler->pid; | |
b0a33c1e | 159 | struct lxc_epoll_descr descr; |
160 | ||
a9e61274 | 161 | if (lxc_mainloop_open(&descr)) { |
36eb9bde | 162 | ERROR("failed to create mainloop"); |
50c8bf05 | 163 | goto out_sigfd; |
b0a33c1e | 164 | } |
165 | ||
166 | if (lxc_mainloop_add_handler(&descr, sigfd, sigchld_handler, &pid)) { | |
36eb9bde | 167 | ERROR("failed to add handler for the signal"); |
b0a33c1e | 168 | goto out_mainloop_open; |
169 | } | |
170 | ||
63376d7d DL |
171 | if (lxc_console_mainloop_add(&descr, handler)) { |
172 | ERROR("failed to add console handler to mainloop"); | |
173 | goto out_mainloop_open; | |
174 | } | |
175 | ||
724e753c | 176 | if (lxc_command_mainloop_add(name, &descr, handler)) |
96fa1ff0 | 177 | goto out_mainloop_open; |
b0a33c1e | 178 | |
c3e13372 | 179 | return lxc_mainloop(&descr); |
b0a33c1e | 180 | |
181 | out_mainloop_open: | |
182 | lxc_mainloop_close(&descr); | |
b0a33c1e | 183 | out_sigfd: |
184 | close(sigfd); | |
c3e13372 | 185 | return -1; |
b0a33c1e | 186 | } |
187 | ||
fae349da | 188 | struct lxc_handler *lxc_init(const char *name, struct lxc_conf *conf) |
59eb99ba | 189 | { |
3a0f472d DL |
190 | struct lxc_handler *handler; |
191 | ||
192 | handler = malloc(sizeof(*handler)); | |
193 | if (!handler) | |
194 | return NULL; | |
59eb99ba DL |
195 | |
196 | memset(handler, 0, sizeof(*handler)); | |
197 | ||
fae349da DL |
198 | handler->conf = conf; |
199 | ||
0ad19a3f | 200 | /* Begin the set the state to STARTING*/ |
25c2aca5 | 201 | if (lxc_set_state(name, handler, STARTING)) { |
59eb99ba | 202 | ERROR("failed to set state '%s'", lxc_state2str(STARTING)); |
884866b3 | 203 | goto out_free; |
0ad19a3f | 204 | } |
205 | ||
fae349da | 206 | if (lxc_create_tty(name, conf)) { |
36eb9bde | 207 | ERROR("failed to create the ttys"); |
59eb99ba | 208 | goto out_aborting; |
b0a33c1e | 209 | } |
210 | ||
1560f6c9 | 211 | if (lxc_create_console(conf)) { |
63376d7d DL |
212 | ERROR("failed to create console"); |
213 | goto out_delete_tty; | |
214 | } | |
215 | ||
b0a33c1e | 216 | /* the signal fd has to be created before forking otherwise |
217 | * if the child process exits before we setup the signal fd, | |
218 | * the event will be lost and the command will be stuck */ | |
59eb99ba DL |
219 | handler->sigfd = setup_sigchld_fd(&handler->oldmask); |
220 | if (handler->sigfd < 0) { | |
36eb9bde | 221 | ERROR("failed to set sigchild fd handler"); |
63376d7d | 222 | goto out_delete_console; |
b0a33c1e | 223 | } |
224 | ||
59eb99ba DL |
225 | /* Avoid signals from terminal */ |
226 | LXC_TTY_ADD_HANDLER(SIGINT); | |
227 | LXC_TTY_ADD_HANDLER(SIGQUIT); | |
228 | ||
c3e13372 | 229 | INFO("'%s' is initialized", name); |
3a0f472d | 230 | return handler; |
59eb99ba | 231 | |
63376d7d DL |
232 | out_delete_console: |
233 | lxc_delete_console(&conf->console); | |
59eb99ba | 234 | out_delete_tty: |
fae349da | 235 | lxc_delete_tty(&conf->tty_info); |
59eb99ba | 236 | out_aborting: |
25c2aca5 | 237 | lxc_set_state(name, handler, ABORTING); |
3a0f472d DL |
238 | out_free: |
239 | free(handler); | |
c3e13372 | 240 | return NULL; |
59eb99ba DL |
241 | } |
242 | ||
1bc5cc8c | 243 | void lxc_fini(const char *name, struct lxc_handler *handler) |
59eb99ba DL |
244 | { |
245 | /* The STOPPING state is there for future cleanup code | |
246 | * which can take awhile | |
247 | */ | |
25c2aca5 MN |
248 | lxc_set_state(name, handler, STOPPING); |
249 | lxc_set_state(name, handler, STOPPED); | |
59eb99ba DL |
250 | lxc_unlink_nsgroup(name); |
251 | ||
63376d7d | 252 | lxc_delete_console(&handler->conf->console); |
b2431939 GK |
253 | lxc_delete_tty(&handler->conf->tty_info); |
254 | free(handler); | |
59eb99ba DL |
255 | |
256 | LXC_TTY_DEL_HANDLER(SIGQUIT); | |
257 | LXC_TTY_DEL_HANDLER(SIGINT); | |
258 | } | |
259 | ||
1bc5cc8c | 260 | void lxc_abort(const char *name, struct lxc_handler *handler) |
59eb99ba | 261 | { |
25c2aca5 | 262 | lxc_set_state(name, handler, ABORTING); |
59eb99ba DL |
263 | kill(handler->pid, SIGKILL); |
264 | } | |
265 | ||
50e98013 DL |
266 | struct start_arg { |
267 | const char *name; | |
268 | char *const *argv; | |
269 | struct lxc_handler *handler; | |
270 | int *sv; | |
271 | }; | |
272 | ||
273 | static int do_start(void *arg) | |
274 | { | |
275 | struct start_arg *start_arg = arg; | |
276 | struct lxc_handler *handler = start_arg->handler; | |
277 | const char *name = start_arg->name; | |
278 | char *const *argv = start_arg->argv; | |
279 | int *sv = start_arg->sv; | |
280 | int err = -1, sync; | |
281 | ||
282 | if (sigprocmask(SIG_SETMASK, &handler->oldmask, NULL)) { | |
283 | SYSERROR("failed to set sigprocmask"); | |
9d7f9e52 | 284 | return -1; |
50e98013 DL |
285 | } |
286 | ||
287 | close(sv[1]); | |
288 | ||
289 | /* Be sure we don't inherit this after the exec */ | |
290 | fcntl(sv[0], F_SETFD, FD_CLOEXEC); | |
291 | ||
292 | /* Tell our father he can begin to configure the container */ | |
293 | if (write(sv[0], &sync, sizeof(sync)) < 0) { | |
294 | SYSERROR("failed to write socket"); | |
9d7f9e52 | 295 | return -1; |
50e98013 DL |
296 | } |
297 | ||
298 | /* Wait for the father to finish the configuration */ | |
299 | if (read(sv[0], &sync, sizeof(sync)) < 0) { | |
300 | SYSERROR("failed to read socket"); | |
9d7f9e52 | 301 | return -1; |
50e98013 DL |
302 | } |
303 | ||
304 | /* Setup the container, ip, names, utsname, ... */ | |
fae349da | 305 | if (lxc_setup(name, handler->conf)) { |
50e98013 DL |
306 | ERROR("failed to setup the container"); |
307 | goto out_warn_father; | |
308 | } | |
309 | ||
310 | if (prctl(PR_CAPBSET_DROP, CAP_SYS_BOOT, 0, 0, 0)) { | |
311 | SYSERROR("failed to remove CAP_SYS_BOOT capability"); | |
9d7f9e52 | 312 | return -1; |
50e98013 DL |
313 | } |
314 | ||
6a6ad7af DL |
315 | if (prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0)) { |
316 | SYSERROR("failed to set pdeath signal"); | |
9d7f9e52 | 317 | return -1; |
6a6ad7af DL |
318 | } |
319 | ||
1ac470c0 DL |
320 | NOTICE("exec'ing '%s'", argv[0]); |
321 | ||
50e98013 DL |
322 | execvp(argv[0], argv); |
323 | SYSERROR("failed to exec %s", argv[0]); | |
324 | ||
325 | out_warn_father: | |
326 | /* If the exec fails, tell that to our father */ | |
327 | if (write(sv[0], &err, sizeof(err)) < 0) | |
328 | SYSERROR("failed to write the socket"); | |
50e98013 DL |
329 | return -1; |
330 | } | |
331 | ||
9618063c | 332 | int lxc_spawn(const char *name, struct lxc_handler *handler, char *const argv[]) |
59eb99ba DL |
333 | { |
334 | int sv[2]; | |
335 | int clone_flags; | |
336 | int err = -1, sync; | |
337 | ||
50e98013 DL |
338 | struct start_arg start_arg = { |
339 | .name = name, | |
340 | .argv = argv, | |
341 | .handler = handler, | |
342 | .sv = sv, | |
343 | }; | |
344 | ||
0ad19a3f | 345 | /* Synchro socketpair */ |
346 | if (socketpair(AF_LOCAL, SOCK_STREAM, 0, sv)) { | |
36eb9bde | 347 | SYSERROR("failed to create communication socketpair"); |
9d7f9e52 | 348 | return -1; |
0ad19a3f | 349 | } |
350 | ||
1ea6db29 | 351 | clone_flags = CLONE_NEWUTS|CLONE_NEWPID|CLONE_NEWIPC|CLONE_NEWNS; |
fae349da | 352 | if (!lxc_list_empty(&handler->conf->network)) { |
82d5ae15 | 353 | |
0ad19a3f | 354 | clone_flags |= CLONE_NEWNET; |
355 | ||
82d5ae15 DL |
356 | /* that should be done before the clone because we will |
357 | * fill the netdev index and use them in the child | |
358 | */ | |
fae349da | 359 | if (lxc_create_network(&handler->conf->network)) { |
82d5ae15 DL |
360 | ERROR("failed to create the network"); |
361 | goto out_close; | |
362 | } | |
363 | } | |
364 | ||
0ad19a3f | 365 | /* Create a process in a new set of namespaces */ |
50e98013 | 366 | handler->pid = lxc_clone(do_start, &start_arg, clone_flags); |
59eb99ba | 367 | if (handler->pid < 0) { |
36eb9bde | 368 | SYSERROR("failed to fork into a new namespace"); |
59eb99ba | 369 | goto out_close; |
0ad19a3f | 370 | } |
371 | ||
0ad19a3f | 372 | close(sv[0]); |
373 | ||
374 | /* Wait for the child to be ready */ | |
375 | if (read(sv[1], &sync, sizeof(sync)) < 0) { | |
36eb9bde | 376 | SYSERROR("failed to read the socket"); |
59eb99ba | 377 | goto out_abort; |
0ad19a3f | 378 | } |
379 | ||
9f44c578 | 380 | if (lxc_rename_nsgroup(name, handler)) |
2b31f553 | 381 | goto out_abort; |
218d4250 | 382 | |
0ad19a3f | 383 | /* Create the network configuration */ |
82d5ae15 | 384 | if (clone_flags & CLONE_NEWNET) { |
fae349da | 385 | if (lxc_assign_network(&handler->conf->network, handler->pid)) { |
82d5ae15 DL |
386 | ERROR("failed to create the configured network"); |
387 | goto out_abort; | |
388 | } | |
0ad19a3f | 389 | } |
390 | ||
391 | /* Tell the child to continue its initialization */ | |
392 | if (write(sv[1], &sync, sizeof(sync)) < 0) { | |
36eb9bde | 393 | SYSERROR("failed to write the socket"); |
59eb99ba | 394 | goto out_abort; |
0ad19a3f | 395 | } |
396 | ||
397 | /* Wait for the child to exec or returning an error */ | |
e043236e | 398 | if (read(sv[1], &sync, sizeof(sync)) < 0) { |
36eb9bde | 399 | ERROR("failed to read the socket"); |
59eb99ba | 400 | goto out_abort; |
0ad19a3f | 401 | } |
402 | ||
25c2aca5 | 403 | if (lxc_set_state(name, handler, RUNNING)) { |
59eb99ba DL |
404 | ERROR("failed to set state to %s", |
405 | lxc_state2str(RUNNING)); | |
406 | goto out_abort; | |
3f21c114 | 407 | } |
22ebac19 | 408 | |
59eb99ba | 409 | err = 0; |
22ebac19 | 410 | |
1ac470c0 DL |
411 | NOTICE("'%s' started with pid '%d'", argv[0], handler->pid); |
412 | ||
59eb99ba DL |
413 | out_close: |
414 | close(sv[0]); | |
415 | close(sv[1]); | |
59eb99ba | 416 | return err; |
0ad19a3f | 417 | |
59eb99ba DL |
418 | out_abort: |
419 | lxc_abort(name, handler); | |
b79fcd86 GK |
420 | close(sv[1]); |
421 | return -1; | |
59eb99ba | 422 | } |
0ad19a3f | 423 | |
fae349da | 424 | int lxc_start(const char *name, char *const argv[], struct lxc_conf *conf) |
59eb99ba | 425 | { |
3a0f472d | 426 | struct lxc_handler *handler; |
e043236e | 427 | int err = -1; |
59eb99ba DL |
428 | int status; |
429 | ||
fae349da | 430 | handler = lxc_init(name, conf); |
3a0f472d | 431 | if (!handler) { |
59eb99ba | 432 | ERROR("failed to initialize the container"); |
66aeffc7 | 433 | return -1; |
0ad19a3f | 434 | } |
435 | ||
3a0f472d | 436 | err = lxc_spawn(name, handler, argv); |
59eb99ba DL |
437 | if (err) { |
438 | ERROR("failed to spawn '%s'", argv[0]); | |
9d7f9e52 | 439 | goto out_fini; |
0ad19a3f | 440 | } |
441 | ||
af795875 | 442 | err = lxc_close_all_inherited_fd(); |
d983b93c MN |
443 | if (err) { |
444 | ERROR("unable to close inherited fds"); | |
445 | goto out_abort; | |
446 | } | |
447 | ||
3a0f472d | 448 | err = lxc_poll(name, handler); |
e043236e | 449 | if (err) { |
59eb99ba DL |
450 | ERROR("mainloop exited with an error"); |
451 | goto out_abort; | |
452 | } | |
0ad19a3f | 453 | |
3a0f472d | 454 | while (waitpid(handler->pid, &status, 0) < 0 && errno == EINTR) |
1bc5cc8c | 455 | continue; |
e043236e | 456 | |
3a0f472d | 457 | err = lxc_error_set_and_log(handler->pid, status); |
9d7f9e52 | 458 | out_fini: |
3a0f472d | 459 | lxc_fini(name, handler); |
0ad19a3f | 460 | return err; |
461 | ||
59eb99ba | 462 | out_abort: |
3a0f472d | 463 | lxc_abort(name, handler); |
9d7f9e52 | 464 | goto out_fini; |
0ad19a3f | 465 | } |