Skip to content

Commit

Permalink
daemons: lib: locking and unlocking pidfile in main program before fork
Browse files Browse the repository at this point in the history
s
  • Loading branch information
alaaeddineelamri committed Oct 25, 2021
1 parent 9b8892d commit 72a2407
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 8 deletions.
2 changes: 1 addition & 1 deletion core/src/dird/dird.cc
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions core/src/filed/filed.cc
Expand Up @@ -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.
Expand Down
24 changes: 22 additions & 2 deletions core/src/lib/bsys.cc
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -494,6 +511,9 @@ int CreatePidFile(const char* pidfile_path)
pidfile_path, be.bstrerror());
}
}

LockPidFile(progname, pidfd, pidfile_path);

return pidfd;
#endif
}
Expand Down
3 changes: 2 additions & 1 deletion core/src/lib/bsys.h
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions core/src/lib/daemon.cc
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions core/src/stored/stored.cc
Expand Up @@ -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.
Expand Down

0 comments on commit 72a2407

Please sign in to comment.