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 From 4b9d90f31c700298185aa4c7b20fe1c5e8bf19c2 Mon Sep 17 00:00:00 2001 From: RincewindsHat <12514511+RincewindsHat@users.noreply.github.com> Date: Sun, 15 Oct 2023 18:17:36 +0200 Subject: Whitespace fixes in lib --- lib/extra_opts.c | 12 ++++++------ lib/parse_ini.c | 14 +++++++------- lib/tests/test_base64.c | 10 +++++----- lib/tests/test_cmd.c | 10 +++++----- lib/tests/test_disk.c | 18 +++++++++--------- lib/tests/test_ini1.c | 10 +++++----- lib/tests/test_ini3.c | 10 +++++----- lib/tests/test_opts2.c | 2 +- lib/tests/test_tcp.c | 14 +++++++------- lib/tests/test_utils.c | 21 ++++++++++----------- lib/utils_base.c | 24 ++++++++++++------------ lib/utils_base.h | 4 ++-- lib/utils_cmd.c | 12 ++++++------ lib/utils_cmd.h | 6 +++--- lib/utils_disk.c | 23 +++++++++++------------ lib/utils_tcp.c | 20 ++++++++++---------- 16 files changed, 104 insertions(+), 106 deletions(-) (limited to 'lib') diff --git a/lib/extra_opts.c b/lib/extra_opts.c index f4d5e66a..89b10560 100644 --- a/lib/extra_opts.c +++ b/lib/extra_opts.c @@ -1,23 +1,23 @@ /***************************************************************************** -* +* * Monitoring Plugins extra_opts library -* +* * License: GPL * Copyright (c) 2007 Monitoring Plugins Development Team -* +* * 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 3 of the License, 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, see . -* +* *****************************************************************************/ #include "common.h" diff --git a/lib/parse_ini.c b/lib/parse_ini.c index 547af433..57e60942 100644 --- a/lib/parse_ini.c +++ b/lib/parse_ini.c @@ -1,24 +1,24 @@ /***************************************************************************** -* +* * Monitoring Plugins parse_ini library -* +* * License: GPL * Copyright (c) 2007 Monitoring Plugins Development Team -* +* * 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 3 of the License, 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, see . -* -* +* +* *****************************************************************************/ #include "common.h" diff --git a/lib/tests/test_base64.c b/lib/tests/test_base64.c index 5103d10d..05dd7943 100644 --- a/lib/tests/test_base64.c +++ b/lib/tests/test_base64.c @@ -1,19 +1,19 @@ /***************************************************************************** -* +* * 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 3 of the License, 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, see . -* -* +* +* *****************************************************************************/ #include "common.h" diff --git a/lib/tests/test_cmd.c b/lib/tests/test_cmd.c index 4bb60aac..02ae11f5 100644 --- a/lib/tests/test_cmd.c +++ b/lib/tests/test_cmd.c @@ -1,19 +1,19 @@ /***************************************************************************** -* +* * 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 3 of the License, 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, see . -* -* +* +* *****************************************************************************/ #include "common.h" diff --git a/lib/tests/test_disk.c b/lib/tests/test_disk.c index 9bd68c7a..269d20b6 100644 --- a/lib/tests/test_disk.c +++ b/lib/tests/test_disk.c @@ -1,19 +1,19 @@ /***************************************************************************** -* +* * 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 3 of the License, 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, see . -* -* +* +* *****************************************************************************/ #include "common.h" @@ -167,7 +167,7 @@ main (int argc, char **argv) } ok(found == 0, "first element successfully deleted"); found = 0; - + p=paths; while (p) { if (! strcmp(p->name, "/tmp")) @@ -203,9 +203,9 @@ main (int argc, char **argv) } -void +void np_test_mount_entry_regex (struct mount_entry *dummy_mount_list, char *regstr, int cflags, int expect, char *desc) -{ +{ int matches = 0; regex_t re; struct mount_entry *me; @@ -214,7 +214,7 @@ np_test_mount_entry_regex (struct mount_entry *dummy_mount_list, char *regstr, i if(np_regex_match_mount_entry(me,&re)) matches++; } - ok( matches == expect, + ok( matches == expect, "%s '%s' matched %i/3 entries. ok: %i/3", desc, regstr, expect, matches); diff --git a/lib/tests/test_ini1.c b/lib/tests/test_ini1.c index 77f88549..6843bac2 100644 --- a/lib/tests/test_ini1.c +++ b/lib/tests/test_ini1.c @@ -1,19 +1,19 @@ /***************************************************************************** -* +* * 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 3 of the License, 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, see . -* -* +* +* *****************************************************************************/ #include "common.h" diff --git a/lib/tests/test_ini3.c b/lib/tests/test_ini3.c index 814b3ec0..8a2a0414 100644 --- a/lib/tests/test_ini3.c +++ b/lib/tests/test_ini3.c @@ -1,19 +1,19 @@ /***************************************************************************** -* +* * 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 3 of the License, 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, see . -* -* +* +* *****************************************************************************/ #include "parse_ini.h" diff --git a/lib/tests/test_opts2.c b/lib/tests/test_opts2.c index c3d2067f..780220ee 100644 --- a/lib/tests/test_opts2.c +++ b/lib/tests/test_opts2.c @@ -12,7 +12,7 @@ * * You should have received a copy of the GNU General Public License * along with this program. If not, see . -* +* *****************************************************************************/ #include "common.h" diff --git a/lib/tests/test_tcp.c b/lib/tests/test_tcp.c index 114252b4..1954b0fb 100644 --- a/lib/tests/test_tcp.c +++ b/lib/tests/test_tcp.c @@ -1,19 +1,19 @@ /***************************************************************************** -* +* * 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 3 of the License, 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, see . -* -* +* +* *****************************************************************************/ #include "common.h" @@ -33,7 +33,7 @@ main(void) server_expect[0] = strdup("AA"); server_expect[1] = strdup("bb"); server_expect[2] = strdup("CC"); - + ok(np_expect_match("AA bb CC XX", server_expect, server_expect_count, NP_MATCH_EXACT) == NP_MATCH_SUCCESS, "Test matching any string at the beginning (first expect string)"); ok(np_expect_match("bb AA CC XX", server_expect, server_expect_count, NP_MATCH_EXACT) == NP_MATCH_SUCCESS, @@ -52,7 +52,7 @@ main(void) "Test not matching all strings"); ok(np_expect_match("XX XX", server_expect, server_expect_count, NP_MATCH_ALL) == NP_MATCH_RETRY, "Test not matching any string (testing all)"); - + return exit_status(); } diff --git a/lib/tests/test_utils.c b/lib/tests/test_utils.c index 7b10494c..a109e3f4 100644 --- a/lib/tests/test_utils.c +++ b/lib/tests/test_utils.c @@ -1,19 +1,19 @@ /***************************************************************************** -* +* * 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 3 of the License, 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, see . -* -* +* +* *****************************************************************************/ #include "common.h" @@ -377,13 +377,13 @@ main (int argc, char **argv) /* temp_fp = fopen("var/statefile", "r"); - if (temp_fp==NULL) + if (temp_fp==NULL) printf("Error opening. errno=%d\n", errno); printf("temp_fp=%s\n", temp_fp); - ok( _np_state_read_file(temp_fp) == TRUE, "Can read state file" ); + ok( _np_state_read_file(temp_fp) == true, "Can read state file" ); fclose(temp_fp); */ - + temp_state_key->_filename="var/statefile"; temp_state_data = np_state_read(); ok( this_monitoring_plugin->state->state_data!=NULL, "Got state data now" ) || diag("Are you running in right directory? Will get coredump next if not"); @@ -446,14 +446,14 @@ main (int argc, char **argv) /* Check time is set to current_time */ ok(system("cmp var/generated var/statefile > /dev/null")!=0, "Generated file should be different this time"); ok(this_monitoring_plugin->state->state_data->time-current_time<=1, "Has time generated from current time"); - + /* Don't know how to automatically test this. Need to be able to redefine die and catch the error */ /* temp_state_key->_filename="/dev/do/not/expect/to/be/able/to/write"; np_state_write_string(0, "Bad file"); */ - + np_cleanup(); @@ -508,4 +508,3 @@ main (int argc, char **argv) return exit_status(); } - diff --git a/lib/utils_base.c b/lib/utils_base.c index 0f521263..eabcd7ee 100644 --- a/lib/utils_base.c +++ b/lib/utils_base.c @@ -6,21 +6,21 @@ * Copyright (c) 2006 Monitoring Plugins Development Team * * Library of useful functions for plugins -* +* * * 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 3 of the License, 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, see . -* +* * *****************************************************************************/ @@ -640,10 +640,10 @@ int _np_state_read_file(FILE *f) { } /* - * If time=NULL, use current time. Create state file, with state format - * version, default text. Writes version, time, and data. Avoid locking - * problems - use mv to write and then swap. Possible loss of state data if - * two things writing to same key at same time. + * If time=NULL, use current time. Create state file, with state format + * version, default text. Writes version, time, and data. Avoid locking + * problems - use mv to write and then swap. Possible loss of state data if + * two things writing to same key at same time. * Will die with UNKNOWN if errors */ void np_state_write_string(time_t data_time, char *data_string) { @@ -658,7 +658,7 @@ void np_state_write_string(time_t data_time, char *data_string) { time(¤t_time); else current_time=data_time; - + /* If file doesn't currently exist, create directories */ if(access(this_monitoring_plugin->state->_filename,F_OK)!=0) { result = asprintf(&directories, "%s", this_monitoring_plugin->state->_filename); @@ -697,15 +697,15 @@ void np_state_write_string(time_t data_time, char *data_string) { np_free(temp_file); die(STATE_UNKNOWN, _("Unable to open temporary state file")); } - + fprintf(fp,"# NP State file\n"); fprintf(fp,"%d\n",NP_STATE_FORMAT_VERSION); fprintf(fp,"%d\n",this_monitoring_plugin->state->data_version); fprintf(fp,"%lu\n",current_time); fprintf(fp,"%s\n",data_string); - + fchmod(fd, S_IRUSR | S_IWUSR | S_IRGRP); - + fflush(fp); result=fclose(fp); diff --git a/lib/utils_base.h b/lib/utils_base.h index 9cb42767..9abf5957 100644 --- a/lib/utils_base.h +++ b/lib/utils_base.h @@ -6,7 +6,7 @@ # include "sha256.h" #endif -/* This file holds header information for thresholds - use this in preference to +/* This file holds header information for thresholds - use this in preference to individual plugin logic */ /* This has not been merged with utils.h because of problems with @@ -79,7 +79,7 @@ void die (int, const char *, ...) __attribute__((noreturn,format(printf, 2, 3))) #define NP_RANGE_UNPARSEABLE 1 #define NP_WARN_WITHIN_CRIT 2 -/* a simple check to see if we're running as root. +/* a simple check to see if we're running as root. * returns zero on failure, nonzero on success */ int np_check_if_root(void); diff --git a/lib/utils_cmd.c b/lib/utils_cmd.c index f66fd573..cfb2073c 100644 --- a/lib/utils_cmd.c +++ b/lib/utils_cmd.c @@ -18,18 +18,18 @@ * Care has been taken to make sure the functions are async-safe. The one * function which isn't is cmd_init() which it doesn't make sense to * call twice anyway, so the api as a whole should be considered async-safe. -* -* +* +* * 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 3 of the License, 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, see . * @@ -377,10 +377,10 @@ cmd_file_read ( char *filename, output *out, int flags) if ((fd = open(filename, O_RDONLY)) == -1) { die( STATE_UNKNOWN, _("Error opening %s: %s"), filename, strerror(errno) ); } - + if(out) out->lines = _cmd_fetch_output (fd, out, flags); - + if (close(fd) == -1) die( STATE_UNKNOWN, _("Error closing %s: %s"), filename, strerror(errno) ); diff --git a/lib/utils_cmd.h b/lib/utils_cmd.h index f1b06c82..061f5d4f 100644 --- a/lib/utils_cmd.h +++ b/lib/utils_cmd.h @@ -1,10 +1,10 @@ #ifndef _UTILS_CMD_ #define _UTILS_CMD_ -/* +/* * Header file for Monitoring Plugins utils_cmd.c - * - * + * + * */ /** types **/ diff --git a/lib/utils_disk.c b/lib/utils_disk.c index f5ac0b30..5e95aef6 100644 --- a/lib/utils_disk.c +++ b/lib/utils_disk.c @@ -1,29 +1,29 @@ /***************************************************************************** -* +* * Library for check_disk -* +* * License: GPL * Copyright (c) 1999-2007 Monitoring Plugins Development Team -* +* * Description: -* +* * This file contains utilities for check_disk. These are tested by libtap -* -* +* +* * 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 3 of the License, 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, see . -* -* +* +* *****************************************************************************/ #include "common.h" @@ -98,7 +98,7 @@ np_add_parameter(struct parameter_list **list, const char *name) new_path->freeinodes_percent = NULL; new_path->group = NULL; new_path->dfree_pct = -1; - new_path->dused_pct = -1; + new_path->dused_pct = -1; new_path->total = 0; new_path->available = 0; new_path->available_to_root = 0; @@ -279,4 +279,3 @@ np_regex_match_mount_entry (struct mount_entry* me, regex_t* re) return FALSE; } } - diff --git a/lib/utils_tcp.c b/lib/utils_tcp.c index b37c446f..23ee4a95 100644 --- a/lib/utils_tcp.c +++ b/lib/utils_tcp.c @@ -1,29 +1,29 @@ /***************************************************************************** -* +* * Library for check_tcp -* +* * License: GPL * Copyright (c) 1999-2013 Monitoring Plugins Development Team -* +* * Description: -* +* * This file contains utilities for check_tcp. These are tested by libtap -* -* +* +* * 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 3 of the License, 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, see . -* -* +* +* *****************************************************************************/ #include "common.h" -- cgit v1.2.3-74-g34f1 From ddbabaa3b659bed9dcf5c5a2bfc430fb816277c7 Mon Sep 17 00:00:00 2001 From: RincewindsHat <12514511+RincewindsHat@users.noreply.github.com> Date: Sun, 15 Oct 2023 18:21:31 +0200 Subject: Replace all old school booleans in lib witch C99 ones --- lib/extra_opts.c | 9 +++--- lib/parse_ini.c | 7 +++-- lib/tests/test_disk.c | 20 ++++++------- lib/tests/test_utils.c | 76 +++++++++++++++++++++++++------------------------- lib/utils_base.c | 51 +++++++++++++++++---------------- lib/utils_base.h | 4 +-- lib/utils_disk.c | 43 +++++++++++----------------- lib/utils_disk.h | 10 +++---- 8 files changed, 104 insertions(+), 116 deletions(-) (limited to 'lib') diff --git a/lib/extra_opts.c b/lib/extra_opts.c index 89b10560..b9843eba 100644 --- a/lib/extra_opts.c +++ b/lib/extra_opts.c @@ -26,15 +26,14 @@ #include "extra_opts.h" /* FIXME: copied from utils.h; we should move a bunch of libs! */ -int -is_option2 (char *str) +bool is_option2 (char *str) { if (!str) - return FALSE; + return false; else if (strspn (str, "-") == 1 || strspn (str, "-") == 2) - return TRUE; + return true; else - return FALSE; + return false; } /* this is the externally visible function used by plugins */ diff --git a/lib/parse_ini.c b/lib/parse_ini.c index 57e60942..0cc864ae 100644 --- a/lib/parse_ini.c +++ b/lib/parse_ini.c @@ -131,7 +131,7 @@ np_get_defaults(const char *locator, const char *default_section) if (inifile == NULL) die(STATE_UNKNOWN, _("Can't read config file: %s\n"), strerror(errno)); - if (read_defaults(inifile, i.stanza, &defaults) == FALSE) + if (!read_defaults(inifile, i.stanza, &defaults)) die(STATE_UNKNOWN, _("Invalid section '%s' in config file '%s'\n"), i.stanza, i.file); @@ -157,7 +157,8 @@ np_get_defaults(const char *locator, const char *default_section) static int read_defaults(FILE *f, const char *stanza, np_arg_list **opts) { - int c, status = FALSE; + int c = 0; + bool status = false; size_t i, stanza_len; enum { NOSTANZA, WRONGSTANZA, RIGHTSTANZA } stanzastate = NOSTANZA; @@ -219,7 +220,7 @@ read_defaults(FILE *f, const char *stanza, np_arg_list **opts) die(STATE_UNKNOWN, "%s\n", _("Config file error")); } - status = TRUE; + status = true; break; } break; diff --git a/lib/tests/test_disk.c b/lib/tests/test_disk.c index 269d20b6..e283fe2e 100644 --- a/lib/tests/test_disk.c +++ b/lib/tests/test_disk.c @@ -44,19 +44,19 @@ main (int argc, char **argv) plan_tests(33); - ok( np_find_name(exclude_filesystem, "/var/log") == FALSE, "/var/log not in list"); + ok( np_find_name(exclude_filesystem, "/var/log") == false, "/var/log not in list"); np_add_name(&exclude_filesystem, "/var/log"); - ok( np_find_name(exclude_filesystem, "/var/log") == TRUE, "is in list now"); - ok( np_find_name(exclude_filesystem, "/home") == FALSE, "/home not in list"); + ok( np_find_name(exclude_filesystem, "/var/log") == true, "is in list now"); + ok( np_find_name(exclude_filesystem, "/home") == false, "/home not in list"); np_add_name(&exclude_filesystem, "/home"); - ok( np_find_name(exclude_filesystem, "/home") == TRUE, "is in list now"); - ok( np_find_name(exclude_filesystem, "/var/log") == TRUE, "/var/log still in list"); + ok( np_find_name(exclude_filesystem, "/home") == true, "is in list now"); + ok( np_find_name(exclude_filesystem, "/var/log") == true, "/var/log still in list"); - ok( np_find_name(exclude_fstype, "iso9660") == FALSE, "iso9660 not in list"); + ok( np_find_name(exclude_fstype, "iso9660") == false, "iso9660 not in list"); np_add_name(&exclude_fstype, "iso9660"); - ok( np_find_name(exclude_fstype, "iso9660") == TRUE, "is in list now"); + ok( np_find_name(exclude_fstype, "iso9660") == true, "is in list now"); - ok( np_find_name(exclude_filesystem, "iso9660") == FALSE, "Make sure no clashing in variables"); + ok( np_find_name(exclude_filesystem, "iso9660") == false, "Make sure no clashing in variables"); /* for (temp_name = exclude_filesystem; temp_name; temp_name = temp_name->next) { @@ -120,7 +120,7 @@ main (int argc, char **argv) np_add_parameter(&paths, "/home/tonvoon"); np_add_parameter(&paths, "/dev/c2t0d0s0"); - np_set_best_match(paths, dummy_mount_list, FALSE); + np_set_best_match(paths, dummy_mount_list, false); for (p = paths; p; p = p->name_next) { struct mount_entry *temp_me; temp_me = p->best_match; @@ -144,7 +144,7 @@ main (int argc, char **argv) np_add_parameter(&paths, "/home/tonvoon"); np_add_parameter(&paths, "/home"); - np_set_best_match(paths, dummy_mount_list, TRUE); + np_set_best_match(paths, dummy_mount_list, true); for (p = paths; p; p = p->name_next) { if (! strcmp(p->name, "/home/groups")) { ok( ! p->best_match , "/home/groups correctly not found"); diff --git a/lib/tests/test_utils.c b/lib/tests/test_utils.c index a109e3f4..01afacdc 100644 --- a/lib/tests/test_utils.c +++ b/lib/tests/test_utils.c @@ -62,99 +62,99 @@ main (int argc, char **argv) range = parse_range_string("6"); ok( range != NULL, "'6' is valid range"); ok( range->start == 0, "Start correct"); - ok( range->start_infinity == FALSE, "Not using negative infinity"); + ok( range->start_infinity == false, "Not using negative infinity"); ok( range->end == 6, "End correct"); - ok( range->end_infinity == FALSE, "Not using infinity"); + ok( range->end_infinity == false, "Not using infinity"); free(range); range = parse_range_string("1:12%%"); ok( range != NULL, "'1:12%%' is valid - percentages are ignored"); ok( range->start == 1, "Start correct"); - ok( range->start_infinity == FALSE, "Not using negative infinity"); + ok( range->start_infinity == false, "Not using negative infinity"); ok( range->end == 12, "End correct"); - ok( range->end_infinity == FALSE, "Not using infinity"); + ok( range->end_infinity == false, "Not using infinity"); free(range); range = parse_range_string("-7:23"); ok( range != NULL, "'-7:23' is valid range"); ok( range->start == -7, "Start correct"); - ok( range->start_infinity == FALSE, "Not using negative infinity"); + ok( range->start_infinity == false, "Not using negative infinity"); ok( range->end == 23, "End correct"); - ok( range->end_infinity == FALSE, "Not using infinity"); + ok( range->end_infinity == false, "Not using infinity"); free(range); range = parse_range_string(":5.75"); ok( range != NULL, "':5.75' is valid range"); ok( range->start == 0, "Start correct"); - ok( range->start_infinity == FALSE, "Not using negative infinity"); + ok( range->start_infinity == false, "Not using negative infinity"); ok( range->end == 5.75, "End correct"); - ok( range->end_infinity == FALSE, "Not using infinity"); + ok( range->end_infinity == false, "Not using infinity"); free(range); range = parse_range_string("~:-95.99"); ok( range != NULL, "~:-95.99' is valid range"); - ok( range->start_infinity == TRUE, "Using negative infinity"); + ok( range->start_infinity == true, "Using negative infinity"); ok( range->end == -95.99, "End correct (with rounding errors)"); - ok( range->end_infinity == FALSE, "Not using infinity"); + ok( range->end_infinity == false, "Not using infinity"); free(range); range = parse_range_string("12345678901234567890:"); temp = atof("12345678901234567890"); /* Can't just use this because number too large */ ok( range != NULL, "'12345678901234567890:' is valid range"); ok( range->start == temp, "Start correct"); - ok( range->start_infinity == FALSE, "Not using negative infinity"); - ok( range->end_infinity == TRUE, "Using infinity"); + ok( range->start_infinity == false, "Not using negative infinity"); + ok( range->end_infinity == true, "Using infinity"); /* Cannot do a "-1" on temp, as it appears to be same value */ - ok( check_range(temp/1.1, range) == TRUE, "12345678901234567890/1.1 - alert"); - ok( check_range(temp, range) == FALSE, "12345678901234567890 - no alert"); - ok( check_range(temp*2, range) == FALSE, "12345678901234567890*2 - no alert"); + ok( check_range(temp/1.1, range) == true, "12345678901234567890/1.1 - alert"); + ok( check_range(temp, range) == false, "12345678901234567890 - no alert"); + ok( check_range(temp*2, range) == false, "12345678901234567890*2 - no alert"); free(range); range = parse_range_string("~:0"); ok( range != NULL, "'~:0' is valid range"); - ok( range->start_infinity == TRUE, "Using negative infinity"); + ok( range->start_infinity == true, "Using negative infinity"); ok( range->end == 0, "End correct"); - ok( range->end_infinity == FALSE, "Not using infinity"); + ok( range->end_infinity == false, "Not using infinity"); ok( range->alert_on == OUTSIDE, "Will alert on outside of this range"); - ok( check_range(0.5, range) == TRUE, "0.5 - alert"); - ok( check_range(-10, range) == FALSE, "-10 - no alert"); - ok( check_range(0, range) == FALSE, "0 - no alert"); + ok( check_range(0.5, range) == true, "0.5 - alert"); + ok( check_range(-10, range) == false, "-10 - no alert"); + ok( check_range(0, range) == false, "0 - no alert"); free(range); - + range = parse_range_string("@0:657.8210567"); ok( range != 0, "@0:657.8210567' is a valid range"); ok( range->start == 0, "Start correct"); - ok( range->start_infinity == FALSE, "Not using negative infinity"); + ok( range->start_infinity == false, "Not using negative infinity"); ok( range->end == 657.8210567, "End correct"); - ok( range->end_infinity == FALSE, "Not using infinity"); + ok( range->end_infinity == false, "Not using infinity"); ok( range->alert_on == INSIDE, "Will alert on inside of this range" ); - ok( check_range(32.88, range) == TRUE, "32.88 - alert"); - ok( check_range(-2, range) == FALSE, "-2 - no alert"); - ok( check_range(657.8210567, range) == TRUE, "657.8210567 - alert"); - ok( check_range(0, range) == TRUE, "0 - alert"); + ok( check_range(32.88, range) == true, "32.88 - alert"); + ok( check_range(-2, range) == false, "-2 - no alert"); + ok( check_range(657.8210567, range) == true, "657.8210567 - alert"); + ok( check_range(0, range) == true, "0 - alert"); free(range); range = parse_range_string("@1:1"); ok( range != NULL, "'@1:1' is a valid range"); ok( range->start == 1, "Start correct"); - ok( range->start_infinity == FALSE, "Not using negative infinity"); + ok( range->start_infinity == false, "Not using negative infinity"); ok( range->end == 1, "End correct"); - ok( range->end_infinity == FALSE, "Not using infinity"); + ok( range->end_infinity == false, "Not using infinity"); ok( range->alert_on == INSIDE, "Will alert on inside of this range" ); - ok( check_range(0.5, range) == FALSE, "0.5 - no alert"); - ok( check_range(1, range) == TRUE, "1 - alert"); - ok( check_range(5.2, range) == FALSE, "5.2 - no alert"); + ok( check_range(0.5, range) == false, "0.5 - no alert"); + ok( check_range(1, range) == true, "1 - alert"); + ok( check_range(5.2, range) == false, "5.2 - no alert"); free(range); range = parse_range_string("1:1"); ok( range != NULL, "'1:1' is a valid range"); ok( range->start == 1, "Start correct"); - ok( range->start_infinity == FALSE, "Not using negative infinity"); + ok( range->start_infinity == false, "Not using negative infinity"); ok( range->end == 1, "End correct"); - ok( range->end_infinity == FALSE, "Not using infinity"); - ok( check_range(0.5, range) == TRUE, "0.5 - alert"); - ok( check_range(1, range) == FALSE, "1 - no alert"); - ok( check_range(5.2, range) == TRUE, "5.2 - alert"); + ok( range->end_infinity == false, "Not using infinity"); + ok( check_range(0.5, range) == true, "0.5 - alert"); + ok( check_range(1, range) == false, "1 - no alert"); + ok( check_range(5.2, range) == true, "5.2 - alert"); free(range); range = parse_range_string("2:1"); @@ -459,7 +459,7 @@ main (int argc, char **argv) ok(this_monitoring_plugin==NULL, "Free'd this_monitoring_plugin"); - ok(mp_suid() == FALSE, "Test aren't suid"); + ok(mp_suid() == false, "Test aren't suid"); /* base states with random case */ char *states[] = { diff --git a/lib/utils_base.c b/lib/utils_base.c index eabcd7ee..3c7221c8 100644 --- a/lib/utils_base.c +++ b/lib/utils_base.c @@ -40,7 +40,7 @@ monitoring_plugin *this_monitoring_plugin=NULL; unsigned int timeout_state = STATE_CRITICAL; unsigned int timeout_interval = DEFAULT_SOCKET_TIMEOUT; -int _np_state_read_file(FILE *); +bool _np_state_read_file(FILE *); void np_init( char *plugin_name, int argc, char **argv ) { if (this_monitoring_plugin==NULL) { @@ -105,12 +105,12 @@ die (int result, const char *fmt, ...) void set_range_start (range *this, double value) { this->start = value; - this->start_infinity = FALSE; + this->start_infinity = false; } void set_range_end (range *this, double value) { this->end = value; - this->end_infinity = FALSE; + this->end_infinity = false; } range @@ -124,9 +124,9 @@ range /* Set defaults */ temp_range->start = 0; - temp_range->start_infinity = FALSE; + temp_range->start_infinity = false; temp_range->end = 0; - temp_range->end_infinity = TRUE; + temp_range->end_infinity = true; temp_range->alert_on = OUTSIDE; temp_range->text = strdup(str); @@ -138,7 +138,7 @@ range end_str = index(str, ':'); if (end_str != NULL) { if (str[0] == '~') { - temp_range->start_infinity = TRUE; + temp_range->start_infinity = true; } else { start = strtod(str, NULL); /* Will stop at the ':' */ set_range_start(temp_range, start); @@ -152,8 +152,8 @@ range set_range_end(temp_range, end); } - if (temp_range->start_infinity == TRUE || - temp_range->end_infinity == TRUE || + if (temp_range->start_infinity == true || + temp_range->end_infinity == true || temp_range->start <= temp_range->end) { return temp_range; } @@ -223,31 +223,30 @@ void print_thresholds(const char *threshold_name, thresholds *my_threshold) { printf("\n"); } -/* Returns TRUE if alert should be raised based on the range */ -int -check_range(double value, range *my_range) +/* Returns true if alert should be raised based on the range */ +bool check_range(double value, range *my_range) { - int no = FALSE; - int yes = TRUE; + bool no = false; + bool yes = true; if (my_range->alert_on == INSIDE) { - no = TRUE; - yes = FALSE; + no = true; + yes = false; } - if (my_range->end_infinity == FALSE && my_range->start_infinity == FALSE) { + if (my_range->end_infinity == false && my_range->start_infinity == false) { if ((my_range->start <= value) && (value <= my_range->end)) { return no; } else { return yes; } - } else if (my_range->start_infinity == FALSE && my_range->end_infinity == TRUE) { + } else if (my_range->start_infinity == false && my_range->end_infinity == true) { if (my_range->start <= value) { return no; } else { return yes; } - } else if (my_range->start_infinity == TRUE && my_range->end_infinity == FALSE) { + } else if (my_range->start_infinity == true && my_range->end_infinity == false) { if (value <= my_range->end) { return no; } else { @@ -263,12 +262,12 @@ int get_status(double value, thresholds *my_thresholds) { if (my_thresholds->critical != NULL) { - if (check_range(value, my_thresholds->critical) == TRUE) { + if (check_range(value, my_thresholds->critical) == true) { return STATE_CRITICAL; } } if (my_thresholds->warning != NULL) { - if (check_range(value, my_thresholds->warning) == TRUE) { + if (check_range(value, my_thresholds->warning) == true) { return STATE_WARNING; } } @@ -465,7 +464,7 @@ char* _np_state_calculate_location_prefix(){ /* Do not allow passing MP_STATE_PATH in setuid plugins * for security reasons */ - if (mp_suid() == FALSE) { + if (!mp_suid()) { env_dir = getenv("MP_STATE_PATH"); if(env_dir && env_dir[0] != '\0') return env_dir; @@ -541,7 +540,7 @@ void np_enable_state(char *keyname, int expected_data_version) { state_data *np_state_read() { state_data *this_state_data=NULL; FILE *statefile; - int rc = FALSE; + bool rc = false; if(this_monitoring_plugin==NULL) die(STATE_UNKNOWN, _("This requires np_init to be called")); @@ -563,7 +562,7 @@ state_data *np_state_read() { fclose(statefile); } - if(rc==FALSE) { + if(!rc) { _cleanup_state_data(); } @@ -573,8 +572,8 @@ state_data *np_state_read() { /* * Read the state file */ -int _np_state_read_file(FILE *f) { - int status=FALSE; +bool _np_state_read_file(FILE *f) { + bool status = false; size_t pos; char *line; int i; @@ -628,7 +627,7 @@ int _np_state_read_file(FILE *f) { if(this_monitoring_plugin->state->state_data->data==NULL) die(STATE_UNKNOWN, _("Cannot execute strdup: %s"), strerror(errno)); expected=STATE_DATA_END; - status=TRUE; + status=true; break; case STATE_DATA_END: ; diff --git a/lib/utils_base.h b/lib/utils_base.h index 9abf5957..80b87435 100644 --- a/lib/utils_base.h +++ b/lib/utils_base.h @@ -21,7 +21,7 @@ typedef struct range_struct { double start; - int start_infinity; /* FALSE (default) or TRUE */ + bool start_infinity; double end; int end_infinity; int alert_on; /* OUTSIDE (default) or INSIDE */ @@ -61,7 +61,7 @@ range *parse_range_string (char *); int _set_thresholds(thresholds **, char *, char *); void set_thresholds(thresholds **, char *, char *); void print_thresholds(const char *, thresholds *); -int check_range(double, range *); +bool check_range(double, range *); int get_status(double, thresholds *); /* Handle timeouts */ diff --git a/lib/utils_disk.c b/lib/utils_disk.c index 5e95aef6..483be06d 100644 --- a/lib/utils_disk.c +++ b/lib/utils_disk.c @@ -170,9 +170,7 @@ np_find_parameter(struct parameter_list *list, const char *name) return NULL; } -void -np_set_best_match(struct parameter_list *desired, struct mount_entry *mount_list, int exact) -{ +void np_set_best_match(struct parameter_list *desired, struct mount_entry *mount_list, bool exact) { struct parameter_list *d; for (d = desired; d; d= d->name_next) { if (! d->best_match) { @@ -195,9 +193,9 @@ np_set_best_match(struct parameter_list *desired, struct mount_entry *mount_list if (! best_match) { for (me = mount_list; me; me = me->me_next) { size_t len = strlen (me->me_mountdir); - if ((exact == FALSE && (best_match_len <= len && len <= name_len && + if ((!exact && (best_match_len <= len && len <= name_len && (len == 1 || strncmp (me->me_mountdir, d->name, len) == 0))) - || (exact == TRUE && strcmp(me->me_mountdir, d->name)==0)) + || (exact && strcmp(me->me_mountdir, d->name)==0)) { if (get_fs_usage(me->me_mountdir, me->me_devname, &fsp) >= 0) { best_match = me; @@ -216,27 +214,23 @@ np_set_best_match(struct parameter_list *desired, struct mount_entry *mount_list } } -/* Returns TRUE if name is in list */ -int -np_find_name (struct name_list *list, const char *name) -{ +/* Returns true if name is in list */ +bool np_find_name (struct name_list *list, const char *name) { const struct name_list *n; if (list == NULL || name == NULL) { - return FALSE; + return false; } for (n = list; n; n = n->next) { if (!strcmp(name, n->name)) { - return TRUE; + return true; } } - return FALSE; + return false; } -/* Returns TRUE if name is in list */ -bool -np_find_regmatch (struct regex_list *list, const char *name) -{ +/* Returns true if name is in list */ +bool np_find_regmatch (struct regex_list *list, const char *name) { int len; regmatch_t m; @@ -257,25 +251,20 @@ np_find_regmatch (struct regex_list *list, const char *name) return false; } -int -np_seen_name(struct name_list *list, const char *name) -{ +bool np_seen_name(struct name_list *list, const char *name) { const struct name_list *s; for (s = list; s; s=s->next) { if (!strcmp(s->name, name)) { - return TRUE; + return true; } } - return FALSE; + return false; } -int -np_regex_match_mount_entry (struct mount_entry* me, regex_t* re) -{ +bool np_regex_match_mount_entry (struct mount_entry* me, regex_t* re) { if (regexec(re, me->me_devname, (size_t) 0, NULL, 0) == 0 || regexec(re, me->me_mountdir, (size_t) 0, NULL, 0) == 0 ) { - return TRUE; - } else { - return FALSE; + return true; } + return false; } diff --git a/lib/utils_disk.h b/lib/utils_disk.h index 6b83ac74..5b2caf23 100644 --- a/lib/utils_disk.h +++ b/lib/utils_disk.h @@ -39,14 +39,14 @@ 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); +bool np_find_name (struct name_list *list, const char *name); +bool 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); - + int search_parameter_list (struct parameter_list *list, const char *name); -void np_set_best_match(struct parameter_list *desired, struct mount_entry *mount_list, int exact); -int np_regex_match_mount_entry (struct mount_entry* me, regex_t* re); +void np_set_best_match(struct parameter_list *desired, struct mount_entry *mount_list, bool exact); +bool np_regex_match_mount_entry (struct mount_entry* me, regex_t* re); -- cgit v1.2.3-74-g34f1 From 6972242126f1dbfb929dbd1c1582d973d2094d8a Mon Sep 17 00:00:00 2001 From: RincewindsHat <12514511+RincewindsHat@users.noreply.github.com> Date: Mon, 16 Oct 2023 00:44:08 +0200 Subject: Fixes for compiler warning -Wparentheses --- lib/extra_opts.c | 4 ++-- lib/utils_base.c | 4 ++-- plugins/check_http.c | 2 +- plugins/check_procs.c | 5 +++-- 4 files changed, 8 insertions(+), 7 deletions(-) (limited to 'lib') diff --git a/lib/extra_opts.c b/lib/extra_opts.c index b9843eba..771621d8 100644 --- a/lib/extra_opts.c +++ b/lib/extra_opts.c @@ -92,14 +92,14 @@ char **np_extra_opts(int *argc, char **argv, const char *plugin_name){ /* append the list to extra_args */ if(extra_args==NULL){ extra_args=ea1; - while(ea1=ea1->next) ea_num++; + while((ea1 = ea1->next)) ea_num++; }else{ ea_tmp=extra_args; while(ea_tmp->next) { ea_tmp=ea_tmp->next; } ea_tmp->next=ea1; - while(ea1=ea1->next) ea_num++; + while((ea1 = ea1->next)) ea_num++; } ea1=ea_tmp=NULL; } diff --git a/lib/utils_base.c b/lib/utils_base.c index 3c7221c8..f86efbeb 100644 --- a/lib/utils_base.c +++ b/lib/utils_base.c @@ -331,7 +331,7 @@ char *np_extract_value(const char *varlist, const char *name, char sep) { /* strip leading spaces */ for (; isspace(varlist[0]); varlist++); - if (tmp = index(varlist, sep)) { + if ((tmp = index(varlist, sep))) { /* Value is delimited by a comma */ if (tmp-varlist == 0) continue; value = (char *)calloc(1, tmp-varlist+1); @@ -347,7 +347,7 @@ char *np_extract_value(const char *varlist, const char *name, char sep) { break; } } - if (tmp = index(varlist, sep)) { + if ((tmp = index(varlist, sep))) { /* More keys, keep going... */ varlist = tmp + 1; } else { diff --git a/plugins/check_http.c b/plugins/check_http.c index 718c8ee7..b9d8145c 100644 --- a/plugins/check_http.c +++ b/plugins/check_http.c @@ -1094,7 +1094,7 @@ check_http (void) microsec_firstbyte = deltime (tv_temp); elapsed_time_firstbyte = (double)microsec_firstbyte / 1.0e6; } - while (pos = memchr(buffer, '\0', i)) { + while ((pos = memchr(buffer, '\0', i))) { /* replace nul character with a blank */ *pos = ' '; } diff --git a/plugins/check_procs.c b/plugins/check_procs.c index c17c6996..1637e3e3 100644 --- a/plugins/check_procs.c +++ b/plugins/check_procs.c @@ -241,8 +241,9 @@ main (int argc, char **argv) /* Ignore self */ if ((usepid && mypid == procpid) || - (!usepid && ((ret = stat_exe(procpid, &statbuf) != -1) && statbuf.st_dev == mydev && statbuf.st_ino == myino) || - (ret == -1 && errno == ENOENT))) { + ( ((!usepid) && ((ret = stat_exe(procpid, &statbuf) != -1) && statbuf.st_dev == mydev && statbuf.st_ino == myino)) || + (ret == -1 && errno == ENOENT)) + ) { if (verbose >= 3) printf("not considering - is myself or gone\n"); continue; -- cgit v1.2.3-74-g34f1