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