]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/monitor.c
Allow multiple monitor clients
[mirror_lxc.git] / src / lxc / monitor.c
1 /*
2 * lxc: linux Container library
3 *
4 * (C) Copyright IBM Corp. 2007, 2008
5 *
6 * Authors:
7 * Daniel Lezcano <daniel.lezcano at free.fr>
8 * Dwight Engen <dwight.engen@oracle.com>
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
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24
25 #include <stdio.h>
26 #include <errno.h>
27 #include <unistd.h>
28 #include <string.h>
29 #include <stdlib.h>
30 #include <fcntl.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <sys/param.h>
34 #include <sys/socket.h>
35 #include <sys/wait.h>
36 #include <netinet/in.h>
37 #include <net/if.h>
38
39 #include "error.h"
40 #include "af_unix.h"
41
42 #include <lxc/log.h>
43 #include <lxc/state.h>
44 #include <lxc/monitor.h>
45 #include <lxc/utils.h>
46
47 lxc_log_define(lxc_monitor, lxc);
48
49 /* routines used by monitor publishers (containers) */
50 static void lxc_monitor_fifo_send(struct lxc_msg *msg, const char *lxcpath)
51 {
52 int fd,ret;
53 char fifo_path[PATH_MAX];
54
55 BUILD_BUG_ON(sizeof(*msg) > PIPE_BUF); /* write not guaranteed atomic */
56 ret = snprintf(fifo_path, sizeof(fifo_path), "%s/monitor-fifo", lxcpath);
57 if (ret < 0 || ret >= sizeof(fifo_path)) {
58 ERROR("lxcpath too long to open monitor fifo");
59 return;
60 }
61
62 fd = open(fifo_path, O_WRONLY);
63 if (fd < 0) {
64 /* it is normal for this open to fail when there is no monitor
65 * running, so we don't log it
66 */
67 return;
68 }
69
70 ret = write(fd, msg, sizeof(*msg));
71 if (ret != sizeof(*msg)) {
72 SYSERROR("failed to write monitor fifo %s", fifo_path);
73 return;
74 }
75
76 close(fd);
77 }
78
79 void lxc_monitor_send_state(const char *name, lxc_state_t state, const char *lxcpath)
80 {
81 struct lxc_msg msg = { .type = lxc_msg_state,
82 .value = state };
83 strncpy(msg.name, name, sizeof(msg.name));
84 msg.name[sizeof(msg.name) - 1] = 0;
85
86 lxc_monitor_fifo_send(&msg, lxcpath);
87 }
88
89
90 /* routines used by monitor subscribers (lxc-monitor) */
91 int lxc_monitor_close(int fd)
92 {
93 return close(fd);
94 }
95
96 int lxc_monitor_sock_name(const char *lxcpath, struct sockaddr_un *addr) {
97 size_t len;
98 int ret;
99 char *sockname = &addr->sun_path[0]; // 1 for abstract
100
101 /* addr.sun_path is only 108 bytes.
102 * should we take a hash of lxcpath? a subset of it? ftok()? we need
103 * to make sure it is unique.
104 */
105 memset(addr, 0, sizeof(*addr));
106 addr->sun_family = AF_UNIX;
107 len = sizeof(addr->sun_path) - 1;
108 ret = snprintf(sockname, len, "%s/monitor-sock", lxcpath);
109 if (ret < 0 || ret >= len) {
110 ERROR("lxcpath too long for unix socket");
111 return -1;
112 }
113 return 0;
114 }
115
116 int lxc_monitor_open(const char *lxcpath)
117 {
118 struct sockaddr_un addr;
119 int fd,ret;
120 int retry,backoff_ms[] = {10, 50, 100};
121
122 if (lxc_monitor_sock_name(lxcpath, &addr) < 0)
123 return -1;
124
125 fd = socket(PF_UNIX, SOCK_STREAM, 0);
126 if (fd < 0) {
127 ERROR("socket : %s", strerror(errno));
128 return -1;
129 }
130
131 for (retry = 0; retry < sizeof(backoff_ms)/sizeof(backoff_ms[0]); retry++) {
132 ret = connect(fd, (struct sockaddr *)&addr, sizeof(addr));
133 if (ret == 0 || errno != ECONNREFUSED)
134 break;
135 ERROR("connect : backing off %d", backoff_ms[retry]);
136 usleep(backoff_ms[retry] * 1000);
137 }
138
139 if (ret < 0) {
140 ERROR("connect : %s", strerror(errno));
141 goto err1;
142 }
143 return fd;
144 err1:
145 close(fd);
146 return ret;
147 }
148
149 int lxc_monitor_read_timeout(int fd, struct lxc_msg *msglxc, int timeout)
150 {
151 fd_set rfds;
152 struct timeval tv;
153 int ret;
154
155 if (timeout != -1) {
156 FD_ZERO(&rfds);
157 FD_SET(fd, &rfds);
158
159 tv.tv_sec = timeout;
160 tv.tv_usec = 0;
161
162 ret = select(fd+1, &rfds, NULL, NULL, &tv);
163 if (ret == -1)
164 return -1;
165 else if (!ret)
166 return -2; // timed out
167 }
168
169 ret = recv(fd, msglxc, sizeof(*msglxc), 0);
170 if (ret <= 0) {
171 SYSERROR("client failed to recv (monitord died?) %s",
172 strerror(errno));
173 return -1;
174 }
175 return ret;
176 }
177
178 int lxc_monitor_read(int fd, struct lxc_msg *msg)
179 {
180 return lxc_monitor_read_timeout(fd, msg, -1);
181 }
182
183
184
185 /* used to spawn a monitord either on startup of a daemon container, or when
186 * lxc-monitor starts
187 */
188 int lxc_monitord_spawn(const char *lxcpath)
189 {
190 pid_t pid1,pid2;
191 int pipefd[2];
192 char pipefd_str[11];
193
194 char * const args[] = {
195 "/usr/bin/lxc-monitord",
196 (char *)lxcpath,
197 pipefd_str,
198 NULL,
199 };
200
201 /* double fork to avoid zombies when monitord exits */
202 pid1 = fork();
203 if (pid1 < 0) {
204 SYSERROR("failed to fork");
205 return -1;
206 }
207
208 if (pid1) {
209 waitpid(pid1, NULL, 0);
210 return 0;
211 }
212
213 if (pipe(pipefd) < 0) {
214 SYSERROR("failed to create pipe");
215 exit(EXIT_FAILURE);
216 }
217
218 pid2 = fork();
219 if (pid2 < 0) {
220 SYSERROR("failed to fork");
221 exit(EXIT_FAILURE);
222 }
223 if (pid2) {
224 char c;
225 /* wait for daemon to create socket */
226 close(pipefd[1]);
227 /* sync with child, we're ignoring the return from read
228 * because regardless if it works or not, either way we've
229 * synced with the child process. the if-empty-statement
230 * construct is to quiet the warn-unused-result warning.
231 */
232 if (read(pipefd[0], &c, 1)) ;
233 close(pipefd[0]);
234 exit(EXIT_SUCCESS);
235 }
236
237 umask(0);
238 if (setsid() < 0) {
239 SYSERROR("failed to setsid");
240 exit(EXIT_FAILURE);
241 }
242 close(0);
243 close(1);
244 close(2);
245 open("/dev/null", O_RDONLY);
246 open("/dev/null", O_RDWR);
247 open("/dev/null", O_RDWR);
248 close(pipefd[0]);
249 sprintf(pipefd_str, "%d", pipefd[1]);
250 execvp(args[0], args);
251 exit(EXIT_FAILURE);
252 }