]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/monitor.c
monitor: s/read()/lxc_read_nointr()/g
[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
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
ac2cecc4 57lxc_log_define(monitor, lxc);
0ad19a3f 58
e51d4895 59/* routines used by monitor publishers (containers) */
9e60f51d
DE
60int 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 94static 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
135void 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
144void 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) */
154int 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
168int 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
209int 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;
6d1400b5 224 SYSERROR("The name of monitor socket too long (%zu bytes)", len);
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;
6d1400b5 232
233 SYSERROR("Failed to connect to monitor socket. Retrying in %d ms", backoff_ms[retry]);
e51d4895 234 usleep(backoff_ms[retry] * 1000);
0ad19a3f 235 }
236
fcaef9c7 237 if (fd < 0) {
6d1400b5 238 SYSERROR("Failed to connect to monitor socket");
c8dcf778 239 return -1;
e51d4895 240 }
292b1d17 241
0ad19a3f 242 return fd;
243}
244
2366b8a7 245int lxc_monitor_read_fdset(struct pollfd *fds, nfds_t nfds, struct lxc_msg *msg,
8d06bd13 246 int timeout)
0ad19a3f 247{
2366b8a7
SH
248 long i;
249 int ret;
0ad19a3f 250
2366b8a7 251 ret = poll(fds, nfds, timeout * 1000);
8d06bd13 252 if (ret == -1)
75b1e198 253 return -1;
8d06bd13 254 else if (ret == 0)
1a0e70ac 255 return -2; /* timed out */
8d06bd13 256
292b1d17
CB
257 /* Only read from the first ready fd, the others will remain ready for
258 * when this routine is called again.
8d06bd13
DE
259 */
260 for (i = 0; i < nfds; i++) {
2366b8a7
SH
261 if (fds[i].revents != 0) {
262 fds[i].revents = 0;
263 ret = recv(fds[i].fd, msg, sizeof(*msg), 0);
8d06bd13 264 if (ret <= 0) {
b5be6a7c 265 SYSERROR("Failed to receive message. Did monitord die?");
8d06bd13
DE
266 return -1;
267 }
268 return ret;
269 }
0ad19a3f 270 }
292b1d17
CB
271
272 SYSERROR("No ready fd found.");
273
8d06bd13
DE
274 return -1;
275}
276
277int lxc_monitor_read_timeout(int fd, struct lxc_msg *msg, int timeout)
278{
2366b8a7 279 struct pollfd fds;
8d06bd13 280
2366b8a7
SH
281 fds.fd = fd;
282 fds.events = POLLIN | POLLPRI;
283 fds.revents = 0;
8d06bd13 284
2366b8a7 285 return lxc_monitor_read_fdset(&fds, 1, msg, timeout);
0ad19a3f 286}
287
72d0e1cb
SG
288int lxc_monitor_read(int fd, struct lxc_msg *msg)
289{
290 return lxc_monitor_read_timeout(fd, msg, -1);
291}
292
45e854dc 293#define LXC_MONITORD_PATH LIBEXECDIR "/lxc/lxc-monitord"
e51d4895 294
292b1d17
CB
295/* Used to spawn a monitord either on startup of a daemon container, or when
296 * lxc-monitor starts.
e51d4895
DE
297 */
298int lxc_monitord_spawn(const char *lxcpath)
0ad19a3f 299{
487b14b6 300 int ret;
e51d4895 301 int pipefd[2];
eab15c1e 302 char pipefd_str[LXC_NUMSTRLEN64];
487b14b6 303 pid_t pid1, pid2;
e51d4895 304
292b1d17 305 char *const args[] = {
457e3c5d 306 LXC_MONITORD_PATH,
307 (char *)lxcpath,
308 pipefd_str,
309 NULL,
e51d4895
DE
310 };
311
312 /* double fork to avoid zombies when monitord exits */
313 pid1 = fork();
314 if (pid1 < 0) {
292b1d17 315 SYSERROR("Failed to fork().");
e51d4895
DE
316 return -1;
317 }
318
319 if (pid1) {
292b1d17 320 DEBUG("Going to wait for pid %d.", pid1);
457e3c5d 321
f2bbe86d
DE
322 if (waitpid(pid1, NULL, 0) != pid1)
323 return -1;
457e3c5d 324
487b14b6 325 DEBUG("Finished waiting on pid %d.", pid1);
e51d4895
DE
326 return 0;
327 }
328
329 if (pipe(pipefd) < 0) {
292b1d17 330 SYSERROR("Failed to create pipe.");
f15e4fd2 331 _exit(EXIT_FAILURE);
e51d4895
DE
332 }
333
334 pid2 = fork();
335 if (pid2 < 0) {
292b1d17 336 SYSERROR("Failed to fork().");
f15e4fd2 337 _exit(EXIT_FAILURE);
e51d4895 338 }
292b1d17 339
e51d4895 340 if (pid2) {
5cc0f22d 341 DEBUG("Trying to sync with child process.");
e51d4895 342 char c;
292b1d17 343 /* Wait for daemon to create socket. */
e51d4895 344 close(pipefd[1]);
292b1d17
CB
345
346 /* Sync with child, we're ignoring the return from read
e51d4895
DE
347 * because regardless if it works or not, either way we've
348 * synced with the child process. the if-empty-statement
349 * construct is to quiet the warn-unused-result warning.
350 */
2a71131d 351 if (lxc_read_nointr(pipefd[0], &c, 1))
8f47bc3f 352 ;
292b1d17 353
e51d4895 354 close(pipefd[0]);
292b1d17 355
073000e2 356 DEBUG("Successfully synced with child process.");
f15e4fd2 357 _exit(EXIT_SUCCESS);
e51d4895
DE
358 }
359
e51d4895 360 if (setsid() < 0) {
292b1d17 361 SYSERROR("Failed to setsid().");
f15e4fd2 362 _exit(EXIT_FAILURE);
e51d4895 363 }
292b1d17 364
47a46cf1 365 lxc_check_inherited(NULL, true, &pipefd[1], 1);
aec1ea62
CB
366 if (null_stdfds() < 0) {
367 SYSERROR("Failed to dup2() standard file descriptors to /dev/null.");
f15e4fd2 368 _exit(EXIT_FAILURE);
aec1ea62 369 }
292b1d17 370
e51d4895 371 close(pipefd[0]);
292b1d17 372
eab15c1e
CB
373 ret = snprintf(pipefd_str, LXC_NUMSTRLEN64, "%d", pipefd[1]);
374 if (ret < 0 || ret >= LXC_NUMSTRLEN64) {
aec1ea62 375 ERROR("Failed to create pid argument to pass to monitord.");
f15e4fd2 376 _exit(EXIT_FAILURE);
aec1ea62 377 }
487b14b6
CB
378
379 DEBUG("Using pipe file descriptor %d for monitord.", pipefd[1]);
292b1d17 380
e51d4895 381 execvp(args[0], args);
00cccc8b 382 SYSERROR("failed to exec lxc-monitord");
292b1d17 383
f15e4fd2 384 _exit(EXIT_FAILURE);
0ad19a3f 385}