From c61d7955d39904c0d778f5ed27cee7515c80e8e4 Mon Sep 17 00:00:00 2001 From: Alaa Eddine Elamri Date: Mon, 4 Oct 2021 17:00:10 +0200 Subject: [PATCH] daemons: lib: changed pidfile deletion --- core/src/dird/dird.cc | 11 +++++++---- core/src/filed/filed.cc | 10 ++++++---- core/src/lib/bsys.cc | 13 +++---------- core/src/lib/bsys.h | 2 +- core/src/stored/stored.cc | 10 ++++++---- 5 files changed, 23 insertions(+), 23 deletions(-) diff --git a/core/src/dird/dird.cc b/core/src/dird/dird.cc index 36db555fbf9..008d7a5abf5 100644 --- a/core/src/dird/dird.cc +++ b/core/src/dird/dird.cc @@ -95,6 +95,8 @@ static bool test_config = false; struct resource_table_reference; static alist* reload_table = nullptr; +static char* pidfile_path = nullptr; + /* Globals Imported */ extern ResourceItem job_items[]; @@ -221,7 +223,6 @@ int main(int argc, char* argv[]) bool export_config_schema = false; char* uid = nullptr; char* gid = nullptr; - char* pidfile_path = nullptr; setlocale(LC_ALL, ""); tzset(); @@ -322,7 +323,10 @@ int main(int argc, char* argv[]) } if (argc) { usage(); } - int pidfile_fd = CreatePidFile("bareos-dir", pidfile_path); + int pidfile_fd = 0; + if (!test_config && background && pidfile_path) { + pidfile_fd = CreatePidFile("bareos-dir", pidfile_path); + } // See if we want to drop privs. if (geteuid() == 0) { @@ -492,8 +496,7 @@ static if (!test_config && me) { /* we don't need to do this block in test mode */ WriteStateFile(me->working_directory, "bareos-dir", GetFirstPortHostOrder(me->DIRaddrs)); - DeletePidFile(me->pid_directory, "bareos-dir", - GetFirstPortHostOrder(me->DIRaddrs)); + DeletePidFile(pidfile_path); } Scheduler::GetMainScheduler().Terminate(); TermJobServer(); diff --git a/core/src/filed/filed.cc b/core/src/filed/filed.cc index 8da5b0e5e7b..6445fc0bdb0 100644 --- a/core/src/filed/filed.cc +++ b/core/src/filed/filed.cc @@ -51,6 +51,7 @@ extern bool PrintMessage(void* sock, const char* fmt, ...); static bool CheckResources(); static bool foreground = false; +static char* pidfile_path = nullptr; static void usage() { @@ -100,7 +101,6 @@ int main(int argc, char* argv[]) bool keep_readall_caps = false; char* uid = nullptr; char* gid = nullptr; - char* pidfile_path = nullptr; setlocale(LC_ALL, ""); tzset(); @@ -207,7 +207,10 @@ int main(int argc, char* argv[]) Emsg0(M_ERROR_TERM, 0, _("-k option has no meaning without -u option.\n")); } - int pidfile_fd = CreatePidFile("bareos-fd", pidfile_path); + int pidfile_fd = 0; + if (!foreground && !test_config) { + pidfile_fd = CreatePidFile("bareos-fd", pidfile_path); + } // See if we want to drop privs. if (geteuid() == 0) { drop(uid, gid, keep_readall_caps); } @@ -326,8 +329,7 @@ void TerminateFiled(int sig) FlushMntentCache(); WriteStateFile(me->working_directory, "bareos-fd", GetFirstPortHostOrder(me->FDaddrs)); - DeletePidFile(me->pid_directory, "bareos-fd", - GetFirstPortHostOrder(me->FDaddrs)); + DeletePidFile(pidfile_path); if (configfile != nullptr) { free(configfile); } diff --git a/core/src/lib/bsys.cc b/core/src/lib/bsys.cc index bcfc8c106a1..25662a6e4b5 100644 --- a/core/src/lib/bsys.cc +++ b/core/src/lib/bsys.cc @@ -504,19 +504,12 @@ int WritePidFile(int pidfile_fd, const char* pidFile) } // Delete the pid file if we created it -int DeletePidFile(char* dir, const char* progname, int port) +int DeletePidFile(char* pidfile_path) { #if !defined(HAVE_WIN32) - POOLMEM* fname = GetPoolMemory(PM_FNAME); - - if (!del_pid_file_ok) { - FreePoolMemory(fname); - return 0; - } + if (!del_pid_file_ok) { return 0; } del_pid_file_ok = false; - Mmsg(fname, "%s/%s.%d.pid", dir, progname, port); - unlink(fname); - FreePoolMemory(fname); + unlink(pidfile_path); #endif return 1; } diff --git a/core/src/lib/bsys.h b/core/src/lib/bsys.h index 53cb200a282..5c051e33a40 100644 --- a/core/src/lib/bsys.h +++ b/core/src/lib/bsys.h @@ -36,7 +36,7 @@ int Bvsnprintf(char* str, int32_t size, const char* format, va_list ap); int PoolSprintf(char* pool_buf, const char* fmt, ...); int CreatePidFile(const char* progName, const char* pidFile); int WritePidFile(int pidfile_fd, const char* pidFile); -int DeletePidFile(char* dir, const char* progname, int port); +int DeletePidFile(char* pidfile_path); void drop(char* uid, char* gid, bool keep_readall_caps); int Bmicrosleep(int32_t sec, int32_t usec); char* bfgets(char* s, int size, FILE* fd); diff --git a/core/src/stored/stored.cc b/core/src/stored/stored.cc index c3359195575..03c00a5a25f 100644 --- a/core/src/stored/stored.cc +++ b/core/src/stored/stored.cc @@ -84,6 +84,7 @@ extern "C" void* device_initialization(void* arg); /* Global static variables */ static bool foreground = 0; +static char* pidfile_path = nullptr; static void usage() { @@ -130,7 +131,6 @@ int main(int argc, char* argv[]) pthread_t thid; char* uid = nullptr; char* gid = nullptr; - char* pidfile_path = nullptr; setlocale(LC_ALL, ""); tzset(); @@ -238,7 +238,10 @@ int main(int argc, char* argv[]) } if (argc) { usage(); } - int pidfile_fd = CreatePidFile("bareos-sd", pidfile_path); + int pidfile_fd = 0; + if (!foreground && !test_config) { + pidfile_fd = CreatePidFile("bareos-sd", pidfile_path); + } // See if we want to drop privs. if (geteuid() == 0) { drop(uid, gid, false); } @@ -684,8 +687,7 @@ static WriteStateFile(me->working_directory, "bareos-sd", GetFirstPortHostOrder(me->SDaddrs)); - DeletePidFile(me->pid_directory, "bareos-sd", - GetFirstPortHostOrder(me->SDaddrs)); + DeletePidFile(pidfile_path); Dmsg1(200, "In TerminateStored() sig=%d\n", sig);