Skip to content

Commit

Permalink
core/conf: Add support for configurable maximum log & datasource mess…
Browse files Browse the repository at this point in the history
…age lengths
  • Loading branch information
bostjan committed Jul 26, 2023
1 parent a849433 commit f3e2f7e
Show file tree
Hide file tree
Showing 123 changed files with 1,034 additions and 252 deletions.
1 change: 1 addition & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
New features:
=============
o Add OpenSUSE 15.5 support.
o Configurable max log message length.

Deprecations:
=============
Expand Down
34 changes: 34 additions & 0 deletions etc/snoopy.ini.in
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,37 @@
;
; Example:
;syslog_level = LOG_INFO



;;; Maximum individual data source's message length
;
; The length limit of a message emitted by an individual data source.
; WARNING: Memmory of this size is allocated on every Snoopy invocation.
; Increasing this setting beyond reasonable limits may impact your system's stability.
;
; Possible values:
; Between 255 and 1048575
;
; Default value:
; 2047
;
; Example:
;datasource_message_max_length = 2047



;;; Maximum formatted log message length
;
; The length limit of a final formatted log message.
; WARNING: Memmory of this size is allocated on every Snoopy invocation.
; Increasing this setting beyond reasonable limits may impact your system's stability.
;
; Possible values:
; Between 255 and 1048575
;
; Default value:
; 16383
;
; Example:
;log_message_max_length = 16383
4 changes: 2 additions & 2 deletions src/action/log-syscall-exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ void snoopy_action_log_syscall_exec ()
#endif

/* Initialize empty log message */
logMessage = malloc(SNOOPY_LOG_MESSAGE_BUF_SIZE);
logMessage = malloc(CFG->log_message_max_length+1);
logMessage[0] = '\0';

/* Generate log message in specified format */
snoopy_message_generateFromFormat(logMessage, SNOOPY_LOG_MESSAGE_BUF_SIZE, CFG->message_format);
snoopy_message_generateFromFormat(logMessage, CFG->log_message_max_length+1, CFG->datasource_message_max_length+1, CFG->message_format);

/* Dispatch the message to configured output */
snoopy_action_log_message_dispatch(logMessage);
Expand Down
6 changes: 3 additions & 3 deletions src/cli/action-version.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@

int snoopy_cli_action_version ()
{
int (* snoopy_ds_version_ptr) (char * const result, char const * const arg);
char ds_message_buf[SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE] = "";
int (* snoopy_ds_version_ptr) (char * const resultBuf, size_t resultBufSize, char const * const arg);
char ds_message_buf[255] = "";
int ds_status;

// Load the library
Expand All @@ -48,7 +48,7 @@ int snoopy_cli_action_version ()
*(void **) (&snoopy_ds_version_ptr) = libsnoopySo_dlsym("snoopy_datasource_snoopy_version");

// Call the function
ds_status = snoopy_ds_version_ptr(ds_message_buf, "");
ds_status = snoopy_ds_version_ptr(ds_message_buf, 255, "");
if (SNOOPY_DATASOURCE_FAILED(ds_status)) {
fatalError("Snoopy version datasource failed");
}
Expand Down
6 changes: 6 additions & 0 deletions src/cli/cli-subroutines.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ void fatalErrorValue (const char * const message, const char * const value)
printErrorValue(message, value);
exit(127);
}
void fatalErrorValueFree (const char * const message, char * const value)
{
printErrorValue(message, value);
free(value);
exit(127);
}


char * libsnoopySo_getFilePath ()
Expand Down
1 change: 1 addition & 0 deletions src/cli/cli-subroutines.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ void printError (const char * const message);
void printErrorValue (const char * const message, const char * const value);
void fatalError (const char * const message);
void fatalErrorValue (const char * const message, const char * const value);
void fatalErrorValueFree(const char * const message, char * const value);

