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