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