]>
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 | */ | |
d0fbc7ba | 542 | handler->am_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 | ||
ffe1e01a | 791 | static int do_start(void *data) |
50e98013 | 792 | { |
7c661726 | 793 | struct lxc_list *iterator; |
23c53af9 | 794 | struct lxc_handler *handler = data; |
7a55c157 TA |
795 | int devnull_fd = -1, ret; |
796 | char path[PATH_MAX]; | |
50e98013 DL |
797 | |
798 | if (sigprocmask(SIG_SETMASK, &handler->oldmask, NULL)) { | |
408da065 | 799 | SYSERROR("Failed to set signal mask."); |
9d7f9e52 | 800 | return -1; |
50e98013 DL |
801 | } |
802 | ||
408da065 CB |
803 | /* This prctl must be before the synchro, so if the parent dies before |
804 | * we set the parent death signal, we will detect its death with the | |
805 | * synchro right after, otherwise we have a window where the parent can | |
806 | * exit before we set the pdeath signal leading to a unsupervized | |
807 | * container. | |
743ecd2e DL |
808 | */ |
809 | if (prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0)) { | |
408da065 | 810 | SYSERROR("Failed to set PR_SET_PDEATHSIG to SIGKILL."); |
743ecd2e DL |
811 | return -1; |
812 | } | |
813 | ||
3c22086f | 814 | lxc_sync_fini_parent(handler); |
50e98013 | 815 | |
408da065 | 816 | /* Don't leak the pinfd to the container. */ |
025ed0f3 | 817 | if (handler->pinfd >= 0) { |
0d03360a | 818 | close(handler->pinfd); |
025ed0f3 | 819 | } |
2b0e17e4 | 820 | |
5b1e83cb SH |
821 | if (lxc_sync_wait_parent(handler, LXC_SYNC_STARTUP)) |
822 | return -1; | |
823 | ||
408da065 CB |
824 | /* Unshare CLONE_NEWNET after CLONE_NEWUSER. See |
825 | * https://github.com/lxc/lxd/issues/1978. | |
826 | */ | |
5b1e83cb | 827 | if ((handler->clone_flags & (CLONE_NEWNET | CLONE_NEWUSER)) == |
408da065 | 828 | (CLONE_NEWNET | CLONE_NEWUSER)) { |
5b1e83cb SH |
829 | ret = unshare(CLONE_NEWNET); |
830 | if (ret < 0) { | |
408da065 | 831 | SYSERROR("Failed to unshare CLONE_NEWNET."); |
5b1e83cb SH |
832 | goto out_warn_father; |
833 | } | |
408da065 | 834 | INFO("Unshared CLONE_NEWNET."); |
5b1e83cb SH |
835 | } |
836 | ||
408da065 CB |
837 | /* Tell the parent task it can begin to configure the container and wait |
838 | * for it to finish. | |
3c22086f CLG |
839 | */ |
840 | if (lxc_sync_barrier_parent(handler, LXC_SYNC_CONFIGURE)) | |
9d7f9e52 | 841 | return -1; |
50e98013 | 842 | |
7ab1ba02 CB |
843 | if (lxc_network_recv_veth_names_from_parent(handler) < 0) { |
844 | ERROR("Failed to receive veth names from parent"); | |
658979c5 | 845 | goto out_warn_father; |
7ab1ba02 | 846 | } |
658979c5 | 847 | |
408da065 | 848 | /* If we are in a new user namespace, become root there to have |
d08f8d2f | 849 | * privilege over our namespace. |
f6d3e3e4 SH |
850 | */ |
851 | if (!lxc_list_empty(&handler->conf->id_map)) { | |
d08f8d2f | 852 | if (lxc_switch_uid_gid(0, 0) < 0) |
f6d3e3e4 | 853 | goto out_warn_father; |
d08f8d2f CB |
854 | |
855 | /* Drop groups only after we switched to a valid gid in the new | |
856 | * user namespace. | |
857 | */ | |
858 | if (lxc_setgroups(0, NULL) < 0) | |
c476bdce | 859 | goto out_warn_father; |
f6d3e3e4 SH |
860 | } |
861 | ||
99b71824 | 862 | if (access(handler->lxcpath, X_OK)) { |
c8154066 SH |
863 | print_top_failing_dir(handler->lxcpath); |
864 | goto out_warn_father; | |
865 | } | |
866 | ||
a91cde21 | 867 | ret = snprintf(path, sizeof(path), "%s/dev/null", handler->conf->rootfs.mount); |
408da065 | 868 | if (ret < 0 || ret >= sizeof(path)) |
7a55c157 | 869 | goto out_warn_father; |
7a55c157 TA |
870 | |
871 | /* In order to checkpoint restore, we need to have everything in the | |
872 | * same mount namespace. However, some containers may not have a | |
873 | * reasonable /dev (in particular, they may not have /dev/null), so we | |
874 | * can't set init's std fds to /dev/null by opening it from inside the | |
875 | * container. | |
876 | * | |
877 | * If that's the case, fall back to using the host's /dev/null. This | |
878 | * means that migration won't work, but at least we won't spew output | |
879 | * where it isn't wanted. | |
880 | */ | |
881 | if (handler->backgrounded && !handler->conf->autodev && access(path, F_OK) < 0) { | |
c44de748 AM |
882 | devnull_fd = open_devnull(); |
883 | ||
884 | if (devnull_fd < 0) | |
885 | goto out_warn_father; | |
408da065 CB |
886 | WARN("Using /dev/null from the host for container init's " |
887 | "standard file descriptors. Migration will not work."); | |
c44de748 AM |
888 | } |
889 | ||
408da065 | 890 | /* Ask father to setup cgroups and wait for him to finish. */ |
544a48a0 | 891 | if (lxc_sync_barrier_parent(handler, LXC_SYNC_CGROUP)) |
c44de748 | 892 | goto out_error; |
544a48a0 | 893 | |
deefdf8a CB |
894 | /* Unshare cgroup namespace after we have setup our cgroups. If we do it |
895 | * earlier we end up with a wrong view of /proc/self/cgroup. For | |
896 | * example, assume we unshare(CLONE_NEWCGROUP) first, and then create | |
897 | * the cgroup for the container, say /sys/fs/cgroup/cpuset/lxc/c, then | |
898 | * /proc/self/cgroup would show us: | |
899 | * | |
900 | * 8:cpuset:/lxc/c | |
901 | * | |
902 | * whereas it should actually show | |
903 | * | |
904 | * 8:cpuset:/ | |
905 | */ | |
906 | if (cgns_supported()) { | |
907 | if (unshare(CLONE_NEWCGROUP) < 0) { | |
908 | INFO("Failed to unshare CLONE_NEWCGROUP."); | |
909 | goto out_warn_father; | |
910 | } | |
911 | INFO("Unshared CLONE_NEWCGROUP."); | |
912 | } | |
913 | ||
f4152036 | 914 | /* Setup the container, ip, names, utsname, ... */ |
7729f8e5 CB |
915 | ret = lxc_setup(handler); |
916 | close(handler->data_sock[0]); | |
917 | close(handler->data_sock[1]); | |
918 | if (ret < 0) { | |
f4152036 CB |
919 | ERROR("Failed to setup container \"%s\".", handler->name); |
920 | goto out_warn_father; | |
921 | } | |
922 | ||
408da065 | 923 | /* Set the label to change to when we exec(2) the container's init. */ |
7aff4f43 | 924 | if (lsm_process_label_set(NULL, handler->conf, 1, 1) < 0) |
e075f5d9 | 925 | goto out_warn_father; |
5112cd70 | 926 | |
029cdff5 | 927 | /* Set PR_SET_NO_NEW_PRIVS after we changed the lsm label. If we do it |
408da065 CB |
928 | * before we aren't allowed anymore. |
929 | */ | |
029cdff5 CB |
930 | if (handler->conf->no_new_privs) { |
931 | if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) < 0) { | |
932 | SYSERROR("Could not set PR_SET_NO_NEW_PRIVS to block execve() gainable privileges."); | |
933 | goto out_warn_father; | |
934 | } | |
935 | DEBUG("Set PR_SET_NO_NEW_PRIVS to block execve() gainable privileges."); | |
936 | } | |
937 | ||
0d9acb99 DE |
938 | /* Some init's such as busybox will set sane tty settings on stdin, |
939 | * stdout, stderr which it thinks is the console. We already set them | |
940 | * the way we wanted on the real terminal, and we want init to do its | |
408da065 CB |
941 | * setup on its console ie. the pty allocated in lxc_console_create() so |
942 | * make sure that that pty is stdin,stdout,stderr. | |
0d9acb99 | 943 | */ |
c5b93afb LF |
944 | if (handler->conf->console.slave >= 0) |
945 | if (set_stdfds(handler->conf->console.slave) < 0) { | |
946 | ERROR("Failed to redirect std{in,out,err} to pty file " | |
947 | "descriptor %d", | |
948 | handler->conf->console.slave); | |
949 | goto out_warn_father; | |
950 | } | |
0d9acb99 | 951 | |
408da065 | 952 | /* If we mounted a temporary proc, then unmount it now. */ |
5112cd70 | 953 | tmp_proc_unmount(handler->conf); |
e075f5d9 | 954 | |
8f2c3a70 SH |
955 | if (lxc_seccomp_load(handler->conf) != 0) |
956 | goto out_warn_father; | |
957 | ||
283678ed | 958 | if (run_lxc_hooks(handler->name, "start", handler->conf, handler->lxcpath, NULL)) { |
408da065 | 959 | ERROR("Failed to run lxc.hook.start for container \"%s\".", handler->name); |
773fb9ca SH |
960 | goto out_warn_father; |
961 | } | |
fc25b815 | 962 | |
d08f8d2f CB |
963 | /* The container has been setup. We can now switch to an unprivileged |
964 | * uid/gid. | |
965 | */ | |
966 | if (handler->conf->is_execute) { | |
87bf0db0 CB |
967 | bool have_cap_setgid; |
968 | uid_t new_uid = handler->conf->init_uid; | |
969 | gid_t new_gid = handler->conf->init_gid; | |
d08f8d2f CB |
970 | |
971 | /* If we are in a new user namespace we already dropped all | |
972 | * groups when we switched to root in the new user namespace | |
973 | * further above. Only drop groups if we can, so ensure that we | |
974 | * have necessary privilege. | |
975 | */ | |
e37dda71 | 976 | #if HAVE_LIBCAP |
207c4c71 | 977 | have_cap_setgid = lxc_proc_cap_is_set(CAP_SETGID, CAP_EFFECTIVE); |
df11e022 BN |
978 | #else |
979 | have_cap_setgid = false; | |
980 | #endif | |
87bf0db0 | 981 | if (lxc_list_empty(&handler->conf->id_map) && have_cap_setgid) { |
d08f8d2f CB |
982 | if (lxc_setgroups(0, NULL) < 0) |
983 | goto out_warn_father; | |
984 | } | |
985 | ||
986 | if (lxc_switch_uid_gid(new_uid, new_gid) < 0) | |
987 | goto out_warn_father; | |
988 | } | |
989 | ||
408da065 CB |
990 | /* The clearenv() and putenv() calls have been moved here to allow us to |
991 | * use environment variables passed to the various hooks, such as the | |
992 | * start hook above. Not all of the variables like CONFIG_PATH or ROOTFS | |
993 | * are valid in this context but others are. | |
994 | */ | |
f7bee6c6 | 995 | if (clearenv()) { |
408da065 CB |
996 | SYSERROR("Failed to clear environment."); |
997 | /* Don't error out though. */ | |
f7bee6c6 MW |
998 | } |
999 | ||
7c661726 MP |
1000 | lxc_list_for_each(iterator, &handler->conf->environment) { |
1001 | if (putenv((char *)iterator->elem)) { | |
408da065 | 1002 | SYSERROR("Failed to set environment variable: %s.", (char *)iterator->elem); |
7c661726 MP |
1003 | goto out_warn_father; |
1004 | } | |
1005 | } | |
1006 | ||
f7bee6c6 | 1007 | if (putenv("container=lxc")) { |
408da065 | 1008 | SYSERROR("Failed to set environment variable: container=lxc."); |
c4ea60df | 1009 | goto out_warn_father; |
f7bee6c6 MW |
1010 | } |
1011 | ||
393903d1 SH |
1012 | if (handler->conf->pty_names) { |
1013 | if (putenv(handler->conf->pty_names)) { | |
408da065 | 1014 | SYSERROR("Failed to set environment variable for container ptys."); |
393903d1 SH |
1015 | goto out_warn_father; |
1016 | } | |
1017 | } | |
1018 | ||
773fb9ca | 1019 | close(handler->sigfd); |
26ddeedd | 1020 | |
7a55c157 TA |
1021 | if (devnull_fd < 0) { |
1022 | devnull_fd = open_devnull(); | |
1023 | ||
1024 | if (devnull_fd < 0) | |
1025 | goto out_warn_father; | |
1026 | } | |
1027 | ||
c5b93afb LF |
1028 | if (handler->conf->console.slave < 0 && handler->backgrounded) |
1029 | if (set_stdfds(devnull_fd) < 0) { | |
1030 | ERROR("Failed to redirect std{in,out,err} to " | |
1031 | "\"/dev/null\""); | |
1032 | goto out_warn_father; | |
1033 | } | |
507cee36 | 1034 | |
c44de748 AM |
1035 | if (devnull_fd >= 0) { |
1036 | close(devnull_fd); | |
1037 | devnull_fd = -1; | |
1038 | } | |
1039 | ||
8c9a7665 TA |
1040 | setsid(); |
1041 | ||
f4152036 CB |
1042 | if (lxc_sync_barrier_parent(handler, LXC_SYNC_CGROUP_LIMITS)) |
1043 | goto out_warn_father; | |
1044 | ||
408da065 CB |
1045 | /* After this call, we are in error because this ops should not return |
1046 | * as it execs. | |
1047 | */ | |
c4ea60df | 1048 | handler->ops->start(handler, handler->data); |
50e98013 DL |
1049 | |
1050 | out_warn_father: | |
408da065 CB |
1051 | /* We want the parent to know something went wrong, so we return a |
1052 | * special error code. | |
1053 | */ | |
d1ccb562 | 1054 | lxc_sync_wake_parent(handler, LXC_SYNC_ERROR); |
c44de748 AM |
1055 | |
1056 | out_error: | |
1057 | if (devnull_fd >= 0) | |
1058 | close(devnull_fd); | |
1059 | ||
50e98013 DL |
1060 | return -1; |
1061 | } | |
1062 | ||
ae467c54 | 1063 | static int lxc_recv_ttys_from_child(struct lxc_handler *handler) |
e8bd4e43 | 1064 | { |
ae467c54 | 1065 | int i; |
ae467c54 CB |
1066 | struct lxc_pty_info *pty_info; |
1067 | int ret = -1; | |
c6012571 | 1068 | int sock = handler->data_sock[1]; |
e8bd4e43 | 1069 | struct lxc_conf *conf = handler->conf; |
e8bd4e43 SH |
1070 | struct lxc_tty_info *tty_info = &conf->tty_info; |
1071 | ||
1072 | if (!conf->tty) | |
1073 | return 0; | |
1074 | ||
408da065 CB |
1075 | tty_info->pty_info = malloc(sizeof(*tty_info->pty_info) * conf->tty); |
1076 | if (!tty_info->pty_info) | |
e8bd4e43 | 1077 | return -1; |
e8bd4e43 | 1078 | |
672c1e58 CB |
1079 | for (i = 0; i < conf->tty; i++) { |
1080 | int ttyfds[2]; | |
1081 | ||
1082 | ret = lxc_abstract_unix_recv_fds(sock, ttyfds, 2, NULL, 0); | |
1083 | if (ret < 0) | |
1084 | break; | |
ae467c54 | 1085 | |
672c1e58 | 1086 | pty_info = &tty_info->pty_info[i]; |
e8bd4e43 | 1087 | pty_info->busy = 0; |
672c1e58 CB |
1088 | pty_info->master = ttyfds[0]; |
1089 | pty_info->slave = ttyfds[1]; | |
1090 | TRACE("Received pty with master fd %d and slave fd %d from " | |
ae467c54 | 1091 | "parent", pty_info->master, pty_info->slave); |
e8bd4e43 | 1092 | } |
ae467c54 | 1093 | if (ret < 0) |
672c1e58 | 1094 | ERROR("Failed to receive %d ttys from child: %s", conf->tty, |
ae467c54 CB |
1095 | strerror(errno)); |
1096 | else | |
672c1e58 CB |
1097 | TRACE("Received %d ttys from child", conf->tty); |
1098 | ||
1099 | tty_info->nbtty = conf->tty; | |
ae467c54 CB |
1100 | |
1101 | return ret; | |
e8bd4e43 SH |
1102 | } |
1103 | ||
f813849c TA |
1104 | void resolve_clone_flags(struct lxc_handler *handler) |
1105 | { | |
1106 | handler->clone_flags = CLONE_NEWPID | CLONE_NEWNS; | |
1107 | ||
408da065 | 1108 | if (!lxc_list_empty(&handler->conf->id_map)) |
f813849c | 1109 | handler->clone_flags |= CLONE_NEWUSER; |
f813849c TA |
1110 | |
1111 | if (handler->conf->inherit_ns_fd[LXC_NS_NET] == -1) { | |
408da065 | 1112 | if (!lxc_requests_empty_network(handler)) |
f813849c TA |
1113 | handler->clone_flags |= CLONE_NEWNET; |
1114 | } else { | |
198cbbaa | 1115 | INFO("Inheriting a NET namespace."); |
f813849c TA |
1116 | } |
1117 | ||
408da065 | 1118 | if (handler->conf->inherit_ns_fd[LXC_NS_IPC] == -1) |
f813849c | 1119 | handler->clone_flags |= CLONE_NEWIPC; |
408da065 | 1120 | else |
198cbbaa | 1121 | INFO("Inheriting an IPC namespace."); |
f813849c | 1122 | |
408da065 | 1123 | if (handler->conf->inherit_ns_fd[LXC_NS_UTS] == -1) |
f813849c | 1124 | handler->clone_flags |= CLONE_NEWUTS; |
408da065 | 1125 | else |
198cbbaa | 1126 | INFO("Inheriting a UTS namespace."); |
f813849c TA |
1127 | } |
1128 | ||
480588e6 CB |
1129 | /* lxc_spawn() performs crucial setup tasks and clone()s the new process which |
1130 | * exec()s the requested container binary. | |
1131 | * Note that lxc_spawn() runs in the parent namespaces. Any operations performed | |
1132 | * right here should be double checked if they'd pose a security risk. (For | |
1133 | * example, any {u}mount() operations performed here will be reflected on the | |
1134 | * host!) | |
1135 | */ | |
74a3920a | 1136 | static int lxc_spawn(struct lxc_handler *handler) |
59eb99ba | 1137 | { |
7ab1ba02 | 1138 | int i, flags, ret; |
ffe1e01a | 1139 | const char *name = handler->name; |
57927bf2 | 1140 | bool wants_to_map_ids; |
7ab1ba02 | 1141 | int saved_ns_fd[LXC_NS_MAX]; |
57927bf2 | 1142 | struct lxc_list *id_map; |
d0b915aa CB |
1143 | int failed_before_rename = 0, preserve_mask = 0; |
1144 | bool cgroups_connected = false; | |
9f30a190 | 1145 | |
57927bf2 CB |
1146 | id_map = &handler->conf->id_map; |
1147 | wants_to_map_ids = !lxc_list_empty(id_map); | |
2f2623ec | 1148 | |
9f30a190 | 1149 | for (i = 0; i < LXC_NS_MAX; i++) |
6c544cb3 | 1150 | if (handler->conf->inherit_ns_fd[i] != -1) |
9f30a190 | 1151 | preserve_mask |= ns_info[i].clone_flag; |
50e98013 | 1152 | |
3c22086f | 1153 | if (lxc_sync_init(handler)) |
9d7f9e52 | 1154 | return -1; |
0ad19a3f | 1155 | |
b9f522c5 | 1156 | ret = socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, |
790255cf | 1157 | handler->data_sock); |
c6012571 | 1158 | if (ret < 0) { |
e8bd4e43 SH |
1159 | lxc_sync_fini(handler); |
1160 | return -1; | |
1161 | } | |
1162 | ||
f813849c | 1163 | resolve_clone_flags(handler); |
9f30a190 | 1164 | |
f813849c | 1165 | if (handler->clone_flags & CLONE_NEWNET) { |
26b797f3 SH |
1166 | if (!lxc_list_empty(&handler->conf->network)) { |
1167 | ||
9f30a190 MM |
1168 | /* Find gateway addresses from the link device, which is |
1169 | * no longer accessible inside the container. Do this | |
1170 | * before creating network interfaces, since goto | |
408da065 CB |
1171 | * out_delete_net does not work before lxc_clone. |
1172 | */ | |
9f30a190 | 1173 | if (lxc_find_gateway_addresses(handler)) { |
408da065 | 1174 | ERROR("Failed to find gateway addresses."); |
9f30a190 MM |
1175 | lxc_sync_fini(handler); |
1176 | return -1; | |
1177 | } | |
1178 | ||
408da065 CB |
1179 | /* That should be done before the clone because we will |
1180 | * fill the netdev index and use them in the child. | |
9f30a190 | 1181 | */ |
811ef482 | 1182 | if (lxc_create_network_priv(handler)) { |
408da065 | 1183 | ERROR("Failed to create the network."); |
9f30a190 MM |
1184 | lxc_sync_fini(handler); |
1185 | return -1; | |
1186 | } | |
19a26f82 | 1187 | } |
82d5ae15 DL |
1188 | } |
1189 | ||
d4ef7c50 | 1190 | if (!cgroup_init(handler)) { |
408da065 | 1191 | ERROR("Failed initializing cgroup support."); |
33ad9f1a CS |
1192 | goto out_delete_net; |
1193 | } | |
1194 | ||
7e4dfe0b SH |
1195 | cgroups_connected = true; |
1196 | ||
d4ef7c50 | 1197 | if (!cgroup_create(handler)) { |
408da065 | 1198 | ERROR("Failed creating cgroups."); |
47d8fb3b CS |
1199 | goto out_delete_net; |
1200 | } | |
1201 | ||
408da065 CB |
1202 | /* If the rootfs is not a blockdev, prevent the container from marking |
1203 | * it readonly. | |
1204 | * If the container is unprivileged then skip rootfs pinning. | |
0c547523 | 1205 | */ |
0ee35059 | 1206 | if (!wants_to_map_ids) { |
5e32a990 ÇO |
1207 | handler->pinfd = pin_rootfs(handler->conf->rootfs.path); |
1208 | if (handler->pinfd == -1) | |
408da065 | 1209 | INFO("Failed to pin the rootfs for container \"%s\".", handler->name); |
5e32a990 | 1210 | } |
0c547523 | 1211 | |
4d8ac866 | 1212 | if (!preserve_ns(saved_ns_fd, preserve_mask, getpid())) |
cd43d2d1 | 1213 | goto out_delete_net; |
4d8ac866 | 1214 | |
cd43d2d1 SH |
1215 | if (attach_ns(handler->conf->inherit_ns_fd) < 0) |
1216 | goto out_delete_net; | |
9f30a190 | 1217 | |
408da065 | 1218 | /* Create a process in a new set of namespaces. */ |
5b1e83cb | 1219 | flags = handler->clone_flags; |
408da065 CB |
1220 | if (handler->clone_flags & CLONE_NEWUSER) { |
1221 | /* If CLONE_NEWUSER and CLONE_NEWNET was requested, we need to | |
1222 | * clone a new user namespace first and only later unshare our | |
1223 | * network namespace to ensure that network devices ownership is | |
1224 | * set up correctly. | |
1225 | */ | |
5b1e83cb | 1226 | flags &= ~CLONE_NEWNET; |
408da065 | 1227 | } |
9fac8fbb | 1228 | handler->pid = lxc_clone(do_start, handler, flags); |
59eb99ba | 1229 | if (handler->pid < 0) { |
408da065 | 1230 | SYSERROR("Failed to clone a new set of namespaces."); |
7fef7a06 | 1231 | goto out_delete_net; |
0ad19a3f | 1232 | } |
1bd8d726 | 1233 | |
9662e444 CB |
1234 | for (i = 0; i < LXC_NS_MAX; i++) |
1235 | if (flags & ns_info[i].clone_flag) | |
1236 | INFO("Cloned %s.", ns_info[i].flag_name); | |
0ad19a3f | 1237 | |
4d8ac866 CB |
1238 | if (!preserve_ns(handler->nsfd, handler->clone_flags | preserve_mask, handler->pid)) |
1239 | INFO("Failed to preserve namespace for lxc.hook.stop."); | |
b6b2b194 | 1240 | |
cd43d2d1 | 1241 | if (attach_ns(saved_ns_fd)) |
408da065 | 1242 | WARN("Failed to restore saved namespaces."); |
9f30a190 | 1243 | |
3c22086f CLG |
1244 | lxc_sync_fini_child(handler); |
1245 | ||
408da065 CB |
1246 | /* Map the container uids. The container became an invalid userid the |
1247 | * moment it was cloned with CLONE_NEWUSER. This call doesn't change | |
1248 | * anything immediately, but allows the container to setuid(0) (0 being | |
1249 | * mapped to something else on the host.) later to become a valid uid | |
1250 | * again. | |
1251 | */ | |
57927bf2 | 1252 | if (wants_to_map_ids && lxc_map_ids(id_map, handler->pid)) { |
408da065 | 1253 | ERROR("Failed to set up id mapping."); |
5b1e83cb SH |
1254 | goto out_delete_net; |
1255 | } | |
1256 | ||
1257 | if (lxc_sync_wake_child(handler, LXC_SYNC_STARTUP)) { | |
99a6af52 | 1258 | failed_before_rename = 1; |
5b1e83cb SH |
1259 | goto out_delete_net; |
1260 | } | |
1261 | ||
1262 | if (lxc_sync_wait_child(handler, LXC_SYNC_CONFIGURE)) { | |
1263 | failed_before_rename = 1; | |
1264 | goto out_delete_net; | |
1265 | } | |
0ad19a3f | 1266 | |
d4ef7c50 | 1267 | if (!cgroup_create_legacy(handler)) { |
408da065 | 1268 | ERROR("Failed to setup legacy cgroups for container \"%s\".", name); |
ae5c8b8e | 1269 | goto out_delete_net; |
33ad9f1a | 1270 | } |
9daf6f5d | 1271 | if (!cgroup_setup_limits(handler, false)) { |
408da065 | 1272 | ERROR("Failed to setup cgroup limits for container \"%s\".", name); |
6031a6e5 DE |
1273 | goto out_delete_net; |
1274 | } | |
1275 | ||
d4ef7c50 | 1276 | if (!cgroup_enter(handler)) |
7fef7a06 | 1277 | goto out_delete_net; |
218d4250 | 1278 | |
0996e18a SH |
1279 | if (!cgroup_chown(handler)) |
1280 | goto out_delete_net; | |
1281 | ||
99a6af52 MN |
1282 | if (failed_before_rename) |
1283 | goto out_delete_net; | |
1284 | ||
1bd8d726 CB |
1285 | handler->netnsfd = lxc_preserve_ns(handler->pid, "net"); |
1286 | if (handler->netnsfd < 0) { | |
1287 | ERROR("Failed to preserve network namespace"); | |
1288 | goto out_delete_net; | |
1289 | } | |
1290 | ||
408da065 | 1291 | /* Create the network configuration. */ |
d5088cf2 | 1292 | if (handler->clone_flags & CLONE_NEWNET) { |
74c6e2b0 CB |
1293 | if (lxc_network_move_created_netdev_priv(handler->lxcpath, |
1294 | handler->name, | |
1295 | &handler->conf->network, | |
1296 | handler->pid)) { | |
408da065 | 1297 | ERROR("Failed to create the configured network."); |
7fef7a06 | 1298 | goto out_delete_net; |
82d5ae15 | 1299 | } |
74c6e2b0 CB |
1300 | |
1301 | if (lxc_create_network_unpriv(handler->lxcpath, handler->name, | |
1302 | &handler->conf->network, | |
1303 | handler->pid)) { | |
1304 | ERROR("Failed to create the configured network."); | |
1305 | goto out_delete_net; | |
1306 | } | |
0ad19a3f | 1307 | } |
1308 | ||
7ab1ba02 CB |
1309 | if (lxc_network_send_veth_names_to_child(handler) < 0) { |
1310 | ERROR("Failed to send veth names to child"); | |
1311 | goto out_delete_net; | |
658979c5 SH |
1312 | } |
1313 | ||
408da065 CB |
1314 | /* Tell the child to continue its initialization. We'll get |
1315 | * LXC_SYNC_CGROUP when it is ready for us to setup cgroups. | |
3c22086f CLG |
1316 | */ |
1317 | if (lxc_sync_barrier_child(handler, LXC_SYNC_POST_CONFIGURE)) | |
544a48a0 SH |
1318 | goto out_delete_net; |
1319 | ||
c6d09e15 WB |
1320 | if (!lxc_list_empty(&handler->conf->limits) && setup_resource_limits(&handler->conf->limits, handler->pid)) { |
1321 | ERROR("failed to setup resource limits for '%s'", name); | |
84ff3af7 | 1322 | goto out_delete_net; |
c6d09e15 WB |
1323 | } |
1324 | ||
f4152036 CB |
1325 | if (lxc_sync_barrier_child(handler, LXC_SYNC_CGROUP_UNSHARE)) |
1326 | goto out_delete_net; | |
1327 | ||
9daf6f5d | 1328 | if (!cgroup_setup_limits(handler, true)) { |
408da065 | 1329 | ERROR("Failed to setup the devices cgroup for container \"%s\".", name); |
b98f7d6e | 1330 | goto out_delete_net; |
544a48a0 | 1331 | } |
f4152036 | 1332 | TRACE("Set up cgroup device limits"); |
544a48a0 | 1333 | |
73d28d42 | 1334 | cgroup_disconnect(); |
7e4dfe0b | 1335 | cgroups_connected = false; |
73d28d42 | 1336 | |
408da065 CB |
1337 | /* Tell the child to complete its initialization and wait for it to exec |
1338 | * or return an error. (The child will never return | |
1339 | * LXC_SYNC_POST_CGROUP+1. It will either close the sync pipe, causing | |
1340 | * lxc_sync_barrier_child to return success, or return a different | |
1341 | * value, causing us to error out). | |
544a48a0 SH |
1342 | */ |
1343 | if (lxc_sync_barrier_child(handler, LXC_SYNC_POST_CGROUP)) | |
3c22086f | 1344 | return -1; |
0ad19a3f | 1345 | |
790255cf CB |
1346 | if (lxc_network_recv_name_and_ifindex_from_child(handler) < 0) { |
1347 | ERROR("Failed to receive names and ifindices for network " | |
1348 | "devices from child"); | |
1349 | goto out_delete_net; | |
1350 | } | |
1351 | ||
1352 | /* Now all networks are created, network devices are moved into place, | |
1353 | * and the correct names and ifindeces in the respective namespaces have | |
1354 | * been recorded. The corresponding structs have now all been filled. So | |
1355 | * log them for debugging purposes. | |
1356 | */ | |
1357 | lxc_log_configured_netdevs(handler->conf); | |
1358 | ||
f4152036 CB |
1359 | /* Read tty fds allocated by child. */ |
1360 | if (lxc_recv_ttys_from_child(handler) < 0) { | |
1361 | ERROR("Failed to receive tty info from child process."); | |
1362 | goto out_delete_net; | |
1363 | } | |
1364 | ||
23c53af9 | 1365 | if (handler->ops->post_start(handler, handler->data)) |
e6126dbe MN |
1366 | goto out_abort; |
1367 | ||
25c2aca5 | 1368 | if (lxc_set_state(name, handler, RUNNING)) { |
408da065 CB |
1369 | ERROR("Failed to set state for container \"%s\" to \"%s\".", name, |
1370 | lxc_state2str(RUNNING)); | |
59eb99ba | 1371 | goto out_abort; |
3f21c114 | 1372 | } |
22ebac19 | 1373 | |
3c22086f | 1374 | lxc_sync_fini(handler); |
0c547523 | 1375 | |
e6126dbe | 1376 | return 0; |
1ac470c0 | 1377 | |
7fef7a06 | 1378 | out_delete_net: |
7e4dfe0b SH |
1379 | if (cgroups_connected) |
1380 | cgroup_disconnect(); | |
1bd8d726 CB |
1381 | |
1382 | if (handler->clone_flags & CLONE_NEWNET) { | |
1383 | DEBUG("Tearing down network devices"); | |
1384 | if (!lxc_delete_network_priv(handler)) | |
1385 | DEBUG("Failed tearing down network devices"); | |
1386 | ||
1387 | if (!lxc_delete_network_unpriv(handler)) | |
1388 | DEBUG("Failed tearing down network devices"); | |
1389 | } | |
1390 | ||
59eb99ba DL |
1391 | out_abort: |
1392 | lxc_abort(name, handler); | |
3c22086f | 1393 | lxc_sync_fini(handler); |
5c068da9 SH |
1394 | if (handler->pinfd >= 0) { |
1395 | close(handler->pinfd); | |
1396 | handler->pinfd = -1; | |
1397 | } | |
1398 | ||
1bd8d726 CB |
1399 | if (handler->netnsfd >= 0) { |
1400 | close(handler->netnsfd); | |
1401 | handler->netnsfd = -1; | |
1402 | } | |
1403 | ||
b79fcd86 | 1404 | return -1; |
59eb99ba | 1405 | } |
0ad19a3f | 1406 | |
aa460476 | 1407 | int __lxc_start(const char *name, struct lxc_handler *handler, |
507cee36 TA |
1408 | struct lxc_operations* ops, void *data, const char *lxcpath, |
1409 | bool backgrounded) | |
59eb99ba | 1410 | { |
59eb99ba | 1411 | int status; |
aa460476 | 1412 | int err = -1; |
aa460476 | 1413 | struct lxc_conf *conf = handler->conf; |
80090207 | 1414 | |
aa460476 | 1415 | if (lxc_init(name, handler) < 0) { |
408da065 | 1416 | ERROR("Failed to initialize container \"%s\".", name); |
66aeffc7 | 1417 | return -1; |
0ad19a3f | 1418 | } |
ee70bf78 CLG |
1419 | handler->ops = ops; |
1420 | handler->data = data; | |
507cee36 | 1421 | handler->backgrounded = backgrounded; |
738d0deb | 1422 | handler->netnsfd = -1; |
e6126dbe | 1423 | |
76a26f55 | 1424 | if (!attach_block_device(handler->conf)) { |
408da065 | 1425 | ERROR("Failed to attach block device."); |
76a26f55 SH |
1426 | goto out_fini_nonet; |
1427 | } | |
1428 | ||
35120d9c | 1429 | if (geteuid() == 0 && !lxc_list_empty(&conf->id_map)) { |
408da065 | 1430 | /* If the backing store is a device, mount it here and now. */ |
35120d9c SH |
1431 | if (rootfs_is_blockdev(conf)) { |
1432 | if (unshare(CLONE_NEWNS) < 0) { | |
408da065 | 1433 | ERROR("Failed to unshare CLONE_NEWNS."); |
35120d9c SH |
1434 | goto out_fini_nonet; |
1435 | } | |
408da065 CB |
1436 | INFO("Unshared CLONE_NEWNS."); |
1437 | ||
6a0c909a | 1438 | remount_all_slave(); |
35120d9c | 1439 | if (do_rootfs_setup(conf, name, lxcpath) < 0) { |
408da065 | 1440 | ERROR("Error setting up rootfs mount as root before spawn."); |
35120d9c SH |
1441 | goto out_fini_nonet; |
1442 | } | |
408da065 | 1443 | INFO("Set up container rootfs as host root."); |
35120d9c SH |
1444 | } |
1445 | } | |
1446 | ||
23c53af9 | 1447 | err = lxc_spawn(handler); |
59eb99ba | 1448 | if (err) { |
408da065 | 1449 | ERROR("Failed to spawn container \"%s\".", name); |
76a26f55 | 1450 | goto out_detach_blockdev; |
0ad19a3f | 1451 | } |
1452 | ||
8bee8851 WB |
1453 | handler->conf->reboot = 0; |
1454 | ||
3a0f472d | 1455 | err = lxc_poll(name, handler); |
e043236e | 1456 | if (err) { |
408da065 | 1457 | ERROR("LXC mainloop exited with error: %d.", err); |
59eb99ba DL |
1458 | goto out_abort; |
1459 | } | |
0ad19a3f | 1460 | |
3a0f472d | 1461 | while (waitpid(handler->pid, &status, 0) < 0 && errno == EINTR) |
1bc5cc8c | 1462 | continue; |
e043236e | 1463 | |
408da065 CB |
1464 | /* If the child process exited but was not signaled, it didn't call |
1465 | * reboot. This should mean it was an lxc-execute which simply exited. | |
1466 | * In any case, treat it as a 'halt'. | |
8b004f07 | 1467 | */ |
d028235d | 1468 | if (WIFSIGNALED(status)) { |
8b004f07 SH |
1469 | switch(WTERMSIG(status)) { |
1470 | case SIGINT: /* halt */ | |
408da065 | 1471 | DEBUG("Container \"%s\" is halting.", name); |
8b004f07 SH |
1472 | break; |
1473 | case SIGHUP: /* reboot */ | |
408da065 | 1474 | DEBUG("Container \"%s\" is rebooting.", name); |
8b004f07 SH |
1475 | handler->conf->reboot = 1; |
1476 | break; | |
c2b9bd9e | 1477 | case SIGSYS: /* seccomp */ |
408da065 | 1478 | DEBUG("Container \"%s\" violated its seccomp policy.", name); |
c2b9bd9e | 1479 | break; |
8b004f07 | 1480 | default: |
408da065 | 1481 | DEBUG("Unknown exit status for container \"%s\" init %d.", name, WTERMSIG(status)); |
8b004f07 SH |
1482 | break; |
1483 | } | |
d028235d | 1484 | } |
828695d9 | 1485 | |
b809f232 CB |
1486 | err = lxc_restore_phys_nics_to_netns(handler); |
1487 | if (err < 0) | |
1488 | ERROR("Failed to move physical network devices back to parent " | |
1489 | "network namespace"); | |
ce5782df | 1490 | |
5c068da9 SH |
1491 | if (handler->pinfd >= 0) { |
1492 | close(handler->pinfd); | |
1493 | handler->pinfd = -1; | |
1494 | } | |
1495 | ||
1787abca | 1496 | lxc_monitor_send_exit_code(name, status, handler->lxcpath); |
3a0f472d | 1497 | err = lxc_error_set_and_log(handler->pid, status); |
1bd8d726 | 1498 | |
9d7f9e52 | 1499 | out_fini: |
1bd8d726 CB |
1500 | DEBUG("Tearing down network devices"); |
1501 | if (!lxc_delete_network_priv(handler)) | |
1502 | DEBUG("Failed tearing down network devices"); | |
1503 | ||
1504 | if (!lxc_delete_network_unpriv(handler)) | |
1505 | DEBUG("Failed tearing down network devices"); | |
1506 | ||
1507 | if (handler->netnsfd >= 0) { | |
1508 | close(handler->netnsfd); | |
1509 | handler->netnsfd = -1; | |
358daf49 | 1510 | } |
74a2b586 | 1511 | |
76a26f55 SH |
1512 | out_detach_blockdev: |
1513 | detach_block_device(handler->conf); | |
1514 | ||
74a2b586 | 1515 | out_fini_nonet: |
3a0f472d | 1516 | lxc_fini(name, handler); |
0ad19a3f | 1517 | return err; |
1518 | ||
59eb99ba | 1519 | out_abort: |
3a0f472d | 1520 | lxc_abort(name, handler); |
9d7f9e52 | 1521 | goto out_fini; |
0ad19a3f | 1522 | } |
ee70bf78 CLG |
1523 | |
1524 | struct start_args { | |
1525 | char *const *argv; | |
1526 | }; | |
1527 | ||
1528 | static int start(struct lxc_handler *handler, void* data) | |
1529 | { | |
1530 | struct start_args *arg = data; | |
1531 | ||
408da065 | 1532 | NOTICE("Exec'ing \"%s\".", arg->argv[0]); |
ee70bf78 CLG |
1533 | |
1534 | execvp(arg->argv[0], arg->argv); | |
408da065 | 1535 | SYSERROR("Failed to exec \"%s\".", arg->argv[0]); |
ee70bf78 CLG |
1536 | return 0; |
1537 | } | |
1538 | ||
1539 | static int post_start(struct lxc_handler *handler, void* data) | |
1540 | { | |
1541 | struct start_args *arg = data; | |
1542 | ||
408da065 | 1543 | NOTICE("Started \"%s\" with pid \"%d\".", arg->argv[0], handler->pid); |
ee70bf78 CLG |
1544 | return 0; |
1545 | } | |
1546 | ||
1547 | static struct lxc_operations start_ops = { | |
1548 | .start = start, | |
1549 | .post_start = post_start | |
1550 | }; | |
1551 | ||
aa460476 | 1552 | int lxc_start(const char *name, char *const argv[], struct lxc_handler *handler, |
507cee36 | 1553 | const char *lxcpath, bool backgrounded) |
ee70bf78 CLG |
1554 | { |
1555 | struct start_args start_arg = { | |
1556 | .argv = argv, | |
1557 | }; | |
1558 | ||
aa460476 | 1559 | return __lxc_start(name, handler, &start_ops, &start_arg, lxcpath, backgrounded); |
ee70bf78 | 1560 | } |
28272964 CB |
1561 | |
1562 | static void lxc_destroy_container_on_signal(struct lxc_handler *handler, | |
1563 | const char *name) | |
1564 | { | |
1565 | char destroy[MAXPATHLEN]; | |
1566 | bool bret = true; | |
1567 | int ret = 0; | |
f01f7975 | 1568 | struct lxc_container *c; |
df31363a | 1569 | if (handler->conf->rootfs.path && handler->conf->rootfs.mount) { |
790255cf | 1570 | bret = do_destroy_container(handler); |
28272964 | 1571 | if (!bret) { |
408da065 | 1572 | ERROR("Error destroying rootfs for container \"%s\".", name); |
28272964 CB |
1573 | return; |
1574 | } | |
1575 | } | |
408da065 | 1576 | INFO("Destroyed rootfs for container \"%s\".", name); |
28272964 CB |
1577 | |
1578 | ret = snprintf(destroy, MAXPATHLEN, "%s/%s", handler->lxcpath, name); | |
1579 | if (ret < 0 || ret >= MAXPATHLEN) { | |
408da065 | 1580 | ERROR("Error destroying directory for container \"%s\".", name); |
28272964 CB |
1581 | return; |
1582 | } | |
1583 | ||
f01f7975 CB |
1584 | c = lxc_container_new(name, handler->lxcpath); |
1585 | if (c) { | |
1586 | if (container_disk_lock(c)) { | |
408da065 | 1587 | INFO("Could not update lxc_snapshots file."); |
f01f7975 CB |
1588 | lxc_container_put(c); |
1589 | } else { | |
1590 | mod_all_rdeps(c, false); | |
1591 | container_disk_unlock(c); | |
1592 | lxc_container_put(c); | |
1593 | } | |
1594 | } | |
1595 | ||
d0fbc7ba | 1596 | if (!handler->am_root) |
c9b7c33e CB |
1597 | ret = userns_exec_1(handler->conf, lxc_rmdir_onedev_wrapper, |
1598 | destroy, "lxc_rmdir_onedev_wrapper"); | |
28272964 CB |
1599 | else |
1600 | ret = lxc_rmdir_onedev(destroy, NULL); | |
1601 | ||
1602 | if (ret < 0) { | |
408da065 | 1603 | ERROR("Error destroying directory for container \"%s\".", name); |
28272964 CB |
1604 | return; |
1605 | } | |
408da065 | 1606 | INFO("Destroyed directory for container \"%s\".", name); |
28272964 CB |
1607 | } |
1608 | ||
1609 | static int lxc_rmdir_onedev_wrapper(void *data) | |
1610 | { | |
1611 | char *arg = (char *) data; | |
1612 | return lxc_rmdir_onedev(arg, NULL); | |
1613 | } | |
1614 | ||
790255cf | 1615 | static bool do_destroy_container(struct lxc_handler *handler) { |
d0fbc7ba | 1616 | if (!handler->am_root) { |
790255cf CB |
1617 | if (userns_exec_1(handler->conf, storage_destroy_wrapper, |
1618 | handler->conf, "storage_destroy_wrapper") < 0) | |
d028235d | 1619 | return false; |
10bc1861 | 1620 | |
d028235d SG |
1621 | return true; |
1622 | } | |
10bc1861 | 1623 | |
790255cf | 1624 | return storage_destroy(handler->conf); |
28272964 | 1625 | } |