]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/start.c
$(localstatedir) != $(prefix)/var
[mirror_lxc.git] / src / lxc / start.c
CommitLineData
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>
42ff343d 40#include <sys/capability.h>
0ad19a3f 41#include <sys/wait.h>
b0a33c1e 42#include <sys/un.h>
43#include <sys/poll.h>
ff218c25 44
45#ifdef HAVE_SYS_SIGNALFD_H
8ca61733 46# include <sys/signalfd.h>
ff218c25 47#else
8ca61733
MJ
48# ifndef __NR_signalfd4
49/* assume kernel headers are too old */
50# if __i386__
51# define __NR_signalfd4 327
52# elif __x86_64__
53# define __NR_signalfd4 289
bfa38025
MH
54# elif __powerpc__
55# define __NR_signalfd4 313
47f38330
SH
56# elif __s390x__
57# define __NR_signalfd4 322
8ca61733
MJ
58# endif
59#endif
60
61# ifndef __NR_signalfd
62/* assume kernel headers are too old */
63# if __i386__
64# define __NR_signalfd 321
65# elif __x86_64__
66# define __NR_signalfd 282
bfa38025
MH
67# elif __powerpc__
68# define __NR_signalfd 305
47f38330
SH
69# elif __s390x__
70# define __NR_signalfd 316
8ca61733
MJ
71# endif
72#endif
73
74int signalfd(int fd, const sigset_t *mask, int flags)
75{
76 int retval;
77
78 retval = syscall (__NR_signalfd4, fd, mask, _NSIG / 8, flags);
79 if (errno == ENOSYS && flags == 0)
80 retval = syscall (__NR_signalfd, fd, mask, _NSIG / 8);
81 return retval;
82}
ff218c25 83#endif
0ad19a3f 84
656994bb
MH
85#if !HAVE_DECL_PR_CAPBSET_DROP
86#define PR_CAPBSET_DROP 24
87#endif
88
e2bcd7db 89#include "error.h"
b0a33c1e 90#include "af_unix.h"
91#include "mainloop.h"
e2bcd7db 92
b113348e 93#include <lxc/lxc.h>
0ad19a3f 94
95LXC_TTY_HANDLER(SIGINT);
96LXC_TTY_HANDLER(SIGQUIT);
97
b0a33c1e 98static int setup_sigchld_fd(sigset_t *oldmask)
99{
100 sigset_t mask;
101 int fd;
102
103 if (sigprocmask(SIG_BLOCK, NULL, &mask)) {
104 lxc_log_syserror("failed to get mask signal");
105 return -1;
106 }
107
108 if (sigaddset(&mask, SIGCHLD) || sigprocmask(SIG_BLOCK, &mask, oldmask)) {
109 lxc_log_syserror("failed to set mask signal");
110 return -1;
111 }
112
113 fd = signalfd(-1, &mask, 0);
114 if (fd < 0) {
115 lxc_log_syserror("failed to create the signal fd");
116 return -1;
117 }
118
119 if (fcntl(fd, F_SETFD, FD_CLOEXEC)) {
120 lxc_log_syserror("failed to set sigfd to close-on-exec");
121 close(fd);
122 return -1;
123 }
124
125 return fd;
126}
127
128static int setup_tty_service(const char *name, int *ttyfd)
129{
130 int fd;
131 struct sockaddr_un addr = { 0 };
132 char *offset = &addr.sun_path[1];
133
134 strcpy(offset, name);
135 addr.sun_path[0] = '\0';
136
137 fd = lxc_af_unix_open(addr.sun_path, SOCK_STREAM, 0);
138 if (fd < 0)
139 return -1;
140
141 if (fcntl(fd, F_SETFD, FD_CLOEXEC)) {
142 lxc_log_syserror("failed to close-on-exec flag");
143 close(fd);
144 return -1;
145 }
146
147 *ttyfd = fd;
148
149 return 0;
150}
151
152static int sigchld_handler(int fd, void *data,
153 struct lxc_epoll_descr *descr)
154{
155 pid_t *pid = data;
156
157 waitpid(*pid, NULL, 0);
158
159 return 1;
160}
161
162static 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
181static 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) {
189 lxc_log_syserror("failed to accept tty client");
190 return -1;
191 }
192
193 if (setsockopt(conn, SOL_SOCKET, SO_PASSCRED, &val, sizeof(val))) {
194 lxc_log_syserror("failed to enable credential on socket");
195 goto out_close;
196 }
197
198 if (lxc_af_unix_rcv_credential(conn, &ttynum, sizeof(ttynum)))
199 goto out_close;
200
201 if (ttynum <= 0 || ttynum > tty_info->nbtty)
202 goto out_close;
203
204 /* fixup index array (eg. tty1 is index 0) */
205 ttynum--;
206
207 if (tty_info->pty_info[ttynum].busy)
208 goto out_close;
209
210 if (lxc_af_unix_send_fd(conn, tty_info->pty_info[ttynum].master,
211 NULL, 0) < 0) {
212 lxc_log_error("failed to send tty to client");
213 goto out_close;
214 }
215
216 if (lxc_mainloop_add_handler(descr, conn,
217 ttyclient_handler, tty_info)) {
218 lxc_log_error("failed to add tty client handler");
219 goto out_close;
220 }
221
222 tty_info->pty_info[ttynum].busy = conn;
223
224 ret = 0;
225
226out:
227 return ret;
228out_close:
229 close(conn);
230 goto out;
231}
232
233static int mainloop(const char *name, pid_t pid, int sigfd,
234 const struct lxc_tty_info *tty_info)
235{
236 int nfds, ttyfd = -1, ret = -1;
237 struct lxc_epoll_descr descr;
238
239 if (tty_info->nbtty && setup_tty_service(name, &ttyfd)) {
240 lxc_log_error("failed to create the tty service point");
241 goto out_sigfd;
242 }
243
244 /* sigfd + nb tty + tty service
245 * if tty is enabled */
246 nfds = tty_info->nbtty + 1 + tty_info->nbtty ? 1 : 0;
247
248 if (lxc_mainloop_open(nfds, &descr)) {
249 lxc_log_error("failed to create mainloop");
250 goto out_ttyfd;
251 }
252
253 if (lxc_mainloop_add_handler(&descr, sigfd, sigchld_handler, &pid)) {
254 lxc_log_error("failed to add handler for the signal");
255 goto out_mainloop_open;
256 }
257
258 if (tty_info->nbtty) {
259 if (lxc_mainloop_add_handler(&descr, ttyfd,
260 ttyservice_handler,
261 (void *)tty_info)) {
262 lxc_log_error("failed to add handler for the tty");
263 goto out_mainloop_open;
264 }
265 }
266
267 ret = lxc_mainloop(&descr);
268
269out:
270 return ret;
271
272out_mainloop_open:
273 lxc_mainloop_close(&descr);
274out_ttyfd:
275 close(ttyfd);
276out_sigfd:
277 close(sigfd);
278 goto out;
279}
280
05f05512 281int lxc_start(const char *name, char *argv[])
0ad19a3f 282{
b0a33c1e 283 struct lxc_tty_info tty_info = { 0 };
284 sigset_t oldmask;
22ebac19 285 char init[MAXPATHLEN];
939229eb 286 char tty[MAXPATHLEN];
22ebac19 287 char *val = NULL;
b0a33c1e 288 int fd, sigfd, lock, sv[2], sync = 0, err = -LXC_ERROR_INTERNAL;
0ad19a3f 289 pid_t pid;
290 int clone_flags;
f4d507d5 291
0ad19a3f 292 lock = lxc_get_lock(name);
b0a33c1e 293 if (lock < 0)
294 return lock;
0ad19a3f 295
0ad19a3f 296 /* Begin the set the state to STARTING*/
297 if (lxc_setstate(name, STARTING)) {
8b8b04f8 298 lxc_log_error("failed to set state '%s'",
299 lxc_state2str(STARTING));
0ad19a3f 300 goto out;
301 }
302
caf249f4 303 /* If we are not attached to a tty, disable it */
304 if (ttyname_r(0, tty, sizeof(tty)))
939229eb 305 tty[0] = '\0';
939229eb 306
b0a33c1e 307 if (lxc_create_tty(name, &tty_info)) {
308 lxc_log_error("failed to create the ttys");
309 goto out;
310 }
311
312 /* the signal fd has to be created before forking otherwise
313 * if the child process exits before we setup the signal fd,
314 * the event will be lost and the command will be stuck */
315 sigfd = setup_sigchld_fd(&oldmask);
316 if (sigfd < 0) {
317 lxc_log_error("failed to set sigchild fd handler");
318 return -1;
319 }
320
0ad19a3f 321 /* Synchro socketpair */
322 if (socketpair(AF_LOCAL, SOCK_STREAM, 0, sv)) {
323 lxc_log_syserror("failed to create communication socketpair");
f4d507d5 324 goto out;
0ad19a3f 325 }
326
327 /* Avoid signals from terminal */
328 LXC_TTY_ADD_HANDLER(SIGINT);
329 LXC_TTY_ADD_HANDLER(SIGQUIT);
330
f4d507d5 331 clone_flags = CLONE_NEWPID|CLONE_NEWIPC|CLONE_NEWNS;
0ad19a3f 332 if (conf_has_utsname(name))
333 clone_flags |= CLONE_NEWUTS;
334 if (conf_has_network(name))
335 clone_flags |= CLONE_NEWNET;
336
337 /* Create a process in a new set of namespaces */
338 pid = fork_ns(clone_flags);
339 if (pid < 0) {
340 lxc_log_syserror("failed to fork into a new namespace");
341 goto err_fork_ns;
342 }
343
344 if (!pid) {
345
b0a33c1e 346 if (sigprocmask(SIG_SETMASK, &oldmask, NULL)) {
347 lxc_log_syserror("failed to set sigprocmask");
348 return -1;
349 }
350
0ad19a3f 351 close(sv[1]);
352
353 /* Be sure we don't inherit this after the exec */
354 fcntl(sv[0], F_SETFD, FD_CLOEXEC);
355
356 /* Tell our father he can begin to configure the container */
357 if (write(sv[0], &sync, sizeof(sync)) < 0) {
358 lxc_log_syserror("failed to write socket");
57545890 359 goto out_child;
0ad19a3f 360 }
361
362 /* Wait for the father to finish the configuration */
363 if (read(sv[0], &sync, sizeof(sync)) < 0) {
364 lxc_log_syserror("failed to read socket");
57545890 365 goto out_child;
0ad19a3f 366 }
367
368 /* Setup the container, ip, names, utsname, ... */
b0a33c1e 369 err = lxc_setup(name, tty, &tty_info);
e5bda9ee 370 if (err) {
0ad19a3f 371 lxc_log_error("failed to setup the container");
e5bda9ee 372 if (write(sv[0], &err, sizeof(err)) < 0)
0ad19a3f 373 lxc_log_syserror("failed to write the socket");
57545890 374 goto out_child;
0ad19a3f 375 }
376
42ff343d 377 if (prctl(PR_CAPBSET_DROP, CAP_SYS_BOOT, 0, 0, 0)) {
378 lxc_log_syserror("failed to remove CAP_SYS_BOOT capability");
379 goto out_child;
380 }
381
0ad19a3f 382 execvp(argv[0], argv);
383 lxc_log_syserror("failed to exec %s", argv[0]);
384
b3223262 385 err = LXC_ERROR_WRONG_COMMAND;
0ad19a3f 386 /* If the exec fails, tell that to our father */
e5bda9ee 387 if (write(sv[0], &err, sizeof(err)) < 0)
0ad19a3f 388 lxc_log_syserror("failed to write the socket");
389
57545890 390 out_child:
e5bda9ee 391 exit(err);
0ad19a3f 392 }
393
394 close(sv[0]);
395
396 /* Wait for the child to be ready */
397 if (read(sv[1], &sync, sizeof(sync)) < 0) {
398 lxc_log_syserror("failed to read the socket");
399 goto err_pipe_read;
400 }
401
218d4250 402 if (lxc_link_nsgroup(name, pid))
403 lxc_log_warning("cgroupfs not found: cgroup disabled");
404
0ad19a3f 405 /* Create the network configuration */
406 if (clone_flags & CLONE_NEWNET && conf_create_network(name, pid)) {
407 lxc_log_error("failed to create the configured network");
408 goto err_create_network;
409 }
410
411 /* Tell the child to continue its initialization */
412 if (write(sv[1], &sync, sizeof(sync)) < 0) {
413 lxc_log_syserror("failed to write the socket");
414 goto err_pipe_write;
415 }
416
417 /* Wait for the child to exec or returning an error */
418 err = read(sv[1], &sync, sizeof(sync));
419 if (err < 0) {
420 lxc_log_error("failed to read the socket");
421 goto err_pipe_read2;
422 }
423
424 if (err > 0) {
e5bda9ee 425 err = sync;
0ad19a3f 426 waitpid(pid, NULL, 0);
427 goto err_child_failed;
428 }
429
430 asprintf(&val, "%d\n", pid);
22ebac19 431
432 snprintf(init, MAXPATHLEN, LXCPATH "/%s/init", name);
433
0ad19a3f 434 fd = open(init, O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR);
435 if (fd < 0) {
436 lxc_log_syserror("failed to open '%s'", init);
437 goto err_write;
438 }
439
440 if (write(fd, val, strlen(val)) < 0) {
441 lxc_log_syserror("failed to write the init pid");
442 goto err_write;
443 }
444
445 close(fd);
446
0ad19a3f 447 if (lxc_setstate(name, RUNNING)) {
448 lxc_log_error("failed to set state to %s",
449 lxc_state2str(RUNNING));
450 goto err_state_failed;
451 }
452
b0a33c1e 453 if (mainloop(name, pid, sigfd, &tty_info)) {
454 lxc_log_error("mainloop exited with an error");
455 goto err_mailoop_failed;
0ad19a3f 456 }
457
458 if (lxc_setstate(name, STOPPING))
459 lxc_log_error("failed to set state %s", lxc_state2str(STOPPING));
460
461 if (clone_flags & CLONE_NEWNET && conf_destroy_network(name))
462 lxc_log_error("failed to destroy the network");
463
464 err = 0;
465out:
466 if (lxc_setstate(name, STOPPED))
467 lxc_log_error("failed to set state %s", lxc_state2str(STOPPED));
468
b0a33c1e 469 lxc_delete_tty(&tty_info);
0ad19a3f 470 lxc_unlink_nsgroup(name);
471 unlink(init);
0ad19a3f 472 free(val);
473 lxc_put_lock(lock);
536b97f0 474 LXC_TTY_DEL_HANDLER(SIGQUIT);
475 LXC_TTY_DEL_HANDLER(SIGINT);
0ad19a3f 476
477 return err;
478
479err_write:
480 close(fd);
481
482err_state_failed:
483err_child_failed:
484err_pipe_read2:
485err_pipe_write:
486 if (clone_flags & CLONE_NEWNET)
487 conf_destroy_network(name);
488err_create_network:
489err_pipe_read:
b0a33c1e 490err_mailoop_failed:
0ad19a3f 491 if (lxc_setstate(name, ABORTING))
492 lxc_log_error("failed to set state %s", lxc_state2str(STOPPED));
493
494 kill(pid, SIGKILL);
495err_fork_ns:
0ad19a3f 496 close(sv[0]);
497 close(sv[1]);
0ad19a3f 498 goto out;
499}