charon: Unlink PID file after daemon deinit (i.e. after unloading plugins etc.)

Make sure, though, that we only remove the file if we actually
created it (e.g. not for --help or --version).  And do so before
deinitializing libstrongswan due to leak detective.

Fixes #2460.
This commit is contained in:
Tobias Brunner 2017-11-07 10:30:05 +01:00
parent 9cc61baaf5
commit 1b4d97dbb7
1 changed files with 14 additions and 9 deletions

View File

@ -1,9 +1,9 @@
/* /*
* Copyright (C) 2006-2012 Tobias Brunner * Copyright (C) 2006-2017 Tobias Brunner
* Copyright (C) 2005-2009 Martin Willi * Copyright (C) 2005-2009 Martin Willi
* Copyright (C) 2006 Daniel Roethlisberger * Copyright (C) 2006 Daniel Roethlisberger
* Copyright (C) 2005 Jan Hutter * Copyright (C) 2005 Jan Hutter
* Hochschule fuer Technik Rapperswil * HSR Hochschule fuer Technik Rapperswil
* *
* This program is free software; you can redistribute it and/or modify it * This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the * under the terms of the GNU General Public License as published by the
@ -203,8 +203,10 @@ static bool check_pidfile()
pid = atoi(buf); pid = atoi(buf);
} }
fclose(pidfile); fclose(pidfile);
pidfile = NULL;
if (pid && kill(pid, 0) == 0) if (pid && kill(pid, 0) == 0)
{ /* such a process is running */ {
DBG1(DBG_DMN, "charon already running ('"PID_FILE"' exists)");
return TRUE; return TRUE;
} }
} }
@ -224,13 +226,18 @@ static bool check_pidfile()
DBG1(DBG_LIB, "setting FD_CLOEXEC for '"PID_FILE"' failed: %s", DBG1(DBG_LIB, "setting FD_CLOEXEC for '"PID_FILE"' failed: %s",
strerror(errno)); strerror(errno));
} }
ignore_result(fchown(fileno(pidfile), ignore_result(fchown(fd,
lib->caps->get_uid(lib->caps), lib->caps->get_uid(lib->caps),
lib->caps->get_gid(lib->caps))); lib->caps->get_gid(lib->caps)));
fprintf(pidfile, "%d\n", getpid()); fprintf(pidfile, "%d\n", getpid());
fflush(pidfile); fflush(pidfile);
return FALSE;
}
else
{
DBG1(DBG_DMN, "unable to create pidfile '"PID_FILE"'");
return TRUE;
} }
return FALSE;
} }
/** /**
@ -246,8 +253,8 @@ static void unlink_pidfile()
{ {
ignore_result(ftruncate(fileno(pidfile), 0)); ignore_result(ftruncate(fileno(pidfile), 0));
fclose(pidfile); fclose(pidfile);
unlink(PID_FILE);
} }
unlink(PID_FILE);
} }
/** /**
@ -402,7 +409,6 @@ int main(int argc, char *argv[])
if (check_pidfile()) if (check_pidfile())
{ {
DBG1(DBG_DMN, "charon already running (\""PID_FILE"\" exists)");
goto deinit; goto deinit;
} }
@ -434,12 +440,11 @@ int main(int argc, char *argv[])
/* main thread goes to run loop */ /* main thread goes to run loop */
run(); run();
/* normal termination, cleanup and exit */
unlink_pidfile();
status = 0; status = 0;
deinit: deinit:
libcharon_deinit(); libcharon_deinit();
unlink_pidfile();
library_deinit(); library_deinit();
return status; return status;
} }