]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/start.c
Add LUA api get_ips(), get_interfaces(), rename() functions
[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:
9afe19d6 7 * Daniel Lezcano <daniel.lezcano at free.fr>
0ad19a3f 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
250b1eec 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
0ad19a3f 22 */
23
f549edcc
GK
24#include "config.h"
25
0ad19a3f 26#include <stdio.h>
0ad19a3f 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>
c476bdce 34#include <grp.h>
37515ebd 35#include <poll.h>
0ad19a3f 36#include <sys/param.h>
37#include <sys/file.h>
f4d507d5 38#include <sys/mount.h>
b4f8660e 39#include <sys/stat.h>
0ad19a3f 40#include <sys/types.h>
8173e600 41#include <sys/socket.h>
0ad19a3f 42#include <sys/prctl.h>
ddceb1f9 43#include <sys/types.h>
0ad19a3f 44#include <sys/wait.h>
b0a33c1e 45#include <sys/un.h>
8173e600 46#include <sys/syscall.h>
ff218c25 47
495d2046
SG
48#if HAVE_SYS_CAPABILITY_H
49#include <sys/capability.h>
50#endif
51
656994bb
MH
52#if !HAVE_DECL_PR_CAPBSET_DROP
53#define PR_CAPBSET_DROP 24
54#endif
55
63376d7d
DL
56#include "start.h"
57#include "conf.h"
58#include "log.h"
563f2f2c 59#include "cgroup.h"
e2bcd7db 60#include "error.h"
b0a33c1e 61#include "af_unix.h"
62#include "mainloop.h"
63376d7d 63#include "utils.h"
565c2d76 64#include "lxcutmp.h"
63376d7d 65#include "monitor.h"
96fa1ff0 66#include "commands.h"
63376d7d 67#include "console.h"
3c22086f 68#include "sync.h"
f549edcc 69#include "namespace.h"
0d0527a9 70#include "lxcseccomp.h"
8173e600 71#include "caps.h"
76a26f55 72#include "bdev.h"
fe4de9a6 73#include "lsm/lsm.h"
f01f7975 74#include "lxclock.h"
36eb9bde
CLG
75
76lxc_log_define(lxc_start, lxc);
77
9f30a190
MM
78const struct ns_info ns_info[LXC_NS_MAX] = {
79 [LXC_NS_MNT] = {"mnt", CLONE_NEWNS},
80 [LXC_NS_PID] = {"pid", CLONE_NEWPID},
81 [LXC_NS_UTS] = {"uts", CLONE_NEWUTS},
82 [LXC_NS_IPC] = {"ipc", CLONE_NEWIPC},
83 [LXC_NS_USER] = {"user", CLONE_NEWUSER},
84 [LXC_NS_NET] = {"net", CLONE_NEWNET}
85};
86
f01f7975 87extern void mod_all_rdeps(struct lxc_container *c, bool inc);
28272964
CB
88static bool do_destroy_container(struct lxc_conf *conf);
89static int lxc_rmdir_onedev_wrapper(void *data);
90static void lxc_destroy_container_on_signal(struct lxc_handler *handler,
91 const char *name);
92
c8154066
SH
93static void print_top_failing_dir(const char *path)
94{
95 size_t len = strlen(path);
96 char *copy = alloca(len+1), *p, *e, saved;
97 strcpy(copy, path);
98
99 p = copy;
100 e = copy + len;
101 while (p < e) {
102 while (p < e && *p == '/') p++;
103 while (p < e && *p != '/') p++;
c8154066
SH
104 saved = *p;
105 *p = '\0';
106 if (access(copy, X_OK)) {
107 SYSERROR("could not access %s. Please grant it 'x' " \
108 "access, or add an ACL for the container root.",
109 copy);
110 return;
111 }
112 *p = saved;
113 }
114}
115
9f30a190
MM
116static void close_ns(int ns_fd[LXC_NS_MAX]) {
117 int i;
118
9f30a190
MM
119 for (i = 0; i < LXC_NS_MAX; i++) {
120 if (ns_fd[i] > -1) {
121 close(ns_fd[i]);
122 ns_fd[i] = -1;
123 }
124 }
9f30a190
MM
125}
126
62d05d9b
SH
127/*
128 * preserve_ns: open /proc/@pid/ns/@ns for each namespace specified
129 * in clone_flags.
130 * Return true on success, false on failure. On failure, leave an error
131 * message in *errmsg, which caller must free.
132 */
133static
134bool preserve_ns(int ns_fd[LXC_NS_MAX], int clone_flags, pid_t pid, char **errmsg) {
135 int i, ret;
9f30a190
MM
136 char path[MAXPATHLEN];
137
9f30a190
MM
138 for (i = 0; i < LXC_NS_MAX; i++)
139 ns_fd[i] = -1;
140
035a38fc
WB
141 snprintf(path, MAXPATHLEN, "/proc/%d/ns", pid);
142 if (access(path, X_OK)) {
62d05d9b
SH
143 if (asprintf(errmsg, "Kernel does not support setns.") == -1)
144 *errmsg = NULL;
145 return false;
cd43d2d1
SH
146 }
147
9f30a190
MM
148 for (i = 0; i < LXC_NS_MAX; i++) {
149 if ((clone_flags & ns_info[i].clone_flag) == 0)
150 continue;
035a38fc
WB
151 snprintf(path, MAXPATHLEN, "/proc/%d/ns/%s", pid,
152 ns_info[i].proc_name);
9f30a190 153 ns_fd[i] = open(path, O_RDONLY | O_CLOEXEC);
9f30a190
MM
154 if (ns_fd[i] < 0)
155 goto error;
156 }
157
62d05d9b 158 return true;
9f30a190
MM
159
160error:
62d05d9b
SH
161 if (errno == ENOENT) {
162 ret = asprintf(errmsg, "Kernel does not support setns for %s",
163 ns_info[i].proc_name);
164 } else {
165 ret = asprintf(errmsg, "Failed to open %s: %s",
166 path, strerror(errno));
167 }
168 if (ret == -1)
169 *errmsg = NULL;
9f30a190 170 close_ns(ns_fd);
62d05d9b 171 return false;
9f30a190
MM
172}
173
174static int attach_ns(const int ns_fd[LXC_NS_MAX]) {
175 int i;
176
177 for (i = 0; i < LXC_NS_MAX; i++) {
178 if (ns_fd[i] < 0)
179 continue;
180
181 if (setns(ns_fd[i], 0) != 0)
182 goto error;
183 }
184 return 0;
185
186error:
187 SYSERROR("failed to set namespace '%s'", ns_info[i].proc_name);
188 return -1;
189}
190
80090207
CLG
191static int match_fd(int fd)
192{
193 return (fd == 0 || fd == 1 || fd == 2);
194}
195
d2cf4c37
SH
196/*
197 * Check for any fds we need to close
198 * * if fd_to_ignore != -1, then if we find that fd open we will ignore it.
199 * * By default we warn about open fds we find.
200 * * If closeall is true, we will close open fds.
201 * * If lxc-start was passed "-C", then conf->close_all_fds will be true,
202 * in which case we also close all open fds.
203 * * A daemonized container will always pass closeall=true.
204 */
205int lxc_check_inherited(struct lxc_conf *conf, bool closeall, int fd_to_ignore)
80090207
CLG
206{
207 struct dirent dirent, *direntp;
208 int fd, fddir;
209 DIR *dir;
80090207 210
d2cf4c37
SH
211 if (conf && conf->close_all_fds)
212 closeall = true;
213
b119f362 214restart:
80090207
CLG
215 dir = opendir("/proc/self/fd");
216 if (!dir) {
217 WARN("failed to open directory: %m");
218 return -1;
219 }
220
221 fddir = dirfd(dir);
222
223 while (!readdir_r(dir, &dirent, &direntp)) {
80090207
CLG
224 if (!direntp)
225 break;
226
227 if (!strcmp(direntp->d_name, "."))
228 continue;
229
230 if (!strcmp(direntp->d_name, ".."))
231 continue;
232
233 fd = atoi(direntp->d_name);
234
f2faa8fa 235 if (fd == fddir || fd == lxc_log_fd || fd == fd_to_ignore)
80090207
CLG
236 continue;
237
858377e4
SH
238 if (current_config && fd == current_config->logfd)
239 continue;
240
80090207
CLG
241 if (match_fd(fd))
242 continue;
80090207 243
d2cf4c37 244 if (closeall) {
b119f362
SH
245 close(fd);
246 closedir(dir);
247 INFO("closed inherited fd %d", fd);
248 goto restart;
249 }
92c7f629 250 WARN("inherited fd %d", fd);
80090207
CLG
251 }
252
92c7f629
GK
253 closedir(dir); /* cannot fail */
254 return 0;
80090207
CLG
255}
256
83ee7875 257static int setup_signal_fd(sigset_t *oldmask)
b0a33c1e 258{
259 sigset_t mask;
260 int fd;
261
f3304a29
FW
262 /* Block everything except serious error signals */
263 if (sigfillset(&mask) ||
264 sigdelset(&mask, SIGILL) ||
265 sigdelset(&mask, SIGSEGV) ||
266 sigdelset(&mask, SIGBUS) ||
b5159817 267 sigdelset(&mask, SIGWINCH) ||
f3304a29
FW
268 sigprocmask(SIG_BLOCK, &mask, oldmask)) {
269 SYSERROR("failed to set signal mask");
b0a33c1e 270 return -1;
271 }
272
273 fd = signalfd(-1, &mask, 0);
274 if (fd < 0) {
36eb9bde 275 SYSERROR("failed to create the signal fd");
b0a33c1e 276 return -1;
277 }
278
279 if (fcntl(fd, F_SETFD, FD_CLOEXEC)) {
36eb9bde 280 SYSERROR("failed to set sigfd to close-on-exec");
b0a33c1e 281 close(fd);
282 return -1;
283 }
284
1ac470c0
DL
285 DEBUG("sigchild handler set");
286
b0a33c1e 287 return fd;
288}
289
84c92abd 290static int signal_handler(int fd, uint32_t events, void *data,
b0a33c1e 291 struct lxc_epoll_descr *descr)
292{
15cd25fd 293 struct signalfd_siginfo siginfo;
80507ee8 294 siginfo_t info;
15cd25fd 295 int ret;
82d89dce 296 pid_t *pid = data;
80507ee8 297 bool init_died = false;
15cd25fd
DL
298
299 ret = read(fd, &siginfo, sizeof(siginfo));
300 if (ret < 0) {
f3304a29 301 ERROR("failed to read signal info");
15cd25fd
DL
302 return -1;
303 }
304
305 if (ret != sizeof(siginfo)) {
306 ERROR("unexpected siginfo size");
307 return -1;
308 }
309
80507ee8
SH
310 // check whether init is running
311 info.si_pid = 0;
312 ret = waitid(P_PID, *pid, &info, WEXITED | WNOWAIT | WNOHANG);
313 if (ret == 0 && info.si_pid == *pid) {
314 init_died = true;
315 }
316
f3304a29
FW
317 if (siginfo.ssi_signo != SIGCHLD) {
318 kill(*pid, siginfo.ssi_signo);
319 INFO("forwarded signal %d to pid %d", siginfo.ssi_signo, *pid);
80507ee8 320 return init_died ? 1 : 0;
f3304a29
FW
321 }
322
15cd25fd
DL
323 if (siginfo.ssi_code == CLD_STOPPED ||
324 siginfo.ssi_code == CLD_CONTINUED) {
325 INFO("container init process was stopped/continued");
80507ee8 326 return init_died ? 1 : 0;
15cd25fd 327 }
1ac470c0 328
82d89dce
DL
329 /* more robustness, protect ourself from a SIGCHLD sent
330 * by a process different from the container init
331 */
332 if (siginfo.ssi_pid != *pid) {
333 WARN("invalid pid for SIGCHLD");
80507ee8 334 return init_died ? 1 : 0;
82d89dce
DL
335 }
336
15cd25fd 337 DEBUG("container init process exited");
b0a33c1e 338 return 1;
339}
340
735f2c6e 341int lxc_set_state(const char *name, struct lxc_handler *handler, lxc_state_t state)
66aeffc7
DL
342{
343 handler->state = state;
9123e471 344 lxc_monitor_send_state(name, state, handler->lxcpath);
66aeffc7
DL
345 return 0;
346}
347
735f2c6e 348int lxc_poll(const char *name, struct lxc_handler *handler)
b0a33c1e 349{
ca5f7926
DL
350 int sigfd = handler->sigfd;
351 int pid = handler->pid;
b0a33c1e 352 struct lxc_epoll_descr descr;
353
a9e61274 354 if (lxc_mainloop_open(&descr)) {
36eb9bde 355 ERROR("failed to create mainloop");
50c8bf05 356 goto out_sigfd;
b0a33c1e 357 }
358
83ee7875 359 if (lxc_mainloop_add_handler(&descr, sigfd, signal_handler, &pid)) {
36eb9bde 360 ERROR("failed to add handler for the signal");
b0a33c1e 361 goto out_mainloop_open;
362 }
363
63376d7d
DL
364 if (lxc_console_mainloop_add(&descr, handler)) {
365 ERROR("failed to add console handler to mainloop");
366 goto out_mainloop_open;
367 }
368
ef6e34ee 369 if (lxc_cmd_mainloop_add(name, &descr, handler)) {
563f2f2c 370 ERROR("failed to add command handler to mainloop");
96fa1ff0 371 goto out_mainloop_open;
563f2f2c
DL
372 }
373
828695d9 374 if (handler->conf->need_utmp_watch) {
495d2046 375 #if HAVE_SYS_CAPABILITY_H
828695d9
SH
376 if (lxc_utmp_mainloop_add(&descr, handler)) {
377 ERROR("failed to add utmp handler to mainloop");
378 goto out_mainloop_open;
379 }
495d2046 380 #else
959aee9c 381 DEBUG("not starting utmp handler as cap_sys_boot cannot be dropped without capabilities support");
495d2046 382 #endif
563f2f2c 383 }
b0a33c1e 384
e51d4895 385 return lxc_mainloop(&descr, -1);
b0a33c1e 386
387out_mainloop_open:
388 lxc_mainloop_close(&descr);
b0a33c1e 389out_sigfd:
390 close(sigfd);
c3e13372 391 return -1;
b0a33c1e 392}
393
13f5be62 394struct lxc_handler *lxc_init(const char *name, struct lxc_conf *conf, const char *lxcpath)
59eb99ba 395{
b6b2b194 396 int i;
3a0f472d
DL
397 struct lxc_handler *handler;
398
399 handler = malloc(sizeof(*handler));
400 if (!handler)
401 return NULL;
59eb99ba
DL
402
403 memset(handler, 0, sizeof(*handler));
404
e8bd4e43 405 handler->ttysock[0] = handler->ttysock[1] = -1;
fae349da 406 handler->conf = conf;
9123e471 407 handler->lxcpath = lxcpath;
5c068da9 408 handler->pinfd = -1;
fae349da 409
b6b2b194
WB
410 for (i = 0; i < LXC_NS_MAX; i++)
411 handler->nsfd[i] = -1;
412
fe4de9a6
DE
413 lsm_init();
414
3bdf52d7
DL
415 handler->name = strdup(name);
416 if (!handler->name) {
417 ERROR("failed to allocate memory");
418 goto out_free;
419 }
420
ef6e34ee 421 if (lxc_cmd_init(name, handler, lxcpath))
d2e30e99
DE
422 goto out_free_name;
423
8f2c3a70
SH
424 if (lxc_read_seccomp_config(conf) != 0) {
425 ERROR("failed loading seccomp policy");
d2e30e99 426 goto out_close_maincmd_fd;
8f2c3a70
SH
427 }
428
051151de 429 /* Begin by setting the state to STARTING */
25c2aca5 430 if (lxc_set_state(name, handler, STARTING)) {
59eb99ba 431 ERROR("failed to set state '%s'", lxc_state2str(STARTING));
051151de 432 goto out_close_maincmd_fd;
0ad19a3f 433 }
434
f7bee6c6 435 /* Start of environment variable setup for hooks */
ab7efcf5 436 if (name && setenv("LXC_NAME", name, 1)) {
f7bee6c6
MW
437 SYSERROR("failed to set environment variable for container name");
438 }
ab7efcf5 439 if (conf->rcfile && setenv("LXC_CONFIG_FILE", conf->rcfile, 1)) {
f7bee6c6
MW
440 SYSERROR("failed to set environment variable for config path");
441 }
ab7efcf5 442 if (conf->rootfs.mount && setenv("LXC_ROOTFS_MOUNT", conf->rootfs.mount, 1)) {
f7bee6c6
MW
443 SYSERROR("failed to set environment variable for rootfs mount");
444 }
ab7efcf5 445 if (conf->rootfs.path && setenv("LXC_ROOTFS_PATH", conf->rootfs.path, 1)) {
f7bee6c6
MW
446 SYSERROR("failed to set environment variable for rootfs mount");
447 }
448 if (conf->console.path && setenv("LXC_CONSOLE", conf->console.path, 1)) {
449 SYSERROR("failed to set environment variable for console path");
450 }
451 if (conf->console.log_path && setenv("LXC_CONSOLE_LOGPATH", conf->console.log_path, 1)) {
452 SYSERROR("failed to set environment variable for console log");
453 }
454 /* End of environment variable setup for hooks */
455
283678ed 456 if (run_lxc_hooks(name, "pre-start", conf, handler->lxcpath, NULL)) {
773fb9ca
SH
457 ERROR("failed to run pre-start hooks for container '%s'.", name);
458 goto out_aborting;
459 }
26ddeedd 460
b0a33c1e 461 /* the signal fd has to be created before forking otherwise
462 * if the child process exits before we setup the signal fd,
463 * the event will be lost and the command will be stuck */
83ee7875 464 handler->sigfd = setup_signal_fd(&handler->oldmask);
59eb99ba 465 if (handler->sigfd < 0) {
36eb9bde 466 ERROR("failed to set sigchild fd handler");
b5159817
DE
467 goto out_delete_tty;
468 }
469
470 /* do this after setting up signals since it might unblock SIGWINCH */
471 if (lxc_console_create(conf)) {
472 ERROR("failed to create console");
473 goto out_restore_sigmask;
b0a33c1e 474 }
475
c4d10a05
SH
476 if (ttys_shift_ids(conf) < 0) {
477 ERROR("Failed to shift tty into container");
478 goto out_restore_sigmask;
479 }
480
c3e13372 481 INFO("'%s' is initialized", name);
3a0f472d 482 return handler;
59eb99ba 483
b5159817
DE
484out_restore_sigmask:
485 sigprocmask(SIG_SETMASK, &handler->oldmask, NULL);
59eb99ba 486out_delete_tty:
fae349da 487 lxc_delete_tty(&conf->tty_info);
59eb99ba 488out_aborting:
25c2aca5 489 lxc_set_state(name, handler, ABORTING);
d2e30e99
DE
490out_close_maincmd_fd:
491 close(conf->maincmd_fd);
492 conf->maincmd_fd = -1;
3bdf52d7
DL
493out_free_name:
494 free(handler->name);
495 handler->name = NULL;
3a0f472d
DL
496out_free:
497 free(handler);
c3e13372 498 return NULL;
59eb99ba
DL
499}
500
3b72c4a0 501void lxc_fini(const char *name, struct lxc_handler *handler)
59eb99ba 502{
b3286b62
WB
503 int i, rc;
504 pid_t self = getpid();
505 char *namespaces[LXC_NS_MAX+1];
506 size_t namespace_count = 0;
b6b2b194 507
59eb99ba
DL
508 /* The STOPPING state is there for future cleanup code
509 * which can take awhile
510 */
25c2aca5 511 lxc_set_state(name, handler, STOPPING);
b6b2b194 512
b3286b62
WB
513 for (i = 0; i < LXC_NS_MAX; i++) {
514 if (handler->nsfd[i] != -1) {
515 rc = asprintf(&namespaces[namespace_count], "%s:/proc/%d/fd/%d",
516 ns_info[i].proc_name, self, handler->nsfd[i]);
517 if (rc == -1) {
518 SYSERROR("failed to allocate memory");
519 break;
520 }
521 ++namespace_count;
522 }
523 }
524 namespaces[namespace_count] = NULL;
c154af98
SG
525
526 if (handler->conf->reboot && setenv("LXC_TARGET", "reboot", 1)) {
527 SYSERROR("failed to set environment variable for stop target");
528 }
529 if (!handler->conf->reboot && setenv("LXC_TARGET", "stop", 1)) {
530 SYSERROR("failed to set environment variable for stop target");
531 }
532
b3286b62 533 if (run_lxc_hooks(name, "stop", handler->conf, handler->lxcpath, namespaces))
8438bfbd 534 ERROR("failed to run stop hooks for container '%s'.", name);
c154af98 535
b3286b62
WB
536 while (namespace_count--)
537 free(namespaces[namespace_count]);
b6b2b194
WB
538 for (i = 0; i < LXC_NS_MAX; i++) {
539 if (handler->nsfd[i] != -1) {
540 close(handler->nsfd[i]);
541 handler->nsfd[i] = -1;
542 }
543 }
25c2aca5 544 lxc_set_state(name, handler, STOPPED);
59eb99ba 545
283678ed 546 if (run_lxc_hooks(name, "post-stop", handler->conf, handler->lxcpath, NULL))
773fb9ca 547 ERROR("failed to run post-stop hooks for container '%s'.", name);
26ddeedd 548
83ee7875 549 /* reset mask set by setup_signal_fd */
8f64a3f6
MN
550 if (sigprocmask(SIG_SETMASK, &handler->oldmask, NULL))
551 WARN("failed to restore sigprocmask");
552
b5159817 553 lxc_console_delete(&handler->conf->console);
b2431939 554 lxc_delete_tty(&handler->conf->tty_info);
d2e30e99
DE
555 close(handler->conf->maincmd_fd);
556 handler->conf->maincmd_fd = -1;
3bdf52d7 557 free(handler->name);
e8bd4e43
SH
558 if (handler->ttysock[0] != -1) {
559 close(handler->ttysock[0]);
560 close(handler->ttysock[1]);
561 }
776d170c 562 if (handler->conf->ephemeral == 1 && handler->conf->reboot != 1) {
28272964
CB
563 lxc_destroy_container_on_signal(handler, name);
564 }
d4ef7c50 565 cgroup_destroy(handler);
b2431939 566 free(handler);
59eb99ba
DL
567}
568
735f2c6e 569void lxc_abort(const char *name, struct lxc_handler *handler)
59eb99ba 570{
73e608b2
SH
571 int ret, status;
572
25c2aca5 573 lxc_set_state(name, handler, ABORTING);
7d9fb3e9
DL
574 if (handler->pid > 0)
575 kill(handler->pid, SIGKILL);
73e608b2 576 while ((ret = waitpid(-1, &status, 0)) > 0) ;
59eb99ba
DL
577}
578
828695d9
SH
579#include <sys/reboot.h>
580#include <linux/reboot.h>
581
e2fa1520
SH
582/*
583 * reboot(LINUX_REBOOT_CMD_CAD_ON) will return -EINVAL
584 * in a child pid namespace if container reboot support exists.
585 * Otherwise, it will either succeed or return -EPERM.
586 */
587static int container_reboot_supported(void *arg)
828695d9 588{
d028235d 589 int *cmd = arg;
828695d9 590 int ret;
828695d9 591
d028235d 592 ret = reboot(*cmd);
e2fa1520
SH
593 if (ret == -1 && errno == EINVAL)
594 return 1;
595 return 0;
596}
597
b60ed720 598static int must_drop_cap_sys_boot(struct lxc_conf *conf)
e2fa1520 599{
025ed0f3 600 FILE *f;
b60ed720 601 int ret, cmd, v, flags;
d028235d
SG
602 long stack_size = 4096;
603 void *stack = alloca(stack_size);
604 int status;
605 pid_t pid;
e2fa1520 606
025ed0f3 607 f = fopen("/proc/sys/kernel/ctrl-alt-del", "r");
e2fa1520
SH
608 if (!f) {
609 DEBUG("failed to open /proc/sys/kernel/ctrl-alt-del");
828695d9 610 return 1;
e2fa1520 611 }
828695d9
SH
612
613 ret = fscanf(f, "%d", &v);
614 fclose(f);
e2fa1520
SH
615 if (ret != 1) {
616 DEBUG("Failed to read /proc/sys/kernel/ctrl-alt-del");
828695d9 617 return 1;
e2fa1520
SH
618 }
619 cmd = v ? LINUX_REBOOT_CMD_CAD_ON : LINUX_REBOOT_CMD_CAD_OFF;
620
b60ed720
SH
621 flags = CLONE_NEWPID | SIGCHLD;
622 if (!lxc_list_empty(&conf->id_map))
623 flags |= CLONE_NEWUSER;
624
7f145a6d 625#ifdef __ia64__
959aee9c 626 pid = __clone2(container_reboot_supported, stack, stack_size, flags, &cmd);
7f145a6d 627#else
959aee9c
SG
628 stack += stack_size;
629 pid = clone(container_reboot_supported, stack, flags, &cmd);
7f145a6d 630#endif
959aee9c 631 if (pid < 0) {
c08220e9 632 if (flags & CLONE_NEWUSER)
4a7e5f4f 633 ERROR("failed to clone (%#x): %s (includes CLONE_NEWUSER)", flags, strerror(errno));
c08220e9 634 else
4a7e5f4f 635 ERROR("failed to clone (%#x): %s", flags, strerror(errno));
959aee9c
SG
636 return -1;
637 }
638 if (wait(&status) < 0) {
639 SYSERROR("unexpected wait error: %m");
640 return -1;
641 }
e2fa1520
SH
642
643 if (WEXITSTATUS(status) != 1)
828695d9 644 return 1;
e2fa1520 645
828695d9
SH
646 return 0;
647}
648
658979c5
SH
649/*
650 * netpipe is used in the unprivileged case to transfer the ifindexes
651 * from parent to child
652 */
653static int netpipe = -1;
654
655static inline int count_veths(struct lxc_list *network)
656{
657 struct lxc_list *iterator;
658 struct lxc_netdev *netdev;
659 int count = 0;
660
661 lxc_list_for_each(iterator, network) {
662 netdev = iterator->elem;
663 if (netdev->type != LXC_NET_VETH)
664 continue;
665 count++;
666 }
667 return count;
668}
669
670static int read_unpriv_netifindex(struct lxc_list *network)
671{
672 struct lxc_list *iterator;
673 struct lxc_netdev *netdev;
674
675 if (netpipe == -1)
676 return 0;
677 lxc_list_for_each(iterator, network) {
678 netdev = iterator->elem;
679 if (netdev->type != LXC_NET_VETH)
680 continue;
681 if (!(netdev->name = malloc(IFNAMSIZ))) {
682 ERROR("Out of memory");
683 close(netpipe);
684 return -1;
685 }
686 if (read(netpipe, netdev->name, IFNAMSIZ) != IFNAMSIZ) {
687 close(netpipe);
688 return -1;
689 }
690 }
691 close(netpipe);
692 return 0;
693}
694
ffe1e01a 695static int do_start(void *data)
50e98013 696{
7c661726 697 struct lxc_list *iterator;
23c53af9 698 struct lxc_handler *handler = data;
50e98013
DL
699
700 if (sigprocmask(SIG_SETMASK, &handler->oldmask, NULL)) {
701 SYSERROR("failed to set sigprocmask");
9d7f9e52 702 return -1;
50e98013
DL
703 }
704
d028235d 705 /* This prctl must be before the synchro, so if the parent
743ecd2e
DL
706 * dies before we set the parent death signal, we will detect
707 * its death with the synchro right after, otherwise we have
708 * a window where the parent can exit before we set the pdeath
709 * signal leading to a unsupervized container.
710 */
711 if (prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0)) {
712 SYSERROR("failed to set pdeath signal");
713 return -1;
714 }
715
3c22086f 716 lxc_sync_fini_parent(handler);
50e98013 717
2b0e17e4 718 /* don't leak the pinfd to the container */
025ed0f3 719 if (handler->pinfd >= 0) {
0d03360a 720 close(handler->pinfd);
025ed0f3 721 }
2b0e17e4 722
3c22086f
CLG
723 /* Tell the parent task it can begin to configure the
724 * container and wait for it to finish
725 */
726 if (lxc_sync_barrier_parent(handler, LXC_SYNC_CONFIGURE))
9d7f9e52 727 return -1;
50e98013 728
658979c5
SH
729 if (read_unpriv_netifindex(&handler->conf->network) < 0)
730 goto out_warn_father;
731
f6d3e3e4
SH
732 /*
733 * if we are in a new user namespace, become root there to have
62d00539
PT
734 * privilege over our namespace. When using lxc-execute we default to root,
735 * but this can be overriden using the lxc.init_uid and lxc.init_gid
736 * configuration options.
f6d3e3e4
SH
737 */
738 if (!lxc_list_empty(&handler->conf->id_map)) {
c5cd20ce
PT
739 gid_t new_gid = 0;
740 if (handler->conf->is_execute && handler->conf->init_gid)
741 new_gid = handler->conf->init_gid;
742
743 uid_t new_uid = 0;
744 if (handler->conf->is_execute && handler->conf->init_uid)
745 new_uid = handler->conf->init_uid;
746
56f8ff00
PT
747 NOTICE("switching to gid/uid %d/%d in new user namespace", new_gid, new_uid);
748 if (setgid(new_gid)) {
f6d3e3e4
SH
749 SYSERROR("setgid");
750 goto out_warn_father;
751 }
56f8ff00 752 if (setuid(new_uid)) {
f6d3e3e4
SH
753 SYSERROR("setuid");
754 goto out_warn_father;
755 }
c476bdce
SH
756 if (setgroups(0, NULL)) {
757 SYSERROR("setgroups");
758 goto out_warn_father;
759 }
f6d3e3e4
SH
760 }
761
99b71824 762 if (access(handler->lxcpath, X_OK)) {
c8154066
SH
763 print_top_failing_dir(handler->lxcpath);
764 goto out_warn_father;
765 }
766
495d2046 767 #if HAVE_SYS_CAPABILITY_H
69182a31 768 if (handler->conf->need_utmp_watch) {
828695d9
SH
769 if (prctl(PR_CAPBSET_DROP, CAP_SYS_BOOT, 0, 0, 0)) {
770 SYSERROR("failed to remove CAP_SYS_BOOT capability");
c4ea60df 771 goto out_warn_father;
828695d9 772 }
959aee9c 773 DEBUG("Dropped cap_sys_boot");
e2fa1520 774 }
495d2046 775 #endif
e2fa1520
SH
776
777 /* Setup the container, ip, names, utsname, ... */
d4ef7c50 778 if (lxc_setup(handler)) {
e2fa1520
SH
779 ERROR("failed to setup the container");
780 goto out_warn_father;
781 }
50e98013 782
544a48a0
SH
783 /* ask father to setup cgroups and wait for him to finish */
784 if (lxc_sync_barrier_parent(handler, LXC_SYNC_CGROUP))
785 return -1;
786
72863294 787 /* Set the label to change to when we exec(2) the container's init */
7aff4f43 788 if (lsm_process_label_set(NULL, handler->conf, 1, 1) < 0)
e075f5d9 789 goto out_warn_father;
5112cd70 790
0d9acb99
DE
791 /* Some init's such as busybox will set sane tty settings on stdin,
792 * stdout, stderr which it thinks is the console. We already set them
793 * the way we wanted on the real terminal, and we want init to do its
794 * setup on its console ie. the pty allocated in lxc_console_create()
795 * so make sure that that pty is stdin,stdout,stderr.
796 */
797 if (lxc_console_set_stdfds(handler) < 0)
798 goto out_warn_father;
799
5112cd70
SH
800 /* If we mounted a temporary proc, then unmount it now */
801 tmp_proc_unmount(handler->conf);
e075f5d9 802
8f2c3a70
SH
803 if (lxc_seccomp_load(handler->conf) != 0)
804 goto out_warn_father;
805
283678ed 806 if (run_lxc_hooks(handler->name, "start", handler->conf, handler->lxcpath, NULL)) {
773fb9ca
SH
807 ERROR("failed to run start hooks for container '%s'.", handler->name);
808 goto out_warn_father;
809 }
fc25b815 810
f7bee6c6 811 /* The clearenv() and putenv() calls have been moved here
ec64264d 812 * to allow us to use environment variables passed to the various
f7bee6c6
MW
813 * hooks, such as the start hook above. Not all of the
814 * variables like CONFIG_PATH or ROOTFS are valid in this
815 * context but others are. */
816 if (clearenv()) {
817 SYSERROR("failed to clear environment");
818 /* don't error out though */
819 }
820
7c661726
MP
821 lxc_list_for_each(iterator, &handler->conf->environment) {
822 if (putenv((char *)iterator->elem)) {
823 SYSERROR("failed to set environment variable '%s'", (char *)iterator->elem);
824 goto out_warn_father;
825 }
826 }
827
f7bee6c6 828 if (putenv("container=lxc")) {
7c661726 829 SYSERROR("failed to set environment variable 'container=lxc'");
c4ea60df 830 goto out_warn_father;
f7bee6c6
MW
831 }
832
393903d1
SH
833 if (handler->conf->pty_names) {
834 if (putenv(handler->conf->pty_names)) {
835 SYSERROR("failed to set environment variable for container ptys");
836 goto out_warn_father;
837 }
838 }
839
773fb9ca 840 close(handler->sigfd);
26ddeedd 841
69aeabac
TA
842 if (handler->backgrounded && null_stdfds() < 0)
843 goto out_warn_father;
507cee36 844
e6126dbe
MN
845 /* after this call, we are in error because this
846 * ops should not return as it execs */
c4ea60df 847 handler->ops->start(handler, handler->data);
50e98013
DL
848
849out_warn_father:
544a48a0
SH
850 /* we want the parent to know something went wrong, so any
851 * value other than what it expects is ok. */
3c22086f 852 lxc_sync_wake_parent(handler, LXC_SYNC_POST_CONFIGURE);
50e98013
DL
853 return -1;
854}
855
74a3920a 856static int save_phys_nics(struct lxc_conf *conf)
7b35f3d6
SH
857{
858 struct lxc_list *iterator;
859
860 lxc_list_for_each(iterator, &conf->network) {
861 struct lxc_netdev *netdev = iterator->elem;
862
863 if (netdev->type != LXC_NET_PHYS)
864 continue;
865 conf->saved_nics = realloc(conf->saved_nics,
866 (conf->num_savednics+1)*sizeof(struct saved_nic));
867 if (!conf->saved_nics) {
868 SYSERROR("failed to allocate memory");
869 return -1;
870 }
871 conf->saved_nics[conf->num_savednics].ifindex = netdev->ifindex;
872 conf->saved_nics[conf->num_savednics].orig_name = strdup(netdev->link);
873 if (!conf->saved_nics[conf->num_savednics].orig_name) {
874 SYSERROR("failed to allocate memory");
875 return -1;
876 }
959aee9c 877 INFO("stored saved_nic #%d idx %d name %s", conf->num_savednics,
7b35f3d6
SH
878 conf->saved_nics[conf->num_savednics].ifindex,
879 conf->saved_nics[conf->num_savednics].orig_name);
880 conf->num_savednics++;
881 }
882
883 return 0;
884}
885
e8bd4e43
SH
886static int recv_fd(int sock, int *fd)
887{
888 if (lxc_abstract_unix_recv_fd(sock, fd, NULL, 0) < 0) {
889 SYSERROR("Error receiving tty fd from child");
890 return -1;
891 }
892 if (*fd == -1)
893 return -1;
894 return 0;
895}
896
897static int recv_ttys_from_child(struct lxc_handler *handler)
898{
899 struct lxc_conf *conf = handler->conf;
900 int i, sock = handler->ttysock[1];
901 struct lxc_tty_info *tty_info = &conf->tty_info;
902
903 if (!conf->tty)
904 return 0;
905
906 tty_info->pty_info = malloc(sizeof(*tty_info->pty_info)*conf->tty);
907 if (!tty_info->pty_info) {
908 SYSERROR("failed to allocate pty_info");
909 return -1;
910 }
911
912 for (i = 0; i < conf->tty; i++) {
913 struct lxc_pty_info *pty_info = &tty_info->pty_info[i];
914 pty_info->busy = 0;
915 if (recv_fd(sock, &pty_info->slave) < 0 ||
916 recv_fd(sock, &pty_info->master) < 0) {
917 ERROR("Error receiving tty info from child");
918 return -1;
919 }
920 }
921 tty_info->nbtty = conf->tty;
922
923 return 0;
924}
925
f813849c
TA
926void resolve_clone_flags(struct lxc_handler *handler)
927{
928 handler->clone_flags = CLONE_NEWPID | CLONE_NEWNS;
929
930 if (!lxc_list_empty(&handler->conf->id_map)) {
931 INFO("Cloning a new user namespace");
932 handler->clone_flags |= CLONE_NEWUSER;
933 }
934
935 if (handler->conf->inherit_ns_fd[LXC_NS_NET] == -1) {
936 if (!lxc_requests_empty_network(handler))
937 handler->clone_flags |= CLONE_NEWNET;
938 } else {
939 INFO("Inheriting a net namespace");
940 }
941
942 if (handler->conf->inherit_ns_fd[LXC_NS_IPC] == -1) {
943 handler->clone_flags |= CLONE_NEWIPC;
944 } else {
945 INFO("Inheriting an IPC namespace");
946 }
947
948 if (handler->conf->inherit_ns_fd[LXC_NS_UTS] == -1) {
949 handler->clone_flags |= CLONE_NEWUTS;
950 } else {
951 INFO("Inheriting a UTS namespace");
952 }
953}
954
74a3920a 955static int lxc_spawn(struct lxc_handler *handler)
59eb99ba 956{
b98f7d6e 957 int failed_before_rename = 0;
ffe1e01a 958 const char *name = handler->name;
62d05d9b 959 char *errmsg = NULL;
7e4dfe0b 960 bool cgroups_connected = false;
9f30a190
MM
961 int saved_ns_fd[LXC_NS_MAX];
962 int preserve_mask = 0, i;
658979c5 963 int netpipepair[2], nveths;
9f30a190 964
2f2623ec
SH
965 netpipe = -1;
966
9f30a190 967 for (i = 0; i < LXC_NS_MAX; i++)
6c544cb3 968 if (handler->conf->inherit_ns_fd[i] != -1)
9f30a190 969 preserve_mask |= ns_info[i].clone_flag;
50e98013 970
3c22086f 971 if (lxc_sync_init(handler))
9d7f9e52 972 return -1;
0ad19a3f 973
e8bd4e43
SH
974 if (socketpair(AF_UNIX, SOCK_DGRAM, 0, handler->ttysock) < 0) {
975 lxc_sync_fini(handler);
976 return -1;
977 }
978
f813849c 979 resolve_clone_flags(handler);
9f30a190 980
f813849c 981 if (handler->clone_flags & CLONE_NEWNET) {
26b797f3
SH
982 if (!lxc_list_empty(&handler->conf->network)) {
983
9f30a190
MM
984 /* Find gateway addresses from the link device, which is
985 * no longer accessible inside the container. Do this
986 * before creating network interfaces, since goto
987 * out_delete_net does not work before lxc_clone. */
988 if (lxc_find_gateway_addresses(handler)) {
989 ERROR("failed to find gateway addresses");
990 lxc_sync_fini(handler);
991 return -1;
992 }
993
994 /* that should be done before the clone because we will
995 * fill the netdev index and use them in the child
996 */
997 if (lxc_create_network(handler)) {
38521a3b 998 ERROR("failed to create the network");
9f30a190
MM
999 lxc_sync_fini(handler);
1000 return -1;
1001 }
19a26f82
MK
1002 }
1003
9f30a190
MM
1004 if (save_phys_nics(handler->conf)) {
1005 ERROR("failed to save physical nic info");
1006 goto out_abort;
82d5ae15
DL
1007 }
1008 }
1009
d4ef7c50
SH
1010 if (!cgroup_init(handler)) {
1011 ERROR("failed initializing cgroup support");
33ad9f1a
CS
1012 goto out_delete_net;
1013 }
1014
7e4dfe0b
SH
1015 cgroups_connected = true;
1016
d4ef7c50
SH
1017 if (!cgroup_create(handler)) {
1018 ERROR("failed creating cgroups");
47d8fb3b
CS
1019 goto out_delete_net;
1020 }
1021
0c547523
SH
1022 /*
1023 * if the rootfs is not a blockdev, prevent the container from
1024 * marking it readonly.
5e32a990
ÇO
1025 *
1026 * if the container is unprivileged then skip rootfs pinning
0c547523 1027 */
5e32a990
ÇO
1028 if (lxc_list_empty(&handler->conf->id_map)) {
1029 handler->pinfd = pin_rootfs(handler->conf->rootfs.path);
1030 if (handler->pinfd == -1)
1031 INFO("failed to pin the container's rootfs");
1032 }
0c547523 1033
62d05d9b
SH
1034 if (!preserve_ns(saved_ns_fd, preserve_mask, getpid(), &errmsg)) {
1035 SYSERROR("Failed to preserve requested namespaces: %s",
1036 errmsg ? errmsg : "(Out of memory)");
1037 free(errmsg);
cd43d2d1 1038 goto out_delete_net;
62d05d9b 1039 }
cd43d2d1
SH
1040 if (attach_ns(handler->conf->inherit_ns_fd) < 0)
1041 goto out_delete_net;
9f30a190 1042
658979c5
SH
1043 if (am_unpriv() && (nveths = count_veths(&handler->conf->network))) {
1044 if (pipe(netpipepair) < 0) {
1045 SYSERROR("Error creating pipe");
1046 goto out_delete_net;
1047 }
1048 /* store netpipe in the global var for do_start's use */
1049 netpipe = netpipepair[0];
1050 }
1051
0ad19a3f 1052 /* Create a process in a new set of namespaces */
d5088cf2 1053 handler->pid = lxc_clone(do_start, handler, handler->clone_flags);
59eb99ba 1054 if (handler->pid < 0) {
36eb9bde 1055 SYSERROR("failed to fork into a new namespace");
7fef7a06 1056 goto out_delete_net;
0ad19a3f 1057 }
1058
62d05d9b
SH
1059 if (preserve_ns(handler->nsfd, handler->clone_flags, handler->pid,
1060 &errmsg) < 0) {
1061 INFO("Failed to store namespace references for stop hook: %s",
1062 errmsg ? errmsg : "(Out of memory)");
1063 free(errmsg);
b6b2b194
WB
1064 }
1065
cd43d2d1
SH
1066 if (attach_ns(saved_ns_fd))
1067 WARN("failed to restore saved namespaces");
9f30a190 1068
3c22086f
CLG
1069 lxc_sync_fini_child(handler);
1070
1071 if (lxc_sync_wait_child(handler, LXC_SYNC_CONFIGURE))
99a6af52 1072 failed_before_rename = 1;
0ad19a3f 1073
d4ef7c50
SH
1074 if (!cgroup_create_legacy(handler)) {
1075 ERROR("failed to setup the legacy cgroups for %s", name);
ae5c8b8e 1076 goto out_delete_net;
33ad9f1a 1077 }
9daf6f5d
SH
1078 if (!cgroup_setup_limits(handler, false)) {
1079 ERROR("failed to setup the cgroup limits for '%s'", name);
6031a6e5
DE
1080 goto out_delete_net;
1081 }
1082
d4ef7c50 1083 if (!cgroup_enter(handler))
7fef7a06 1084 goto out_delete_net;
218d4250 1085
0996e18a
SH
1086 if (!cgroup_chown(handler))
1087 goto out_delete_net;
1088
99a6af52
MN
1089 if (failed_before_rename)
1090 goto out_delete_net;
1091
0ad19a3f 1092 /* Create the network configuration */
d5088cf2 1093 if (handler->clone_flags & CLONE_NEWNET) {
fae349da 1094 if (lxc_assign_network(&handler->conf->network, handler->pid)) {
82d5ae15 1095 ERROR("failed to create the configured network");
7fef7a06 1096 goto out_delete_net;
82d5ae15 1097 }
0ad19a3f 1098 }
1099
658979c5
SH
1100 if (netpipe != -1) {
1101 struct lxc_list *iterator;
1102 struct lxc_netdev *netdev;
1103
1104 close(netpipe);
1105 lxc_list_for_each(iterator, &handler->conf->network) {
1106 netdev = iterator->elem;
1107 if (netdev->type != LXC_NET_VETH)
1108 continue;
1109 if (write(netpipepair[1], netdev->name, IFNAMSIZ) != IFNAMSIZ) {
1110 ERROR("Error writing veth name to container");
1111 goto out_delete_net;
1112 }
1113 }
1114 close(netpipepair[1]);
1115 }
1116
f6d3e3e4
SH
1117 /* map the container uids - the container became an invalid
1118 * userid the moment it was cloned with CLONE_NEWUSER - this
1119 * call doesn't change anything immediately, but allows the
1120 * container to setuid(0) (0 being mapped to something else on
1121 * the host) later to become a valid uid again */
1122 if (lxc_map_ids(&handler->conf->id_map, handler->pid)) {
1123 ERROR("failed to set up id mapping");
1124 goto out_delete_net;
1125 }
1126
544a48a0
SH
1127 /* Tell the child to continue its initialization. we'll get
1128 * LXC_SYNC_CGROUP when it is ready for us to setup cgroups
3c22086f
CLG
1129 */
1130 if (lxc_sync_barrier_child(handler, LXC_SYNC_POST_CONFIGURE))
544a48a0
SH
1131 goto out_delete_net;
1132
9daf6f5d 1133 if (!cgroup_setup_limits(handler, true)) {
b98f7d6e
SH
1134 ERROR("failed to setup the devices cgroup for '%s'", name);
1135 goto out_delete_net;
544a48a0
SH
1136 }
1137
73d28d42 1138 cgroup_disconnect();
7e4dfe0b 1139 cgroups_connected = false;
73d28d42 1140
e8bd4e43
SH
1141 /* read tty fds allocated by child */
1142 if (recv_ttys_from_child(handler) < 0) {
1143 ERROR("failed to receive tty info from child");
1144 goto out_delete_net;
1145 }
1146
544a48a0
SH
1147 /* Tell the child to complete its initialization and wait for
1148 * it to exec or return an error. (the child will never
1149 * return LXC_SYNC_POST_CGROUP+1. It will either close the
1150 * sync pipe, causing lxc_sync_barrier_child to return
1151 * success, or return a different value, causing us to error
1152 * out).
1153 */
1154 if (lxc_sync_barrier_child(handler, LXC_SYNC_POST_CGROUP))
3c22086f 1155 return -1;
0ad19a3f 1156
cc28d0b0
SH
1157 if (detect_shared_rootfs())
1158 umount2(handler->conf->rootfs.mount, MNT_DETACH);
1159
23c53af9 1160 if (handler->ops->post_start(handler, handler->data))
e6126dbe
MN
1161 goto out_abort;
1162
25c2aca5 1163 if (lxc_set_state(name, handler, RUNNING)) {
59eb99ba
DL
1164 ERROR("failed to set state to %s",
1165 lxc_state2str(RUNNING));
1166 goto out_abort;
3f21c114 1167 }
22ebac19 1168
3c22086f 1169 lxc_sync_fini(handler);
0c547523 1170
e6126dbe 1171 return 0;
1ac470c0 1172
7fef7a06 1173out_delete_net:
7e4dfe0b
SH
1174 if (cgroups_connected)
1175 cgroup_disconnect();
d5088cf2 1176 if (handler->clone_flags & CLONE_NEWNET)
74a2b586 1177 lxc_delete_network(handler);
59eb99ba
DL
1178out_abort:
1179 lxc_abort(name, handler);
3c22086f 1180 lxc_sync_fini(handler);
5c068da9
SH
1181 if (handler->pinfd >= 0) {
1182 close(handler->pinfd);
1183 handler->pinfd = -1;
1184 }
1185
b79fcd86 1186 return -1;
59eb99ba 1187}
0ad19a3f 1188
2af6bd1b
SH
1189int get_netns_fd(int pid)
1190{
1191 char path[MAXPATHLEN];
1192 int ret, fd;
1193
1194 ret = snprintf(path, MAXPATHLEN, "/proc/%d/ns/net", pid);
1195 if (ret < 0 || ret >= MAXPATHLEN) {
1196 WARN("Failed to pin netns file for pid %d", pid);
1197 return -1;
1198 }
1199
1200 fd = open(path, O_RDONLY);
1201 if (fd < 0) {
1202 WARN("Failed to pin netns file %s for pid %d: %s",
1203 path, pid, strerror(errno));
1204 return -1;
1205 }
1206 return fd;
1207}
1208
ee70bf78 1209int __lxc_start(const char *name, struct lxc_conf *conf,
507cee36
TA
1210 struct lxc_operations* ops, void *data, const char *lxcpath,
1211 bool backgrounded)
59eb99ba 1212{
3a0f472d 1213 struct lxc_handler *handler;
e043236e 1214 int err = -1;
59eb99ba 1215 int status;
2af6bd1b 1216 int netnsfd = -1;
80090207 1217
13f5be62 1218 handler = lxc_init(name, conf, lxcpath);
3a0f472d 1219 if (!handler) {
59eb99ba 1220 ERROR("failed to initialize the container");
66aeffc7 1221 return -1;
0ad19a3f 1222 }
ee70bf78
CLG
1223 handler->ops = ops;
1224 handler->data = data;
507cee36 1225 handler->backgrounded = backgrounded;
e6126dbe 1226
b60ed720 1227 if (must_drop_cap_sys_boot(handler->conf)) {
495d2046 1228 #if HAVE_SYS_CAPABILITY_H
959aee9c 1229 DEBUG("Dropping cap_sys_boot");
495d2046 1230 #else
959aee9c 1231 DEBUG("Can't drop cap_sys_boot as capabilities aren't supported");
495d2046 1232 #endif
69182a31 1233 } else {
959aee9c 1234 DEBUG("Not dropping cap_sys_boot or watching utmp");
69182a31
SH
1235 handler->conf->need_utmp_watch = 0;
1236 }
1237
76a26f55
SH
1238 if (!attach_block_device(handler->conf)) {
1239 ERROR("Failure attaching block device");
1240 goto out_fini_nonet;
1241 }
1242
35120d9c
SH
1243 if (geteuid() == 0 && !lxc_list_empty(&conf->id_map)) {
1244 /* if the backing store is a device, mount it here and now */
1245 if (rootfs_is_blockdev(conf)) {
1246 if (unshare(CLONE_NEWNS) < 0) {
1247 ERROR("Error unsharing mounts");
1248 goto out_fini_nonet;
1249 }
6a0c909a 1250 remount_all_slave();
35120d9c
SH
1251 if (do_rootfs_setup(conf, name, lxcpath) < 0) {
1252 ERROR("Error setting up rootfs mount as root before spawn");
1253 goto out_fini_nonet;
1254 }
1255 INFO("Set up container rootfs as host root");
1256 }
1257 }
1258
23c53af9 1259 err = lxc_spawn(handler);
59eb99ba 1260 if (err) {
ee70bf78 1261 ERROR("failed to spawn '%s'", name);
76a26f55 1262 goto out_detach_blockdev;
0ad19a3f 1263 }
1264
8bee8851
WB
1265 handler->conf->reboot = 0;
1266
2af6bd1b
SH
1267 netnsfd = get_netns_fd(handler->pid);
1268
3a0f472d 1269 err = lxc_poll(name, handler);
e043236e 1270 if (err) {
59eb99ba 1271 ERROR("mainloop exited with an error");
4b2f98f5
SH
1272 if (netnsfd >= 0)
1273 close(netnsfd);
59eb99ba
DL
1274 goto out_abort;
1275 }
0ad19a3f 1276
3a0f472d 1277 while (waitpid(handler->pid, &status, 0) < 0 && errno == EINTR)
1bc5cc8c 1278 continue;
e043236e 1279
8b004f07
SH
1280 /*
1281 * If the child process exited but was not signaled,
1282 * it didn't call reboot. This should mean it was an
1283 * lxc-execute which simply exited. In any case, treat
1284 * it as a 'halt'
1285 */
d028235d 1286 if (WIFSIGNALED(status)) {
8b004f07
SH
1287 switch(WTERMSIG(status)) {
1288 case SIGINT: /* halt */
1289 DEBUG("Container halting");
1290 break;
1291 case SIGHUP: /* reboot */
1292 DEBUG("Container rebooting");
1293 handler->conf->reboot = 1;
1294 break;
c2b9bd9e
SH
1295 case SIGSYS: /* seccomp */
1296 DEBUG("Container violated its seccomp policy");
1297 break;
8b004f07 1298 default:
959aee9c 1299 DEBUG("unknown exit status for init: %d", WTERMSIG(status));
8b004f07
SH
1300 break;
1301 }
d028235d 1302 }
828695d9 1303
ce5782df 1304 DEBUG("Pushing physical nics back to host namespace");
2af6bd1b 1305 lxc_rename_phys_nics_on_shutdown(netnsfd, handler->conf);
ce5782df
MH
1306
1307 DEBUG("Tearing down virtual network devices used by container");
1308 lxc_delete_network(handler);
1309
4b2f98f5
SH
1310 if (netnsfd >= 0)
1311 close(netnsfd);
7b35f3d6 1312
5c068da9
SH
1313 if (handler->pinfd >= 0) {
1314 close(handler->pinfd);
1315 handler->pinfd = -1;
1316 }
1317
1787abca 1318 lxc_monitor_send_exit_code(name, status, handler->lxcpath);
3a0f472d 1319 err = lxc_error_set_and_log(handler->pid, status);
9d7f9e52 1320out_fini:
74a2b586
JK
1321 lxc_delete_network(handler);
1322
76a26f55
SH
1323out_detach_blockdev:
1324 detach_block_device(handler->conf);
1325
74a2b586 1326out_fini_nonet:
3a0f472d 1327 lxc_fini(name, handler);
0ad19a3f 1328 return err;
1329
59eb99ba 1330out_abort:
3a0f472d 1331 lxc_abort(name, handler);
9d7f9e52 1332 goto out_fini;
0ad19a3f 1333}
ee70bf78
CLG
1334
1335struct start_args {
1336 char *const *argv;
1337};
1338
1339static int start(struct lxc_handler *handler, void* data)
1340{
1341 struct start_args *arg = data;
1342
1343 NOTICE("exec'ing '%s'", arg->argv[0]);
1344
1345 execvp(arg->argv[0], arg->argv);
1346 SYSERROR("failed to exec %s", arg->argv[0]);
1347 return 0;
1348}
1349
1350static int post_start(struct lxc_handler *handler, void* data)
1351{
1352 struct start_args *arg = data;
1353
1354 NOTICE("'%s' started with pid '%d'", arg->argv[0], handler->pid);
1355 return 0;
1356}
1357
1358static struct lxc_operations start_ops = {
1359 .start = start,
1360 .post_start = post_start
1361};
1362
13f5be62 1363int lxc_start(const char *name, char *const argv[], struct lxc_conf *conf,
507cee36 1364 const char *lxcpath, bool backgrounded)
ee70bf78
CLG
1365{
1366 struct start_args start_arg = {
1367 .argv = argv,
1368 };
1369
828695d9 1370 conf->need_utmp_watch = 1;
507cee36 1371 return __lxc_start(name, conf, &start_ops, &start_arg, lxcpath, backgrounded);
ee70bf78 1372}
28272964
CB
1373
1374static void lxc_destroy_container_on_signal(struct lxc_handler *handler,
1375 const char *name)
1376{
1377 char destroy[MAXPATHLEN];
1378 bool bret = true;
1379 int ret = 0;
f01f7975 1380 struct lxc_container *c;
df31363a 1381 if (handler->conf->rootfs.path && handler->conf->rootfs.mount) {
28272964
CB
1382 bret = do_destroy_container(handler->conf);
1383 if (!bret) {
1384 ERROR("Error destroying rootfs for %s", name);
1385 return;
1386 }
1387 }
1388 INFO("Destroyed rootfs for %s", name);
1389
1390 ret = snprintf(destroy, MAXPATHLEN, "%s/%s", handler->lxcpath, name);
1391 if (ret < 0 || ret >= MAXPATHLEN) {
1392 ERROR("Error printing path for %s", name);
1393 ERROR("Error destroying directory for %s", name);
1394 return;
1395 }
1396
f01f7975
CB
1397 c = lxc_container_new(name, handler->lxcpath);
1398 if (c) {
1399 if (container_disk_lock(c)) {
1400 INFO("Could not update lxc_snapshots file");
1401 lxc_container_put(c);
1402 } else {
1403 mod_all_rdeps(c, false);
1404 container_disk_unlock(c);
1405 lxc_container_put(c);
1406 }
1407 }
1408
28272964
CB
1409 if (am_unpriv())
1410 ret = userns_exec_1(handler->conf, lxc_rmdir_onedev_wrapper, destroy);
1411 else
1412 ret = lxc_rmdir_onedev(destroy, NULL);
1413
1414 if (ret < 0) {
1415 ERROR("Error destroying directory for %s", name);
1416 return;
1417 }
1418 INFO("Destroyed directory for %s", name);
1419}
1420
1421static int lxc_rmdir_onedev_wrapper(void *data)
1422{
1423 char *arg = (char *) data;
1424 return lxc_rmdir_onedev(arg, NULL);
1425}
1426
1427static bool do_destroy_container(struct lxc_conf *conf) {
d028235d
SG
1428 if (am_unpriv()) {
1429 if (userns_exec_1(conf, bdev_destroy_wrapper, conf) < 0)
1430 return false;
1431 return true;
1432 }
1433 return bdev_destroy(conf);
28272964
CB
1434}
1435