char * libsnoopySo_getFilePath ();
char * libsnoopySo_getFilePathNoCheck ();
Expand Down
97 changes: 89 additions & 8 deletions src/configfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "snoopy.h"
#include "configuration.h"
#include "outputregistry.h"
#include "util/parser-snoopy.h"
#include "util/string-snoopy.h"
#include "util/syslog-snoopy.h"

Expand Down Expand Up @@ -63,16 +64,18 @@
* Define supported config options
*/
snoopy_configfile_option_t snoopy_configfile_optionRegistry[] = {
{ "error_logging", { SNOOPY_CONFIGFILE_OPTION_TYPE_BOOL, &snoopy_configfile_parseValue_error_logging, &snoopy_configfile_getOptionValueAsString_error_logging } },
{ "error_logging", { SNOOPY_CONFIGFILE_OPTION_TYPE_BOOL, &snoopy_configfile_parseValue_error_logging, &snoopy_configfile_getOptionValueAsString_error_logging } },
#ifdef SNOOPY_FILTERING_ENABLED
{ "filter_chain", { SNOOPY_CONFIGFILE_OPTION_TYPE_STRING, &snoopy_configfile_parseValue_filter_chain, &snoopy_configfile_getOptionValueAsString_filter_chain } },
{ "filter_chain", { SNOOPY_CONFIGFILE_OPTION_TYPE_STRING, &snoopy_configfile_parseValue_filter_chain, &snoopy_configfile_getOptionValueAsString_filter_chain } },
#endif
{ "message_format", { SNOOPY_CONFIGFILE_OPTION_TYPE_STRING, &snoopy_configfile_parseValue_message_format, &snoopy_configfile_getOptionValueAsString_message_format } },
{ "output", { SNOOPY_CONFIGFILE_OPTION_TYPE_STRING, &snoopy_configfile_parseValue_output, &snoopy_configfile_getOptionValueAsString_output } },
{ "syslog_facility", { SNOOPY_CONFIGFILE_OPTION_TYPE_STRING, &snoopy_configfile_parseValue_syslog_facility, &snoopy_configfile_getOptionValueAsString_syslog_facility } },
{ "syslog_ident", { SNOOPY_CONFIGFILE_OPTION_TYPE_STRING, &snoopy_configfile_parseValue_syslog_ident, &snoopy_configfile_getOptionValueAsString_syslog_ident } },
{ "syslog_level", { SNOOPY_CONFIGFILE_OPTION_TYPE_STRING, &snoopy_configfile_parseValue_syslog_level, &snoopy_configfile_getOptionValueAsString_syslog_level } },
{ "", { SNOOPY_CONFIGFILE_OPTION_TYPE_NONE, NULL, NULL } },
{ "message_format", { SNOOPY_CONFIGFILE_OPTION_TYPE_STRING, &snoopy_configfile_parseValue_message_format, &snoopy_configfile_getOptionValueAsString_message_format } },
{ "output", { SNOOPY_CONFIGFILE_OPTION_TYPE_STRING, &snoopy_configfile_parseValue_output, &snoopy_configfile_getOptionValueAsString_output } },
{ "syslog_facility", { SNOOPY_CONFIGFILE_OPTION_TYPE_STRING, &snoopy_configfile_parseValue_syslog_facility, &snoopy_configfile_getOptionValueAsString_syslog_facility } },
{ "syslog_ident", { SNOOPY_CONFIGFILE_OPTION_TYPE_STRING, &snoopy_configfile_parseValue_syslog_ident, &snoopy_configfile_getOptionValueAsString_syslog_ident } },
{ "syslog_level", { SNOOPY_CONFIGFILE_OPTION_TYPE_STRING, &snoopy_configfile_parseValue_syslog_level, &snoopy_configfile_getOptionValueAsString_syslog_level } },
{ "datasource_message_max_length", { SNOOPY_CONFIGFILE_OPTION_TYPE_INT, &snoopy_configfile_parseValue_datasource_message_max_length, &snoopy_configfile_getOptionValueAsString_datasource_message_max_length } },
{ "log_message_max_length", { SNOOPY_CONFIGFILE_OPTION_TYPE_INT, &snoopy_configfile_parseValue_log_message_max_length, &snoopy_configfile_getOptionValueAsString_log_message_max_length } },
{ "", { SNOOPY_CONFIGFILE_OPTION_TYPE_NONE, NULL, NULL } },
};


