Skip to content

Commit

Permalink
Fix issue e2guardian#799 - write PID file in parent process not child
Browse files Browse the repository at this point in the history
  • Loading branch information
philipianpearce committed Feb 9, 2024
1 parent 874c997 commit 60cee16
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 31 deletions.
56 changes: 29 additions & 27 deletions src/FatController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ extern std::atomic<bool> g_is_starting;
extern OptionContainer o;
extern bool is_daemonised;

pid_t master_pid = 0;

void stat_rec::clear() {
conx = 0;
reqs = 0;
Expand Down Expand Up @@ -273,7 +275,7 @@ extern "C" {
void log_listener(Queue<std::string> *log_Q, bool is_RQlog);

// fork off into background
bool daemonise();
bool daemonise(int pidfilefd);
// create specified amount of child threads

//void handle_connections(int tindex); // needs changing to be threadish
Expand Down Expand Up @@ -307,18 +309,7 @@ bool drop_priv_completely() {
}

// Fork ourselves off into the background
bool daemonise() {
if (o.proc.no_daemon) {
return true;
}
#ifdef DEBUG_LOW
return true; // if debug mode is enabled we don't want to detach
#endif

if (is_daemonised) {
return true; // we are already daemonised so this must be a
// reload caused by a HUP
}
bool daemonise(int pidfileid) {

int nullfd = -1;
if ((nullfd = open("/dev/null", O_WRONLY, 0)) == -1) {
Expand All @@ -331,11 +322,15 @@ bool daemonise() {
// Error!!
close(nullfd);
return false;
} else if (pid != 0) {
// parent goes...
} else if (pid != 0) {// parent goes...A
master_pid = pid;
if (nullfd != -1) {
close(nullfd);
}
int rc = sysv_writepidfile(pidfileid, master_pid);
if (rc != 0) {
E2LOGGER_error("Error writing to the e2guardian.pid file: ", strerror(errno));
}
// bye-bye
exit(0);
}
Expand Down Expand Up @@ -1407,13 +1402,17 @@ int fc_controlit() //
return 1;
}

if (!daemonise()) {
// detached daemon
E2LOGGER_error("Error daemonising");
close(pidfilefd);
delete[] serversockfds;
return 1;
#ifndef DEBUG_LOW // remain in foreground when in low debug mode
if (!(is_daemonised||o.proc.no_daemon)) {
if (!daemonise(pidfilefd)) {
// detached daemon
E2LOGGER_error("Error daemonising");
close(pidfilefd);
delete[] serversockfds;
return 1;
}
}
#endif

//init open ssl
SSL_load_error_strings();
Expand All @@ -1434,13 +1433,16 @@ int fc_controlit() //
}
SSL_library_init();

// this has to be done after daemonise to ensure we get the correct PID.
rc = sysv_writepidfile(pidfilefd); // also closes the fd
if (rc != 0) {
E2LOGGER_error("Error writing to the e2guardian.pid file: ", strerror(errno));
delete[] serversockfds;
return false;
// only done now if not daemonised as pidfile must be written from parent when forking - required so that systemd will pick up master pid from pidfile
if (!is_daemonised) {
rc = sysv_writepidfile(pidfilefd, 0); // also closes the fd
if (rc != 0) {
E2LOGGER_error("Error writing to the e2guardian.pid file: ", strerror(errno));
delete[] serversockfds;
return false;
}
}

// We are now a daemon so all errors need to go in the syslog, rather
// than being reported on screen as we've detached from the console and
// trying to write to stdout will not be nice.
Expand Down
1 change: 1 addition & 0 deletions src/FatController.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ struct stat_rec {
};



#endif
6 changes: 3 additions & 3 deletions src/SysV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,11 @@ int sysv_openpidfile(std::string pidfile)
}

// write pid to file & close it
int sysv_writepidfile(int pidfilefd)
int sysv_writepidfile(int pidfilefd, pid_t pid = 0)
{
pid_t p = getpid();
if(pid == 0) pid = getpid();
char pidbuff[32];
sprintf(pidbuff, "%d", (int)p); // Messy, but it works!
sprintf(pidbuff, "%d", (int)pid); // Messy, but it works!
int len = strlen(pidbuff) + 1;
pidbuff[len - 1] = '\n';
int rc = write(pidfilefd, pidbuff, len);
Expand Down
2 changes: 1 addition & 1 deletion src/SysV.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ bool sysv_amirunning(std::string pidfile);
// delete any existing file with this name, and create a new one with relevant mode flags
int sysv_openpidfile(std::string pidfile);
// write our pid to the given file & close it
int sysv_writepidfile(int pidfilefd);
int sysv_writepidfile(int pidfilefd,pid_t master_pid);

// send HUP or USR1 to the process in the pidfile
int sysv_hup(std::string pidfile);
Expand Down

0 comments on commit 60cee16

Please sign in to comment.