]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/monitor.c
tree-wide: s/strncpy()/strlcpy()/g
[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 #ifndef HAVE_STRLCPY
53 #include "include/strlcpy.h"
54 #endif
55
56 lxc_log_define(lxc_monitor, lxc);
57
58 /* routines used by monitor publishers (containers) */
59 int lxc_monitor_fifo_name(const char *lxcpath, char *fifo_path, size_t fifo_path_sz,
60 int do_mkdirp)
61 {
62 int ret;
63 char *rundir;
64
65 rundir = get_rundir();
66 if (!rundir)
67 return -1;
68
69 if (do_mkdirp) {
70 ret = snprintf(fifo_path, fifo_path_sz, "%s/lxc/%s", rundir, lxcpath);
71 if (ret < 0 || (size_t)ret >= fifo_path_sz) {
72 ERROR("rundir/lxcpath (%s/%s) too long for monitor fifo.", rundir, lxcpath);
73 free(rundir);
74 return -1;
75 }
76 ret = mkdir_p(fifo_path, 0755);
77 if (ret < 0) {
78 ERROR("Unable to create monitor fifo directory %s.", fifo_path);
79 free(rundir);
80 return ret;
81 }
82 }
83 ret = snprintf(fifo_path, fifo_path_sz, "%s/lxc/%s/monitor-fifo", rundir, lxcpath);
84 if (ret < 0 || (size_t)ret >= fifo_path_sz) {
85 ERROR("rundir/lxcpath (%s/%s) too long for monitor fifo.", rundir, lxcpath);
86 free(rundir);
87 return -1;
88 }
89 free(rundir);
90 return 0;
91 }
92
93 static void lxc_monitor_fifo_send(struct lxc_msg *msg, const char *lxcpath)
94 {
95 int fd,ret;
96 char fifo_path[PATH_MAX];
97
98 BUILD_BUG_ON(sizeof(*msg) > PIPE_BUF); /* write not guaranteed atomic */
99
100 ret = lxc_monitor_fifo_name(lxcpath, fifo_path, sizeof(fifo_path), 0);
101 if (ret < 0)
102 return;
103
104 /* Open the fifo nonblock in case the monitor is dead, we don't want the
105 * open to wait for a reader since it may never come.
106 */
107 fd = open(fifo_path, O_WRONLY | O_NONBLOCK);
108 if (fd < 0) {
109 /* It is normal for this open() to fail with ENXIO when there is
110 * no monitor running, so we don't log it.
111 */
112 if (errno == ENXIO || errno == ENOENT)
113 return;
114
115 WARN("%s - Failed to open fifo to send message", strerror(errno));
116 return;
117 }
118
119 if (fcntl(fd, F_SETFL, O_WRONLY) < 0) {
120 close(fd);
121 return;
122 }
123
124 ret = write(fd, msg, sizeof(*msg));
125 if (ret != sizeof(*msg)) {
126 close(fd);
127 SYSERROR("Failed to write to monitor fifo \"%s\".", fifo_path);
128 return;
129 }
130
131 close(fd);
132 }
133
134 void lxc_monitor_send_state(const char *name, lxc_state_t state,
135 const char *lxcpath)
136 {
137 struct lxc_msg msg = {.type = lxc_msg_state, .value = state};
138
139 (void)strlcpy(msg.name, name, sizeof(msg.name));
140 lxc_monitor_fifo_send(&msg, lxcpath);
141 }
142
143 void lxc_monitor_send_exit_code(const char *name, int exit_code,
144 const char *lxcpath)
145 {
146 struct lxc_msg msg = {.type = lxc_msg_exit_code, .value = exit_code};
147
148 (void)strlcpy(msg.name, name, sizeof(msg.name));
149 lxc_monitor_fifo_send(&msg, lxcpath);
150 }
151
152 /* routines used by monitor subscribers (lxc-monitor) */
153 int lxc_monitor_close(int fd)
154 {
155 return close(fd);
156 }
157
158 /* Enforces \0-termination for the abstract unix socket. This is not required
159 * but allows us to print it out.
160 *
161 * Older version of liblxc only allowed for 105 bytes to be used for the
162 * abstract unix domain socket name because the code for our abstract unix
163 * socket handling performed invalid checks. Since we \0-terminate we could now
164 * have a maximum of 106 chars. But to not break backwards compatibility we keep
165 * the limit at 105.
166 */
167 int lxc_monitor_sock_name(const char *lxcpath, struct sockaddr_un *addr) {
168 size_t len;
169 int ret;
170 char *path;
171 uint64_t hash;
172
173 /* addr.sun_path is only 108 bytes, so we hash the full name and
174 * then append as much of the name as we can fit.
175 */
176 memset(addr, 0, sizeof(*addr));
177 addr->sun_family = AF_UNIX;
178
179 /* strlen("lxc/") + strlen("/monitor-sock") + 1 = 18 */
180 len = strlen(lxcpath) + 18;
181 path = alloca(len);
182 ret = snprintf(path, len, "lxc/%s/monitor-sock", lxcpath);
183 if (ret < 0 || (size_t)ret >= len) {
184 ERROR("failed to create name for monitor socket");
185 return -1;
186 }
187
188 /* Note: snprintf() will \0-terminate addr->sun_path on the 106th byte
189 * and so the abstract socket name has 105 "meaningful" characters. This
190 * is absolutely intentional. For further info read the comment for this
191 * function above!
192 */
193 len = sizeof(addr->sun_path) - 1;
194 hash = fnv_64a_buf(path, ret, FNV1A_64_INIT);
195 ret = snprintf(addr->sun_path, len, "@lxc/%016" PRIx64 "/%s", hash, lxcpath);
196 if (ret < 0) {
197 ERROR("failed to create hashed name for monitor socket");
198 return -1;
199 }
200
201 /* replace @ with \0 */
202 addr->sun_path[0] = '\0';
203 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);
204
205 return 0;
206 }
207
208 int lxc_monitor_open(const char *lxcpath)
209 {
210 struct sockaddr_un addr;
211 int fd;
212 size_t retry;
213 size_t len;
214 int backoff_ms[] = {10, 50, 100};
215
216 if (lxc_monitor_sock_name(lxcpath, &addr) < 0)
217 return -1;
218
219 fd = socket(PF_UNIX, SOCK_STREAM, 0);
220 if (fd < 0) {
221 ERROR("Failed to create socket: %s.", strerror(errno));
222 return -1;
223 }
224
225 len = strlen(&addr.sun_path[1]);
226 DEBUG("opening monitor socket %s with len %zu", &addr.sun_path[1], len);
227 if (len >= sizeof(addr.sun_path) - 1) {
228 errno = ENAMETOOLONG;
229 ERROR("name of monitor socket too long (%zu bytes): %s", len, strerror(errno));
230 close(fd);
231 return -1;
232 }
233
234 for (retry = 0; retry < sizeof(backoff_ms) / sizeof(backoff_ms[0]); retry++) {
235 fd = lxc_abstract_unix_connect(addr.sun_path);
236 if (fd != -1 || errno != ECONNREFUSED)
237 break;
238 ERROR("Failed to connect to monitor socket. Retrying in %d ms: %s", backoff_ms[retry], strerror(errno));
239 usleep(backoff_ms[retry] * 1000);
240 }
241
242 if (fd < 0) {
243 ERROR("Failed to connect to monitor socket: %s.", strerror(errno));
244 return -1;
245 }
246
247 return fd;
248 }
249
250 int lxc_monitor_read_fdset(struct pollfd *fds, nfds_t nfds, struct lxc_msg *msg,
251 int timeout)
252 {
253 long i;
254 int ret;
255
256 ret = poll(fds, nfds, timeout * 1000);
257 if (ret == -1)
258 return -1;
259 else if (ret == 0)
260 return -2; /* timed out */
261
262 /* Only read from the first ready fd, the others will remain ready for
263 * when this routine is called again.
264 */
265 for (i = 0; i < nfds; i++) {
266 if (fds[i].revents != 0) {
267 fds[i].revents = 0;
268 ret = recv(fds[i].fd, msg, sizeof(*msg), 0);
269 if (ret <= 0) {
270 SYSERROR("Failed to receive message. Did monitord die?: %s.", strerror(errno));
271 return -1;
272 }
273 return ret;
274 }
275 }
276
277 SYSERROR("No ready fd found.");
278
279 return -1;
280 }
281
282 int lxc_monitor_read_timeout(int fd, struct lxc_msg *msg, int timeout)
283 {
284 struct pollfd fds;
285
286 fds.fd = fd;
287 fds.events = POLLIN | POLLPRI;
288 fds.revents = 0;
289
290 return lxc_monitor_read_fdset(&fds, 1, msg, timeout);
291 }
292
293 int lxc_monitor_read(int fd, struct lxc_msg *msg)
294 {
295 return lxc_monitor_read_timeout(fd, msg, -1);
296 }
297
298 #define LXC_MONITORD_PATH LIBEXECDIR "/lxc/lxc-monitord"
299
300 /* Used to spawn a monitord either on startup of a daemon container, or when
301 * lxc-monitor starts.
302 */
303 int lxc_monitord_spawn(const char *lxcpath)
304 {
305 int ret;
306 int pipefd[2];
307 char pipefd_str[LXC_NUMSTRLEN64];
308 pid_t pid1, pid2;
309
310 char *const args[] = {
311 LXC_MONITORD_PATH,
312 (char *)lxcpath,
313 pipefd_str,
314 NULL,
315 };
316
317 /* double fork to avoid zombies when monitord exits */
318 pid1 = fork();
319 if (pid1 < 0) {
320 SYSERROR("Failed to fork().");
321 return -1;
322 }
323
324 if (pid1) {
325 DEBUG("Going to wait for pid %d.", pid1);
326 if (waitpid(pid1, NULL, 0) != pid1)
327 return -1;
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 (read(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, LXC_NUMSTRLEN64, "%d", pipefd[1]);
377 if (ret < 0 || ret >= LXC_NUMSTRLEN64) {
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 }