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