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