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