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