Expand Down Expand Up @@ -591,6 +594,84 @@ int snoopy_configfile_getboolean (const char *c, int notfound)



/*
* Parse 'datasource_message_max_length' config option
*
* Params:
* confValString: Value from configuration file
* CFG: Snoopy configuration struct
*
* Return:
* int SNOOPY_CONFIGFILE_PARSEVALUE_SUCCESS or
* SNOOPY_CONFIGFILE_PARSEVALUE_ERROR
*/
int snoopy_configfile_parseValue_datasource_message_max_length (
const char *confValString,
snoopy_configuration_t* CFG
) {
CFG->datasource_message_max_length = snoopy_util_parser_strByteLength(
confValString,
SNOOPY_DATASOURCE_MESSAGE_MAX_LENGTH_HARDMIN,
SNOOPY_DATASOURCE_MESSAGE_MAX_LENGTH_HARDMAX,
SNOOPY_DATASOURCE_MESSAGE_MAX_LENGTH_DEFAULT
);

return SNOOPY_CONFIGFILE_PARSEVALUE_SUCCESS;
}



/*
* Parse 'log_message_max_length' config option
*
* Params:
* confValString: Value from configuration file
* CFG: Snoopy configuration struct
*
* Return:
* int SNOOPY_CONFIGFILE_PARSEVALUE_SUCCESS or
* SNOOPY_CONFIGFILE_PARSEVALUE_ERROR
*/
int snoopy_configfile_parseValue_log_message_max_length (
const char *confValString,
snoopy_configuration_t* CFG
) {
CFG->log_message_max_length = snoopy_util_parser_strByteLength(
confValString,
SNOOPY_LOG_MESSAGE_MAX_LENGTH_HARDMIN,
SNOOPY_LOG_MESSAGE_MAX_LENGTH_HARDMAX,
SNOOPY_LOG_MESSAGE_MAX_LENGTH_DEFAULT
);

return SNOOPY_CONFIGFILE_PARSEVALUE_SUCCESS;
}



char * snoopy_configfile_getOptionValueAsString_datasource_message_max_length ()
{
const snoopy_configuration_t * CFG = snoopy_configuration_get();

size_t strBufSize = sizeof(CFG->datasource_message_max_length)*8 + 1;
char * strBuf = malloc(strBufSize);
snprintf(strBuf, strBufSize, "%d", CFG->datasource_message_max_length);
return strBuf;
}



char * snoopy_configfile_getOptionValueAsString_log_message_max_length ()
{
const snoopy_configuration_t * CFG = snoopy_configuration_get();

size_t strBufSize = sizeof(CFG->log_message_max_length)*8 + 1;
char * strBuf = malloc(strBufSize);
snprintf(strBuf, strBufSize, "%d", CFG->log_message_max_length);
return strBuf;
}



