]>
Commit | Line | Data |
---|---|---|
cc73685d | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
e51d4895 | 2 | |
d38dd64a CB |
3 | #ifndef _GNU_SOURCE |
4 | #define _GNU_SOURCE 1 | |
5 | #endif | |
0ad19a3f | 6 | #include <errno.h> |
0ad19a3f | 7 | #include <fcntl.h> |
b45c7011 | 8 | #include <inttypes.h> |
d38dd64a CB |
9 | #include <net/if.h> |
10 | #include <netinet/in.h> | |
292b1d17 CB |
11 | #include <poll.h> |
12 | #include <stddef.h> | |
b45c7011 | 13 | #include <stdint.h> |
292b1d17 CB |
14 | #include <stdio.h> |
15 | #include <stdlib.h> | |
16 | #include <string.h> | |
0ad19a3f | 17 | #include <sys/param.h> |
0ad19a3f | 18 | #include <sys/socket.h> |
292b1d17 CB |
19 | #include <sys/stat.h> |
20 | #include <sys/types.h> | |
e51d4895 | 21 | #include <sys/wait.h> |
d38dd64a | 22 | #include <unistd.h> |
b113348e | 23 | |
31c53c2e | 24 | #include "af_unix.h" |
d38dd64a | 25 | #include "config.h" |
292b1d17 | 26 | #include "error.h" |
f2363e38 ÇO |
27 | #include "log.h" |
28 | #include "lxclock.h" | |
397a8d30 | 29 | #include "macro.h" |
54b43e8f | 30 | #include "memory_utils.h" |
f2363e38 | 31 | #include "monitor.h" |
292b1d17 | 32 | #include "state.h" |
f2363e38 | 33 | #include "utils.h" |
36eb9bde | 34 | |
9de31d5a | 35 | #ifndef HAVE_STRLCPY |
58db1a61 | 36 | #include "strlcpy.h" |
9de31d5a CB |
37 | #endif |
38 | ||
ac2cecc4 | 39 | lxc_log_define(monitor, lxc); |
0ad19a3f | 40 | |
e51d4895 | 41 | /* routines used by monitor publishers (containers) */ |
9e60f51d DE |
42 | int lxc_monitor_fifo_name(const char *lxcpath, char *fifo_path, size_t fifo_path_sz, |
43 | int do_mkdirp) | |
44 | { | |
45 | int ret; | |
44b9ae4b | 46 | char *rundir; |
9e60f51d DE |
47 | |
48 | rundir = get_rundir(); | |
97a696c6 SG |
49 | if (!rundir) |
50 | return -1; | |
51 | ||
9e60f51d | 52 | if (do_mkdirp) { |
3ca3b230 CB |
53 | ret = strnprintf(fifo_path, fifo_path_sz, "%s/lxc/%s", rundir, lxcpath); |
54 | if (ret < 0) { | |
6dd32d35 | 55 | ERROR("rundir/lxcpath (%s/%s) too long for monitor fifo", rundir, lxcpath); |
44b9ae4b | 56 | free(rundir); |
9e60f51d DE |
57 | return -1; |
58 | } | |
9e60f51d | 59 | ret = mkdir_p(fifo_path, 0755); |
9e60f51d | 60 | if (ret < 0) { |
47903908 | 61 | ERROR("Unable to create monitor fifo directory %s", fifo_path); |
44b9ae4b | 62 | free(rundir); |
9e60f51d DE |
63 | return ret; |
64 | } | |
65 | } | |
3ca3b230 CB |
66 | ret = strnprintf(fifo_path, fifo_path_sz, "%s/lxc/%s/monitor-fifo", rundir, lxcpath); |
67 | if (ret < 0) { | |
6dd32d35 | 68 | ERROR("rundir/lxcpath (%s/%s) too long for monitor fifo", rundir, lxcpath); |
44b9ae4b | 69 | free(rundir); |
9e60f51d DE |
70 | return -1; |
71 | } | |
44b9ae4b | 72 | free(rundir); |
9e60f51d DE |
73 | return 0; |
74 | } | |
75 | ||
e51d4895 | 76 | static void lxc_monitor_fifo_send(struct lxc_msg *msg, const char *lxcpath) |
0ad19a3f | 77 | { |
e51d4895 DE |
78 | int fd,ret; |
79 | char fifo_path[PATH_MAX]; | |
80 | ||
81 | BUILD_BUG_ON(sizeof(*msg) > PIPE_BUF); /* write not guaranteed atomic */ | |
9e60f51d DE |
82 | |
83 | ret = lxc_monitor_fifo_name(lxcpath, fifo_path, sizeof(fifo_path), 0); | |
84 | if (ret < 0) | |
9123e471 | 85 | return; |
80f41298 | 86 | |
292b1d17 CB |
87 | /* Open the fifo nonblock in case the monitor is dead, we don't want the |
88 | * open to wait for a reader since it may never come. | |
8bf1e61e | 89 | */ |
292b1d17 | 90 | fd = open(fifo_path, O_WRONLY | O_NONBLOCK); |
e51d4895 | 91 | if (fd < 0) { |
292b1d17 CB |
92 | /* It is normal for this open() to fail with ENXIO when there is |
93 | * no monitor running, so we don't log it. | |
e51d4895 | 94 | */ |
2469f9b6 | 95 | if (errno == ENXIO || errno == ENOENT) |
292b1d17 CB |
96 | return; |
97 | ||
a24c5678 | 98 | SYSWARN("Failed to open fifo to send message"); |
31c53c2e | 99 | return; |
e51d4895 | 100 | } |
0ad19a3f | 101 | |
92ffb6d8 DE |
102 | if (fcntl(fd, F_SETFL, O_WRONLY) < 0) { |
103 | close(fd); | |
8bf1e61e | 104 | return; |
92ffb6d8 | 105 | } |
8bf1e61e | 106 | |
802e609a | 107 | ret = lxc_write_nointr(fd, msg, sizeof(*msg)); |
e51d4895 | 108 | if (ret != sizeof(*msg)) { |
e8b9ac8f | 109 | close(fd); |
6dd32d35 | 110 | SYSERROR("Failed to write to monitor fifo \"%s\"", fifo_path); |
e51d4895 DE |
111 | return; |
112 | } | |
0ad19a3f | 113 | |
114 | close(fd); | |
115 | } | |
116 | ||
292b1d17 CB |
117 | void lxc_monitor_send_state(const char *name, lxc_state_t state, |
118 | const char *lxcpath) | |
eae6543d | 119 | { |
292b1d17 | 120 | struct lxc_msg msg = {.type = lxc_msg_state, .value = state}; |
eae6543d | 121 | |
9de31d5a | 122 | (void)strlcpy(msg.name, name, sizeof(msg.name)); |
e51d4895 | 123 | lxc_monitor_fifo_send(&msg, lxcpath); |
0ad19a3f | 124 | } |
125 | ||
292b1d17 CB |
126 | void lxc_monitor_send_exit_code(const char *name, int exit_code, |
127 | const char *lxcpath) | |
1787abca | 128 | { |
292b1d17 | 129 | struct lxc_msg msg = {.type = lxc_msg_exit_code, .value = exit_code}; |
1787abca | 130 | |
9de31d5a | 131 | (void)strlcpy(msg.name, name, sizeof(msg.name)); |
1787abca JTLB |
132 | lxc_monitor_fifo_send(&msg, lxcpath); |
133 | } | |
134 | ||
e51d4895 DE |
135 | /* routines used by monitor subscribers (lxc-monitor) */ |
136 | int lxc_monitor_close(int fd) | |
0ad19a3f | 137 | { |
dd1d77f9 | 138 | return close(fd); |
e51d4895 DE |
139 | } |
140 | ||
fcaef9c7 CB |
141 | /* Enforces \0-termination for the abstract unix socket. This is not required |
142 | * but allows us to print it out. | |
143 | * | |
144 | * Older version of liblxc only allowed for 105 bytes to be used for the | |
145 | * abstract unix domain socket name because the code for our abstract unix | |
146 | * socket handling performed invalid checks. Since we \0-terminate we could now | |
147 | * have a maximum of 106 chars. But to not break backwards compatibility we keep | |
148 | * the limit at 105. | |
149 | */ | |
95e523c8 | 150 | int lxc_monitor_sock_name(const char *lxcpath, struct sockaddr_un *addr) |
151 | { | |
2db56bd6 | 152 | __do_free char *path = NULL; |
e51d4895 DE |
153 | size_t len; |
154 | int ret; | |
b45c7011 | 155 | uint64_t hash; |
e51d4895 | 156 | |
b45c7011 DE |
157 | /* addr.sun_path is only 108 bytes, so we hash the full name and |
158 | * then append as much of the name as we can fit. | |
9123e471 | 159 | */ |
e51d4895 DE |
160 | memset(addr, 0, sizeof(*addr)); |
161 | addr->sun_family = AF_UNIX; | |
292b1d17 | 162 | |
fcaef9c7 | 163 | /* strlen("lxc/") + strlen("/monitor-sock") + 1 = 18 */ |
073135ba | 164 | len = strlen(lxcpath) + 18; |
54b43e8f | 165 | path = must_realloc(NULL, len); |
3ca3b230 CB |
166 | ret = strnprintf(path, len, "lxc/%s/monitor-sock", lxcpath); |
167 | if (ret < 0) { | |
6dd32d35 | 168 | ERROR("Failed to create name for monitor socket"); |
9e60f51d DE |
169 | return -1; |
170 | } | |
9e60f51d | 171 | |
3ca3b230 | 172 | /* Note: strnprintf() will \0-terminate addr->sun_path on the 106th byte |
fcaef9c7 CB |
173 | * and so the abstract socket name has 105 "meaningful" characters. This |
174 | * is absolutely intentional. For further info read the comment for this | |
175 | * function above! | |
176 | */ | |
073135ba | 177 | len = sizeof(addr->sun_path) - 1; |
b45c7011 | 178 | hash = fnv_64a_buf(path, ret, FNV1A_64_INIT); |
3ca3b230 | 179 | ret = strnprintf(addr->sun_path, len, "@lxc/%016" PRIx64 "/%s", hash, lxcpath); |
fcaef9c7 | 180 | if (ret < 0) { |
6dd32d35 | 181 | ERROR("Failed to create hashed name for monitor socket"); |
2f126499 | 182 | goto on_error; |
fcaef9c7 | 183 | } |
292b1d17 | 184 | |
fcaef9c7 CB |
185 | /* replace @ with \0 */ |
186 | addr->sun_path[0] = '\0'; | |
6dd32d35 | 187 | INFO("Using monitor socket name \"%s\" (length of socket name %zu must be <= %zu)", &addr->sun_path[1], strlen(&addr->sun_path[1]), sizeof(addr->sun_path) - 3); |
292b1d17 | 188 | |
e51d4895 | 189 | return 0; |
2f126499 | 190 | |
191 | on_error: | |
192 | return -1; | |
e51d4895 | 193 | } |
0ad19a3f | 194 | |
e51d4895 DE |
195 | int lxc_monitor_open(const char *lxcpath) |
196 | { | |
197 | struct sockaddr_un addr; | |
292b1d17 CB |
198 | int fd; |
199 | size_t retry; | |
fcaef9c7 | 200 | int backoff_ms[] = {10, 50, 100}; |
e51d4895 DE |
201 | |
202 | if (lxc_monitor_sock_name(lxcpath, &addr) < 0) | |
203 | return -1; | |
204 | ||
2f126499 | 205 | DEBUG("Opening monitor socket %s with len %zu", &addr.sun_path[1], strlen(&addr.sun_path[1])); |
aae93dd3 | 206 | |
292b1d17 | 207 | for (retry = 0; retry < sizeof(backoff_ms) / sizeof(backoff_ms[0]); retry++) { |
fcaef9c7 | 208 | fd = lxc_abstract_unix_connect(addr.sun_path); |
94bc08e9 | 209 | if (fd != -1 || errno != ECONNREFUSED) |
e51d4895 | 210 | break; |
6d1400b5 | 211 | |
212 | SYSERROR("Failed to connect to monitor socket. Retrying in %d ms", backoff_ms[retry]); | |
e51d4895 | 213 | usleep(backoff_ms[retry] * 1000); |
0ad19a3f | 214 | } |
215 | ||
fcaef9c7 | 216 | if (fd < 0) { |
6d1400b5 | 217 | SYSERROR("Failed to connect to monitor socket"); |
c8dcf778 | 218 | return -1; |
e51d4895 | 219 | } |
292b1d17 | 220 | |
0ad19a3f | 221 | return fd; |
222 | } | |
223 | ||
2366b8a7 | 224 | int lxc_monitor_read_fdset(struct pollfd *fds, nfds_t nfds, struct lxc_msg *msg, |
8d06bd13 | 225 | int timeout) |
0ad19a3f | 226 | { |
2366b8a7 SH |
227 | long i; |
228 | int ret; | |
0ad19a3f | 229 | |
2366b8a7 | 230 | ret = poll(fds, nfds, timeout * 1000); |
8d06bd13 | 231 | if (ret == -1) |
75b1e198 | 232 | return -1; |
8d06bd13 | 233 | else if (ret == 0) |
1a0e70ac | 234 | return -2; /* timed out */ |
8d06bd13 | 235 | |
292b1d17 CB |
236 | /* Only read from the first ready fd, the others will remain ready for |
237 | * when this routine is called again. | |
8d06bd13 DE |
238 | */ |
239 | for (i = 0; i < nfds; i++) { | |
2366b8a7 SH |
240 | if (fds[i].revents != 0) { |
241 | fds[i].revents = 0; | |
242 | ret = recv(fds[i].fd, msg, sizeof(*msg), 0); | |
8d06bd13 | 243 | if (ret <= 0) { |
b5be6a7c | 244 | SYSERROR("Failed to receive message. Did monitord die?"); |
8d06bd13 DE |
245 | return -1; |
246 | } | |
247 | return ret; | |
248 | } | |
0ad19a3f | 249 | } |
292b1d17 | 250 | |
6dd32d35 | 251 | SYSERROR("No ready fd found"); |
292b1d17 | 252 | |
8d06bd13 DE |
253 | return -1; |
254 | } | |
255 | ||
256 | int lxc_monitor_read_timeout(int fd, struct lxc_msg *msg, int timeout) | |
257 | { | |
2366b8a7 | 258 | struct pollfd fds; |
8d06bd13 | 259 | |
2366b8a7 SH |
260 | fds.fd = fd; |
261 | fds.events = POLLIN | POLLPRI; | |
262 | fds.revents = 0; | |
8d06bd13 | 263 | |
2366b8a7 | 264 | return lxc_monitor_read_fdset(&fds, 1, msg, timeout); |
0ad19a3f | 265 | } |
266 | ||
72d0e1cb SG |
267 | int lxc_monitor_read(int fd, struct lxc_msg *msg) |
268 | { | |
269 | return lxc_monitor_read_timeout(fd, msg, -1); | |
270 | } | |
271 | ||
45e854dc | 272 | #define LXC_MONITORD_PATH LIBEXECDIR "/lxc/lxc-monitord" |
e51d4895 | 273 | |
292b1d17 CB |
274 | /* Used to spawn a monitord either on startup of a daemon container, or when |
275 | * lxc-monitor starts. | |
e51d4895 DE |
276 | */ |
277 | int lxc_monitord_spawn(const char *lxcpath) | |
0ad19a3f | 278 | { |
487b14b6 | 279 | int ret; |
e51d4895 | 280 | int pipefd[2]; |
397a8d30 | 281 | char pipefd_str[INTTYPE_TO_STRLEN(int)]; |
487b14b6 | 282 | pid_t pid1, pid2; |
e51d4895 | 283 | |
292b1d17 | 284 | char *const args[] = { |
457e3c5d | 285 | LXC_MONITORD_PATH, |
286 | (char *)lxcpath, | |
287 | pipefd_str, | |
288 | NULL, | |
e51d4895 DE |
289 | }; |
290 | ||
291 | /* double fork to avoid zombies when monitord exits */ | |
292 | pid1 = fork(); | |
293 | if (pid1 < 0) { | |
6dd32d35 | 294 | SYSERROR("Failed to fork()"); |
e51d4895 DE |
295 | return -1; |
296 | } | |
297 | ||
298 | if (pid1) { | |
6dd32d35 | 299 | DEBUG("Going to wait for pid %d", pid1); |
457e3c5d | 300 | |
f2bbe86d DE |
301 | if (waitpid(pid1, NULL, 0) != pid1) |
302 | return -1; | |
457e3c5d | 303 | |
6dd32d35 | 304 | DEBUG("Finished waiting on pid %d", pid1); |
e51d4895 DE |
305 | return 0; |
306 | } | |
307 | ||
308 | if (pipe(pipefd) < 0) { | |
6dd32d35 | 309 | SYSERROR("Failed to create pipe"); |
f15e4fd2 | 310 | _exit(EXIT_FAILURE); |
e51d4895 DE |
311 | } |
312 | ||
313 | pid2 = fork(); | |
314 | if (pid2 < 0) { | |
6dd32d35 | 315 | SYSERROR("Failed to fork()"); |
f15e4fd2 | 316 | _exit(EXIT_FAILURE); |
e51d4895 | 317 | } |
292b1d17 | 318 | |
e51d4895 | 319 | if (pid2) { |
6dd32d35 | 320 | DEBUG("Trying to sync with child process"); |
e51d4895 | 321 | char c; |
292b1d17 | 322 | /* Wait for daemon to create socket. */ |
e51d4895 | 323 | close(pipefd[1]); |
292b1d17 CB |
324 | |
325 | /* Sync with child, we're ignoring the return from read | |
e51d4895 DE |
326 | * because regardless if it works or not, either way we've |
327 | * synced with the child process. the if-empty-statement | |
328 | * construct is to quiet the warn-unused-result warning. | |
329 | */ | |
2a71131d | 330 | if (lxc_read_nointr(pipefd[0], &c, 1)) |
8f47bc3f | 331 | ; |
292b1d17 | 332 | |
e51d4895 | 333 | close(pipefd[0]); |
292b1d17 | 334 | |
6dd32d35 | 335 | DEBUG("Successfully synced with child process"); |
f15e4fd2 | 336 | _exit(EXIT_SUCCESS); |
e51d4895 DE |
337 | } |
338 | ||
e51d4895 | 339 | if (setsid() < 0) { |
6dd32d35 | 340 | SYSERROR("Failed to setsid()"); |
f15e4fd2 | 341 | _exit(EXIT_FAILURE); |
e51d4895 | 342 | } |
292b1d17 | 343 | |
47a46cf1 | 344 | lxc_check_inherited(NULL, true, &pipefd[1], 1); |
aec1ea62 | 345 | if (null_stdfds() < 0) { |
6dd32d35 | 346 | SYSERROR("Failed to dup2() standard file descriptors to /dev/null"); |
f15e4fd2 | 347 | _exit(EXIT_FAILURE); |
aec1ea62 | 348 | } |
292b1d17 | 349 | |
e51d4895 | 350 | close(pipefd[0]); |
292b1d17 | 351 | |
3ca3b230 CB |
352 | ret = strnprintf(pipefd_str, sizeof(pipefd_str), "%d", pipefd[1]); |
353 | if (ret < 0) { | |
6dd32d35 | 354 | ERROR("Failed to create pid argument to pass to monitord"); |
f15e4fd2 | 355 | _exit(EXIT_FAILURE); |
aec1ea62 | 356 | } |
487b14b6 | 357 | |
6dd32d35 | 358 | DEBUG("Using pipe file descriptor %d for monitord", pipefd[1]); |
292b1d17 | 359 | |
e51d4895 | 360 | execvp(args[0], args); |
6dd32d35 | 361 | SYSERROR("Failed to exec lxc-monitord"); |
292b1d17 | 362 | |
f15e4fd2 | 363 | _exit(EXIT_FAILURE); |
0ad19a3f | 364 | } |