From f855c5b5bbbc6d5436741fd8108be64825a3c76b Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Mon, 15 Sep 2025 14:06:55 +0200 Subject: general refactorin in lib, more local variables, real booleans --- lib/extra_opts.c | 39 ++++++++------ lib/maxfd.c | 1 - lib/parse_ini.c | 141 ++++++++++++++++++++++++++++--------------------- lib/tests/test_cmd.c | 14 +++-- lib/tests/test_ini1.c | 3 +- lib/tests/test_opts1.c | 17 +++--- lib/tests/test_opts2.c | 17 +++--- lib/tests/test_tcp.c | 5 +- lib/utils_base.c | 2 +- lib/utils_base.h | 2 +- lib/utils_cmd.c | 107 +++++++++++++++++-------------------- lib/utils_cmd.h | 8 +-- lib/utils_tcp.c | 20 ++++--- 13 files changed, 196 insertions(+), 180 deletions(-) diff --git a/lib/extra_opts.c b/lib/extra_opts.c index 857b34b4..3fe69014 100644 --- a/lib/extra_opts.c +++ b/lib/extra_opts.c @@ -29,26 +29,30 @@ bool is_option2(char *str) { if (!str) { return false; - } else if (strspn(str, "-") == 1 || strspn(str, "-") == 2) { + } + + if (strspn(str, "-") == 1 || strspn(str, "-") == 2) { return true; - } else { - return false; } + + return false; } /* this is the externally visible function used by plugins */ char **np_extra_opts(int *argc, char **argv, const char *plugin_name) { - np_arg_list *extra_args = NULL, *ea1 = NULL, *ea_tmp = NULL; - char **argv_new = NULL; - char *argptr = NULL; - int i, j, optfound, argc_new, ea_num = *argc; - if (*argc < 2) { /* No arguments provided */ return argv; } - for (i = 1; i < *argc; i++) { + np_arg_list *extra_args = NULL; + np_arg_list *ea1 = NULL; + np_arg_list *ea_tmp = NULL; + char *argptr = NULL; + int optfound; + size_t ea_num = (size_t)*argc; + + for (int i = 1; i < *argc; i++) { argptr = NULL; optfound = 0; @@ -57,9 +61,10 @@ char **np_extra_opts(int *argc, char **argv, const char *plugin_name) { /* It is a single argument with value */ argptr = argv[i] + 13; /* Delete the extra opts argument */ - for (j = i; j < *argc; j++) { + for (int j = i; j < *argc; j++) { argv[j] = argv[j + 1]; } + i--; *argc -= 1; } else if (strcmp(argv[i], "--extra-opts") == 0) { @@ -67,9 +72,10 @@ char **np_extra_opts(int *argc, char **argv, const char *plugin_name) { /* It is a argument with separate value */ argptr = argv[i + 1]; /* Delete the extra-opts argument/value */ - for (j = i; j < *argc - 1; j++) { + for (int j = i; j < *argc - 1; j++) { argv[j] = argv[j + 2]; } + i -= 2; *argc -= 2; ea_num--; @@ -77,9 +83,10 @@ char **np_extra_opts(int *argc, char **argv, const char *plugin_name) { /* It has no value */ optfound = 1; /* Delete the extra opts argument */ - for (j = i; j < *argc; j++) { + for (int j = i; j < *argc; j++) { argv[j] = argv[j + 1]; } + i--; *argc -= 1; } @@ -115,20 +122,20 @@ char **np_extra_opts(int *argc, char **argv, const char *plugin_name) { } } /* lather, rince, repeat */ - if (ea_num == *argc && extra_args == NULL) { + if (ea_num == (size_t)*argc && extra_args == NULL) { /* No extra-opts */ return argv; } /* done processing arguments. now create a new argv array... */ - argv_new = (char **)malloc((ea_num + 1) * sizeof(char **)); + char **argv_new = (char **)malloc((ea_num + 1) * sizeof(char **)); if (argv_new == NULL) { die(STATE_UNKNOWN, _("malloc() failed!\n")); } /* starting with program name */ argv_new[0] = argv[0]; - argc_new = 1; + int argc_new = 1; /* then parsed ini opts (frying them up in the same run) */ while (extra_args) { argv_new[argc_new++] = extra_args->arg; @@ -137,7 +144,7 @@ char **np_extra_opts(int *argc, char **argv, const char *plugin_name) { free(ea1); } /* finally the rest of the argv array */ - for (i = 1; i < *argc; i++) { + for (int i = 1; i < *argc; i++) { argv_new[argc_new++] = argv[i]; } *argc = argc_new; diff --git a/lib/maxfd.c b/lib/maxfd.c index 9b58d8e3..a0f79949 100644 --- a/lib/maxfd.c +++ b/lib/maxfd.c @@ -19,7 +19,6 @@ *****************************************************************************/ #include "./maxfd.h" -#include long mp_open_max(void) { long maxfd = 0L; diff --git a/lib/parse_ini.c b/lib/parse_ini.c index 4c3c1b93..db337622 100644 --- a/lib/parse_ini.c +++ b/lib/parse_ini.c @@ -59,10 +59,10 @@ static char *default_ini_path_names[] = { } while ((c) != EOF && (c) != (n)) /* internal function that returns the constructed defaults options */ -static int read_defaults(FILE *f, const char *stanza, np_arg_list **opts); +static bool read_defaults(FILE *defaults_file, const char *stanza, np_arg_list **opts); /* internal function that converts a single line into options format */ -static int add_option(FILE *f, np_arg_list **optlst); +static int add_option(FILE *filePointer, np_arg_list **optlst); /* internal functions to find default file */ static char *default_file(void); @@ -74,7 +74,8 @@ static char *default_file_in_path(void); * into its separate parts. */ static void parse_locator(const char *locator, const char *def_stanza, np_ini_info *i) { - size_t locator_len = 0, stanza_len = 0; + size_t locator_len = 0; + size_t stanza_len = 0; /* if locator is NULL we'll use default values */ if (locator != NULL) { @@ -112,33 +113,34 @@ static void parse_locator(const char *locator, const char *def_stanza, np_ini_in * This is the externally visible function used by extra_opts. */ np_arg_list *np_get_defaults(const char *locator, const char *default_section) { - FILE *inifile = NULL; - np_arg_list *defaults = NULL; - np_ini_info i; int is_suid_plugin = mp_suid(); if (is_suid_plugin && idpriv_temp_drop() == -1) { die(STATE_UNKNOWN, _("Cannot drop privileges: %s\n"), strerror(errno)); } - parse_locator(locator, default_section, &i); - inifile = strcmp(i.file, "-") == 0 ? stdin : fopen(i.file, "r"); + FILE *inifile = NULL; + np_ini_info ini_info; + parse_locator(locator, default_section, &ini_info); + inifile = strcmp(ini_info.file, "-") == 0 ? stdin : fopen(ini_info.file, "r"); if (inifile == NULL) { die(STATE_UNKNOWN, _("Can't read config file: %s\n"), strerror(errno)); } - if (!read_defaults(inifile, i.stanza, &defaults)) { - die(STATE_UNKNOWN, _("Invalid section '%s' in config file '%s'\n"), i.stanza, i.file); + + np_arg_list *defaults = NULL; + if (!read_defaults(inifile, ini_info.stanza, &defaults)) { + die(STATE_UNKNOWN, _("Invalid section '%s' in config file '%s'\n"), ini_info.stanza, ini_info.file); } - if (i.file_string_on_heap) { - free(i.file); + if (ini_info.file_string_on_heap) { + free(ini_info.file); } if (inifile != stdin) { fclose(inifile); } - free(i.stanza); + free(ini_info.stanza); if (is_suid_plugin && idpriv_temp_restore() == -1) { die(STATE_UNKNOWN, _("Cannot restore privileges: %s\n"), strerror(errno)); } @@ -153,59 +155,58 @@ np_arg_list *np_get_defaults(const char *locator, const char *default_section) { * be extra careful about user-supplied input (i.e. avoiding possible * format string vulnerabilities, etc). */ -static int read_defaults(FILE *f, const char *stanza, np_arg_list **opts) { - int c = 0; +static bool read_defaults(FILE *defaults_file, const char *stanza, np_arg_list **opts) { bool status = false; - size_t i, stanza_len; enum { NOSTANZA, WRONGSTANZA, RIGHTSTANZA } stanzastate = NOSTANZA; - stanza_len = strlen(stanza); + size_t stanza_len = strlen(stanza); /* our little stanza-parsing state machine */ - while ((c = fgetc(f)) != EOF) { + int current_char = 0; + while ((current_char = fgetc(defaults_file)) != EOF) { /* gobble up leading whitespace */ - if (isspace(c)) { + if (isspace(current_char)) { continue; } - switch (c) { + switch (current_char) { /* globble up comment lines */ case ';': case '#': - GOBBLE_TO(f, c, '\n'); + GOBBLE_TO(defaults_file, current_char, '\n'); break; /* start of a stanza, check to see if it matches */ - case '[': + case '[': { stanzastate = WRONGSTANZA; + size_t i; for (i = 0; i < stanza_len; i++) { - c = fgetc(f); + current_char = fgetc(defaults_file); /* strip leading whitespace */ if (i == 0) { - for (; isspace(c); c = fgetc(f)) { - continue; + for (; isspace(current_char); current_char = fgetc(defaults_file)) { } } /* nope, read to the end of the line */ - if (c != stanza[i]) { - GOBBLE_TO(f, c, '\n'); + if (current_char != stanza[i]) { + GOBBLE_TO(defaults_file, current_char, '\n'); break; } } + /* if it matched up to here and the next char is ']'... */ if (i == stanza_len) { - c = fgetc(f); + current_char = fgetc(defaults_file); /* strip trailing whitespace */ - for (; isspace(c); c = fgetc(f)) { - continue; + for (; isspace(current_char); current_char = fgetc(defaults_file)) { } - if (c == ']') { + if (current_char == ']') { stanzastate = RIGHTSTANZA; } } - break; + } break; /* otherwise, we're in the body of a stanza or a parse error */ default: switch (stanzastate) { @@ -216,12 +217,12 @@ static int read_defaults(FILE *f, const char *stanza, np_arg_list **opts) { die(STATE_UNKNOWN, "%s\n", _("Config file error")); /* we're in a stanza, but for a different plugin */ case WRONGSTANZA: - GOBBLE_TO(f, c, '\n'); + GOBBLE_TO(defaults_file, current_char, '\n'); break; /* okay, this is where we start taking the config */ case RIGHTSTANZA: - ungetc(c, f); - if (add_option(f, opts)) { + ungetc(current_char, defaults_file); + if (add_option(defaults_file, opts)) { die(STATE_UNKNOWN, "%s\n", _("Config file error")); } status = true; @@ -240,13 +241,12 @@ static int read_defaults(FILE *f, const char *stanza, np_arg_list **opts) { * --option[=value] * appending it to the linked list optbuf. */ -static int add_option(FILE *f, np_arg_list **optlst) { - np_arg_list *opttmp = *optlst, *optnew; - char *linebuf = NULL, *lineend = NULL, *optptr = NULL, *optend = NULL; - char *eqptr = NULL, *valptr = NULL, *valend = NULL; - short done_reading = 0, equals = 0, value = 0; - size_t cfg_len = 0, read_sz = 8, linebuf_sz = 0, read_pos = 0; - size_t opt_len = 0, val_len = 0; +static int add_option(FILE *filePointer, np_arg_list **optlst) { + char *linebuf = NULL; + bool done_reading = false; + const size_t read_sz = 8; + size_t linebuf_sz = 0; + size_t read_pos = 0; /* read one line from the file */ while (!done_reading) { @@ -258,24 +258,29 @@ static int add_option(FILE *f, np_arg_list **optlst) { die(STATE_UNKNOWN, _("malloc() failed!\n")); } } - if (fgets(&linebuf[read_pos], (int)read_sz, f) == NULL) { - done_reading = 1; + + if (fgets(&linebuf[read_pos], (int)read_sz, filePointer) == NULL) { + done_reading = true; } else { read_pos = strlen(linebuf); if (linebuf[read_pos - 1] == '\n') { linebuf[--read_pos] = '\0'; - done_reading = 1; + done_reading = true; } } } - lineend = &linebuf[read_pos]; + + char *lineend = &linebuf[read_pos]; /* all that to read one line, isn't C fun? :) now comes the parsing :/ */ /* skip leading whitespace */ + char *optptr = NULL; for (optptr = linebuf; optptr < lineend && isspace(*optptr); optptr++) { - continue; } + /* continue to '=' or EOL, watching for spaces that might precede it */ + char *eqptr = NULL; + char *optend = NULL; for (eqptr = optptr; eqptr < lineend && *eqptr != '='; eqptr++) { if (isspace(*eqptr) && optend == NULL) { optend = eqptr; @@ -283,55 +288,67 @@ static int add_option(FILE *f, np_arg_list **optlst) { optend = NULL; } } + if (optend == NULL) { optend = eqptr; } + --optend; + /* ^[[:space:]]*=foo is a syntax error */ if (optptr == eqptr) { die(STATE_UNKNOWN, "%s\n", _("Config file error")); } + /* continue from '=' to start of value or EOL */ + char *valptr = NULL; for (valptr = eqptr + 1; valptr < lineend && isspace(*valptr); valptr++) { - continue; } + /* continue to the end of value */ + char *valend = NULL; for (valend = valptr; valend < lineend; valend++) { - continue; } + --valend; + /* finally trim off trailing spaces */ for (; isspace(*valend); valend--) { - continue; } + /* calculate the length of "--foo" */ - opt_len = (size_t)(1 + optend - optptr); + size_t opt_len = (size_t)(1 + optend - optptr); /* 1-character params needs only one dash */ + size_t cfg_len = 0; if (opt_len == 1) { cfg_len = 1 + (opt_len); } else { cfg_len = 2 + (opt_len); } + + size_t val_len = 0; + bool equals = false; + bool value = false; /* if valptrnext = NULL; read_pos = 0; @@ -357,6 +374,7 @@ static int add_option(FILE *f, np_arg_list **optlst) { if (*optlst == NULL) { *optlst = optnew; } else { + np_arg_list *opttmp = *optlst; while (opttmp->next != NULL) { opttmp = opttmp->next; } @@ -384,8 +402,11 @@ static char *default_file(void) { } static char *default_file_in_path(void) { - char *config_path, **file; - char *dir, *ini_file, *tokens; + char *config_path; + char **file; + char *dir; + char *ini_file; + char *tokens; if ((config_path = getenv("NAGIOS_CONFIG_PATH")) == NULL) { return NULL; diff --git a/lib/tests/test_cmd.c b/lib/tests/test_cmd.c index ade0da90..d51016cc 100644 --- a/lib/tests/test_cmd.c +++ b/lib/tests/test_cmd.c @@ -38,31 +38,29 @@ char *get_command(char *const *line) { } int main(int argc, char **argv) { - char **command_line = malloc(sizeof(char *) * COMMAND_LINE); - char *command = NULL; - char *perl; - output chld_out, chld_err; - int c; - int result = UNSET; - plan_tests(51); diag("Running plain echo command, set one"); /* ensure everything is empty before we begin */ + + output chld_out; memset(&chld_out, 0, sizeof(output)); + output chld_err; memset(&chld_err, 0, sizeof(output)); ok(chld_out.lines == 0, "(initialised) Checking stdout is reset"); ok(chld_err.lines == 0, "(initialised) Checking stderr is reset"); + int result = UNSET; ok(result == UNSET, "(initialised) Checking exit code is reset"); + char **command_line = malloc(sizeof(char *) * COMMAND_LINE); command_line[0] = strdup("/bin/echo"); command_line[1] = strdup("this"); command_line[2] = strdup("is"); command_line[3] = strdup("test"); command_line[4] = strdup("one"); - command = get_command(command_line); + char *command = get_command(command_line); result = cmd_run_array(command_line, &chld_out, &chld_err, 0); ok(chld_out.lines == 1, "(array) Check for expected number of stdout lines"); diff --git a/lib/tests/test_ini1.c b/lib/tests/test_ini1.c index 3792d142..de983764 100644 --- a/lib/tests/test_ini1.c +++ b/lib/tests/test_ini1.c @@ -50,11 +50,10 @@ char *list2str(np_arg_list *optlst) { } int main(int argc, char **argv) { - char *optstr = NULL; plan_tests(12); - optstr = list2str(np_get_defaults("section@./config-tiny.ini", "check_disk")); + char *optstr = list2str(np_get_defaults("section@./config-tiny.ini", "check_disk")); ok(!strcmp(optstr, "--one=two --Foo=Bar --this=Your Mother! --blank"), "config-tiny.ini's section as expected"); my_free(optstr); diff --git a/lib/tests/test_opts1.c b/lib/tests/test_opts1.c index 99da5596..fa95c4d4 100644 --- a/lib/tests/test_opts1.c +++ b/lib/tests/test_opts1.c @@ -40,15 +40,16 @@ void my_free(int *argc, char **newargv, char **argv) { #else void my_free(int *argc, char **newargv, char **argv) { /* Free stuff (and print while we're at it) */ - int i, freeflag = 1; + bool freeflag = true; printf(" Arg(%i): ", *argc + 1); printf("'%s' ", newargv[0]); - for (i = 1; i < *argc; i++) { + + for (int i = 1; i < *argc; i++) { printf("'%s' ", newargv[i]); /* Stop freeing when we get to the start of the original array */ if (freeflag) { if (newargv[i] == argv[1]) { - freeflag = 0; + freeflag = false; } else { free(newargv[i]); } @@ -64,13 +65,12 @@ void my_free(int *argc, char **newargv, char **argv) { #endif int array_diff(int i1, char **a1, int i2, char **a2) { - int i; - if (i1 != i2) { printf(" Argument count doesn't match!\n"); return 0; } - for (i = 0; i <= i1; i++) { + + for (int i = 0; i <= i1; i++) { if (a1[i] == NULL && a2[i] == NULL) { continue; } @@ -87,11 +87,10 @@ int array_diff(int i1, char **a1, int i2, char **a2) { } int main(int argc, char **argv) { - char **argv_new = NULL; - int i, argc_test; - plan_tests(5); + char **argv_new = NULL; + int argc_test; { char *argv_test[] = {"prog_name", (char *)NULL}; argc_test = 1; diff --git a/lib/tests/test_opts2.c b/lib/tests/test_opts2.c index d1b0aca3..3dd1b039 100644 --- a/lib/tests/test_opts2.c +++ b/lib/tests/test_opts2.c @@ -23,15 +23,16 @@ void my_free(int *argc, char **newargv, char **argv) { /* Free stuff (and print while we're at it) */ - int i, freeflag = 1; + bool freeflag = true; + printf(" Arg(%i): ", *argc + 1); printf("'%s' ", newargv[0]); - for (i = 1; i < *argc; i++) { + for (int i = 1; i < *argc; i++) { printf("'%s' ", newargv[i]); /* Stop freeing when we get to the start of the original array */ if (freeflag) { if (newargv[i] == argv[1]) { - freeflag = 0; + freeflag = false; } else { free(newargv[i]); } @@ -46,13 +47,12 @@ void my_free(int *argc, char **newargv, char **argv) { } int array_diff(int i1, char **a1, int i2, char **a2) { - int i; - if (i1 != i2) { printf(" Argument count doesn't match!\n"); return 0; } - for (i = 0; i <= i1; i++) { + + for (int i = 0; i <= i1; i++) { if (a1[i] == NULL && a2[i] == NULL) { continue; } @@ -69,11 +69,10 @@ int array_diff(int i1, char **a1, int i2, char **a2) { } int main(int argc, char **argv) { - char **argv_new = NULL; - int i, argc_test; - plan_tests(5); + char **argv_new = NULL; + int argc_test; { char *argv_test[] = {"prog_name", "arg1", "--extra-opts", "--arg3", "val2", (char *)NULL}; argc_test = 5; diff --git a/lib/tests/test_tcp.c b/lib/tests/test_tcp.c index de3a2102..37c818c9 100644 --- a/lib/tests/test_tcp.c +++ b/lib/tests/test_tcp.c @@ -21,11 +21,10 @@ #include "tap.h" int main(void) { - char **server_expect; - int server_expect_count = 3; - plan_tests(9); + char **server_expect; + const int server_expect_count = 3; server_expect = malloc(sizeof(char *) * server_expect_count); server_expect[0] = strdup("AA"); diff --git a/lib/utils_base.c b/lib/utils_base.c index 69024bc9..28e6dc47 100644 --- a/lib/utils_base.c +++ b/lib/utils_base.c @@ -44,7 +44,7 @@ monitoring_plugin *this_monitoring_plugin = NULL; -int timeout_state = STATE_CRITICAL; +mp_state_enum timeout_state = STATE_CRITICAL; unsigned int timeout_interval = DEFAULT_SOCKET_TIMEOUT; bool _np_state_read_file(FILE *state_file); diff --git a/lib/utils_base.h b/lib/utils_base.h index f31299c4..27884bf0 100644 --- a/lib/utils_base.h +++ b/lib/utils_base.h @@ -41,7 +41,7 @@ bool mp_check_range(mp_perfdata_value, mp_range); mp_state_enum get_status(double, thresholds *); /* Handle timeouts */ -extern int timeout_state; +extern mp_state_enum timeout_state; extern unsigned int timeout_interval; /* All possible characters in a threshold range */ diff --git a/lib/utils_cmd.c b/lib/utils_cmd.c index d1feaa33..35b83297 100644 --- a/lib/utils_cmd.c +++ b/lib/utils_cmd.c @@ -40,7 +40,6 @@ /** includes **/ #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. @@ -62,9 +61,6 @@ static pid_t *_cmd_pids = NULL; # include #endif -/* used in _cmd_open to pass the environment to commands */ -extern char **environ; - /** macros **/ #ifndef WEXITSTATUS # define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8) @@ -80,14 +76,12 @@ extern char **environ; #endif /** prototypes **/ -static int _cmd_open(char *const *, int *, int *) __attribute__((__nonnull__(1, 2, 3))); - -static int _cmd_fetch_output(int, output *, int) __attribute__((__nonnull__(2))); +static int _cmd_open(char *const *argv, int *pfd, int *pfderr) + __attribute__((__nonnull__(1, 2, 3))); -static int _cmd_close(int); +static int _cmd_fetch_output(int fileDescriptor, output *cmd_output, int flags) __attribute__((__nonnull__(2))); -/* prototype imported from utils.h */ -extern void die(int, const char *, ...) __attribute__((__noreturn__, __format__(__printf__, 2, 3))); +static int _cmd_close(int fileDescriptor); /* this function is NOT async-safe. It is exported so multithreaded * plugins (or other apps) can call it prior to running any commands @@ -110,7 +104,6 @@ void cmd_init(void) { /* Start running a command, array style */ static int _cmd_open(char *const *argv, int *pfd, int *pfderr) { - pid_t pid; #ifdef RLIMIT_CORE struct rlimit limit; #endif @@ -123,6 +116,7 @@ static int _cmd_open(char *const *argv, int *pfd, int *pfderr) { setenv("LC_ALL", "C", 1); + pid_t pid; if (pipe(pfd) < 0 || pipe(pfderr) < 0 || (pid = fork()) < 0) { return -1; /* errno set by the failing function */ } @@ -171,22 +165,23 @@ static int _cmd_open(char *const *argv, int *pfd, int *pfderr) { return pfd[0]; } -static int _cmd_close(int fd) { - int status; +static int _cmd_close(int fileDescriptor) { pid_t pid; /* make sure the provided fd was opened */ long maxfd = mp_open_max(); - if (fd < 0 || fd > maxfd || !_cmd_pids || (pid = _cmd_pids[fd]) == 0) { + if (fileDescriptor < 0 || fileDescriptor > maxfd || !_cmd_pids || + (pid = _cmd_pids[fileDescriptor]) == 0) { return -1; } - _cmd_pids[fd] = 0; - if (close(fd) == -1) { + _cmd_pids[fileDescriptor] = 0; + if (close(fileDescriptor) == -1) { return -1; } /* EINTR is ok (sort of), everything else is bad */ + int status; while (waitpid(pid, &status, 0) < 0) { if (errno != EINTR) { return -1; @@ -197,68 +192,67 @@ static int _cmd_close(int fd) { return (WIFEXITED(status)) ? WEXITSTATUS(status) : -1; } -static int _cmd_fetch_output(int fd, output *op, int flags) { - size_t len = 0, i = 0, lineno = 0; - size_t rsf = 6, ary_size = 0; /* rsf = right shift factor, dec'ed uncond once */ - char *buf = NULL; - int ret; +static int _cmd_fetch_output(int fileDescriptor, output *cmd_output, int flags) { char tmpbuf[4096]; - - op->buf = NULL; - op->buflen = 0; - while ((ret = read(fd, tmpbuf, sizeof(tmpbuf))) > 0) { - len = (size_t)ret; - op->buf = realloc(op->buf, op->buflen + len + 1); - memcpy(op->buf + op->buflen, tmpbuf, len); - op->buflen += len; - i++; + cmd_output->buf = NULL; + cmd_output->buflen = 0; + ssize_t ret; + while ((ret = read(fileDescriptor, tmpbuf, sizeof(tmpbuf))) > 0) { + size_t len = (size_t)ret; + cmd_output->buf = realloc(cmd_output->buf, cmd_output->buflen + len + 1); + memcpy(cmd_output->buf + cmd_output->buflen, tmpbuf, len); + cmd_output->buflen += len; } if (ret < 0) { - printf("read() returned %d: %s\n", ret, strerror(errno)); + printf("read() returned %zd: %s\n", ret, strerror(errno)); return ret; } /* some plugins may want to keep output unbroken, and some commands * will yield no output, so return here for those */ - if (flags & CMD_NO_ARRAYS || !op->buf || !op->buflen) { - return op->buflen; + if (flags & CMD_NO_ARRAYS || !cmd_output->buf || !cmd_output->buflen) { + return cmd_output->buflen; } /* and some may want both */ + char *buf = NULL; if (flags & CMD_NO_ASSOC) { - buf = malloc(op->buflen); - memcpy(buf, op->buf, op->buflen); + buf = malloc(cmd_output->buflen); + memcpy(buf, cmd_output->buf, cmd_output->buflen); } else { - buf = op->buf; + buf = cmd_output->buf; } - op->line = NULL; - op->lens = NULL; - i = 0; - while (i < op->buflen) { + cmd_output->line = NULL; + cmd_output->lens = NULL; + size_t i = 0; + size_t ary_size = 0; /* rsf = right shift factor, dec'ed uncond once */ + size_t rsf = 6; + size_t lineno = 0; + while (i < cmd_output->buflen) { /* make sure we have enough memory */ if (lineno >= ary_size) { /* ary_size must never be zero */ do { - ary_size = op->buflen >> --rsf; + ary_size = cmd_output->buflen >> --rsf; } while (!ary_size); - op->line = realloc(op->line, ary_size * sizeof(char *)); - op->lens = realloc(op->lens, ary_size * sizeof(size_t)); + cmd_output->line = realloc(cmd_output->line, ary_size * sizeof(char *)); + cmd_output->lens = realloc(cmd_output->lens, ary_size * sizeof(size_t)); } /* set the pointer to the string */ - op->line[lineno] = &buf[i]; + cmd_output->line[lineno] = &buf[i]; /* hop to next newline or end of buffer */ - while (buf[i] != '\n' && i < op->buflen) { + while (buf[i] != '\n' && i < cmd_output->buflen) { i++; } buf[i] = '\0'; /* calculate the string length using pointer difference */ - op->lens[lineno] = (size_t)&buf[i] - (size_t)op->line[lineno]; + cmd_output->lens[lineno] = (size_t)&buf[i] - (size_t)cmd_output->line[lineno]; lineno++; i++; @@ -268,12 +262,6 @@ static int _cmd_fetch_output(int fd, output *op, int flags) { } int cmd_run(const char *cmdstring, output *out, output *err, int flags) { - int i = 0, argc; - size_t cmdlen; - char **argv = NULL; - char *cmd = NULL; - char *str = NULL; - if (cmdstring == NULL) { return -1; } @@ -288,7 +276,8 @@ int cmd_run(const char *cmdstring, output *out, output *err, int flags) { /* make copy of command string so strtok() doesn't silently modify it */ /* (the calling program may want to access it later) */ - cmdlen = strlen(cmdstring); + size_t cmdlen = strlen(cmdstring); + char *cmd = NULL; if ((cmd = malloc(cmdlen + 1)) == NULL) { return -1; } @@ -307,8 +296,8 @@ int cmd_run(const char *cmdstring, output *out, output *err, int flags) { /* each arg must be whitespace-separated, so args can be a maximum * of (len / 2) + 1. We add 1 extra to the mix for NULL termination */ - argc = (cmdlen >> 1) + 2; - argv = calloc((size_t)argc, sizeof(char *)); + int argc = (cmdlen >> 1) + 2; + char **argv = calloc((size_t)argc, sizeof(char *)); if (argv == NULL) { printf("%s\n", _("Could not malloc argv array in popen()")); @@ -316,8 +305,9 @@ int cmd_run(const char *cmdstring, output *out, output *err, int flags) { } /* get command arguments (stupidly, but fairly quickly) */ + int i = 0; while (cmd) { - str = cmd; + char *str = cmd; str += strspn(str, " \t\r\n"); /* trim any leading whitespace */ if (strstr(str, "'") == str) { /* handle SIMPLE quoted strings */ @@ -347,8 +337,6 @@ int cmd_run(const char *cmdstring, output *out, output *err, int flags) { } int cmd_run_array(char *const *argv, output *out, output *err, int flags) { - int fd, pfd_out[2], pfd_err[2]; - /* initialize the structs */ if (out) { memset(out, 0, sizeof(output)); @@ -357,6 +345,9 @@ int cmd_run_array(char *const *argv, output *out, output *err, int flags) { memset(err, 0, sizeof(output)); } + int fd; + int pfd_out[2]; + int pfd_err[2]; if ((fd = _cmd_open(argv, pfd_out, pfd_err)) == -1) { die(STATE_UNKNOWN, _("Could not open pipe: %s\n"), argv[0]); } diff --git a/lib/utils_cmd.h b/lib/utils_cmd.h index 728ece23..3672cdc9 100644 --- a/lib/utils_cmd.h +++ b/lib/utils_cmd.h @@ -5,17 +5,17 @@ * Header file for Monitoring Plugins utils_cmd.c * */ +#include "../config.h" +#include /** types **/ -struct output { +typedef struct { char *buf; /* output buffer */ size_t buflen; /* output buffer content length */ char **line; /* array of lines (points to buf) */ size_t *lens; /* string lengths */ size_t lines; /* lines of output */ -}; - -typedef struct output output; +} output; /** prototypes **/ int cmd_run(const char *, output *, output *, int); diff --git a/lib/utils_tcp.c b/lib/utils_tcp.c index 1482458b..a82d5a3f 100644 --- a/lib/utils_tcp.c +++ b/lib/utils_tcp.c @@ -26,8 +26,10 @@ * *****************************************************************************/ -#include "common.h" +#include "../config.h" #include "utils_tcp.h" +#include +#include #define VERBOSE(message) \ do { \ @@ -37,9 +39,9 @@ enum np_match_result np_expect_match(char *status, char **server_expect, int expect_count, int flags) { - int i, match = 0, partial = 0; - - for (i = 0; i < expect_count; i++) { + int match = 0; + int partial = 0; + for (int i = 0; i < expect_count; i++) { if (flags & NP_MATCH_VERBOSE) { printf("looking for [%s] %s [%s]\n", server_expect[i], (flags & NP_MATCH_EXACT) ? "in beginning of" : "anywhere in", status); @@ -50,7 +52,9 @@ enum np_match_result np_expect_match(char *status, char **server_expect, int exp VERBOSE("found it"); match++; continue; - } else if (strncmp(status, server_expect[i], strlen(status)) == 0) { + } + + if (strncmp(status, server_expect[i], strlen(status)) == 0) { VERBOSE("found a substring"); partial++; continue; @@ -66,9 +70,9 @@ enum np_match_result np_expect_match(char *status, char **server_expect, int exp if ((flags & NP_MATCH_ALL && match == expect_count) || (!(flags & NP_MATCH_ALL) && match >= 1)) { return NP_MATCH_SUCCESS; - } else if (partial > 0 || !(flags & NP_MATCH_EXACT)) { + } + if (partial > 0 || !(flags & NP_MATCH_EXACT)) { return NP_MATCH_RETRY; - } else { - return NP_MATCH_FAILURE; } + return NP_MATCH_FAILURE; } -- cgit v1.2.3-74-g34f1 From 811da10fda11b1d0453453040647270700862d8f Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Mon, 15 Sep 2025 15:49:26 +0200 Subject: More refactoring --- plugins-root/check_icmp.d/check_icmp_helpers.c | 4 +- plugins/check_load.c | 4 +- plugins/netutils.c | 224 +++++++++++++------------ plugins/netutils.h | 32 ++-- 4 files changed, 139 insertions(+), 125 deletions(-) diff --git a/plugins-root/check_icmp.d/check_icmp_helpers.c b/plugins-root/check_icmp.d/check_icmp_helpers.c index d56fbd8b..1b96a392 100644 --- a/plugins-root/check_icmp.d/check_icmp_helpers.c +++ b/plugins-root/check_icmp.d/check_icmp_helpers.c @@ -76,7 +76,7 @@ check_icmp_state check_icmp_state_init() { ping_target_create_wrapper ping_target_create(struct sockaddr_storage address) { ping_target_create_wrapper result = { - .errorcode = OK, + .errorcode = 0, }; struct sockaddr_storage *tmp_addr = &address; @@ -88,7 +88,7 @@ ping_target_create_wrapper ping_target_create(struct sockaddr_storage address) { ((struct sockaddr_in *)tmp_addr)->sin_addr.s_addr == INADDR_ANY))) || (tmp_addr->ss_family == AF_INET6 && (((struct sockaddr_in6 *)tmp_addr)->sin6_addr.s6_addr == in6addr_any.s6_addr))) { - result.errorcode = ERROR; + result.errorcode = 1; return result; } diff --git a/plugins/check_load.c b/plugins/check_load.c index f7a6f7fd..644cd604 100644 --- a/plugins/check_load.c +++ b/plugins/check_load.c @@ -452,8 +452,8 @@ static top_processes_result print_top_consuming_processes(unsigned long n_procs_ top_processes_result result = { .errorcode = OK, }; - struct output chld_out; - struct output chld_err; + output chld_out; + output chld_err; if (np_runcmd(PS_COMMAND, &chld_out, &chld_err, 0) != 0) { fprintf(stderr, _("'%s' exited with non-zero status.\n"), PS_COMMAND); result.errorcode = ERROR; diff --git a/plugins/netutils.c b/plugins/netutils.c index 92c53e4c..b4c6ff0a 100644 --- a/plugins/netutils.c +++ b/plugins/netutils.c @@ -30,13 +30,14 @@ #include "common.h" #include "output.h" #include "states.h" +#include #include "netutils.h" unsigned int socket_timeout = DEFAULT_SOCKET_TIMEOUT; -unsigned int socket_timeout_state = STATE_CRITICAL; - -int econn_refuse_state = STATE_CRITICAL; +mp_state_enum socket_timeout_state = STATE_CRITICAL; +mp_state_enum econn_refuse_state = STATE_CRITICAL; bool was_refused = false; + #if USE_IPV6 int address_family = AF_UNSPEC; #else @@ -63,39 +64,40 @@ void socket_timeout_alarm_handler(int sig) { /* connects to a host on a specified tcp port, sends a string, and gets a response. loops on select-recv until timeout or eof to get all of a multi-packet answer */ -int process_tcp_request2(const char *server_address, int server_port, const char *send_buffer, - char *recv_buffer, int recv_size) { +mp_state_enum process_tcp_request2(const char *server_address, const int server_port, + const char *send_buffer, char *recv_buffer, + const int recv_size) { - int result; - int send_result; - int recv_result; - int sd; - struct timeval tv; - fd_set readfds; - int recv_length = 0; + int socket; - result = np_net_connect(server_address, server_port, &sd, IPPROTO_TCP); - if (result != STATE_OK) { + mp_state_enum connect_result = + np_net_connect(server_address, server_port, &socket, IPPROTO_TCP); + if (connect_result != STATE_OK) { return STATE_CRITICAL; } - send_result = send(sd, send_buffer, strlen(send_buffer), 0); + mp_state_enum result; + ssize_t send_result = send(socket, send_buffer, strlen(send_buffer), 0); if (send_result < 0 || (size_t)send_result != strlen(send_buffer)) { // printf("%s\n", _("Send failed")); result = STATE_WARNING; } - while (1) { + fd_set readfds; + ssize_t recv_length = 0; + while (true) { /* wait up to the number of seconds for socket timeout minus one for data from the host */ - tv.tv_sec = socket_timeout - 1; - tv.tv_usec = 0; + struct timeval timeout = { + .tv_sec = socket_timeout - 1, + .tv_usec = 0, + }; FD_ZERO(&readfds); - FD_SET(sd, &readfds); - select(sd + 1, &readfds, NULL, NULL, &tv); + FD_SET(socket, &readfds); + select(socket + 1, &readfds, NULL, NULL, &timeout); /* make sure some data has arrived */ - if (!FD_ISSET(sd, &readfds)) { /* it hasn't */ + if (!FD_ISSET(socket, &readfds)) { /* it hasn't */ if (!recv_length) { strcpy(recv_buffer, ""); // printf("%s\n", _("No data was received from host!")); @@ -104,72 +106,69 @@ int process_tcp_request2(const char *server_address, int server_port, const char recv_buffer[recv_length] = 0; } break; - } else { /* it has */ - recv_result = - recv(sd, recv_buffer + recv_length, (size_t)recv_size - recv_length - 1, 0); - if (recv_result == -1) { - /* recv failed, bail out */ - strcpy(recv_buffer + recv_length, ""); - result = STATE_WARNING; - break; - } else if (recv_result == 0) { - /* end of file ? */ - recv_buffer[recv_length] = 0; - break; - } else { /* we got data! */ - recv_length += recv_result; - if (recv_length >= recv_size - 1) { - /* buffer full, we're done */ - recv_buffer[recv_size - 1] = 0; - break; - } - } + } /* it has */ + + ssize_t recv_result = + recv(socket, recv_buffer + recv_length, (size_t)(recv_size - recv_length - 1), 0); + if (recv_result == -1) { + /* recv failed, bail out */ + strcpy(recv_buffer + recv_length, ""); + result = STATE_WARNING; + break; + } + + if (recv_result == 0) { + /* end of file ? */ + recv_buffer[recv_length] = 0; + break; + } + + /* we got data! */ + recv_length += recv_result; + if (recv_length >= recv_size - 1) { + /* buffer full, we're done */ + recv_buffer[recv_size - 1] = 0; + break; } /* end if(!FD_ISSET(sd,&readfds)) */ } - /* end while(1) */ - close(sd); + close(socket); return result; } /* connects to a host on a specified port, sends a string, and gets a response */ -int process_request(const char *server_address, int server_port, int proto, const char *send_buffer, - char *recv_buffer, int recv_size) { - int result; - int sd; +mp_state_enum process_request(const char *server_address, const int server_port, const int proto, + const char *send_buffer, char *recv_buffer, const int recv_size) { - result = STATE_OK; - - result = np_net_connect(server_address, server_port, &sd, proto); + mp_state_enum result = STATE_OK; + int socket; + result = np_net_connect(server_address, server_port, &socket, proto); if (result != STATE_OK) { return STATE_CRITICAL; } - result = send_request(sd, proto, send_buffer, recv_buffer, recv_size); + result = send_request(socket, proto, send_buffer, recv_buffer, recv_size); - close(sd); + close(socket); return result; } /* opens a tcp or udp connection to a remote host or local socket */ -int np_net_connect(const char *host_name, int port, int *sd, int proto) { +mp_state_enum np_net_connect(const char *host_name, int port, int *socketDescriptor, + const int proto) { /* send back STATE_UNKOWN if there's an error send back STATE_OK if we connect send back STATE_CRITICAL if we can't connect. Let upstream figure out what to send to the user. */ - struct addrinfo hints; - struct addrinfo *r, *res; - struct sockaddr_un su; - char port_str[6], host[MAX_HOST_ADDRESS_LENGTH]; - size_t len; - int socktype, result; - short is_socket = (host_name[0] == '/'); - - socktype = (proto == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM; + bool is_socket = (host_name[0] == '/'); + int socktype = (proto == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM; + struct addrinfo hints = {}; + struct addrinfo *res = NULL; + int result; /* as long as it doesn't start with a '/', it's assumed a host or ip */ if (!is_socket) { memset(&hints, 0, sizeof(hints)); @@ -177,38 +176,46 @@ int np_net_connect(const char *host_name, int port, int *sd, int proto) { hints.ai_protocol = proto; hints.ai_socktype = socktype; - len = strlen(host_name); + size_t len = strlen(host_name); /* check for an [IPv6] address (and strip the brackets) */ if (len >= 2 && host_name[0] == '[' && host_name[len - 1] == ']') { host_name++; len -= 2; } + + char host[MAX_HOST_ADDRESS_LENGTH]; + if (len >= sizeof(host)) { return STATE_UNKNOWN; } + memcpy(host, host_name, len); host[len] = '\0'; + + char port_str[6]; snprintf(port_str, sizeof(port_str), "%d", port); - result = getaddrinfo(host, port_str, &hints, &res); + int getaddrinfo_err = getaddrinfo(host, port_str, &hints, &res); - if (result != 0) { + if (getaddrinfo_err != 0) { // printf("%s\n", gai_strerror(result)); return STATE_UNKNOWN; } - r = res; - while (r) { + struct addrinfo *addressPointer = res; + while (addressPointer) { /* attempt to create a socket */ - *sd = socket(r->ai_family, socktype, r->ai_protocol); + *socketDescriptor = + socket(addressPointer->ai_family, socktype, addressPointer->ai_protocol); - if (*sd < 0) { + if (*socketDescriptor < 0) { // printf("%s\n", _("Socket creation failed")); - freeaddrinfo(r); + freeaddrinfo(addressPointer); return STATE_UNKNOWN; } /* attempt to open a connection */ - result = connect(*sd, r->ai_addr, r->ai_addrlen); + result = + connect(*socketDescriptor, addressPointer->ai_addr, addressPointer->ai_addrlen); if (result == 0) { was_refused = false; @@ -223,24 +230,28 @@ int np_net_connect(const char *host_name, int port, int *sd, int proto) { } } - close(*sd); - r = r->ai_next; + close(*socketDescriptor); + addressPointer = addressPointer->ai_next; } + freeaddrinfo(res); - } - /* else the hostname is interpreted as a path to a unix socket */ - else { + + } else { + /* else the hostname is interpreted as a path to a unix socket */ if (strlen(host_name) >= UNIX_PATH_MAX) { die(STATE_UNKNOWN, _("Supplied path too long unix domain socket")); } - memset(&su, 0, sizeof(su)); + + struct sockaddr_un su = {}; su.sun_family = AF_UNIX; strncpy(su.sun_path, host_name, UNIX_PATH_MAX); - *sd = socket(PF_UNIX, SOCK_STREAM, 0); - if (*sd < 0) { + *socketDescriptor = socket(PF_UNIX, SOCK_STREAM, 0); + + if (*socketDescriptor < 0) { die(STATE_UNKNOWN, _("Socket creation failed")); } - result = connect(*sd, (struct sockaddr *)&su, sizeof(su)); + + result = connect(*socketDescriptor, (struct sockaddr *)&su, sizeof(su)); if (result < 0 && errno == ECONNREFUSED) { was_refused = true; } @@ -248,7 +259,9 @@ int np_net_connect(const char *host_name, int port, int *sd, int proto) { if (result == 0) { return STATE_OK; - } else if (was_refused) { + } + + if (was_refused) { switch (econn_refuse_state) { /* a user-defined expected outcome */ case STATE_OK: case STATE_WARNING: /* user wants WARN or OK on refusal, or... */ @@ -275,14 +288,11 @@ int np_net_connect(const char *host_name, int port, int *sd, int proto) { } } -int send_request(int sd, int proto, const char *send_buffer, char *recv_buffer, int recv_size) { - int result = STATE_OK; - int send_result; - int recv_result; - struct timeval tv; - fd_set readfds; +mp_state_enum send_request(const int socket, const int proto, const char *send_buffer, + char *recv_buffer, const int recv_size) { + mp_state_enum result = STATE_OK; - send_result = send(sd, send_buffer, strlen(send_buffer), 0); + ssize_t send_result = send(socket, send_buffer, strlen(send_buffer), 0); if (send_result < 0 || (size_t)send_result != strlen(send_buffer)) { // printf("%s\n", _("Send failed")); result = STATE_WARNING; @@ -290,21 +300,22 @@ int send_request(int sd, int proto, const char *send_buffer, char *recv_buffer, /* wait up to the number of seconds for socket timeout minus one for data from the host */ - tv.tv_sec = socket_timeout - 1; - tv.tv_usec = 0; + struct timeval timestamp = { + .tv_sec = socket_timeout - 1, + .tv_usec = 0, + }; + fd_set readfds; FD_ZERO(&readfds); - FD_SET(sd, &readfds); - select(sd + 1, &readfds, NULL, NULL, &tv); + FD_SET(socket, &readfds); + select(socket + 1, &readfds, NULL, NULL, ×tamp); /* make sure some data has arrived */ - if (!FD_ISSET(sd, &readfds)) { + if (!FD_ISSET(socket, &readfds)) { strcpy(recv_buffer, ""); // printf("%s\n", _("No data was received from host!")); result = STATE_WARNING; - } - - else { - recv_result = recv(sd, recv_buffer, (size_t)recv_size - 1, 0); + } else { + ssize_t recv_result = recv(socket, recv_buffer, (size_t)(recv_size - 1), 0); if (recv_result == -1) { strcpy(recv_buffer, ""); if (proto != IPPROTO_TCP) { @@ -318,6 +329,7 @@ int send_request(int sd, int proto, const char *send_buffer, char *recv_buffer, /* die returned string */ recv_buffer[recv_size - 1] = 0; } + return result; } @@ -339,27 +351,27 @@ bool is_addr(const char *address) { #ifdef USE_IPV6 if (address_family == AF_INET && is_inet_addr(address)) { return true; - } else if (address_family == AF_INET6 && is_inet6_addr(address)) { + } + + if (address_family == AF_INET6 && is_inet6_addr(address)) { return true; } #else if (is_inet_addr(address)) { - return (true); + return true; } #endif - return (false); + return false; } -int dns_lookup(const char *in, struct sockaddr_storage *ss, int family) { +bool dns_lookup(const char *node_string, struct sockaddr_storage *ss, const int family) { struct addrinfo hints; - struct addrinfo *res; - int retval; - memset(&hints, 0, sizeof(struct addrinfo)); hints.ai_family = family; - retval = getaddrinfo(in, NULL, &hints, &res); + struct addrinfo *res; + int retval = getaddrinfo(node_string, NULL, &hints, &res); if (retval != 0) { return false; } @@ -367,6 +379,8 @@ int dns_lookup(const char *in, struct sockaddr_storage *ss, int family) { if (ss != NULL) { memcpy(ss, res->ai_addr, res->ai_addrlen); } + freeaddrinfo(res); + return true; } diff --git a/plugins/netutils.h b/plugins/netutils.h index 6adb8e01..c4461113 100644 --- a/plugins/netutils.h +++ b/plugins/netutils.h @@ -31,7 +31,6 @@ #ifndef _NETUTILS_H_ #define _NETUTILS_H_ -#include "common.h" #include "output.h" #include "states.h" #include "utils.h" @@ -56,25 +55,26 @@ process_request(addr, port, IPPROTO_TCP, sbuf, rbuf, rsize) #define process_udp_request(addr, port, sbuf, rbuf, rsize) \ process_request(addr, port, IPPROTO_UDP, sbuf, rbuf, rsize) -int process_tcp_request2(const char *address, int port, const char *sbuffer, char *rbuffer, - int rsize); -int process_request(const char *address, int port, int proto, const char *sbuffer, char *rbuffer, - int rsize); +mp_state_enum process_tcp_request2(const char *server_address, int server_port, + const char *send_buffer, char *recv_buffer, int recv_size); +mp_state_enum process_request(const char *server_address, int server_port, int proto, + const char *send_buffer, char *recv_buffer, int recv_size); /* my_connect and wrapper macros */ #define my_tcp_connect(addr, port, s) np_net_connect(addr, port, s, IPPROTO_TCP) #define my_udp_connect(addr, port, s) np_net_connect(addr, port, s, IPPROTO_UDP) -int np_net_connect(const char *address, int port, int *sd, int proto); +mp_state_enum np_net_connect(const char *host_name, int port, int *socketDescriptor, int proto); /* send_request and wrapper macros */ #define send_tcp_request(s, sbuf, rbuf, rsize) send_request(s, IPPROTO_TCP, sbuf, rbuf, rsize) #define send_udp_request(s, sbuf, rbuf, rsize) send_request(s, IPPROTO_UDP, sbuf, rbuf, rsize) -int send_request(int sd, int proto, const char *send_buffer, char *recv_buffer, int recv_size); +mp_state_enum send_request(int socket, int proto, const char *send_buffer, char *recv_buffer, + int recv_size); /* "is_*" wrapper macros and functions */ bool is_host(const char *); bool is_addr(const char *); -int dns_lookup(const char *, struct sockaddr_storage *, int); +bool dns_lookup(const char *, struct sockaddr_storage *, int); void host_or_die(const char *str); #define resolve_host_or_addr(addr, family) dns_lookup(addr, NULL, family) #define is_inet_addr(addr) resolve_host_or_addr(addr, AF_INET) @@ -86,8 +86,8 @@ void host_or_die(const char *str); #endif extern unsigned int socket_timeout; -extern unsigned int socket_timeout_state; -extern int econn_refuse_state; +extern mp_state_enum socket_timeout_state; +extern mp_state_enum econn_refuse_state; extern bool was_refused; extern int address_family; @@ -106,12 +106,12 @@ void socket_timeout_alarm_handler(int) __attribute__((noreturn)); # define MP_TLSv1_1_OR_NEWER 9 # define MP_TLSv1_2_OR_NEWER 10 /* maybe this could be merged with the above np_net_connect, via some flags */ -int np_net_ssl_init(int sd); -int np_net_ssl_init_with_hostname(int sd, char *host_name); -int np_net_ssl_init_with_hostname_and_version(int sd, char *host_name, int version); -int np_net_ssl_init_with_hostname_version_and_cert(int sd, char *host_name, int version, char *cert, - char *privkey); -void np_net_ssl_cleanup(); +int np_net_ssl_init(int socket); +int np_net_ssl_init_with_hostname(int socket, char *host_name); +int np_net_ssl_init_with_hostname_and_version(int socket, char *host_name, int version); +int np_net_ssl_init_with_hostname_version_and_cert(int socket, char *host_name, int version, + char *cert, char *privkey); +void np_net_ssl_cleanup(void); int np_net_ssl_write(const void *buf, int num); int np_net_ssl_read(void *buf, int num); mp_state_enum np_net_ssl_check_cert(int days_till_exp_warn, int days_till_exp_crit); -- cgit v1.2.3-74-g34f1 From 15ecdb73ce5cda54f824e5a63ee8e801bf16a996 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Mon, 15 Sep 2025 16:00:35 +0200 Subject: Fix types in check_apt --- plugins/check_apt.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/plugins/check_apt.c b/plugins/check_apt.c index e840184b..ab66a8d2 100644 --- a/plugins/check_apt.c +++ b/plugins/check_apt.c @@ -310,8 +310,8 @@ run_upgrade_result run_upgrade(const upgrade_type upgrade, const char *do_includ die(STATE_UNKNOWN, _("%s: Error compiling regexp: %s"), progname, rerrbuf); } - struct output chld_out; - struct output chld_err; + output chld_out; + output chld_err; char *cmdline = NULL; cmdline = construct_cmdline(upgrade, upgrade_opts); if (input_filename != NULL) { @@ -410,8 +410,8 @@ int run_update(char *update_opts) { /* run the update */ cmdline = construct_cmdline(NO_UPGRADE, update_opts); - struct output chld_out; - struct output chld_err; + output chld_out; + output chld_err; result = np_runcmd(cmdline, &chld_out, &chld_err, 0); /* apt-get update changes exit status if it can't fetch packages. * since we were explicitly asked to do so, this is treated as -- cgit v1.2.3-74-g34f1