1 /* strongSwan charon launcher
2 * Copyright (C) 2001-2002 Mathieu Lafon - Arkoon Network Security
3 * Copyright (C) 2006 Martin Willi - Hochschule fuer Technik Rapperswil
5 * Ported from invokepluto.c to fit charons needs.
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>.
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
18 #include <sys/types.h>
29 #include "../pluto/constants.h"
30 #include "../pluto/defs.h"
33 #include "invokecharon.h"
36 static int _charon_pid
= 0;
37 static int _stop_requested
;
39 pid_t
starter_charon_pid(void)
44 void starter_charon_sigchild(pid_t pid
, int status
)
46 if (pid
== _charon_pid
)
49 if (status
== SS_RC_LIBSTRONGSWAN_INTEGRITY
||
50 status
== SS_RC_DAEMON_INTEGRITY
)
52 DBG1(DBG_APP
, "charon has quit: integrity test of %s failed",
53 (status
== 64) ?
"libstrongswan" : "charon");
56 else if (status
== SS_RC_INITIALIZATION_FAILED
)
58 DBG1(DBG_APP
, "charon has quit: initialization failed");
63 DBG1(DBG_APP
, "charon has died -- restart scheduled (%dsec)",
64 CHARON_RESTART_DELAY
);
65 alarm(CHARON_RESTART_DELAY
); // restart in 5 sec
67 unlink(CHARON_PID_FILE
);
71 int starter_stop_charon (void)
74 pid_t pid
= _charon_pid
;
80 /* be more and more aggressive */
81 for (i
= 0; i
< 50 && (pid
= _charon_pid
) != 0; i
++)
94 DBG1(DBG_APP
, "starter_stop_charon(): charon does not respond, sending KILL");
100 usleep(200000); /* sleep for 200 ms */
102 if (_charon_pid
== 0)
104 DBG1(DBG_APP
, "charon stopped after %d ms", 200*i
);
107 DBG1(DBG_APP
, "starter_stop_charon(): can't stop charon !!!");
112 DBG1(DBG_APP
, "stater_stop_charon(): charon was not started...");
118 int starter_start_charon (starter_config_t
*cfg
, bool no_fork
, bool attach_gdb
)
122 char buffer
[BUF_LEN
];
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
134 arg
[argc
++] = "/usr/bin/gdb";
135 arg
[argc
++] = "--args";
136 arg
[argc
++] = CHARON_CMD
;
140 arg
[argc
++] = "--use-syslog";
143 /* parse debug string */
147 char *pos
= cfg
->setup
.charondebug
;
148 char *buf_pos
= buffer
;
150 while (pos
&& sscanf(pos
, "%3s %d,", type
, &level
) == 2)
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
))
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
))
168 pos
= strchr(pos
, ',');
178 DBG1(DBG_APP
, "starter_start_charon(): charon already started...");
183 unlink(CHARON_CTL_FILE
);
190 DBG1(DBG_APP
, "can't fork(): %s", strerror(errno
));
196 sigprocmask(SIG_SETMASK
, 0, NULL
);
197 /* disable glibc's malloc checker, conflicts with leak detective */
198 setenv("MALLOC_CHECK_", "0", 1);
200 DBG1(DBG_APP
, "can't execv(%s,...): %s", arg
[0], strerror(errno
));
205 for (i
= 0; i
< 500 && _charon_pid
; i
++)
207 /* wait for charon for a maximum of 500 x 20 ms = 10 s */
209 if (stat(CHARON_PID_FILE
, &stb
) == 0)
211 DBG1(DBG_APP
, "charon (%d) started after %d ms",
212 _charon_pid
, 20*(i
+1));
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
++)
234 usleep(20000); /* sleep for 20 ms */
239 DBG1(DBG_APP
, "charon refused to be started");