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