/*
* optionRegistry :: getIdFromName()
*
Expand Down
5 changes: 5 additions & 0 deletions src/configfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

#define SNOOPY_CONFIGFILE_OPTION_TYPE_BOOL 1
#define SNOOPY_CONFIGFILE_OPTION_TYPE_STRING 2
#define SNOOPY_CONFIGFILE_OPTION_TYPE_INT 3
#define SNOOPY_CONFIGFILE_OPTION_TYPE_NONE 0 // Reserved for internal use


Expand Down Expand Up @@ -65,6 +66,8 @@ int snoopy_configfile_parseValue_output (const char *confValSt
int snoopy_configfile_parseValue_syslog_facility (const char *confValString, snoopy_configuration_t* CFG);
int snoopy_configfile_parseValue_syslog_ident (const char *confValString, snoopy_configuration_t* CFG);
int snoopy_configfile_parseValue_syslog_level (const char *confValString, snoopy_configuration_t* CFG);
int snoopy_configfile_parseValue_datasource_message_max_length (const char *confValString, snoopy_configuration_t* CFG);
int snoopy_configfile_parseValue_log_message_max_length (const char *confValString, snoopy_configuration_t* CFG);

char * snoopy_configfile_getOptionValueAsString_error_logging ();
#ifdef SNOOPY_FILTERING_ENABLED
Expand All @@ -75,6 +78,8 @@ char * snoopy_configfile_getOptionValueAsString_output ();
char * snoopy_configfile_getOptionValueAsString_syslog_facility ();
char * snoopy_configfile_getOptionValueAsString_syslog_ident ();
char * snoopy_configfile_getOptionValueAsString_syslog_level ();
char * snoopy_configfile_getOptionValueAsString_datasource_message_max_length ();
char * snoopy_configfile_getOptionValueAsString_log_message_max_length ();



Expand Down
3 changes: 3 additions & 0 deletions src/configuration.c
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,9 @@ void snoopy_configuration_setDefaults
CFG->syslog_ident_format = SNOOPY_SYSLOG_IDENT_FORMAT;
CFG->syslog_ident_format_malloced = SNOOPY_FALSE;
CFG->syslog_level = SNOOPY_SYSLOG_LEVEL;

CFG->datasource_message_max_length = SNOOPY_DATASOURCE_MESSAGE_MAX_LENGTH_DEFAULT;
CFG->log_message_max_length = SNOOPY_LOG_MESSAGE_MAX_LENGTH_DEFAULT;
}


Expand Down
3 changes: 3 additions & 0 deletions src/configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ typedef struct {
int syslog_level;
int syslog_ident_format_malloced;
char *syslog_ident_format;

unsigned int datasource_message_max_length;
unsigned int log_message_max_length;
} snoopy_configuration_t;


Expand Down
1 change: 1 addition & 0 deletions src/datasource/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ noinst_LTLIBRARIES = libsnoopy_datasources_all.la
# Please maintain alphabetical order, equal to what `ls` would do.
#
libsnoopy_datasources_all_la_SOURCES = \
datasource-common.h \
failure.c \
failure.h \
noop.c \
Expand Down
10 changes: 5 additions & 5 deletions src/datasource/cgroup.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ static int doesCgroupEntryContainController (char const * const cgroupEntry, cha



int snoopy_datasource_cgroup (char * const result, char const * const arg)
int snoopy_datasource_cgroup (char * const resultBuf, size_t resultBufSize, char const * const arg)
{
int myPid;
char procPidCgroupFilePath[PROC_PID_CGROUP_PATH_SIZE_MAX];
Expand All @@ -60,7 +60,7 @@ int snoopy_datasource_cgroup (char * const result, char const * const arg)

// Verify the argument
if (0 == strcmp(arg, "")) {
snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "Missing cgroup selection argument");
snprintf(resultBuf, resultBufSize, "Missing cgroup selection argument");
return SNOOPY_DATASOURCE_FAILURE;
}

Expand All @@ -72,7 +72,7 @@ int snoopy_datasource_cgroup (char * const result, char const * const arg)

// Get the cgroup info content
if (snoopy_util_file_getSmallTextFileContent(procPidCgroupFilePath, &procPidCgroupContent) < 0) {
snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "Unable to read file %s, reason: %s", procPidCgroupFilePath, procPidCgroupContent);
snprintf(resultBuf, resultBufSize, "Unable to read file %s, reason: %s", procPidCgroupFilePath, procPidCgroupContent);
free(procPidCgroupContent);
return SNOOPY_DATASOURCE_FAILURE;
}
Expand Down Expand Up @@ -119,12 +119,12 @@ int snoopy_datasource_cgroup (char * const result, char const * const arg)
// Not found?
if (NULL == cgroupEntry) {
free(procPidCgroupContent);
return snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "%s", "(none)");
return snprintf(resultBuf, resultBufSize, "%s", "(none)");
}


// Found
retMsgLen = snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "%s", cgroupEntry);
retMsgLen = snprintf(resultBuf, resultBufSize, "%s", cgroupEntry);
free(procPidCgroupContent);
return retMsgLen;
}
Expand Down
6 changes: 5 additions & 1 deletion src/datasource/cgroup.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@



#include "datasource-common.h"



/*
* SNOOPY DATA SOURCE: cgroup
*/
int snoopy_datasource_cgroup (char * const result, char const * const arg);
int snoopy_datasource_cgroup (char * const resultBuf, size_t resultBufSize, char const * const arg);
34 changes: 17 additions & 17 deletions src/datasource/cmdline.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@
* Return:
* number of characters in the returned string, or SNOOPY_DATASOURCE_FAILURE
*/
int snoopy_datasource_cmdline (char * const result, __attribute__((unused)) char const * const arg)
int snoopy_datasource_cmdline (char * const resultBuf, size_t resultBufSize, __attribute__((unused)) char const * const arg)
{
const snoopy_inputdatastorage_t * snoopy_inputdatastorage;
size_t bytesWrittenToResult;
size_t bytesWrittenToResultBuf;

/* Get argument data of execv/e() call */
snoopy_inputdatastorage = snoopy_inputdatastorage_get();
Expand All @@ -69,41 +69,41 @@ int snoopy_datasource_cmdline (char * const result, __attribute__((unused)) char
(snoopy_inputdatastorage->argv[0] == NULL)
) {
if (NULL == snoopy_inputdatastorage->filename) {
return snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "(unknown)");
return snprintf(resultBuf, resultBufSize, "(unknown)");
} else {
return snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "%s", snoopy_inputdatastorage->filename);
return snprintf(resultBuf, resultBufSize, "%s", snoopy_inputdatastorage->filename);
}
}

