]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/monitor.c
Merge pull request #3059 from brauner/2019-06-21/seccomp_notify
[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
d38dd64a
CB
25#ifndef _GNU_SOURCE
26#define _GNU_SOURCE 1
27#endif
0ad19a3f 28#include <errno.h>
0ad19a3f 29#include <fcntl.h>
b45c7011 30#include <inttypes.h>
d38dd64a
CB
31#include <net/if.h>
32#include <netinet/in.h>
292b1d17
CB
33#include <poll.h>
34#include <stddef.h>
b45c7011 35#include <stdint.h>
292b1d17
CB
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
0ad19a3f 39#include <sys/param.h>
0ad19a3f 40#include <sys/socket.h>
292b1d17
CB
41#include <sys/stat.h>
42#include <sys/types.h>
e51d4895 43#include <sys/wait.h>
d38dd64a 44#include <unistd.h>
b113348e 45
31c53c2e 46#include "af_unix.h"
d38dd64a 47#include "config.h"
292b1d17 48#include "error.h"
f2363e38
ÇO
49#include "log.h"
50#include "lxclock.h"
397a8d30 51#include "macro.h"
54b43e8f 52#include "memory_utils.h"
f2363e38 53#include "monitor.h"
292b1d17 54#include "state.h"
f2363e38 55#include "utils.h"
36eb9bde 56
9de31d5a
CB
57#ifndef HAVE_STRLCPY
58#include "include/strlcpy.h"
59#endif
60
ac2cecc4 61lxc_log_define(monitor, lxc);
0ad19a3f 62
e51d4895 63/* routines used by monitor publishers (containers) */
9e60f51d
DE
64int lxc_monitor_fifo_name(const char *lxcpath, char *fifo_path, size_t fifo_path_sz,
65 int do_mkdirp)
66{
67 int ret;
44b9ae4b 68 char *rundir;
9e60f51d
DE
69
70 rundir = get_rundir();
97a696c6
SG
71 if (!rundir)
72 return -1;
73
9e60f51d
DE
74 if (do_mkdirp) {
75 ret = snprintf(fifo_path, fifo_path_sz, "%s/lxc/%s", rundir, lxcpath);
292b1d17 76 if (ret < 0 || (size_t)ret >= fifo_path_sz) {
6dd32d35 77 ERROR("rundir/lxcpath (%s/%s) too long for monitor fifo", rundir, lxcpath);
44b9ae4b 78 free(rundir);
9e60f51d
DE
79 return -1;
80 }
9e60f51d 81 ret = mkdir_p(fifo_path, 0755);
9e60f51d 82 if (ret < 0) {
47903908 83 ERROR("Unable to create monitor fifo directory %s", fifo_path);
44b9ae4b 84 free(rundir);
9e60f51d
DE
85 return ret;
86 }
87 }
88 ret = snprintf(fifo_path, fifo_path_sz, "%s/lxc/%s/monitor-fifo", rundir, lxcpath);
292b1d17 89 if (ret < 0 || (size_t)ret >= fifo_path_sz) {
6dd32d35 90 ERROR("rundir/lxcpath (%s/%s) too long for monitor fifo", rundir, lxcpath);
44b9ae4b 91 free(rundir);
9e60f51d
DE
92 return -1;
93 }
44b9ae4b 94 free(rundir);
9e60f51d
DE
95 return 0;
96}
97
e51d4895 98static void lxc_monitor_fifo_send(struct lxc_msg *msg, const char *lxcpath)
0ad19a3f 99{
e51d4895
DE
100 int fd,ret;
101 char fifo_path[PATH_MAX];
102
103 BUILD_BUG_ON(sizeof(*msg) > PIPE_BUF); /* write not guaranteed atomic */
9e60f51d
DE
104
105 ret = lxc_monitor_fifo_name(lxcpath, fifo_path, sizeof(fifo_path), 0);
106 if (ret < 0)
9123e471 107 return;
80f41298 108
292b1d17
CB
109 /* Open the fifo nonblock in case the monitor is dead, we don't want the
110 * open to wait for a reader since it may never come.
8bf1e61e 111 */
292b1d17 112 fd = open(fifo_path, O_WRONLY | O_NONBLOCK);
e51d4895 113 if (fd < 0) {
292b1d17
CB
114 /* It is normal for this open() to fail with ENXIO when there is
115 * no monitor running, so we don't log it.
e51d4895 116 */
2469f9b6 117 if (errno == ENXIO || errno == ENOENT)
292b1d17
CB
118 return;
119
a24c5678 120 SYSWARN("Failed to open fifo to send message");
31c53c2e 121 return;
e51d4895 122 }
0ad19a3f 123
92ffb6d8
DE
124 if (fcntl(fd, F_SETFL, O_WRONLY) < 0) {
125 close(fd);
8bf1e61e 126 return;
92ffb6d8 127 }
8bf1e61e 128
802e609a 129 ret = lxc_write_nointr(fd, msg, sizeof(*msg));
e51d4895 130 if (ret != sizeof(*msg)) {
e8b9ac8f 131 close(fd);
6dd32d35 132 SYSERROR("Failed to write to monitor fifo \"%s\"", fifo_path);
e51d4895
DE
133 return;
134 }
0ad19a3f 135
136 close(fd);
137}
138
292b1d17
CB
139void lxc_monitor_send_state(const char *name, lxc_state_t state,
140 const char *lxcpath)
eae6543d 141{
292b1d17 142 struct lxc_msg msg = {.type = lxc_msg_state, .value = state};
eae6543d 143
9de31d5a 144 (void)strlcpy(msg.name, name, sizeof(msg.name));
e51d4895 145 lxc_monitor_fifo_send(&msg, lxcpath);
0ad19a3f 146}
147
292b1d17
CB
148void lxc_monitor_send_exit_code(const char *name, int exit_code,
149 const char *lxcpath)
1787abca 150{
292b1d17 151 struct lxc_msg msg = {.type = lxc_msg_exit_code, .value = exit_code};
1787abca 152
9de31d5a 153 (void)strlcpy(msg.name, name, sizeof(msg.name));
1787abca
JTLB
154 lxc_monitor_fifo_send(&msg, lxcpath);
155}
156
e51d4895
DE
157/* routines used by monitor subscribers (lxc-monitor) */
158int lxc_monitor_close(int fd)
0ad19a3f 159{
dd1d77f9 160 return close(fd);
e51d4895
DE
161}
162
fcaef9c7
CB
163/* Enforces \0-termination for the abstract unix socket. This is not required
164 * but allows us to print it out.
165 *
166 * Older version of liblxc only allowed for 105 bytes to be used for the
167 * abstract unix domain socket name because the code for our abstract unix
168 * socket handling performed invalid checks. Since we \0-terminate we could now
169 * have a maximum of 106 chars. But to not break backwards compatibility we keep
170 * the limit at 105.
171 */
95e523c8 172int lxc_monitor_sock_name(const char *lxcpath, struct sockaddr_un *addr)
173{
2db56bd6 174 __do_free char *path = NULL;
e51d4895
DE
175 size_t len;
176 int ret;
b45c7011 177 uint64_t hash;
e51d4895 178
b45c7011
DE
179 /* addr.sun_path is only 108 bytes, so we hash the full name and
180 * then append as much of the name as we can fit.
9123e471 181 */
e51d4895
DE
182 memset(addr, 0, sizeof(*addr));
183 addr->sun_family = AF_UNIX;
292b1d17 184
fcaef9c7 185 /* strlen("lxc/") + strlen("/monitor-sock") + 1 = 18 */
073135ba 186 len = strlen(lxcpath) + 18;
54b43e8f 187 path = must_realloc(NULL, len);
073135ba 188 ret = snprintf(path, len, "lxc/%s/monitor-sock", lxcpath);
292b1d17 189 if (ret < 0 || (size_t)ret >= len) {
6dd32d35 190 ERROR("Failed to create name for monitor socket");
9e60f51d
DE
191 return -1;
192 }
9e60f51d 193
fcaef9c7
CB
194 /* Note: snprintf() will \0-terminate addr->sun_path on the 106th byte
195 * and so the abstract socket name has 105 "meaningful" characters. This
196 * is absolutely intentional. For further info read the comment for this
197 * function above!
198 */
073135ba 199 len = sizeof(addr->sun_path) - 1;
b45c7011 200 hash = fnv_64a_buf(path, ret, FNV1A_64_INIT);
fcaef9c7
CB
201 ret = snprintf(addr->sun_path, len, "@lxc/%016" PRIx64 "/%s", hash, lxcpath);
202 if (ret < 0) {
6dd32d35 203 ERROR("Failed to create hashed name for monitor socket");
2f126499 204 goto on_error;
205 } else if ((size_t)ret >= len) {
206 errno = ENAMETOOLONG;
207 SYSERROR("The name of monitor socket too long (%d bytes)", ret);
208 goto on_error;
fcaef9c7 209 }
292b1d17 210
fcaef9c7
CB
211 /* replace @ with \0 */
212 addr->sun_path[0] = '\0';
6dd32d35 213 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 214
e51d4895 215 return 0;
2f126499 216
217on_error:
218 return -1;
e51d4895 219}
0ad19a3f 220
e51d4895
DE
221int lxc_monitor_open(const char *lxcpath)
222{
223 struct sockaddr_un addr;
292b1d17
CB
224 int fd;
225 size_t retry;
fcaef9c7 226 int backoff_ms[] = {10, 50, 100};
e51d4895
DE
227
228 if (lxc_monitor_sock_name(lxcpath, &addr) < 0)
229 return -1;
230
2f126499 231 DEBUG("Opening monitor socket %s with len %zu", &addr.sun_path[1], strlen(&addr.sun_path[1]));
aae93dd3 232
292b1d17 233 for (retry = 0; retry < sizeof(backoff_ms) / sizeof(backoff_ms[0]); retry++) {
fcaef9c7 234 fd = lxc_abstract_unix_connect(addr.sun_path);
94bc08e9 235 if (fd != -1 || errno != ECONNREFUSED)
e51d4895 236 break;
6d1400b5 237
238 SYSERROR("Failed to connect to monitor socket. Retrying in %d ms", backoff_ms[retry]);
e51d4895 239 usleep(backoff_ms[retry] * 1000);
0ad19a3f 240 }
241
fcaef9c7 242 if (fd < 0) {
6d1400b5 243 SYSERROR("Failed to connect to monitor socket");
c8dcf778 244 return -1;
e51d4895 245 }
292b1d17 246
0ad19a3f 247 return fd;
248}
249
2366b8a7 250int lxc_monitor_read_fdset(struct pollfd *fds, nfds_t nfds, struct lxc_msg *msg,
8d06bd13 251 int timeout)
0ad19a3f 252{
2366b8a7
SH
253 long i;
254 int ret;
0ad19a3f 255
2366b8a7 256 ret = poll(fds, nfds, timeout * 1000);
8d06bd13 257 if (ret == -1)
75b1e198 258 return -1;
8d06bd13 259 else if (ret == 0)
1a0e70ac 260 return -2; /* timed out */
8d06bd13 261
292b1d17
CB
262 /* Only read from the first ready fd, the others will remain ready for
263 * when this routine is called again.
8d06bd13
DE
264 */
265 for (i = 0; i < nfds; i++) {
2366b8a7
SH
266 if (fds[i].revents != 0) {
267 fds[i].revents = 0;
268 ret = recv(fds[i].fd, msg, sizeof(*msg), 0);
8d06bd13 269 if (ret <= 0) {
b5be6a7c 270 SYSERROR("Failed to receive message. Did monitord die?");
8d06bd13
DE
271 return -1;
272 }
273 return ret;
274 }
0ad19a3f 275 }
292b1d17 276
6dd32d35 277 SYSERROR("No ready fd found");
292b1d17 278
8d06bd13
DE
279 return -1;
280}
281
282int lxc_monitor_read_timeout(int fd, struct lxc_msg *msg, int timeout)
283{
2366b8a7 284 struct pollfd fds;
8d06bd13 285
2366b8a7
SH
286 fds.fd = fd;
287 fds.events = POLLIN | POLLPRI;
288 fds.revents = 0;
8d06bd13 289
2366b8a7 290 return lxc_monitor_read_fdset(&fds, 1, msg, timeout);
0ad19a3f 291}
292
72d0e1cb
SG
293int lxc_monitor_read(int fd, struct lxc_msg *msg)
294{
295 return lxc_monitor_read_timeout(fd, msg, -1);
296}
297
45e854dc 298#define LXC_MONITORD_PATH LIBEXECDIR "/lxc/lxc-monitord"
e51d4895 299
292b1d17
CB
300/* Used to spawn a monitord either on startup of a daemon container, or when
301 * lxc-monitor starts.
e51d4895
DE
302 */
303int lxc_monitord_spawn(const char *lxcpath)
0ad19a3f 304{
487b14b6 305 int ret;
e51d4895 306 int pipefd[2];
397a8d30 307 char pipefd_str[INTTYPE_TO_STRLEN(int)];
487b14b6 308 pid_t pid1, pid2;
e51d4895 309
292b1d17 310 char *const args[] = {
457e3c5d 311 LXC_MONITORD_PATH,
312 (char *)lxcpath,
313 pipefd_str,
314 NULL,
e51d4895
DE
315 };
316
317 /* double fork to avoid zombies when monitord exits */
318 pid1 = fork();
319 if (pid1 < 0) {
6dd32d35 320 SYSERROR("Failed to fork()");
e51d4895
DE
321 return -1;
322 }
323
324 if (pid1) {
6dd32d35 325 DEBUG("Going to wait for pid %d", pid1);
457e3c5d 326
f2bbe86d
DE
327 if (waitpid(pid1, NULL, 0) != pid1)
328 return -1;
457e3c5d 329
6dd32d35 330 DEBUG("Finished waiting on pid %d", pid1);
e51d4895
DE
331 return 0;
332 }
333
334 if (pipe(pipefd) < 0) {
6dd32d35 335 SYSERROR("Failed to create pipe");
f15e4fd2 336 _exit(EXIT_FAILURE);
e51d4895
DE
337 }
338
339 pid2 = fork();
340 if (pid2 < 0) {
6dd32d35 341 SYSERROR("Failed to fork()");
f15e4fd2 342 _exit(EXIT_FAILURE);
e51d4895 343 }
292b1d17 344
e51d4895 345 if (pid2) {
6dd32d35 346 DEBUG("Trying to sync with child process");
e51d4895 347 char c;
292b1d17 348 /* Wait for daemon to create socket. */
e51d4895 349 close(pipefd[1]);
292b1d17
CB
350
351 /* Sync with child, we're ignoring the return from read
e51d4895
DE
352 * because regardless if it works or not, either way we've
353 * synced with the child process. the if-empty-statement
354 * construct is to quiet the warn-unused-result warning.
355 */
2a71131d 356 if (lxc_read_nointr(pipefd[0], &c, 1))
8f47bc3f 357 ;
292b1d17 358
e51d4895 359 close(pipefd[0]);
292b1d17 360
6dd32d35 361 DEBUG("Successfully synced with child process");
f15e4fd2 362 _exit(EXIT_SUCCESS);
e51d4895
DE
363 }
364
e51d4895 365 if (setsid() < 0) {
6dd32d35 366 SYSERROR("Failed to setsid()");
f15e4fd2 367 _exit(EXIT_FAILURE);
e51d4895 368 }
292b1d17 369
47a46cf1 370 lxc_check_inherited(NULL, true, &pipefd[1], 1);
aec1ea62 371 if (null_stdfds() < 0) {
6dd32d35 372 SYSERROR("Failed to dup2() standard file descriptors to /dev/null");
f15e4fd2 373 _exit(EXIT_FAILURE);
aec1ea62 374 }
292b1d17 375
e51d4895 376 close(pipefd[0]);
292b1d17 377
979a0d93
CB
378 ret = snprintf(pipefd_str, sizeof(pipefd_str), "%d", pipefd[1]);
379 if (ret < 0 || ret >= sizeof(pipefd_str)) {
6dd32d35 380 ERROR("Failed to create pid argument to pass to monitord");
f15e4fd2 381 _exit(EXIT_FAILURE);
aec1ea62 382 }
487b14b6 383
6dd32d35 384 DEBUG("Using pipe file descriptor %d for monitord", pipefd[1]);
292b1d17 385
e51d4895 386 execvp(args[0], args);
6dd32d35 387 SYSERROR("Failed to exec lxc-monitord");
292b1d17 388
f15e4fd2 389 _exit(EXIT_FAILURE);
0ad19a3f 390}