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