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