extern CLIENT *me;
extern DLL_IMP_EXP char *exepath;
+extern DLL_IMP_EXP char *sysconfigpath;
extern DLL_IMP_EXP char *version;
extern DLL_IMP_EXP char *dist_name;
extern DLL_IMP_EXP int beef;
case bVarExePath:
*(char **)value = exepath;
break;
+ case bVarSysConfigPath:
+ *(char **)value = sysconfigpath;
+ break;
case bVarVersion:
*(char **)value = version;
break;
case bVarWorkingDir:
case bVarPluginDir:
case bVarExePath:
+ case bVarSysConfigPath:
case bVarVersion:
case bVarDistName:
break;
bVarReplace = 24,
bVarMaxDedupBlockSize = 25,
bVarMinDedupBlockSize = 26,
- bVarIsCanceled = 27
+ bVarIsCanceled = 27,
+ bVarSysConfigPath = 28
} bVariable;
/* Events that are passed to plugin */
config = New(CONFIG());
parse_fd_config(config, configfile, M_ERROR_TERM);
+ set_sysconfig_path(configfile); /* Set bVarSysConfigPath for plugins */
if (init_crypto() != 0) {
Emsg0(M_ERROR, 0, _("Cryptography library initialization failed.\n"));
}
}
+/* Get path/fname from argument, use realpath to get the full name if
+ * available
+ */
+void get_path_and_fname(const char *name, char **path, char **fname)
+{
+ char *l, *p;
+ char *cpath;
+ char *cargv0;
+ int len;
+ int path_max;
+ bool respath;
+
+ if (name) {
+ /* use a dynamic PATH_MAX and allocate temporary variables */
+ path_max = pathconf(name, _PC_PATH_MAX);
+ if (path_max < 4096){
+ path_max = 4096;
+ }
+ cpath = (char *)malloc(path_max);
+ cargv0 = (char *)malloc(path_max);
+
+ respath = false;
+#ifdef HAVE_REALPATH
+ /* make a canonical argv[0] */
+ if (realpath(name, cargv0) != NULL){
+ respath = true;
+ }
+#endif
+ if (!respath){
+ /* no resolved_path available in cargv0, so populate it */
+ bstrncpy(cargv0, name, path_max);
+ }
+ /* strip trailing filename and save exepath */
+ for (l=p=cargv0; *p; p++) {
+ if (IsPathSeparator(*p)) {
+ l = p; /* set pos of last path separator */
+ }
+ }
+ if (IsPathSeparator(*l)) {
+ l++;
+ } else {
+ l = cargv0;
+#if defined(HAVE_WIN32)
+ /* On Windows allow c: drive specification */
+ if (l[1] == ':') {
+ l += 2;
+ }
+#endif
+ }
+ len = strlen(l) + 1;
+ if (*fname) {
+ free(*fname);
+ }
+ *fname = (char *)malloc(len);
+ strcpy(*fname, l);
+ if (*path) {
+ free(*path);
+ }
+ /* separate exepath from exename */
+ *l = 0;
+ *path = bstrdup(cargv0);
+ if (strstr(*path, PathSeparatorUp) != NULL || strstr(*fname, PathSeparatorCur) != NULL || !IsPathSeparator(*path[0])) {
+ /* fallback to legacy code */
+ if (getcwd(cpath, path_max)) {
+ free(*path);
+ *path = (char *)malloc(strlen(cpath) + 1 + len);
+ strcpy(*path, cpath);
+ }
+ }
+ Dmsg2(500, "path=%s fname=%s\n", *path, *fname);
+ free(cpath);
+ free(cargv0);
+ }
+}
+
#ifdef TEST_PROGRAM
#include "unittests.h"
ok(bstrcasestr(p1, "xxxxxxxxxxxxxxxxxxxxxxxxx") == NULL, "Test with non existing string");
is(bstrcasestr(p1, " iS"), " is a test", "Test with a middle string");
}
+ {
+ char *fname = NULL;
+ char *path = NULL;
+ char cwd[512];
+ bstrncpy(cwd, getenv("PWD"), sizeof(cwd));
+ bstrncat(cwd, "/", sizeof(cwd));
+ get_path_and_fname("./config", &path, &fname);
+ is(fname, "config", "get_path_and_fname(./config)");
+ is(path, cwd, "get_path_and_fname(./config)");
+
+ get_path_and_fname("/etc/passwd", &path, &fname);
+ is(fname, "passwd", "get_path_and_fname(/etc/passwd)");
+ is(path, "/etc/", "get_path_and_fname(/etc/passwd)");
+
+ get_path_and_fname("/tmp/../etc/passwd", &path, &fname);
+ is(fname, "passwd", "get_path_and_fname(/tmp/../etc/passwd)");
+ is(path, "/etc/", "get_path_and_fname(/tmp/../etc/passwd)");
+
+ get_path_and_fname("/etc/./passwd", &path, &fname);
+ is(fname, "passwd", "get_path_and_fname(/etc/./passwd)");
+ is(path, "/etc/", "get_path_and_fname(/etc/./passwd)");
+
+ free(path);
+ free(fname);
+ }
return report();
}
#endif
const char *version = VERSION " (" BDATE ")";
const char *dist_name = DISTNAME " " DISTVER;
char *exepath = (char *)NULL;
+char *sysconfigpath = (char *)NULL;
char *exename = (char *)NULL;
char db_engine_name[50] = {0}; /* Database engine name or type */
char con_fname[500]; /* Console filename */
message_callback = msg_callback;
}
-
/*
* Set daemon name. Also, find canonical execution
* path. Note, exepath has spare room for tacking on
*/
void my_name_is(int argc, char *argv[], const char *name)
{
- char *l, *p;
- char *cpath;
- char *cargv0;
- int len;
- int path_max;
- bool respath;
-
if (gethostname(host_name, sizeof(host_name)) != 0) {
bstrncpy(host_name, "Hostname unknown", sizeof(host_name));
}
bstrncpy(my_name, name, sizeof(my_name));
if (argc>0 && argv && argv[0]) {
- /* use a dynamic PATH_MAX and allocate temporary variables */
- path_max = pathconf(argv[0], _PC_PATH_MAX);
- if (path_max < 4096){
- path_max = 4096;
- }
- cpath = (char *)malloc(path_max);
- cargv0 = (char *)malloc(path_max);
-
- respath = false;
-#ifdef HAVE_REALPATH
- /* make a canonical argv[0] */
- if (realpath(argv[0], cargv0) != NULL){
- respath = true;
- }
-#endif
- if (!respath){
- /* no resolved_path available in cargv0, so populate it */
- bstrncpy(cargv0, argv[0], path_max);
- }
- /* strip trailing filename and save exepath */
- for (l=p=cargv0; *p; p++) {
- if (IsPathSeparator(*p)) {
- l = p; /* set pos of last path separator */
- }
- }
- if (IsPathSeparator(*l)) {
- l++;
- } else {
- l = cargv0;
-#if defined(HAVE_WIN32)
- /* On Windows allow c: drive specification */
- if (l[1] == ':') {
- l += 2;
- }
-#endif
- }
- len = strlen(l) + 1;
- if (exename) {
- free(exename);
- }
- exename = (char *)malloc(len);
- strcpy(exename, l);
- if (exepath) {
- free(exepath);
- }
- /* separate exepath from exename */
- *l = 0;
- exepath = bstrdup(cargv0);
- if (strstr(exepath, PathSeparatorUp) != NULL || strstr(exepath, PathSeparatorCur) != NULL || !IsPathSeparator(exepath[0])) {
- /* fallback to legacy code */
- if (getcwd(cpath, path_max)) {
- free(exepath);
- exepath = (char *)malloc(strlen(cpath) + 1 + len);
- strcpy(exepath, cpath);
- }
- }
- Dmsg2(500, "exepath=%s\nexename=%s\n", exepath, exename);
- free(cpath);
- free(cargv0);
+ get_path_and_fname(argv[0], &exepath, &exename);
}
}
+void set_sysconfig_path(const char *file)
+{
+ char *notused=NULL;
+ /* /opt/bacula/etc/bacula-fd.conf -> /opt/bacula/etc/ */
+ get_path_and_fname(file, &sysconfigpath, ¬used);
+ bfree_and_null(notused);
+}
+
/* Set special ASSERT2 message where debugger can find it */
void
set_assert_msg(const char *file, int line, const char *msg)
free(exename);
exename = NULL;
}
+ if (sysconfigpath) {
+ free(sysconfigpath);
+ sysconfigpath = NULL;
+ }
if (trace_fd != -1) {
close(trace_fd);
trace_fd = -1;
void display_collector_types(HPKT &hpkt);
/* bsys.c */
+
+void get_path_and_fname(const char *file, char **path, char **fname);
const char *get_timezone();
int get_user_home_directory(const char *user, POOLMEM *&home);
int get_home_directories(const char *grpname, alist *dirs);
}
/* message.c */
+void set_sysconfig_path (const char *file);
void my_name_is (int argc, char *argv[], const char *name);
void init_msg (JCR *jcr, MSGS *msg, job_code_callback_t job_code_callback = NULL);
void term_msg (void);
return bRC_Error;
}
#endif
-
+ const char *dir;
+ bfuncs->getBaculaValue(ctx, bVarWorkingDir, &dir);
+ Mmsg(cmd, "WorkingDir=%s\n", dir);
+ rc = backend.ctx->write_command(ctx, cmd);
+ if (rc < 0) {
+ /* error */
+ return bRC_Error;
+ }
+ bfuncs->getBaculaValue(ctx, bVarSysConfigPath, &dir);
+ Mmsg(cmd, "SysconfigPath=%s\n", dir);
+ rc = backend.ctx->write_command(ctx, cmd);
+ if (rc < 0) {
+ /* error */
+ return bRC_Error;
+ }
+ bfuncs->getBaculaValue(ctx, bVarExePath, &dir);
+ Mmsg(cmd, "ExePath=%s\n", dir);
+ rc = backend.ctx->write_command(ctx, cmd);
+ if (rc < 0) {
+ /* error */
+ return bRC_Error;
+ }
// signal end of parameters block
backend.ctx->signal_eod(ctx);
/* ack Params command */