Skip to content

Commit

Permalink
daemons: changed how pidfiles are created
Browse files Browse the repository at this point in the history
The default is now not to create a pidfile. Daemons can be run with
parameter `-p <file>` where <file> is the full path to a pidfile that
should be created.

Previously pidfiles were created in a rather unsafe manner in a location
determined by Bareos' parameters.
  • Loading branch information
alaaeddineelamri authored and arogge committed Nov 30, 2021
1 parent 2d6b11b commit 99cbd59
Show file tree
Hide file tree
Showing 7 changed files with 285 additions and 144 deletions.
74 changes: 56 additions & 18 deletions core/src/dird/dird.cc
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 @@ -187,6 +189,9 @@ static void usage()
" -f run in foreground (for debugging)\n"
" -g <group> run as group <group>\n"
" -m print kaboom output (for debugging)\n"
#if !defined(HAVE_WIN32)
" -p <file> full path to pidfile (default: none)\n"
#endif
" -r <job> run <job> now\n"
" -s no signals (for debugging)\n"
" -t test - read configuration and exit\n"
Expand Down Expand Up @@ -233,7 +238,13 @@ int main(int argc, char* argv[])

console_command = RunConsoleCommand;

while ((ch = getopt(argc, argv, "c:d:fg:mr:stu:vx:z:?")) != -1) {
#if HAVE_WIN32
std::string allowed_parameters("c:d:fg:mr:stu:vx:z:?");
#else
std::string allowed_parameters("c:d:fg:mr:p:stu:vx:z:?");
#endif

while ((ch = getopt(argc, argv, allowed_parameters.c_str())) != -1) {
switch (ch) {
case 'c': /* specify config file */
if (configfile != nullptr) { free(configfile); }
Expand Down Expand Up @@ -262,6 +273,10 @@ int main(int argc, char* argv[])
prt_kaboom = true;
break;

case 'p':
pidfile_path = strdup(optarg);
break;

case 'r': /* run job */
if (runjob != nullptr) { free(runjob); }
if (optarg) { runjob = strdup(optarg); }
Expand Down Expand Up @@ -306,6 +321,14 @@ int main(int argc, char* argv[])
argc -= optind;
argv += optind;

if (!background && pidfile_path) {
Emsg0(M_WARNING, 0,
"Options -p and -f cannot be used together. You cannot create a "
"pid file when in foreground mode.\n");

exit(0);
}

if (!no_signals) { InitSignals(TerminateDird); }

if (argc) {
Expand All @@ -316,6 +339,12 @@ int main(int argc, char* argv[])
}
if (argc) { usage(); }

int pidfile_fd = -1;
#if !defined(HAVE_WIN32)
if (!test_config && background && pidfile_path) {
pidfile_fd = CreatePidFile("bareos-dir", pidfile_path);
}
#endif
// See if we want to drop privs.
if (geteuid() == 0) {
drop(uid, gid, false); /* reduce privileges if requested */
Expand All @@ -327,40 +356,47 @@ int main(int argc, char* argv[])
my_config = InitDirConfig(configfile, M_ERROR_TERM);
PrintConfigSchemaJson(buffer);
printf("%s\n", buffer.c_str());
goto bail_out;

TerminateDird(0);
return 0;
}

my_config = InitDirConfig(configfile, M_ERROR_TERM);
my_config->ParseConfig();

if (export_config) {
my_config->DumpResources(PrintMessage, nullptr);
goto bail_out;

TerminateDird(0);
return 0;
}

if (!CheckResources()) {
Jmsg((JobControlRecord*)NULL, M_ERROR_TERM, 0,
_("Please correct the configuration in %s\n"),
my_config->get_base_config_path().c_str());

TerminateDird(0);
return 0;
}

if (!test_config) { /* we don't need to do this block in test mode */
if (background) {
daemon_start();
daemon_start("bareos-dir", pidfile_fd, pidfile_path);
InitStackDump(); /* grab new pid */
}
}

if (InitCrypto() != 0) {
Jmsg((JobControlRecord*)nullptr, M_ERROR_TERM, 0,
_("Cryptography library initialization failed.\n"));
goto bail_out;
}

if (!CheckResources()) {
Jmsg((JobControlRecord*)NULL, M_ERROR_TERM, 0,
_("Please correct the configuration in %s\n"),
my_config->get_base_config_path().c_str());
goto bail_out;
TerminateDird(0);
return 0;
}

if (!test_config) { /* we don't need to do this block in test mode */
/* Create pid must come after we are a daemon -- so we have our final pid */
CreatePidFile(me->pid_directory, "bareos-dir",
GetFirstPortHostOrder(me->DIRaddrs));
ReadStateFile(me->working_directory, "bareos-dir",
GetFirstPortHostOrder(me->DIRaddrs));
}
Expand All @@ -383,7 +419,9 @@ int main(int argc, char* argv[])
Jmsg((JobControlRecord*)nullptr, M_ERROR_TERM, 0,
_("Please correct the configuration in %s\n"),
my_config->get_base_config_path().c_str());
goto bail_out;

TerminateDird(0);
return 0;
}

if (test_config) {
Expand All @@ -401,7 +439,9 @@ int main(int argc, char* argv[])
Jmsg((JobControlRecord*)nullptr, M_ERROR_TERM, 0,
_("Please correct the configuration in %s\n"),
my_config->get_base_config_path().c_str());
goto bail_out;

TerminateDird(0);
return 0;
}

MyNameIs(0, nullptr, me->resource_name_); /* set user defined name */
Expand Down Expand Up @@ -443,7 +483,6 @@ int main(int argc, char* argv[])
Scheduler::GetMainScheduler().Run();
}

bail_out:
TerminateDird(0);
return 0;
}
Expand Down Expand Up @@ -479,8 +518,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
57 changes: 42 additions & 15 deletions core/src/filed/filed.cc
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 All @@ -66,6 +67,9 @@ static void usage()
" -g <group> run as group <group>\n"
" -k keep readall capabilities\n"
" -m print kaboom output (for debugging)\n"
#if !defined(HAVE_WIN32)
" -p <file> full path to pidfile (default: none)\n"
#endif
" -r restore only mode\n"
" -s no signals (for debugging)\n"
" -t test configuration file and exit\n"
Expand Down Expand Up @@ -110,7 +114,13 @@ int main(int argc, char* argv[])
InitMsg(nullptr, nullptr);
daemon_start_time = time(nullptr);

