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