]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * lxc: linux Container library | |
3 | * | |
4 | * (C) Copyright IBM Corp. 2007, 2008 | |
5 | * | |
6 | * Authors: | |
7 | * Daniel Lezcano <daniel.lezcano at free.fr> | |
8 | * Serge Hallyn <serge@hallyn.com> | |
9 | * Christian Brauner <christian.brauner@ubuntu.com> | |
10 | * | |
11 | * This library is free software; you can redistribute it and/or | |
12 | * modify it under the terms of the GNU Lesser General Public | |
13 | * License as published by the Free Software Foundation; either | |
14 | * version 2.1 of the License, or (at your option) any later version. | |
15 | * | |
16 | * This library is distributed in the hope that it will be useful, | |
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
19 | * Lesser General Public License for more details. | |
20 | * | |
21 | * You should have received a copy of the GNU Lesser General Public | |
22 | * License along with this library; if not, write to the Free Software | |
23 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
24 | */ | |
25 | ||
26 | #define _GNU_SOURCE | |
27 | #include "config.h" | |
28 | ||
29 | #include <alloca.h> | |
30 | #include <dirent.h> | |
31 | #include <errno.h> | |
32 | #include <fcntl.h> | |
33 | #include <grp.h> | |
34 | #include <poll.h> | |
35 | #include <signal.h> | |
36 | #include <stdio.h> | |
37 | #include <stdlib.h> | |
38 | #include <string.h> | |
39 | #include <unistd.h> | |
40 | #include <sys/file.h> | |
41 | #include <sys/mount.h> | |
42 | #include <sys/param.h> | |
43 | #include <sys/prctl.h> | |
44 | #include <sys/socket.h> | |
45 | #include <sys/stat.h> | |
46 | #include <sys/syscall.h> | |
47 | #include <sys/types.h> | |
48 | #include <sys/un.h> | |
49 | #include <sys/wait.h> | |
50 | ||
51 | #if HAVE_LIBCAP | |
52 | #include <sys/capability.h> | |
53 | #endif | |
54 | ||
55 | #if !HAVE_DECL_PR_CAPBSET_DROP | |
56 | #define PR_CAPBSET_DROP 24 | |
57 | #endif | |
58 | ||
59 | #if !HAVE_DECL_PR_SET_NO_NEW_PRIVS | |
60 | #define PR_SET_NO_NEW_PRIVS 38 | |
61 | #endif | |
62 | ||
63 | #if !HAVE_DECL_PR_GET_NO_NEW_PRIVS | |
64 | #define PR_GET_NO_NEW_PRIVS 39 | |
65 | #endif | |
66 | ||
67 | #include "af_unix.h" | |
68 | #include "caps.h" | |
69 | #include "cgroup.h" | |
70 | #include "commands.h" | |
71 | #include "commands_utils.h" | |
72 | #include "conf.h" | |
73 | #include "confile_utils.h" | |
74 | #include "console.h" | |
75 | #include "error.h" | |
76 | #include "list.h" | |
77 | #include "log.h" | |
78 | #include "lxccontainer.h" | |
79 | #include "lxclock.h" | |
80 | #include "lxcseccomp.h" | |
81 | #include "mainloop.h" | |
82 | #include "monitor.h" | |
83 | #include "namespace.h" | |
84 | #include "network.h" | |
85 | #include "start.h" | |
86 | #include "storage.h" | |
87 | #include "storage_utils.h" | |
88 | #include "sync.h" | |
89 | #include "utils.h" | |
90 | #include "lsm/lsm.h" | |
91 | ||
92 | lxc_log_define(lxc_start, lxc); | |
93 | ||
94 | extern void mod_all_rdeps(struct lxc_container *c, bool inc); | |
95 | static bool do_destroy_container(struct lxc_handler *handler); | |
96 | static int lxc_rmdir_onedev_wrapper(void *data); | |
97 | static void lxc_destroy_container_on_signal(struct lxc_handler *handler, | |
98 | const char *name); | |
99 | ||
100 | static void print_top_failing_dir(const char *path) | |
101 | { | |
102 | size_t len = strlen(path); | |
103 | char *copy = alloca(len + 1), *p, *e, saved; | |
104 | strcpy(copy, path); | |
105 | ||
106 | p = copy; | |
107 | e = copy + len; | |
108 | while (p < e) { | |
109 | while (p < e && *p == '/') | |
110 | p++; | |
111 | while (p < e && *p != '/') | |
112 | p++; | |
113 | saved = *p; | |
114 | *p = '\0'; | |
115 | if (access(copy, X_OK)) { | |
116 | SYSERROR("Could not access %s. Please grant it x " | |
117 | "access, or add an ACL for the container " | |
118 | "root.", copy); | |
119 | return; | |
120 | } | |
121 | *p = saved; | |
122 | } | |
123 | } | |
124 | ||
125 | static void close_ns(int ns_fd[LXC_NS_MAX]) | |
126 | { | |
127 | int i; | |
128 | ||
129 | for (i = 0; i < LXC_NS_MAX; i++) { | |
130 | if (ns_fd[i] > -1) { | |
131 | close(ns_fd[i]); | |
132 | ns_fd[i] = -1; | |
133 | } | |
134 | } | |
135 | } | |
136 | ||
137 | /* preserve_ns: open /proc/@pid/ns/@ns for each namespace specified | |
138 | * in clone_flags. | |
139 | * Return true on success, false on failure. | |
140 | */ | |
141 | static bool preserve_ns(int ns_fd[LXC_NS_MAX], int clone_flags, pid_t pid) | |
142 | { | |
143 | int i, ret; | |
144 | ||
145 | for (i = 0; i < LXC_NS_MAX; i++) | |
146 | ns_fd[i] = -1; | |
147 | ||
148 | ret = lxc_preserve_ns(pid, ""); | |
149 | if (ret < 0) { | |
150 | SYSERROR("Kernel does not support attaching to namespaces."); | |
151 | return false; | |
152 | } else { | |
153 | close(ret); | |
154 | } | |
155 | ||
156 | for (i = 0; i < LXC_NS_MAX; i++) { | |
157 | if ((clone_flags & ns_info[i].clone_flag) == 0) | |
158 | continue; | |
159 | ||
160 | ns_fd[i] = lxc_preserve_ns(pid, ns_info[i].proc_name); | |
161 | if (ns_fd[i] < 0) | |
162 | goto error; | |
163 | ||
164 | DEBUG("Preserved %s namespace via fd %d", ns_info[i].proc_name, ns_fd[i]); | |
165 | } | |
166 | ||
167 | return true; | |
168 | ||
169 | error: | |
170 | if (errno == ENOENT) | |
171 | SYSERROR("Kernel does not support attaching to %s namespaces.", ns_info[i].proc_name); | |
172 | else | |
173 | SYSERROR("Failed to open file descriptor for %s namespace: %s.", ns_info[i].proc_name, strerror(errno)); | |
174 | close_ns(ns_fd); | |
175 | return false; | |
176 | } | |
177 | ||
178 | static int match_fd(int fd) | |
179 | { | |
180 | return (fd == 0 || fd == 1 || fd == 2); | |
181 | } | |
182 | ||
183 | int lxc_check_inherited(struct lxc_conf *conf, bool closeall, | |
184 | int *fds_to_ignore, size_t len_fds) | |
185 | { | |
186 | struct dirent *direntp; | |
187 | int fd, fddir; | |
188 | size_t i; | |
189 | DIR *dir; | |
190 | ||
191 | if (conf && conf->close_all_fds) | |
192 | closeall = true; | |
193 | ||
194 | restart: | |
195 | dir = opendir("/proc/self/fd"); | |
196 | if (!dir) { | |
197 | WARN("Failed to open directory: %s.", strerror(errno)); | |
198 | return -1; | |
199 | } | |
200 | ||
201 | fddir = dirfd(dir); | |
202 | ||
203 | while ((direntp = readdir(dir))) { | |
204 | struct lxc_list *cur; | |
205 | bool matched = false; | |
206 | ||
207 | if (!strcmp(direntp->d_name, ".")) | |
208 | continue; | |
209 | ||
210 | if (!strcmp(direntp->d_name, "..")) | |
211 | continue; | |
212 | ||
213 | if (lxc_safe_int(direntp->d_name, &fd) < 0) { | |
214 | INFO("Could not parse file descriptor for: %s", direntp->d_name); | |
215 | continue; | |
216 | } | |
217 | ||
218 | for (i = 0; i < len_fds; i++) | |
219 | if (fds_to_ignore[i] == fd) | |
220 | break; | |
221 | ||
222 | if (fd == fddir || fd == lxc_log_fd || | |
223 | (i < len_fds && fd == fds_to_ignore[i])) | |
224 | continue; | |
225 | ||
226 | /* Keep state clients that wait on reboots. */ | |
227 | if (conf) { | |
228 | lxc_list_for_each(cur, &conf->state_clients) { | |
229 | struct lxc_state_client *client = cur->elem; | |
230 | ||
231 | if (client->clientfd != fd) | |
232 | continue; | |
233 | ||
234 | matched = true; | |
235 | break; | |
236 | } | |
237 | } | |
238 | ||
239 | if (matched) | |
240 | continue; | |
241 | ||
242 | if (current_config && fd == current_config->logfd) | |
243 | continue; | |
244 | ||
245 | if (match_fd(fd)) | |
246 | continue; | |
247 | ||
248 | if (closeall) { | |
249 | close(fd); | |
250 | closedir(dir); | |
251 | INFO("Closed inherited fd %d", fd); | |
252 | goto restart; | |
253 | } | |
254 | WARN("Inherited fd %d", fd); | |
255 | } | |
256 | ||
257 | /* Only enable syslog at this point to avoid the above logging function | |
258 | * to open a new fd and make the check_inherited function enter an | |
259 | * infinite loop. | |
260 | */ | |
261 | lxc_log_enable_syslog(); | |
262 | ||
263 | closedir(dir); /* cannot fail */ | |
264 | return 0; | |
265 | } | |
266 | ||
267 | static int setup_signal_fd(sigset_t *oldmask) | |
268 | { | |
269 | int ret, sig; | |
270 | sigset_t mask; | |
271 | int signals[] = {SIGBUS, SIGILL, SIGSEGV, SIGWINCH}; | |
272 | ||
273 | /* Block everything except serious error signals. */ | |
274 | ret = sigfillset(&mask); | |
275 | if (ret < 0) | |
276 | return -EBADF; | |
277 | ||
278 | for (sig = 0; sig < (sizeof(signals) / sizeof(signals[0])); sig++) { | |
279 | ret = sigdelset(&mask, signals[sig]); | |
280 | if (ret < 0) | |
281 | return -EBADF; | |
282 | } | |
283 | ||
284 | ret = sigprocmask(SIG_BLOCK, &mask, oldmask); | |
285 | if (ret < 0) { | |
286 | SYSERROR("Failed to set signal mask"); | |
287 | return -EBADF; | |
288 | } | |
289 | ||
290 | ret = signalfd(-1, &mask, SFD_CLOEXEC); | |
291 | if (ret < 0) { | |
292 | SYSERROR("Failed to create signal file descriptor"); | |
293 | return -EBADF; | |
294 | } | |
295 | ||
296 | TRACE("Created signal file descriptor %d", ret); | |
297 | ||
298 | return ret; | |
299 | } | |
300 | ||
301 | static int signal_handler(int fd, uint32_t events, void *data, | |
302 | struct lxc_epoll_descr *descr) | |
303 | { | |
304 | int ret; | |
305 | siginfo_t info; | |
306 | struct signalfd_siginfo siginfo; | |
307 | struct lxc_handler *hdlr = data; | |
308 | ||
309 | ret = read(fd, &siginfo, sizeof(siginfo)); | |
310 | if (ret < 0) { | |
311 | ERROR("Failed to read signal info from signal file descriptor: %d.", fd); | |
312 | return -1; | |
313 | } | |
314 | ||
315 | if (ret != sizeof(siginfo)) { | |
316 | ERROR("Unexpected size for siginfo struct."); | |
317 | return -1; | |
318 | } | |
319 | ||
320 | /* Check whether init is running. */ | |
321 | info.si_pid = 0; | |
322 | ret = waitid(P_PID, hdlr->pid, &info, WEXITED | WNOWAIT | WNOHANG); | |
323 | if (ret == 0 && info.si_pid == hdlr->pid) | |
324 | hdlr->init_died = true; | |
325 | ||
326 | /* Try to figure out a reasonable exit status to report. */ | |
327 | if (hdlr->init_died) { | |
328 | switch (info.si_code) { | |
329 | case CLD_EXITED: | |
330 | hdlr->exit_status = info.si_status << 8; | |
331 | break; | |
332 | case CLD_KILLED: | |
333 | case CLD_DUMPED: | |
334 | case CLD_STOPPED: | |
335 | hdlr->exit_status = info.si_status << 8 | 0x7f; | |
336 | break; | |
337 | case CLD_CONTINUED: | |
338 | /* Huh? The waitid() told us it's dead *and* continued? */ | |
339 | WARN("Init %d dead and continued?", hdlr->pid); | |
340 | hdlr->exit_status = 1; | |
341 | break; | |
342 | default: | |
343 | ERROR("Unknown si_code: %d", hdlr->init_died); | |
344 | } | |
345 | } | |
346 | ||
347 | /* More robustness, protect ourself from a SIGCHLD sent | |
348 | * by a process different from the container init. | |
349 | */ | |
350 | if (siginfo.ssi_pid != hdlr->pid) { | |
351 | NOTICE("Received %d from pid %d instead of container init %d.", siginfo.ssi_signo, siginfo.ssi_pid, hdlr->pid); | |
352 | return hdlr->init_died ? LXC_MAINLOOP_CLOSE : 0; | |
353 | } | |
354 | ||
355 | if (siginfo.ssi_signo != SIGCHLD) { | |
356 | kill(hdlr->pid, siginfo.ssi_signo); | |
357 | INFO("Forwarded signal %d to pid %d.", siginfo.ssi_signo, hdlr->pid); | |
358 | return hdlr->init_died ? LXC_MAINLOOP_CLOSE : 0; | |
359 | } | |
360 | ||
361 | if (siginfo.ssi_code == CLD_STOPPED) { | |
362 | INFO("Container init process was stopped."); | |
363 | return hdlr->init_died ? LXC_MAINLOOP_CLOSE : 0; | |
364 | } else if (siginfo.ssi_code == CLD_CONTINUED) { | |
365 | INFO("Container init process was continued."); | |
366 | return hdlr->init_died ? LXC_MAINLOOP_CLOSE : 0; | |
367 | } | |
368 | ||
369 | DEBUG("Container init process %d exited.", hdlr->pid); | |
370 | return LXC_MAINLOOP_CLOSE; | |
371 | } | |
372 | ||
373 | int lxc_serve_state_clients(const char *name, struct lxc_handler *handler, | |
374 | lxc_state_t state) | |
375 | { | |
376 | ssize_t ret; | |
377 | struct lxc_list *cur, *next; | |
378 | struct lxc_state_client *client; | |
379 | struct lxc_msg msg = {.type = lxc_msg_state, .value = state}; | |
380 | ||
381 | process_lock(); | |
382 | if (state == THAWED) | |
383 | handler->state = RUNNING; | |
384 | else | |
385 | handler->state = state; | |
386 | ||
387 | TRACE("Set container state to %s", lxc_state2str(state)); | |
388 | ||
389 | if (lxc_list_empty(&handler->conf->state_clients)) { | |
390 | TRACE("No state clients registered"); | |
391 | process_unlock(); | |
392 | lxc_monitor_send_state(name, state, handler->lxcpath); | |
393 | return 0; | |
394 | } | |
395 | ||
396 | strncpy(msg.name, name, sizeof(msg.name)); | |
397 | msg.name[sizeof(msg.name) - 1] = 0; | |
398 | ||
399 | lxc_list_for_each_safe(cur, &handler->conf->state_clients, next) { | |
400 | client = cur->elem; | |
401 | ||
402 | if (client->states[state] == 0) { | |
403 | TRACE("State %s not registered for state client %d", | |
404 | lxc_state2str(state), client->clientfd); | |
405 | continue; | |
406 | } | |
407 | ||
408 | TRACE("Sending state %s to state client %d", | |
409 | lxc_state2str(state), client->clientfd); | |
410 | ||
411 | again: | |
412 | ret = send(client->clientfd, &msg, sizeof(msg), 0); | |
413 | if (ret <= 0) { | |
414 | if (errno == EINTR) { | |
415 | TRACE("Caught EINTR; retrying"); | |
416 | goto again; | |
417 | } | |
418 | ||
419 | ERROR("%s - Failed to send message to client", | |
420 | strerror(errno)); | |
421 | } | |
422 | ||
423 | /* kick client from list */ | |
424 | close(client->clientfd); | |
425 | lxc_list_del(cur); | |
426 | free(cur->elem); | |
427 | free(cur); | |
428 | } | |
429 | process_unlock(); | |
430 | ||
431 | return 0; | |
432 | } | |
433 | ||
434 | static int lxc_serve_state_socket_pair(const char *name, | |
435 | struct lxc_handler *handler, | |
436 | lxc_state_t state) | |
437 | { | |
438 | ssize_t ret; | |
439 | ||
440 | if (!handler->backgrounded || | |
441 | handler->state_socket_pair[1] < 0 || | |
442 | state == STARTING) | |
443 | return 0; | |
444 | ||
445 | /* Close read end of the socket pair. */ | |
446 | close(handler->state_socket_pair[0]); | |
447 | handler->state_socket_pair[0] = -1; | |
448 | ||
449 | again: | |
450 | ret = lxc_abstract_unix_send_credential(handler->state_socket_pair[1], | |
451 | &(int){state}, sizeof(int)); | |
452 | if (ret != sizeof(int)) { | |
453 | if (errno == EINTR) | |
454 | goto again; | |
455 | SYSERROR("Failed to send state to %d", | |
456 | handler->state_socket_pair[1]); | |
457 | return -1; | |
458 | } | |
459 | ||
460 | TRACE("Sent container state \"%s\" to %d", lxc_state2str(state), | |
461 | handler->state_socket_pair[1]); | |
462 | ||
463 | /* Close write end of the socket pair. */ | |
464 | close(handler->state_socket_pair[1]); | |
465 | handler->state_socket_pair[1] = -1; | |
466 | ||
467 | return 0; | |
468 | } | |
469 | ||
470 | int lxc_set_state(const char *name, struct lxc_handler *handler, | |
471 | lxc_state_t state) | |
472 | { | |
473 | int ret; | |
474 | ||
475 | ret = lxc_serve_state_socket_pair(name, handler, state); | |
476 | if (ret < 0) { | |
477 | ERROR("Failed to synchronize via anonymous pair of unix sockets"); | |
478 | return -1; | |
479 | } | |
480 | ||
481 | ret = lxc_serve_state_clients(name, handler, state); | |
482 | if (ret < 0) | |
483 | return -1; | |
484 | ||
485 | /* This function will try to connect to the legacy lxc-monitord state | |
486 | * server and only exists for backwards compatibility. | |
487 | */ | |
488 | lxc_monitor_send_state(name, state, handler->lxcpath); | |
489 | ||
490 | return 0; | |
491 | } | |
492 | ||
493 | int lxc_poll(const char *name, struct lxc_handler *handler) | |
494 | { | |
495 | int ret; | |
496 | bool has_console = (handler->conf->rootfs.path != NULL); | |
497 | struct lxc_epoll_descr descr, descr_console; | |
498 | ||
499 | ret = lxc_mainloop_open(&descr); | |
500 | if (ret < 0) { | |
501 | ERROR("Failed to create mainloop"); | |
502 | goto out_sigfd; | |
503 | } | |
504 | ||
505 | if (has_console) { | |
506 | ret = lxc_mainloop_open(&descr_console); | |
507 | if (ret < 0) { | |
508 | ERROR("Failed to create console mainloop"); | |
509 | goto out_mainloop; | |
510 | } | |
511 | } | |
512 | ||
513 | ret = lxc_mainloop_add_handler(&descr, handler->sigfd, signal_handler, handler); | |
514 | if (ret < 0) { | |
515 | ERROR("Failed to add signal handler for %d to mainloop", handler->sigfd); | |
516 | goto out_mainloop_console; | |
517 | } | |
518 | ||
519 | if (has_console) { | |
520 | struct lxc_console *console = &handler->conf->console; | |
521 | ||
522 | ret = lxc_console_mainloop_add(&descr, console); | |
523 | if (ret < 0) { | |
524 | ERROR("Failed to add console handlers to mainloop"); | |
525 | goto out_mainloop_console; | |
526 | } | |
527 | ||
528 | ret = lxc_console_mainloop_add(&descr_console, console); | |
529 | if (ret < 0) { | |
530 | ERROR("Failed to add console handlers to console mainloop"); | |
531 | goto out_mainloop_console; | |
532 | } | |
533 | } | |
534 | ||
535 | ret = lxc_cmd_mainloop_add(name, &descr, handler); | |
536 | if (ret < 0) { | |
537 | ERROR("Failed to add command handler to mainloop"); | |
538 | goto out_mainloop_console; | |
539 | } | |
540 | ||
541 | TRACE("Mainloop is ready"); | |
542 | ||
543 | ret = lxc_mainloop(&descr, -1); | |
544 | close(descr.epfd); | |
545 | descr.epfd = -EBADF; | |
546 | if (ret < 0 || !handler->init_died) | |
547 | goto out_mainloop; | |
548 | ||
549 | if (has_console) | |
550 | ret = lxc_mainloop(&descr_console, 0); | |
551 | ||
552 | ||
553 | out_mainloop: | |
554 | lxc_mainloop_close(&descr); | |
555 | TRACE("Closed mainloop"); | |
556 | ||
557 | out_mainloop_console: | |
558 | if (has_console) { | |
559 | lxc_mainloop_close(&descr_console); | |
560 | TRACE("Closed console mainloop"); | |
561 | } | |
562 | ||
563 | out_sigfd: | |
564 | close(handler->sigfd); | |
565 | TRACE("Closed signal file descriptor %d", handler->sigfd); | |
566 | handler->sigfd = -EBADF; | |
567 | ||
568 | return ret; | |
569 | } | |
570 | ||
571 | void lxc_zero_handler(struct lxc_handler *handler) | |
572 | { | |
573 | int i; | |
574 | ||
575 | memset(handler, 0, sizeof(struct lxc_handler)); | |
576 | ||
577 | handler->clone_flags = -1; | |
578 | ||
579 | handler->pinfd = -1; | |
580 | ||
581 | handler->sigfd = -1; | |
582 | ||
583 | for (i = 0; i < LXC_NS_MAX; i++) | |
584 | handler->nsfd[i] = -1; | |
585 | ||
586 | handler->data_sock[0] = -1; | |
587 | handler->data_sock[1] = -1; | |
588 | ||
589 | handler->state_socket_pair[0] = -1; | |
590 | handler->state_socket_pair[1] = -1; | |
591 | ||
592 | handler->sync_sock[0] = -1; | |
593 | handler->sync_sock[1] = -1; | |
594 | } | |
595 | ||
596 | static void lxc_put_nsfds(struct lxc_handler *handler) | |
597 | { | |
598 | int i; | |
599 | ||
600 | for (i = 0; i < LXC_NS_MAX; i++) { | |
601 | if (handler->nsfd[i] < 0) | |
602 | continue; | |
603 | ||
604 | close(handler->nsfd[i]); | |
605 | handler->nsfd[i] = -EBADF; | |
606 | } | |
607 | } | |
608 | ||
609 | void lxc_free_handler(struct lxc_handler *handler) | |
610 | { | |
611 | if (handler->pinfd >= 0) | |
612 | close(handler->pinfd); | |
613 | ||
614 | if (handler->sigfd >= 0) | |
615 | close(handler->sigfd); | |
616 | ||
617 | lxc_put_nsfds(handler); | |
618 | ||
619 | if (handler->conf && handler->conf->reboot == 0) | |
620 | if (handler->conf->maincmd_fd) | |
621 | close(handler->conf->maincmd_fd); | |
622 | ||
623 | if (handler->state_socket_pair[0] >= 0) | |
624 | close(handler->state_socket_pair[0]); | |
625 | ||
626 | if (handler->state_socket_pair[1] >= 0) | |
627 | close(handler->state_socket_pair[1]); | |
628 | ||
629 | handler->conf = NULL; | |
630 | free(handler); | |
631 | handler = NULL; | |
632 | } | |
633 | ||
634 | struct lxc_handler *lxc_init_handler(const char *name, struct lxc_conf *conf, | |
635 | const char *lxcpath, bool daemonize) | |
636 | { | |
637 | int i, ret; | |
638 | struct lxc_handler *handler; | |
639 | ||
640 | handler = malloc(sizeof(*handler)); | |
641 | if (!handler) { | |
642 | ERROR("failed to allocate memory"); | |
643 | return NULL; | |
644 | } | |
645 | ||
646 | memset(handler, 0, sizeof(*handler)); | |
647 | ||
648 | /* Note that am_unpriv() checks the effective uid. We probably don't | |
649 | * care if we are real root only if we are running as root so this | |
650 | * should be fine. | |
651 | */ | |
652 | handler->am_root = !am_unpriv(); | |
653 | handler->data_sock[0] = handler->data_sock[1] = -1; | |
654 | handler->conf = conf; | |
655 | handler->lxcpath = lxcpath; | |
656 | handler->pinfd = -1; | |
657 | handler->sigfd = -EBADF; | |
658 | handler->init_died = false; | |
659 | handler->state_socket_pair[0] = handler->state_socket_pair[1] = -1; | |
660 | if (handler->conf->reboot == 0) | |
661 | lxc_list_init(&handler->conf->state_clients); | |
662 | ||
663 | for (i = 0; i < LXC_NS_MAX; i++) | |
664 | handler->nsfd[i] = -1; | |
665 | ||
666 | handler->name = name; | |
667 | ||
668 | if (daemonize && !handler->conf->reboot) { | |
669 | /* Create socketpair() to synchronize on daemonized startup. | |
670 | * When the container reboots we don't need to synchronize again | |
671 | * currently so don't open another socketpair(). | |
672 | */ | |
673 | ret = socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, | |
674 | handler->state_socket_pair); | |
675 | if (ret < 0) { | |
676 | ERROR("Failed to create anonymous pair of unix sockets"); | |
677 | goto on_error; | |
678 | } | |
679 | TRACE("Created anonymous pair {%d,%d} of unix sockets", | |
680 | handler->state_socket_pair[0], | |
681 | handler->state_socket_pair[1]); | |
682 | } | |
683 | ||
684 | if (handler->conf->reboot == 0) { | |
685 | handler->conf->maincmd_fd = lxc_cmd_init(name, lxcpath, "command"); | |
686 | if (handler->conf->maincmd_fd < 0) { | |
687 | ERROR("Failed to set up command socket"); | |
688 | goto on_error; | |
689 | } | |
690 | } | |
691 | TRACE("Unix domain socket %d for command server is ready", | |
692 | handler->conf->maincmd_fd); | |
693 | ||
694 | return handler; | |
695 | ||
696 | on_error: | |
697 | lxc_free_handler(handler); | |
698 | ||
699 | return NULL; | |
700 | } | |
701 | ||
702 | int lxc_init(const char *name, struct lxc_handler *handler) | |
703 | { | |
704 | int ret; | |
705 | const char *loglevel; | |
706 | struct lxc_conf *conf = handler->conf; | |
707 | ||
708 | lsm_init(); | |
709 | TRACE("initialized LSM"); | |
710 | ||
711 | if (lxc_read_seccomp_config(conf) != 0) { | |
712 | ERROR("Failed loading seccomp policy."); | |
713 | goto out_close_maincmd_fd; | |
714 | } | |
715 | TRACE("read seccomp policy"); | |
716 | ||
717 | /* Begin by setting the state to STARTING. */ | |
718 | if (lxc_set_state(name, handler, STARTING)) { | |
719 | ERROR("Failed to set state for container \"%s\" to \"%s\".", name, lxc_state2str(STARTING)); | |
720 | goto out_close_maincmd_fd; | |
721 | } | |
722 | TRACE("set container state to \"STARTING\""); | |
723 | ||
724 | /* Start of environment variable setup for hooks. */ | |
725 | if (name && setenv("LXC_NAME", name, 1)) | |
726 | SYSERROR("Failed to set environment variable: LXC_NAME=%s.", name); | |
727 | ||
728 | if (conf->rcfile && setenv("LXC_CONFIG_FILE", conf->rcfile, 1)) | |
729 | SYSERROR("Failed to set environment variable: LXC_CONFIG_FILE=%s.", conf->rcfile); | |
730 | ||
731 | if (conf->rootfs.mount && setenv("LXC_ROOTFS_MOUNT", conf->rootfs.mount, 1)) | |
732 | SYSERROR("Failed to set environment variable: LXC_ROOTFS_MOUNT=%s.", conf->rootfs.mount); | |
733 | ||
734 | if (conf->rootfs.path && setenv("LXC_ROOTFS_PATH", conf->rootfs.path, 1)) | |
735 | SYSERROR("Failed to set environment variable: LXC_ROOTFS_PATH=%s.", conf->rootfs.path); | |
736 | ||
737 | if (conf->console.path && setenv("LXC_CONSOLE", conf->console.path, 1)) | |
738 | SYSERROR("Failed to set environment variable: LXC_CONSOLE=%s.", conf->console.path); | |
739 | ||
740 | if (conf->console.log_path && setenv("LXC_CONSOLE_LOGPATH", conf->console.log_path, 1)) | |
741 | SYSERROR("Failed to set environment variable: LXC_CONSOLE_LOGPATH=%s.", conf->console.log_path); | |
742 | ||
743 | if (setenv("LXC_CGNS_AWARE", "1", 1)) | |
744 | SYSERROR("Failed to set environment variable LXC_CGNS_AWARE=1."); | |
745 | ||
746 | loglevel = lxc_log_priority_to_string(lxc_log_get_level()); | |
747 | if (setenv("LXC_LOG_LEVEL", loglevel, 1)) | |
748 | SYSERROR("Failed to set environment variable LXC_LOG_LEVEL=%s", loglevel); | |
749 | ||
750 | if (conf->hooks_version == 0) | |
751 | ret = setenv("LXC_HOOK_VERSION", "0", 1); | |
752 | else | |
753 | ret = setenv("LXC_HOOK_VERSION", "1", 1); | |
754 | if (ret < 0) | |
755 | SYSERROR("Failed to set environment variable LXC_HOOK_VERSION=%u", conf->hooks_version); | |
756 | /* End of environment variable setup for hooks. */ | |
757 | ||
758 | TRACE("set environment variables"); | |
759 | ||
760 | if (run_lxc_hooks(name, "pre-start", conf, NULL)) { | |
761 | ERROR("Failed to run lxc.hook.pre-start for container \"%s\".", name); | |
762 | goto out_aborting; | |
763 | } | |
764 | TRACE("ran pre-start hooks"); | |
765 | ||
766 | /* The signal fd has to be created before forking otherwise if the child | |
767 | * process exits before we setup the signal fd, the event will be lost | |
768 | * and the command will be stuck. | |
769 | */ | |
770 | handler->sigfd = setup_signal_fd(&handler->oldmask); | |
771 | if (handler->sigfd < 0) { | |
772 | ERROR("Failed to setup SIGCHLD fd handler."); | |
773 | goto out_delete_tty; | |
774 | } | |
775 | TRACE("set up signal fd"); | |
776 | ||
777 | /* Do this after setting up signals since it might unblock SIGWINCH. */ | |
778 | ret = lxc_console_create(conf); | |
779 | if (ret < 0) { | |
780 | ERROR("Failed to create console"); | |
781 | goto out_restore_sigmask; | |
782 | } | |
783 | TRACE("Created console"); | |
784 | ||
785 | ret = lxc_pty_map_ids(conf, &conf->console); | |
786 | if (ret < 0) { | |
787 | ERROR("Failed to shift tty into container."); | |
788 | goto out_restore_sigmask; | |
789 | } | |
790 | TRACE("shifted tty ids"); | |
791 | ||
792 | INFO("container \"%s\" is initialized", name); | |
793 | return 0; | |
794 | ||
795 | out_restore_sigmask: | |
796 | sigprocmask(SIG_SETMASK, &handler->oldmask, NULL); | |
797 | out_delete_tty: | |
798 | lxc_delete_tty(&conf->tty_info); | |
799 | out_aborting: | |
800 | lxc_set_state(name, handler, ABORTING); | |
801 | out_close_maincmd_fd: | |
802 | close(conf->maincmd_fd); | |
803 | conf->maincmd_fd = -1; | |
804 | return -1; | |
805 | } | |
806 | ||
807 | void lxc_fini(const char *name, struct lxc_handler *handler) | |
808 | { | |
809 | int i, rc; | |
810 | pid_t self; | |
811 | struct lxc_list *cur, *next; | |
812 | char *namespaces[LXC_NS_MAX + 1]; | |
813 | size_t namespace_count = 0; | |
814 | ||
815 | /* The STOPPING state is there for future cleanup code which can take | |
816 | * awhile. | |
817 | */ | |
818 | lxc_set_state(name, handler, STOPPING); | |
819 | ||
820 | self = lxc_raw_getpid(); | |
821 | for (i = 0; i < LXC_NS_MAX; i++) { | |
822 | if (handler->nsfd[i] < 0) | |
823 | continue; | |
824 | ||
825 | if (handler->conf->hooks_version == 0) | |
826 | rc = asprintf(&namespaces[namespace_count], | |
827 | "%s:/proc/%d/fd/%d", ns_info[i].proc_name, | |
828 | self, handler->nsfd[i]); | |
829 | else | |
830 | rc = asprintf(&namespaces[namespace_count], | |
831 | "/proc/%d/fd/%d", self, handler->nsfd[i]); | |
832 | if (rc == -1) { | |
833 | SYSERROR("Failed to allocate memory."); | |
834 | break; | |
835 | } | |
836 | ||
837 | if (handler->conf->hooks_version == 0) { | |
838 | namespace_count++; | |
839 | continue; | |
840 | } | |
841 | ||
842 | rc = setenv(ns_info[i].env_name, namespaces[namespace_count], 1); | |
843 | if (rc < 0) | |
844 | SYSERROR("Failed to set environment variable %s=%s", | |
845 | ns_info[i].env_name, namespaces[namespace_count]); | |
846 | else | |
847 | TRACE("Set environment variable %s=%s", | |
848 | ns_info[i].env_name, namespaces[namespace_count]); | |
849 | ||
850 | namespace_count++; | |
851 | } | |
852 | namespaces[namespace_count] = NULL; | |
853 | ||
854 | if (handler->conf->reboot && setenv("LXC_TARGET", "reboot", 1)) | |
855 | SYSERROR("Failed to set environment variable: LXC_TARGET=reboot."); | |
856 | ||
857 | if (!handler->conf->reboot && setenv("LXC_TARGET", "stop", 1)) | |
858 | SYSERROR("Failed to set environment variable: LXC_TARGET=stop."); | |
859 | ||
860 | if (handler->conf->hooks_version == 0) | |
861 | rc = run_lxc_hooks(name, "stop", handler->conf, namespaces); | |
862 | else | |
863 | rc = run_lxc_hooks(name, "stop", handler->conf, NULL); | |
864 | ||
865 | while (namespace_count--) | |
866 | free(namespaces[namespace_count]); | |
867 | ||
868 | cgroup_destroy(handler); | |
869 | ||
870 | if (handler->conf->reboot == 0) { | |
871 | /* For all new state clients simply close the command socket. | |
872 | * This will inform all state clients that the container is | |
873 | * STOPPED and also prevents a race between a open()/close() on | |
874 | * the command socket causing a new process to get ECONNREFUSED | |
875 | * because we haven't yet closed the command socket. | |
876 | */ | |
877 | close(handler->conf->maincmd_fd); | |
878 | handler->conf->maincmd_fd = -1; | |
879 | TRACE("Closed command socket"); | |
880 | ||
881 | /* This function will try to connect to the legacy lxc-monitord | |
882 | * state server and only exists for backwards compatibility. | |
883 | */ | |
884 | lxc_monitor_send_state(name, STOPPED, handler->lxcpath); | |
885 | ||
886 | /* The command socket is closed so no one can acces the command | |
887 | * socket anymore so there's no need to lock it. | |
888 | */ | |
889 | handler->state = STOPPED; | |
890 | TRACE("Set container state to \"STOPPED\""); | |
891 | } else { | |
892 | lxc_set_state(name, handler, STOPPED); | |
893 | } | |
894 | ||
895 | if (run_lxc_hooks(name, "post-stop", handler->conf, NULL)) { | |
896 | ERROR("Failed to run lxc.hook.post-stop for container \"%s\".", name); | |
897 | if (handler->conf->reboot) { | |
898 | WARN("Container will be stopped instead of rebooted."); | |
899 | handler->conf->reboot = 0; | |
900 | if (setenv("LXC_TARGET", "stop", 1)) | |
901 | WARN("Failed to set environment variable: LXC_TARGET=stop."); | |
902 | } | |
903 | } | |
904 | ||
905 | /* Reset mask set by setup_signal_fd. */ | |
906 | if (sigprocmask(SIG_SETMASK, &handler->oldmask, NULL)) | |
907 | WARN("Failed to restore signal mask."); | |
908 | ||
909 | lxc_console_delete(&handler->conf->console); | |
910 | lxc_delete_tty(&handler->conf->tty_info); | |
911 | ||
912 | /* The command socket is now closed, no more state clients can register | |
913 | * themselves from now on. So free the list of state clients. | |
914 | */ | |
915 | lxc_list_for_each_safe(cur, &handler->conf->state_clients, next) { | |
916 | struct lxc_state_client *client = cur->elem; | |
917 | ||
918 | /* Keep state clients that want to be notified about reboots. */ | |
919 | if ((handler->conf->reboot > 0) && (client->states[RUNNING] == 2)) | |
920 | continue; | |
921 | ||
922 | /* close state client socket */ | |
923 | close(client->clientfd); | |
924 | lxc_list_del(cur); | |
925 | free(cur->elem); | |
926 | free(cur); | |
927 | } | |
928 | ||
929 | if (handler->conf->ephemeral == 1 && handler->conf->reboot != 1) | |
930 | lxc_destroy_container_on_signal(handler, name); | |
931 | ||
932 | lxc_free_handler(handler); | |
933 | } | |
934 | ||
935 | void lxc_abort(const char *name, struct lxc_handler *handler) | |
936 | { | |
937 | int ret, status; | |
938 | ||
939 | lxc_set_state(name, handler, ABORTING); | |
940 | if (handler->pid > 0) | |
941 | kill(handler->pid, SIGKILL); | |
942 | while ((ret = waitpid(-1, &status, 0)) > 0) { | |
943 | ; | |
944 | } | |
945 | } | |
946 | ||
947 | static int lxc_set_death_signal(int signal) | |
948 | { | |
949 | int ret; | |
950 | pid_t ppid; | |
951 | ||
952 | ret = prctl(PR_SET_PDEATHSIG, signal, 0, 0, 0); | |
953 | ||
954 | /* Check whether we have been orphaned. */ | |
955 | ppid = (pid_t)syscall(SYS_getppid); | |
956 | if (ppid == 1) { | |
957 | pid_t self; | |
958 | ||
959 | self = lxc_raw_getpid(); | |
960 | ret = kill(self, SIGKILL); | |
961 | if (ret < 0) | |
962 | return -1; | |
963 | } | |
964 | ||
965 | if (ret < 0) { | |
966 | SYSERROR("Failed to set PR_SET_PDEATHSIG to %d", signal); | |
967 | return -1; | |
968 | } | |
969 | ||
970 | return 0; | |
971 | } | |
972 | ||
973 | static int do_start(void *data) | |
974 | { | |
975 | int ret; | |
976 | struct lxc_list *iterator; | |
977 | char path[PATH_MAX]; | |
978 | struct lxc_handler *handler = data; | |
979 | bool have_cap_setgid; | |
980 | uid_t new_uid; | |
981 | gid_t new_gid; | |
982 | int devnull_fd = -1; | |
983 | ||
984 | lxc_sync_fini_parent(handler); | |
985 | ||
986 | /* This prctl must be before the synchro, so if the parent dies before | |
987 | * we set the parent death signal, we will detect its death with the | |
988 | * synchro right after, otherwise we have a window where the parent can | |
989 | * exit before we set the pdeath signal leading to a unsupervized | |
990 | * container. | |
991 | */ | |
992 | ret = lxc_set_death_signal(SIGKILL); | |
993 | if (ret < 0) { | |
994 | SYSERROR("Failed to set PR_SET_PDEATHSIG to SIGKILL"); | |
995 | goto out_warn_father; | |
996 | } | |
997 | ||
998 | ret = sigprocmask(SIG_SETMASK, &handler->oldmask, NULL); | |
999 | if (ret < 0) { | |
1000 | SYSERROR("Failed to set signal mask"); | |
1001 | goto out_warn_father; | |
1002 | } | |
1003 | ||
1004 | /* Don't leak the pinfd to the container. */ | |
1005 | if (handler->pinfd >= 0) | |
1006 | close(handler->pinfd); | |
1007 | ||
1008 | ret = lxc_sync_wait_parent(handler, LXC_SYNC_STARTUP); | |
1009 | if (ret < 0) | |
1010 | goto out_warn_father; | |
1011 | ||
1012 | /* Unshare CLONE_NEWNET after CLONE_NEWUSER. See | |
1013 | * https://github.com/lxc/lxd/issues/1978. | |
1014 | */ | |
1015 | if ((handler->clone_flags & (CLONE_NEWNET | CLONE_NEWUSER)) == | |
1016 | (CLONE_NEWNET | CLONE_NEWUSER)) { | |
1017 | ret = unshare(CLONE_NEWNET); | |
1018 | if (ret < 0) { | |
1019 | SYSERROR("Failed to unshare CLONE_NEWNET."); | |
1020 | goto out_warn_father; | |
1021 | } | |
1022 | INFO("Unshared CLONE_NEWNET."); | |
1023 | } | |
1024 | ||
1025 | /* Tell the parent task it can begin to configure the container and wait | |
1026 | * for it to finish. | |
1027 | */ | |
1028 | if (lxc_sync_barrier_parent(handler, LXC_SYNC_CONFIGURE)) | |
1029 | return -1; | |
1030 | ||
1031 | if (lxc_network_recv_veth_names_from_parent(handler) < 0) { | |
1032 | ERROR("Failed to receive veth names from parent"); | |
1033 | goto out_warn_father; | |
1034 | } | |
1035 | ||
1036 | /* If we are in a new user namespace, become root there to have | |
1037 | * privilege over our namespace. | |
1038 | */ | |
1039 | if (!lxc_list_empty(&handler->conf->id_map)) { | |
1040 | uid_t nsuid = (handler->conf->root_nsuid_map != NULL) | |
1041 | ? 0 | |
1042 | : handler->conf->init_uid; | |
1043 | gid_t nsgid = (handler->conf->root_nsgid_map != NULL) | |
1044 | ? 0 | |
1045 | : handler->conf->init_gid; | |
1046 | ||
1047 | ret = lxc_switch_uid_gid(nsuid, nsgid); | |
1048 | if (ret < 0) | |
1049 | goto out_warn_father; | |
1050 | ||
1051 | /* Drop groups only after we switched to a valid gid in the new | |
1052 | * user namespace. | |
1053 | */ | |
1054 | ret = lxc_setgroups(0, NULL); | |
1055 | if (ret < 0 && (handler->am_root || errno != EPERM)) | |
1056 | goto out_warn_father; | |
1057 | ||
1058 | if (!handler->am_root) { | |
1059 | ret = prctl(PR_SET_DUMPABLE, 1, 0, 0, 0); | |
1060 | if (ret < 0) | |
1061 | goto out_warn_father; | |
1062 | } | |
1063 | ||
1064 | /* set{g,u}id() clears deathsignal */ | |
1065 | ret = lxc_set_death_signal(SIGKILL); | |
1066 | if (ret < 0) { | |
1067 | SYSERROR("Failed to set PR_SET_PDEATHSIG to SIGKILL"); | |
1068 | goto out_warn_father; | |
1069 | } | |
1070 | } | |
1071 | ||
1072 | if (access(handler->lxcpath, X_OK)) { | |
1073 | print_top_failing_dir(handler->lxcpath); | |
1074 | goto out_warn_father; | |
1075 | } | |
1076 | ||
1077 | ret = snprintf(path, sizeof(path), "%s/dev/null", handler->conf->rootfs.mount); | |
1078 | if (ret < 0 || ret >= sizeof(path)) | |
1079 | goto out_warn_father; | |
1080 | ||
1081 | /* In order to checkpoint restore, we need to have everything in the | |
1082 | * same mount namespace. However, some containers may not have a | |
1083 | * reasonable /dev (in particular, they may not have /dev/null), so we | |
1084 | * can't set init's std fds to /dev/null by opening it from inside the | |
1085 | * container. | |
1086 | * | |
1087 | * If that's the case, fall back to using the host's /dev/null. This | |
1088 | * means that migration won't work, but at least we won't spew output | |
1089 | * where it isn't wanted. | |
1090 | */ | |
1091 | if (handler->backgrounded && !handler->conf->autodev && access(path, F_OK) < 0) { | |
1092 | devnull_fd = open_devnull(); | |
1093 | ||
1094 | if (devnull_fd < 0) | |
1095 | goto out_warn_father; | |
1096 | WARN("Using /dev/null from the host for container init's " | |
1097 | "standard file descriptors. Migration will not work."); | |
1098 | } | |
1099 | ||
1100 | /* Ask father to setup cgroups and wait for him to finish. */ | |
1101 | if (lxc_sync_barrier_parent(handler, LXC_SYNC_CGROUP)) | |
1102 | goto out_error; | |
1103 | ||
1104 | /* Unshare cgroup namespace after we have setup our cgroups. If we do it | |
1105 | * earlier we end up with a wrong view of /proc/self/cgroup. For | |
1106 | * example, assume we unshare(CLONE_NEWCGROUP) first, and then create | |
1107 | * the cgroup for the container, say /sys/fs/cgroup/cpuset/lxc/c, then | |
1108 | * /proc/self/cgroup would show us: | |
1109 | * | |
1110 | * 8:cpuset:/lxc/c | |
1111 | * | |
1112 | * whereas it should actually show | |
1113 | * | |
1114 | * 8:cpuset:/ | |
1115 | */ | |
1116 | if (handler->clone_flags & CLONE_NEWCGROUP) { | |
1117 | if (unshare(CLONE_NEWCGROUP) < 0) { | |
1118 | INFO("Failed to unshare CLONE_NEWCGROUP."); | |
1119 | goto out_warn_father; | |
1120 | } | |
1121 | INFO("Unshared CLONE_NEWCGROUP."); | |
1122 | } | |
1123 | ||
1124 | /* Add the requested environment variables to the current environment to | |
1125 | * allow them to be used by the various hooks, such as the start hook | |
1126 | * above. | |
1127 | */ | |
1128 | lxc_list_for_each(iterator, &handler->conf->environment) { | |
1129 | if (putenv((char *)iterator->elem)) { | |
1130 | SYSERROR("Failed to set environment variable: %s.", (char *)iterator->elem); | |
1131 | goto out_warn_father; | |
1132 | } | |
1133 | } | |
1134 | ||
1135 | /* Setup the container, ip, names, utsname, ... */ | |
1136 | ret = lxc_setup(handler); | |
1137 | close(handler->data_sock[1]); | |
1138 | close(handler->data_sock[0]); | |
1139 | if (ret < 0) { | |
1140 | ERROR("Failed to setup container \"%s\".", handler->name); | |
1141 | goto out_warn_father; | |
1142 | } | |
1143 | ||
1144 | /* Set the label to change to when we exec(2) the container's init. */ | |
1145 | if (lsm_process_label_set(NULL, handler->conf, 1, 1) < 0) | |
1146 | goto out_warn_father; | |
1147 | ||
1148 | /* Set PR_SET_NO_NEW_PRIVS after we changed the lsm label. If we do it | |
1149 | * before we aren't allowed anymore. | |
1150 | */ | |
1151 | if (handler->conf->no_new_privs) { | |
1152 | if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) < 0) { | |
1153 | SYSERROR("Could not set PR_SET_NO_NEW_PRIVS to block execve() gainable privileges."); | |
1154 | goto out_warn_father; | |
1155 | } | |
1156 | DEBUG("Set PR_SET_NO_NEW_PRIVS to block execve() gainable privileges."); | |
1157 | } | |
1158 | ||
1159 | /* Some init's such as busybox will set sane tty settings on stdin, | |
1160 | * stdout, stderr which it thinks is the console. We already set them | |
1161 | * the way we wanted on the real terminal, and we want init to do its | |
1162 | * setup on its console ie. the pty allocated in lxc_console_create() so | |
1163 | * make sure that that pty is stdin,stdout,stderr. | |
1164 | */ | |
1165 | if (handler->conf->console.slave >= 0) { | |
1166 | if (handler->backgrounded || handler->conf->is_execute == 0) | |
1167 | ret = set_stdfds(handler->conf->console.slave); | |
1168 | else | |
1169 | ret = lxc_console_set_stdfds(handler->conf->console.slave); | |
1170 | if (ret < 0) { | |
1171 | ERROR("Failed to redirect std{in,out,err} to pty file " | |
1172 | "descriptor %d", | |
1173 | handler->conf->console.slave); | |
1174 | goto out_warn_father; | |
1175 | } | |
1176 | } | |
1177 | ||
1178 | /* If we mounted a temporary proc, then unmount it now. */ | |
1179 | tmp_proc_unmount(handler->conf); | |
1180 | ||
1181 | if (lxc_seccomp_load(handler->conf) != 0) | |
1182 | goto out_warn_father; | |
1183 | ||
1184 | if (run_lxc_hooks(handler->name, "start", handler->conf, NULL)) { | |
1185 | ERROR("Failed to run lxc.hook.start for container \"%s\".", handler->name); | |
1186 | goto out_warn_father; | |
1187 | } | |
1188 | ||
1189 | close(handler->sigfd); | |
1190 | ||
1191 | if (devnull_fd < 0) { | |
1192 | devnull_fd = open_devnull(); | |
1193 | ||
1194 | if (devnull_fd < 0) | |
1195 | goto out_warn_father; | |
1196 | } | |
1197 | ||
1198 | if (handler->conf->console.slave < 0 && handler->backgrounded) | |
1199 | if (set_stdfds(devnull_fd) < 0) { | |
1200 | ERROR("Failed to redirect std{in,out,err} to " | |
1201 | "\"/dev/null\""); | |
1202 | goto out_warn_father; | |
1203 | } | |
1204 | ||
1205 | if (devnull_fd >= 0) { | |
1206 | close(devnull_fd); | |
1207 | devnull_fd = -1; | |
1208 | } | |
1209 | ||
1210 | setsid(); | |
1211 | ||
1212 | if (handler->conf->init_cwd && chdir(handler->conf->init_cwd)) { | |
1213 | SYSERROR("Could not change directory to \"%s\"", handler->conf->init_cwd); | |
1214 | goto out_warn_father; | |
1215 | } | |
1216 | ||
1217 | if (lxc_sync_barrier_parent(handler, LXC_SYNC_CGROUP_LIMITS)) | |
1218 | goto out_warn_father; | |
1219 | ||
1220 | /* Reset the environment variables the user requested in a clear | |
1221 | * environment. | |
1222 | */ | |
1223 | if (clearenv()) { | |
1224 | SYSERROR("Failed to clear environment."); | |
1225 | /* Don't error out though. */ | |
1226 | } | |
1227 | ||
1228 | lxc_list_for_each(iterator, &handler->conf->environment) { | |
1229 | if (putenv((char *)iterator->elem)) { | |
1230 | SYSERROR("Failed to set environment variable: %s.", (char *)iterator->elem); | |
1231 | goto out_warn_father; | |
1232 | } | |
1233 | } | |
1234 | ||
1235 | if (putenv("container=lxc")) { | |
1236 | SYSERROR("Failed to set environment variable: container=lxc."); | |
1237 | goto out_warn_father; | |
1238 | } | |
1239 | ||
1240 | if (handler->conf->pty_names) { | |
1241 | if (putenv(handler->conf->pty_names)) { | |
1242 | SYSERROR("Failed to set environment variable for container ptys."); | |
1243 | goto out_warn_father; | |
1244 | } | |
1245 | } | |
1246 | ||
1247 | /* The container has been setup. We can now switch to an unprivileged | |
1248 | * uid/gid. | |
1249 | */ | |
1250 | new_uid = handler->conf->init_uid; | |
1251 | new_gid = handler->conf->init_gid; | |
1252 | ||
1253 | /* If we are in a new user namespace we already dropped all | |
1254 | * groups when we switched to root in the new user namespace | |
1255 | * further above. Only drop groups if we can, so ensure that we | |
1256 | * have necessary privilege. | |
1257 | */ | |
1258 | #if HAVE_LIBCAP | |
1259 | have_cap_setgid = lxc_proc_cap_is_set(CAP_SETGID, CAP_EFFECTIVE); | |
1260 | #else | |
1261 | have_cap_setgid = false; | |
1262 | #endif | |
1263 | if (lxc_list_empty(&handler->conf->id_map) && have_cap_setgid) { | |
1264 | if (lxc_setgroups(0, NULL) < 0) | |
1265 | goto out_warn_father; | |
1266 | } | |
1267 | ||
1268 | if (lxc_switch_uid_gid(new_uid, new_gid) < 0) | |
1269 | goto out_warn_father; | |
1270 | ||
1271 | /* After this call, we are in error because this ops should not return | |
1272 | * as it execs. | |
1273 | */ | |
1274 | handler->ops->start(handler, handler->data); | |
1275 | ||
1276 | out_warn_father: | |
1277 | /* We want the parent to know something went wrong, so we return a | |
1278 | * special error code. | |
1279 | */ | |
1280 | lxc_sync_wake_parent(handler, LXC_SYNC_ERROR); | |
1281 | ||
1282 | out_error: | |
1283 | if (devnull_fd >= 0) | |
1284 | close(devnull_fd); | |
1285 | ||
1286 | return -1; | |
1287 | } | |
1288 | ||
1289 | static int lxc_recv_ttys_from_child(struct lxc_handler *handler) | |
1290 | { | |
1291 | int i; | |
1292 | struct lxc_pty_info *pty_info; | |
1293 | int ret = -1; | |
1294 | int sock = handler->data_sock[1]; | |
1295 | struct lxc_conf *conf = handler->conf; | |
1296 | struct lxc_tty_info *tty_info = &conf->tty_info; | |
1297 | ||
1298 | if (!conf->tty) | |
1299 | return 0; | |
1300 | ||
1301 | tty_info->pty_info = malloc(sizeof(*tty_info->pty_info) * conf->tty); | |
1302 | if (!tty_info->pty_info) | |
1303 | return -1; | |
1304 | ||
1305 | for (i = 0; i < conf->tty; i++) { | |
1306 | int ttyfds[2]; | |
1307 | ||
1308 | ret = lxc_abstract_unix_recv_fds(sock, ttyfds, 2, NULL, 0); | |
1309 | if (ret < 0) | |
1310 | break; | |
1311 | ||
1312 | pty_info = &tty_info->pty_info[i]; | |
1313 | pty_info->busy = 0; | |
1314 | pty_info->master = ttyfds[0]; | |
1315 | pty_info->slave = ttyfds[1]; | |
1316 | TRACE("Received pty with master fd %d and slave fd %d from " | |
1317 | "parent", pty_info->master, pty_info->slave); | |
1318 | } | |
1319 | if (ret < 0) | |
1320 | ERROR("Failed to receive %d ttys from child: %s", conf->tty, | |
1321 | strerror(errno)); | |
1322 | else | |
1323 | TRACE("Received %d ttys from child", conf->tty); | |
1324 | ||
1325 | tty_info->nbtty = conf->tty; | |
1326 | ||
1327 | return ret; | |
1328 | } | |
1329 | ||
1330 | int resolve_clone_flags(struct lxc_handler *handler) | |
1331 | { | |
1332 | handler->clone_flags = CLONE_NEWNS; | |
1333 | ||
1334 | if (!handler->conf->inherit_ns[LXC_NS_USER]) { | |
1335 | if (!lxc_list_empty(&handler->conf->id_map)) | |
1336 | handler->clone_flags |= CLONE_NEWUSER; | |
1337 | } else { | |
1338 | INFO("Inheriting user namespace"); | |
1339 | } | |
1340 | ||
1341 | if (!handler->conf->inherit_ns[LXC_NS_NET]) { | |
1342 | if (!lxc_requests_empty_network(handler)) | |
1343 | handler->clone_flags |= CLONE_NEWNET; | |
1344 | } else { | |
1345 | INFO("Inheriting net namespace"); | |
1346 | } | |
1347 | ||
1348 | if (!handler->conf->inherit_ns[LXC_NS_IPC]) | |
1349 | handler->clone_flags |= CLONE_NEWIPC; | |
1350 | else | |
1351 | INFO("Inheriting ipc namespace"); | |
1352 | ||
1353 | if (!handler->conf->inherit_ns[LXC_NS_UTS]) | |
1354 | handler->clone_flags |= CLONE_NEWUTS; | |
1355 | else | |
1356 | INFO("Inheriting uts namespace"); | |
1357 | ||
1358 | if (!handler->conf->inherit_ns[LXC_NS_PID]) | |
1359 | handler->clone_flags |= CLONE_NEWPID; | |
1360 | else | |
1361 | INFO("Inheriting pid namespace"); | |
1362 | ||
1363 | if (cgns_supported()) { | |
1364 | if (!handler->conf->inherit_ns[LXC_NS_CGROUP]) | |
1365 | handler->clone_flags |= CLONE_NEWCGROUP; | |
1366 | else | |
1367 | INFO("Inheriting cgroup namespace"); | |
1368 | } else if (handler->conf->inherit_ns[LXC_NS_CGROUP]) { | |
1369 | return -EINVAL; | |
1370 | } | |
1371 | ||
1372 | return 0; | |
1373 | } | |
1374 | ||
1375 | /* Note that this function is used with clone(CLONE_VM). Some glibc versions | |
1376 | * used to reset the pid/tid to -1 when CLONE_VM was used without CLONE_THREAD. | |
1377 | * But since the memory between parent and child is shared on CLONE_VM this | |
1378 | * would invalidate the getpid() cache that glibc used to maintain and so | |
1379 | * getpid() in the child would return the parent's pid. This is all fixed in | |
1380 | * newer glibc versions where the getpid() cache is removed and the pid/tid is | |
1381 | * not reset anymore. | |
1382 | * However, if for whatever reason you - dear commiter - somehow need to get the | |
1383 | * pid of the dummy intermediate process for do_share_ns() you need to call | |
1384 | * lxc_raw_getpid(). The next lxc_raw_clone() call does not employ CLONE_VM and | |
1385 | * will be fine. | |
1386 | */ | |
1387 | static inline int do_share_ns(void *arg) | |
1388 | { | |
1389 | int i, flags, ret; | |
1390 | struct lxc_handler *handler = arg; | |
1391 | ||
1392 | for (i = 0; i < LXC_NS_MAX; i++) { | |
1393 | if (handler->nsfd[i] < 0) | |
1394 | continue; | |
1395 | ||
1396 | ret = setns(handler->nsfd[i], 0); | |
1397 | if (ret < 0) | |
1398 | return -1; | |
1399 | ||
1400 | DEBUG("Inherited %s namespace", ns_info[i].proc_name); | |
1401 | } | |
1402 | ||
1403 | flags = handler->on_clone_flags; | |
1404 | flags |= CLONE_PARENT; | |
1405 | handler->pid = lxc_raw_clone_cb(do_start, handler, flags); | |
1406 | if (handler->pid < 0) | |
1407 | return -1; | |
1408 | ||
1409 | return 0; | |
1410 | } | |
1411 | ||
1412 | /* lxc_spawn() performs crucial setup tasks and clone()s the new process which | |
1413 | * exec()s the requested container binary. | |
1414 | * Note that lxc_spawn() runs in the parent namespaces. Any operations performed | |
1415 | * right here should be double checked if they'd pose a security risk. (For | |
1416 | * example, any {u}mount() operations performed here will be reflected on the | |
1417 | * host!) | |
1418 | */ | |
1419 | static int lxc_spawn(struct lxc_handler *handler) | |
1420 | { | |
1421 | int i, ret; | |
1422 | char pidstr[20]; | |
1423 | bool wants_to_map_ids; | |
1424 | struct lxc_list *id_map; | |
1425 | const char *name = handler->name; | |
1426 | const char *lxcpath = handler->lxcpath; | |
1427 | bool cgroups_connected = false, share_ns = false; | |
1428 | struct lxc_conf *conf = handler->conf; | |
1429 | ||
1430 | id_map = &conf->id_map; | |
1431 | wants_to_map_ids = !lxc_list_empty(id_map); | |
1432 | ||
1433 | for (i = 0; i < LXC_NS_MAX; i++) { | |
1434 | if (!conf->inherit_ns[i]) | |
1435 | continue; | |
1436 | ||
1437 | handler->nsfd[i] = lxc_inherit_namespace(conf->inherit_ns[i], lxcpath, ns_info[i].proc_name); | |
1438 | if (handler->nsfd[i] < 0) | |
1439 | return -1; | |
1440 | ||
1441 | share_ns = true; | |
1442 | } | |
1443 | ||
1444 | if (lxc_sync_init(handler)) | |
1445 | return -1; | |
1446 | ||
1447 | ret = socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, | |
1448 | handler->data_sock); | |
1449 | if (ret < 0) { | |
1450 | lxc_sync_fini(handler); | |
1451 | return -1; | |
1452 | } | |
1453 | ||
1454 | ret = resolve_clone_flags(handler); | |
1455 | if (ret < 0) { | |
1456 | lxc_sync_fini(handler); | |
1457 | return -1; | |
1458 | } | |
1459 | ||
1460 | if (handler->clone_flags & CLONE_NEWNET) { | |
1461 | if (!lxc_list_empty(&conf->network)) { | |
1462 | ||
1463 | /* Find gateway addresses from the link device, which is | |
1464 | * no longer accessible inside the container. Do this | |
1465 | * before creating network interfaces, since goto | |
1466 | * out_delete_net does not work before lxc_clone. | |
1467 | */ | |
1468 | if (lxc_find_gateway_addresses(handler)) { | |
1469 | ERROR("Failed to find gateway addresses."); | |
1470 | lxc_sync_fini(handler); | |
1471 | return -1; | |
1472 | } | |
1473 | ||
1474 | /* That should be done before the clone because we will | |
1475 | * fill the netdev index and use them in the child. | |
1476 | */ | |
1477 | if (lxc_create_network_priv(handler)) { | |
1478 | ERROR("Failed to create the network."); | |
1479 | lxc_sync_fini(handler); | |
1480 | return -1; | |
1481 | } | |
1482 | } | |
1483 | } | |
1484 | ||
1485 | if (!cgroup_init(handler)) { | |
1486 | ERROR("Failed initializing cgroup support."); | |
1487 | goto out_delete_net; | |
1488 | } | |
1489 | ||
1490 | cgroups_connected = true; | |
1491 | ||
1492 | if (!cgroup_create(handler)) { | |
1493 | ERROR("Failed creating cgroups."); | |
1494 | goto out_delete_net; | |
1495 | } | |
1496 | ||
1497 | /* If the rootfs is not a blockdev, prevent the container from marking | |
1498 | * it readonly. | |
1499 | * If the container is unprivileged then skip rootfs pinning. | |
1500 | */ | |
1501 | if (!wants_to_map_ids) { | |
1502 | handler->pinfd = pin_rootfs(conf->rootfs.path); | |
1503 | if (handler->pinfd == -1) | |
1504 | INFO("Failed to pin the rootfs for container \"%s\".", handler->name); | |
1505 | } | |
1506 | ||
1507 | /* Create a process in a new set of namespaces. */ | |
1508 | handler->on_clone_flags = handler->clone_flags; | |
1509 | if (handler->clone_flags & CLONE_NEWUSER) { | |
1510 | /* If CLONE_NEWUSER and CLONE_NEWNET was requested, we need to | |
1511 | * clone a new user namespace first and only later unshare our | |
1512 | * network namespace to ensure that network devices ownership is | |
1513 | * set up correctly. | |
1514 | */ | |
1515 | handler->on_clone_flags &= ~CLONE_NEWNET; | |
1516 | } | |
1517 | /* The cgroup namespace gets unshare()ed not clone()ed. */ | |
1518 | handler->on_clone_flags &= ~CLONE_NEWCGROUP; | |
1519 | ||
1520 | if (share_ns) { | |
1521 | pid_t attacher_pid; | |
1522 | ||
1523 | attacher_pid = lxc_clone(do_share_ns, handler, CLONE_VFORK | CLONE_VM | CLONE_FILES); | |
1524 | if (attacher_pid < 0) { | |
1525 | SYSERROR(LXC_CLONE_ERROR); | |
1526 | goto out_delete_net; | |
1527 | } | |
1528 | ||
1529 | ret = wait_for_pid(attacher_pid); | |
1530 | if (ret < 0) { | |
1531 | SYSERROR("Intermediate process failed"); | |
1532 | goto out_delete_net; | |
1533 | } | |
1534 | } else { | |
1535 | handler->pid = lxc_raw_clone_cb(do_start, handler, handler->on_clone_flags); | |
1536 | } | |
1537 | if (handler->pid < 0) { | |
1538 | SYSERROR(LXC_CLONE_ERROR); | |
1539 | goto out_delete_net; | |
1540 | } | |
1541 | TRACE("Cloned child process %d", handler->pid); | |
1542 | ||
1543 | for (i = 0; i < LXC_NS_MAX; i++) | |
1544 | if (handler->on_clone_flags & ns_info[i].clone_flag) | |
1545 | INFO("Cloned %s", ns_info[i].flag_name); | |
1546 | ||
1547 | if (!preserve_ns(handler->nsfd, handler->on_clone_flags, handler->pid)) { | |
1548 | ERROR("Failed to preserve cloned namespaces for lxc.hook.stop"); | |
1549 | goto out_delete_net; | |
1550 | } | |
1551 | ||
1552 | lxc_sync_fini_child(handler); | |
1553 | ||
1554 | /* Map the container uids. The container became an invalid userid the | |
1555 | * moment it was cloned with CLONE_NEWUSER. This call doesn't change | |
1556 | * anything immediately, but allows the container to setuid(0) (0 being | |
1557 | * mapped to something else on the host.) later to become a valid uid | |
1558 | * again. | |
1559 | */ | |
1560 | if (wants_to_map_ids) { | |
1561 | if (!handler->conf->inherit_ns[LXC_NS_USER]) { | |
1562 | ret = lxc_map_ids(id_map, handler->pid); | |
1563 | if (ret < 0) { | |
1564 | ERROR("Failed to set up id mapping."); | |
1565 | goto out_delete_net; | |
1566 | } | |
1567 | } | |
1568 | } | |
1569 | ||
1570 | if (lxc_sync_wake_child(handler, LXC_SYNC_STARTUP)) | |
1571 | goto out_delete_net; | |
1572 | ||
1573 | if (lxc_sync_wait_child(handler, LXC_SYNC_CONFIGURE)) | |
1574 | goto out_delete_net; | |
1575 | ||
1576 | if (!cgroup_create_legacy(handler)) { | |
1577 | ERROR("Failed to setup legacy cgroups for container \"%s\".", name); | |
1578 | goto out_delete_net; | |
1579 | } | |
1580 | if (!cgroup_setup_limits(handler, false)) { | |
1581 | ERROR("Failed to setup cgroup limits for container \"%s\".", name); | |
1582 | goto out_delete_net; | |
1583 | } | |
1584 | ||
1585 | if (!cgroup_enter(handler)) | |
1586 | goto out_delete_net; | |
1587 | ||
1588 | if (!cgroup_chown(handler)) | |
1589 | goto out_delete_net; | |
1590 | ||
1591 | /* Now we're ready to preserve the network namespace */ | |
1592 | ret = lxc_preserve_ns(handler->pid, "net"); | |
1593 | if (ret < 0) { | |
1594 | ERROR("%s - Failed to preserve net namespace", strerror(errno)); | |
1595 | goto out_delete_net; | |
1596 | } | |
1597 | handler->nsfd[LXC_NS_NET] = ret; | |
1598 | DEBUG("Preserved net namespace via fd %d", ret); | |
1599 | ||
1600 | /* Create the network configuration. */ | |
1601 | if (handler->clone_flags & CLONE_NEWNET) { | |
1602 | if (lxc_network_move_created_netdev_priv(handler->lxcpath, | |
1603 | handler->name, | |
1604 | &conf->network, | |
1605 | handler->pid)) { | |
1606 | ERROR("Failed to create the configured network."); | |
1607 | goto out_delete_net; | |
1608 | } | |
1609 | ||
1610 | if (lxc_create_network_unpriv(handler->lxcpath, handler->name, | |
1611 | &conf->network, | |
1612 | handler->pid)) { | |
1613 | ERROR("Failed to create the configured network."); | |
1614 | goto out_delete_net; | |
1615 | } | |
1616 | } | |
1617 | ||
1618 | if (lxc_network_send_veth_names_to_child(handler) < 0) { | |
1619 | ERROR("Failed to send veth names to child"); | |
1620 | goto out_delete_net; | |
1621 | } | |
1622 | ||
1623 | if (!lxc_list_empty(&conf->procs)) { | |
1624 | ret = setup_proc_filesystem(&conf->procs, handler->pid); | |
1625 | if (ret < 0) | |
1626 | goto out_delete_net; | |
1627 | } | |
1628 | ||
1629 | /* Tell the child to continue its initialization. We'll get | |
1630 | * LXC_SYNC_CGROUP when it is ready for us to setup cgroups. | |
1631 | */ | |
1632 | if (lxc_sync_barrier_child(handler, LXC_SYNC_POST_CONFIGURE)) | |
1633 | goto out_delete_net; | |
1634 | ||
1635 | if (!lxc_list_empty(&conf->limits) && setup_resource_limits(&conf->limits, handler->pid)) { | |
1636 | ERROR("failed to setup resource limits for '%s'", name); | |
1637 | goto out_delete_net; | |
1638 | } | |
1639 | ||
1640 | if (lxc_sync_barrier_child(handler, LXC_SYNC_CGROUP_UNSHARE)) | |
1641 | goto out_delete_net; | |
1642 | ||
1643 | if (!cgroup_setup_limits(handler, true)) { | |
1644 | ERROR("Failed to setup the devices cgroup for container \"%s\".", name); | |
1645 | goto out_delete_net; | |
1646 | } | |
1647 | TRACE("Set up cgroup device limits"); | |
1648 | ||
1649 | cgroup_disconnect(); | |
1650 | cgroups_connected = false; | |
1651 | ||
1652 | if (handler->clone_flags & CLONE_NEWCGROUP) { | |
1653 | /* Now we're ready to preserve the cgroup namespace */ | |
1654 | ret = lxc_preserve_ns(handler->pid, "cgroup"); | |
1655 | if (ret < 0) { | |
1656 | ERROR("%s - Failed to preserve cgroup namespace", strerror(errno)); | |
1657 | goto out_delete_net; | |
1658 | } | |
1659 | handler->nsfd[LXC_NS_CGROUP] = ret; | |
1660 | DEBUG("Preserved cgroup namespace via fd %d", ret); | |
1661 | } | |
1662 | ||
1663 | snprintf(pidstr, 20, "%d", handler->pid); | |
1664 | if (setenv("LXC_PID", pidstr, 1)) | |
1665 | SYSERROR("Failed to set environment variable: LXC_PID=%s.", pidstr); | |
1666 | ||
1667 | /* Run any host-side start hooks */ | |
1668 | if (run_lxc_hooks(name, "start-host", conf, NULL)) { | |
1669 | ERROR("Failed to run lxc.hook.start-host for container \"%s\".", name); | |
1670 | return -1; | |
1671 | } | |
1672 | ||
1673 | /* Tell the child to complete its initialization and wait for it to exec | |
1674 | * or return an error. (The child will never return | |
1675 | * LXC_SYNC_READY_START+1. It will either close the sync pipe, causing | |
1676 | * lxc_sync_barrier_child to return success, or return a different | |
1677 | * value, causing us to error out). | |
1678 | */ | |
1679 | if (lxc_sync_barrier_child(handler, LXC_SYNC_READY_START)) | |
1680 | return -1; | |
1681 | ||
1682 | if (lxc_network_recv_name_and_ifindex_from_child(handler) < 0) { | |
1683 | ERROR("Failed to receive names and ifindices for network " | |
1684 | "devices from child"); | |
1685 | goto out_delete_net; | |
1686 | } | |
1687 | ||
1688 | /* Now all networks are created, network devices are moved into place, | |
1689 | * and the correct names and ifindeces in the respective namespaces have | |
1690 | * been recorded. The corresponding structs have now all been filled. So | |
1691 | * log them for debugging purposes. | |
1692 | */ | |
1693 | lxc_log_configured_netdevs(conf); | |
1694 | ||
1695 | /* Read tty fds allocated by child. */ | |
1696 | if (lxc_recv_ttys_from_child(handler) < 0) { | |
1697 | ERROR("Failed to receive tty info from child process."); | |
1698 | goto out_delete_net; | |
1699 | } | |
1700 | ||
1701 | if (handler->ops->post_start(handler, handler->data)) | |
1702 | goto out_abort; | |
1703 | ||
1704 | if (lxc_set_state(name, handler, RUNNING)) { | |
1705 | ERROR("Failed to set state for container \"%s\" to \"%s\".", name, | |
1706 | lxc_state2str(RUNNING)); | |
1707 | goto out_abort; | |
1708 | } | |
1709 | ||
1710 | lxc_sync_fini(handler); | |
1711 | ||
1712 | return 0; | |
1713 | ||
1714 | out_delete_net: | |
1715 | if (cgroups_connected) | |
1716 | cgroup_disconnect(); | |
1717 | ||
1718 | if (handler->clone_flags & CLONE_NEWNET) | |
1719 | lxc_delete_network(handler); | |
1720 | ||
1721 | out_abort: | |
1722 | lxc_abort(name, handler); | |
1723 | lxc_sync_fini(handler); | |
1724 | if (handler->pinfd >= 0) { | |
1725 | close(handler->pinfd); | |
1726 | handler->pinfd = -1; | |
1727 | } | |
1728 | ||
1729 | return -1; | |
1730 | } | |
1731 | ||
1732 | int __lxc_start(const char *name, struct lxc_handler *handler, | |
1733 | struct lxc_operations* ops, void *data, const char *lxcpath, | |
1734 | bool backgrounded) | |
1735 | { | |
1736 | int status; | |
1737 | int err = -1; | |
1738 | struct lxc_conf *conf = handler->conf; | |
1739 | ||
1740 | if (lxc_init(name, handler) < 0) { | |
1741 | ERROR("Failed to initialize container \"%s\".", name); | |
1742 | return -1; | |
1743 | } | |
1744 | handler->ops = ops; | |
1745 | handler->data = data; | |
1746 | handler->backgrounded = backgrounded; | |
1747 | ||
1748 | if (!attach_block_device(handler->conf)) { | |
1749 | ERROR("Failed to attach block device."); | |
1750 | goto out_fini_nonet; | |
1751 | } | |
1752 | ||
1753 | if (geteuid() == 0 && !lxc_list_empty(&conf->id_map)) { | |
1754 | /* If the backing store is a device, mount it here and now. */ | |
1755 | if (rootfs_is_blockdev(conf)) { | |
1756 | if (unshare(CLONE_NEWNS) < 0) { | |
1757 | ERROR("Failed to unshare CLONE_NEWNS."); | |
1758 | goto out_fini_nonet; | |
1759 | } | |
1760 | INFO("Unshared CLONE_NEWNS."); | |
1761 | ||
1762 | remount_all_slave(); | |
1763 | if (do_rootfs_setup(conf, name, lxcpath) < 0) { | |
1764 | ERROR("Error setting up rootfs mount as root before spawn."); | |
1765 | goto out_fini_nonet; | |
1766 | } | |
1767 | INFO("Set up container rootfs as host root."); | |
1768 | } | |
1769 | } | |
1770 | ||
1771 | err = lxc_spawn(handler); | |
1772 | if (err) { | |
1773 | ERROR("Failed to spawn container \"%s\".", name); | |
1774 | goto out_detach_blockdev; | |
1775 | } | |
1776 | /* close parent side of data socket */ | |
1777 | close(handler->data_sock[0]); | |
1778 | handler->data_sock[0] = -1; | |
1779 | close(handler->data_sock[1]); | |
1780 | handler->data_sock[1] = -1; | |
1781 | ||
1782 | handler->conf->reboot = 0; | |
1783 | ||
1784 | err = lxc_poll(name, handler); | |
1785 | if (err) { | |
1786 | ERROR("LXC mainloop exited with error: %d.", err); | |
1787 | goto out_abort; | |
1788 | } | |
1789 | ||
1790 | while (waitpid(handler->pid, &status, 0) < 0 && errno == EINTR) | |
1791 | continue; | |
1792 | ||
1793 | /* If the child process exited but was not signaled, it didn't call | |
1794 | * reboot. This should mean it was an lxc-execute which simply exited. | |
1795 | * In any case, treat it as a 'halt'. | |
1796 | */ | |
1797 | if (WIFSIGNALED(status)) { | |
1798 | switch(WTERMSIG(status)) { | |
1799 | case SIGINT: /* halt */ | |
1800 | DEBUG("Container \"%s\" is halting.", name); | |
1801 | break; | |
1802 | case SIGHUP: /* reboot */ | |
1803 | DEBUG("Container \"%s\" is rebooting.", name); | |
1804 | handler->conf->reboot = 1; | |
1805 | break; | |
1806 | case SIGSYS: /* seccomp */ | |
1807 | DEBUG("Container \"%s\" violated its seccomp policy.", name); | |
1808 | break; | |
1809 | default: | |
1810 | DEBUG("Unknown exit status for container \"%s\" init %d.", name, WTERMSIG(status)); | |
1811 | break; | |
1812 | } | |
1813 | } | |
1814 | ||
1815 | err = lxc_restore_phys_nics_to_netns(handler); | |
1816 | if (err < 0) | |
1817 | ERROR("Failed to move physical network devices back to parent " | |
1818 | "network namespace"); | |
1819 | ||
1820 | if (handler->pinfd >= 0) { | |
1821 | close(handler->pinfd); | |
1822 | handler->pinfd = -1; | |
1823 | } | |
1824 | ||
1825 | lxc_monitor_send_exit_code(name, status, handler->lxcpath); | |
1826 | lxc_error_set_and_log(handler->pid, status); | |
1827 | ||
1828 | out_fini: | |
1829 | lxc_delete_network(handler); | |
1830 | ||
1831 | out_detach_blockdev: | |
1832 | detach_block_device(handler->conf); | |
1833 | ||
1834 | out_fini_nonet: | |
1835 | lxc_fini(name, handler); | |
1836 | return err; | |
1837 | ||
1838 | out_abort: | |
1839 | lxc_abort(name, handler); | |
1840 | goto out_fini; | |
1841 | } | |
1842 | ||
1843 | struct start_args { | |
1844 | char *const *argv; | |
1845 | }; | |
1846 | ||
1847 | static int start(struct lxc_handler *handler, void* data) | |
1848 | { | |
1849 | struct start_args *arg = data; | |
1850 | ||
1851 | NOTICE("Exec'ing \"%s\".", arg->argv[0]); | |
1852 | ||
1853 | execvp(arg->argv[0], arg->argv); | |
1854 | SYSERROR("Failed to exec \"%s\".", arg->argv[0]); | |
1855 | return 0; | |
1856 | } | |
1857 | ||
1858 | static int post_start(struct lxc_handler *handler, void* data) | |
1859 | { | |
1860 | struct start_args *arg = data; | |
1861 | ||
1862 | NOTICE("Started \"%s\" with pid \"%d\".", arg->argv[0], handler->pid); | |
1863 | return 0; | |
1864 | } | |
1865 | ||
1866 | static struct lxc_operations start_ops = { | |
1867 | .start = start, | |
1868 | .post_start = post_start | |
1869 | }; | |
1870 | ||
1871 | int lxc_start(const char *name, char *const argv[], struct lxc_handler *handler, | |
1872 | const char *lxcpath, bool backgrounded) | |
1873 | { | |
1874 | struct start_args start_arg = { | |
1875 | .argv = argv, | |
1876 | }; | |
1877 | ||
1878 | return __lxc_start(name, handler, &start_ops, &start_arg, lxcpath, backgrounded); | |
1879 | } | |
1880 | ||
1881 | static void lxc_destroy_container_on_signal(struct lxc_handler *handler, | |
1882 | const char *name) | |
1883 | { | |
1884 | char destroy[MAXPATHLEN]; | |
1885 | bool bret = true; | |
1886 | int ret = 0; | |
1887 | struct lxc_container *c; | |
1888 | if (handler->conf->rootfs.path && handler->conf->rootfs.mount) { | |
1889 | bret = do_destroy_container(handler); | |
1890 | if (!bret) { | |
1891 | ERROR("Error destroying rootfs for container \"%s\".", name); | |
1892 | return; | |
1893 | } | |
1894 | } | |
1895 | INFO("Destroyed rootfs for container \"%s\".", name); | |
1896 | ||
1897 | ret = snprintf(destroy, MAXPATHLEN, "%s/%s", handler->lxcpath, name); | |
1898 | if (ret < 0 || ret >= MAXPATHLEN) { | |
1899 | ERROR("Error destroying directory for container \"%s\".", name); | |
1900 | return; | |
1901 | } | |
1902 | ||
1903 | c = lxc_container_new(name, handler->lxcpath); | |
1904 | if (c) { | |
1905 | if (container_disk_lock(c)) { | |
1906 | INFO("Could not update lxc_snapshots file."); | |
1907 | lxc_container_put(c); | |
1908 | } else { | |
1909 | mod_all_rdeps(c, false); | |
1910 | container_disk_unlock(c); | |
1911 | lxc_container_put(c); | |
1912 | } | |
1913 | } | |
1914 | ||
1915 | if (!handler->am_root) | |
1916 | ret = userns_exec_full(handler->conf, lxc_rmdir_onedev_wrapper, | |
1917 | destroy, "lxc_rmdir_onedev_wrapper"); | |
1918 | else | |
1919 | ret = lxc_rmdir_onedev(destroy, NULL); | |
1920 | ||
1921 | if (ret < 0) { | |
1922 | ERROR("Error destroying directory for container \"%s\".", name); | |
1923 | return; | |
1924 | } | |
1925 | INFO("Destroyed directory for container \"%s\".", name); | |
1926 | } | |
1927 | ||
1928 | static int lxc_rmdir_onedev_wrapper(void *data) | |
1929 | { | |
1930 | char *arg = (char *) data; | |
1931 | return lxc_rmdir_onedev(arg, NULL); | |
1932 | } | |
1933 | ||
1934 | static bool do_destroy_container(struct lxc_handler *handler) { | |
1935 | int ret; | |
1936 | ||
1937 | if (!handler->am_root) { | |
1938 | ret = userns_exec_full(handler->conf, storage_destroy_wrapper, | |
1939 | handler->conf, "storage_destroy_wrapper"); | |
1940 | if (ret < 0) | |
1941 | return false; | |
1942 | ||
1943 | return true; | |
1944 | } | |
1945 | ||
1946 | return storage_destroy(handler->conf); | |
1947 | } |