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