f5ca7d300f36d3c054b19c818312c5d462907ee9
[strongswan.git] / src / dumm / dumm.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 #include <sys/stat.h>
17
18 #include <debug.h>
19
20 #include "dumm.h"
21
22 typedef struct private_dumm_t private_dumm_t;
23
24 struct private_dumm_t {
25 dumm_t public;
26 linked_list_t *guests;
27 };
28
29 static guest_t* start_guest(private_dumm_t *this, char *name, char *kernel,
30 char *master, int mem)
31 {
32 guest_t *guest;
33
34 guest = guest_create(name, kernel, master, mem);
35 if (guest)
36 {
37 if (guest->start(guest))
38 {
39 this->guests->insert_last(this->guests, guest);
40 return guest;
41 }
42 guest->destroy(guest);
43 }
44 return NULL;
45 }
46
47 static iterator_t* create_guest_iterator(private_dumm_t *this)
48 {
49 return this->guests->create_iterator(this->guests, TRUE);
50 }
51
52 static void destroy(private_dumm_t *this)
53 {
54 this->guests->destroy_offset(this->guests, offsetof(guest_t, destroy));
55 free(this);
56 }
57
58 /**
59 * check for a directory, create if it does not exist
60 */
61 static bool makedir(char *dir)
62 {
63 struct stat st;
64
65 if (stat(dir, &st) != 0)
66 {
67 return mkdir(dir, S_IRWXU) == 0;
68 }
69 return S_ISDIR(st.st_mode);
70 }
71
72 dumm_t *dumm_create()
73 {
74 private_dumm_t *this = malloc_thing(private_dumm_t);
75
76 this->public.start_guest = (void*)start_guest;
77 this->public.create_guest_iterator = (void*)create_guest_iterator;
78 this->public.destroy = (void*)destroy;
79
80 if (!makedir(HOST_DIR) || !makedir(MOUNT_DIR) || !makedir(RUN_DIR))
81 {
82 free(this);
83 return NULL;
84 }
85
86 this->guests = linked_list_create();
87 return &this->public;
88 }
89