exec on a guest now returns the return value of the executed process
[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 <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 u_int32_t magic;
58 u_int32_t version;
59 u_int32_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 u_int32_t err;
68 u_int32_t more;
69 u_int32_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 u_int32_t magic;
77 u_int32_t version;
78 enum {
79 MCONSOLE_SOCKET,
80 MCONSOLE_PANIC,
81 MCONSOLE_HANG,
82 MCONSOLE_USER_NOTIFY,
83 } type;
84 u_int32_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("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("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 DBG1("received mconsole error %d: %*.s",
151 reply.err, reply.len, reply.data);
152 break;
153 }
154 }
155 }
156 while (reply.more);
157
158 return reply.err;
159 }
160
161 /**
162 * ignore error message
163 */
164 static void ignore(void *data, char *buf, size_t len)
165 {
166 }
167
168 /**
169 * Implementation of mconsole_t.add_iface.
170 */
171 static bool add_iface(private_mconsole_t *this, char *guest, char *host)
172 {
173 int tries = 0;
174
175 while (tries++ < 5)
176 {
177 if (request(this, ignore, NULL, "config %s=tuntap,%s", guest, host) == 0)
178 {
179 return TRUE;
180 }
181 usleep(10000 * tries * tries);
182 }
183 return FALSE;
184 }
185
186 /**
187 * Implementation of mconsole_t.del_iface.
188 */
189 static bool del_iface(private_mconsole_t *this, char *guest)
190 {
191 if (request(this, NULL, NULL, "remove %s", guest) != 0)
192 {
193 return FALSE;
194 }
195 return TRUE;
196 }
197
198 /**
199 * Implementation of mconsole_t.exec
200 */
201 static int exec(private_mconsole_t *this, void(*cb)(void*,char*,size_t),
202 void *data, char *cmd)
203 {
204 return request(this, cb, data, "exec %s", cmd);
205 }
206
207 /**
208 * Poll until guest is ready
209 */
210 static bool wait_bootup(private_mconsole_t *this)
211 {
212 int res;
213
214 while (TRUE)
215 {
216 res = request(this, ignore, NULL, "config eth9=mcast");
217 if (res < 0)
218 {
219 return FALSE;
220 }
221 if (res == 0)
222 {
223 while (request(this, ignore, NULL, "remove eth9") != 0)
224 {
225 usleep(50000);
226 }
227 return TRUE;
228 }
229 if (this->idle)
230 {
231 this->idle();
232 }
233 else
234 {
235 usleep(50000);
236 }
237 }
238 }
239
240 /**
241 * Implementation of mconsole_t.destroy.
242 */
243 static void destroy(private_mconsole_t *this)
244 {
245 close(this->console);
246 close(this->notify);
247 free(this);
248 }
249
250 /**
251 * setup the mconsole notify connection and wait for its readyness
252 */
253 static bool wait_for_notify(private_mconsole_t *this, char *nsock)
254 {
255 struct sockaddr_un addr;
256 mconsole_notify notify;
257 int len, flags = 0;
258
259 this->notify = socket(AF_UNIX, SOCK_DGRAM, 0);
260 if (this->notify < 0)
261 {
262 DBG1("opening mconsole notify socket failed: %m");
263 return FALSE;
264 }
265 memset(&addr, 0, sizeof(addr));
266 addr.sun_family = AF_UNIX;
267 strncpy(addr.sun_path, nsock, sizeof(addr));
268 if (bind(this->notify, (struct sockaddr*)&addr, sizeof(addr)) < 0)
269 {
270 DBG1("binding mconsole notify socket to '%s' failed: %m", nsock);
271 close(this->notify);
272 return FALSE;
273 }
274 if (this->idle)
275 {
276 flags = MSG_DONTWAIT;
277 }
278 do
279 {
280 if (this->idle)
281 {
282 this->idle();
283 }
284 len = recvfrom(this->notify, &notify, sizeof(notify), flags, NULL, 0);
285 }
286 while (len < 0 && (errno == EINTR || errno == EAGAIN));
287
288 if (len < 0 || len >= sizeof(notify))
289 {
290 DBG1("reading from mconsole notify socket failed: %m");
291 close(this->notify);
292 unlink(nsock);
293 return FALSE;
294 }
295 if (notify.magic != MCONSOLE_MAGIC ||
296 notify.version != MCONSOLE_VERSION ||
297 notify.type != MCONSOLE_SOCKET)
298 {
299 DBG1("received unexpected message from mconsole notify socket: %b",
300 &notify, sizeof(notify));
301 close(this->notify);
302 unlink(nsock);
303 return FALSE;
304 }
305 memset(&this->uml, 0, sizeof(this->uml));
306 this->uml.sun_family = AF_UNIX;
307 strncpy(this->uml.sun_path, (char*)&notify.data, sizeof(this->uml.sun_path));
308 return TRUE;
309 }
310
311 /**
312 * setup the mconsole console connection
313 */
314 static bool setup_console(private_mconsole_t *this)
315 {
316 struct sockaddr_un addr;
317
318 this->console = socket(AF_UNIX, SOCK_DGRAM, 0);
319 if (this->console < 0)
320 {
321 DBG1("opening mconsole socket failed: %m");
322 return FALSE;
323 }
324 memset(&addr, 0, sizeof(addr));
325 addr.sun_family = AF_UNIX;
326 snprintf(&addr.sun_path[1], sizeof(addr.sun_path), "%5d-%d",
327 getpid(), this->console);
328 if (bind(this->console, (struct sockaddr*)&addr, sizeof(addr)) < 0)
329 {
330 DBG1("binding mconsole socket to '%s' failed: %m", &addr.sun_path[1]);
331 close(this->console);
332 return FALSE;
333 }
334 return TRUE;
335 }
336
337 /**
338 * create the mconsole instance
339 */
340 mconsole_t *mconsole_create(char *notify, void(*idle)(void))
341 {
342 private_mconsole_t *this = malloc_thing(private_mconsole_t);
343
344 this->public.add_iface = (bool(*)(mconsole_t*, char *guest, char *host))add_iface;
345 this->public.del_iface = (bool(*)(mconsole_t*, char *guest))del_iface;
346 this->public.exec = (int(*)(mconsole_t*, void(*cb)(void*,char*,size_t), void *data, char *cmd))exec;
347 this->public.destroy = (void*)destroy;
348
349 this->idle = idle;
350
351 if (!wait_for_notify(this, notify))
352 {
353 free(this);
354 return NULL;
355 }
356
357 if (!setup_console(this))
358 {
359 close(this->notify);
360 unlink(notify);
361 free(this);
362 return NULL;
363 }
364 unlink(notify);
365
366 if (!wait_bootup(this))
367 {
368 destroy(this);
369 return NULL;
370 }
371
372 return &this->public;
373 }
374