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