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