charon-tkm: Unlink PID file after deinit

Same change as for charon in the previous commit.

References #2460.
This commit is contained in:
Tobias Brunner 2017-11-07 10:44:22 +01:00
parent 1b4d97dbb7
commit 291b02262d

View File

@ -1,8 +1,8 @@
/* /*
* Copyright (C) 2012 Tobias Brunner * Copyright (C) 2012-2017 Tobias Brunner
* Copyright (C) 2012 Reto Buerki * Copyright (C) 2012 Reto Buerki
* Copyright (C) 2012 Adrian-Ken Rueegsegger * Copyright (C) 2012 Adrian-Ken Rueegsegger
* 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
@ -24,6 +24,7 @@
#include <sys/types.h> #include <sys/types.h>
#include <unistd.h> #include <unistd.h>
#include <libgen.h> #include <libgen.h>
#include <fcntl.h>
#include <errno.h> #include <errno.h>
#include <daemon.h> #include <daemon.h>
@ -49,6 +50,11 @@
*/ */
static tkm_listener_t *listener; static tkm_listener_t *listener;
/**
* Name of the daemon
*/
static char *dmn_name;
/** /**
* PID file, in which charon-tkm stores its process id * PID file, in which charon-tkm stores its process id
*/ */
@ -186,8 +192,11 @@ 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, "%s already running ('%s' exists)", dmn_name,
pidfile_name);
return TRUE; return TRUE;
} }
} }
@ -199,13 +208,26 @@ static bool check_pidfile()
pidfile = fopen(pidfile_name, "w"); pidfile = fopen(pidfile_name, "w");
if (pidfile) if (pidfile)
{ {
ignore_result(fchown(fileno(pidfile), int fd;
fd = fileno(pidfile);
if (fd == -1 || fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
{
DBG1(DBG_LIB, "setting FD_CLOEXEC for '%s' failed: %s",
pidfile_name, strerror(errno));
}
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 '%s'", pidfile_name);
return TRUE;
} }
return FALSE;
} }
/** /**
@ -221,15 +243,15 @@ static void unlink_pidfile()
{ {
ignore_result(ftruncate(fileno(pidfile), 0)); ignore_result(ftruncate(fileno(pidfile), 0));
fclose(pidfile); fclose(pidfile);
unlink(pidfile_name);
} }
unlink(pidfile_name);
} }
/** /**
* Main function, starts TKM backend. * Main function, starts TKM backend.
*/ */
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
char *dmn_name;
if (argc > 0 && strlen(argv[0]) > 0) if (argc > 0 && strlen(argv[0]) > 0)
{ {
dmn_name = basename(argv[0]); dmn_name = basename(argv[0]);
@ -322,8 +344,6 @@ int main(int argc, char *argv[])
if (check_pidfile()) if (check_pidfile())
{ {
DBG1(DBG_DMN, "%s already running (\"%s\" exists)", dmn_name,
pidfile_name);
goto deinit; goto deinit;
} }
@ -372,8 +392,6 @@ int main(int argc, char *argv[])
/* main thread goes to run loop */ /* main thread goes to run loop */
run(); run();
unlink_pidfile();
free(pidfile_name);
status = 0; status = 0;
charon->bus->remove_listener(charon->bus, &listener->listener); charon->bus->remove_listener(charon->bus, &listener->listener);
listener->destroy(listener); listener->destroy(listener);
@ -384,6 +402,8 @@ deinit:
destroy_dh_mapping(); destroy_dh_mapping();
libcharon_deinit(); libcharon_deinit();
tkm_deinit(); tkm_deinit();
unlink_pidfile();
free(pidfile_name);
library_deinit(); library_deinit();
return status; return status;
} }