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