]>
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 |
becad0ec | 146 | * in ns_clone_flags. |
4d8ac866 | 147 | * Return true on success, false on failure. |
62d05d9b | 148 | */ |
becad0ec | 149 | static bool lxc_preserve_namespaces(struct lxc_handler *handler, int ns_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 | 156 | for (i = 0; i < LXC_NS_MAX; i++) { |
becad0ec | 157 | if ((ns_clone_flags & ns_info[i].clone_flag) == 0) |
9f30a190 | 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 | ||
974a8aba CB |
393 | if (state == THAWED) |
394 | handler->state = RUNNING; | |
395 | else | |
396 | handler->state = state; | |
397 | ||
9dfa4041 | 398 | TRACE("Set container state to %s", lxc_state2str(state)); |
dbc9832d | 399 | |
d39b10eb | 400 | if (lxc_list_empty(&handler->conf->state_clients)) { |
9dfa4041 | 401 | TRACE("No state clients registered"); |
dbc9832d CB |
402 | return 0; |
403 | } | |
404 | ||
405 | strncpy(msg.name, name, sizeof(msg.name)); | |
406 | msg.name[sizeof(msg.name) - 1] = 0; | |
407 | ||
d39b10eb | 408 | lxc_list_for_each_safe(cur, &handler->conf->state_clients, next) { |
dbc9832d CB |
409 | client = cur->elem; |
410 | ||
d39b10eb | 411 | if (client->states[state] == 0) { |
9dfa4041 | 412 | TRACE("State %s not registered for state client %d", |
dbc9832d CB |
413 | lxc_state2str(state), client->clientfd); |
414 | continue; | |
415 | } | |
416 | ||
9dfa4041 | 417 | TRACE("Sending state %s to state client %d", |
dbc9832d CB |
418 | lxc_state2str(state), client->clientfd); |
419 | ||
420 | again: | |
421 | ret = send(client->clientfd, &msg, sizeof(msg), 0); | |
ee8377bd CB |
422 | if (ret <= 0) { |
423 | if (errno == EINTR) { | |
424 | TRACE("Caught EINTR; retrying"); | |
dbc9832d | 425 | goto again; |
ee8377bd | 426 | } |
dbc9832d | 427 | |
9dfa4041 CB |
428 | ERROR("%s - Failed to send message to client", |
429 | strerror(errno)); | |
dbc9832d CB |
430 | } |
431 | ||
432 | /* kick client from list */ | |
dbc9832d | 433 | lxc_list_del(cur); |
300d1cb4 | 434 | close(client->clientfd); |
dbc9832d CB |
435 | free(cur->elem); |
436 | free(cur); | |
437 | } | |
dbc9832d | 438 | |
5e5576a4 CB |
439 | return 0; |
440 | } | |
441 | ||
442 | static int lxc_serve_state_socket_pair(const char *name, | |
443 | struct lxc_handler *handler, | |
444 | lxc_state_t state) | |
445 | { | |
446 | ssize_t ret; | |
447 | ||
448 | if (!handler->backgrounded || | |
449 | handler->state_socket_pair[1] < 0 || | |
450 | state == STARTING) | |
451 | return 0; | |
452 | ||
453 | /* Close read end of the socket pair. */ | |
454 | close(handler->state_socket_pair[0]); | |
455 | handler->state_socket_pair[0] = -1; | |
456 | ||
ee8377bd | 457 | again: |
5e5576a4 CB |
458 | ret = lxc_abstract_unix_send_credential(handler->state_socket_pair[1], |
459 | &(int){state}, sizeof(int)); | |
ee8377bd CB |
460 | if (ret != sizeof(int)) { |
461 | if (errno == EINTR) | |
462 | goto again; | |
5e5576a4 CB |
463 | SYSERROR("Failed to send state to %d", |
464 | handler->state_socket_pair[1]); | |
ee8377bd CB |
465 | return -1; |
466 | } | |
5e5576a4 CB |
467 | |
468 | TRACE("Sent container state \"%s\" to %d", lxc_state2str(state), | |
469 | handler->state_socket_pair[1]); | |
470 | ||
471 | /* Close write end of the socket pair. */ | |
472 | close(handler->state_socket_pair[1]); | |
473 | handler->state_socket_pair[1] = -1; | |
474 | ||
475 | return 0; | |
476 | } | |
477 | ||
478 | int lxc_set_state(const char *name, struct lxc_handler *handler, | |
479 | lxc_state_t state) | |
480 | { | |
481 | int ret; | |
482 | ||
483 | ret = lxc_serve_state_socket_pair(name, handler, state); | |
484 | if (ret < 0) { | |
485 | ERROR("Failed to synchronize via anonymous pair of unix sockets"); | |
486 | return -1; | |
487 | } | |
488 | ||
489 | ret = lxc_serve_state_clients(name, handler, state); | |
490 | if (ret < 0) | |
491 | return -1; | |
492 | ||
dbc9832d CB |
493 | /* This function will try to connect to the legacy lxc-monitord state |
494 | * server and only exists for backwards compatibility. | |
495 | */ | |
9123e471 | 496 | lxc_monitor_send_state(name, state, handler->lxcpath); |
dbc9832d | 497 | |
66aeffc7 DL |
498 | return 0; |
499 | } | |
500 | ||
735f2c6e | 501 | int lxc_poll(const char *name, struct lxc_handler *handler) |
b0a33c1e | 502 | { |
3c319edb | 503 | int ret; |
86530b0a | 504 | bool has_console = true; |
3c319edb | 505 | struct lxc_epoll_descr descr, descr_console; |
b0a33c1e | 506 | |
03760215 CB |
507 | if (handler->conf->console.path && |
508 | strcmp(handler->conf->console.path, "none") == 0) | |
86530b0a L |
509 | has_console = false; |
510 | ||
3c319edb CB |
511 | ret = lxc_mainloop_open(&descr); |
512 | if (ret < 0) { | |
513 | ERROR("Failed to create mainloop"); | |
50c8bf05 | 514 | goto out_sigfd; |
b0a33c1e | 515 | } |
516 | ||
30a33fbd CB |
517 | if (has_console) { |
518 | ret = lxc_mainloop_open(&descr_console); | |
519 | if (ret < 0) { | |
520 | ERROR("Failed to create console mainloop"); | |
521 | goto out_mainloop; | |
522 | } | |
3c319edb CB |
523 | } |
524 | ||
1cc8bd4b | 525 | ret = lxc_mainloop_add_handler(&descr, handler->sigfd, signal_handler, handler); |
3c319edb | 526 | if (ret < 0) { |
1cc8bd4b | 527 | ERROR("Failed to add signal handler for %d to mainloop", handler->sigfd); |
3c319edb | 528 | goto out_mainloop_console; |
b0a33c1e | 529 | } |
530 | ||
30a33fbd | 531 | if (has_console) { |
9785b539 | 532 | struct lxc_pty *console = &handler->conf->console; |
63376d7d | 533 | |
093bce5f | 534 | ret = lxc_terminal_mainloop_add(&descr, console); |
30a33fbd CB |
535 | if (ret < 0) { |
536 | ERROR("Failed to add console handlers to mainloop"); | |
537 | goto out_mainloop_console; | |
538 | } | |
539 | ||
093bce5f | 540 | ret = lxc_terminal_mainloop_add(&descr_console, console); |
30a33fbd CB |
541 | if (ret < 0) { |
542 | ERROR("Failed to add console handlers to console mainloop"); | |
543 | goto out_mainloop_console; | |
544 | } | |
a54585ad L |
545 | |
546 | handler->conf->console.descr = &descr; | |
563f2f2c DL |
547 | } |
548 | ||
3c319edb CB |
549 | ret = lxc_cmd_mainloop_add(name, &descr, handler); |
550 | if (ret < 0) { | |
551 | ERROR("Failed to add command handler to mainloop"); | |
552 | goto out_mainloop_console; | |
553 | } | |
b0a33c1e | 554 | |
3c319edb | 555 | TRACE("Mainloop is ready"); |
b0a33c1e | 556 | |
3c319edb | 557 | ret = lxc_mainloop(&descr, -1); |
1cc8bd4b CB |
558 | close(descr.epfd); |
559 | descr.epfd = -EBADF; | |
560 | if (ret < 0 || !handler->init_died) | |
42b09f94 | 561 | goto out_mainloop_console; |
1cc8bd4b | 562 | |
30a33fbd CB |
563 | if (has_console) |
564 | ret = lxc_mainloop(&descr_console, 0); | |
565 | ||
3c319edb | 566 | out_mainloop_console: |
30a33fbd CB |
567 | if (has_console) { |
568 | lxc_mainloop_close(&descr_console); | |
569 | TRACE("Closed console mainloop"); | |
570 | } | |
3c319edb | 571 | |
42b09f94 CB |
572 | out_mainloop: |
573 | lxc_mainloop_close(&descr); | |
574 | TRACE("Closed mainloop"); | |
575 | ||
b0a33c1e | 576 | out_sigfd: |
1cc8bd4b CB |
577 | close(handler->sigfd); |
578 | TRACE("Closed signal file descriptor %d", handler->sigfd); | |
579 | handler->sigfd = -EBADF; | |
408da065 | 580 | |
1cc8bd4b | 581 | return ret; |
b0a33c1e | 582 | } |
583 | ||
41784e4e CB |
584 | void lxc_zero_handler(struct lxc_handler *handler) |
585 | { | |
586 | int i; | |
587 | ||
588 | memset(handler, 0, sizeof(struct lxc_handler)); | |
589 | ||
becad0ec | 590 | handler->ns_clone_flags = -1; |
41784e4e CB |
591 | |
592 | handler->pinfd = -1; | |
593 | ||
594 | handler->sigfd = -1; | |
595 | ||
596 | for (i = 0; i < LXC_NS_MAX; i++) | |
597 | handler->nsfd[i] = -1; | |
598 | ||
599 | handler->data_sock[0] = -1; | |
600 | handler->data_sock[1] = -1; | |
601 | ||
602 | handler->state_socket_pair[0] = -1; | |
603 | handler->state_socket_pair[1] = -1; | |
604 | ||
605 | handler->sync_sock[0] = -1; | |
606 | handler->sync_sock[1] = -1; | |
607 | } | |
608 | ||
f2e07cb6 CB |
609 | void lxc_free_handler(struct lxc_handler *handler) |
610 | { | |
1cc8bd4b CB |
611 | if (handler->pinfd >= 0) |
612 | close(handler->pinfd); | |
613 | ||
614 | if (handler->sigfd >= 0) | |
615 | close(handler->sigfd); | |
616 | ||
617 | lxc_put_nsfds(handler); | |
618 | ||
619 | if (handler->conf && handler->conf->reboot == 0) | |
0d0d3655 | 620 | if (handler->conf->maincmd_fd >= 0) |
1cc8bd4b | 621 | close(handler->conf->maincmd_fd); |
f2e07cb6 | 622 | |
5e5576a4 CB |
623 | if (handler->state_socket_pair[0] >= 0) |
624 | close(handler->state_socket_pair[0]); | |
625 | ||
626 | if (handler->state_socket_pair[1] >= 0) | |
627 | close(handler->state_socket_pair[1]); | |
628 | ||
f2e07cb6 CB |
629 | handler->conf = NULL; |
630 | free(handler); | |
1cc8bd4b | 631 | handler = NULL; |
f2e07cb6 CB |
632 | } |
633 | ||
aa460476 | 634 | struct lxc_handler *lxc_init_handler(const char *name, struct lxc_conf *conf, |
5e5576a4 | 635 | const char *lxcpath, bool daemonize) |
59eb99ba | 636 | { |
5e5576a4 | 637 | int i, ret; |
3a0f472d DL |
638 | struct lxc_handler *handler; |
639 | ||
640 | handler = malloc(sizeof(*handler)); | |
fdecbc9c | 641 | if (!handler) |
3a0f472d | 642 | return NULL; |
59eb99ba DL |
643 | memset(handler, 0, sizeof(*handler)); |
644 | ||
fdecbc9c CB |
645 | /* Note that am_guest_unpriv() checks the effective uid. We |
646 | * probably don't care if we are real root only if we are running | |
647 | * as root so this should be fine. | |
790255cf | 648 | */ |
e0010464 | 649 | handler->am_root = !am_guest_unpriv(); |
c6012571 | 650 | handler->data_sock[0] = handler->data_sock[1] = -1; |
fae349da | 651 | handler->conf = conf; |
9123e471 | 652 | handler->lxcpath = lxcpath; |
5c068da9 | 653 | handler->pinfd = -1; |
1cc8bd4b CB |
654 | handler->sigfd = -EBADF; |
655 | handler->init_died = false; | |
5e5576a4 | 656 | handler->state_socket_pair[0] = handler->state_socket_pair[1] = -1; |
d39b10eb CB |
657 | if (handler->conf->reboot == 0) |
658 | lxc_list_init(&handler->conf->state_clients); | |
fae349da | 659 | |
b6b2b194 WB |
660 | for (i = 0; i < LXC_NS_MAX; i++) |
661 | handler->nsfd[i] = -1; | |
662 | ||
f0ecc19d | 663 | handler->name = name; |
3bdf52d7 | 664 | |
5e5576a4 CB |
665 | if (daemonize && !handler->conf->reboot) { |
666 | /* Create socketpair() to synchronize on daemonized startup. | |
fdecbc9c CB |
667 | * When the container reboots we don't need to synchronize |
668 | * again currently so don't open another socketpair(). | |
5e5576a4 CB |
669 | */ |
670 | ret = socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, | |
671 | handler->state_socket_pair); | |
672 | if (ret < 0) { | |
673 | ERROR("Failed to create anonymous pair of unix sockets"); | |
674 | goto on_error; | |
675 | } | |
676 | TRACE("Created anonymous pair {%d,%d} of unix sockets", | |
677 | handler->state_socket_pair[0], | |
678 | handler->state_socket_pair[1]); | |
679 | } | |
680 | ||
d39b10eb CB |
681 | if (handler->conf->reboot == 0) { |
682 | handler->conf->maincmd_fd = lxc_cmd_init(name, lxcpath, "command"); | |
683 | if (handler->conf->maincmd_fd < 0) { | |
684 | ERROR("Failed to set up command socket"); | |
685 | goto on_error; | |
686 | } | |
aa460476 | 687 | } |
9dfa4041 | 688 | TRACE("Unix domain socket %d for command server is ready", |
aa460476 CB |
689 | handler->conf->maincmd_fd); |
690 | ||
691 | return handler; | |
692 | ||
f2e07cb6 CB |
693 | on_error: |
694 | lxc_free_handler(handler); | |
aa460476 CB |
695 | |
696 | return NULL; | |
697 | } | |
698 | ||
699 | int lxc_init(const char *name, struct lxc_handler *handler) | |
700 | { | |
a2c09be0 | 701 | int ret; |
4a03ded4 | 702 | const char *loglevel; |
aa460476 CB |
703 | struct lxc_conf *conf = handler->conf; |
704 | ||
705 | lsm_init(); | |
2170b263 | 706 | TRACE("Initialized LSM"); |
d2e30e99 | 707 | |
2170b263 CB |
708 | ret = lxc_read_seccomp_config(conf); |
709 | if (ret < 0) { | |
710 | ERROR("Failed loading seccomp policy"); | |
d2e30e99 | 711 | goto out_close_maincmd_fd; |
8f2c3a70 | 712 | } |
2170b263 | 713 | TRACE("Read seccomp policy"); |
8f2c3a70 | 714 | |
408da065 | 715 | /* Begin by setting the state to STARTING. */ |
2170b263 CB |
716 | ret = lxc_set_state(name, handler, STARTING); |
717 | if (ret < 0) { | |
718 | ERROR("Failed to set state to \"%s\"", lxc_state2str(STARTING)); | |
051151de | 719 | goto out_close_maincmd_fd; |
0ad19a3f | 720 | } |
46800e77 | 721 | TRACE("Set container state to \"STARTING\""); |
0ad19a3f | 722 | |
408da065 | 723 | /* Start of environment variable setup for hooks. */ |
2170b263 CB |
724 | if (name) { |
725 | ret = setenv("LXC_NAME", name, 1); | |
726 | if (ret < 0) | |
727 | SYSERROR("Failed to set environment variable: LXC_NAME=%s", name); | |
728 | } | |
408da065 | 729 | |
2170b263 CB |
730 | if (conf->rcfile) { |
731 | ret = setenv("LXC_CONFIG_FILE", conf->rcfile, 1); | |
732 | if (ret < 0) | |
733 | SYSERROR("Failed to set environment variable: " | |
734 | "LXC_CONFIG_FILE=%s", conf->rcfile); | |
735 | } | |
408da065 | 736 | |
2170b263 CB |
737 | if (conf->rootfs.mount) { |
738 | ret = setenv("LXC_ROOTFS_MOUNT", conf->rootfs.mount, 1); | |
739 | if (ret < 0) | |
740 | SYSERROR("Failed to set environment variable: " | |
741 | "LXC_ROOTFS_MOUNT=%s", conf->rootfs.mount); | |
742 | } | |
408da065 | 743 | |
2170b263 CB |
744 | if (conf->rootfs.path) { |
745 | ret = setenv("LXC_ROOTFS_PATH", conf->rootfs.path, 1); | |
746 | if (ret < 0) | |
747 | SYSERROR("Failed to set environment variable: " | |
748 | "LXC_ROOTFS_PATH=%s", conf->rootfs.path); | |
749 | } | |
408da065 | 750 | |
2170b263 CB |
751 | if (conf->console.path) { |
752 | ret = setenv("LXC_CONSOLE", conf->console.path, 1); | |
753 | if (ret < 0) | |
754 | SYSERROR("Failed to set environment variable: " | |
755 | "LXC_CONSOLE=%s", conf->console.path); | |
756 | } | |
408da065 | 757 | |
2170b263 CB |
758 | if (conf->console.log_path) { |
759 | ret = setenv("LXC_CONSOLE_LOGPATH", conf->console.log_path, 1); | |
760 | if (ret < 0) | |
761 | SYSERROR("Failed to set environment variable: " | |
762 | "LXC_CONSOLE_LOGPATH=%s", conf->console.log_path); | |
763 | } | |
408da065 | 764 | |
2170b263 CB |
765 | if (cgns_supported()) { |
766 | ret = setenv("LXC_CGNS_AWARE", "1", 1); | |
767 | if (ret < 0) | |
768 | SYSERROR("Failed to set environment variable " | |
769 | "LXC_CGNS_AWARE=1"); | |
770 | } | |
b8f88d9b | 771 | |
4a03ded4 | 772 | loglevel = lxc_log_priority_to_string(lxc_log_get_level()); |
2170b263 CB |
773 | ret = setenv("LXC_LOG_LEVEL", loglevel, 1); |
774 | if (ret < 0) | |
775 | SYSERROR("Set environment variable LXC_LOG_LEVEL=%s", | |
776 | loglevel); | |
a2c09be0 CB |
777 | |
778 | if (conf->hooks_version == 0) | |
779 | ret = setenv("LXC_HOOK_VERSION", "0", 1); | |
780 | else | |
781 | ret = setenv("LXC_HOOK_VERSION", "1", 1); | |
782 | if (ret < 0) | |
783 | SYSERROR("Failed to set environment variable LXC_HOOK_VERSION=%u", conf->hooks_version); | |
408da065 | 784 | /* End of environment variable setup for hooks. */ |
f7bee6c6 | 785 | |
2170b263 | 786 | TRACE("Set environment variables"); |
dbc9832d | 787 | |
2170b263 CB |
788 | ret = run_lxc_hooks(name, "pre-start", conf, NULL); |
789 | if (ret < 0) { | |
790 | ERROR("Failed to run lxc.hook.pre-start for container \"%s\"", name); | |
773fb9ca SH |
791 | goto out_aborting; |
792 | } | |
2170b263 | 793 | TRACE("Ran pre-start hooks"); |
26ddeedd | 794 | |
408da065 CB |
795 | /* The signal fd has to be created before forking otherwise if the child |
796 | * process exits before we setup the signal fd, the event will be lost | |
797 | * and the command will be stuck. | |
798 | */ | |
83ee7875 | 799 | handler->sigfd = setup_signal_fd(&handler->oldmask); |
59eb99ba | 800 | if (handler->sigfd < 0) { |
408da065 | 801 | ERROR("Failed to setup SIGCHLD fd handler."); |
b5159817 DE |
802 | goto out_delete_tty; |
803 | } | |
2170b263 | 804 | TRACE("Set up signal fd"); |
b5159817 | 805 | |
408da065 | 806 | /* Do this after setting up signals since it might unblock SIGWINCH. */ |
4d1ffb0a CB |
807 | ret = lxc_console_create(conf); |
808 | if (ret < 0) { | |
809 | ERROR("Failed to create console"); | |
b5159817 | 810 | goto out_restore_sigmask; |
b0a33c1e | 811 | } |
4d1ffb0a | 812 | TRACE("Created console"); |
b0a33c1e | 813 | |
7cfeddd7 CB |
814 | ret = lxc_pty_map_ids(conf, &conf->console); |
815 | if (ret < 0) { | |
2170b263 | 816 | ERROR("Failed to chown console"); |
c4d10a05 SH |
817 | goto out_restore_sigmask; |
818 | } | |
2170b263 | 819 | TRACE("Chowned console"); |
c4d10a05 | 820 | |
2170b263 | 821 | INFO("Container \"%s\" is initialized", name); |
aa460476 | 822 | return 0; |
59eb99ba | 823 | |
b5159817 DE |
824 | out_restore_sigmask: |
825 | sigprocmask(SIG_SETMASK, &handler->oldmask, NULL); | |
59eb99ba | 826 | out_delete_tty: |
fae349da | 827 | lxc_delete_tty(&conf->tty_info); |
59eb99ba | 828 | out_aborting: |
25c2aca5 | 829 | lxc_set_state(name, handler, ABORTING); |
d2e30e99 DE |
830 | out_close_maincmd_fd: |
831 | close(conf->maincmd_fd); | |
832 | conf->maincmd_fd = -1; | |
aa460476 | 833 | return -1; |
59eb99ba DL |
834 | } |
835 | ||
3b72c4a0 | 836 | void lxc_fini(const char *name, struct lxc_handler *handler) |
59eb99ba | 837 | { |
c097467f | 838 | int i, ret; |
18b3b9c1 | 839 | pid_t self; |
dbc9832d | 840 | struct lxc_list *cur, *next; |
dbc9832d | 841 | char *namespaces[LXC_NS_MAX + 1]; |
b3286b62 | 842 | size_t namespace_count = 0; |
b6b2b194 | 843 | |
408da065 CB |
844 | /* The STOPPING state is there for future cleanup code which can take |
845 | * awhile. | |
59eb99ba | 846 | */ |
25c2aca5 | 847 | lxc_set_state(name, handler, STOPPING); |
b6b2b194 | 848 | |
0059379f | 849 | self = lxc_raw_getpid(); |
b3286b62 | 850 | for (i = 0; i < LXC_NS_MAX; i++) { |
18b3b9c1 CB |
851 | if (handler->nsfd[i] < 0) |
852 | continue; | |
853 | ||
854 | if (handler->conf->hooks_version == 0) | |
c097467f | 855 | ret = asprintf(&namespaces[namespace_count], |
18b3b9c1 CB |
856 | "%s:/proc/%d/fd/%d", ns_info[i].proc_name, |
857 | self, handler->nsfd[i]); | |
858 | else | |
c097467f | 859 | ret = asprintf(&namespaces[namespace_count], |
18b3b9c1 | 860 | "/proc/%d/fd/%d", self, handler->nsfd[i]); |
c097467f CB |
861 | if (ret == -1) { |
862 | SYSERROR("Failed to allocate memory"); | |
18b3b9c1 | 863 | break; |
b3286b62 | 864 | } |
18b3b9c1 CB |
865 | |
866 | if (handler->conf->hooks_version == 0) { | |
867 | namespace_count++; | |
868 | continue; | |
869 | } | |
870 | ||
c097467f CB |
871 | ret = setenv(ns_info[i].env_name, namespaces[namespace_count], 1); |
872 | if (ret < 0) | |
18b3b9c1 CB |
873 | SYSERROR("Failed to set environment variable %s=%s", |
874 | ns_info[i].env_name, namespaces[namespace_count]); | |
875 | else | |
876 | TRACE("Set environment variable %s=%s", | |
877 | ns_info[i].env_name, namespaces[namespace_count]); | |
878 | ||
879 | namespace_count++; | |
b3286b62 WB |
880 | } |
881 | namespaces[namespace_count] = NULL; | |
c154af98 | 882 | |
c097467f CB |
883 | if (handler->conf->reboot) { |
884 | ret = setenv("LXC_TARGET", "reboot", 1); | |
885 | if (ret < 0) | |
886 | SYSERROR("Failed to set environment variable: " | |
887 | "LXC_TARGET=reboot"); | |
888 | } | |
408da065 | 889 | |
c097467f CB |
890 | if (!handler->conf->reboot) { |
891 | ret = setenv("LXC_TARGET", "stop", 1); | |
892 | if (ret < 0) | |
893 | SYSERROR("Failed to set environment variable: " | |
894 | "LXC_TARGET=stop"); | |
895 | } | |
c154af98 | 896 | |
18b3b9c1 | 897 | if (handler->conf->hooks_version == 0) |
c097467f | 898 | ret = run_lxc_hooks(name, "stop", handler->conf, namespaces); |
18b3b9c1 | 899 | else |
c097467f CB |
900 | ret = run_lxc_hooks(name, "stop", handler->conf, NULL); |
901 | if (ret < 0) | |
902 | ERROR("Failed to run \"lxc.hook.stop\" hook"); | |
c154af98 | 903 | |
b3286b62 WB |
904 | while (namespace_count--) |
905 | free(namespaces[namespace_count]); | |
28d9e29e | 906 | |
4288b79f CB |
907 | cgroup_destroy(handler); |
908 | ||
d39b10eb | 909 | if (handler->conf->reboot == 0) { |
f893d898 CB |
910 | /* For all new state clients simply close the command socket. |
911 | * This will inform all state clients that the container is | |
912 | * STOPPED and also prevents a race between a open()/close() on | |
913 | * the command socket causing a new process to get ECONNREFUSED | |
914 | * because we haven't yet closed the command socket. | |
915 | */ | |
d39b10eb CB |
916 | close(handler->conf->maincmd_fd); |
917 | handler->conf->maincmd_fd = -1; | |
c3184275 CB |
918 | TRACE("Closed command socket"); |
919 | ||
920 | /* This function will try to connect to the legacy lxc-monitord | |
921 | * state server and only exists for backwards compatibility. | |
922 | */ | |
923 | lxc_monitor_send_state(name, STOPPED, handler->lxcpath); | |
924 | ||
925 | /* The command socket is closed so no one can acces the command | |
926 | * socket anymore so there's no need to lock it. | |
927 | */ | |
928 | handler->state = STOPPED; | |
929 | TRACE("Set container state to \"STOPPED\""); | |
f893d898 CB |
930 | } else { |
931 | lxc_set_state(name, handler, STOPPED); | |
d39b10eb | 932 | } |
4288b79f | 933 | |
46800e77 CB |
934 | ret = run_lxc_hooks(name, "post-stop", handler->conf, NULL); |
935 | if (ret < 0) { | |
c097467f | 936 | ERROR("Failed to run lxc.hook.post-stop for container \"%s\"", name); |
f3787121 | 937 | if (handler->conf->reboot) { |
c097467f | 938 | WARN("Container will be stopped instead of rebooted"); |
f3787121 | 939 | handler->conf->reboot = 0; |
c097467f CB |
940 | |
941 | ret = setenv("LXC_TARGET", "stop", 1); | |
942 | if (ret < 0) | |
943 | WARN("Failed to set environment variable: " | |
944 | "LXC_TARGET=stop"); | |
f3787121 CB |
945 | } |
946 | } | |
26ddeedd | 947 | |
408da065 | 948 | /* Reset mask set by setup_signal_fd. */ |
c097467f CB |
949 | ret = sigprocmask(SIG_SETMASK, &handler->oldmask, NULL); |
950 | if (ret < 0) | |
951 | WARN("%s - Failed to restore signal mask", strerror(errno)); | |
8f64a3f6 | 952 | |
b5159817 | 953 | lxc_console_delete(&handler->conf->console); |
b2431939 | 954 | lxc_delete_tty(&handler->conf->tty_info); |
dbc9832d | 955 | |
dbc9832d CB |
956 | /* The command socket is now closed, no more state clients can register |
957 | * themselves from now on. So free the list of state clients. | |
958 | */ | |
d39b10eb CB |
959 | lxc_list_for_each_safe(cur, &handler->conf->state_clients, next) { |
960 | struct lxc_state_client *client = cur->elem; | |
961 | ||
962 | /* Keep state clients that want to be notified about reboots. */ | |
963 | if ((handler->conf->reboot > 0) && (client->states[RUNNING] == 2)) | |
964 | continue; | |
965 | ||
dbc9832d | 966 | /* close state client socket */ |
dbc9832d | 967 | lxc_list_del(cur); |
c097467f | 968 | close(client->clientfd); |
dbc9832d CB |
969 | free(cur->elem); |
970 | free(cur); | |
971 | } | |
972 | ||
2c5f2ede | 973 | if (handler->conf->ephemeral == 1 && handler->conf->reboot != 1) |
28272964 | 974 | lxc_destroy_container_on_signal(handler, name); |
2c5f2ede | 975 | |
1cc8bd4b | 976 | lxc_free_handler(handler); |
59eb99ba DL |
977 | } |
978 | ||
735f2c6e | 979 | void lxc_abort(const char *name, struct lxc_handler *handler) |
59eb99ba | 980 | { |
73e608b2 SH |
981 | int ret, status; |
982 | ||
25c2aca5 | 983 | lxc_set_state(name, handler, ABORTING); |
0e4f9d51 CB |
984 | |
985 | if (handler->pid > 0) { | |
986 | ret = kill(handler->pid, SIGKILL); | |
987 | if (ret < 0) | |
988 | SYSERROR("Failed to send SIGKILL to %d", handler->pid); | |
989 | } | |
990 | ||
408da065 CB |
991 | while ((ret = waitpid(-1, &status, 0)) > 0) { |
992 | ; | |
993 | } | |
59eb99ba DL |
994 | } |
995 | ||
ffe1e01a | 996 | static int do_start(void *data) |
50e98013 | 997 | { |
8bf3abfb | 998 | int ret; |
7a55c157 | 999 | char path[PATH_MAX]; |
928b1f04 YT |
1000 | bool have_cap_setgid; |
1001 | uid_t new_uid; | |
1002 | gid_t new_gid; | |
e96a536c | 1003 | struct lxc_list *iterator; |
5af9369b | 1004 | int devnull_fd = -1; |
e96a536c | 1005 | struct lxc_handler *handler = data; |
50e98013 | 1006 | |
18225d19 | 1007 | lxc_sync_fini_parent(handler); |
50e98013 | 1008 | |
408da065 CB |
1009 | /* This prctl must be before the synchro, so if the parent dies before |
1010 | * we set the parent death signal, we will detect its death with the | |
1011 | * synchro right after, otherwise we have a window where the parent can | |
1012 | * exit before we set the pdeath signal leading to a unsupervized | |
1013 | * container. | |
743ecd2e | 1014 | */ |
18225d19 | 1015 | ret = lxc_set_death_signal(SIGKILL); |
912314fc CB |
1016 | if (ret < 0) { |
1017 | SYSERROR("Failed to set PR_SET_PDEATHSIG to SIGKILL"); | |
18225d19 | 1018 | goto out_warn_father; |
743ecd2e DL |
1019 | } |
1020 | ||
18225d19 CB |
1021 | ret = sigprocmask(SIG_SETMASK, &handler->oldmask, NULL); |
1022 | if (ret < 0) { | |
1023 | SYSERROR("Failed to set signal mask"); | |
1024 | goto out_warn_father; | |
1025 | } | |
50e98013 | 1026 | |
408da065 | 1027 | /* Don't leak the pinfd to the container. */ |
d39b10eb | 1028 | if (handler->pinfd >= 0) |
0d03360a | 1029 | close(handler->pinfd); |
2b0e17e4 | 1030 | |
18225d19 CB |
1031 | ret = lxc_sync_wait_parent(handler, LXC_SYNC_STARTUP); |
1032 | if (ret < 0) | |
1033 | goto out_warn_father; | |
5b1e83cb | 1034 | |
408da065 CB |
1035 | /* Unshare CLONE_NEWNET after CLONE_NEWUSER. See |
1036 | * https://github.com/lxc/lxd/issues/1978. | |
1037 | */ | |
becad0ec | 1038 | if ((handler->ns_clone_flags & (CLONE_NEWNET | CLONE_NEWUSER)) == |
408da065 | 1039 | (CLONE_NEWNET | CLONE_NEWUSER)) { |
5b1e83cb SH |
1040 | ret = unshare(CLONE_NEWNET); |
1041 | if (ret < 0) { | |
e96a536c | 1042 | SYSERROR("Failed to unshare CLONE_NEWNET"); |
5b1e83cb SH |
1043 | goto out_warn_father; |
1044 | } | |
e96a536c | 1045 | INFO("Unshared CLONE_NEWNET"); |
5b1e83cb SH |
1046 | } |
1047 | ||
408da065 CB |
1048 | /* Tell the parent task it can begin to configure the container and wait |
1049 | * for it to finish. | |
3c22086f | 1050 | */ |
e96a536c CB |
1051 | ret = lxc_sync_barrier_parent(handler, LXC_SYNC_CONFIGURE); |
1052 | if (ret < 0) | |
9d7f9e52 | 1053 | return -1; |
50e98013 | 1054 | |
e96a536c CB |
1055 | ret = lxc_network_recv_veth_names_from_parent(handler); |
1056 | if (ret < 0) { | |
7ab1ba02 | 1057 | ERROR("Failed to receive veth names from parent"); |
658979c5 | 1058 | goto out_warn_father; |
7ab1ba02 | 1059 | } |
658979c5 | 1060 | |
408da065 | 1061 | /* If we are in a new user namespace, become root there to have |
d08f8d2f | 1062 | * privilege over our namespace. |
f6d3e3e4 SH |
1063 | */ |
1064 | if (!lxc_list_empty(&handler->conf->id_map)) { | |
4160c3a0 CB |
1065 | uid_t nsuid = (handler->conf->root_nsuid_map != NULL) |
1066 | ? 0 | |
1067 | : handler->conf->init_uid; | |
1068 | gid_t nsgid = (handler->conf->root_nsgid_map != NULL) | |
1069 | ? 0 | |
1070 | : handler->conf->init_gid; | |
1071 | ||
1072 | ret = lxc_switch_uid_gid(nsuid, nsgid); | |
4b826b1f | 1073 | if (ret < 0) |
f6d3e3e4 | 1074 | goto out_warn_father; |
d08f8d2f CB |
1075 | |
1076 | /* Drop groups only after we switched to a valid gid in the new | |
1077 | * user namespace. | |
1078 | */ | |
4b826b1f | 1079 | ret = lxc_setgroups(0, NULL); |
a19b974f | 1080 | if (ret < 0 && (handler->am_root || errno != EPERM)) |
c476bdce | 1081 | goto out_warn_father; |
4b826b1f CB |
1082 | |
1083 | if (!handler->am_root) { | |
1084 | ret = prctl(PR_SET_DUMPABLE, 1, 0, 0, 0); | |
1085 | if (ret < 0) | |
1086 | goto out_warn_father; | |
1087 | } | |
912314fc CB |
1088 | |
1089 | /* set{g,u}id() clears deathsignal */ | |
18225d19 | 1090 | ret = lxc_set_death_signal(SIGKILL); |
912314fc CB |
1091 | if (ret < 0) { |
1092 | SYSERROR("Failed to set PR_SET_PDEATHSIG to SIGKILL"); | |
1093 | goto out_warn_father; | |
1094 | } | |
f6d3e3e4 SH |
1095 | } |
1096 | ||
e96a536c CB |
1097 | ret = access(handler->lxcpath, X_OK); |
1098 | if (ret != 0) { | |
c8154066 SH |
1099 | print_top_failing_dir(handler->lxcpath); |
1100 | goto out_warn_father; | |
1101 | } | |
1102 | ||
e96a536c CB |
1103 | ret = snprintf(path, sizeof(path), "%s/dev/null", |
1104 | handler->conf->rootfs.mount); | |
408da065 | 1105 | if (ret < 0 || ret >= sizeof(path)) |
7a55c157 | 1106 | goto out_warn_father; |
7a55c157 TA |
1107 | |
1108 | /* In order to checkpoint restore, we need to have everything in the | |
1109 | * same mount namespace. However, some containers may not have a | |
1110 | * reasonable /dev (in particular, they may not have /dev/null), so we | |
1111 | * can't set init's std fds to /dev/null by opening it from inside the | |
1112 | * container. | |
1113 | * | |
1114 | * If that's the case, fall back to using the host's /dev/null. This | |
1115 | * means that migration won't work, but at least we won't spew output | |
1116 | * where it isn't wanted. | |
1117 | */ | |
e96a536c CB |
1118 | if (handler->backgrounded && !handler->conf->autodev) { |
1119 | ret = access(path, F_OK); | |
1120 | if (ret != 0) { | |
1121 | devnull_fd = open_devnull(); | |
c44de748 | 1122 | |
e96a536c CB |
1123 | if (devnull_fd < 0) |
1124 | goto out_warn_father; | |
1125 | WARN("Using /dev/null from the host for container " | |
1126 | "init's standard file descriptors. Migration will " | |
1127 | "not work"); | |
1128 | } | |
c44de748 AM |
1129 | } |
1130 | ||
408da065 | 1131 | /* Ask father to setup cgroups and wait for him to finish. */ |
e96a536c CB |
1132 | ret = lxc_sync_barrier_parent(handler, LXC_SYNC_CGROUP); |
1133 | if (ret < 0) | |
c44de748 | 1134 | goto out_error; |
544a48a0 | 1135 | |
deefdf8a CB |
1136 | /* Unshare cgroup namespace after we have setup our cgroups. If we do it |
1137 | * earlier we end up with a wrong view of /proc/self/cgroup. For | |
1138 | * example, assume we unshare(CLONE_NEWCGROUP) first, and then create | |
1139 | * the cgroup for the container, say /sys/fs/cgroup/cpuset/lxc/c, then | |
1140 | * /proc/self/cgroup would show us: | |
1141 | * | |
1142 | * 8:cpuset:/lxc/c | |
1143 | * | |
1144 | * whereas it should actually show | |
1145 | * | |
1146 | * 8:cpuset:/ | |
1147 | */ | |
becad0ec | 1148 | if (handler->ns_clone_flags & CLONE_NEWCGROUP) { |
e96a536c CB |
1149 | ret = unshare(CLONE_NEWCGROUP); |
1150 | if (ret < 0) { | |
1151 | INFO("Failed to unshare CLONE_NEWCGROUP"); | |
deefdf8a CB |
1152 | goto out_warn_father; |
1153 | } | |
e96a536c | 1154 | INFO("Unshared CLONE_NEWCGROUP"); |
deefdf8a CB |
1155 | } |
1156 | ||
149857af CB |
1157 | /* Add the requested environment variables to the current environment to |
1158 | * allow them to be used by the various hooks, such as the start hook | |
1159 | * above. | |
98ff08ca | 1160 | */ |
98ff08ca | 1161 | lxc_list_for_each(iterator, &handler->conf->environment) { |
e96a536c CB |
1162 | ret = putenv((char *)iterator->elem); |
1163 | if (ret < 0) { | |
1164 | SYSERROR("Failed to set environment variable: %s", | |
1165 | (char *)iterator->elem); | |
98ff08ca CB |
1166 | goto out_warn_father; |
1167 | } | |
1168 | } | |
1169 | ||
f4152036 | 1170 | /* Setup the container, ip, names, utsname, ... */ |
3b988b33 | 1171 | ret = lxc_setup(handler); |
7729f8e5 | 1172 | close(handler->data_sock[1]); |
8bf3abfb | 1173 | close(handler->data_sock[0]); |
7729f8e5 | 1174 | if (ret < 0) { |
e96a536c | 1175 | ERROR("Failed to setup container \"%s\"", handler->name); |
f4152036 CB |
1176 | goto out_warn_father; |
1177 | } | |
1178 | ||
408da065 | 1179 | /* Set the label to change to when we exec(2) the container's init. */ |
e96a536c CB |
1180 | ret = lsm_process_label_set(NULL, handler->conf, 1, 1); |
1181 | if (ret < 0) | |
e075f5d9 | 1182 | goto out_warn_father; |
5112cd70 | 1183 | |
029cdff5 | 1184 | /* Set PR_SET_NO_NEW_PRIVS after we changed the lsm label. If we do it |
408da065 CB |
1185 | * before we aren't allowed anymore. |
1186 | */ | |
029cdff5 | 1187 | if (handler->conf->no_new_privs) { |
e96a536c CB |
1188 | ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); |
1189 | if (ret < 0) { | |
1190 | SYSERROR("Could not set PR_SET_NO_NEW_PRIVS to block " | |
1191 | "execve() gainable privileges"); | |
029cdff5 CB |
1192 | goto out_warn_father; |
1193 | } | |
e96a536c CB |
1194 | DEBUG("Set PR_SET_NO_NEW_PRIVS to block execve() gainable " |
1195 | "privileges"); | |
029cdff5 CB |
1196 | } |
1197 | ||
0d9acb99 DE |
1198 | /* Some init's such as busybox will set sane tty settings on stdin, |
1199 | * stdout, stderr which it thinks is the console. We already set them | |
1200 | * the way we wanted on the real terminal, and we want init to do its | |
408da065 CB |
1201 | * setup on its console ie. the pty allocated in lxc_console_create() so |
1202 | * make sure that that pty is stdin,stdout,stderr. | |
0d9acb99 | 1203 | */ |
5d113f65 CB |
1204 | if (handler->conf->console.slave >= 0) { |
1205 | if (handler->backgrounded || handler->conf->is_execute == 0) | |
1206 | ret = set_stdfds(handler->conf->console.slave); | |
1207 | else | |
1208 | ret = lxc_console_set_stdfds(handler->conf->console.slave); | |
1209 | if (ret < 0) { | |
c5b93afb | 1210 | ERROR("Failed to redirect std{in,out,err} to pty file " |
e96a536c | 1211 | "descriptor %d", handler->conf->console.slave); |
c5b93afb LF |
1212 | goto out_warn_father; |
1213 | } | |
5d113f65 | 1214 | } |
0d9acb99 | 1215 | |
408da065 | 1216 | /* If we mounted a temporary proc, then unmount it now. */ |
5112cd70 | 1217 | tmp_proc_unmount(handler->conf); |
e075f5d9 | 1218 | |
e96a536c CB |
1219 | ret = lxc_seccomp_load(handler->conf); |
1220 | if (ret < 0) | |
8f2c3a70 SH |
1221 | goto out_warn_father; |
1222 | ||
e96a536c CB |
1223 | ret = run_lxc_hooks(handler->name, "start", handler->conf, NULL); |
1224 | if (ret < 0) { | |
1225 | ERROR("Failed to run lxc.hook.start for container \"%s\"", | |
1226 | handler->name); | |
773fb9ca SH |
1227 | goto out_warn_father; |
1228 | } | |
fc25b815 | 1229 | |
773fb9ca | 1230 | close(handler->sigfd); |
26ddeedd | 1231 | |
7a55c157 TA |
1232 | if (devnull_fd < 0) { |
1233 | devnull_fd = open_devnull(); | |
7a55c157 TA |
1234 | if (devnull_fd < 0) |
1235 | goto out_warn_father; | |
1236 | } | |
1237 | ||
e96a536c CB |
1238 | if (handler->conf->console.slave < 0 && handler->backgrounded) { |
1239 | ret = set_stdfds(devnull_fd); | |
1240 | if (ret < 0) { | |
1241 | ERROR("Failed to redirect std{in,out,err} to \"/dev/null\""); | |
c5b93afb LF |
1242 | goto out_warn_father; |
1243 | } | |
e96a536c | 1244 | } |
507cee36 | 1245 | |
c44de748 AM |
1246 | if (devnull_fd >= 0) { |
1247 | close(devnull_fd); | |
1248 | devnull_fd = -1; | |
1249 | } | |
1250 | ||
8c9a7665 TA |
1251 | setsid(); |
1252 | ||
e96a536c CB |
1253 | if (handler->conf->init_cwd) { |
1254 | ret = chdir(handler->conf->init_cwd); | |
1255 | if (ret < 0) { | |
1256 | SYSERROR("Could not change directory to \"%s\"", | |
1257 | handler->conf->init_cwd); | |
1258 | goto out_warn_father; | |
1259 | } | |
3c491553 L |
1260 | } |
1261 | ||
e96a536c CB |
1262 | ret = lxc_sync_barrier_parent(handler, LXC_SYNC_CGROUP_LIMITS); |
1263 | if (ret < 0) | |
f4152036 CB |
1264 | goto out_warn_father; |
1265 | ||
149857af CB |
1266 | /* Reset the environment variables the user requested in a clear |
1267 | * environment. | |
1268 | */ | |
e96a536c CB |
1269 | ret = clearenv(); |
1270 | /* Don't error out though. */ | |
1271 | if (ret < 0) | |
149857af | 1272 | SYSERROR("Failed to clear environment."); |
149857af CB |
1273 | |
1274 | lxc_list_for_each(iterator, &handler->conf->environment) { | |
e96a536c CB |
1275 | ret = putenv((char *)iterator->elem); |
1276 | if (ret < 0) { | |
1277 | SYSERROR("Failed to set environment variable: %s", | |
1278 | (char *)iterator->elem); | |
149857af CB |
1279 | goto out_warn_father; |
1280 | } | |
1281 | } | |
1282 | ||
e96a536c CB |
1283 | ret = putenv("container=lxc"); |
1284 | if (ret < 0) { | |
1285 | SYSERROR("Failed to set environment variable: container=lxc"); | |
98ff08ca CB |
1286 | goto out_warn_father; |
1287 | } | |
1288 | ||
1289 | if (handler->conf->pty_names) { | |
e96a536c CB |
1290 | ret = putenv(handler->conf->pty_names); |
1291 | if (ret < 0) { | |
1292 | SYSERROR("Failed to set environment variable for container ptys"); | |
98ff08ca CB |
1293 | goto out_warn_father; |
1294 | } | |
1295 | } | |
1296 | ||
76bdf299 CB |
1297 | /* The container has been setup. We can now switch to an unprivileged |
1298 | * uid/gid. | |
1299 | */ | |
928b1f04 YT |
1300 | new_uid = handler->conf->init_uid; |
1301 | new_gid = handler->conf->init_gid; | |
76bdf299 | 1302 | |
e96a536c CB |
1303 | /* If we are in a new user namespace we already dropped all groups when |
1304 | * we switched to root in the new user namespace further above. Only | |
1305 | * drop groups if we can, so ensure that we have necessary privilege. | |
928b1f04 YT |
1306 | */ |
1307 | #if HAVE_LIBCAP | |
1308 | have_cap_setgid = lxc_proc_cap_is_set(CAP_SETGID, CAP_EFFECTIVE); | |
1309 | #else | |
1310 | have_cap_setgid = false; | |
1311 | #endif | |
1312 | if (lxc_list_empty(&handler->conf->id_map) && have_cap_setgid) { | |
e96a536c CB |
1313 | ret = lxc_setgroups(0, NULL); |
1314 | if (ret < 0) | |
76bdf299 CB |
1315 | goto out_warn_father; |
1316 | } | |
1317 | ||
e96a536c CB |
1318 | ret = lxc_switch_uid_gid(new_uid, new_gid); |
1319 | if (ret < 0) | |
928b1f04 YT |
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) | |
becad0ec | 1389 | handler->ns_clone_flags |= ns_info[i].clone_flag; |
8bc8c715 CB |
1390 | } else if (conf->ns_clone != 0) { |
1391 | if ((conf->ns_clone & ns_info[i].clone_flag) > 0) | |
becad0ec | 1392 | handler->ns_clone_flags |= ns_info[i].clone_flag; |
8bc8c715 CB |
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 | ||
becad0ec | 1403 | handler->ns_clone_flags |= ns_info[i].clone_flag; |
8bc8c715 | 1404 | } |
03df7ab5 | 1405 | |
8bc8c715 CB |
1406 | if (!conf->ns_share[i]) |
1407 | continue; | |
5af9369b | 1408 | |
becad0ec | 1409 | handler->ns_clone_flags &= ~ns_info[i].clone_flag; |
8bc8c715 | 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 | ||
becad0ec | 1444 | flags = handler->ns_on_clone_flags; |
8deca6c9 | 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 | |
cfc62c60 CB |
1485 | ret = lxc_sync_init(handler); |
1486 | if (ret < 0) | |
9d7f9e52 | 1487 | return -1; |
0ad19a3f | 1488 | |
b9f522c5 | 1489 | ret = socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, |
790255cf | 1490 | handler->data_sock); |
c6012571 | 1491 | if (ret < 0) { |
e8bd4e43 SH |
1492 | lxc_sync_fini(handler); |
1493 | return -1; | |
1494 | } | |
1495 | ||
5af9369b CB |
1496 | ret = resolve_clone_flags(handler); |
1497 | if (ret < 0) { | |
1498 | lxc_sync_fini(handler); | |
1499 | return -1; | |
1500 | } | |
9f30a190 | 1501 | |
becad0ec | 1502 | if (handler->ns_clone_flags & CLONE_NEWNET) { |
28d9e29e | 1503 | if (!lxc_list_empty(&conf->network)) { |
26b797f3 | 1504 | |
9f30a190 MM |
1505 | /* Find gateway addresses from the link device, which is |
1506 | * no longer accessible inside the container. Do this | |
1507 | * before creating network interfaces, since goto | |
408da065 CB |
1508 | * out_delete_net does not work before lxc_clone. |
1509 | */ | |
cfc62c60 CB |
1510 | ret = lxc_find_gateway_addresses(handler); |
1511 | if (ret < 0) { | |
1512 | ERROR("Failed to find gateway addresses"); | |
9f30a190 MM |
1513 | lxc_sync_fini(handler); |
1514 | return -1; | |
1515 | } | |
1516 | ||
408da065 CB |
1517 | /* That should be done before the clone because we will |
1518 | * fill the netdev index and use them in the child. | |
9f30a190 | 1519 | */ |
cfc62c60 CB |
1520 | ret = lxc_create_network_priv(handler); |
1521 | if (ret < 0) { | |
1522 | ERROR("Failed to create the network"); | |
9f30a190 MM |
1523 | lxc_sync_fini(handler); |
1524 | return -1; | |
1525 | } | |
19a26f82 | 1526 | } |
82d5ae15 DL |
1527 | } |
1528 | ||
d4ef7c50 | 1529 | if (!cgroup_init(handler)) { |
cfc62c60 | 1530 | ERROR("Failed initializing cgroup support"); |
33ad9f1a CS |
1531 | goto out_delete_net; |
1532 | } | |
1533 | ||
7e4dfe0b SH |
1534 | cgroups_connected = true; |
1535 | ||
d4ef7c50 | 1536 | if (!cgroup_create(handler)) { |
cfc62c60 | 1537 | ERROR("Failed creating cgroups"); |
47d8fb3b CS |
1538 | goto out_delete_net; |
1539 | } | |
1540 | ||
408da065 CB |
1541 | /* If the rootfs is not a blockdev, prevent the container from marking |
1542 | * it readonly. | |
1543 | * If the container is unprivileged then skip rootfs pinning. | |
0c547523 | 1544 | */ |
0ee35059 | 1545 | if (!wants_to_map_ids) { |
28d9e29e | 1546 | handler->pinfd = pin_rootfs(conf->rootfs.path); |
5e32a990 | 1547 | if (handler->pinfd == -1) |
cfc62c60 | 1548 | INFO("Failed to pin the rootfs for container \"%s\"", handler->name); |
5e32a990 | 1549 | } |
0c547523 | 1550 | |
408da065 | 1551 | /* Create a process in a new set of namespaces. */ |
becad0ec CB |
1552 | handler->ns_on_clone_flags = handler->ns_clone_flags; |
1553 | if (handler->ns_clone_flags & CLONE_NEWUSER) { | |
408da065 CB |
1554 | /* If CLONE_NEWUSER and CLONE_NEWNET was requested, we need to |
1555 | * clone a new user namespace first and only later unshare our | |
1556 | * network namespace to ensure that network devices ownership is | |
1557 | * set up correctly. | |
1558 | */ | |
becad0ec | 1559 | handler->ns_on_clone_flags &= ~CLONE_NEWNET; |
408da065 | 1560 | } |
5af9369b | 1561 | /* The cgroup namespace gets unshare()ed not clone()ed. */ |
becad0ec | 1562 | handler->ns_on_clone_flags &= ~CLONE_NEWCGROUP; |
732375f5 | 1563 | |
4e232466 CB |
1564 | if (share_ns) { |
1565 | pid_t attacher_pid; | |
1566 | ||
cfc62c60 CB |
1567 | attacher_pid = lxc_clone(do_share_ns, handler, |
1568 | CLONE_VFORK | CLONE_VM | CLONE_FILES); | |
4e232466 CB |
1569 | if (attacher_pid < 0) { |
1570 | SYSERROR(LXC_CLONE_ERROR); | |
1571 | goto out_delete_net; | |
1572 | } | |
1573 | ||
1574 | ret = wait_for_pid(attacher_pid); | |
1575 | if (ret < 0) { | |
1576 | SYSERROR("Intermediate process failed"); | |
1577 | goto out_delete_net; | |
1578 | } | |
1579 | } else { | |
cfc62c60 | 1580 | handler->pid = lxc_raw_clone_cb(do_start, handler, |
becad0ec | 1581 | handler->ns_on_clone_flags); |
4e232466 CB |
1582 | } |
1583 | if (handler->pid < 0) { | |
1584 | SYSERROR(LXC_CLONE_ERROR); | |
7fef7a06 | 1585 | goto out_delete_net; |
0ad19a3f | 1586 | } |
fa3a5b22 | 1587 | TRACE("Cloned child process %d", handler->pid); |
1bd8d726 | 1588 | |
9662e444 | 1589 | for (i = 0; i < LXC_NS_MAX; i++) |
becad0ec | 1590 | if (handler->ns_on_clone_flags & ns_info[i].clone_flag) |
28d9e29e | 1591 | INFO("Cloned %s", ns_info[i].flag_name); |
0ad19a3f | 1592 | |
becad0ec | 1593 | if (!lxc_preserve_namespaces(handler, handler->ns_on_clone_flags, handler->pid)) { |
28d9e29e CB |
1594 | ERROR("Failed to preserve cloned namespaces for lxc.hook.stop"); |
1595 | goto out_delete_net; | |
1596 | } | |
b6b2b194 | 1597 | |
3c22086f CLG |
1598 | lxc_sync_fini_child(handler); |
1599 | ||
408da065 CB |
1600 | /* Map the container uids. The container became an invalid userid the |
1601 | * moment it was cloned with CLONE_NEWUSER. This call doesn't change | |
1602 | * anything immediately, but allows the container to setuid(0) (0 being | |
1603 | * mapped to something else on the host.) later to become a valid uid | |
1604 | * again. | |
1605 | */ | |
fa3a5b22 | 1606 | if (wants_to_map_ids) { |
df3c5314 | 1607 | if (!handler->conf->ns_share[LXC_NS_USER] && |
46186acd | 1608 | (handler->conf->ns_keep & CLONE_NEWUSER) == 0) { |
fa3a5b22 CB |
1609 | ret = lxc_map_ids(id_map, handler->pid); |
1610 | if (ret < 0) { | |
1611 | ERROR("Failed to set up id mapping."); | |
1612 | goto out_delete_net; | |
1613 | } | |
1614 | } | |
5b1e83cb SH |
1615 | } |
1616 | ||
cfc62c60 CB |
1617 | ret = lxc_sync_wake_child(handler, LXC_SYNC_STARTUP); |
1618 | if (ret < 0) | |
5b1e83cb | 1619 | goto out_delete_net; |
5b1e83cb | 1620 | |
cfc62c60 CB |
1621 | ret = lxc_sync_wait_child(handler, LXC_SYNC_CONFIGURE); |
1622 | if (ret < 0) | |
5b1e83cb | 1623 | goto out_delete_net; |
0ad19a3f | 1624 | |
9daf6f5d | 1625 | if (!cgroup_setup_limits(handler, false)) { |
cfc62c60 | 1626 | ERROR("Failed to setup cgroup limits for container \"%s\"", name); |
6031a6e5 DE |
1627 | goto out_delete_net; |
1628 | } | |
1629 | ||
d4ef7c50 | 1630 | if (!cgroup_enter(handler)) |
7fef7a06 | 1631 | goto out_delete_net; |
218d4250 | 1632 | |
0996e18a SH |
1633 | if (!cgroup_chown(handler)) |
1634 | goto out_delete_net; | |
1635 | ||
fa3a5b22 CB |
1636 | /* Now we're ready to preserve the network namespace */ |
1637 | ret = lxc_preserve_ns(handler->pid, "net"); | |
1638 | if (ret < 0) { | |
1639 | ERROR("%s - Failed to preserve net namespace", strerror(errno)); | |
1640 | goto out_delete_net; | |
1641 | } | |
1642 | handler->nsfd[LXC_NS_NET] = ret; | |
1643 | DEBUG("Preserved net namespace via fd %d", ret); | |
1644 | ||
408da065 | 1645 | /* Create the network configuration. */ |
becad0ec | 1646 | if (handler->ns_clone_flags & CLONE_NEWNET) { |
cfc62c60 CB |
1647 | ret = lxc_network_move_created_netdev_priv(handler->lxcpath, |
1648 | handler->name, | |
1649 | &conf->network, | |
1650 | handler->pid); | |
1651 | if (ret < 0) { | |
1652 | ERROR("Failed to create the configured network"); | |
7fef7a06 | 1653 | goto out_delete_net; |
82d5ae15 | 1654 | } |
74c6e2b0 | 1655 | |
cfc62c60 CB |
1656 | ret = lxc_create_network_unpriv(handler->lxcpath, handler->name, |
1657 | &conf->network, handler->pid); | |
1658 | if (ret < 0) { | |
1659 | ERROR("Failed to create the configured network"); | |
74c6e2b0 CB |
1660 | goto out_delete_net; |
1661 | } | |
0ad19a3f | 1662 | } |
1663 | ||
cfc62c60 CB |
1664 | ret = lxc_network_send_veth_names_to_child(handler); |
1665 | if (ret < 0) { | |
7ab1ba02 CB |
1666 | ERROR("Failed to send veth names to child"); |
1667 | goto out_delete_net; | |
658979c5 SH |
1668 | } |
1669 | ||
61d7a733 YT |
1670 | if (!lxc_list_empty(&conf->procs)) { |
1671 | ret = setup_proc_filesystem(&conf->procs, handler->pid); | |
1672 | if (ret < 0) | |
1673 | goto out_delete_net; | |
1674 | } | |
1675 | ||
408da065 CB |
1676 | /* Tell the child to continue its initialization. We'll get |
1677 | * LXC_SYNC_CGROUP when it is ready for us to setup cgroups. | |
3c22086f | 1678 | */ |
cfc62c60 CB |
1679 | ret = lxc_sync_barrier_child(handler, LXC_SYNC_POST_CONFIGURE); |
1680 | if (ret < 0) | |
544a48a0 SH |
1681 | goto out_delete_net; |
1682 | ||
cfc62c60 CB |
1683 | if (!lxc_list_empty(&conf->limits)) { |
1684 | ret = setup_resource_limits(&conf->limits, handler->pid); | |
1685 | if (ret < 0) { | |
1686 | ERROR("Failed to setup resource limits"); | |
1687 | goto out_delete_net; | |
1688 | } | |
c6d09e15 WB |
1689 | } |
1690 | ||
cfc62c60 CB |
1691 | ret = lxc_sync_barrier_child(handler, LXC_SYNC_CGROUP_UNSHARE); |
1692 | if (ret < 0) | |
f4152036 CB |
1693 | goto out_delete_net; |
1694 | ||
9daf6f5d | 1695 | if (!cgroup_setup_limits(handler, true)) { |
cfc62c60 | 1696 | ERROR("Failed to setup legacy device cgroup controller limits"); |
b98f7d6e | 1697 | goto out_delete_net; |
544a48a0 | 1698 | } |
cfc62c60 | 1699 | TRACE("Set up legacy device cgroup controller limits"); |
544a48a0 | 1700 | |
73d28d42 | 1701 | cgroup_disconnect(); |
7e4dfe0b | 1702 | cgroups_connected = false; |
73d28d42 | 1703 | |
becad0ec | 1704 | if (handler->ns_clone_flags & CLONE_NEWCGROUP) { |
8bf3abfb CB |
1705 | /* Now we're ready to preserve the cgroup namespace */ |
1706 | ret = lxc_preserve_ns(handler->pid, "cgroup"); | |
1707 | if (ret < 0) { | |
1708 | ERROR("%s - Failed to preserve cgroup namespace", strerror(errno)); | |
1709 | goto out_delete_net; | |
1710 | } | |
1711 | handler->nsfd[LXC_NS_CGROUP] = ret; | |
1712 | DEBUG("Preserved cgroup namespace via fd %d", ret); | |
1713 | } | |
1714 | ||
5b7f756a CB |
1715 | ret = snprintf(pidstr, 20, "%d", handler->pid); |
1716 | if (ret < 0 || ret >= 20) | |
1717 | goto out_delete_net; | |
1718 | ||
cfc62c60 CB |
1719 | ret = setenv("LXC_PID", pidstr, 1); |
1720 | if (ret < 0) | |
1721 | SYSERROR("Failed to set environment variable: LXC_PID=%s", pidstr); | |
08dd2805 SH |
1722 | |
1723 | /* Run any host-side start hooks */ | |
cfc62c60 CB |
1724 | ret = run_lxc_hooks(name, "start-host", conf, NULL); |
1725 | if (ret < 0) { | |
1726 | ERROR("Failed to run lxc.hook.start-host"); | |
341ed84c | 1727 | goto out_delete_net; |
08dd2805 SH |
1728 | } |
1729 | ||
408da065 CB |
1730 | /* Tell the child to complete its initialization and wait for it to exec |
1731 | * or return an error. (The child will never return | |
08dd2805 | 1732 | * LXC_SYNC_READY_START+1. It will either close the sync pipe, causing |
408da065 CB |
1733 | * lxc_sync_barrier_child to return success, or return a different |
1734 | * value, causing us to error out). | |
544a48a0 | 1735 | */ |
cfc62c60 CB |
1736 | ret = lxc_sync_barrier_child(handler, LXC_SYNC_READY_START); |
1737 | if (ret < 0) | |
341ed84c | 1738 | goto out_delete_net; |
0ad19a3f | 1739 | |
cfc62c60 CB |
1740 | ret = lxc_network_recv_name_and_ifindex_from_child(handler); |
1741 | if (ret < 0) { | |
790255cf CB |
1742 | ERROR("Failed to receive names and ifindices for network " |
1743 | "devices from child"); | |
1744 | goto out_delete_net; | |
1745 | } | |
1746 | ||
1747 | /* Now all networks are created, network devices are moved into place, | |
1748 | * and the correct names and ifindeces in the respective namespaces have | |
1749 | * been recorded. The corresponding structs have now all been filled. So | |
1750 | * log them for debugging purposes. | |
1751 | */ | |
28d9e29e | 1752 | lxc_log_configured_netdevs(conf); |
790255cf | 1753 | |
f4152036 | 1754 | /* Read tty fds allocated by child. */ |
cfc62c60 CB |
1755 | ret = lxc_recv_ttys_from_child(handler); |
1756 | if (ret < 0) { | |
1757 | ERROR("Failed to receive tty info from child process"); | |
f4152036 CB |
1758 | goto out_delete_net; |
1759 | } | |
1760 | ||
cfc62c60 CB |
1761 | ret = handler->ops->post_start(handler, handler->data); |
1762 | if (ret < 0) | |
e6126dbe MN |
1763 | goto out_abort; |
1764 | ||
cfc62c60 CB |
1765 | ret = lxc_set_state(name, handler, RUNNING); |
1766 | if (ret < 0) { | |
1767 | ERROR("Failed to set state to \"%s\"", lxc_state2str(RUNNING)); | |
59eb99ba | 1768 | goto out_abort; |
3f21c114 | 1769 | } |
22ebac19 | 1770 | |
3c22086f | 1771 | lxc_sync_fini(handler); |
0c547523 | 1772 | |
e6126dbe | 1773 | return 0; |
1ac470c0 | 1774 | |
7fef7a06 | 1775 | out_delete_net: |
7e4dfe0b SH |
1776 | if (cgroups_connected) |
1777 | cgroup_disconnect(); | |
1bd8d726 | 1778 | |
becad0ec | 1779 | if (handler->ns_clone_flags & CLONE_NEWNET) |
bb84beda | 1780 | lxc_delete_network(handler); |
1bd8d726 | 1781 | |
59eb99ba DL |
1782 | out_abort: |
1783 | lxc_abort(name, handler); | |
3c22086f | 1784 | lxc_sync_fini(handler); |
5c068da9 SH |
1785 | if (handler->pinfd >= 0) { |
1786 | close(handler->pinfd); | |
1787 | handler->pinfd = -1; | |
1788 | } | |
1789 | ||
b79fcd86 | 1790 | return -1; |
59eb99ba | 1791 | } |
0ad19a3f | 1792 | |
aa460476 | 1793 | int __lxc_start(const char *name, struct lxc_handler *handler, |
507cee36 TA |
1794 | struct lxc_operations* ops, void *data, const char *lxcpath, |
1795 | bool backgrounded) | |
59eb99ba | 1796 | { |
c30e9b19 | 1797 | int ret, status; |
aa460476 | 1798 | struct lxc_conf *conf = handler->conf; |
80090207 | 1799 | |
c30e9b19 CB |
1800 | ret = lxc_init(name, handler); |
1801 | if (ret < 0) { | |
1802 | ERROR("Failed to initialize container \"%s\"", name); | |
66aeffc7 | 1803 | return -1; |
0ad19a3f | 1804 | } |
ee70bf78 CLG |
1805 | handler->ops = ops; |
1806 | handler->data = data; | |
507cee36 | 1807 | handler->backgrounded = backgrounded; |
e6126dbe | 1808 | |
76a26f55 | 1809 | if (!attach_block_device(handler->conf)) { |
c30e9b19 | 1810 | ERROR("Failed to attach block device"); |
76a26f55 SH |
1811 | goto out_fini_nonet; |
1812 | } | |
1813 | ||
35120d9c | 1814 | if (geteuid() == 0 && !lxc_list_empty(&conf->id_map)) { |
408da065 | 1815 | /* If the backing store is a device, mount it here and now. */ |
35120d9c | 1816 | if (rootfs_is_blockdev(conf)) { |
c30e9b19 CB |
1817 | ret = unshare(CLONE_NEWNS); |
1818 | if (ret < 0) { | |
1819 | ERROR("Failed to unshare CLONE_NEWNS"); | |
35120d9c SH |
1820 | goto out_fini_nonet; |
1821 | } | |
c30e9b19 | 1822 | INFO("Unshared CLONE_NEWNS"); |
408da065 | 1823 | |
6a0c909a | 1824 | remount_all_slave(); |
c30e9b19 CB |
1825 | ret = do_rootfs_setup(conf, name, lxcpath); |
1826 | if (ret < 0) { | |
1827 | ERROR("Error setting up rootfs mount as root before spawn"); | |
35120d9c SH |
1828 | goto out_fini_nonet; |
1829 | } | |
c30e9b19 | 1830 | INFO("Set up container rootfs as host root"); |
35120d9c SH |
1831 | } |
1832 | } | |
1833 | ||
c30e9b19 CB |
1834 | ret = lxc_spawn(handler); |
1835 | if (ret < 0) { | |
1836 | ERROR("Failed to spawn container \"%s\"", name); | |
76a26f55 | 1837 | goto out_detach_blockdev; |
0ad19a3f | 1838 | } |
a9e1109e CB |
1839 | /* close parent side of data socket */ |
1840 | close(handler->data_sock[0]); | |
1841 | handler->data_sock[0] = -1; | |
1842 | close(handler->data_sock[1]); | |
1843 | handler->data_sock[1] = -1; | |
0ad19a3f | 1844 | |
8bee8851 WB |
1845 | handler->conf->reboot = 0; |
1846 | ||
c30e9b19 CB |
1847 | ret = lxc_poll(name, handler); |
1848 | if (ret) { | |
1849 | ERROR("LXC mainloop exited with error: %d", ret); | |
59eb99ba DL |
1850 | goto out_abort; |
1851 | } | |
0ad19a3f | 1852 | |
c30e9b19 CB |
1853 | status = lxc_wait_for_pid_status(handler->pid); |
1854 | if (status < 0) | |
1855 | SYSERROR("Failed to retrieve status for %d", handler->pid); | |
e043236e | 1856 | |
408da065 CB |
1857 | /* If the child process exited but was not signaled, it didn't call |
1858 | * reboot. This should mean it was an lxc-execute which simply exited. | |
1859 | * In any case, treat it as a 'halt'. | |
8b004f07 | 1860 | */ |
d028235d | 1861 | if (WIFSIGNALED(status)) { |
8b004f07 SH |
1862 | switch(WTERMSIG(status)) { |
1863 | case SIGINT: /* halt */ | |
c30e9b19 | 1864 | DEBUG("Container \"%s\" is halting", name); |
8b004f07 SH |
1865 | break; |
1866 | case SIGHUP: /* reboot */ | |
c30e9b19 | 1867 | DEBUG("Container \"%s\" is rebooting", name); |
8b004f07 SH |
1868 | handler->conf->reboot = 1; |
1869 | break; | |
c2b9bd9e | 1870 | case SIGSYS: /* seccomp */ |
c30e9b19 | 1871 | DEBUG("Container \"%s\" violated its seccomp policy", name); |
c2b9bd9e | 1872 | break; |
8b004f07 | 1873 | default: |
c30e9b19 | 1874 | DEBUG("Unknown exit status for container \"%s\" init %d", name, WTERMSIG(status)); |
8b004f07 SH |
1875 | break; |
1876 | } | |
d028235d | 1877 | } |
828695d9 | 1878 | |
c30e9b19 CB |
1879 | ret = lxc_restore_phys_nics_to_netns(handler); |
1880 | if (ret < 0) | |
b809f232 CB |
1881 | ERROR("Failed to move physical network devices back to parent " |
1882 | "network namespace"); | |
ce5782df | 1883 | |
5c068da9 SH |
1884 | if (handler->pinfd >= 0) { |
1885 | close(handler->pinfd); | |
1886 | handler->pinfd = -1; | |
1887 | } | |
1888 | ||
1787abca | 1889 | lxc_monitor_send_exit_code(name, status, handler->lxcpath); |
eb808539 | 1890 | lxc_error_set_and_log(handler->pid, status); |
1bd8d726 | 1891 | |
9d7f9e52 | 1892 | out_fini: |
bb84beda | 1893 | lxc_delete_network(handler); |
74a2b586 | 1894 | |
76a26f55 SH |
1895 | out_detach_blockdev: |
1896 | detach_block_device(handler->conf); | |
1897 | ||
74a2b586 | 1898 | out_fini_nonet: |
3a0f472d | 1899 | lxc_fini(name, handler); |
c30e9b19 | 1900 | return ret; |
0ad19a3f | 1901 | |
59eb99ba | 1902 | out_abort: |
3a0f472d | 1903 | lxc_abort(name, handler); |
9d7f9e52 | 1904 | goto out_fini; |
0ad19a3f | 1905 | } |
ee70bf78 CLG |
1906 | |
1907 | struct start_args { | |
1908 | char *const *argv; | |
1909 | }; | |
1910 | ||
1911 | static int start(struct lxc_handler *handler, void* data) | |
1912 | { | |
1913 | struct start_args *arg = data; | |
1914 | ||
984984e4 | 1915 | NOTICE("Exec'ing \"%s\"", arg->argv[0]); |
ee70bf78 CLG |
1916 | |
1917 | execvp(arg->argv[0], arg->argv); | |
984984e4 | 1918 | SYSERROR("Failed to exec \"%s\"", arg->argv[0]); |
ee70bf78 CLG |
1919 | return 0; |
1920 | } | |
1921 | ||
1922 | static int post_start(struct lxc_handler *handler, void* data) | |
1923 | { | |
1924 | struct start_args *arg = data; | |
1925 | ||
4c8e880e | 1926 | NOTICE("Started \"%s\" with pid \"%d\"", arg->argv[0], handler->pid); |
ee70bf78 CLG |
1927 | return 0; |
1928 | } | |
1929 | ||
1930 | static struct lxc_operations start_ops = { | |
1931 | .start = start, | |
1932 | .post_start = post_start | |
1933 | }; | |
1934 | ||
aa460476 | 1935 | int lxc_start(const char *name, char *const argv[], struct lxc_handler *handler, |
507cee36 | 1936 | const char *lxcpath, bool backgrounded) |
ee70bf78 CLG |
1937 | { |
1938 | struct start_args start_arg = { | |
1939 | .argv = argv, | |
1940 | }; | |
1941 | ||
aa460476 | 1942 | return __lxc_start(name, handler, &start_ops, &start_arg, lxcpath, backgrounded); |
ee70bf78 | 1943 | } |
28272964 CB |
1944 | |
1945 | static void lxc_destroy_container_on_signal(struct lxc_handler *handler, | |
1946 | const char *name) | |
1947 | { | |
1948 | char destroy[MAXPATHLEN]; | |
f01f7975 | 1949 | struct lxc_container *c; |
ae3beac9 CB |
1950 | int ret = 0; |
1951 | bool bret = true; | |
1952 | ||
df31363a | 1953 | if (handler->conf->rootfs.path && handler->conf->rootfs.mount) { |
790255cf | 1954 | bret = do_destroy_container(handler); |
28272964 | 1955 | if (!bret) { |
ae3beac9 | 1956 | ERROR("Error destroying rootfs for container \"%s\"", name); |
28272964 CB |
1957 | return; |
1958 | } | |
1959 | } | |
ae3beac9 | 1960 | INFO("Destroyed rootfs for container \"%s\"", name); |
28272964 CB |
1961 | |
1962 | ret = snprintf(destroy, MAXPATHLEN, "%s/%s", handler->lxcpath, name); | |
1963 | if (ret < 0 || ret >= MAXPATHLEN) { | |
ae3beac9 | 1964 | ERROR("Error destroying directory for container \"%s\"", name); |
28272964 CB |
1965 | return; |
1966 | } | |
1967 | ||
f01f7975 CB |
1968 | c = lxc_container_new(name, handler->lxcpath); |
1969 | if (c) { | |
1970 | if (container_disk_lock(c)) { | |
ae3beac9 | 1971 | INFO("Could not update lxc_snapshots file"); |
f01f7975 CB |
1972 | lxc_container_put(c); |
1973 | } else { | |
1974 | mod_all_rdeps(c, false); | |
1975 | container_disk_unlock(c); | |
1976 | lxc_container_put(c); | |
1977 | } | |
1978 | } | |
1979 | ||
d0fbc7ba | 1980 | if (!handler->am_root) |
94b0a3ac CB |
1981 | ret = userns_exec_full(handler->conf, lxc_rmdir_onedev_wrapper, |
1982 | destroy, "lxc_rmdir_onedev_wrapper"); | |
28272964 CB |
1983 | else |
1984 | ret = lxc_rmdir_onedev(destroy, NULL); | |
1985 | ||
1986 | if (ret < 0) { | |
ae3beac9 | 1987 | ERROR("Error destroying directory for container \"%s\"", name); |
28272964 CB |
1988 | return; |
1989 | } | |
ae3beac9 | 1990 | INFO("Destroyed directory for container \"%s\"", name); |
28272964 CB |
1991 | } |
1992 | ||
1993 | static int lxc_rmdir_onedev_wrapper(void *data) | |
1994 | { | |
1995 | char *arg = (char *) data; | |
1996 | return lxc_rmdir_onedev(arg, NULL); | |
1997 | } | |
1998 | ||
134df645 CB |
1999 | static bool do_destroy_container(struct lxc_handler *handler) |
2000 | { | |
94b0a3ac CB |
2001 | int ret; |
2002 | ||
d0fbc7ba | 2003 | if (!handler->am_root) { |
94b0a3ac CB |
2004 | ret = userns_exec_full(handler->conf, storage_destroy_wrapper, |
2005 | handler->conf, "storage_destroy_wrapper"); | |
2006 | if (ret < 0) | |
d028235d | 2007 | return false; |
10bc1861 | 2008 | |
d028235d SG |
2009 | return true; |
2010 | } | |
10bc1861 | 2011 | |
790255cf | 2012 | return storage_destroy(handler->conf); |
28272964 | 2013 | } |