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