]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/monitor.c
monitor: log cleanups
[mirror_lxc.git] / src / lxc / monitor.c
1 /*
2 * lxc: linux Container library
3 *
4 * (C) Copyright IBM Corp. 2007, 2008
5 *
6 * Authors:
7 * Daniel Lezcano <daniel.lezcano at free.fr>
8 * Dwight Engen <dwight.engen@oracle.com>
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
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25 #ifndef _GNU_SOURCE
26 #define _GNU_SOURCE 1
27 #endif
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <inttypes.h>
31 #include <net/if.h>
32 #include <netinet/in.h>
33 #include <poll.h>
34 #include <stddef.h>
35 #include <stdint.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <sys/param.h>
40 #include <sys/socket.h>
41 #include <sys/stat.h>
42 #include <sys/types.h>
43 #include <sys/wait.h>
44 #include <unistd.h>
45
46 #include "af_unix.h"
47 #include "config.h"
48 #include "error.h"
49 #include "log.h"
50 #include "lxclock.h"
51 #include "macro.h"
52 #include "monitor.h"
53 #include "state.h"
54 #include "utils.h"
55
56 #ifndef HAVE_STRLCPY
57 #include "include/strlcpy.h"
58 #endif
59
60 lxc_log_define(monitor, lxc);
61
62 /* routines used by monitor publishers (containers) */
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;
67 char *rundir;
68
69 rundir = get_rundir();
70 if (!rundir)
71 return -1;
72
73 if (do_mkdirp) {
74 ret = snprintf(fifo_path, fifo_path_sz, "%s/lxc/%s", rundir, lxcpath);
75 if (ret < 0 || (size_t)ret >= fifo_path_sz) {
76 ERROR("rundir/lxcpath (%s/%s) too long for monitor fifo", rundir, lxcpath);
77 free(rundir);
78 return -1;
79 }
80 ret = mkdir_p(fifo_path, 0755);
81 if (ret < 0) {
82 ERROR("Unable to create monitor fifo directory %s.", fifo_path);
83 free(rundir);
84 return ret;
85 }
86 }
87 ret = snprintf(fifo_path, fifo_path_sz, "%s/lxc/%s/monitor-fifo", rundir, lxcpath);
88 if (ret < 0 || (size_t)ret >= fifo_path_sz) {
89 ERROR("rundir/lxcpath (%s/%s) too long for monitor fifo", rundir, lxcpath);
90 free(rundir);
91 return -1;
92 }
93 free(rundir);
94 return 0;
95 }
96
97 static void lxc_monitor_fifo_send(struct lxc_msg *msg, const char *lxcpath)
98 {
99 int fd,ret;
100 char fifo_path[PATH_MAX];
101
102 BUILD_BUG_ON(sizeof(*msg) > PIPE_BUF); /* write not guaranteed atomic */
103
104 ret = lxc_monitor_fifo_name(lxcpath, fifo_path, sizeof(fifo_path), 0);
105 if (ret < 0)
106 return;
107
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.
110 */
111 fd = open(fifo_path, O_WRONLY | O_NONBLOCK);
112 if (fd < 0) {
113 /* It is normal for this open() to fail with ENXIO when there is
114 * no monitor running, so we don't log it.
115 */
116 if (errno == ENXIO || errno == ENOENT)
117 return;
118
119 SYSWARN("Failed to open fifo to send message");
120 return;
121 }
122
123 if (fcntl(fd, F_SETFL, O_WRONLY) < 0) {
124 close(fd);
125 return;
126 }
127
128 ret = lxc_write_nointr(fd, msg, sizeof(*msg));
129 if (ret != sizeof(*msg)) {
130 close(fd);
131 SYSERROR("Failed to write to monitor fifo \"%s\"", fifo_path);
132 return;
133 }
134
135 close(fd);
136 }
137
138 void lxc_monitor_send_state(const char *name, lxc_state_t state,
139 const char *lxcpath)
140 {
141 struct lxc_msg msg = {.type = lxc_msg_state, .value = state};
142
143 (void)strlcpy(msg.name, name, sizeof(msg.name));
144 lxc_monitor_fifo_send(&msg, lxcpath);
145 }
146
147 void lxc_monitor_send_exit_code(const char *name, int exit_code,
148 const char *lxcpath)
149 {
150 struct lxc_msg msg = {.type = lxc_msg_exit_code, .value = exit_code};
151
152 (void)strlcpy(msg.name, name, sizeof(msg.name));
153 lxc_monitor_fifo_send(&msg, lxcpath);
154 }
155
156 /* routines used by monitor subscribers (lxc-monitor) */
157 int lxc_monitor_close(int fd)
158 {
159 return close(fd);
160 }
161
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 */
171 int lxc_monitor_sock_name(const char *lxcpath, struct sockaddr_un *addr) {
172 size_t len;
173 int ret;
174 char *path;
175 uint64_t hash;
176
177 /* addr.sun_path is only 108 bytes, so we hash the full name and
178 * then append as much of the name as we can fit.
179 */
180 memset(addr, 0, sizeof(*addr));
181 addr->sun_family = AF_UNIX;
182
183 /* strlen("lxc/") + strlen("/monitor-sock") + 1 = 18 */
184 len = strlen(lxcpath) + 18;
185 path = alloca(len);
186 ret = snprintf(path, len, "lxc/%s/monitor-sock", lxcpath);
187 if (ret < 0 || (size_t)ret >= len) {
188 ERROR("Failed to create name for monitor socket");
189 return -1;
190 }
191
192 /* Note: snprintf() will \0-terminate addr->sun_path on the 106th byte
193 * and so the abstract socket name has 105 "meaningful" characters. This
194 * is absolutely intentional. For further info read the comment for this
195 * function above!
196 */
197 len = sizeof(addr->sun_path) - 1;
198 hash = fnv_64a_buf(path, ret, FNV1A_64_INIT);
199 ret = snprintf(addr->sun_path, len, "@lxc/%016" PRIx64 "/%s", hash, lxcpath);
200 if (ret < 0) {
201 ERROR("Failed to create hashed name for monitor socket");
202 return -1;
203 }
204
205 /* replace @ with \0 */
206 addr->sun_path[0] = '\0';
207 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);
208
209 return 0;
210 }
211
212 int lxc_monitor_open(const char *lxcpath)
213 {
214 struct sockaddr_un addr;
215 int fd;
216 size_t retry;
217 size_t len;
218 int backoff_ms[] = {10, 50, 100};
219
220 if (lxc_monitor_sock_name(lxcpath, &addr) < 0)
221 return -1;
222
223 len = strlen(&addr.sun_path[1]);
224 DEBUG("Opening monitor socket %s with len %zu", &addr.sun_path[1], len);
225 if (len >= sizeof(addr.sun_path) - 1) {
226 errno = ENAMETOOLONG;
227 SYSERROR("The name of monitor socket too long (%zu bytes)", len);
228 return -1;
229 }
230
231 for (retry = 0; retry < sizeof(backoff_ms) / sizeof(backoff_ms[0]); retry++) {
232 fd = lxc_abstract_unix_connect(addr.sun_path);
233 if (fd != -1 || errno != ECONNREFUSED)
234 break;
235
236 SYSERROR("Failed to connect to monitor socket. Retrying in %d ms", backoff_ms[retry]);
237 usleep(backoff_ms[retry] * 1000);
238 }
239
240 if (fd < 0) {
241 SYSERROR("Failed to connect to monitor socket");
242 return -1;
243 }
244
245 return fd;
246 }
247
248 int lxc_monitor_read_fdset(struct pollfd *fds, nfds_t nfds, struct lxc_msg *msg,
249 int timeout)
250 {
251 long i;
252 int ret;
253
254 ret = poll(fds, nfds, timeout * 1000);
255 if (ret == -1)
256 return -1;
257 else if (ret == 0)
258 return -2; /* timed out */
259
260 /* Only read from the first ready fd, the others will remain ready for
261 * when this routine is called again.
262 */
263 for (i = 0; i < nfds; i++) {
264 if (fds[i].revents != 0) {
265 fds[i].revents = 0;
266 ret = recv(fds[i].fd, msg, sizeof(*msg), 0);
267 if (ret <= 0) {
268 SYSERROR("Failed to receive message. Did monitord die?");
269 return -1;
270 }
271 return ret;
272 }
273 }
274
275 SYSERROR("No ready fd found");
276
277 return -1;
278 }
279
280 int lxc_monitor_read_timeout(int fd, struct lxc_msg *msg, int timeout)
281 {
282 struct pollfd fds;
283
284 fds.fd = fd;
285 fds.events = POLLIN | POLLPRI;
286 fds.revents = 0;
287
288 return lxc_monitor_read_fdset(&fds, 1, msg, timeout);
289 }
290
291 int lxc_monitor_read(int fd, struct lxc_msg *msg)
292 {
293 return lxc_monitor_read_timeout(fd, msg, -1);
294 }
295
296 #define LXC_MONITORD_PATH LIBEXECDIR "/lxc/lxc-monitord"
297
298 /* Used to spawn a monitord either on startup of a daemon container, or when
299 * lxc-monitor starts.
300 */
301 int lxc_monitord_spawn(const char *lxcpath)
302 {
303 int ret;
304 int pipefd[2];
305 char pipefd_str[INTTYPE_TO_STRLEN(int)];
306 pid_t pid1, pid2;
307
308 char *const args[] = {
309 LXC_MONITORD_PATH,
310 (char *)lxcpath,
311 pipefd_str,
312 NULL,
313 };
314
315 /* double fork to avoid zombies when monitord exits */
316 pid1 = fork();
317 if (pid1 < 0) {
318 SYSERROR("Failed to fork()");
319 return -1;
320 }
321
322 if (pid1) {
323 DEBUG("Going to wait for pid %d", pid1);
324
325 if (waitpid(pid1, NULL, 0) != pid1)
326 return -1;
327
328 DEBUG("Finished waiting on pid %d", pid1);
329 return 0;
330 }
331
332 if (pipe(pipefd) < 0) {
333 SYSERROR("Failed to create pipe");
334 _exit(EXIT_FAILURE);
335 }
336
337 pid2 = fork();
338 if (pid2 < 0) {
339 SYSERROR("Failed to fork()");
340 _exit(EXIT_FAILURE);
341 }
342
343 if (pid2) {
344 DEBUG("Trying to sync with child process");
345 char c;
346 /* Wait for daemon to create socket. */
347 close(pipefd[1]);
348
349 /* Sync with child, we're ignoring the return from read
350 * because regardless if it works or not, either way we've
351 * synced with the child process. the if-empty-statement
352 * construct is to quiet the warn-unused-result warning.
353 */
354 if (lxc_read_nointr(pipefd[0], &c, 1))
355 ;
356
357 close(pipefd[0]);
358
359 DEBUG("Successfully synced with child process");
360 _exit(EXIT_SUCCESS);
361 }
362
363 if (setsid() < 0) {
364 SYSERROR("Failed to setsid()");
365 _exit(EXIT_FAILURE);
366 }
367
368 lxc_check_inherited(NULL, true, &pipefd[1], 1);
369 if (null_stdfds() < 0) {
370 SYSERROR("Failed to dup2() standard file descriptors to /dev/null");
371 _exit(EXIT_FAILURE);
372 }
373
374 close(pipefd[0]);
375
376 ret = snprintf(pipefd_str, sizeof(pipefd_str), "%d", pipefd[1]);
377 if (ret < 0 || ret >= sizeof(pipefd_str)) {
378 ERROR("Failed to create pid argument to pass to monitord");
379 _exit(EXIT_FAILURE);
380 }
381
382 DEBUG("Using pipe file descriptor %d for monitord", pipefd[1]);
383
384 execvp(args[0], args);
385 SYSERROR("Failed to exec lxc-monitord");
386
387 _exit(EXIT_FAILURE);
388 }