From 7c98e2b345b91d8ef3fb1f7a1bcf74194d54c966 Mon Sep 17 00:00:00 2001 From: RincewindsHat <12514511+RincewindsHat@users.noreply.github.com> Date: Sun, 12 Mar 2023 12:14:41 +0100 Subject: Use default OPENSSL sha functions if available --- lib/utils_base.c | 27 +++++++++++++++++++-------- lib/utils_base.h | 4 +++- 2 files changed, 22 insertions(+), 9 deletions(-) (limited to 'lib') diff --git a/lib/utils_base.c b/lib/utils_base.c index eb1823bb..39032cbd 100644 --- a/lib/utils_base.c +++ b/lib/utils_base.c @@ -402,26 +402,37 @@ int mp_translate_state (char *state_text) { * parse of argv, so that uniqueness in parameters are reflected there. */ char *_np_state_generate_key() { - struct sha256_ctx ctx; int i; char **argv = this_monitoring_plugin->argv; unsigned char result[20]; char keyname[41]; char *p=NULL; +#ifdef USE_OPENSSL + /* + * This code path is chosen if openssl is available (which should be the most common + * scenario). Alternatively, the gnulib implementation/ + * + */ + EVP_MD_CTX *ctx = EVP_MD_CTX_new(); + + EVP_DigestInit(ctx, EVP_sha256()); + + for(i=0; iargc; i++) { + EVP_DigestUpdate(ctx, argv[i], strlen(argv[i])); + } + + EVP_DigestFinalXOF(ctx, &result, 20); +#else + struct sha256_ctx ctx; - sha256_init_ctx(&ctx); - for(i=0; iargc; i++) { sha256_process_bytes(argv[i], strlen(argv[i]), &ctx); } sha256_finish_ctx(&ctx, &result); - - for (i=0; i<20; ++i) { - sprintf(&keyname[2*i], "%02x", result[i]); - } +#endif // FOUNDOPENSSL keyname[40]='\0'; - + p = strdup(keyname); if(p==NULL) { die(STATE_UNKNOWN, _("Cannot execute strdup: %s"), strerror(errno)); diff --git a/lib/utils_base.h b/lib/utils_base.h index 59065504..9cb42767 100644 --- a/lib/utils_base.h +++ b/lib/utils_base.h @@ -2,7 +2,9 @@ #define _UTILS_BASE_ /* Header file for Monitoring Plugins utils_base.c */ -#include "sha256.h" +#ifndef USE_OPENSSL +# include "sha256.h" +#endif /* This file holds header information for thresholds - use this in preference to individual plugin logic */ -- cgit v1.2.3-74-g34f1 From f6f2ba34c713b5bc65936af836be24ebc74faf46 Mon Sep 17 00:00:00 2001 From: RincewindsHat <12514511+RincewindsHat@users.noreply.github.com> Date: Sun, 12 Mar 2023 13:58:25 +0100 Subject: Fix hash creation --- lib/utils_base.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'lib') diff --git a/lib/utils_base.c b/lib/utils_base.c index 39032cbd..105ff44e 100644 --- a/lib/utils_base.c +++ b/lib/utils_base.c @@ -404,9 +404,15 @@ int mp_translate_state (char *state_text) { char *_np_state_generate_key() { int i; char **argv = this_monitoring_plugin->argv; - unsigned char result[20]; char keyname[41]; char *p=NULL; + + unsigned char *result = malloc(256 * sizeof(unsigned char)); + + if (result == NULL) { + die(STATE_UNKNOWN, _("Failed to allocate memory for hashes: %s"), strerror(errno)); + } + #ifdef USE_OPENSSL /* * This code path is chosen if openssl is available (which should be the most common @@ -421,16 +427,22 @@ char *_np_state_generate_key() { EVP_DigestUpdate(ctx, argv[i], strlen(argv[i])); } - EVP_DigestFinalXOF(ctx, &result, 20); + EVP_DigestFinal(ctx, result, NULL); #else + struct sha256_ctx ctx; for(i=0; iargc; i++) { sha256_process_bytes(argv[i], strlen(argv[i]), &ctx); } - sha256_finish_ctx(&ctx, &result); + sha256_finish_ctx(&ctx, result); #endif // FOUNDOPENSSL + + for (i=0; i<20; ++i) { + sprintf(&keyname[2*i], "%02x", result[i]); + } + keyname[40]='\0'; p = strdup(keyname); -- cgit v1.2.3-74-g34f1 From a00c412e7ba1474b32f478daf039d2bdf71f072a Mon Sep 17 00:00:00 2001 From: RincewindsHat <12514511+RincewindsHat@users.noreply.github.com> Date: Sun, 12 Mar 2023 14:59:23 +0100 Subject: Fixes for -Wnonnull-compare --- lib/utils_cmd.c | 4 ---- plugins/runcmd.c | 4 ---- 2 files changed, 8 deletions(-) (limited to 'lib') diff --git a/lib/utils_cmd.c b/lib/utils_cmd.c index 8b8e5708..34fb3909 100644 --- a/lib/utils_cmd.c +++ b/lib/utils_cmd.c @@ -118,10 +118,6 @@ _cmd_open (char *const *argv, int *pfd, int *pfderr) int i = 0; - /* if no command was passed, return with no error */ - if (argv == NULL) - return -1; - if (!_cmd_pids) CMD_INIT; diff --git a/plugins/runcmd.c b/plugins/runcmd.c index 1bd2ca1f..ff1987fd 100644 --- a/plugins/runcmd.c +++ b/plugins/runcmd.c @@ -114,10 +114,6 @@ np_runcmd_open(const char *cmdstring, int *pfd, int *pfderr) env[0] = strdup("LC_ALL=C"); env[1] = '\0'; - /* if no command was passed, return with no error */ - if (cmdstring == NULL) - return -1; - /* make copy of command string so strtok() doesn't silently modify it */ /* (the calling program may want to access it later) */ cmdlen = strlen(cmdstring); -- cgit v1.2.3-74-g34f1 From c5e90822d7db1db504e19007a7078d1fa09267f2 Mon Sep 17 00:00:00 2001 From: RincewindsHat <12514511+RincewindsHat@users.noreply.github.com> Date: Sun, 23 Jul 2023 22:07:33 +0200 Subject: Use memory on stack instead of heap for temporary variables --- lib/utils_base.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) (limited to 'lib') diff --git a/lib/utils_base.c b/lib/utils_base.c index 176fa850..0f521263 100644 --- a/lib/utils_base.c +++ b/lib/utils_base.c @@ -407,11 +407,7 @@ char *_np_state_generate_key() { char keyname[41]; char *p=NULL; - unsigned char *result = malloc(256 * sizeof(unsigned char)); - - if (result == NULL) { - die(STATE_UNKNOWN, _("Failed to allocate memory for hashes: %s"), strerror(errno)); - } + unsigned char result[256]; #ifdef USE_OPENSSL /* -- cgit v1.2.3-74-g34f1 From 7fd0e6f36d90a341e0d9b418f1cd64a3a5472a94 Mon Sep 17 00:00:00 2001 From: RincewindsHat <12514511+RincewindsHat@users.noreply.github.com> Date: Sun, 5 Mar 2023 15:37:47 +0100 Subject: Rework maxfd/open_max to avoid unused variables --- lib/Makefile.am | 2 +- lib/utils_cmd.c | 15 +++++++-------- plugins/common.h | 14 -------------- plugins/popen.c | 8 ++++---- plugins/runcmd.c | 6 ++++-- plugins/utils.c | 16 ---------------- plugins/utils.h | 2 -- 7 files changed, 16 insertions(+), 47 deletions(-) (limited to 'lib') diff --git a/lib/Makefile.am b/lib/Makefile.am index 01d73a64..1a47395d 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -7,7 +7,7 @@ noinst_LIBRARIES = libmonitoringplug.a AM_CPPFLAGS = -DNP_STATE_DIR_PREFIX=\"$(localstatedir)\" \ -I$(srcdir) -I$(top_srcdir)/gl -I$(top_srcdir)/intl -I$(top_srcdir)/plugins -libmonitoringplug_a_SOURCES = utils_base.c utils_disk.c utils_tcp.c utils_cmd.c +libmonitoringplug_a_SOURCES = utils_base.c utils_disk.c utils_tcp.c utils_cmd.c maxfd.c EXTRA_DIST = utils_base.h utils_disk.h utils_tcp.h utils_cmd.h parse_ini.h extra_opts.h if USE_PARSE_INI diff --git a/lib/utils_cmd.c b/lib/utils_cmd.c index 34fb3909..71da9d24 100644 --- a/lib/utils_cmd.c +++ b/lib/utils_cmd.c @@ -43,6 +43,9 @@ #include "utils.h" #include "utils_cmd.h" #include "utils_base.h" + +#include "./maxfd.h" + #include #ifdef HAVE_SYS_WAIT_H @@ -86,13 +89,7 @@ extern void die (int, const char *, ...) void cmd_init (void) { -#ifndef maxfd - if (!maxfd && (maxfd = sysconf (_SC_OPEN_MAX)) < 0) { - /* possibly log or emit a warning here, since there's no - * guarantee that our guess at maxfd will be adequate */ - maxfd = DEFAULT_MAXFD; - } -#endif + long maxfd = open_max(); /* if maxfd is unnaturally high, we force it to a lower value * ( e.g. on SunOS, when ulimit is set to unlimited: 2147483647 this would cause @@ -148,6 +145,7 @@ _cmd_open (char *const *argv, int *pfd, int *pfderr) /* close all descriptors in _cmd_pids[] * This is executed in a separate address space (pure child), * so we don't have to worry about async safety */ + long maxfd = open_max(); for (i = 0; i < maxfd; i++) if (_cmd_pids[i] > 0) close (i); @@ -174,6 +172,7 @@ _cmd_close (int fd) pid_t pid; /* make sure the provided fd was opened */ + long maxfd = open_max(); if (fd < 0 || fd > maxfd || !_cmd_pids || (pid = _cmd_pids[fd]) == 0) return -1; @@ -265,7 +264,6 @@ _cmd_fetch_output (int fd, output * op, int flags) int cmd_run (const char *cmdstring, output * out, output * err, int flags) { - int fd, pfd_out[2], pfd_err[2]; int i = 0, argc; size_t cmdlen; char **argv = NULL; @@ -387,6 +385,7 @@ timeout_alarm_handler (int signo) printf (_("%s - Plugin timed out after %d seconds\n"), state_text(timeout_state), timeout_interval); + long maxfd = open_max(); if(_cmd_pids) for(i = 0; i < maxfd; i++) { if(_cmd_pids[i] != 0) kill(_cmd_pids[i], SIGKILL); } diff --git a/plugins/common.h b/plugins/common.h index 0f08e2f6..6bf4fca4 100644 --- a/plugins/common.h +++ b/plugins/common.h @@ -225,18 +225,4 @@ enum { # define __attribute__(x) /* do nothing */ #endif -/* Try sysconf(_SC_OPEN_MAX) first, as it can be higher than OPEN_MAX. - * If that fails and the macro isn't defined, we fall back to an educated - * guess. There's no guarantee that our guess is adequate and the program - * will die with SIGSEGV if it isn't and the upper boundary is breached. */ -#define DEFAULT_MAXFD 256 /* fallback value if no max open files value is set */ -#define MAXFD_LIMIT 8192 /* upper limit of open files */ -#ifdef _SC_OPEN_MAX -static long maxfd = 0; -#elif defined(OPEN_MAX) -# define maxfd OPEN_MAX -#else /* sysconf macro unavailable, so guess (may be wildly inaccurate) */ -# define maxfd DEFAULT_MAXFD -#endif - #endif /* _COMMON_H_ */ diff --git a/plugins/popen.c b/plugins/popen.c index 723817d5..7703afc8 100644 --- a/plugins/popen.c +++ b/plugins/popen.c @@ -38,8 +38,9 @@ * *****************************************************************************/ -#include "common.h" -#include "utils.h" +#include "./common.h" +#include "./utils.h" +#include "../lib/maxfd.h" /* extern so plugin has pid to kill exec'd process on timeouts */ extern pid_t *childpid; @@ -177,8 +178,7 @@ spopen (const char *cmdstring) } argv[i] = NULL; - if(maxfd == 0) - maxfd = open_max(); + long maxfd = open_max(); if (childpid == NULL) { /* first time through */ if ((childpid = calloc ((size_t)maxfd, sizeof (pid_t))) == NULL) diff --git a/plugins/runcmd.c b/plugins/runcmd.c index 102191e4..98161421 100644 --- a/plugins/runcmd.c +++ b/plugins/runcmd.c @@ -88,8 +88,7 @@ extern void die (int, const char *, ...) * through this api and thus achieve async-safeness throughout the api */ void np_runcmd_init(void) { - if(maxfd == 0) - maxfd = open_max(); + long maxfd = open_max(); if(!np_pids) np_pids = calloc(maxfd, sizeof(pid_t)); } @@ -192,6 +191,7 @@ np_runcmd_open(const char *cmdstring, int *pfd, int *pfderr) /* close all descriptors in np_pids[] * This is executed in a separate address space (pure child), * so we don't have to worry about async safety */ + long maxfd = open_max(); for (i = 0; i < maxfd; i++) if(np_pids[i] > 0) close (i); @@ -219,6 +219,7 @@ np_runcmd_close(int fd) pid_t pid; /* make sure this fd was opened by popen() */ + long maxfd = open_max(); if(fd < 0 || fd > maxfd || !np_pids || (pid = np_pids[fd]) == 0) return -1; @@ -242,6 +243,7 @@ runcmd_timeout_alarm_handler (int signo) if (signo == SIGALRM) puts(_("CRITICAL - Plugin timed out while executing system call")); + long maxfd = open_max(); if(np_pids) for(i = 0; i < maxfd; i++) { if(np_pids[i] != 0) kill(np_pids[i], SIGKILL); } diff --git a/plugins/utils.c b/plugins/utils.c index b4214c61..71c0bdd8 100644 --- a/plugins/utils.c +++ b/plugins/utils.c @@ -804,19 +804,3 @@ char *sperfdata_int (const char *label, return data; } - -int -open_max (void) -{ - errno = 0; - if (maxfd > 0) - return(maxfd); - - if ((maxfd = sysconf (_SC_OPEN_MAX)) < 0) { - if (errno == 0) - maxfd = DEFAULT_MAXFD; /* it's indeterminate */ - else - die (STATE_UNKNOWN, _("sysconf error for _SC_OPEN_MAX\n")); - } - return(maxfd); -} diff --git a/plugins/utils.h b/plugins/utils.h index c76b3216..cb979ce7 100644 --- a/plugins/utils.h +++ b/plugins/utils.h @@ -106,8 +106,6 @@ char *sperfdata (const char *, double, const char *, char *, char *, char *sperfdata_int (const char *, int, const char *, char *, char *, int, int, int, int); -int open_max (void); - /* The idea here is that, although not every plugin will use all of these, most will or should. Therefore, for consistency, these very common options should have only these meanings throughout the overall suite */ -- cgit v1.2.3-74-g34f1 From 0162cb2d4f7040e3b2d48095182f87ce565866a5 Mon Sep 17 00:00:00 2001 From: RincewindsHat <12514511+RincewindsHat@users.noreply.github.com> Date: Sun, 5 Mar 2023 16:03:37 +0100 Subject: fixup! Rework maxfd/open_max to avoid unused variables --- lib/maxfd.c | 26 ++++++++++++++++++++++++++ lib/maxfd.h | 9 +++++++++ 2 files changed, 35 insertions(+) create mode 100644 lib/maxfd.c create mode 100644 lib/maxfd.h (limited to 'lib') diff --git a/lib/maxfd.c b/lib/maxfd.c new file mode 100644 index 00000000..dcd4d3db --- /dev/null +++ b/lib/maxfd.c @@ -0,0 +1,26 @@ +#include "./maxfd.h" +#include + +long open_max (void) { + long maxfd = 0L; + /* Try sysconf(_SC_OPEN_MAX) first, as it can be higher than OPEN_MAX. + * If that fails and the macro isn't defined, we fall back to an educated + * guess. There's no guarantee that our guess is adequate and the program + * will die with SIGSEGV if it isn't and the upper boundary is breached. */ + +#ifdef _SC_OPEN_MAX + errno = 0; + if ((maxfd = sysconf (_SC_OPEN_MAX)) < 0) { + if (errno == 0) + maxfd = DEFAULT_MAXFD; /* it's indeterminate */ + else + die (STATE_UNKNOWN, _("sysconf error for _SC_OPEN_MAX\n")); + } +#elif defined(OPEN_MAX) + return OPEN_MAX +#else /* sysconf macro unavailable, so guess (may be wildly inaccurate) */ + return DEFAULT_MAXFD; +#endif + + return(maxfd); +} diff --git a/lib/maxfd.h b/lib/maxfd.h new file mode 100644 index 00000000..0d734c5c --- /dev/null +++ b/lib/maxfd.h @@ -0,0 +1,9 @@ +#ifndef _MAXFD_ +#define _MAXFD_ + +#define DEFAULT_MAXFD 256 /* fallback value if no max open files value is set */ +#define MAXFD_LIMIT 8192 /* upper limit of open files */ + +long open_max (void); + +#endif // _MAXFD_ -- cgit v1.2.3-74-g34f1 From a3029c5a2e71d4aa4955901f282d3f4669acf97d Mon Sep 17 00:00:00 2001 From: RincewindsHat <12514511+RincewindsHat@users.noreply.github.com> Date: Sun, 5 Mar 2023 15:47:49 +0100 Subject: Place _cmd_pids in object not header to avoid unsused variables --- lib/utils_cmd.c | 10 ++++++++++ lib/utils_cmd.h | 9 --------- 2 files changed, 10 insertions(+), 9 deletions(-) (limited to 'lib') diff --git a/lib/utils_cmd.c b/lib/utils_cmd.c index 34fb3909..883f1ec1 100644 --- a/lib/utils_cmd.c +++ b/lib/utils_cmd.c @@ -42,6 +42,16 @@ #include "common.h" #include "utils.h" #include "utils_cmd.h" +/* This variable must be global, since there's no way the caller + * can forcibly slay a dead or ungainly running program otherwise. + * Multithreading apps and plugins can initialize it (via CMD_INIT) + * in an async safe manner PRIOR to calling cmd_run() or cmd_run_array() + * for the first time. + * + * The check for initialized values is atomic and can + * occur in any number of threads simultaneously. */ +static pid_t *_cmd_pids = NULL; + #include "utils_base.h" #include diff --git a/lib/utils_cmd.h b/lib/utils_cmd.h index 6f3aeb81..1fc2968c 100644 --- a/lib/utils_cmd.h +++ b/lib/utils_cmd.h @@ -32,15 +32,6 @@ void cmd_init (void); #define CMD_NO_ARRAYS 0x01 /* don't populate arrays at all */ #define CMD_NO_ASSOC 0x02 /* output.line won't point to buf */ -/* This variable must be global, since there's no way the caller - * can forcibly slay a dead or ungainly running program otherwise. - * Multithreading apps and plugins can initialize it (via CMD_INIT) - * in an async safe manner PRIOR to calling cmd_run() or cmd_run_array() - * for the first time. - * - * The check for initialized values is atomic and can - * occur in any number of threads simultaneously. */ -static pid_t *_cmd_pids = NULL; RETSIGTYPE timeout_alarm_handler (int); -- cgit v1.2.3-74-g34f1 From 4295decfbf06adfa1bf019d28e9044971607b2d6 Mon Sep 17 00:00:00 2001 From: RincewindsHat <12514511+RincewindsHat@users.noreply.github.com> Date: Sat, 23 Sep 2023 10:33:06 +0200 Subject: open_max is a library function now, it should be mp_open_max --- lib/maxfd.c | 2 +- lib/maxfd.h | 2 +- lib/utils_cmd.c | 8 ++++---- plugins/popen.c | 2 +- plugins/runcmd.c | 8 ++++---- 5 files changed, 11 insertions(+), 11 deletions(-) (limited to 'lib') diff --git a/lib/maxfd.c b/lib/maxfd.c index dcd4d3db..529b3568 100644 --- a/lib/maxfd.c +++ b/lib/maxfd.c @@ -1,7 +1,7 @@ #include "./maxfd.h" #include -long open_max (void) { +long mp_open_max (void) { long maxfd = 0L; /* Try sysconf(_SC_OPEN_MAX) first, as it can be higher than OPEN_MAX. * If that fails and the macro isn't defined, we fall back to an educated diff --git a/lib/maxfd.h b/lib/maxfd.h index 0d734c5c..45218d0f 100644 --- a/lib/maxfd.h +++ b/lib/maxfd.h @@ -4,6 +4,6 @@ #define DEFAULT_MAXFD 256 /* fallback value if no max open files value is set */ #define MAXFD_LIMIT 8192 /* upper limit of open files */ -long open_max (void); +long mp_open_max (void); #endif // _MAXFD_ diff --git a/lib/utils_cmd.c b/lib/utils_cmd.c index 71da9d24..ef7053a5 100644 --- a/lib/utils_cmd.c +++ b/lib/utils_cmd.c @@ -89,7 +89,7 @@ extern void die (int, const char *, ...) void cmd_init (void) { - long maxfd = open_max(); + long maxfd = mp_open_max(); /* if maxfd is unnaturally high, we force it to a lower value * ( e.g. on SunOS, when ulimit is set to unlimited: 2147483647 this would cause @@ -145,7 +145,7 @@ _cmd_open (char *const *argv, int *pfd, int *pfderr) /* close all descriptors in _cmd_pids[] * This is executed in a separate address space (pure child), * so we don't have to worry about async safety */ - long maxfd = open_max(); + long maxfd = mp_open_max(); for (i = 0; i < maxfd; i++) if (_cmd_pids[i] > 0) close (i); @@ -172,7 +172,7 @@ _cmd_close (int fd) pid_t pid; /* make sure the provided fd was opened */ - long maxfd = open_max(); + long maxfd = mp_open_max(); if (fd < 0 || fd > maxfd || !_cmd_pids || (pid = _cmd_pids[fd]) == 0) return -1; @@ -385,7 +385,7 @@ timeout_alarm_handler (int signo) printf (_("%s - Plugin timed out after %d seconds\n"), state_text(timeout_state), timeout_interval); - long maxfd = open_max(); + long maxfd = mp_open_max(); if(_cmd_pids) for(i = 0; i < maxfd; i++) { if(_cmd_pids[i] != 0) kill(_cmd_pids[i], SIGKILL); } diff --git a/plugins/popen.c b/plugins/popen.c index 7703afc8..b395f14a 100644 --- a/plugins/popen.c +++ b/plugins/popen.c @@ -178,7 +178,7 @@ spopen (const char *cmdstring) } argv[i] = NULL; - long maxfd = open_max(); + long maxfd = mp_open_max(); if (childpid == NULL) { /* first time through */ if ((childpid = calloc ((size_t)maxfd, sizeof (pid_t))) == NULL) diff --git a/plugins/runcmd.c b/plugins/runcmd.c index 98161421..bc0a4974 100644 --- a/plugins/runcmd.c +++ b/plugins/runcmd.c @@ -88,7 +88,7 @@ extern void die (int, const char *, ...) * through this api and thus achieve async-safeness throughout the api */ void np_runcmd_init(void) { - long maxfd = open_max(); + long maxfd = mp_open_max(); if(!np_pids) np_pids = calloc(maxfd, sizeof(pid_t)); } @@ -191,7 +191,7 @@ np_runcmd_open(const char *cmdstring, int *pfd, int *pfderr) /* close all descriptors in np_pids[] * This is executed in a separate address space (pure child), * so we don't have to worry about async safety */ - long maxfd = open_max(); + long maxfd = mp_open_max(); for (i = 0; i < maxfd; i++) if(np_pids[i] > 0) close (i); @@ -219,7 +219,7 @@ np_runcmd_close(int fd) pid_t pid; /* make sure this fd was opened by popen() */ - long maxfd = open_max(); + long maxfd = mp_open_max(); if(fd < 0 || fd > maxfd || !np_pids || (pid = np_pids[fd]) == 0) return -1; @@ -243,7 +243,7 @@ runcmd_timeout_alarm_handler (int signo) if (signo == SIGALRM) puts(_("CRITICAL - Plugin timed out while executing system call")); - long maxfd = open_max(); + long maxfd = mp_open_max(); if(np_pids) for(i = 0; i < maxfd; i++) { if(np_pids[i] != 0) kill(np_pids[i], SIGKILL); } -- cgit v1.2.3-74-g34f1 From 513929d796af668e977ca7981800c259304a2f25 Mon Sep 17 00:00:00 2001 From: RincewindsHat <12514511+RincewindsHat@users.noreply.github.com> Date: Sat, 23 Sep 2023 12:31:33 +0200 Subject: Remove check for RETSIGTYPE in autoconf stuff autoupdate tells me, that since C89 I can safely assume RETSIGTYPE is void. Therefore to simplify things I removed the corresponding configure.ac line and replaced all mentions of RETSIGTYPE with void. --- configure.ac | 1 - lib/utils_cmd.h | 2 +- plugins/netutils.h | 2 +- plugins/popen.c | 8 ++++---- plugins/popen.h | 2 +- 5 files changed, 7 insertions(+), 8 deletions(-) (limited to 'lib') diff --git a/configure.ac b/configure.ac index a294b00f..e6a40d3f 100644 --- a/configure.ac +++ b/configure.ac @@ -621,7 +621,6 @@ AC_C_CONST AC_STRUCT_TM AC_TYPE_PID_T AC_TYPE_SIZE_T -AC_TYPE_SIGNAL AC_CACHE_CHECK([for va_copy],ac_cv_HAVE_VA_COPY,[ AC_TRY_LINK([#include diff --git a/lib/utils_cmd.h b/lib/utils_cmd.h index 1fc2968c..f1b06c82 100644 --- a/lib/utils_cmd.h +++ b/lib/utils_cmd.h @@ -33,7 +33,7 @@ void cmd_init (void); #define CMD_NO_ASSOC 0x02 /* output.line won't point to buf */ -RETSIGTYPE timeout_alarm_handler (int); +void timeout_alarm_handler (int); #endif /* _UTILS_CMD_ */ diff --git a/plugins/netutils.h b/plugins/netutils.h index d7ee0ddd..ea653e72 100644 --- a/plugins/netutils.h +++ b/plugins/netutils.h @@ -92,7 +92,7 @@ extern int econn_refuse_state; extern int was_refused; extern int address_family; -RETSIGTYPE socket_timeout_alarm_handler (int) __attribute__((noreturn)); +void socket_timeout_alarm_handler (int) __attribute__((noreturn)); /* SSL-Related functionality */ #ifdef HAVE_SSL diff --git a/plugins/popen.c b/plugins/popen.c index b395f14a..036bc608 100644 --- a/plugins/popen.c +++ b/plugins/popen.c @@ -50,9 +50,9 @@ extern FILE *child_process; FILE *spopen (const char *); int spclose (FILE *); #ifdef REDHAT_SPOPEN_ERROR -RETSIGTYPE popen_sigchld_handler (int); +void popen_sigchld_handler (int); #endif -RETSIGTYPE popen_timeout_alarm_handler (int); +void popen_timeout_alarm_handler (int); #include /* ANSI C header file */ #include @@ -266,7 +266,7 @@ spclose (FILE * fp) } #ifdef REDHAT_SPOPEN_ERROR -RETSIGTYPE +void popen_sigchld_handler (int signo) { if (signo == SIGCHLD) @@ -274,7 +274,7 @@ popen_sigchld_handler (int signo) } #endif -RETSIGTYPE +void popen_timeout_alarm_handler (int signo) { int fh; diff --git a/plugins/popen.h b/plugins/popen.h index a5dd8fa7..1ea69632 100644 --- a/plugins/popen.h +++ b/plugins/popen.h @@ -5,7 +5,7 @@ FILE *spopen (const char *); int spclose (FILE *); -RETSIGTYPE popen_timeout_alarm_handler (int); +void popen_timeout_alarm_handler (int); pid_t *childpid=NULL; int *child_stderr_array=NULL; -- cgit v1.2.3-74-g34f1 From f457615d845ec3bf9d3072511999b69d93c934c5 Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Wed, 23 Aug 2023 17:33:16 +0200 Subject: Introduce regex_list --- lib/utils_disk.h | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'lib') diff --git a/lib/utils_disk.h b/lib/utils_disk.h index 3b5a45f8..442fd94f 100644 --- a/lib/utils_disk.h +++ b/lib/utils_disk.h @@ -10,6 +10,12 @@ struct name_list struct name_list *next; }; +struct regex_list +{ + regex_t regex; + struct regex_list *next; +}; + struct parameter_list { char *name; -- cgit v1.2.3-74-g34f1 From d31a696cadb0bba0914d76aad0eb48c6e7962b8e Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Wed, 23 Aug 2023 18:13:01 +0200 Subject: Introduce np_add_regex() --- lib/utils_disk.c | 11 +++++++++++ lib/utils_disk.h | 1 + 2 files changed, 12 insertions(+) (limited to 'lib') diff --git a/lib/utils_disk.c b/lib/utils_disk.c index 582d3ea1..ce02fdff 100644 --- a/lib/utils_disk.c +++ b/lib/utils_disk.c @@ -40,6 +40,17 @@ np_add_name (struct name_list **list, const char *name) *list = new_entry; } +/* Initialises a new regex at the begin of list via regcomp(3) */ +int +np_add_regex (struct regex_list **list, const char *regex, int cflags) +{ + struct regex_list *new_entry = (struct regex_list *) malloc (sizeof *new_entry); + new_entry->next = *list; + *list = new_entry; + + return regcomp(&new_entry->regex, regex, cflags); +} + /* Initialises a new parameter at the end of list */ struct parameter_list * np_add_parameter(struct parameter_list **list, const char *name) diff --git a/lib/utils_disk.h b/lib/utils_disk.h index 442fd94f..bda088f6 100644 --- a/lib/utils_disk.h +++ b/lib/utils_disk.h @@ -41,6 +41,7 @@ struct parameter_list void np_add_name (struct name_list **list, const char *name); int np_find_name (struct name_list *list, const char *name); int np_seen_name (struct name_list *list, const char *name); +int np_add_regex (struct regex_list **list, const char *regex, int cflags); struct parameter_list *np_add_parameter(struct parameter_list **list, const char *name); struct parameter_list *np_find_parameter(struct parameter_list *list, const char *name); struct parameter_list *np_del_parameter(struct parameter_list *item, struct parameter_list *prev); -- cgit v1.2.3-74-g34f1 From 1f694195b4a6beef30bbbbffa5835a993be97a9c Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Wed, 23 Aug 2023 18:26:12 +0200 Subject: Introduce np_find_regmatch() --- lib/utils_disk.c | 25 +++++++++++++++++++++++++ lib/utils_disk.h | 1 + 2 files changed, 26 insertions(+) (limited to 'lib') diff --git a/lib/utils_disk.c b/lib/utils_disk.c index ce02fdff..34401e21 100644 --- a/lib/utils_disk.c +++ b/lib/utils_disk.c @@ -29,6 +29,7 @@ #include "common.h" #include "utils_disk.h" #include "gl/fsusage.h" +#include void np_add_name (struct name_list **list, const char *name) @@ -207,6 +208,30 @@ np_find_name (struct name_list *list, const char *name) return FALSE; } +/* Returns TRUE if name is in list */ +bool +np_find_regmatch (struct regex_list *list, const char *name) +{ + int len; + regmatch_t m; + + if (name == NULL) { + return false; + } + + len = strlen(name); + + for (; list; list = list->next) { + /* Emulate a full match as if surrounded with ^( )$ + by checking whether the match spans the whole name */ + if (!regexec(&list->regex, name, 1, &m, 0) && m.rm_so == 0 && m.rm_eo == len) { + return true; + } + } + + return false; +} + int np_seen_name(struct name_list *list, const char *name) { diff --git a/lib/utils_disk.h b/lib/utils_disk.h index bda088f6..6b83ac74 100644 --- a/lib/utils_disk.h +++ b/lib/utils_disk.h @@ -42,6 +42,7 @@ void np_add_name (struct name_list **list, const char *name); int np_find_name (struct name_list *list, const char *name); int np_seen_name (struct name_list *list, const char *name); int np_add_regex (struct regex_list **list, const char *regex, int cflags); +bool np_find_regmatch (struct regex_list *list, const char *name); struct parameter_list *np_add_parameter(struct parameter_list **list, const char *name); struct parameter_list *np_find_parameter(struct parameter_list *list, const char *name); struct parameter_list *np_del_parameter(struct parameter_list *item, struct parameter_list *prev); -- cgit v1.2.3-74-g34f1 From 51aa8b2d9d3812b74fb4d15da712a31d549d928b Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Sat, 30 Sep 2023 12:55:49 +0200 Subject: Document new np_add_regex more and add error handling --- lib/utils_disk.c | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) (limited to 'lib') diff --git a/lib/utils_disk.c b/lib/utils_disk.c index 34401e21..884f0052 100644 --- a/lib/utils_disk.c +++ b/lib/utils_disk.c @@ -41,15 +41,40 @@ np_add_name (struct name_list **list, const char *name) *list = new_entry; } -/* Initialises a new regex at the begin of list via regcomp(3) */ +/* @brief Initialises a new regex at the begin of list via regcomp(3) + * + * @details if the regex fails to compile the error code of regcomp(3) is returned + * and list is not modified, otherwise list is modified to point to the new + * element + * @param list Pointer to a linked list of regex_list elements + * @param regex the string containing the regex which should be inserted into the list + * @param clags the cflags parameter for regcomp(3) + */ int np_add_regex (struct regex_list **list, const char *regex, int cflags) { struct regex_list *new_entry = (struct regex_list *) malloc (sizeof *new_entry); - new_entry->next = *list; - *list = new_entry; - return regcomp(&new_entry->regex, regex, cflags); + if (new_entry == NULL) { + die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), + strerror(errno)); + } + + int regcomp_result = regcomp(&new_entry->regex, regex, cflags); + + if (!regcomp_result) { + // regcomp succeded + new_entry->next = *list; + *list = new_entry; + + return 0; + } else { + // regcomp failed + free(new_entry); + + return regcomp_result; + } + } /* Initialises a new parameter at the end of list */ -- cgit v1.2.3-74-g34f1 From 128a24be2296af175c5e7adf875f9d0e8a619278 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Sat, 30 Sep 2023 12:59:26 +0200 Subject: Fix typo --- lib/utils_disk.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/utils_disk.c b/lib/utils_disk.c index 884f0052..f5ac0b30 100644 --- a/lib/utils_disk.c +++ b/lib/utils_disk.c @@ -63,7 +63,7 @@ np_add_regex (struct regex_list **list, const char *regex, int cflags) int regcomp_result = regcomp(&new_entry->regex, regex, cflags); if (!regcomp_result) { - // regcomp succeded + // regcomp succeeded new_entry->next = *list; *list = new_entry; -- cgit v1.2.3-74-g34f1