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