From 72a240776fcd8f299dc55809a1ec183749e57e61 Mon Sep 17 00:00:00 2001 From: Alaa Eddine Elamri Date: Mon, 25 Oct 2021 12:57:50 +0200 Subject: [PATCH] daemons: lib: locking and unlocking pidfile in main program before fork s --- core/src/dird/dird.cc | 2 +- core/src/filed/filed.cc | 4 ++-- core/src/lib/bsys.cc | 24 ++++++++++++++++++++++-- core/src/lib/bsys.h | 3 ++- core/src/lib/daemon.cc | 2 ++ core/src/stored/stored.cc | 4 ++-- 6 files changed, 31 insertions(+), 8 deletions(-) diff --git a/core/src/dird/dird.cc b/core/src/dird/dird.cc index 9e4ba77e7d4..74ea20aa8cb 100644 --- a/core/src/dird/dird.cc +++ b/core/src/dird/dird.cc @@ -339,7 +339,7 @@ int main(int argc, char* argv[]) int pidfile_fd = -1; if (!test_config && background && pidfile_path) { - pidfile_fd = CreatePidFile(pidfile_path); + pidfile_fd = CreatePidFile("bareos-dir", pidfile_path); } // See if we want to drop privs. diff --git a/core/src/filed/filed.cc b/core/src/filed/filed.cc index 033a66964f2..b5fbb3cf8cc 100644 --- a/core/src/filed/filed.cc +++ b/core/src/filed/filed.cc @@ -221,9 +221,9 @@ int main(int argc, char* argv[]) Emsg0(M_ERROR_TERM, 0, _("-k option has no meaning without -u option.\n")); } - int pidfile_fd = 0; + int pidfile_fd = -1; if (!foreground && !test_config && pidfile_path) { - pidfile_fd = CreatePidFile(pidfile_path); + pidfile_fd = CreatePidFile("bareos-fd", pidfile_path); } // See if we want to drop privs. diff --git a/core/src/lib/bsys.cc b/core/src/lib/bsys.cc index 90bb2aaa770..1e385b315d8 100644 --- a/core/src/lib/bsys.cc +++ b/core/src/lib/bsys.cc @@ -417,7 +417,6 @@ int b_strerror(int errnum, char* buf, size_t bufsiz) return status; } - static void LockPidFile(const char* progname, int pidfile_fd, const char* pidfile_path) @@ -446,6 +445,24 @@ static void LockPidFile(const char* progname, } #endif } + +void UnlockPidFile(int pidfile_fd, const char* pidfile_path) +{ +#if !defined(HAVE_WIN32) + struct flock fl; + + fl.l_type = F_UNLCK; + fl.l_whence = SEEK_SET; + fl.l_start = 0; + fl.l_len = 0; + + if (fcntl(pidfile_fd, F_SETLK, &fl)) { + BErrNo be; + Emsg2(M_ERROR_TERM, 0, _("Unable to unlock PID file '%s'. ERR=%s\n"), + pidfile_path, be.bstrerror()); + } +#endif +} /* The content of this function (CreatePidFile) was inspired and modified to fit current needs from filelock/create_pid_file.c (Listing 55-4, page 1143), an @@ -458,7 +475,7 @@ static void LockPidFile(const char* progname, #define CPF_CLOEXEC 1 -int CreatePidFile(const char* pidfile_path) +int CreatePidFile(const char* progname, const char* pidfile_path) { #if !defined(HAVE_WIN32) int pidfd; @@ -494,6 +511,9 @@ int CreatePidFile(const char* pidfile_path) pidfile_path, be.bstrerror()); } } + + LockPidFile(progname, pidfd, pidfile_path); + return pidfd; #endif } diff --git a/core/src/lib/bsys.h b/core/src/lib/bsys.h index 21a23432fab..bbf4ddac9d0 100644 --- a/core/src/lib/bsys.h +++ b/core/src/lib/bsys.h @@ -36,11 +36,12 @@ int cstrlen(const char* str); int Bsnprintf(char* str, int32_t size, const char* format, ...); 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* pidfile_path); +int CreatePidFile(const char* progname, const char* pidfile_path); void WritePidFile(const int& pidfile_fd, const char* pidfile_path, const pid_t& forkpid, const char* progname); +void UnlockPidFile(int pidfile_fd, const char* pidfile_path); int DeletePidFile(const char* pidfile_path); void drop(char* uid, char* gid, bool keep_readall_caps); int Bmicrosleep(int32_t sec, int32_t usec); diff --git a/core/src/lib/daemon.cc b/core/src/lib/daemon.cc index b2d593d423b..2369e4c30d3 100644 --- a/core/src/lib/daemon.cc +++ b/core/src/lib/daemon.cc @@ -68,6 +68,8 @@ void daemon_start(const char* progname, int pidfile_fd, char* pidfile_path) { Dmsg0(900, "Enter daemon_start\n"); + if (pidfile_path) { UnlockPidFile(pidfile_fd, pidfile_path); } + pid_t forkpid = fork(); switch (forkpid) { diff --git a/core/src/stored/stored.cc b/core/src/stored/stored.cc index f9b250f6ceb..ca136e6ec1c 100644 --- a/core/src/stored/stored.cc +++ b/core/src/stored/stored.cc @@ -252,9 +252,9 @@ int main(int argc, char* argv[]) } if (argc) { usage(); } - int pidfile_fd = 0; + int pidfile_fd = -1; if (!foreground && !test_config && pidfile_path) { - pidfile_fd = CreatePidFile(pidfile_path); + pidfile_fd = CreatePidFile("bareos-sd", pidfile_path); } // See if we want to drop privs.