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