]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/start.c
tree-wide: hide further unnecessary symbols
[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"
59eac805 33#include "compiler.h"
f3787121 34#include "conf.h"
d38dd64a 35#include "config.h"
74c6e2b0 36#include "confile_utils.h"
e2bcd7db 37#include "error.h"
7fbb15ec 38#include "file_utils.h"
1cc8bd4b 39#include "list.h"
f3787121 40#include "log.h"
d38dd64a 41#include "lsm/lsm.h"
f2d5a09d 42#include "lxccontainer.h"
f3787121
CB
43#include "lxclock.h"
44#include "lxcseccomp.h"
3ef9b3d3 45#include "macro.h"
f3787121 46#include "mainloop.h"
4ffeaf27 47#include "memory_utils.h"
63376d7d 48#include "monitor.h"
f549edcc 49#include "namespace.h"
811ef482 50#include "network.h"
f40988c7 51#include "process_utils.h"
f3787121 52#include "start.h"
6be5397b
CB
53#include "storage/storage.h"
54#include "storage/storage_utils.h"
0ed9b1bc 55#include "sync.h"
59524108 56#include "syscall_wrappers.h"
0ed9b1bc
CB
57#include "terminal.h"
58#include "utils.h"
36eb9bde 59
d38dd64a
CB
60#if HAVE_LIBCAP
61#include <sys/capability.h>
62#endif
63
9de31d5a
CB
64#ifndef HAVE_STRLCPY
65#include "include/strlcpy.h"
66#endif
67
ac2cecc4 68lxc_log_define(start, lxc);
36eb9bde 69
f01f7975 70extern void mod_all_rdeps(struct lxc_container *c, bool inc);
790255cf 71static bool do_destroy_container(struct lxc_handler *handler);
28272964
CB
72static int lxc_rmdir_onedev_wrapper(void *data);
73static void lxc_destroy_container_on_signal(struct lxc_handler *handler,
74 const char *name);
75
c8154066
SH
76static void print_top_failing_dir(const char *path)
77{
dcd9a847 78 __do_free char *copy = NULL;
9e5f5f2f 79 int ret;
4ffeaf27 80 char *e, *p, saved;
cbb9c7c7 81
4ffeaf27 82 copy = must_copy_string(path);
c8154066 83 p = copy;
4ffeaf27 84 e = copy + strlen(path);
cbb9c7c7 85
c8154066 86 while (p < e) {
f3787121
CB
87 while (p < e && *p == '/')
88 p++;
9e5f5f2f 89
f3787121
CB
90 while (p < e && *p != '/')
91 p++;
9e5f5f2f 92
c8154066
SH
93 saved = *p;
94 *p = '\0';
9e5f5f2f
CB
95
96 ret = access(copy, X_OK);
97 if (ret != 0) {
818a57fc 98 SYSERROR("Could not access %s. Please grant it x access, or add an ACL for the container " "root", copy);
c8154066
SH
99 return;
100 }
101 *p = saved;
102 }
103}
104
5032bf39 105static void lxc_put_nsfds(struct lxc_handler *handler)
408da065 106{
818a57fc 107 for (int i = 0; i < LXC_NS_MAX; i++) {
5032bf39 108 if (handler->nsfd[i] < 0)
39cd919c
CB
109 continue;
110
818a57fc 111 close_prot_errno_disarm(handler->nsfd[i]);
9f30a190 112 }
9f30a190
MM
113}
114
4cb53844
CB
115static int lxc_try_preserve_ns(const int pid, const char *ns)
116{
117 int fd;
118
119 fd = lxc_preserve_ns(pid, ns);
120 if (fd < 0) {
818a57fc
CB
121 if (errno != ENOENT)
122 return log_error_errno(-EINVAL,
123 errno, "Failed to preserve %s namespace",
124 ns);
4cb53844 125
b87ee312
CB
126 return log_warn_errno(-EOPNOTSUPP,
127 errno, "Kernel does not support preserving %s namespaces",
128 ns);
4cb53844
CB
129 }
130
131 return fd;
132}
133
134/* lxc_try_preserve_namespaces: open /proc/@pid/ns/@ns for each namespace
135 * specified in ns_clone_flags.
4d8ac866 136 * Return true on success, false on failure.
62d05d9b 137 */
4cb53844
CB
138static bool lxc_try_preserve_namespaces(struct lxc_handler *handler,
139 int ns_clone_flags, pid_t pid)
f3787121 140{
9fef3355 141 int i;
9f30a190 142
9f30a190 143 for (i = 0; i < LXC_NS_MAX; i++)
5032bf39 144 handler->nsfd[i] = -EBADF;
cd43d2d1 145
9f30a190 146 for (i = 0; i < LXC_NS_MAX; i++) {
4cb53844
CB
147 int fd;
148
becad0ec 149 if ((ns_clone_flags & ns_info[i].clone_flag) == 0)
9f30a190 150 continue;
28d9e29e 151
4cb53844
CB
152 fd = lxc_try_preserve_ns(pid, ns_info[i].proc_name);
153 if (fd < 0) {
4cb53844
CB
154 /* Do not fail to start container on kernels that do
155 * not support interacting with namespaces through
156 * /proc.
157 */
158 if (fd == -EOPNOTSUPP)
159 continue;
fa3a5b22 160
4cb53844
CB
161 lxc_put_nsfds(handler);
162 return false;
163 }
164
165 handler->nsfd[i] = fd;
166 DEBUG("Preserved %s namespace via fd %d", ns_info[i].proc_name,
167 handler->nsfd[i]);
9f30a190
MM
168 }
169
62d05d9b 170 return true;
9f30a190
MM
171}
172
ec1dc633 173static inline bool match_stdfds(int fd)
80090207 174{
ec1dc633 175 return (fd == STDIN_FILENO || fd == STDOUT_FILENO || fd == STDERR_FILENO);
80090207
CLG
176}
177
4c03724e 178#ifdef HAVE_DLOG
179static bool match_dlog_fds(struct dirent *direntp)
180{
181 char path[PATH_MAX] = {0};
182 char link[PATH_MAX] = {0};
183 ssize_t linklen;
184 int ret;
185
186 ret = snprintf(path, PATH_MAX, "/proc/self/fd/%s", direntp->d_name);
818a57fc
CB
187 if (ret < 0 || ret >= PATH_MAX)
188 return log_error(false, "Failed to create file descriptor name");
4c03724e 189
190 linklen = readlink(path, link, PATH_MAX);
818a57fc
CB
191 if (linklen < 0)
192 return log_error(false, "Failed to read link path - \"%s\"", path);
193 else if (linklen >= PATH_MAX)
194 return log_error(false, "The name of link path is too long - \"%s\"", path);
195
196 if (strcmp(link, "/dev/log_main") == 0 ||
197 strcmp(link, "/dev/log_system") == 0 ||
198 strcmp(link, "/dev/log_radio") == 0)
4c03724e 199 return true;
200
201 return false;
202}
203#endif
204
47a46cf1
CB
205int lxc_check_inherited(struct lxc_conf *conf, bool closeall,
206 int *fds_to_ignore, size_t len_fds)
80090207 207{
80090207 208 int fd, fddir;
47a46cf1 209 size_t i;
80090207 210 DIR *dir;
a5a70219 211 struct dirent *direntp;
80090207 212
d2cf4c37
SH
213 if (conf && conf->close_all_fds)
214 closeall = true;
215
4e2d6b9a
CB
216 /*
217 * Disable syslog at this point to avoid the above logging
218 * function to open a new fd and make the check_inherited function
219 * enter an infinite loop.
220 */
221 lxc_log_syslog_disable();
222
b119f362 223restart:
80090207 224 dir = opendir("/proc/self/fd");
818a57fc
CB
225 if (!dir)
226 return log_warn(-1, "Failed to open directory");
80090207
CLG
227
228 fddir = dirfd(dir);
229
74f96976 230 while ((direntp = readdir(dir))) {
a5a70219 231 int ret;
d39b10eb
CB
232 struct lxc_list *cur;
233 bool matched = false;
234
a5a70219 235 if (strcmp(direntp->d_name, ".") == 0)
80090207
CLG
236 continue;
237
a5a70219 238 if (strcmp(direntp->d_name, "..") == 0)
80090207
CLG
239 continue;
240
a5a70219
CB
241 ret = lxc_safe_int(direntp->d_name, &fd);
242 if (ret < 0) {
243 INFO("Could not parse file descriptor for \"%s\"", direntp->d_name);
d4cff0d2
CB
244 continue;
245 }
80090207 246
47a46cf1
CB
247 for (i = 0; i < len_fds; i++)
248 if (fds_to_ignore[i] == fd)
249 break;
250
251 if (fd == fddir || fd == lxc_log_fd ||
252 (i < len_fds && fd == fds_to_ignore[i]))
80090207
CLG
253 continue;
254
d39b10eb 255 /* Keep state clients that wait on reboots. */
3ee9e4fb
CB
256 if (conf) {
257 lxc_list_for_each(cur, &conf->state_clients) {
258 struct lxc_state_client *client = cur->elem;
d39b10eb 259
3ee9e4fb
CB
260 if (client->clientfd != fd)
261 continue;
d39b10eb 262
3ee9e4fb
CB
263 matched = true;
264 break;
265 }
d39b10eb
CB
266 }
267
268 if (matched)
269 continue;
270
858377e4
SH
271 if (current_config && fd == current_config->logfd)
272 continue;
273
ec1dc633 274 if (match_stdfds(fd))
80090207 275 continue;
80090207 276
1d5e5f26 277#ifdef HAVE_DLOG
278 if (match_dlog_fds(direntp))
279 continue;
280
281#endif
d2cf4c37 282 if (closeall) {
4e2d6b9a
CB
283 if (close(fd))
284 SYSINFO("Closed inherited fd %d", fd);
285 else
286 INFO("Closed inherited fd %d", fd);
b119f362 287 closedir(dir);
b119f362
SH
288 goto restart;
289 }
28d9e29e 290 WARN("Inherited fd %d", fd);
80090207 291 }
4e2d6b9a 292 closedir(dir);
80090207 293
4e2d6b9a
CB
294 /*
295 * Only enable syslog at this point to avoid the above logging
296 * function to open a new fd and make the check_inherited function
297 * enter an infinite loop.
64c57ea1 298 */
4e2d6b9a 299 lxc_log_syslog_enable();
64c57ea1 300
92c7f629 301 return 0;
80090207
CLG
302}
303
83ee7875 304static int setup_signal_fd(sigset_t *oldmask)
b0a33c1e 305{
7288dfb6 306 int ret;
b0a33c1e 307 sigset_t mask;
7288dfb6 308 const int signals[] = {SIGBUS, SIGILL, SIGSEGV, SIGWINCH};
b0a33c1e 309
408da065 310 /* Block everything except serious error signals. */
df0795b1
CB
311 ret = sigfillset(&mask);
312 if (ret < 0)
313 return -EBADF;
314
9b10bef5 315 for (int sig = 0; sig < (sizeof(signals) / sizeof(signals[0])); sig++) {
df0795b1
CB
316 ret = sigdelset(&mask, signals[sig]);
317 if (ret < 0)
318 return -EBADF;
b0a33c1e 319 }
320
b467714b 321 ret = pthread_sigmask(SIG_BLOCK, &mask, oldmask);
818a57fc
CB
322 if (ret < 0)
323 return log_error_errno(-EBADF, errno,
324 "Failed to set signal mask");
b0a33c1e 325
df0795b1 326 ret = signalfd(-1, &mask, SFD_CLOEXEC);
818a57fc
CB
327 if (ret < 0)
328 return log_error_errno(-EBADF,
329 errno, "Failed to create signal file descriptor");
b0a33c1e 330
df0795b1 331 TRACE("Created signal file descriptor %d", ret);
1ac470c0 332
df0795b1 333 return ret;
b0a33c1e 334}
335
84c92abd 336static int signal_handler(int fd, uint32_t events, void *data,
f3787121 337 struct lxc_epoll_descr *descr)
b0a33c1e 338{
15cd25fd 339 int ret;
3c319edb
CB
340 siginfo_t info;
341 struct signalfd_siginfo siginfo;
342 struct lxc_handler *hdlr = data;
15cd25fd 343
489f39be 344 ret = lxc_read_nointr(fd, &siginfo, sizeof(siginfo));
818a57fc
CB
345 if (ret < 0)
346 return log_error(LXC_MAINLOOP_ERROR, "Failed to read signal info from signal file descriptor %d", fd);
15cd25fd 347
818a57fc 348 if (ret != sizeof(siginfo))
f7a97743 349 return log_error(LXC_MAINLOOP_ERROR, "Unexpected size for struct signalfd_siginfo");
15cd25fd 350
408da065 351 /* Check whether init is running. */
80507ee8 352 info.si_pid = 0;
3c319edb
CB
353 ret = waitid(P_PID, hdlr->pid, &info, WEXITED | WNOWAIT | WNOHANG);
354 if (ret == 0 && info.si_pid == hdlr->pid)
355 hdlr->init_died = true;
80507ee8 356
cd5177e9
TA
357 /* Try to figure out a reasonable exit status to report. */
358 if (hdlr->init_died) {
359 switch (info.si_code) {
360 case CLD_EXITED:
361 hdlr->exit_status = info.si_status << 8;
362 break;
363 case CLD_KILLED:
364 case CLD_DUMPED:
365 case CLD_STOPPED:
366 hdlr->exit_status = info.si_status << 8 | 0x7f;
367 break;
368 case CLD_CONTINUED:
369 /* Huh? The waitid() told us it's dead *and* continued? */
370 WARN("Init %d dead and continued?", hdlr->pid);
371 hdlr->exit_status = 1;
372 break;
373 default:
20993a97 374 ERROR("Unknown si_code: %d", info.si_code);
55e1311e 375 hdlr->exit_status = 1;
cd5177e9
TA
376 }
377 }
378
d4b5d7a8 379 if (siginfo.ssi_signo == SIGHUP) {
33942046
CB
380 if (hdlr->pidfd >= 0)
381 lxc_raw_pidfd_send_signal(hdlr->pidfd, SIGTERM, NULL, 0);
d9bb2fba
CB
382 else
383 kill(hdlr->pid, SIGTERM);
d4b5d7a8 384 INFO("Killing %d since terminal hung up", hdlr->pid);
9b10bef5
CB
385 return hdlr->init_died ? LXC_MAINLOOP_CLOSE
386 : LXC_MAINLOOP_CONTINUE;
d4b5d7a8
TA
387 }
388
9cb94384 389 if (siginfo.ssi_signo != SIGCHLD) {
33942046
CB
390 if (hdlr->pidfd >= 0)
391 lxc_raw_pidfd_send_signal(hdlr->pidfd,
392 siginfo.ssi_signo, NULL, 0);
d9bb2fba
CB
393 else
394 kill(hdlr->pid, siginfo.ssi_signo);
9cb94384 395 INFO("Forwarded signal %d to pid %d", siginfo.ssi_signo, hdlr->pid);
9b10bef5
CB
396 return hdlr->init_died ? LXC_MAINLOOP_CLOSE
397 : LXC_MAINLOOP_CONTINUE;
9cb94384
TA
398 }
399
3a9e949f
TA
400 /* More robustness, protect ourself from a SIGCHLD sent
401 * by a process different from the container init.
402 */
403 if (siginfo.ssi_pid != hdlr->pid) {
6e94162a
CB
404 NOTICE("Received %d from pid %d instead of container init %d",
405 siginfo.ssi_signo, siginfo.ssi_pid, hdlr->pid);
9b10bef5
CB
406 return hdlr->init_died ? LXC_MAINLOOP_CLOSE
407 : LXC_MAINLOOP_CONTINUE;
3a9e949f
TA
408 }
409
408da065 410 if (siginfo.ssi_code == CLD_STOPPED) {
6e94162a 411 INFO("Container init process was stopped");
9b10bef5
CB
412 return hdlr->init_died ? LXC_MAINLOOP_CLOSE
413 : LXC_MAINLOOP_CONTINUE;
414 }
415
416 if (siginfo.ssi_code == CLD_CONTINUED) {
6e94162a 417 INFO("Container init process was continued");
9b10bef5
CB
418 return hdlr->init_died ? LXC_MAINLOOP_CLOSE
419 : LXC_MAINLOOP_CONTINUE;
15cd25fd 420 }
1ac470c0 421
818a57fc 422 return log_debug(LXC_MAINLOOP_CLOSE, "Container init process %d exited", hdlr->pid);
b0a33c1e 423}
424
974a8aba
CB
425int lxc_serve_state_clients(const char *name, struct lxc_handler *handler,
426 lxc_state_t state)
66aeffc7 427{
9de31d5a 428 size_t retlen;
dbc9832d
CB
429 ssize_t ret;
430 struct lxc_list *cur, *next;
dbc9832d
CB
431 struct lxc_msg msg = {.type = lxc_msg_state, .value = state};
432
974a8aba
CB
433 if (state == THAWED)
434 handler->state = RUNNING;
435 else
436 handler->state = state;
437
9dfa4041 438 TRACE("Set container state to %s", lxc_state2str(state));
dbc9832d 439
818a57fc
CB
440 if (lxc_list_empty(&handler->conf->state_clients))
441 return log_trace(0, "No state clients registered");
dbc9832d 442
9de31d5a
CB
443 retlen = strlcpy(msg.name, name, sizeof(msg.name));
444 if (retlen >= sizeof(msg.name))
445 return -E2BIG;
dbc9832d 446
d39b10eb 447 lxc_list_for_each_safe(cur, &handler->conf->state_clients, next) {
d85617bc 448 struct lxc_state_client *client = cur->elem;
dbc9832d 449
d39b10eb 450 if (client->states[state] == 0) {
9dfa4041 451 TRACE("State %s not registered for state client %d",
dbc9832d
CB
452 lxc_state2str(state), client->clientfd);
453 continue;
454 }
455
9dfa4041 456 TRACE("Sending state %s to state client %d",
dbc9832d
CB
457 lxc_state2str(state), client->clientfd);
458
7fbb15ec
CB
459 ret = lxc_send_nointr(client->clientfd, &msg, sizeof(msg), MSG_NOSIGNAL);
460 if (ret <= 0)
6d1400b5 461 SYSERROR("Failed to send message to client");
dbc9832d
CB
462
463 /* kick client from list */
dbc9832d 464 lxc_list_del(cur);
300d1cb4 465 close(client->clientfd);
dbc9832d
CB
466 free(cur->elem);
467 free(cur);
468 }
dbc9832d 469
5e5576a4
CB
470 return 0;
471}
472
473static int lxc_serve_state_socket_pair(const char *name,
474 struct lxc_handler *handler,
475 lxc_state_t state)
476{
477 ssize_t ret;
478
bb955810 479 if (!handler->daemonize ||
5e5576a4
CB
480 handler->state_socket_pair[1] < 0 ||
481 state == STARTING)
482 return 0;
483
484 /* Close read end of the socket pair. */
a783a414 485 close_prot_errno_disarm(handler->state_socket_pair[0]);
5e5576a4 486
ee8377bd 487again:
5e5576a4
CB
488 ret = lxc_abstract_unix_send_credential(handler->state_socket_pair[1],
489 &(int){state}, sizeof(int));
9044b79e 490 if (ret < 0) {
491 SYSERROR("Failed to send state to %d", handler->state_socket_pair[1]);
492
ee8377bd
CB
493 if (errno == EINTR)
494 goto again;
9044b79e 495
496 return -1;
497 }
498
818a57fc
CB
499 if (ret != sizeof(int))
500 return log_error(-1, "Message too long : %d", handler->state_socket_pair[1]);
5e5576a4
CB
501
502 TRACE("Sent container state \"%s\" to %d", lxc_state2str(state),
503 handler->state_socket_pair[1]);
504
505 /* Close write end of the socket pair. */
818a57fc 506 close_prot_errno_disarm(handler->state_socket_pair[1]);
5e5576a4
CB
507
508 return 0;
509}
510
511int lxc_set_state(const char *name, struct lxc_handler *handler,
512 lxc_state_t state)
513{
514 int ret;
515
516 ret = lxc_serve_state_socket_pair(name, handler, state);
818a57fc
CB
517 if (ret < 0)
518 return log_error(-1, "Failed to synchronize via anonymous pair of unix sockets");
5e5576a4
CB
519
520 ret = lxc_serve_state_clients(name, handler, state);
521 if (ret < 0)
522 return -1;
523
dbc9832d
CB
524 /* This function will try to connect to the legacy lxc-monitord state
525 * server and only exists for backwards compatibility.
526 */
9123e471 527 lxc_monitor_send_state(name, state, handler->lxcpath);
dbc9832d 528
66aeffc7
DL
529 return 0;
530}
531
735f2c6e 532int lxc_poll(const char *name, struct lxc_handler *handler)
b0a33c1e 533{
3c319edb 534 int ret;
86530b0a 535 bool has_console = true;
3c319edb 536 struct lxc_epoll_descr descr, descr_console;
b0a33c1e 537
03760215
CB
538 if (handler->conf->console.path &&
539 strcmp(handler->conf->console.path, "none") == 0)
86530b0a
L
540 has_console = false;
541
3c319edb
CB
542 ret = lxc_mainloop_open(&descr);
543 if (ret < 0) {
544 ERROR("Failed to create mainloop");
50c8bf05 545 goto out_sigfd;
b0a33c1e 546 }
547
30a33fbd
CB
548 if (has_console) {
549 ret = lxc_mainloop_open(&descr_console);
550 if (ret < 0) {
551 ERROR("Failed to create console mainloop");
552 goto out_mainloop;
553 }
3c319edb
CB
554 }
555
1cc8bd4b 556 ret = lxc_mainloop_add_handler(&descr, handler->sigfd, signal_handler, handler);
3c319edb 557 if (ret < 0) {
1cc8bd4b 558 ERROR("Failed to add signal handler for %d to mainloop", handler->sigfd);
3c319edb 559 goto out_mainloop_console;
b0a33c1e 560 }
561
2ac0f627
CB
562 ret = lxc_seccomp_setup_proxy(&handler->conf->seccomp, &descr, handler);
563 if (ret < 0) {
564 ERROR("Failed to setup seccomp proxy");
c3e3c21a 565 goto out_mainloop_console;
2ac0f627 566 }
cdb2a47f 567
30a33fbd 568 if (has_console) {
dcad02f8 569 struct lxc_terminal *console = &handler->conf->console;
63376d7d 570
093bce5f 571 ret = lxc_terminal_mainloop_add(&descr, console);
30a33fbd
CB
572 if (ret < 0) {
573 ERROR("Failed to add console handlers to mainloop");
574 goto out_mainloop_console;
575 }
576
093bce5f 577 ret = lxc_terminal_mainloop_add(&descr_console, console);
30a33fbd
CB
578 if (ret < 0) {
579 ERROR("Failed to add console handlers to console mainloop");
580 goto out_mainloop_console;
581 }
a54585ad
L
582
583 handler->conf->console.descr = &descr;
563f2f2c
DL
584 }
585
3c319edb
CB
586 ret = lxc_cmd_mainloop_add(name, &descr, handler);
587 if (ret < 0) {
588 ERROR("Failed to add command handler to mainloop");
589 goto out_mainloop_console;
590 }
b0a33c1e 591
3c319edb 592 TRACE("Mainloop is ready");
b0a33c1e 593
3c319edb 594 ret = lxc_mainloop(&descr, -1);
a783a414 595 close_prot_errno_disarm(descr.epfd);
1cc8bd4b 596 if (ret < 0 || !handler->init_died)
42b09f94 597 goto out_mainloop_console;
1cc8bd4b 598
30a33fbd
CB
599 if (has_console)
600 ret = lxc_mainloop(&descr_console, 0);
601
3c319edb 602out_mainloop_console:
30a33fbd
CB
603 if (has_console) {
604 lxc_mainloop_close(&descr_console);
605 TRACE("Closed console mainloop");
606 }
3c319edb 607
42b09f94
CB
608out_mainloop:
609 lxc_mainloop_close(&descr);
610 TRACE("Closed mainloop");
611
b0a33c1e 612out_sigfd:
1cc8bd4b 613 TRACE("Closed signal file descriptor %d", handler->sigfd);
a783a414 614 close_prot_errno_disarm(handler->sigfd);
408da065 615
1cc8bd4b 616 return ret;
b0a33c1e 617}
618
a42abcce 619void lxc_put_handler(struct lxc_handler *handler)
f2e07cb6 620{
a783a414
CB
621 close_prot_errno_disarm(handler->pinfd);
622 close_prot_errno_disarm(handler->pidfd);
623 close_prot_errno_disarm(handler->sigfd);
1cc8bd4b 624 lxc_put_nsfds(handler);
80308d07 625 if (handler->conf && handler->conf->reboot == REBOOT_NONE)
a783a414
CB
626 close_prot_errno_disarm(handler->conf->maincmd_fd);
627 close_prot_errno_disarm(handler->monitor_status_fd);
628 close_prot_errno_disarm(handler->state_socket_pair[0]);
629 close_prot_errno_disarm(handler->state_socket_pair[1]);
630 cgroup_exit(handler->cgroup_ops);
a42abcce
CB
631 if (handler->conf && handler->conf->reboot == REBOOT_NONE)
632 free_disarm(handler);
633 else
634 handler->conf = NULL;
f2e07cb6
CB
635}
636
a42abcce
CB
637struct lxc_handler *lxc_init_handler(struct lxc_handler *old,
638 const char *name, struct lxc_conf *conf,
5e5576a4 639 const char *lxcpath, bool daemonize)
59eb99ba 640{
85c279bb 641 int nr_keep_fds = 0;
818a57fc 642 int ret;
3a0f472d
DL
643 struct lxc_handler *handler;
644
a42abcce
CB
645 if (!old)
646 handler = zalloc(sizeof(*handler));
647 else
648 handler = old;
fdecbc9c 649 if (!handler)
3a0f472d 650 return NULL;
9044b79e 651
fdecbc9c
CB
652 /* Note that am_guest_unpriv() checks the effective uid. We
653 * probably don't care if we are real root only if we are running
654 * as root so this should be fine.
790255cf 655 */
e0010464 656 handler->am_root = !am_guest_unpriv();
fae349da 657 handler->conf = conf;
9123e471 658 handler->lxcpath = lxcpath;
818a57fc
CB
659 handler->init_died = false;
660 handler->data_sock[0] = -EBADF;
661 handler->data_sock[1] = -EBADF;
4d8bdfa0 662 handler->monitor_status_fd = -EBADF;
818a57fc 663 handler->pinfd = -EBADF;
33942046 664 handler->pidfd = -EBADF;
1cc8bd4b 665 handler->sigfd = -EBADF;
818a57fc
CB
666 handler->state_socket_pair[0] = -EBADF;
667 handler->state_socket_pair[1] = -EBADF;
80308d07 668 if (handler->conf->reboot == REBOOT_NONE)
d39b10eb 669 lxc_list_init(&handler->conf->state_clients);
fae349da 670
818a57fc
CB
671 for (int i = 0; i < LXC_NS_MAX; i++)
672 handler->nsfd[i] = -EBADF;
b6b2b194 673
f0ecc19d 674 handler->name = name;
c581d2a6
CB
675 if (daemonize)
676 handler->transient_pid = lxc_raw_getpid();
677 else
678 handler->transient_pid = -1;
3bdf52d7 679
80308d07 680 if (daemonize && handler->conf->reboot == REBOOT_NONE) {
5e5576a4 681 /* Create socketpair() to synchronize on daemonized startup.
fdecbc9c
CB
682 * When the container reboots we don't need to synchronize
683 * again currently so don't open another socketpair().
5e5576a4
CB
684 */
685 ret = socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0,
686 handler->state_socket_pair);
687 if (ret < 0) {
688 ERROR("Failed to create anonymous pair of unix sockets");
689 goto on_error;
690 }
9044b79e 691
5e5576a4
CB
692 TRACE("Created anonymous pair {%d,%d} of unix sockets",
693 handler->state_socket_pair[0],
694 handler->state_socket_pair[1]);
85c279bb
CB
695 handler->keep_fds[nr_keep_fds++] = handler->state_socket_pair[0];
696 handler->keep_fds[nr_keep_fds++] = handler->state_socket_pair[1];
5e5576a4
CB
697 }
698
80308d07 699 if (handler->conf->reboot == REBOOT_NONE) {
d39b10eb
CB
700 handler->conf->maincmd_fd = lxc_cmd_init(name, lxcpath, "command");
701 if (handler->conf->maincmd_fd < 0) {
702 ERROR("Failed to set up command socket");
703 goto on_error;
704 }
85c279bb 705 handler->keep_fds[nr_keep_fds++] = handler->conf->maincmd_fd;
aa460476 706 }
9044b79e 707
9dfa4041 708 TRACE("Unix domain socket %d for command server is ready",
aa460476
CB
709 handler->conf->maincmd_fd);
710
711 return handler;
712
f2e07cb6 713on_error:
a42abcce 714 lxc_put_handler(handler);
aa460476
CB
715
716 return NULL;
717}
718
719int lxc_init(const char *name, struct lxc_handler *handler)
720{
f62cf1d4 721 __do_close int status_fd = -EBADF;
a2c09be0 722 int ret;
4a03ded4 723 const char *loglevel;
aa460476
CB
724 struct lxc_conf *conf = handler->conf;
725
434c8e15 726 handler->monitor_pid = lxc_raw_getpid();
4d8bdfa0 727 status_fd = open("/proc/self/status", O_RDONLY | O_CLOEXEC);
59b1b67e
CB
728 if (status_fd < 0)
729 return log_error_errno(-1, errno, "Failed to open monitor status fd");
434c8e15 730
aa460476 731 lsm_init();
2170b263 732 TRACE("Initialized LSM");
d2e30e99 733
408da065 734 /* Begin by setting the state to STARTING. */
2170b263 735 ret = lxc_set_state(name, handler, STARTING);
59b1b67e
CB
736 if (ret < 0)
737 return log_error(-1, "Failed to set state to \"%s\"", lxc_state2str(STARTING));
46800e77 738 TRACE("Set container state to \"STARTING\"");
0ad19a3f 739
408da065 740 /* Start of environment variable setup for hooks. */
24c2a48d
CB
741 ret = setenv("LXC_NAME", name, 1);
742 if (ret < 0)
743 SYSERROR("Failed to set environment variable: LXC_NAME=%s", name);
408da065 744
2170b263
CB
745 if (conf->rcfile) {
746 ret = setenv("LXC_CONFIG_FILE", conf->rcfile, 1);
747 if (ret < 0)
818a57fc 748 SYSERROR("Failed to set environment variable: LXC_CONFIG_FILE=%s", conf->rcfile);
2170b263 749 }
408da065 750
2170b263
CB
751 if (conf->rootfs.mount) {
752 ret = setenv("LXC_ROOTFS_MOUNT", conf->rootfs.mount, 1);
753 if (ret < 0)
818a57fc 754 SYSERROR("Failed to set environment variable: LXC_ROOTFS_MOUNT=%s", conf->rootfs.mount);
2170b263 755 }
408da065 756
2170b263
CB
757 if (conf->rootfs.path) {
758 ret = setenv("LXC_ROOTFS_PATH", conf->rootfs.path, 1);
759 if (ret < 0)
818a57fc 760 SYSERROR("Failed to set environment variable: LXC_ROOTFS_PATH=%s", conf->rootfs.path);
2170b263 761 }
408da065 762
2170b263
CB
763 if (conf->console.path) {
764 ret = setenv("LXC_CONSOLE", conf->console.path, 1);
765 if (ret < 0)
818a57fc 766 SYSERROR("Failed to set environment variable: LXC_CONSOLE=%s", conf->console.path);
2170b263 767 }
408da065 768
2170b263
CB
769 if (conf->console.log_path) {
770 ret = setenv("LXC_CONSOLE_LOGPATH", conf->console.log_path, 1);
771 if (ret < 0)
818a57fc 772 SYSERROR("Failed to set environment variable: LXC_CONSOLE_LOGPATH=%s", conf->console.log_path);
2170b263 773 }
408da065 774
2170b263
CB
775 if (cgns_supported()) {
776 ret = setenv("LXC_CGNS_AWARE", "1", 1);
777 if (ret < 0)
818a57fc 778 SYSERROR("Failed to set environment variable LXC_CGNS_AWARE=1");
2170b263 779 }
b8f88d9b 780
4a03ded4 781 loglevel = lxc_log_priority_to_string(lxc_log_get_level());
2170b263
CB
782 ret = setenv("LXC_LOG_LEVEL", loglevel, 1);
783 if (ret < 0)
818a57fc 784 SYSERROR("Set environment variable LXC_LOG_LEVEL=%s", loglevel);
a2c09be0
CB
785
786 if (conf->hooks_version == 0)
787 ret = setenv("LXC_HOOK_VERSION", "0", 1);
788 else
789 ret = setenv("LXC_HOOK_VERSION", "1", 1);
790 if (ret < 0)
791 SYSERROR("Failed to set environment variable LXC_HOOK_VERSION=%u", conf->hooks_version);
408da065 792 /* End of environment variable setup for hooks. */
f7bee6c6 793
2170b263 794 TRACE("Set environment variables");
dbc9832d 795
2170b263 796 ret = run_lxc_hooks(name, "pre-start", conf, NULL);
65a2dc8b
CB
797 if (ret < 0)
798 return log_error(-1, "Failed to run lxc.hook.pre-start for container \"%s\"", name);
2170b263 799 TRACE("Ran pre-start hooks");
26ddeedd 800
408da065
CB
801 /* The signal fd has to be created before forking otherwise if the child
802 * process exits before we setup the signal fd, the event will be lost
803 * and the command will be stuck.
804 */
83ee7875 805 handler->sigfd = setup_signal_fd(&handler->oldmask);
65a2dc8b
CB
806 if (handler->sigfd < 0)
807 return log_error(-1, "Failed to setup SIGCHLD fd handler.");
2170b263 808 TRACE("Set up signal fd");
b5159817 809
408da065 810 /* Do this after setting up signals since it might unblock SIGWINCH. */
564e31c4 811 ret = lxc_terminal_setup(conf);
4d1ffb0a
CB
812 if (ret < 0) {
813 ERROR("Failed to create console");
b5159817 814 goto out_restore_sigmask;
b0a33c1e 815 }
4d1ffb0a 816 TRACE("Created console");
b0a33c1e 817
2f835b4b 818 ret = lxc_terminal_map_ids(conf, &conf->console);
7cfeddd7 819 if (ret < 0) {
2170b263 820 ERROR("Failed to chown console");
5b74eb3c 821 goto out_delete_terminal;
c4d10a05 822 }
2170b263 823 TRACE("Chowned console");
c4d10a05 824
5a087e05 825 handler->cgroup_ops = cgroup_init(handler->conf);
2202afc9
CB
826 if (!handler->cgroup_ops) {
827 ERROR("Failed to initialize cgroup driver");
5b74eb3c 828 goto out_delete_terminal;
2202afc9
CB
829 }
830 TRACE("Initialized cgroup driver");
831
2e1361a6
CB
832 ret = lxc_read_seccomp_config(conf);
833 if (ret < 0)
834 return log_error(-1, "Failed loading seccomp policy");
835 TRACE("Read seccomp policy");
836
1800f924
WB
837 ret = lsm_process_prepare(conf, handler->lxcpath);
838 if (ret < 0) {
839 ERROR("Failed to initialize LSM");
c581d2a6 840 goto out_delete_terminal;
1800f924
WB
841 }
842 TRACE("Initialized LSM");
843
2170b263 844 INFO("Container \"%s\" is initialized", name);
4d8bdfa0 845 handler->monitor_status_fd = move_fd(status_fd);
aa460476 846 return 0;
59eb99ba 847
5b74eb3c
CB
848out_delete_terminal:
849 lxc_terminal_delete(&handler->conf->console);
ea918412 850
b5159817 851out_restore_sigmask:
4c496daa 852 (void)pthread_sigmask(SIG_SETMASK, &handler->oldmask, NULL);
ea918412 853
aa460476 854 return -1;
59eb99ba
DL
855}
856
fd5be714 857void lxc_end(struct lxc_handler *handler)
59eb99ba 858{
818a57fc 859 int ret;
18b3b9c1 860 pid_t self;
dbc9832d 861 struct lxc_list *cur, *next;
dbc9832d 862 char *namespaces[LXC_NS_MAX + 1];
b3286b62 863 size_t namespace_count = 0;
0c5859ff 864 const char *name = handler->name;
2202afc9 865 struct cgroup_ops *cgroup_ops = handler->cgroup_ops;
b6b2b194 866
408da065
CB
867 /* The STOPPING state is there for future cleanup code which can take
868 * awhile.
59eb99ba 869 */
25c2aca5 870 lxc_set_state(name, handler, STOPPING);
b6b2b194 871
0059379f 872 self = lxc_raw_getpid();
818a57fc 873 for (int i = 0; i < LXC_NS_MAX; i++) {
18b3b9c1
CB
874 if (handler->nsfd[i] < 0)
875 continue;
876
877 if (handler->conf->hooks_version == 0)
c097467f 878 ret = asprintf(&namespaces[namespace_count],
18b3b9c1
CB
879 "%s:/proc/%d/fd/%d", ns_info[i].proc_name,
880 self, handler->nsfd[i]);
881 else
c097467f 882 ret = asprintf(&namespaces[namespace_count],
18b3b9c1 883 "/proc/%d/fd/%d", self, handler->nsfd[i]);
818a57fc 884 if (ret < 0) {
c097467f 885 SYSERROR("Failed to allocate memory");
18b3b9c1 886 break;
b3286b62 887 }
18b3b9c1
CB
888
889 if (handler->conf->hooks_version == 0) {
890 namespace_count++;
891 continue;
892 }
893
c097467f
CB
894 ret = setenv(ns_info[i].env_name, namespaces[namespace_count], 1);
895 if (ret < 0)
18b3b9c1
CB
896 SYSERROR("Failed to set environment variable %s=%s",
897 ns_info[i].env_name, namespaces[namespace_count]);
898 else
899 TRACE("Set environment variable %s=%s",
900 ns_info[i].env_name, namespaces[namespace_count]);
901
902 namespace_count++;
b3286b62
WB
903 }
904 namespaces[namespace_count] = NULL;
c154af98 905
80308d07 906 if (handler->conf->reboot > REBOOT_NONE) {
c097467f
CB
907 ret = setenv("LXC_TARGET", "reboot", 1);
908 if (ret < 0)
818a57fc 909 SYSERROR("Failed to set environment variable: LXC_TARGET=reboot");
c097467f 910 }
408da065 911
80308d07 912 if (handler->conf->reboot == REBOOT_NONE) {
c097467f
CB
913 ret = setenv("LXC_TARGET", "stop", 1);
914 if (ret < 0)
818a57fc 915 SYSERROR("Failed to set environment variable: LXC_TARGET=stop");
c097467f 916 }
c154af98 917
18b3b9c1 918 if (handler->conf->hooks_version == 0)
c097467f 919 ret = run_lxc_hooks(name, "stop", handler->conf, namespaces);
18b3b9c1 920 else
c097467f
CB
921 ret = run_lxc_hooks(name, "stop", handler->conf, NULL);
922 if (ret < 0)
923 ERROR("Failed to run \"lxc.hook.stop\" hook");
c154af98 924
b3286b62
WB
925 while (namespace_count--)
926 free(namespaces[namespace_count]);
28d9e29e 927
1800f924
WB
928 lsm_process_cleanup(handler->conf, handler->lxcpath);
929
e2aed383
WB
930 if (cgroup_ops) {
931 cgroup_ops->payload_destroy(cgroup_ops, handler);
932 cgroup_ops->monitor_destroy(cgroup_ops, handler);
933 }
4288b79f 934
80308d07 935 if (handler->conf->reboot == REBOOT_NONE) {
f893d898
CB
936 /* For all new state clients simply close the command socket.
937 * This will inform all state clients that the container is
938 * STOPPED and also prevents a race between a open()/close() on
939 * the command socket causing a new process to get ECONNREFUSED
940 * because we haven't yet closed the command socket.
941 */
b87ee312 942 close_prot_errno_disarm(handler->conf->maincmd_fd);
c3184275
CB
943 TRACE("Closed command socket");
944
945 /* This function will try to connect to the legacy lxc-monitord
946 * state server and only exists for backwards compatibility.
947 */
948 lxc_monitor_send_state(name, STOPPED, handler->lxcpath);
949
950 /* The command socket is closed so no one can acces the command
951 * socket anymore so there's no need to lock it.
952 */
953 handler->state = STOPPED;
954 TRACE("Set container state to \"STOPPED\"");
f893d898
CB
955 } else {
956 lxc_set_state(name, handler, STOPPED);
695d0e56 957 TRACE("Set container state to \"STOPPED\"");
d39b10eb 958 }
4288b79f 959
048493a3
CB
960 /* Avoid lingering namespace references. */
961 lxc_put_nsfds(handler);
962
46800e77
CB
963 ret = run_lxc_hooks(name, "post-stop", handler->conf, NULL);
964 if (ret < 0) {
c097467f 965 ERROR("Failed to run lxc.hook.post-stop for container \"%s\"", name);
80308d07 966 if (handler->conf->reboot > REBOOT_NONE) {
c097467f 967 WARN("Container will be stopped instead of rebooted");
80308d07 968 handler->conf->reboot = REBOOT_NONE;
c097467f
CB
969
970 ret = setenv("LXC_TARGET", "stop", 1);
971 if (ret < 0)
818a57fc 972 WARN("Failed to set environment variable: LXC_TARGET=stop");
f3787121
CB
973 }
974 }
26ddeedd 975
408da065 976 /* Reset mask set by setup_signal_fd. */
b467714b 977 ret = pthread_sigmask(SIG_SETMASK, &handler->oldmask, NULL);
c097467f 978 if (ret < 0)
a24c5678 979 SYSWARN("Failed to restore signal mask");
8f64a3f6 980
2aac071b 981 lxc_terminal_delete(&handler->conf->console);
0e4be3cf 982 lxc_delete_tty(&handler->conf->ttys);
dbc9832d 983
dbc9832d
CB
984 /* The command socket is now closed, no more state clients can register
985 * themselves from now on. So free the list of state clients.
986 */
d39b10eb
CB
987 lxc_list_for_each_safe(cur, &handler->conf->state_clients, next) {
988 struct lxc_state_client *client = cur->elem;
989
990 /* Keep state clients that want to be notified about reboots. */
80308d07
CB
991 if ((handler->conf->reboot > REBOOT_NONE) &&
992 (client->states[RUNNING] == 2))
d39b10eb
CB
993 continue;
994
dbc9832d 995 /* close state client socket */
dbc9832d 996 lxc_list_del(cur);
c097467f 997 close(client->clientfd);
dbc9832d
CB
998 free(cur->elem);
999 free(cur);
1000 }
1001
80308d07 1002 if (handler->conf->ephemeral == 1 && handler->conf->reboot != REBOOT_REQ)
28272964 1003 lxc_destroy_container_on_signal(handler, name);
2c5f2ede 1004
a42abcce 1005 lxc_put_handler(handler);
59eb99ba
DL
1006}
1007
0c5859ff 1008void lxc_abort(struct lxc_handler *handler)
59eb99ba 1009{
33942046
CB
1010 int ret = 0;
1011 int status;
73e608b2 1012
0c5859ff 1013 lxc_set_state(handler->name, handler, ABORTING);
0e4f9d51 1014
c718fac1 1015 if (handler->pidfd >= 0) {
33942046 1016 ret = lxc_raw_pidfd_send_signal(handler->pidfd, SIGKILL, NULL, 0);
c718fac1 1017 if (ret)
11c7d349
CB
1018 SYSWARN("Failed to send SIGKILL via pidfd %d for process %d",
1019 handler->pidfd, handler->pid);
c718fac1
CB
1020 }
1021
65a2dc8b 1022 if ((!ret || errno != ESRCH) && handler->pid > 0)
11c7d349
CB
1023 if (kill(handler->pid, SIGKILL))
1024 SYSWARN("Failed to send SIGKILL to %d", handler->pid);
0e4f9d51 1025
33942046
CB
1026 do {
1027 ret = waitpid(-1, &status, 0);
1028 } while (ret > 0);
59eb99ba
DL
1029}
1030
ffe1e01a 1031static int do_start(void *data)
50e98013 1032{
cdb2a47f 1033 struct lxc_handler *handler = data;
f62cf1d4 1034 __lxc_unused __do_close int data_sock0 = handler->data_sock[0],
9ff57a59 1035 data_sock1 = handler->data_sock[1];
d3103162 1036 __do_close int devnull_fd = -EBADF, status_fd = -EBADF;
8bf3abfb 1037 int ret;
928b1f04
YT
1038 uid_t new_uid;
1039 gid_t new_gid;
e96a536c 1040 struct lxc_list *iterator;
964581c2
CB
1041 uid_t nsuid = 0;
1042 gid_t nsgid = 0;
50e98013 1043
18225d19 1044 lxc_sync_fini_parent(handler);
50e98013 1045
9ff57a59 1046 if (lxc_abstract_unix_recv_fds(data_sock1, &status_fd, 1, NULL, 0) < 0) {
4d8bdfa0
CB
1047 ERROR("Failed to receive status file descriptor to child process");
1048 goto out_warn_father;
1049 }
1050
408da065
CB
1051 /* This prctl must be before the synchro, so if the parent dies before
1052 * we set the parent death signal, we will detect its death with the
1053 * synchro right after, otherwise we have a window where the parent can
1054 * exit before we set the pdeath signal leading to a unsupervized
1055 * container.
743ecd2e 1056 */
4d8bdfa0 1057 ret = lxc_set_death_signal(SIGKILL, handler->monitor_pid, status_fd);
912314fc
CB
1058 if (ret < 0) {
1059 SYSERROR("Failed to set PR_SET_PDEATHSIG to SIGKILL");
18225d19 1060 goto out_warn_father;
743ecd2e
DL
1061 }
1062
611ddd34
CB
1063 ret = lxc_ambient_caps_up();
1064 if (ret < 0) {
6f5e532f 1065 ERROR("Failed to raise ambient capabilities");
611ddd34
CB
1066 goto out_warn_father;
1067 }
1068
b467714b 1069 ret = pthread_sigmask(SIG_SETMASK, &handler->oldmask, NULL);
18225d19
CB
1070 if (ret < 0) {
1071 SYSERROR("Failed to set signal mask");
1072 goto out_warn_father;
1073 }
50e98013 1074
408da065 1075 /* Don't leak the pinfd to the container. */
a783a414 1076 close_prot_errno_disarm(handler->pinfd);
2b0e17e4 1077
18225d19
CB
1078 ret = lxc_sync_wait_parent(handler, LXC_SYNC_STARTUP);
1079 if (ret < 0)
1080 goto out_warn_father;
5b1e83cb 1081
408da065
CB
1082 /* Unshare CLONE_NEWNET after CLONE_NEWUSER. See
1083 * https://github.com/lxc/lxd/issues/1978.
1084 */
f7176c3e 1085 if (handler->ns_unshare_flags & CLONE_NEWNET) {
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 */
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);
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 */
41808e20 1301 if (handler->conf->console.pty >= 0) {
bb955810 1302 if (handler->daemonize || !handler->conf->is_execute)
41808e20 1303 ret = set_stdfds(handler->conf->console.pty);
5d113f65 1304 else
41808e20 1305 ret = lxc_terminal_set_stdfds(handler->conf->console.pty);
5d113f65 1306 if (ret < 0) {
818a57fc 1307 ERROR("Failed to redirect std{in,out,err} to pty file descriptor %d",
41808e20 1308 handler->conf->console.pty);
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
41808e20 1335 if (handler->conf->console.pty < 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;
36a94ce8 1487 tty->ptx = ttyfds[0];
41808e20
CB
1488 tty->pty = ttyfds[1];
1489 TRACE("Received pty with ptx fd %d and pty fd %d from child", tty->ptx, tty->pty);
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
f7176c3e
CB
1540 /* Deal with namespaces that are unshared. */
1541 if (handler->ns_clone_flags & CLONE_NEWTIME)
1542 handler->ns_unshare_flags |= CLONE_NEWTIME;
1543
1544 if (!pure_unified_layout(handler->cgroup_ops) && handler->ns_clone_flags & CLONE_NEWCGROUP)
1545 handler->ns_unshare_flags |= CLONE_NEWCGROUP;
1546
1547 if ((handler->ns_clone_flags & (CLONE_NEWNET | CLONE_NEWUSER)) ==
1548 (CLONE_NEWNET | CLONE_NEWUSER))
1549 handler->ns_unshare_flags |= CLONE_NEWNET;
1550
1551 /* Deal with namespaces that are spawned. */
1552 handler->ns_on_clone_flags = handler->ns_clone_flags & ~handler->ns_unshare_flags;
1553
1554 handler->clone_flags = handler->ns_on_clone_flags | CLONE_PIDFD;
1555
5af9369b 1556 return 0;
f813849c
TA
1557}
1558
8deca6c9
CB
1559/* Note that this function is used with clone(CLONE_VM). Some glibc versions
1560 * used to reset the pid/tid to -1 when CLONE_VM was used without CLONE_THREAD.
1561 * But since the memory between parent and child is shared on CLONE_VM this
1562 * would invalidate the getpid() cache that glibc used to maintain and so
1563 * getpid() in the child would return the parent's pid. This is all fixed in
1564 * newer glibc versions where the getpid() cache is removed and the pid/tid is
1565 * not reset anymore.
b35ddc5b 1566 * However, if for whatever reason you - dear committer - somehow need to get the
8deca6c9 1567 * pid of the dummy intermediate process for do_share_ns() you need to call
0059379f
CB
1568 * lxc_raw_getpid(). The next lxc_raw_clone() call does not employ CLONE_VM and
1569 * will be fine.
8deca6c9
CB
1570 */
1571static inline int do_share_ns(void *arg)
fa3a5b22 1572{
8deca6c9 1573 int i, flags, ret;
fa3a5b22
CB
1574 struct lxc_handler *handler = arg;
1575
8deca6c9
CB
1576 for (i = 0; i < LXC_NS_MAX; i++) {
1577 if (handler->nsfd[i] < 0)
1578 continue;
fa3a5b22 1579
8deca6c9 1580 ret = setns(handler->nsfd[i], 0);
c0b48eff
CB
1581 if (ret < 0) {
1582 /*
1583 * Note that joining a user and/or mount namespace
1584 * requires the process is not multithreaded otherwise
1585 * setns() will fail here.
1586 */
1587 SYSERROR("Failed to inherit %s namespace",
1588 ns_info[i].proc_name);
8deca6c9 1589 return -1;
c0b48eff 1590 }
fa3a5b22 1591
8deca6c9 1592 DEBUG("Inherited %s namespace", ns_info[i].proc_name);
fa3a5b22
CB
1593 }
1594
becad0ec 1595 flags = handler->ns_on_clone_flags;
8deca6c9 1596 flags |= CLONE_PARENT;
33942046
CB
1597 handler->pid = lxc_raw_clone_cb(do_start, handler, CLONE_PIDFD | flags,
1598 &handler->pidfd);
8deca6c9 1599 if (handler->pid < 0)
fa3a5b22
CB
1600 return -1;
1601
8deca6c9 1602 return 0;
fa3a5b22
CB
1603}
1604
480588e6
CB
1605/* lxc_spawn() performs crucial setup tasks and clone()s the new process which
1606 * exec()s the requested container binary.
1607 * Note that lxc_spawn() runs in the parent namespaces. Any operations performed
1608 * right here should be double checked if they'd pose a security risk. (For
1609 * example, any {u}mount() operations performed here will be reflected on the
1610 * host!)
1611 */
74a3920a 1612static int lxc_spawn(struct lxc_handler *handler)
59eb99ba 1613{
f62cf1d4 1614 __do_close int data_sock0 = -EBADF, data_sock1 = -EBADF;
8deca6c9 1615 int i, ret;
08dd2805 1616 char pidstr[20];
57927bf2
CB
1617 bool wants_to_map_ids;
1618 struct lxc_list *id_map;
6e5fc7a5 1619 const char *name = handler->name;
28d9e29e 1620 const char *lxcpath = handler->lxcpath;
2202afc9 1621 bool share_ns = false;
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;
57927bf2 1626 wants_to_map_ids = !lxc_list_empty(id_map);
2f2623ec 1627
28d9e29e 1628 for (i = 0; i < LXC_NS_MAX; i++) {
b074bbf1 1629 if (!conf->ns_share[i])
28d9e29e
CB
1630 continue;
1631
b074bbf1 1632 handler->nsfd[i] = lxc_inherit_namespace(conf->ns_share[i], lxcpath, ns_info[i].proc_name);
28d9e29e
CB
1633 if (handler->nsfd[i] < 0)
1634 return -1;
fa3a5b22 1635
8deca6c9 1636 share_ns = true;
28d9e29e 1637 }
50e98013 1638
cfc62c60
CB
1639 ret = lxc_sync_init(handler);
1640 if (ret < 0)
9d7f9e52 1641 return -1;
0ad19a3f 1642
b9f522c5 1643 ret = socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0,
790255cf 1644 handler->data_sock);
95ef0d7c
CB
1645 if (ret < 0)
1646 goto out_sync_fini;
cdb2a47f
CB
1647 data_sock0 = handler->data_sock[0];
1648 data_sock1 = handler->data_sock[1];
e8bd4e43 1649
5af9369b 1650 ret = resolve_clone_flags(handler);
95ef0d7c
CB
1651 if (ret < 0)
1652 goto out_sync_fini;
9f30a190 1653
03ca4af8
TP
1654 if (handler->ns_clone_flags & CLONE_NEWNET) {
1655 ret = lxc_find_gateway_addresses(handler);
1656 if (ret) {
1657 ERROR("Failed to find gateway addresses");
1658 goto out_sync_fini;
1659 }
1660 }
1661
e8b181f5 1662 if (!cgroup_ops->payload_create(cgroup_ops, handler)) {
cfc62c60 1663 ERROR("Failed creating cgroups");
47d8fb3b
CS
1664 goto out_delete_net;
1665 }
1666
408da065
CB
1667 /* If the rootfs is not a blockdev, prevent the container from marking
1668 * it readonly.
1669 * If the container is unprivileged then skip rootfs pinning.
0c547523 1670 */
0ee35059 1671 if (!wants_to_map_ids) {
28d9e29e 1672 handler->pinfd = pin_rootfs(conf->rootfs.path);
b87ee312 1673 if (handler->pinfd == -EBADF)
cfc62c60 1674 INFO("Failed to pin the rootfs for container \"%s\"", handler->name);
5e32a990 1675 }
0c547523 1676
408da065 1677 /* Create a process in a new set of namespaces. */
4e232466
CB
1678 if (share_ns) {
1679 pid_t attacher_pid;
1680
cfc62c60 1681 attacher_pid = lxc_clone(do_share_ns, handler,
33258b95 1682 CLONE_VFORK | CLONE_VM | CLONE_FILES, NULL);
4e232466
CB
1683 if (attacher_pid < 0) {
1684 SYSERROR(LXC_CLONE_ERROR);
1685 goto out_delete_net;
1686 }
1687
1688 ret = wait_for_pid(attacher_pid);
1689 if (ret < 0) {
1690 SYSERROR("Intermediate process failed");
1691 goto out_delete_net;
1692 }
f7176c3e
CB
1693
1694 if (handler->pid < 0) {
1695 SYSERROR(LXC_CLONE_ERROR);
1696 goto out_delete_net;
1697 }
4e232466 1698 } else {
84b66ced 1699 int cgroup_fd = -EBADF;
f7176c3e
CB
1700
1701 struct lxc_clone_args clone_args = {
1702 .flags = handler->clone_flags,
1703 .pidfd = ptr_to_u64(&handler->pidfd),
1704 .exit_signal = SIGCHLD,
1705 };
1706
1707 if (handler->ns_clone_flags & CLONE_NEWCGROUP) {
1708 cgroup_fd = cgroup_unified_fd(cgroup_ops);
1709 if (cgroup_fd >= 0) {
1710 handler->clone_flags |= CLONE_INTO_CGROUP;
1711 clone_args.flags |= CLONE_INTO_CGROUP;
1712 clone_args.cgroup = cgroup_fd;
1713 }
1714 }
1715
1716 /* Try to spawn directly into target cgroup. */
1717 handler->pid = lxc_clone3(&clone_args, CLONE_ARGS_SIZE_VER2);
1718 if (handler->pid < 0) {
1719 SYSTRACE("Failed to spawn container directly into target cgroup");
1720
1721 /* Kernel might simply be too old for CLONE_INTO_CGROUP. */
1722 handler->clone_flags &= ~(CLONE_INTO_CGROUP | CLONE_NEWCGROUP);
1723 handler->ns_on_clone_flags &= ~CLONE_NEWCGROUP;
1724 handler->ns_unshare_flags |= CLONE_NEWCGROUP;
1725
1726 clone_args.flags = handler->clone_flags;
1727
1728 handler->pid = lxc_clone3(&clone_args, CLONE_ARGS_SIZE_VER0);
1729 } else if (cgroup_fd >= 0) {
1730 TRACE("Spawned container directly into target cgroup via cgroup2 fd %d", cgroup_fd);
1731 }
1732
1733 /* Kernel might be too old for clone3(). */
1734 if (handler->pid < 0) {
1735 SYSTRACE("Failed to spawn container via clone3()");
1736 handler->pid = lxc_raw_legacy_clone(handler->clone_flags, &handler->pidfd);
1737 }
1738
1739 if (handler->pid < 0) {
1740 SYSERROR(LXC_CLONE_ERROR);
1741 goto out_delete_net;
1742 }
1743
1744 if (handler->pid == 0) {
1745 (void)do_start(handler);
1746 _exit(EXIT_FAILURE);
1747 }
0ad19a3f 1748 }
f7176c3e
CB
1749 if (handler->pidfd < 0)
1750 handler->clone_flags &= ~CLONE_PIDFD;
fa3a5b22 1751 TRACE("Cloned child process %d", handler->pid);
1bd8d726 1752
39293f22
CB
1753 /* Verify that we can actually make use of pidfds. */
1754 if (!lxc_can_use_pidfd(handler->pidfd))
1755 close_prot_errno_disarm(handler->pidfd);
1756
1871e646
CB
1757 ret = snprintf(pidstr, 20, "%d", handler->pid);
1758 if (ret < 0 || ret >= 20)
1759 goto out_delete_net;
1760
1761 ret = setenv("LXC_PID", pidstr, 1);
1762 if (ret < 0)
1763 SYSERROR("Failed to set environment variable: LXC_PID=%s", pidstr);
1764
9662e444 1765 for (i = 0; i < LXC_NS_MAX; i++)
becad0ec 1766 if (handler->ns_on_clone_flags & ns_info[i].clone_flag)
28d9e29e 1767 INFO("Cloned %s", ns_info[i].flag_name);
0ad19a3f 1768
4cb53844 1769 if (!lxc_try_preserve_namespaces(handler, handler->ns_on_clone_flags, handler->pid)) {
28d9e29e
CB
1770 ERROR("Failed to preserve cloned namespaces for lxc.hook.stop");
1771 goto out_delete_net;
1772 }
b6b2b194 1773
3c22086f
CLG
1774 lxc_sync_fini_child(handler);
1775
4d8bdfa0
CB
1776 if (lxc_abstract_unix_send_fds(handler->data_sock[0], &handler->monitor_status_fd, 1, NULL, 0) < 0) {
1777 ERROR("Failed to send status file descriptor to child process");
1778 goto out_delete_net;
1779 }
1780 close_prot_errno_disarm(handler->monitor_status_fd);
1781
408da065
CB
1782 /* Map the container uids. The container became an invalid userid the
1783 * moment it was cloned with CLONE_NEWUSER. This call doesn't change
1784 * anything immediately, but allows the container to setuid(0) (0 being
1785 * mapped to something else on the host.) later to become a valid uid
1786 * again.
1787 */
fa3a5b22 1788 if (wants_to_map_ids) {
df3c5314 1789 if (!handler->conf->ns_share[LXC_NS_USER] &&
46186acd 1790 (handler->conf->ns_keep & CLONE_NEWUSER) == 0) {
fa3a5b22
CB
1791 ret = lxc_map_ids(id_map, handler->pid);
1792 if (ret < 0) {
1793 ERROR("Failed to set up id mapping.");
1794 goto out_delete_net;
1795 }
1796 }
5b1e83cb
SH
1797 }
1798
cfc62c60
CB
1799 ret = lxc_sync_wake_child(handler, LXC_SYNC_STARTUP);
1800 if (ret < 0)
5b1e83cb 1801 goto out_delete_net;
5b1e83cb 1802
cfc62c60
CB
1803 ret = lxc_sync_wait_child(handler, LXC_SYNC_CONFIGURE);
1804 if (ret < 0)
5b1e83cb 1805 goto out_delete_net;
0ad19a3f 1806
c581d2a6 1807 if (!cgroup_ops->setup_limits_legacy(cgroup_ops, handler->conf, false)) {
cfc62c60 1808 ERROR("Failed to setup cgroup limits for container \"%s\"", name);
6031a6e5
DE
1809 goto out_delete_net;
1810 }
1811
fe70edee 1812 if (!cgroup_ops->payload_enter(cgroup_ops, handler)) {
b1ee458e 1813 ERROR("Failed to enter cgroups");
7fef7a06 1814 goto out_delete_net;
fe70edee 1815 }
218d4250 1816
c581d2a6
CB
1817 if (!cgroup_ops->payload_delegate_controllers(cgroup_ops)) {
1818 ERROR("Failed to delegate controllers to payload cgroup");
1819 goto out_delete_net;
1820 }
1821
1822 if (!cgroup_ops->setup_limits(cgroup_ops, handler)) {
1823 ERROR("Failed to setup cgroup limits for container \"%s\"", name);
1824 goto out_delete_net;
1825 }
1826
2202afc9 1827 if (!cgroup_ops->chown(cgroup_ops, handler->conf))
0996e18a
SH
1828 goto out_delete_net;
1829
aa0c0e7b
RK
1830 /* If not done yet, we're now ready to preserve the network namespace */
1831 if (handler->nsfd[LXC_NS_NET] < 0) {
1832 ret = lxc_try_preserve_ns(handler->pid, "net");
1833 if (ret < 0) {
1834 if (ret != -EOPNOTSUPP) {
1835 SYSERROR("Failed to preserve net namespace");
1836 goto out_delete_net;
1837 }
1838 } else {
1839 handler->nsfd[LXC_NS_NET] = ret;
1840 DEBUG("Preserved net namespace via fd %d", ret);
4cb53844 1841 }
fa3a5b22 1842 }
aa0c0e7b
RK
1843 ret = lxc_netns_set_nsid(handler->nsfd[LXC_NS_NET]);
1844 if (ret < 0)
1845 SYSWARN("Failed to allocate new network namespace id");
1846 else
1847 TRACE("Allocated new network namespace id");
fa3a5b22 1848
408da065 1849 /* Create the network configuration. */
becad0ec 1850 if (handler->ns_clone_flags & CLONE_NEWNET) {
e389f2af 1851 ret = lxc_create_network(handler);
cfc62c60 1852 if (ret < 0) {
e389f2af 1853 ERROR("Failed to create the network");
7fef7a06 1854 goto out_delete_net;
82d5ae15 1855 }
74c6e2b0 1856
3c09b97c 1857 ret = lxc_network_send_to_child(handler);
cfc62c60 1858 if (ret < 0) {
e389f2af 1859 ERROR("Failed to send veth names to child");
74c6e2b0
CB
1860 goto out_delete_net;
1861 }
0ad19a3f 1862 }
1863
61d7a733
YT
1864 if (!lxc_list_empty(&conf->procs)) {
1865 ret = setup_proc_filesystem(&conf->procs, handler->pid);
1866 if (ret < 0)
1867 goto out_delete_net;
1868 }
1869
408da065
CB
1870 /* Tell the child to continue its initialization. We'll get
1871 * LXC_SYNC_CGROUP when it is ready for us to setup cgroups.
3c22086f 1872 */
cfc62c60
CB
1873 ret = lxc_sync_barrier_child(handler, LXC_SYNC_POST_CONFIGURE);
1874 if (ret < 0)
544a48a0
SH
1875 goto out_delete_net;
1876
cfc62c60
CB
1877 if (!lxc_list_empty(&conf->limits)) {
1878 ret = setup_resource_limits(&conf->limits, handler->pid);
1879 if (ret < 0) {
1880 ERROR("Failed to setup resource limits");
1881 goto out_delete_net;
1882 }
c6d09e15
WB
1883 }
1884
cfc62c60
CB
1885 ret = lxc_sync_barrier_child(handler, LXC_SYNC_CGROUP_UNSHARE);
1886 if (ret < 0)
f4152036
CB
1887 goto out_delete_net;
1888
432faf20
WB
1889 /*
1890 * with isolation the limiting devices cgroup was already setup, so
1891 * only setup devices here if we have no namespace directory
1892 */
1893 if (!handler->conf->cgroup_meta.namespace_dir &&
1894 !cgroup_ops->setup_limits_legacy(cgroup_ops, handler->conf, true)) {
cfc62c60 1895 ERROR("Failed to setup legacy device cgroup controller limits");
b98f7d6e 1896 goto out_delete_net;
544a48a0 1897 }
cfc62c60 1898 TRACE("Set up legacy device cgroup controller limits");
544a48a0 1899
bf651989
CB
1900 if (!cgroup_ops->devices_activate(cgroup_ops, handler)) {
1901 ERROR("Failed to setup cgroup2 device controller limits");
1902 goto out_delete_net;
1903 }
1904 TRACE("Set up cgroup2 device controller limits");
1905
f7176c3e 1906 if (handler->ns_unshare_flags & CLONE_NEWCGROUP) {
8bf3abfb 1907 /* Now we're ready to preserve the cgroup namespace */
4cb53844 1908 ret = lxc_try_preserve_ns(handler->pid, "cgroup");
8bf3abfb 1909 if (ret < 0) {
4cb53844 1910 if (ret != -EOPNOTSUPP) {
6d1400b5 1911 SYSERROR("Failed to preserve cgroup namespace");
4cb53844
CB
1912 goto out_delete_net;
1913 }
1914 } else {
1915 handler->nsfd[LXC_NS_CGROUP] = ret;
1916 DEBUG("Preserved cgroup namespace via fd %d", ret);
8bf3abfb 1917 }
8bf3abfb
CB
1918 }
1919
78eb6aa6
CB
1920 cgroup_ops->payload_finalize(cgroup_ops);
1921 TRACE("Finished setting up cgroups");
1922
f7176c3e 1923 if (handler->ns_unshare_flags & CLONE_NEWTIME) {
f1c43439
CB
1924 /* Now we're ready to preserve the cgroup namespace */
1925 ret = lxc_try_preserve_ns(handler->pid, "time");
1926 if (ret < 0) {
1927 if (ret != -EOPNOTSUPP) {
1928 SYSERROR("Failed to preserve time namespace");
1929 goto out_delete_net;
1930 }
1931 } else {
1932 handler->nsfd[LXC_NS_TIME] = ret;
1933 DEBUG("Preserved time namespace via fd %d", ret);
1934 }
1935 }
1936
08dd2805 1937 /* Run any host-side start hooks */
cfc62c60
CB
1938 ret = run_lxc_hooks(name, "start-host", conf, NULL);
1939 if (ret < 0) {
1940 ERROR("Failed to run lxc.hook.start-host");
341ed84c 1941 goto out_delete_net;
08dd2805
SH
1942 }
1943
408da065
CB
1944 /* Tell the child to complete its initialization and wait for it to exec
1945 * or return an error. (The child will never return
08dd2805 1946 * LXC_SYNC_READY_START+1. It will either close the sync pipe, causing
408da065
CB
1947 * lxc_sync_barrier_child to return success, or return a different
1948 * value, causing us to error out).
544a48a0 1949 */
cfc62c60
CB
1950 ret = lxc_sync_barrier_child(handler, LXC_SYNC_READY_START);
1951 if (ret < 0)
341ed84c 1952 goto out_delete_net;
0ad19a3f 1953
e389f2af
CB
1954 if (handler->ns_clone_flags & CLONE_NEWNET) {
1955 ret = lxc_network_recv_name_and_ifindex_from_child(handler);
1956 if (ret < 0) {
1957 ERROR("Failed to receive names and ifindices for network devices from child");
1958 goto out_delete_net;
1959 }
790255cf
CB
1960 }
1961
1962 /* Now all networks are created, network devices are moved into place,
ad2ddfcd 1963 * and the correct names and ifindices in the respective namespaces have
790255cf
CB
1964 * been recorded. The corresponding structs have now all been filled. So
1965 * log them for debugging purposes.
1966 */
28d9e29e 1967 lxc_log_configured_netdevs(conf);
790255cf 1968
f4152036 1969 /* Read tty fds allocated by child. */
cfc62c60
CB
1970 ret = lxc_recv_ttys_from_child(handler);
1971 if (ret < 0) {
1972 ERROR("Failed to receive tty info from child process");
f4152036
CB
1973 goto out_delete_net;
1974 }
1975
c3e3c21a
CB
1976 ret = lxc_seccomp_recv_notifier_fd(&handler->conf->seccomp, data_sock1);
1977 if (ret < 0) {
1978 SYSERROR("Failed to receive seccomp notify fd from child");
1979 goto out_delete_net;
cdb2a47f 1980 }
cdb2a47f 1981
cfc62c60
CB
1982 ret = handler->ops->post_start(handler, handler->data);
1983 if (ret < 0)
e6126dbe
MN
1984 goto out_abort;
1985
cfc62c60
CB
1986 ret = lxc_set_state(name, handler, RUNNING);
1987 if (ret < 0) {
1988 ERROR("Failed to set state to \"%s\"", lxc_state2str(RUNNING));
59eb99ba 1989 goto out_abort;
3f21c114 1990 }
22ebac19 1991
3c22086f 1992 lxc_sync_fini(handler);
0c547523 1993
e6126dbe 1994 return 0;
1ac470c0 1995
7fef7a06 1996out_delete_net:
becad0ec 1997 if (handler->ns_clone_flags & CLONE_NEWNET)
bb84beda 1998 lxc_delete_network(handler);
1bd8d726 1999
59eb99ba 2000out_abort:
0c5859ff 2001 lxc_abort(handler);
95ef0d7c
CB
2002
2003out_sync_fini:
3c22086f 2004 lxc_sync_fini(handler);
a783a414 2005 close_prot_errno_disarm(handler->pinfd);
5c068da9 2006
b79fcd86 2007 return -1;
59eb99ba 2008}
0ad19a3f 2009
7bdf97d2
CB
2010int __lxc_start(struct lxc_handler *handler, struct lxc_operations *ops,
2011 void *data, const char *lxcpath, bool daemonize, int *error_num)
59eb99ba 2012{
c30e9b19 2013 int ret, status;
7bdf97d2 2014 const char *name = handler->name;
aa460476 2015 struct lxc_conf *conf = handler->conf;
72068e74 2016 struct cgroup_ops *cgroup_ops;
80090207 2017
c30e9b19
CB
2018 ret = lxc_init(name, handler);
2019 if (ret < 0) {
2020 ERROR("Failed to initialize container \"%s\"", name);
9ad66589 2021 goto out_abort;
0ad19a3f 2022 }
ee70bf78
CLG
2023 handler->ops = ops;
2024 handler->data = data;
bb955810 2025 handler->daemonize = daemonize;
72068e74 2026 cgroup_ops = handler->cgroup_ops;
e6126dbe 2027
76a26f55 2028 if (!attach_block_device(handler->conf)) {
c30e9b19 2029 ERROR("Failed to attach block device");
575ea467 2030 ret = -1;
9ad66589 2031 goto out_abort;
76a26f55
SH
2032 }
2033
72068e74
CB
2034 if (!cgroup_ops->monitor_create(cgroup_ops, handler)) {
2035 ERROR("Failed to create monitor cgroup");
575ea467 2036 ret = -1;
9ad66589 2037 goto out_abort;
eeef32bb
CB
2038 }
2039
c581d2a6 2040 if (!cgroup_ops->monitor_enter(cgroup_ops, handler)) {
eeef32bb 2041 ERROR("Failed to enter monitor cgroup");
575ea467 2042 ret = -1;
9ad66589 2043 goto out_abort;
72068e74
CB
2044 }
2045
c581d2a6
CB
2046 if (!cgroup_ops->monitor_delegate_controllers(cgroup_ops)) {
2047 ERROR("Failed to delegate controllers to monitor cgroup");
2048 ret = -1;
9ad66589 2049 goto out_abort;
c581d2a6
CB
2050 }
2051
35120d9c 2052 if (geteuid() == 0 && !lxc_list_empty(&conf->id_map)) {
408da065 2053 /* If the backing store is a device, mount it here and now. */
35120d9c 2054 if (rootfs_is_blockdev(conf)) {
c30e9b19
CB
2055 ret = unshare(CLONE_NEWNS);
2056 if (ret < 0) {
2057 ERROR("Failed to unshare CLONE_NEWNS");
9ad66589 2058 goto out_abort;
35120d9c 2059 }
c30e9b19 2060 INFO("Unshared CLONE_NEWNS");
408da065 2061
9e61fb1f 2062 turn_into_dependent_mounts();
8ce1abc2 2063 ret = lxc_setup_rootfs_prepare_root(conf, name, lxcpath);
c30e9b19
CB
2064 if (ret < 0) {
2065 ERROR("Error setting up rootfs mount as root before spawn");
9ad66589 2066 goto out_abort;
35120d9c 2067 }
c30e9b19 2068 INFO("Set up container rootfs as host root");
35120d9c
SH
2069 }
2070 }
2071
c30e9b19
CB
2072 ret = lxc_spawn(handler);
2073 if (ret < 0) {
2074 ERROR("Failed to spawn container \"%s\"", name);
76a26f55 2075 goto out_detach_blockdev;
0ad19a3f 2076 }
2077
80308d07 2078 handler->conf->reboot = REBOOT_NONE;
8bee8851 2079
c30e9b19
CB
2080 ret = lxc_poll(name, handler);
2081 if (ret) {
2082 ERROR("LXC mainloop exited with error: %d", ret);
9ad66589 2083 goto out_delete_network;
59eb99ba 2084 }
0ad19a3f 2085
321db026
DJ
2086 if (!handler->init_died && handler->pid > 0) {
2087 ERROR("Child process is not killed");
575ea467 2088 ret = -1;
9ad66589 2089 goto out_delete_network;
321db026
DJ
2090 }
2091
c30e9b19
CB
2092 status = lxc_wait_for_pid_status(handler->pid);
2093 if (status < 0)
2094 SYSERROR("Failed to retrieve status for %d", handler->pid);
e043236e 2095
408da065
CB
2096 /* If the child process exited but was not signaled, it didn't call
2097 * reboot. This should mean it was an lxc-execute which simply exited.
2098 * In any case, treat it as a 'halt'.
8b004f07 2099 */
d028235d 2100 if (WIFSIGNALED(status)) {
8b004f07
SH
2101 switch(WTERMSIG(status)) {
2102 case SIGINT: /* halt */
c30e9b19 2103 DEBUG("Container \"%s\" is halting", name);
8b004f07
SH
2104 break;
2105 case SIGHUP: /* reboot */
c30e9b19 2106 DEBUG("Container \"%s\" is rebooting", name);
80308d07 2107 handler->conf->reboot = REBOOT_REQ;
8b004f07 2108 break;
c2b9bd9e 2109 case SIGSYS: /* seccomp */
c30e9b19 2110 DEBUG("Container \"%s\" violated its seccomp policy", name);
c2b9bd9e 2111 break;
8b004f07 2112 default:
c30e9b19 2113 DEBUG("Unknown exit status for container \"%s\" init %d", name, WTERMSIG(status));
8b004f07
SH
2114 break;
2115 }
d028235d 2116 }
828695d9 2117
c30e9b19
CB
2118 ret = lxc_restore_phys_nics_to_netns(handler);
2119 if (ret < 0)
818a57fc 2120 ERROR("Failed to move physical network devices back to parent network namespace");
ce5782df 2121
a783a414 2122 close_prot_errno_disarm(handler->pinfd);
5c068da9 2123
1787abca 2124 lxc_monitor_send_exit_code(name, status, handler->lxcpath);
eb808539 2125 lxc_error_set_and_log(handler->pid, status);
a3b4f3d6
TA
2126 if (error_num)
2127 *error_num = handler->exit_status;
1bd8d726 2128
9ad66589
CB
2129/* These are not the droids you are looking for. */
2130__private_goto1:
bb84beda 2131 lxc_delete_network(handler);
74a2b586 2132
9ad66589 2133__private_goto2:
76a26f55
SH
2134 detach_block_device(handler->conf);
2135
9ad66589 2136__private_goto3:
fd5be714 2137 lxc_end(handler);
65a2dc8b 2138
c30e9b19 2139 return ret;
0ad19a3f 2140
9ad66589
CB
2141/* These are the droids you are looking for. */
2142out_abort:
0c5859ff 2143 lxc_abort(handler);
9ad66589 2144 goto __private_goto3;
65a2dc8b
CB
2145
2146out_detach_blockdev:
0c5859ff 2147 lxc_abort(handler);
9ad66589 2148 goto __private_goto2;
65a2dc8b 2149
9ad66589 2150out_delete_network:
0c5859ff 2151 lxc_abort(handler);
9ad66589 2152 goto __private_goto1;
0ad19a3f 2153}
ee70bf78
CLG
2154
2155struct start_args {
2156 char *const *argv;
2157};
2158
2159static int start(struct lxc_handler *handler, void* data)
2160{
2161 struct start_args *arg = data;
2162
984984e4 2163 NOTICE("Exec'ing \"%s\"", arg->argv[0]);
ee70bf78
CLG
2164
2165 execvp(arg->argv[0], arg->argv);
984984e4 2166 SYSERROR("Failed to exec \"%s\"", arg->argv[0]);
ee70bf78
CLG
2167 return 0;
2168}
2169
2170static int post_start(struct lxc_handler *handler, void* data)
2171{
2172 struct start_args *arg = data;
2173
4c8e880e 2174 NOTICE("Started \"%s\" with pid \"%d\"", arg->argv[0], handler->pid);
ee70bf78
CLG
2175 return 0;
2176}
2177
2178static struct lxc_operations start_ops = {
2179 .start = start,
2180 .post_start = post_start
2181};
2182
7bdf97d2 2183int lxc_start(char *const argv[], struct lxc_handler *handler,
bb955810 2184 const char *lxcpath, bool daemonize, int *error_num)
ee70bf78
CLG
2185{
2186 struct start_args start_arg = {
2187 .argv = argv,
2188 };
2189
b2efeb0b 2190 TRACE("Doing lxc_start");
7bdf97d2 2191 return __lxc_start(handler, &start_ops, &start_arg, lxcpath, daemonize, error_num);
ee70bf78 2192}
28272964
CB
2193
2194static void lxc_destroy_container_on_signal(struct lxc_handler *handler,
2195 const char *name)
2196{
245100a0 2197 char destroy[PATH_MAX];
f01f7975 2198 struct lxc_container *c;
ae3beac9
CB
2199 int ret = 0;
2200 bool bret = true;
2201
df31363a 2202 if (handler->conf->rootfs.path && handler->conf->rootfs.mount) {
790255cf 2203 bret = do_destroy_container(handler);
28272964 2204 if (!bret) {
ae3beac9 2205 ERROR("Error destroying rootfs for container \"%s\"", name);
28272964
CB
2206 return;
2207 }
2208 }
ae3beac9 2209 INFO("Destroyed rootfs for container \"%s\"", name);
28272964 2210
245100a0
CB
2211 ret = snprintf(destroy, PATH_MAX, "%s/%s", handler->lxcpath, name);
2212 if (ret < 0 || ret >= PATH_MAX) {
ae3beac9 2213 ERROR("Error destroying directory for container \"%s\"", name);
28272964
CB
2214 return;
2215 }
2216
f01f7975
CB
2217 c = lxc_container_new(name, handler->lxcpath);
2218 if (c) {
2219 if (container_disk_lock(c)) {
ae3beac9 2220 INFO("Could not update lxc_snapshots file");
f01f7975
CB
2221 lxc_container_put(c);
2222 } else {
2223 mod_all_rdeps(c, false);
2224 container_disk_unlock(c);
2225 lxc_container_put(c);
2226 }
2227 }
2228
d0fbc7ba 2229 if (!handler->am_root)
94b0a3ac
CB
2230 ret = userns_exec_full(handler->conf, lxc_rmdir_onedev_wrapper,
2231 destroy, "lxc_rmdir_onedev_wrapper");
28272964
CB
2232 else
2233 ret = lxc_rmdir_onedev(destroy, NULL);
2234
2235 if (ret < 0) {
ae3beac9 2236 ERROR("Error destroying directory for container \"%s\"", name);
28272964
CB
2237 return;
2238 }
ae3beac9 2239 INFO("Destroyed directory for container \"%s\"", name);
28272964
CB
2240}
2241
2242static int lxc_rmdir_onedev_wrapper(void *data)
2243{
2244 char *arg = (char *) data;
2245 return lxc_rmdir_onedev(arg, NULL);
2246}
2247
134df645
CB
2248static bool do_destroy_container(struct lxc_handler *handler)
2249{
94b0a3ac
CB
2250 int ret;
2251
d0fbc7ba 2252 if (!handler->am_root) {
94b0a3ac
CB
2253 ret = userns_exec_full(handler->conf, storage_destroy_wrapper,
2254 handler->conf, "storage_destroy_wrapper");
2255 if (ret < 0)
d028235d 2256 return false;
10bc1861 2257
d028235d
SG
2258 return true;
2259 }
10bc1861 2260
790255cf 2261 return storage_destroy(handler->conf);
28272964 2262}