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