starter: Migrated logging to libstrongswan.
[strongswan.git] / src / starter / invokecharon.c
1 /* strongSwan charon launcher
2 * Copyright (C) 2001-2002 Mathieu Lafon - Arkoon Network Security
3 * Copyright (C) 2006 Martin Willi - Hochschule fuer Technik Rapperswil
4 *
5 * Ported from invokepluto.c to fit charons needs.
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 */
17
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <unistd.h>
21 #include <signal.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <errno.h>
25
26 #include <library.h>
27 #include <debug.h>
28
29 #include "../pluto/constants.h"
30 #include "../pluto/defs.h"
31
32 #include "confread.h"
33 #include "invokecharon.h"
34 #include "files.h"
35
36 static int _charon_pid = 0;
37 static int _stop_requested;
38
39 pid_t starter_charon_pid(void)
40 {
41 return _charon_pid;
42 }
43
44 void starter_charon_sigchild(pid_t pid, int status)
45 {
46 if (pid == _charon_pid)
47 {
48 _charon_pid = 0;
49 if (status == SS_RC_LIBSTRONGSWAN_INTEGRITY ||
50 status == SS_RC_DAEMON_INTEGRITY)
51 {
52 DBG1(DBG_APP, "charon has quit: integrity test of %s failed",
53 (status == 64) ? "libstrongswan" : "charon");
54 _stop_requested = 1;
55 }
56 else if (status == SS_RC_INITIALIZATION_FAILED)
57 {
58 DBG1(DBG_APP, "charon has quit: initialization failed");
59 _stop_requested = 1;
60 }
61 if (!_stop_requested)
62 {
63 DBG1(DBG_APP, "charon has died -- restart scheduled (%dsec)",
64 CHARON_RESTART_DELAY);
65 alarm(CHARON_RESTART_DELAY); // restart in 5 sec
66 }
67 unlink(CHARON_PID_FILE);
68 }
69 }
70
71 int starter_stop_charon (void)
72 {
73 int i;
74 pid_t pid = _charon_pid;
75
76 if (pid)
77 {
78 _stop_requested = 1;
79
80 /* be more and more aggressive */
81 for (i = 0; i < 50 && (pid = _charon_pid) != 0; i++)
82 {
83 if (i == 0)
84 {
85 kill(pid, SIGINT);
86 }
87 else if (i < 40)
88 {
89 kill(pid, SIGTERM);
90 }
91 else if (i == 40)
92 {
93 kill(pid, SIGKILL);
94 DBG1(DBG_APP, "starter_stop_charon(): charon does not respond, sending KILL");
95 }
96 else
97 {
98 kill(pid, SIGKILL);
99 }
100 usleep(200000); /* sleep for 200 ms */
101 }
102 if (_charon_pid == 0)
103 {
104 DBG1(DBG_APP, "charon stopped after %d ms", 200*i);
105 return 0;
106 }
107 DBG1(DBG_APP, "starter_stop_charon(): can't stop charon !!!");
108 return -1;
109 }
110 else
111 {
112 DBG1(DBG_APP, "stater_stop_charon(): charon was not started...");
113 }
114 return -1;
115 }
116
117
118 int starter_start_charon (starter_config_t *cfg, bool no_fork, bool attach_gdb)
119 {
120 struct stat stb;
121 int pid, i;
122 char buffer[BUF_LEN];
123 int argc = 1;
124 char *arg[] = {
125 CHARON_CMD, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
126 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
127 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
128 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
129 };
130
131 if (attach_gdb)
132 {
133 argc = 0;
134 arg[argc++] = "/usr/bin/gdb";
135 arg[argc++] = "--args";
136 arg[argc++] = CHARON_CMD;
137 }
138 if (!no_fork)
139 {
140 arg[argc++] = "--use-syslog";
141 }
142
143 /* parse debug string */
144 {
145 int level;
146 char type[4];
147 char *pos = cfg->setup.charondebug;
148 char *buf_pos = buffer;
149
150 while (pos && sscanf(pos, "%3s %d,", type, &level) == 2)
151 {
152 snprintf(buf_pos, buffer + sizeof(buffer) - buf_pos, "--debug-%s", type);
153 arg[argc++] = buf_pos;
154 buf_pos += strlen(buf_pos) + 1;
155 if (buf_pos >= buffer + sizeof(buffer))
156 {
157 break;
158 }
159 snprintf(buf_pos, buffer + sizeof(buffer) - buf_pos, "%d", level);
160 arg[argc++] = buf_pos;
161 buf_pos += strlen(buf_pos) + 1;
162 if (buf_pos >= buffer + sizeof(buffer))
163 {
164 break;
165 }
166
167 /* get next */
168 pos = strchr(pos, ',');
169 if (pos)
170 {
171 pos++;
172 }
173 }
174 }
175
176 if (_charon_pid)
177 {
178 DBG1(DBG_APP, "starter_start_charon(): charon already started...");
179 return -1;
180 }
181 else
182 {
183 unlink(CHARON_CTL_FILE);
184 _stop_requested = 0;
185
186 pid = fork();
187 switch (pid)
188 {
189 case -1:
190 DBG1(DBG_APP, "can't fork(): %s", strerror(errno));
191 return -1;
192 case 0:
193 /* child */
194 setsid();
195 closefrom(3);
196 sigprocmask(SIG_SETMASK, 0, NULL);
197 /* disable glibc's malloc checker, conflicts with leak detective */
198 setenv("MALLOC_CHECK_", "0", 1);
199 execv(arg[0], arg);
200 DBG1(DBG_APP, "can't execv(%s,...): %s", arg[0], strerror(errno));
201 exit(1);
202 default:
203 /* father */
204 _charon_pid = pid;
205 for (i = 0; i < 500 && _charon_pid; i++)
206 {
207 /* wait for charon for a maximum of 500 x 20 ms = 10 s */
208 usleep(20000);
209 if (stat(CHARON_PID_FILE, &stb) == 0)
210 {
211 DBG1(DBG_APP, "charon (%d) started after %d ms",
212 _charon_pid, 20*(i+1));
213 return 0;
214 }
215 }
216 if (_charon_pid)
217 {
218 /* If charon is started but with no ctl file, stop it */
219 DBG1(DBG_APP, "charon too long to start... - kill kill");
220 for (i = 0; i < 20 && (pid = _charon_pid) != 0; i++)
221 {
222 if (i == 0)
223 {
224 kill(pid, SIGINT);
225 }
226 else if (i < 10)
227 {
228 kill(pid, SIGTERM);
229 }
230 else
231 {
232 kill(pid, SIGKILL);
233 }
234 usleep(20000); /* sleep for 20 ms */
235 }
236 }
237 else
238 {
239 DBG1(DBG_APP, "charon refused to be started");
240 }
241 return -1;
242 }
243 }
244 return -1;
245 }