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