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