In scanf the maxmium length of %s does not include the null-terminator.
[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 <freeswan.h>
27
28 #include "../pluto/constants.h"
29 #include "../pluto/defs.h"
30 #include "../pluto/log.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 plog("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 plog("charon has quit: initialization failed");
59 _stop_requested = 1;
60 }
61 if (!_stop_requested)
62 {
63 plog("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 plog("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 plog("charon stopped after %d ms", 200*i);
105 return 0;
106 }
107 plog("starter_stop_charon(): can't stop charon !!!");
108 return -1;
109 }
110 else
111 {
112 plog("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 plog("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 plog("can't fork(): %s", strerror(errno));
191 return -1;
192 case 0:
193 /* child */
194 setsid();
195 sigprocmask(SIG_SETMASK, 0, NULL);
196 /* disable glibc's malloc checker, conflicts with leak detective */
197 setenv("MALLOC_CHECK_", "0", 1);
198 execv(arg[0], arg);
199 plog("can't execv(%s,...): %s", arg[0], strerror(errno));
200 exit(1);
201 default:
202 /* father */
203 _charon_pid = pid;
204 for (i = 0; i < 500 && _charon_pid; i++)
205 {
206 /* wait for charon for a maximum of 500 x 20 ms = 10 s */
207 usleep(20000);
208 if (stat(CHARON_PID_FILE, &stb) == 0)
209 {
210 plog("charon (%d) started after %d ms", _charon_pid, 20*(i+1));
211 return 0;
212 }
213 }
214 if (_charon_pid)
215 {
216 /* If charon is started but with no ctl file, stop it */
217 plog("charon too long to start... - kill kill");
218 for (i = 0; i < 20 && (pid = _charon_pid) != 0; i++)
219 {
220 if (i == 0)
221 {
222 kill(pid, SIGINT);
223 }
224 else if (i < 10)
225 {
226 kill(pid, SIGTERM);
227 }
228 else
229 {
230 kill(pid, SIGKILL);
231 }
232 usleep(20000); /* sleep for 20 ms */
233 }
234 }
235 else
236 {
237 plog("charon refused to be started");
238 }
239 return -1;
240 }
241 }
242 return -1;
243 }