From 2e3bdccbd218aef1e1e27f4ff127d884ec2abaa3 Mon Sep 17 00:00:00 2001 From: Alaa Eddine Elamri Date: Mon, 4 Oct 2021 16:02:16 +0200 Subject: [PATCH] daemons: lib: separated pid file creation and writing --- core/src/dird/dird.cc | 6 ++-- core/src/filed/filed.cc | 4 ++- core/src/lib/bsys.cc | 61 +++++++++------------------------------ core/src/lib/bsys.h | 4 +-- core/src/stored/stored.cc | 4 ++- 5 files changed, 24 insertions(+), 55 deletions(-) diff --git a/core/src/dird/dird.cc b/core/src/dird/dird.cc index 523666d49cb..36db555fbf9 100644 --- a/core/src/dird/dird.cc +++ b/core/src/dird/dird.cc @@ -322,6 +322,8 @@ int main(int argc, char* argv[]) } if (argc) { usage(); } + int pidfile_fd = CreatePidFile("bareos-dir", pidfile_path); + // See if we want to drop privs. if (geteuid() == 0) { drop(uid, gid, false); /* reduce privileges if requested */ @@ -355,9 +357,7 @@ int main(int argc, char* argv[]) if (background) { pid_t forkpid = daemon_start(); if (forkpid) { - if (pidfile_path) { - CreatePidFile(pidfile_path, "bareos-dir", forkpid); - } + if (pidfile_path) { WritePidFile(pidfile_fd, pidfile_path); } #if !defined(HAVE_WIN32) exit(0); #endif diff --git a/core/src/filed/filed.cc b/core/src/filed/filed.cc index 2d680c7c1d0..8da5b0e5e7b 100644 --- a/core/src/filed/filed.cc +++ b/core/src/filed/filed.cc @@ -207,6 +207,8 @@ 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); + // See if we want to drop privs. if (geteuid() == 0) { drop(uid, gid, keep_readall_caps); } @@ -243,7 +245,7 @@ int main(int argc, char* argv[]) if (!foreground && !test_config) { pid_t forkpid = daemon_start(); if (forkpid) { - if (pidfile_path) { CreatePidFile(pidfile_path, "bareos-fd", forkpid); } + if (pidfile_path) { WritePidFile(pidfile_fd, pidfile_path); } #if !defined(HAVE_WIN32) exit(0); #endif diff --git a/core/src/lib/bsys.cc b/core/src/lib/bsys.cc index 455e124de6b..bcfc8c106a1 100644 --- a/core/src/lib/bsys.cc +++ b/core/src/lib/bsys.cc @@ -428,10 +428,9 @@ static bool del_pid_file_ok = false; #endif -int NewCreatePidFile(const char* progName, const char* pidFile, int flags) +int CreatePidFile(const char* progName, const char* pidFile) { int fd; - char buf[BUF_SIZE]; fd = open(pidFile, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); if (fd == -1) { @@ -440,7 +439,8 @@ int NewCreatePidFile(const char* progName, const char* pidFile, int flags) be.bstrerror()); } - if (flags & CPF_CLOEXEC) { + int flags; + if (CPF_CLOEXEC) { /* Set the close-on-exec file descriptor flag */ /* Instead of the following steps, we could (on Linux) have opened the @@ -479,63 +479,28 @@ int NewCreatePidFile(const char* progName, const char* pidFile, int flags) } } - if (ftruncate(fd, 0) == -1) { + return fd; +} + +int WritePidFile(int pidfile_fd, const char* pidFile) +{ + char buf[BUF_SIZE]; + if (ftruncate(pidfile_fd, 0) == -1) { BErrNo be; Emsg2(M_ERROR_TERM, 0, _("Could not truncate PID file '%s'. ERR=%s\n"), pidFile, be.bstrerror()); } snprintf(buf, BUF_SIZE, "%ld\n", (long)getpid()); - if (write(fd, buf, strlen(buf)) != strlen(buf)) { + if (write(pidfile_fd, buf, strlen(buf)) != strlen(buf)) { BErrNo be; Emsg2(M_FATAL, 0, _("Writing to PID file '%s'. ERR=%s\n"), pidFile, be.bstrerror()); } - return fd; -} - -// Create a standard "Unix" pid file. -void CreatePidFile(char* dir, const char* progname, pid_t newpid) -{ -#if !defined(HAVE_WIN32) - int pidfd = -1; - int len; - char pidbuf[20]; - POOLMEM* fname = GetPoolMemory(PM_FNAME); - struct stat statp; - - Mmsg(fname, "%s/%s.pid", dir, progname); - if (stat(fname, &statp) == 0) { - /* File exists, see what we have */ - *pidbuf = 0; - if ((pidfd = open(fname, O_RDONLY | O_BINARY, 0)) < 0 - || read(pidfd, &pidbuf, sizeof(pidbuf)) < 0) { - BErrNo be; - Emsg2(M_ERROR_TERM, 0, _("Cannot open pid file. %s ERR=%s\n"), fname, - be.bstrerror()); - } - - if (pidfd >= 0) { close(pidfd); } - - // He is not alive, so take over file ownership - unlink(fname); /* remove stale pid file */ - } + del_pid_file_ok = true; - // Create new pid file - if ((pidfd = open(fname, O_CREAT | O_TRUNC | O_WRONLY | O_BINARY, 0640)) - >= 0) { - len = sprintf(pidbuf, "%d\n", newpid); - write(pidfd, pidbuf, len); - close(pidfd); - del_pid_file_ok = true; /* we created it so we can delete it */ - } else { - BErrNo be; - Emsg2(M_ERROR_TERM, 0, _("Could not open pid file. %s ERR=%s\n"), fname, - be.bstrerror()); - } - FreePoolMemory(fname); -#endif + return pidfile_fd; } // Delete the pid file if we created it diff --git a/core/src/lib/bsys.h b/core/src/lib/bsys.h index 0900d768f38..53cb200a282 100644 --- a/core/src/lib/bsys.h +++ b/core/src/lib/bsys.h @@ -34,8 +34,8 @@ 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 NewCreatePidFile(const char* progName, const char* pidFile, int flags); -void CreatePidFile(char* dir, const char* progname, pid_t newpid); +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); void drop(char* uid, char* gid, bool keep_readall_caps); int Bmicrosleep(int32_t sec, int32_t usec); diff --git a/core/src/stored/stored.cc b/core/src/stored/stored.cc index 563e2844901..c3359195575 100644 --- a/core/src/stored/stored.cc +++ b/core/src/stored/stored.cc @@ -238,6 +238,8 @@ int main(int argc, char* argv[]) } if (argc) { usage(); } + int pidfile_fd = CreatePidFile("bareos-sd", pidfile_path); + // See if we want to drop privs. if (geteuid() == 0) { drop(uid, gid, false); } @@ -273,7 +275,7 @@ int main(int argc, char* argv[]) if (!foreground && !test_config) { pid_t forkpid = daemon_start(); if (forkpid) { - if (pidfile_path) { CreatePidFile(pidfile_path, "bareos-dir", forkpid); } + if (pidfile_path) { WritePidFile(pidfile_fd, pidfile_path); } #if !defined(HAVE_WIN32) exit(0); #endif