]>
Commit | Line | Data |
---|---|---|
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> | |
30 | #include <fcntl.h> | |
31 | #include <sys/types.h> | |
32 | #include <sys/stat.h> | |
33 | #include <sys/param.h> | |
0ad19a3f | 34 | #include <sys/socket.h> |
e51d4895 | 35 | #include <sys/wait.h> |
0ad19a3f | 36 | #include <netinet/in.h> |
37 | #include <net/if.h> | |
b113348e | 38 | |
e2bcd7db | 39 | #include "error.h" |
31c53c2e | 40 | #include "af_unix.h" |
00b3c2e2 | 41 | |
36eb9bde | 42 | #include <lxc/log.h> |
00b3c2e2 CLG |
43 | #include <lxc/state.h> |
44 | #include <lxc/monitor.h> | |
e51d4895 | 45 | #include <lxc/utils.h> |
36eb9bde CLG |
46 | |
47 | lxc_log_define(lxc_monitor, lxc); | |
0ad19a3f | 48 | |
e51d4895 DE |
49 | /* routines used by monitor publishers (containers) */ |
50 | static void lxc_monitor_fifo_send(struct lxc_msg *msg, const char *lxcpath) | |
0ad19a3f | 51 | { |
e51d4895 DE |
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"); | |
9123e471 SH |
59 | return; |
60 | } | |
80f41298 | 61 | |
e51d4895 DE |
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 | */ | |
31c53c2e | 67 | return; |
e51d4895 | 68 | } |
0ad19a3f | 69 | |
e51d4895 DE |
70 | ret = write(fd, msg, sizeof(*msg)); |
71 | if (ret != sizeof(*msg)) { | |
e8b9ac8f | 72 | close(fd); |
e51d4895 DE |
73 | SYSERROR("failed to write monitor fifo %s", fifo_path); |
74 | return; | |
75 | } | |
0ad19a3f | 76 | |
77 | close(fd); | |
78 | } | |
79 | ||
9123e471 | 80 | void lxc_monitor_send_state(const char *name, lxc_state_t state, const char *lxcpath) |
eae6543d | 81 | { |
82 | struct lxc_msg msg = { .type = lxc_msg_state, | |
83 | .value = state }; | |
80f41298 | 84 | strncpy(msg.name, name, sizeof(msg.name)); |
f3bc28bd | 85 | msg.name[sizeof(msg.name) - 1] = 0; |
eae6543d | 86 | |
e51d4895 | 87 | lxc_monitor_fifo_send(&msg, lxcpath); |
0ad19a3f | 88 | } |
89 | ||
e51d4895 DE |
90 | |
91 | /* routines used by monitor subscribers (lxc-monitor) */ | |
92 | int lxc_monitor_close(int fd) | |
0ad19a3f | 93 | { |
e51d4895 DE |
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. | |
9123e471 | 105 | */ |
e51d4895 DE |
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); | |
9123e471 | 110 | if (ret < 0 || ret >= len) { |
e51d4895 | 111 | ERROR("lxcpath too long for unix socket"); |
9123e471 SH |
112 | return -1; |
113 | } | |
e51d4895 DE |
114 | return 0; |
115 | } | |
0ad19a3f | 116 | |
e51d4895 DE |
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); | |
2c396e12 MN |
127 | if (fd < 0) { |
128 | ERROR("socket : %s", strerror(errno)); | |
31c53c2e | 129 | return -1; |
2c396e12 | 130 | } |
0ad19a3f | 131 | |
e51d4895 DE |
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); | |
0ad19a3f | 138 | } |
139 | ||
e51d4895 DE |
140 | if (ret < 0) { |
141 | ERROR("connect : %s", strerror(errno)); | |
142 | goto err1; | |
143 | } | |
0ad19a3f | 144 | return fd; |
e51d4895 DE |
145 | err1: |
146 | close(fd); | |
147 | return ret; | |
0ad19a3f | 148 | } |
149 | ||
8d06bd13 DE |
150 | int lxc_monitor_read_fdset(fd_set *rfds, int nfds, struct lxc_msg *msg, |
151 | int timeout) | |
0ad19a3f | 152 | { |
8d06bd13 DE |
153 | struct timeval tval,*tv = NULL; |
154 | int ret,i; | |
72d0e1cb SG |
155 | |
156 | if (timeout != -1) { | |
8d06bd13 DE |
157 | tv = &tval; |
158 | tv->tv_sec = timeout; | |
159 | tv->tv_usec = 0; | |
72d0e1cb | 160 | } |
0ad19a3f | 161 | |
8d06bd13 DE |
162 | ret = select(nfds, rfds, NULL, NULL, tv); |
163 | if (ret == -1) | |
75b1e198 | 164 | return -1; |
8d06bd13 DE |
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 | } | |
0ad19a3f | 181 | } |
8d06bd13 DE |
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); | |
0ad19a3f | 194 | } |
195 | ||
72d0e1cb SG |
196 | int lxc_monitor_read(int fd, struct lxc_msg *msg) |
197 | { | |
198 | return lxc_monitor_read_timeout(fd, msg, -1); | |
199 | } | |
200 | ||
e51d4895 DE |
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) | |
0ad19a3f | 207 | { |
e51d4895 DE |
208 | pid_t pid1,pid2; |
209 | int pipefd[2]; | |
210 | char pipefd_str[11]; | |
211 | ||
212 | char * const args[] = { | |
31f58b3f | 213 | "lxc-monitord", |
e51d4895 DE |
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) { | |
f2bbe86d DE |
227 | if (waitpid(pid1, NULL, 0) != pid1) |
228 | return -1; | |
e51d4895 DE |
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); | |
0ad19a3f | 271 | } |