child-delete: Reply as usual when concurrently rekeying the IKE_SA
[strongswan.git] / src / dumm / mconsole.c
1 /*
2 * Copyright (C) 2007 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 * Copyright (C) 2001-2004 Jeff Dike
5 *
6 * Based on the "uml_mconsole" utility from Jeff Dike.
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
17 */
18
19 #define _GNU_SOURCE
20
21 #include <sys/types.h>
22 #include <unistd.h>
23 #include <stdio.h>
24 #include <sys/socket.h>
25 #include <errno.h>
26 #include <sys/un.h>
27
28 #include <utils/debug.h>
29
30 #include "mconsole.h"
31
32 #define MCONSOLE_MAGIC 0xcafebabe
33 #define MCONSOLE_VERSION 2
34 #define MCONSOLE_MAX_DATA 512
35
36 typedef struct private_mconsole_t private_mconsole_t;
37
38 struct private_mconsole_t {
39 /** public interface */
40 mconsole_t public;
41 /** mconsole socket */
42 int console;
43 /** notify socket */
44 int notify;
45 /** address of uml socket */
46 struct sockaddr_un uml;
47 /** idle function */
48 void (*idle)(void);
49 };
50
51 /**
52 * mconsole message format from "arch/um/include/mconsole.h"
53 */
54 typedef struct mconsole_request mconsole_request;
55 /** mconsole request message */
56 struct mconsole_request {
57 uint32_t magic;
58 uint32_t version;
59 uint32_t len;
60 char data[MCONSOLE_MAX_DATA];
61 };
62
63
64 typedef struct mconsole_reply mconsole_reply;
65 /** mconsole reply message */
66 struct mconsole_reply {
67 uint32_t err;
68 uint32_t more;
69 uint32_t len;
70 char data[MCONSOLE_MAX_DATA];
71 };
72
73 typedef struct mconsole_notify mconsole_notify;
74 /** mconsole notify message */
75 struct mconsole_notify {
76 uint32_t magic;
77 uint32_t version;
78 enum {
79 MCONSOLE_SOCKET,
80 MCONSOLE_PANIC,
81 MCONSOLE_HANG,
82 MCONSOLE_USER_NOTIFY,
83 } type;
84 uint32_t len;
85 char data[MCONSOLE_MAX_DATA];
86 };
87
88 /**
89 * send a request to UML using mconsole
90 */
91 static int request(private_mconsole_t *this, void(*cb)(void*,char*,size_t),
92 void *data, char *command, ...)
93 {
94 mconsole_request request;
95 mconsole_reply reply;
96 int len, flags = 0;
97 va_list args;
98
99 memset(&request, 0, sizeof(request));
100 request.magic = MCONSOLE_MAGIC;
101 request.version = MCONSOLE_VERSION;
102 va_start(args, command);
103 request.len = vsnprintf(request.data, sizeof(request.data), command, args);
104 va_end(args);
105
106 if (this->idle)
107 {
108 flags = MSG_DONTWAIT;
109 }
110 do
111 {
112 if (this->idle)
113 {
114 this->idle();
115 }
116 len = sendto(this->console, &request, sizeof(request), flags,
117 (struct sockaddr*)&this->uml, sizeof(this->uml));
118 }
119 while (len < 0 && (errno == EINTR || errno == EAGAIN));
120
121 if (len < 0)
122 {
123 DBG1(DBG_LIB, "sending mconsole command to UML failed: %m");
124 return -1;
125 }
126 do
127 {
128 len = recv(this->console, &reply, sizeof(reply), flags);
129 if (len < 0 && (errno == EINTR || errno == EAGAIN))
130 {
131 if (this->idle)
132 {
133 this->idle();
134 }
135 continue;
136 }
137 if (len < 0)
138 {
139 DBG1(DBG_LIB, "receiving from mconsole failed: %m");
140 return -1;
141 }
142 if (len > 0)
143 {
144 if (cb)
145 {
146 cb(data, reply.data, reply.len);
147 }
148 else if (reply.err)
149 {
150 if (reply.len && *reply.data)
151 {
152 DBG1(DBG_LIB, "received mconsole error %d: %.*s",
153 reply.err, (int)reply.len, reply.data);
154 }
155 break;
156 }
157 }
158 }
159 while (reply.more);
160
161 return reply.err;
162 }
163
164 /**
165 * ignore error message
166 */
167 static void ignore(void *data, char *buf, size_t len)
168 {
169 }
170
171 METHOD(mconsole_t, add_iface, bool,
172 private_mconsole_t *this, char *guest, char *host)
173 {
174 int tries = 0;
175
176 while (tries++ < 5)
177 {
178 if (request(this, ignore, NULL, "config %s=tuntap,%s", guest, host) == 0)
179 {
180 return TRUE;
181 }
182 usleep(10000 * tries * tries);
183 }
184 return FALSE;
185 }
186
187 METHOD(mconsole_t, del_iface, bool,
188 private_mconsole_t *this, char *guest)
189 {
190 if (request(this, NULL, NULL, "remove %s", guest) != 0)
191 {
192 return FALSE;
193 }
194 return TRUE;
195 }
196
197 METHOD(mconsole_t, exec, int,
198 private_mconsole_t *this, void(*cb)(void*,char*,size_t), void *data,
199 char *cmd)
200 {
201 return request(this, cb, data, "%s", cmd);
202 }
203
204 /**
205 * Poll until guest is ready
206 */
207 static void wait_bootup(private_mconsole_t *this)
208 {
209 /* wait for init process to appear */
210 while (request(this, ignore, NULL, "exec ps -p 1 > /dev/null"))
211 {
212 if (this->idle)
213 {
214 this->idle();
215 }
216 usleep(100000);
217 }
218 }
219
220 METHOD(mconsole_t, destroy, void,
221 private_mconsole_t *this)
222 {
223 close(this->console);
224 close(this->notify);
225 free(this);
226 }
227
228 /**
229 * setup the mconsole notify connection and wait for its readiness
230 */
231 static bool wait_for_notify(private_mconsole_t *this, char *nsock)
232 {
233 struct sockaddr_un addr;
234 mconsole_notify notify;
235 int len, flags = 0;
236
237 this->notify = socket(AF_UNIX, SOCK_DGRAM, 0);
238 if (this->notify < 0)
239 {
240 DBG1(DBG_LIB, "opening mconsole notify socket failed: %m");
241 return FALSE;
242 }
243 memset(&addr, 0, sizeof(addr));
244 addr.sun_family = AF_UNIX;
245 strncpy(addr.sun_path, nsock, sizeof(addr.sun_path));
246 if (bind(this->notify, (struct sockaddr*)&addr, sizeof(addr)) < 0)
247 {
248 DBG1(DBG_LIB, "binding mconsole notify socket to '%s' failed: %m",
249 nsock);
250 close(this->notify);
251 return FALSE;
252 }
253 if (this->idle)
254 {
255 flags = MSG_DONTWAIT;
256 }
257 do
258 {
259 if (this->idle)
260 {
261 this->idle();
262 }
263 len = recvfrom(this->notify, &notify, sizeof(notify), flags, NULL, 0);
264 }
265 while (len < 0 && (errno == EINTR || errno == EAGAIN));
266
267 if (len < 0 || len >= sizeof(notify))
268 {
269 DBG1(DBG_LIB, "reading from mconsole notify socket failed: %m");
270 close(this->notify);
271 unlink(nsock);
272 return FALSE;
273 }
274 if (notify.magic != MCONSOLE_MAGIC ||
275 notify.version != MCONSOLE_VERSION ||
276 notify.type != MCONSOLE_SOCKET)
277 {
278 DBG1(DBG_LIB, "received unexpected message from mconsole notify"
279 " socket: %b", &notify, sizeof(notify));
280 close(this->notify);
281 unlink(nsock);
282 return FALSE;
283 }
284 memset(&this->uml, 0, sizeof(this->uml));
285 this->uml.sun_family = AF_UNIX;
286 strncpy(this->uml.sun_path, (char*)&notify.data, sizeof(this->uml.sun_path));
287 return TRUE;
288 }
289
290 /**
291 * setup the mconsole console connection
292 */
293 static bool setup_console(private_mconsole_t *this)
294 {
295 struct sockaddr_un addr;
296
297 this->console = socket(AF_UNIX, SOCK_DGRAM, 0);
298 if (this->console < 0)
299 {
300 DBG1(DBG_LIB, "opening mconsole socket failed: %m");
301 return FALSE;
302 }
303 memset(&addr, 0, sizeof(addr));
304 addr.sun_family = AF_UNIX;
305 snprintf(&addr.sun_path[1], sizeof(addr.sun_path)-1, "%5d-%d",
306 getpid(), this->console);
307 if (bind(this->console, (struct sockaddr*)&addr, sizeof(addr)) < 0)
308 {
309 DBG1(DBG_LIB, "binding mconsole socket to '%s' failed: %m",
310 &addr.sun_path[1]);
311 close(this->console);
312 return FALSE;
313 }
314 return TRUE;
315 }
316
317 /**
318 * create the mconsole instance
319 */
320 mconsole_t *mconsole_create(char *notify, void(*idle)(void))
321 {
322 private_mconsole_t *this;
323
324 INIT(this,
325 .public = {
326 .add_iface = _add_iface,
327 .del_iface = _del_iface,
328 .exec = _exec,
329 .destroy = _destroy,
330 },
331 .idle = idle,
332 );
333
334 if (!wait_for_notify(this, notify))
335 {
336 free(this);
337 return NULL;
338 }
339
340 if (!setup_console(this))
341 {
342 close(this->notify);
343 unlink(notify);
344 free(this);
345 return NULL;
346 }
347 unlink(notify);
348
349 wait_bootup(this);
350
351 return &this->public;
352 }
353