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