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