started own cowfs implementation
[strongswan.git] / src / dumm / guest.c
1 /*
2 * Copyright (C) 2007 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #define _GNU_SOURCE
17
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <sys/wait.h>
21 #include <sys/uio.h>
22 #include <unistd.h>
23 #include <stdio.h>
24 #include <fcntl.h>
25 #include <signal.h>
26 #include <dirent.h>
27
28 #include <debug.h>
29 #include <utils/linked_list.h>
30
31 #include "dumm.h"
32 #include "guest.h"
33 #include "mconsole.h"
34 #include "cowfs.h"
35
36 #define PERME (S_IRWXU | S_IRWXG)
37 #define PERM (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP)
38
39 typedef struct private_guest_t private_guest_t;
40
41 struct private_guest_t {
42 /** implemented public interface */
43 guest_t public;
44 /** name of the guest */
45 char *name;
46 /** directory of guest */
47 int dir;
48 /** directory name of guest */
49 char *dirname;
50 /** amount of memory for guest, in MB */
51 int mem;
52 /** pid of guest child process */
53 int pid;
54 /** state of guest */
55 guest_state_t state;
56 /** log file for console 0 */
57 int bootlog;
58 /** FUSE cowfs instance */
59 cowfs_t *cowfs;
60 /** mconsole to control running UML */
61 mconsole_t *mconsole;
62 /** list of interfaces attached to the guest */
63 linked_list_t *ifaces;
64 };
65
66 ENUM(guest_state_names, GUEST_STOPPED, GUEST_STOPPING,
67 "STOPPED",
68 "STARTING",
69 "RUNNING",
70 "PAUSED",
71 "STOPPING",
72 );
73
74 /**
75 * Implementation of guest_t.get_name.
76 */
77 static char* get_name(private_guest_t *this)
78 {
79 return this->name;
80 }
81
82 /**
83 * Implementation of guest_t.create_iface.
84 */
85 static iface_t* create_iface(private_guest_t *this, char *name)
86 {
87 iterator_t *iterator;
88 iface_t *iface;
89
90 if (this->state != GUEST_RUNNING)
91 {
92 DBG1("guest '%s' not running, unable to add interface", this->name);
93 return NULL;
94 }
95
96 iterator = this->ifaces->create_iterator(this->ifaces, TRUE);
97 while (iterator->iterate(iterator, (void**)&iface))
98 {
99 if (streq(name, iface->get_guestif(iface)))
100 {
101 DBG1("guest '%s' already has an interface '%s'", this->name, name);
102 iterator->destroy(iterator);
103 return NULL;
104 }
105 }
106 iterator->destroy(iterator);
107
108 iface = iface_create(this->name, name, this->mconsole);
109 if (iface)
110 {
111 this->ifaces->insert_last(this->ifaces, iface);
112 }
113 return iface;
114 }
115
116 /**
117 * Implementation of guest_t.create_iface_iterator.
118 */
119 static iterator_t* create_iface_iterator(private_guest_t *this)
120 {
121 return this->ifaces->create_iterator(this->ifaces, TRUE);
122 }
123
124 /**
125 * Implementation of guest_t.get_state.
126 */
127 static guest_state_t get_state(private_guest_t *this)
128 {
129 return this->state;
130 }
131
132 /**
133 * Implementation of guest_t.get_pid.
134 */
135 static pid_t get_pid(private_guest_t *this)
136 {
137 return this->pid;
138 }
139
140 /**
141 * write format string to a buffer, and advance buffer position
142 */
143 static char* write_arg(char **pos, size_t *left, char *format, ...)
144 {
145 size_t len;
146 char *res = NULL;
147 va_list args;
148
149 va_start(args, format);
150 len = vsnprintf(*pos, *left, format, args);
151 va_end(args);
152 if (len < *left)
153 {
154 res = *pos;
155 len++;
156 *pos += len + 1;
157 *left -= len + 1;
158 }
159 return res;
160 }
161
162 /**
163 * Implementation of guest_t.stop.
164 */
165 static void stop(private_guest_t *this)
166 {
167 if (this->state != GUEST_STOPPED)
168 {
169 this->state = GUEST_STOPPING;
170 this->ifaces->destroy_offset(this->ifaces, offsetof(iface_t, destroy));
171 this->ifaces = linked_list_create();
172 kill(this->pid, SIGINT);
173 while (this->state == GUEST_STOPPING)
174 {
175 sched_yield();
176 }
177 }
178 }
179
180 /**
181 * Implementation of guest_t.start.
182 */
183 static bool start(private_guest_t *this, char *kernel)
184 {
185 char buf[2048];
186 char *notify;
187 char *pos = buf;
188 char *args[16];
189 int i = 0;
190 size_t left = sizeof(buf);
191
192 if (this->state != GUEST_STOPPED)
193 {
194 DBG1("unable to start guest in state %N", guest_state_names, this->state);
195 return FALSE;
196 }
197 this->state = GUEST_STARTING;
198
199 notify = write_arg(&pos, &left, "%s/%s", this->dirname, NOTIFY_FILE);
200
201 args[i++] = write_arg(&pos, &left, "%s/%s", this->dirname, KERNEL_FILE);
202 args[i++] = write_arg(&pos, &left, "root=/dev/root");
203 args[i++] = write_arg(&pos, &left, "rootfstype=hostfs");
204 args[i++] = write_arg(&pos, &left, "rootflags=%s/%s", this->dirname, UNION_DIR);
205 args[i++] = write_arg(&pos, &left, "uml_dir=%s", this->dirname);
206 args[i++] = write_arg(&pos, &left, "umid=%s", this->name);
207 args[i++] = write_arg(&pos, &left, "mem=%dM", this->mem);
208 args[i++] = write_arg(&pos, &left, "mconsole=notify:%s", notify);
209 /*args[i++] = write_arg(&pos, &left, "con=pts");*/
210 args[i++] = write_arg(&pos, &left, "con0=null,fd:%d", this->bootlog);
211 /*args[i++] = write_arg(&pos, &left, "con1=fd:0,fd:1");*/
212 args[i++] = write_arg(&pos, &left, "con2=null,null");
213 args[i++] = write_arg(&pos, &left, "con3=null,null");
214 args[i++] = write_arg(&pos, &left, "con4=null,null");
215 args[i++] = write_arg(&pos, &left, "con5=null,null");
216 args[i++] = write_arg(&pos, &left, "con6=null,null");
217 args[i++] = NULL;
218
219 this->pid = fork();
220 switch (this->pid)
221 {
222 case 0: /* child, */
223 dup2(open("/dev/null", 0), 0);
224 dup2(open("/dev/null", 0), 1);
225 dup2(open("/dev/null", 0), 2);
226 execvp(args[0], args);
227 DBG1("starting UML kernel '%s' failed: %m", args[0]);
228 exit(1);
229 case -1:
230 this->state = GUEST_STOPPED;
231 return FALSE;
232 default:
233 break;
234 }
235 /* open mconsole */
236 this->mconsole = mconsole_create(notify);
237 if (this->mconsole == NULL)
238 {
239 DBG1("opening mconsole at '%s' failed, stopping guest", buf);
240 stop(this);
241 return FALSE;
242 }
243 this->state = GUEST_RUNNING;
244 return TRUE;
245 }
246
247 /**
248 * Implementation of guest_t.sigchild.
249 */
250 static void sigchild(private_guest_t *this)
251 {
252 waitpid(this->pid, NULL, WNOHANG);
253 DESTROY_IF(this->mconsole);
254 this->mconsole = NULL;
255 this->state = GUEST_STOPPED;
256 }
257
258 /**
259 * umount the union filesystem
260 */
261 static bool umount_unionfs(private_guest_t *this)
262 {
263 if (this->cowfs)
264 {
265 this->cowfs->destroy(this->cowfs);
266 this->cowfs = NULL;
267 return TRUE;
268 }
269 return FALSE;
270 }
271
272 /**
273 * mount the union filesystem
274 */
275 static bool mount_unionfs(private_guest_t *this)
276 {
277 char master[PATH_MAX];
278 char diff[PATH_MAX];
279 char mount[PATH_MAX];
280
281 snprintf(master, sizeof(master), "%s/%s", this->dirname, MASTER_DIR);
282 snprintf(diff, sizeof(diff), "%s/%s", this->dirname, DIFF_DIR);
283 snprintf(mount, sizeof(mount), "%s/%s", this->dirname, UNION_DIR);
284
285 if (this->cowfs == NULL)
286 {
287 this->cowfs = cowfs_create(master, diff, mount);
288 if (this->cowfs)
289 {
290 return TRUE;
291 }
292 }
293 return FALSE;
294 }
295
296 /**
297 * open logfile for boot messages
298 */
299 static int open_bootlog(private_guest_t *this)
300 {
301 int fd;
302
303 fd = openat(this->dir, LOG_FILE, O_WRONLY | O_CREAT, PERM);
304 if (fd == -1)
305 {
306 DBG1("opening bootlog failed, using stdout");
307 return 1;
308 }
309 return fd;
310 }
311
312 /**
313 * load memory configuration from file
314 */
315 int loadmem(private_guest_t *this)
316 {
317 FILE *file;
318 int mem = 0;
319
320 file = fdopen(openat(this->dir, MEMORY_FILE, O_RDONLY, PERM), "r");
321 if (file)
322 {
323 if (fscanf(file, "%d", &mem) <= 0)
324 {
325 mem = 0;
326 }
327 fclose(file);
328 }
329 return mem;
330 }
331
332 /**
333 * save memory configuration to file
334 */
335 bool savemem(private_guest_t *this, int mem)
336 {
337 FILE *file;
338 bool retval = FALSE;
339
340 file = fdopen(openat(this->dir, MEMORY_FILE, O_RDWR | O_CREAT | O_TRUNC,
341 PERM), "w");
342 if (file)
343 {
344 if (fprintf(file, "%d", mem) > 0)
345 {
346 retval = TRUE;
347 }
348 fclose(file);
349 }
350 return retval;
351 }
352
353 /**
354 * Implementation of guest_t.destroy.
355 */
356 static void destroy(private_guest_t *this)
357 {
358 stop(this);
359 umount_unionfs(this);
360 if (this->bootlog > 1)
361 {
362 close(this->bootlog);
363 }
364 if (this->dir > 0)
365 {
366 close(this->dir);
367 }
368 free(this->dirname);
369 free(this->name);
370 free(this);
371 }
372
373 /**
374 * generic guest constructor
375 */
376 static private_guest_t *guest_create_generic(char *parent, char *name,
377 bool create)
378 {
379 char cwd[PATH_MAX];
380 private_guest_t *this = malloc_thing(private_guest_t);
381
382 this->public.get_name = (void*)get_name;
383 this->public.get_pid = (pid_t(*)(guest_t*))get_pid;
384 this->public.get_state = (guest_state_t(*)(guest_t*))get_state;
385 this->public.create_iface = (iface_t*(*)(guest_t*,char*))create_iface;
386 this->public.create_iface_iterator = (iterator_t*(*)(guest_t*))create_iface_iterator;
387 this->public.start = (void*)start;
388 this->public.stop = (void*)stop;
389 this->public.sigchild = (void(*)(guest_t*))sigchild;
390 this->public.destroy = (void*)destroy;
391
392 if (*parent == '/' || getcwd(cwd, sizeof(cwd)) == NULL)
393 {
394 asprintf(&this->dirname, "%s/%s", parent, name);
395 }
396 else
397 {
398 asprintf(&this->dirname, "%s/%s/%s", cwd, parent, name);
399 }
400 if (create)
401 {
402 mkdir(this->dirname, PERME);
403 }
404 this->dir = open(this->dirname, O_DIRECTORY, PERME);
405 if (this->dir < 0)
406 {
407 DBG1("opening guest directory '%s' failed: %m", this->dirname);
408 free(this->dirname);
409 free(this);
410 return NULL;
411 }
412
413 this->pid = 0;
414 this->state = GUEST_STOPPED;
415 this->mconsole = NULL;
416 this->ifaces = linked_list_create();
417 this->mem = 0;
418 this->bootlog = open_bootlog(this);
419 this->name = strdup(name);
420 this->cowfs = NULL;
421
422 return this;
423 }
424
425 /**
426 * create a symlink to old called new in our working dir
427 */
428 static bool make_symlink(private_guest_t *this, char *old, char *new)
429 {
430 char cwd[PATH_MAX];
431 char buf[PATH_MAX];
432
433 if (*old == '/' || getcwd(cwd, sizeof(cwd)) == NULL)
434 {
435 snprintf(buf, sizeof(buf), "%s", old);
436 }
437 else
438 {
439 snprintf(buf, sizeof(buf), "%s/%s", cwd, old);
440 }
441 return symlinkat(buf, this->dir, new) == 0;
442 }
443
444
445 /**
446 * create the guest instance, including required dirs and mounts
447 */
448 guest_t *guest_create(char *parent, char *name, char *kernel,
449 char *master, int mem)
450 {
451 private_guest_t *this = guest_create_generic(parent, name, TRUE);
452
453 if (this == NULL)
454 {
455 return NULL;
456 }
457
458 if (!make_symlink(this, master, MASTER_DIR) ||
459 !make_symlink(this, kernel, KERNEL_FILE))
460 {
461 DBG1("creating master/kernel symlink failed: %m");
462 destroy(this);
463 return NULL;
464 }
465
466 if (mkdirat(this->dir, UNION_DIR, PERME) != 0 ||
467 mkdirat(this->dir, DIFF_DIR, PERME) != 0)
468 {
469 DBG1("unable to create directories for '%s': %m", name);
470 destroy(this);
471 return NULL;
472 }
473
474 this->mem = mem;
475 if (!savemem(this, mem))
476 {
477 destroy(this);
478 return NULL;
479 }
480
481 if (!mount_unionfs(this))
482 {
483 destroy(this);
484 return NULL;
485 }
486
487 return &this->public;
488 }
489
490 /**
491 * load an already created guest
492 */
493 guest_t *guest_load(char *parent, char *name)
494 {
495 private_guest_t *this = guest_create_generic(parent, name, FALSE);
496
497 if (this == NULL)
498 {
499 return NULL;
500 }
501
502 this->mem = loadmem(this);
503 if (this->mem == 0)
504 {
505 DBG1("unable to open memory configuration file: %m", name);
506 destroy(this);
507 return NULL;
508 }
509
510 if (!mount_unionfs(this))
511 {
512 destroy(this);
513 return NULL;
514 }
515
516 return &this->public;
517 }
518