From e4e0229aba2b90d93bf681cc4e309c93f7cff61f Mon Sep 17 00:00:00 2001 From: Quentin Young Date: Mon, 30 Nov 2020 17:37:18 -0500 Subject: [PATCH] lib: add support for scripts directory Specify default via --with-scriptdir at compile time, override default with --scriptdir at runtime. If unspecified, it's {sysconfdir}/scripts (usually /etc/frr/scripts) Signed-off-by: Quentin Young --- configure.ac | 11 +++++++++++ lib/frrscript.c | 7 +++++-- lib/frrscript.h | 7 ++++--- lib/libfrr.c | 14 +++++++++++++- lib/libfrr.h | 2 ++ 5 files changed, 35 insertions(+), 6 deletions(-) diff --git a/configure.ac b/configure.ac index 11b06cb127..c89aa3fa08 100755 --- a/configure.ac +++ b/configure.ac @@ -138,6 +138,12 @@ AC_ARG_WITH([moduledir], [AS_HELP_STRING([--with-moduledir=DIR], [module directo ]) AC_SUBST([moduledir], [$moduledir]) +AC_ARG_WITH([scriptdir], [AS_HELP_STRING([--with-scriptdir=DIR], [script directory (${libdir}/frr/scripts)])], [ + scriptdir="$withval" +], [ + scriptdir="\${sysconfdir}/scripts" +]) +AC_SUBST([scriptdir], [$scriptdir]) AC_ARG_WITH([yangmodelsdir], [AS_HELP_STRING([--with-yangmodelsdir=DIR], [yang models directory (${datarootdir}/yang)])], [ yangmodelsdir="$withval" @@ -2423,19 +2429,23 @@ CFG_SBIN="$sbindir" CFG_STATE="$frr_statedir" CFG_MODULE="$moduledir" CFG_YANGMODELS="$yangmodelsdir" +CFG_SCRIPT="$scriptdir" for I in 1 2 3 4 5 6 7 8 9 10; do eval CFG_SYSCONF="\"$CFG_SYSCONF\"" eval CFG_SBIN="\"$CFG_SBIN\"" eval CFG_STATE="\"$CFG_STATE\"" eval CFG_MODULE="\"$CFG_MODULE\"" eval CFG_YANGMODELS="\"$CFG_YANGMODELS\"" + eval CFG_SCRIPT="\"$CFG_SCRIPT\"" done AC_SUBST([CFG_SYSCONF]) AC_SUBST([CFG_SBIN]) AC_SUBST([CFG_STATE]) AC_SUBST([CFG_MODULE]) +AC_SUBST([CFG_SCRIPT]) AC_SUBST([CFG_YANGMODELS]) AC_DEFINE_UNQUOTED([MODULE_PATH], ["$CFG_MODULE"], [path to modules]) +AC_DEFINE_UNQUOTED([SCRIPT_PATH], ["$CFG_SCRIPT"], [path to scripts]) AC_DEFINE_UNQUOTED([YANG_MODELS_PATH], ["$CFG_YANGMODELS"], [path to YANG data models]) AC_DEFINE_UNQUOTED([WATCHFRR_SH_PATH], ["${CFG_SBIN%/}/watchfrr.sh"], [path to watchfrr.sh]) @@ -2546,6 +2556,7 @@ state file directory : ${frr_statedir} config file directory : `eval echo \`echo ${sysconfdir}\`` example directory : `eval echo \`echo ${exampledir}\`` module directory : ${CFG_MODULE} +script directory : ${CFG_SCRIPT} user to run as : ${enable_user} group to run as : ${enable_group} group for vty sockets : ${enable_vty_group} diff --git a/lib/frrscript.c b/lib/frrscript.c index 6d03ca119f..7d28379194 100644 --- a/lib/frrscript.c +++ b/lib/frrscript.c @@ -64,6 +64,7 @@ struct frrscript_codec frrscript_codecs_lib[] = { /* Type codecs */ struct hash *codec_hash; +char scriptdir[MAXPATHLEN]; static unsigned int codec_hash_key(const void *data) { @@ -213,7 +214,7 @@ struct frrscript *frrscript_load(const char *name, frrlua_export_logging(fs->L); char fname[MAXPATHLEN]; - snprintf(fname, sizeof(fname), FRRSCRIPT_PATH "/%s.lua", fs->name); + snprintf(fname, sizeof(fname), "%s/%s.lua", scriptdir, fs->name); int ret = luaL_loadfile(fs->L, fname); @@ -262,11 +263,13 @@ void frrscript_unload(struct frrscript *fs) XFREE(MTYPE_SCRIPT, fs); } -void frrscript_init() +void frrscript_init(const char *sd) { codec_hash = hash_create(codec_hash_key, codec_hash_cmp, "Lua type encoders"); + strlcpy(scriptdir, sd, sizeof(scriptdir)); + /* Register core library types */ frrscript_register_type_codecs(frrscript_codecs_lib); } diff --git a/lib/frrscript.h b/lib/frrscript.h index 4206420f48..f4057f531b 100644 --- a/lib/frrscript.h +++ b/lib/frrscript.h @@ -30,8 +30,6 @@ extern "C" { #endif -#define FRRSCRIPT_PATH "/etc/frr/scripts" - typedef void (*encoder_func)(lua_State *, const void *); typedef void *(*decoder_func)(lua_State *, int); @@ -92,8 +90,11 @@ void frrscript_register_type_codecs(struct frrscript_codec *codecs); /* * Initialize scripting subsystem. Call this before anything else. + * + * scriptdir + * Directory in which to look for scripts */ -void frrscript_init(void); +void frrscript_init(const char *scriptdir); /* diff --git a/lib/libfrr.c b/lib/libfrr.c index 05e6cf36e4..b83883779c 100644 --- a/lib/libfrr.c +++ b/lib/libfrr.c @@ -56,6 +56,7 @@ char frr_vtydir[256]; const char frr_dbdir[] = DAEMON_DB_DIR; #endif const char frr_moduledir[] = MODULE_PATH; +const char frr_scriptdir[] = SCRIPT_PATH; char frr_protoname[256] = "NONE"; char frr_protonameinst[256] = "NONE"; @@ -101,6 +102,7 @@ static void opt_extend(const struct optspec *os) #define OPTION_DB_FILE 1006 #define OPTION_LOGGING 1007 #define OPTION_LIMIT_FDS 1008 +#define OPTION_SCRIPTDIR 1009 static const struct option lo_always[] = { {"help", no_argument, NULL, 'h'}, @@ -111,6 +113,7 @@ static const struct option lo_always[] = { {"pathspace", required_argument, NULL, 'N'}, {"vty_socket", required_argument, NULL, OPTION_VTYSOCK}, {"moduledir", required_argument, NULL, OPTION_MODULEDIR}, + {"scriptdir", required_argument, NULL, OPTION_SCRIPTDIR}, {"log", required_argument, NULL, OPTION_LOG}, {"log-level", required_argument, NULL, OPTION_LOGLEVEL}, {"tcli", no_argument, NULL, OPTION_TCLI}, @@ -127,6 +130,7 @@ static const struct optspec os_always = { " -N, --pathspace Insert prefix into config & socket paths\n" " --vty_socket Override vty socket path\n" " --moduledir Override modules directory\n" + " --scriptdir Override scripts directory\n" " --log Set Logging to stdout, syslog, or file:\n" " --log-level Set Logging Level to use, debug, info, warn, etc\n" " --tcli Use transaction-based CLI\n" @@ -534,6 +538,14 @@ static int frr_opt(int opt) } di->module_path = optarg; break; + case OPTION_SCRIPTDIR: + if (di->script_path) { + fprintf(stderr, "--scriptdir option specified more than once!\n"); + errors++; + break; + } + di->script_path = optarg; + break; case OPTION_TCLI: di->cli_mode = FRR_CLI_TRANSACTIONAL; break; @@ -719,7 +731,7 @@ struct thread_master *frr_init(void) frr_pthread_init(); #ifdef HAVE_SCRIPTING - frrscript_init(); + frrscript_init(di->script_path ? di->script_path : frr_scriptdir); #endif log_ref_init(); diff --git a/lib/libfrr.h b/lib/libfrr.h index 2e4dcbe093..c446931468 100644 --- a/lib/libfrr.h +++ b/lib/libfrr.h @@ -81,6 +81,7 @@ struct frr_daemon_info { #endif const char *vty_path; const char *module_path; + const char *script_path; const char *pathspace; bool zpathspace; @@ -162,6 +163,7 @@ extern char frr_zclientpath[256]; extern const char frr_sysconfdir[]; extern char frr_vtydir[256]; extern const char frr_moduledir[]; +extern const char frr_scriptdir[]; extern char frr_protoname[]; extern char frr_protonameinst[]; -- 2.39.5