]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/start.c
rename network type enum
[mirror_lxc.git] / src / lxc / start.c
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
24 #include "../config.h"
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>
33 #include <fcntl.h>
34 #include <termios.h>
35 #include <namespace.h>
36 #include <sys/param.h>
37 #include <sys/file.h>
38 #include <sys/mount.h>
39 #include <sys/types.h>
40 #include <sys/prctl.h>
41 #include <sys/types.h>
42 #include <sys/capability.h>
43 #include <sys/wait.h>
44 #include <sys/un.h>
45 #include <sys/poll.h>
46
47 #ifdef HAVE_SYS_SIGNALFD_H
48 # include <sys/signalfd.h>
49 #else
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
56 # elif __powerpc__
57 # define __NR_signalfd4 313
58 # elif __s390x__
59 # define __NR_signalfd4 322
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
69 # elif __powerpc__
70 # define __NR_signalfd 305
71 # elif __s390x__
72 # define __NR_signalfd 316
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 }
85 #endif
86
87 #if !HAVE_DECL_PR_CAPBSET_DROP
88 #define PR_CAPBSET_DROP 24
89 #endif
90
91 #include <lxc/log.h>
92 #include <lxc/conf.h>
93 #include <lxc/confile.h>
94 #include <lxc/start.h>
95 #include <lxc/utils.h>
96 #include <lxc/cgroup.h>
97 #include <lxc/monitor.h>
98
99 #include "error.h"
100 #include "af_unix.h"
101 #include "mainloop.h"
102 #include "commands.h"
103
104
105 lxc_log_define(lxc_start, lxc);
106
107 LXC_TTY_HANDLER(SIGINT);
108 LXC_TTY_HANDLER(SIGQUIT);
109
110 static int setup_sigchld_fd(sigset_t *oldmask)
111 {
112 sigset_t mask;
113 int fd;
114
115 if (sigprocmask(SIG_BLOCK, NULL, &mask)) {
116 SYSERROR("failed to get mask signal");
117 return -1;
118 }
119
120 if (sigaddset(&mask, SIGCHLD) || sigprocmask(SIG_BLOCK, &mask, oldmask)) {
121 SYSERROR("failed to set mask signal");
122 return -1;
123 }
124
125 fd = signalfd(-1, &mask, 0);
126 if (fd < 0) {
127 SYSERROR("failed to create the signal fd");
128 return -1;
129 }
130
131 if (fcntl(fd, F_SETFD, FD_CLOEXEC)) {
132 SYSERROR("failed to set sigfd to close-on-exec");
133 close(fd);
134 return -1;
135 }
136
137 DEBUG("sigchild handler set");
138
139 return fd;
140 }
141
142 static int sigchld_handler(int fd, void *data,
143 struct lxc_epoll_descr *descr)
144 {
145 DEBUG("child exited");
146
147 return 1;
148 }
149
150 int lxc_set_state(const char *name, struct lxc_handler *handler, lxc_state_t state)
151 {
152 handler->state = state;
153 lxc_monitor_send_state(name, state);
154 return 0;
155 }
156
157 int lxc_poll(const char *name, struct lxc_handler *handler)
158 {
159 int sigfd = handler->sigfd;
160 int pid = handler->pid;
161 struct lxc_epoll_descr descr;
162
163 if (lxc_mainloop_open(&descr)) {
164 ERROR("failed to create mainloop");
165 goto out_sigfd;
166 }
167
168 if (lxc_mainloop_add_handler(&descr, sigfd, sigchld_handler, &pid)) {
169 ERROR("failed to add handler for the signal");
170 goto out_mainloop_open;
171 }
172
173 if (lxc_command_mainloop_add(name, &descr, handler))
174 goto out_mainloop_open;
175
176 return lxc_mainloop(&descr);
177
178 out_mainloop_open:
179 lxc_mainloop_close(&descr);
180 out_sigfd:
181 close(sigfd);
182 return -1;
183 }
184
185 static int fdname(int fd, char *name, size_t size)
186 {
187 char path[MAXPATHLEN];
188 ssize_t len;
189
190 snprintf(path, MAXPATHLEN, "/proc/self/fd/%d", fd);
191
192 len = readlink(path, name, size);
193 if (len > 0)
194 path[len] = '\0';
195
196 return (len <= 0) ? -1 : 0;
197 }
198
199 static int console_init(char *console, size_t size)
200 {
201 struct stat stat;
202 int i;
203
204 for (i = 0; i < 3; i++) {
205 if (!isatty(i))
206 continue;
207
208 if (ttyname_r(i, console, size)) {
209 SYSERROR("failed to retrieve tty name");
210 return -1;
211 }
212
213 return 0;
214 }
215
216 if (!fstat(0, &stat)) {
217 if (S_ISREG(stat.st_mode) || S_ISCHR(stat.st_mode) ||
218 S_ISFIFO(stat.st_mode) || S_ISLNK(stat.st_mode))
219 return fdname(0, console, size);
220 }
221
222 console[0] = '\0';
223
224 DEBUG("console initialized");
225
226 return 0;
227 }
228
229 struct lxc_handler *lxc_init(const char *name, struct lxc_conf *conf)
230 {
231 struct lxc_handler *handler;
232
233 handler = malloc(sizeof(*handler));
234 if (!handler)
235 return NULL;
236
237 memset(handler, 0, sizeof(*handler));
238
239 handler->conf = conf;
240
241 /* Begin the set the state to STARTING*/
242 if (lxc_set_state(name, handler, STARTING)) {
243 ERROR("failed to set state '%s'", lxc_state2str(STARTING));
244 goto out_free;
245 }
246
247 if (console_init(conf->console, sizeof(conf->console))) {
248 ERROR("failed to initialize the console");
249 goto out_aborting;
250 }
251
252 if (lxc_create_tty(name, conf)) {
253 ERROR("failed to create the ttys");
254 goto out_aborting;
255 }
256
257 /* the signal fd has to be created before forking otherwise
258 * if the child process exits before we setup the signal fd,
259 * the event will be lost and the command will be stuck */
260 handler->sigfd = setup_sigchld_fd(&handler->oldmask);
261 if (handler->sigfd < 0) {
262 ERROR("failed to set sigchild fd handler");
263 goto out_delete_tty;
264 }
265
266 /* Avoid signals from terminal */
267 LXC_TTY_ADD_HANDLER(SIGINT);
268 LXC_TTY_ADD_HANDLER(SIGQUIT);
269
270 INFO("'%s' is initialized", name);
271 return handler;
272
273 out_delete_tty:
274 lxc_delete_tty(&conf->tty_info);
275 out_aborting:
276 lxc_set_state(name, handler, ABORTING);
277 out_free:
278 free(handler);
279 return NULL;
280 }
281
282 void lxc_fini(const char *name, struct lxc_handler *handler)
283 {
284 /* The STOPPING state is there for future cleanup code
285 * which can take awhile
286 */
287 lxc_set_state(name, handler, STOPPING);
288 lxc_set_state(name, handler, STOPPED);
289 lxc_unlink_nsgroup(name);
290
291 lxc_delete_tty(&handler->conf->tty_info);
292 free(handler);
293
294 LXC_TTY_DEL_HANDLER(SIGQUIT);
295 LXC_TTY_DEL_HANDLER(SIGINT);
296 }
297
298 void lxc_abort(const char *name, struct lxc_handler *handler)
299 {
300 lxc_set_state(name, handler, ABORTING);
301 kill(handler->pid, SIGKILL);
302 }
303
304 struct start_arg {
305 const char *name;
306 char *const *argv;
307 struct lxc_handler *handler;
308 int *sv;
309 };
310
311 static int do_start(void *arg)
312 {
313 struct start_arg *start_arg = arg;
314 struct lxc_handler *handler = start_arg->handler;
315 const char *name = start_arg->name;
316 char *const *argv = start_arg->argv;
317 int *sv = start_arg->sv;
318 int err = -1, sync;
319
320 if (sigprocmask(SIG_SETMASK, &handler->oldmask, NULL)) {
321 SYSERROR("failed to set sigprocmask");
322 return -1;
323 }
324
325 close(sv[1]);
326
327 /* Be sure we don't inherit this after the exec */
328 fcntl(sv[0], F_SETFD, FD_CLOEXEC);
329
330 /* Tell our father he can begin to configure the container */
331 if (write(sv[0], &sync, sizeof(sync)) < 0) {
332 SYSERROR("failed to write socket");
333 return -1;
334 }
335
336 /* Wait for the father to finish the configuration */
337 if (read(sv[0], &sync, sizeof(sync)) < 0) {
338 SYSERROR("failed to read socket");
339 return -1;
340 }
341
342 /* Setup the container, ip, names, utsname, ... */
343 if (lxc_setup(name, handler->conf)) {
344 ERROR("failed to setup the container");
345 goto out_warn_father;
346 }
347
348 if (prctl(PR_CAPBSET_DROP, CAP_SYS_BOOT, 0, 0, 0)) {
349 SYSERROR("failed to remove CAP_SYS_BOOT capability");
350 return -1;
351 }
352
353 if (prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0)) {
354 SYSERROR("failed to set pdeath signal");
355 return -1;
356 }
357
358 NOTICE("exec'ing '%s'", argv[0]);
359
360 execvp(argv[0], argv);
361 SYSERROR("failed to exec %s", argv[0]);
362
363 out_warn_father:
364 /* If the exec fails, tell that to our father */
365 if (write(sv[0], &err, sizeof(err)) < 0)
366 SYSERROR("failed to write the socket");
367 return -1;
368 }
369
370 int lxc_spawn(const char *name, struct lxc_handler *handler, char *const argv[])
371 {
372 int sv[2];
373 int clone_flags;
374 int err = -1, sync;
375
376 struct start_arg start_arg = {
377 .name = name,
378 .argv = argv,
379 .handler = handler,
380 .sv = sv,
381 };
382
383 /* Synchro socketpair */
384 if (socketpair(AF_LOCAL, SOCK_STREAM, 0, sv)) {
385 SYSERROR("failed to create communication socketpair");
386 return -1;
387 }
388
389 clone_flags = CLONE_NEWUTS|CLONE_NEWPID|CLONE_NEWIPC|CLONE_NEWNS;
390 if (!lxc_list_empty(&handler->conf->network)) {
391
392 clone_flags |= CLONE_NEWNET;
393
394 /* that should be done before the clone because we will
395 * fill the netdev index and use them in the child
396 */
397 if (lxc_create_network(&handler->conf->network)) {
398 ERROR("failed to create the network");
399 goto out_close;
400 }
401 }
402
403 /* Create a process in a new set of namespaces */
404 handler->pid = lxc_clone(do_start, &start_arg, clone_flags);
405 if (handler->pid < 0) {
406 SYSERROR("failed to fork into a new namespace");
407 goto out_close;
408 }
409
410 close(sv[0]);
411
412 /* Wait for the child to be ready */
413 if (read(sv[1], &sync, sizeof(sync)) < 0) {
414 SYSERROR("failed to read the socket");
415 goto out_abort;
416 }
417
418 if (lxc_rename_nsgroup(name, handler))
419 goto out_abort;
420
421 /* Create the network configuration */
422 if (clone_flags & CLONE_NEWNET) {
423 if (lxc_assign_network(&handler->conf->network, handler->pid)) {
424 ERROR("failed to create the configured network");
425 goto out_abort;
426 }
427 }
428
429 /* Tell the child to continue its initialization */
430 if (write(sv[1], &sync, sizeof(sync)) < 0) {
431 SYSERROR("failed to write the socket");
432 goto out_abort;
433 }
434
435 /* Wait for the child to exec or returning an error */
436 if (read(sv[1], &sync, sizeof(sync)) < 0) {
437 ERROR("failed to read the socket");
438 goto out_abort;
439 }
440
441 if (lxc_set_state(name, handler, RUNNING)) {
442 ERROR("failed to set state to %s",
443 lxc_state2str(RUNNING));
444 goto out_abort;
445 }
446
447 err = 0;
448
449 NOTICE("'%s' started with pid '%d'", argv[0], handler->pid);
450
451 out_close:
452 close(sv[0]);
453 close(sv[1]);
454 return err;
455
456 out_abort:
457 lxc_abort(name, handler);
458 close(sv[1]);
459 return -1;
460 }
461
462 int lxc_start(const char *name, char *const argv[], struct lxc_conf *conf)
463 {
464 struct lxc_handler *handler;
465 int err = -1;
466 int status;
467
468 handler = lxc_init(name, conf);
469 if (!handler) {
470 ERROR("failed to initialize the container");
471 return -1;
472 }
473
474 err = lxc_spawn(name, handler, argv);
475 if (err) {
476 ERROR("failed to spawn '%s'", argv[0]);
477 goto out_fini;
478 }
479
480 err = lxc_close_all_inherited_fd();
481 if (err) {
482 ERROR("unable to close inherited fds");
483 goto out_abort;
484 }
485
486 err = lxc_poll(name, handler);
487 if (err) {
488 ERROR("mainloop exited with an error");
489 goto out_abort;
490 }
491
492 while (waitpid(handler->pid, &status, 0) < 0 && errno == EINTR)
493 continue;
494
495 err = lxc_error_set_and_log(handler->pid, status);
496 out_fini:
497 lxc_fini(name, handler);
498 return err;
499
500 out_abort:
501 lxc_abort(name, handler);
502 goto out_fini;
503 }