]>
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 | ||
464 | if (!handler->backgrounded || | |
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 | bool have_cap_setgid; |
1050 | uid_t new_uid; | |
1051 | gid_t new_gid; | |
e96a536c | 1052 | struct lxc_list *iterator; |
5af9369b | 1053 | int devnull_fd = -1; |
e96a536c | 1054 | struct lxc_handler *handler = data; |
50e98013 | 1055 | |
18225d19 | 1056 | lxc_sync_fini_parent(handler); |
50e98013 | 1057 | |
408da065 CB |
1058 | /* This prctl must be before the synchro, so if the parent dies before |
1059 | * we set the parent death signal, we will detect its death with the | |
1060 | * synchro right after, otherwise we have a window where the parent can | |
1061 | * exit before we set the pdeath signal leading to a unsupervized | |
1062 | * container. | |
743ecd2e | 1063 | */ |
18225d19 | 1064 | ret = lxc_set_death_signal(SIGKILL); |
912314fc CB |
1065 | if (ret < 0) { |
1066 | SYSERROR("Failed to set PR_SET_PDEATHSIG to SIGKILL"); | |
18225d19 | 1067 | goto out_warn_father; |
743ecd2e DL |
1068 | } |
1069 | ||
611ddd34 CB |
1070 | ret = lxc_ambient_caps_up(); |
1071 | if (ret < 0) { | |
6f5e532f | 1072 | ERROR("Failed to raise ambient capabilities"); |
611ddd34 CB |
1073 | goto out_warn_father; |
1074 | } | |
1075 | ||
b467714b | 1076 | ret = pthread_sigmask(SIG_SETMASK, &handler->oldmask, NULL); |
18225d19 CB |
1077 | if (ret < 0) { |
1078 | SYSERROR("Failed to set signal mask"); | |
1079 | goto out_warn_father; | |
1080 | } | |
50e98013 | 1081 | |
408da065 | 1082 | /* Don't leak the pinfd to the container. */ |
d39b10eb | 1083 | if (handler->pinfd >= 0) |
0d03360a | 1084 | close(handler->pinfd); |
2b0e17e4 | 1085 | |
18225d19 CB |
1086 | ret = lxc_sync_wait_parent(handler, LXC_SYNC_STARTUP); |
1087 | if (ret < 0) | |
1088 | goto out_warn_father; | |
5b1e83cb | 1089 | |
408da065 CB |
1090 | /* Unshare CLONE_NEWNET after CLONE_NEWUSER. See |
1091 | * https://github.com/lxc/lxd/issues/1978. | |
1092 | */ | |
becad0ec | 1093 | if ((handler->ns_clone_flags & (CLONE_NEWNET | CLONE_NEWUSER)) == |
408da065 | 1094 | (CLONE_NEWNET | CLONE_NEWUSER)) { |
5b1e83cb SH |
1095 | ret = unshare(CLONE_NEWNET); |
1096 | if (ret < 0) { | |
e96a536c | 1097 | SYSERROR("Failed to unshare CLONE_NEWNET"); |
5b1e83cb SH |
1098 | goto out_warn_father; |
1099 | } | |
e96a536c | 1100 | INFO("Unshared CLONE_NEWNET"); |
5b1e83cb SH |
1101 | } |
1102 | ||
408da065 CB |
1103 | /* Tell the parent task it can begin to configure the container and wait |
1104 | * for it to finish. | |
3c22086f | 1105 | */ |
e96a536c CB |
1106 | ret = lxc_sync_barrier_parent(handler, LXC_SYNC_CONFIGURE); |
1107 | if (ret < 0) | |
611ddd34 | 1108 | goto out_error; |
50e98013 | 1109 | |
e96a536c CB |
1110 | ret = lxc_network_recv_veth_names_from_parent(handler); |
1111 | if (ret < 0) { | |
7ab1ba02 | 1112 | ERROR("Failed to receive veth names from parent"); |
658979c5 | 1113 | goto out_warn_father; |
7ab1ba02 | 1114 | } |
658979c5 | 1115 | |
408da065 | 1116 | /* If we are in a new user namespace, become root there to have |
d08f8d2f | 1117 | * privilege over our namespace. |
f6d3e3e4 SH |
1118 | */ |
1119 | if (!lxc_list_empty(&handler->conf->id_map)) { | |
4160c3a0 CB |
1120 | uid_t nsuid = (handler->conf->root_nsuid_map != NULL) |
1121 | ? 0 | |
1122 | : handler->conf->init_uid; | |
1123 | gid_t nsgid = (handler->conf->root_nsgid_map != NULL) | |
1124 | ? 0 | |
1125 | : handler->conf->init_gid; | |
1126 | ||
1127 | ret = lxc_switch_uid_gid(nsuid, nsgid); | |
4b826b1f | 1128 | if (ret < 0) |
f6d3e3e4 | 1129 | goto out_warn_father; |
d08f8d2f CB |
1130 | |
1131 | /* Drop groups only after we switched to a valid gid in the new | |
1132 | * user namespace. | |
1133 | */ | |
4b826b1f | 1134 | ret = lxc_setgroups(0, NULL); |
a19b974f | 1135 | if (ret < 0 && (handler->am_root || errno != EPERM)) |
c476bdce | 1136 | goto out_warn_father; |
4b826b1f | 1137 | |
b81689a1 CB |
1138 | ret = prctl(PR_SET_DUMPABLE, prctl_arg(1), prctl_arg(0), |
1139 | prctl_arg(0), prctl_arg(0)); | |
d7883725 CB |
1140 | if (ret < 0) |
1141 | goto out_warn_father; | |
912314fc CB |
1142 | |
1143 | /* set{g,u}id() clears deathsignal */ | |
18225d19 | 1144 | ret = lxc_set_death_signal(SIGKILL); |
912314fc CB |
1145 | if (ret < 0) { |
1146 | SYSERROR("Failed to set PR_SET_PDEATHSIG to SIGKILL"); | |
1147 | goto out_warn_father; | |
1148 | } | |
f6d3e3e4 SH |
1149 | } |
1150 | ||
e96a536c CB |
1151 | ret = access(handler->lxcpath, X_OK); |
1152 | if (ret != 0) { | |
c8154066 SH |
1153 | print_top_failing_dir(handler->lxcpath); |
1154 | goto out_warn_father; | |
1155 | } | |
1156 | ||
e96a536c CB |
1157 | ret = snprintf(path, sizeof(path), "%s/dev/null", |
1158 | handler->conf->rootfs.mount); | |
408da065 | 1159 | if (ret < 0 || ret >= sizeof(path)) |
7a55c157 | 1160 | goto out_warn_father; |
7a55c157 TA |
1161 | |
1162 | /* In order to checkpoint restore, we need to have everything in the | |
1163 | * same mount namespace. However, some containers may not have a | |
1164 | * reasonable /dev (in particular, they may not have /dev/null), so we | |
1165 | * can't set init's std fds to /dev/null by opening it from inside the | |
1166 | * container. | |
1167 | * | |
1168 | * If that's the case, fall back to using the host's /dev/null. This | |
1169 | * means that migration won't work, but at least we won't spew output | |
1170 | * where it isn't wanted. | |
1171 | */ | |
e96a536c CB |
1172 | if (handler->backgrounded && !handler->conf->autodev) { |
1173 | ret = access(path, F_OK); | |
1174 | if (ret != 0) { | |
1175 | devnull_fd = open_devnull(); | |
c44de748 | 1176 | |
e96a536c CB |
1177 | if (devnull_fd < 0) |
1178 | goto out_warn_father; | |
1179 | WARN("Using /dev/null from the host for container " | |
1180 | "init's standard file descriptors. Migration will " | |
1181 | "not work"); | |
1182 | } | |
c44de748 AM |
1183 | } |
1184 | ||
408da065 | 1185 | /* Ask father to setup cgroups and wait for him to finish. */ |
e96a536c CB |
1186 | ret = lxc_sync_barrier_parent(handler, LXC_SYNC_CGROUP); |
1187 | if (ret < 0) | |
c44de748 | 1188 | goto out_error; |
544a48a0 | 1189 | |
deefdf8a CB |
1190 | /* Unshare cgroup namespace after we have setup our cgroups. If we do it |
1191 | * earlier we end up with a wrong view of /proc/self/cgroup. For | |
1192 | * example, assume we unshare(CLONE_NEWCGROUP) first, and then create | |
1193 | * the cgroup for the container, say /sys/fs/cgroup/cpuset/lxc/c, then | |
1194 | * /proc/self/cgroup would show us: | |
1195 | * | |
1196 | * 8:cpuset:/lxc/c | |
1197 | * | |
1198 | * whereas it should actually show | |
1199 | * | |
1200 | * 8:cpuset:/ | |
1201 | */ | |
becad0ec | 1202 | if (handler->ns_clone_flags & CLONE_NEWCGROUP) { |
e96a536c CB |
1203 | ret = unshare(CLONE_NEWCGROUP); |
1204 | if (ret < 0) { | |
1205 | INFO("Failed to unshare CLONE_NEWCGROUP"); | |
deefdf8a CB |
1206 | goto out_warn_father; |
1207 | } | |
e96a536c | 1208 | INFO("Unshared CLONE_NEWCGROUP"); |
deefdf8a CB |
1209 | } |
1210 | ||
149857af CB |
1211 | /* Add the requested environment variables to the current environment to |
1212 | * allow them to be used by the various hooks, such as the start hook | |
1213 | * above. | |
98ff08ca | 1214 | */ |
98ff08ca | 1215 | lxc_list_for_each(iterator, &handler->conf->environment) { |
e96a536c CB |
1216 | ret = putenv((char *)iterator->elem); |
1217 | if (ret < 0) { | |
1218 | SYSERROR("Failed to set environment variable: %s", | |
1219 | (char *)iterator->elem); | |
98ff08ca CB |
1220 | goto out_warn_father; |
1221 | } | |
1222 | } | |
1223 | ||
f4152036 | 1224 | /* Setup the container, ip, names, utsname, ... */ |
3b988b33 | 1225 | ret = lxc_setup(handler); |
7729f8e5 | 1226 | close(handler->data_sock[1]); |
8bf3abfb | 1227 | close(handler->data_sock[0]); |
7729f8e5 | 1228 | if (ret < 0) { |
e96a536c | 1229 | ERROR("Failed to setup container \"%s\"", handler->name); |
f4152036 CB |
1230 | goto out_warn_father; |
1231 | } | |
1232 | ||
408da065 | 1233 | /* Set the label to change to when we exec(2) the container's init. */ |
1800f924 | 1234 | ret = lsm_process_label_set(NULL, handler->conf, true); |
e96a536c | 1235 | if (ret < 0) |
e075f5d9 | 1236 | goto out_warn_father; |
5112cd70 | 1237 | |
029cdff5 | 1238 | /* Set PR_SET_NO_NEW_PRIVS after we changed the lsm label. If we do it |
408da065 CB |
1239 | * before we aren't allowed anymore. |
1240 | */ | |
029cdff5 | 1241 | if (handler->conf->no_new_privs) { |
b81689a1 CB |
1242 | ret = prctl(PR_SET_NO_NEW_PRIVS, prctl_arg(1), prctl_arg(0), |
1243 | prctl_arg(0), prctl_arg(0)); | |
e96a536c CB |
1244 | if (ret < 0) { |
1245 | SYSERROR("Could not set PR_SET_NO_NEW_PRIVS to block " | |
1246 | "execve() gainable privileges"); | |
029cdff5 CB |
1247 | goto out_warn_father; |
1248 | } | |
e96a536c CB |
1249 | DEBUG("Set PR_SET_NO_NEW_PRIVS to block execve() gainable " |
1250 | "privileges"); | |
029cdff5 CB |
1251 | } |
1252 | ||
0d9acb99 DE |
1253 | /* Some init's such as busybox will set sane tty settings on stdin, |
1254 | * stdout, stderr which it thinks is the console. We already set them | |
1255 | * the way we wanted on the real terminal, and we want init to do its | |
564e31c4 | 1256 | * setup on its console ie. the pty allocated in lxc_terminal_setup() so |
408da065 | 1257 | * make sure that that pty is stdin,stdout,stderr. |
0d9acb99 | 1258 | */ |
5d113f65 | 1259 | if (handler->conf->console.slave >= 0) { |
07c4ea31 | 1260 | if (handler->backgrounded || !handler->conf->is_execute) |
5d113f65 CB |
1261 | ret = set_stdfds(handler->conf->console.slave); |
1262 | else | |
ae6d3913 | 1263 | ret = lxc_terminal_set_stdfds(handler->conf->console.slave); |
5d113f65 | 1264 | if (ret < 0) { |
c5b93afb | 1265 | ERROR("Failed to redirect std{in,out,err} to pty file " |
e96a536c | 1266 | "descriptor %d", handler->conf->console.slave); |
c5b93afb LF |
1267 | goto out_warn_father; |
1268 | } | |
5d113f65 | 1269 | } |
0d9acb99 | 1270 | |
408da065 | 1271 | /* If we mounted a temporary proc, then unmount it now. */ |
5112cd70 | 1272 | tmp_proc_unmount(handler->conf); |
e075f5d9 | 1273 | |
e96a536c CB |
1274 | ret = lxc_seccomp_load(handler->conf); |
1275 | if (ret < 0) | |
8f2c3a70 SH |
1276 | goto out_warn_father; |
1277 | ||
e96a536c CB |
1278 | ret = run_lxc_hooks(handler->name, "start", handler->conf, NULL); |
1279 | if (ret < 0) { | |
1280 | ERROR("Failed to run lxc.hook.start for container \"%s\"", | |
1281 | handler->name); | |
773fb9ca SH |
1282 | goto out_warn_father; |
1283 | } | |
fc25b815 | 1284 | |
773fb9ca | 1285 | close(handler->sigfd); |
26ddeedd | 1286 | |
e96a536c | 1287 | if (handler->conf->console.slave < 0 && handler->backgrounded) { |
f4c177c3 CB |
1288 | if (devnull_fd < 0) { |
1289 | devnull_fd = open_devnull(); | |
1290 | if (devnull_fd < 0) | |
1291 | goto out_warn_father; | |
1292 | } | |
1293 | ||
e96a536c CB |
1294 | ret = set_stdfds(devnull_fd); |
1295 | if (ret < 0) { | |
1296 | ERROR("Failed to redirect std{in,out,err} to \"/dev/null\""); | |
c5b93afb LF |
1297 | goto out_warn_father; |
1298 | } | |
e96a536c | 1299 | } |
507cee36 | 1300 | |
c44de748 AM |
1301 | if (devnull_fd >= 0) { |
1302 | close(devnull_fd); | |
1303 | devnull_fd = -1; | |
1304 | } | |
1305 | ||
8c9a7665 TA |
1306 | setsid(); |
1307 | ||
e96a536c CB |
1308 | if (handler->conf->init_cwd) { |
1309 | ret = chdir(handler->conf->init_cwd); | |
1310 | if (ret < 0) { | |
1311 | SYSERROR("Could not change directory to \"%s\"", | |
1312 | handler->conf->init_cwd); | |
1313 | goto out_warn_father; | |
1314 | } | |
3c491553 L |
1315 | } |
1316 | ||
e96a536c CB |
1317 | ret = lxc_sync_barrier_parent(handler, LXC_SYNC_CGROUP_LIMITS); |
1318 | if (ret < 0) | |
f4152036 CB |
1319 | goto out_warn_father; |
1320 | ||
149857af CB |
1321 | /* Reset the environment variables the user requested in a clear |
1322 | * environment. | |
1323 | */ | |
e96a536c CB |
1324 | ret = clearenv(); |
1325 | /* Don't error out though. */ | |
1326 | if (ret < 0) | |
149857af | 1327 | SYSERROR("Failed to clear environment."); |
149857af CB |
1328 | |
1329 | lxc_list_for_each(iterator, &handler->conf->environment) { | |
e96a536c CB |
1330 | ret = putenv((char *)iterator->elem); |
1331 | if (ret < 0) { | |
1332 | SYSERROR("Failed to set environment variable: %s", | |
1333 | (char *)iterator->elem); | |
149857af CB |
1334 | goto out_warn_father; |
1335 | } | |
1336 | } | |
1337 | ||
e96a536c CB |
1338 | ret = putenv("container=lxc"); |
1339 | if (ret < 0) { | |
1340 | SYSERROR("Failed to set environment variable: container=lxc"); | |
98ff08ca CB |
1341 | goto out_warn_father; |
1342 | } | |
1343 | ||
885766f5 CB |
1344 | if (handler->conf->ttys.tty_names) { |
1345 | ret = putenv(handler->conf->ttys.tty_names); | |
e96a536c CB |
1346 | if (ret < 0) { |
1347 | SYSERROR("Failed to set environment variable for container ptys"); | |
98ff08ca CB |
1348 | goto out_warn_father; |
1349 | } | |
1350 | } | |
1351 | ||
76bdf299 CB |
1352 | /* The container has been setup. We can now switch to an unprivileged |
1353 | * uid/gid. | |
1354 | */ | |
928b1f04 YT |
1355 | new_uid = handler->conf->init_uid; |
1356 | new_gid = handler->conf->init_gid; | |
76bdf299 | 1357 | |
e96a536c CB |
1358 | /* If we are in a new user namespace we already dropped all groups when |
1359 | * we switched to root in the new user namespace further above. Only | |
1360 | * drop groups if we can, so ensure that we have necessary privilege. | |
928b1f04 YT |
1361 | */ |
1362 | #if HAVE_LIBCAP | |
1363 | have_cap_setgid = lxc_proc_cap_is_set(CAP_SETGID, CAP_EFFECTIVE); | |
1364 | #else | |
1365 | have_cap_setgid = false; | |
1366 | #endif | |
1367 | if (lxc_list_empty(&handler->conf->id_map) && have_cap_setgid) { | |
e96a536c CB |
1368 | ret = lxc_setgroups(0, NULL); |
1369 | if (ret < 0) | |
76bdf299 CB |
1370 | goto out_warn_father; |
1371 | } | |
1372 | ||
e96a536c CB |
1373 | ret = lxc_switch_uid_gid(new_uid, new_gid); |
1374 | if (ret < 0) | |
928b1f04 YT |
1375 | goto out_warn_father; |
1376 | ||
611ddd34 CB |
1377 | ret = lxc_ambient_caps_down(); |
1378 | if (ret < 0) { | |
6f5e532f | 1379 | ERROR("Failed to clear ambient capabilities"); |
611ddd34 CB |
1380 | goto out_warn_father; |
1381 | } | |
1382 | ||
258f8051 CB |
1383 | if (handler->conf->monitor_signal_pdeath != SIGKILL) { |
1384 | ret = lxc_set_death_signal(handler->conf->monitor_signal_pdeath); | |
1385 | if (ret < 0) { | |
1386 | SYSERROR("Failed to set PR_SET_PDEATHSIG to %d", | |
1387 | handler->conf->monitor_signal_pdeath); | |
1388 | goto out_warn_father; | |
1389 | } | |
1390 | } | |
1391 | ||
408da065 CB |
1392 | /* After this call, we are in error because this ops should not return |
1393 | * as it execs. | |
1394 | */ | |
c4ea60df | 1395 | handler->ops->start(handler, handler->data); |
50e98013 DL |
1396 | |
1397 | out_warn_father: | |
408da065 CB |
1398 | /* We want the parent to know something went wrong, so we return a |
1399 | * special error code. | |
1400 | */ | |
d1ccb562 | 1401 | lxc_sync_wake_parent(handler, LXC_SYNC_ERROR); |
c44de748 AM |
1402 | |
1403 | out_error: | |
1404 | if (devnull_fd >= 0) | |
1405 | close(devnull_fd); | |
1406 | ||
50e98013 DL |
1407 | return -1; |
1408 | } | |
1409 | ||
ae467c54 | 1410 | static int lxc_recv_ttys_from_child(struct lxc_handler *handler) |
e8bd4e43 | 1411 | { |
ae467c54 | 1412 | int i; |
2520facd | 1413 | struct lxc_terminal_info *tty; |
ae467c54 | 1414 | int ret = -1; |
c6012571 | 1415 | int sock = handler->data_sock[1]; |
e8bd4e43 | 1416 | struct lxc_conf *conf = handler->conf; |
0e4be3cf | 1417 | struct lxc_tty_info *ttys = &conf->ttys; |
e8bd4e43 | 1418 | |
885766f5 | 1419 | if (!conf->ttys.max) |
e8bd4e43 SH |
1420 | return 0; |
1421 | ||
885766f5 | 1422 | ttys->tty = malloc(sizeof(*ttys->tty) * ttys->max); |
0e4be3cf | 1423 | if (!ttys->tty) |
e8bd4e43 | 1424 | return -1; |
e8bd4e43 | 1425 | |
885766f5 | 1426 | for (i = 0; i < conf->ttys.max; i++) { |
672c1e58 CB |
1427 | int ttyfds[2]; |
1428 | ||
1429 | ret = lxc_abstract_unix_recv_fds(sock, ttyfds, 2, NULL, 0); | |
1430 | if (ret < 0) | |
1431 | break; | |
ae467c54 | 1432 | |
0e4be3cf | 1433 | tty = &ttys->tty[i]; |
2520facd CB |
1434 | tty->busy = 0; |
1435 | tty->master = ttyfds[0]; | |
1436 | tty->slave = ttyfds[1]; | |
672c1e58 | 1437 | TRACE("Received pty with master fd %d and slave fd %d from " |
2520facd | 1438 | "parent", tty->master, tty->slave); |
e8bd4e43 | 1439 | } |
6d1400b5 | 1440 | |
ae467c54 | 1441 | if (ret < 0) |
6d1400b5 | 1442 | SYSERROR("Failed to receive %zu ttys from child", ttys->max); |
ae467c54 | 1443 | else |
885766f5 | 1444 | TRACE("Received %zu ttys from child", ttys->max); |
ae467c54 CB |
1445 | |
1446 | return ret; | |
e8bd4e43 SH |
1447 | } |
1448 | ||
5af9369b | 1449 | int resolve_clone_flags(struct lxc_handler *handler) |
f813849c | 1450 | { |
8bc8c715 CB |
1451 | int i; |
1452 | struct lxc_conf *conf = handler->conf; | |
f813849c | 1453 | |
8bc8c715 CB |
1454 | for (i = 0; i < LXC_NS_MAX; i++) { |
1455 | if (conf->ns_keep != 0) { | |
1456 | if ((conf->ns_keep & ns_info[i].clone_flag) == 0) | |
becad0ec | 1457 | handler->ns_clone_flags |= ns_info[i].clone_flag; |
8bc8c715 CB |
1458 | } else if (conf->ns_clone != 0) { |
1459 | if ((conf->ns_clone & ns_info[i].clone_flag) > 0) | |
becad0ec | 1460 | handler->ns_clone_flags |= ns_info[i].clone_flag; |
8bc8c715 CB |
1461 | } else { |
1462 | if (i == LXC_NS_USER && lxc_list_empty(&handler->conf->id_map)) | |
1463 | continue; | |
1464 | ||
1465 | if (i == LXC_NS_NET && lxc_requests_empty_network(handler)) | |
1466 | continue; | |
1467 | ||
7bd05339 CB |
1468 | if (i == LXC_NS_CGROUP && !cgns_supported()) |
1469 | continue; | |
1470 | ||
becad0ec | 1471 | handler->ns_clone_flags |= ns_info[i].clone_flag; |
8bc8c715 | 1472 | } |
03df7ab5 | 1473 | |
8bc8c715 CB |
1474 | if (!conf->ns_share[i]) |
1475 | continue; | |
5af9369b | 1476 | |
becad0ec | 1477 | handler->ns_clone_flags &= ~ns_info[i].clone_flag; |
8bc8c715 | 1478 | TRACE("Sharing %s namespace", ns_info[i].proc_name); |
5af9369b CB |
1479 | } |
1480 | ||
1481 | return 0; | |
f813849c TA |
1482 | } |
1483 | ||
8deca6c9 CB |
1484 | /* Note that this function is used with clone(CLONE_VM). Some glibc versions |
1485 | * used to reset the pid/tid to -1 when CLONE_VM was used without CLONE_THREAD. | |
1486 | * But since the memory between parent and child is shared on CLONE_VM this | |
1487 | * would invalidate the getpid() cache that glibc used to maintain and so | |
1488 | * getpid() in the child would return the parent's pid. This is all fixed in | |
1489 | * newer glibc versions where the getpid() cache is removed and the pid/tid is | |
1490 | * not reset anymore. | |
1491 | * However, if for whatever reason you - dear commiter - somehow need to get the | |
1492 | * pid of the dummy intermediate process for do_share_ns() you need to call | |
0059379f CB |
1493 | * lxc_raw_getpid(). The next lxc_raw_clone() call does not employ CLONE_VM and |
1494 | * will be fine. | |
8deca6c9 CB |
1495 | */ |
1496 | static inline int do_share_ns(void *arg) | |
fa3a5b22 | 1497 | { |
8deca6c9 | 1498 | int i, flags, ret; |
fa3a5b22 CB |
1499 | struct lxc_handler *handler = arg; |
1500 | ||
8deca6c9 CB |
1501 | for (i = 0; i < LXC_NS_MAX; i++) { |
1502 | if (handler->nsfd[i] < 0) | |
1503 | continue; | |
fa3a5b22 | 1504 | |
8deca6c9 | 1505 | ret = setns(handler->nsfd[i], 0); |
c0b48eff CB |
1506 | if (ret < 0) { |
1507 | /* | |
1508 | * Note that joining a user and/or mount namespace | |
1509 | * requires the process is not multithreaded otherwise | |
1510 | * setns() will fail here. | |
1511 | */ | |
1512 | SYSERROR("Failed to inherit %s namespace", | |
1513 | ns_info[i].proc_name); | |
8deca6c9 | 1514 | return -1; |
c0b48eff | 1515 | } |
fa3a5b22 | 1516 | |
8deca6c9 | 1517 | DEBUG("Inherited %s namespace", ns_info[i].proc_name); |
fa3a5b22 CB |
1518 | } |
1519 | ||
becad0ec | 1520 | flags = handler->ns_on_clone_flags; |
8deca6c9 | 1521 | flags |= CLONE_PARENT; |
ac2ba696 | 1522 | handler->pid = lxc_raw_clone_cb(do_start, handler, flags); |
8deca6c9 | 1523 | if (handler->pid < 0) |
fa3a5b22 CB |
1524 | return -1; |
1525 | ||
8deca6c9 | 1526 | return 0; |
fa3a5b22 CB |
1527 | } |
1528 | ||
ea0e06dd CB |
1529 | static int lxc_setup_shmount(struct lxc_conf *conf) |
1530 | { | |
4d6cc24c LT |
1531 | size_t len_cont; |
1532 | char *full_cont_path; | |
1533 | int ret = -1; | |
1534 | ||
ea0e06dd | 1535 | /* Construct the shmount path under the container root. */ |
7a41e857 | 1536 | len_cont = strlen(conf->rootfs.mount) + 1 + strlen(conf->shmount.path_cont); |
4d6cc24c LT |
1537 | /* +1 for the terminating '\0' */ |
1538 | full_cont_path = malloc(len_cont + 1); | |
ea0e06dd | 1539 | if (!full_cont_path) { |
4d6cc24c LT |
1540 | SYSERROR("Not enough memory"); |
1541 | return -ENOMEM; | |
1542 | } | |
ea0e06dd CB |
1543 | |
1544 | ret = snprintf(full_cont_path, len_cont + 1, "%s/%s", | |
1545 | conf->rootfs.mount, conf->shmount.path_cont); | |
4d6cc24c LT |
1546 | if (ret < 0 || ret >= len_cont + 1) { |
1547 | SYSERROR("Failed to create filename"); | |
1548 | free(full_cont_path); | |
1549 | return -1; | |
1550 | } | |
1551 | ||
ea0e06dd | 1552 | /* Check if shmount point is already set up. */ |
7a41e857 | 1553 | if (is_shared_mountpoint(conf->shmount.path_host)) { |
ea0e06dd CB |
1554 | INFO("Path \"%s\" is already MS_SHARED. Reusing", |
1555 | conf->shmount.path_host); | |
4d6cc24c LT |
1556 | free(full_cont_path); |
1557 | return 0; | |
1558 | } | |
1559 | ||
1560 | /* Create host and cont mount paths */ | |
7a41e857 | 1561 | ret = mkdir_p(conf->shmount.path_host, 0711); |
4d6cc24c | 1562 | if (ret < 0 && errno != EEXIST) { |
ea0e06dd CB |
1563 | SYSERROR("Failed to create directory \"%s\"", |
1564 | conf->shmount.path_host); | |
4d6cc24c LT |
1565 | free(full_cont_path); |
1566 | return ret; | |
1567 | } | |
1568 | ||
1569 | ret = mkdir_p(full_cont_path, 0711); | |
1570 | if (ret < 0 && errno != EEXIST) { | |
1571 | SYSERROR("Failed to create directory \"%s\"", full_cont_path); | |
1572 | free(full_cont_path); | |
1573 | return ret; | |
1574 | } | |
1575 | ||
1576 | /* Prepare host mountpoint */ | |
ea0e06dd CB |
1577 | ret = mount("tmpfs", conf->shmount.path_host, "tmpfs", 0, |
1578 | "size=100k,mode=0711"); | |
4d6cc24c | 1579 | if (ret < 0) { |
7a41e857 | 1580 | SYSERROR("Failed to mount \"%s\"", conf->shmount.path_host); |
4d6cc24c LT |
1581 | free(full_cont_path); |
1582 | return ret; | |
1583 | } | |
ea0e06dd | 1584 | |
7a41e857 | 1585 | ret = mount(conf->shmount.path_host, conf->shmount.path_host, "none", |
ea0e06dd | 1586 | MS_REC | MS_SHARED, ""); |
4d6cc24c | 1587 | if (ret < 0) { |
7a41e857 | 1588 | SYSERROR("Failed to make shared \"%s\"", conf->shmount.path_host); |
4d6cc24c LT |
1589 | free(full_cont_path); |
1590 | return ret; | |
1591 | } | |
1592 | ||
ea0e06dd | 1593 | INFO("Setup shared mount point \"%s\"", conf->shmount.path_host); |
4d6cc24c LT |
1594 | free(full_cont_path); |
1595 | return 0; | |
1596 | } | |
1597 | ||
480588e6 CB |
1598 | /* lxc_spawn() performs crucial setup tasks and clone()s the new process which |
1599 | * exec()s the requested container binary. | |
1600 | * Note that lxc_spawn() runs in the parent namespaces. Any operations performed | |
1601 | * right here should be double checked if they'd pose a security risk. (For | |
1602 | * example, any {u}mount() operations performed here will be reflected on the | |
1603 | * host!) | |
1604 | */ | |
74a3920a | 1605 | static int lxc_spawn(struct lxc_handler *handler) |
59eb99ba | 1606 | { |
8deca6c9 | 1607 | int i, ret; |
08dd2805 | 1608 | char pidstr[20]; |
57927bf2 CB |
1609 | bool wants_to_map_ids; |
1610 | struct lxc_list *id_map; | |
6e5fc7a5 | 1611 | const char *name = handler->name; |
28d9e29e | 1612 | const char *lxcpath = handler->lxcpath; |
2202afc9 | 1613 | bool share_ns = false; |
28d9e29e | 1614 | struct lxc_conf *conf = handler->conf; |
2202afc9 | 1615 | struct cgroup_ops *cgroup_ops = handler->cgroup_ops; |
9f30a190 | 1616 | |
28d9e29e | 1617 | id_map = &conf->id_map; |
57927bf2 | 1618 | wants_to_map_ids = !lxc_list_empty(id_map); |
2f2623ec | 1619 | |
28d9e29e | 1620 | for (i = 0; i < LXC_NS_MAX; i++) { |
b074bbf1 | 1621 | if (!conf->ns_share[i]) |
28d9e29e CB |
1622 | continue; |
1623 | ||
b074bbf1 | 1624 | handler->nsfd[i] = lxc_inherit_namespace(conf->ns_share[i], lxcpath, ns_info[i].proc_name); |
28d9e29e CB |
1625 | if (handler->nsfd[i] < 0) |
1626 | return -1; | |
fa3a5b22 | 1627 | |
8deca6c9 | 1628 | share_ns = true; |
28d9e29e | 1629 | } |
50e98013 | 1630 | |
cfc62c60 CB |
1631 | ret = lxc_sync_init(handler); |
1632 | if (ret < 0) | |
9d7f9e52 | 1633 | return -1; |
0ad19a3f | 1634 | |
b9f522c5 | 1635 | ret = socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, |
790255cf | 1636 | handler->data_sock); |
95ef0d7c CB |
1637 | if (ret < 0) |
1638 | goto out_sync_fini; | |
e8bd4e43 | 1639 | |
5af9369b | 1640 | ret = resolve_clone_flags(handler); |
95ef0d7c CB |
1641 | if (ret < 0) |
1642 | goto out_sync_fini; | |
9f30a190 | 1643 | |
78852a0c | 1644 | if (conf->shmount.path_host) { |
95ef0d7c CB |
1645 | if (!conf->shmount.path_cont) |
1646 | goto out_sync_fini; | |
78852a0c JS |
1647 | |
1648 | ret = lxc_setup_shmount(conf); | |
1649 | if (ret < 0) { | |
1650 | ERROR("Failed to setup shared mount point"); | |
95ef0d7c | 1651 | goto out_sync_fini; |
78852a0c JS |
1652 | } |
1653 | } | |
1654 | ||
becad0ec | 1655 | if (handler->ns_clone_flags & CLONE_NEWNET) { |
28d9e29e | 1656 | if (!lxc_list_empty(&conf->network)) { |
26b797f3 | 1657 | |
9f30a190 MM |
1658 | /* Find gateway addresses from the link device, which is |
1659 | * no longer accessible inside the container. Do this | |
1660 | * before creating network interfaces, since goto | |
408da065 CB |
1661 | * out_delete_net does not work before lxc_clone. |
1662 | */ | |
cfc62c60 CB |
1663 | ret = lxc_find_gateway_addresses(handler); |
1664 | if (ret < 0) { | |
1665 | ERROR("Failed to find gateway addresses"); | |
95ef0d7c | 1666 | goto out_sync_fini; |
9f30a190 MM |
1667 | } |
1668 | ||
408da065 CB |
1669 | /* That should be done before the clone because we will |
1670 | * fill the netdev index and use them in the child. | |
9f30a190 | 1671 | */ |
cfc62c60 CB |
1672 | ret = lxc_create_network_priv(handler); |
1673 | if (ret < 0) { | |
1674 | ERROR("Failed to create the network"); | |
78852a0c | 1675 | goto out_delete_net; |
9f30a190 | 1676 | } |
19a26f82 | 1677 | } |
82d5ae15 DL |
1678 | } |
1679 | ||
2202afc9 | 1680 | if (!cgroup_ops->create(cgroup_ops, handler)) { |
cfc62c60 | 1681 | ERROR("Failed creating cgroups"); |
47d8fb3b CS |
1682 | goto out_delete_net; |
1683 | } | |
1684 | ||
408da065 CB |
1685 | /* If the rootfs is not a blockdev, prevent the container from marking |
1686 | * it readonly. | |
1687 | * If the container is unprivileged then skip rootfs pinning. | |
0c547523 | 1688 | */ |
0ee35059 | 1689 | if (!wants_to_map_ids) { |
28d9e29e | 1690 | handler->pinfd = pin_rootfs(conf->rootfs.path); |
5e32a990 | 1691 | if (handler->pinfd == -1) |
cfc62c60 | 1692 | INFO("Failed to pin the rootfs for container \"%s\"", handler->name); |
5e32a990 | 1693 | } |
0c547523 | 1694 | |
408da065 | 1695 | /* Create a process in a new set of namespaces. */ |
becad0ec CB |
1696 | handler->ns_on_clone_flags = handler->ns_clone_flags; |
1697 | if (handler->ns_clone_flags & CLONE_NEWUSER) { | |
408da065 CB |
1698 | /* If CLONE_NEWUSER and CLONE_NEWNET was requested, we need to |
1699 | * clone a new user namespace first and only later unshare our | |
1700 | * network namespace to ensure that network devices ownership is | |
1701 | * set up correctly. | |
1702 | */ | |
becad0ec | 1703 | handler->ns_on_clone_flags &= ~CLONE_NEWNET; |
408da065 | 1704 | } |
5af9369b | 1705 | /* The cgroup namespace gets unshare()ed not clone()ed. */ |
becad0ec | 1706 | handler->ns_on_clone_flags &= ~CLONE_NEWCGROUP; |
732375f5 | 1707 | |
4e232466 CB |
1708 | if (share_ns) { |
1709 | pid_t attacher_pid; | |
1710 | ||
cfc62c60 CB |
1711 | attacher_pid = lxc_clone(do_share_ns, handler, |
1712 | CLONE_VFORK | CLONE_VM | CLONE_FILES); | |
4e232466 CB |
1713 | if (attacher_pid < 0) { |
1714 | SYSERROR(LXC_CLONE_ERROR); | |
1715 | goto out_delete_net; | |
1716 | } | |
1717 | ||
1718 | ret = wait_for_pid(attacher_pid); | |
1719 | if (ret < 0) { | |
1720 | SYSERROR("Intermediate process failed"); | |
1721 | goto out_delete_net; | |
1722 | } | |
1723 | } else { | |
cfc62c60 | 1724 | handler->pid = lxc_raw_clone_cb(do_start, handler, |
becad0ec | 1725 | handler->ns_on_clone_flags); |
4e232466 CB |
1726 | } |
1727 | if (handler->pid < 0) { | |
1728 | SYSERROR(LXC_CLONE_ERROR); | |
7fef7a06 | 1729 | goto out_delete_net; |
0ad19a3f | 1730 | } |
fa3a5b22 | 1731 | TRACE("Cloned child process %d", handler->pid); |
1bd8d726 | 1732 | |
9662e444 | 1733 | for (i = 0; i < LXC_NS_MAX; i++) |
becad0ec | 1734 | if (handler->ns_on_clone_flags & ns_info[i].clone_flag) |
28d9e29e | 1735 | INFO("Cloned %s", ns_info[i].flag_name); |
0ad19a3f | 1736 | |
4cb53844 | 1737 | if (!lxc_try_preserve_namespaces(handler, handler->ns_on_clone_flags, handler->pid)) { |
28d9e29e CB |
1738 | ERROR("Failed to preserve cloned namespaces for lxc.hook.stop"); |
1739 | goto out_delete_net; | |
1740 | } | |
b6b2b194 | 1741 | |
3c22086f CLG |
1742 | lxc_sync_fini_child(handler); |
1743 | ||
408da065 CB |
1744 | /* Map the container uids. The container became an invalid userid the |
1745 | * moment it was cloned with CLONE_NEWUSER. This call doesn't change | |
1746 | * anything immediately, but allows the container to setuid(0) (0 being | |
1747 | * mapped to something else on the host.) later to become a valid uid | |
1748 | * again. | |
1749 | */ | |
fa3a5b22 | 1750 | if (wants_to_map_ids) { |
df3c5314 | 1751 | if (!handler->conf->ns_share[LXC_NS_USER] && |
46186acd | 1752 | (handler->conf->ns_keep & CLONE_NEWUSER) == 0) { |
fa3a5b22 CB |
1753 | ret = lxc_map_ids(id_map, handler->pid); |
1754 | if (ret < 0) { | |
1755 | ERROR("Failed to set up id mapping."); | |
1756 | goto out_delete_net; | |
1757 | } | |
1758 | } | |
5b1e83cb SH |
1759 | } |
1760 | ||
cfc62c60 CB |
1761 | ret = lxc_sync_wake_child(handler, LXC_SYNC_STARTUP); |
1762 | if (ret < 0) | |
5b1e83cb | 1763 | goto out_delete_net; |
5b1e83cb | 1764 | |
cfc62c60 CB |
1765 | ret = lxc_sync_wait_child(handler, LXC_SYNC_CONFIGURE); |
1766 | if (ret < 0) | |
5b1e83cb | 1767 | goto out_delete_net; |
0ad19a3f | 1768 | |
2202afc9 | 1769 | if (!cgroup_ops->setup_limits(cgroup_ops, handler->conf, false)) { |
cfc62c60 | 1770 | ERROR("Failed to setup cgroup limits for container \"%s\"", name); |
6031a6e5 DE |
1771 | goto out_delete_net; |
1772 | } | |
1773 | ||
2202afc9 | 1774 | if (!cgroup_ops->enter(cgroup_ops, handler->pid)) |
7fef7a06 | 1775 | goto out_delete_net; |
218d4250 | 1776 | |
2202afc9 | 1777 | if (!cgroup_ops->chown(cgroup_ops, handler->conf)) |
0996e18a SH |
1778 | goto out_delete_net; |
1779 | ||
fa3a5b22 | 1780 | /* Now we're ready to preserve the network namespace */ |
4cb53844 | 1781 | ret = lxc_try_preserve_ns(handler->pid, "net"); |
fa3a5b22 | 1782 | if (ret < 0) { |
4cb53844 | 1783 | if (ret != -EOPNOTSUPP) { |
6d1400b5 | 1784 | SYSERROR("Failed to preserve net namespace"); |
4cb53844 CB |
1785 | goto out_delete_net; |
1786 | } | |
1787 | } else { | |
1788 | handler->nsfd[LXC_NS_NET] = ret; | |
1789 | DEBUG("Preserved net namespace via fd %d", ret); | |
1cd95214 CB |
1790 | |
1791 | ret = lxc_netns_set_nsid(handler->nsfd[LXC_NS_NET]); | |
bfcedc7e | 1792 | if (ret < 0) |
b7986d0d | 1793 | SYSERROR("Failed to allocate new network namespace id"); |
bfcedc7e | 1794 | else |
b7986d0d | 1795 | TRACE("Allocated new network namespace id"); |
fa3a5b22 | 1796 | } |
fa3a5b22 | 1797 | |
408da065 | 1798 | /* Create the network configuration. */ |
becad0ec | 1799 | if (handler->ns_clone_flags & CLONE_NEWNET) { |
cfc62c60 CB |
1800 | ret = lxc_network_move_created_netdev_priv(handler->lxcpath, |
1801 | handler->name, | |
1802 | &conf->network, | |
1803 | handler->pid); | |
1804 | if (ret < 0) { | |
1805 | ERROR("Failed to create the configured network"); | |
7fef7a06 | 1806 | goto out_delete_net; |
82d5ae15 | 1807 | } |
74c6e2b0 | 1808 | |
cfc62c60 | 1809 | ret = lxc_create_network_unpriv(handler->lxcpath, handler->name, |
4d781681 | 1810 | &conf->network, handler->pid, conf->hooks_version); |
cfc62c60 CB |
1811 | if (ret < 0) { |
1812 | ERROR("Failed to create the configured network"); | |
74c6e2b0 CB |
1813 | goto out_delete_net; |
1814 | } | |
0ad19a3f | 1815 | } |
1816 | ||
cfc62c60 CB |
1817 | ret = lxc_network_send_veth_names_to_child(handler); |
1818 | if (ret < 0) { | |
7ab1ba02 CB |
1819 | ERROR("Failed to send veth names to child"); |
1820 | goto out_delete_net; | |
658979c5 SH |
1821 | } |
1822 | ||
61d7a733 YT |
1823 | if (!lxc_list_empty(&conf->procs)) { |
1824 | ret = setup_proc_filesystem(&conf->procs, handler->pid); | |
1825 | if (ret < 0) | |
1826 | goto out_delete_net; | |
1827 | } | |
1828 | ||
408da065 CB |
1829 | /* Tell the child to continue its initialization. We'll get |
1830 | * LXC_SYNC_CGROUP when it is ready for us to setup cgroups. | |
3c22086f | 1831 | */ |
cfc62c60 CB |
1832 | ret = lxc_sync_barrier_child(handler, LXC_SYNC_POST_CONFIGURE); |
1833 | if (ret < 0) | |
544a48a0 SH |
1834 | goto out_delete_net; |
1835 | ||
cfc62c60 CB |
1836 | if (!lxc_list_empty(&conf->limits)) { |
1837 | ret = setup_resource_limits(&conf->limits, handler->pid); | |
1838 | if (ret < 0) { | |
1839 | ERROR("Failed to setup resource limits"); | |
1840 | goto out_delete_net; | |
1841 | } | |
c6d09e15 WB |
1842 | } |
1843 | ||
cfc62c60 CB |
1844 | ret = lxc_sync_barrier_child(handler, LXC_SYNC_CGROUP_UNSHARE); |
1845 | if (ret < 0) | |
f4152036 CB |
1846 | goto out_delete_net; |
1847 | ||
2202afc9 | 1848 | if (!cgroup_ops->setup_limits(cgroup_ops, handler->conf, true)) { |
cfc62c60 | 1849 | ERROR("Failed to setup legacy device cgroup controller limits"); |
b98f7d6e | 1850 | goto out_delete_net; |
544a48a0 | 1851 | } |
cfc62c60 | 1852 | TRACE("Set up legacy device cgroup controller limits"); |
544a48a0 | 1853 | |
becad0ec | 1854 | if (handler->ns_clone_flags & CLONE_NEWCGROUP) { |
8bf3abfb | 1855 | /* Now we're ready to preserve the cgroup namespace */ |
4cb53844 | 1856 | ret = lxc_try_preserve_ns(handler->pid, "cgroup"); |
8bf3abfb | 1857 | if (ret < 0) { |
4cb53844 | 1858 | if (ret != -EOPNOTSUPP) { |
6d1400b5 | 1859 | SYSERROR("Failed to preserve cgroup namespace"); |
4cb53844 CB |
1860 | goto out_delete_net; |
1861 | } | |
1862 | } else { | |
1863 | handler->nsfd[LXC_NS_CGROUP] = ret; | |
1864 | DEBUG("Preserved cgroup namespace via fd %d", ret); | |
8bf3abfb | 1865 | } |
8bf3abfb CB |
1866 | } |
1867 | ||
5b7f756a CB |
1868 | ret = snprintf(pidstr, 20, "%d", handler->pid); |
1869 | if (ret < 0 || ret >= 20) | |
1870 | goto out_delete_net; | |
1871 | ||
cfc62c60 CB |
1872 | ret = setenv("LXC_PID", pidstr, 1); |
1873 | if (ret < 0) | |
1874 | SYSERROR("Failed to set environment variable: LXC_PID=%s", pidstr); | |
08dd2805 SH |
1875 | |
1876 | /* Run any host-side start hooks */ | |
cfc62c60 CB |
1877 | ret = run_lxc_hooks(name, "start-host", conf, NULL); |
1878 | if (ret < 0) { | |
1879 | ERROR("Failed to run lxc.hook.start-host"); | |
341ed84c | 1880 | goto out_delete_net; |
08dd2805 SH |
1881 | } |
1882 | ||
408da065 CB |
1883 | /* Tell the child to complete its initialization and wait for it to exec |
1884 | * or return an error. (The child will never return | |
08dd2805 | 1885 | * LXC_SYNC_READY_START+1. It will either close the sync pipe, causing |
408da065 CB |
1886 | * lxc_sync_barrier_child to return success, or return a different |
1887 | * value, causing us to error out). | |
544a48a0 | 1888 | */ |
cfc62c60 CB |
1889 | ret = lxc_sync_barrier_child(handler, LXC_SYNC_READY_START); |
1890 | if (ret < 0) | |
341ed84c | 1891 | goto out_delete_net; |
0ad19a3f | 1892 | |
cfc62c60 CB |
1893 | ret = lxc_network_recv_name_and_ifindex_from_child(handler); |
1894 | if (ret < 0) { | |
790255cf CB |
1895 | ERROR("Failed to receive names and ifindices for network " |
1896 | "devices from child"); | |
1897 | goto out_delete_net; | |
1898 | } | |
1899 | ||
1900 | /* Now all networks are created, network devices are moved into place, | |
1901 | * and the correct names and ifindeces in the respective namespaces have | |
1902 | * been recorded. The corresponding structs have now all been filled. So | |
1903 | * log them for debugging purposes. | |
1904 | */ | |
28d9e29e | 1905 | lxc_log_configured_netdevs(conf); |
790255cf | 1906 | |
f4152036 | 1907 | /* Read tty fds allocated by child. */ |
cfc62c60 CB |
1908 | ret = lxc_recv_ttys_from_child(handler); |
1909 | if (ret < 0) { | |
1910 | ERROR("Failed to receive tty info from child process"); | |
f4152036 CB |
1911 | goto out_delete_net; |
1912 | } | |
1913 | ||
cfc62c60 CB |
1914 | ret = handler->ops->post_start(handler, handler->data); |
1915 | if (ret < 0) | |
e6126dbe MN |
1916 | goto out_abort; |
1917 | ||
cfc62c60 CB |
1918 | ret = lxc_set_state(name, handler, RUNNING); |
1919 | if (ret < 0) { | |
1920 | ERROR("Failed to set state to \"%s\"", lxc_state2str(RUNNING)); | |
59eb99ba | 1921 | goto out_abort; |
3f21c114 | 1922 | } |
22ebac19 | 1923 | |
3c22086f | 1924 | lxc_sync_fini(handler); |
0c547523 | 1925 | |
e6126dbe | 1926 | return 0; |
1ac470c0 | 1927 | |
7fef7a06 | 1928 | out_delete_net: |
becad0ec | 1929 | if (handler->ns_clone_flags & CLONE_NEWNET) |
bb84beda | 1930 | lxc_delete_network(handler); |
1bd8d726 | 1931 | |
59eb99ba DL |
1932 | out_abort: |
1933 | lxc_abort(name, handler); | |
95ef0d7c CB |
1934 | |
1935 | out_sync_fini: | |
3c22086f | 1936 | lxc_sync_fini(handler); |
5c068da9 SH |
1937 | if (handler->pinfd >= 0) { |
1938 | close(handler->pinfd); | |
1939 | handler->pinfd = -1; | |
1940 | } | |
1941 | ||
b79fcd86 | 1942 | return -1; |
59eb99ba | 1943 | } |
0ad19a3f | 1944 | |
aa460476 | 1945 | int __lxc_start(const char *name, struct lxc_handler *handler, |
507cee36 | 1946 | struct lxc_operations* ops, void *data, const char *lxcpath, |
a3b4f3d6 | 1947 | bool backgrounded, int *error_num) |
59eb99ba | 1948 | { |
c30e9b19 | 1949 | int ret, status; |
aa460476 | 1950 | struct lxc_conf *conf = handler->conf; |
80090207 | 1951 | |
c30e9b19 CB |
1952 | ret = lxc_init(name, handler); |
1953 | if (ret < 0) { | |
1954 | ERROR("Failed to initialize container \"%s\"", name); | |
66aeffc7 | 1955 | return -1; |
0ad19a3f | 1956 | } |
ee70bf78 CLG |
1957 | handler->ops = ops; |
1958 | handler->data = data; | |
507cee36 | 1959 | handler->backgrounded = backgrounded; |
e6126dbe | 1960 | |
76a26f55 | 1961 | if (!attach_block_device(handler->conf)) { |
c30e9b19 | 1962 | ERROR("Failed to attach block device"); |
76a26f55 SH |
1963 | goto out_fini_nonet; |
1964 | } | |
1965 | ||
35120d9c | 1966 | if (geteuid() == 0 && !lxc_list_empty(&conf->id_map)) { |
408da065 | 1967 | /* If the backing store is a device, mount it here and now. */ |
35120d9c | 1968 | if (rootfs_is_blockdev(conf)) { |
c30e9b19 CB |
1969 | ret = unshare(CLONE_NEWNS); |
1970 | if (ret < 0) { | |
1971 | ERROR("Failed to unshare CLONE_NEWNS"); | |
35120d9c SH |
1972 | goto out_fini_nonet; |
1973 | } | |
c30e9b19 | 1974 | INFO("Unshared CLONE_NEWNS"); |
408da065 | 1975 | |
6a0c909a | 1976 | remount_all_slave(); |
8ce1abc2 | 1977 | ret = lxc_setup_rootfs_prepare_root(conf, name, lxcpath); |
c30e9b19 CB |
1978 | if (ret < 0) { |
1979 | ERROR("Error setting up rootfs mount as root before spawn"); | |
35120d9c SH |
1980 | goto out_fini_nonet; |
1981 | } | |
c30e9b19 | 1982 | INFO("Set up container rootfs as host root"); |
35120d9c SH |
1983 | } |
1984 | } | |
1985 | ||
c30e9b19 CB |
1986 | ret = lxc_spawn(handler); |
1987 | if (ret < 0) { | |
1988 | ERROR("Failed to spawn container \"%s\"", name); | |
76a26f55 | 1989 | goto out_detach_blockdev; |
0ad19a3f | 1990 | } |
a9e1109e CB |
1991 | /* close parent side of data socket */ |
1992 | close(handler->data_sock[0]); | |
1993 | handler->data_sock[0] = -1; | |
1994 | close(handler->data_sock[1]); | |
1995 | handler->data_sock[1] = -1; | |
0ad19a3f | 1996 | |
80308d07 | 1997 | handler->conf->reboot = REBOOT_NONE; |
8bee8851 | 1998 | |
c30e9b19 CB |
1999 | ret = lxc_poll(name, handler); |
2000 | if (ret) { | |
2001 | ERROR("LXC mainloop exited with error: %d", ret); | |
59eb99ba DL |
2002 | goto out_abort; |
2003 | } | |
0ad19a3f | 2004 | |
321db026 DJ |
2005 | if (!handler->init_died && handler->pid > 0) { |
2006 | ERROR("Child process is not killed"); | |
2007 | goto out_abort; | |
2008 | } | |
2009 | ||
c30e9b19 CB |
2010 | status = lxc_wait_for_pid_status(handler->pid); |
2011 | if (status < 0) | |
2012 | SYSERROR("Failed to retrieve status for %d", handler->pid); | |
e043236e | 2013 | |
408da065 CB |
2014 | /* If the child process exited but was not signaled, it didn't call |
2015 | * reboot. This should mean it was an lxc-execute which simply exited. | |
2016 | * In any case, treat it as a 'halt'. | |
8b004f07 | 2017 | */ |
d028235d | 2018 | if (WIFSIGNALED(status)) { |
8b004f07 SH |
2019 | switch(WTERMSIG(status)) { |
2020 | case SIGINT: /* halt */ | |
c30e9b19 | 2021 | DEBUG("Container \"%s\" is halting", name); |
8b004f07 SH |
2022 | break; |
2023 | case SIGHUP: /* reboot */ | |
c30e9b19 | 2024 | DEBUG("Container \"%s\" is rebooting", name); |
80308d07 | 2025 | handler->conf->reboot = REBOOT_REQ; |
8b004f07 | 2026 | break; |
c2b9bd9e | 2027 | case SIGSYS: /* seccomp */ |
c30e9b19 | 2028 | DEBUG("Container \"%s\" violated its seccomp policy", name); |
c2b9bd9e | 2029 | break; |
8b004f07 | 2030 | default: |
c30e9b19 | 2031 | DEBUG("Unknown exit status for container \"%s\" init %d", name, WTERMSIG(status)); |
8b004f07 SH |
2032 | break; |
2033 | } | |
d028235d | 2034 | } |
828695d9 | 2035 | |
c30e9b19 CB |
2036 | ret = lxc_restore_phys_nics_to_netns(handler); |
2037 | if (ret < 0) | |
b809f232 CB |
2038 | ERROR("Failed to move physical network devices back to parent " |
2039 | "network namespace"); | |
ce5782df | 2040 | |
5c068da9 SH |
2041 | if (handler->pinfd >= 0) { |
2042 | close(handler->pinfd); | |
2043 | handler->pinfd = -1; | |
2044 | } | |
2045 | ||
1787abca | 2046 | lxc_monitor_send_exit_code(name, status, handler->lxcpath); |
eb808539 | 2047 | lxc_error_set_and_log(handler->pid, status); |
a3b4f3d6 TA |
2048 | if (error_num) |
2049 | *error_num = handler->exit_status; | |
1bd8d726 | 2050 | |
9d7f9e52 | 2051 | out_fini: |
bb84beda | 2052 | lxc_delete_network(handler); |
74a2b586 | 2053 | |
76a26f55 SH |
2054 | out_detach_blockdev: |
2055 | detach_block_device(handler->conf); | |
2056 | ||
74a2b586 | 2057 | out_fini_nonet: |
3a0f472d | 2058 | lxc_fini(name, handler); |
c30e9b19 | 2059 | return ret; |
0ad19a3f | 2060 | |
59eb99ba | 2061 | out_abort: |
3a0f472d | 2062 | lxc_abort(name, handler); |
9d7f9e52 | 2063 | goto out_fini; |
0ad19a3f | 2064 | } |
ee70bf78 CLG |
2065 | |
2066 | struct start_args { | |
2067 | char *const *argv; | |
2068 | }; | |
2069 | ||
2070 | static int start(struct lxc_handler *handler, void* data) | |
2071 | { | |
2072 | struct start_args *arg = data; | |
2073 | ||
984984e4 | 2074 | NOTICE("Exec'ing \"%s\"", arg->argv[0]); |
ee70bf78 CLG |
2075 | |
2076 | execvp(arg->argv[0], arg->argv); | |
984984e4 | 2077 | SYSERROR("Failed to exec \"%s\"", arg->argv[0]); |
ee70bf78 CLG |
2078 | return 0; |
2079 | } | |
2080 | ||
2081 | static int post_start(struct lxc_handler *handler, void* data) | |
2082 | { | |
2083 | struct start_args *arg = data; | |
2084 | ||
4c8e880e | 2085 | NOTICE("Started \"%s\" with pid \"%d\"", arg->argv[0], handler->pid); |
ee70bf78 CLG |
2086 | return 0; |
2087 | } | |
2088 | ||
2089 | static struct lxc_operations start_ops = { | |
2090 | .start = start, | |
2091 | .post_start = post_start | |
2092 | }; | |
2093 | ||
aa460476 | 2094 | int lxc_start(const char *name, char *const argv[], struct lxc_handler *handler, |
a3b4f3d6 | 2095 | const char *lxcpath, bool backgrounded, int *error_num) |
ee70bf78 CLG |
2096 | { |
2097 | struct start_args start_arg = { | |
2098 | .argv = argv, | |
2099 | }; | |
2100 | ||
b2efeb0b | 2101 | TRACE("Doing lxc_start"); |
a3b4f3d6 | 2102 | return __lxc_start(name, handler, &start_ops, &start_arg, lxcpath, backgrounded, error_num); |
ee70bf78 | 2103 | } |
28272964 CB |
2104 | |
2105 | static void lxc_destroy_container_on_signal(struct lxc_handler *handler, | |
2106 | const char *name) | |
2107 | { | |
2108 | char destroy[MAXPATHLEN]; | |
f01f7975 | 2109 | struct lxc_container *c; |
ae3beac9 CB |
2110 | int ret = 0; |
2111 | bool bret = true; | |
2112 | ||
df31363a | 2113 | if (handler->conf->rootfs.path && handler->conf->rootfs.mount) { |
790255cf | 2114 | bret = do_destroy_container(handler); |
28272964 | 2115 | if (!bret) { |
ae3beac9 | 2116 | ERROR("Error destroying rootfs for container \"%s\"", name); |
28272964 CB |
2117 | return; |
2118 | } | |
2119 | } | |
ae3beac9 | 2120 | INFO("Destroyed rootfs for container \"%s\"", name); |
28272964 CB |
2121 | |
2122 | ret = snprintf(destroy, MAXPATHLEN, "%s/%s", handler->lxcpath, name); | |
2123 | if (ret < 0 || ret >= MAXPATHLEN) { | |
ae3beac9 | 2124 | ERROR("Error destroying directory for container \"%s\"", name); |
28272964 CB |
2125 | return; |
2126 | } | |
2127 | ||
f01f7975 CB |
2128 | c = lxc_container_new(name, handler->lxcpath); |
2129 | if (c) { | |
2130 | if (container_disk_lock(c)) { | |
ae3beac9 | 2131 | INFO("Could not update lxc_snapshots file"); |
f01f7975 CB |
2132 | lxc_container_put(c); |
2133 | } else { | |
2134 | mod_all_rdeps(c, false); | |
2135 | container_disk_unlock(c); | |
2136 | lxc_container_put(c); | |
2137 | } | |
2138 | } | |
2139 | ||
d0fbc7ba | 2140 | if (!handler->am_root) |
94b0a3ac CB |
2141 | ret = userns_exec_full(handler->conf, lxc_rmdir_onedev_wrapper, |
2142 | destroy, "lxc_rmdir_onedev_wrapper"); | |
28272964 CB |
2143 | else |
2144 | ret = lxc_rmdir_onedev(destroy, NULL); | |
2145 | ||
2146 | if (ret < 0) { | |
ae3beac9 | 2147 | ERROR("Error destroying directory for container \"%s\"", name); |
28272964 CB |
2148 | return; |
2149 | } | |
ae3beac9 | 2150 | INFO("Destroyed directory for container \"%s\"", name); |
28272964 CB |
2151 | } |
2152 | ||
2153 | static int lxc_rmdir_onedev_wrapper(void *data) | |
2154 | { | |
2155 | char *arg = (char *) data; | |
2156 | return lxc_rmdir_onedev(arg, NULL); | |
2157 | } | |
2158 | ||
134df645 CB |
2159 | static bool do_destroy_container(struct lxc_handler *handler) |
2160 | { | |
94b0a3ac CB |
2161 | int ret; |
2162 | ||
d0fbc7ba | 2163 | if (!handler->am_root) { |
94b0a3ac CB |
2164 | ret = userns_exec_full(handler->conf, storage_destroy_wrapper, |
2165 | handler->conf, "storage_destroy_wrapper"); | |
2166 | if (ret < 0) | |
d028235d | 2167 | return false; |
10bc1861 | 2168 | |
d028235d SG |
2169 | return true; |
2170 | } | |
10bc1861 | 2171 | |
790255cf | 2172 | return storage_destroy(handler->conf); |
28272964 | 2173 | } |