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