Signed-off-by: Florent Bruneau <florent.bruneau@polytechnique.org>
void common_sighandler(int sig)
{
switch (sig) {
void common_sighandler(int sig)
{
switch (sig) {
case SIGINT:
sigint = true;
return;
case SIGINT:
sigint = true;
return;
- err("Killed (got signal %d)...", sig);
+ err("Killed (got signal %d)...", sig);
pid = fork();
if (pid < 0) {
return -1;
pid = fork();
if (pid < 0) {
return -1;
- daemon_process = false;
+ daemon_process = false;
pidfile = fopen(name, "w");
if (!pidfile)
return -1;
pidfile = fopen(name, "w");
if (!pidfile)
return -1;
- fprintf(pidfile, "%d\n", getpid());
+ fprintf(pidfile, "%d\n", getpid());
return fflush(pidfile);
}
return 0;
return fflush(pidfile);
}
return 0;
static void pidfile_close(void)
{
if (pidfile) {
static void pidfile_close(void)
{
if (pidfile) {
- rewind(pidfile);
- ftruncate(fileno(pidfile), 0);
- fclose(pidfile);
+ if (daemon_process) {
+ rewind(pidfile);
+ ftruncate(fileno(pidfile), 0);
+ }
+ fclose(pidfile);
int common_setup(const char* pidfilename, bool unsafe, const char* runas_user,
const char* runas_group, bool daemonize)
{
int common_setup(const char* pidfilename, bool unsafe, const char* runas_user,
const char* runas_group, bool daemonize)
{
+ if (pidfile_open(pidfilename) < 0) {
+ crit("unable to write pidfile %s", pidfilename);
+ return EXIT_FAILURE;
+ }
+
if (!unsafe && drop_privileges(runas_user, runas_group) < 0) {
crit("unable to drop privileges");
return EXIT_FAILURE;
if (!unsafe && drop_privileges(runas_user, runas_group) < 0) {
crit("unable to drop privileges");
return EXIT_FAILURE;
- if (pidfile_open(pidfilename) < 0) {
- crit("unable to write pidfile %s", pidfilename);
- return EXIT_FAILURE;
- }
-
pidfile_refresh();
return EXIT_SUCCESS;
}
pidfile_refresh();
return EXIT_SUCCESS;
}
static void common_shutdown(void)
{
static void common_shutdown(void)
{
- if (daemon_process) {
- info("stopping...");
- pidfile_close();
- }
+ if (daemon_process) {
+ info("stopping...");
+ }
+ pidfile_close();
for (int i = -1; __madexit[i]; i--) {
(*__madexit[i])();
}
for (int i = -1; __madexit[i]; i--) {
(*__madexit[i])();
}