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