]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/start.c
Remove useless lines
[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 if (handler) {
292 lxc_delete_tty(&handler->conf->tty_info);
293 free(handler);
294 }
295
296 LXC_TTY_DEL_HANDLER(SIGQUIT);
297 LXC_TTY_DEL_HANDLER(SIGINT);
298 }
299
300 void lxc_abort(const char *name, struct lxc_handler *handler)
301 {
302 lxc_set_state(name, handler, ABORTING);
303 kill(handler->pid, SIGKILL);
304 }
305
306 struct start_arg {
307 const char *name;
308 char *const *argv;
309 struct lxc_handler *handler;
310 int *sv;
311 };
312
313 static int do_start(void *arg)
314 {
315 struct start_arg *start_arg = arg;
316 struct lxc_handler *handler = start_arg->handler;
317 const char *name = start_arg->name;
318 char *const *argv = start_arg->argv;
319 int *sv = start_arg->sv;
320 int err = -1, sync;
321
322 if (sigprocmask(SIG_SETMASK, &handler->oldmask, NULL)) {
323 SYSERROR("failed to set sigprocmask");
324 return -1;
325 }
326
327 close(sv[1]);
328
329 /* Be sure we don't inherit this after the exec */
330 fcntl(sv[0], F_SETFD, FD_CLOEXEC);
331
332 /* Tell our father he can begin to configure the container */
333 if (write(sv[0], &sync, sizeof(sync)) < 0) {
334 SYSERROR("failed to write socket");
335 return -1;
336 }
337
338 /* Wait for the father to finish the configuration */
339 if (read(sv[0], &sync, sizeof(sync)) < 0) {
340 SYSERROR("failed to read socket");
341 return -1;
342 }
343
344 /* Setup the container, ip, names, utsname, ... */
345 if (lxc_setup(name, handler->conf)) {
346 ERROR("failed to setup the container");
347 goto out_warn_father;
348 }
349
350 if (prctl(PR_CAPBSET_DROP, CAP_SYS_BOOT, 0, 0, 0)) {
351 SYSERROR("failed to remove CAP_SYS_BOOT capability");
352 return -1;
353 }
354
355 if (prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0)) {
356 SYSERROR("failed to set pdeath signal");
357 return -1;
358 }
359
360 NOTICE("exec'ing '%s'", argv[0]);
361
362 execvp(argv[0], argv);
363 SYSERROR("failed to exec %s", argv[0]);
364
365 out_warn_father:
366 /* If the exec fails, tell that to our father */
367 if (write(sv[0], &err, sizeof(err)) < 0)
368 SYSERROR("failed to write the socket");
369 return -1;
370 }
371
372 int lxc_spawn(const char *name, struct lxc_handler *handler, char *const argv[])
373 {
374 int sv[2];
375 int clone_flags;
376 int err = -1, sync;
377
378 struct start_arg start_arg = {
379 .name = name,
380 .argv = argv,
381 .handler = handler,
382 .sv = sv,
383 };
384
385 /* Synchro socketpair */
386 if (socketpair(AF_LOCAL, SOCK_STREAM, 0, sv)) {
387 SYSERROR("failed to create communication socketpair");
388 return -1;
389 }
390
391 clone_flags = CLONE_NEWUTS|CLONE_NEWPID|CLONE_NEWIPC|CLONE_NEWNS;
392 if (!lxc_list_empty(&handler->conf->network)) {
393
394 clone_flags |= CLONE_NEWNET;
395
396 /* that should be done before the clone because we will
397 * fill the netdev index and use them in the child
398 */
399 if (lxc_create_network(&handler->conf->network)) {
400 ERROR("failed to create the network");
401 goto out_close;
402 }
403 }
404
405 /* Create a process in a new set of namespaces */
406 handler->pid = lxc_clone(do_start, &start_arg, clone_flags);
407 if (handler->pid < 0) {
408 SYSERROR("failed to fork into a new namespace");
409 goto out_close;
410 }
411
412 close(sv[0]);
413
414 /* Wait for the child to be ready */
415 if (read(sv[1], &sync, sizeof(sync)) < 0) {
416 SYSERROR("failed to read the socket");
417 goto out_abort;
418 }
419
420 if (lxc_rename_nsgroup(name, handler))
421 goto out_abort;
422
423 /* Create the network configuration */
424 if (clone_flags & CLONE_NEWNET) {
425 if (lxc_assign_network(&handler->conf->network, handler->pid)) {
426 ERROR("failed to create the configured network");
427 goto out_abort;
428 }
429 }
430
431 /* Tell the child to continue its initialization */
432 if (write(sv[1], &sync, sizeof(sync)) < 0) {
433 SYSERROR("failed to write the socket");
434 goto out_abort;
435 }
436
437 /* Wait for the child to exec or returning an error */
438 if (read(sv[1], &sync, sizeof(sync)) < 0) {
439 ERROR("failed to read the socket");
440 goto out_abort;
441 }
442
443 if (lxc_set_state(name, handler, RUNNING)) {
444 ERROR("failed to set state to %s",
445 lxc_state2str(RUNNING));
446 goto out_abort;
447 }
448
449 err = 0;
450
451 NOTICE("'%s' started with pid '%d'", argv[0], handler->pid);
452
453 out_close:
454 close(sv[0]);
455 close(sv[1]);
456 return err;
457
458 out_abort:
459 lxc_abort(name, handler);
460 goto out_close;
461 }
462
463 int lxc_start(const char *name, char *const argv[], struct lxc_conf *conf)
464 {
465 struct lxc_handler *handler;
466 int err = -1;
467 int status;
468
469 handler = lxc_init(name, conf);
470 if (!handler) {
471 ERROR("failed to initialize the container");
472 return -1;
473 }
474
475 err = lxc_spawn(name, handler, argv);
476 if (err) {
477 ERROR("failed to spawn '%s'", argv[0]);
478 goto out_fini;
479 }
480
481 err = lxc_close_all_inherited_fd();
482 if (err) {
483 ERROR("unable to close inherited fds");
484 goto out_abort;
485 }
486
487 err = lxc_poll(name, handler);
488 if (err) {
489 ERROR("mainloop exited with an error");
490 goto out_abort;
491 }
492
493 while (waitpid(handler->pid, &status, 0) < 0 && errno == EINTR)
494 continue;
495
496 err = lxc_error_set_and_log(handler->pid, status);
497 out_fini:
498 lxc_fini(name, handler);
499 return err;
500
501 out_abort:
502 lxc_abort(name, handler);
503 goto out_fini;
504 }