Skip to content
Permalink
Browse files

Daemonize after certain possible errors (#177)

* Daemonize after certain possible errors.

* Removed nochdir variable
  • Loading branch information...
Hritik14 authored and kernc committed Jan 22, 2018
1 parent 5c36832 commit 7a9f19fb6b152d9f00a0b3fe29ab266ff1f88129
Showing with 11 additions and 10 deletions.
  1. +11 −10 src/logkeys.cc
@@ -116,7 +116,7 @@ void create_PID_file()
close(pid_fd);
}
else {
if (errno == EEXIST)
if (errno == EEXIST) // This should never happen
error(EXIT_FAILURE, errno, "Another process already running? Quitting. (" PID_FILE ")");
else error(EXIT_FAILURE, errno, "Error opening PID file '" PID_FILE "'");
}
@@ -424,14 +424,6 @@ int main(int argc, char **argv)

set_signal_handling();

if (!(args.flags & FLAG_NO_DAEMON)) {
int nochdir = 0;
if (args.logfile[0] != '/')
nochdir = 1; // don't chdir (logfile specified with relative path)
int noclose = 1; // don't close streams (stderr used)
if (daemon(nochdir, noclose) == -1) // become daemon
error(EXIT_FAILURE, errno, "Failed to become daemon");
}
close(STDIN_FILENO);
// leave stderr open
if (args.logfile != "-") {
@@ -461,7 +453,16 @@ int main(int argc, char **argv)
}
if (!out)
error(EXIT_FAILURE, errno, "Error opening output file '%s'", args.logfile.c_str());


if (access(PID_FILE, F_OK) != -1) // PID file already exists
error(EXIT_FAILURE, errno, "Another process already running? Quitting. (" PID_FILE ")");

if (!(args.flags & FLAG_NO_DAEMON)) {
int noclose = 1; // don't close streams (stderr used)
if (daemon(0, noclose) == -1) // become daemon
error(EXIT_FAILURE, errno, "Failed to become daemon");
}

// now we need those privileges back in order to create system-wide PID_FILE
seteuid(0); setegid(0);
if (!(args.flags & FLAG_NO_DAEMON)) {

0 comments on commit 7a9f19f

Please sign in to comment.
You can’t perform that action at this time.