From 5955c00f9ca4b8c1104974854c560cad43249e4a Mon Sep 17 00:00:00 2001 From: Mark Stemm Date: Wed, 13 Jul 2016 17:57:11 -0700 Subject: [PATCH] Add a verbose flag. Add a verbose flag -v which implies printing additional info. This is passed down to lua during load_rules and sets the per-module verbose value for the compiler and parser modules. Later commits will use this to print additional info when loading rules. --- userspace/falco/falco.cpp | 9 +++++++-- userspace/falco/lua/compiler.lua | 7 +++++++ userspace/falco/lua/parser.lua | 6 ++++++ userspace/falco/lua/rule_loader.lua | 4 +++- userspace/falco/rules.cpp | 5 +++-- userspace/falco/rules.h | 2 +- 6 files changed, 27 insertions(+), 6 deletions(-) diff --git a/userspace/falco/falco.cpp b/userspace/falco/falco.cpp index d269a473..d3b8ed14 100644 --- a/userspace/falco/falco.cpp +++ b/userspace/falco/falco.cpp @@ -55,6 +55,7 @@ static void usage() " -r Rules file (defaults to value set in configuration file, or /etc/falco_rules.yaml).\n" " -L Show the name and description of all rules and exit.\n" " -l Show the name and description of the rule with name and exit.\n" + " -v Verbose output.\n" "\n" ); } @@ -253,6 +254,7 @@ int falco_init(int argc, char **argv) string pidfilename = "/var/run/falco.pid"; bool describe_all_rules = false; string describe_rule = ""; + bool verbose = false; static struct option long_options[] = { @@ -272,7 +274,7 @@ int falco_init(int argc, char **argv) // Parse the args // while((op = getopt_long(argc, argv, - "c:ho:e:r:dp:Ll:", + "c:ho:e:r:dp:Ll:v", long_options, &long_index)) != -1) { switch(op) @@ -301,6 +303,9 @@ int falco_init(int argc, char **argv) case 'L': describe_all_rules = true; break; + case 'v': + verbose = true; + break; case 'l': describe_rule = optarg; break; @@ -397,7 +402,7 @@ int falco_init(int argc, char **argv) inspector->set_drop_event_flags(EF_DROP_FALCO); - rules->load_rules(config.m_rules_filename); + rules->load_rules(config.m_rules_filename, verbose); inspector->set_filter(rules->get_filter()); falco_logger::log(LOG_INFO, "Parsed rules from file " + config.m_rules_filename + "\n"); diff --git a/userspace/falco/lua/compiler.lua b/userspace/falco/lua/compiler.lua index df88e7fb..9c6a59be 100644 --- a/userspace/falco/lua/compiler.lua +++ b/userspace/falco/lua/compiler.lua @@ -1,6 +1,13 @@ local parser = require("parser") local compiler = {} +compiler.verbose = false + +function compiler.set_verbose(verbose) + compiler.verbose = verbose + parser.set_verbose(verbose) +end + function map(f, arr) local res = {} for i,v in ipairs(arr) do diff --git a/userspace/falco/lua/parser.lua b/userspace/falco/lua/parser.lua index 5f5f9558..62b238a6 100644 --- a/userspace/falco/lua/parser.lua +++ b/userspace/falco/lua/parser.lua @@ -11,6 +11,12 @@ local parser = {} +parser.verbose = false + +function parser.set_verbose(verbose) + parser.verbose = verbose +end + local lpeg = require "lpeg" lpeg.locale(lpeg) diff --git a/userspace/falco/lua/rule_loader.lua b/userspace/falco/lua/rule_loader.lua index f668de60..24180e51 100644 --- a/userspace/falco/lua/rule_loader.lua +++ b/userspace/falco/lua/rule_loader.lua @@ -117,7 +117,9 @@ end -- to a rule. local state = {macros={}, lists={}, filter_ast=nil, rules_by_name={}, n_rules=0, rules_by_idx={}} -function load_rules(filename) +function load_rules(filename, verbose) + + compiler.set_verbose(verbose) local f = assert(io.open(filename, "r")) local s = f:read("*all") diff --git a/userspace/falco/rules.cpp b/userspace/falco/rules.cpp index dc2b7072..25926afe 100644 --- a/userspace/falco/rules.cpp +++ b/userspace/falco/rules.cpp @@ -40,7 +40,7 @@ void falco_rules::load_compiler(string lua_main_filename) } } -void falco_rules::load_rules(string rules_filename) +void falco_rules::load_rules(string rules_filename, bool verbose) { lua_getglobal(m_ls, m_lua_load_rules.c_str()); if(lua_isfunction(m_ls, -1)) @@ -82,7 +82,8 @@ void falco_rules::load_rules(string rules_filename) lua_setglobal(m_ls, m_lua_ignored_syscalls.c_str()); lua_pushstring(m_ls, rules_filename.c_str()); - if(lua_pcall(m_ls, 1, 0, 0) != 0) + lua_pushboolean(m_ls, (verbose ? 1 : 0)); + if(lua_pcall(m_ls, 2, 0, 0) != 0) { const char* lerr = lua_tostring(m_ls, -1); string err = "Error loading rules:" + string(lerr); diff --git a/userspace/falco/rules.h b/userspace/falco/rules.h index 91bf6fa5..547ae210 100644 --- a/userspace/falco/rules.h +++ b/userspace/falco/rules.h @@ -8,7 +8,7 @@ class falco_rules public: falco_rules(sinsp* inspector, lua_State *ls, string lua_main_filename); ~falco_rules(); - void load_rules(string rules_filename); + void load_rules(string rules_filename, bool verbose); void describe_rule(string *rule); sinsp_filter* get_filter();