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