]>
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 | ||
f549edcc GK |
24 | #include "config.h" |
25 | ||
0ad19a3f | 26 | #include <stdio.h> |
27 | #undef _GNU_SOURCE | |
28 | #include <string.h> | |
29 | #include <stdlib.h> | |
30 | #include <dirent.h> | |
31 | #include <errno.h> | |
32 | #include <unistd.h> | |
33 | #include <signal.h> | |
b0a33c1e | 34 | #include <fcntl.h> |
35 | #include <termios.h> | |
0ad19a3f | 36 | #include <sys/param.h> |
37 | #include <sys/file.h> | |
f4d507d5 | 38 | #include <sys/mount.h> |
b4f8660e | 39 | #include <sys/stat.h> |
0ad19a3f | 40 | #include <sys/types.h> |
8173e600 | 41 | #include <sys/socket.h> |
0ad19a3f | 42 | #include <sys/prctl.h> |
ddceb1f9 | 43 | #include <sys/types.h> |
0ad19a3f | 44 | #include <sys/wait.h> |
b0a33c1e | 45 | #include <sys/un.h> |
46 | #include <sys/poll.h> | |
8173e600 | 47 | #include <sys/syscall.h> |
ff218c25 | 48 | |
495d2046 SG |
49 | #if HAVE_SYS_CAPABILITY_H |
50 | #include <sys/capability.h> | |
51 | #endif | |
52 | ||
656994bb MH |
53 | #if !HAVE_DECL_PR_CAPBSET_DROP |
54 | #define PR_CAPBSET_DROP 24 | |
55 | #endif | |
56 | ||
63376d7d DL |
57 | #include "start.h" |
58 | #include "conf.h" | |
59 | #include "log.h" | |
563f2f2c | 60 | #include "cgroup.h" |
e2bcd7db | 61 | #include "error.h" |
b0a33c1e | 62 | #include "af_unix.h" |
63 | #include "mainloop.h" | |
63376d7d | 64 | #include "utils.h" |
565c2d76 | 65 | #include "lxcutmp.h" |
63376d7d | 66 | #include "monitor.h" |
96fa1ff0 | 67 | #include "commands.h" |
63376d7d | 68 | #include "console.h" |
3c22086f | 69 | #include "sync.h" |
f549edcc | 70 | #include "namespace.h" |
0d0527a9 | 71 | #include "lxcseccomp.h" |
8173e600 | 72 | #include "caps.h" |
fe4de9a6 | 73 | #include "lsm/lsm.h" |
36eb9bde CLG |
74 | |
75 | lxc_log_define(lxc_start, lxc); | |
76 | ||
9f30a190 MM |
77 | const struct ns_info ns_info[LXC_NS_MAX] = { |
78 | [LXC_NS_MNT] = {"mnt", CLONE_NEWNS}, | |
79 | [LXC_NS_PID] = {"pid", CLONE_NEWPID}, | |
80 | [LXC_NS_UTS] = {"uts", CLONE_NEWUTS}, | |
81 | [LXC_NS_IPC] = {"ipc", CLONE_NEWIPC}, | |
82 | [LXC_NS_USER] = {"user", CLONE_NEWUSER}, | |
83 | [LXC_NS_NET] = {"net", CLONE_NEWNET} | |
84 | }; | |
85 | ||
86 | static void close_ns(int ns_fd[LXC_NS_MAX]) { | |
87 | int i; | |
88 | ||
9f30a190 MM |
89 | for (i = 0; i < LXC_NS_MAX; i++) { |
90 | if (ns_fd[i] > -1) { | |
91 | close(ns_fd[i]); | |
92 | ns_fd[i] = -1; | |
93 | } | |
94 | } | |
9f30a190 MM |
95 | } |
96 | ||
97 | static int preserve_ns(int ns_fd[LXC_NS_MAX], int clone_flags) { | |
98 | int i, saved_errno; | |
99 | char path[MAXPATHLEN]; | |
100 | ||
9f30a190 MM |
101 | for (i = 0; i < LXC_NS_MAX; i++) |
102 | ns_fd[i] = -1; | |
103 | ||
cd43d2d1 SH |
104 | if (access("/proc/self/ns", X_OK)) { |
105 | WARN("Kernel does not support attach; preserve_ns ignored"); | |
106 | return 0; | |
107 | } | |
108 | ||
9f30a190 MM |
109 | for (i = 0; i < LXC_NS_MAX; i++) { |
110 | if ((clone_flags & ns_info[i].clone_flag) == 0) | |
111 | continue; | |
112 | snprintf(path, MAXPATHLEN, "/proc/self/ns/%s", ns_info[i].proc_name); | |
9f30a190 | 113 | ns_fd[i] = open(path, O_RDONLY | O_CLOEXEC); |
9f30a190 MM |
114 | if (ns_fd[i] < 0) |
115 | goto error; | |
116 | } | |
117 | ||
118 | return 0; | |
119 | ||
120 | error: | |
121 | saved_errno = errno; | |
122 | close_ns(ns_fd); | |
123 | errno = saved_errno; | |
124 | SYSERROR("failed to open '%s'", path); | |
125 | return -1; | |
126 | } | |
127 | ||
128 | static int attach_ns(const int ns_fd[LXC_NS_MAX]) { | |
129 | int i; | |
130 | ||
131 | for (i = 0; i < LXC_NS_MAX; i++) { | |
132 | if (ns_fd[i] < 0) | |
133 | continue; | |
134 | ||
135 | if (setns(ns_fd[i], 0) != 0) | |
136 | goto error; | |
137 | } | |
138 | return 0; | |
139 | ||
140 | error: | |
141 | SYSERROR("failed to set namespace '%s'", ns_info[i].proc_name); | |
142 | return -1; | |
143 | } | |
144 | ||
80090207 CLG |
145 | static int match_fd(int fd) |
146 | { | |
147 | return (fd == 0 || fd == 1 || fd == 2); | |
148 | } | |
149 | ||
b119f362 | 150 | int lxc_check_inherited(struct lxc_conf *conf, int fd_to_ignore) |
80090207 CLG |
151 | { |
152 | struct dirent dirent, *direntp; | |
153 | int fd, fddir; | |
154 | DIR *dir; | |
80090207 | 155 | |
b119f362 | 156 | restart: |
80090207 CLG |
157 | dir = opendir("/proc/self/fd"); |
158 | if (!dir) { | |
159 | WARN("failed to open directory: %m"); | |
160 | return -1; | |
161 | } | |
162 | ||
163 | fddir = dirfd(dir); | |
164 | ||
165 | while (!readdir_r(dir, &dirent, &direntp)) { | |
80090207 CLG |
166 | if (!direntp) |
167 | break; | |
168 | ||
169 | if (!strcmp(direntp->d_name, ".")) | |
170 | continue; | |
171 | ||
172 | if (!strcmp(direntp->d_name, "..")) | |
173 | continue; | |
174 | ||
175 | fd = atoi(direntp->d_name); | |
176 | ||
f2faa8fa | 177 | if (fd == fddir || fd == lxc_log_fd || fd == fd_to_ignore) |
80090207 CLG |
178 | continue; |
179 | ||
180 | if (match_fd(fd)) | |
181 | continue; | |
80090207 | 182 | |
b119f362 SH |
183 | if (conf->close_all_fds) { |
184 | close(fd); | |
185 | closedir(dir); | |
186 | INFO("closed inherited fd %d", fd); | |
187 | goto restart; | |
188 | } | |
92c7f629 | 189 | WARN("inherited fd %d", fd); |
80090207 CLG |
190 | } |
191 | ||
92c7f629 GK |
192 | closedir(dir); /* cannot fail */ |
193 | return 0; | |
80090207 CLG |
194 | } |
195 | ||
83ee7875 | 196 | static int setup_signal_fd(sigset_t *oldmask) |
b0a33c1e | 197 | { |
198 | sigset_t mask; | |
199 | int fd; | |
200 | ||
f3304a29 FW |
201 | /* Block everything except serious error signals */ |
202 | if (sigfillset(&mask) || | |
203 | sigdelset(&mask, SIGILL) || | |
204 | sigdelset(&mask, SIGSEGV) || | |
205 | sigdelset(&mask, SIGBUS) || | |
b5159817 | 206 | sigdelset(&mask, SIGWINCH) || |
f3304a29 FW |
207 | sigprocmask(SIG_BLOCK, &mask, oldmask)) { |
208 | SYSERROR("failed to set signal mask"); | |
b0a33c1e | 209 | return -1; |
210 | } | |
211 | ||
212 | fd = signalfd(-1, &mask, 0); | |
213 | if (fd < 0) { | |
36eb9bde | 214 | SYSERROR("failed to create the signal fd"); |
b0a33c1e | 215 | return -1; |
216 | } | |
217 | ||
218 | if (fcntl(fd, F_SETFD, FD_CLOEXEC)) { | |
36eb9bde | 219 | SYSERROR("failed to set sigfd to close-on-exec"); |
b0a33c1e | 220 | close(fd); |
221 | return -1; | |
222 | } | |
223 | ||
1ac470c0 DL |
224 | DEBUG("sigchild handler set"); |
225 | ||
b0a33c1e | 226 | return fd; |
227 | } | |
228 | ||
84c92abd | 229 | static int signal_handler(int fd, uint32_t events, void *data, |
b0a33c1e | 230 | struct lxc_epoll_descr *descr) |
231 | { | |
15cd25fd | 232 | struct signalfd_siginfo siginfo; |
80507ee8 | 233 | siginfo_t info; |
15cd25fd | 234 | int ret; |
82d89dce | 235 | pid_t *pid = data; |
80507ee8 | 236 | bool init_died = false; |
15cd25fd DL |
237 | |
238 | ret = read(fd, &siginfo, sizeof(siginfo)); | |
239 | if (ret < 0) { | |
f3304a29 | 240 | ERROR("failed to read signal info"); |
15cd25fd DL |
241 | return -1; |
242 | } | |
243 | ||
244 | if (ret != sizeof(siginfo)) { | |
245 | ERROR("unexpected siginfo size"); | |
246 | return -1; | |
247 | } | |
248 | ||
80507ee8 SH |
249 | // check whether init is running |
250 | info.si_pid = 0; | |
251 | ret = waitid(P_PID, *pid, &info, WEXITED | WNOWAIT | WNOHANG); | |
252 | if (ret == 0 && info.si_pid == *pid) { | |
253 | init_died = true; | |
254 | } | |
255 | ||
f3304a29 FW |
256 | if (siginfo.ssi_signo != SIGCHLD) { |
257 | kill(*pid, siginfo.ssi_signo); | |
258 | INFO("forwarded signal %d to pid %d", siginfo.ssi_signo, *pid); | |
80507ee8 | 259 | return init_died ? 1 : 0; |
f3304a29 FW |
260 | } |
261 | ||
15cd25fd DL |
262 | if (siginfo.ssi_code == CLD_STOPPED || |
263 | siginfo.ssi_code == CLD_CONTINUED) { | |
264 | INFO("container init process was stopped/continued"); | |
80507ee8 | 265 | return init_died ? 1 : 0; |
15cd25fd | 266 | } |
1ac470c0 | 267 | |
82d89dce DL |
268 | /* more robustness, protect ourself from a SIGCHLD sent |
269 | * by a process different from the container init | |
270 | */ | |
271 | if (siginfo.ssi_pid != *pid) { | |
272 | WARN("invalid pid for SIGCHLD"); | |
80507ee8 | 273 | return init_died ? 1 : 0; |
82d89dce DL |
274 | } |
275 | ||
15cd25fd | 276 | DEBUG("container init process exited"); |
b0a33c1e | 277 | return 1; |
278 | } | |
279 | ||
74a3920a | 280 | static int lxc_set_state(const char *name, struct lxc_handler *handler, lxc_state_t state) |
66aeffc7 DL |
281 | { |
282 | handler->state = state; | |
9123e471 | 283 | lxc_monitor_send_state(name, state, handler->lxcpath); |
66aeffc7 DL |
284 | return 0; |
285 | } | |
286 | ||
74a3920a | 287 | static int lxc_poll(const char *name, struct lxc_handler *handler) |
b0a33c1e | 288 | { |
ca5f7926 DL |
289 | int sigfd = handler->sigfd; |
290 | int pid = handler->pid; | |
b0a33c1e | 291 | struct lxc_epoll_descr descr; |
292 | ||
a9e61274 | 293 | if (lxc_mainloop_open(&descr)) { |
36eb9bde | 294 | ERROR("failed to create mainloop"); |
50c8bf05 | 295 | goto out_sigfd; |
b0a33c1e | 296 | } |
297 | ||
83ee7875 | 298 | if (lxc_mainloop_add_handler(&descr, sigfd, signal_handler, &pid)) { |
36eb9bde | 299 | ERROR("failed to add handler for the signal"); |
b0a33c1e | 300 | goto out_mainloop_open; |
301 | } | |
302 | ||
63376d7d DL |
303 | if (lxc_console_mainloop_add(&descr, handler)) { |
304 | ERROR("failed to add console handler to mainloop"); | |
305 | goto out_mainloop_open; | |
306 | } | |
307 | ||
ef6e34ee | 308 | if (lxc_cmd_mainloop_add(name, &descr, handler)) { |
563f2f2c | 309 | ERROR("failed to add command handler to mainloop"); |
96fa1ff0 | 310 | goto out_mainloop_open; |
563f2f2c DL |
311 | } |
312 | ||
828695d9 | 313 | if (handler->conf->need_utmp_watch) { |
495d2046 | 314 | #if HAVE_SYS_CAPABILITY_H |
828695d9 SH |
315 | if (lxc_utmp_mainloop_add(&descr, handler)) { |
316 | ERROR("failed to add utmp handler to mainloop"); | |
317 | goto out_mainloop_open; | |
318 | } | |
495d2046 SG |
319 | #else |
320 | DEBUG("not starting utmp handler as cap_sys_boot cannot be dropped without capabilities support\n"); | |
321 | #endif | |
563f2f2c | 322 | } |
b0a33c1e | 323 | |
e51d4895 | 324 | return lxc_mainloop(&descr, -1); |
b0a33c1e | 325 | |
326 | out_mainloop_open: | |
327 | lxc_mainloop_close(&descr); | |
b0a33c1e | 328 | out_sigfd: |
329 | close(sigfd); | |
c3e13372 | 330 | return -1; |
b0a33c1e | 331 | } |
332 | ||
13f5be62 | 333 | struct lxc_handler *lxc_init(const char *name, struct lxc_conf *conf, const char *lxcpath) |
59eb99ba | 334 | { |
3a0f472d DL |
335 | struct lxc_handler *handler; |
336 | ||
337 | handler = malloc(sizeof(*handler)); | |
338 | if (!handler) | |
339 | return NULL; | |
59eb99ba DL |
340 | |
341 | memset(handler, 0, sizeof(*handler)); | |
342 | ||
fae349da | 343 | handler->conf = conf; |
9123e471 | 344 | handler->lxcpath = lxcpath; |
5c068da9 | 345 | handler->pinfd = -1; |
fae349da | 346 | |
fe4de9a6 DE |
347 | lsm_init(); |
348 | ||
3bdf52d7 DL |
349 | handler->name = strdup(name); |
350 | if (!handler->name) { | |
351 | ERROR("failed to allocate memory"); | |
352 | goto out_free; | |
353 | } | |
354 | ||
ef6e34ee | 355 | if (lxc_cmd_init(name, handler, lxcpath)) |
d2e30e99 DE |
356 | goto out_free_name; |
357 | ||
8f2c3a70 SH |
358 | if (lxc_read_seccomp_config(conf) != 0) { |
359 | ERROR("failed loading seccomp policy"); | |
d2e30e99 | 360 | goto out_close_maincmd_fd; |
8f2c3a70 SH |
361 | } |
362 | ||
051151de | 363 | /* Begin by setting the state to STARTING */ |
25c2aca5 | 364 | if (lxc_set_state(name, handler, STARTING)) { |
59eb99ba | 365 | ERROR("failed to set state '%s'", lxc_state2str(STARTING)); |
051151de | 366 | goto out_close_maincmd_fd; |
0ad19a3f | 367 | } |
368 | ||
f7bee6c6 MW |
369 | /* Start of environment variable setup for hooks */ |
370 | if (setenv("LXC_NAME", name, 1)) { | |
371 | SYSERROR("failed to set environment variable for container name"); | |
372 | } | |
373 | if (setenv("LXC_CONFIG_FILE", conf->rcfile, 1)) { | |
374 | SYSERROR("failed to set environment variable for config path"); | |
375 | } | |
376 | if (setenv("LXC_ROOTFS_MOUNT", conf->rootfs.mount, 1)) { | |
377 | SYSERROR("failed to set environment variable for rootfs mount"); | |
378 | } | |
379 | if (setenv("LXC_ROOTFS_PATH", conf->rootfs.path, 1)) { | |
380 | SYSERROR("failed to set environment variable for rootfs mount"); | |
381 | } | |
382 | if (conf->console.path && setenv("LXC_CONSOLE", conf->console.path, 1)) { | |
383 | SYSERROR("failed to set environment variable for console path"); | |
384 | } | |
385 | if (conf->console.log_path && setenv("LXC_CONSOLE_LOGPATH", conf->console.log_path, 1)) { | |
386 | SYSERROR("failed to set environment variable for console log"); | |
387 | } | |
388 | /* End of environment variable setup for hooks */ | |
389 | ||
283678ed | 390 | if (run_lxc_hooks(name, "pre-start", conf, handler->lxcpath, NULL)) { |
773fb9ca SH |
391 | ERROR("failed to run pre-start hooks for container '%s'.", name); |
392 | goto out_aborting; | |
393 | } | |
26ddeedd | 394 | |
fae349da | 395 | if (lxc_create_tty(name, conf)) { |
36eb9bde | 396 | ERROR("failed to create the ttys"); |
59eb99ba | 397 | goto out_aborting; |
b0a33c1e | 398 | } |
399 | ||
400 | /* the signal fd has to be created before forking otherwise | |
401 | * if the child process exits before we setup the signal fd, | |
402 | * the event will be lost and the command will be stuck */ | |
83ee7875 | 403 | handler->sigfd = setup_signal_fd(&handler->oldmask); |
59eb99ba | 404 | if (handler->sigfd < 0) { |
36eb9bde | 405 | ERROR("failed to set sigchild fd handler"); |
b5159817 DE |
406 | goto out_delete_tty; |
407 | } | |
408 | ||
409 | /* do this after setting up signals since it might unblock SIGWINCH */ | |
410 | if (lxc_console_create(conf)) { | |
411 | ERROR("failed to create console"); | |
412 | goto out_restore_sigmask; | |
b0a33c1e | 413 | } |
414 | ||
c4d10a05 SH |
415 | if (ttys_shift_ids(conf) < 0) { |
416 | ERROR("Failed to shift tty into container"); | |
417 | goto out_restore_sigmask; | |
418 | } | |
419 | ||
c3e13372 | 420 | INFO("'%s' is initialized", name); |
3a0f472d | 421 | return handler; |
59eb99ba | 422 | |
b5159817 DE |
423 | out_restore_sigmask: |
424 | sigprocmask(SIG_SETMASK, &handler->oldmask, NULL); | |
59eb99ba | 425 | out_delete_tty: |
fae349da | 426 | lxc_delete_tty(&conf->tty_info); |
59eb99ba | 427 | out_aborting: |
25c2aca5 | 428 | lxc_set_state(name, handler, ABORTING); |
d2e30e99 DE |
429 | out_close_maincmd_fd: |
430 | close(conf->maincmd_fd); | |
431 | conf->maincmd_fd = -1; | |
3bdf52d7 DL |
432 | out_free_name: |
433 | free(handler->name); | |
434 | handler->name = NULL; | |
3a0f472d DL |
435 | out_free: |
436 | free(handler); | |
c3e13372 | 437 | return NULL; |
59eb99ba DL |
438 | } |
439 | ||
ae5c8b8e | 440 | static void lxc_fini(const char *name, struct lxc_handler *handler) |
59eb99ba DL |
441 | { |
442 | /* The STOPPING state is there for future cleanup code | |
443 | * which can take awhile | |
444 | */ | |
25c2aca5 MN |
445 | lxc_set_state(name, handler, STOPPING); |
446 | lxc_set_state(name, handler, STOPPED); | |
59eb99ba | 447 | |
283678ed | 448 | if (run_lxc_hooks(name, "post-stop", handler->conf, handler->lxcpath, NULL)) |
773fb9ca | 449 | ERROR("failed to run post-stop hooks for container '%s'.", name); |
26ddeedd | 450 | |
83ee7875 | 451 | /* reset mask set by setup_signal_fd */ |
8f64a3f6 MN |
452 | if (sigprocmask(SIG_SETMASK, &handler->oldmask, NULL)) |
453 | WARN("failed to restore sigprocmask"); | |
454 | ||
b5159817 | 455 | lxc_console_delete(&handler->conf->console); |
b2431939 | 456 | lxc_delete_tty(&handler->conf->tty_info); |
d2e30e99 DE |
457 | close(handler->conf->maincmd_fd); |
458 | handler->conf->maincmd_fd = -1; | |
3bdf52d7 | 459 | free(handler->name); |
d4ef7c50 | 460 | cgroup_destroy(handler); |
b2431939 | 461 | free(handler); |
59eb99ba DL |
462 | } |
463 | ||
74a3920a | 464 | static void lxc_abort(const char *name, struct lxc_handler *handler) |
59eb99ba | 465 | { |
73e608b2 SH |
466 | int ret, status; |
467 | ||
25c2aca5 | 468 | lxc_set_state(name, handler, ABORTING); |
7d9fb3e9 DL |
469 | if (handler->pid > 0) |
470 | kill(handler->pid, SIGKILL); | |
73e608b2 | 471 | while ((ret = waitpid(-1, &status, 0)) > 0) ; |
59eb99ba DL |
472 | } |
473 | ||
828695d9 SH |
474 | #include <sys/reboot.h> |
475 | #include <linux/reboot.h> | |
476 | ||
e2fa1520 SH |
477 | /* |
478 | * reboot(LINUX_REBOOT_CMD_CAD_ON) will return -EINVAL | |
479 | * in a child pid namespace if container reboot support exists. | |
480 | * Otherwise, it will either succeed or return -EPERM. | |
481 | */ | |
482 | static int container_reboot_supported(void *arg) | |
828695d9 | 483 | { |
e2fa1520 | 484 | int *cmd = arg; |
828695d9 | 485 | int ret; |
828695d9 | 486 | |
e2fa1520 SH |
487 | ret = reboot(*cmd); |
488 | if (ret == -1 && errno == EINVAL) | |
489 | return 1; | |
490 | return 0; | |
491 | } | |
492 | ||
b60ed720 | 493 | static int must_drop_cap_sys_boot(struct lxc_conf *conf) |
e2fa1520 | 494 | { |
025ed0f3 | 495 | FILE *f; |
b60ed720 | 496 | int ret, cmd, v, flags; |
e2fa1520 | 497 | long stack_size = 4096; |
7f145a6d | 498 | void *stack = alloca(stack_size); |
e2fa1520 SH |
499 | int status; |
500 | pid_t pid; | |
501 | ||
025ed0f3 | 502 | f = fopen("/proc/sys/kernel/ctrl-alt-del", "r"); |
e2fa1520 SH |
503 | if (!f) { |
504 | DEBUG("failed to open /proc/sys/kernel/ctrl-alt-del"); | |
828695d9 | 505 | return 1; |
e2fa1520 | 506 | } |
828695d9 SH |
507 | |
508 | ret = fscanf(f, "%d", &v); | |
509 | fclose(f); | |
e2fa1520 SH |
510 | if (ret != 1) { |
511 | DEBUG("Failed to read /proc/sys/kernel/ctrl-alt-del"); | |
828695d9 | 512 | return 1; |
e2fa1520 SH |
513 | } |
514 | cmd = v ? LINUX_REBOOT_CMD_CAD_ON : LINUX_REBOOT_CMD_CAD_OFF; | |
515 | ||
b60ed720 SH |
516 | flags = CLONE_NEWPID | SIGCHLD; |
517 | if (!lxc_list_empty(&conf->id_map)) | |
518 | flags |= CLONE_NEWUSER; | |
519 | ||
7f145a6d | 520 | #ifdef __ia64__ |
b60ed720 | 521 | pid = __clone2(container_reboot_supported, stack, stack_size, flags, &cmd); |
7f145a6d DS |
522 | #else |
523 | stack += stack_size; | |
b60ed720 | 524 | pid = clone(container_reboot_supported, stack, flags, &cmd); |
7f145a6d | 525 | #endif |
e2fa1520 SH |
526 | if (pid < 0) { |
527 | SYSERROR("failed to clone\n"); | |
528 | return -1; | |
529 | } | |
530 | if (wait(&status) < 0) { | |
531 | SYSERROR("unexpected wait error: %m\n"); | |
532 | return -1; | |
533 | } | |
534 | ||
535 | if (WEXITSTATUS(status) != 1) | |
828695d9 | 536 | return 1; |
e2fa1520 | 537 | |
828695d9 SH |
538 | return 0; |
539 | } | |
540 | ||
ffe1e01a | 541 | static int do_start(void *data) |
50e98013 | 542 | { |
23c53af9 | 543 | struct lxc_handler *handler = data; |
e0b6898a | 544 | const char *lsm_label = NULL; |
50e98013 DL |
545 | |
546 | if (sigprocmask(SIG_SETMASK, &handler->oldmask, NULL)) { | |
547 | SYSERROR("failed to set sigprocmask"); | |
9d7f9e52 | 548 | return -1; |
50e98013 DL |
549 | } |
550 | ||
743ecd2e DL |
551 | /* This prctl must be before the synchro, so if the parent |
552 | * dies before we set the parent death signal, we will detect | |
553 | * its death with the synchro right after, otherwise we have | |
554 | * a window where the parent can exit before we set the pdeath | |
555 | * signal leading to a unsupervized container. | |
556 | */ | |
557 | if (prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0)) { | |
558 | SYSERROR("failed to set pdeath signal"); | |
559 | return -1; | |
560 | } | |
561 | ||
3c22086f | 562 | lxc_sync_fini_parent(handler); |
50e98013 | 563 | |
2b0e17e4 | 564 | /* don't leak the pinfd to the container */ |
025ed0f3 | 565 | if (handler->pinfd >= 0) { |
0d03360a | 566 | close(handler->pinfd); |
025ed0f3 | 567 | } |
2b0e17e4 | 568 | |
3c22086f CLG |
569 | /* Tell the parent task it can begin to configure the |
570 | * container and wait for it to finish | |
571 | */ | |
572 | if (lxc_sync_barrier_parent(handler, LXC_SYNC_CONFIGURE)) | |
9d7f9e52 | 573 | return -1; |
50e98013 | 574 | |
f6d3e3e4 SH |
575 | /* |
576 | * if we are in a new user namespace, become root there to have | |
577 | * privilege over our namespace | |
578 | */ | |
579 | if (!lxc_list_empty(&handler->conf->id_map)) { | |
580 | NOTICE("switching to gid/uid 0 in new user namespace"); | |
581 | if (setgid(0)) { | |
582 | SYSERROR("setgid"); | |
583 | goto out_warn_father; | |
584 | } | |
585 | if (setuid(0)) { | |
586 | SYSERROR("setuid"); | |
587 | goto out_warn_father; | |
588 | } | |
589 | } | |
590 | ||
495d2046 | 591 | #if HAVE_SYS_CAPABILITY_H |
69182a31 | 592 | if (handler->conf->need_utmp_watch) { |
828695d9 SH |
593 | if (prctl(PR_CAPBSET_DROP, CAP_SYS_BOOT, 0, 0, 0)) { |
594 | SYSERROR("failed to remove CAP_SYS_BOOT capability"); | |
c4ea60df | 595 | goto out_warn_father; |
828695d9 | 596 | } |
e2fa1520 | 597 | DEBUG("Dropped cap_sys_boot\n"); |
e2fa1520 | 598 | } |
495d2046 | 599 | #endif |
e2fa1520 SH |
600 | |
601 | /* Setup the container, ip, names, utsname, ... */ | |
d4ef7c50 | 602 | if (lxc_setup(handler)) { |
e2fa1520 SH |
603 | ERROR("failed to setup the container"); |
604 | goto out_warn_father; | |
605 | } | |
50e98013 | 606 | |
544a48a0 SH |
607 | /* ask father to setup cgroups and wait for him to finish */ |
608 | if (lxc_sync_barrier_parent(handler, LXC_SYNC_CGROUP)) | |
609 | return -1; | |
610 | ||
72863294 | 611 | /* Set the label to change to when we exec(2) the container's init */ |
e0b6898a DE |
612 | if (!strcmp(lsm_name(), "AppArmor")) |
613 | lsm_label = handler->conf->lsm_aa_profile; | |
614 | else if (!strcmp(lsm_name(), "SELinux")) | |
615 | lsm_label = handler->conf->lsm_se_context; | |
616 | if (lsm_process_label_set(lsm_label, 1, 1) < 0) | |
e075f5d9 | 617 | goto out_warn_father; |
fe4de9a6 | 618 | lsm_proc_unmount(handler->conf); |
e075f5d9 | 619 | |
8f2c3a70 SH |
620 | if (lxc_seccomp_load(handler->conf) != 0) |
621 | goto out_warn_father; | |
622 | ||
283678ed | 623 | if (run_lxc_hooks(handler->name, "start", handler->conf, handler->lxcpath, NULL)) { |
773fb9ca SH |
624 | ERROR("failed to run start hooks for container '%s'.", handler->name); |
625 | goto out_warn_father; | |
626 | } | |
fc25b815 | 627 | |
f7bee6c6 MW |
628 | /* The clearenv() and putenv() calls have been moved here |
629 | * to allow us to use enviroment variables passed to the various | |
630 | * hooks, such as the start hook above. Not all of the | |
631 | * variables like CONFIG_PATH or ROOTFS are valid in this | |
632 | * context but others are. */ | |
633 | if (clearenv()) { | |
634 | SYSERROR("failed to clear environment"); | |
635 | /* don't error out though */ | |
636 | } | |
637 | ||
638 | if (putenv("container=lxc")) { | |
639 | SYSERROR("failed to set environment variable"); | |
c4ea60df | 640 | goto out_warn_father; |
f7bee6c6 MW |
641 | } |
642 | ||
773fb9ca | 643 | close(handler->sigfd); |
26ddeedd | 644 | |
e6126dbe MN |
645 | /* after this call, we are in error because this |
646 | * ops should not return as it execs */ | |
c4ea60df | 647 | handler->ops->start(handler, handler->data); |
50e98013 DL |
648 | |
649 | out_warn_father: | |
544a48a0 SH |
650 | /* we want the parent to know something went wrong, so any |
651 | * value other than what it expects is ok. */ | |
3c22086f | 652 | lxc_sync_wake_parent(handler, LXC_SYNC_POST_CONFIGURE); |
50e98013 DL |
653 | return -1; |
654 | } | |
655 | ||
74a3920a | 656 | static int save_phys_nics(struct lxc_conf *conf) |
7b35f3d6 SH |
657 | { |
658 | struct lxc_list *iterator; | |
659 | ||
660 | lxc_list_for_each(iterator, &conf->network) { | |
661 | struct lxc_netdev *netdev = iterator->elem; | |
662 | ||
663 | if (netdev->type != LXC_NET_PHYS) | |
664 | continue; | |
665 | conf->saved_nics = realloc(conf->saved_nics, | |
666 | (conf->num_savednics+1)*sizeof(struct saved_nic)); | |
667 | if (!conf->saved_nics) { | |
668 | SYSERROR("failed to allocate memory"); | |
669 | return -1; | |
670 | } | |
671 | conf->saved_nics[conf->num_savednics].ifindex = netdev->ifindex; | |
672 | conf->saved_nics[conf->num_savednics].orig_name = strdup(netdev->link); | |
673 | if (!conf->saved_nics[conf->num_savednics].orig_name) { | |
674 | SYSERROR("failed to allocate memory"); | |
675 | return -1; | |
676 | } | |
677 | INFO("stored saved_nic #%d idx %d name %s\n", conf->num_savednics, | |
678 | conf->saved_nics[conf->num_savednics].ifindex, | |
679 | conf->saved_nics[conf->num_savednics].orig_name); | |
680 | conf->num_savednics++; | |
681 | } | |
682 | ||
683 | return 0; | |
684 | } | |
685 | ||
74a3920a | 686 | static int lxc_spawn(struct lxc_handler *handler) |
59eb99ba | 687 | { |
b98f7d6e | 688 | int failed_before_rename = 0; |
ffe1e01a | 689 | const char *name = handler->name; |
9f30a190 MM |
690 | int saved_ns_fd[LXC_NS_MAX]; |
691 | int preserve_mask = 0, i; | |
692 | ||
693 | for (i = 0; i < LXC_NS_MAX; i++) | |
6c544cb3 | 694 | if (handler->conf->inherit_ns_fd[i] != -1) |
9f30a190 | 695 | preserve_mask |= ns_info[i].clone_flag; |
50e98013 | 696 | |
3c22086f | 697 | if (lxc_sync_init(handler)) |
9d7f9e52 | 698 | return -1; |
0ad19a3f | 699 | |
6c544cb3 | 700 | handler->clone_flags = CLONE_NEWPID|CLONE_NEWNS; |
f6d3e3e4 SH |
701 | if (!lxc_list_empty(&handler->conf->id_map)) { |
702 | INFO("Cloning a new user namespace"); | |
703 | handler->clone_flags |= CLONE_NEWUSER; | |
704 | } | |
0ad19a3f | 705 | |
9f30a190 | 706 | if (handler->conf->inherit_ns_fd[LXC_NS_NET] == -1) { |
26b797f3 | 707 | if (!lxc_requests_empty_network(handler)) |
9f30a190 MM |
708 | handler->clone_flags |= CLONE_NEWNET; |
709 | ||
26b797f3 SH |
710 | if (!lxc_list_empty(&handler->conf->network)) { |
711 | ||
9f30a190 MM |
712 | /* Find gateway addresses from the link device, which is |
713 | * no longer accessible inside the container. Do this | |
714 | * before creating network interfaces, since goto | |
715 | * out_delete_net does not work before lxc_clone. */ | |
716 | if (lxc_find_gateway_addresses(handler)) { | |
717 | ERROR("failed to find gateway addresses"); | |
718 | lxc_sync_fini(handler); | |
719 | return -1; | |
720 | } | |
721 | ||
722 | /* that should be done before the clone because we will | |
723 | * fill the netdev index and use them in the child | |
724 | */ | |
725 | if (lxc_create_network(handler)) { | |
38521a3b | 726 | ERROR("failed to create the network"); |
9f30a190 MM |
727 | lxc_sync_fini(handler); |
728 | return -1; | |
729 | } | |
19a26f82 MK |
730 | } |
731 | ||
9f30a190 MM |
732 | if (save_phys_nics(handler->conf)) { |
733 | ERROR("failed to save physical nic info"); | |
734 | goto out_abort; | |
82d5ae15 | 735 | } |
9f30a190 MM |
736 | } else { |
737 | INFO("Inheriting a net namespace"); | |
82d5ae15 DL |
738 | } |
739 | ||
3c93577b MM |
740 | if (handler->conf->inherit_ns_fd[LXC_NS_IPC] == -1) { |
741 | handler->clone_flags |= CLONE_NEWIPC; | |
742 | } else { | |
743 | INFO("Inheriting an IPC namespace"); | |
7b35f3d6 SH |
744 | } |
745 | ||
6c544cb3 MM |
746 | if (handler->conf->inherit_ns_fd[LXC_NS_UTS] == -1) { |
747 | handler->clone_flags |= CLONE_NEWUTS; | |
748 | } else { | |
749 | INFO("Inheriting a UTS namespace"); | |
750 | } | |
751 | ||
7b35f3d6 | 752 | |
d4ef7c50 SH |
753 | if (!cgroup_init(handler)) { |
754 | ERROR("failed initializing cgroup support"); | |
33ad9f1a CS |
755 | goto out_delete_net; |
756 | } | |
757 | ||
d4ef7c50 SH |
758 | if (!cgroup_create(handler)) { |
759 | ERROR("failed creating cgroups"); | |
47d8fb3b CS |
760 | goto out_delete_net; |
761 | } | |
762 | ||
0c547523 SH |
763 | /* |
764 | * if the rootfs is not a blockdev, prevent the container from | |
765 | * marking it readonly. | |
766 | */ | |
767 | ||
2b0e17e4 | 768 | handler->pinfd = pin_rootfs(handler->conf->rootfs.path); |
00ec333b SH |
769 | if (handler->pinfd == -1) |
770 | INFO("failed to pin the container's rootfs"); | |
0c547523 | 771 | |
cd43d2d1 SH |
772 | if (preserve_ns(saved_ns_fd, preserve_mask) < 0) |
773 | goto out_delete_net; | |
774 | if (attach_ns(handler->conf->inherit_ns_fd) < 0) | |
775 | goto out_delete_net; | |
9f30a190 | 776 | |
0ad19a3f | 777 | /* Create a process in a new set of namespaces */ |
d5088cf2 | 778 | handler->pid = lxc_clone(do_start, handler, handler->clone_flags); |
59eb99ba | 779 | if (handler->pid < 0) { |
36eb9bde | 780 | SYSERROR("failed to fork into a new namespace"); |
7fef7a06 | 781 | goto out_delete_net; |
0ad19a3f | 782 | } |
783 | ||
cd43d2d1 SH |
784 | if (attach_ns(saved_ns_fd)) |
785 | WARN("failed to restore saved namespaces"); | |
9f30a190 | 786 | |
3c22086f CLG |
787 | lxc_sync_fini_child(handler); |
788 | ||
789 | if (lxc_sync_wait_child(handler, LXC_SYNC_CONFIGURE)) | |
99a6af52 | 790 | failed_before_rename = 1; |
0ad19a3f | 791 | |
d4ef7c50 SH |
792 | if (!cgroup_create_legacy(handler)) { |
793 | ERROR("failed to setup the legacy cgroups for %s", name); | |
ae5c8b8e | 794 | goto out_delete_net; |
33ad9f1a | 795 | } |
d4ef7c50 | 796 | if (!cgroup_setup_without_devices(handler)) { |
6031a6e5 DE |
797 | ERROR("failed to setup the cgroups for '%s'", name); |
798 | goto out_delete_net; | |
799 | } | |
800 | ||
d4ef7c50 | 801 | if (!cgroup_enter(handler)) |
7fef7a06 | 802 | goto out_delete_net; |
218d4250 | 803 | |
99a6af52 MN |
804 | if (failed_before_rename) |
805 | goto out_delete_net; | |
806 | ||
0ad19a3f | 807 | /* Create the network configuration */ |
d5088cf2 | 808 | if (handler->clone_flags & CLONE_NEWNET) { |
fae349da | 809 | if (lxc_assign_network(&handler->conf->network, handler->pid)) { |
82d5ae15 | 810 | ERROR("failed to create the configured network"); |
7fef7a06 | 811 | goto out_delete_net; |
82d5ae15 | 812 | } |
0ad19a3f | 813 | } |
814 | ||
f6d3e3e4 SH |
815 | /* map the container uids - the container became an invalid |
816 | * userid the moment it was cloned with CLONE_NEWUSER - this | |
817 | * call doesn't change anything immediately, but allows the | |
818 | * container to setuid(0) (0 being mapped to something else on | |
819 | * the host) later to become a valid uid again */ | |
820 | if (lxc_map_ids(&handler->conf->id_map, handler->pid)) { | |
821 | ERROR("failed to set up id mapping"); | |
822 | goto out_delete_net; | |
823 | } | |
824 | ||
544a48a0 SH |
825 | /* Tell the child to continue its initialization. we'll get |
826 | * LXC_SYNC_CGROUP when it is ready for us to setup cgroups | |
3c22086f CLG |
827 | */ |
828 | if (lxc_sync_barrier_child(handler, LXC_SYNC_POST_CONFIGURE)) | |
544a48a0 SH |
829 | goto out_delete_net; |
830 | ||
d4ef7c50 | 831 | if (!cgroup_setup_devices(handler)) { |
b98f7d6e SH |
832 | ERROR("failed to setup the devices cgroup for '%s'", name); |
833 | goto out_delete_net; | |
544a48a0 SH |
834 | } |
835 | ||
544a48a0 SH |
836 | /* Tell the child to complete its initialization and wait for |
837 | * it to exec or return an error. (the child will never | |
838 | * return LXC_SYNC_POST_CGROUP+1. It will either close the | |
839 | * sync pipe, causing lxc_sync_barrier_child to return | |
840 | * success, or return a different value, causing us to error | |
841 | * out). | |
842 | */ | |
843 | if (lxc_sync_barrier_child(handler, LXC_SYNC_POST_CGROUP)) | |
3c22086f | 844 | return -1; |
0ad19a3f | 845 | |
cc28d0b0 SH |
846 | if (detect_shared_rootfs()) |
847 | umount2(handler->conf->rootfs.mount, MNT_DETACH); | |
848 | ||
23c53af9 | 849 | if (handler->ops->post_start(handler, handler->data)) |
e6126dbe MN |
850 | goto out_abort; |
851 | ||
25c2aca5 | 852 | if (lxc_set_state(name, handler, RUNNING)) { |
59eb99ba DL |
853 | ERROR("failed to set state to %s", |
854 | lxc_state2str(RUNNING)); | |
855 | goto out_abort; | |
3f21c114 | 856 | } |
22ebac19 | 857 | |
3c22086f | 858 | lxc_sync_fini(handler); |
0c547523 | 859 | |
e6126dbe | 860 | return 0; |
1ac470c0 | 861 | |
7fef7a06 | 862 | out_delete_net: |
d5088cf2 | 863 | if (handler->clone_flags & CLONE_NEWNET) |
74a2b586 | 864 | lxc_delete_network(handler); |
59eb99ba DL |
865 | out_abort: |
866 | lxc_abort(name, handler); | |
3c22086f | 867 | lxc_sync_fini(handler); |
5c068da9 SH |
868 | if (handler->pinfd >= 0) { |
869 | close(handler->pinfd); | |
870 | handler->pinfd = -1; | |
871 | } | |
872 | ||
b79fcd86 | 873 | return -1; |
59eb99ba | 874 | } |
0ad19a3f | 875 | |
ee70bf78 | 876 | int __lxc_start(const char *name, struct lxc_conf *conf, |
13f5be62 | 877 | struct lxc_operations* ops, void *data, const char *lxcpath) |
59eb99ba | 878 | { |
3a0f472d | 879 | struct lxc_handler *handler; |
e043236e | 880 | int err = -1; |
59eb99ba | 881 | int status; |
80090207 | 882 | |
13f5be62 | 883 | handler = lxc_init(name, conf, lxcpath); |
3a0f472d | 884 | if (!handler) { |
59eb99ba | 885 | ERROR("failed to initialize the container"); |
66aeffc7 | 886 | return -1; |
0ad19a3f | 887 | } |
ee70bf78 CLG |
888 | handler->ops = ops; |
889 | handler->data = data; | |
e6126dbe | 890 | |
b60ed720 | 891 | if (must_drop_cap_sys_boot(handler->conf)) { |
495d2046 | 892 | #if HAVE_SYS_CAPABILITY_H |
f51db2b3 | 893 | DEBUG("Dropping cap_sys_boot\n"); |
495d2046 SG |
894 | #else |
895 | DEBUG("Can't drop cap_sys_boot as capabilities aren't supported\n"); | |
896 | #endif | |
69182a31 SH |
897 | } else { |
898 | DEBUG("Not dropping cap_sys_boot or watching utmp\n"); | |
899 | handler->conf->need_utmp_watch = 0; | |
900 | } | |
901 | ||
23c53af9 | 902 | err = lxc_spawn(handler); |
59eb99ba | 903 | if (err) { |
ee70bf78 | 904 | ERROR("failed to spawn '%s'", name); |
74a2b586 | 905 | goto out_fini_nonet; |
0ad19a3f | 906 | } |
907 | ||
3a0f472d | 908 | err = lxc_poll(name, handler); |
e043236e | 909 | if (err) { |
59eb99ba DL |
910 | ERROR("mainloop exited with an error"); |
911 | goto out_abort; | |
912 | } | |
0ad19a3f | 913 | |
3a0f472d | 914 | while (waitpid(handler->pid, &status, 0) < 0 && errno == EINTR) |
1bc5cc8c | 915 | continue; |
e043236e | 916 | |
8b004f07 SH |
917 | /* |
918 | * If the child process exited but was not signaled, | |
919 | * it didn't call reboot. This should mean it was an | |
920 | * lxc-execute which simply exited. In any case, treat | |
921 | * it as a 'halt' | |
922 | */ | |
923 | if (WIFSIGNALED(status)) { | |
924 | switch(WTERMSIG(status)) { | |
925 | case SIGINT: /* halt */ | |
926 | DEBUG("Container halting"); | |
927 | break; | |
928 | case SIGHUP: /* reboot */ | |
929 | DEBUG("Container rebooting"); | |
930 | handler->conf->reboot = 1; | |
931 | break; | |
932 | default: | |
933 | DEBUG("unknown exit status for init: %d\n", WTERMSIG(status)); | |
934 | break; | |
935 | } | |
828695d9 SH |
936 | } |
937 | ||
7b35f3d6 SH |
938 | lxc_rename_phys_nics_on_shutdown(handler->conf); |
939 | ||
5c068da9 SH |
940 | if (handler->pinfd >= 0) { |
941 | close(handler->pinfd); | |
942 | handler->pinfd = -1; | |
943 | } | |
944 | ||
3a0f472d | 945 | err = lxc_error_set_and_log(handler->pid, status); |
9d7f9e52 | 946 | out_fini: |
74a2b586 JK |
947 | lxc_delete_network(handler); |
948 | ||
949 | out_fini_nonet: | |
3a0f472d | 950 | lxc_fini(name, handler); |
0ad19a3f | 951 | return err; |
952 | ||
59eb99ba | 953 | out_abort: |
3a0f472d | 954 | lxc_abort(name, handler); |
9d7f9e52 | 955 | goto out_fini; |
0ad19a3f | 956 | } |
ee70bf78 CLG |
957 | |
958 | struct start_args { | |
959 | char *const *argv; | |
960 | }; | |
961 | ||
962 | static int start(struct lxc_handler *handler, void* data) | |
963 | { | |
964 | struct start_args *arg = data; | |
965 | ||
966 | NOTICE("exec'ing '%s'", arg->argv[0]); | |
967 | ||
968 | execvp(arg->argv[0], arg->argv); | |
969 | SYSERROR("failed to exec %s", arg->argv[0]); | |
970 | return 0; | |
971 | } | |
972 | ||
973 | static int post_start(struct lxc_handler *handler, void* data) | |
974 | { | |
975 | struct start_args *arg = data; | |
976 | ||
977 | NOTICE("'%s' started with pid '%d'", arg->argv[0], handler->pid); | |
978 | return 0; | |
979 | } | |
980 | ||
981 | static struct lxc_operations start_ops = { | |
982 | .start = start, | |
983 | .post_start = post_start | |
984 | }; | |
985 | ||
13f5be62 SH |
986 | int lxc_start(const char *name, char *const argv[], struct lxc_conf *conf, |
987 | const char *lxcpath) | |
ee70bf78 CLG |
988 | { |
989 | struct start_args start_arg = { | |
990 | .argv = argv, | |
991 | }; | |
992 | ||
b119f362 | 993 | if (lxc_check_inherited(conf, -1)) |
ee70bf78 CLG |
994 | return -1; |
995 | ||
828695d9 | 996 | conf->need_utmp_watch = 1; |
13f5be62 | 997 | return __lxc_start(name, conf, &start_ops, &start_arg, lxcpath); |
ee70bf78 | 998 | } |