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