]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/monitor.c
ovl_rsync: make sure to umount
[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 <stdio.h>
26#include <errno.h>
27#include <unistd.h>
28#include <string.h>
29#include <stdlib.h>
aae93dd3 30#include <stddef.h>
0ad19a3f 31#include <fcntl.h>
b45c7011
DE
32#include <inttypes.h>
33#include <stdint.h>
0ad19a3f 34#include <sys/types.h>
35#include <sys/stat.h>
36#include <sys/param.h>
0ad19a3f 37#include <sys/socket.h>
e51d4895 38#include <sys/wait.h>
0ad19a3f 39#include <netinet/in.h>
40#include <net/if.h>
2366b8a7 41#include <poll.h>
b113348e 42
e2bcd7db 43#include "error.h"
31c53c2e 44#include "af_unix.h"
f2363e38
ÇO
45#include "log.h"
46#include "lxclock.h"
47#include "state.h"
48#include "monitor.h"
49#include "utils.h"
36eb9bde
CLG
50
51lxc_log_define(lxc_monitor, lxc);
0ad19a3f 52
e51d4895 53/* routines used by monitor publishers (containers) */
9e60f51d
DE
54int lxc_monitor_fifo_name(const char *lxcpath, char *fifo_path, size_t fifo_path_sz,
55 int do_mkdirp)
56{
57 int ret;
44b9ae4b 58 char *rundir;
9e60f51d
DE
59
60 rundir = get_rundir();
97a696c6
SG
61 if (!rundir)
62 return -1;
63
9e60f51d
DE
64 if (do_mkdirp) {
65 ret = snprintf(fifo_path, fifo_path_sz, "%s/lxc/%s", rundir, lxcpath);
66 if (ret < 0 || ret >= fifo_path_sz) {
67 ERROR("rundir/lxcpath (%s/%s) too long for monitor fifo", rundir, lxcpath);
44b9ae4b 68 free(rundir);
9e60f51d
DE
69 return -1;
70 }
9e60f51d 71 ret = mkdir_p(fifo_path, 0755);
9e60f51d
DE
72 if (ret < 0) {
73 ERROR("unable to create monitor fifo dir %s", fifo_path);
44b9ae4b 74 free(rundir);
9e60f51d
DE
75 return ret;
76 }
77 }
78 ret = snprintf(fifo_path, fifo_path_sz, "%s/lxc/%s/monitor-fifo", rundir, lxcpath);
79 if (ret < 0 || ret >= fifo_path_sz) {
80 ERROR("rundir/lxcpath (%s/%s) too long for monitor fifo", rundir, lxcpath);
44b9ae4b 81 free(rundir);
9e60f51d
DE
82 return -1;
83 }
44b9ae4b 84 free(rundir);
9e60f51d
DE
85 return 0;
86}
87
e51d4895 88static void lxc_monitor_fifo_send(struct lxc_msg *msg, const char *lxcpath)
0ad19a3f 89{
e51d4895
DE
90 int fd,ret;
91 char fifo_path[PATH_MAX];
92
93 BUILD_BUG_ON(sizeof(*msg) > PIPE_BUF); /* write not guaranteed atomic */
9e60f51d
DE
94
95 ret = lxc_monitor_fifo_name(lxcpath, fifo_path, sizeof(fifo_path), 0);
96 if (ret < 0)
9123e471 97 return;
80f41298 98
8bf1e61e
DE
99 /* open the fifo nonblock in case the monitor is dead, we don't want
100 * the open to wait for a reader since it may never come.
101 */
102 fd = open(fifo_path, O_WRONLY|O_NONBLOCK);
e51d4895 103 if (fd < 0) {
8bf1e61e
DE
104 /* it is normal for this open to fail ENXIO when there is no
105 * monitor running, so we don't log it
e51d4895 106 */
31c53c2e 107 return;
e51d4895 108 }
0ad19a3f 109
92ffb6d8
DE
110 if (fcntl(fd, F_SETFL, O_WRONLY) < 0) {
111 close(fd);
8bf1e61e 112 return;
92ffb6d8 113 }
8bf1e61e 114
e51d4895
DE
115 ret = write(fd, msg, sizeof(*msg));
116 if (ret != sizeof(*msg)) {
e8b9ac8f 117 close(fd);
e51d4895
DE
118 SYSERROR("failed to write monitor fifo %s", fifo_path);
119 return;
120 }
0ad19a3f 121
122 close(fd);
123}
124
9123e471 125void lxc_monitor_send_state(const char *name, lxc_state_t state, const char *lxcpath)
eae6543d 126{
127 struct lxc_msg msg = { .type = lxc_msg_state,
128 .value = state };
80f41298 129 strncpy(msg.name, name, sizeof(msg.name));
f3bc28bd 130 msg.name[sizeof(msg.name) - 1] = 0;
eae6543d 131
e51d4895 132 lxc_monitor_fifo_send(&msg, lxcpath);
0ad19a3f 133}
134
1787abca
JTLB
135void lxc_monitor_send_exit_code(const char *name, int exit_code, const char *lxcpath)
136{
137 struct lxc_msg msg = { .type = lxc_msg_exit_code,
138 .value = exit_code };
139 strncpy(msg.name, name, sizeof(msg.name));
140 msg.name[sizeof(msg.name) - 1] = 0;
141
142 lxc_monitor_fifo_send(&msg, lxcpath);
143}
144
e51d4895
DE
145
146/* routines used by monitor subscribers (lxc-monitor) */
147int lxc_monitor_close(int fd)
0ad19a3f 148{
dd1d77f9 149 return close(fd);
e51d4895
DE
150}
151
152int lxc_monitor_sock_name(const char *lxcpath, struct sockaddr_un *addr) {
153 size_t len;
154 int ret;
b45c7011 155 char *sockname = &addr->sun_path[1];
073135ba 156 char *path;
b45c7011 157 uint64_t hash;
e51d4895 158
b45c7011
DE
159 /* addr.sun_path is only 108 bytes, so we hash the full name and
160 * then append as much of the name as we can fit.
9123e471 161 */
e51d4895
DE
162 memset(addr, 0, sizeof(*addr));
163 addr->sun_family = AF_UNIX;
073135ba
SH
164 len = strlen(lxcpath) + 18;
165 path = alloca(len);
166 ret = snprintf(path, len, "lxc/%s/monitor-sock", lxcpath);
167 if (ret < 0 || ret >= len) {
168 ERROR("memory error creating monitor path");
9e60f51d
DE
169 return -1;
170 }
9e60f51d 171
073135ba 172 len = sizeof(addr->sun_path) - 1;
b45c7011
DE
173 hash = fnv_64a_buf(path, ret, FNV1A_64_INIT);
174 ret = snprintf(sockname, len, "lxc/%016" PRIx64 "/%s", hash, lxcpath);
175 if (ret < 0)
9123e471 176 return -1;
073135ba 177 sockname[sizeof(addr->sun_path)-3] = '\0';
b45c7011 178 INFO("using monitor sock name %s", sockname);
e51d4895
DE
179 return 0;
180}
0ad19a3f 181
e51d4895
DE
182int lxc_monitor_open(const char *lxcpath)
183{
184 struct sockaddr_un addr;
f58ad87a 185 int fd,ret = 0;
e51d4895 186 int retry,backoff_ms[] = {10, 50, 100};
aae93dd3 187 size_t len;
e51d4895
DE
188
189 if (lxc_monitor_sock_name(lxcpath, &addr) < 0)
190 return -1;
191
192 fd = socket(PF_UNIX, SOCK_STREAM, 0);
2c396e12
MN
193 if (fd < 0) {
194 ERROR("socket : %s", strerror(errno));
31c53c2e 195 return -1;
2c396e12 196 }
0ad19a3f 197
aae93dd3
ÇO
198 len = strlen(&addr.sun_path[1]) + 1;
199 if (len >= sizeof(addr.sun_path) - 1) {
200 ret = -1;
201 errno = ENAMETOOLONG;
202 goto err1;
203 }
204
e51d4895 205 for (retry = 0; retry < sizeof(backoff_ms)/sizeof(backoff_ms[0]); retry++) {
aae93dd3 206 ret = connect(fd, (struct sockaddr *)&addr, offsetof(struct sockaddr_un, sun_path) + len);
e51d4895
DE
207 if (ret == 0 || errno != ECONNREFUSED)
208 break;
209 ERROR("connect : backing off %d", backoff_ms[retry]);
210 usleep(backoff_ms[retry] * 1000);
0ad19a3f 211 }
212
e51d4895
DE
213 if (ret < 0) {
214 ERROR("connect : %s", strerror(errno));
215 goto err1;
216 }
0ad19a3f 217 return fd;
e51d4895
DE
218err1:
219 close(fd);
220 return ret;
0ad19a3f 221}
222
2366b8a7 223int lxc_monitor_read_fdset(struct pollfd *fds, nfds_t nfds, struct lxc_msg *msg,
8d06bd13 224 int timeout)
0ad19a3f 225{
2366b8a7
SH
226 long i;
227 int ret;
0ad19a3f 228
2366b8a7 229 ret = poll(fds, nfds, timeout * 1000);
8d06bd13 230 if (ret == -1)
75b1e198 231 return -1;
8d06bd13
DE
232 else if (ret == 0)
233 return -2; // timed out
234
235 /* only read from the first ready fd, the others will remain ready
236 * for when this routine is called again
237 */
238 for (i = 0; i < nfds; i++) {
2366b8a7
SH
239 if (fds[i].revents != 0) {
240 fds[i].revents = 0;
241 ret = recv(fds[i].fd, msg, sizeof(*msg), 0);
8d06bd13
DE
242 if (ret <= 0) {
243 SYSERROR("client failed to recv (monitord died?) %s",
244 strerror(errno));
245 return -1;
246 }
247 return ret;
248 }
0ad19a3f 249 }
8d06bd13
DE
250 SYSERROR("no ready fd found?");
251 return -1;
252}
253
254int lxc_monitor_read_timeout(int fd, struct lxc_msg *msg, int timeout)
255{
2366b8a7 256 struct pollfd fds;
8d06bd13 257
2366b8a7
SH
258 fds.fd = fd;
259 fds.events = POLLIN | POLLPRI;
260 fds.revents = 0;
8d06bd13 261
2366b8a7 262 return lxc_monitor_read_fdset(&fds, 1, msg, timeout);
0ad19a3f 263}
264
72d0e1cb
SG
265int lxc_monitor_read(int fd, struct lxc_msg *msg)
266{
267 return lxc_monitor_read_timeout(fd, msg, -1);
268}
269
e51d4895 270
45e854dc 271#define LXC_MONITORD_PATH LIBEXECDIR "/lxc/lxc-monitord"
e51d4895
DE
272
273/* used to spawn a monitord either on startup of a daemon container, or when
274 * lxc-monitor starts
275 */
276int lxc_monitord_spawn(const char *lxcpath)
0ad19a3f 277{
e51d4895
DE
278 pid_t pid1,pid2;
279 int pipefd[2];
280 char pipefd_str[11];
281
282 char * const args[] = {
45e854dc 283 LXC_MONITORD_PATH,
e51d4895
DE
284 (char *)lxcpath,
285 pipefd_str,
286 NULL,
287 };
288
289 /* double fork to avoid zombies when monitord exits */
290 pid1 = fork();
291 if (pid1 < 0) {
292 SYSERROR("failed to fork");
293 return -1;
294 }
295
296 if (pid1) {
f2bbe86d
DE
297 if (waitpid(pid1, NULL, 0) != pid1)
298 return -1;
e51d4895
DE
299 return 0;
300 }
301
302 if (pipe(pipefd) < 0) {
303 SYSERROR("failed to create pipe");
304 exit(EXIT_FAILURE);
305 }
306
307 pid2 = fork();
308 if (pid2 < 0) {
309 SYSERROR("failed to fork");
310 exit(EXIT_FAILURE);
311 }
312 if (pid2) {
313 char c;
314 /* wait for daemon to create socket */
315 close(pipefd[1]);
316 /* sync with child, we're ignoring the return from read
317 * because regardless if it works or not, either way we've
318 * synced with the child process. the if-empty-statement
319 * construct is to quiet the warn-unused-result warning.
320 */
8f47bc3f
SG
321 if (read(pipefd[0], &c, 1))
322 ;
e51d4895
DE
323 close(pipefd[0]);
324 exit(EXIT_SUCCESS);
325 }
326
e51d4895
DE
327 if (setsid() < 0) {
328 SYSERROR("failed to setsid");
329 exit(EXIT_FAILURE);
330 }
d2cf4c37 331 lxc_check_inherited(NULL, true, pipefd[1]);
69aeabac
TA
332 if (null_stdfds() < 0)
333 exit(EXIT_FAILURE);
e51d4895
DE
334 close(pipefd[0]);
335 sprintf(pipefd_str, "%d", pipefd[1]);
336 execvp(args[0], args);
337 exit(EXIT_FAILURE);
0ad19a3f 338}