]>
Commit | Line | Data |
---|---|---|
0ad19a3f | 1 | /* |
2 | * lxc: linux Container library | |
3 | * | |
4 | * (C) Copyright IBM Corp. 2007, 2008 | |
5 | * | |
6 | * Authors: | |
9afe19d6 | 7 | * Daniel Lezcano <daniel.lezcano at free.fr> |
35a02107 CB |
8 | * Serge Hallyn <serge@hallyn.com> |
9 | * Christian Brauner <christian.brauner@ubuntu.com> | |
0ad19a3f | 10 | * |
11 | * This library is free software; you can redistribute it and/or | |
12 | * modify it under the terms of the GNU Lesser General Public | |
13 | * License as published by the Free Software Foundation; either | |
14 | * version 2.1 of the License, or (at your option) any later version. | |
15 | * | |
16 | * This library is distributed in the hope that it will be useful, | |
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
19 | * Lesser General Public License for more details. | |
20 | * | |
21 | * You should have received a copy of the GNU Lesser General Public | |
22 | * License along with this library; if not, write to the Free Software | |
250b1eec | 23 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
0ad19a3f | 24 | */ |
25 | ||
f3787121 | 26 | #define _GNU_SOURCE |
f549edcc GK |
27 | #include "config.h" |
28 | ||
f3787121 | 29 | #include <alloca.h> |
0ad19a3f | 30 | #include <dirent.h> |
31 | #include <errno.h> | |
b0a33c1e | 32 | #include <fcntl.h> |
c476bdce | 33 | #include <grp.h> |
37515ebd | 34 | #include <poll.h> |
f3787121 CB |
35 | #include <signal.h> |
36 | #include <stdio.h> | |
37 | #include <stdlib.h> | |
38 | #include <string.h> | |
39 | #include <unistd.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> |
ff218c25 | 50 | |
e37dda71 | 51 | #if HAVE_LIBCAP |
495d2046 SG |
52 | #include <sys/capability.h> |
53 | #endif | |
54 | ||
995acccc | 55 | #if !HAVE_DECL_PR_CAPBSET_DROP |
656994bb MH |
56 | #define PR_CAPBSET_DROP 24 |
57 | #endif | |
58 | ||
995acccc | 59 | #if !HAVE_DECL_PR_SET_NO_NEW_PRIVS |
955e2a02 CB |
60 | #define PR_SET_NO_NEW_PRIVS 38 |
61 | #endif | |
62 | ||
995acccc | 63 | #if !HAVE_DECL_PR_GET_NO_NEW_PRIVS |
955e2a02 CB |
64 | #define PR_GET_NO_NEW_PRIVS 39 |
65 | #endif | |
66 | ||
f3787121 CB |
67 | #include "af_unix.h" |
68 | #include "caps.h" | |
563f2f2c | 69 | #include "cgroup.h" |
f3787121 | 70 | #include "commands.h" |
bbf5cf35 | 71 | #include "commands_utils.h" |
f3787121 | 72 | #include "conf.h" |
74c6e2b0 | 73 | #include "confile_utils.h" |
e2bcd7db | 74 | #include "error.h" |
1cc8bd4b | 75 | #include "list.h" |
0ed9b1bc | 76 | #include "lsm/lsm.h" |
f3787121 | 77 | #include "log.h" |
f2d5a09d | 78 | #include "lxccontainer.h" |
f3787121 CB |
79 | #include "lxclock.h" |
80 | #include "lxcseccomp.h" | |
f3787121 | 81 | #include "mainloop.h" |
63376d7d | 82 | #include "monitor.h" |
f549edcc | 83 | #include "namespace.h" |
811ef482 | 84 | #include "network.h" |
f3787121 | 85 | #include "start.h" |
6be5397b CB |
86 | #include "storage/storage.h" |
87 | #include "storage/storage_utils.h" | |
0ed9b1bc CB |
88 | #include "sync.h" |
89 | #include "terminal.h" | |
90 | #include "utils.h" | |
36eb9bde CLG |
91 | |
92 | lxc_log_define(lxc_start, lxc); | |
93 | ||
f01f7975 | 94 | extern void mod_all_rdeps(struct lxc_container *c, bool inc); |
790255cf | 95 | static bool do_destroy_container(struct lxc_handler *handler); |
28272964 CB |
96 | static int lxc_rmdir_onedev_wrapper(void *data); |
97 | static void lxc_destroy_container_on_signal(struct lxc_handler *handler, | |
98 | const char *name); | |
99 | ||
c8154066 SH |
100 | static void print_top_failing_dir(const char *path) |
101 | { | |
9e5f5f2f CB |
102 | int ret; |
103 | size_t len; | |
104 | char *copy, *e, *p, saved; | |
c8154066 | 105 | |
9e5f5f2f CB |
106 | len = strlen(path); |
107 | copy = alloca(len + 1); | |
108 | strcpy(copy, path); | |
c8154066 SH |
109 | p = copy; |
110 | e = copy + len; | |
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 | ||
156 | WARN("%s - Kernel does not support preserving %s namespaces", | |
157 | strerror(errno), ns); | |
158 | return -EOPNOTSUPP; | |
159 | } | |
160 | ||
161 | return fd; | |
162 | } | |
163 | ||
164 | /* lxc_try_preserve_namespaces: open /proc/@pid/ns/@ns for each namespace | |
165 | * specified in ns_clone_flags. | |
4d8ac866 | 166 | * Return true on success, false on failure. |
62d05d9b | 167 | */ |
4cb53844 CB |
168 | static bool lxc_try_preserve_namespaces(struct lxc_handler *handler, |
169 | int ns_clone_flags, pid_t pid) | |
f3787121 | 170 | { |
9fef3355 | 171 | int i; |
9f30a190 | 172 | |
9f30a190 | 173 | for (i = 0; i < LXC_NS_MAX; i++) |
5032bf39 | 174 | handler->nsfd[i] = -EBADF; |
cd43d2d1 | 175 | |
9f30a190 | 176 | for (i = 0; i < LXC_NS_MAX; i++) { |
4cb53844 CB |
177 | int fd; |
178 | ||
becad0ec | 179 | if ((ns_clone_flags & ns_info[i].clone_flag) == 0) |
9f30a190 | 180 | continue; |
28d9e29e | 181 | |
4cb53844 CB |
182 | fd = lxc_try_preserve_ns(pid, ns_info[i].proc_name); |
183 | if (fd < 0) { | |
184 | handler->nsfd[i] = -EBADF; | |
185 | ||
186 | /* Do not fail to start container on kernels that do | |
187 | * not support interacting with namespaces through | |
188 | * /proc. | |
189 | */ | |
190 | if (fd == -EOPNOTSUPP) | |
191 | continue; | |
fa3a5b22 | 192 | |
4cb53844 CB |
193 | lxc_put_nsfds(handler); |
194 | return false; | |
195 | } | |
196 | ||
197 | handler->nsfd[i] = fd; | |
198 | DEBUG("Preserved %s namespace via fd %d", ns_info[i].proc_name, | |
199 | handler->nsfd[i]); | |
9f30a190 MM |
200 | } |
201 | ||
62d05d9b | 202 | return true; |
9f30a190 MM |
203 | } |
204 | ||
80090207 CLG |
205 | static int match_fd(int fd) |
206 | { | |
207 | return (fd == 0 || fd == 1 || fd == 2); | |
208 | } | |
209 | ||
47a46cf1 CB |
210 | int lxc_check_inherited(struct lxc_conf *conf, bool closeall, |
211 | int *fds_to_ignore, size_t len_fds) | |
80090207 | 212 | { |
80090207 | 213 | int fd, fddir; |
47a46cf1 | 214 | size_t i; |
80090207 | 215 | DIR *dir; |
a5a70219 | 216 | struct dirent *direntp; |
80090207 | 217 | |
d2cf4c37 SH |
218 | if (conf && conf->close_all_fds) |
219 | closeall = true; | |
220 | ||
b119f362 | 221 | restart: |
80090207 CLG |
222 | dir = opendir("/proc/self/fd"); |
223 | if (!dir) { | |
a5a70219 | 224 | WARN("%s - Failed to open directory", strerror(errno)); |
80090207 CLG |
225 | return -1; |
226 | } | |
227 | ||
228 | fddir = dirfd(dir); | |
229 | ||
74f96976 | 230 | while ((direntp = readdir(dir))) { |
a5a70219 | 231 | int ret; |
d39b10eb CB |
232 | struct lxc_list *cur; |
233 | bool matched = false; | |
234 | ||
a5a70219 | 235 | if (strcmp(direntp->d_name, ".") == 0) |
80090207 CLG |
236 | continue; |
237 | ||
a5a70219 | 238 | if (strcmp(direntp->d_name, "..") == 0) |
80090207 CLG |
239 | continue; |
240 | ||
a5a70219 CB |
241 | ret = lxc_safe_int(direntp->d_name, &fd); |
242 | if (ret < 0) { | |
243 | INFO("Could not parse file descriptor for \"%s\"", direntp->d_name); | |
d4cff0d2 CB |
244 | continue; |
245 | } | |
80090207 | 246 | |
47a46cf1 CB |
247 | for (i = 0; i < len_fds; i++) |
248 | if (fds_to_ignore[i] == fd) | |
249 | break; | |
250 | ||
251 | if (fd == fddir || fd == lxc_log_fd || | |
252 | (i < len_fds && fd == fds_to_ignore[i])) | |
80090207 CLG |
253 | continue; |
254 | ||
d39b10eb | 255 | /* Keep state clients that wait on reboots. */ |
3ee9e4fb CB |
256 | if (conf) { |
257 | lxc_list_for_each(cur, &conf->state_clients) { | |
258 | struct lxc_state_client *client = cur->elem; | |
d39b10eb | 259 | |
3ee9e4fb CB |
260 | if (client->clientfd != fd) |
261 | continue; | |
d39b10eb | 262 | |
3ee9e4fb CB |
263 | matched = true; |
264 | break; | |
265 | } | |
d39b10eb CB |
266 | } |
267 | ||
268 | if (matched) | |
269 | continue; | |
270 | ||
858377e4 SH |
271 | if (current_config && fd == current_config->logfd) |
272 | continue; | |
273 | ||
80090207 CLG |
274 | if (match_fd(fd)) |
275 | continue; | |
80090207 | 276 | |
d2cf4c37 | 277 | if (closeall) { |
b119f362 SH |
278 | close(fd); |
279 | closedir(dir); | |
28d9e29e | 280 | INFO("Closed inherited fd %d", fd); |
b119f362 SH |
281 | goto restart; |
282 | } | |
28d9e29e | 283 | WARN("Inherited fd %d", fd); |
80090207 CLG |
284 | } |
285 | ||
408da065 | 286 | /* Only enable syslog at this point to avoid the above logging function |
64c57ea1 BD |
287 | * to open a new fd and make the check_inherited function enter an |
288 | * infinite loop. | |
289 | */ | |
290 | lxc_log_enable_syslog(); | |
291 | ||
92c7f629 GK |
292 | closedir(dir); /* cannot fail */ |
293 | return 0; | |
80090207 CLG |
294 | } |
295 | ||
83ee7875 | 296 | static int setup_signal_fd(sigset_t *oldmask) |
b0a33c1e | 297 | { |
df0795b1 | 298 | int ret, sig; |
b0a33c1e | 299 | sigset_t mask; |
df0795b1 | 300 | 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 | ||
df0795b1 CB |
313 | ret = sigprocmask(SIG_BLOCK, &mask, oldmask); |
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 DL |
337 | |
338 | ret = read(fd, &siginfo, sizeof(siginfo)); | |
339 | if (ret < 0) { | |
6e94162a | 340 | ERROR("Failed to read signal info from signal file descriptor %d", fd); |
15cd25fd DL |
341 | return -1; |
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: | |
372 | ERROR("Unknown si_code: %d", hdlr->init_died); | |
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); | |
380 | return hdlr->init_died ? LXC_MAINLOOP_CLOSE : 0; | |
381 | } | |
382 | ||
3a9e949f TA |
383 | /* More robustness, protect ourself from a SIGCHLD sent |
384 | * by a process different from the container init. | |
385 | */ | |
386 | if (siginfo.ssi_pid != hdlr->pid) { | |
6e94162a CB |
387 | NOTICE("Received %d from pid %d instead of container init %d", |
388 | siginfo.ssi_signo, siginfo.ssi_pid, hdlr->pid); | |
3a9e949f TA |
389 | return hdlr->init_died ? LXC_MAINLOOP_CLOSE : 0; |
390 | } | |
391 | ||
f3304a29 | 392 | if (siginfo.ssi_signo != SIGCHLD) { |
3c319edb | 393 | kill(hdlr->pid, siginfo.ssi_signo); |
6e94162a | 394 | INFO("Forwarded signal %d to pid %d", siginfo.ssi_signo, hdlr->pid); |
3c319edb | 395 | return hdlr->init_died ? LXC_MAINLOOP_CLOSE : 0; |
f3304a29 FW |
396 | } |
397 | ||
408da065 | 398 | if (siginfo.ssi_code == CLD_STOPPED) { |
6e94162a | 399 | INFO("Container init process was stopped"); |
3c319edb | 400 | return hdlr->init_died ? LXC_MAINLOOP_CLOSE : 0; |
408da065 | 401 | } else if (siginfo.ssi_code == CLD_CONTINUED) { |
6e94162a | 402 | INFO("Container init process was continued"); |
3c319edb | 403 | return hdlr->init_died ? LXC_MAINLOOP_CLOSE : 0; |
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 | { |
dbc9832d CB |
413 | ssize_t ret; |
414 | struct lxc_list *cur, *next; | |
d39b10eb | 415 | struct lxc_state_client *client; |
dbc9832d CB |
416 | struct lxc_msg msg = {.type = lxc_msg_state, .value = state}; |
417 | ||
974a8aba CB |
418 | if (state == THAWED) |
419 | handler->state = RUNNING; | |
420 | else | |
421 | handler->state = state; | |
422 | ||
9dfa4041 | 423 | TRACE("Set container state to %s", lxc_state2str(state)); |
dbc9832d | 424 | |
d39b10eb | 425 | if (lxc_list_empty(&handler->conf->state_clients)) { |
9dfa4041 | 426 | TRACE("No state clients registered"); |
dbc9832d CB |
427 | return 0; |
428 | } | |
429 | ||
430 | strncpy(msg.name, name, sizeof(msg.name)); | |
431 | msg.name[sizeof(msg.name) - 1] = 0; | |
432 | ||
d39b10eb | 433 | lxc_list_for_each_safe(cur, &handler->conf->state_clients, next) { |
dbc9832d CB |
434 | client = cur->elem; |
435 | ||
d39b10eb | 436 | if (client->states[state] == 0) { |
9dfa4041 | 437 | TRACE("State %s not registered for state client %d", |
dbc9832d CB |
438 | lxc_state2str(state), client->clientfd); |
439 | continue; | |
440 | } | |
441 | ||
9dfa4041 | 442 | TRACE("Sending state %s to state client %d", |
dbc9832d CB |
443 | lxc_state2str(state), client->clientfd); |
444 | ||
445 | again: | |
446 | ret = send(client->clientfd, &msg, sizeof(msg), 0); | |
ee8377bd CB |
447 | if (ret <= 0) { |
448 | if (errno == EINTR) { | |
449 | TRACE("Caught EINTR; retrying"); | |
dbc9832d | 450 | goto again; |
ee8377bd | 451 | } |
dbc9832d | 452 | |
9dfa4041 CB |
453 | ERROR("%s - Failed to send message to client", |
454 | strerror(errno)); | |
dbc9832d CB |
455 | } |
456 | ||
457 | /* kick client from list */ | |
dbc9832d | 458 | lxc_list_del(cur); |
300d1cb4 | 459 | close(client->clientfd); |
dbc9832d CB |
460 | free(cur->elem); |
461 | free(cur); | |
462 | } | |
dbc9832d | 463 | |
5e5576a4 CB |
464 | return 0; |
465 | } | |
466 | ||
467 | static int lxc_serve_state_socket_pair(const char *name, | |
468 | struct lxc_handler *handler, | |
469 | lxc_state_t state) | |
470 | { | |
471 | ssize_t ret; | |
472 | ||
473 | if (!handler->backgrounded || | |
474 | handler->state_socket_pair[1] < 0 || | |
475 | state == STARTING) | |
476 | return 0; | |
477 | ||
478 | /* Close read end of the socket pair. */ | |
479 | close(handler->state_socket_pair[0]); | |
480 | handler->state_socket_pair[0] = -1; | |
481 | ||
ee8377bd | 482 | again: |
5e5576a4 CB |
483 | ret = lxc_abstract_unix_send_credential(handler->state_socket_pair[1], |
484 | &(int){state}, sizeof(int)); | |
ee8377bd CB |
485 | if (ret != sizeof(int)) { |
486 | if (errno == EINTR) | |
487 | goto again; | |
5e5576a4 CB |
488 | SYSERROR("Failed to send state to %d", |
489 | 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 | ||
becad0ec | 615 | handler->ns_clone_flags = -1; |
41784e4e CB |
616 | |
617 | handler->pinfd = -1; | |
618 | ||
619 | handler->sigfd = -1; | |
620 | ||
621 | for (i = 0; i < LXC_NS_MAX; i++) | |
622 | handler->nsfd[i] = -1; | |
623 | ||
624 | handler->data_sock[0] = -1; | |
625 | handler->data_sock[1] = -1; | |
626 | ||
627 | handler->state_socket_pair[0] = -1; | |
628 | handler->state_socket_pair[1] = -1; | |
629 | ||
630 | handler->sync_sock[0] = -1; | |
631 | handler->sync_sock[1] = -1; | |
632 | } | |
633 | ||
f2e07cb6 CB |
634 | void lxc_free_handler(struct lxc_handler *handler) |
635 | { | |
1cc8bd4b CB |
636 | if (handler->pinfd >= 0) |
637 | close(handler->pinfd); | |
638 | ||
639 | if (handler->sigfd >= 0) | |
640 | close(handler->sigfd); | |
641 | ||
642 | lxc_put_nsfds(handler); | |
643 | ||
644 | if (handler->conf && handler->conf->reboot == 0) | |
0d0d3655 | 645 | if (handler->conf->maincmd_fd >= 0) |
1cc8bd4b | 646 | close(handler->conf->maincmd_fd); |
f2e07cb6 | 647 | |
5e5576a4 CB |
648 | if (handler->state_socket_pair[0] >= 0) |
649 | close(handler->state_socket_pair[0]); | |
650 | ||
651 | if (handler->state_socket_pair[1] >= 0) | |
652 | close(handler->state_socket_pair[1]); | |
653 | ||
f2e07cb6 CB |
654 | handler->conf = NULL; |
655 | free(handler); | |
1cc8bd4b | 656 | handler = NULL; |
f2e07cb6 CB |
657 | } |
658 | ||
aa460476 | 659 | struct lxc_handler *lxc_init_handler(const char *name, struct lxc_conf *conf, |
5e5576a4 | 660 | const char *lxcpath, bool daemonize) |
59eb99ba | 661 | { |
5e5576a4 | 662 | int i, ret; |
3a0f472d DL |
663 | struct lxc_handler *handler; |
664 | ||
665 | handler = malloc(sizeof(*handler)); | |
fdecbc9c | 666 | if (!handler) |
3a0f472d | 667 | return NULL; |
59eb99ba DL |
668 | memset(handler, 0, sizeof(*handler)); |
669 | ||
fdecbc9c CB |
670 | /* Note that am_guest_unpriv() checks the effective uid. We |
671 | * probably don't care if we are real root only if we are running | |
672 | * as root so this should be fine. | |
790255cf | 673 | */ |
e0010464 | 674 | handler->am_root = !am_guest_unpriv(); |
c6012571 | 675 | handler->data_sock[0] = handler->data_sock[1] = -1; |
fae349da | 676 | handler->conf = conf; |
9123e471 | 677 | handler->lxcpath = lxcpath; |
5c068da9 | 678 | handler->pinfd = -1; |
1cc8bd4b CB |
679 | handler->sigfd = -EBADF; |
680 | handler->init_died = false; | |
5e5576a4 | 681 | handler->state_socket_pair[0] = handler->state_socket_pair[1] = -1; |
d39b10eb CB |
682 | if (handler->conf->reboot == 0) |
683 | lxc_list_init(&handler->conf->state_clients); | |
fae349da | 684 | |
b6b2b194 WB |
685 | for (i = 0; i < LXC_NS_MAX; i++) |
686 | handler->nsfd[i] = -1; | |
687 | ||
f0ecc19d | 688 | handler->name = name; |
3bdf52d7 | 689 | |
5e5576a4 CB |
690 | if (daemonize && !handler->conf->reboot) { |
691 | /* Create socketpair() to synchronize on daemonized startup. | |
fdecbc9c CB |
692 | * When the container reboots we don't need to synchronize |
693 | * again currently so don't open another socketpair(). | |
5e5576a4 CB |
694 | */ |
695 | ret = socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, | |
696 | handler->state_socket_pair); | |
697 | if (ret < 0) { | |
698 | ERROR("Failed to create anonymous pair of unix sockets"); | |
699 | goto on_error; | |
700 | } | |
701 | TRACE("Created anonymous pair {%d,%d} of unix sockets", | |
702 | handler->state_socket_pair[0], | |
703 | handler->state_socket_pair[1]); | |
704 | } | |
705 | ||
d39b10eb CB |
706 | if (handler->conf->reboot == 0) { |
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 | } |
9dfa4041 | 713 | TRACE("Unix domain socket %d for command server is ready", |
aa460476 CB |
714 | handler->conf->maincmd_fd); |
715 | ||
716 | return handler; | |
717 | ||
f2e07cb6 CB |
718 | on_error: |
719 | lxc_free_handler(handler); | |
aa460476 CB |
720 | |
721 | return NULL; | |
722 | } | |
723 | ||
724 | int lxc_init(const char *name, struct lxc_handler *handler) | |
725 | { | |
a2c09be0 | 726 | int ret; |
4a03ded4 | 727 | const char *loglevel; |
aa460476 CB |
728 | struct lxc_conf *conf = handler->conf; |
729 | ||
730 | lsm_init(); | |
2170b263 | 731 | TRACE("Initialized LSM"); |
d2e30e99 | 732 | |
2170b263 CB |
733 | ret = lxc_read_seccomp_config(conf); |
734 | if (ret < 0) { | |
735 | ERROR("Failed loading seccomp policy"); | |
d2e30e99 | 736 | goto out_close_maincmd_fd; |
8f2c3a70 | 737 | } |
2170b263 | 738 | TRACE("Read seccomp policy"); |
8f2c3a70 | 739 | |
408da065 | 740 | /* Begin by setting the state to STARTING. */ |
2170b263 CB |
741 | ret = lxc_set_state(name, handler, STARTING); |
742 | if (ret < 0) { | |
743 | ERROR("Failed to set state to \"%s\"", lxc_state2str(STARTING)); | |
051151de | 744 | goto out_close_maincmd_fd; |
0ad19a3f | 745 | } |
46800e77 | 746 | TRACE("Set container state to \"STARTING\""); |
0ad19a3f | 747 | |
408da065 | 748 | /* Start of environment variable setup for hooks. */ |
2170b263 CB |
749 | if (name) { |
750 | ret = setenv("LXC_NAME", name, 1); | |
751 | if (ret < 0) | |
752 | SYSERROR("Failed to set environment variable: LXC_NAME=%s", name); | |
753 | } | |
408da065 | 754 | |
2170b263 CB |
755 | if (conf->rcfile) { |
756 | ret = setenv("LXC_CONFIG_FILE", conf->rcfile, 1); | |
757 | if (ret < 0) | |
758 | SYSERROR("Failed to set environment variable: " | |
759 | "LXC_CONFIG_FILE=%s", conf->rcfile); | |
760 | } | |
408da065 | 761 | |
2170b263 CB |
762 | if (conf->rootfs.mount) { |
763 | ret = setenv("LXC_ROOTFS_MOUNT", conf->rootfs.mount, 1); | |
764 | if (ret < 0) | |
765 | SYSERROR("Failed to set environment variable: " | |
766 | "LXC_ROOTFS_MOUNT=%s", conf->rootfs.mount); | |
767 | } | |
408da065 | 768 | |
2170b263 CB |
769 | if (conf->rootfs.path) { |
770 | ret = setenv("LXC_ROOTFS_PATH", conf->rootfs.path, 1); | |
771 | if (ret < 0) | |
772 | SYSERROR("Failed to set environment variable: " | |
773 | "LXC_ROOTFS_PATH=%s", conf->rootfs.path); | |
774 | } | |
408da065 | 775 | |
2170b263 CB |
776 | if (conf->console.path) { |
777 | ret = setenv("LXC_CONSOLE", conf->console.path, 1); | |
778 | if (ret < 0) | |
779 | SYSERROR("Failed to set environment variable: " | |
780 | "LXC_CONSOLE=%s", conf->console.path); | |
781 | } | |
408da065 | 782 | |
2170b263 CB |
783 | if (conf->console.log_path) { |
784 | ret = setenv("LXC_CONSOLE_LOGPATH", conf->console.log_path, 1); | |
785 | if (ret < 0) | |
786 | SYSERROR("Failed to set environment variable: " | |
787 | "LXC_CONSOLE_LOGPATH=%s", conf->console.log_path); | |
788 | } | |
408da065 | 789 | |
2170b263 CB |
790 | if (cgns_supported()) { |
791 | ret = setenv("LXC_CGNS_AWARE", "1", 1); | |
792 | if (ret < 0) | |
793 | SYSERROR("Failed to set environment variable " | |
794 | "LXC_CGNS_AWARE=1"); | |
795 | } | |
b8f88d9b | 796 | |
4a03ded4 | 797 | loglevel = lxc_log_priority_to_string(lxc_log_get_level()); |
2170b263 CB |
798 | ret = setenv("LXC_LOG_LEVEL", loglevel, 1); |
799 | if (ret < 0) | |
800 | SYSERROR("Set environment variable LXC_LOG_LEVEL=%s", | |
801 | loglevel); | |
a2c09be0 CB |
802 | |
803 | if (conf->hooks_version == 0) | |
804 | ret = setenv("LXC_HOOK_VERSION", "0", 1); | |
805 | else | |
806 | ret = setenv("LXC_HOOK_VERSION", "1", 1); | |
807 | if (ret < 0) | |
808 | SYSERROR("Failed to set environment variable LXC_HOOK_VERSION=%u", conf->hooks_version); | |
408da065 | 809 | /* End of environment variable setup for hooks. */ |
f7bee6c6 | 810 | |
2170b263 | 811 | TRACE("Set environment variables"); |
dbc9832d | 812 | |
2170b263 CB |
813 | ret = run_lxc_hooks(name, "pre-start", conf, NULL); |
814 | if (ret < 0) { | |
815 | ERROR("Failed to run lxc.hook.pre-start for container \"%s\"", name); | |
773fb9ca SH |
816 | goto out_aborting; |
817 | } | |
2170b263 | 818 | TRACE("Ran pre-start hooks"); |
26ddeedd | 819 | |
408da065 CB |
820 | /* The signal fd has to be created before forking otherwise if the child |
821 | * process exits before we setup the signal fd, the event will be lost | |
822 | * and the command will be stuck. | |
823 | */ | |
83ee7875 | 824 | handler->sigfd = setup_signal_fd(&handler->oldmask); |
59eb99ba | 825 | if (handler->sigfd < 0) { |
408da065 | 826 | ERROR("Failed to setup SIGCHLD fd handler."); |
b5159817 DE |
827 | goto out_delete_tty; |
828 | } | |
2170b263 | 829 | TRACE("Set up signal fd"); |
b5159817 | 830 | |
408da065 | 831 | /* Do this after setting up signals since it might unblock SIGWINCH. */ |
564e31c4 | 832 | ret = lxc_terminal_setup(conf); |
4d1ffb0a CB |
833 | if (ret < 0) { |
834 | ERROR("Failed to create console"); | |
b5159817 | 835 | goto out_restore_sigmask; |
b0a33c1e | 836 | } |
4d1ffb0a | 837 | TRACE("Created console"); |
b0a33c1e | 838 | |
2f835b4b | 839 | ret = lxc_terminal_map_ids(conf, &conf->console); |
7cfeddd7 | 840 | if (ret < 0) { |
2170b263 | 841 | ERROR("Failed to chown console"); |
c4d10a05 SH |
842 | goto out_restore_sigmask; |
843 | } | |
2170b263 | 844 | TRACE("Chowned console"); |
c4d10a05 | 845 | |
2170b263 | 846 | INFO("Container \"%s\" is initialized", name); |
aa460476 | 847 | return 0; |
59eb99ba | 848 | |
b5159817 DE |
849 | out_restore_sigmask: |
850 | sigprocmask(SIG_SETMASK, &handler->oldmask, NULL); | |
59eb99ba | 851 | out_delete_tty: |
0e4be3cf | 852 | lxc_delete_tty(&conf->ttys); |
59eb99ba | 853 | out_aborting: |
25c2aca5 | 854 | lxc_set_state(name, handler, ABORTING); |
d2e30e99 DE |
855 | out_close_maincmd_fd: |
856 | close(conf->maincmd_fd); | |
857 | conf->maincmd_fd = -1; | |
aa460476 | 858 | return -1; |
59eb99ba DL |
859 | } |
860 | ||
3b72c4a0 | 861 | void lxc_fini(const char *name, struct lxc_handler *handler) |
59eb99ba | 862 | { |
c097467f | 863 | int i, ret; |
18b3b9c1 | 864 | pid_t self; |
dbc9832d | 865 | struct lxc_list *cur, *next; |
dbc9832d | 866 | char *namespaces[LXC_NS_MAX + 1]; |
b3286b62 | 867 | size_t namespace_count = 0; |
b6b2b194 | 868 | |
408da065 CB |
869 | /* The STOPPING state is there for future cleanup code which can take |
870 | * awhile. | |
59eb99ba | 871 | */ |
25c2aca5 | 872 | lxc_set_state(name, handler, STOPPING); |
b6b2b194 | 873 | |
0059379f | 874 | self = lxc_raw_getpid(); |
b3286b62 | 875 | for (i = 0; i < LXC_NS_MAX; i++) { |
18b3b9c1 CB |
876 | if (handler->nsfd[i] < 0) |
877 | continue; | |
878 | ||
879 | if (handler->conf->hooks_version == 0) | |
c097467f | 880 | ret = asprintf(&namespaces[namespace_count], |
18b3b9c1 CB |
881 | "%s:/proc/%d/fd/%d", ns_info[i].proc_name, |
882 | self, handler->nsfd[i]); | |
883 | else | |
c097467f | 884 | ret = asprintf(&namespaces[namespace_count], |
18b3b9c1 | 885 | "/proc/%d/fd/%d", self, handler->nsfd[i]); |
c097467f CB |
886 | if (ret == -1) { |
887 | SYSERROR("Failed to allocate memory"); | |
18b3b9c1 | 888 | break; |
b3286b62 | 889 | } |
18b3b9c1 CB |
890 | |
891 | if (handler->conf->hooks_version == 0) { | |
892 | namespace_count++; | |
893 | continue; | |
894 | } | |
895 | ||
c097467f CB |
896 | ret = setenv(ns_info[i].env_name, namespaces[namespace_count], 1); |
897 | if (ret < 0) | |
18b3b9c1 CB |
898 | SYSERROR("Failed to set environment variable %s=%s", |
899 | ns_info[i].env_name, namespaces[namespace_count]); | |
900 | else | |
901 | TRACE("Set environment variable %s=%s", | |
902 | ns_info[i].env_name, namespaces[namespace_count]); | |
903 | ||
904 | namespace_count++; | |
b3286b62 WB |
905 | } |
906 | namespaces[namespace_count] = NULL; | |
c154af98 | 907 | |
c097467f CB |
908 | if (handler->conf->reboot) { |
909 | ret = setenv("LXC_TARGET", "reboot", 1); | |
910 | if (ret < 0) | |
911 | SYSERROR("Failed to set environment variable: " | |
912 | "LXC_TARGET=reboot"); | |
913 | } | |
408da065 | 914 | |
c097467f CB |
915 | if (!handler->conf->reboot) { |
916 | ret = setenv("LXC_TARGET", "stop", 1); | |
917 | if (ret < 0) | |
918 | SYSERROR("Failed to set environment variable: " | |
919 | "LXC_TARGET=stop"); | |
920 | } | |
c154af98 | 921 | |
18b3b9c1 | 922 | if (handler->conf->hooks_version == 0) |
c097467f | 923 | ret = run_lxc_hooks(name, "stop", handler->conf, namespaces); |
18b3b9c1 | 924 | else |
c097467f CB |
925 | ret = run_lxc_hooks(name, "stop", handler->conf, NULL); |
926 | if (ret < 0) | |
927 | ERROR("Failed to run \"lxc.hook.stop\" hook"); | |
c154af98 | 928 | |
b3286b62 WB |
929 | while (namespace_count--) |
930 | free(namespaces[namespace_count]); | |
28d9e29e | 931 | |
4288b79f CB |
932 | cgroup_destroy(handler); |
933 | ||
d39b10eb | 934 | if (handler->conf->reboot == 0) { |
f893d898 CB |
935 | /* For all new state clients simply close the command socket. |
936 | * This will inform all state clients that the container is | |
937 | * STOPPED and also prevents a race between a open()/close() on | |
938 | * the command socket causing a new process to get ECONNREFUSED | |
939 | * because we haven't yet closed the command socket. | |
940 | */ | |
d39b10eb CB |
941 | close(handler->conf->maincmd_fd); |
942 | handler->conf->maincmd_fd = -1; | |
c3184275 CB |
943 | TRACE("Closed command socket"); |
944 | ||
945 | /* This function will try to connect to the legacy lxc-monitord | |
946 | * state server and only exists for backwards compatibility. | |
947 | */ | |
948 | lxc_monitor_send_state(name, STOPPED, handler->lxcpath); | |
949 | ||
950 | /* The command socket is closed so no one can acces the command | |
951 | * socket anymore so there's no need to lock it. | |
952 | */ | |
953 | handler->state = STOPPED; | |
954 | TRACE("Set container state to \"STOPPED\""); | |
f893d898 CB |
955 | } else { |
956 | lxc_set_state(name, handler, STOPPED); | |
d39b10eb | 957 | } |
4288b79f | 958 | |
46800e77 CB |
959 | ret = run_lxc_hooks(name, "post-stop", handler->conf, NULL); |
960 | if (ret < 0) { | |
c097467f | 961 | ERROR("Failed to run lxc.hook.post-stop for container \"%s\"", name); |
f3787121 | 962 | if (handler->conf->reboot) { |
c097467f | 963 | WARN("Container will be stopped instead of rebooted"); |
f3787121 | 964 | handler->conf->reboot = 0; |
c097467f CB |
965 | |
966 | ret = setenv("LXC_TARGET", "stop", 1); | |
967 | if (ret < 0) | |
968 | WARN("Failed to set environment variable: " | |
969 | "LXC_TARGET=stop"); | |
f3787121 CB |
970 | } |
971 | } | |
26ddeedd | 972 | |
408da065 | 973 | /* Reset mask set by setup_signal_fd. */ |
c097467f CB |
974 | ret = sigprocmask(SIG_SETMASK, &handler->oldmask, NULL); |
975 | if (ret < 0) | |
976 | WARN("%s - Failed to restore signal mask", strerror(errno)); | |
8f64a3f6 | 977 | |
2aac071b | 978 | lxc_terminal_delete(&handler->conf->console); |
0e4be3cf | 979 | lxc_delete_tty(&handler->conf->ttys); |
dbc9832d | 980 | |
dbc9832d CB |
981 | /* The command socket is now closed, no more state clients can register |
982 | * themselves from now on. So free the list of state clients. | |
983 | */ | |
d39b10eb CB |
984 | lxc_list_for_each_safe(cur, &handler->conf->state_clients, next) { |
985 | struct lxc_state_client *client = cur->elem; | |
986 | ||
987 | /* Keep state clients that want to be notified about reboots. */ | |
988 | if ((handler->conf->reboot > 0) && (client->states[RUNNING] == 2)) | |
989 | continue; | |
990 | ||
dbc9832d | 991 | /* close state client socket */ |
dbc9832d | 992 | lxc_list_del(cur); |
c097467f | 993 | close(client->clientfd); |
dbc9832d CB |
994 | free(cur->elem); |
995 | free(cur); | |
996 | } | |
997 | ||
2c5f2ede | 998 | if (handler->conf->ephemeral == 1 && handler->conf->reboot != 1) |
28272964 | 999 | lxc_destroy_container_on_signal(handler, name); |
2c5f2ede | 1000 | |
1cc8bd4b | 1001 | lxc_free_handler(handler); |
59eb99ba DL |
1002 | } |
1003 | ||
735f2c6e | 1004 | void lxc_abort(const char *name, struct lxc_handler *handler) |
59eb99ba | 1005 | { |
73e608b2 SH |
1006 | int ret, status; |
1007 | ||
25c2aca5 | 1008 | lxc_set_state(name, handler, ABORTING); |
0e4f9d51 CB |
1009 | |
1010 | if (handler->pid > 0) { | |
1011 | ret = kill(handler->pid, SIGKILL); | |
1012 | if (ret < 0) | |
1013 | SYSERROR("Failed to send SIGKILL to %d", handler->pid); | |
1014 | } | |
1015 | ||
408da065 CB |
1016 | while ((ret = waitpid(-1, &status, 0)) > 0) { |
1017 | ; | |
1018 | } | |
59eb99ba DL |
1019 | } |
1020 | ||
ffe1e01a | 1021 | static int do_start(void *data) |
50e98013 | 1022 | { |
8bf3abfb | 1023 | int ret; |
7a55c157 | 1024 | char path[PATH_MAX]; |
928b1f04 YT |
1025 | bool have_cap_setgid; |
1026 | uid_t new_uid; | |
1027 | gid_t new_gid; | |
e96a536c | 1028 | struct lxc_list *iterator; |
5af9369b | 1029 | int devnull_fd = -1; |
e96a536c | 1030 | struct lxc_handler *handler = data; |
50e98013 | 1031 | |
18225d19 | 1032 | lxc_sync_fini_parent(handler); |
50e98013 | 1033 | |
408da065 CB |
1034 | /* This prctl must be before the synchro, so if the parent dies before |
1035 | * we set the parent death signal, we will detect its death with the | |
1036 | * synchro right after, otherwise we have a window where the parent can | |
1037 | * exit before we set the pdeath signal leading to a unsupervized | |
1038 | * container. | |
743ecd2e | 1039 | */ |
18225d19 | 1040 | ret = lxc_set_death_signal(SIGKILL); |
912314fc CB |
1041 | if (ret < 0) { |
1042 | SYSERROR("Failed to set PR_SET_PDEATHSIG to SIGKILL"); | |
18225d19 | 1043 | goto out_warn_father; |
743ecd2e DL |
1044 | } |
1045 | ||
18225d19 CB |
1046 | ret = sigprocmask(SIG_SETMASK, &handler->oldmask, NULL); |
1047 | if (ret < 0) { | |
1048 | SYSERROR("Failed to set signal mask"); | |
1049 | goto out_warn_father; | |
1050 | } | |
50e98013 | 1051 | |
408da065 | 1052 | /* Don't leak the pinfd to the container. */ |
d39b10eb | 1053 | if (handler->pinfd >= 0) |
0d03360a | 1054 | close(handler->pinfd); |
2b0e17e4 | 1055 | |
18225d19 CB |
1056 | ret = lxc_sync_wait_parent(handler, LXC_SYNC_STARTUP); |
1057 | if (ret < 0) | |
1058 | goto out_warn_father; | |
5b1e83cb | 1059 | |
408da065 CB |
1060 | /* Unshare CLONE_NEWNET after CLONE_NEWUSER. See |
1061 | * https://github.com/lxc/lxd/issues/1978. | |
1062 | */ | |
becad0ec | 1063 | if ((handler->ns_clone_flags & (CLONE_NEWNET | CLONE_NEWUSER)) == |
408da065 | 1064 | (CLONE_NEWNET | CLONE_NEWUSER)) { |
5b1e83cb SH |
1065 | ret = unshare(CLONE_NEWNET); |
1066 | if (ret < 0) { | |
e96a536c | 1067 | SYSERROR("Failed to unshare CLONE_NEWNET"); |
5b1e83cb SH |
1068 | goto out_warn_father; |
1069 | } | |
e96a536c | 1070 | INFO("Unshared CLONE_NEWNET"); |
5b1e83cb SH |
1071 | } |
1072 | ||
408da065 CB |
1073 | /* Tell the parent task it can begin to configure the container and wait |
1074 | * for it to finish. | |
3c22086f | 1075 | */ |
e96a536c CB |
1076 | ret = lxc_sync_barrier_parent(handler, LXC_SYNC_CONFIGURE); |
1077 | if (ret < 0) | |
9d7f9e52 | 1078 | return -1; |
50e98013 | 1079 | |
e96a536c CB |
1080 | ret = lxc_network_recv_veth_names_from_parent(handler); |
1081 | if (ret < 0) { | |
7ab1ba02 | 1082 | ERROR("Failed to receive veth names from parent"); |
658979c5 | 1083 | goto out_warn_father; |
7ab1ba02 | 1084 | } |
658979c5 | 1085 | |
408da065 | 1086 | /* If we are in a new user namespace, become root there to have |
d08f8d2f | 1087 | * privilege over our namespace. |
f6d3e3e4 SH |
1088 | */ |
1089 | if (!lxc_list_empty(&handler->conf->id_map)) { | |
4160c3a0 CB |
1090 | uid_t nsuid = (handler->conf->root_nsuid_map != NULL) |
1091 | ? 0 | |
1092 | : handler->conf->init_uid; | |
1093 | gid_t nsgid = (handler->conf->root_nsgid_map != NULL) | |
1094 | ? 0 | |
1095 | : handler->conf->init_gid; | |
1096 | ||
1097 | ret = lxc_switch_uid_gid(nsuid, nsgid); | |
4b826b1f | 1098 | if (ret < 0) |
f6d3e3e4 | 1099 | goto out_warn_father; |
d08f8d2f CB |
1100 | |
1101 | /* Drop groups only after we switched to a valid gid in the new | |
1102 | * user namespace. | |
1103 | */ | |
4b826b1f | 1104 | ret = lxc_setgroups(0, NULL); |
a19b974f | 1105 | if (ret < 0 && (handler->am_root || errno != EPERM)) |
c476bdce | 1106 | goto out_warn_father; |
4b826b1f | 1107 | |
d7883725 CB |
1108 | ret = prctl(PR_SET_DUMPABLE, 1, 0, 0, 0); |
1109 | if (ret < 0) | |
1110 | goto out_warn_father; | |
912314fc CB |
1111 | |
1112 | /* set{g,u}id() clears deathsignal */ | |
18225d19 | 1113 | ret = lxc_set_death_signal(SIGKILL); |
912314fc CB |
1114 | if (ret < 0) { |
1115 | SYSERROR("Failed to set PR_SET_PDEATHSIG to SIGKILL"); | |
1116 | goto out_warn_father; | |
1117 | } | |
f6d3e3e4 SH |
1118 | } |
1119 | ||
e96a536c CB |
1120 | ret = access(handler->lxcpath, X_OK); |
1121 | if (ret != 0) { | |
c8154066 SH |
1122 | print_top_failing_dir(handler->lxcpath); |
1123 | goto out_warn_father; | |
1124 | } | |
1125 | ||
e96a536c CB |
1126 | ret = snprintf(path, sizeof(path), "%s/dev/null", |
1127 | handler->conf->rootfs.mount); | |
408da065 | 1128 | if (ret < 0 || ret >= sizeof(path)) |
7a55c157 | 1129 | goto out_warn_father; |
7a55c157 TA |
1130 | |
1131 | /* In order to checkpoint restore, we need to have everything in the | |
1132 | * same mount namespace. However, some containers may not have a | |
1133 | * reasonable /dev (in particular, they may not have /dev/null), so we | |
1134 | * can't set init's std fds to /dev/null by opening it from inside the | |
1135 | * container. | |
1136 | * | |
1137 | * If that's the case, fall back to using the host's /dev/null. This | |
1138 | * means that migration won't work, but at least we won't spew output | |
1139 | * where it isn't wanted. | |
1140 | */ | |
e96a536c CB |
1141 | if (handler->backgrounded && !handler->conf->autodev) { |
1142 | ret = access(path, F_OK); | |
1143 | if (ret != 0) { | |
1144 | devnull_fd = open_devnull(); | |
c44de748 | 1145 | |
e96a536c CB |
1146 | if (devnull_fd < 0) |
1147 | goto out_warn_father; | |
1148 | WARN("Using /dev/null from the host for container " | |
1149 | "init's standard file descriptors. Migration will " | |
1150 | "not work"); | |
1151 | } | |
c44de748 AM |
1152 | } |
1153 | ||
408da065 | 1154 | /* Ask father to setup cgroups and wait for him to finish. */ |
e96a536c CB |
1155 | ret = lxc_sync_barrier_parent(handler, LXC_SYNC_CGROUP); |
1156 | if (ret < 0) | |
c44de748 | 1157 | goto out_error; |
544a48a0 | 1158 | |
deefdf8a CB |
1159 | /* Unshare cgroup namespace after we have setup our cgroups. If we do it |
1160 | * earlier we end up with a wrong view of /proc/self/cgroup. For | |
1161 | * example, assume we unshare(CLONE_NEWCGROUP) first, and then create | |
1162 | * the cgroup for the container, say /sys/fs/cgroup/cpuset/lxc/c, then | |
1163 | * /proc/self/cgroup would show us: | |
1164 | * | |
1165 | * 8:cpuset:/lxc/c | |
1166 | * | |
1167 | * whereas it should actually show | |
1168 | * | |
1169 | * 8:cpuset:/ | |
1170 | */ | |
becad0ec | 1171 | if (handler->ns_clone_flags & CLONE_NEWCGROUP) { |
e96a536c CB |
1172 | ret = unshare(CLONE_NEWCGROUP); |
1173 | if (ret < 0) { | |
1174 | INFO("Failed to unshare CLONE_NEWCGROUP"); | |
deefdf8a CB |
1175 | goto out_warn_father; |
1176 | } | |
e96a536c | 1177 | INFO("Unshared CLONE_NEWCGROUP"); |
deefdf8a CB |
1178 | } |
1179 | ||
149857af CB |
1180 | /* Add the requested environment variables to the current environment to |
1181 | * allow them to be used by the various hooks, such as the start hook | |
1182 | * above. | |
98ff08ca | 1183 | */ |
98ff08ca | 1184 | lxc_list_for_each(iterator, &handler->conf->environment) { |
e96a536c CB |
1185 | ret = putenv((char *)iterator->elem); |
1186 | if (ret < 0) { | |
1187 | SYSERROR("Failed to set environment variable: %s", | |
1188 | (char *)iterator->elem); | |
98ff08ca CB |
1189 | goto out_warn_father; |
1190 | } | |
1191 | } | |
1192 | ||
f4152036 | 1193 | /* Setup the container, ip, names, utsname, ... */ |
3b988b33 | 1194 | ret = lxc_setup(handler); |
7729f8e5 | 1195 | close(handler->data_sock[1]); |
8bf3abfb | 1196 | close(handler->data_sock[0]); |
7729f8e5 | 1197 | if (ret < 0) { |
e96a536c | 1198 | ERROR("Failed to setup container \"%s\"", handler->name); |
f4152036 CB |
1199 | goto out_warn_father; |
1200 | } | |
1201 | ||
408da065 | 1202 | /* Set the label to change to when we exec(2) the container's init. */ |
e96a536c CB |
1203 | ret = lsm_process_label_set(NULL, handler->conf, 1, 1); |
1204 | if (ret < 0) | |
e075f5d9 | 1205 | goto out_warn_father; |
5112cd70 | 1206 | |
029cdff5 | 1207 | /* Set PR_SET_NO_NEW_PRIVS after we changed the lsm label. If we do it |
408da065 CB |
1208 | * before we aren't allowed anymore. |
1209 | */ | |
029cdff5 | 1210 | if (handler->conf->no_new_privs) { |
e96a536c CB |
1211 | ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); |
1212 | if (ret < 0) { | |
1213 | SYSERROR("Could not set PR_SET_NO_NEW_PRIVS to block " | |
1214 | "execve() gainable privileges"); | |
029cdff5 CB |
1215 | goto out_warn_father; |
1216 | } | |
e96a536c CB |
1217 | DEBUG("Set PR_SET_NO_NEW_PRIVS to block execve() gainable " |
1218 | "privileges"); | |
029cdff5 CB |
1219 | } |
1220 | ||
0d9acb99 DE |
1221 | /* Some init's such as busybox will set sane tty settings on stdin, |
1222 | * stdout, stderr which it thinks is the console. We already set them | |
1223 | * the way we wanted on the real terminal, and we want init to do its | |
564e31c4 | 1224 | * setup on its console ie. the pty allocated in lxc_terminal_setup() so |
408da065 | 1225 | * make sure that that pty is stdin,stdout,stderr. |
0d9acb99 | 1226 | */ |
5d113f65 CB |
1227 | if (handler->conf->console.slave >= 0) { |
1228 | if (handler->backgrounded || handler->conf->is_execute == 0) | |
1229 | ret = set_stdfds(handler->conf->console.slave); | |
1230 | else | |
ae6d3913 | 1231 | ret = lxc_terminal_set_stdfds(handler->conf->console.slave); |
5d113f65 | 1232 | if (ret < 0) { |
c5b93afb | 1233 | ERROR("Failed to redirect std{in,out,err} to pty file " |
e96a536c | 1234 | "descriptor %d", handler->conf->console.slave); |
c5b93afb LF |
1235 | goto out_warn_father; |
1236 | } | |
5d113f65 | 1237 | } |
0d9acb99 | 1238 | |
408da065 | 1239 | /* If we mounted a temporary proc, then unmount it now. */ |
5112cd70 | 1240 | tmp_proc_unmount(handler->conf); |
e075f5d9 | 1241 | |
e96a536c CB |
1242 | ret = lxc_seccomp_load(handler->conf); |
1243 | if (ret < 0) | |
8f2c3a70 SH |
1244 | goto out_warn_father; |
1245 | ||
e96a536c CB |
1246 | ret = run_lxc_hooks(handler->name, "start", handler->conf, NULL); |
1247 | if (ret < 0) { | |
1248 | ERROR("Failed to run lxc.hook.start for container \"%s\"", | |
1249 | handler->name); | |
773fb9ca SH |
1250 | goto out_warn_father; |
1251 | } | |
fc25b815 | 1252 | |
773fb9ca | 1253 | close(handler->sigfd); |
26ddeedd | 1254 | |
7a55c157 TA |
1255 | if (devnull_fd < 0) { |
1256 | devnull_fd = open_devnull(); | |
7a55c157 TA |
1257 | if (devnull_fd < 0) |
1258 | goto out_warn_father; | |
1259 | } | |
1260 | ||
e96a536c CB |
1261 | if (handler->conf->console.slave < 0 && handler->backgrounded) { |
1262 | ret = set_stdfds(devnull_fd); | |
1263 | if (ret < 0) { | |
1264 | ERROR("Failed to redirect std{in,out,err} to \"/dev/null\""); | |
c5b93afb LF |
1265 | goto out_warn_father; |
1266 | } | |
e96a536c | 1267 | } |
507cee36 | 1268 | |
c44de748 AM |
1269 | if (devnull_fd >= 0) { |
1270 | close(devnull_fd); | |
1271 | devnull_fd = -1; | |
1272 | } | |
1273 | ||
8c9a7665 TA |
1274 | setsid(); |
1275 | ||
e96a536c CB |
1276 | if (handler->conf->init_cwd) { |
1277 | ret = chdir(handler->conf->init_cwd); | |
1278 | if (ret < 0) { | |
1279 | SYSERROR("Could not change directory to \"%s\"", | |
1280 | handler->conf->init_cwd); | |
1281 | goto out_warn_father; | |
1282 | } | |
3c491553 L |
1283 | } |
1284 | ||
e96a536c CB |
1285 | ret = lxc_sync_barrier_parent(handler, LXC_SYNC_CGROUP_LIMITS); |
1286 | if (ret < 0) | |
f4152036 CB |
1287 | goto out_warn_father; |
1288 | ||
149857af CB |
1289 | /* Reset the environment variables the user requested in a clear |
1290 | * environment. | |
1291 | */ | |
e96a536c CB |
1292 | ret = clearenv(); |
1293 | /* Don't error out though. */ | |
1294 | if (ret < 0) | |
149857af | 1295 | SYSERROR("Failed to clear environment."); |
149857af CB |
1296 | |
1297 | lxc_list_for_each(iterator, &handler->conf->environment) { | |
e96a536c CB |
1298 | ret = putenv((char *)iterator->elem); |
1299 | if (ret < 0) { | |
1300 | SYSERROR("Failed to set environment variable: %s", | |
1301 | (char *)iterator->elem); | |
149857af CB |
1302 | goto out_warn_father; |
1303 | } | |
1304 | } | |
1305 | ||
e96a536c CB |
1306 | ret = putenv("container=lxc"); |
1307 | if (ret < 0) { | |
1308 | SYSERROR("Failed to set environment variable: container=lxc"); | |
98ff08ca CB |
1309 | goto out_warn_father; |
1310 | } | |
1311 | ||
1312 | if (handler->conf->pty_names) { | |
e96a536c CB |
1313 | ret = putenv(handler->conf->pty_names); |
1314 | if (ret < 0) { | |
1315 | SYSERROR("Failed to set environment variable for container ptys"); | |
98ff08ca CB |
1316 | goto out_warn_father; |
1317 | } | |
1318 | } | |
1319 | ||
76bdf299 CB |
1320 | /* The container has been setup. We can now switch to an unprivileged |
1321 | * uid/gid. | |
1322 | */ | |
928b1f04 YT |
1323 | new_uid = handler->conf->init_uid; |
1324 | new_gid = handler->conf->init_gid; | |
76bdf299 | 1325 | |
e96a536c CB |
1326 | /* If we are in a new user namespace we already dropped all groups when |
1327 | * we switched to root in the new user namespace further above. Only | |
1328 | * drop groups if we can, so ensure that we have necessary privilege. | |
928b1f04 YT |
1329 | */ |
1330 | #if HAVE_LIBCAP | |
1331 | have_cap_setgid = lxc_proc_cap_is_set(CAP_SETGID, CAP_EFFECTIVE); | |
1332 | #else | |
1333 | have_cap_setgid = false; | |
1334 | #endif | |
1335 | if (lxc_list_empty(&handler->conf->id_map) && have_cap_setgid) { | |
e96a536c CB |
1336 | ret = lxc_setgroups(0, NULL); |
1337 | if (ret < 0) | |
76bdf299 CB |
1338 | goto out_warn_father; |
1339 | } | |
1340 | ||
e96a536c CB |
1341 | ret = lxc_switch_uid_gid(new_uid, new_gid); |
1342 | if (ret < 0) | |
928b1f04 YT |
1343 | goto out_warn_father; |
1344 | ||
408da065 CB |
1345 | /* After this call, we are in error because this ops should not return |
1346 | * as it execs. | |
1347 | */ | |
c4ea60df | 1348 | handler->ops->start(handler, handler->data); |
50e98013 DL |
1349 | |
1350 | out_warn_father: | |
408da065 CB |
1351 | /* We want the parent to know something went wrong, so we return a |
1352 | * special error code. | |
1353 | */ | |
d1ccb562 | 1354 | lxc_sync_wake_parent(handler, LXC_SYNC_ERROR); |
c44de748 AM |
1355 | |
1356 | out_error: | |
1357 | if (devnull_fd >= 0) | |
1358 | close(devnull_fd); | |
1359 | ||
50e98013 DL |
1360 | return -1; |
1361 | } | |
1362 | ||
ae467c54 | 1363 | static int lxc_recv_ttys_from_child(struct lxc_handler *handler) |
e8bd4e43 | 1364 | { |
ae467c54 | 1365 | int i; |
2520facd | 1366 | struct lxc_terminal_info *tty; |
ae467c54 | 1367 | int ret = -1; |
c6012571 | 1368 | int sock = handler->data_sock[1]; |
e8bd4e43 | 1369 | struct lxc_conf *conf = handler->conf; |
0e4be3cf | 1370 | struct lxc_tty_info *ttys = &conf->ttys; |
e8bd4e43 SH |
1371 | |
1372 | if (!conf->tty) | |
1373 | return 0; | |
1374 | ||
0e4be3cf CB |
1375 | ttys->tty = malloc(sizeof(*ttys->tty) * conf->tty); |
1376 | if (!ttys->tty) | |
e8bd4e43 | 1377 | return -1; |
e8bd4e43 | 1378 | |
672c1e58 CB |
1379 | for (i = 0; i < conf->tty; i++) { |
1380 | int ttyfds[2]; | |
1381 | ||
1382 | ret = lxc_abstract_unix_recv_fds(sock, ttyfds, 2, NULL, 0); | |
1383 | if (ret < 0) | |
1384 | break; | |
ae467c54 | 1385 | |
0e4be3cf | 1386 | tty = &ttys->tty[i]; |
2520facd CB |
1387 | tty->busy = 0; |
1388 | tty->master = ttyfds[0]; | |
1389 | tty->slave = ttyfds[1]; | |
672c1e58 | 1390 | TRACE("Received pty with master fd %d and slave fd %d from " |
2520facd | 1391 | "parent", tty->master, tty->slave); |
e8bd4e43 | 1392 | } |
ae467c54 | 1393 | if (ret < 0) |
672c1e58 | 1394 | ERROR("Failed to receive %d ttys from child: %s", conf->tty, |
ae467c54 CB |
1395 | strerror(errno)); |
1396 | else | |
672c1e58 CB |
1397 | TRACE("Received %d ttys from child", conf->tty); |
1398 | ||
0e4be3cf | 1399 | ttys->nbtty = conf->tty; |
ae467c54 CB |
1400 | |
1401 | return ret; | |
e8bd4e43 SH |
1402 | } |
1403 | ||
5af9369b | 1404 | int resolve_clone_flags(struct lxc_handler *handler) |
f813849c | 1405 | { |
8bc8c715 CB |
1406 | int i; |
1407 | struct lxc_conf *conf = handler->conf; | |
f813849c | 1408 | |
8bc8c715 CB |
1409 | for (i = 0; i < LXC_NS_MAX; i++) { |
1410 | if (conf->ns_keep != 0) { | |
1411 | if ((conf->ns_keep & ns_info[i].clone_flag) == 0) | |
becad0ec | 1412 | handler->ns_clone_flags |= ns_info[i].clone_flag; |
8bc8c715 CB |
1413 | } else if (conf->ns_clone != 0) { |
1414 | if ((conf->ns_clone & ns_info[i].clone_flag) > 0) | |
becad0ec | 1415 | handler->ns_clone_flags |= ns_info[i].clone_flag; |
8bc8c715 CB |
1416 | } else { |
1417 | if (i == LXC_NS_USER && lxc_list_empty(&handler->conf->id_map)) | |
1418 | continue; | |
1419 | ||
1420 | if (i == LXC_NS_NET && lxc_requests_empty_network(handler)) | |
1421 | continue; | |
1422 | ||
7bd05339 CB |
1423 | if (i == LXC_NS_CGROUP && !cgns_supported()) |
1424 | continue; | |
1425 | ||
becad0ec | 1426 | handler->ns_clone_flags |= ns_info[i].clone_flag; |
8bc8c715 | 1427 | } |
03df7ab5 | 1428 | |
8bc8c715 CB |
1429 | if (!conf->ns_share[i]) |
1430 | continue; | |
5af9369b | 1431 | |
becad0ec | 1432 | handler->ns_clone_flags &= ~ns_info[i].clone_flag; |
8bc8c715 | 1433 | TRACE("Sharing %s namespace", ns_info[i].proc_name); |
5af9369b CB |
1434 | } |
1435 | ||
1436 | return 0; | |
f813849c TA |
1437 | } |
1438 | ||
8deca6c9 CB |
1439 | /* Note that this function is used with clone(CLONE_VM). Some glibc versions |
1440 | * used to reset the pid/tid to -1 when CLONE_VM was used without CLONE_THREAD. | |
1441 | * But since the memory between parent and child is shared on CLONE_VM this | |
1442 | * would invalidate the getpid() cache that glibc used to maintain and so | |
1443 | * getpid() in the child would return the parent's pid. This is all fixed in | |
1444 | * newer glibc versions where the getpid() cache is removed and the pid/tid is | |
1445 | * not reset anymore. | |
1446 | * However, if for whatever reason you - dear commiter - somehow need to get the | |
1447 | * pid of the dummy intermediate process for do_share_ns() you need to call | |
0059379f CB |
1448 | * lxc_raw_getpid(). The next lxc_raw_clone() call does not employ CLONE_VM and |
1449 | * will be fine. | |
8deca6c9 CB |
1450 | */ |
1451 | static inline int do_share_ns(void *arg) | |
fa3a5b22 | 1452 | { |
8deca6c9 | 1453 | int i, flags, ret; |
fa3a5b22 CB |
1454 | struct lxc_handler *handler = arg; |
1455 | ||
8deca6c9 CB |
1456 | for (i = 0; i < LXC_NS_MAX; i++) { |
1457 | if (handler->nsfd[i] < 0) | |
1458 | continue; | |
fa3a5b22 | 1459 | |
8deca6c9 | 1460 | ret = setns(handler->nsfd[i], 0); |
fa3a5b22 | 1461 | if (ret < 0) |
8deca6c9 | 1462 | return -1; |
fa3a5b22 | 1463 | |
8deca6c9 | 1464 | DEBUG("Inherited %s namespace", ns_info[i].proc_name); |
fa3a5b22 CB |
1465 | } |
1466 | ||
becad0ec | 1467 | flags = handler->ns_on_clone_flags; |
8deca6c9 | 1468 | flags |= CLONE_PARENT; |
ac2ba696 | 1469 | handler->pid = lxc_raw_clone_cb(do_start, handler, flags); |
8deca6c9 | 1470 | if (handler->pid < 0) |
fa3a5b22 CB |
1471 | return -1; |
1472 | ||
8deca6c9 | 1473 | return 0; |
fa3a5b22 CB |
1474 | } |
1475 | ||
480588e6 CB |
1476 | /* lxc_spawn() performs crucial setup tasks and clone()s the new process which |
1477 | * exec()s the requested container binary. | |
1478 | * Note that lxc_spawn() runs in the parent namespaces. Any operations performed | |
1479 | * right here should be double checked if they'd pose a security risk. (For | |
1480 | * example, any {u}mount() operations performed here will be reflected on the | |
1481 | * host!) | |
1482 | */ | |
74a3920a | 1483 | static int lxc_spawn(struct lxc_handler *handler) |
59eb99ba | 1484 | { |
8deca6c9 | 1485 | int i, ret; |
08dd2805 | 1486 | char pidstr[20]; |
57927bf2 CB |
1487 | bool wants_to_map_ids; |
1488 | struct lxc_list *id_map; | |
6e5fc7a5 | 1489 | const char *name = handler->name; |
28d9e29e | 1490 | const char *lxcpath = handler->lxcpath; |
8deca6c9 | 1491 | bool cgroups_connected = false, share_ns = false; |
28d9e29e | 1492 | struct lxc_conf *conf = handler->conf; |
9f30a190 | 1493 | |
28d9e29e | 1494 | id_map = &conf->id_map; |
57927bf2 | 1495 | wants_to_map_ids = !lxc_list_empty(id_map); |
2f2623ec | 1496 | |
28d9e29e | 1497 | for (i = 0; i < LXC_NS_MAX; i++) { |
b074bbf1 | 1498 | if (!conf->ns_share[i]) |
28d9e29e CB |
1499 | continue; |
1500 | ||
b074bbf1 | 1501 | handler->nsfd[i] = lxc_inherit_namespace(conf->ns_share[i], lxcpath, ns_info[i].proc_name); |
28d9e29e CB |
1502 | if (handler->nsfd[i] < 0) |
1503 | return -1; | |
fa3a5b22 | 1504 | |
8deca6c9 | 1505 | share_ns = true; |
28d9e29e | 1506 | } |
50e98013 | 1507 | |
cfc62c60 CB |
1508 | ret = lxc_sync_init(handler); |
1509 | if (ret < 0) | |
9d7f9e52 | 1510 | return -1; |
0ad19a3f | 1511 | |
b9f522c5 | 1512 | ret = socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, |
790255cf | 1513 | handler->data_sock); |
c6012571 | 1514 | if (ret < 0) { |
e8bd4e43 SH |
1515 | lxc_sync_fini(handler); |
1516 | return -1; | |
1517 | } | |
1518 | ||
5af9369b CB |
1519 | ret = resolve_clone_flags(handler); |
1520 | if (ret < 0) { | |
1521 | lxc_sync_fini(handler); | |
1522 | return -1; | |
1523 | } | |
9f30a190 | 1524 | |
becad0ec | 1525 | if (handler->ns_clone_flags & CLONE_NEWNET) { |
28d9e29e | 1526 | if (!lxc_list_empty(&conf->network)) { |
26b797f3 | 1527 | |
9f30a190 MM |
1528 | /* Find gateway addresses from the link device, which is |
1529 | * no longer accessible inside the container. Do this | |
1530 | * before creating network interfaces, since goto | |
408da065 CB |
1531 | * out_delete_net does not work before lxc_clone. |
1532 | */ | |
cfc62c60 CB |
1533 | ret = lxc_find_gateway_addresses(handler); |
1534 | if (ret < 0) { | |
1535 | ERROR("Failed to find gateway addresses"); | |
9f30a190 MM |
1536 | lxc_sync_fini(handler); |
1537 | return -1; | |
1538 | } | |
1539 | ||
408da065 CB |
1540 | /* That should be done before the clone because we will |
1541 | * fill the netdev index and use them in the child. | |
9f30a190 | 1542 | */ |
cfc62c60 CB |
1543 | ret = lxc_create_network_priv(handler); |
1544 | if (ret < 0) { | |
1545 | ERROR("Failed to create the network"); | |
9f30a190 MM |
1546 | lxc_sync_fini(handler); |
1547 | return -1; | |
1548 | } | |
19a26f82 | 1549 | } |
82d5ae15 DL |
1550 | } |
1551 | ||
d4ef7c50 | 1552 | if (!cgroup_init(handler)) { |
cfc62c60 | 1553 | ERROR("Failed initializing cgroup support"); |
33ad9f1a CS |
1554 | goto out_delete_net; |
1555 | } | |
1556 | ||
7e4dfe0b SH |
1557 | cgroups_connected = true; |
1558 | ||
d4ef7c50 | 1559 | if (!cgroup_create(handler)) { |
cfc62c60 | 1560 | ERROR("Failed creating cgroups"); |
47d8fb3b CS |
1561 | goto out_delete_net; |
1562 | } | |
1563 | ||
408da065 CB |
1564 | /* If the rootfs is not a blockdev, prevent the container from marking |
1565 | * it readonly. | |
1566 | * If the container is unprivileged then skip rootfs pinning. | |
0c547523 | 1567 | */ |
0ee35059 | 1568 | if (!wants_to_map_ids) { |
28d9e29e | 1569 | handler->pinfd = pin_rootfs(conf->rootfs.path); |
5e32a990 | 1570 | if (handler->pinfd == -1) |
cfc62c60 | 1571 | INFO("Failed to pin the rootfs for container \"%s\"", handler->name); |
5e32a990 | 1572 | } |
0c547523 | 1573 | |
408da065 | 1574 | /* Create a process in a new set of namespaces. */ |
becad0ec CB |
1575 | handler->ns_on_clone_flags = handler->ns_clone_flags; |
1576 | if (handler->ns_clone_flags & CLONE_NEWUSER) { | |
408da065 CB |
1577 | /* If CLONE_NEWUSER and CLONE_NEWNET was requested, we need to |
1578 | * clone a new user namespace first and only later unshare our | |
1579 | * network namespace to ensure that network devices ownership is | |
1580 | * set up correctly. | |
1581 | */ | |
becad0ec | 1582 | handler->ns_on_clone_flags &= ~CLONE_NEWNET; |
408da065 | 1583 | } |
5af9369b | 1584 | /* The cgroup namespace gets unshare()ed not clone()ed. */ |
becad0ec | 1585 | handler->ns_on_clone_flags &= ~CLONE_NEWCGROUP; |
732375f5 | 1586 | |
4e232466 CB |
1587 | if (share_ns) { |
1588 | pid_t attacher_pid; | |
1589 | ||
cfc62c60 CB |
1590 | attacher_pid = lxc_clone(do_share_ns, handler, |
1591 | CLONE_VFORK | CLONE_VM | CLONE_FILES); | |
4e232466 CB |
1592 | if (attacher_pid < 0) { |
1593 | SYSERROR(LXC_CLONE_ERROR); | |
1594 | goto out_delete_net; | |
1595 | } | |
1596 | ||
1597 | ret = wait_for_pid(attacher_pid); | |
1598 | if (ret < 0) { | |
1599 | SYSERROR("Intermediate process failed"); | |
1600 | goto out_delete_net; | |
1601 | } | |
1602 | } else { | |
cfc62c60 | 1603 | handler->pid = lxc_raw_clone_cb(do_start, handler, |
becad0ec | 1604 | handler->ns_on_clone_flags); |
4e232466 CB |
1605 | } |
1606 | if (handler->pid < 0) { | |
1607 | SYSERROR(LXC_CLONE_ERROR); | |
7fef7a06 | 1608 | goto out_delete_net; |
0ad19a3f | 1609 | } |
fa3a5b22 | 1610 | TRACE("Cloned child process %d", handler->pid); |
1bd8d726 | 1611 | |
9662e444 | 1612 | for (i = 0; i < LXC_NS_MAX; i++) |
becad0ec | 1613 | if (handler->ns_on_clone_flags & ns_info[i].clone_flag) |
28d9e29e | 1614 | INFO("Cloned %s", ns_info[i].flag_name); |
0ad19a3f | 1615 | |
4cb53844 | 1616 | if (!lxc_try_preserve_namespaces(handler, handler->ns_on_clone_flags, handler->pid)) { |
28d9e29e CB |
1617 | ERROR("Failed to preserve cloned namespaces for lxc.hook.stop"); |
1618 | goto out_delete_net; | |
1619 | } | |
b6b2b194 | 1620 | |
3c22086f CLG |
1621 | lxc_sync_fini_child(handler); |
1622 | ||
408da065 CB |
1623 | /* Map the container uids. The container became an invalid userid the |
1624 | * moment it was cloned with CLONE_NEWUSER. This call doesn't change | |
1625 | * anything immediately, but allows the container to setuid(0) (0 being | |
1626 | * mapped to something else on the host.) later to become a valid uid | |
1627 | * again. | |
1628 | */ | |
fa3a5b22 | 1629 | if (wants_to_map_ids) { |
df3c5314 | 1630 | if (!handler->conf->ns_share[LXC_NS_USER] && |
46186acd | 1631 | (handler->conf->ns_keep & CLONE_NEWUSER) == 0) { |
fa3a5b22 CB |
1632 | ret = lxc_map_ids(id_map, handler->pid); |
1633 | if (ret < 0) { | |
1634 | ERROR("Failed to set up id mapping."); | |
1635 | goto out_delete_net; | |
1636 | } | |
1637 | } | |
5b1e83cb SH |
1638 | } |
1639 | ||
cfc62c60 CB |
1640 | ret = lxc_sync_wake_child(handler, LXC_SYNC_STARTUP); |
1641 | if (ret < 0) | |
5b1e83cb | 1642 | goto out_delete_net; |
5b1e83cb | 1643 | |
cfc62c60 CB |
1644 | ret = lxc_sync_wait_child(handler, LXC_SYNC_CONFIGURE); |
1645 | if (ret < 0) | |
5b1e83cb | 1646 | goto out_delete_net; |
0ad19a3f | 1647 | |
9daf6f5d | 1648 | if (!cgroup_setup_limits(handler, false)) { |
cfc62c60 | 1649 | ERROR("Failed to setup cgroup limits for container \"%s\"", name); |
6031a6e5 DE |
1650 | goto out_delete_net; |
1651 | } | |
1652 | ||
d4ef7c50 | 1653 | if (!cgroup_enter(handler)) |
7fef7a06 | 1654 | goto out_delete_net; |
218d4250 | 1655 | |
0996e18a SH |
1656 | if (!cgroup_chown(handler)) |
1657 | goto out_delete_net; | |
1658 | ||
fa3a5b22 | 1659 | /* Now we're ready to preserve the network namespace */ |
4cb53844 | 1660 | ret = lxc_try_preserve_ns(handler->pid, "net"); |
fa3a5b22 | 1661 | if (ret < 0) { |
4cb53844 CB |
1662 | if (ret != -EOPNOTSUPP) { |
1663 | ERROR("%s - Failed to preserve net namespace", strerror(errno)); | |
1664 | goto out_delete_net; | |
1665 | } | |
1666 | } else { | |
1667 | handler->nsfd[LXC_NS_NET] = ret; | |
1668 | DEBUG("Preserved net namespace via fd %d", ret); | |
fa3a5b22 | 1669 | } |
fa3a5b22 | 1670 | |
408da065 | 1671 | /* Create the network configuration. */ |
becad0ec | 1672 | if (handler->ns_clone_flags & CLONE_NEWNET) { |
cfc62c60 CB |
1673 | ret = lxc_network_move_created_netdev_priv(handler->lxcpath, |
1674 | handler->name, | |
1675 | &conf->network, | |
1676 | handler->pid); | |
1677 | if (ret < 0) { | |
1678 | ERROR("Failed to create the configured network"); | |
7fef7a06 | 1679 | goto out_delete_net; |
82d5ae15 | 1680 | } |
74c6e2b0 | 1681 | |
cfc62c60 CB |
1682 | ret = lxc_create_network_unpriv(handler->lxcpath, handler->name, |
1683 | &conf->network, handler->pid); | |
1684 | if (ret < 0) { | |
1685 | ERROR("Failed to create the configured network"); | |
74c6e2b0 CB |
1686 | goto out_delete_net; |
1687 | } | |
0ad19a3f | 1688 | } |
1689 | ||
cfc62c60 CB |
1690 | ret = lxc_network_send_veth_names_to_child(handler); |
1691 | if (ret < 0) { | |
7ab1ba02 CB |
1692 | ERROR("Failed to send veth names to child"); |
1693 | goto out_delete_net; | |
658979c5 SH |
1694 | } |
1695 | ||
61d7a733 YT |
1696 | if (!lxc_list_empty(&conf->procs)) { |
1697 | ret = setup_proc_filesystem(&conf->procs, handler->pid); | |
1698 | if (ret < 0) | |
1699 | goto out_delete_net; | |
1700 | } | |
1701 | ||
408da065 CB |
1702 | /* Tell the child to continue its initialization. We'll get |
1703 | * LXC_SYNC_CGROUP when it is ready for us to setup cgroups. | |
3c22086f | 1704 | */ |
cfc62c60 CB |
1705 | ret = lxc_sync_barrier_child(handler, LXC_SYNC_POST_CONFIGURE); |
1706 | if (ret < 0) | |
544a48a0 SH |
1707 | goto out_delete_net; |
1708 | ||
cfc62c60 CB |
1709 | if (!lxc_list_empty(&conf->limits)) { |
1710 | ret = setup_resource_limits(&conf->limits, handler->pid); | |
1711 | if (ret < 0) { | |
1712 | ERROR("Failed to setup resource limits"); | |
1713 | goto out_delete_net; | |
1714 | } | |
c6d09e15 WB |
1715 | } |
1716 | ||
cfc62c60 CB |
1717 | ret = lxc_sync_barrier_child(handler, LXC_SYNC_CGROUP_UNSHARE); |
1718 | if (ret < 0) | |
f4152036 CB |
1719 | goto out_delete_net; |
1720 | ||
9daf6f5d | 1721 | if (!cgroup_setup_limits(handler, true)) { |
cfc62c60 | 1722 | ERROR("Failed to setup legacy device cgroup controller limits"); |
b98f7d6e | 1723 | goto out_delete_net; |
544a48a0 | 1724 | } |
cfc62c60 | 1725 | TRACE("Set up legacy device cgroup controller limits"); |
544a48a0 | 1726 | |
73d28d42 | 1727 | cgroup_disconnect(); |
7e4dfe0b | 1728 | cgroups_connected = false; |
73d28d42 | 1729 | |
becad0ec | 1730 | if (handler->ns_clone_flags & CLONE_NEWCGROUP) { |
8bf3abfb | 1731 | /* Now we're ready to preserve the cgroup namespace */ |
4cb53844 | 1732 | ret = lxc_try_preserve_ns(handler->pid, "cgroup"); |
8bf3abfb | 1733 | if (ret < 0) { |
4cb53844 CB |
1734 | if (ret != -EOPNOTSUPP) { |
1735 | ERROR("%s - Failed to preserve cgroup namespace", | |
1736 | strerror(errno)); | |
1737 | goto out_delete_net; | |
1738 | } | |
1739 | } else { | |
1740 | handler->nsfd[LXC_NS_CGROUP] = ret; | |
1741 | DEBUG("Preserved cgroup namespace via fd %d", ret); | |
8bf3abfb | 1742 | } |
8bf3abfb CB |
1743 | } |
1744 | ||
5b7f756a CB |
1745 | ret = snprintf(pidstr, 20, "%d", handler->pid); |
1746 | if (ret < 0 || ret >= 20) | |
1747 | goto out_delete_net; | |
1748 | ||
cfc62c60 CB |
1749 | ret = setenv("LXC_PID", pidstr, 1); |
1750 | if (ret < 0) | |
1751 | SYSERROR("Failed to set environment variable: LXC_PID=%s", pidstr); | |
08dd2805 SH |
1752 | |
1753 | /* Run any host-side start hooks */ | |
cfc62c60 CB |
1754 | ret = run_lxc_hooks(name, "start-host", conf, NULL); |
1755 | if (ret < 0) { | |
1756 | ERROR("Failed to run lxc.hook.start-host"); | |
341ed84c | 1757 | goto out_delete_net; |
08dd2805 SH |
1758 | } |
1759 | ||
408da065 CB |
1760 | /* Tell the child to complete its initialization and wait for it to exec |
1761 | * or return an error. (The child will never return | |
08dd2805 | 1762 | * LXC_SYNC_READY_START+1. It will either close the sync pipe, causing |
408da065 CB |
1763 | * lxc_sync_barrier_child to return success, or return a different |
1764 | * value, causing us to error out). | |
544a48a0 | 1765 | */ |
cfc62c60 CB |
1766 | ret = lxc_sync_barrier_child(handler, LXC_SYNC_READY_START); |
1767 | if (ret < 0) | |
341ed84c | 1768 | goto out_delete_net; |
0ad19a3f | 1769 | |
cfc62c60 CB |
1770 | ret = lxc_network_recv_name_and_ifindex_from_child(handler); |
1771 | if (ret < 0) { | |
790255cf CB |
1772 | ERROR("Failed to receive names and ifindices for network " |
1773 | "devices from child"); | |
1774 | goto out_delete_net; | |
1775 | } | |
1776 | ||
1777 | /* Now all networks are created, network devices are moved into place, | |
1778 | * and the correct names and ifindeces in the respective namespaces have | |
1779 | * been recorded. The corresponding structs have now all been filled. So | |
1780 | * log them for debugging purposes. | |
1781 | */ | |
28d9e29e | 1782 | lxc_log_configured_netdevs(conf); |
790255cf | 1783 | |
f4152036 | 1784 | /* Read tty fds allocated by child. */ |
cfc62c60 CB |
1785 | ret = lxc_recv_ttys_from_child(handler); |
1786 | if (ret < 0) { | |
1787 | ERROR("Failed to receive tty info from child process"); | |
f4152036 CB |
1788 | goto out_delete_net; |
1789 | } | |
1790 | ||
cfc62c60 CB |
1791 | ret = handler->ops->post_start(handler, handler->data); |
1792 | if (ret < 0) | |
e6126dbe MN |
1793 | goto out_abort; |
1794 | ||
cfc62c60 CB |
1795 | ret = lxc_set_state(name, handler, RUNNING); |
1796 | if (ret < 0) { | |
1797 | ERROR("Failed to set state to \"%s\"", lxc_state2str(RUNNING)); | |
59eb99ba | 1798 | goto out_abort; |
3f21c114 | 1799 | } |
22ebac19 | 1800 | |
3c22086f | 1801 | lxc_sync_fini(handler); |
0c547523 | 1802 | |
e6126dbe | 1803 | return 0; |
1ac470c0 | 1804 | |
7fef7a06 | 1805 | out_delete_net: |
7e4dfe0b SH |
1806 | if (cgroups_connected) |
1807 | cgroup_disconnect(); | |
1bd8d726 | 1808 | |
becad0ec | 1809 | if (handler->ns_clone_flags & CLONE_NEWNET) |
bb84beda | 1810 | lxc_delete_network(handler); |
1bd8d726 | 1811 | |
59eb99ba DL |
1812 | out_abort: |
1813 | lxc_abort(name, handler); | |
3c22086f | 1814 | lxc_sync_fini(handler); |
5c068da9 SH |
1815 | if (handler->pinfd >= 0) { |
1816 | close(handler->pinfd); | |
1817 | handler->pinfd = -1; | |
1818 | } | |
1819 | ||
b79fcd86 | 1820 | return -1; |
59eb99ba | 1821 | } |
0ad19a3f | 1822 | |
aa460476 | 1823 | int __lxc_start(const char *name, struct lxc_handler *handler, |
507cee36 | 1824 | struct lxc_operations* ops, void *data, const char *lxcpath, |
a3b4f3d6 | 1825 | bool backgrounded, int *error_num) |
59eb99ba | 1826 | { |
c30e9b19 | 1827 | int ret, status; |
aa460476 | 1828 | struct lxc_conf *conf = handler->conf; |
80090207 | 1829 | |
c30e9b19 CB |
1830 | ret = lxc_init(name, handler); |
1831 | if (ret < 0) { | |
1832 | ERROR("Failed to initialize container \"%s\"", name); | |
66aeffc7 | 1833 | return -1; |
0ad19a3f | 1834 | } |
ee70bf78 CLG |
1835 | handler->ops = ops; |
1836 | handler->data = data; | |
507cee36 | 1837 | handler->backgrounded = backgrounded; |
e6126dbe | 1838 | |
76a26f55 | 1839 | if (!attach_block_device(handler->conf)) { |
c30e9b19 | 1840 | ERROR("Failed to attach block device"); |
76a26f55 SH |
1841 | goto out_fini_nonet; |
1842 | } | |
1843 | ||
35120d9c | 1844 | if (geteuid() == 0 && !lxc_list_empty(&conf->id_map)) { |
408da065 | 1845 | /* If the backing store is a device, mount it here and now. */ |
35120d9c | 1846 | if (rootfs_is_blockdev(conf)) { |
c30e9b19 CB |
1847 | ret = unshare(CLONE_NEWNS); |
1848 | if (ret < 0) { | |
1849 | ERROR("Failed to unshare CLONE_NEWNS"); | |
35120d9c SH |
1850 | goto out_fini_nonet; |
1851 | } | |
c30e9b19 | 1852 | INFO("Unshared CLONE_NEWNS"); |
408da065 | 1853 | |
6a0c909a | 1854 | remount_all_slave(); |
c30e9b19 CB |
1855 | ret = do_rootfs_setup(conf, name, lxcpath); |
1856 | if (ret < 0) { | |
1857 | ERROR("Error setting up rootfs mount as root before spawn"); | |
35120d9c SH |
1858 | goto out_fini_nonet; |
1859 | } | |
c30e9b19 | 1860 | INFO("Set up container rootfs as host root"); |
35120d9c SH |
1861 | } |
1862 | } | |
1863 | ||
c30e9b19 CB |
1864 | ret = lxc_spawn(handler); |
1865 | if (ret < 0) { | |
1866 | ERROR("Failed to spawn container \"%s\"", name); | |
76a26f55 | 1867 | goto out_detach_blockdev; |
0ad19a3f | 1868 | } |
a9e1109e CB |
1869 | /* close parent side of data socket */ |
1870 | close(handler->data_sock[0]); | |
1871 | handler->data_sock[0] = -1; | |
1872 | close(handler->data_sock[1]); | |
1873 | handler->data_sock[1] = -1; | |
0ad19a3f | 1874 | |
8bee8851 WB |
1875 | handler->conf->reboot = 0; |
1876 | ||
c30e9b19 CB |
1877 | ret = lxc_poll(name, handler); |
1878 | if (ret) { | |
1879 | ERROR("LXC mainloop exited with error: %d", ret); | |
59eb99ba DL |
1880 | goto out_abort; |
1881 | } | |
0ad19a3f | 1882 | |
c30e9b19 CB |
1883 | status = lxc_wait_for_pid_status(handler->pid); |
1884 | if (status < 0) | |
1885 | SYSERROR("Failed to retrieve status for %d", handler->pid); | |
e043236e | 1886 | |
408da065 CB |
1887 | /* If the child process exited but was not signaled, it didn't call |
1888 | * reboot. This should mean it was an lxc-execute which simply exited. | |
1889 | * In any case, treat it as a 'halt'. | |
8b004f07 | 1890 | */ |
d028235d | 1891 | if (WIFSIGNALED(status)) { |
8b004f07 SH |
1892 | switch(WTERMSIG(status)) { |
1893 | case SIGINT: /* halt */ | |
c30e9b19 | 1894 | DEBUG("Container \"%s\" is halting", name); |
8b004f07 SH |
1895 | break; |
1896 | case SIGHUP: /* reboot */ | |
c30e9b19 | 1897 | DEBUG("Container \"%s\" is rebooting", name); |
8b004f07 SH |
1898 | handler->conf->reboot = 1; |
1899 | break; | |
c2b9bd9e | 1900 | case SIGSYS: /* seccomp */ |
c30e9b19 | 1901 | DEBUG("Container \"%s\" violated its seccomp policy", name); |
c2b9bd9e | 1902 | break; |
8b004f07 | 1903 | default: |
c30e9b19 | 1904 | DEBUG("Unknown exit status for container \"%s\" init %d", name, WTERMSIG(status)); |
8b004f07 SH |
1905 | break; |
1906 | } | |
d028235d | 1907 | } |
828695d9 | 1908 | |
c30e9b19 CB |
1909 | ret = lxc_restore_phys_nics_to_netns(handler); |
1910 | if (ret < 0) | |
b809f232 CB |
1911 | ERROR("Failed to move physical network devices back to parent " |
1912 | "network namespace"); | |
ce5782df | 1913 | |
5c068da9 SH |
1914 | if (handler->pinfd >= 0) { |
1915 | close(handler->pinfd); | |
1916 | handler->pinfd = -1; | |
1917 | } | |
1918 | ||
1787abca | 1919 | lxc_monitor_send_exit_code(name, status, handler->lxcpath); |
eb808539 | 1920 | lxc_error_set_and_log(handler->pid, status); |
a3b4f3d6 TA |
1921 | if (error_num) |
1922 | *error_num = handler->exit_status; | |
1bd8d726 | 1923 | |
9d7f9e52 | 1924 | out_fini: |
bb84beda | 1925 | lxc_delete_network(handler); |
74a2b586 | 1926 | |
76a26f55 SH |
1927 | out_detach_blockdev: |
1928 | detach_block_device(handler->conf); | |
1929 | ||
74a2b586 | 1930 | out_fini_nonet: |
3a0f472d | 1931 | lxc_fini(name, handler); |
c30e9b19 | 1932 | return ret; |
0ad19a3f | 1933 | |
59eb99ba | 1934 | out_abort: |
3a0f472d | 1935 | lxc_abort(name, handler); |
9d7f9e52 | 1936 | goto out_fini; |
0ad19a3f | 1937 | } |
ee70bf78 CLG |
1938 | |
1939 | struct start_args { | |
1940 | char *const *argv; | |
1941 | }; | |
1942 | ||
1943 | static int start(struct lxc_handler *handler, void* data) | |
1944 | { | |
1945 | struct start_args *arg = data; | |
1946 | ||
984984e4 | 1947 | NOTICE("Exec'ing \"%s\"", arg->argv[0]); |
ee70bf78 CLG |
1948 | |
1949 | execvp(arg->argv[0], arg->argv); | |
984984e4 | 1950 | SYSERROR("Failed to exec \"%s\"", arg->argv[0]); |
ee70bf78 CLG |
1951 | return 0; |
1952 | } | |
1953 | ||
1954 | static int post_start(struct lxc_handler *handler, void* data) | |
1955 | { | |
1956 | struct start_args *arg = data; | |
1957 | ||
4c8e880e | 1958 | NOTICE("Started \"%s\" with pid \"%d\"", arg->argv[0], handler->pid); |
ee70bf78 CLG |
1959 | return 0; |
1960 | } | |
1961 | ||
1962 | static struct lxc_operations start_ops = { | |
1963 | .start = start, | |
1964 | .post_start = post_start | |
1965 | }; | |
1966 | ||
aa460476 | 1967 | int lxc_start(const char *name, char *const argv[], struct lxc_handler *handler, |
a3b4f3d6 | 1968 | const char *lxcpath, bool backgrounded, int *error_num) |
ee70bf78 CLG |
1969 | { |
1970 | struct start_args start_arg = { | |
1971 | .argv = argv, | |
1972 | }; | |
1973 | ||
a3b4f3d6 | 1974 | return __lxc_start(name, handler, &start_ops, &start_arg, lxcpath, backgrounded, error_num); |
ee70bf78 | 1975 | } |
28272964 CB |
1976 | |
1977 | static void lxc_destroy_container_on_signal(struct lxc_handler *handler, | |
1978 | const char *name) | |
1979 | { | |
1980 | char destroy[MAXPATHLEN]; | |
f01f7975 | 1981 | struct lxc_container *c; |
ae3beac9 CB |
1982 | int ret = 0; |
1983 | bool bret = true; | |
1984 | ||
df31363a | 1985 | if (handler->conf->rootfs.path && handler->conf->rootfs.mount) { |
790255cf | 1986 | bret = do_destroy_container(handler); |
28272964 | 1987 | if (!bret) { |
ae3beac9 | 1988 | ERROR("Error destroying rootfs for container \"%s\"", name); |
28272964 CB |
1989 | return; |
1990 | } | |
1991 | } | |
ae3beac9 | 1992 | INFO("Destroyed rootfs for container \"%s\"", name); |
28272964 CB |
1993 | |
1994 | ret = snprintf(destroy, MAXPATHLEN, "%s/%s", handler->lxcpath, name); | |
1995 | if (ret < 0 || ret >= MAXPATHLEN) { | |
ae3beac9 | 1996 | ERROR("Error destroying directory for container \"%s\"", name); |
28272964 CB |
1997 | return; |
1998 | } | |
1999 | ||
f01f7975 CB |
2000 | c = lxc_container_new(name, handler->lxcpath); |
2001 | if (c) { | |
2002 | if (container_disk_lock(c)) { | |
ae3beac9 | 2003 | INFO("Could not update lxc_snapshots file"); |
f01f7975 CB |
2004 | lxc_container_put(c); |
2005 | } else { | |
2006 | mod_all_rdeps(c, false); | |
2007 | container_disk_unlock(c); | |
2008 | lxc_container_put(c); | |
2009 | } | |
2010 | } | |
2011 | ||
d0fbc7ba | 2012 | if (!handler->am_root) |
94b0a3ac CB |
2013 | ret = userns_exec_full(handler->conf, lxc_rmdir_onedev_wrapper, |
2014 | destroy, "lxc_rmdir_onedev_wrapper"); | |
28272964 CB |
2015 | else |
2016 | ret = lxc_rmdir_onedev(destroy, NULL); | |
2017 | ||
2018 | if (ret < 0) { | |
ae3beac9 | 2019 | ERROR("Error destroying directory for container \"%s\"", name); |
28272964 CB |
2020 | return; |
2021 | } | |
ae3beac9 | 2022 | INFO("Destroyed directory for container \"%s\"", name); |
28272964 CB |
2023 | } |
2024 | ||
2025 | static int lxc_rmdir_onedev_wrapper(void *data) | |
2026 | { | |
2027 | char *arg = (char *) data; | |
2028 | return lxc_rmdir_onedev(arg, NULL); | |
2029 | } | |
2030 | ||
134df645 CB |
2031 | static bool do_destroy_container(struct lxc_handler *handler) |
2032 | { | |
94b0a3ac CB |
2033 | int ret; |
2034 | ||
d0fbc7ba | 2035 | if (!handler->am_root) { |
94b0a3ac CB |
2036 | ret = userns_exec_full(handler->conf, storage_destroy_wrapper, |
2037 | handler->conf, "storage_destroy_wrapper"); | |
2038 | if (ret < 0) | |
d028235d | 2039 | return false; |
10bc1861 | 2040 | |
d028235d SG |
2041 | return true; |
2042 | } | |
10bc1861 | 2043 | |
790255cf | 2044 | return storage_destroy(handler->conf); |
28272964 | 2045 | } |