Skip to content

Commit

Permalink
lib: Add CLI option --moduledir to override default module location (…
Browse files Browse the repository at this point in the history
…needed for snap support)

Signed-off-by: Martin Winter <mwinter@opensourcerouting.org>
  • Loading branch information
mwinter-osr committed Apr 21, 2017
1 parent ae49894 commit 80b4df3
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
21 changes: 18 additions & 3 deletions lib/libfrr.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ DEFINE_HOOK(frr_late_init, (struct thread_master *tm), (tm))

const char frr_sysconfdir[] = SYSCONFDIR;
const char frr_vtydir[] = DAEMON_VTY_DIR;
const char frr_moduledir[] = MODULE_PATH;

char config_default[256];
static char pidfile_default[256];
Expand Down Expand Up @@ -61,14 +62,16 @@ static void opt_extend(const struct optspec *os)
}


#define OPTION_VTYSOCK 1000
#define OPTION_VTYSOCK 1000
#define OPTION_MODULEDIR 1002

static const struct option lo_always[] = {
{ "help", no_argument, NULL, 'h' },
{ "version", no_argument, NULL, 'v' },
{ "daemon", no_argument, NULL, 'd' },
{ "module", no_argument, NULL, 'M' },
{ "vty_socket", required_argument, NULL, OPTION_VTYSOCK },
{ "moduledir", required_argument, NULL, OPTION_MODULEDIR },
{ NULL }
};
static const struct optspec os_always = {
Expand All @@ -77,7 +80,8 @@ static const struct optspec os_always = {
" -v, --version Print program version\n"
" -d, --daemon Runs in daemon mode\n"
" -M, --module Load specified module\n"
" --vty_socket Override vty socket path\n",
" --vty_socket Override vty socket path\n"
" --moduledir Override modules directory\n",
lo_always
};

Expand Down Expand Up @@ -193,6 +197,7 @@ struct option_chain {
struct option_chain *next;
const char *arg;
};

static struct option_chain *modules = NULL, **modnext = &modules;
static int errors = 0;

Expand Down Expand Up @@ -277,6 +282,14 @@ static int frr_opt(int opt)
}
di->vty_sock_path = optarg;
break;
case OPTION_MODULEDIR:
if (di->module_path) {
fprintf(stderr, "----moduledir option specified more than once!\n");
errors++;
break;
}
di->module_path = optarg;
break;
case 'u':
if (di->flags & FRR_NO_PRIVSEP)
return 1;
Expand Down Expand Up @@ -319,6 +332,8 @@ struct thread_master *frr_init(void)
struct option_chain *oc;
struct frrmod_runtime *module;
char moderr[256];
const char *dir;
dir = di->module_path ? di->module_path : frr_moduledir;

srandom(time(NULL));

Expand All @@ -331,7 +346,7 @@ struct thread_master *frr_init(void)
frrmod_init(di->module);
while (modules) {
modules = (oc = modules)->next;
module = frrmod_load(oc->arg, moderr, sizeof(moderr));
module = frrmod_load(oc->arg, dir, moderr, sizeof(moderr));
if (!module) {
fprintf(stderr, "%s\n", moderr);
exit(1);
Expand Down
2 changes: 2 additions & 0 deletions lib/libfrr.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ struct frr_daemon_info {
const char *config_file;
const char *pid_file;
const char *vty_path;
const char *module_path;

const char *proghelp;
void (*printhelp)(FILE *target);
Expand Down Expand Up @@ -107,5 +108,6 @@ extern void frr_run(struct thread_master *master);
extern char config_default[256];
extern const char frr_sysconfdir[];
extern const char frr_vtydir[];
extern const char frr_moduledir[];

#endif /* _ZEBRA_FRR_H */
6 changes: 3 additions & 3 deletions lib/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ void frrmod_init(struct frrmod_runtime *modinfo)
}

struct frrmod_runtime *frrmod_load(const char *spec,
char *err, size_t err_len)
const char *dir, char *err, size_t err_len)
{
void *handle = NULL;
char name[PATH_MAX], fullpath[PATH_MAX], *args;
Expand All @@ -84,12 +84,12 @@ struct frrmod_runtime *frrmod_load(const char *spec,
if (!strchr(name, '/')) {
if (!handle && execname) {
snprintf(fullpath, sizeof(fullpath), "%s/%s_%s.so",
MODULE_PATH, execname, name);
dir, execname, name);
handle = dlopen(fullpath, RTLD_NOW | RTLD_GLOBAL);
}
if (!handle) {
snprintf(fullpath, sizeof(fullpath), "%s/%s.so",
MODULE_PATH, name);
dir, name);
handle = dlopen(fullpath, RTLD_NOW | RTLD_GLOBAL);
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/module.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ extern struct frrmod_runtime *frrmod_list;

extern void frrmod_init(struct frrmod_runtime *modinfo);
extern struct frrmod_runtime *frrmod_load(const char *spec,
char *err, size_t err_len);
const char *dir, char *err, size_t err_len);
#if 0
/* not implemented yet */
extern void frrmod_unload(struct frrmod_runtime *module);
Expand Down

0 comments on commit 80b4df3

Please sign in to comment.