while ((ch = getopt(argc, argv, "bc:d:fg:kmrstu:vx:z:?")) != -1) {
#if HAVE_WIN32
std::string allowed_parameters("bc:d:fg:kmrstu:vx:z:?");
#else
std::string allowed_parameters("bc:d:fg:kmrp:stu:vx:z:?");
#endif

while ((ch = getopt(argc, argv, allowed_parameters.c_str())) != -1) {
switch (ch) {
case 'b':
backup_only_mode = true;
Expand Down Expand Up @@ -146,6 +156,10 @@ int main(int argc, char* argv[])
prt_kaboom = true;
break;

case 'p':
pidfile_path = strdup(optarg);
break;

case 'r':
restore_only_mode = true;
break;
Expand Down Expand Up @@ -189,6 +203,14 @@ int main(int argc, char* argv[])
argc -= optind;
argv += optind;

if (foreground && pidfile_path) {
Emsg0(M_WARNING, 0,
"Options -p and -f cannot be used together. You cannot create a "
"pid file when in foreground mode.\n");

exit(0);
}

if (argc) {
if (configfile != nullptr) free(configfile);
configfile = strdup(*argv);
Expand All @@ -201,6 +223,13 @@ int main(int argc, char* argv[])
Emsg0(M_ERROR_TERM, 0, _("-k option has no meaning without -u option.\n"));
}

int pidfile_fd = -1;
#if !defined(HAVE_WIN32)
if (!foreground && !test_config && pidfile_path) {
pidfile_fd = CreatePidFile("bareos-fd", pidfile_path);
}
#endif

// See if we want to drop privs.
if (geteuid() == 0) { drop(uid, gid, keep_readall_caps); }

Expand All @@ -217,19 +246,27 @@ int main(int argc, char* argv[])
my_config = InitFdConfig(configfile, M_ERROR_TERM);
PrintConfigSchemaJson(buffer);
printf("%s\n", buffer.c_str());
goto bail_out;

exit(0);
}

my_config = InitFdConfig(configfile, M_ERROR_TERM);
my_config->ParseConfig();

if (export_config) {
my_config->DumpResources(PrintMessage, nullptr);
goto bail_out;

exit(0);
}

if (!CheckResources()) {
Emsg1(M_ERROR, 0, _("Please correct configuration file: %s\n"),
my_config->get_base_config_path().c_str());
TerminateFiled(1);
}

if (!foreground && !test_config) {
daemon_start();
daemon_start("bareos-fd", pidfile_fd, pidfile_path);
InitStackDump(); /* set new pid */
}

Expand All @@ -238,12 +275,6 @@ int main(int argc, char* argv[])
TerminateFiled(1);
}

if (!CheckResources()) {
Emsg1(M_ERROR, 0, _("Please correct configuration file: %s\n"),
my_config->get_base_config_path().c_str());
TerminateFiled(1);
}

SetWorkingDirectory(me->working_directory);

#if defined(HAVE_WIN32)
Expand All @@ -266,8 +297,6 @@ int main(int argc, char* argv[])
}

/* Maximum 1 daemon at a time */
CreatePidFile(me->pid_directory, "bareos-fd",
GetFirstPortHostOrder(me->FDaddrs));
ReadStateFile(me->working_directory, "bareos-fd",
GetFirstPortHostOrder(me->FDaddrs));
LoadFdPlugins(me->plugin_directory, me->plugin_names);
Expand All @@ -289,7 +318,6 @@ int main(int argc, char* argv[])

TerminateFiled(0);

bail_out:
exit(0);
}

Expand All @@ -314,8 +342,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

0 comments on commit 99cbd59

Please sign in to comment.