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