]>
Commit | Line | Data |
---|---|---|
0ad19a3f | 1 | /* |
2 | * lxc: linux Container library | |
3 | * | |
4 | * (C) Copyright IBM Corp. 2007, 2008 | |
5 | * | |
6 | * Authors: | |
9afe19d6 | 7 | * Daniel Lezcano <daniel.lezcano at free.fr> |
0ad19a3f | 8 | * |
9 | * This library is free software; you can redistribute it and/or | |
10 | * modify it under the terms of the GNU Lesser General Public | |
11 | * License as published by the Free Software Foundation; either | |
12 | * version 2.1 of the License, or (at your option) any later version. | |
13 | * | |
14 | * This library is distributed in the hope that it will be useful, | |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
17 | * Lesser General Public License for more details. | |
18 | * | |
19 | * You should have received a copy of the GNU Lesser General Public | |
20 | * License along with this library; if not, write to the Free Software | |
250b1eec | 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
0ad19a3f | 22 | */ |
23 | ||
f3787121 | 24 | #define _GNU_SOURCE |
f549edcc GK |
25 | #include "config.h" |
26 | ||
f3787121 | 27 | #include <alloca.h> |
0ad19a3f | 28 | #include <dirent.h> |
29 | #include <errno.h> | |
b0a33c1e | 30 | #include <fcntl.h> |
c476bdce | 31 | #include <grp.h> |
37515ebd | 32 | #include <poll.h> |
f3787121 CB |
33 | #include <signal.h> |
34 | #include <stdio.h> | |
35 | #include <stdlib.h> | |
36 | #include <string.h> | |
37 | #include <unistd.h> | |
0ad19a3f | 38 | #include <sys/file.h> |
f4d507d5 | 39 | #include <sys/mount.h> |
f3787121 | 40 | #include <sys/param.h> |
0ad19a3f | 41 | #include <sys/prctl.h> |
f3787121 CB |
42 | #include <sys/socket.h> |
43 | #include <sys/stat.h> | |
44 | #include <sys/syscall.h> | |
ddceb1f9 | 45 | #include <sys/types.h> |
b0a33c1e | 46 | #include <sys/un.h> |
f3787121 | 47 | #include <sys/wait.h> |
ff218c25 | 48 | |
495d2046 SG |
49 | #if HAVE_SYS_CAPABILITY_H |
50 | #include <sys/capability.h> | |
51 | #endif | |
52 | ||
955e2a02 | 53 | #ifndef HAVE_DECL_PR_CAPBSET_DROP |
656994bb MH |
54 | #define PR_CAPBSET_DROP 24 |
55 | #endif | |
56 | ||
955e2a02 CB |
57 | #ifndef HAVE_DECL_PR_SET_NO_NEW_PRIVS |
58 | #define PR_SET_NO_NEW_PRIVS 38 | |
59 | #endif | |
60 | ||
61 | #ifndef HAVE_DECL_PR_GET_NO_NEW_PRIVS | |
62 | #define PR_GET_NO_NEW_PRIVS 39 | |
63 | #endif | |
64 | ||
f3787121 | 65 | #include "af_unix.h" |
d8e48992 | 66 | #include "bdev.h" |
f3787121 | 67 | #include "caps.h" |
563f2f2c | 68 | #include "cgroup.h" |
f3787121 CB |
69 | #include "commands.h" |
70 | #include "conf.h" | |
71 | #include "console.h" | |
e2bcd7db | 72 | #include "error.h" |
f3787121 CB |
73 | #include "log.h" |
74 | #include "lxclock.h" | |
75 | #include "lxcseccomp.h" | |
565c2d76 | 76 | #include "lxcutmp.h" |
f3787121 | 77 | #include "mainloop.h" |
63376d7d | 78 | #include "monitor.h" |
f549edcc | 79 | #include "namespace.h" |
f3787121 CB |
80 | #include "start.h" |
81 | #include "sync.h" | |
82 | #include "utils.h" | |
fe4de9a6 | 83 | #include "lsm/lsm.h" |
36eb9bde CLG |
84 | |
85 | lxc_log_define(lxc_start, lxc); | |
86 | ||
9f30a190 MM |
87 | const struct ns_info ns_info[LXC_NS_MAX] = { |
88 | [LXC_NS_MNT] = {"mnt", CLONE_NEWNS}, | |
89 | [LXC_NS_PID] = {"pid", CLONE_NEWPID}, | |
90 | [LXC_NS_UTS] = {"uts", CLONE_NEWUTS}, | |
91 | [LXC_NS_IPC] = {"ipc", CLONE_NEWIPC}, | |
92 | [LXC_NS_USER] = {"user", CLONE_NEWUSER}, | |
ceecc92c CB |
93 | [LXC_NS_NET] = {"net", CLONE_NEWNET}, |
94 | [LXC_NS_CGROUP] = {"cgroup", CLONE_NEWCGROUP} | |
9f30a190 MM |
95 | }; |
96 | ||
f01f7975 | 97 | extern void mod_all_rdeps(struct lxc_container *c, bool inc); |
28272964 CB |
98 | static bool do_destroy_container(struct lxc_conf *conf); |
99 | static int lxc_rmdir_onedev_wrapper(void *data); | |
100 | static void lxc_destroy_container_on_signal(struct lxc_handler *handler, | |
101 | const char *name); | |
102 | ||
c8154066 SH |
103 | static void print_top_failing_dir(const char *path) |
104 | { | |
105 | size_t len = strlen(path); | |
f3787121 | 106 | char *copy = alloca(len + 1), *p, *e, saved; |
c8154066 SH |
107 | strcpy(copy, path); |
108 | ||
109 | p = copy; | |
110 | e = copy + len; | |
111 | while (p < e) { | |
f3787121 CB |
112 | while (p < e && *p == '/') |
113 | p++; | |
114 | while (p < e && *p != '/') | |
115 | p++; | |
c8154066 SH |
116 | saved = *p; |
117 | *p = '\0'; | |
118 | if (access(copy, X_OK)) { | |
119 | SYSERROR("could not access %s. Please grant it 'x' " \ | |
120 | "access, or add an ACL for the container root.", | |
121 | copy); | |
122 | return; | |
123 | } | |
124 | *p = saved; | |
125 | } | |
126 | } | |
127 | ||
9f30a190 MM |
128 | static void close_ns(int ns_fd[LXC_NS_MAX]) { |
129 | int i; | |
130 | ||
9f30a190 MM |
131 | for (i = 0; i < LXC_NS_MAX; i++) { |
132 | if (ns_fd[i] > -1) { | |
133 | close(ns_fd[i]); | |
134 | ns_fd[i] = -1; | |
135 | } | |
136 | } | |
9f30a190 MM |
137 | } |
138 | ||
62d05d9b SH |
139 | /* |
140 | * preserve_ns: open /proc/@pid/ns/@ns for each namespace specified | |
141 | * in clone_flags. | |
142 | * Return true on success, false on failure. On failure, leave an error | |
143 | * message in *errmsg, which caller must free. | |
144 | */ | |
f3787121 CB |
145 | static bool preserve_ns(int ns_fd[LXC_NS_MAX], int clone_flags, pid_t pid, |
146 | char **errmsg) | |
147 | { | |
62d05d9b | 148 | int i, ret; |
9f30a190 MM |
149 | char path[MAXPATHLEN]; |
150 | ||
9f30a190 MM |
151 | for (i = 0; i < LXC_NS_MAX; i++) |
152 | ns_fd[i] = -1; | |
153 | ||
035a38fc WB |
154 | snprintf(path, MAXPATHLEN, "/proc/%d/ns", pid); |
155 | if (access(path, X_OK)) { | |
62d05d9b SH |
156 | if (asprintf(errmsg, "Kernel does not support setns.") == -1) |
157 | *errmsg = NULL; | |
158 | return false; | |
cd43d2d1 SH |
159 | } |
160 | ||
9f30a190 MM |
161 | for (i = 0; i < LXC_NS_MAX; i++) { |
162 | if ((clone_flags & ns_info[i].clone_flag) == 0) | |
163 | continue; | |
035a38fc WB |
164 | snprintf(path, MAXPATHLEN, "/proc/%d/ns/%s", pid, |
165 | ns_info[i].proc_name); | |
9f30a190 | 166 | ns_fd[i] = open(path, O_RDONLY | O_CLOEXEC); |
9f30a190 MM |
167 | if (ns_fd[i] < 0) |
168 | goto error; | |
169 | } | |
170 | ||
62d05d9b | 171 | return true; |
9f30a190 MM |
172 | |
173 | error: | |
62d05d9b SH |
174 | if (errno == ENOENT) { |
175 | ret = asprintf(errmsg, "Kernel does not support setns for %s", | |
176 | ns_info[i].proc_name); | |
177 | } else { | |
178 | ret = asprintf(errmsg, "Failed to open %s: %s", | |
179 | path, strerror(errno)); | |
180 | } | |
181 | if (ret == -1) | |
182 | *errmsg = NULL; | |
9f30a190 | 183 | close_ns(ns_fd); |
62d05d9b | 184 | return false; |
9f30a190 MM |
185 | } |
186 | ||
187 | static int attach_ns(const int ns_fd[LXC_NS_MAX]) { | |
188 | int i; | |
189 | ||
190 | for (i = 0; i < LXC_NS_MAX; i++) { | |
191 | if (ns_fd[i] < 0) | |
192 | continue; | |
193 | ||
194 | if (setns(ns_fd[i], 0) != 0) | |
195 | goto error; | |
196 | } | |
197 | return 0; | |
198 | ||
199 | error: | |
200 | SYSERROR("failed to set namespace '%s'", ns_info[i].proc_name); | |
201 | return -1; | |
202 | } | |
203 | ||
80090207 CLG |
204 | static int match_fd(int fd) |
205 | { | |
206 | return (fd == 0 || fd == 1 || fd == 2); | |
207 | } | |
208 | ||
d2cf4c37 SH |
209 | /* |
210 | * Check for any fds we need to close | |
211 | * * if fd_to_ignore != -1, then if we find that fd open we will ignore it. | |
212 | * * By default we warn about open fds we find. | |
213 | * * If closeall is true, we will close open fds. | |
214 | * * If lxc-start was passed "-C", then conf->close_all_fds will be true, | |
215 | * in which case we also close all open fds. | |
216 | * * A daemonized container will always pass closeall=true. | |
217 | */ | |
218 | int lxc_check_inherited(struct lxc_conf *conf, bool closeall, int fd_to_ignore) | |
80090207 | 219 | { |
74f96976 | 220 | struct dirent *direntp; |
80090207 CLG |
221 | int fd, fddir; |
222 | DIR *dir; | |
80090207 | 223 | |
d2cf4c37 SH |
224 | if (conf && conf->close_all_fds) |
225 | closeall = true; | |
226 | ||
b119f362 | 227 | restart: |
80090207 CLG |
228 | dir = opendir("/proc/self/fd"); |
229 | if (!dir) { | |
230 | WARN("failed to open directory: %m"); | |
231 | return -1; | |
232 | } | |
233 | ||
234 | fddir = dirfd(dir); | |
235 | ||
74f96976 | 236 | while ((direntp = readdir(dir))) { |
80090207 CLG |
237 | if (!direntp) |
238 | break; | |
239 | ||
240 | if (!strcmp(direntp->d_name, ".")) | |
241 | continue; | |
242 | ||
243 | if (!strcmp(direntp->d_name, "..")) | |
244 | continue; | |
245 | ||
246 | fd = atoi(direntp->d_name); | |
247 | ||
f2faa8fa | 248 | if (fd == fddir || fd == lxc_log_fd || fd == fd_to_ignore) |
80090207 CLG |
249 | continue; |
250 | ||
858377e4 SH |
251 | if (current_config && fd == current_config->logfd) |
252 | continue; | |
253 | ||
80090207 CLG |
254 | if (match_fd(fd)) |
255 | continue; | |
80090207 | 256 | |
d2cf4c37 | 257 | if (closeall) { |
b119f362 SH |
258 | close(fd); |
259 | closedir(dir); | |
260 | INFO("closed inherited fd %d", fd); | |
261 | goto restart; | |
262 | } | |
92c7f629 | 263 | WARN("inherited fd %d", fd); |
80090207 CLG |
264 | } |
265 | ||
64c57ea1 BD |
266 | /* |
267 | * only enable syslog at this point to avoid the above logging function | |
268 | * to open a new fd and make the check_inherited function enter an | |
269 | * infinite loop. | |
270 | */ | |
271 | lxc_log_enable_syslog(); | |
272 | ||
92c7f629 GK |
273 | closedir(dir); /* cannot fail */ |
274 | return 0; | |
80090207 CLG |
275 | } |
276 | ||
83ee7875 | 277 | static int setup_signal_fd(sigset_t *oldmask) |
b0a33c1e | 278 | { |
279 | sigset_t mask; | |
280 | int fd; | |
281 | ||
f3304a29 FW |
282 | /* Block everything except serious error signals */ |
283 | if (sigfillset(&mask) || | |
284 | sigdelset(&mask, SIGILL) || | |
285 | sigdelset(&mask, SIGSEGV) || | |
286 | sigdelset(&mask, SIGBUS) || | |
b5159817 | 287 | sigdelset(&mask, SIGWINCH) || |
f3304a29 FW |
288 | sigprocmask(SIG_BLOCK, &mask, oldmask)) { |
289 | SYSERROR("failed to set signal mask"); | |
b0a33c1e | 290 | return -1; |
291 | } | |
292 | ||
293 | fd = signalfd(-1, &mask, 0); | |
294 | if (fd < 0) { | |
36eb9bde | 295 | SYSERROR("failed to create the signal fd"); |
b0a33c1e | 296 | return -1; |
297 | } | |
298 | ||
299 | if (fcntl(fd, F_SETFD, FD_CLOEXEC)) { | |
36eb9bde | 300 | SYSERROR("failed to set sigfd to close-on-exec"); |
b0a33c1e | 301 | close(fd); |
302 | return -1; | |
303 | } | |
304 | ||
1ac470c0 DL |
305 | DEBUG("sigchild handler set"); |
306 | ||
b0a33c1e | 307 | return fd; |
308 | } | |
309 | ||
84c92abd | 310 | static int signal_handler(int fd, uint32_t events, void *data, |
f3787121 | 311 | struct lxc_epoll_descr *descr) |
b0a33c1e | 312 | { |
15cd25fd | 313 | struct signalfd_siginfo siginfo; |
80507ee8 | 314 | siginfo_t info; |
15cd25fd | 315 | int ret; |
82d89dce | 316 | pid_t *pid = data; |
80507ee8 | 317 | bool init_died = false; |
15cd25fd DL |
318 | |
319 | ret = read(fd, &siginfo, sizeof(siginfo)); | |
320 | if (ret < 0) { | |
f3304a29 | 321 | ERROR("failed to read signal info"); |
15cd25fd DL |
322 | return -1; |
323 | } | |
324 | ||
325 | if (ret != sizeof(siginfo)) { | |
326 | ERROR("unexpected siginfo size"); | |
327 | return -1; | |
328 | } | |
329 | ||
80507ee8 SH |
330 | // check whether init is running |
331 | info.si_pid = 0; | |
332 | ret = waitid(P_PID, *pid, &info, WEXITED | WNOWAIT | WNOHANG); | |
333 | if (ret == 0 && info.si_pid == *pid) { | |
334 | init_died = true; | |
335 | } | |
336 | ||
f3304a29 FW |
337 | if (siginfo.ssi_signo != SIGCHLD) { |
338 | kill(*pid, siginfo.ssi_signo); | |
339 | INFO("forwarded signal %d to pid %d", siginfo.ssi_signo, *pid); | |
80507ee8 | 340 | return init_died ? 1 : 0; |
f3304a29 FW |
341 | } |
342 | ||
15cd25fd DL |
343 | if (siginfo.ssi_code == CLD_STOPPED || |
344 | siginfo.ssi_code == CLD_CONTINUED) { | |
345 | INFO("container init process was stopped/continued"); | |
80507ee8 | 346 | return init_died ? 1 : 0; |
15cd25fd | 347 | } |
1ac470c0 | 348 | |
82d89dce DL |
349 | /* more robustness, protect ourself from a SIGCHLD sent |
350 | * by a process different from the container init | |
351 | */ | |
352 | if (siginfo.ssi_pid != *pid) { | |
353 | WARN("invalid pid for SIGCHLD"); | |
80507ee8 | 354 | return init_died ? 1 : 0; |
82d89dce DL |
355 | } |
356 | ||
15cd25fd | 357 | DEBUG("container init process exited"); |
b0a33c1e | 358 | return 1; |
359 | } | |
360 | ||
735f2c6e | 361 | int lxc_set_state(const char *name, struct lxc_handler *handler, lxc_state_t state) |
66aeffc7 DL |
362 | { |
363 | handler->state = state; | |
9123e471 | 364 | lxc_monitor_send_state(name, state, handler->lxcpath); |
66aeffc7 DL |
365 | return 0; |
366 | } | |
367 | ||
735f2c6e | 368 | int lxc_poll(const char *name, struct lxc_handler *handler) |
b0a33c1e | 369 | { |
ca5f7926 DL |
370 | int sigfd = handler->sigfd; |
371 | int pid = handler->pid; | |
b0a33c1e | 372 | struct lxc_epoll_descr descr; |
373 | ||
a9e61274 | 374 | if (lxc_mainloop_open(&descr)) { |
36eb9bde | 375 | ERROR("failed to create mainloop"); |
50c8bf05 | 376 | goto out_sigfd; |
b0a33c1e | 377 | } |
378 | ||
83ee7875 | 379 | if (lxc_mainloop_add_handler(&descr, sigfd, signal_handler, &pid)) { |
36eb9bde | 380 | ERROR("failed to add handler for the signal"); |
b0a33c1e | 381 | goto out_mainloop_open; |
382 | } | |
383 | ||
da41561c | 384 | if (lxc_console_mainloop_add(&descr, handler->conf)) { |
63376d7d DL |
385 | ERROR("failed to add console handler to mainloop"); |
386 | goto out_mainloop_open; | |
387 | } | |
388 | ||
ef6e34ee | 389 | if (lxc_cmd_mainloop_add(name, &descr, handler)) { |
563f2f2c | 390 | ERROR("failed to add command handler to mainloop"); |
96fa1ff0 | 391 | goto out_mainloop_open; |
563f2f2c DL |
392 | } |
393 | ||
828695d9 | 394 | if (handler->conf->need_utmp_watch) { |
495d2046 | 395 | #if HAVE_SYS_CAPABILITY_H |
828695d9 SH |
396 | if (lxc_utmp_mainloop_add(&descr, handler)) { |
397 | ERROR("failed to add utmp handler to mainloop"); | |
398 | goto out_mainloop_open; | |
399 | } | |
495d2046 | 400 | #else |
959aee9c | 401 | DEBUG("not starting utmp handler as cap_sys_boot cannot be dropped without capabilities support"); |
495d2046 | 402 | #endif |
563f2f2c | 403 | } |
b0a33c1e | 404 | |
e51d4895 | 405 | return lxc_mainloop(&descr, -1); |
b0a33c1e | 406 | |
407 | out_mainloop_open: | |
408 | lxc_mainloop_close(&descr); | |
b0a33c1e | 409 | out_sigfd: |
410 | close(sigfd); | |
c3e13372 | 411 | return -1; |
b0a33c1e | 412 | } |
413 | ||
13f5be62 | 414 | struct lxc_handler *lxc_init(const char *name, struct lxc_conf *conf, const char *lxcpath) |
59eb99ba | 415 | { |
b6b2b194 | 416 | int i; |
3a0f472d DL |
417 | struct lxc_handler *handler; |
418 | ||
419 | handler = malloc(sizeof(*handler)); | |
420 | if (!handler) | |
421 | return NULL; | |
59eb99ba DL |
422 | |
423 | memset(handler, 0, sizeof(*handler)); | |
424 | ||
e8bd4e43 | 425 | handler->ttysock[0] = handler->ttysock[1] = -1; |
fae349da | 426 | handler->conf = conf; |
9123e471 | 427 | handler->lxcpath = lxcpath; |
5c068da9 | 428 | handler->pinfd = -1; |
fae349da | 429 | |
b6b2b194 WB |
430 | for (i = 0; i < LXC_NS_MAX; i++) |
431 | handler->nsfd[i] = -1; | |
432 | ||
fe4de9a6 DE |
433 | lsm_init(); |
434 | ||
3bdf52d7 DL |
435 | handler->name = strdup(name); |
436 | if (!handler->name) { | |
437 | ERROR("failed to allocate memory"); | |
438 | goto out_free; | |
439 | } | |
440 | ||
ef6e34ee | 441 | if (lxc_cmd_init(name, handler, lxcpath)) |
d2e30e99 DE |
442 | goto out_free_name; |
443 | ||
8f2c3a70 SH |
444 | if (lxc_read_seccomp_config(conf) != 0) { |
445 | ERROR("failed loading seccomp policy"); | |
d2e30e99 | 446 | goto out_close_maincmd_fd; |
8f2c3a70 SH |
447 | } |
448 | ||
051151de | 449 | /* Begin by setting the state to STARTING */ |
25c2aca5 | 450 | if (lxc_set_state(name, handler, STARTING)) { |
59eb99ba | 451 | ERROR("failed to set state '%s'", lxc_state2str(STARTING)); |
051151de | 452 | goto out_close_maincmd_fd; |
0ad19a3f | 453 | } |
454 | ||
f7bee6c6 | 455 | /* Start of environment variable setup for hooks */ |
ab7efcf5 | 456 | if (name && setenv("LXC_NAME", name, 1)) { |
f7bee6c6 MW |
457 | SYSERROR("failed to set environment variable for container name"); |
458 | } | |
ab7efcf5 | 459 | if (conf->rcfile && setenv("LXC_CONFIG_FILE", conf->rcfile, 1)) { |
f7bee6c6 MW |
460 | SYSERROR("failed to set environment variable for config path"); |
461 | } | |
ab7efcf5 | 462 | if (conf->rootfs.mount && setenv("LXC_ROOTFS_MOUNT", conf->rootfs.mount, 1)) { |
f7bee6c6 MW |
463 | SYSERROR("failed to set environment variable for rootfs mount"); |
464 | } | |
ab7efcf5 | 465 | if (conf->rootfs.path && setenv("LXC_ROOTFS_PATH", conf->rootfs.path, 1)) { |
f7bee6c6 MW |
466 | SYSERROR("failed to set environment variable for rootfs mount"); |
467 | } | |
468 | if (conf->console.path && setenv("LXC_CONSOLE", conf->console.path, 1)) { | |
469 | SYSERROR("failed to set environment variable for console path"); | |
470 | } | |
471 | if (conf->console.log_path && setenv("LXC_CONSOLE_LOGPATH", conf->console.log_path, 1)) { | |
472 | SYSERROR("failed to set environment variable for console log"); | |
473 | } | |
c4cafa08 SH |
474 | if (setenv("LXC_CGNS_AWARE", "1", 1)) { |
475 | SYSERROR("failed to set LXC_CGNS_AWARE environment variable"); | |
476 | } | |
f7bee6c6 MW |
477 | /* End of environment variable setup for hooks */ |
478 | ||
283678ed | 479 | if (run_lxc_hooks(name, "pre-start", conf, handler->lxcpath, NULL)) { |
773fb9ca SH |
480 | ERROR("failed to run pre-start hooks for container '%s'.", name); |
481 | goto out_aborting; | |
482 | } | |
26ddeedd | 483 | |
b0a33c1e | 484 | /* the signal fd has to be created before forking otherwise |
485 | * if the child process exits before we setup the signal fd, | |
486 | * the event will be lost and the command will be stuck */ | |
83ee7875 | 487 | handler->sigfd = setup_signal_fd(&handler->oldmask); |
59eb99ba | 488 | if (handler->sigfd < 0) { |
36eb9bde | 489 | ERROR("failed to set sigchild fd handler"); |
b5159817 DE |
490 | goto out_delete_tty; |
491 | } | |
492 | ||
493 | /* do this after setting up signals since it might unblock SIGWINCH */ | |
494 | if (lxc_console_create(conf)) { | |
495 | ERROR("failed to create console"); | |
496 | goto out_restore_sigmask; | |
b0a33c1e | 497 | } |
498 | ||
c4d10a05 SH |
499 | if (ttys_shift_ids(conf) < 0) { |
500 | ERROR("Failed to shift tty into container"); | |
501 | goto out_restore_sigmask; | |
502 | } | |
503 | ||
c3e13372 | 504 | INFO("'%s' is initialized", name); |
3a0f472d | 505 | return handler; |
59eb99ba | 506 | |
b5159817 DE |
507 | out_restore_sigmask: |
508 | sigprocmask(SIG_SETMASK, &handler->oldmask, NULL); | |
59eb99ba | 509 | out_delete_tty: |
fae349da | 510 | lxc_delete_tty(&conf->tty_info); |
59eb99ba | 511 | out_aborting: |
25c2aca5 | 512 | lxc_set_state(name, handler, ABORTING); |
d2e30e99 DE |
513 | out_close_maincmd_fd: |
514 | close(conf->maincmd_fd); | |
515 | conf->maincmd_fd = -1; | |
3bdf52d7 DL |
516 | out_free_name: |
517 | free(handler->name); | |
518 | handler->name = NULL; | |
3a0f472d DL |
519 | out_free: |
520 | free(handler); | |
c3e13372 | 521 | return NULL; |
59eb99ba DL |
522 | } |
523 | ||
3b72c4a0 | 524 | void lxc_fini(const char *name, struct lxc_handler *handler) |
59eb99ba | 525 | { |
b3286b62 WB |
526 | int i, rc; |
527 | pid_t self = getpid(); | |
528 | char *namespaces[LXC_NS_MAX+1]; | |
529 | size_t namespace_count = 0; | |
b6b2b194 | 530 | |
59eb99ba DL |
531 | /* The STOPPING state is there for future cleanup code |
532 | * which can take awhile | |
533 | */ | |
25c2aca5 | 534 | lxc_set_state(name, handler, STOPPING); |
b6b2b194 | 535 | |
b3286b62 WB |
536 | for (i = 0; i < LXC_NS_MAX; i++) { |
537 | if (handler->nsfd[i] != -1) { | |
538 | rc = asprintf(&namespaces[namespace_count], "%s:/proc/%d/fd/%d", | |
539 | ns_info[i].proc_name, self, handler->nsfd[i]); | |
540 | if (rc == -1) { | |
541 | SYSERROR("failed to allocate memory"); | |
542 | break; | |
543 | } | |
544 | ++namespace_count; | |
545 | } | |
546 | } | |
547 | namespaces[namespace_count] = NULL; | |
c154af98 SG |
548 | |
549 | if (handler->conf->reboot && setenv("LXC_TARGET", "reboot", 1)) { | |
550 | SYSERROR("failed to set environment variable for stop target"); | |
551 | } | |
552 | if (!handler->conf->reboot && setenv("LXC_TARGET", "stop", 1)) { | |
553 | SYSERROR("failed to set environment variable for stop target"); | |
554 | } | |
555 | ||
b3286b62 | 556 | if (run_lxc_hooks(name, "stop", handler->conf, handler->lxcpath, namespaces)) |
8438bfbd | 557 | ERROR("failed to run stop hooks for container '%s'.", name); |
c154af98 | 558 | |
b3286b62 WB |
559 | while (namespace_count--) |
560 | free(namespaces[namespace_count]); | |
b6b2b194 WB |
561 | for (i = 0; i < LXC_NS_MAX; i++) { |
562 | if (handler->nsfd[i] != -1) { | |
563 | close(handler->nsfd[i]); | |
564 | handler->nsfd[i] = -1; | |
565 | } | |
566 | } | |
738d0deb CB |
567 | |
568 | if (handler->netnsfd >= 0) { | |
569 | close(handler->netnsfd); | |
570 | handler->netnsfd = -1; | |
571 | } | |
572 | ||
25c2aca5 | 573 | lxc_set_state(name, handler, STOPPED); |
59eb99ba | 574 | |
f3787121 | 575 | if (run_lxc_hooks(name, "post-stop", handler->conf, handler->lxcpath, NULL)) { |
773fb9ca | 576 | ERROR("failed to run post-stop hooks for container '%s'.", name); |
f3787121 CB |
577 | if (handler->conf->reboot) { |
578 | WARN("Container will be stopped instead of rebooted."); | |
579 | handler->conf->reboot = 0; | |
580 | setenv("LXC_TARGET", "stop", 1); | |
581 | } | |
582 | } | |
26ddeedd | 583 | |
83ee7875 | 584 | /* reset mask set by setup_signal_fd */ |
8f64a3f6 MN |
585 | if (sigprocmask(SIG_SETMASK, &handler->oldmask, NULL)) |
586 | WARN("failed to restore sigprocmask"); | |
587 | ||
b5159817 | 588 | lxc_console_delete(&handler->conf->console); |
b2431939 | 589 | lxc_delete_tty(&handler->conf->tty_info); |
d2e30e99 DE |
590 | close(handler->conf->maincmd_fd); |
591 | handler->conf->maincmd_fd = -1; | |
3bdf52d7 | 592 | free(handler->name); |
e8bd4e43 SH |
593 | if (handler->ttysock[0] != -1) { |
594 | close(handler->ttysock[0]); | |
595 | close(handler->ttysock[1]); | |
596 | } | |
2c5f2ede CB |
597 | |
598 | if (handler->conf->ephemeral == 1 && handler->conf->reboot != 1) | |
28272964 | 599 | lxc_destroy_container_on_signal(handler, name); |
2c5f2ede | 600 | |
d4ef7c50 | 601 | cgroup_destroy(handler); |
b2431939 | 602 | free(handler); |
59eb99ba DL |
603 | } |
604 | ||
735f2c6e | 605 | void lxc_abort(const char *name, struct lxc_handler *handler) |
59eb99ba | 606 | { |
73e608b2 SH |
607 | int ret, status; |
608 | ||
25c2aca5 | 609 | lxc_set_state(name, handler, ABORTING); |
7d9fb3e9 DL |
610 | if (handler->pid > 0) |
611 | kill(handler->pid, SIGKILL); | |
73e608b2 | 612 | while ((ret = waitpid(-1, &status, 0)) > 0) ; |
59eb99ba DL |
613 | } |
614 | ||
828695d9 SH |
615 | #include <sys/reboot.h> |
616 | #include <linux/reboot.h> | |
617 | ||
e2fa1520 SH |
618 | /* |
619 | * reboot(LINUX_REBOOT_CMD_CAD_ON) will return -EINVAL | |
620 | * in a child pid namespace if container reboot support exists. | |
621 | * Otherwise, it will either succeed or return -EPERM. | |
622 | */ | |
623 | static int container_reboot_supported(void *arg) | |
828695d9 | 624 | { |
d028235d | 625 | int *cmd = arg; |
828695d9 | 626 | int ret; |
828695d9 | 627 | |
d028235d | 628 | ret = reboot(*cmd); |
e2fa1520 SH |
629 | if (ret == -1 && errno == EINVAL) |
630 | return 1; | |
631 | return 0; | |
632 | } | |
633 | ||
b60ed720 | 634 | static int must_drop_cap_sys_boot(struct lxc_conf *conf) |
e2fa1520 | 635 | { |
025ed0f3 | 636 | FILE *f; |
b60ed720 | 637 | int ret, cmd, v, flags; |
d028235d SG |
638 | long stack_size = 4096; |
639 | void *stack = alloca(stack_size); | |
640 | int status; | |
641 | pid_t pid; | |
e2fa1520 | 642 | |
025ed0f3 | 643 | f = fopen("/proc/sys/kernel/ctrl-alt-del", "r"); |
e2fa1520 SH |
644 | if (!f) { |
645 | DEBUG("failed to open /proc/sys/kernel/ctrl-alt-del"); | |
828695d9 | 646 | return 1; |
e2fa1520 | 647 | } |
828695d9 SH |
648 | |
649 | ret = fscanf(f, "%d", &v); | |
650 | fclose(f); | |
e2fa1520 SH |
651 | if (ret != 1) { |
652 | DEBUG("Failed to read /proc/sys/kernel/ctrl-alt-del"); | |
828695d9 | 653 | return 1; |
e2fa1520 SH |
654 | } |
655 | cmd = v ? LINUX_REBOOT_CMD_CAD_ON : LINUX_REBOOT_CMD_CAD_OFF; | |
656 | ||
b60ed720 SH |
657 | flags = CLONE_NEWPID | SIGCHLD; |
658 | if (!lxc_list_empty(&conf->id_map)) | |
659 | flags |= CLONE_NEWUSER; | |
660 | ||
7f145a6d | 661 | #ifdef __ia64__ |
959aee9c | 662 | pid = __clone2(container_reboot_supported, stack, stack_size, flags, &cmd); |
7f145a6d | 663 | #else |
959aee9c SG |
664 | stack += stack_size; |
665 | pid = clone(container_reboot_supported, stack, flags, &cmd); | |
7f145a6d | 666 | #endif |
959aee9c | 667 | if (pid < 0) { |
c08220e9 | 668 | if (flags & CLONE_NEWUSER) |
4a7e5f4f | 669 | ERROR("failed to clone (%#x): %s (includes CLONE_NEWUSER)", flags, strerror(errno)); |
c08220e9 | 670 | else |
4a7e5f4f | 671 | ERROR("failed to clone (%#x): %s", flags, strerror(errno)); |
959aee9c SG |
672 | return -1; |
673 | } | |
674 | if (wait(&status) < 0) { | |
675 | SYSERROR("unexpected wait error: %m"); | |
676 | return -1; | |
677 | } | |
e2fa1520 SH |
678 | |
679 | if (WEXITSTATUS(status) != 1) | |
828695d9 | 680 | return 1; |
e2fa1520 | 681 | |
828695d9 SH |
682 | return 0; |
683 | } | |
684 | ||
658979c5 SH |
685 | /* |
686 | * netpipe is used in the unprivileged case to transfer the ifindexes | |
687 | * from parent to child | |
688 | */ | |
689 | static int netpipe = -1; | |
690 | ||
691 | static inline int count_veths(struct lxc_list *network) | |
692 | { | |
693 | struct lxc_list *iterator; | |
694 | struct lxc_netdev *netdev; | |
695 | int count = 0; | |
696 | ||
697 | lxc_list_for_each(iterator, network) { | |
698 | netdev = iterator->elem; | |
699 | if (netdev->type != LXC_NET_VETH) | |
700 | continue; | |
701 | count++; | |
702 | } | |
703 | return count; | |
704 | } | |
705 | ||
706 | static int read_unpriv_netifindex(struct lxc_list *network) | |
707 | { | |
708 | struct lxc_list *iterator; | |
709 | struct lxc_netdev *netdev; | |
710 | ||
711 | if (netpipe == -1) | |
712 | return 0; | |
713 | lxc_list_for_each(iterator, network) { | |
714 | netdev = iterator->elem; | |
715 | if (netdev->type != LXC_NET_VETH) | |
716 | continue; | |
717 | if (!(netdev->name = malloc(IFNAMSIZ))) { | |
718 | ERROR("Out of memory"); | |
719 | close(netpipe); | |
720 | return -1; | |
721 | } | |
722 | if (read(netpipe, netdev->name, IFNAMSIZ) != IFNAMSIZ) { | |
723 | close(netpipe); | |
724 | return -1; | |
725 | } | |
726 | } | |
727 | close(netpipe); | |
728 | return 0; | |
729 | } | |
730 | ||
ffe1e01a | 731 | static int do_start(void *data) |
50e98013 | 732 | { |
7c661726 | 733 | struct lxc_list *iterator; |
23c53af9 | 734 | struct lxc_handler *handler = data; |
7a55c157 TA |
735 | int devnull_fd = -1, ret; |
736 | char path[PATH_MAX]; | |
50e98013 DL |
737 | |
738 | if (sigprocmask(SIG_SETMASK, &handler->oldmask, NULL)) { | |
739 | SYSERROR("failed to set sigprocmask"); | |
9d7f9e52 | 740 | return -1; |
50e98013 DL |
741 | } |
742 | ||
d028235d | 743 | /* This prctl must be before the synchro, so if the parent |
743ecd2e DL |
744 | * dies before we set the parent death signal, we will detect |
745 | * its death with the synchro right after, otherwise we have | |
746 | * a window where the parent can exit before we set the pdeath | |
747 | * signal leading to a unsupervized container. | |
748 | */ | |
749 | if (prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0)) { | |
750 | SYSERROR("failed to set pdeath signal"); | |
751 | return -1; | |
752 | } | |
753 | ||
3c22086f | 754 | lxc_sync_fini_parent(handler); |
50e98013 | 755 | |
2b0e17e4 | 756 | /* don't leak the pinfd to the container */ |
025ed0f3 | 757 | if (handler->pinfd >= 0) { |
0d03360a | 758 | close(handler->pinfd); |
025ed0f3 | 759 | } |
2b0e17e4 | 760 | |
5b1e83cb SH |
761 | if (lxc_sync_wait_parent(handler, LXC_SYNC_STARTUP)) |
762 | return -1; | |
763 | ||
764 | /* Unshare CLONE_NEWNET after CLONE_NEWUSER - see | |
765 | https://github.com/lxc/lxd/issues/1978 */ | |
766 | if ((handler->clone_flags & (CLONE_NEWNET | CLONE_NEWUSER)) == | |
767 | (CLONE_NEWNET | CLONE_NEWUSER)) { | |
768 | ret = unshare(CLONE_NEWNET); | |
769 | if (ret < 0) { | |
770 | SYSERROR("Error unsharing network namespace"); | |
771 | goto out_warn_father; | |
198cbbaa CB |
772 | } else { |
773 | INFO("Unshared NET namespace."); | |
5b1e83cb SH |
774 | } |
775 | } | |
776 | ||
3c22086f CLG |
777 | /* Tell the parent task it can begin to configure the |
778 | * container and wait for it to finish | |
779 | */ | |
780 | if (lxc_sync_barrier_parent(handler, LXC_SYNC_CONFIGURE)) | |
9d7f9e52 | 781 | return -1; |
50e98013 | 782 | |
658979c5 SH |
783 | if (read_unpriv_netifindex(&handler->conf->network) < 0) |
784 | goto out_warn_father; | |
785 | ||
f6d3e3e4 SH |
786 | /* |
787 | * if we are in a new user namespace, become root there to have | |
62d00539 PT |
788 | * privilege over our namespace. When using lxc-execute we default to root, |
789 | * but this can be overriden using the lxc.init_uid and lxc.init_gid | |
790 | * configuration options. | |
f6d3e3e4 SH |
791 | */ |
792 | if (!lxc_list_empty(&handler->conf->id_map)) { | |
c5cd20ce PT |
793 | gid_t new_gid = 0; |
794 | if (handler->conf->is_execute && handler->conf->init_gid) | |
795 | new_gid = handler->conf->init_gid; | |
796 | ||
797 | uid_t new_uid = 0; | |
798 | if (handler->conf->is_execute && handler->conf->init_uid) | |
799 | new_uid = handler->conf->init_uid; | |
800 | ||
56f8ff00 PT |
801 | NOTICE("switching to gid/uid %d/%d in new user namespace", new_gid, new_uid); |
802 | if (setgid(new_gid)) { | |
f6d3e3e4 SH |
803 | SYSERROR("setgid"); |
804 | goto out_warn_father; | |
805 | } | |
56f8ff00 | 806 | if (setuid(new_uid)) { |
f6d3e3e4 SH |
807 | SYSERROR("setuid"); |
808 | goto out_warn_father; | |
809 | } | |
c476bdce SH |
810 | if (setgroups(0, NULL)) { |
811 | SYSERROR("setgroups"); | |
812 | goto out_warn_father; | |
813 | } | |
f6d3e3e4 SH |
814 | } |
815 | ||
99b71824 | 816 | if (access(handler->lxcpath, X_OK)) { |
c8154066 SH |
817 | print_top_failing_dir(handler->lxcpath); |
818 | goto out_warn_father; | |
819 | } | |
820 | ||
495d2046 | 821 | #if HAVE_SYS_CAPABILITY_H |
69182a31 | 822 | if (handler->conf->need_utmp_watch) { |
828695d9 SH |
823 | if (prctl(PR_CAPBSET_DROP, CAP_SYS_BOOT, 0, 0, 0)) { |
824 | SYSERROR("failed to remove CAP_SYS_BOOT capability"); | |
c4ea60df | 825 | goto out_warn_father; |
828695d9 | 826 | } |
959aee9c | 827 | DEBUG("Dropped cap_sys_boot"); |
e2fa1520 | 828 | } |
495d2046 | 829 | #endif |
e2fa1520 | 830 | |
a91cde21 | 831 | ret = snprintf(path, sizeof(path), "%s/dev/null", handler->conf->rootfs.mount); |
7a55c157 TA |
832 | if (ret < 0 || ret >= sizeof(path)) { |
833 | SYSERROR("sprintf'd too many chars"); | |
834 | goto out_warn_father; | |
835 | } | |
836 | ||
837 | /* In order to checkpoint restore, we need to have everything in the | |
838 | * same mount namespace. However, some containers may not have a | |
839 | * reasonable /dev (in particular, they may not have /dev/null), so we | |
840 | * can't set init's std fds to /dev/null by opening it from inside the | |
841 | * container. | |
842 | * | |
843 | * If that's the case, fall back to using the host's /dev/null. This | |
844 | * means that migration won't work, but at least we won't spew output | |
845 | * where it isn't wanted. | |
846 | */ | |
847 | if (handler->backgrounded && !handler->conf->autodev && access(path, F_OK) < 0) { | |
c44de748 AM |
848 | devnull_fd = open_devnull(); |
849 | ||
850 | if (devnull_fd < 0) | |
851 | goto out_warn_father; | |
7a55c157 | 852 | WARN("using host's /dev/null for container init's std fds, migraiton won't work"); |
c44de748 AM |
853 | } |
854 | ||
e2fa1520 | 855 | /* Setup the container, ip, names, utsname, ... */ |
d4ef7c50 | 856 | if (lxc_setup(handler)) { |
e2fa1520 SH |
857 | ERROR("failed to setup the container"); |
858 | goto out_warn_father; | |
859 | } | |
50e98013 | 860 | |
544a48a0 SH |
861 | /* ask father to setup cgroups and wait for him to finish */ |
862 | if (lxc_sync_barrier_parent(handler, LXC_SYNC_CGROUP)) | |
c44de748 | 863 | goto out_error; |
544a48a0 | 864 | |
deefdf8a CB |
865 | /* Unshare cgroup namespace after we have setup our cgroups. If we do it |
866 | * earlier we end up with a wrong view of /proc/self/cgroup. For | |
867 | * example, assume we unshare(CLONE_NEWCGROUP) first, and then create | |
868 | * the cgroup for the container, say /sys/fs/cgroup/cpuset/lxc/c, then | |
869 | * /proc/self/cgroup would show us: | |
870 | * | |
871 | * 8:cpuset:/lxc/c | |
872 | * | |
873 | * whereas it should actually show | |
874 | * | |
875 | * 8:cpuset:/ | |
876 | */ | |
877 | if (cgns_supported()) { | |
878 | if (unshare(CLONE_NEWCGROUP) < 0) { | |
879 | INFO("Failed to unshare CLONE_NEWCGROUP."); | |
880 | goto out_warn_father; | |
881 | } | |
882 | INFO("Unshared CLONE_NEWCGROUP."); | |
883 | } | |
884 | ||
72863294 | 885 | /* Set the label to change to when we exec(2) the container's init */ |
7aff4f43 | 886 | if (lsm_process_label_set(NULL, handler->conf, 1, 1) < 0) |
e075f5d9 | 887 | goto out_warn_father; |
5112cd70 | 888 | |
029cdff5 CB |
889 | /* Set PR_SET_NO_NEW_PRIVS after we changed the lsm label. If we do it |
890 | * before we aren't allowed anymore. */ | |
891 | if (handler->conf->no_new_privs) { | |
892 | if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) < 0) { | |
893 | SYSERROR("Could not set PR_SET_NO_NEW_PRIVS to block execve() gainable privileges."); | |
894 | goto out_warn_father; | |
895 | } | |
896 | DEBUG("Set PR_SET_NO_NEW_PRIVS to block execve() gainable privileges."); | |
897 | } | |
898 | ||
0d9acb99 DE |
899 | /* Some init's such as busybox will set sane tty settings on stdin, |
900 | * stdout, stderr which it thinks is the console. We already set them | |
901 | * the way we wanted on the real terminal, and we want init to do its | |
902 | * setup on its console ie. the pty allocated in lxc_console_create() | |
903 | * so make sure that that pty is stdin,stdout,stderr. | |
904 | */ | |
39a78bbe | 905 | if (lxc_console_set_stdfds(handler->conf->console.slave) < 0) |
0d9acb99 DE |
906 | goto out_warn_father; |
907 | ||
5112cd70 SH |
908 | /* If we mounted a temporary proc, then unmount it now */ |
909 | tmp_proc_unmount(handler->conf); | |
e075f5d9 | 910 | |
8f2c3a70 SH |
911 | if (lxc_seccomp_load(handler->conf) != 0) |
912 | goto out_warn_father; | |
913 | ||
283678ed | 914 | if (run_lxc_hooks(handler->name, "start", handler->conf, handler->lxcpath, NULL)) { |
773fb9ca SH |
915 | ERROR("failed to run start hooks for container '%s'.", handler->name); |
916 | goto out_warn_father; | |
917 | } | |
fc25b815 | 918 | |
f7bee6c6 | 919 | /* The clearenv() and putenv() calls have been moved here |
ec64264d | 920 | * to allow us to use environment variables passed to the various |
f7bee6c6 MW |
921 | * hooks, such as the start hook above. Not all of the |
922 | * variables like CONFIG_PATH or ROOTFS are valid in this | |
923 | * context but others are. */ | |
924 | if (clearenv()) { | |
925 | SYSERROR("failed to clear environment"); | |
926 | /* don't error out though */ | |
927 | } | |
928 | ||
7c661726 MP |
929 | lxc_list_for_each(iterator, &handler->conf->environment) { |
930 | if (putenv((char *)iterator->elem)) { | |
931 | SYSERROR("failed to set environment variable '%s'", (char *)iterator->elem); | |
932 | goto out_warn_father; | |
933 | } | |
934 | } | |
935 | ||
f7bee6c6 | 936 | if (putenv("container=lxc")) { |
7c661726 | 937 | SYSERROR("failed to set environment variable 'container=lxc'"); |
c4ea60df | 938 | goto out_warn_father; |
f7bee6c6 MW |
939 | } |
940 | ||
393903d1 SH |
941 | if (handler->conf->pty_names) { |
942 | if (putenv(handler->conf->pty_names)) { | |
943 | SYSERROR("failed to set environment variable for container ptys"); | |
944 | goto out_warn_father; | |
945 | } | |
946 | } | |
947 | ||
773fb9ca | 948 | close(handler->sigfd); |
26ddeedd | 949 | |
7a55c157 TA |
950 | if (devnull_fd < 0) { |
951 | devnull_fd = open_devnull(); | |
952 | ||
953 | if (devnull_fd < 0) | |
954 | goto out_warn_father; | |
955 | } | |
956 | ||
c44de748 | 957 | if (handler->backgrounded && set_stdfds(devnull_fd)) |
69aeabac | 958 | goto out_warn_father; |
507cee36 | 959 | |
c44de748 AM |
960 | if (devnull_fd >= 0) { |
961 | close(devnull_fd); | |
962 | devnull_fd = -1; | |
963 | } | |
964 | ||
8c9a7665 TA |
965 | setsid(); |
966 | ||
e6126dbe MN |
967 | /* after this call, we are in error because this |
968 | * ops should not return as it execs */ | |
c4ea60df | 969 | handler->ops->start(handler, handler->data); |
50e98013 DL |
970 | |
971 | out_warn_father: | |
d1ccb562 AM |
972 | /* we want the parent to know something went wrong, so we return a special |
973 | * error code. */ | |
974 | lxc_sync_wake_parent(handler, LXC_SYNC_ERROR); | |
c44de748 AM |
975 | |
976 | out_error: | |
977 | if (devnull_fd >= 0) | |
978 | close(devnull_fd); | |
979 | ||
50e98013 DL |
980 | return -1; |
981 | } | |
982 | ||
74a3920a | 983 | static int save_phys_nics(struct lxc_conf *conf) |
7b35f3d6 SH |
984 | { |
985 | struct lxc_list *iterator; | |
40f2f8a2 | 986 | int am_root = (getuid() == 0); |
7b35f3d6 | 987 | |
40f2f8a2 LQ |
988 | if (!am_root) |
989 | return 0; | |
2c5f2ede | 990 | |
7b35f3d6 SH |
991 | lxc_list_for_each(iterator, &conf->network) { |
992 | struct lxc_netdev *netdev = iterator->elem; | |
993 | ||
994 | if (netdev->type != LXC_NET_PHYS) | |
995 | continue; | |
996 | conf->saved_nics = realloc(conf->saved_nics, | |
997 | (conf->num_savednics+1)*sizeof(struct saved_nic)); | |
998 | if (!conf->saved_nics) { | |
999 | SYSERROR("failed to allocate memory"); | |
1000 | return -1; | |
1001 | } | |
1002 | conf->saved_nics[conf->num_savednics].ifindex = netdev->ifindex; | |
1003 | conf->saved_nics[conf->num_savednics].orig_name = strdup(netdev->link); | |
1004 | if (!conf->saved_nics[conf->num_savednics].orig_name) { | |
1005 | SYSERROR("failed to allocate memory"); | |
1006 | return -1; | |
1007 | } | |
959aee9c | 1008 | INFO("stored saved_nic #%d idx %d name %s", conf->num_savednics, |
7b35f3d6 SH |
1009 | conf->saved_nics[conf->num_savednics].ifindex, |
1010 | conf->saved_nics[conf->num_savednics].orig_name); | |
1011 | conf->num_savednics++; | |
1012 | } | |
1013 | ||
1014 | return 0; | |
1015 | } | |
1016 | ||
e8bd4e43 SH |
1017 | static int recv_fd(int sock, int *fd) |
1018 | { | |
1019 | if (lxc_abstract_unix_recv_fd(sock, fd, NULL, 0) < 0) { | |
1020 | SYSERROR("Error receiving tty fd from child"); | |
1021 | return -1; | |
1022 | } | |
1023 | if (*fd == -1) | |
1024 | return -1; | |
1025 | return 0; | |
1026 | } | |
1027 | ||
1028 | static int recv_ttys_from_child(struct lxc_handler *handler) | |
1029 | { | |
1030 | struct lxc_conf *conf = handler->conf; | |
1031 | int i, sock = handler->ttysock[1]; | |
1032 | struct lxc_tty_info *tty_info = &conf->tty_info; | |
1033 | ||
1034 | if (!conf->tty) | |
1035 | return 0; | |
1036 | ||
1037 | tty_info->pty_info = malloc(sizeof(*tty_info->pty_info)*conf->tty); | |
1038 | if (!tty_info->pty_info) { | |
1039 | SYSERROR("failed to allocate pty_info"); | |
1040 | return -1; | |
1041 | } | |
1042 | ||
1043 | for (i = 0; i < conf->tty; i++) { | |
1044 | struct lxc_pty_info *pty_info = &tty_info->pty_info[i]; | |
1045 | pty_info->busy = 0; | |
1046 | if (recv_fd(sock, &pty_info->slave) < 0 || | |
1047 | recv_fd(sock, &pty_info->master) < 0) { | |
1048 | ERROR("Error receiving tty info from child"); | |
1049 | return -1; | |
1050 | } | |
1051 | } | |
1052 | tty_info->nbtty = conf->tty; | |
1053 | ||
1054 | return 0; | |
1055 | } | |
1056 | ||
f813849c TA |
1057 | void resolve_clone_flags(struct lxc_handler *handler) |
1058 | { | |
1059 | handler->clone_flags = CLONE_NEWPID | CLONE_NEWNS; | |
198cbbaa CB |
1060 | INFO("Adding CLONE_NEWPID to clone flags."); |
1061 | INFO("Adding CLONE_NEWNS to clone flags."); | |
f813849c TA |
1062 | |
1063 | if (!lxc_list_empty(&handler->conf->id_map)) { | |
198cbbaa | 1064 | INFO("Adding CLONE_NEWUSER to clone flags."); |
f813849c TA |
1065 | handler->clone_flags |= CLONE_NEWUSER; |
1066 | } | |
1067 | ||
1068 | if (handler->conf->inherit_ns_fd[LXC_NS_NET] == -1) { | |
738d0deb | 1069 | if (!lxc_requests_empty_network(handler)) { |
198cbbaa | 1070 | INFO("Adding CLONE_NEWNET to clone flags."); |
f813849c | 1071 | handler->clone_flags |= CLONE_NEWNET; |
738d0deb | 1072 | } |
f813849c | 1073 | } else { |
198cbbaa | 1074 | INFO("Inheriting a NET namespace."); |
f813849c TA |
1075 | } |
1076 | ||
1077 | if (handler->conf->inherit_ns_fd[LXC_NS_IPC] == -1) { | |
198cbbaa | 1078 | INFO("Adding CLONE_NEWIPC to clone flags."); |
f813849c TA |
1079 | handler->clone_flags |= CLONE_NEWIPC; |
1080 | } else { | |
198cbbaa | 1081 | INFO("Inheriting an IPC namespace."); |
f813849c TA |
1082 | } |
1083 | ||
1084 | if (handler->conf->inherit_ns_fd[LXC_NS_UTS] == -1) { | |
198cbbaa | 1085 | INFO("Adding CLONE_NEWUTS to clone flags."); |
f813849c TA |
1086 | handler->clone_flags |= CLONE_NEWUTS; |
1087 | } else { | |
198cbbaa | 1088 | INFO("Inheriting a UTS namespace."); |
f813849c TA |
1089 | } |
1090 | } | |
1091 | ||
74a3920a | 1092 | static int lxc_spawn(struct lxc_handler *handler) |
59eb99ba | 1093 | { |
b98f7d6e | 1094 | int failed_before_rename = 0; |
ffe1e01a | 1095 | const char *name = handler->name; |
62d05d9b | 1096 | char *errmsg = NULL; |
7e4dfe0b | 1097 | bool cgroups_connected = false; |
9f30a190 | 1098 | int saved_ns_fd[LXC_NS_MAX]; |
5b1e83cb | 1099 | int preserve_mask = 0, i, flags; |
658979c5 | 1100 | int netpipepair[2], nveths; |
9f30a190 | 1101 | |
2f2623ec SH |
1102 | netpipe = -1; |
1103 | ||
9f30a190 | 1104 | for (i = 0; i < LXC_NS_MAX; i++) |
6c544cb3 | 1105 | if (handler->conf->inherit_ns_fd[i] != -1) |
9f30a190 | 1106 | preserve_mask |= ns_info[i].clone_flag; |
50e98013 | 1107 | |
3c22086f | 1108 | if (lxc_sync_init(handler)) |
9d7f9e52 | 1109 | return -1; |
0ad19a3f | 1110 | |
e8bd4e43 SH |
1111 | if (socketpair(AF_UNIX, SOCK_DGRAM, 0, handler->ttysock) < 0) { |
1112 | lxc_sync_fini(handler); | |
1113 | return -1; | |
1114 | } | |
1115 | ||
f813849c | 1116 | resolve_clone_flags(handler); |
9f30a190 | 1117 | |
f813849c | 1118 | if (handler->clone_flags & CLONE_NEWNET) { |
26b797f3 SH |
1119 | if (!lxc_list_empty(&handler->conf->network)) { |
1120 | ||
9f30a190 MM |
1121 | /* Find gateway addresses from the link device, which is |
1122 | * no longer accessible inside the container. Do this | |
1123 | * before creating network interfaces, since goto | |
1124 | * out_delete_net does not work before lxc_clone. */ | |
1125 | if (lxc_find_gateway_addresses(handler)) { | |
1126 | ERROR("failed to find gateway addresses"); | |
1127 | lxc_sync_fini(handler); | |
1128 | return -1; | |
1129 | } | |
1130 | ||
1131 | /* that should be done before the clone because we will | |
1132 | * fill the netdev index and use them in the child | |
1133 | */ | |
1134 | if (lxc_create_network(handler)) { | |
38521a3b | 1135 | ERROR("failed to create the network"); |
9f30a190 MM |
1136 | lxc_sync_fini(handler); |
1137 | return -1; | |
1138 | } | |
19a26f82 MK |
1139 | } |
1140 | ||
9f30a190 MM |
1141 | if (save_phys_nics(handler->conf)) { |
1142 | ERROR("failed to save physical nic info"); | |
1143 | goto out_abort; | |
82d5ae15 DL |
1144 | } |
1145 | } | |
1146 | ||
d4ef7c50 SH |
1147 | if (!cgroup_init(handler)) { |
1148 | ERROR("failed initializing cgroup support"); | |
33ad9f1a CS |
1149 | goto out_delete_net; |
1150 | } | |
1151 | ||
7e4dfe0b SH |
1152 | cgroups_connected = true; |
1153 | ||
d4ef7c50 SH |
1154 | if (!cgroup_create(handler)) { |
1155 | ERROR("failed creating cgroups"); | |
47d8fb3b CS |
1156 | goto out_delete_net; |
1157 | } | |
1158 | ||
0c547523 SH |
1159 | /* |
1160 | * if the rootfs is not a blockdev, prevent the container from | |
1161 | * marking it readonly. | |
5e32a990 ÇO |
1162 | * |
1163 | * if the container is unprivileged then skip rootfs pinning | |
0c547523 | 1164 | */ |
5e32a990 ÇO |
1165 | if (lxc_list_empty(&handler->conf->id_map)) { |
1166 | handler->pinfd = pin_rootfs(handler->conf->rootfs.path); | |
1167 | if (handler->pinfd == -1) | |
1168 | INFO("failed to pin the container's rootfs"); | |
1169 | } | |
0c547523 | 1170 | |
62d05d9b SH |
1171 | if (!preserve_ns(saved_ns_fd, preserve_mask, getpid(), &errmsg)) { |
1172 | SYSERROR("Failed to preserve requested namespaces: %s", | |
1173 | errmsg ? errmsg : "(Out of memory)"); | |
1174 | free(errmsg); | |
cd43d2d1 | 1175 | goto out_delete_net; |
62d05d9b | 1176 | } |
cd43d2d1 SH |
1177 | if (attach_ns(handler->conf->inherit_ns_fd) < 0) |
1178 | goto out_delete_net; | |
9f30a190 | 1179 | |
658979c5 SH |
1180 | if (am_unpriv() && (nveths = count_veths(&handler->conf->network))) { |
1181 | if (pipe(netpipepair) < 0) { | |
1182 | SYSERROR("Error creating pipe"); | |
1183 | goto out_delete_net; | |
1184 | } | |
1185 | /* store netpipe in the global var for do_start's use */ | |
1186 | netpipe = netpipepair[0]; | |
1187 | } | |
1188 | ||
0ad19a3f | 1189 | /* Create a process in a new set of namespaces */ |
5b1e83cb SH |
1190 | flags = handler->clone_flags; |
1191 | if (handler->clone_flags & CLONE_NEWUSER) | |
1192 | flags &= ~CLONE_NEWNET; | |
9fac8fbb | 1193 | handler->pid = lxc_clone(do_start, handler, flags); |
59eb99ba | 1194 | if (handler->pid < 0) { |
198cbbaa | 1195 | SYSERROR("Failed to fork into a set of new namespaces."); |
7fef7a06 | 1196 | goto out_delete_net; |
198cbbaa CB |
1197 | } else { |
1198 | INFO("Cloned a set of new namespaces."); | |
0ad19a3f | 1199 | } |
1200 | ||
330cbe36 | 1201 | if (!preserve_ns(handler->nsfd, handler->clone_flags | preserve_mask, handler->pid, &errmsg)) { |
62d05d9b SH |
1202 | INFO("Failed to store namespace references for stop hook: %s", |
1203 | errmsg ? errmsg : "(Out of memory)"); | |
1204 | free(errmsg); | |
b6b2b194 WB |
1205 | } |
1206 | ||
cd43d2d1 SH |
1207 | if (attach_ns(saved_ns_fd)) |
1208 | WARN("failed to restore saved namespaces"); | |
9f30a190 | 1209 | |
3c22086f CLG |
1210 | lxc_sync_fini_child(handler); |
1211 | ||
5b1e83cb SH |
1212 | /* map the container uids - the container became an invalid |
1213 | * userid the moment it was cloned with CLONE_NEWUSER - this | |
1214 | * call doesn't change anything immediately, but allows the | |
1215 | * container to setuid(0) (0 being mapped to something else on | |
1216 | * the host) later to become a valid uid again */ | |
1217 | if (lxc_map_ids(&handler->conf->id_map, handler->pid)) { | |
1218 | ERROR("failed to set up id mapping"); | |
1219 | goto out_delete_net; | |
1220 | } | |
1221 | ||
1222 | if (lxc_sync_wake_child(handler, LXC_SYNC_STARTUP)) { | |
99a6af52 | 1223 | failed_before_rename = 1; |
5b1e83cb SH |
1224 | goto out_delete_net; |
1225 | } | |
1226 | ||
1227 | if (lxc_sync_wait_child(handler, LXC_SYNC_CONFIGURE)) { | |
1228 | failed_before_rename = 1; | |
1229 | goto out_delete_net; | |
1230 | } | |
0ad19a3f | 1231 | |
d4ef7c50 SH |
1232 | if (!cgroup_create_legacy(handler)) { |
1233 | ERROR("failed to setup the legacy cgroups for %s", name); | |
ae5c8b8e | 1234 | goto out_delete_net; |
33ad9f1a | 1235 | } |
9daf6f5d SH |
1236 | if (!cgroup_setup_limits(handler, false)) { |
1237 | ERROR("failed to setup the cgroup limits for '%s'", name); | |
6031a6e5 DE |
1238 | goto out_delete_net; |
1239 | } | |
1240 | ||
d4ef7c50 | 1241 | if (!cgroup_enter(handler)) |
7fef7a06 | 1242 | goto out_delete_net; |
218d4250 | 1243 | |
0996e18a SH |
1244 | if (!cgroup_chown(handler)) |
1245 | goto out_delete_net; | |
1246 | ||
99a6af52 MN |
1247 | if (failed_before_rename) |
1248 | goto out_delete_net; | |
1249 | ||
0ad19a3f | 1250 | /* Create the network configuration */ |
d5088cf2 | 1251 | if (handler->clone_flags & CLONE_NEWNET) { |
c43cbc04 SH |
1252 | if (lxc_assign_network(handler->lxcpath, handler->name, |
1253 | &handler->conf->network, handler->pid)) { | |
82d5ae15 | 1254 | ERROR("failed to create the configured network"); |
7fef7a06 | 1255 | goto out_delete_net; |
82d5ae15 | 1256 | } |
0ad19a3f | 1257 | } |
1258 | ||
658979c5 SH |
1259 | if (netpipe != -1) { |
1260 | struct lxc_list *iterator; | |
1261 | struct lxc_netdev *netdev; | |
1262 | ||
1263 | close(netpipe); | |
1264 | lxc_list_for_each(iterator, &handler->conf->network) { | |
1265 | netdev = iterator->elem; | |
1266 | if (netdev->type != LXC_NET_VETH) | |
1267 | continue; | |
1268 | if (write(netpipepair[1], netdev->name, IFNAMSIZ) != IFNAMSIZ) { | |
1269 | ERROR("Error writing veth name to container"); | |
1270 | goto out_delete_net; | |
1271 | } | |
1272 | } | |
1273 | close(netpipepair[1]); | |
1274 | } | |
1275 | ||
544a48a0 SH |
1276 | /* Tell the child to continue its initialization. we'll get |
1277 | * LXC_SYNC_CGROUP when it is ready for us to setup cgroups | |
3c22086f CLG |
1278 | */ |
1279 | if (lxc_sync_barrier_child(handler, LXC_SYNC_POST_CONFIGURE)) | |
544a48a0 SH |
1280 | goto out_delete_net; |
1281 | ||
9daf6f5d | 1282 | if (!cgroup_setup_limits(handler, true)) { |
b98f7d6e SH |
1283 | ERROR("failed to setup the devices cgroup for '%s'", name); |
1284 | goto out_delete_net; | |
544a48a0 SH |
1285 | } |
1286 | ||
73d28d42 | 1287 | cgroup_disconnect(); |
7e4dfe0b | 1288 | cgroups_connected = false; |
73d28d42 | 1289 | |
e8bd4e43 SH |
1290 | /* read tty fds allocated by child */ |
1291 | if (recv_ttys_from_child(handler) < 0) { | |
1292 | ERROR("failed to receive tty info from child"); | |
1293 | goto out_delete_net; | |
1294 | } | |
1295 | ||
544a48a0 SH |
1296 | /* Tell the child to complete its initialization and wait for |
1297 | * it to exec or return an error. (the child will never | |
1298 | * return LXC_SYNC_POST_CGROUP+1. It will either close the | |
1299 | * sync pipe, causing lxc_sync_barrier_child to return | |
1300 | * success, or return a different value, causing us to error | |
1301 | * out). | |
1302 | */ | |
1303 | if (lxc_sync_barrier_child(handler, LXC_SYNC_POST_CGROUP)) | |
3c22086f | 1304 | return -1; |
0ad19a3f | 1305 | |
cc28d0b0 SH |
1306 | if (detect_shared_rootfs()) |
1307 | umount2(handler->conf->rootfs.mount, MNT_DETACH); | |
1308 | ||
23c53af9 | 1309 | if (handler->ops->post_start(handler, handler->data)) |
e6126dbe MN |
1310 | goto out_abort; |
1311 | ||
25c2aca5 | 1312 | if (lxc_set_state(name, handler, RUNNING)) { |
59eb99ba DL |
1313 | ERROR("failed to set state to %s", |
1314 | lxc_state2str(RUNNING)); | |
1315 | goto out_abort; | |
3f21c114 | 1316 | } |
22ebac19 | 1317 | |
3c22086f | 1318 | lxc_sync_fini(handler); |
738d0deb | 1319 | handler->netnsfd = lxc_preserve_ns(handler->pid, "net"); |
0c547523 | 1320 | |
e6126dbe | 1321 | return 0; |
1ac470c0 | 1322 | |
7fef7a06 | 1323 | out_delete_net: |
7e4dfe0b SH |
1324 | if (cgroups_connected) |
1325 | cgroup_disconnect(); | |
d5088cf2 | 1326 | if (handler->clone_flags & CLONE_NEWNET) |
74a2b586 | 1327 | lxc_delete_network(handler); |
59eb99ba DL |
1328 | out_abort: |
1329 | lxc_abort(name, handler); | |
3c22086f | 1330 | lxc_sync_fini(handler); |
5c068da9 SH |
1331 | if (handler->pinfd >= 0) { |
1332 | close(handler->pinfd); | |
1333 | handler->pinfd = -1; | |
1334 | } | |
1335 | ||
b79fcd86 | 1336 | return -1; |
59eb99ba | 1337 | } |
0ad19a3f | 1338 | |
ee70bf78 | 1339 | int __lxc_start(const char *name, struct lxc_conf *conf, |
507cee36 TA |
1340 | struct lxc_operations* ops, void *data, const char *lxcpath, |
1341 | bool backgrounded) | |
59eb99ba | 1342 | { |
3a0f472d | 1343 | struct lxc_handler *handler; |
e043236e | 1344 | int err = -1; |
59eb99ba | 1345 | int status; |
80090207 | 1346 | |
13f5be62 | 1347 | handler = lxc_init(name, conf, lxcpath); |
3a0f472d | 1348 | if (!handler) { |
59eb99ba | 1349 | ERROR("failed to initialize the container"); |
66aeffc7 | 1350 | return -1; |
0ad19a3f | 1351 | } |
ee70bf78 CLG |
1352 | handler->ops = ops; |
1353 | handler->data = data; | |
507cee36 | 1354 | handler->backgrounded = backgrounded; |
738d0deb | 1355 | handler->netnsfd = -1; |
e6126dbe | 1356 | |
b60ed720 | 1357 | if (must_drop_cap_sys_boot(handler->conf)) { |
495d2046 | 1358 | #if HAVE_SYS_CAPABILITY_H |
959aee9c | 1359 | DEBUG("Dropping cap_sys_boot"); |
495d2046 | 1360 | #else |
959aee9c | 1361 | DEBUG("Can't drop cap_sys_boot as capabilities aren't supported"); |
495d2046 | 1362 | #endif |
69182a31 | 1363 | } else { |
959aee9c | 1364 | DEBUG("Not dropping cap_sys_boot or watching utmp"); |
69182a31 SH |
1365 | handler->conf->need_utmp_watch = 0; |
1366 | } | |
1367 | ||
76a26f55 SH |
1368 | if (!attach_block_device(handler->conf)) { |
1369 | ERROR("Failure attaching block device"); | |
1370 | goto out_fini_nonet; | |
1371 | } | |
1372 | ||
35120d9c SH |
1373 | if (geteuid() == 0 && !lxc_list_empty(&conf->id_map)) { |
1374 | /* if the backing store is a device, mount it here and now */ | |
1375 | if (rootfs_is_blockdev(conf)) { | |
1376 | if (unshare(CLONE_NEWNS) < 0) { | |
198cbbaa | 1377 | ERROR("Error unsharing MOUNT namespace."); |
35120d9c | 1378 | goto out_fini_nonet; |
198cbbaa CB |
1379 | } else { |
1380 | INFO("Unshared MOUNT namespace."); | |
35120d9c | 1381 | } |
6a0c909a | 1382 | remount_all_slave(); |
35120d9c SH |
1383 | if (do_rootfs_setup(conf, name, lxcpath) < 0) { |
1384 | ERROR("Error setting up rootfs mount as root before spawn"); | |
1385 | goto out_fini_nonet; | |
1386 | } | |
1387 | INFO("Set up container rootfs as host root"); | |
1388 | } | |
1389 | } | |
1390 | ||
23c53af9 | 1391 | err = lxc_spawn(handler); |
59eb99ba | 1392 | if (err) { |
ee70bf78 | 1393 | ERROR("failed to spawn '%s'", name); |
76a26f55 | 1394 | goto out_detach_blockdev; |
0ad19a3f | 1395 | } |
1396 | ||
8bee8851 WB |
1397 | handler->conf->reboot = 0; |
1398 | ||
3a0f472d | 1399 | err = lxc_poll(name, handler); |
e043236e | 1400 | if (err) { |
59eb99ba | 1401 | ERROR("mainloop exited with an error"); |
738d0deb CB |
1402 | if (handler->netnsfd >= 0) { |
1403 | close(handler->netnsfd); | |
1404 | handler->netnsfd = -1; | |
1405 | } | |
59eb99ba DL |
1406 | goto out_abort; |
1407 | } | |
0ad19a3f | 1408 | |
3a0f472d | 1409 | while (waitpid(handler->pid, &status, 0) < 0 && errno == EINTR) |
1bc5cc8c | 1410 | continue; |
e043236e | 1411 | |
8b004f07 SH |
1412 | /* |
1413 | * If the child process exited but was not signaled, | |
1414 | * it didn't call reboot. This should mean it was an | |
1415 | * lxc-execute which simply exited. In any case, treat | |
1416 | * it as a 'halt' | |
1417 | */ | |
d028235d | 1418 | if (WIFSIGNALED(status)) { |
8b004f07 SH |
1419 | switch(WTERMSIG(status)) { |
1420 | case SIGINT: /* halt */ | |
1421 | DEBUG("Container halting"); | |
1422 | break; | |
1423 | case SIGHUP: /* reboot */ | |
1424 | DEBUG("Container rebooting"); | |
1425 | handler->conf->reboot = 1; | |
1426 | break; | |
c2b9bd9e SH |
1427 | case SIGSYS: /* seccomp */ |
1428 | DEBUG("Container violated its seccomp policy"); | |
1429 | break; | |
8b004f07 | 1430 | default: |
959aee9c | 1431 | DEBUG("unknown exit status for init: %d", WTERMSIG(status)); |
8b004f07 SH |
1432 | break; |
1433 | } | |
d028235d | 1434 | } |
828695d9 | 1435 | |
ce5782df | 1436 | DEBUG("Pushing physical nics back to host namespace"); |
738d0deb | 1437 | lxc_restore_phys_nics_to_netns(handler->netnsfd, handler->conf); |
ce5782df MH |
1438 | |
1439 | DEBUG("Tearing down virtual network devices used by container"); | |
1440 | lxc_delete_network(handler); | |
1441 | ||
5c068da9 SH |
1442 | if (handler->pinfd >= 0) { |
1443 | close(handler->pinfd); | |
1444 | handler->pinfd = -1; | |
1445 | } | |
1446 | ||
1787abca | 1447 | lxc_monitor_send_exit_code(name, status, handler->lxcpath); |
3a0f472d | 1448 | err = lxc_error_set_and_log(handler->pid, status); |
9d7f9e52 | 1449 | out_fini: |
74a2b586 JK |
1450 | lxc_delete_network(handler); |
1451 | ||
76a26f55 SH |
1452 | out_detach_blockdev: |
1453 | detach_block_device(handler->conf); | |
1454 | ||
74a2b586 | 1455 | out_fini_nonet: |
3a0f472d | 1456 | lxc_fini(name, handler); |
0ad19a3f | 1457 | return err; |
1458 | ||
59eb99ba | 1459 | out_abort: |
3a0f472d | 1460 | lxc_abort(name, handler); |
9d7f9e52 | 1461 | goto out_fini; |
0ad19a3f | 1462 | } |
ee70bf78 CLG |
1463 | |
1464 | struct start_args { | |
1465 | char *const *argv; | |
1466 | }; | |
1467 | ||
1468 | static int start(struct lxc_handler *handler, void* data) | |
1469 | { | |
1470 | struct start_args *arg = data; | |
1471 | ||
1472 | NOTICE("exec'ing '%s'", arg->argv[0]); | |
1473 | ||
1474 | execvp(arg->argv[0], arg->argv); | |
1475 | SYSERROR("failed to exec %s", arg->argv[0]); | |
1476 | return 0; | |
1477 | } | |
1478 | ||
1479 | static int post_start(struct lxc_handler *handler, void* data) | |
1480 | { | |
1481 | struct start_args *arg = data; | |
1482 | ||
1483 | NOTICE("'%s' started with pid '%d'", arg->argv[0], handler->pid); | |
1484 | return 0; | |
1485 | } | |
1486 | ||
1487 | static struct lxc_operations start_ops = { | |
1488 | .start = start, | |
1489 | .post_start = post_start | |
1490 | }; | |
1491 | ||
13f5be62 | 1492 | int lxc_start(const char *name, char *const argv[], struct lxc_conf *conf, |
507cee36 | 1493 | const char *lxcpath, bool backgrounded) |
ee70bf78 CLG |
1494 | { |
1495 | struct start_args start_arg = { | |
1496 | .argv = argv, | |
1497 | }; | |
1498 | ||
828695d9 | 1499 | conf->need_utmp_watch = 1; |
507cee36 | 1500 | return __lxc_start(name, conf, &start_ops, &start_arg, lxcpath, backgrounded); |
ee70bf78 | 1501 | } |
28272964 CB |
1502 | |
1503 | static void lxc_destroy_container_on_signal(struct lxc_handler *handler, | |
1504 | const char *name) | |
1505 | { | |
1506 | char destroy[MAXPATHLEN]; | |
1507 | bool bret = true; | |
1508 | int ret = 0; | |
f01f7975 | 1509 | struct lxc_container *c; |
df31363a | 1510 | if (handler->conf->rootfs.path && handler->conf->rootfs.mount) { |
28272964 CB |
1511 | bret = do_destroy_container(handler->conf); |
1512 | if (!bret) { | |
1513 | ERROR("Error destroying rootfs for %s", name); | |
1514 | return; | |
1515 | } | |
1516 | } | |
1517 | INFO("Destroyed rootfs for %s", name); | |
1518 | ||
1519 | ret = snprintf(destroy, MAXPATHLEN, "%s/%s", handler->lxcpath, name); | |
1520 | if (ret < 0 || ret >= MAXPATHLEN) { | |
1521 | ERROR("Error printing path for %s", name); | |
1522 | ERROR("Error destroying directory for %s", name); | |
1523 | return; | |
1524 | } | |
1525 | ||
f01f7975 CB |
1526 | c = lxc_container_new(name, handler->lxcpath); |
1527 | if (c) { | |
1528 | if (container_disk_lock(c)) { | |
1529 | INFO("Could not update lxc_snapshots file"); | |
1530 | lxc_container_put(c); | |
1531 | } else { | |
1532 | mod_all_rdeps(c, false); | |
1533 | container_disk_unlock(c); | |
1534 | lxc_container_put(c); | |
1535 | } | |
1536 | } | |
1537 | ||
28272964 CB |
1538 | if (am_unpriv()) |
1539 | ret = userns_exec_1(handler->conf, lxc_rmdir_onedev_wrapper, destroy); | |
1540 | else | |
1541 | ret = lxc_rmdir_onedev(destroy, NULL); | |
1542 | ||
1543 | if (ret < 0) { | |
1544 | ERROR("Error destroying directory for %s", name); | |
1545 | return; | |
1546 | } | |
1547 | INFO("Destroyed directory for %s", name); | |
1548 | } | |
1549 | ||
1550 | static int lxc_rmdir_onedev_wrapper(void *data) | |
1551 | { | |
1552 | char *arg = (char *) data; | |
1553 | return lxc_rmdir_onedev(arg, NULL); | |
1554 | } | |
1555 | ||
1556 | static bool do_destroy_container(struct lxc_conf *conf) { | |
d028235d SG |
1557 | if (am_unpriv()) { |
1558 | if (userns_exec_1(conf, bdev_destroy_wrapper, conf) < 0) | |
1559 | return false; | |
1560 | return true; | |
1561 | } | |
1562 | return bdev_destroy(conf); | |
28272964 | 1563 | } |