aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorTobias Brunner <tobias@strongswan.org>2011-09-28 12:07:19 +0200
committerTobias Brunner <tobias@strongswan.org>2011-09-28 13:57:59 +0200
commit0b706426a58cebad08cb46363a86eab1fe007eef (patch)
treeff1b85a02ebf2497ae5b4f19ceefa72d211c1030 /src
parent6d36f8b60a947e144c6b4831f5cdde46aad2b865 (diff)
downloadstrongswan-0b706426a58cebad08cb46363a86eab1fe007eef.tar.bz2
strongswan-0b706426a58cebad08cb46363a86eab1fe007eef.tar.xz
starter: Check for processes with PIDs stored in pid files.
Diffstat (limited to 'src')
-rw-r--r--src/starter/starter.c45
1 files changed, 39 insertions, 6 deletions
diff --git a/src/starter/starter.c b/src/starter/starter.c
index d86da21b8..814713cda 100644
--- a/src/starter/starter.c
+++ b/src/starter/starter.c
@@ -217,6 +217,36 @@ static void generate_selfcert()
}
}
+static bool check_pid(char *pid_file)
+{
+ struct stat stb;
+ FILE *pidfile;
+
+ if (stat(pid_file, &stb) == 0)
+ {
+ pidfile = fopen(pid_file, "r");
+ if (pidfile)
+ {
+ char buf[64];
+ pid_t pid = 0;
+ memset(buf, 0, sizeof(buf));
+ if (fread(buf, 1, sizeof(buf), pidfile))
+ {
+ buf[sizeof(buf) - 1] = '\0';
+ pid = atoi(buf);
+ }
+ fclose(pidfile);
+ if (pid && kill(pid, 0) == 0)
+ { /* such a process is running */
+ return TRUE;
+ }
+ }
+ plog("removing pidfile '%s', process not running", pid_file);
+ unlink(pid_file);
+ }
+ return FALSE;
+}
+
static void usage(char *name)
{
fprintf(stderr, "Usage: starter [--nofork] [--auto-update <sec>] "
@@ -322,17 +352,19 @@ int main (int argc, char **argv)
exit(LSB_RC_NOT_ALLOWED);
}
- if (stat(PLUTO_PID_FILE, &stb) == 0)
+ if (check_pid(PLUTO_PID_FILE))
{
- plog("pluto is already running (%s exists) -- skipping pluto start", PLUTO_PID_FILE);
+ plog("pluto is already running (%s exists) -- skipping pluto start",
+ PLUTO_PID_FILE);
}
else
{
_action_ |= FLAG_ACTION_START_PLUTO;
}
- if (stat(CHARON_PID_FILE, &stb) == 0)
+ if (check_pid(CHARON_PID_FILE))
{
- plog("charon is already running (%s exists) -- skipping charon start", CHARON_PID_FILE);
+ plog("charon is already running (%s exists) -- skipping charon start",
+ CHARON_PID_FILE);
}
else
{
@@ -374,9 +406,10 @@ int main (int argc, char **argv)
last_reload = time_monotonic(NULL);
- if (stat(STARTER_PID_FILE, &stb) == 0)
+ if (check_pid(STARTER_PID_FILE))
{
- plog("starter is already running (%s exists) -- no fork done", STARTER_PID_FILE);
+ plog("starter is already running (%s exists) -- no fork done",
+ STARTER_PID_FILE);
confread_free(cfg);
exit(LSB_RC_SUCCESS);
}