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