Skip to content

Commit

Permalink
daemons: lib: changed pidfile deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
alaaeddineelamri committed Oct 4, 2021
1 parent 2e3bdcc commit c61d795
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 23 deletions.
11 changes: 7 additions & 4 deletions core/src/dird/dird.cc
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ static bool test_config = false;
struct resource_table_reference;
static alist<resource_table_reference*>* reload_table = nullptr;

static char* pidfile_path = nullptr;

/* Globals Imported */
extern ResourceItem job_items[];

Expand Down Expand Up @@ -221,7 +223,6 @@ int main(int argc, char* argv[])
bool export_config_schema = false;
char* uid = nullptr;
char* gid = nullptr;
char* pidfile_path = nullptr;

setlocale(LC_ALL, "");
tzset();
Expand Down Expand Up @@ -322,7 +323,10 @@ int main(int argc, char* argv[])
}
if (argc) { usage(); }

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

// See if we want to drop privs.
if (geteuid() == 0) {
Expand Down Expand Up @@ -492,8 +496,7 @@ static
if (!test_config && me) { /* we don't need to do this block in test mode */
WriteStateFile(me->working_directory, "bareos-dir",
GetFirstPortHostOrder(me->DIRaddrs));
DeletePidFile(me->pid_directory, "bareos-dir",
GetFirstPortHostOrder(me->DIRaddrs));
DeletePidFile(pidfile_path);
}
Scheduler::GetMainScheduler().Terminate();
TermJobServer();
Expand Down
10 changes: 6 additions & 4 deletions core/src/filed/filed.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ extern bool PrintMessage(void* sock, const char* fmt, ...);
static bool CheckResources();

static bool foreground = false;
static char* pidfile_path = nullptr;

static void usage()
{
Expand Down Expand Up @@ -100,7 +101,6 @@ int main(int argc, char* argv[])
bool keep_readall_caps = false;
char* uid = nullptr;
char* gid = nullptr;
char* pidfile_path = nullptr;

setlocale(LC_ALL, "");
tzset();
Expand Down Expand Up @@ -207,7 +207,10 @@ 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);
int pidfile_fd = 0;
if (!foreground && !test_config) {
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 @@ -326,8 +329,7 @@ void TerminateFiled(int sig)
FlushMntentCache();
WriteStateFile(me->working_directory, "bareos-fd",
GetFirstPortHostOrder(me->FDaddrs));
DeletePidFile(me->pid_directory, "bareos-fd",
GetFirstPortHostOrder(me->FDaddrs));
DeletePidFile(pidfile_path);

if (configfile != nullptr) { free(configfile); }

Expand Down
13 changes: 3 additions & 10 deletions core/src/lib/bsys.cc
Original file line number Diff line number Diff line change
Expand Up @@ -504,19 +504,12 @@ int WritePidFile(int pidfile_fd, const char* pidFile)
}

// Delete the pid file if we created it
int DeletePidFile(char* dir, const char* progname, int port)
int DeletePidFile(char* pidfile_path)
{
#if !defined(HAVE_WIN32)
POOLMEM* fname = GetPoolMemory(PM_FNAME);

if (!del_pid_file_ok) {
FreePoolMemory(fname);
return 0;
}
if (!del_pid_file_ok) { return 0; }
del_pid_file_ok = false;
Mmsg(fname, "%s/%s.%d.pid", dir, progname, port);
unlink(fname);
FreePoolMemory(fname);
unlink(pidfile_path);
#endif
return 1;
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/lib/bsys.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ 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 DeletePidFile(char* dir, const char* progname, int port);
int DeletePidFile(char* pidfile_path);
void drop(char* uid, char* gid, bool keep_readall_caps);
int Bmicrosleep(int32_t sec, int32_t usec);
char* bfgets(char* s, int size, FILE* fd);
Expand Down
10 changes: 6 additions & 4 deletions core/src/stored/stored.cc
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ extern "C" void* device_initialization(void* arg);

/* Global static variables */
static bool foreground = 0;
static char* pidfile_path = nullptr;

static void usage()
{
Expand Down Expand Up @@ -130,7 +131,6 @@ int main(int argc, char* argv[])
pthread_t thid;
char* uid = nullptr;
char* gid = nullptr;
char* pidfile_path = nullptr;

setlocale(LC_ALL, "");
tzset();
Expand Down Expand Up @@ -238,7 +238,10 @@ int main(int argc, char* argv[])
}
if (argc) { usage(); }

int pidfile_fd = CreatePidFile("bareos-sd", pidfile_path);
int pidfile_fd = 0;
if (!foreground && !test_config) {
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 @@ -684,8 +687,7 @@ static

WriteStateFile(me->working_directory, "bareos-sd",
GetFirstPortHostOrder(me->SDaddrs));
DeletePidFile(me->pid_directory, "bareos-sd",
GetFirstPortHostOrder(me->SDaddrs));
DeletePidFile(pidfile_path);

Dmsg1(200, "In TerminateStored() sig=%d\n", sig);

Expand Down

0 comments on commit c61d795

Please sign in to comment.