support for killing guests properly
[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* create_guest(private_dumm_t *this, char *name, char *master, int mem)
30 {
31 guest_t *guest;
32
33 guest = guest_create(name, master, mem);
34 if (guest)
35 {
36 this->guests->insert_last(this->guests, guest);
37 }
38 return guest;
39 }
40
41 static iterator_t* create_guest_iterator(private_dumm_t *this)
42 {
43 return this->guests->create_iterator(this->guests, TRUE);
44 }
45
46 /**
47 * Implementation of dumm_t.sigchild_handler.
48 */
49 static void sigchild_handler(private_dumm_t *this, siginfo_t *info)
50 {
51 switch (info->si_code)
52 {
53 case CLD_EXITED:
54 case CLD_KILLED:
55 case CLD_DUMPED:
56 case CLD_STOPPED:
57 {
58 iterator_t *iterator;
59 guest_t *guest;
60
61 iterator = this->guests->create_iterator(this->guests, TRUE);
62 while (iterator->iterate(iterator, (void**)&guest))
63 {
64 if (guest->get_pid(guest) == info->si_pid)
65 {
66 guest->sigchild(guest);
67 break;
68 }
69 }
70 iterator->destroy(iterator);
71 break;
72 }
73 default:
74 break;
75 }
76 }
77
78 static void destroy(private_dumm_t *this)
79 {
80 this->guests->destroy_offset(this->guests, offsetof(guest_t, destroy));
81 free(this);
82 }
83
84 /**
85 * check for a directory, create if it does not exist
86 */
87 static bool makedir(char *dir)
88 {
89 struct stat st;
90
91 if (stat(dir, &st) != 0)
92 {
93 return mkdir(dir, S_IRWXU) == 0;
94 }
95 return S_ISDIR(st.st_mode);
96 }
97
98 dumm_t *dumm_create()
99 {
100 private_dumm_t *this = malloc_thing(private_dumm_t);
101
102 this->public.sigchild_handler = (void(*)(dumm_t*, siginfo_t *info))sigchild_handler;
103 this->public.create_guest = (void*)create_guest;
104 this->public.create_guest_iterator = (void*)create_guest_iterator;
105 this->public.destroy = (void*)destroy;
106
107 if (!makedir(HOST_DIR) || !makedir(MOUNT_DIR) || !makedir(RUN_DIR))
108 {
109 free(this);
110 return NULL;
111 }
112
113 this->guests = linked_list_create();
114 return &this->public;
115 }
116