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