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