]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/start.c
Merge pull request #3190 from idatahu/fix_ovs_log
[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>
35a02107
CB
8 * Serge Hallyn <serge@hallyn.com>
9 * Christian Brauner <christian.brauner@ubuntu.com>
0ad19a3f 10 *
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
250b1eec 23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
0ad19a3f 24 */
25
d38dd64a
CB
26#ifndef _GNU_SOURCE
27#define _GNU_SOURCE 1
28#endif
0ad19a3f 29#include <dirent.h>
30#include <errno.h>
b0a33c1e 31#include <fcntl.h>
c476bdce 32#include <grp.h>
37515ebd 33#include <poll.h>
b467714b 34#include <pthread.h>
f3787121
CB
35#include <signal.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
0ad19a3f 39#include <sys/file.h>
f4d507d5 40#include <sys/mount.h>
f3787121 41#include <sys/param.h>
0ad19a3f 42#include <sys/prctl.h>
f3787121
CB
43#include <sys/socket.h>
44#include <sys/stat.h>
45#include <sys/syscall.h>
ddceb1f9 46#include <sys/types.h>
b0a33c1e 47#include <sys/un.h>
f3787121 48#include <sys/wait.h>
d38dd64a 49#include <unistd.h>
495d2046 50
f3787121
CB
51#include "af_unix.h"
52#include "caps.h"
563f2f2c 53#include "cgroup.h"
f3787121 54#include "commands.h"
bbf5cf35 55#include "commands_utils.h"
f3787121 56#include "conf.h"
d38dd64a 57#include "config.h"
74c6e2b0 58#include "confile_utils.h"
e2bcd7db 59#include "error.h"
7fbb15ec 60#include "file_utils.h"
1cc8bd4b 61#include "list.h"
f3787121 62#include "log.h"
d38dd64a 63#include "lsm/lsm.h"
f2d5a09d 64#include "lxccontainer.h"
f3787121
CB
65#include "lxclock.h"
66#include "lxcseccomp.h"
3ef9b3d3 67#include "macro.h"
f3787121 68#include "mainloop.h"
4ffeaf27 69#include "memory_utils.h"
63376d7d 70#include "monitor.h"
f549edcc 71#include "namespace.h"
811ef482 72#include "network.h"
38e5c2db 73#include "raw_syscalls.h"
f3787121 74#include "start.h"
6be5397b
CB
75#include "storage/storage.h"
76#include "storage/storage_utils.h"
0ed9b1bc 77#include "sync.h"
59524108 78#include "syscall_wrappers.h"
0ed9b1bc
CB
79#include "terminal.h"
80#include "utils.h"
36eb9bde 81
d38dd64a
CB
82#if HAVE_LIBCAP
83#include <sys/capability.h>
84#endif
85
9de31d5a
CB
86#ifndef HAVE_STRLCPY
87#include "include/strlcpy.h"
88#endif
89
ac2cecc4 90lxc_log_define(start, lxc);
36eb9bde 91
f01f7975 92extern void mod_all_rdeps(struct lxc_container *c, bool inc);
790255cf 93static bool do_destroy_container(struct lxc_handler *handler);
28272964
CB
94static int lxc_rmdir_onedev_wrapper(void *data);
95static void lxc_destroy_container_on_signal(struct lxc_handler *handler,
96 const char *name);
97
c8154066
SH
98static void print_top_failing_dir(const char *path)
99{
dcd9a847 100 __do_free char *copy = NULL;
9e5f5f2f 101 int ret;
4ffeaf27 102 char *e, *p, saved;
cbb9c7c7 103
4ffeaf27 104 copy = must_copy_string(path);
c8154066 105 p = copy;
4ffeaf27 106 e = copy + strlen(path);
cbb9c7c7 107
c8154066 108 while (p < e) {
f3787121
CB
109 while (p < e && *p == '/')
110 p++;
9e5f5f2f 111
f3787121
CB
112 while (p < e && *p != '/')
113 p++;
9e5f5f2f 114
c8154066
SH
115 saved = *p;
116 *p = '\0';
9e5f5f2f
CB
117
118 ret = access(copy, X_OK);
119 if (ret != 0) {
408da065
CB
120 SYSERROR("Could not access %s. Please grant it x "
121 "access, or add an ACL for the container "
9e5f5f2f 122 "root", copy);
c8154066
SH
123 return;
124 }
125 *p = saved;
126 }
127}
128
5032bf39 129static void lxc_put_nsfds(struct lxc_handler *handler)
408da065 130{
9f30a190
MM
131 int i;
132
9f30a190 133 for (i = 0; i < LXC_NS_MAX; i++) {
5032bf39 134 if (handler->nsfd[i] < 0)
39cd919c
CB
135 continue;
136
5032bf39
CB
137 close(handler->nsfd[i]);
138 handler->nsfd[i] = -EBADF;
9f30a190 139 }
9f30a190
MM
140}
141
4cb53844
CB
142static int lxc_try_preserve_ns(const int pid, const char *ns)
143{
144 int fd;
145
146 fd = lxc_preserve_ns(pid, ns);
147 if (fd < 0) {
148 if (errno != ENOENT) {
149 SYSERROR("Failed to preserve %s namespace", ns);
150 return -EINVAL;
151 }
152
a24c5678 153 SYSWARN("Kernel does not support preserving %s namespaces", ns);
4cb53844
CB
154 return -EOPNOTSUPP;
155 }
156
157 return fd;
158}
159
160/* lxc_try_preserve_namespaces: open /proc/@pid/ns/@ns for each namespace
161 * specified in ns_clone_flags.
4d8ac866 162 * Return true on success, false on failure.
62d05d9b 163 */
4cb53844
CB
164static bool lxc_try_preserve_namespaces(struct lxc_handler *handler,
165 int ns_clone_flags, pid_t pid)
f3787121 166{
9fef3355 167 int i;
9f30a190 168
9f30a190 169 for (i = 0; i < LXC_NS_MAX; i++)
5032bf39 170 handler->nsfd[i] = -EBADF;
cd43d2d1 171
9f30a190 172 for (i = 0; i < LXC_NS_MAX; i++) {
4cb53844
CB
173 int fd;
174
becad0ec 175 if ((ns_clone_flags & ns_info[i].clone_flag) == 0)
9f30a190 176 continue;
28d9e29e 177
4cb53844
CB
178 fd = lxc_try_preserve_ns(pid, ns_info[i].proc_name);
179 if (fd < 0) {
4cb53844
CB
180 /* Do not fail to start container on kernels that do
181 * not support interacting with namespaces through
182 * /proc.
183 */
184 if (fd == -EOPNOTSUPP)
185 continue;
fa3a5b22 186
4cb53844
CB
187 lxc_put_nsfds(handler);
188 return false;
189 }
190
191 handler->nsfd[i] = fd;
192 DEBUG("Preserved %s namespace via fd %d", ns_info[i].proc_name,
193 handler->nsfd[i]);
9f30a190
MM
194 }
195
62d05d9b 196 return true;
9f30a190
MM
197}
198
ec1dc633 199static inline bool match_stdfds(int fd)
80090207 200{
ec1dc633 201 return (fd == STDIN_FILENO || fd == STDOUT_FILENO || fd == STDERR_FILENO);
80090207
CLG
202}
203
4c03724e 204#ifdef HAVE_DLOG
205static bool match_dlog_fds(struct dirent *direntp)
206{
207 char path[PATH_MAX] = {0};
208 char link[PATH_MAX] = {0};
209 ssize_t linklen;
210 int ret;
211
212 ret = snprintf(path, PATH_MAX, "/proc/self/fd/%s", direntp->d_name);
213 if (ret < 0 || ret >= PATH_MAX) {
214 ERROR("Failed to create file descriptor name");
215 return false;
216 }
217
218 linklen = readlink(path, link, PATH_MAX);
219 if (linklen < 0) {
220 SYSERROR("Failed to read link path - \"%s\"", path);
221 return false;
222 } else if (linklen >= PATH_MAX) {
223 ERROR("The name of link path is too long - \"%s\"", path);
224 return false;
225 }
226
227 if (strcmp(link, "/dev/log_main") == 0 ||
228 strcmp(link, "/dev/log_system") == 0 ||
229 strcmp(link, "/dev/log_radio") == 0)
230 return true;
231
232 return false;
233}
234#endif
235
47a46cf1
CB
236int lxc_check_inherited(struct lxc_conf *conf, bool closeall,
237 int *fds_to_ignore, size_t len_fds)
80090207 238{
80090207 239 int fd, fddir;
47a46cf1 240 size_t i;
80090207 241 DIR *dir;
a5a70219 242 struct dirent *direntp;
80090207 243
d2cf4c37
SH
244 if (conf && conf->close_all_fds)
245 closeall = true;
246
b119f362 247restart:
80090207
CLG
248 dir = opendir("/proc/self/fd");
249 if (!dir) {
a24c5678 250 SYSWARN("Failed to open directory");
80090207
CLG
251 return -1;
252 }
253
254 fddir = dirfd(dir);
255
74f96976 256 while ((direntp = readdir(dir))) {
a5a70219 257 int ret;
d39b10eb
CB
258 struct lxc_list *cur;
259 bool matched = false;
260
a5a70219 261 if (strcmp(direntp->d_name, ".") == 0)
80090207
CLG
262 continue;
263
a5a70219 264 if (strcmp(direntp->d_name, "..") == 0)
80090207
CLG
265 continue;
266
a5a70219
CB
267 ret = lxc_safe_int(direntp->d_name, &fd);
268 if (ret < 0) {
269 INFO("Could not parse file descriptor for \"%s\"", direntp->d_name);
d4cff0d2
CB
270 continue;
271 }
80090207 272
47a46cf1
CB
273 for (i = 0; i < len_fds; i++)
274 if (fds_to_ignore[i] == fd)
275 break;
276
277 if (fd == fddir || fd == lxc_log_fd ||
278 (i < len_fds && fd == fds_to_ignore[i]))
80090207
CLG
279 continue;
280
d39b10eb 281 /* Keep state clients that wait on reboots. */
3ee9e4fb
CB
282 if (conf) {
283 lxc_list_for_each(cur, &conf->state_clients) {
284 struct lxc_state_client *client = cur->elem;
d39b10eb 285
3ee9e4fb
CB
286 if (client->clientfd != fd)
287 continue;
d39b10eb 288
3ee9e4fb
CB
289 matched = true;
290 break;
291 }
d39b10eb
CB
292 }
293
294 if (matched)
295 continue;
296
858377e4
SH
297 if (current_config && fd == current_config->logfd)
298 continue;
299
ec1dc633 300 if (match_stdfds(fd))
80090207 301 continue;
80090207 302
1d5e5f26 303#ifdef HAVE_DLOG
304 if (match_dlog_fds(direntp))
305 continue;
306
307#endif
d2cf4c37 308 if (closeall) {
b119f362
SH
309 close(fd);
310 closedir(dir);
28d9e29e 311 INFO("Closed inherited fd %d", fd);
b119f362
SH
312 goto restart;
313 }
28d9e29e 314 WARN("Inherited fd %d", fd);
80090207
CLG
315 }
316
408da065 317 /* Only enable syslog at this point to avoid the above logging function
64c57ea1
BD
318 * to open a new fd and make the check_inherited function enter an
319 * infinite loop.
320 */
321 lxc_log_enable_syslog();
322
92c7f629
GK
323 closedir(dir); /* cannot fail */
324 return 0;
80090207
CLG
325}
326
83ee7875 327static int setup_signal_fd(sigset_t *oldmask)
b0a33c1e 328{
7288dfb6 329 int ret;
b0a33c1e 330 sigset_t mask;
7288dfb6 331 const int signals[] = {SIGBUS, SIGILL, SIGSEGV, SIGWINCH};
b0a33c1e 332
408da065 333 /* Block everything except serious error signals. */
df0795b1
CB
334 ret = sigfillset(&mask);
335 if (ret < 0)
336 return -EBADF;
337
9b10bef5 338 for (int sig = 0; sig < (sizeof(signals) / sizeof(signals[0])); sig++) {
df0795b1
CB
339 ret = sigdelset(&mask, signals[sig]);
340 if (ret < 0)
341 return -EBADF;
b0a33c1e 342 }
343
b467714b 344 ret = pthread_sigmask(SIG_BLOCK, &mask, oldmask);
df0795b1
CB
345 if (ret < 0) {
346 SYSERROR("Failed to set signal mask");
347 return -EBADF;
b0a33c1e 348 }
349
df0795b1
CB
350 ret = signalfd(-1, &mask, SFD_CLOEXEC);
351 if (ret < 0) {
352 SYSERROR("Failed to create signal file descriptor");
353 return -EBADF;
b0a33c1e 354 }
355
df0795b1 356 TRACE("Created signal file descriptor %d", ret);
1ac470c0 357
df0795b1 358 return ret;
b0a33c1e 359}
360
84c92abd 361static int signal_handler(int fd, uint32_t events, void *data,
f3787121 362 struct lxc_epoll_descr *descr)
b0a33c1e 363{
15cd25fd 364 int ret;
3c319edb
CB
365 siginfo_t info;
366 struct signalfd_siginfo siginfo;
367 struct lxc_handler *hdlr = data;
15cd25fd 368
489f39be 369 ret = lxc_read_nointr(fd, &siginfo, sizeof(siginfo));
15cd25fd 370 if (ret < 0) {
6e94162a 371 ERROR("Failed to read signal info from signal file descriptor %d", fd);
b2a48508 372 return LXC_MAINLOOP_ERROR;
15cd25fd
DL
373 }
374
375 if (ret != sizeof(siginfo)) {
6e94162a
CB
376 ERROR("Unexpected size for struct signalfd_siginfo");
377 return -EINVAL;
15cd25fd
DL
378 }
379
408da065 380 /* Check whether init is running. */
80507ee8 381 info.si_pid = 0;
3c319edb
CB
382 ret = waitid(P_PID, hdlr->pid, &info, WEXITED | WNOWAIT | WNOHANG);
383 if (ret == 0 && info.si_pid == hdlr->pid)
384 hdlr->init_died = true;
80507ee8 385
cd5177e9
TA
386 /* Try to figure out a reasonable exit status to report. */
387 if (hdlr->init_died) {
388 switch (info.si_code) {
389 case CLD_EXITED:
390 hdlr->exit_status = info.si_status << 8;
391 break;
392 case CLD_KILLED:
393 case CLD_DUMPED:
394 case CLD_STOPPED:
395 hdlr->exit_status = info.si_status << 8 | 0x7f;
396 break;
397 case CLD_CONTINUED:
398 /* Huh? The waitid() told us it's dead *and* continued? */
399 WARN("Init %d dead and continued?", hdlr->pid);
400 hdlr->exit_status = 1;
401 break;
402 default:
20993a97 403 ERROR("Unknown si_code: %d", info.si_code);
55e1311e 404 hdlr->exit_status = 1;
cd5177e9
TA
405 }
406 }
407
d4b5d7a8 408 if (siginfo.ssi_signo == SIGHUP) {
33942046
CB
409 if (hdlr->pidfd >= 0)
410 lxc_raw_pidfd_send_signal(hdlr->pidfd, SIGTERM, NULL, 0);
411 else if (hdlr->proc_pidfd >= 0)
d9bb2fba
CB
412 lxc_raw_pidfd_send_signal(hdlr->proc_pidfd, SIGTERM, NULL, 0);
413 else
414 kill(hdlr->pid, SIGTERM);
d4b5d7a8 415 INFO("Killing %d since terminal hung up", hdlr->pid);
9b10bef5
CB
416 return hdlr->init_died ? LXC_MAINLOOP_CLOSE
417 : LXC_MAINLOOP_CONTINUE;
d4b5d7a8
TA
418 }
419
9cb94384 420 if (siginfo.ssi_signo != SIGCHLD) {
33942046
CB
421 if (hdlr->pidfd >= 0)
422 lxc_raw_pidfd_send_signal(hdlr->pidfd,
423 siginfo.ssi_signo, NULL, 0);
424 else if (hdlr->proc_pidfd >= 0)
d9bb2fba
CB
425 lxc_raw_pidfd_send_signal(hdlr->proc_pidfd,
426 siginfo.ssi_signo, NULL, 0);
427 else
428 kill(hdlr->pid, siginfo.ssi_signo);
9cb94384 429 INFO("Forwarded signal %d to pid %d", siginfo.ssi_signo, hdlr->pid);
9b10bef5
CB
430 return hdlr->init_died ? LXC_MAINLOOP_CLOSE
431 : LXC_MAINLOOP_CONTINUE;
9cb94384
TA
432 }
433
3a9e949f
TA
434 /* More robustness, protect ourself from a SIGCHLD sent
435 * by a process different from the container init.
436 */
437 if (siginfo.ssi_pid != hdlr->pid) {
6e94162a
CB
438 NOTICE("Received %d from pid %d instead of container init %d",
439 siginfo.ssi_signo, siginfo.ssi_pid, hdlr->pid);
9b10bef5
CB
440 return hdlr->init_died ? LXC_MAINLOOP_CLOSE
441 : LXC_MAINLOOP_CONTINUE;
3a9e949f
TA
442 }
443
408da065 444 if (siginfo.ssi_code == CLD_STOPPED) {
6e94162a 445 INFO("Container init process was stopped");
9b10bef5
CB
446 return hdlr->init_died ? LXC_MAINLOOP_CLOSE
447 : LXC_MAINLOOP_CONTINUE;
448 }
449
450 if (siginfo.ssi_code == CLD_CONTINUED) {
6e94162a 451 INFO("Container init process was continued");
9b10bef5
CB
452 return hdlr->init_died ? LXC_MAINLOOP_CLOSE
453 : LXC_MAINLOOP_CONTINUE;
15cd25fd 454 }
1ac470c0 455
6e94162a 456 DEBUG("Container init process %d exited", hdlr->pid);
9b10bef5 457
3c319edb 458 return LXC_MAINLOOP_CLOSE;
b0a33c1e 459}
460
974a8aba
CB
461int lxc_serve_state_clients(const char *name, struct lxc_handler *handler,
462 lxc_state_t state)
66aeffc7 463{
9de31d5a 464 size_t retlen;
dbc9832d
CB
465 ssize_t ret;
466 struct lxc_list *cur, *next;
dbc9832d
CB
467 struct lxc_msg msg = {.type = lxc_msg_state, .value = state};
468
974a8aba
CB
469 if (state == THAWED)
470 handler->state = RUNNING;
471 else
472 handler->state = state;
473
9dfa4041 474 TRACE("Set container state to %s", lxc_state2str(state));
dbc9832d 475
d39b10eb 476 if (lxc_list_empty(&handler->conf->state_clients)) {
9dfa4041 477 TRACE("No state clients registered");
dbc9832d
CB
478 return 0;
479 }
480
9de31d5a
CB
481 retlen = strlcpy(msg.name, name, sizeof(msg.name));
482 if (retlen >= sizeof(msg.name))
483 return -E2BIG;
dbc9832d 484
d39b10eb 485 lxc_list_for_each_safe(cur, &handler->conf->state_clients, next) {
d85617bc 486 struct lxc_state_client *client = cur->elem;
dbc9832d 487
d39b10eb 488 if (client->states[state] == 0) {
9dfa4041 489 TRACE("State %s not registered for state client %d",
dbc9832d
CB
490 lxc_state2str(state), client->clientfd);
491 continue;
492 }
493
9dfa4041 494 TRACE("Sending state %s to state client %d",
dbc9832d
CB
495 lxc_state2str(state), client->clientfd);
496
7fbb15ec
CB
497 ret = lxc_send_nointr(client->clientfd, &msg, sizeof(msg), MSG_NOSIGNAL);
498 if (ret <= 0)
6d1400b5 499 SYSERROR("Failed to send message to client");
dbc9832d
CB
500
501 /* kick client from list */
dbc9832d 502 lxc_list_del(cur);
300d1cb4 503 close(client->clientfd);
dbc9832d
CB
504 free(cur->elem);
505 free(cur);
506 }
dbc9832d 507
5e5576a4
CB
508 return 0;
509}
510
511static int lxc_serve_state_socket_pair(const char *name,
512 struct lxc_handler *handler,
513 lxc_state_t state)
514{
515 ssize_t ret;
516
bb955810 517 if (!handler->daemonize ||
5e5576a4
CB
518 handler->state_socket_pair[1] < 0 ||
519 state == STARTING)
520 return 0;
521
522 /* Close read end of the socket pair. */
523 close(handler->state_socket_pair[0]);
524 handler->state_socket_pair[0] = -1;
525
ee8377bd 526again:
5e5576a4
CB
527 ret = lxc_abstract_unix_send_credential(handler->state_socket_pair[1],
528 &(int){state}, sizeof(int));
9044b79e 529 if (ret < 0) {
530 SYSERROR("Failed to send state to %d", handler->state_socket_pair[1]);
531
ee8377bd
CB
532 if (errno == EINTR)
533 goto again;
9044b79e 534
535 return -1;
536 }
537
538 if (ret != sizeof(int)) {
539 ERROR("Message too long : %d", handler->state_socket_pair[1]);
ee8377bd
CB
540 return -1;
541 }
5e5576a4
CB
542
543 TRACE("Sent container state \"%s\" to %d", lxc_state2str(state),
544 handler->state_socket_pair[1]);
545
546 /* Close write end of the socket pair. */
547 close(handler->state_socket_pair[1]);
548 handler->state_socket_pair[1] = -1;
549
550 return 0;
551}
552
553int lxc_set_state(const char *name, struct lxc_handler *handler,
554 lxc_state_t state)
555{
556 int ret;
557
558 ret = lxc_serve_state_socket_pair(name, handler, state);
559 if (ret < 0) {
560 ERROR("Failed to synchronize via anonymous pair of unix sockets");
561 return -1;
562 }
563
564 ret = lxc_serve_state_clients(name, handler, state);
565 if (ret < 0)
566 return -1;
567
dbc9832d
CB
568 /* This function will try to connect to the legacy lxc-monitord state
569 * server and only exists for backwards compatibility.
570 */
9123e471 571 lxc_monitor_send_state(name, state, handler->lxcpath);
dbc9832d 572
66aeffc7
DL
573 return 0;
574}
575
735f2c6e 576int lxc_poll(const char *name, struct lxc_handler *handler)
b0a33c1e 577{
3c319edb 578 int ret;
86530b0a 579 bool has_console = true;
3c319edb 580 struct lxc_epoll_descr descr, descr_console;
b0a33c1e 581
03760215
CB
582 if (handler->conf->console.path &&
583 strcmp(handler->conf->console.path, "none") == 0)
86530b0a
L
584 has_console = false;
585
3c319edb
CB
586 ret = lxc_mainloop_open(&descr);
587 if (ret < 0) {
588 ERROR("Failed to create mainloop");
50c8bf05 589 goto out_sigfd;
b0a33c1e 590 }
591
30a33fbd
CB
592 if (has_console) {
593 ret = lxc_mainloop_open(&descr_console);
594 if (ret < 0) {
595 ERROR("Failed to create console mainloop");
596 goto out_mainloop;
597 }
3c319edb
CB
598 }
599
1cc8bd4b 600 ret = lxc_mainloop_add_handler(&descr, handler->sigfd, signal_handler, handler);
3c319edb 601 if (ret < 0) {
1cc8bd4b 602 ERROR("Failed to add signal handler for %d to mainloop", handler->sigfd);
3c319edb 603 goto out_mainloop_console;
b0a33c1e 604 }
605
2ac0f627
CB
606 ret = lxc_seccomp_setup_proxy(&handler->conf->seccomp, &descr, handler);
607 if (ret < 0) {
608 ERROR("Failed to setup seccomp proxy");
c3e3c21a 609 goto out_mainloop_console;
2ac0f627 610 }
cdb2a47f 611
30a33fbd 612 if (has_console) {
dcad02f8 613 struct lxc_terminal *console = &handler->conf->console;
63376d7d 614
093bce5f 615 ret = lxc_terminal_mainloop_add(&descr, console);
30a33fbd
CB
616 if (ret < 0) {
617 ERROR("Failed to add console handlers to mainloop");
618 goto out_mainloop_console;
619 }
620
093bce5f 621 ret = lxc_terminal_mainloop_add(&descr_console, console);
30a33fbd
CB
622 if (ret < 0) {
623 ERROR("Failed to add console handlers to console mainloop");
624 goto out_mainloop_console;
625 }
a54585ad
L
626
627 handler->conf->console.descr = &descr;
563f2f2c
DL
628 }
629
3c319edb
CB
630 ret = lxc_cmd_mainloop_add(name, &descr, handler);
631 if (ret < 0) {
632 ERROR("Failed to add command handler to mainloop");
633 goto out_mainloop_console;
634 }
b0a33c1e 635
3c319edb 636 TRACE("Mainloop is ready");
b0a33c1e 637
3c319edb 638 ret = lxc_mainloop(&descr, -1);
1cc8bd4b
CB
639 close(descr.epfd);
640 descr.epfd = -EBADF;
641 if (ret < 0 || !handler->init_died)
42b09f94 642 goto out_mainloop_console;
1cc8bd4b 643
30a33fbd
CB
644 if (has_console)
645 ret = lxc_mainloop(&descr_console, 0);
646
3c319edb 647out_mainloop_console:
30a33fbd
CB
648 if (has_console) {
649 lxc_mainloop_close(&descr_console);
650 TRACE("Closed console mainloop");
651 }
3c319edb 652
42b09f94
CB
653out_mainloop:
654 lxc_mainloop_close(&descr);
655 TRACE("Closed mainloop");
656
b0a33c1e 657out_sigfd:
1cc8bd4b
CB
658 close(handler->sigfd);
659 TRACE("Closed signal file descriptor %d", handler->sigfd);
660 handler->sigfd = -EBADF;
408da065 661
1cc8bd4b 662 return ret;
b0a33c1e 663}
664
41784e4e
CB
665void lxc_zero_handler(struct lxc_handler *handler)
666{
667 int i;
668
669 memset(handler, 0, sizeof(struct lxc_handler));
670
41784e4e
CB
671 handler->pinfd = -1;
672
33942046
CB
673 handler->pidfd = -EBADF;
674
d9bb2fba
CB
675 handler->proc_pidfd = -EBADF;
676
41784e4e
CB
677 handler->sigfd = -1;
678
679 for (i = 0; i < LXC_NS_MAX; i++)
680 handler->nsfd[i] = -1;
681
682 handler->data_sock[0] = -1;
683 handler->data_sock[1] = -1;
684
685 handler->state_socket_pair[0] = -1;
686 handler->state_socket_pair[1] = -1;
687
688 handler->sync_sock[0] = -1;
689 handler->sync_sock[1] = -1;
690}
691
f2e07cb6
CB
692void lxc_free_handler(struct lxc_handler *handler)
693{
1cc8bd4b
CB
694 if (handler->pinfd >= 0)
695 close(handler->pinfd);
696
33942046
CB
697 if (handler->pidfd >= 0)
698 close(handler->pidfd);
699
d9bb2fba
CB
700 if (handler->proc_pidfd >= 0)
701 close(handler->proc_pidfd);
702
1cc8bd4b
CB
703 if (handler->sigfd >= 0)
704 close(handler->sigfd);
705
706 lxc_put_nsfds(handler);
707
80308d07 708 if (handler->conf && handler->conf->reboot == REBOOT_NONE)
0d0d3655 709 if (handler->conf->maincmd_fd >= 0)
9044b79e 710 lxc_abstract_unix_close(handler->conf->maincmd_fd);
f2e07cb6 711
4d8bdfa0
CB
712 if (handler->monitor_status_fd >= 0)
713 close(handler->monitor_status_fd);
714
5e5576a4
CB
715 if (handler->state_socket_pair[0] >= 0)
716 close(handler->state_socket_pair[0]);
717
718 if (handler->state_socket_pair[1] >= 0)
719 close(handler->state_socket_pair[1]);
720
9b10bef5
CB
721 if (handler->cgroup_ops)
722 cgroup_exit(handler->cgroup_ops);
723
f2e07cb6
CB
724 handler->conf = NULL;
725 free(handler);
1cc8bd4b 726 handler = NULL;
f2e07cb6
CB
727}
728
aa460476 729struct lxc_handler *lxc_init_handler(const char *name, struct lxc_conf *conf,
5e5576a4 730 const char *lxcpath, bool daemonize)
59eb99ba 731{
5e5576a4 732 int i, ret;
3a0f472d
DL
733 struct lxc_handler *handler;
734
735 handler = malloc(sizeof(*handler));
fdecbc9c 736 if (!handler)
3a0f472d 737 return NULL;
9044b79e 738
59eb99ba
DL
739 memset(handler, 0, sizeof(*handler));
740
fdecbc9c
CB
741 /* Note that am_guest_unpriv() checks the effective uid. We
742 * probably don't care if we are real root only if we are running
743 * as root so this should be fine.
790255cf 744 */
e0010464 745 handler->am_root = !am_guest_unpriv();
c6012571 746 handler->data_sock[0] = handler->data_sock[1] = -1;
fae349da 747 handler->conf = conf;
9123e471 748 handler->lxcpath = lxcpath;
4d8bdfa0 749 handler->monitor_status_fd = -EBADF;
5c068da9 750 handler->pinfd = -1;
33942046 751 handler->pidfd = -EBADF;
d9bb2fba 752 handler->proc_pidfd = -EBADF;
1cc8bd4b
CB
753 handler->sigfd = -EBADF;
754 handler->init_died = false;
5e5576a4 755 handler->state_socket_pair[0] = handler->state_socket_pair[1] = -1;
80308d07 756 if (handler->conf->reboot == REBOOT_NONE)
d39b10eb 757 lxc_list_init(&handler->conf->state_clients);
fae349da 758
b6b2b194
WB
759 for (i = 0; i < LXC_NS_MAX; i++)
760 handler->nsfd[i] = -1;
761
f0ecc19d 762 handler->name = name;
3bdf52d7 763
80308d07 764 if (daemonize && handler->conf->reboot == REBOOT_NONE) {
5e5576a4 765 /* Create socketpair() to synchronize on daemonized startup.
fdecbc9c
CB
766 * When the container reboots we don't need to synchronize
767 * again currently so don't open another socketpair().
5e5576a4
CB
768 */
769 ret = socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0,
770 handler->state_socket_pair);
771 if (ret < 0) {
772 ERROR("Failed to create anonymous pair of unix sockets");
773 goto on_error;
774 }
9044b79e 775
5e5576a4
CB
776 TRACE("Created anonymous pair {%d,%d} of unix sockets",
777 handler->state_socket_pair[0],
778 handler->state_socket_pair[1]);
779 }
780
80308d07 781 if (handler->conf->reboot == REBOOT_NONE) {
d39b10eb
CB
782 handler->conf->maincmd_fd = lxc_cmd_init(name, lxcpath, "command");
783 if (handler->conf->maincmd_fd < 0) {
784 ERROR("Failed to set up command socket");
785 goto on_error;
786 }
aa460476 787 }
9044b79e 788
9dfa4041 789 TRACE("Unix domain socket %d for command server is ready",
aa460476
CB
790 handler->conf->maincmd_fd);
791
792 return handler;
793
f2e07cb6
CB
794on_error:
795 lxc_free_handler(handler);
aa460476
CB
796
797 return NULL;
798}
799
800int lxc_init(const char *name, struct lxc_handler *handler)
801{
4d8bdfa0 802 __do_close_prot_errno int status_fd = -EBADF;
a2c09be0 803 int ret;
4a03ded4 804 const char *loglevel;
aa460476
CB
805 struct lxc_conf *conf = handler->conf;
806
434c8e15 807 handler->monitor_pid = lxc_raw_getpid();
4d8bdfa0
CB
808 status_fd = open("/proc/self/status", O_RDONLY | O_CLOEXEC);
809 if (status_fd < 0) {
810 SYSERROR("Failed to open monitor status fd");
811 goto out_close_maincmd_fd;
812 }
434c8e15 813
aa460476 814 lsm_init();
2170b263 815 TRACE("Initialized LSM");
d2e30e99 816
2170b263
CB
817 ret = lxc_read_seccomp_config(conf);
818 if (ret < 0) {
819 ERROR("Failed loading seccomp policy");
d2e30e99 820 goto out_close_maincmd_fd;
8f2c3a70 821 }
2170b263 822 TRACE("Read seccomp policy");
8f2c3a70 823
408da065 824 /* Begin by setting the state to STARTING. */
2170b263
CB
825 ret = lxc_set_state(name, handler, STARTING);
826 if (ret < 0) {
827 ERROR("Failed to set state to \"%s\"", lxc_state2str(STARTING));
051151de 828 goto out_close_maincmd_fd;
0ad19a3f 829 }
46800e77 830 TRACE("Set container state to \"STARTING\"");
0ad19a3f 831
408da065 832 /* Start of environment variable setup for hooks. */
24c2a48d
CB
833 ret = setenv("LXC_NAME", name, 1);
834 if (ret < 0)
835 SYSERROR("Failed to set environment variable: LXC_NAME=%s", name);
408da065 836
2170b263
CB
837 if (conf->rcfile) {
838 ret = setenv("LXC_CONFIG_FILE", conf->rcfile, 1);
839 if (ret < 0)
840 SYSERROR("Failed to set environment variable: "
841 "LXC_CONFIG_FILE=%s", conf->rcfile);
842 }
408da065 843
2170b263
CB
844 if (conf->rootfs.mount) {
845 ret = setenv("LXC_ROOTFS_MOUNT", conf->rootfs.mount, 1);
846 if (ret < 0)
847 SYSERROR("Failed to set environment variable: "
848 "LXC_ROOTFS_MOUNT=%s", conf->rootfs.mount);
849 }
408da065 850
2170b263
CB
851 if (conf->rootfs.path) {
852 ret = setenv("LXC_ROOTFS_PATH", conf->rootfs.path, 1);
853 if (ret < 0)
854 SYSERROR("Failed to set environment variable: "
855 "LXC_ROOTFS_PATH=%s", conf->rootfs.path);
856 }
408da065 857
2170b263
CB
858 if (conf->console.path) {
859 ret = setenv("LXC_CONSOLE", conf->console.path, 1);
860 if (ret < 0)
861 SYSERROR("Failed to set environment variable: "
862 "LXC_CONSOLE=%s", conf->console.path);
863 }
408da065 864
2170b263
CB
865 if (conf->console.log_path) {
866 ret = setenv("LXC_CONSOLE_LOGPATH", conf->console.log_path, 1);
867 if (ret < 0)
868 SYSERROR("Failed to set environment variable: "
869 "LXC_CONSOLE_LOGPATH=%s", conf->console.log_path);
870 }
408da065 871
2170b263
CB
872 if (cgns_supported()) {
873 ret = setenv("LXC_CGNS_AWARE", "1", 1);
874 if (ret < 0)
875 SYSERROR("Failed to set environment variable "
876 "LXC_CGNS_AWARE=1");
877 }
b8f88d9b 878
4a03ded4 879 loglevel = lxc_log_priority_to_string(lxc_log_get_level());
2170b263
CB
880 ret = setenv("LXC_LOG_LEVEL", loglevel, 1);
881 if (ret < 0)
882 SYSERROR("Set environment variable LXC_LOG_LEVEL=%s",
883 loglevel);
a2c09be0
CB
884
885 if (conf->hooks_version == 0)
886 ret = setenv("LXC_HOOK_VERSION", "0", 1);
887 else
888 ret = setenv("LXC_HOOK_VERSION", "1", 1);
889 if (ret < 0)
890 SYSERROR("Failed to set environment variable LXC_HOOK_VERSION=%u", conf->hooks_version);
408da065 891 /* End of environment variable setup for hooks. */
f7bee6c6 892
2170b263 893 TRACE("Set environment variables");
dbc9832d 894
2170b263
CB
895 ret = run_lxc_hooks(name, "pre-start", conf, NULL);
896 if (ret < 0) {
897 ERROR("Failed to run lxc.hook.pre-start for container \"%s\"", name);
773fb9ca
SH
898 goto out_aborting;
899 }
2170b263 900 TRACE("Ran pre-start hooks");
26ddeedd 901
408da065
CB
902 /* The signal fd has to be created before forking otherwise if the child
903 * process exits before we setup the signal fd, the event will be lost
904 * and the command will be stuck.
905 */
83ee7875 906 handler->sigfd = setup_signal_fd(&handler->oldmask);
59eb99ba 907 if (handler->sigfd < 0) {
408da065 908 ERROR("Failed to setup SIGCHLD fd handler.");
b5159817
DE
909 goto out_delete_tty;
910 }
2170b263 911 TRACE("Set up signal fd");
b5159817 912
408da065 913 /* Do this after setting up signals since it might unblock SIGWINCH. */
564e31c4 914 ret = lxc_terminal_setup(conf);
4d1ffb0a
CB
915 if (ret < 0) {
916 ERROR("Failed to create console");
b5159817 917 goto out_restore_sigmask;
b0a33c1e 918 }
4d1ffb0a 919 TRACE("Created console");
b0a33c1e 920
2f835b4b 921 ret = lxc_terminal_map_ids(conf, &conf->console);
7cfeddd7 922 if (ret < 0) {
2170b263 923 ERROR("Failed to chown console");
5b74eb3c 924 goto out_delete_terminal;
c4d10a05 925 }
2170b263 926 TRACE("Chowned console");
c4d10a05 927
5a087e05 928 handler->cgroup_ops = cgroup_init(handler->conf);
2202afc9
CB
929 if (!handler->cgroup_ops) {
930 ERROR("Failed to initialize cgroup driver");
5b74eb3c 931 goto out_delete_terminal;
2202afc9
CB
932 }
933 TRACE("Initialized cgroup driver");
934
1800f924
WB
935 ret = lsm_process_prepare(conf, handler->lxcpath);
936 if (ret < 0) {
937 ERROR("Failed to initialize LSM");
938 goto out_destroy_cgroups;
939 }
940 TRACE("Initialized LSM");
941
2170b263 942 INFO("Container \"%s\" is initialized", name);
4d8bdfa0 943 handler->monitor_status_fd = move_fd(status_fd);
aa460476 944 return 0;
59eb99ba 945
1800f924 946out_destroy_cgroups:
434c8e15
CB
947 handler->cgroup_ops->payload_destroy(handler->cgroup_ops, handler);
948 handler->cgroup_ops->monitor_destroy(handler->cgroup_ops, handler);
1800f924 949
5b74eb3c
CB
950out_delete_terminal:
951 lxc_terminal_delete(&handler->conf->console);
ea918412 952
b5159817 953out_restore_sigmask:
4c496daa 954 (void)pthread_sigmask(SIG_SETMASK, &handler->oldmask, NULL);
ea918412 955
59eb99ba 956out_delete_tty:
0e4be3cf 957 lxc_delete_tty(&conf->ttys);
ea918412 958
59eb99ba 959out_aborting:
4c496daa 960 (void)lxc_set_state(name, handler, ABORTING);
ea918412 961
d2e30e99 962out_close_maincmd_fd:
9044b79e 963 lxc_abstract_unix_close(conf->maincmd_fd);
d2e30e99 964 conf->maincmd_fd = -1;
aa460476 965 return -1;
59eb99ba
DL
966}
967
3b72c4a0 968void lxc_fini(const char *name, struct lxc_handler *handler)
59eb99ba 969{
c097467f 970 int i, ret;
18b3b9c1 971 pid_t self;
dbc9832d 972 struct lxc_list *cur, *next;
dbc9832d 973 char *namespaces[LXC_NS_MAX + 1];
b3286b62 974 size_t namespace_count = 0;
2202afc9 975 struct cgroup_ops *cgroup_ops = handler->cgroup_ops;
b6b2b194 976
408da065
CB
977 /* The STOPPING state is there for future cleanup code which can take
978 * awhile.
59eb99ba 979 */
25c2aca5 980 lxc_set_state(name, handler, STOPPING);
b6b2b194 981
0059379f 982 self = lxc_raw_getpid();
b3286b62 983 for (i = 0; i < LXC_NS_MAX; i++) {
18b3b9c1
CB
984 if (handler->nsfd[i] < 0)
985 continue;
986
987 if (handler->conf->hooks_version == 0)
c097467f 988 ret = asprintf(&namespaces[namespace_count],
18b3b9c1
CB
989 "%s:/proc/%d/fd/%d", ns_info[i].proc_name,
990 self, handler->nsfd[i]);
991 else
c097467f 992 ret = asprintf(&namespaces[namespace_count],
18b3b9c1 993 "/proc/%d/fd/%d", self, handler->nsfd[i]);
c097467f
CB
994 if (ret == -1) {
995 SYSERROR("Failed to allocate memory");
18b3b9c1 996 break;
b3286b62 997 }
18b3b9c1
CB
998
999 if (handler->conf->hooks_version == 0) {
1000 namespace_count++;
1001 continue;
1002 }
1003
c097467f
CB
1004 ret = setenv(ns_info[i].env_name, namespaces[namespace_count], 1);
1005 if (ret < 0)
18b3b9c1
CB
1006 SYSERROR("Failed to set environment variable %s=%s",
1007 ns_info[i].env_name, namespaces[namespace_count]);
1008 else
1009 TRACE("Set environment variable %s=%s",
1010 ns_info[i].env_name, namespaces[namespace_count]);
1011
1012 namespace_count++;
b3286b62
WB
1013 }
1014 namespaces[namespace_count] = NULL;
c154af98 1015
80308d07 1016 if (handler->conf->reboot > REBOOT_NONE) {
c097467f
CB
1017 ret = setenv("LXC_TARGET", "reboot", 1);
1018 if (ret < 0)
1019 SYSERROR("Failed to set environment variable: "
1020 "LXC_TARGET=reboot");
1021 }
408da065 1022
80308d07 1023 if (handler->conf->reboot == REBOOT_NONE) {
c097467f
CB
1024 ret = setenv("LXC_TARGET", "stop", 1);
1025 if (ret < 0)
1026 SYSERROR("Failed to set environment variable: "
1027 "LXC_TARGET=stop");
1028 }
c154af98 1029
18b3b9c1 1030 if (handler->conf->hooks_version == 0)
c097467f 1031 ret = run_lxc_hooks(name, "stop", handler->conf, namespaces);
18b3b9c1 1032 else
c097467f
CB
1033 ret = run_lxc_hooks(name, "stop", handler->conf, NULL);
1034 if (ret < 0)
1035 ERROR("Failed to run \"lxc.hook.stop\" hook");
c154af98 1036
b3286b62
WB
1037 while (namespace_count--)
1038 free(namespaces[namespace_count]);
28d9e29e 1039
1800f924
WB
1040 lsm_process_cleanup(handler->conf, handler->lxcpath);
1041
434c8e15
CB
1042 cgroup_ops->payload_destroy(cgroup_ops, handler);
1043 cgroup_ops->monitor_destroy(cgroup_ops, handler);
4288b79f 1044
80308d07 1045 if (handler->conf->reboot == REBOOT_NONE) {
f893d898
CB
1046 /* For all new state clients simply close the command socket.
1047 * This will inform all state clients that the container is
1048 * STOPPED and also prevents a race between a open()/close() on
1049 * the command socket causing a new process to get ECONNREFUSED
1050 * because we haven't yet closed the command socket.
1051 */
9044b79e 1052 lxc_abstract_unix_close(handler->conf->maincmd_fd);
d39b10eb 1053 handler->conf->maincmd_fd = -1;
c3184275
CB
1054 TRACE("Closed command socket");
1055
1056 /* This function will try to connect to the legacy lxc-monitord
1057 * state server and only exists for backwards compatibility.
1058 */
1059 lxc_monitor_send_state(name, STOPPED, handler->lxcpath);
1060
1061 /* The command socket is closed so no one can acces the command
1062 * socket anymore so there's no need to lock it.
1063 */
1064 handler->state = STOPPED;
1065 TRACE("Set container state to \"STOPPED\"");
f893d898
CB
1066 } else {
1067 lxc_set_state(name, handler, STOPPED);
d39b10eb 1068 }
4288b79f 1069
048493a3
CB
1070 /* Avoid lingering namespace references. */
1071 lxc_put_nsfds(handler);
1072
46800e77
CB
1073 ret = run_lxc_hooks(name, "post-stop", handler->conf, NULL);
1074 if (ret < 0) {
c097467f 1075 ERROR("Failed to run lxc.hook.post-stop for container \"%s\"", name);
80308d07 1076 if (handler->conf->reboot > REBOOT_NONE) {
c097467f 1077 WARN("Container will be stopped instead of rebooted");
80308d07 1078 handler->conf->reboot = REBOOT_NONE;
c097467f
CB
1079
1080 ret = setenv("LXC_TARGET", "stop", 1);
1081 if (ret < 0)
1082 WARN("Failed to set environment variable: "
1083 "LXC_TARGET=stop");
f3787121
CB
1084 }
1085 }
26ddeedd 1086
408da065 1087 /* Reset mask set by setup_signal_fd. */
b467714b 1088 ret = pthread_sigmask(SIG_SETMASK, &handler->oldmask, NULL);
c097467f 1089 if (ret < 0)
a24c5678 1090 SYSWARN("Failed to restore signal mask");
8f64a3f6 1091
2aac071b 1092 lxc_terminal_delete(&handler->conf->console);
0e4be3cf 1093 lxc_delete_tty(&handler->conf->ttys);
dbc9832d 1094
dbc9832d
CB
1095 /* The command socket is now closed, no more state clients can register
1096 * themselves from now on. So free the list of state clients.
1097 */
d39b10eb
CB
1098 lxc_list_for_each_safe(cur, &handler->conf->state_clients, next) {
1099 struct lxc_state_client *client = cur->elem;
1100
1101 /* Keep state clients that want to be notified about reboots. */
80308d07
CB
1102 if ((handler->conf->reboot > REBOOT_NONE) &&
1103 (client->states[RUNNING] == 2))
d39b10eb
CB
1104 continue;
1105
dbc9832d 1106 /* close state client socket */
dbc9832d 1107 lxc_list_del(cur);
c097467f 1108 close(client->clientfd);
dbc9832d
CB
1109 free(cur->elem);
1110 free(cur);
1111 }
1112
80308d07 1113 if (handler->conf->ephemeral == 1 && handler->conf->reboot != REBOOT_REQ)
28272964 1114 lxc_destroy_container_on_signal(handler, name);
2c5f2ede 1115
1cc8bd4b 1116 lxc_free_handler(handler);
59eb99ba
DL
1117}
1118
735f2c6e 1119void lxc_abort(const char *name, struct lxc_handler *handler)
59eb99ba 1120{
33942046
CB
1121 int ret = 0;
1122 int status;
73e608b2 1123
25c2aca5 1124 lxc_set_state(name, handler, ABORTING);
0e4f9d51 1125
d1687497 1126 if (handler->pidfd >= 0)
33942046 1127 ret = lxc_raw_pidfd_send_signal(handler->pidfd, SIGKILL, NULL, 0);
d1687497 1128 else if (handler->proc_pidfd >= 0)
d9bb2fba 1129 ret = lxc_raw_pidfd_send_signal(handler->proc_pidfd, SIGKILL, NULL, 0);
33942046
CB
1130 else if (handler->pid > 0)
1131 ret = kill(handler->pid, SIGKILL);
1132 if (ret < 0)
1133 SYSERROR("Failed to send SIGKILL to %d", handler->pid);
0e4f9d51 1134
33942046
CB
1135 do {
1136 ret = waitpid(-1, &status, 0);
1137 } while (ret > 0);
59eb99ba
DL
1138}
1139
ffe1e01a 1140static int do_start(void *data)
50e98013 1141{
cdb2a47f 1142 struct lxc_handler *handler = data;
eab649dc
CB
1143 ATTR_UNUSED __do_close_prot_errno int data_sock0 = handler->data_sock[0],
1144 data_sock1 = handler->data_sock[1];
4d8bdfa0 1145 __do_close_prot_errno int status_fd = -EBADF;
8bf3abfb 1146 int ret;
928b1f04
YT
1147 uid_t new_uid;
1148 gid_t new_gid;
e96a536c 1149 struct lxc_list *iterator;
964581c2
CB
1150 uid_t nsuid = 0;
1151 gid_t nsgid = 0;
5af9369b 1152 int devnull_fd = -1;
50e98013 1153
18225d19 1154 lxc_sync_fini_parent(handler);
50e98013 1155
4d8bdfa0
CB
1156 if (lxc_abstract_unix_recv_fds(handler->data_sock[1], &status_fd, 1, NULL, 0) < 0) {
1157 ERROR("Failed to receive status file descriptor to child process");
1158 goto out_warn_father;
1159 }
1160
408da065
CB
1161 /* This prctl must be before the synchro, so if the parent dies before
1162 * we set the parent death signal, we will detect its death with the
1163 * synchro right after, otherwise we have a window where the parent can
1164 * exit before we set the pdeath signal leading to a unsupervized
1165 * container.
743ecd2e 1166 */
4d8bdfa0 1167 ret = lxc_set_death_signal(SIGKILL, handler->monitor_pid, status_fd);
912314fc
CB
1168 if (ret < 0) {
1169 SYSERROR("Failed to set PR_SET_PDEATHSIG to SIGKILL");
18225d19 1170 goto out_warn_father;
743ecd2e
DL
1171 }
1172
611ddd34
CB
1173 ret = lxc_ambient_caps_up();
1174 if (ret < 0) {
6f5e532f 1175 ERROR("Failed to raise ambient capabilities");
611ddd34
CB
1176 goto out_warn_father;
1177 }
1178
b467714b 1179 ret = pthread_sigmask(SIG_SETMASK, &handler->oldmask, NULL);
18225d19
CB
1180 if (ret < 0) {
1181 SYSERROR("Failed to set signal mask");
1182 goto out_warn_father;
1183 }
50e98013 1184
408da065 1185 /* Don't leak the pinfd to the container. */
d39b10eb 1186 if (handler->pinfd >= 0)
0d03360a 1187 close(handler->pinfd);
2b0e17e4 1188
18225d19
CB
1189 ret = lxc_sync_wait_parent(handler, LXC_SYNC_STARTUP);
1190 if (ret < 0)
1191 goto out_warn_father;
5b1e83cb 1192
408da065
CB
1193 /* Unshare CLONE_NEWNET after CLONE_NEWUSER. See
1194 * https://github.com/lxc/lxd/issues/1978.
1195 */
becad0ec 1196 if ((handler->ns_clone_flags & (CLONE_NEWNET | CLONE_NEWUSER)) ==
408da065 1197 (CLONE_NEWNET | CLONE_NEWUSER)) {
5b1e83cb
SH
1198 ret = unshare(CLONE_NEWNET);
1199 if (ret < 0) {
e96a536c 1200 SYSERROR("Failed to unshare CLONE_NEWNET");
5b1e83cb
SH
1201 goto out_warn_father;
1202 }
e96a536c 1203 INFO("Unshared CLONE_NEWNET");
5b1e83cb
SH
1204 }
1205
408da065
CB
1206 /* Tell the parent task it can begin to configure the container and wait
1207 * for it to finish.
3c22086f 1208 */
e96a536c
CB
1209 ret = lxc_sync_barrier_parent(handler, LXC_SYNC_CONFIGURE);
1210 if (ret < 0)
611ddd34 1211 goto out_error;
50e98013 1212
e389f2af 1213 if (handler->ns_clone_flags & CLONE_NEWNET) {
3c09b97c 1214 ret = lxc_network_recv_from_parent(handler);
e389f2af
CB
1215 if (ret < 0) {
1216 ERROR("Failed to receive veth names from parent");
1217 goto out_warn_father;
1218 }
7ab1ba02 1219 }
658979c5 1220
408da065 1221 /* If we are in a new user namespace, become root there to have
d08f8d2f 1222 * privilege over our namespace.
f6d3e3e4
SH
1223 */
1224 if (!lxc_list_empty(&handler->conf->id_map)) {
964581c2
CB
1225 if (!handler->conf->root_nsuid_map)
1226 nsuid = handler->conf->init_uid;
1227
1228 if (!handler->conf->root_nsgid_map)
1229 nsgid = handler->conf->init_gid;
4160c3a0 1230
464c4611 1231 if (!lxc_switch_uid_gid(nsuid, nsgid))
f6d3e3e4 1232 goto out_warn_father;
d08f8d2f
CB
1233
1234 /* Drop groups only after we switched to a valid gid in the new
1235 * user namespace.
1236 */
8af07f82
CB
1237 if (!lxc_setgroups(0, NULL) &&
1238 (handler->am_root || errno != EPERM))
c476bdce 1239 goto out_warn_father;
4b826b1f 1240
b81689a1
CB
1241 ret = prctl(PR_SET_DUMPABLE, prctl_arg(1), prctl_arg(0),
1242 prctl_arg(0), prctl_arg(0));
d7883725
CB
1243 if (ret < 0)
1244 goto out_warn_father;
912314fc
CB
1245
1246 /* set{g,u}id() clears deathsignal */
4d8bdfa0 1247 ret = lxc_set_death_signal(SIGKILL, handler->monitor_pid, status_fd);
912314fc
CB
1248 if (ret < 0) {
1249 SYSERROR("Failed to set PR_SET_PDEATHSIG to SIGKILL");
1250 goto out_warn_father;
1251 }
f6d3e3e4
SH
1252 }
1253
e96a536c
CB
1254 ret = access(handler->lxcpath, X_OK);
1255 if (ret != 0) {
c8154066
SH
1256 print_top_failing_dir(handler->lxcpath);
1257 goto out_warn_father;
1258 }
1259
7a55c157
TA
1260 /* In order to checkpoint restore, we need to have everything in the
1261 * same mount namespace. However, some containers may not have a
1262 * reasonable /dev (in particular, they may not have /dev/null), so we
1263 * can't set init's std fds to /dev/null by opening it from inside the
1264 * container.
1265 *
1266 * If that's the case, fall back to using the host's /dev/null. This
1267 * means that migration won't work, but at least we won't spew output
1268 * where it isn't wanted.
1269 */
bb955810 1270 if (handler->daemonize && !handler->conf->autodev) {
2806a87d 1271 char path[PATH_MAX];
3c09b97c 1272
2806a87d
RK
1273 ret = snprintf(path, sizeof(path), "%s/dev/null",
1274 handler->conf->rootfs.mount);
1275 if (ret < 0 || ret >= sizeof(path))
1276 goto out_warn_father;
3c09b97c 1277
e96a536c
CB
1278 ret = access(path, F_OK);
1279 if (ret != 0) {
1280 devnull_fd = open_devnull();
c44de748 1281
e96a536c
CB
1282 if (devnull_fd < 0)
1283 goto out_warn_father;
1284 WARN("Using /dev/null from the host for container "
1285 "init's standard file descriptors. Migration will "
1286 "not work");
1287 }
c44de748
AM
1288 }
1289
408da065 1290 /* Ask father to setup cgroups and wait for him to finish. */
e96a536c
CB
1291 ret = lxc_sync_barrier_parent(handler, LXC_SYNC_CGROUP);
1292 if (ret < 0)
c44de748 1293 goto out_error;
544a48a0 1294
deefdf8a
CB
1295 /* Unshare cgroup namespace after we have setup our cgroups. If we do it
1296 * earlier we end up with a wrong view of /proc/self/cgroup. For
1297 * example, assume we unshare(CLONE_NEWCGROUP) first, and then create
1298 * the cgroup for the container, say /sys/fs/cgroup/cpuset/lxc/c, then
1299 * /proc/self/cgroup would show us:
1300 *
1301 * 8:cpuset:/lxc/c
1302 *
1303 * whereas it should actually show
1304 *
1305 * 8:cpuset:/
1306 */
becad0ec 1307 if (handler->ns_clone_flags & CLONE_NEWCGROUP) {
e96a536c
CB
1308 ret = unshare(CLONE_NEWCGROUP);
1309 if (ret < 0) {
bca7c59c
CB
1310 if (errno != EINVAL) {
1311 SYSERROR("Failed to unshare CLONE_NEWCGROUP");
1312 goto out_warn_father;
1313 }
1314
1315 handler->ns_clone_flags &= ~CLONE_NEWCGROUP;
1316 SYSINFO("Kernel does not support CLONE_NEWCGROUP");
1317 } else {
1318 INFO("Unshared CLONE_NEWCGROUP");
deefdf8a 1319 }
deefdf8a
CB
1320 }
1321
149857af
CB
1322 /* Add the requested environment variables to the current environment to
1323 * allow them to be used by the various hooks, such as the start hook
317494f1 1324 * below.
98ff08ca 1325 */
98ff08ca 1326 lxc_list_for_each(iterator, &handler->conf->environment) {
e96a536c
CB
1327 ret = putenv((char *)iterator->elem);
1328 if (ret < 0) {
1329 SYSERROR("Failed to set environment variable: %s",
1330 (char *)iterator->elem);
98ff08ca
CB
1331 goto out_warn_father;
1332 }
1333 }
1334
f4152036 1335 /* Setup the container, ip, names, utsname, ... */
3b988b33 1336 ret = lxc_setup(handler);
7729f8e5 1337 if (ret < 0) {
e96a536c 1338 ERROR("Failed to setup container \"%s\"", handler->name);
f4152036
CB
1339 goto out_warn_father;
1340 }
1341
408da065 1342 /* Set the label to change to when we exec(2) the container's init. */
1800f924 1343 ret = lsm_process_label_set(NULL, handler->conf, true);
e96a536c 1344 if (ret < 0)
e075f5d9 1345 goto out_warn_father;
5112cd70 1346
029cdff5 1347 /* Set PR_SET_NO_NEW_PRIVS after we changed the lsm label. If we do it
408da065
CB
1348 * before we aren't allowed anymore.
1349 */
029cdff5 1350 if (handler->conf->no_new_privs) {
b81689a1
CB
1351 ret = prctl(PR_SET_NO_NEW_PRIVS, prctl_arg(1), prctl_arg(0),
1352 prctl_arg(0), prctl_arg(0));
e96a536c
CB
1353 if (ret < 0) {
1354 SYSERROR("Could not set PR_SET_NO_NEW_PRIVS to block "
1355 "execve() gainable privileges");
029cdff5
CB
1356 goto out_warn_father;
1357 }
e96a536c
CB
1358 DEBUG("Set PR_SET_NO_NEW_PRIVS to block execve() gainable "
1359 "privileges");
029cdff5
CB
1360 }
1361
0d9acb99
DE
1362 /* Some init's such as busybox will set sane tty settings on stdin,
1363 * stdout, stderr which it thinks is the console. We already set them
1364 * the way we wanted on the real terminal, and we want init to do its
564e31c4 1365 * setup on its console ie. the pty allocated in lxc_terminal_setup() so
408da065 1366 * make sure that that pty is stdin,stdout,stderr.
0d9acb99 1367 */
5d113f65 1368 if (handler->conf->console.slave >= 0) {
bb955810 1369 if (handler->daemonize || !handler->conf->is_execute)
5d113f65
CB
1370 ret = set_stdfds(handler->conf->console.slave);
1371 else
ae6d3913 1372 ret = lxc_terminal_set_stdfds(handler->conf->console.slave);
5d113f65 1373 if (ret < 0) {
c5b93afb 1374 ERROR("Failed to redirect std{in,out,err} to pty file "
e96a536c 1375 "descriptor %d", handler->conf->console.slave);
c5b93afb
LF
1376 goto out_warn_father;
1377 }
5d113f65 1378 }
0d9acb99 1379
408da065 1380 /* If we mounted a temporary proc, then unmount it now. */
5112cd70 1381 tmp_proc_unmount(handler->conf);
e075f5d9 1382
e96a536c
CB
1383 ret = lxc_seccomp_load(handler->conf);
1384 if (ret < 0)
8f2c3a70
SH
1385 goto out_warn_father;
1386
c3e3c21a
CB
1387 ret = lxc_seccomp_send_notifier_fd(&handler->conf->seccomp, data_sock0);
1388 if (ret < 0) {
1389 SYSERROR("Failed to send seccomp notify fd to parent");
1390 goto out_warn_father;
cdb2a47f 1391 }
cdb2a47f 1392
e96a536c
CB
1393 ret = run_lxc_hooks(handler->name, "start", handler->conf, NULL);
1394 if (ret < 0) {
1395 ERROR("Failed to run lxc.hook.start for container \"%s\"",
1396 handler->name);
773fb9ca
SH
1397 goto out_warn_father;
1398 }
fc25b815 1399
773fb9ca 1400 close(handler->sigfd);
26ddeedd 1401
bb955810 1402 if (handler->conf->console.slave < 0 && handler->daemonize) {
f4c177c3
CB
1403 if (devnull_fd < 0) {
1404 devnull_fd = open_devnull();
1405 if (devnull_fd < 0)
1406 goto out_warn_father;
1407 }
1408
e96a536c
CB
1409 ret = set_stdfds(devnull_fd);
1410 if (ret < 0) {
1411 ERROR("Failed to redirect std{in,out,err} to \"/dev/null\"");
c5b93afb
LF
1412 goto out_warn_father;
1413 }
e96a536c 1414 }
507cee36 1415
c44de748
AM
1416 if (devnull_fd >= 0) {
1417 close(devnull_fd);
1418 devnull_fd = -1;
1419 }
1420
8c9a7665
TA
1421 setsid();
1422
e96a536c
CB
1423 if (handler->conf->init_cwd) {
1424 ret = chdir(handler->conf->init_cwd);
1425 if (ret < 0) {
1426 SYSERROR("Could not change directory to \"%s\"",
1427 handler->conf->init_cwd);
1428 goto out_warn_father;
1429 }
3c491553
L
1430 }
1431
e96a536c
CB
1432 ret = lxc_sync_barrier_parent(handler, LXC_SYNC_CGROUP_LIMITS);
1433 if (ret < 0)
f4152036
CB
1434 goto out_warn_father;
1435
149857af
CB
1436 /* Reset the environment variables the user requested in a clear
1437 * environment.
1438 */
e96a536c
CB
1439 ret = clearenv();
1440 /* Don't error out though. */
1441 if (ret < 0)
149857af 1442 SYSERROR("Failed to clear environment.");
149857af
CB
1443
1444 lxc_list_for_each(iterator, &handler->conf->environment) {
e96a536c
CB
1445 ret = putenv((char *)iterator->elem);
1446 if (ret < 0) {
1447 SYSERROR("Failed to set environment variable: %s",
1448 (char *)iterator->elem);
149857af
CB
1449 goto out_warn_father;
1450 }
1451 }
1452
e96a536c
CB
1453 ret = putenv("container=lxc");
1454 if (ret < 0) {
1455 SYSERROR("Failed to set environment variable: container=lxc");
98ff08ca
CB
1456 goto out_warn_father;
1457 }
1458
885766f5
CB
1459 if (handler->conf->ttys.tty_names) {
1460 ret = putenv(handler->conf->ttys.tty_names);
e96a536c
CB
1461 if (ret < 0) {
1462 SYSERROR("Failed to set environment variable for container ptys");
98ff08ca
CB
1463 goto out_warn_father;
1464 }
1465 }
1466
76bdf299
CB
1467 /* The container has been setup. We can now switch to an unprivileged
1468 * uid/gid.
1469 */
928b1f04
YT
1470 new_uid = handler->conf->init_uid;
1471 new_gid = handler->conf->init_gid;
76bdf299 1472
964581c2
CB
1473 /* Avoid unnecessary syscalls. */
1474 if (new_uid == nsuid)
1475 new_uid = LXC_INVALID_UID;
1476
1477 if (new_gid == nsgid)
1478 new_gid = LXC_INVALID_GID;
1479
464c4611 1480 if (!lxc_switch_uid_gid(new_uid, new_gid))
928b1f04
YT
1481 goto out_warn_father;
1482
8af07f82
CB
1483 /* If we are in a new user namespace we already dropped all groups when
1484 * we switched to root in the new user namespace further above. Only
1485 * drop groups if we can, so ensure that we have necessary privilege.
1486 */
1487 if (lxc_list_empty(&handler->conf->id_map))
1488 #if HAVE_LIBCAP
1489 if (lxc_proc_cap_is_set(CAP_SETGID, CAP_EFFECTIVE))
1490 #endif
1491 if (!lxc_setgroups(0, NULL))
1492 goto out_warn_father;
1493
611ddd34
CB
1494 ret = lxc_ambient_caps_down();
1495 if (ret < 0) {
6f5e532f 1496 ERROR("Failed to clear ambient capabilities");
611ddd34
CB
1497 goto out_warn_father;
1498 }
1499
258f8051 1500 if (handler->conf->monitor_signal_pdeath != SIGKILL) {
4d8bdfa0
CB
1501 ret = lxc_set_death_signal(handler->conf->monitor_signal_pdeath,
1502 handler->monitor_pid, status_fd);
258f8051
CB
1503 if (ret < 0) {
1504 SYSERROR("Failed to set PR_SET_PDEATHSIG to %d",
1505 handler->conf->monitor_signal_pdeath);
1506 goto out_warn_father;
1507 }
1508 }
1509
408da065
CB
1510 /* After this call, we are in error because this ops should not return
1511 * as it execs.
1512 */
c4ea60df 1513 handler->ops->start(handler, handler->data);
50e98013
DL
1514
1515out_warn_father:
408da065
CB
1516 /* We want the parent to know something went wrong, so we return a
1517 * special error code.
1518 */
d1ccb562 1519 lxc_sync_wake_parent(handler, LXC_SYNC_ERROR);
c44de748
AM
1520
1521out_error:
1522 if (devnull_fd >= 0)
1523 close(devnull_fd);
1524
50e98013
DL
1525 return -1;
1526}
1527
ae467c54 1528static int lxc_recv_ttys_from_child(struct lxc_handler *handler)
e8bd4e43 1529{
ae467c54 1530 int i;
2520facd 1531 struct lxc_terminal_info *tty;
ae467c54 1532 int ret = -1;
c6012571 1533 int sock = handler->data_sock[1];
e8bd4e43 1534 struct lxc_conf *conf = handler->conf;
0e4be3cf 1535 struct lxc_tty_info *ttys = &conf->ttys;
e8bd4e43 1536
885766f5 1537 if (!conf->ttys.max)
e8bd4e43
SH
1538 return 0;
1539
885766f5 1540 ttys->tty = malloc(sizeof(*ttys->tty) * ttys->max);
0e4be3cf 1541 if (!ttys->tty)
e8bd4e43 1542 return -1;
e8bd4e43 1543
885766f5 1544 for (i = 0; i < conf->ttys.max; i++) {
672c1e58
CB
1545 int ttyfds[2];
1546
1547 ret = lxc_abstract_unix_recv_fds(sock, ttyfds, 2, NULL, 0);
1548 if (ret < 0)
1549 break;
ae467c54 1550
0e4be3cf 1551 tty = &ttys->tty[i];
2520facd
CB
1552 tty->busy = 0;
1553 tty->master = ttyfds[0];
1554 tty->slave = ttyfds[1];
672c1e58 1555 TRACE("Received pty with master fd %d and slave fd %d from "
2520facd 1556 "parent", tty->master, tty->slave);
e8bd4e43 1557 }
6d1400b5 1558
ae467c54 1559 if (ret < 0)
6d1400b5 1560 SYSERROR("Failed to receive %zu ttys from child", ttys->max);
ae467c54 1561 else
885766f5 1562 TRACE("Received %zu ttys from child", ttys->max);
ae467c54
CB
1563
1564 return ret;
e8bd4e43
SH
1565}
1566
5af9369b 1567int resolve_clone_flags(struct lxc_handler *handler)
f813849c 1568{
8bc8c715
CB
1569 int i;
1570 struct lxc_conf *conf = handler->conf;
f813849c 1571
8bc8c715 1572 for (i = 0; i < LXC_NS_MAX; i++) {
f7a0c6ee
CB
1573 if (conf->ns_keep) {
1574 if (!(conf->ns_keep & ns_info[i].clone_flag))
becad0ec 1575 handler->ns_clone_flags |= ns_info[i].clone_flag;
f7a0c6ee
CB
1576 } else if (conf->ns_clone) {
1577 if ((conf->ns_clone & ns_info[i].clone_flag))
becad0ec 1578 handler->ns_clone_flags |= ns_info[i].clone_flag;
8bc8c715
CB
1579 } else {
1580 if (i == LXC_NS_USER && lxc_list_empty(&handler->conf->id_map))
1581 continue;
1582
1583 if (i == LXC_NS_NET && lxc_requests_empty_network(handler))
1584 continue;
1585
7bd05339
CB
1586 if (i == LXC_NS_CGROUP && !cgns_supported())
1587 continue;
1588
becad0ec 1589 handler->ns_clone_flags |= ns_info[i].clone_flag;
8bc8c715 1590 }
03df7ab5 1591
8bc8c715
CB
1592 if (!conf->ns_share[i])
1593 continue;
5af9369b 1594
becad0ec 1595 handler->ns_clone_flags &= ~ns_info[i].clone_flag;
8bc8c715 1596 TRACE("Sharing %s namespace", ns_info[i].proc_name);
5af9369b
CB
1597 }
1598
1599 return 0;
f813849c
TA
1600}
1601
8deca6c9
CB
1602/* Note that this function is used with clone(CLONE_VM). Some glibc versions
1603 * used to reset the pid/tid to -1 when CLONE_VM was used without CLONE_THREAD.
1604 * But since the memory between parent and child is shared on CLONE_VM this
1605 * would invalidate the getpid() cache that glibc used to maintain and so
1606 * getpid() in the child would return the parent's pid. This is all fixed in
1607 * newer glibc versions where the getpid() cache is removed and the pid/tid is
1608 * not reset anymore.
b35ddc5b 1609 * However, if for whatever reason you - dear committer - somehow need to get the
8deca6c9 1610 * pid of the dummy intermediate process for do_share_ns() you need to call
0059379f
CB
1611 * lxc_raw_getpid(). The next lxc_raw_clone() call does not employ CLONE_VM and
1612 * will be fine.
8deca6c9
CB
1613 */
1614static inline int do_share_ns(void *arg)
fa3a5b22 1615{
8deca6c9 1616 int i, flags, ret;
fa3a5b22
CB
1617 struct lxc_handler *handler = arg;
1618
8deca6c9
CB
1619 for (i = 0; i < LXC_NS_MAX; i++) {
1620 if (handler->nsfd[i] < 0)
1621 continue;
fa3a5b22 1622
8deca6c9 1623 ret = setns(handler->nsfd[i], 0);
c0b48eff
CB
1624 if (ret < 0) {
1625 /*
1626 * Note that joining a user and/or mount namespace
1627 * requires the process is not multithreaded otherwise
1628 * setns() will fail here.
1629 */
1630 SYSERROR("Failed to inherit %s namespace",
1631 ns_info[i].proc_name);
8deca6c9 1632 return -1;
c0b48eff 1633 }
fa3a5b22 1634
8deca6c9 1635 DEBUG("Inherited %s namespace", ns_info[i].proc_name);
fa3a5b22
CB
1636 }
1637
becad0ec 1638 flags = handler->ns_on_clone_flags;
8deca6c9 1639 flags |= CLONE_PARENT;
33942046
CB
1640 handler->pid = lxc_raw_clone_cb(do_start, handler, CLONE_PIDFD | flags,
1641 &handler->pidfd);
8deca6c9 1642 if (handler->pid < 0)
fa3a5b22
CB
1643 return -1;
1644
8deca6c9 1645 return 0;
fa3a5b22
CB
1646}
1647
d9bb2fba
CB
1648static int proc_pidfd_open(pid_t pid)
1649{
1650 __do_close_prot_errno int proc_pidfd = -EBADF;
1651 char path[100];
1652
1653 snprintf(path, sizeof(path), "/proc/%d", pid);
1654 proc_pidfd = open(path, O_DIRECTORY | O_RDONLY | O_CLOEXEC);
1655 if (proc_pidfd < 0) {
1656 SYSERROR("Failed to open %s", path);
1657 return -1;
1658 }
1659
1660 /* Test whether we can send signals. */
1661 if (lxc_raw_pidfd_send_signal(proc_pidfd, 0, NULL, 0)) {
9c579205
TA
1662 if (errno != ENOSYS)
1663 SYSERROR("Failed to send signal through pidfd");
1664 else
1665 INFO("Sending signals through pidfds not supported on this kernel");
d9bb2fba
CB
1666 return -1;
1667 }
1668
1669 return move_fd(proc_pidfd);
1670}
1671
480588e6
CB
1672/* lxc_spawn() performs crucial setup tasks and clone()s the new process which
1673 * exec()s the requested container binary.
1674 * Note that lxc_spawn() runs in the parent namespaces. Any operations performed
1675 * right here should be double checked if they'd pose a security risk. (For
1676 * example, any {u}mount() operations performed here will be reflected on the
1677 * host!)
1678 */
74a3920a 1679static int lxc_spawn(struct lxc_handler *handler)
59eb99ba 1680{
cdb2a47f 1681 __do_close_prot_errno int data_sock0 = -EBADF, data_sock1 = -EBADF;
8deca6c9 1682 int i, ret;
08dd2805 1683 char pidstr[20];
57927bf2
CB
1684 bool wants_to_map_ids;
1685 struct lxc_list *id_map;
6e5fc7a5 1686 const char *name = handler->name;
28d9e29e 1687 const char *lxcpath = handler->lxcpath;
2202afc9 1688 bool share_ns = false;
28d9e29e 1689 struct lxc_conf *conf = handler->conf;
2202afc9 1690 struct cgroup_ops *cgroup_ops = handler->cgroup_ops;
9f30a190 1691
28d9e29e 1692 id_map = &conf->id_map;
57927bf2 1693 wants_to_map_ids = !lxc_list_empty(id_map);
2f2623ec 1694
28d9e29e 1695 for (i = 0; i < LXC_NS_MAX; i++) {
b074bbf1 1696 if (!conf->ns_share[i])
28d9e29e
CB
1697 continue;
1698
b074bbf1 1699 handler->nsfd[i] = lxc_inherit_namespace(conf->ns_share[i], lxcpath, ns_info[i].proc_name);
28d9e29e
CB
1700 if (handler->nsfd[i] < 0)
1701 return -1;
fa3a5b22 1702
8deca6c9 1703 share_ns = true;
28d9e29e 1704 }
50e98013 1705
cfc62c60
CB
1706 ret = lxc_sync_init(handler);
1707 if (ret < 0)
9d7f9e52 1708 return -1;
0ad19a3f 1709
b9f522c5 1710 ret = socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0,
790255cf 1711 handler->data_sock);
95ef0d7c
CB
1712 if (ret < 0)
1713 goto out_sync_fini;
cdb2a47f
CB
1714 data_sock0 = handler->data_sock[0];
1715 data_sock1 = handler->data_sock[1];
e8bd4e43 1716
5af9369b 1717 ret = resolve_clone_flags(handler);
95ef0d7c
CB
1718 if (ret < 0)
1719 goto out_sync_fini;
9f30a190 1720
03ca4af8
TP
1721 if (handler->ns_clone_flags & CLONE_NEWNET) {
1722 ret = lxc_find_gateway_addresses(handler);
1723 if (ret) {
1724 ERROR("Failed to find gateway addresses");
1725 goto out_sync_fini;
1726 }
1727 }
1728
e8b181f5 1729 if (!cgroup_ops->payload_create(cgroup_ops, handler)) {
cfc62c60 1730 ERROR("Failed creating cgroups");
47d8fb3b
CS
1731 goto out_delete_net;
1732 }
1733
408da065
CB
1734 /* If the rootfs is not a blockdev, prevent the container from marking
1735 * it readonly.
1736 * If the container is unprivileged then skip rootfs pinning.
0c547523 1737 */
0ee35059 1738 if (!wants_to_map_ids) {
28d9e29e 1739 handler->pinfd = pin_rootfs(conf->rootfs.path);
5e32a990 1740 if (handler->pinfd == -1)
cfc62c60 1741 INFO("Failed to pin the rootfs for container \"%s\"", handler->name);
5e32a990 1742 }
0c547523 1743
408da065 1744 /* Create a process in a new set of namespaces. */
becad0ec
CB
1745 handler->ns_on_clone_flags = handler->ns_clone_flags;
1746 if (handler->ns_clone_flags & CLONE_NEWUSER) {
408da065
CB
1747 /* If CLONE_NEWUSER and CLONE_NEWNET was requested, we need to
1748 * clone a new user namespace first and only later unshare our
1749 * network namespace to ensure that network devices ownership is
1750 * set up correctly.
1751 */
becad0ec 1752 handler->ns_on_clone_flags &= ~CLONE_NEWNET;
408da065 1753 }
5af9369b 1754 /* The cgroup namespace gets unshare()ed not clone()ed. */
becad0ec 1755 handler->ns_on_clone_flags &= ~CLONE_NEWCGROUP;
732375f5 1756
4e232466
CB
1757 if (share_ns) {
1758 pid_t attacher_pid;
1759
cfc62c60 1760 attacher_pid = lxc_clone(do_share_ns, handler,
33258b95 1761 CLONE_VFORK | CLONE_VM | CLONE_FILES, NULL);
4e232466
CB
1762 if (attacher_pid < 0) {
1763 SYSERROR(LXC_CLONE_ERROR);
1764 goto out_delete_net;
1765 }
1766
1767 ret = wait_for_pid(attacher_pid);
1768 if (ret < 0) {
1769 SYSERROR("Intermediate process failed");
1770 goto out_delete_net;
1771 }
1772 } else {
cfc62c60 1773 handler->pid = lxc_raw_clone_cb(do_start, handler,
33942046
CB
1774 CLONE_PIDFD | handler->ns_on_clone_flags,
1775 &handler->pidfd);
4e232466
CB
1776 }
1777 if (handler->pid < 0) {
1778 SYSERROR(LXC_CLONE_ERROR);
7fef7a06 1779 goto out_delete_net;
0ad19a3f 1780 }
fa3a5b22 1781 TRACE("Cloned child process %d", handler->pid);
1bd8d726 1782
33942046
CB
1783 if (handler->pidfd < 0) {
1784 handler->proc_pidfd = proc_pidfd_open(handler->pid);
1785 if (handler->proc_pidfd < 0 && (errno != ENOSYS))
1786 goto out_delete_net;
1787 }
d9bb2fba 1788
1871e646
CB
1789 ret = snprintf(pidstr, 20, "%d", handler->pid);
1790 if (ret < 0 || ret >= 20)
1791 goto out_delete_net;
1792
1793 ret = setenv("LXC_PID", pidstr, 1);
1794 if (ret < 0)
1795 SYSERROR("Failed to set environment variable: LXC_PID=%s", pidstr);
1796
9662e444 1797 for (i = 0; i < LXC_NS_MAX; i++)
becad0ec 1798 if (handler->ns_on_clone_flags & ns_info[i].clone_flag)
28d9e29e 1799 INFO("Cloned %s", ns_info[i].flag_name);
0ad19a3f 1800
4cb53844 1801 if (!lxc_try_preserve_namespaces(handler, handler->ns_on_clone_flags, handler->pid)) {
28d9e29e
CB
1802 ERROR("Failed to preserve cloned namespaces for lxc.hook.stop");
1803 goto out_delete_net;
1804 }
b6b2b194 1805
3c22086f
CLG
1806 lxc_sync_fini_child(handler);
1807
4d8bdfa0
CB
1808 if (lxc_abstract_unix_send_fds(handler->data_sock[0], &handler->monitor_status_fd, 1, NULL, 0) < 0) {
1809 ERROR("Failed to send status file descriptor to child process");
1810 goto out_delete_net;
1811 }
1812 close_prot_errno_disarm(handler->monitor_status_fd);
1813
408da065
CB
1814 /* Map the container uids. The container became an invalid userid the
1815 * moment it was cloned with CLONE_NEWUSER. This call doesn't change
1816 * anything immediately, but allows the container to setuid(0) (0 being
1817 * mapped to something else on the host.) later to become a valid uid
1818 * again.
1819 */
fa3a5b22 1820 if (wants_to_map_ids) {
df3c5314 1821 if (!handler->conf->ns_share[LXC_NS_USER] &&
46186acd 1822 (handler->conf->ns_keep & CLONE_NEWUSER) == 0) {
fa3a5b22
CB
1823 ret = lxc_map_ids(id_map, handler->pid);
1824 if (ret < 0) {
1825 ERROR("Failed to set up id mapping.");
1826 goto out_delete_net;
1827 }
1828 }
5b1e83cb
SH
1829 }
1830
cfc62c60
CB
1831 ret = lxc_sync_wake_child(handler, LXC_SYNC_STARTUP);
1832 if (ret < 0)
5b1e83cb 1833 goto out_delete_net;
5b1e83cb 1834
cfc62c60
CB
1835 ret = lxc_sync_wait_child(handler, LXC_SYNC_CONFIGURE);
1836 if (ret < 0)
5b1e83cb 1837 goto out_delete_net;
0ad19a3f 1838
2202afc9 1839 if (!cgroup_ops->setup_limits(cgroup_ops, handler->conf, false)) {
cfc62c60 1840 ERROR("Failed to setup cgroup limits for container \"%s\"", name);
6031a6e5
DE
1841 goto out_delete_net;
1842 }
1843
e8b181f5 1844 if (!cgroup_ops->payload_enter(cgroup_ops, handler->pid))
7fef7a06 1845 goto out_delete_net;
218d4250 1846
2202afc9 1847 if (!cgroup_ops->chown(cgroup_ops, handler->conf))
0996e18a
SH
1848 goto out_delete_net;
1849
aa0c0e7b
RK
1850 /* If not done yet, we're now ready to preserve the network namespace */
1851 if (handler->nsfd[LXC_NS_NET] < 0) {
1852 ret = lxc_try_preserve_ns(handler->pid, "net");
1853 if (ret < 0) {
1854 if (ret != -EOPNOTSUPP) {
1855 SYSERROR("Failed to preserve net namespace");
1856 goto out_delete_net;
1857 }
1858 } else {
1859 handler->nsfd[LXC_NS_NET] = ret;
1860 DEBUG("Preserved net namespace via fd %d", ret);
4cb53844 1861 }
fa3a5b22 1862 }
aa0c0e7b
RK
1863 ret = lxc_netns_set_nsid(handler->nsfd[LXC_NS_NET]);
1864 if (ret < 0)
1865 SYSWARN("Failed to allocate new network namespace id");
1866 else
1867 TRACE("Allocated new network namespace id");
fa3a5b22 1868
408da065 1869 /* Create the network configuration. */
becad0ec 1870 if (handler->ns_clone_flags & CLONE_NEWNET) {
e389f2af 1871 ret = lxc_create_network(handler);
cfc62c60 1872 if (ret < 0) {
e389f2af 1873 ERROR("Failed to create the network");
7fef7a06 1874 goto out_delete_net;
82d5ae15 1875 }
74c6e2b0 1876
3c09b97c 1877 ret = lxc_network_send_to_child(handler);
cfc62c60 1878 if (ret < 0) {
e389f2af 1879 ERROR("Failed to send veth names to child");
74c6e2b0
CB
1880 goto out_delete_net;
1881 }
0ad19a3f 1882 }
1883
61d7a733
YT
1884 if (!lxc_list_empty(&conf->procs)) {
1885 ret = setup_proc_filesystem(&conf->procs, handler->pid);
1886 if (ret < 0)
1887 goto out_delete_net;
1888 }
1889
408da065
CB
1890 /* Tell the child to continue its initialization. We'll get
1891 * LXC_SYNC_CGROUP when it is ready for us to setup cgroups.
3c22086f 1892 */
cfc62c60
CB
1893 ret = lxc_sync_barrier_child(handler, LXC_SYNC_POST_CONFIGURE);
1894 if (ret < 0)
544a48a0
SH
1895 goto out_delete_net;
1896
cfc62c60
CB
1897 if (!lxc_list_empty(&conf->limits)) {
1898 ret = setup_resource_limits(&conf->limits, handler->pid);
1899 if (ret < 0) {
1900 ERROR("Failed to setup resource limits");
1901 goto out_delete_net;
1902 }
c6d09e15
WB
1903 }
1904
cfc62c60
CB
1905 ret = lxc_sync_barrier_child(handler, LXC_SYNC_CGROUP_UNSHARE);
1906 if (ret < 0)
f4152036
CB
1907 goto out_delete_net;
1908
2202afc9 1909 if (!cgroup_ops->setup_limits(cgroup_ops, handler->conf, true)) {
cfc62c60 1910 ERROR("Failed to setup legacy device cgroup controller limits");
b98f7d6e 1911 goto out_delete_net;
544a48a0 1912 }
cfc62c60 1913 TRACE("Set up legacy device cgroup controller limits");
544a48a0 1914
becad0ec 1915 if (handler->ns_clone_flags & CLONE_NEWCGROUP) {
8bf3abfb 1916 /* Now we're ready to preserve the cgroup namespace */
4cb53844 1917 ret = lxc_try_preserve_ns(handler->pid, "cgroup");
8bf3abfb 1918 if (ret < 0) {
4cb53844 1919 if (ret != -EOPNOTSUPP) {
6d1400b5 1920 SYSERROR("Failed to preserve cgroup namespace");
4cb53844
CB
1921 goto out_delete_net;
1922 }
1923 } else {
1924 handler->nsfd[LXC_NS_CGROUP] = ret;
1925 DEBUG("Preserved cgroup namespace via fd %d", ret);
8bf3abfb 1926 }
8bf3abfb
CB
1927 }
1928
08dd2805 1929 /* Run any host-side start hooks */
cfc62c60
CB
1930 ret = run_lxc_hooks(name, "start-host", conf, NULL);
1931 if (ret < 0) {
1932 ERROR("Failed to run lxc.hook.start-host");
341ed84c 1933 goto out_delete_net;
08dd2805
SH
1934 }
1935
408da065
CB
1936 /* Tell the child to complete its initialization and wait for it to exec
1937 * or return an error. (The child will never return
08dd2805 1938 * LXC_SYNC_READY_START+1. It will either close the sync pipe, causing
408da065
CB
1939 * lxc_sync_barrier_child to return success, or return a different
1940 * value, causing us to error out).
544a48a0 1941 */
cfc62c60
CB
1942 ret = lxc_sync_barrier_child(handler, LXC_SYNC_READY_START);
1943 if (ret < 0)
341ed84c 1944 goto out_delete_net;
0ad19a3f 1945
e389f2af
CB
1946 if (handler->ns_clone_flags & CLONE_NEWNET) {
1947 ret = lxc_network_recv_name_and_ifindex_from_child(handler);
1948 if (ret < 0) {
1949 ERROR("Failed to receive names and ifindices for network devices from child");
1950 goto out_delete_net;
1951 }
790255cf
CB
1952 }
1953
1954 /* Now all networks are created, network devices are moved into place,
ad2ddfcd 1955 * and the correct names and ifindices in the respective namespaces have
790255cf
CB
1956 * been recorded. The corresponding structs have now all been filled. So
1957 * log them for debugging purposes.
1958 */
28d9e29e 1959 lxc_log_configured_netdevs(conf);
790255cf 1960
f4152036 1961 /* Read tty fds allocated by child. */
cfc62c60
CB
1962 ret = lxc_recv_ttys_from_child(handler);
1963 if (ret < 0) {
1964 ERROR("Failed to receive tty info from child process");
f4152036
CB
1965 goto out_delete_net;
1966 }
1967
c3e3c21a
CB
1968 ret = lxc_seccomp_recv_notifier_fd(&handler->conf->seccomp, data_sock1);
1969 if (ret < 0) {
1970 SYSERROR("Failed to receive seccomp notify fd from child");
1971 goto out_delete_net;
cdb2a47f 1972 }
cdb2a47f 1973
cfc62c60
CB
1974 ret = handler->ops->post_start(handler, handler->data);
1975 if (ret < 0)
e6126dbe
MN
1976 goto out_abort;
1977
cfc62c60
CB
1978 ret = lxc_set_state(name, handler, RUNNING);
1979 if (ret < 0) {
1980 ERROR("Failed to set state to \"%s\"", lxc_state2str(RUNNING));
59eb99ba 1981 goto out_abort;
3f21c114 1982 }
22ebac19 1983
3c22086f 1984 lxc_sync_fini(handler);
0c547523 1985
e6126dbe 1986 return 0;
1ac470c0 1987
7fef7a06 1988out_delete_net:
becad0ec 1989 if (handler->ns_clone_flags & CLONE_NEWNET)
bb84beda 1990 lxc_delete_network(handler);
1bd8d726 1991
59eb99ba
DL
1992out_abort:
1993 lxc_abort(name, handler);
95ef0d7c
CB
1994
1995out_sync_fini:
3c22086f 1996 lxc_sync_fini(handler);
5c068da9
SH
1997 if (handler->pinfd >= 0) {
1998 close(handler->pinfd);
1999 handler->pinfd = -1;
2000 }
2001
b79fcd86 2002 return -1;
59eb99ba 2003}
0ad19a3f 2004
aa460476 2005int __lxc_start(const char *name, struct lxc_handler *handler,
507cee36 2006 struct lxc_operations* ops, void *data, const char *lxcpath,
bb955810 2007 bool daemonize, int *error_num)
59eb99ba 2008{
c30e9b19 2009 int ret, status;
aa460476 2010 struct lxc_conf *conf = handler->conf;
72068e74 2011 struct cgroup_ops *cgroup_ops;
80090207 2012
c30e9b19
CB
2013 ret = lxc_init(name, handler);
2014 if (ret < 0) {
2015 ERROR("Failed to initialize container \"%s\"", name);
39cb2d9e 2016 goto out_fini_nonet;
0ad19a3f 2017 }
ee70bf78
CLG
2018 handler->ops = ops;
2019 handler->data = data;
bb955810 2020 handler->daemonize = daemonize;
72068e74 2021 cgroup_ops = handler->cgroup_ops;
e6126dbe 2022
76a26f55 2023 if (!attach_block_device(handler->conf)) {
c30e9b19 2024 ERROR("Failed to attach block device");
575ea467 2025 ret = -1;
76a26f55
SH
2026 goto out_fini_nonet;
2027 }
2028
72068e74
CB
2029 if (!cgroup_ops->monitor_create(cgroup_ops, handler)) {
2030 ERROR("Failed to create monitor cgroup");
575ea467 2031 ret = -1;
eeef32bb
CB
2032 goto out_fini_nonet;
2033 }
2034
434c8e15 2035 if (!cgroup_ops->monitor_enter(cgroup_ops, handler->monitor_pid)) {
eeef32bb 2036 ERROR("Failed to enter monitor cgroup");
575ea467 2037 ret = -1;
72068e74
CB
2038 goto out_fini_nonet;
2039 }
2040
35120d9c 2041 if (geteuid() == 0 && !lxc_list_empty(&conf->id_map)) {
408da065 2042 /* If the backing store is a device, mount it here and now. */
35120d9c 2043 if (rootfs_is_blockdev(conf)) {
c30e9b19
CB
2044 ret = unshare(CLONE_NEWNS);
2045 if (ret < 0) {
2046 ERROR("Failed to unshare CLONE_NEWNS");
35120d9c
SH
2047 goto out_fini_nonet;
2048 }
c30e9b19 2049 INFO("Unshared CLONE_NEWNS");
408da065 2050
6a0c909a 2051 remount_all_slave();
8ce1abc2 2052 ret = lxc_setup_rootfs_prepare_root(conf, name, lxcpath);
c30e9b19
CB
2053 if (ret < 0) {
2054 ERROR("Error setting up rootfs mount as root before spawn");
35120d9c
SH
2055 goto out_fini_nonet;
2056 }
c30e9b19 2057 INFO("Set up container rootfs as host root");
35120d9c
SH
2058 }
2059 }
2060
c30e9b19
CB
2061 ret = lxc_spawn(handler);
2062 if (ret < 0) {
2063 ERROR("Failed to spawn container \"%s\"", name);
76a26f55 2064 goto out_detach_blockdev;
0ad19a3f 2065 }
2066
80308d07 2067 handler->conf->reboot = REBOOT_NONE;
8bee8851 2068
c30e9b19
CB
2069 ret = lxc_poll(name, handler);
2070 if (ret) {
2071 ERROR("LXC mainloop exited with error: %d", ret);
59eb99ba
DL
2072 goto out_abort;
2073 }
0ad19a3f 2074
321db026
DJ
2075 if (!handler->init_died && handler->pid > 0) {
2076 ERROR("Child process is not killed");
575ea467 2077 ret = -1;
321db026
DJ
2078 goto out_abort;
2079 }
2080
c30e9b19
CB
2081 status = lxc_wait_for_pid_status(handler->pid);
2082 if (status < 0)
2083 SYSERROR("Failed to retrieve status for %d", handler->pid);
e043236e 2084
408da065
CB
2085 /* If the child process exited but was not signaled, it didn't call
2086 * reboot. This should mean it was an lxc-execute which simply exited.
2087 * In any case, treat it as a 'halt'.
8b004f07 2088 */
d028235d 2089 if (WIFSIGNALED(status)) {
8b004f07
SH
2090 switch(WTERMSIG(status)) {
2091 case SIGINT: /* halt */
c30e9b19 2092 DEBUG("Container \"%s\" is halting", name);
8b004f07
SH
2093 break;
2094 case SIGHUP: /* reboot */
c30e9b19 2095 DEBUG("Container \"%s\" is rebooting", name);
80308d07 2096 handler->conf->reboot = REBOOT_REQ;
8b004f07 2097 break;
c2b9bd9e 2098 case SIGSYS: /* seccomp */
c30e9b19 2099 DEBUG("Container \"%s\" violated its seccomp policy", name);
c2b9bd9e 2100 break;
8b004f07 2101 default:
c30e9b19 2102 DEBUG("Unknown exit status for container \"%s\" init %d", name, WTERMSIG(status));
8b004f07
SH
2103 break;
2104 }
d028235d 2105 }
828695d9 2106
c30e9b19
CB
2107 ret = lxc_restore_phys_nics_to_netns(handler);
2108 if (ret < 0)
b809f232
CB
2109 ERROR("Failed to move physical network devices back to parent "
2110 "network namespace");
ce5782df 2111
5c068da9
SH
2112 if (handler->pinfd >= 0) {
2113 close(handler->pinfd);
2114 handler->pinfd = -1;
2115 }
2116
1787abca 2117 lxc_monitor_send_exit_code(name, status, handler->lxcpath);
eb808539 2118 lxc_error_set_and_log(handler->pid, status);
a3b4f3d6
TA
2119 if (error_num)
2120 *error_num = handler->exit_status;
1bd8d726 2121
9d7f9e52 2122out_fini:
bb84beda 2123 lxc_delete_network(handler);
74a2b586 2124
76a26f55
SH
2125out_detach_blockdev:
2126 detach_block_device(handler->conf);
2127
74a2b586 2128out_fini_nonet:
3a0f472d 2129 lxc_fini(name, handler);
c30e9b19 2130 return ret;
0ad19a3f 2131
59eb99ba 2132out_abort:
3a0f472d 2133 lxc_abort(name, handler);
9d7f9e52 2134 goto out_fini;
0ad19a3f 2135}
ee70bf78
CLG
2136
2137struct start_args {
2138 char *const *argv;
2139};
2140
2141static int start(struct lxc_handler *handler, void* data)
2142{
2143 struct start_args *arg = data;
2144
984984e4 2145 NOTICE("Exec'ing \"%s\"", arg->argv[0]);
ee70bf78
CLG
2146
2147 execvp(arg->argv[0], arg->argv);
984984e4 2148 SYSERROR("Failed to exec \"%s\"", arg->argv[0]);
ee70bf78
CLG
2149 return 0;
2150}
2151
2152static int post_start(struct lxc_handler *handler, void* data)
2153{
2154 struct start_args *arg = data;
2155
4c8e880e 2156 NOTICE("Started \"%s\" with pid \"%d\"", arg->argv[0], handler->pid);
ee70bf78
CLG
2157 return 0;
2158}
2159
2160static struct lxc_operations start_ops = {
2161 .start = start,
2162 .post_start = post_start
2163};
2164
aa460476 2165int lxc_start(const char *name, char *const argv[], struct lxc_handler *handler,
bb955810 2166 const char *lxcpath, bool daemonize, int *error_num)
ee70bf78
CLG
2167{
2168 struct start_args start_arg = {
2169 .argv = argv,
2170 };
2171
b2efeb0b 2172 TRACE("Doing lxc_start");
bb955810 2173 return __lxc_start(name, handler, &start_ops, &start_arg, lxcpath, daemonize, error_num);
ee70bf78 2174}
28272964
CB
2175
2176static void lxc_destroy_container_on_signal(struct lxc_handler *handler,
2177 const char *name)
2178{
245100a0 2179 char destroy[PATH_MAX];
f01f7975 2180 struct lxc_container *c;
ae3beac9
CB
2181 int ret = 0;
2182 bool bret = true;
2183
df31363a 2184 if (handler->conf->rootfs.path && handler->conf->rootfs.mount) {
790255cf 2185 bret = do_destroy_container(handler);
28272964 2186 if (!bret) {
ae3beac9 2187 ERROR("Error destroying rootfs for container \"%s\"", name);
28272964
CB
2188 return;
2189 }
2190 }
ae3beac9 2191 INFO("Destroyed rootfs for container \"%s\"", name);
28272964 2192
245100a0
CB
2193 ret = snprintf(destroy, PATH_MAX, "%s/%s", handler->lxcpath, name);
2194 if (ret < 0 || ret >= PATH_MAX) {
ae3beac9 2195 ERROR("Error destroying directory for container \"%s\"", name);
28272964
CB
2196 return;
2197 }
2198
f01f7975
CB
2199 c = lxc_container_new(name, handler->lxcpath);
2200 if (c) {
2201 if (container_disk_lock(c)) {
ae3beac9 2202 INFO("Could not update lxc_snapshots file");
f01f7975
CB
2203 lxc_container_put(c);
2204 } else {
2205 mod_all_rdeps(c, false);
2206 container_disk_unlock(c);
2207 lxc_container_put(c);
2208 }
2209 }
2210
d0fbc7ba 2211 if (!handler->am_root)
94b0a3ac
CB
2212 ret = userns_exec_full(handler->conf, lxc_rmdir_onedev_wrapper,
2213 destroy, "lxc_rmdir_onedev_wrapper");
28272964
CB
2214 else
2215 ret = lxc_rmdir_onedev(destroy, NULL);
2216
2217 if (ret < 0) {
ae3beac9 2218 ERROR("Error destroying directory for container \"%s\"", name);
28272964
CB
2219 return;
2220 }
ae3beac9 2221 INFO("Destroyed directory for container \"%s\"", name);
28272964
CB
2222}
2223
2224static int lxc_rmdir_onedev_wrapper(void *data)
2225{
2226 char *arg = (char *) data;
2227 return lxc_rmdir_onedev(arg, NULL);
2228}
2229
134df645
CB
2230static bool do_destroy_container(struct lxc_handler *handler)
2231{
94b0a3ac
CB
2232 int ret;
2233
d0fbc7ba 2234 if (!handler->am_root) {
94b0a3ac
CB
2235 ret = userns_exec_full(handler->conf, storage_destroy_wrapper,
2236 handler->conf, "storage_destroy_wrapper");
2237 if (ret < 0)
d028235d 2238 return false;
10bc1861 2239
d028235d
SG
2240 return true;
2241 }
10bc1861 2242
790255cf 2243 return storage_destroy(handler->conf);
28272964 2244}