Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix duplicate process detection #2184

Merged
merged 3 commits into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions src/config/toml_writer.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,7 @@ bool writeFTLtoml(const bool verbose)
// Try to open a temporary config file for writing
FILE *fp;
if((fp = openFTLtoml("w", 0)) == NULL)
{
log_warn("Cannot write to FTL config file (%s), content not updated", strerror(errno));
return false;
}

// Write header
fprintf(fp, "# Pi-hole configuration file (%s)\n", get_FTL_version());
Expand Down
47 changes: 38 additions & 9 deletions src/daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,17 +104,27 @@ void go_daemon(void)
exit(EXIT_SUCCESS);
}

savepid();
savePID();

// Closing stdin, stdout and stderr is handled by dnsmasq
}

void savepid(void)
/**
* @brief Save the current process ID (PID) to a file.
*
* This function retrieves the PID of the current process and writes it to a
* specified file. If the file cannot be opened for writing, an error is logged.
* Otherwise, the PID is written to the file and the file is closed. The PID is
* also logged for informational purposes.
*
* @return void
*/
void savePID(void)
{
FILE *f;
// Get PID of the current process
const pid_t pid = getpid();
// Open file for writing
FILE *f = NULL;
if((f = fopen(config.files.pid.v.s, "w+")) == NULL)
{
// Log error
Expand All @@ -129,20 +139,39 @@ void savepid(void)
log_info("PID of FTL process: %i", (int)pid);
}

static void removepid(void)
/**
* @brief Empties the PID file
*
* This function opens the PID file in write mode, which effectively
* empties its contents. If the file cannot be opened, a warning is logged.
*
* @note This function does not remove the PID file, it only empties it.
*/
static void removePID(void)
{
// Note that this function is not really removing the PID file but
// rather emptying it
FILE *f;
// Open file for writing (emptying it)
FILE *f = NULL;
// Open file for writing to overwrite/empty it
if((f = fopen(config.files.pid.v.s, "w")) == NULL)
{
log_warn("Unable to empty PID file: %s", strerror(errno));
return;
}
fclose(f);

log_info("PID file emptied");
}

/**
* @brief Retrieves the username of the effective user ID of the calling process.
*
* This function uses the `geteuid()` function to get the effective user ID (EUID) of the calling process
* and then searches the user database for an entry with a matching UID using the `getpwuid()` function.
* If a matching entry is found, the username is returned. If no matching entry is found, the UID is
* returned as a string. If an error occurs during the lookup, a warning is logged.
*
* @return A dynamically allocated string containing the username or UID. The caller is responsible for
* freeing the allocated memory. Returns NULL if memory allocation fails.
*/
char *getUserName(void)
{
char *name;
Expand Down Expand Up @@ -376,7 +405,7 @@ void cleanup(const int ret)
}

// Remove PID file
removepid();
removePID();

// Free regex filter memory
free_regex();
Expand Down
2 changes: 1 addition & 1 deletion src/daemon.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
extern pthread_t threads[THREADS_MAX];

void go_daemon(void);
void savepid(void);
void savePID(void);
char *getUserName(void);
const char *hostname(void);
const char *domainname(void);
Expand Down
2 changes: 1 addition & 1 deletion src/dnsmasq_interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -3175,7 +3175,7 @@ void FTL_fork_and_bind_sockets(struct passwd *ent_pw, bool dnsmasq_start)
if(daemonmode)
go_daemon();
else
savepid();
savePID();

// Initialize query database (pihole-FTL.db)
db_init();
Expand Down
2 changes: 1 addition & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ int main (int argc, char *argv[])

// Write PID early on so systemd cannot be fooled during DELAY_STARTUP
// times. The PID in this file will later be overwritten after forking
savepid();
savePID();

// Delay startup (if requested)
// Do this before reading the database to make this option not only
Expand Down
Loading
Loading