Skip to content

Commit

Permalink
daemons: lib: separated pid file creation and writing
Browse files Browse the repository at this point in the history
  • Loading branch information
alaaeddineelamri committed Oct 4, 2021
1 parent 9a95c1c commit 2e3bdcc
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 55 deletions.
6 changes: 3 additions & 3 deletions core/src/dird/dird.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion core/src/filed/filed.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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); }

Expand Down Expand Up @@ -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
Expand Down
61 changes: 13 additions & 48 deletions core/src/lib/bsys.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions core/src/lib/bsys.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion core/src/stored/stored.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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); }

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 2e3bdcc

Please sign in to comment.