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