]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/monitor.c
Merge pull request #2172 from stgraber/master
[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 #include <errno.h>
26 #include <fcntl.h>
27 #include <inttypes.h>
28 #include <poll.h>
29 #include <stddef.h>
30 #include <stdint.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <unistd.h>
35 #include <net/if.h>
36 #include <netinet/in.h>
37 #include <sys/param.h>
38 #include <sys/socket.h>
39 #include <sys/stat.h>
40 #include <sys/types.h>
41 #include <sys/wait.h>
42
43 #include "config.h"
44 #include "af_unix.h"
45 #include "error.h"
46 #include "log.h"
47 #include "lxclock.h"
48 #include "monitor.h"
49 #include "state.h"
50 #include "utils.h"
51
52 lxc_log_define(lxc_monitor, lxc);
53
54 /* routines used by monitor publishers (containers) */
55 int lxc_monitor_fifo_name(const char *lxcpath, char *fifo_path, size_t fifo_path_sz,
56 int do_mkdirp)
57 {
58 int ret;
59 char *rundir;
60
61 rundir = get_rundir();
62 if (!rundir)
63 return -1;
64
65 if (do_mkdirp) {
66 ret = snprintf(fifo_path, fifo_path_sz, "%s/lxc/%s", rundir, lxcpath);
67 if (ret < 0 || (size_t)ret >= fifo_path_sz) {
68 ERROR("rundir/lxcpath (%s/%s) too long for monitor fifo.", rundir, lxcpath);
69 free(rundir);
70 return -1;
71 }
72 ret = mkdir_p(fifo_path, 0755);
73 if (ret < 0) {
74 ERROR("Unable to create monitor fifo directory %s.", fifo_path);
75 free(rundir);
76 return ret;
77 }
78 }
79 ret = snprintf(fifo_path, fifo_path_sz, "%s/lxc/%s/monitor-fifo", rundir, lxcpath);
80 if (ret < 0 || (size_t)ret >= fifo_path_sz) {
81 ERROR("rundir/lxcpath (%s/%s) too long for monitor fifo.", rundir, lxcpath);
82 free(rundir);
83 return -1;
84 }
85 free(rundir);
86 return 0;
87 }
88
89 static void lxc_monitor_fifo_send(struct lxc_msg *msg, const char *lxcpath)
90 {
91 int fd,ret;
92 char fifo_path[PATH_MAX];
93
94 BUILD_BUG_ON(sizeof(*msg) > PIPE_BUF); /* write not guaranteed atomic */
95
96 ret = lxc_monitor_fifo_name(lxcpath, fifo_path, sizeof(fifo_path), 0);
97 if (ret < 0)
98 return;
99
100 /* Open the fifo nonblock in case the monitor is dead, we don't want the
101 * open to wait for a reader since it may never come.
102 */
103 fd = open(fifo_path, O_WRONLY | O_NONBLOCK);
104 if (fd < 0) {
105 /* It is normal for this open() to fail with ENXIO when there is
106 * no monitor running, so we don't log it.
107 */
108 if (errno == ENXIO || errno == ENOENT)
109 return;
110
111 WARN("%s - Failed to open fifo to send message", strerror(errno));
112 return;
113 }
114
115 if (fcntl(fd, F_SETFL, O_WRONLY) < 0) {
116 close(fd);
117 return;
118 }
119
120 ret = write(fd, msg, sizeof(*msg));
121 if (ret != sizeof(*msg)) {
122 close(fd);
123 SYSERROR("Failed to write to monitor fifo \"%s\".", fifo_path);
124 return;
125 }
126
127 close(fd);
128 }
129
130 void lxc_monitor_send_state(const char *name, lxc_state_t state,
131 const char *lxcpath)
132 {
133 struct lxc_msg msg = {.type = lxc_msg_state, .value = state};
134 strncpy(msg.name, name, sizeof(msg.name));
135 msg.name[sizeof(msg.name) - 1] = 0;
136
137 lxc_monitor_fifo_send(&msg, lxcpath);
138 }
139
140 void lxc_monitor_send_exit_code(const char *name, int exit_code,
141 const char *lxcpath)
142 {
143 struct lxc_msg msg = {.type = lxc_msg_exit_code, .value = exit_code};
144 strncpy(msg.name, name, sizeof(msg.name));
145 msg.name[sizeof(msg.name) - 1] = 0;
146
147 lxc_monitor_fifo_send(&msg, lxcpath);
148 }
149
150 /* routines used by monitor subscribers (lxc-monitor) */
151 int lxc_monitor_close(int fd)
152 {
153 return close(fd);
154 }
155
156 /* Enforces \0-termination for the abstract unix socket. This is not required
157 * but allows us to print it out.
158 *
159 * Older version of liblxc only allowed for 105 bytes to be used for the
160 * abstract unix domain socket name because the code for our abstract unix
161 * socket handling performed invalid checks. Since we \0-terminate we could now
162 * have a maximum of 106 chars. But to not break backwards compatibility we keep
163 * the limit at 105.
164 */
165 int lxc_monitor_sock_name(const char *lxcpath, struct sockaddr_un *addr) {
166 size_t len;
167 int ret;
168 char *path;
169 uint64_t hash;
170
171 /* addr.sun_path is only 108 bytes, so we hash the full name and
172 * then append as much of the name as we can fit.
173 */
174 memset(addr, 0, sizeof(*addr));
175 addr->sun_family = AF_UNIX;
176
177 /* strlen("lxc/") + strlen("/monitor-sock") + 1 = 18 */
178 len = strlen(lxcpath) + 18;
179 path = alloca(len);
180 ret = snprintf(path, len, "lxc/%s/monitor-sock", lxcpath);
181 if (ret < 0 || (size_t)ret >= len) {
182 ERROR("failed to create name for monitor socket");
183 return -1;
184 }
185
186 /* Note: snprintf() will \0-terminate addr->sun_path on the 106th byte
187 * and so the abstract socket name has 105 "meaningful" characters. This
188 * is absolutely intentional. For further info read the comment for this
189 * function above!
190 */
191 len = sizeof(addr->sun_path) - 1;
192 hash = fnv_64a_buf(path, ret, FNV1A_64_INIT);
193 ret = snprintf(addr->sun_path, len, "@lxc/%016" PRIx64 "/%s", hash, lxcpath);
194 if (ret < 0) {
195 ERROR("failed to create hashed name for monitor socket");
196 return -1;
197 }
198
199 /* replace @ with \0 */
200 addr->sun_path[0] = '\0';
201 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);
202
203 return 0;
204 }
205
206 int lxc_monitor_open(const char *lxcpath)
207 {
208 struct sockaddr_un addr;
209 int fd;
210 size_t retry;
211 size_t len;
212 int backoff_ms[] = {10, 50, 100};
213
214 if (lxc_monitor_sock_name(lxcpath, &addr) < 0)
215 return -1;
216
217 fd = socket(PF_UNIX, SOCK_STREAM, 0);
218 if (fd < 0) {
219 ERROR("Failed to create socket: %s.", strerror(errno));
220 return -1;
221 }
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 ERROR("name of monitor socket too long (%zu bytes): %s", len, strerror(errno));
228 close(fd);
229 return -1;
230 }
231
232 for (retry = 0; retry < sizeof(backoff_ms) / sizeof(backoff_ms[0]); retry++) {
233 fd = lxc_abstract_unix_connect(addr.sun_path);
234 if (fd != -1 || errno != ECONNREFUSED)
235 break;
236 ERROR("Failed to connect to monitor socket. Retrying in %d ms: %s", backoff_ms[retry], strerror(errno));
237 usleep(backoff_ms[retry] * 1000);
238 }
239
240 if (fd < 0) {
241 ERROR("Failed to connect to monitor socket: %s.", strerror(errno));
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?: %s.", strerror(errno));
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[LXC_NUMSTRLEN64];
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 if (waitpid(pid1, NULL, 0) != pid1)
325 return -1;
326 DEBUG("Finished waiting on pid %d.", pid1);
327 return 0;
328 }
329
330 if (pipe(pipefd) < 0) {
331 SYSERROR("Failed to create pipe.");
332 exit(EXIT_FAILURE);
333 }
334
335 pid2 = fork();
336 if (pid2 < 0) {
337 SYSERROR("Failed to fork().");
338 exit(EXIT_FAILURE);
339 }
340
341 if (pid2) {
342 DEBUG("Trying to sync with child process.");
343 char c;
344 /* Wait for daemon to create socket. */
345 close(pipefd[1]);
346
347 /* Sync with child, we're ignoring the return from read
348 * because regardless if it works or not, either way we've
349 * synced with the child process. the if-empty-statement
350 * construct is to quiet the warn-unused-result warning.
351 */
352 if (read(pipefd[0], &c, 1))
353 ;
354
355 close(pipefd[0]);
356
357 DEBUG("Successfully synced with child process.");
358 exit(EXIT_SUCCESS);
359 }
360
361 if (setsid() < 0) {
362 SYSERROR("Failed to setsid().");
363 exit(EXIT_FAILURE);
364 }
365
366 lxc_check_inherited(NULL, true, &pipefd[1], 1);
367 if (null_stdfds() < 0) {
368 SYSERROR("Failed to dup2() standard file descriptors to /dev/null.");
369 exit(EXIT_FAILURE);
370 }
371
372 close(pipefd[0]);
373
374 ret = snprintf(pipefd_str, LXC_NUMSTRLEN64, "%d", pipefd[1]);
375 if (ret < 0 || ret >= LXC_NUMSTRLEN64) {
376 ERROR("Failed to create pid argument to pass to monitord.");
377 exit(EXIT_FAILURE);
378 }
379
380 DEBUG("Using pipe file descriptor %d for monitord.", pipefd[1]);
381
382 execvp(args[0], args);
383 SYSERROR("failed to exec lxc-monitord");
384
385 exit(EXIT_FAILURE);
386 }