]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/monitor.c
licensing: Add missing headers and FSF address
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 close(fd);
73 SYSERROR("failed to write monitor fifo %s", fifo_path);
74 return;
75 }
76
77 close(fd);
78 }
79
80 void lxc_monitor_send_state(const char *name, lxc_state_t state, const char *lxcpath)
81 {
82 struct lxc_msg msg = { .type = lxc_msg_state,
83 .value = state };
84 strncpy(msg.name, name, sizeof(msg.name));
85 msg.name[sizeof(msg.name) - 1] = 0;
86
87 lxc_monitor_fifo_send(&msg, lxcpath);
88 }
89
90
91 /* routines used by monitor subscribers (lxc-monitor) */
92 int lxc_monitor_close(int fd)
93 {
94 return close(fd);
95 }
96
97 int lxc_monitor_sock_name(const char *lxcpath, struct sockaddr_un *addr) {
98 size_t len;
99 int ret;
100 char *sockname = &addr->sun_path[0]; // 1 for abstract
101
102 /* addr.sun_path is only 108 bytes.
103 * should we take a hash of lxcpath? a subset of it? ftok()? we need
104 * to make sure it is unique.
105 */
106 memset(addr, 0, sizeof(*addr));
107 addr->sun_family = AF_UNIX;
108 len = sizeof(addr->sun_path) - 1;
109 ret = snprintf(sockname, len, "%s/monitor-sock", lxcpath);
110 if (ret < 0 || ret >= len) {
111 ERROR("lxcpath too long for unix socket");
112 return -1;
113 }
114 return 0;
115 }
116
117 int lxc_monitor_open(const char *lxcpath)
118 {
119 struct sockaddr_un addr;
120 int fd,ret;
121 int retry,backoff_ms[] = {10, 50, 100};
122
123 if (lxc_monitor_sock_name(lxcpath, &addr) < 0)
124 return -1;
125
126 fd = socket(PF_UNIX, SOCK_STREAM, 0);
127 if (fd < 0) {
128 ERROR("socket : %s", strerror(errno));
129 return -1;
130 }
131
132 for (retry = 0; retry < sizeof(backoff_ms)/sizeof(backoff_ms[0]); retry++) {
133 ret = connect(fd, (struct sockaddr *)&addr, sizeof(addr));
134 if (ret == 0 || errno != ECONNREFUSED)
135 break;
136 ERROR("connect : backing off %d", backoff_ms[retry]);
137 usleep(backoff_ms[retry] * 1000);
138 }
139
140 if (ret < 0) {
141 ERROR("connect : %s", strerror(errno));
142 goto err1;
143 }
144 return fd;
145 err1:
146 close(fd);
147 return ret;
148 }
149
150 int lxc_monitor_read_fdset(fd_set *rfds, int nfds, struct lxc_msg *msg,
151 int timeout)
152 {
153 struct timeval tval,*tv = NULL;
154 int ret,i;
155
156 if (timeout != -1) {
157 tv = &tval;
158 tv->tv_sec = timeout;
159 tv->tv_usec = 0;
160 }
161
162 ret = select(nfds, rfds, NULL, NULL, tv);
163 if (ret == -1)
164 return -1;
165 else if (ret == 0)
166 return -2; // timed out
167
168 /* only read from the first ready fd, the others will remain ready
169 * for when this routine is called again
170 */
171 for (i = 0; i < nfds; i++) {
172 if (FD_ISSET(i, rfds)) {
173 ret = recv(i, msg, sizeof(*msg), 0);
174 if (ret <= 0) {
175 SYSERROR("client failed to recv (monitord died?) %s",
176 strerror(errno));
177 return -1;
178 }
179 return ret;
180 }
181 }
182 SYSERROR("no ready fd found?");
183 return -1;
184 }
185
186 int lxc_monitor_read_timeout(int fd, struct lxc_msg *msg, int timeout)
187 {
188 fd_set rfds;
189
190 FD_ZERO(&rfds);
191 FD_SET(fd, &rfds);
192
193 return lxc_monitor_read_fdset(&rfds, fd+1, msg, timeout);
194 }
195
196 int lxc_monitor_read(int fd, struct lxc_msg *msg)
197 {
198 return lxc_monitor_read_timeout(fd, msg, -1);
199 }
200
201
202
203 /* used to spawn a monitord either on startup of a daemon container, or when
204 * lxc-monitor starts
205 */
206 int lxc_monitord_spawn(const char *lxcpath)
207 {
208 pid_t pid1,pid2;
209 int pipefd[2];
210 char pipefd_str[11];
211
212 char * const args[] = {
213 "lxc-monitord",
214 (char *)lxcpath,
215 pipefd_str,
216 NULL,
217 };
218
219 /* double fork to avoid zombies when monitord exits */
220 pid1 = fork();
221 if (pid1 < 0) {
222 SYSERROR("failed to fork");
223 return -1;
224 }
225
226 if (pid1) {
227 if (waitpid(pid1, NULL, 0) != pid1)
228 return -1;
229 return 0;
230 }
231
232 if (pipe(pipefd) < 0) {
233 SYSERROR("failed to create pipe");
234 exit(EXIT_FAILURE);
235 }
236
237 pid2 = fork();
238 if (pid2 < 0) {
239 SYSERROR("failed to fork");
240 exit(EXIT_FAILURE);
241 }
242 if (pid2) {
243 char c;
244 /* wait for daemon to create socket */
245 close(pipefd[1]);
246 /* sync with child, we're ignoring the return from read
247 * because regardless if it works or not, either way we've
248 * synced with the child process. the if-empty-statement
249 * construct is to quiet the warn-unused-result warning.
250 */
251 if (read(pipefd[0], &c, 1)) ;
252 close(pipefd[0]);
253 exit(EXIT_SUCCESS);
254 }
255
256 umask(0);
257 if (setsid() < 0) {
258 SYSERROR("failed to setsid");
259 exit(EXIT_FAILURE);
260 }
261 close(0);
262 close(1);
263 close(2);
264 open("/dev/null", O_RDONLY);
265 open("/dev/null", O_RDWR);
266 open("/dev/null", O_RDWR);
267 close(pipefd[0]);
268 sprintf(pipefd_str, "%d", pipefd[1]);
269 execvp(args[0], args);
270 exit(EXIT_FAILURE);
271 }