diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index befe21c7ea..9c3f2e0d68 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -255,6 +255,7 @@ set(MAIN_SRCS NFA.cc Net.cc NetVar.cc + Notifier.cc Obj.cc OpaqueVal.cc Options.cc @@ -270,12 +271,12 @@ set(MAIN_SRCS RuleAction.cc RuleCondition.cc RuleMatcher.cc - SmithWaterman.cc + ScannedFile.cc Scope.cc ScriptCoverageManager.cc SerializationFormat.cc Sessions.cc - Notifier.cc + SmithWaterman.cc Stats.cc Stmt.cc Tag.cc diff --git a/src/Net.cc b/src/Net.cc index 28a7e22ab8..cb2776c626 100644 --- a/src/Net.cc +++ b/src/Net.cc @@ -397,9 +397,6 @@ void net_delete() delete zeek::detail::ip_anonymizer[i]; } -std::list files_scanned; -std::vector sig_files; - } // namespace detail bool reading_live = false; @@ -461,6 +458,3 @@ bool& is_parsing = zeek::net::is_parsing; const zeek::Packet*& current_pkt = zeek::net::current_pkt; int& current_dispatched = zeek::net::current_dispatched; double& current_timestamp = zeek::net::current_timestamp; - -std::list& files_scanned = zeek::net::detail::files_scanned; -std::vector& sig_files = zeek::net::detail::sig_files; diff --git a/src/Net.h b/src/Net.h index aedc77ce92..3034c80c8f 100644 --- a/src/Net.h +++ b/src/Net.h @@ -11,6 +11,8 @@ #include #include +#include "ScannedFile.h" + ZEEK_FORWARD_DECLARE_NAMESPACED(IOSource, zeek, iosource); ZEEK_FORWARD_DECLARE_NAMESPACED(PktSrc, zeek, iosource); ZEEK_FORWARD_DECLARE_NAMESPACED(PktDumper, zeek, iosource); @@ -44,30 +46,6 @@ extern zeek::iosource::PktDumper* pkt_dumper; // where to save packets // on future timers). extern bool have_pending_timers; - -// Script file we have already scanned (or are in the process of scanning). -// They are identified by normalized realpath. -struct ScannedFile { - int include_level; - bool skipped; // This ScannedFile was @unload'd. - bool prefixes_checked; // If loading prefixes for this file has been tried. - std::string name; - std::string canonical_path; // normalized, absolute path via realpath() - - ScannedFile(int arg_include_level, - std::string arg_name, bool arg_skipped = false, - bool arg_prefixes_checked = false); - - /** - * Compares the canonical path of this file against every canonical path - * in files_scanned and returns whether there's any match. - */ - bool AlreadyScanned() const; -}; - -extern std::list files_scanned; -extern std::vector sig_files; - } // namespace detail // Functions to temporarily suspend processing of live input (network packets @@ -144,7 +122,3 @@ extern bool& is_parsing [[deprecated("Remove in v4.1. Use zeek::net::is_parsing. extern const zeek::Packet*& current_pkt [[deprecated("Remove in v4.1. Use zeek::net::current_pkt.")]]; extern int& current_dispatched [[deprecated("Remove in v4.1. Use zeek::net::current_dispatched.")]]; extern double& current_timestamp [[deprecated("Remove in v4.1. Use zeek::net::current_timestamp.")]]; - -using ScannedFile [[deprecated("Remove in v4.1. Use zeek::net::detail::ScannedFile.")]] = zeek::net::detail::ScannedFile; -extern std::list& files_scanned [[deprecated("Remove in v4.1. Use zeek::net::detail::files_scanned.")]]; -extern std::vector& sig_files [[deprecated("Remove in v4.1. Use zeek::net::detail::sig_files.")]]; diff --git a/src/ScannedFile.cc b/src/ScannedFile.cc new file mode 100644 index 0000000000..8d69666814 --- /dev/null +++ b/src/ScannedFile.cc @@ -0,0 +1,56 @@ +#include "ScannedFile.h" + +#include +#include // for PATH_MAX + +#include "DebugLogger.h" +#include "Reporter.h" + +namespace zeek::detail { + +std::list files_scanned; +std::vector sig_files; + +ScannedFile::ScannedFile(int arg_include_level, + std::string arg_name, + bool arg_skipped, + bool arg_prefixes_checked) + : include_level(arg_include_level), + skipped(arg_skipped), + prefixes_checked(arg_prefixes_checked), + name(std::move(arg_name)) + { + if ( name == canonical_stdin_path ) + canonical_path = canonical_stdin_path; + else + { + char buf[PATH_MAX]; + auto res = realpath(name.data(), buf); + + if ( ! res ) + zeek::reporter->FatalError("failed to get realpath() of %s: %s", + name.data(), strerror(errno)); + + canonical_path = res; + } + } + +bool ScannedFile::AlreadyScanned() const + { + auto rval = false; + + for ( const auto& it : files_scanned ) + if ( it.canonical_path == canonical_path ) + { + rval = true; + break; + } + + DBG_LOG(zeek::DBG_SCRIPTS, "AlreadyScanned result (%d) %s", rval, canonical_path.data()); + return rval; + } + +} // namespace zeek::detail + +std::list& files_scanned = zeek::detail::files_scanned; +std::vector& sig_files = zeek::detail::sig_files; diff --git a/src/ScannedFile.h b/src/ScannedFile.h new file mode 100644 index 0000000000..7fe564b560 --- /dev/null +++ b/src/ScannedFile.h @@ -0,0 +1,44 @@ +// See the file "COPYING" in the main distribution directory for copyright. + +#pragma once + +#include +#include +#include + +namespace zeek::detail { + +// Script file we have already scanned (or are in the process of scanning). +// They are identified by normalized realpath. +class ScannedFile { + +public: + + ScannedFile(int arg_include_level, + std::string arg_name, bool arg_skipped = false, + bool arg_prefixes_checked = false); + + /** + * Compares the canonical path of this file against every canonical path + * in files_scanned and returns whether there's any match. + */ + bool AlreadyScanned() const; + + int include_level; + bool skipped; // This ScannedFile was @unload'd. + bool prefixes_checked; // If loading prefixes for this file has been tried. + std::string name; + std::string canonical_path; // normalized, absolute path via realpath() + + static auto constexpr canonical_stdin_path = ""; + +}; + +extern std::list files_scanned; +extern std::vector sig_files; + +} // namespace zeek::detail + +using ScannedFile [[deprecated("Remove in v4.1. Use zeek::detail::ScannedFile.")]] = zeek::detail::ScannedFile; +extern std::list& files_scanned [[deprecated("Remove in v4.1. Use zeek::detail::files_scanned.")]]; +extern std::vector& sig_files [[deprecated("Remove in v4.1. Use zeek::detail::sig_files.")]]; diff --git a/src/scan.l b/src/scan.l index 7e399f6a98..f466c1cb57 100644 --- a/src/scan.l +++ b/src/scan.l @@ -34,6 +34,7 @@ #include "Net.h" #include "Traverse.h" #include "module_util.h" +#include "ScannedFile.h" #include "analyzer/Analyzer.h" #include "zeekygen/Manager.h" @@ -347,7 +348,7 @@ when return TOK_WHEN; zeek::reporter->Error("failed to find file associated with @load-sigs %s", file); else - zeek::net::detail::sig_files.push_back(zeek::util::copy_string(path.c_str())); + zeek::detail::sig_files.push_back(zeek::util::copy_string(path.c_str())); break; case 0: @@ -404,8 +405,8 @@ when return TOK_WHEN; else { // All we have to do is pretend we've already scanned it. - zeek::net::detail::ScannedFile sf(file_stack.length(), std::move(path), true); - zeek::net::detail::files_scanned.push_back(std::move(sf)); + zeek::detail::ScannedFile sf(file_stack.length(), std::move(path), true); + zeek::detail::files_scanned.push_back(std::move(sf)); } } @@ -571,8 +572,6 @@ YYLTYPE zeek::detail::GetCurrentLocation() return currloc; } -static auto constexpr canonical_stdin_path = ""; - static int load_files(const char* orig_file) { std::string file_path = find_relative_script_file(orig_file); @@ -599,7 +598,7 @@ static int load_files(const char* orig_file) if ( zeek::util::streq(orig_file, "-") ) { f = stdin; - file_path = canonical_stdin_path; + file_path = zeek::detail::ScannedFile::canonical_stdin_path; if ( zeek::detail::g_policy_debug ) { @@ -622,7 +621,7 @@ static int load_files(const char* orig_file) zeek::reporter->FatalError("can't open %s", file_path.c_str()); } - zeek::net::detail::ScannedFile sf(file_stack.length(), file_path); + zeek::detail::ScannedFile sf(file_stack.length(), file_path); if ( sf.AlreadyScanned() ) { @@ -632,7 +631,7 @@ static int load_files(const char* orig_file) return 0; } - zeek::net::detail::files_scanned.push_back(std::move(sf)); + zeek::detail::files_scanned.push_back(std::move(sf)); if ( zeek::detail::g_policy_debug && ! file_path.empty() ) { @@ -889,13 +888,12 @@ int yywrap() // file name is discarded. If the prefix is non-empty, it gets placed // in front of the flattened path, separated with another '.' bool found_prefixed_files = false; - for ( auto it = zeek::net::detail::files_scanned.begin(); - it != zeek::net::detail::files_scanned.end(); ++it ) + for ( auto& scanned_file : zeek::detail::files_scanned ) { - if ( it->skipped || it->prefixes_checked ) + if ( scanned_file.skipped || scanned_file.prefixes_checked ) continue; - it->prefixes_checked = true; + scanned_file.prefixes_checked = true; // Prefixes are pushed onto a stack, so iterate backwards. for ( int i = zeek::detail::zeek_script_prefixes.size() - 1; i >= 0; --i ) { @@ -903,7 +901,7 @@ int yywrap() if ( ! zeek::detail::zeek_script_prefixes[i][0] ) continue; - std::string canon = zeek::util::detail::without_zeekpath_component(it->name); + std::string canon = zeek::util::detail::without_zeekpath_component(scanned_file.name); std::string flat = zeek::util::detail::flatten_script_name(canon, zeek::detail::zeek_script_prefixes[i]); std::string path = find_relative_script_file(flat); @@ -914,7 +912,7 @@ int yywrap() } //printf("====== prefix search ======\n"); - //printf("File : %s\n", it->name.c_str()); + //printf("File : %s\n", scanned_file.name.c_str()); //printf("Canon : %s\n", canon.c_str()); //printf("Flat : %s\n", flat.c_str()); //printf("Found : %s\n", path.empty() ? "F" : "T"); @@ -1027,42 +1025,3 @@ FileInfo::~FileInfo() if ( restore_module != "" ) zeek::detail::current_module = restore_module; } - -zeek::net::detail::ScannedFile::ScannedFile(int arg_include_level, - std::string arg_name, - bool arg_skipped, - bool arg_prefixes_checked) - : include_level(arg_include_level), - skipped(arg_skipped), - prefixes_checked(arg_prefixes_checked), - name(std::move(arg_name)) - { - if ( name == canonical_stdin_path ) - canonical_path = canonical_stdin_path; - else - { - char buf[PATH_MAX]; - auto res = realpath(name.data(), buf); - - if ( ! res ) - zeek::reporter->FatalError("failed to get realpath() of %s: %s", - name.data(), strerror(errno)); - - canonical_path = res; - } - } - -bool zeek::net::detail::ScannedFile::AlreadyScanned() const - { - auto rval = false; - - for ( const auto& it : zeek::net::detail::files_scanned ) - if ( it.canonical_path == canonical_path ) - { - rval = true; - break; - } - - DBG_LOG(zeek::DBG_SCRIPTS, "AlreadyScanned result (%d) %s", rval, canonical_path.data()); - return rval; - } diff --git a/src/zeek-setup.cc b/src/zeek-setup.cc index 8d458814fe..09cc15f2fa 100644 --- a/src/zeek-setup.cc +++ b/src/zeek-setup.cc @@ -46,6 +46,7 @@ extern "C" { #include "Trigger.h" #include "Hash.h" #include "Func.h" +#include "ScannedFile.h" #include "supervisor/Supervisor.h" #include "threading/Manager.h" @@ -712,7 +713,7 @@ zeek::detail::SetupResult setup(int argc, char** argv, all_signature_files.emplace_back(std::move(sf)); // Append signature files defined in @load-sigs - for ( const auto& sf : zeek::net::detail::sig_files ) + for ( const auto& sf : zeek::detail::sig_files ) all_signature_files.emplace_back(sf); if ( ! all_signature_files.empty() ) @@ -850,7 +851,7 @@ zeek::detail::SetupResult setup(int argc, char** argv, if ( zeek_script_loaded ) { // Queue events reporting loaded scripts. - for ( const auto& file : zeek::net::detail::files_scanned ) + for ( const auto& file : zeek::detail::files_scanned ) { if ( file.skipped ) continue;