Skip to content

Commit

Permalink
daemons: lib: pidfile of forked daemon is written
Browse files Browse the repository at this point in the history
  • Loading branch information
alaaeddineelamri committed Oct 4, 2021
1 parent c61d795 commit 9c638e7
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 8 deletions.
2 changes: 1 addition & 1 deletion core/src/dird/dird.cc
Expand Up @@ -361,7 +361,7 @@ int main(int argc, char* argv[])
if (background) {
pid_t forkpid = daemon_start();
if (forkpid) {
if (pidfile_path) { WritePidFile(pidfile_fd, pidfile_path); }
if (pidfile_path) { WritePidFile(pidfile_fd, pidfile_path, forkpid); }
#if !defined(HAVE_WIN32)
exit(0);
#endif
Expand Down
4 changes: 2 additions & 2 deletions core/src/filed/filed.cc
Expand Up @@ -208,7 +208,7 @@ int main(int argc, char* argv[])
}

int pidfile_fd = 0;
if (!foreground && !test_config) {
if (!foreground && !test_config && pidfile_path) {
pidfile_fd = CreatePidFile("bareos-fd", pidfile_path);
}

Expand Down Expand Up @@ -248,7 +248,7 @@ int main(int argc, char* argv[])
if (!foreground && !test_config) {
pid_t forkpid = daemon_start();
if (forkpid) {
if (pidfile_path) { WritePidFile(pidfile_fd, pidfile_path); }
if (pidfile_path) { WritePidFile(pidfile_fd, pidfile_path, forkpid); }
#if !defined(HAVE_WIN32)
exit(0);
#endif
Expand Down
4 changes: 2 additions & 2 deletions core/src/lib/bsys.cc
Expand Up @@ -482,7 +482,7 @@ int CreatePidFile(const char* progName, const char* pidFile)
return fd;
}

int WritePidFile(int pidfile_fd, const char* pidFile)
int WritePidFile(int pidfile_fd, const char* pidFile, pid_t fork_pid)
{
char buf[BUF_SIZE];
if (ftruncate(pidfile_fd, 0) == -1) {
Expand All @@ -491,7 +491,7 @@ int WritePidFile(int pidfile_fd, const char* pidFile)
pidFile, be.bstrerror());
}

snprintf(buf, BUF_SIZE, "%ld\n", (long)getpid());
snprintf(buf, BUF_SIZE, "%d\n", fork_pid);
if (write(pidfile_fd, buf, strlen(buf)) != strlen(buf)) {
BErrNo be;
Emsg2(M_FATAL, 0, _("Writing to PID file '%s'. ERR=%s\n"), pidFile,
Expand Down
2 changes: 1 addition & 1 deletion core/src/lib/bsys.h
Expand Up @@ -35,7 +35,7 @@ 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* progName, const char* pidFile);
int WritePidFile(int pidfile_fd, const char* pidFile);
int WritePidFile(int pidfile_fd, const char* pidFile, pid_t fork_pid);
int DeletePidFile(char* pidfile_path);
void drop(char* uid, char* gid, bool keep_readall_caps);
int Bmicrosleep(int32_t sec, int32_t usec);
Expand Down
4 changes: 2 additions & 2 deletions core/src/stored/stored.cc
Expand Up @@ -239,7 +239,7 @@ int main(int argc, char* argv[])
if (argc) { usage(); }

int pidfile_fd = 0;
if (!foreground && !test_config) {
if (!foreground && !test_config && pidfile_path) {
pidfile_fd = CreatePidFile("bareos-sd", pidfile_path);
}

Expand Down Expand Up @@ -278,7 +278,7 @@ int main(int argc, char* argv[])
if (!foreground && !test_config) {
pid_t forkpid = daemon_start();
if (forkpid) {
if (pidfile_path) { WritePidFile(pidfile_fd, pidfile_path); }
if (pidfile_path) { WritePidFile(pidfile_fd, pidfile_path, forkpid); }
#if !defined(HAVE_WIN32)
exit(0);
#endif
Expand Down

0 comments on commit 9c638e7

Please sign in to comment.