// Recompose the command into a result string
bytesWrittenToResult = 0;
bytesWrittenToResultBuf = 0;
for (int argId=0 ; snoopy_inputdatastorage->argv[argId] != NULL ; argId++) {

// Add space before every non-first argument
if ((argId > 0) && (bytesWrittenToResult < SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE)) {
bytesWrittenToResult += snprintf(
result+bytesWrittenToResult,
SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE - bytesWrittenToResult,
if ((argId > 0) && (bytesWrittenToResultBuf < resultBufSize)) {
bytesWrittenToResultBuf += snprintf(
resultBuf + bytesWrittenToResultBuf,
resultBufSize - bytesWrittenToResultBuf,
" "
);
}

// Copy the content
if (bytesWrittenToResult < SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE) {
bytesWrittenToResult += snprintf(
result+bytesWrittenToResult,
SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE - bytesWrittenToResult,
if (bytesWrittenToResultBuf < resultBufSize) {
bytesWrittenToResultBuf += snprintf(
resultBuf + bytesWrittenToResultBuf,
resultBufSize - bytesWrittenToResultBuf,
"%s", snoopy_inputdatastorage->argv[argId]
);
}
}

// Ensure the presence of a terminating null character
if (bytesWrittenToResult < SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE) {
result[bytesWrittenToResult] = '\0';
if (bytesWrittenToResultBuf < resultBufSize) {
resultBuf[bytesWrittenToResultBuf] = '\0';
} else {
result[SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE-1] = '\0';
resultBuf[resultBufSize-1] = '\0';
}

return (int) bytesWrittenToResult;
return (int) bytesWrittenToResultBuf;
}
Loading

0 comments on commit f3e2f7e

Please sign in to comment.