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