diff --git a/.github/workflows/build-qa-arch-matrix.yml b/.github/workflows/build-qa-arch-matrix.yml index e2661bfc..927540a1 100644 --- a/.github/workflows/build-qa-arch-matrix.yml +++ b/.github/workflows/build-qa-arch-matrix.yml @@ -95,7 +95,10 @@ jobs: fetch-depth: 0 # Work around the fix for CVE-2022-24765 - - run: git config --global --add safe.directory $GITHUB_WORKSPACE || true + # (In this particular instance, git is not installed yet at this point.) + # (The repo "clone" here is that actions/checkout@v1 magic that kinda looks like a git clone.) + # (This command has been moved further down to where git is actually available [more or less accidentally, tbh].) + #- run: git config --global --add safe.directory $GITHUB_WORKSPACE || true @@ -108,13 +111,15 @@ jobs: ### Build # - run: ./dev-tools/install-dev-software.sh + + # Work around the fix for CVE-2022-24765 + # (Should be done higher up, but see higher up for why it is down here.) + - run: git config --global --add safe.directory $GITHUB_WORKSPACE || true + - run: ./bootstrap.sh - run: ./configure --enable-option-checking=fatal --enable-everything - run: make -j4 - run: make -j4 check - - uses: actions/upload-artifact@v2 + - run: cat tests/*/test-suite.log if: failure() - with: - name: tests-directory - path: tests/ diff --git a/.github/workflows/build-qa-config-matrix-large.yml b/.github/workflows/build-qa-config-matrix-large.yml index f72b2548..bf8e26a7 100644 --- a/.github/workflows/build-qa-config-matrix-large.yml +++ b/.github/workflows/build-qa-config-matrix-large.yml @@ -100,8 +100,5 @@ jobs: - run: make -j4 - run: make -j4 check - - uses: actions/upload-artifact@v2 + - run: cat tests/*/test-suite.log if: failure() - with: - name: tests-directory - path: tests/ diff --git a/.github/workflows/build-qa-config-matrix-small.yml b/.github/workflows/build-qa-config-matrix-small.yml index 372827f2..4fd46008 100644 --- a/.github/workflows/build-qa-config-matrix-small.yml +++ b/.github/workflows/build-qa-config-matrix-small.yml @@ -85,8 +85,5 @@ jobs: - run: make -j4 - run: make -j4 check - - uses: actions/upload-artifact@v2 + - run: cat tests/*/test-suite.log if: failure() - with: - name: tests-directory - path: tests/ diff --git a/.github/workflows/build-qa-os-matrix-large.yml b/.github/workflows/build-qa-os-matrix-large.yml index 1756edf7..5081514c 100644 --- a/.github/workflows/build-qa-os-matrix-large.yml +++ b/.github/workflows/build-qa-os-matrix-large.yml @@ -95,8 +95,5 @@ jobs: - run: make -j4 - run: make -j4 check - - uses: actions/upload-artifact@v2 + - run: cat tests/*/test-suite.log if: failure() - with: - name: tests-directory - path: tests/ diff --git a/.github/workflows/build-qa-os-matrix-small.yml b/.github/workflows/build-qa-os-matrix-small.yml index 85d3baa0..c07d2bdd 100644 --- a/.github/workflows/build-qa-os-matrix-small.yml +++ b/.github/workflows/build-qa-os-matrix-small.yml @@ -84,8 +84,5 @@ jobs: - run: make -j4 - run: make -j4 check - - uses: actions/upload-artifact@v2 + - run: cat tests/*/test-suite.log if: failure() - with: - name: tests-directory - path: tests/ diff --git a/.github/workflows/build-qa-out-of-tree-build.yml b/.github/workflows/build-qa-out-of-tree-build.yml index a73c9ca0..0688290f 100644 --- a/.github/workflows/build-qa-out-of-tree-build.yml +++ b/.github/workflows/build-qa-out-of-tree-build.yml @@ -83,8 +83,5 @@ jobs: - run: cd tmp/build1 && make -j4 - run: cd tmp/build1 && make -j4 check - - uses: actions/upload-artifact@v2 + - run: cat tmp/build1/tests/*/test-suite.log if: failure() - with: - name: tests-directory - path: tmp/build1/tests/ diff --git a/.github/workflows/install-qa-os-matrix.yml b/.github/workflows/install-qa-os-matrix.yml index fbae3ca5..fd64be39 100644 --- a/.github/workflows/install-qa-os-matrix.yml +++ b/.github/workflows/install-qa-os-matrix.yml @@ -100,11 +100,8 @@ jobs: ### Report debugging info on failure # - - uses: actions/upload-artifact@v2 + - run: cat install-snoopy.log if: failure() - with: - name: install-snoopy.log - path: install-snoopy.log diff --git a/ChangeLog b/ChangeLog index f5c6c600..2ca4a9ca 100644 --- a/ChangeLog +++ b/ChangeLog @@ -9,12 +9,20 @@ New features: ============= o Add OpenSUSE 15.5 support. +o Configurable max log message length. + Ref: https://github.com/a2o/snoopy/issue/254 + Ref: https://github.com/a2o/snoopy/pull/264 Deprecations: ============= o Remove Ubuntu 18.04 support. o Remove OpenSUSE 15.3 support. +Bugfixes: +========= +o Fix incorrectly placed free() call in `systemd_unit_name` datasource + Ref: https://github.com/a2o/snoopy/pull/264 + 2022-09-28 - Version 2.5.1 diff --git a/etc/snoopy.ini.in b/etc/snoopy.ini.in index e49277f6..3fcfdf9c 100644 --- a/etc/snoopy.ini.in +++ b/etc/snoopy.ini.in @@ -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 diff --git a/src/action/log-syscall-exec.c b/src/action/log-syscall-exec.c index cb071965..ea9c668a 100644 --- a/src/action/log-syscall-exec.c +++ b/src/action/log-syscall-exec.c @@ -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); diff --git a/src/cli/action-version.c b/src/cli/action-version.c index 694928d6..29b94069 100644 --- a/src/cli/action-version.c +++ b/src/cli/action-version.c @@ -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 @@ -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"); } diff --git a/src/cli/cli-subroutines.c b/src/cli/cli-subroutines.c index e0828689..1dedba96 100644 --- a/src/cli/cli-subroutines.c +++ b/src/cli/cli-subroutines.c @@ -114,6 +114,12 @@ void fatalErrorValue (const char * const message, const char * const value) printErrorValue(message, value); exit(127); } +__attribute__((noreturn)) void fatalErrorValueFree (const char * const message, char * const value) +{ + printErrorValue(message, value); + free(value); + exit(127); +} char * libsnoopySo_getFilePath () diff --git a/src/cli/cli-subroutines.h b/src/cli/cli-subroutines.h index 66489597..16b8d289 100644 --- a/src/cli/cli-subroutines.h +++ b/src/cli/cli-subroutines.h @@ -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 (); diff --git a/src/configfile.c b/src/configfile.c index 48a6bc94..678cad51 100644 --- a/src/configfile.c +++ b/src/configfile.c @@ -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" @@ -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 } }, }; @@ -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, "%zu", 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, "%zu", CFG->log_message_max_length); + return strBuf; +} + + + /* * optionRegistry :: getIdFromName() * diff --git a/src/configfile.h b/src/configfile.h index 3a2139b2..937f3819 100644 --- a/src/configfile.h +++ b/src/configfile.h @@ -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 @@ -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 @@ -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 (); diff --git a/src/configuration.c b/src/configuration.c index 5b402363..ce4d7700 100644 --- a/src/configuration.c +++ b/src/configuration.c @@ -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; } diff --git a/src/configuration.h b/src/configuration.h index b139f63d..aaffd9ed 100644 --- a/src/configuration.h +++ b/src/configuration.h @@ -22,6 +22,10 @@ +#include + + + /* * START: Prevent cyclic inclusions */ @@ -59,6 +63,9 @@ typedef struct { int syslog_level; int syslog_ident_format_malloced; char *syslog_ident_format; + + size_t datasource_message_max_length; + size_t log_message_max_length; } snoopy_configuration_t; diff --git a/src/datasource/Makefile.am b/src/datasource/Makefile.am index 8cc61120..838cb9f3 100644 --- a/src/datasource/Makefile.am +++ b/src/datasource/Makefile.am @@ -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 \ diff --git a/src/datasource/cgroup.c b/src/datasource/cgroup.c index 728688af..1e679fa6 100644 --- a/src/datasource/cgroup.c +++ b/src/datasource/cgroup.c @@ -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]; @@ -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; } @@ -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; } @@ -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; } diff --git a/src/datasource/cgroup.h b/src/datasource/cgroup.h index 68b26bb3..e92a07e0 100644 --- a/src/datasource/cgroup.h +++ b/src/datasource/cgroup.h @@ -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); diff --git a/src/datasource/cmdline.c b/src/datasource/cmdline.c index 7536e707..c56c70c5 100644 --- a/src/datasource/cmdline.c +++ b/src/datasource/cmdline.c @@ -49,10 +49,16 @@ * 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; + + // Return on this impossible condition + // We're only adding this check here because SonarCloud is detecting a "bug" with resultBufSize=0. + if (resultBufSize <= 0) { + return 0; + } /* Get argument data of execv/e() call */ snoopy_inputdatastorage = snoopy_inputdatastorage_get(); @@ -69,41 +75,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; } diff --git a/src/datasource/cmdline.h b/src/datasource/cmdline.h index 18a0c41b..8847a3a3 100644 --- a/src/datasource/cmdline.h +++ b/src/datasource/cmdline.h @@ -22,7 +22,11 @@ +#include "datasource-common.h" + + + /* * SNOOPY DATA SOURCE: cmdline */ -int snoopy_datasource_cmdline (char * const result, char const * const arg); +int snoopy_datasource_cmdline (char * const resultBuf, size_t resultBufSize, char const * const arg); diff --git a/src/datasource/cwd.c b/src/datasource/cwd.c index c63f85c5..2144407a 100644 --- a/src/datasource/cwd.c +++ b/src/datasource/cwd.c @@ -48,12 +48,12 @@ * Return: * number of characters in the returned string, or SNOOPY_DATASOURCE_FAILURE */ -int snoopy_datasource_cwd (char * const result, __attribute__((unused)) char const * const arg) +int snoopy_datasource_cwd (char * const resultBuf, size_t resultBufSize, __attribute__((unused)) char const * const arg) { char cwdBuf[PATH_MAX+1]; if (getcwd(cwdBuf, PATH_MAX+1)) { - return snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "%s", cwdBuf); + return snprintf(resultBuf, resultBufSize, "%s", cwdBuf); } return SNOOPY_DATASOURCE_FAILURE; } diff --git a/src/datasource/cwd.h b/src/datasource/cwd.h index 2f07844e..e87125cb 100644 --- a/src/datasource/cwd.h +++ b/src/datasource/cwd.h @@ -22,7 +22,11 @@ +#include "datasource-common.h" + + + /* * SNOOPY DATA SOURCE: cwd */ -int snoopy_datasource_cwd (char * const result, char const * const arg); +int snoopy_datasource_cwd (char * const resultBuf, size_t resultBufSize, char const * const arg); diff --git a/src/datasource/datasource-common.h b/src/datasource/datasource-common.h new file mode 100644 index 00000000..adeb0653 --- /dev/null +++ b/src/datasource/datasource-common.h @@ -0,0 +1,26 @@ +/* + * SNOOPY COMMAND LOGGER + * + * File: snoopy/datasource/datasource-common.h + * + * Copyright (c) 2023 Bostjan Skufca Jese + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + + + +/* Required for size_t type */ +#include diff --git a/src/datasource/datetime.c b/src/datasource/datetime.c index b09e564f..0e9f4891 100644 --- a/src/datasource/datetime.c +++ b/src/datasource/datetime.c @@ -48,7 +48,7 @@ * Return: * number of characters in the returned string, or SNOOPY_DATASOURCE_FAILURE */ -int snoopy_datasource_datetime (char * const result, char const * const arg) +int snoopy_datasource_datetime (char * const resultBuf, size_t resultBufSize, char const * const arg) { time_t curTime; struct tm curLocalTimeBuf; @@ -58,13 +58,13 @@ int snoopy_datasource_datetime (char * const result, char const * const arg) // Get current time if ((time_t) -1 == time(&curTime)) { - return snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "(error @ time(): %d)", errno); + return snprintf(resultBuf, resultBufSize, "(error @ time(): %d)", errno); } // Convert to local time curLocalTime = localtime_r(&curTime, &curLocalTimeBuf); if (NULL == curLocalTime) { - return snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "(error @ localtime_r())"); + return snprintf(resultBuf, resultBufSize, "(error @ localtime_r())"); } // Determine the format to use @@ -76,9 +76,9 @@ int snoopy_datasource_datetime (char * const result, char const * const arg) // Format it if (0 == strftime(timeBuffer, SNOOPY_DATASOURCE_DATETIME_sizeMaxWithNull, formatToUse, curLocalTime)) { - return snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "(error @ strftime())"); + return snprintf(resultBuf, resultBufSize, "(error @ strftime())"); } // Copy it - return snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "%s", timeBuffer); + return snprintf(resultBuf, resultBufSize, "%s", timeBuffer); } diff --git a/src/datasource/datetime.h b/src/datasource/datetime.h index 8e1199a7..6041ddde 100644 --- a/src/datasource/datetime.h +++ b/src/datasource/datetime.h @@ -22,6 +22,10 @@ +#include "datasource-common.h" + + + /* * Local defines */ @@ -33,4 +37,4 @@ /* * SNOOPY DATA SOURCE: datetime */ -int snoopy_datasource_datetime (char * const result, char const * const arg); +int snoopy_datasource_datetime (char * const resultBuf, size_t resultBufSize, char const * const arg); diff --git a/src/datasource/domain.c b/src/datasource/domain.c index 2ac830ce..0022b038 100644 --- a/src/datasource/domain.c +++ b/src/datasource/domain.c @@ -64,7 +64,7 @@ * Return: * number of characters in the returned string, or SNOOPY_DATASOURCE_FAILURE */ -int snoopy_datasource_domain (char * const result, __attribute__((unused)) char const * const arg) +int snoopy_datasource_domain (char * const resultBuf, size_t resultBufSize, __attribute__((unused)) char const * const arg) { FILE *fp; char hostname[HOST_NAME_BUF_SIZE]; @@ -78,7 +78,7 @@ int snoopy_datasource_domain (char * const result, __attribute__((unused)) char /* Get my hostname first */ retVal = gethostname(hostname, HOST_NAME_MAX); if (0 != retVal) { - return snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "(error @ gethostname(): %d)", errno); + return snprintf(resultBuf, resultBufSize, "(error @ gethostname(): %d)", errno); } // If hostname was something alien (longer than HOST_NAME_MAX), then the @@ -92,11 +92,11 @@ int snoopy_datasource_domain (char * const result, __attribute__((unused)) char /* Check hostname length */ hostnameLen = (int) strlen(hostname); if (0 == hostnameLen) { - snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "Got empty hostname"); + snprintf(resultBuf, resultBufSize, "Got empty hostname"); return SNOOPY_DATASOURCE_FAILURE; } if (hostnameLen > HOST_NAME_BUF_SIZE - 2) { - snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "INTERNAL ERROR: Got too long hostname, length: %d", hostnameLen); + snprintf(resultBuf, resultBufSize, "INTERNAL ERROR: Got too long hostname, length: %d", hostnameLen); return SNOOPY_DATASOURCE_FAILURE; } @@ -108,7 +108,7 @@ int snoopy_datasource_domain (char * const result, __attribute__((unused)) char /* Try to open file in read mode */ fp = fopen(HOSTS_PATH, "r"); if (NULL == fp) { - snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "Unable to open file for reading: %s", HOSTS_PATH); + snprintf(resultBuf, resultBufSize, "Unable to open file for reading: %s", HOSTS_PATH); return SNOOPY_OUTPUT_FAILURE; } @@ -145,8 +145,8 @@ int snoopy_datasource_domain (char * const result, __attribute__((unused)) char /* Cleanup and return */ fclose(fp); if (NULL != domainPtr) { - return snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "%s", domainPtr); + return snprintf(resultBuf, resultBufSize, "%s", domainPtr); } else { - return snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "(none)"); + return snprintf(resultBuf, resultBufSize, "(none)"); } } diff --git a/src/datasource/domain.h b/src/datasource/domain.h index d382d7a6..e8d80a8d 100644 --- a/src/datasource/domain.h +++ b/src/datasource/domain.h @@ -22,7 +22,11 @@ +#include "datasource-common.h" + + + /* * SNOOPY DATA SOURCE: domain */ -int snoopy_datasource_domain (char * const result, char const * const arg); +int snoopy_datasource_domain (char * const resultBuf, size_t resultBufSize, char const * const arg); diff --git a/src/datasource/egid.c b/src/datasource/egid.c index 19de9e11..457b30fc 100644 --- a/src/datasource/egid.c +++ b/src/datasource/egid.c @@ -48,7 +48,7 @@ * Return: * number of characters in the returned string, or SNOOPY_DATASOURCE_FAILURE */ -int snoopy_datasource_egid (char * const result, __attribute__((unused)) char const * const arg) +int snoopy_datasource_egid (char * const resultBuf, size_t resultBufSize, __attribute__((unused)) char const * const arg) { - return snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "%u", getegid()); + return snprintf(resultBuf, resultBufSize, "%u", getegid()); } diff --git a/src/datasource/egid.h b/src/datasource/egid.h index 2953d6fe..26ca5b01 100644 --- a/src/datasource/egid.h +++ b/src/datasource/egid.h @@ -22,7 +22,11 @@ +#include "datasource-common.h" + + + /* * SNOOPY DATA SOURCE: egid */ -int snoopy_datasource_egid (char * const result, char const * const arg); +int snoopy_datasource_egid (char * const resultBuf, size_t resultBufSize, char const * const arg); diff --git a/src/datasource/egroup.c b/src/datasource/egroup.c index 72ec177f..2574bf2c 100644 --- a/src/datasource/egroup.c +++ b/src/datasource/egroup.c @@ -50,7 +50,7 @@ * Return: * number of characters in the returned string, or SNOOPY_DATASOURCE_FAILURE */ -int snoopy_datasource_egroup (char * const result, __attribute__((unused)) char const * const arg) +int snoopy_datasource_egroup (char * const resultBuf, size_t resultBufSize, __attribute__((unused)) char const * const arg) { struct group gr; struct group *gr_gid = NULL; @@ -65,17 +65,17 @@ int snoopy_datasource_egroup (char * const result, __attribute__((unused)) char } buffgr_gid = malloc(buffgrsize_gid); if (NULL == buffgr_gid) { - return snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "ERROR(malloc)"); + return snprintf(resultBuf, resultBufSize, "ERROR(malloc)"); } /* Try to get data */ if (0 != getgrgid_r(getegid(), &gr, buffgr_gid, buffgrsize_gid, &gr_gid)) { - messageLength = snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "ERROR(getgrgid_r)"); + messageLength = snprintf(resultBuf, resultBufSize, "ERROR(getgrgid_r)"); } else { if (NULL == gr_gid) { - messageLength = snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "(undefined)"); + messageLength = snprintf(resultBuf, resultBufSize, "(undefined)"); } else { - messageLength = snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "%s", gr_gid->gr_name); + messageLength = snprintf(resultBuf, resultBufSize, "%s", gr_gid->gr_name); } } diff --git a/src/datasource/egroup.h b/src/datasource/egroup.h index 4771ca3a..9f5784b1 100644 --- a/src/datasource/egroup.h +++ b/src/datasource/egroup.h @@ -22,7 +22,11 @@ +#include "datasource-common.h" + + + /* * SNOOPY DATA SOURCE: egroup */ -int snoopy_datasource_egroup (char * const result, char const * const arg); +int snoopy_datasource_egroup (char * const resultBuf, size_t resultBufSize, char const * const arg); diff --git a/src/datasource/env.c b/src/datasource/env.c index c65dc117..4e6447c5 100644 --- a/src/datasource/env.c +++ b/src/datasource/env.c @@ -47,14 +47,14 @@ * Return: * number of characters in the returned string, or SNOOPY_DATASOURCE_FAILURE */ -int snoopy_datasource_env (char * const result, char const * const arg) +int snoopy_datasource_env (char * const resultBuf, size_t resultBufSize, char const * const arg) { char *env = getenv(arg); /* Return empty string if environmental variable does not exist */ if (NULL == env) { - return snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "(undefined)"); + return snprintf(resultBuf, resultBufSize, "(undefined)"); } - return snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "%s", env); + return snprintf(resultBuf, resultBufSize, "%s", env); } diff --git a/src/datasource/env.h b/src/datasource/env.h index f23d640d..250bac39 100644 --- a/src/datasource/env.h +++ b/src/datasource/env.h @@ -22,7 +22,11 @@ +#include "datasource-common.h" + + + /* * SNOOPY DATA SOURCE: env */ -int snoopy_datasource_env (char * const result, char const * const arg); +int snoopy_datasource_env (char * const resultBuf, size_t resultBufSize, char const * const arg); diff --git a/src/datasource/env_all.c b/src/datasource/env_all.c index ca95a14e..6afafe5f 100644 --- a/src/datasource/env_all.c +++ b/src/datasource/env_all.c @@ -55,7 +55,7 @@ extern char **environ; * Return: * number of characters in the returned string, or SNOOPY_DATASOURCE_FAILURE */ -int snoopy_datasource_env_all (char * const result, __attribute__((unused)) char const * const arg) +int snoopy_datasource_env_all (char * const resultBuf, size_t resultBufSize, __attribute__((unused)) char const * const arg) { int resultSize = 0; // Current size of message to be returned back - does not include trailing null character @@ -64,36 +64,35 @@ int snoopy_datasource_env_all (char * const result, __attribute__((unused)) char int i = 0; while (NULL != envItem) { i++; - int remResultSize = 0; - remResultSize = SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE - resultSize; + size_t remResultSize = resultBufSize - resultSize; // Add comma if required - must be still 5 characters available for this (,...\0) if ((i > 1) && (remResultSize >= 5)) { - result[resultSize] = ','; - result[resultSize+1] = '\0'; + resultBuf[resultSize] = ','; + resultBuf[resultSize+1] = '\0'; resultSize++; remResultSize--; } // Do we append whole environmental variable, or just part of it? // +3 to account for ... and +1 for null character - if ((int)(strlen(envItem) + 3 + 1) < remResultSize) { + if ((strlen(envItem) + 3 + 1) < remResultSize) { int strSizeCopied = 0; // Append whole ENV variable - strSizeCopied = snprintf(&result[resultSize], remResultSize, "%s", envItem); + strSizeCopied = snprintf(&resultBuf[resultSize], remResultSize, "%s", envItem); resultSize += strSizeCopied; } else { - int strSizeToCopy = 0; + size_t strSizeToCopy = 0; // This one includes null char, therefore the actual string length that will be copied is X-1 characters + \0 strSizeToCopy = remResultSize - 3; // -4 to account for "...\0" - snprintf(&result[resultSize], strSizeToCopy, "%s", envItem); + snprintf(&resultBuf[resultSize], strSizeToCopy, "%s", envItem); resultSize += strSizeToCopy - 1; // Account for added \0 at the end - we do not use strSizeCopied here, which already includes it strSizeToCopy = 4; // -4 to account for "...\0" - snprintf(&result[resultSize], strSizeToCopy, "..."); + snprintf(&resultBuf[resultSize], strSizeToCopy, "..."); resultSize += strSizeToCopy-1; break; } diff --git a/src/datasource/env_all.h b/src/datasource/env_all.h index 76ca512c..fe7655d0 100644 --- a/src/datasource/env_all.h +++ b/src/datasource/env_all.h @@ -22,7 +22,11 @@ +#include "datasource-common.h" + + + /* * SNOOPY DATA SOURCE: env_all */ -int snoopy_datasource_env_all (char * const result, char const * const arg); +int snoopy_datasource_env_all (char * const resultBuf, size_t resultBufSize, char const * const arg); diff --git a/src/datasource/euid.c b/src/datasource/euid.c index d56b8bc0..92530620 100644 --- a/src/datasource/euid.c +++ b/src/datasource/euid.c @@ -48,7 +48,7 @@ * Return: * number of characters in the returned string, or SNOOPY_DATASOURCE_FAILURE */ -int snoopy_datasource_euid (char * const result, __attribute__((unused)) char const * const arg) +int snoopy_datasource_euid (char * const resultBuf, size_t resultBufSize, __attribute__((unused)) char const * const arg) { - return snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "%u", geteuid()); + return snprintf(resultBuf, resultBufSize, "%u", geteuid()); } diff --git a/src/datasource/euid.h b/src/datasource/euid.h index ee4a25b2..47e17913 100644 --- a/src/datasource/euid.h +++ b/src/datasource/euid.h @@ -22,7 +22,11 @@ +#include "datasource-common.h" + + + /* * SNOOPY DATA SOURCE: euid */ -int snoopy_datasource_euid (char * const result, char const * const arg); +int snoopy_datasource_euid (char * const resultBuf, size_t resultBufSize, char const * const arg); diff --git a/src/datasource/eusername.c b/src/datasource/eusername.c index 009cdd05..958c8d99 100644 --- a/src/datasource/eusername.c +++ b/src/datasource/eusername.c @@ -50,7 +50,7 @@ * Return: * number of characters in the returned string, or SNOOPY_DATASOURCE_FAILURE */ -int snoopy_datasource_eusername (char * const result, __attribute__((unused)) char const * const arg) +int snoopy_datasource_eusername (char * const resultBuf, size_t resultBufSize, __attribute__((unused)) char const * const arg) { struct passwd pwd; struct passwd *pwd_uid = NULL; @@ -65,17 +65,17 @@ int snoopy_datasource_eusername (char * const result, __attribute__((unused)) ch } buffpwd_uid = malloc(buffpwdsize_uid); if (NULL == buffpwd_uid) { - return snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "ERROR(malloc)"); + return snprintf(resultBuf, resultBufSize, "ERROR(malloc)"); } /* Try to get data */ if (0 != getpwuid_r(geteuid(), &pwd, buffpwd_uid, buffpwdsize_uid, &pwd_uid)) { - messageLength = snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "ERROR(getpwuid_r)"); + messageLength = snprintf(resultBuf, resultBufSize, "ERROR(getpwuid_r)"); } else { if (NULL == pwd_uid) { - messageLength = snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "(undefined)"); + messageLength = snprintf(resultBuf, resultBufSize, "(undefined)"); } else { - messageLength = snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "%s", pwd_uid->pw_name); + messageLength = snprintf(resultBuf, resultBufSize, "%s", pwd_uid->pw_name); } } diff --git a/src/datasource/eusername.h b/src/datasource/eusername.h index 6bb646b6..f9432da7 100644 --- a/src/datasource/eusername.h +++ b/src/datasource/eusername.h @@ -22,7 +22,11 @@ +#include "datasource-common.h" + + + /* * SNOOPY DATA SOURCE: eusername */ -int snoopy_datasource_eusername (char * const result, char const * const arg); +int snoopy_datasource_eusername (char * const resultBuf, size_t resultBufSize, char const * const arg); diff --git a/src/datasource/failure.c b/src/datasource/failure.c index 862d6b72..f7b4f1a9 100644 --- a/src/datasource/failure.c +++ b/src/datasource/failure.c @@ -45,8 +45,8 @@ * Return: * SNOOPY_DATASOURCE_FAILURE */ -int snoopy_datasource_failure (char * const result, __attribute__((unused)) char const * const arg) +int snoopy_datasource_failure (char * const resultBuf, size_t resultBufSize, __attribute__((unused)) char const * const arg) { - snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "Artificial datasource failure triggered"); + snprintf(resultBuf, resultBufSize, "Artificial datasource failure triggered"); return SNOOPY_DATASOURCE_FAILURE; } diff --git a/src/datasource/failure.h b/src/datasource/failure.h index 3ec090ee..2e95b3ad 100644 --- a/src/datasource/failure.h +++ b/src/datasource/failure.h @@ -22,7 +22,11 @@ +#include "datasource-common.h" + + + /* * SNOOPY DATA SOURCE: failure */ -int snoopy_datasource_failure (char * const result, char const * const arg); +int snoopy_datasource_failure (char * const resultBuf, size_t resultBufSize, char const * const arg); diff --git a/src/datasource/filename.c b/src/datasource/filename.c index 5b68fe8a..8f0a4a60 100644 --- a/src/datasource/filename.c +++ b/src/datasource/filename.c @@ -47,12 +47,12 @@ * Return: * number of characters in the returned string, or SNOOPY_DATASOURCE_FAILURE */ -int snoopy_datasource_filename (char * const result, __attribute__((unused)) char const * const arg) +int snoopy_datasource_filename (char * const resultBuf, size_t resultBufSize, __attribute__((unused)) char const * const arg) { const snoopy_inputdatastorage_t * snoopy_inputdatastorage; /* Get argument data of execv/e() call */ snoopy_inputdatastorage = snoopy_inputdatastorage_get(); - return snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "%s", snoopy_inputdatastorage->filename); + return snprintf(resultBuf, resultBufSize, "%s", snoopy_inputdatastorage->filename); } diff --git a/src/datasource/filename.h b/src/datasource/filename.h index 55123c18..de047ecc 100644 --- a/src/datasource/filename.h +++ b/src/datasource/filename.h @@ -22,7 +22,11 @@ +#include "datasource-common.h" + + + /* * SNOOPY DATA SOURCE: filename */ -int snoopy_datasource_filename (char * const result, char const * const arg); +int snoopy_datasource_filename (char * const resultBuf, size_t resultBufSize, char const * const arg); diff --git a/src/datasource/gid.c b/src/datasource/gid.c index 8b5d42bf..27a0744a 100644 --- a/src/datasource/gid.c +++ b/src/datasource/gid.c @@ -48,7 +48,7 @@ * Return: * number of characters in the returned string, or SNOOPY_DATASOURCE_FAILURE */ -int snoopy_datasource_gid (char * const result, __attribute__((unused)) char const * const arg) +int snoopy_datasource_gid (char * const resultBuf, size_t resultBufSize, __attribute__((unused)) char const * const arg) { - return snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "%u", getgid()); + return snprintf(resultBuf, resultBufSize, "%u", getgid()); } diff --git a/src/datasource/gid.h b/src/datasource/gid.h index 4dd18478..a40e531c 100644 --- a/src/datasource/gid.h +++ b/src/datasource/gid.h @@ -22,7 +22,11 @@ +#include "datasource-common.h" + + + /* * SNOOPY DATA SOURCE: gid */ -int snoopy_datasource_gid (char * const result, char const * const arg); +int snoopy_datasource_gid (char * const resultBuf, size_t resultBufSize, char const * const arg); diff --git a/src/datasource/group.c b/src/datasource/group.c index 653edbda..11283f69 100644 --- a/src/datasource/group.c +++ b/src/datasource/group.c @@ -50,7 +50,7 @@ * Return: * number of characters in the returned string, or SNOOPY_DATASOURCE_FAILURE */ -int snoopy_datasource_group (char * const result, __attribute__((unused)) char const * const arg) +int snoopy_datasource_group (char * const resultBuf, size_t resultBufSize, __attribute__((unused)) char const * const arg) { struct group gr; struct group *gr_gid = NULL; @@ -65,17 +65,17 @@ int snoopy_datasource_group (char * const result, __attribute__((unused)) char c } buffgr_gid = malloc(buffgrsize_gid); if(NULL == buffgr_gid) { - return snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "ERROR(malloc)"); + return snprintf(resultBuf, resultBufSize, "ERROR(malloc)"); } /* Try to get data */ if (0 != getgrgid_r(getgid(), &gr, buffgr_gid, buffgrsize_gid, &gr_gid)) { - messageLength = snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "ERROR(getgrgid_r)"); + messageLength = snprintf(resultBuf, resultBufSize, "ERROR(getgrgid_r)"); } else { if (NULL == gr_gid) { - messageLength = snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "(undefined)"); + messageLength = snprintf(resultBuf, resultBufSize, "(undefined)"); } else { - messageLength = snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "%s", gr_gid->gr_name); + messageLength = snprintf(resultBuf, resultBufSize, "%s", gr_gid->gr_name); } } diff --git a/src/datasource/group.h b/src/datasource/group.h index ef780c2b..d629dd59 100644 --- a/src/datasource/group.h +++ b/src/datasource/group.h @@ -22,7 +22,11 @@ +#include "datasource-common.h" + + + /* * SNOOPY DATA SOURCE: group */ -int snoopy_datasource_group (char * const result, char const * const arg); +int snoopy_datasource_group (char * const resultBuf, size_t resultBufSize, char const * const arg); diff --git a/src/datasource/hostname.c b/src/datasource/hostname.c index 7e7a96d0..9f6e2483 100644 --- a/src/datasource/hostname.c +++ b/src/datasource/hostname.c @@ -49,19 +49,19 @@ * Return: * number of characters in the returned string, or SNOOPY_DATASOURCE_FAILURE */ -int snoopy_datasource_hostname (char * const result, __attribute__((unused)) char const * const arg) +int snoopy_datasource_hostname (char * const resultBuf, size_t resultBufSize, __attribute__((unused)) char const * const arg) { int charCount; int retVal; - retVal = gethostname(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE); + retVal = gethostname(resultBuf, resultBufSize); if (0 != retVal) { - return snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "(error @ gethostname(): %d)", errno); + return snprintf(resultBuf, resultBufSize, "(error @ gethostname(): %d)", errno); } // If hostname was something alien (longer than 1024 characters), // set last char to null just in case - result[SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE-1] = '\0'; - charCount = (int) strlen(result); + resultBuf[resultBufSize-1] = '\0'; + charCount = (int) strlen(resultBuf); return charCount; } diff --git a/src/datasource/hostname.h b/src/datasource/hostname.h index b60bef60..eb715007 100644 --- a/src/datasource/hostname.h +++ b/src/datasource/hostname.h @@ -22,7 +22,11 @@ +#include "datasource-common.h" + + + /* * SNOOPY DATA SOURCE: hostname */ -int snoopy_datasource_hostname (char * const result, char const * const arg); +int snoopy_datasource_hostname (char * const resultBuf, size_t resultBufSize, char const * const arg); diff --git a/src/datasource/ipaddr.c b/src/datasource/ipaddr.c index 779dc35b..090b682f 100644 --- a/src/datasource/ipaddr.c +++ b/src/datasource/ipaddr.c @@ -55,7 +55,7 @@ * Return: * number of characters in the returned string, or SNOOPY_DATASOURCE_FAILURE */ -int snoopy_datasource_ipaddr (char * const result, __attribute__((unused)) char const * const arg) +int snoopy_datasource_ipaddr (char * const resultBuf, size_t resultBufSize, __attribute__((unused)) char const * const arg) { char ttyPathBuf[SNOOPY_DS_IPADDR_TTY_PATH_BUF_LEN]; struct utmp utmpEntryBuf; @@ -66,23 +66,23 @@ int snoopy_datasource_ipaddr (char * const result, __attribute__((unused)) char ttyPathBuf[0] = '\0'; retVal = ttyname_r(STDIN_FILENO, ttyPathBuf, SNOOPY_DS_IPADDR_TTY_PATH_BUF_LEN); if (0 != retVal) { - return snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "-"); + return snprintf(resultBuf, resultBufSize, "-"); } ttyPathBuf[SNOOPY_DS_IPADDR_TTY_PATH_BUF_LEN-1] = '\0'; // Find the matching utmp entry if (SNOOPY_TRUE != snoopy_util_utmp_findUtmpEntryByPath(ttyPathBuf, utmpEntry)) { - return snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "-"); // This can happen, i.e. in in a `docker run ...` environment + return snprintf(resultBuf, resultBufSize, "-"); // This can happen, i.e. in in a `docker run ...` environment } // Does the associated IP address actually exist? if (SNOOPY_TRUE != snoopy_util_utmp_doesEntryContainIpAddr(utmpEntry)) { - return snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "-"); + return snprintf(resultBuf, resultBufSize, "-"); } // Convert to string IP address - snoopy_util_utmp_getUtmpIpAddrAsString(utmpEntry, result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE); + snoopy_util_utmp_getUtmpIpAddrAsString(utmpEntry, resultBuf, resultBufSize); // Clean up - return (int) strlen(result); + return (int) strlen(resultBuf); } diff --git a/src/datasource/ipaddr.h b/src/datasource/ipaddr.h index 61a028dd..ea6dd197 100644 --- a/src/datasource/ipaddr.h +++ b/src/datasource/ipaddr.h @@ -20,4 +20,8 @@ -int snoopy_datasource_ipaddr (char * const result, char const * const arg); +#include "datasource-common.h" + + + +int snoopy_datasource_ipaddr (char * const resultBuf, size_t resultBufSize, char const * const arg); diff --git a/src/datasource/login.c b/src/datasource/login.c index d3ae864c..e67b1d69 100644 --- a/src/datasource/login.c +++ b/src/datasource/login.c @@ -57,7 +57,7 @@ * Return: * number of characters in the returned string, or SNOOPY_DATASOURCE_FAILURE */ -int snoopy_datasource_login (char * const result, __attribute__((unused)) char const * const arg) +int snoopy_datasource_login (char * const resultBuf, size_t resultBufSize, __attribute__((unused)) char const * const arg) { int loginSizeMaxWithoutNull = SNOOPY_DATASOURCE_LOGIN_loginSizeMaxWithoutNull; int loginSizeMaxWithNull = SNOOPY_DATASOURCE_LOGIN_loginSizeMaxWithNull; @@ -88,5 +88,5 @@ int snoopy_datasource_login (char * const result, __attribute__((unused)) char c } } } - return snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "%s", login); + return snprintf(resultBuf, resultBufSize, "%s", login); } diff --git a/src/datasource/login.h b/src/datasource/login.h index 2e416435..c9667faa 100644 --- a/src/datasource/login.h +++ b/src/datasource/login.h @@ -22,7 +22,11 @@ +#include "datasource-common.h" + + + /* * SNOOPY DATA SOURCE: login */ -int snoopy_datasource_login (char * const result, char const * const arg); +int snoopy_datasource_login (char * const resultBuf, size_t resultBufSize, char const * const arg); diff --git a/src/datasource/noop.c b/src/datasource/noop.c index 171eb356..c91d1fe5 100644 --- a/src/datasource/noop.c +++ b/src/datasource/noop.c @@ -42,7 +42,7 @@ * Return: * number of characters in the returned string, or SNOOPY_DATASOURCE_FAILURE */ -int snoopy_datasource_noop (__attribute__((unused)) char * const result, __attribute__((unused)) char const * const arg) +int snoopy_datasource_noop (__attribute__((unused)) char * const resultBuf, __attribute__((unused)) size_t resultBufSize, __attribute__((unused)) char const * const arg) { return 0; } diff --git a/src/datasource/noop.h b/src/datasource/noop.h index cbb4b1b5..adbb3d8e 100644 --- a/src/datasource/noop.h +++ b/src/datasource/noop.h @@ -22,7 +22,11 @@ +#include "datasource-common.h" + + + /* * SNOOPY DATA SOURCE: noop */ -int snoopy_datasource_noop (char * const result, char const * const arg); +int snoopy_datasource_noop (char * const resultBuf, size_t resultBufSize, char const * const arg); diff --git a/src/datasource/pid.c b/src/datasource/pid.c index b23ebd3f..d76a19dd 100644 --- a/src/datasource/pid.c +++ b/src/datasource/pid.c @@ -48,7 +48,7 @@ * Return: * number of characters in the returned string, or SNOOPY_DATASOURCE_FAILURE */ -int snoopy_datasource_pid (char * const result, __attribute__((unused)) char const * const arg) +int snoopy_datasource_pid (char * const resultBuf, size_t resultBufSize, __attribute__((unused)) char const * const arg) { - return snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "%u", getpid()); + return snprintf(resultBuf, resultBufSize, "%u", getpid()); } diff --git a/src/datasource/pid.h b/src/datasource/pid.h index 776ddbb7..90fe68b9 100644 --- a/src/datasource/pid.h +++ b/src/datasource/pid.h @@ -22,7 +22,11 @@ +#include "datasource-common.h" + + + /* * SNOOPY DATA SOURCE: pid */ -int snoopy_datasource_pid (char * const result, char const * const arg); +int snoopy_datasource_pid (char * const resultBuf, size_t resultBufSize, char const * const arg); diff --git a/src/datasource/ppid.c b/src/datasource/ppid.c index 4acd214b..400159d1 100644 --- a/src/datasource/ppid.c +++ b/src/datasource/ppid.c @@ -48,7 +48,7 @@ * Return: * number of characters in the returned string, or SNOOPY_DATASOURCE_FAILURE */ -int snoopy_datasource_ppid (char * const result, __attribute__((unused)) char const * const arg) +int snoopy_datasource_ppid (char * const resultBuf, size_t resultBufSize, __attribute__((unused)) char const * const arg) { - return snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "%u", getppid()); + return snprintf(resultBuf, resultBufSize, "%u", getppid()); } diff --git a/src/datasource/ppid.h b/src/datasource/ppid.h index e1aba6c9..636ab833 100644 --- a/src/datasource/ppid.h +++ b/src/datasource/ppid.h @@ -22,7 +22,11 @@ +#include "datasource-common.h" + + + /* * SNOOPY DATA SOURCE: ppid */ -int snoopy_datasource_ppid (char * const result, char const * const arg); +int snoopy_datasource_ppid (char * const resultBuf, size_t resultBufSize, char const * const arg); diff --git a/src/datasource/rpname.c b/src/datasource/rpname.c index 8fa9ffa8..c6b18533 100644 --- a/src/datasource/rpname.c +++ b/src/datasource/rpname.c @@ -66,7 +66,7 @@ * Non-public function prototypes */ static int get_parent_pid (int pid); -static int get_rpname (int pid, char *result); +static int get_rpname (int pid, char *resultBuf, size_t resultBufSize); static char* read_proc_property (int pid, const char * prop_name); @@ -84,9 +84,9 @@ static char* read_proc_property (int pid, const char * prop_name); * Return: * number of characters in the returned string, or SNOOPY_DATASOURCE_FAILURE */ -int snoopy_datasource_rpname (char * const result, __attribute__((unused)) char const * const arg) +int snoopy_datasource_rpname (char * const resultBuf, size_t resultBufSize, __attribute__((unused)) char const * const arg) { - return get_rpname(getpid(), result); + return get_rpname(getpid(), resultBuf, resultBufSize); } @@ -195,7 +195,7 @@ static int get_parent_pid (int pid) /* Find root process name */ -static int get_rpname (int pid, char *result) +static int get_rpname (int pid, char *resultBuf, size_t resultBufSize) { int parentPid; char *name; @@ -205,15 +205,15 @@ static int get_rpname (int pid, char *result) if ((PID_ROOT == parentPid) || (PID_ZERO == parentPid)) { name = read_proc_property(pid, PROC_PID_STATUS_KEY_NAME); if (NULL != name) { - nameLen = snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "%s", name); + nameLen = snprintf(resultBuf, resultBufSize, "%s", name); free(name); } else { - nameLen = snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "%s", UNKNOWN_STR); + nameLen = snprintf(resultBuf, resultBufSize, "%s", UNKNOWN_STR); } return (int) nameLen; } else if (PID_UNKNOWN == parentPid) { - return snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "%s", UNKNOWN_STR); + return snprintf(resultBuf, resultBufSize, "%s", UNKNOWN_STR); } else { - return get_rpname(parentPid, result); + return get_rpname(parentPid, resultBuf, resultBufSize); } } diff --git a/src/datasource/rpname.h b/src/datasource/rpname.h index eb547dbc..f4a3078b 100644 --- a/src/datasource/rpname.h +++ b/src/datasource/rpname.h @@ -22,7 +22,11 @@ +#include "datasource-common.h" + + + /* * SNOOPY DATA SOURCE: rpname */ -int snoopy_datasource_rpname (char * const result, char const * const arg); +int snoopy_datasource_rpname (char * const resultBuf, size_t resultBufSize, char const * const arg); diff --git a/src/datasource/sid.c b/src/datasource/sid.c index a692464e..66cf953a 100644 --- a/src/datasource/sid.c +++ b/src/datasource/sid.c @@ -51,7 +51,7 @@ * Return: * number of characters in the returned string, or SNOOPY_DATASOURCE_FAILURE */ -int snoopy_datasource_sid (char * const result, __attribute__((unused)) char const * const arg) +int snoopy_datasource_sid (char * const resultBuf, size_t resultBufSize, __attribute__((unused)) char const * const arg) { - return snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "%u", getsid(0)); + return snprintf(resultBuf, resultBufSize, "%u", getsid(0)); } diff --git a/src/datasource/sid.h b/src/datasource/sid.h index ea9f5610..ec405557 100644 --- a/src/datasource/sid.h +++ b/src/datasource/sid.h @@ -22,7 +22,11 @@ +#include "datasource-common.h" + + + /* * SNOOPY DATA SOURCE: sid */ -int snoopy_datasource_sid (char * const result, char const * const arg); +int snoopy_datasource_sid (char * const resultBuf, size_t resultBufSize, char const * const arg); diff --git a/src/datasource/snoopy_configure_command.c b/src/datasource/snoopy_configure_command.c index 496ef58f..ae30621c 100644 --- a/src/datasource/snoopy_configure_command.c +++ b/src/datasource/snoopy_configure_command.c @@ -48,7 +48,7 @@ * Return: * number of characters in the returned string, or SNOOPY_DATASOURCE_FAILURE */ -int snoopy_datasource_snoopy_configure_command (char * const result, __attribute__((unused)) char const * const arg) +int snoopy_datasource_snoopy_configure_command (char * const resultBuf, size_t resultBufSize, __attribute__((unused)) char const * const arg) { - return snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "%s", SNOOPY_CONFIGURE_COMMAND); + return snprintf(resultBuf, resultBufSize, "%s", SNOOPY_CONFIGURE_COMMAND); } diff --git a/src/datasource/snoopy_configure_command.h b/src/datasource/snoopy_configure_command.h index b2f2473e..08e3faf6 100644 --- a/src/datasource/snoopy_configure_command.h +++ b/src/datasource/snoopy_configure_command.h @@ -22,7 +22,11 @@ +#include "datasource-common.h" + + + /* * SNOOPY DATA SOURCE: snoopy_configure_command */ -int snoopy_datasource_snoopy_configure_command (char * const result, char const * const arg); +int snoopy_datasource_snoopy_configure_command (char * const resultBuf, size_t resultBufSize, char const * const arg); diff --git a/src/datasource/snoopy_literal.c b/src/datasource/snoopy_literal.c index 293f4794..72fb46a4 100644 --- a/src/datasource/snoopy_literal.c +++ b/src/datasource/snoopy_literal.c @@ -46,7 +46,7 @@ * Return: * number of characters in the returned string, or SNOOPY_DATASOURCE_FAILURE */ -int snoopy_datasource_snoopy_literal (char * const result, char const * const arg) +int snoopy_datasource_snoopy_literal (char * const resultBuf, size_t resultBufSize, char const * const arg) { - return snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "%s", arg); + return snprintf(resultBuf, resultBufSize, "%s", arg); } diff --git a/src/datasource/snoopy_literal.h b/src/datasource/snoopy_literal.h index 23abd46d..7797255c 100644 --- a/src/datasource/snoopy_literal.h +++ b/src/datasource/snoopy_literal.h @@ -22,7 +22,11 @@ +#include "datasource-common.h" + + + /* * SNOOPY DATA SOURCE: snoopy_literal */ -int snoopy_datasource_snoopy_literal (char * const result, char const * const arg); +int snoopy_datasource_snoopy_literal (char * const resultBuf, size_t resultBufSize, char const * const arg); diff --git a/src/datasource/snoopy_threads.c b/src/datasource/snoopy_threads.c index 3486bfc1..ce153e71 100644 --- a/src/datasource/snoopy_threads.c +++ b/src/datasource/snoopy_threads.c @@ -51,7 +51,7 @@ * Return: * number of characters in the returned string, or SNOOPY_DATASOURCE_FAILURE */ -int snoopy_datasource_snoopy_threads (char * const result, __attribute__((unused)) char const * const arg) +int snoopy_datasource_snoopy_threads (char * const resultBuf, size_t resultBufSize, __attribute__((unused)) char const * const arg) { - return snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "%d", snoopy_tsrm_get_threadCount()); + return snprintf(resultBuf, resultBufSize, "%d", snoopy_tsrm_get_threadCount()); } diff --git a/src/datasource/snoopy_threads.h b/src/datasource/snoopy_threads.h index 8f27bb64..3201ca39 100644 --- a/src/datasource/snoopy_threads.h +++ b/src/datasource/snoopy_threads.h @@ -22,7 +22,11 @@ +#include "datasource-common.h" + + + /* * SNOOPY DATA SOURCE: snoopy_threads */ -int snoopy_datasource_snoopy_threads (char * const result, char const * const arg); +int snoopy_datasource_snoopy_threads (char * const resultBuf, size_t resultBufSize, char const * const arg); diff --git a/src/datasource/snoopy_version.c b/src/datasource/snoopy_version.c index 8c187ac7..5db6cf2a 100644 --- a/src/datasource/snoopy_version.c +++ b/src/datasource/snoopy_version.c @@ -51,7 +51,7 @@ * Return: * number of characters in the returned string, or SNOOPY_DATASOURCE_FAILURE */ -__attribute__((visibility("default"))) int snoopy_datasource_snoopy_version (char * const result, __attribute__((unused)) char const * const arg) +__attribute__((visibility("default"))) int snoopy_datasource_snoopy_version (char * const resultBuf, size_t resultBufSize, __attribute__((unused)) char const * const arg) { - return snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "%s", SNOOPY_VERSION); + return snprintf(resultBuf, resultBufSize, "%s", SNOOPY_VERSION); } diff --git a/src/datasource/snoopy_version.h b/src/datasource/snoopy_version.h index aa99a851..f94fc1a8 100644 --- a/src/datasource/snoopy_version.h +++ b/src/datasource/snoopy_version.h @@ -22,7 +22,11 @@ +#include "datasource-common.h" + + + /* * SNOOPY DATA SOURCE: snoopy_version */ -int snoopy_datasource_snoopy_version (char * const result, char const * const arg); +int snoopy_datasource_snoopy_version (char * const resultBuf, size_t resultBufSize, char const * const arg); diff --git a/src/datasource/systemd-unit-name.c b/src/datasource/systemd-unit-name.c index 607272fa..de6c9302 100644 --- a/src/datasource/systemd-unit-name.c +++ b/src/datasource/systemd-unit-name.c @@ -36,22 +36,24 @@ -int snoopy_datasource_systemd_unit_name (char * const result, __attribute__((unused)) char const * const arg) +int snoopy_datasource_systemd_unit_name (char * const resultBuf, size_t resultBufSize, __attribute__((unused)) char const * const arg) { - char cgroupEntry[SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE] = ""; + char *cgroupEntry = NULL; int cgroupDsRetVal; char *unitName = NULL; int retMsgLen; // Get the cgroup entry - cgroupDsRetVal = snoopy_datasource_cgroup(cgroupEntry, "name=systemd"); + cgroupEntry = malloc(resultBufSize); + cgroupDsRetVal = snoopy_datasource_cgroup(cgroupEntry, resultBufSize, "name=systemd"); if ( (cgroupDsRetVal == SNOOPY_DATASOURCE_FAILURE) || (0 == strcmp(cgroupEntry, "(none)")) ) { - snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "Cgroup entry 'name=systemd' not found"); + snprintf(resultBuf, resultBufSize, "Cgroup entry 'name=systemd' not found"); + free(cgroupEntry); return SNOOPY_DATASOURCE_FAILURE; } @@ -59,12 +61,15 @@ int snoopy_datasource_systemd_unit_name (char * const result, __attribute__((unu // Convert unitName = snoopy_util_systemd_convertCgroupEntryToUnitName(cgroupEntry); if (!unitName) { - return snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "%s", cgroupEntry+strlen("1:name=systemd:/")); + retMsgLen = snprintf(resultBuf, resultBufSize, "%s", cgroupEntry+strlen("1:name=systemd:/")); + free(cgroupEntry); + return retMsgLen; } + free(cgroupEntry); // Return - retMsgLen = snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "%s", unitName); + retMsgLen = snprintf(resultBuf, resultBufSize, "%s", unitName); free(unitName); return retMsgLen; } diff --git a/src/datasource/systemd-unit-name.h b/src/datasource/systemd-unit-name.h index 7d8db52b..f4bde98d 100644 --- a/src/datasource/systemd-unit-name.h +++ b/src/datasource/systemd-unit-name.h @@ -20,7 +20,11 @@ +#include "datasource-common.h" + + + /* * SNOOPY DATA SOURCE: systemd_unit_name */ -int snoopy_datasource_systemd_unit_name (char * const result, char const * const arg); +int snoopy_datasource_systemd_unit_name (char * const resultBuf, size_t resultBufSize, char const * const arg); diff --git a/src/datasource/tid.c b/src/datasource/tid.c index 02829166..32097604 100644 --- a/src/datasource/tid.c +++ b/src/datasource/tid.c @@ -47,7 +47,7 @@ * Return: * number of characters in the returned string, or SNOOPY_DATASOURCE_FAILURE */ -int snoopy_datasource_tid (char * const result, __attribute__((unused)) char const * const arg) +int snoopy_datasource_tid (char * const resultBuf, size_t resultBufSize, __attribute__((unused)) char const * const arg) { // Musl (on Alpine 3.16) has pthread_t defined as 'unsigned long', but glibc // defines it as 'unsigned long int'. @@ -57,8 +57,8 @@ int snoopy_datasource_tid (char * const result, __attribute__((unused)) char con // This happens if -lpthread is not given to compiler if (0 == tid) { - return snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "(error @ pthread_self())"); + return snprintf(resultBuf, resultBufSize, "(error @ pthread_self())"); } - return snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "%lu", tid); + return snprintf(resultBuf, resultBufSize, "%lu", tid); } diff --git a/src/datasource/tid.h b/src/datasource/tid.h index cf283423..546f612c 100644 --- a/src/datasource/tid.h +++ b/src/datasource/tid.h @@ -22,7 +22,11 @@ +#include "datasource-common.h" + + + /* * SNOOPY DATA SOURCE: tid */ -int snoopy_datasource_tid (char * const result, char const * const arg); +int snoopy_datasource_tid (char * const resultBuf, size_t resultBufSize, char const * const arg); diff --git a/src/datasource/tid_kernel.c b/src/datasource/tid_kernel.c index 2b0fdf19..0e049670 100644 --- a/src/datasource/tid_kernel.c +++ b/src/datasource/tid_kernel.c @@ -51,14 +51,14 @@ * Return: * number of characters in the returned string, or SNOOPY_DATASOURCE_FAILURE */ -int snoopy_datasource_tid_kernel (char * const result, __attribute__((unused)) char const * const arg) +int snoopy_datasource_tid_kernel (char * const resultBuf, size_t resultBufSize, __attribute__((unused)) char const * const arg) { long unsigned int tid = (long unsigned int) syscall(SYS_gettid); // This happens if -lpthread is not given to compiler if (0 == tid) { - return snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "(error @ syscall(SYS_gettid))"); + return snprintf(resultBuf, resultBufSize, "(error @ syscall(SYS_gettid))"); } - return snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "%lu", tid); + return snprintf(resultBuf, resultBufSize, "%lu", tid); } diff --git a/src/datasource/tid_kernel.h b/src/datasource/tid_kernel.h index 7aec3f42..cd2102d8 100644 --- a/src/datasource/tid_kernel.h +++ b/src/datasource/tid_kernel.h @@ -22,7 +22,11 @@ +#include "datasource-common.h" + + + /* * SNOOPY DATA SOURCE: tid_kernel */ -int snoopy_datasource_tid_kernel (char * const result, char const * const arg); +int snoopy_datasource_tid_kernel (char * const resultBuf, size_t resultBufSize, char const * const arg); diff --git a/src/datasource/timestamp.c b/src/datasource/timestamp.c index 9e0fec5e..72f783df 100644 --- a/src/datasource/timestamp.c +++ b/src/datasource/timestamp.c @@ -48,15 +48,15 @@ * Return: * number of characters in the returned string, or SNOOPY_DATASOURCE_FAILURE */ -int snoopy_datasource_timestamp (char * const result, __attribute__((unused)) char const * const arg) +int snoopy_datasource_timestamp (char * const resultBuf, size_t resultBufSize, __attribute__((unused)) char const * const arg) { struct timeval tv; int retVal; retVal = gettimeofday(&tv, NULL); if (0 == retVal) { - return snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "%d", (int) tv.tv_sec); + return snprintf(resultBuf, resultBufSize, "%d", (int) tv.tv_sec); } else { - return snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "(error: %d)", errno); + return snprintf(resultBuf, resultBufSize, "(error: %d)", errno); } } diff --git a/src/datasource/timestamp.h b/src/datasource/timestamp.h index 7d7128d6..4673ae1b 100644 --- a/src/datasource/timestamp.h +++ b/src/datasource/timestamp.h @@ -22,7 +22,11 @@ +#include "datasource-common.h" + + + /* * SNOOPY DATA SOURCE: timestamp */ -int snoopy_datasource_timestamp (char * const result, char const * const arg); +int snoopy_datasource_timestamp (char * const resultBuf, size_t resultBufSize, char const * const arg); diff --git a/src/datasource/timestamp_ms.c b/src/datasource/timestamp_ms.c index 70d78414..5530a09b 100644 --- a/src/datasource/timestamp_ms.c +++ b/src/datasource/timestamp_ms.c @@ -48,15 +48,15 @@ * Return: * number of characters in the returned string, or SNOOPY_DATASOURCE_FAILURE */ -int snoopy_datasource_timestamp_ms (char * const result, __attribute__((unused)) char const * const arg) +int snoopy_datasource_timestamp_ms (char * const resultBuf, size_t resultBufSize, __attribute__((unused)) char const * const arg) { struct timeval tv; int retVal; retVal = gettimeofday(&tv, NULL); if (0 == retVal) { - return snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "%03d", (int) tv.tv_usec/1000); + return snprintf(resultBuf, resultBufSize, "%03d", (int) tv.tv_usec/1000); } else { - return snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "(error: %d)", errno); + return snprintf(resultBuf, resultBufSize, "(error: %d)", errno); } } diff --git a/src/datasource/timestamp_ms.h b/src/datasource/timestamp_ms.h index d7fc1a1d..81528e10 100644 --- a/src/datasource/timestamp_ms.h +++ b/src/datasource/timestamp_ms.h @@ -22,7 +22,11 @@ +#include "datasource-common.h" + + + /* * SNOOPY DATA SOURCE: timestamp_ms */ -int snoopy_datasource_timestamp_ms (char * const result, char const * const arg); +int snoopy_datasource_timestamp_ms (char * const resultBuf, size_t resultBufSize, char const * const arg); diff --git a/src/datasource/timestamp_us.c b/src/datasource/timestamp_us.c index 09e4a1a3..61ba7dde 100644 --- a/src/datasource/timestamp_us.c +++ b/src/datasource/timestamp_us.c @@ -48,15 +48,15 @@ * Return: * number of characters in the returned string, or SNOOPY_DATASOURCE_FAILURE */ -int snoopy_datasource_timestamp_us (char * const result, __attribute__((unused)) char const * const arg) +int snoopy_datasource_timestamp_us (char * const resultBuf, size_t resultBufSize, __attribute__((unused)) char const * const arg) { struct timeval tv; int retVal; retVal = gettimeofday(&tv, NULL); if (0 == retVal) { - return snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "%06d", (int) tv.tv_usec); + return snprintf(resultBuf, resultBufSize, "%06d", (int) tv.tv_usec); } else { - return snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "(error: %d)", errno); + return snprintf(resultBuf, resultBufSize, "(error: %d)", errno); } } diff --git a/src/datasource/timestamp_us.h b/src/datasource/timestamp_us.h index 691dc8d1..96680c37 100644 --- a/src/datasource/timestamp_us.h +++ b/src/datasource/timestamp_us.h @@ -22,7 +22,11 @@ +#include "datasource-common.h" + + + /* * SNOOPY DATA SOURCE: timestamp_us */ -int snoopy_datasource_timestamp_us (char * const result, char const * const arg); +int snoopy_datasource_timestamp_us (char * const resultBuf, size_t resultBufSize, char const * const arg); diff --git a/src/datasource/tty.c b/src/datasource/tty.c index 1f6acd65..d199384c 100644 --- a/src/datasource/tty.c +++ b/src/datasource/tty.c @@ -48,7 +48,7 @@ * Return: * number of characters in the returned string, or SNOOPY_DATASOURCE_FAILURE */ -int snoopy_datasource_tty (char * const result, __attribute__((unused)) char const * const arg) +int snoopy_datasource_tty (char * const resultBuf, size_t resultBufSize, __attribute__((unused)) char const * const arg) { char ttyPath[SNOOPY_DATASOURCE_TTY_sizeMaxWithNull]; size_t ttyPathLen = SNOOPY_DATASOURCE_TTY_sizeMaxWithoutNull; @@ -57,16 +57,16 @@ int snoopy_datasource_tty (char * const result, __attribute__((unused)) char con retVal = ttyname_r(0, ttyPath, ttyPathLen); if (0 != retVal) { if (EBADF == retVal) { - return snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "ERROR(ttyname_r->EBADF)"); + return snprintf(resultBuf, resultBufSize, "ERROR(ttyname_r->EBADF)"); } if (ERANGE == retVal) { - return snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "ERROR(ttyname_r->ERANGE)"); + return snprintf(resultBuf, resultBufSize, "ERROR(ttyname_r->ERANGE)"); } if (ENOTTY == retVal) { - return snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "(none)"); + return snprintf(resultBuf, resultBufSize, "(none)"); } - return snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "(unknown)"); + return snprintf(resultBuf, resultBufSize, "(unknown)"); } - return snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "%s", ttyPath); + return snprintf(resultBuf, resultBufSize, "%s", ttyPath); } diff --git a/src/datasource/tty.h b/src/datasource/tty.h index 0fb9fdfc..edebce25 100644 --- a/src/datasource/tty.h +++ b/src/datasource/tty.h @@ -22,6 +22,10 @@ +#include "datasource-common.h" + + + /* * Local defines */ @@ -33,4 +37,4 @@ /* * SNOOPY DATA SOURCE: tty */ -int snoopy_datasource_tty (char * const result, char const * const arg); +int snoopy_datasource_tty (char * const resultBuf, size_t resultBufSize, char const * const arg); diff --git a/src/datasource/tty__common.c b/src/datasource/tty__common.c index 6e482007..2ef5d52c 100644 --- a/src/datasource/tty__common.c +++ b/src/datasource/tty__common.c @@ -54,7 +54,7 @@ * - 0 on success (result in ttyUid) * - number of characters in the returned string on error */ -int snoopy_datasource_tty__get_tty_uid (uid_t * ttyUid, char * const result) +int snoopy_datasource_tty__get_tty_uid (uid_t * ttyUid, char * const resultBuf, size_t resultBufSize) { char ttyPath[SNOOPY_DATASOURCE_TTY_sizeMaxWithNull]; size_t ttyPathLen = SNOOPY_DATASOURCE_TTY_sizeMaxWithoutNull; @@ -65,20 +65,20 @@ int snoopy_datasource_tty__get_tty_uid (uid_t * ttyUid, char * const result) retVal = ttyname_r(0, ttyPath, ttyPathLen); if (0 != retVal) { if (EBADF == retVal) { - return snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "ERROR(ttyname_r->EBADF)"); + return snprintf(resultBuf, resultBufSize, "ERROR(ttyname_r->EBADF)"); } if (ERANGE == retVal) { - return snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "ERROR(ttyname_r->ERANGE)"); + return snprintf(resultBuf, resultBufSize, "ERROR(ttyname_r->ERANGE)"); } if (ENOTTY == retVal) { - return snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "(none)"); + return snprintf(resultBuf, resultBufSize, "(none)"); } - return snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "(unknown)"); + return snprintf(resultBuf, resultBufSize, "(unknown)"); } /* Get owner of tty */ if (-1 == stat(ttyPath, &statbuffer)) { - return snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "ERROR(unable to stat() %s)", ttyPath); + return snprintf(resultBuf, resultBufSize, "ERROR(unable to stat() %s)", ttyPath); } *ttyUid = statbuffer.st_uid; diff --git a/src/datasource/tty__common.h b/src/datasource/tty__common.h index 311c026f..bc944d2e 100644 --- a/src/datasource/tty__common.h +++ b/src/datasource/tty__common.h @@ -22,6 +22,10 @@ +#include "datasource-common.h" + + + #include "snoopy.h" #include @@ -30,4 +34,4 @@ /* * SNOOPY DATA SOURCE: tty-related common functions */ -int snoopy_datasource_tty__get_tty_uid (uid_t * ttyUid, char * const result); +int snoopy_datasource_tty__get_tty_uid (uid_t * ttyUid, char * const resultBuf, size_t resultBufSize); diff --git a/src/datasource/tty_uid.c b/src/datasource/tty_uid.c index 28db8ac5..8fb479eb 100644 --- a/src/datasource/tty_uid.c +++ b/src/datasource/tty_uid.c @@ -54,15 +54,15 @@ * Return: * number of characters in the returned string, or SNOOPY_DATASOURCE_FAILURE */ -int snoopy_datasource_tty_uid (char * const result, __attribute__((unused)) char const * const arg) +int snoopy_datasource_tty_uid (char * const resultBuf, size_t resultBufSize, __attribute__((unused)) char const * const arg) { int retVal; uid_t ttyUid; - retVal = snoopy_datasource_tty__get_tty_uid(&ttyUid, result); + retVal = snoopy_datasource_tty__get_tty_uid(&ttyUid, resultBuf, resultBufSize); if (retVal > 0) { return retVal; // Error occurred, and the message about it is already in the result buffer } - return snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "%u", ttyUid); + return snprintf(resultBuf, resultBufSize, "%u", ttyUid); } diff --git a/src/datasource/tty_uid.h b/src/datasource/tty_uid.h index 8615a742..bf6e09df 100644 --- a/src/datasource/tty_uid.h +++ b/src/datasource/tty_uid.h @@ -22,7 +22,11 @@ +#include "datasource-common.h" + + + /* * SNOOPY DATA SOURCE: tty_uid */ -int snoopy_datasource_tty_uid (char * const result, char const * const arg); +int snoopy_datasource_tty_uid (char * const resultBuf, size_t resultBufSize, char const * const arg); diff --git a/src/datasource/tty_username.c b/src/datasource/tty_username.c index d484218d..c806803a 100644 --- a/src/datasource/tty_username.c +++ b/src/datasource/tty_username.c @@ -51,7 +51,7 @@ * Return: * number of characters in the returned string, or SNOOPY_DATASOURCE_FAILURE */ -int snoopy_datasource_tty_username (char * const result, __attribute__((unused)) char const * const arg) +int snoopy_datasource_tty_username (char * const resultBuf, size_t resultBufSize, __attribute__((unused)) char const * const arg) { int retVal; uid_t ttyUid; @@ -60,7 +60,7 @@ int snoopy_datasource_tty_username (char * const result, __attribute__((unused)) /* Get tty UID */ - retVal = snoopy_datasource_tty__get_tty_uid(&ttyUid, result); + retVal = snoopy_datasource_tty__get_tty_uid(&ttyUid, resultBuf, resultBufSize); if (retVal > 0) { return retVal; // Error occurred, and the message about it is already in the result buffer } @@ -68,11 +68,11 @@ int snoopy_datasource_tty_username (char * const result, __attribute__((unused)) username = snoopy_util_pwd_convertUidToUsername(ttyUid); if (username == NULL) { - return snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "Unable to convert UID to username"); + return snprintf(resultBuf, resultBufSize, "Unable to convert UID to username"); } - retMsgLen = snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "%s", username); + retMsgLen = snprintf(resultBuf, resultBufSize, "%s", username); free(username); return retMsgLen; } diff --git a/src/datasource/tty_username.h b/src/datasource/tty_username.h index 3cc099e2..19f0f447 100644 --- a/src/datasource/tty_username.h +++ b/src/datasource/tty_username.h @@ -22,7 +22,11 @@ +#include "datasource-common.h" + + + /* * SNOOPY DATA SOURCE: tty_username */ -int snoopy_datasource_tty_username (char * const result, char const * const arg); +int snoopy_datasource_tty_username (char * const resultBuf, size_t resultBufSize, char const * const arg); diff --git a/src/datasource/uid.c b/src/datasource/uid.c index b091dd59..accef09c 100644 --- a/src/datasource/uid.c +++ b/src/datasource/uid.c @@ -48,7 +48,7 @@ * Return: * number of characters in the returned string, or SNOOPY_DATASOURCE_FAILURE */ -int snoopy_datasource_uid (char * const result, __attribute__((unused)) char const * const arg) +int snoopy_datasource_uid (char * const resultBuf, size_t resultBufSize, __attribute__((unused)) char const * const arg) { - return snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "%u", getuid()); + return snprintf(resultBuf, resultBufSize, "%u", getuid()); } diff --git a/src/datasource/uid.h b/src/datasource/uid.h index 6bbca3cb..f53b2cc7 100644 --- a/src/datasource/uid.h +++ b/src/datasource/uid.h @@ -22,7 +22,11 @@ +#include "datasource-common.h" + + + /* * SNOOPY DATA SOURCE: uid */ -int snoopy_datasource_uid (char * const result, char const * const arg); +int snoopy_datasource_uid (char * const resultBuf, size_t resultBufSize, char const * const arg); diff --git a/src/datasource/username.c b/src/datasource/username.c index bbfb23bf..44bf66a9 100644 --- a/src/datasource/username.c +++ b/src/datasource/username.c @@ -50,7 +50,7 @@ * Return: * number of characters in the returned string, or SNOOPY_DATASOURCE_FAILURE */ -int snoopy_datasource_username (char * const result, __attribute__((unused)) char const * const arg) +int snoopy_datasource_username (char * const resultBuf, size_t resultBufSize, __attribute__((unused)) char const * const arg) { char * username = NULL; int retMsgLen = 0; @@ -58,11 +58,11 @@ int snoopy_datasource_username (char * const result, __attribute__((unused)) cha username = snoopy_util_pwd_convertUidToUsername(getuid()); if (username == NULL) { - return snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "Unable to convert UID to username"); + return snprintf(resultBuf, resultBufSize, "Unable to convert UID to username"); } - retMsgLen = snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "%s", username); + retMsgLen = snprintf(resultBuf, resultBufSize, "%s", username); free(username); return retMsgLen; } diff --git a/src/datasource/username.h b/src/datasource/username.h index 46aa5ae3..0d197a66 100644 --- a/src/datasource/username.h +++ b/src/datasource/username.h @@ -22,7 +22,11 @@ +#include "datasource-common.h" + + + /* * SNOOPY DATA SOURCE: username */ -int snoopy_datasource_username (char * const result, char const * const arg); +int snoopy_datasource_username (char * const resultBuf, size_t resultBufSize, char const * const arg); diff --git a/src/datasourceregistry.c b/src/datasourceregistry.c index 15f6a18c..75c83ce8 100644 --- a/src/datasourceregistry.c +++ b/src/datasourceregistry.c @@ -275,7 +275,7 @@ char* snoopy_datasourceregistry_names[] = { "", }; -int (*snoopy_datasourceregistry_ptrs []) (char * const result, char const * const arg) = { +int (*snoopy_datasourceregistry_ptrs []) (char * const resultBuf, size_t resultBufSize, char const * const arg) = { #ifdef SNOOPY_CONF_DATASOURCE_ENABLED_cgroup snoopy_datasource_cgroup, #endif @@ -459,13 +459,13 @@ char* snoopy_datasourceregistry_getName (int datasourceId) * * Call the given datasource by id and return its output */ -int snoopy_datasourceregistry_callById (int datasourceId, char * const result, char const * const datasourceArg) +int snoopy_datasourceregistry_callById (int datasourceId, char * const resultBuf, size_t resultBufSize, char const * const datasourceArg) { if (SNOOPY_FALSE == snoopy_datasourceregistry_doesIdExist(datasourceId)) { return -1; } - return snoopy_datasourceregistry_ptrs[datasourceId](result, datasourceArg); + return snoopy_datasourceregistry_ptrs[datasourceId](resultBuf, resultBufSize, datasourceArg); } @@ -475,7 +475,7 @@ int snoopy_datasourceregistry_callById (int datasourceId, char * const result, c * * Call the given datasource by name and return its output */ -int snoopy_datasourceregistry_callByName (char const * const datasourceName, char * const result, char const * const datasourceArg) +int snoopy_datasourceregistry_callByName (char const * const datasourceName, char * const resultBuf, size_t resultBufSize, char const * const datasourceArg) { int datasourceId; @@ -484,5 +484,5 @@ int snoopy_datasourceregistry_callByName (char const * const datasourceName, cha return -1; } - return snoopy_datasourceregistry_ptrs[datasourceId](result, datasourceArg); + return snoopy_datasourceregistry_ptrs[datasourceId](resultBuf, resultBufSize, datasourceArg); } diff --git a/src/datasourceregistry.h b/src/datasourceregistry.h index 61f362fe..b73f8e54 100644 --- a/src/datasourceregistry.h +++ b/src/datasourceregistry.h @@ -22,6 +22,10 @@ +#include + + + /* * Functions to manage and utilise datasources */ @@ -31,5 +35,5 @@ int snoopy_datasourceregistry_doesNameExist (char const * const datasourceName int snoopy_datasourceregistry_getIdFromName (char const * const datasourceName); char* snoopy_datasourceregistry_getName (int datasourceId); -int snoopy_datasourceregistry_callById (int datasourceId, char * const result, char const * const datasourceArg); -int snoopy_datasourceregistry_callByName (char const * const datasourceName, char * const result, char const * const datasourceArg); +int snoopy_datasourceregistry_callById (int datasourceId, char * const resultBuf, size_t resultBufSize, char const * const datasourceArg); +int snoopy_datasourceregistry_callByName (char const * const datasourceName, char * const resultBuf, size_t resultBufSize, char const * const datasourceArg); diff --git a/src/message.c b/src/message.c index 09d1d2ff..f256b70f 100644 --- a/src/message.c +++ b/src/message.c @@ -57,48 +57,56 @@ void snoopy_message_generateFromFormat ( char * const logMessage, size_t logMessageBufSize, + size_t dataSourceMsgMaxLength, char const * const logMessageFormat ) { + size_t dataSourceMsgBufSize; + char * dataSourceMsg = NULL; + char const * fmtPos_cur; char const * fmtPos_nextFormatTag; char const * fmtPos_nextFormatTagClose; int retVal; + dataSourceMsgBufSize = dataSourceMsgMaxLength+1; + dataSourceMsg = malloc(dataSourceMsgBufSize); + fmtPos_cur = logMessageFormat; fmtPos_nextFormatTag = logMessageFormat; // Loop all the way to the end of log message format specification while (strlen(fmtPos_nextFormatTag) > 0) { - int lengthToCopy; - char fmtStaticText[SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE]; + size_t lengthToCopy; char dataSourceTag[100]; int dataSourceTagLength; char *fmtPos_dataSourceTagArg; const char *dataSourceNamePtr; const char *dataSourceArgPtr; char dataSourceArg[SNOOPY_DATASOURCE_ARG_MAX_SIZE]; - char dataSourceMsg[SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE]; // If no data source tag is found, just copy the text and bail out fmtPos_nextFormatTag = strstr(fmtPos_cur, "%{"); if (NULL == fmtPos_nextFormatTag) { snoopy_message_append(logMessage, logMessageBufSize, fmtPos_cur); + free(dataSourceMsg); return; // Should be "break;" but SonarCloud is complaining about it } // Otherwise copy text up to the next data source tag lengthToCopy = (int) (fmtPos_nextFormatTag - fmtPos_cur + 1); // + 1 for null termination - if (lengthToCopy > (int)(logMessageBufSize-strlen(logMessage))) { - lengthToCopy = (int)(logMessageBufSize-strlen(logMessage)); + if (lengthToCopy > dataSourceMsgBufSize) { + lengthToCopy = dataSourceMsgBufSize; } - fmtStaticText[0] = '\0'; - snprintf(fmtStaticText, lengthToCopy, "%s", fmtPos_cur); - snoopy_message_append(logMessage, logMessageBufSize, fmtStaticText); + dataSourceMsg[0] = '\0'; // Let's just use this buffer, even if it is called something else + snprintf(dataSourceMsg, lengthToCopy, "%s", fmtPos_cur); + snoopy_message_append(logMessage, logMessageBufSize, dataSourceMsg); + dataSourceMsg[0] = '\0'; // And wipe it for later reuse // Get data source tag fmtPos_nextFormatTagClose = strstr(fmtPos_nextFormatTag, "}"); if (NULL == fmtPos_nextFormatTagClose) { snoopy_message_append(logMessage, logMessageBufSize, "[ERROR: Closing data source tag ('}') not found.]"); + free(dataSourceMsg); return; // Should be "break;" but SonarCloud is complaining about it } dataSourceTag[0] = '\0'; @@ -124,12 +132,13 @@ void snoopy_message_generateFromFormat ( snoopy_message_append(logMessage, logMessageBufSize, "[ERROR: Data source '"); snoopy_message_append(logMessage, logMessageBufSize, dataSourceNamePtr); snoopy_message_append(logMessage, logMessageBufSize, "' not found.]"); + free(dataSourceMsg); return; // Should be "break;" but SonarCloud is complaining about it } // Call the provider, and append the results to log message dataSourceMsg[0] = '\0'; - retVal = snoopy_datasourceregistry_callByName(dataSourceNamePtr, dataSourceMsg, dataSourceArgPtr); + retVal = snoopy_datasourceregistry_callByName(dataSourceNamePtr, dataSourceMsg, dataSourceMsgBufSize, dataSourceArgPtr); if (SNOOPY_DATASOURCE_FAILED(retVal)) { snoopy_message_append(logMessage, logMessageBufSize, "[ERROR: Data source '"); snoopy_message_append(logMessage, logMessageBufSize, dataSourceNamePtr); @@ -143,6 +152,8 @@ void snoopy_message_generateFromFormat ( // Where to start next iteration fmtPos_cur = fmtPos_nextFormatTagClose + 1; } + + free(dataSourceMsg); } diff --git a/src/message.h b/src/message.h index 6099b484..f9a2ed70 100644 --- a/src/message.h +++ b/src/message.h @@ -29,6 +29,7 @@ void snoopy_message_generateFromFormat ( char * const logMessage, size_t logMessageBufSize, + size_t dataSourceMsgMaxLength, char const * const logMessageFormat ); diff --git a/src/output/devlogoutput.c b/src/output/devlogoutput.c index 7c13068d..95092c31 100644 --- a/src/output/devlogoutput.c +++ b/src/output/devlogoutput.c @@ -64,13 +64,17 @@ int snoopy_output_devlogoutput (char const * const logMessage, __attribute__((un /* Generate syslog ident string */ char syslogIdent[SNOOPY_SYSLOG_IDENT_FORMAT_BUF_SIZE] = {'\0'}; - snoopy_message_generateFromFormat(syslogIdent, SNOOPY_SYSLOG_IDENT_FORMAT_BUF_SIZE, CFG->syslog_ident_format); + snoopy_message_generateFromFormat(syslogIdent, SNOOPY_SYSLOG_IDENT_FORMAT_BUF_SIZE, SNOOPY_SYSLOG_IDENT_FORMAT_BUF_SIZE, CFG->syslog_ident_format); + + /* Allocate buffer */ + size_t logMessageWithPrefixSize = strlen(logMessage) + SNOOPY_SYSLOG_IDENT_FORMAT_BUF_SIZE + 100; // +100 ought to be enough for... whatever :) + char * logMessageWithPrefix = malloc(logMessageWithPrefixSize); + logMessageWithPrefix[0] = '\0'; /* Generate final message - add prefix which is otherwise added by syslog() syscall */ - char logMessageWithPrefix[SNOOPY_LOG_MESSAGE_BUF_SIZE + SNOOPY_SYSLOG_IDENT_FORMAT_BUF_SIZE + 100] = {'\0'}; // +100 ought to be enough snprintf( logMessageWithPrefix, - SNOOPY_LOG_MESSAGE_BUF_SIZE + SNOOPY_SYSLOG_IDENT_FORMAT_BUF_SIZE + 100, + logMessageWithPrefixSize, "<%d>%.*s[%d]: %s", CFG->syslog_facility | CFG->syslog_level, SNOOPY_SYSLOG_IDENT_FORMAT_BUF_SIZE-1, @@ -80,5 +84,7 @@ int snoopy_output_devlogoutput (char const * const logMessage, __attribute__((un ); /* Pass execution to another output provider */ - return snoopy_output_socketoutput(logMessageWithPrefix, "/dev/log"); + int bytesWritten = snoopy_output_socketoutput(logMessageWithPrefix, "/dev/log"); + free(logMessageWithPrefix); + return bytesWritten; } diff --git a/src/output/fileoutput.c b/src/output/fileoutput.c index af07fba1..c977faed 100644 --- a/src/output/fileoutput.c +++ b/src/output/fileoutput.c @@ -64,7 +64,7 @@ int snoopy_output_fileoutput (char const * const logMessage, char const * const } // Parse the output file specification (i.e. for %{datetime} or similar tags) - snoopy_message_generateFromFormat(filePath, PATH_MAX, arg); + snoopy_message_generateFromFormat(filePath, PATH_MAX, PATH_MAX, arg); // Try to open file in append mode fp = fopen(filePath, "a"); diff --git a/src/output/syslogoutput.c b/src/output/syslogoutput.c index f5a41267..71de1a40 100644 --- a/src/output/syslogoutput.c +++ b/src/output/syslogoutput.c @@ -65,7 +65,7 @@ int snoopy_output_syslogoutput (char const * const logMessage, __attribute__((un /* Generate syslog ident string */ char syslogIdent[SNOOPY_SYSLOG_IDENT_FORMAT_BUF_SIZE] = {'\0'}; - snoopy_message_generateFromFormat(syslogIdent, SNOOPY_SYSLOG_IDENT_FORMAT_BUF_SIZE, CFG->syslog_ident_format); + snoopy_message_generateFromFormat(syslogIdent, SNOOPY_SYSLOG_IDENT_FORMAT_BUF_SIZE, SNOOPY_SYSLOG_IDENT_FORMAT_BUF_SIZE, CFG->syslog_ident_format); openlog(syslogIdent, LOG_PID, CFG->syslog_facility); syslog(CFG->syslog_level, "%s", logMessage); diff --git a/src/snoopy.h b/src/snoopy.h index 68d461de..c5adfcb5 100644 --- a/src/snoopy.h +++ b/src/snoopy.h @@ -57,7 +57,11 @@ * Maximum length of a string returned from any data source function, * including terminating null character. */ -#define SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE 2048 +#define SNOOPY_DATASOURCE_MESSAGE_MAX_LENGTH_DEFAULT 2047 +#define SNOOPY_DATASOURCE_MESSAGE_MAX_LENGTH_HARDMIN 255 +#ifndef SNOOPY_DATASOURCE_MESSAGE_MAX_LENGTH_HARDMAX +#define SNOOPY_DATASOURCE_MESSAGE_MAX_LENGTH_HARDMAX 1048575 +#endif @@ -105,7 +109,11 @@ * * Size of the log message buffer (message + \0). */ -#define SNOOPY_LOG_MESSAGE_BUF_SIZE 16383 +#define SNOOPY_LOG_MESSAGE_MAX_LENGTH_DEFAULT 16383 +#define SNOOPY_LOG_MESSAGE_MAX_LENGTH_HARDMIN 255 +#ifndef SNOOPY_LOG_MESSAGE_MAX_LENGTH_HARDMAX +#define SNOOPY_LOG_MESSAGE_MAX_LENGTH_HARDMAX 1048575 +#endif diff --git a/src/util/parser-snoopy.h b/src/util/parser-snoopy.h index 8b8b04ca..ec49e896 100644 --- a/src/util/parser-snoopy.h +++ b/src/util/parser-snoopy.h @@ -26,3 +26,4 @@ * Parser functions */ int snoopy_util_parser_csvToArgList (char *argListRaw, char ***argListParsed); +int snoopy_util_parser_strByteLength (const char *numberAsText, const int valMin, const int valMax, const int valDefault); diff --git a/src/util/parser.c b/src/util/parser.c index b9bfc3a1..0032d29b 100644 --- a/src/util/parser.c +++ b/src/util/parser.c @@ -29,6 +29,7 @@ #include "util/string-snoopy.h" +#include #include #include @@ -102,3 +103,52 @@ int snoopy_util_parser_csvToArgList (char *argListRaw, char ***argListParsed) return argCount; } + + + +/* + * Parses a textual representation of an integer into an actual integer + * + * Params: + * textInt: String containing CSV to parse + * argListParsed: A pointer that will get malloc()-ed array of char pointers to individual CSV entries + * + * Return: + * int: Number of arguments parsed out. + */ +int snoopy_util_parser_strByteLength (char const * const numberAsText, const int valMin, const int valMax, const int valDefault) +{ + char const *numberAsTextPtr = numberAsText; + int numbersBufLength = 20; // 20 characters are needed to store max long long int in decimal representation + \0. + char numbersBuf[numbersBufLength]; + int numberInt; + int factor = 1; + int result; + + // Extract numbers + while ((*numberAsTextPtr != '\0') && isdigit(*numberAsTextPtr) && (numberAsTextPtr-numberAsText < numbersBufLength-2)) { + numbersBuf[numberAsTextPtr - numberAsText] = *numberAsTextPtr; + numberAsTextPtr++; + } + numbersBuf[numberAsTextPtr - numberAsText] = '\0'; + + // Convert to int + numberInt = atoi(numbersBuf); + if (numberInt == 0) { + return valDefault; + } + + // Apply metric prefixes + if ((*numberAsTextPtr == 'k') || (*numberAsTextPtr == 'K')) { + factor = 1024; + } else if ((*numberAsTextPtr == 'm') || (*numberAsTextPtr == 'M')) { + factor = 1024*1024; + } + result = numberInt * factor; + + // Apply limits + if (result < valMin) result = valMin; + if (result > valMax) result = valMax; + + return result; +} diff --git a/tests/bin/Makefile.am b/tests/bin/Makefile.am index 381bc21d..a29c02c6 100644 --- a/tests/bin/Makefile.am +++ b/tests/bin/Makefile.am @@ -43,6 +43,8 @@ snoopy_test_SOURCES = \ action-unit-outputregistry.h \ action-unit-util.c \ action-unit-util.h \ + action-unit-util-parser.c \ + action-unit-util-parser.h \ action-unit-util-syslog.c \ action-unit-util-syslog.h \ snoopy-test.c diff --git a/tests/bin/action-run-configfile.c b/tests/bin/action-run-configfile.c index c3df9899..f1c86866 100644 --- a/tests/bin/action-run-configfile.c +++ b/tests/bin/action-run-configfile.c @@ -27,12 +27,14 @@ #include "snoopy.h" #include "entrypoint/test-cli.h" +#include "configfile.h" #include "configuration.h" #include "inputdatastorage.h" #include "util/syslog-snoopy.h" #include #include +#include #include #include @@ -70,6 +72,8 @@ void snoopyTestCli_action_run_configfile_showHelp () " syslog_facility\n" " syslog_ident\n" " syslog_level\n" + " datasource_message_max_length\n" + " log_message_max_length\n" "NOTICE: These keys MUST be placed in a section named [snoopy].\n" "\n"; printf("%s", helpContent); @@ -141,6 +145,16 @@ int snoopyTestCli_action_run_configfile (int argc, char **argv) } else if (0 == strcmp(showConfigVar, "syslog_level")) { printf("%s\n", snoopy_util_syslog_convertLevelToStr(CFG->syslog_level)); + } else if (0 == strcmp(showConfigVar, "datasource_message_max_length")) { + char * valBuf = snoopy_configfile_getOptionValueAsString_datasource_message_max_length(); + printf("%s\n", valBuf); + free(valBuf); + + } else if (0 == strcmp(showConfigVar, "log_message_max_length")) { + char * valBuf = snoopy_configfile_getOptionValueAsString_log_message_max_length(); + printf("%s\n", valBuf); + free(valBuf); + } else if (0 == strcmp(showConfigVar, "error_logging")) { printf("%s\n", (CFG->error_logging_enabled == SNOOPY_TRUE ? "y" : "n")); diff --git a/tests/bin/action-run-datasource.c b/tests/bin/action-run-datasource.c index 02653b04..7313c001 100644 --- a/tests/bin/action-run-datasource.c +++ b/tests/bin/action-run-datasource.c @@ -74,14 +74,19 @@ int snoopyTestCli_action_run_datasource (int argc, char ** argv) const char *arg1; const char *datasourceName; const char *datasourceArg; - char datasourceResult[SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE]; + char * datasourceResult; int retVal; + const snoopy_configuration_t *CFG; /* Initialize Snoopy */ snoopy_entrypoint_test_cli_init((char const *)g_argv[0], g_argv, NULL); + /* Get config pointer */ + CFG = snoopy_configuration_get(); + + /* Check if there is a data source name passed as an argument */ if (argc < 1) { snoopyTestCli_action_run_datasource_showHelp(); @@ -92,7 +97,7 @@ int snoopyTestCli_action_run_datasource (int argc, char ** argv) /* Is second argument --list? */ if (0 == strcmp(arg1, "--all")) { - snoopyTestCli_action_run_datasource_all(); + snoopyTestCli_action_run_datasource_all(CFG->datasource_message_max_length); return 0; } if (0 == strcmp(arg1, "--help")) { @@ -121,9 +126,10 @@ int snoopyTestCli_action_run_datasource (int argc, char ** argv) /* Call the datasource */ - retVal = snoopy_datasourceregistry_callByName(datasourceName, datasourceResult, datasourceArg); + datasourceResult = malloc(CFG->datasource_message_max_length+1); + retVal = snoopy_datasourceregistry_callByName(datasourceName, datasourceResult, CFG->datasource_message_max_length+1, datasourceArg); if (SNOOPY_DATASOURCE_FAILED(retVal)) { - fatalErrorValue("Datasource failed", datasourceResult); + fatalErrorValueFree("Datasource failed", datasourceResult); } @@ -132,22 +138,25 @@ int snoopyTestCli_action_run_datasource (int argc, char ** argv) /* Housekeeping and return */ + free(datasourceResult); snoopy_entrypoint_test_cli_exit(); return 0; } -void snoopyTestCli_action_run_datasource_all () +void snoopyTestCli_action_run_datasource_all (size_t datasource_message_max_length) { char *itemName = NULL; const char *itemArgs = NULL; char *itemResult = NULL; - int itemResultSize; + size_t itemResultBufSize; + size_t itemResultSize; int dCount; /* Initialize variables and spaces */ - itemResult = malloc(SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE + 1); + itemResultBufSize = datasource_message_max_length + 1; + itemResult = malloc(itemResultBufSize); /* Loop through all datasources and just send to output */ dCount = snoopy_datasourceregistry_getCount(); @@ -166,8 +175,8 @@ void snoopyTestCli_action_run_datasource_all () } /* Execute the data source function */ - itemResultSize = snoopy_datasourceregistry_callById(i, itemResult, itemArgs); - if (itemResultSize > SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE) { + itemResultSize = snoopy_datasourceregistry_callById(i, itemResult, itemResultBufSize, itemArgs); + if (itemResultSize > itemResultBufSize) { snoopy_error_handler("Maximum data source message size exceeded"); } diff --git a/tests/bin/action-run-datasource.h b/tests/bin/action-run-datasource.h index bacf8b22..053fd01c 100644 --- a/tests/bin/action-run-datasource.h +++ b/tests/bin/action-run-datasource.h @@ -20,5 +20,9 @@ +#include + + + int snoopyTestCli_action_run_datasource (int argc, char ** argv); -void snoopyTestCli_action_run_datasource_all (); +void snoopyTestCli_action_run_datasource_all (size_t datasource_message_max_length); diff --git a/tests/bin/action-run-everything.c b/tests/bin/action-run-everything.c index 554e6b0d..a26cc1fc 100644 --- a/tests/bin/action-run-everything.c +++ b/tests/bin/action-run-everything.c @@ -80,7 +80,7 @@ int snoopyTestCli_action_run_everything () /* Initialize empty log message */ - logMessage = malloc(SNOOPY_LOG_MESSAGE_BUF_SIZE); + logMessage = malloc(CFG->log_message_max_length+1); logMessage[0] = '\0'; @@ -97,13 +97,13 @@ int snoopyTestCli_action_run_everything () printf("SKIPPED - not enabled.\n"); #endif printf("-----[ Datasources ]-----------------------------------\n"); - snoopyTestCli_action_run_datasource_all(); + snoopyTestCli_action_run_datasource_all(CFG->datasource_message_max_length+1); printf("-----[ Outputs ]---------------------------------------\n"); snoopyTestCli_action_run_output_all(); printf("-----[ Message formatting ]----------------------------\n"); - 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); printf("Message: %s\n", logMessage); diff --git a/tests/bin/action-run-messageformat.c b/tests/bin/action-run-messageformat.c index 51528749..96a76f34 100644 --- a/tests/bin/action-run-messageformat.c +++ b/tests/bin/action-run-messageformat.c @@ -60,12 +60,17 @@ int snoopyTestCli_action_run_messageformat (int argc, char **argv) { const char * messageFormat; char * message; + const snoopy_configuration_t *CFG; /* Initialize Snoopy */ snoopy_entrypoint_test_cli_init((char const *)g_argv[0], g_argv, NULL); + /* Get config pointer */ + CFG = snoopy_configuration_get(); + + /* Check if all arguments are present */ if (argc < 1) { snoopyTestCli_action_run_messageformat_showHelp(); @@ -75,12 +80,12 @@ int snoopyTestCli_action_run_messageformat (int argc, char **argv) /* Initialize message */ - message = malloc(SNOOPY_LOG_MESSAGE_BUF_SIZE); + message = malloc(CFG->log_message_max_length+1); message[0] = '\0'; /* Call the filter */ - snoopy_message_generateFromFormat(message, SNOOPY_LOG_MESSAGE_BUF_SIZE, messageFormat); + snoopy_message_generateFromFormat(message, CFG->log_message_max_length+1, CFG->datasource_message_max_length+1, messageFormat); /* Display result */ diff --git a/tests/bin/action-run-output.c b/tests/bin/action-run-output.c index 353cb776..1c481615 100644 --- a/tests/bin/action-run-output.c +++ b/tests/bin/action-run-output.c @@ -145,11 +145,20 @@ void snoopyTestCli_action_run_output_all () const char *itemArgs = NULL; int itemResult; int iCount; + const snoopy_configuration_t *CFG; + + + /* Initialize Snoopy */ + snoopy_entrypoint_test_cli_init((char const *)g_argv[0], g_argv, NULL); + + + /* Get config pointer */ + CFG = snoopy_configuration_get(); /* Initialize variables and spaces */ - message = malloc(SNOOPY_LOG_MESSAGE_BUF_SIZE); - snprintf(message, SNOOPY_LOG_MESSAGE_BUF_SIZE, "Snoopy output debugging"); + message = malloc(CFG->log_message_max_length+1); + snprintf(message, CFG->log_message_max_length+1, "Snoopy output debugging"); /* Loop throught all outputs and run them with bogus arguments */ iCount = snoopy_outputregistry_getCount(); diff --git a/tests/bin/action-stress-threads.c b/tests/bin/action-stress-threads.c index dccd1c39..934ed6c5 100644 --- a/tests/bin/action-stress-threads.c +++ b/tests/bin/action-stress-threads.c @@ -48,7 +48,7 @@ #define THREAD_COUNT_MAX 10000 - +#define DS_MESSAGE_BUF_SIZE 1024 /* @@ -212,7 +212,7 @@ void* snoopyTestCli_action_stress_threads_threadMain (void *args) int dsId; char *dsName; const char *dsArg = ""; - char dsResult[SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE]; + char dsResult[DS_MESSAGE_BUF_SIZE]; int retVal; @@ -240,7 +240,7 @@ void* snoopyTestCli_action_stress_threads_threadMain (void *args) dsCount = snoopy_datasourceregistry_getCount(); dsId = snoopyTestCli_action_stress_threads_randomNumberInclusive(0, dsCount-1); dsName = snoopy_datasourceregistry_getName(dsId); - retVal = snoopy_datasourceregistry_callById(dsId, dsResult, dsArg); + retVal = snoopy_datasourceregistry_callById(dsId, dsResult, DS_MESSAGE_BUF_SIZE, dsArg); if (0 > retVal) { printf(" t%d %llu : Datasource %s returned negative result: %d\n", seqNrPub, (unsigned long long)pthread_self(), dsName, retVal); diff --git a/tests/bin/action-unit-datasource-cmdline.c b/tests/bin/action-unit-datasource-cmdline.c index c877db04..d08e8d51 100644 --- a/tests/bin/action-unit-datasource-cmdline.c +++ b/tests/bin/action-unit-datasource-cmdline.c @@ -36,6 +36,10 @@ +#define RESULT_BUF_SIZE 2048 + + + /* * Local helper functions */ @@ -109,7 +113,7 @@ int snoopyTestCli_action_unit_datasource_cmdline (int argc, char ** argv) mockDatasourceCmdline("test35", "cmdInFn", (char *[]) {str2045, "1", NULL}, str2045exp); - printSuccess("Mocking src/datasource/cmdline.c complete."); + printSuccess("Mocking src/datasource/cmdline.c complete (@datasource_message_max_length=2047)."); return 0; } @@ -121,7 +125,7 @@ static void mockDatasourceCmdline ( char * const argv[], char const * const expectedResult) { - char resultBuf[SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE] = {'\0'}; + char resultBuf[RESULT_BUF_SIZE] = {'\0'}; char * result = resultBuf; int retVal; @@ -129,7 +133,7 @@ static void mockDatasourceCmdline ( snoopy_entrypoint_test_cli_init(filename, argv, NULL); // Run the datasource - retVal = snoopy_datasource_cmdline(result, NULL); + retVal = snoopy_datasource_cmdline(result, RESULT_BUF_SIZE, NULL); if (SNOOPY_DATASOURCE_FAILED(retVal)) { fatalErrorValue("Datasource failure", expectedResult); } diff --git a/tests/bin/action-unit-util-parser.c b/tests/bin/action-unit-util-parser.c new file mode 100644 index 00000000..445438c6 --- /dev/null +++ b/tests/bin/action-unit-util-parser.c @@ -0,0 +1,92 @@ +/* + * SNOOPY COMMAND LOGGER + * + * Copyright (c) 2015 Bostjan Skufca Jese + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + + + +/* + * Includes order: from local to global + */ +#include "action-unit-util-parser.h" +#include "action-common.h" + +#include "snoopy.h" +#include "util/parser-snoopy.h" + +#include +#include +#include + + + +void snoopyTestCli_action_unit_util_parser_showHelp () +{ + char * helpContent = + "Snoopy TEST SUITE CLI utility :: Action `unit` :: Unit `misc` :: Subunit `parser`\n" + "\n" + "Description:\n" + " Mocks src/unit/parser.c implementation code paths (mainly for the coverage of code parts/paths not covered by the test suite).\n" + "\n" + "Usage:\n" + " snoopy-test unit util parser\n" + " snoopy-test unit util parser --help\n" + "\n"; + printf("%s", helpContent); +} + + + +int snoopyTestCli_action_unit_util_parser (int argc, char ** argv) +{ + const char *arg1; + + if (argc > 0) { + arg1 = argv[0]; + } else { + arg1 = ""; + } + + if (0 == strcmp(arg1, "--help")) { + snoopyTestCli_action_unit_util_parser_showHelp(); + return 0; + } + + + // Do the mocking + if (snoopy_util_parser_strByteLength( "1", 100, 30000, 2000) != 100) fatalErrorValue("Error parsing byte length string", "1"); + if (snoopy_util_parser_strByteLength( "100", 100, 30000, 2000) != 100) fatalErrorValue("Error parsing byte length string", "100"); + if (snoopy_util_parser_strByteLength( "101", 100, 30000, 2000) != 101) fatalErrorValue("Error parsing byte length string", "101"); + if (snoopy_util_parser_strByteLength( "1000", 100, 30000, 2000) != 1000) fatalErrorValue("Error parsing byte length string", "1000"); + if (snoopy_util_parser_strByteLength( "2000", 100, 30000, 2000) != 2000) fatalErrorValue("Error parsing byte length string", "2000"); + if (snoopy_util_parser_strByteLength("29999", 100, 30000, 2000) != 29999) fatalErrorValue("Error parsing byte length string", "29999"); + if (snoopy_util_parser_strByteLength("30000", 100, 30000, 2000) != 30000) fatalErrorValue("Error parsing byte length string", "30000"); + if (snoopy_util_parser_strByteLength("30001", 100, 30000, 2000) != 30000) fatalErrorValue("Error parsing byte length string", "30001"); + + if (snoopy_util_parser_strByteLength( "1k", 100, 3000000, 2000) != 1024) fatalErrorValue("Error parsing byte length string", "1k"); + if (snoopy_util_parser_strByteLength( "3k", 100, 3000000, 2000) != 3072) fatalErrorValue("Error parsing byte length string", "3k"); + if (snoopy_util_parser_strByteLength( "32k", 100, 3000000, 2000) != 32768) fatalErrorValue("Error parsing byte length string", "32k"); + if (snoopy_util_parser_strByteLength( "1m", 100, 3000000, 2000) != 1048576) fatalErrorValue("Error parsing byte length string", "1m"); + + if (snoopy_util_parser_strByteLength("" , 100, 30000, 2000) != 2000) fatalErrorValue("Error parsing byte length string", "(nul)"); + if (snoopy_util_parser_strByteLength("0", 100, 30000, 2000) != 2000) fatalErrorValue("Error parsing byte length string", "0"); + if (snoopy_util_parser_strByteLength("asdf", 100, 30000, 2000) != 2000) fatalErrorValue("Error parsing byte length string", "asdf"); + + printSuccess("Mocking src/util/parser.c (strByteLength-only) complete."); + return 0; +} diff --git a/tests/bin/action-unit-util-parser.h b/tests/bin/action-unit-util-parser.h new file mode 100644 index 00000000..875498c6 --- /dev/null +++ b/tests/bin/action-unit-util-parser.h @@ -0,0 +1,23 @@ +/* + * SNOOPY COMMAND LOGGER + * + * Copyright (c) 2022 Bostjan Skufca Jese + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + + + +int snoopyTestCli_action_unit_util_parser (int argc, char ** argv); diff --git a/tests/bin/action-unit-util.c b/tests/bin/action-unit-util.c index 8a92f1a1..9bb21ccb 100644 --- a/tests/bin/action-unit-util.c +++ b/tests/bin/action-unit-util.c @@ -27,6 +27,7 @@ #include "action-common.h" +#include "action-unit-util-parser.h" #include "action-unit-util-syslog.h" #ifdef SNOOPY_CONF_DATASOURCE_ENABLED_systemd_unit_name #include "action-unit-util-systemd.h" @@ -49,6 +50,7 @@ void snoopyTestCli_action_unit_util_showHelp () " snoopy-test unit util SUBUNIT [ARGS]\n" "\n" "Available subunits:\n" + " parser Run a unit test on src/util/parser.c\n" " syslog Run a unit test on src/util/syslog.c\n" #ifdef SNOOPY_CONF_DATASOURCE_ENABLED_systemd_unit_name " systemd Run a unit test on src/util/systemd.c\n" @@ -77,6 +79,9 @@ int snoopyTestCli_action_unit_util (int argc, char ** argv) subunit = argv[0]; + if (0 == strcmp(subunit, "parser")) { + return snoopyTestCli_action_unit_util_parser(argc-1, &argv[1]); + } if (0 == strcmp(subunit, "syslog")) { return snoopyTestCli_action_unit_util_syslog(argc-1, &argv[1]); } diff --git a/tests/configfile/Makefile.am b/tests/configfile/Makefile.am index a6996b1a..7e6fc9b4 100644 --- a/tests/configfile/Makefile.am +++ b/tests/configfile/Makefile.am @@ -10,6 +10,10 @@ TESTS = XFAIL_TESTS = TESTS += configfile-bom.sh +TESTS += configfile-datasource_message_max_length-min.sh +TESTS += configfile-datasource_message_max_length-purenumeric.sh +TESTS += configfile-datasource_message_max_length-suffix-k.sh +TESTS += configfile-datasource_message_max_length-suffix-m-max.sh TESTS += configfile-error_logging-y1.sh TESTS += configfile-error_logging-y2.sh TESTS += configfile-error_logging-y3.sh @@ -20,6 +24,10 @@ TESTS += configfile-inline-comment.sh TESTS += configfile-line-length-1023.sh TESTS += configfile-line-length-1024.sh XFAIL_TESTS += configfile-line-length-1024.sh +TESTS += configfile-log_message_max_length-min.sh +TESTS += configfile-log_message_max_length-purenumeric.sh +TESTS += configfile-log_message_max_length-suffix-k.sh +TESTS += configfile-log_message_max_length-suffix-m-max.sh if FILTERING_ENABLED TESTS += configfile-filter_chain-1.sh endif diff --git a/tests/configfile/configfile-datasource_message_max_length-min.sh b/tests/configfile/configfile-datasource_message_max_length-min.sh new file mode 100755 index 00000000..2c0f620b --- /dev/null +++ b/tests/configfile/configfile-datasource_message_max_length-min.sh @@ -0,0 +1,23 @@ +#!/bin/bash + + + +### Configure shell and bootstrap +# +set -e +set -u +. `dirname $BASH_SOURCE`/_bootstrap.sh + + + +### Set parameters +# +CONFIG_VARIABLE="datasource_message_max_length" +VAL_CONF="1" +VAL_EXPECTED="255" + + + +### Execute generic test +# +$SNOOPY_CUR_TEST_DIR/configfile-generic.sh "$CONFIG_VARIABLE" "$VAL_CONF" "$VAL_EXPECTED" diff --git a/tests/configfile/configfile-datasource_message_max_length-purenumeric.sh b/tests/configfile/configfile-datasource_message_max_length-purenumeric.sh new file mode 100755 index 00000000..20da38b0 --- /dev/null +++ b/tests/configfile/configfile-datasource_message_max_length-purenumeric.sh @@ -0,0 +1,22 @@ +#!/bin/bash + + + +### Configure shell and bootstrap +# +set -e +set -u +. `dirname $BASH_SOURCE`/_bootstrap.sh + + + +### Set parameters +# +CONFIG_VARIABLE="datasource_message_max_length" +VAL_REAL="1511" + + + +### Execute generic test +# +$SNOOPY_CUR_TEST_DIR/configfile-generic.sh "$CONFIG_VARIABLE" "$VAL_REAL" diff --git a/tests/configfile/configfile-datasource_message_max_length-suffix-k.sh b/tests/configfile/configfile-datasource_message_max_length-suffix-k.sh new file mode 100755 index 00000000..40d88271 --- /dev/null +++ b/tests/configfile/configfile-datasource_message_max_length-suffix-k.sh @@ -0,0 +1,23 @@ +#!/bin/bash + + + +### Configure shell and bootstrap +# +set -e +set -u +. `dirname $BASH_SOURCE`/_bootstrap.sh + + + +### Set parameters +# +CONFIG_VARIABLE="datasource_message_max_length" +VAL_CONF="7k" +VAL_EXPECTED="7168" + + + +### Execute generic test +# +$SNOOPY_CUR_TEST_DIR/configfile-generic.sh "$CONFIG_VARIABLE" "$VAL_CONF" "$VAL_EXPECTED" diff --git a/tests/configfile/configfile-datasource_message_max_length-suffix-m-max.sh b/tests/configfile/configfile-datasource_message_max_length-suffix-m-max.sh new file mode 100755 index 00000000..dae61ab6 --- /dev/null +++ b/tests/configfile/configfile-datasource_message_max_length-suffix-m-max.sh @@ -0,0 +1,23 @@ +#!/bin/bash + + + +### Configure shell and bootstrap +# +set -e +set -u +. `dirname $BASH_SOURCE`/_bootstrap.sh + + + +### Set parameters +# +CONFIG_VARIABLE="datasource_message_max_length" +VAL_CONF="1m" +VAL_EXPECTED="1048575" + + + +### Execute generic test +# +$SNOOPY_CUR_TEST_DIR/configfile-generic.sh "$CONFIG_VARIABLE" "$VAL_CONF" "$VAL_EXPECTED" diff --git a/tests/configfile/configfile-generic.sh b/tests/configfile/configfile-generic.sh index aa8b740b..b82d6f2b 100755 --- a/tests/configfile/configfile-generic.sh +++ b/tests/configfile/configfile-generic.sh @@ -13,14 +13,19 @@ set -u ### Get data # CONFIG_VARIABLE="$1" -VAL_REAL="$2" +VAL_CONF="$2" +if [ ! -z "${3:-}" ]; then + VAL_EXPECTED="$3" +else + VAL_EXPECTED="$VAL_CONF" +fi TMP_INI=$SNOOPY_CUR_TEST_FILENAME.$CONFIG_VARIABLE.$$.ini rm -f $TMP_INI cat > $TMP_INI <