Skip to content

Commit

Permalink
refactor(libsinsp): move sinsp mode evaluation in helper class
Browse files Browse the repository at this point in the history
Signed-off-by: Leonardo Di Giovanna <leonardodigiovanna1@gmail.com>
  • Loading branch information
ekoops authored and poiana committed Feb 26, 2025
1 parent c8cc8d1 commit 5a681cb
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 42 deletions.
52 changes: 10 additions & 42 deletions userspace/libsinsp/sinsp.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ limitations under the License.
#include <libsinsp/threadinfo.h>
#include <libsinsp/tuples.h>
#include <libsinsp/utils.h>
#include <libsinsp/sinsp_mode.h>

#include <list>
#include <map>
Expand Down Expand Up @@ -106,39 +107,6 @@ class sinsp_usergroup_manager;
#define DEFAULT_OUTPUT_STR \
"*%evt.num %evt.time %evt.cpu %proc.name (%thread.tid) %evt.dir %evt.type %evt.args"

/*!
\brief Sinsp possible modes
*/
enum sinsp_mode_t {
/*!
* Default value that mostly exists so that sinsp can have a valid value
* before it is initialized.
*/
SINSP_MODE_NONE = 0,
/*!
* Read system call data from a capture file.
*/
SINSP_MODE_CAPTURE,
/*!
* Read system call data from the underlying operating system.
*/
SINSP_MODE_LIVE,
/*!
* Do not read system call data. If next is called, a dummy event is
* returned.
*/
SINSP_MODE_NODRIVER,
/*!
* Do not read system call data. Events come from the configured input plugin.
*/
SINSP_MODE_PLUGIN,
/*!
* Read system call and event data from the test event generator.
* Do not attempt to query the underlying system.
*/
SINSP_MODE_TEST,
};

/**
* @brief Possible platforms to use with plugins
*/
Expand Down Expand Up @@ -572,34 +540,34 @@ class SINSP_PUBLIC sinsp : public capture_stats_source {
/*!
\brief Returns true if the current capture is happening from a scap file
*/
inline bool is_capture() const { return m_mode == SINSP_MODE_CAPTURE; }
inline bool is_capture() const { return m_mode.is_capture(); }

/*!
\brief Returns true if the current capture is offline
*/
inline bool is_offline() const { return is_capture() || m_mode == SINSP_MODE_TEST; }
inline bool is_offline() const { return m_mode.is_offline(); }

/*!
\brief Returns true if the current capture is live
*/
inline bool is_live() const { return m_mode == SINSP_MODE_LIVE; }
inline bool is_live() const { return m_mode.is_live(); }

/*!
\brief Returns true if the kernel module is not loaded
*/
inline bool is_nodriver() const { return m_mode == SINSP_MODE_NODRIVER; }
inline bool is_nodriver() const { return m_mode.is_nodriver(); }

/*!
\brief Returns true if the current capture has a plugin producing events.
*/
inline bool is_plugin() const {
return m_mode == SINSP_MODE_PLUGIN && m_input_plugin != nullptr;
}
inline bool is_plugin() const { return m_mode.is_plugin() && m_input_plugin != nullptr; }

/*!
\brief Returns true if the current capture has a plugin producing syscall events.
*/
inline bool is_syscall_plugin() const { return is_plugin() && m_input_plugin->id() == 0; }
inline bool is_syscall_plugin() const {
return m_mode.is_plugin() && m_input_plugin->id() == 0;
}

/*!
\brief Returns the framework plugin api version as a string with static storage
Expand Down Expand Up @@ -966,7 +934,7 @@ class SINSP_PUBLIC sinsp : public capture_stats_source {
char m_platform_lasterr[SCAP_LASTERR_SIZE];
uint64_t m_nevts;
int64_t m_filesize;
sinsp_mode_t m_mode = SINSP_MODE_NONE;
sinsp_mode m_mode = SINSP_MODE_NONE;

// If non-zero, reading from this fd and m_input_filename contains "fd
// <m_input_fd>". Otherwise, reading from m_input_filename.
Expand Down
79 changes: 79 additions & 0 deletions userspace/libsinsp/sinsp_mode.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#pragma once

/*!
* \brief Sinsp possible modes.
*/
enum sinsp_mode_t : uint8_t {
/*!
* Default value that mostly exists so that sinsp can have a valid value
* before it is initialized.
*/
SINSP_MODE_NONE = 0,
/*!
* Read system call data from a capture file.
*/
SINSP_MODE_CAPTURE,
/*!
* Read system call data from the underlying operating system.
*/
SINSP_MODE_LIVE,
/*!
* Do not read system call data. If next is called, a dummy event is
* returned.
*/
SINSP_MODE_NODRIVER,
/*!
* Do not read system call data. Events come from the configured input plugin.
*/
SINSP_MODE_PLUGIN,
/*!
* Read system call and event data from the test event generator.
* Do not attempt to query the underlying system.
*/
SINSP_MODE_TEST,
};

/*!
* \brief Wrapper around sinsp_mode_t providing convenience methods.
*/

class sinsp_mode {
public:
constexpr sinsp_mode(const sinsp_mode_t val): m_mode{val} {}

/*!
\brief Returns true if the current capture is happening from a scap file.
*/
bool is_capture() const { return m_mode == SINSP_MODE_CAPTURE; }

/*!
\brief Returns true if the current capture is live.
*/
bool is_live() const { return m_mode == SINSP_MODE_LIVE; }

/*!
\brief Returns true if the kernel module is not loaded.
*/
bool is_nodriver() const { return m_mode == SINSP_MODE_NODRIVER; }

/*!
\brief Returns true if the current capture is plugin.
*/
bool is_plugin() const { return m_mode == SINSP_MODE_PLUGIN; }

/*!
\brief Returns true if the kernel module is not loaded.
*/
bool is_test() const { return m_mode == SINSP_MODE_TEST; }

/*!
\brief Returns true if the current capture is offline.
*/
bool is_offline() const { return is_capture() || is_test(); }

constexpr bool operator==(const sinsp_mode other) const { return m_mode == other.m_mode; }
constexpr bool operator!=(const sinsp_mode other) const { return m_mode != other.m_mode; }

private:
sinsp_mode_t m_mode;
};

0 comments on commit 5a681cb

Please sign in to comment.