]>
Commit | Line | Data |
---|---|---|
0ad19a3f | 1 | /* |
2 | * lxc: linux Container library | |
3 | * | |
4 | * (C) Copyright IBM Corp. 2007, 2008 | |
5 | * | |
6 | * Authors: | |
9afe19d6 | 7 | * Daniel Lezcano <daniel.lezcano at free.fr> |
35a02107 CB |
8 | * Serge Hallyn <serge@hallyn.com> |
9 | * Christian Brauner <christian.brauner@ubuntu.com> | |
0ad19a3f | 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 | |
250b1eec | 23 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
0ad19a3f | 24 | */ |
25 | ||
f3787121 | 26 | #define _GNU_SOURCE |
f549edcc GK |
27 | #include "config.h" |
28 | ||
f3787121 | 29 | #include <alloca.h> |
0ad19a3f | 30 | #include <dirent.h> |
31 | #include <errno.h> | |
b0a33c1e | 32 | #include <fcntl.h> |
c476bdce | 33 | #include <grp.h> |
37515ebd | 34 | #include <poll.h> |
f3787121 CB |
35 | #include <signal.h> |
36 | #include <stdio.h> | |
37 | #include <stdlib.h> | |
38 | #include <string.h> | |
39 | #include <unistd.h> | |
0ad19a3f | 40 | #include <sys/file.h> |
f4d507d5 | 41 | #include <sys/mount.h> |
f3787121 | 42 | #include <sys/param.h> |
0ad19a3f | 43 | #include <sys/prctl.h> |
f3787121 CB |
44 | #include <sys/socket.h> |
45 | #include <sys/stat.h> | |
46 | #include <sys/syscall.h> | |
ddceb1f9 | 47 | #include <sys/types.h> |
b0a33c1e | 48 | #include <sys/un.h> |
f3787121 | 49 | #include <sys/wait.h> |
ff218c25 | 50 | |
e37dda71 | 51 | #if HAVE_LIBCAP |
495d2046 SG |
52 | #include <sys/capability.h> |
53 | #endif | |
54 | ||
955e2a02 | 55 | #ifndef HAVE_DECL_PR_CAPBSET_DROP |
656994bb MH |
56 | #define PR_CAPBSET_DROP 24 |
57 | #endif | |
58 | ||
955e2a02 CB |
59 | #ifndef HAVE_DECL_PR_SET_NO_NEW_PRIVS |
60 | #define PR_SET_NO_NEW_PRIVS 38 | |
61 | #endif | |
62 | ||
63 | #ifndef HAVE_DECL_PR_GET_NO_NEW_PRIVS | |
64 | #define PR_GET_NO_NEW_PRIVS 39 | |
65 | #endif | |
66 | ||
f3787121 CB |
67 | #include "af_unix.h" |
68 | #include "caps.h" | |
563f2f2c | 69 | #include "cgroup.h" |
f3787121 | 70 | #include "commands.h" |
bbf5cf35 | 71 | #include "commands_utils.h" |
f3787121 | 72 | #include "conf.h" |
74c6e2b0 | 73 | #include "confile_utils.h" |
f3787121 | 74 | #include "console.h" |
e2bcd7db | 75 | #include "error.h" |
f3787121 | 76 | #include "log.h" |
f2d5a09d | 77 | #include "lxccontainer.h" |
f3787121 CB |
78 | #include "lxclock.h" |
79 | #include "lxcseccomp.h" | |
f3787121 | 80 | #include "mainloop.h" |
63376d7d | 81 | #include "monitor.h" |
f549edcc | 82 | #include "namespace.h" |
811ef482 | 83 | #include "network.h" |
f3787121 | 84 | #include "start.h" |
28d832c4 | 85 | #include "storage.h" |
f2d5a09d | 86 | #include "storage_utils.h" |
f3787121 CB |
87 | #include "sync.h" |
88 | #include "utils.h" | |
fe4de9a6 | 89 | #include "lsm/lsm.h" |
36eb9bde CLG |
90 | |
91 | lxc_log_define(lxc_start, lxc); | |
92 | ||
f01f7975 | 93 | extern void mod_all_rdeps(struct lxc_container *c, bool inc); |
790255cf | 94 | static bool do_destroy_container(struct lxc_handler *handler); |
28272964 CB |
95 | static int lxc_rmdir_onedev_wrapper(void *data); |
96 | static void lxc_destroy_container_on_signal(struct lxc_handler *handler, | |
97 | const char *name); | |
98 | ||
c8154066 SH |
99 | static void print_top_failing_dir(const char *path) |
100 | { | |
101 | size_t len = strlen(path); | |
f3787121 | 102 | char *copy = alloca(len + 1), *p, *e, saved; |
c8154066 SH |
103 | strcpy(copy, path); |
104 | ||
105 | p = copy; | |
106 | e = copy + len; | |
107 | while (p < e) { | |
f3787121 CB |
108 | while (p < e && *p == '/') |
109 | p++; | |
110 | while (p < e && *p != '/') | |
111 | p++; | |
c8154066 SH |
112 | saved = *p; |
113 | *p = '\0'; | |
114 | if (access(copy, X_OK)) { | |
408da065 CB |
115 | SYSERROR("Could not access %s. Please grant it x " |
116 | "access, or add an ACL for the container " | |
117 | "root.", copy); | |
c8154066 SH |
118 | return; |
119 | } | |
120 | *p = saved; | |
121 | } | |
122 | } | |
123 | ||
408da065 CB |
124 | static void close_ns(int ns_fd[LXC_NS_MAX]) |
125 | { | |
9f30a190 MM |
126 | int i; |
127 | ||
9f30a190 MM |
128 | for (i = 0; i < LXC_NS_MAX; i++) { |
129 | if (ns_fd[i] > -1) { | |
130 | close(ns_fd[i]); | |
131 | ns_fd[i] = -1; | |
132 | } | |
133 | } | |
9f30a190 MM |
134 | } |
135 | ||
4d8ac866 | 136 | /* preserve_ns: open /proc/@pid/ns/@ns for each namespace specified |
62d05d9b | 137 | * in clone_flags. |
4d8ac866 | 138 | * Return true on success, false on failure. |
62d05d9b | 139 | */ |
4d8ac866 | 140 | static bool preserve_ns(int ns_fd[LXC_NS_MAX], int clone_flags, pid_t pid) |
f3787121 | 141 | { |
62d05d9b | 142 | int i, ret; |
9f30a190 | 143 | |
9f30a190 MM |
144 | for (i = 0; i < LXC_NS_MAX; i++) |
145 | ns_fd[i] = -1; | |
146 | ||
4d8ac866 CB |
147 | ret = lxc_preserve_ns(pid, ""); |
148 | if (ret < 0) { | |
149 | SYSERROR("Kernel does not support attaching to namespaces."); | |
62d05d9b | 150 | return false; |
4d8ac866 CB |
151 | } else { |
152 | close(ret); | |
cd43d2d1 SH |
153 | } |
154 | ||
9f30a190 MM |
155 | for (i = 0; i < LXC_NS_MAX; i++) { |
156 | if ((clone_flags & ns_info[i].clone_flag) == 0) | |
157 | continue; | |
4d8ac866 | 158 | ns_fd[i] = lxc_preserve_ns(pid, ns_info[i].proc_name); |
9f30a190 MM |
159 | if (ns_fd[i] < 0) |
160 | goto error; | |
161 | } | |
162 | ||
62d05d9b | 163 | return true; |
9f30a190 MM |
164 | |
165 | error: | |
4d8ac866 CB |
166 | if (errno == ENOENT) |
167 | SYSERROR("Kernel does not support attaching to %s namespaces.", ns_info[i].proc_name); | |
168 | else | |
169 | SYSERROR("Failed to open file descriptor for %s namespace: %s.", ns_info[i].proc_name, strerror(errno)); | |
9f30a190 | 170 | close_ns(ns_fd); |
62d05d9b | 171 | return false; |
9f30a190 MM |
172 | } |
173 | ||
174 | static int attach_ns(const int ns_fd[LXC_NS_MAX]) { | |
175 | int i; | |
176 | ||
177 | for (i = 0; i < LXC_NS_MAX; i++) { | |
178 | if (ns_fd[i] < 0) | |
179 | continue; | |
180 | ||
181 | if (setns(ns_fd[i], 0) != 0) | |
182 | goto error; | |
183 | } | |
184 | return 0; | |
185 | ||
186 | error: | |
408da065 | 187 | SYSERROR("Failed to attach %s namespace.", ns_info[i].proc_name); |
9f30a190 MM |
188 | return -1; |
189 | } | |
190 | ||
80090207 CLG |
191 | static int match_fd(int fd) |
192 | { | |
193 | return (fd == 0 || fd == 1 || fd == 2); | |
194 | } | |
195 | ||
47a46cf1 CB |
196 | int lxc_check_inherited(struct lxc_conf *conf, bool closeall, |
197 | int *fds_to_ignore, size_t len_fds) | |
80090207 | 198 | { |
74f96976 | 199 | struct dirent *direntp; |
80090207 | 200 | int fd, fddir; |
47a46cf1 | 201 | size_t i; |
80090207 | 202 | DIR *dir; |
80090207 | 203 | |
d2cf4c37 SH |
204 | if (conf && conf->close_all_fds) |
205 | closeall = true; | |
206 | ||
b119f362 | 207 | restart: |
80090207 CLG |
208 | dir = opendir("/proc/self/fd"); |
209 | if (!dir) { | |
13277ec4 | 210 | WARN("Failed to open directory: %s.", strerror(errno)); |
80090207 CLG |
211 | return -1; |
212 | } | |
213 | ||
214 | fddir = dirfd(dir); | |
215 | ||
74f96976 | 216 | while ((direntp = readdir(dir))) { |
80090207 CLG |
217 | if (!direntp) |
218 | break; | |
219 | ||
220 | if (!strcmp(direntp->d_name, ".")) | |
221 | continue; | |
222 | ||
223 | if (!strcmp(direntp->d_name, "..")) | |
224 | continue; | |
225 | ||
d4cff0d2 CB |
226 | if (lxc_safe_int(direntp->d_name, &fd) < 0) { |
227 | INFO("Could not parse file descriptor for: %s", direntp->d_name); | |
228 | continue; | |
229 | } | |
80090207 | 230 | |
47a46cf1 CB |
231 | for (i = 0; i < len_fds; i++) |
232 | if (fds_to_ignore[i] == fd) | |
233 | break; | |
234 | ||
235 | if (fd == fddir || fd == lxc_log_fd || | |
236 | (i < len_fds && fd == fds_to_ignore[i])) | |
80090207 CLG |
237 | continue; |
238 | ||
3659cba9 L |
239 | if (conf) { |
240 | for (i = 0; i < LXC_NS_MAX; i++) | |
241 | if (conf->inherit_ns_fd[i] == fd) | |
242 | break; | |
243 | ||
244 | if (i < LXC_NS_MAX) | |
245 | continue; | |
246 | } | |
247 | ||
858377e4 SH |
248 | if (current_config && fd == current_config->logfd) |
249 | continue; | |
250 | ||
80090207 CLG |
251 | if (match_fd(fd)) |
252 | continue; | |
80090207 | 253 | |
d2cf4c37 | 254 | if (closeall) { |
b119f362 SH |
255 | close(fd); |
256 | closedir(dir); | |
408da065 | 257 | INFO("Closed inherited fd: %d.", fd); |
b119f362 SH |
258 | goto restart; |
259 | } | |
408da065 | 260 | WARN("Inherited fd: %d.", fd); |
80090207 CLG |
261 | } |
262 | ||
408da065 | 263 | /* Only enable syslog at this point to avoid the above logging function |
64c57ea1 BD |
264 | * to open a new fd and make the check_inherited function enter an |
265 | * infinite loop. | |
266 | */ | |
267 | lxc_log_enable_syslog(); | |
268 | ||
92c7f629 GK |
269 | closedir(dir); /* cannot fail */ |
270 | return 0; | |
80090207 CLG |
271 | } |
272 | ||
83ee7875 | 273 | static int setup_signal_fd(sigset_t *oldmask) |
b0a33c1e | 274 | { |
275 | sigset_t mask; | |
276 | int fd; | |
277 | ||
408da065 | 278 | /* Block everything except serious error signals. */ |
f3304a29 FW |
279 | if (sigfillset(&mask) || |
280 | sigdelset(&mask, SIGILL) || | |
281 | sigdelset(&mask, SIGSEGV) || | |
282 | sigdelset(&mask, SIGBUS) || | |
b5159817 | 283 | sigdelset(&mask, SIGWINCH) || |
f3304a29 | 284 | sigprocmask(SIG_BLOCK, &mask, oldmask)) { |
408da065 | 285 | SYSERROR("Failed to set signal mask."); |
b0a33c1e | 286 | return -1; |
287 | } | |
288 | ||
289 | fd = signalfd(-1, &mask, 0); | |
290 | if (fd < 0) { | |
408da065 | 291 | SYSERROR("Failed to create signal file descriptor."); |
b0a33c1e | 292 | return -1; |
293 | } | |
294 | ||
295 | if (fcntl(fd, F_SETFD, FD_CLOEXEC)) { | |
408da065 | 296 | SYSERROR("Failed to set FD_CLOEXEC on the signal file descriptor: %d.", fd); |
b0a33c1e | 297 | close(fd); |
298 | return -1; | |
299 | } | |
300 | ||
408da065 | 301 | DEBUG("Set SIGCHLD handler with file descriptor: %d.", fd); |
1ac470c0 | 302 | |
b0a33c1e | 303 | return fd; |
304 | } | |
305 | ||
84c92abd | 306 | static int signal_handler(int fd, uint32_t events, void *data, |
f3787121 | 307 | struct lxc_epoll_descr *descr) |
b0a33c1e | 308 | { |
15cd25fd | 309 | struct signalfd_siginfo siginfo; |
80507ee8 | 310 | siginfo_t info; |
15cd25fd | 311 | int ret; |
82d89dce | 312 | pid_t *pid = data; |
80507ee8 | 313 | bool init_died = false; |
15cd25fd DL |
314 | |
315 | ret = read(fd, &siginfo, sizeof(siginfo)); | |
316 | if (ret < 0) { | |
408da065 | 317 | ERROR("Failed to read signal info from signal file descriptor: %d.", fd); |
15cd25fd DL |
318 | return -1; |
319 | } | |
320 | ||
321 | if (ret != sizeof(siginfo)) { | |
408da065 | 322 | ERROR("Unexpected size for siginfo struct."); |
15cd25fd DL |
323 | return -1; |
324 | } | |
325 | ||
408da065 | 326 | /* Check whether init is running. */ |
80507ee8 SH |
327 | info.si_pid = 0; |
328 | ret = waitid(P_PID, *pid, &info, WEXITED | WNOWAIT | WNOHANG); | |
408da065 | 329 | if (ret == 0 && info.si_pid == *pid) |
80507ee8 | 330 | init_died = true; |
80507ee8 | 331 | |
f3304a29 FW |
332 | if (siginfo.ssi_signo != SIGCHLD) { |
333 | kill(*pid, siginfo.ssi_signo); | |
408da065 | 334 | INFO("Forwarded signal %d to pid %d.", siginfo.ssi_signo, *pid); |
80507ee8 | 335 | return init_died ? 1 : 0; |
f3304a29 FW |
336 | } |
337 | ||
408da065 CB |
338 | if (siginfo.ssi_code == CLD_STOPPED) { |
339 | INFO("Container init process was stopped."); | |
340 | return init_died ? 1 : 0; | |
341 | } else if (siginfo.ssi_code == CLD_CONTINUED) { | |
342 | INFO("Container init process was continued."); | |
80507ee8 | 343 | return init_died ? 1 : 0; |
15cd25fd | 344 | } |
1ac470c0 | 345 | |
408da065 CB |
346 | /* More robustness, protect ourself from a SIGCHLD sent |
347 | * by a process different from the container init. | |
82d89dce DL |
348 | */ |
349 | if (siginfo.ssi_pid != *pid) { | |
af4c0f05 | 350 | NOTICE("Received SIGCHLD from pid %d instead of container init %d.", siginfo.ssi_pid, *pid); |
80507ee8 | 351 | return init_died ? 1 : 0; |
82d89dce DL |
352 | } |
353 | ||
408da065 | 354 | DEBUG("Container init process %d exited.", *pid); |
b0a33c1e | 355 | return 1; |
356 | } | |
357 | ||
5e5576a4 CB |
358 | static int lxc_serve_state_clients(const char *name, |
359 | struct lxc_handler *handler, | |
360 | lxc_state_t state) | |
66aeffc7 | 361 | { |
dbc9832d CB |
362 | ssize_t ret; |
363 | struct lxc_list *cur, *next; | |
364 | struct state_client *client; | |
365 | struct lxc_msg msg = {.type = lxc_msg_state, .value = state}; | |
366 | ||
367 | process_lock(); | |
5e5576a4 | 368 | |
dbc9832d | 369 | /* Only set state under process lock held so that we don't cause |
54446942 | 370 | * lxc_cmd_add_state_client() to miss a state. |
dbc9832d | 371 | */ |
66aeffc7 | 372 | handler->state = state; |
dbc9832d CB |
373 | TRACE("set container state to %s", lxc_state2str(state)); |
374 | ||
375 | if (lxc_list_empty(&handler->state_clients)) { | |
376 | TRACE("no state clients registered"); | |
377 | process_unlock(); | |
65ee1673 | 378 | lxc_monitor_send_state(name, state, handler->lxcpath); |
dbc9832d CB |
379 | return 0; |
380 | } | |
381 | ||
382 | strncpy(msg.name, name, sizeof(msg.name)); | |
383 | msg.name[sizeof(msg.name) - 1] = 0; | |
384 | ||
385 | lxc_list_for_each_safe(cur, &handler->state_clients, next) { | |
386 | client = cur->elem; | |
387 | ||
388 | if (!client->states[state]) { | |
389 | TRACE("state %s not registered for state client %d", | |
390 | lxc_state2str(state), client->clientfd); | |
391 | continue; | |
392 | } | |
393 | ||
394 | TRACE("sending state %s to state client %d", | |
395 | lxc_state2str(state), client->clientfd); | |
396 | ||
397 | again: | |
398 | ret = send(client->clientfd, &msg, sizeof(msg), 0); | |
ee8377bd CB |
399 | if (ret <= 0) { |
400 | if (errno == EINTR) { | |
401 | TRACE("Caught EINTR; retrying"); | |
dbc9832d | 402 | goto again; |
ee8377bd | 403 | } |
dbc9832d CB |
404 | |
405 | ERROR("failed to send message to client"); | |
406 | } | |
407 | ||
408 | /* kick client from list */ | |
409 | close(client->clientfd); | |
410 | lxc_list_del(cur); | |
411 | free(cur->elem); | |
412 | free(cur); | |
413 | } | |
414 | process_unlock(); | |
415 | ||
5e5576a4 CB |
416 | return 0; |
417 | } | |
418 | ||
419 | static int lxc_serve_state_socket_pair(const char *name, | |
420 | struct lxc_handler *handler, | |
421 | lxc_state_t state) | |
422 | { | |
423 | ssize_t ret; | |
424 | ||
425 | if (!handler->backgrounded || | |
426 | handler->state_socket_pair[1] < 0 || | |
427 | state == STARTING) | |
428 | return 0; | |
429 | ||
430 | /* Close read end of the socket pair. */ | |
431 | close(handler->state_socket_pair[0]); | |
432 | handler->state_socket_pair[0] = -1; | |
433 | ||
ee8377bd | 434 | again: |
5e5576a4 CB |
435 | ret = lxc_abstract_unix_send_credential(handler->state_socket_pair[1], |
436 | &(int){state}, sizeof(int)); | |
ee8377bd CB |
437 | if (ret != sizeof(int)) { |
438 | if (errno == EINTR) | |
439 | goto again; | |
5e5576a4 CB |
440 | SYSERROR("Failed to send state to %d", |
441 | handler->state_socket_pair[1]); | |
ee8377bd CB |
442 | return -1; |
443 | } | |
5e5576a4 CB |
444 | |
445 | TRACE("Sent container state \"%s\" to %d", lxc_state2str(state), | |
446 | handler->state_socket_pair[1]); | |
447 | ||
448 | /* Close write end of the socket pair. */ | |
449 | close(handler->state_socket_pair[1]); | |
450 | handler->state_socket_pair[1] = -1; | |
451 | ||
452 | return 0; | |
453 | } | |
454 | ||
455 | int lxc_set_state(const char *name, struct lxc_handler *handler, | |
456 | lxc_state_t state) | |
457 | { | |
458 | int ret; | |
459 | ||
460 | ret = lxc_serve_state_socket_pair(name, handler, state); | |
461 | if (ret < 0) { | |
462 | ERROR("Failed to synchronize via anonymous pair of unix sockets"); | |
463 | return -1; | |
464 | } | |
465 | ||
466 | ret = lxc_serve_state_clients(name, handler, state); | |
467 | if (ret < 0) | |
468 | return -1; | |
469 | ||
dbc9832d CB |
470 | /* This function will try to connect to the legacy lxc-monitord state |
471 | * server and only exists for backwards compatibility. | |
472 | */ | |
9123e471 | 473 | lxc_monitor_send_state(name, state, handler->lxcpath); |
dbc9832d | 474 | |
66aeffc7 DL |
475 | return 0; |
476 | } | |
477 | ||
735f2c6e | 478 | int lxc_poll(const char *name, struct lxc_handler *handler) |
b0a33c1e | 479 | { |
ca5f7926 DL |
480 | int sigfd = handler->sigfd; |
481 | int pid = handler->pid; | |
b0a33c1e | 482 | struct lxc_epoll_descr descr; |
483 | ||
a9e61274 | 484 | if (lxc_mainloop_open(&descr)) { |
408da065 | 485 | ERROR("Failed to create LXC mainloop."); |
50c8bf05 | 486 | goto out_sigfd; |
b0a33c1e | 487 | } |
488 | ||
83ee7875 | 489 | if (lxc_mainloop_add_handler(&descr, sigfd, signal_handler, &pid)) { |
408da065 | 490 | ERROR("Failed to add signal handler with file descriptor %d to LXC mainloop.", sigfd); |
b0a33c1e | 491 | goto out_mainloop_open; |
492 | } | |
493 | ||
da41561c | 494 | if (lxc_console_mainloop_add(&descr, handler->conf)) { |
408da065 | 495 | ERROR("Failed to add console handler to LXC mainloop."); |
63376d7d DL |
496 | goto out_mainloop_open; |
497 | } | |
498 | ||
ef6e34ee | 499 | if (lxc_cmd_mainloop_add(name, &descr, handler)) { |
408da065 | 500 | ERROR("Failed to add command handler to LXC mainloop."); |
96fa1ff0 | 501 | goto out_mainloop_open; |
563f2f2c DL |
502 | } |
503 | ||
3f903c04 | 504 | TRACE("lxc mainloop is ready"); |
b0a33c1e | 505 | |
e51d4895 | 506 | return lxc_mainloop(&descr, -1); |
b0a33c1e | 507 | |
508 | out_mainloop_open: | |
509 | lxc_mainloop_close(&descr); | |
408da065 | 510 | |
b0a33c1e | 511 | out_sigfd: |
512 | close(sigfd); | |
408da065 | 513 | |
c3e13372 | 514 | return -1; |
b0a33c1e | 515 | } |
516 | ||
f2e07cb6 CB |
517 | void lxc_free_handler(struct lxc_handler *handler) |
518 | { | |
519 | if (handler->conf && handler->conf->maincmd_fd) | |
520 | close(handler->conf->maincmd_fd); | |
521 | ||
5e5576a4 CB |
522 | if (handler->state_socket_pair[0] >= 0) |
523 | close(handler->state_socket_pair[0]); | |
524 | ||
525 | if (handler->state_socket_pair[1] >= 0) | |
526 | close(handler->state_socket_pair[1]); | |
527 | ||
f2e07cb6 CB |
528 | if (handler->name) |
529 | free(handler->name); | |
530 | ||
531 | handler->conf = NULL; | |
532 | free(handler); | |
533 | } | |
534 | ||
aa460476 | 535 | struct lxc_handler *lxc_init_handler(const char *name, struct lxc_conf *conf, |
5e5576a4 | 536 | const char *lxcpath, bool daemonize) |
59eb99ba | 537 | { |
5e5576a4 | 538 | int i, ret; |
3a0f472d DL |
539 | struct lxc_handler *handler; |
540 | ||
541 | handler = malloc(sizeof(*handler)); | |
aa460476 CB |
542 | if (!handler) { |
543 | ERROR("failed to allocate memory"); | |
3a0f472d | 544 | return NULL; |
aa460476 | 545 | } |
59eb99ba DL |
546 | |
547 | memset(handler, 0, sizeof(*handler)); | |
548 | ||
790255cf CB |
549 | /* Note that am_unpriv() checks the effective uid. We probably don't |
550 | * care if we are real root only if we are running as root so this | |
551 | * should be fine. | |
552 | */ | |
d0fbc7ba | 553 | handler->am_root = !am_unpriv(); |
c6012571 | 554 | handler->data_sock[0] = handler->data_sock[1] = -1; |
fae349da | 555 | handler->conf = conf; |
9123e471 | 556 | handler->lxcpath = lxcpath; |
5c068da9 | 557 | handler->pinfd = -1; |
5e5576a4 | 558 | handler->state_socket_pair[0] = handler->state_socket_pair[1] = -1; |
dbc9832d | 559 | lxc_list_init(&handler->state_clients); |
fae349da | 560 | |
b6b2b194 WB |
561 | for (i = 0; i < LXC_NS_MAX; i++) |
562 | handler->nsfd[i] = -1; | |
563 | ||
3bdf52d7 DL |
564 | handler->name = strdup(name); |
565 | if (!handler->name) { | |
aa460476 | 566 | ERROR("failed to allocate memory"); |
f2e07cb6 | 567 | goto on_error; |
3bdf52d7 DL |
568 | } |
569 | ||
5e5576a4 CB |
570 | if (daemonize && !handler->conf->reboot) { |
571 | /* Create socketpair() to synchronize on daemonized startup. | |
572 | * When the container reboots we don't need to synchronize again | |
573 | * currently so don't open another socketpair(). | |
574 | */ | |
575 | ret = socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, | |
576 | handler->state_socket_pair); | |
577 | if (ret < 0) { | |
578 | ERROR("Failed to create anonymous pair of unix sockets"); | |
579 | goto on_error; | |
580 | } | |
581 | TRACE("Created anonymous pair {%d,%d} of unix sockets", | |
582 | handler->state_socket_pair[0], | |
583 | handler->state_socket_pair[1]); | |
584 | } | |
585 | ||
aa460476 CB |
586 | if (lxc_cmd_init(name, handler, lxcpath)) { |
587 | ERROR("failed to set up command socket"); | |
f2e07cb6 | 588 | goto on_error; |
aa460476 CB |
589 | } |
590 | ||
591 | TRACE("unix domain socket %d for command server is ready", | |
592 | handler->conf->maincmd_fd); | |
593 | ||
594 | return handler; | |
595 | ||
f2e07cb6 CB |
596 | on_error: |
597 | lxc_free_handler(handler); | |
aa460476 CB |
598 | |
599 | return NULL; | |
600 | } | |
601 | ||
602 | int lxc_init(const char *name, struct lxc_handler *handler) | |
603 | { | |
604 | struct lxc_conf *conf = handler->conf; | |
605 | ||
606 | lsm_init(); | |
607 | TRACE("initialized LSM"); | |
d2e30e99 | 608 | |
8f2c3a70 | 609 | if (lxc_read_seccomp_config(conf) != 0) { |
408da065 | 610 | ERROR("Failed loading seccomp policy."); |
d2e30e99 | 611 | goto out_close_maincmd_fd; |
8f2c3a70 | 612 | } |
dbc9832d | 613 | TRACE("read seccomp policy"); |
8f2c3a70 | 614 | |
408da065 | 615 | /* Begin by setting the state to STARTING. */ |
25c2aca5 | 616 | if (lxc_set_state(name, handler, STARTING)) { |
408da065 | 617 | ERROR("Failed to set state for container \"%s\" to \"%s\".", name, lxc_state2str(STARTING)); |
051151de | 618 | goto out_close_maincmd_fd; |
0ad19a3f | 619 | } |
dbc9832d | 620 | TRACE("set container state to \"STARTING\""); |
0ad19a3f | 621 | |
408da065 CB |
622 | /* Start of environment variable setup for hooks. */ |
623 | if (name && setenv("LXC_NAME", name, 1)) | |
624 | SYSERROR("Failed to set environment variable: LXC_NAME=%s.", name); | |
625 | ||
626 | if (conf->rcfile && setenv("LXC_CONFIG_FILE", conf->rcfile, 1)) | |
627 | SYSERROR("Failed to set environment variable: LXC_CONFIG_FILE=%s.", conf->rcfile); | |
628 | ||
629 | if (conf->rootfs.mount && setenv("LXC_ROOTFS_MOUNT", conf->rootfs.mount, 1)) | |
630 | SYSERROR("Failed to set environment variable: LXC_ROOTFS_MOUNT=%s.", conf->rootfs.mount); | |
631 | ||
632 | if (conf->rootfs.path && setenv("LXC_ROOTFS_PATH", conf->rootfs.path, 1)) | |
633 | SYSERROR("Failed to set environment variable: LXC_ROOTFS_PATH=%s.", conf->rootfs.path); | |
634 | ||
635 | if (conf->console.path && setenv("LXC_CONSOLE", conf->console.path, 1)) | |
636 | SYSERROR("Failed to set environment variable: LXC_CONSOLE=%s.", conf->console.path); | |
637 | ||
638 | if (conf->console.log_path && setenv("LXC_CONSOLE_LOGPATH", conf->console.log_path, 1)) | |
639 | SYSERROR("Failed to set environment variable: LXC_CONSOLE_LOGPATH=%s.", conf->console.log_path); | |
640 | ||
641 | if (setenv("LXC_CGNS_AWARE", "1", 1)) | |
642 | SYSERROR("Failed to set environment variable LXC_CGNS_AWARE=1."); | |
b8f88d9b CB |
643 | |
644 | if (setenv("LXC_LOG_LEVEL", lxc_log_priority_to_string(handler->conf->loglevel), 1)) | |
645 | SYSERROR("Failed to set environment variable LXC_CGNS_AWARE=1."); | |
408da065 | 646 | /* End of environment variable setup for hooks. */ |
f7bee6c6 | 647 | |
dbc9832d CB |
648 | TRACE("set environment variables"); |
649 | ||
283678ed | 650 | if (run_lxc_hooks(name, "pre-start", conf, handler->lxcpath, NULL)) { |
408da065 | 651 | ERROR("Failed to run lxc.hook.pre-start for container \"%s\".", name); |
773fb9ca SH |
652 | goto out_aborting; |
653 | } | |
dbc9832d | 654 | TRACE("ran pre-start hooks"); |
26ddeedd | 655 | |
408da065 CB |
656 | /* The signal fd has to be created before forking otherwise if the child |
657 | * process exits before we setup the signal fd, the event will be lost | |
658 | * and the command will be stuck. | |
659 | */ | |
83ee7875 | 660 | handler->sigfd = setup_signal_fd(&handler->oldmask); |
59eb99ba | 661 | if (handler->sigfd < 0) { |
408da065 | 662 | ERROR("Failed to setup SIGCHLD fd handler."); |
b5159817 DE |
663 | goto out_delete_tty; |
664 | } | |
dbc9832d | 665 | TRACE("set up signal fd"); |
b5159817 | 666 | |
408da065 | 667 | /* Do this after setting up signals since it might unblock SIGWINCH. */ |
b5159817 | 668 | if (lxc_console_create(conf)) { |
408da065 | 669 | ERROR("Failed to create console for container \"%s\".", name); |
b5159817 | 670 | goto out_restore_sigmask; |
b0a33c1e | 671 | } |
dbc9832d | 672 | TRACE("created console"); |
b0a33c1e | 673 | |
54117de5 | 674 | if (lxc_ttys_shift_ids(conf) < 0) { |
408da065 | 675 | ERROR("Failed to shift tty into container."); |
c4d10a05 SH |
676 | goto out_restore_sigmask; |
677 | } | |
dbc9832d | 678 | TRACE("shifted tty ids"); |
c4d10a05 | 679 | |
dbc9832d | 680 | INFO("container \"%s\" is initialized", name); |
aa460476 | 681 | return 0; |
59eb99ba | 682 | |
b5159817 DE |
683 | out_restore_sigmask: |
684 | sigprocmask(SIG_SETMASK, &handler->oldmask, NULL); | |
59eb99ba | 685 | out_delete_tty: |
fae349da | 686 | lxc_delete_tty(&conf->tty_info); |
59eb99ba | 687 | out_aborting: |
25c2aca5 | 688 | lxc_set_state(name, handler, ABORTING); |
d2e30e99 DE |
689 | out_close_maincmd_fd: |
690 | close(conf->maincmd_fd); | |
691 | conf->maincmd_fd = -1; | |
aa460476 | 692 | return -1; |
59eb99ba DL |
693 | } |
694 | ||
3b72c4a0 | 695 | void lxc_fini(const char *name, struct lxc_handler *handler) |
59eb99ba | 696 | { |
b3286b62 | 697 | int i, rc; |
dbc9832d | 698 | struct lxc_list *cur, *next; |
b3286b62 | 699 | pid_t self = getpid(); |
dbc9832d | 700 | char *namespaces[LXC_NS_MAX + 1]; |
b3286b62 | 701 | size_t namespace_count = 0; |
b6b2b194 | 702 | |
408da065 CB |
703 | /* The STOPPING state is there for future cleanup code which can take |
704 | * awhile. | |
59eb99ba | 705 | */ |
25c2aca5 | 706 | lxc_set_state(name, handler, STOPPING); |
b6b2b194 | 707 | |
b3286b62 WB |
708 | for (i = 0; i < LXC_NS_MAX; i++) { |
709 | if (handler->nsfd[i] != -1) { | |
710 | rc = asprintf(&namespaces[namespace_count], "%s:/proc/%d/fd/%d", | |
711 | ns_info[i].proc_name, self, handler->nsfd[i]); | |
712 | if (rc == -1) { | |
408da065 | 713 | SYSERROR("Failed to allocate memory."); |
b3286b62 WB |
714 | break; |
715 | } | |
716 | ++namespace_count; | |
717 | } | |
718 | } | |
719 | namespaces[namespace_count] = NULL; | |
c154af98 | 720 | |
408da065 CB |
721 | if (handler->conf->reboot && setenv("LXC_TARGET", "reboot", 1)) |
722 | SYSERROR("Failed to set environment variable: LXC_TARGET=reboot."); | |
723 | ||
724 | if (!handler->conf->reboot && setenv("LXC_TARGET", "stop", 1)) | |
725 | SYSERROR("Failed to set environment variable: LXC_TARGET=stop."); | |
c154af98 | 726 | |
b3286b62 | 727 | if (run_lxc_hooks(name, "stop", handler->conf, handler->lxcpath, namespaces)) |
408da065 | 728 | ERROR("Failed to run lxc.hook.stop for container \"%s\".", name); |
c154af98 | 729 | |
b3286b62 WB |
730 | while (namespace_count--) |
731 | free(namespaces[namespace_count]); | |
b6b2b194 WB |
732 | for (i = 0; i < LXC_NS_MAX; i++) { |
733 | if (handler->nsfd[i] != -1) { | |
734 | close(handler->nsfd[i]); | |
735 | handler->nsfd[i] = -1; | |
736 | } | |
737 | } | |
738d0deb CB |
738 | |
739 | if (handler->netnsfd >= 0) { | |
740 | close(handler->netnsfd); | |
741 | handler->netnsfd = -1; | |
742 | } | |
743 | ||
4288b79f CB |
744 | cgroup_destroy(handler); |
745 | ||
25c2aca5 | 746 | lxc_set_state(name, handler, STOPPED); |
59eb99ba | 747 | |
4288b79f CB |
748 | /* close command socket */ |
749 | close(handler->conf->maincmd_fd); | |
750 | handler->conf->maincmd_fd = -1; | |
751 | ||
f3787121 | 752 | if (run_lxc_hooks(name, "post-stop", handler->conf, handler->lxcpath, NULL)) { |
408da065 | 753 | ERROR("Failed to run lxc.hook.post-stop for container \"%s\".", name); |
f3787121 CB |
754 | if (handler->conf->reboot) { |
755 | WARN("Container will be stopped instead of rebooted."); | |
756 | handler->conf->reboot = 0; | |
408da065 CB |
757 | if (setenv("LXC_TARGET", "stop", 1)) |
758 | WARN("Failed to set environment variable: LXC_TARGET=stop."); | |
f3787121 CB |
759 | } |
760 | } | |
26ddeedd | 761 | |
408da065 | 762 | /* Reset mask set by setup_signal_fd. */ |
8f64a3f6 | 763 | if (sigprocmask(SIG_SETMASK, &handler->oldmask, NULL)) |
408da065 | 764 | WARN("Failed to restore signal mask."); |
8f64a3f6 | 765 | |
b5159817 | 766 | lxc_console_delete(&handler->conf->console); |
b2431939 | 767 | lxc_delete_tty(&handler->conf->tty_info); |
dbc9832d | 768 | |
dbc9832d CB |
769 | /* The command socket is now closed, no more state clients can register |
770 | * themselves from now on. So free the list of state clients. | |
771 | */ | |
772 | lxc_list_for_each_safe(cur, &handler->state_clients, next) { | |
773 | struct state_client *client = cur->elem; | |
774 | /* close state client socket */ | |
775 | close(client->clientfd); | |
776 | lxc_list_del(cur); | |
777 | free(cur->elem); | |
778 | free(cur); | |
779 | } | |
780 | ||
c6012571 CB |
781 | if (handler->data_sock[0] != -1) { |
782 | close(handler->data_sock[0]); | |
783 | close(handler->data_sock[1]); | |
e8bd4e43 | 784 | } |
2c5f2ede CB |
785 | |
786 | if (handler->conf->ephemeral == 1 && handler->conf->reboot != 1) | |
28272964 | 787 | lxc_destroy_container_on_signal(handler, name); |
2c5f2ede | 788 | |
4288b79f | 789 | free(handler->name); |
b2431939 | 790 | free(handler); |
59eb99ba DL |
791 | } |
792 | ||
735f2c6e | 793 | void lxc_abort(const char *name, struct lxc_handler *handler) |
59eb99ba | 794 | { |
73e608b2 SH |
795 | int ret, status; |
796 | ||
25c2aca5 | 797 | lxc_set_state(name, handler, ABORTING); |
7d9fb3e9 DL |
798 | if (handler->pid > 0) |
799 | kill(handler->pid, SIGKILL); | |
408da065 CB |
800 | while ((ret = waitpid(-1, &status, 0)) > 0) { |
801 | ; | |
802 | } | |
59eb99ba DL |
803 | } |
804 | ||
ffe1e01a | 805 | static int do_start(void *data) |
50e98013 | 806 | { |
7c661726 | 807 | struct lxc_list *iterator; |
23c53af9 | 808 | struct lxc_handler *handler = data; |
7a55c157 TA |
809 | int devnull_fd = -1, ret; |
810 | char path[PATH_MAX]; | |
50e98013 DL |
811 | |
812 | if (sigprocmask(SIG_SETMASK, &handler->oldmask, NULL)) { | |
408da065 | 813 | SYSERROR("Failed to set signal mask."); |
9d7f9e52 | 814 | return -1; |
50e98013 DL |
815 | } |
816 | ||
408da065 CB |
817 | /* This prctl must be before the synchro, so if the parent dies before |
818 | * we set the parent death signal, we will detect its death with the | |
819 | * synchro right after, otherwise we have a window where the parent can | |
820 | * exit before we set the pdeath signal leading to a unsupervized | |
821 | * container. | |
743ecd2e DL |
822 | */ |
823 | if (prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0)) { | |
408da065 | 824 | SYSERROR("Failed to set PR_SET_PDEATHSIG to SIGKILL."); |
743ecd2e DL |
825 | return -1; |
826 | } | |
827 | ||
3c22086f | 828 | lxc_sync_fini_parent(handler); |
50e98013 | 829 | |
408da065 | 830 | /* Don't leak the pinfd to the container. */ |
025ed0f3 | 831 | if (handler->pinfd >= 0) { |
0d03360a | 832 | close(handler->pinfd); |
025ed0f3 | 833 | } |
2b0e17e4 | 834 | |
5b1e83cb SH |
835 | if (lxc_sync_wait_parent(handler, LXC_SYNC_STARTUP)) |
836 | return -1; | |
837 | ||
408da065 CB |
838 | /* Unshare CLONE_NEWNET after CLONE_NEWUSER. See |
839 | * https://github.com/lxc/lxd/issues/1978. | |
840 | */ | |
5b1e83cb | 841 | if ((handler->clone_flags & (CLONE_NEWNET | CLONE_NEWUSER)) == |
408da065 | 842 | (CLONE_NEWNET | CLONE_NEWUSER)) { |
5b1e83cb SH |
843 | ret = unshare(CLONE_NEWNET); |
844 | if (ret < 0) { | |
408da065 | 845 | SYSERROR("Failed to unshare CLONE_NEWNET."); |
5b1e83cb SH |
846 | goto out_warn_father; |
847 | } | |
408da065 | 848 | INFO("Unshared CLONE_NEWNET."); |
5b1e83cb SH |
849 | } |
850 | ||
408da065 CB |
851 | /* Tell the parent task it can begin to configure the container and wait |
852 | * for it to finish. | |
3c22086f CLG |
853 | */ |
854 | if (lxc_sync_barrier_parent(handler, LXC_SYNC_CONFIGURE)) | |
9d7f9e52 | 855 | return -1; |
50e98013 | 856 | |
7ab1ba02 CB |
857 | if (lxc_network_recv_veth_names_from_parent(handler) < 0) { |
858 | ERROR("Failed to receive veth names from parent"); | |
658979c5 | 859 | goto out_warn_father; |
7ab1ba02 | 860 | } |
658979c5 | 861 | |
408da065 | 862 | /* If we are in a new user namespace, become root there to have |
d08f8d2f | 863 | * privilege over our namespace. |
f6d3e3e4 SH |
864 | */ |
865 | if (!lxc_list_empty(&handler->conf->id_map)) { | |
d08f8d2f | 866 | if (lxc_switch_uid_gid(0, 0) < 0) |
f6d3e3e4 | 867 | goto out_warn_father; |
d08f8d2f CB |
868 | |
869 | /* Drop groups only after we switched to a valid gid in the new | |
870 | * user namespace. | |
871 | */ | |
872 | if (lxc_setgroups(0, NULL) < 0) | |
c476bdce | 873 | goto out_warn_father; |
f6d3e3e4 SH |
874 | } |
875 | ||
99b71824 | 876 | if (access(handler->lxcpath, X_OK)) { |
c8154066 SH |
877 | print_top_failing_dir(handler->lxcpath); |
878 | goto out_warn_father; | |
879 | } | |
880 | ||
a91cde21 | 881 | ret = snprintf(path, sizeof(path), "%s/dev/null", handler->conf->rootfs.mount); |
408da065 | 882 | if (ret < 0 || ret >= sizeof(path)) |
7a55c157 | 883 | goto out_warn_father; |
7a55c157 TA |
884 | |
885 | /* In order to checkpoint restore, we need to have everything in the | |
886 | * same mount namespace. However, some containers may not have a | |
887 | * reasonable /dev (in particular, they may not have /dev/null), so we | |
888 | * can't set init's std fds to /dev/null by opening it from inside the | |
889 | * container. | |
890 | * | |
891 | * If that's the case, fall back to using the host's /dev/null. This | |
892 | * means that migration won't work, but at least we won't spew output | |
893 | * where it isn't wanted. | |
894 | */ | |
895 | if (handler->backgrounded && !handler->conf->autodev && access(path, F_OK) < 0) { | |
c44de748 AM |
896 | devnull_fd = open_devnull(); |
897 | ||
898 | if (devnull_fd < 0) | |
899 | goto out_warn_father; | |
408da065 CB |
900 | WARN("Using /dev/null from the host for container init's " |
901 | "standard file descriptors. Migration will not work."); | |
c44de748 AM |
902 | } |
903 | ||
408da065 | 904 | /* Ask father to setup cgroups and wait for him to finish. */ |
544a48a0 | 905 | if (lxc_sync_barrier_parent(handler, LXC_SYNC_CGROUP)) |
c44de748 | 906 | goto out_error; |
544a48a0 | 907 | |
deefdf8a CB |
908 | /* Unshare cgroup namespace after we have setup our cgroups. If we do it |
909 | * earlier we end up with a wrong view of /proc/self/cgroup. For | |
910 | * example, assume we unshare(CLONE_NEWCGROUP) first, and then create | |
911 | * the cgroup for the container, say /sys/fs/cgroup/cpuset/lxc/c, then | |
912 | * /proc/self/cgroup would show us: | |
913 | * | |
914 | * 8:cpuset:/lxc/c | |
915 | * | |
916 | * whereas it should actually show | |
917 | * | |
918 | * 8:cpuset:/ | |
919 | */ | |
920 | if (cgns_supported()) { | |
921 | if (unshare(CLONE_NEWCGROUP) < 0) { | |
922 | INFO("Failed to unshare CLONE_NEWCGROUP."); | |
923 | goto out_warn_father; | |
924 | } | |
925 | INFO("Unshared CLONE_NEWCGROUP."); | |
926 | } | |
927 | ||
149857af CB |
928 | /* Add the requested environment variables to the current environment to |
929 | * allow them to be used by the various hooks, such as the start hook | |
930 | * above. | |
98ff08ca | 931 | */ |
98ff08ca CB |
932 | lxc_list_for_each(iterator, &handler->conf->environment) { |
933 | if (putenv((char *)iterator->elem)) { | |
934 | SYSERROR("Failed to set environment variable: %s.", (char *)iterator->elem); | |
935 | goto out_warn_father; | |
936 | } | |
937 | } | |
938 | ||
f4152036 | 939 | /* Setup the container, ip, names, utsname, ... */ |
7f135597 | 940 | ret = lxc_setup_child(handler); |
7729f8e5 CB |
941 | close(handler->data_sock[0]); |
942 | close(handler->data_sock[1]); | |
943 | if (ret < 0) { | |
f4152036 CB |
944 | ERROR("Failed to setup container \"%s\".", handler->name); |
945 | goto out_warn_father; | |
946 | } | |
947 | ||
408da065 | 948 | /* Set the label to change to when we exec(2) the container's init. */ |
7aff4f43 | 949 | if (lsm_process_label_set(NULL, handler->conf, 1, 1) < 0) |
e075f5d9 | 950 | goto out_warn_father; |
5112cd70 | 951 | |
029cdff5 | 952 | /* Set PR_SET_NO_NEW_PRIVS after we changed the lsm label. If we do it |
408da065 CB |
953 | * before we aren't allowed anymore. |
954 | */ | |
029cdff5 CB |
955 | if (handler->conf->no_new_privs) { |
956 | if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) < 0) { | |
957 | SYSERROR("Could not set PR_SET_NO_NEW_PRIVS to block execve() gainable privileges."); | |
958 | goto out_warn_father; | |
959 | } | |
960 | DEBUG("Set PR_SET_NO_NEW_PRIVS to block execve() gainable privileges."); | |
961 | } | |
962 | ||
0d9acb99 DE |
963 | /* Some init's such as busybox will set sane tty settings on stdin, |
964 | * stdout, stderr which it thinks is the console. We already set them | |
965 | * the way we wanted on the real terminal, and we want init to do its | |
408da065 CB |
966 | * setup on its console ie. the pty allocated in lxc_console_create() so |
967 | * make sure that that pty is stdin,stdout,stderr. | |
0d9acb99 | 968 | */ |
c5b93afb LF |
969 | if (handler->conf->console.slave >= 0) |
970 | if (set_stdfds(handler->conf->console.slave) < 0) { | |
971 | ERROR("Failed to redirect std{in,out,err} to pty file " | |
972 | "descriptor %d", | |
973 | handler->conf->console.slave); | |
974 | goto out_warn_father; | |
975 | } | |
0d9acb99 | 976 | |
408da065 | 977 | /* If we mounted a temporary proc, then unmount it now. */ |
5112cd70 | 978 | tmp_proc_unmount(handler->conf); |
e075f5d9 | 979 | |
8f2c3a70 SH |
980 | if (lxc_seccomp_load(handler->conf) != 0) |
981 | goto out_warn_father; | |
982 | ||
283678ed | 983 | if (run_lxc_hooks(handler->name, "start", handler->conf, handler->lxcpath, NULL)) { |
408da065 | 984 | ERROR("Failed to run lxc.hook.start for container \"%s\".", handler->name); |
773fb9ca SH |
985 | goto out_warn_father; |
986 | } | |
fc25b815 | 987 | |
773fb9ca | 988 | close(handler->sigfd); |
26ddeedd | 989 | |
7a55c157 TA |
990 | if (devnull_fd < 0) { |
991 | devnull_fd = open_devnull(); | |
992 | ||
993 | if (devnull_fd < 0) | |
994 | goto out_warn_father; | |
995 | } | |
996 | ||
c5b93afb LF |
997 | if (handler->conf->console.slave < 0 && handler->backgrounded) |
998 | if (set_stdfds(devnull_fd) < 0) { | |
999 | ERROR("Failed to redirect std{in,out,err} to " | |
1000 | "\"/dev/null\""); | |
1001 | goto out_warn_father; | |
1002 | } | |
507cee36 | 1003 | |
c44de748 AM |
1004 | if (devnull_fd >= 0) { |
1005 | close(devnull_fd); | |
1006 | devnull_fd = -1; | |
1007 | } | |
1008 | ||
8c9a7665 TA |
1009 | setsid(); |
1010 | ||
f4152036 CB |
1011 | if (lxc_sync_barrier_parent(handler, LXC_SYNC_CGROUP_LIMITS)) |
1012 | goto out_warn_father; | |
1013 | ||
149857af CB |
1014 | /* Reset the environment variables the user requested in a clear |
1015 | * environment. | |
1016 | */ | |
1017 | if (clearenv()) { | |
1018 | SYSERROR("Failed to clear environment."); | |
1019 | /* Don't error out though. */ | |
1020 | } | |
1021 | ||
1022 | lxc_list_for_each(iterator, &handler->conf->environment) { | |
1023 | if (putenv((char *)iterator->elem)) { | |
1024 | SYSERROR("Failed to set environment variable: %s.", (char *)iterator->elem); | |
1025 | goto out_warn_father; | |
1026 | } | |
1027 | } | |
1028 | ||
98ff08ca CB |
1029 | if (putenv("container=lxc")) { |
1030 | SYSERROR("Failed to set environment variable: container=lxc."); | |
1031 | goto out_warn_father; | |
1032 | } | |
1033 | ||
1034 | if (handler->conf->pty_names) { | |
1035 | if (putenv(handler->conf->pty_names)) { | |
1036 | SYSERROR("Failed to set environment variable for container ptys."); | |
1037 | goto out_warn_father; | |
1038 | } | |
1039 | } | |
1040 | ||
76bdf299 CB |
1041 | /* The container has been setup. We can now switch to an unprivileged |
1042 | * uid/gid. | |
1043 | */ | |
1044 | if (handler->conf->is_execute) { | |
1045 | bool have_cap_setgid; | |
1046 | uid_t new_uid = handler->conf->init_uid; | |
1047 | gid_t new_gid = handler->conf->init_gid; | |
1048 | ||
1049 | /* If we are in a new user namespace we already dropped all | |
1050 | * groups when we switched to root in the new user namespace | |
1051 | * further above. Only drop groups if we can, so ensure that we | |
1052 | * have necessary privilege. | |
1053 | */ | |
1054 | #if HAVE_LIBCAP | |
1055 | have_cap_setgid = lxc_proc_cap_is_set(CAP_SETGID, CAP_EFFECTIVE); | |
1056 | #else | |
1057 | have_cap_setgid = false; | |
1058 | #endif | |
1059 | if (lxc_list_empty(&handler->conf->id_map) && have_cap_setgid) { | |
1060 | if (lxc_setgroups(0, NULL) < 0) | |
1061 | goto out_warn_father; | |
1062 | } | |
1063 | ||
1064 | if (lxc_switch_uid_gid(new_uid, new_gid) < 0) | |
1065 | goto out_warn_father; | |
1066 | } | |
1067 | ||
408da065 CB |
1068 | /* After this call, we are in error because this ops should not return |
1069 | * as it execs. | |
1070 | */ | |
c4ea60df | 1071 | handler->ops->start(handler, handler->data); |
50e98013 DL |
1072 | |
1073 | out_warn_father: | |
408da065 CB |
1074 | /* We want the parent to know something went wrong, so we return a |
1075 | * special error code. | |
1076 | */ | |
d1ccb562 | 1077 | lxc_sync_wake_parent(handler, LXC_SYNC_ERROR); |
c44de748 AM |
1078 | |
1079 | out_error: | |
1080 | if (devnull_fd >= 0) | |
1081 | close(devnull_fd); | |
1082 | ||
50e98013 DL |
1083 | return -1; |
1084 | } | |
1085 | ||
ae467c54 | 1086 | static int lxc_recv_ttys_from_child(struct lxc_handler *handler) |
e8bd4e43 | 1087 | { |
ae467c54 | 1088 | int i; |
ae467c54 CB |
1089 | struct lxc_pty_info *pty_info; |
1090 | int ret = -1; | |
c6012571 | 1091 | int sock = handler->data_sock[1]; |
e8bd4e43 | 1092 | struct lxc_conf *conf = handler->conf; |
e8bd4e43 SH |
1093 | struct lxc_tty_info *tty_info = &conf->tty_info; |
1094 | ||
1095 | if (!conf->tty) | |
1096 | return 0; | |
1097 | ||
408da065 CB |
1098 | tty_info->pty_info = malloc(sizeof(*tty_info->pty_info) * conf->tty); |
1099 | if (!tty_info->pty_info) | |
e8bd4e43 | 1100 | return -1; |
e8bd4e43 | 1101 | |
672c1e58 CB |
1102 | for (i = 0; i < conf->tty; i++) { |
1103 | int ttyfds[2]; | |
1104 | ||
1105 | ret = lxc_abstract_unix_recv_fds(sock, ttyfds, 2, NULL, 0); | |
1106 | if (ret < 0) | |
1107 | break; | |
ae467c54 | 1108 | |
672c1e58 | 1109 | pty_info = &tty_info->pty_info[i]; |
e8bd4e43 | 1110 | pty_info->busy = 0; |
672c1e58 CB |
1111 | pty_info->master = ttyfds[0]; |
1112 | pty_info->slave = ttyfds[1]; | |
1113 | TRACE("Received pty with master fd %d and slave fd %d from " | |
ae467c54 | 1114 | "parent", pty_info->master, pty_info->slave); |
e8bd4e43 | 1115 | } |
ae467c54 | 1116 | if (ret < 0) |
672c1e58 | 1117 | ERROR("Failed to receive %d ttys from child: %s", conf->tty, |
ae467c54 CB |
1118 | strerror(errno)); |
1119 | else | |
672c1e58 CB |
1120 | TRACE("Received %d ttys from child", conf->tty); |
1121 | ||
1122 | tty_info->nbtty = conf->tty; | |
ae467c54 CB |
1123 | |
1124 | return ret; | |
e8bd4e43 SH |
1125 | } |
1126 | ||
f813849c TA |
1127 | void resolve_clone_flags(struct lxc_handler *handler) |
1128 | { | |
03df7ab5 | 1129 | handler->clone_flags = CLONE_NEWNS; |
f813849c | 1130 | |
408da065 | 1131 | if (!lxc_list_empty(&handler->conf->id_map)) |
f813849c | 1132 | handler->clone_flags |= CLONE_NEWUSER; |
f813849c TA |
1133 | |
1134 | if (handler->conf->inherit_ns_fd[LXC_NS_NET] == -1) { | |
408da065 | 1135 | if (!lxc_requests_empty_network(handler)) |
f813849c TA |
1136 | handler->clone_flags |= CLONE_NEWNET; |
1137 | } else { | |
198cbbaa | 1138 | INFO("Inheriting a NET namespace."); |
f813849c TA |
1139 | } |
1140 | ||
408da065 | 1141 | if (handler->conf->inherit_ns_fd[LXC_NS_IPC] == -1) |
f813849c | 1142 | handler->clone_flags |= CLONE_NEWIPC; |
408da065 | 1143 | else |
198cbbaa | 1144 | INFO("Inheriting an IPC namespace."); |
f813849c | 1145 | |
408da065 | 1146 | if (handler->conf->inherit_ns_fd[LXC_NS_UTS] == -1) |
f813849c | 1147 | handler->clone_flags |= CLONE_NEWUTS; |
408da065 | 1148 | else |
198cbbaa | 1149 | INFO("Inheriting a UTS namespace."); |
03df7ab5 L |
1150 | |
1151 | if (handler->conf->inherit_ns_fd[LXC_NS_PID] == -1) | |
1152 | handler->clone_flags |= CLONE_NEWPID; | |
1153 | else | |
1154 | INFO("Inheriting a PID namespace."); | |
f813849c TA |
1155 | } |
1156 | ||
480588e6 CB |
1157 | /* lxc_spawn() performs crucial setup tasks and clone()s the new process which |
1158 | * exec()s the requested container binary. | |
1159 | * Note that lxc_spawn() runs in the parent namespaces. Any operations performed | |
1160 | * right here should be double checked if they'd pose a security risk. (For | |
1161 | * example, any {u}mount() operations performed here will be reflected on the | |
1162 | * host!) | |
1163 | */ | |
74a3920a | 1164 | static int lxc_spawn(struct lxc_handler *handler) |
59eb99ba | 1165 | { |
7ab1ba02 | 1166 | int i, flags, ret; |
ffe1e01a | 1167 | const char *name = handler->name; |
08dd2805 | 1168 | char pidstr[20]; |
57927bf2 | 1169 | bool wants_to_map_ids; |
7ab1ba02 | 1170 | int saved_ns_fd[LXC_NS_MAX]; |
57927bf2 | 1171 | struct lxc_list *id_map; |
9e2bdae4 | 1172 | int preserve_mask = 0; |
d0b915aa | 1173 | bool cgroups_connected = false; |
9f30a190 | 1174 | |
57927bf2 CB |
1175 | id_map = &handler->conf->id_map; |
1176 | wants_to_map_ids = !lxc_list_empty(id_map); | |
2f2623ec | 1177 | |
9f30a190 | 1178 | for (i = 0; i < LXC_NS_MAX; i++) |
6c544cb3 | 1179 | if (handler->conf->inherit_ns_fd[i] != -1) |
9f30a190 | 1180 | preserve_mask |= ns_info[i].clone_flag; |
50e98013 | 1181 | |
3c22086f | 1182 | if (lxc_sync_init(handler)) |
9d7f9e52 | 1183 | return -1; |
0ad19a3f | 1184 | |
b9f522c5 | 1185 | ret = socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, |
790255cf | 1186 | handler->data_sock); |
c6012571 | 1187 | if (ret < 0) { |
e8bd4e43 SH |
1188 | lxc_sync_fini(handler); |
1189 | return -1; | |
1190 | } | |
1191 | ||
f813849c | 1192 | resolve_clone_flags(handler); |
9f30a190 | 1193 | |
f813849c | 1194 | if (handler->clone_flags & CLONE_NEWNET) { |
26b797f3 SH |
1195 | if (!lxc_list_empty(&handler->conf->network)) { |
1196 | ||
9f30a190 MM |
1197 | /* Find gateway addresses from the link device, which is |
1198 | * no longer accessible inside the container. Do this | |
1199 | * before creating network interfaces, since goto | |
408da065 CB |
1200 | * out_delete_net does not work before lxc_clone. |
1201 | */ | |
9f30a190 | 1202 | if (lxc_find_gateway_addresses(handler)) { |
408da065 | 1203 | ERROR("Failed to find gateway addresses."); |
9f30a190 MM |
1204 | lxc_sync_fini(handler); |
1205 | return -1; | |
1206 | } | |
1207 | ||
408da065 CB |
1208 | /* That should be done before the clone because we will |
1209 | * fill the netdev index and use them in the child. | |
9f30a190 | 1210 | */ |
811ef482 | 1211 | if (lxc_create_network_priv(handler)) { |
408da065 | 1212 | ERROR("Failed to create the network."); |
9f30a190 MM |
1213 | lxc_sync_fini(handler); |
1214 | return -1; | |
1215 | } | |
19a26f82 | 1216 | } |
82d5ae15 DL |
1217 | } |
1218 | ||
d4ef7c50 | 1219 | if (!cgroup_init(handler)) { |
408da065 | 1220 | ERROR("Failed initializing cgroup support."); |
33ad9f1a CS |
1221 | goto out_delete_net; |
1222 | } | |
1223 | ||
7e4dfe0b SH |
1224 | cgroups_connected = true; |
1225 | ||
d4ef7c50 | 1226 | if (!cgroup_create(handler)) { |
408da065 | 1227 | ERROR("Failed creating cgroups."); |
47d8fb3b CS |
1228 | goto out_delete_net; |
1229 | } | |
1230 | ||
408da065 CB |
1231 | /* If the rootfs is not a blockdev, prevent the container from marking |
1232 | * it readonly. | |
1233 | * If the container is unprivileged then skip rootfs pinning. | |
0c547523 | 1234 | */ |
0ee35059 | 1235 | if (!wants_to_map_ids) { |
5e32a990 ÇO |
1236 | handler->pinfd = pin_rootfs(handler->conf->rootfs.path); |
1237 | if (handler->pinfd == -1) | |
408da065 | 1238 | INFO("Failed to pin the rootfs for container \"%s\".", handler->name); |
5e32a990 | 1239 | } |
0c547523 | 1240 | |
4d8ac866 | 1241 | if (!preserve_ns(saved_ns_fd, preserve_mask, getpid())) |
cd43d2d1 | 1242 | goto out_delete_net; |
4d8ac866 | 1243 | |
cd43d2d1 SH |
1244 | if (attach_ns(handler->conf->inherit_ns_fd) < 0) |
1245 | goto out_delete_net; | |
9f30a190 | 1246 | |
408da065 | 1247 | /* Create a process in a new set of namespaces. */ |
5b1e83cb | 1248 | flags = handler->clone_flags; |
408da065 CB |
1249 | if (handler->clone_flags & CLONE_NEWUSER) { |
1250 | /* If CLONE_NEWUSER and CLONE_NEWNET was requested, we need to | |
1251 | * clone a new user namespace first and only later unshare our | |
1252 | * network namespace to ensure that network devices ownership is | |
1253 | * set up correctly. | |
1254 | */ | |
5b1e83cb | 1255 | flags &= ~CLONE_NEWNET; |
408da065 | 1256 | } |
732375f5 CB |
1257 | |
1258 | ret = lxc_setup_parent(handler); | |
1259 | if (ret < 0) | |
1260 | goto out_delete_net; | |
1261 | ||
9fac8fbb | 1262 | handler->pid = lxc_clone(do_start, handler, flags); |
59eb99ba | 1263 | if (handler->pid < 0) { |
408da065 | 1264 | SYSERROR("Failed to clone a new set of namespaces."); |
7fef7a06 | 1265 | goto out_delete_net; |
0ad19a3f | 1266 | } |
1bd8d726 | 1267 | |
9662e444 CB |
1268 | for (i = 0; i < LXC_NS_MAX; i++) |
1269 | if (flags & ns_info[i].clone_flag) | |
1270 | INFO("Cloned %s.", ns_info[i].flag_name); | |
0ad19a3f | 1271 | |
4d8ac866 CB |
1272 | if (!preserve_ns(handler->nsfd, handler->clone_flags | preserve_mask, handler->pid)) |
1273 | INFO("Failed to preserve namespace for lxc.hook.stop."); | |
b6b2b194 | 1274 | |
cd43d2d1 | 1275 | if (attach_ns(saved_ns_fd)) |
408da065 | 1276 | WARN("Failed to restore saved namespaces."); |
9f30a190 | 1277 | |
3c22086f CLG |
1278 | lxc_sync_fini_child(handler); |
1279 | ||
408da065 CB |
1280 | /* Map the container uids. The container became an invalid userid the |
1281 | * moment it was cloned with CLONE_NEWUSER. This call doesn't change | |
1282 | * anything immediately, but allows the container to setuid(0) (0 being | |
1283 | * mapped to something else on the host.) later to become a valid uid | |
1284 | * again. | |
1285 | */ | |
57927bf2 | 1286 | if (wants_to_map_ids && lxc_map_ids(id_map, handler->pid)) { |
408da065 | 1287 | ERROR("Failed to set up id mapping."); |
5b1e83cb SH |
1288 | goto out_delete_net; |
1289 | } | |
1290 | ||
9e2bdae4 | 1291 | if (lxc_sync_wake_child(handler, LXC_SYNC_STARTUP)) |
5b1e83cb | 1292 | goto out_delete_net; |
5b1e83cb | 1293 | |
9e2bdae4 | 1294 | if (lxc_sync_wait_child(handler, LXC_SYNC_CONFIGURE)) |
5b1e83cb | 1295 | goto out_delete_net; |
0ad19a3f | 1296 | |
d4ef7c50 | 1297 | if (!cgroup_create_legacy(handler)) { |
408da065 | 1298 | ERROR("Failed to setup legacy cgroups for container \"%s\".", name); |
ae5c8b8e | 1299 | goto out_delete_net; |
33ad9f1a | 1300 | } |
9daf6f5d | 1301 | if (!cgroup_setup_limits(handler, false)) { |
408da065 | 1302 | ERROR("Failed to setup cgroup limits for container \"%s\".", name); |
6031a6e5 DE |
1303 | goto out_delete_net; |
1304 | } | |
1305 | ||
d4ef7c50 | 1306 | if (!cgroup_enter(handler)) |
7fef7a06 | 1307 | goto out_delete_net; |
218d4250 | 1308 | |
0996e18a SH |
1309 | if (!cgroup_chown(handler)) |
1310 | goto out_delete_net; | |
1311 | ||
1bd8d726 CB |
1312 | handler->netnsfd = lxc_preserve_ns(handler->pid, "net"); |
1313 | if (handler->netnsfd < 0) { | |
1314 | ERROR("Failed to preserve network namespace"); | |
1315 | goto out_delete_net; | |
1316 | } | |
1317 | ||
408da065 | 1318 | /* Create the network configuration. */ |
d5088cf2 | 1319 | if (handler->clone_flags & CLONE_NEWNET) { |
74c6e2b0 CB |
1320 | if (lxc_network_move_created_netdev_priv(handler->lxcpath, |
1321 | handler->name, | |
1322 | &handler->conf->network, | |
1323 | handler->pid)) { | |
408da065 | 1324 | ERROR("Failed to create the configured network."); |
7fef7a06 | 1325 | goto out_delete_net; |
82d5ae15 | 1326 | } |
74c6e2b0 CB |
1327 | |
1328 | if (lxc_create_network_unpriv(handler->lxcpath, handler->name, | |
1329 | &handler->conf->network, | |
1330 | handler->pid)) { | |
1331 | ERROR("Failed to create the configured network."); | |
1332 | goto out_delete_net; | |
1333 | } | |
0ad19a3f | 1334 | } |
1335 | ||
7ab1ba02 CB |
1336 | if (lxc_network_send_veth_names_to_child(handler) < 0) { |
1337 | ERROR("Failed to send veth names to child"); | |
1338 | goto out_delete_net; | |
658979c5 SH |
1339 | } |
1340 | ||
408da065 CB |
1341 | /* Tell the child to continue its initialization. We'll get |
1342 | * LXC_SYNC_CGROUP when it is ready for us to setup cgroups. | |
3c22086f CLG |
1343 | */ |
1344 | if (lxc_sync_barrier_child(handler, LXC_SYNC_POST_CONFIGURE)) | |
544a48a0 SH |
1345 | goto out_delete_net; |
1346 | ||
c6d09e15 WB |
1347 | if (!lxc_list_empty(&handler->conf->limits) && setup_resource_limits(&handler->conf->limits, handler->pid)) { |
1348 | ERROR("failed to setup resource limits for '%s'", name); | |
84ff3af7 | 1349 | goto out_delete_net; |
c6d09e15 WB |
1350 | } |
1351 | ||
f4152036 CB |
1352 | if (lxc_sync_barrier_child(handler, LXC_SYNC_CGROUP_UNSHARE)) |
1353 | goto out_delete_net; | |
1354 | ||
9daf6f5d | 1355 | if (!cgroup_setup_limits(handler, true)) { |
408da065 | 1356 | ERROR("Failed to setup the devices cgroup for container \"%s\".", name); |
b98f7d6e | 1357 | goto out_delete_net; |
544a48a0 | 1358 | } |
f4152036 | 1359 | TRACE("Set up cgroup device limits"); |
544a48a0 | 1360 | |
73d28d42 | 1361 | cgroup_disconnect(); |
7e4dfe0b | 1362 | cgroups_connected = false; |
73d28d42 | 1363 | |
08dd2805 SH |
1364 | snprintf(pidstr, 20, "%d", handler->pid); |
1365 | if (setenv("LXC_PID", pidstr, 1)) | |
1366 | SYSERROR("Failed to set environment variable: LXC_PID=%s.", pidstr); | |
1367 | ||
1368 | /* Run any host-side start hooks */ | |
1369 | if (run_lxc_hooks(name, "start-host", handler->conf, handler->lxcpath, NULL)) { | |
1370 | ERROR("Failed to run lxc.hook.start-host for container \"%s\".", name); | |
1371 | return -1; | |
1372 | } | |
1373 | ||
408da065 CB |
1374 | /* Tell the child to complete its initialization and wait for it to exec |
1375 | * or return an error. (The child will never return | |
08dd2805 | 1376 | * LXC_SYNC_READY_START+1. It will either close the sync pipe, causing |
408da065 CB |
1377 | * lxc_sync_barrier_child to return success, or return a different |
1378 | * value, causing us to error out). | |
544a48a0 | 1379 | */ |
08dd2805 | 1380 | if (lxc_sync_barrier_child(handler, LXC_SYNC_READY_START)) |
3c22086f | 1381 | return -1; |
0ad19a3f | 1382 | |
790255cf CB |
1383 | if (lxc_network_recv_name_and_ifindex_from_child(handler) < 0) { |
1384 | ERROR("Failed to receive names and ifindices for network " | |
1385 | "devices from child"); | |
1386 | goto out_delete_net; | |
1387 | } | |
1388 | ||
1389 | /* Now all networks are created, network devices are moved into place, | |
1390 | * and the correct names and ifindeces in the respective namespaces have | |
1391 | * been recorded. The corresponding structs have now all been filled. So | |
1392 | * log them for debugging purposes. | |
1393 | */ | |
1394 | lxc_log_configured_netdevs(handler->conf); | |
1395 | ||
f4152036 CB |
1396 | /* Read tty fds allocated by child. */ |
1397 | if (lxc_recv_ttys_from_child(handler) < 0) { | |
1398 | ERROR("Failed to receive tty info from child process."); | |
1399 | goto out_delete_net; | |
1400 | } | |
1401 | ||
23c53af9 | 1402 | if (handler->ops->post_start(handler, handler->data)) |
e6126dbe MN |
1403 | goto out_abort; |
1404 | ||
25c2aca5 | 1405 | if (lxc_set_state(name, handler, RUNNING)) { |
408da065 CB |
1406 | ERROR("Failed to set state for container \"%s\" to \"%s\".", name, |
1407 | lxc_state2str(RUNNING)); | |
59eb99ba | 1408 | goto out_abort; |
3f21c114 | 1409 | } |
22ebac19 | 1410 | |
3c22086f | 1411 | lxc_sync_fini(handler); |
0c547523 | 1412 | |
e6126dbe | 1413 | return 0; |
1ac470c0 | 1414 | |
7fef7a06 | 1415 | out_delete_net: |
7e4dfe0b SH |
1416 | if (cgroups_connected) |
1417 | cgroup_disconnect(); | |
1bd8d726 | 1418 | |
bb84beda CB |
1419 | if (handler->clone_flags & CLONE_NEWNET) |
1420 | lxc_delete_network(handler); | |
1bd8d726 | 1421 | |
59eb99ba DL |
1422 | out_abort: |
1423 | lxc_abort(name, handler); | |
3c22086f | 1424 | lxc_sync_fini(handler); |
5c068da9 SH |
1425 | if (handler->pinfd >= 0) { |
1426 | close(handler->pinfd); | |
1427 | handler->pinfd = -1; | |
1428 | } | |
1429 | ||
1bd8d726 CB |
1430 | if (handler->netnsfd >= 0) { |
1431 | close(handler->netnsfd); | |
1432 | handler->netnsfd = -1; | |
1433 | } | |
1434 | ||
b79fcd86 | 1435 | return -1; |
59eb99ba | 1436 | } |
0ad19a3f | 1437 | |
aa460476 | 1438 | int __lxc_start(const char *name, struct lxc_handler *handler, |
507cee36 TA |
1439 | struct lxc_operations* ops, void *data, const char *lxcpath, |
1440 | bool backgrounded) | |
59eb99ba | 1441 | { |
59eb99ba | 1442 | int status; |
aa460476 | 1443 | int err = -1; |
aa460476 | 1444 | struct lxc_conf *conf = handler->conf; |
80090207 | 1445 | |
aa460476 | 1446 | if (lxc_init(name, handler) < 0) { |
408da065 | 1447 | ERROR("Failed to initialize container \"%s\".", name); |
66aeffc7 | 1448 | return -1; |
0ad19a3f | 1449 | } |
ee70bf78 CLG |
1450 | handler->ops = ops; |
1451 | handler->data = data; | |
507cee36 | 1452 | handler->backgrounded = backgrounded; |
738d0deb | 1453 | handler->netnsfd = -1; |
e6126dbe | 1454 | |
76a26f55 | 1455 | if (!attach_block_device(handler->conf)) { |
408da065 | 1456 | ERROR("Failed to attach block device."); |
76a26f55 SH |
1457 | goto out_fini_nonet; |
1458 | } | |
1459 | ||
35120d9c | 1460 | if (geteuid() == 0 && !lxc_list_empty(&conf->id_map)) { |
408da065 | 1461 | /* If the backing store is a device, mount it here and now. */ |
35120d9c SH |
1462 | if (rootfs_is_blockdev(conf)) { |
1463 | if (unshare(CLONE_NEWNS) < 0) { | |
408da065 | 1464 | ERROR("Failed to unshare CLONE_NEWNS."); |
35120d9c SH |
1465 | goto out_fini_nonet; |
1466 | } | |
408da065 CB |
1467 | INFO("Unshared CLONE_NEWNS."); |
1468 | ||
6a0c909a | 1469 | remount_all_slave(); |
35120d9c | 1470 | if (do_rootfs_setup(conf, name, lxcpath) < 0) { |
408da065 | 1471 | ERROR("Error setting up rootfs mount as root before spawn."); |
35120d9c SH |
1472 | goto out_fini_nonet; |
1473 | } | |
408da065 | 1474 | INFO("Set up container rootfs as host root."); |
35120d9c SH |
1475 | } |
1476 | } | |
1477 | ||
23c53af9 | 1478 | err = lxc_spawn(handler); |
59eb99ba | 1479 | if (err) { |
408da065 | 1480 | ERROR("Failed to spawn container \"%s\".", name); |
76a26f55 | 1481 | goto out_detach_blockdev; |
0ad19a3f | 1482 | } |
1483 | ||
8bee8851 WB |
1484 | handler->conf->reboot = 0; |
1485 | ||
3a0f472d | 1486 | err = lxc_poll(name, handler); |
e043236e | 1487 | if (err) { |
408da065 | 1488 | ERROR("LXC mainloop exited with error: %d.", err); |
59eb99ba DL |
1489 | goto out_abort; |
1490 | } | |
0ad19a3f | 1491 | |
3a0f472d | 1492 | while (waitpid(handler->pid, &status, 0) < 0 && errno == EINTR) |
1bc5cc8c | 1493 | continue; |
e043236e | 1494 | |
408da065 CB |
1495 | /* If the child process exited but was not signaled, it didn't call |
1496 | * reboot. This should mean it was an lxc-execute which simply exited. | |
1497 | * In any case, treat it as a 'halt'. | |
8b004f07 | 1498 | */ |
d028235d | 1499 | if (WIFSIGNALED(status)) { |
8b004f07 SH |
1500 | switch(WTERMSIG(status)) { |
1501 | case SIGINT: /* halt */ | |
408da065 | 1502 | DEBUG("Container \"%s\" is halting.", name); |
8b004f07 SH |
1503 | break; |
1504 | case SIGHUP: /* reboot */ | |
408da065 | 1505 | DEBUG("Container \"%s\" is rebooting.", name); |
8b004f07 SH |
1506 | handler->conf->reboot = 1; |
1507 | break; | |
c2b9bd9e | 1508 | case SIGSYS: /* seccomp */ |
408da065 | 1509 | DEBUG("Container \"%s\" violated its seccomp policy.", name); |
c2b9bd9e | 1510 | break; |
8b004f07 | 1511 | default: |
408da065 | 1512 | DEBUG("Unknown exit status for container \"%s\" init %d.", name, WTERMSIG(status)); |
8b004f07 SH |
1513 | break; |
1514 | } | |
d028235d | 1515 | } |
828695d9 | 1516 | |
b809f232 CB |
1517 | err = lxc_restore_phys_nics_to_netns(handler); |
1518 | if (err < 0) | |
1519 | ERROR("Failed to move physical network devices back to parent " | |
1520 | "network namespace"); | |
ce5782df | 1521 | |
5c068da9 SH |
1522 | if (handler->pinfd >= 0) { |
1523 | close(handler->pinfd); | |
1524 | handler->pinfd = -1; | |
1525 | } | |
1526 | ||
1787abca | 1527 | lxc_monitor_send_exit_code(name, status, handler->lxcpath); |
3a0f472d | 1528 | err = lxc_error_set_and_log(handler->pid, status); |
1bd8d726 | 1529 | |
9d7f9e52 | 1530 | out_fini: |
bb84beda | 1531 | lxc_delete_network(handler); |
74a2b586 | 1532 | |
76a26f55 SH |
1533 | out_detach_blockdev: |
1534 | detach_block_device(handler->conf); | |
1535 | ||
74a2b586 | 1536 | out_fini_nonet: |
3a0f472d | 1537 | lxc_fini(name, handler); |
0ad19a3f | 1538 | return err; |
1539 | ||
59eb99ba | 1540 | out_abort: |
3a0f472d | 1541 | lxc_abort(name, handler); |
9d7f9e52 | 1542 | goto out_fini; |
0ad19a3f | 1543 | } |
ee70bf78 CLG |
1544 | |
1545 | struct start_args { | |
1546 | char *const *argv; | |
1547 | }; | |
1548 | ||
1549 | static int start(struct lxc_handler *handler, void* data) | |
1550 | { | |
1551 | struct start_args *arg = data; | |
1552 | ||
408da065 | 1553 | NOTICE("Exec'ing \"%s\".", arg->argv[0]); |
ee70bf78 CLG |
1554 | |
1555 | execvp(arg->argv[0], arg->argv); | |
408da065 | 1556 | SYSERROR("Failed to exec \"%s\".", arg->argv[0]); |
ee70bf78 CLG |
1557 | return 0; |
1558 | } | |
1559 | ||
1560 | static int post_start(struct lxc_handler *handler, void* data) | |
1561 | { | |
1562 | struct start_args *arg = data; | |
1563 | ||
408da065 | 1564 | NOTICE("Started \"%s\" with pid \"%d\".", arg->argv[0], handler->pid); |
ee70bf78 CLG |
1565 | return 0; |
1566 | } | |
1567 | ||
1568 | static struct lxc_operations start_ops = { | |
1569 | .start = start, | |
1570 | .post_start = post_start | |
1571 | }; | |
1572 | ||
aa460476 | 1573 | int lxc_start(const char *name, char *const argv[], struct lxc_handler *handler, |
507cee36 | 1574 | const char *lxcpath, bool backgrounded) |
ee70bf78 CLG |
1575 | { |
1576 | struct start_args start_arg = { | |
1577 | .argv = argv, | |
1578 | }; | |
1579 | ||
aa460476 | 1580 | return __lxc_start(name, handler, &start_ops, &start_arg, lxcpath, backgrounded); |
ee70bf78 | 1581 | } |
28272964 CB |
1582 | |
1583 | static void lxc_destroy_container_on_signal(struct lxc_handler *handler, | |
1584 | const char *name) | |
1585 | { | |
1586 | char destroy[MAXPATHLEN]; | |
1587 | bool bret = true; | |
1588 | int ret = 0; | |
f01f7975 | 1589 | struct lxc_container *c; |
df31363a | 1590 | if (handler->conf->rootfs.path && handler->conf->rootfs.mount) { |
790255cf | 1591 | bret = do_destroy_container(handler); |
28272964 | 1592 | if (!bret) { |
408da065 | 1593 | ERROR("Error destroying rootfs for container \"%s\".", name); |
28272964 CB |
1594 | return; |
1595 | } | |
1596 | } | |
408da065 | 1597 | INFO("Destroyed rootfs for container \"%s\".", name); |
28272964 CB |
1598 | |
1599 | ret = snprintf(destroy, MAXPATHLEN, "%s/%s", handler->lxcpath, name); | |
1600 | if (ret < 0 || ret >= MAXPATHLEN) { | |
408da065 | 1601 | ERROR("Error destroying directory for container \"%s\".", name); |
28272964 CB |
1602 | return; |
1603 | } | |
1604 | ||
f01f7975 CB |
1605 | c = lxc_container_new(name, handler->lxcpath); |
1606 | if (c) { | |
1607 | if (container_disk_lock(c)) { | |
408da065 | 1608 | INFO("Could not update lxc_snapshots file."); |
f01f7975 CB |
1609 | lxc_container_put(c); |
1610 | } else { | |
1611 | mod_all_rdeps(c, false); | |
1612 | container_disk_unlock(c); | |
1613 | lxc_container_put(c); | |
1614 | } | |
1615 | } | |
1616 | ||
d0fbc7ba | 1617 | if (!handler->am_root) |
94b0a3ac CB |
1618 | ret = userns_exec_full(handler->conf, lxc_rmdir_onedev_wrapper, |
1619 | destroy, "lxc_rmdir_onedev_wrapper"); | |
28272964 CB |
1620 | else |
1621 | ret = lxc_rmdir_onedev(destroy, NULL); | |
1622 | ||
1623 | if (ret < 0) { | |
408da065 | 1624 | ERROR("Error destroying directory for container \"%s\".", name); |
28272964 CB |
1625 | return; |
1626 | } | |
408da065 | 1627 | INFO("Destroyed directory for container \"%s\".", name); |
28272964 CB |
1628 | } |
1629 | ||
1630 | static int lxc_rmdir_onedev_wrapper(void *data) | |
1631 | { | |
1632 | char *arg = (char *) data; | |
1633 | return lxc_rmdir_onedev(arg, NULL); | |
1634 | } | |
1635 | ||
790255cf | 1636 | static bool do_destroy_container(struct lxc_handler *handler) { |
94b0a3ac CB |
1637 | int ret; |
1638 | ||
d0fbc7ba | 1639 | if (!handler->am_root) { |
94b0a3ac CB |
1640 | ret = userns_exec_full(handler->conf, storage_destroy_wrapper, |
1641 | handler->conf, "storage_destroy_wrapper"); | |
1642 | if (ret < 0) | |
d028235d | 1643 | return false; |
10bc1861 | 1644 | |
d028235d SG |
1645 | return true; |
1646 | } | |
10bc1861 | 1647 | |
790255cf | 1648 | return storage_destroy(handler->conf); |
28272964 | 1649 | } |