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