From e613a6f73c03b338196f71db615047af7862837e Mon Sep 17 00:00:00 2001 From: Quentin Young Date: Sun, 29 Nov 2020 02:02:26 -0500 Subject: lib: initialize scripting system in libfrr Signed-off-by: Quentin Young --- lib/libfrr.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'lib/libfrr.c') diff --git a/lib/libfrr.c b/lib/libfrr.c index 8e7777a1a9..8e21ac11b2 100644 --- a/lib/libfrr.c +++ b/lib/libfrr.c @@ -43,6 +43,7 @@ #include "frrcu.h" #include "frr_pthread.h" #include "defaults.h" +#include "frrscript.h" DEFINE_HOOK(frr_late_init, (struct thread_master * tm), (tm)) DEFINE_HOOK(frr_very_late_init, (struct thread_master * tm), (tm)) @@ -717,6 +718,7 @@ struct thread_master *frr_init(void) lib_cmd_init(); frr_pthread_init(); + frrscript_init(); log_ref_init(); log_ref_vty_init(); -- cgit v1.2.3 From fa22080d22e1e4e69f7bed6d041083df1b85b4b7 Mon Sep 17 00:00:00 2001 From: Quentin Young Date: Mon, 30 Nov 2020 17:01:03 -0500 Subject: build: HAVE_LUA -> HAVE_SCRIPTING And also guard all scripting-related stuff with HAVE_SCRIPTING. Signed-off-by: Quentin Young --- bgpd/bgp_main.c | 2 ++ bgpd/bgp_routemap.c | 13 +++++++------ bgpd/bgp_script.c | 6 +++++- bgpd/bgp_script.h | 8 ++++++++ configure.ac | 24 +++++++++++------------- lib/command.c | 4 ++-- lib/frrlua.c | 6 ++++-- lib/frrlua.h | 7 +++++-- lib/frrscript.c | 7 ++++++- lib/frrscript.h | 6 ++++++ lib/libfrr.c | 2 ++ 11 files changed, 58 insertions(+), 27 deletions(-) (limited to 'lib/libfrr.c') diff --git a/bgpd/bgp_main.c b/bgpd/bgp_main.c index f8669dd663..67704850a7 100644 --- a/bgpd/bgp_main.c +++ b/bgpd/bgp_main.c @@ -507,7 +507,9 @@ int main(int argc, char **argv) /* Initializations. */ bgp_vrf_init(); +#ifdef HAVE_SCRIPTING bgp_script_init(); +#endif hook_register(routing_conf_event, routing_control_plane_protocols_name_validate); diff --git a/bgpd/bgp_routemap.c b/bgpd/bgp_routemap.c index 59c49f76a7..57414eadc0 100644 --- a/bgpd/bgp_routemap.c +++ b/bgpd/bgp_routemap.c @@ -338,7 +338,7 @@ static const struct route_map_rule_cmd route_match_peer_cmd = { route_match_peer_free }; -#if defined(HAVE_LUA) +#ifdef HAVE_SCRIPTING enum frrlua_rm_status { /* @@ -468,7 +468,8 @@ static const struct route_map_rule_cmd route_match_script_cmd = { route_match_script_compile, route_match_script_free }; -#endif + +#endif /* HAVE_SCRIPTING */ /* `match ip address IP_ACCESS_LIST' */ @@ -4134,7 +4135,7 @@ DEFUN (no_match_peer, RMAP_EVENT_MATCH_DELETED); } -#if defined(HAVE_LUA) +#ifdef HAVE_SCRIPTING DEFUN (match_script, match_script_cmd, "[no] match script WORD", @@ -4156,7 +4157,7 @@ DEFUN (match_script, RMAP_EVENT_FILTER_ADDED); } } -#endif +#endif /* HAVE_SCRIPTING */ /* match probability */ DEFUN (match_probability, @@ -5670,7 +5671,7 @@ void bgp_route_map_init(void) route_map_install_match(&route_match_peer_cmd); route_map_install_match(&route_match_local_pref_cmd); -#if defined(HAVE_LUA) +#ifdef HAVE_SCRIPTING route_map_install_match(&route_match_script_cmd); #endif route_map_install_match(&route_match_ip_address_cmd); @@ -5835,7 +5836,7 @@ void bgp_route_map_init(void) install_element(RMAP_NODE, &no_set_ipv6_nexthop_prefer_global_cmd); install_element(RMAP_NODE, &set_ipv6_nexthop_peer_cmd); install_element(RMAP_NODE, &no_set_ipv6_nexthop_peer_cmd); -#if defined(HAVE_LUA) +#ifdef HAVE_SCRIPTING install_element(RMAP_NODE, &match_script_cmd); #endif } diff --git a/bgpd/bgp_script.c b/bgpd/bgp_script.c index c665f2e7c5..0cda1927f8 100644 --- a/bgpd/bgp_script.c +++ b/bgpd/bgp_script.c @@ -19,7 +19,8 @@ */ #include -#include + +#ifdef HAVE_SCRIPTING #include "bgpd.h" #include "bgp_script.h" @@ -27,6 +28,7 @@ #include "bgp_aspath.h" #include "frratomic.h" #include "frrscript.h" +#include "frrlua.h" static void lua_pushpeer(lua_State *L, const struct peer *peer) { @@ -186,3 +188,5 @@ void bgp_script_init(void) { frrscript_register_type_codecs(frrscript_codecs_bgpd); } + +#endif /* HAVE_SCRIPTING */ diff --git a/bgpd/bgp_script.h b/bgpd/bgp_script.h index 15ed3345c4..6682c2eebd 100644 --- a/bgpd/bgp_script.h +++ b/bgpd/bgp_script.h @@ -17,10 +17,18 @@ * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, * MA 02110-1301 USA */ +#ifndef __BGP_SCRIPT__ +#define __BGP_SCRIPT__ #include +#ifdef HAVE_SCRIPTING + /* * Initialize scripting stuff. */ void bgp_script_init(void); + +#endif /* HAVE_SCRIPTING */ + +#endif /* __BGP_SCRIPT__ */ diff --git a/configure.ac b/configure.ac index 6a7353d51a..11b06cb127 100755 --- a/configure.ac +++ b/configure.ac @@ -274,24 +274,22 @@ if test "$enable_clang_coverage" = "yes"; then ]) fi +if test "$enable_scripting" = "yes"; then + AX_PROG_LUA([5.3]) + AX_LUA_HEADERS + AX_LUA_LIBS([ + AC_DEFINE([HAVE_SCRIPTING], [1], [Have support for scripting]) + LIBS="$LIBS $LUA_LIB" + ]) +fi + if test "$enable_dev_build" = "yes"; then AC_DEFINE([DEV_BUILD], [1], [Build for development]) if test "$orig_cflags" = ""; then AC_C_FLAG([-g3]) AC_C_FLAG([-O0]) fi - if test "$enable_lua" = "yes"; then - AX_PROG_LUA([5.3]) - AX_LUA_HEADERS - AX_LUA_LIBS([ - AC_DEFINE([HAVE_LUA], [1], [Have support for Lua interpreter]) - LIBS="$LIBS $LUA_LIB" - ]) - fi else - if test "$enable_lua" = "yes"; then - AC_MSG_ERROR([Lua is not meant to be built/used outside of development at this time]) - fi if test "$orig_cflags" = ""; then AC_C_FLAG([-g]) AC_C_FLAG([-O2]) @@ -693,8 +691,8 @@ fi AC_ARG_ENABLE([dev_build], AS_HELP_STRING([--enable-dev-build], [build for development])) -AC_ARG_ENABLE([lua], - AS_HELP_STRING([--enable-lua], [Build Lua scripting])) +AC_ARG_ENABLE([scripting], + AS_HELP_STRING([--enable-scripting], [Build with scripting support])) if test "$enable_time_check" != "no" ; then if test "$enable_time_check" = "yes" -o "$enable_time_check" = "" ; then diff --git a/lib/command.c b/lib/command.c index 3ca5c5882b..b19147ed81 100644 --- a/lib/command.c +++ b/lib/command.c @@ -2281,7 +2281,7 @@ done: return CMD_SUCCESS; } -#ifdef DEV_BUILD +#if defined(DEV_BUILD) && defined(HAVE_SCRIPTING) DEFUN(script, script_cmd, "script SCRIPT", @@ -2399,7 +2399,7 @@ void cmd_init(int terminal) install_element(VIEW_NODE, &echo_cmd); install_element(VIEW_NODE, &autocomplete_cmd); install_element(VIEW_NODE, &find_cmd); -#ifdef DEV_BUILD +#if defined(DEV_BUILD) && defined(HAVE_SCRIPTING) install_element(VIEW_NODE, &script_cmd); #endif diff --git a/lib/frrlua.c b/lib/frrlua.c index bd1e5b00e5..cd878eb711 100644 --- a/lib/frrlua.c +++ b/lib/frrlua.c @@ -19,9 +19,11 @@ * with this program; see the file COPYING; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ + #include -#if defined(HAVE_LUA) +#ifdef HAVE_SCRIPTING + #include "prefix.h" #include "frrlua.h" #include "log.h" @@ -371,4 +373,4 @@ char *frrlua_stackdump(lua_State *L) return result; } -#endif +#endif /* HAVE_SCRIPTING */ diff --git a/lib/frrlua.h b/lib/frrlua.h index a105bd069d..8e52931e50 100644 --- a/lib/frrlua.h +++ b/lib/frrlua.h @@ -19,7 +19,9 @@ #ifndef __FRRLUA_H__ #define __FRRLUA_H__ -#if defined(HAVE_LUA) +#include + +#ifdef HAVE_SCRIPTING #include #include @@ -166,5 +168,6 @@ char *frrlua_stackdump(lua_State *L); } #endif -#endif /* HAVE_LUA */ +#endif /* HAVE_SCRIPTING */ + #endif /* __FRRLUA_H__ */ diff --git a/lib/frrscript.c b/lib/frrscript.c index 60d3c2fc6d..6d03ca119f 100644 --- a/lib/frrscript.c +++ b/lib/frrscript.c @@ -16,8 +16,10 @@ * with this program; see the file COPYING; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ - #include + +#ifdef HAVE_SCRIPTING + #include #include @@ -27,6 +29,7 @@ #include "hash.h" #include "log.h" + DEFINE_MTYPE_STATIC(LIB, SCRIPT, "Scripting"); /* Codecs */ @@ -267,3 +270,5 @@ void frrscript_init() /* Register core library types */ frrscript_register_type_codecs(frrscript_codecs_lib); } + +#endif /* HAVE_SCRIPTING */ diff --git a/lib/frrscript.h b/lib/frrscript.h index cbc0ca6c51..4206420f48 100644 --- a/lib/frrscript.h +++ b/lib/frrscript.h @@ -19,6 +19,10 @@ #ifndef __FRRSCRIPT_H__ #define __FRRSCRIPT_H__ +#include + +#ifdef HAVE_SCRIPTING + #include #include "frrlua.h" @@ -128,4 +132,6 @@ void *frrscript_get_result(struct frrscript *fs, } #endif /* __cplusplus */ +#endif /* HAVE_SCRIPTING */ + #endif /* __FRRSCRIPT_H__ */ diff --git a/lib/libfrr.c b/lib/libfrr.c index 8e21ac11b2..05e6cf36e4 100644 --- a/lib/libfrr.c +++ b/lib/libfrr.c @@ -718,7 +718,9 @@ struct thread_master *frr_init(void) lib_cmd_init(); frr_pthread_init(); +#ifdef HAVE_SCRIPTING frrscript_init(); +#endif log_ref_init(); log_ref_vty_init(); -- cgit v1.2.3 From e4e0229aba2b90d93bf681cc4e309c93f7cff61f Mon Sep 17 00:00:00 2001 From: Quentin Young Date: Mon, 30 Nov 2020 17:37:18 -0500 Subject: 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(-) (limited to 'lib/libfrr.c') 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[]; -- cgit v1.2.3