From ec47bbbda6b99f0efc1801a424812a4dd457f9b6 Mon Sep 17 00:00:00 2001 From: Andreas Baumann Date: Fri, 9 May 2025 10:49:02 +0200 Subject: changed filename in cmd_file_read to const char * (check_apt warning) --- lib/utils_cmd.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/utils_cmd.h') diff --git a/lib/utils_cmd.h b/lib/utils_cmd.h index d00069c9..728ece23 100644 --- a/lib/utils_cmd.h +++ b/lib/utils_cmd.h @@ -20,7 +20,7 @@ typedef struct output output; /** prototypes **/ int cmd_run(const char *, output *, output *, int); int cmd_run_array(char *const *, output *, output *, int); -int cmd_file_read(char *, output *, int); +int cmd_file_read(const char *, output *, int); /* only multi-threaded plugins need to bother with this */ void cmd_init(void); -- cgit v1.2.3-74-g34f1 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(-) (limited to 'lib/utils_cmd.h') 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 7bfb16e0da721dcf50558f9104d3ed84efe03516 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Sun, 16 Nov 2025 14:26:41 +0100 Subject: Implement replacement functions for executing commands This commit implements replacement functions for the previous exec functions. The replacements are implemented in a more "pure" style, the do no longer receive pointer arguments which they will write to, but create the pointers themselves and should therefore be easier to use, since it is more obvious what goes in and what comes out. Also a essentialy unused variable was removed with this. --- lib/utils_cmd.c | 299 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- lib/utils_cmd.h | 10 +- plugins/runcmd.c | 5 - 3 files changed, 299 insertions(+), 15 deletions(-) (limited to 'lib/utils_cmd.h') diff --git a/lib/utils_cmd.c b/lib/utils_cmd.c index 35b83297..a0213f6b 100644 --- a/lib/utils_cmd.c +++ b/lib/utils_cmd.c @@ -36,6 +36,7 @@ * *****************************************************************************/ +#include #define NAGIOSPLUG_API_C 1 /** includes **/ @@ -79,7 +80,8 @@ static pid_t *_cmd_pids = NULL; static int _cmd_open(char *const *argv, int *pfd, int *pfderr) __attribute__((__nonnull__(1, 2, 3))); -static int _cmd_fetch_output(int fileDescriptor, output *cmd_output, int flags) __attribute__((__nonnull__(2))); +static int _cmd_fetch_output(int fileDescriptor, output *cmd_output, int flags) + __attribute__((__nonnull__(2))); static int _cmd_close(int fileDescriptor); @@ -102,14 +104,85 @@ void cmd_init(void) { } } +typedef struct { + int stdout_pipe_fd[2]; + int stderr_pipe_fd[2]; + int file_descriptor; + int error_code; +} int_cmd_open_result; +static int_cmd_open_result _cmd_open2(char *const *argv) { +#ifdef RLIMIT_CORE + struct rlimit limit; +#endif + + if (!_cmd_pids) { + CMD_INIT; + } + + setenv("LC_ALL", "C", 1); + + int_cmd_open_result result = { + .error_code = 0, + .stdout_pipe_fd = {0, 0}, + .stderr_pipe_fd = {0, 0}, + }; + pid_t pid; + if (pipe(result.stdout_pipe_fd) < 0 || pipe(result.stderr_pipe_fd) < 0 || (pid = fork()) < 0) { + result.error_code = -1; + return result; /* errno set by the failing function */ + } + + /* child runs exceve() and _exit. */ + if (pid == 0) { +#ifdef RLIMIT_CORE + /* the program we execve shouldn't leave core files */ + getrlimit(RLIMIT_CORE, &limit); + limit.rlim_cur = 0; + setrlimit(RLIMIT_CORE, &limit); +#endif + close(result.stdout_pipe_fd[0]); + if (result.stdout_pipe_fd[1] != STDOUT_FILENO) { + dup2(result.stdout_pipe_fd[1], STDOUT_FILENO); + close(result.stdout_pipe_fd[1]); + } + close(result.stderr_pipe_fd[0]); + if (result.stderr_pipe_fd[1] != STDERR_FILENO) { + dup2(result.stderr_pipe_fd[1], STDERR_FILENO); + close(result.stderr_pipe_fd[1]); + } + + /* 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 = mp_open_max(); + for (int i = 0; i < maxfd; i++) { + if (_cmd_pids[i] > 0) { + close(i); + } + } + + execve(argv[0], argv, environ); + _exit(STATE_UNKNOWN); + } + + /* parent picks up execution here */ + /* close children descriptors in our address space */ + close(result.stdout_pipe_fd[1]); + close(result.stderr_pipe_fd[1]); + + /* tag our file's entry in the pid-list and return it */ + _cmd_pids[result.stdout_pipe_fd[0]] = pid; + + result.file_descriptor = result.stdout_pipe_fd[0]; + return result; +} + /* Start running a command, array style */ static int _cmd_open(char *const *argv, int *pfd, int *pfderr) { #ifdef RLIMIT_CORE struct rlimit limit; #endif - int i = 0; - if (!_cmd_pids) { CMD_INIT; } @@ -144,7 +217,7 @@ static int _cmd_open(char *const *argv, int *pfd, int *pfderr) { * This is executed in a separate address space (pure child), * so we don't have to worry about async safety */ long maxfd = mp_open_max(); - for (i = 0; i < maxfd; i++) { + for (int i = 0; i < maxfd; i++) { if (_cmd_pids[i] > 0) { close(i); } @@ -192,6 +265,87 @@ static int _cmd_close(int fileDescriptor) { return (WIFEXITED(status)) ? WEXITSTATUS(status) : -1; } +typedef struct { + int error_code; + output output_container; +} int_cmd_fetch_output2; +static int_cmd_fetch_output2 _cmd_fetch_output2(int fileDescriptor, int flags) { + char tmpbuf[4096]; + + int_cmd_fetch_output2 result = { + .error_code = 0, + .output_container = + { + .buf = NULL, + .buflen = 0, + .line = NULL, + .lines = 0, + }, + }; + ssize_t ret; + while ((ret = read(fileDescriptor, tmpbuf, sizeof(tmpbuf))) > 0) { + size_t len = (size_t)ret; + result.output_container.buf = + realloc(result.output_container.buf, result.output_container.buflen + len + 1); + memcpy(result.output_container.buf + result.output_container.buflen, tmpbuf, len); + result.output_container.buflen += len; + } + + if (ret < 0) { + printf("read() returned %zd: %s\n", ret, strerror(errno)); + result.error_code = -1; + return result; + } + + /* 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 || !result.output_container.buf || !result.output_container.buflen) { + return result; + } + + /* and some may want both */ + char *buf = NULL; + if (flags & CMD_NO_ASSOC) { + buf = malloc(result.output_container.buflen); + memcpy(buf, result.output_container.buf, result.output_container.buflen); + } else { + buf = result.output_container.buf; + } + + result.output_container.line = NULL; + size_t ary_size = 0; /* rsf = right shift factor, dec'ed uncond once */ + size_t rsf = 6; + size_t lineno = 0; + for (size_t i = 0; i < result.output_container.buflen;) { + /* make sure we have enough memory */ + if (lineno >= ary_size) { + /* ary_size must never be zero */ + do { + ary_size = result.output_container.buflen >> --rsf; + } while (!ary_size); + + result.output_container.line = + realloc(result.output_container.line, ary_size * sizeof(char *)); + } + + /* set the pointer to the string */ + result.output_container.line[lineno] = &buf[i]; + + /* hop to next newline or end of buffer */ + while (buf[i] != '\n' && i < result.output_container.buflen) { + i++; + } + buf[i] = '\0'; + + lineno++; + i++; + } + + result.output_container.lines = lineno; + + return result; +} + static int _cmd_fetch_output(int fileDescriptor, output *cmd_output, int flags) { char tmpbuf[4096]; cmd_output->buf = NULL; @@ -225,7 +379,6 @@ static int _cmd_fetch_output(int fileDescriptor, output *cmd_output, int flags) } 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; @@ -239,7 +392,6 @@ static int _cmd_fetch_output(int fileDescriptor, output *cmd_output, int flags) } while (!ary_size); 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 */ @@ -251,9 +403,6 @@ static int _cmd_fetch_output(int fileDescriptor, output *cmd_output, int flags) } buf[i] = '\0'; - /* calculate the string length using pointer difference */ - cmd_output->lens[lineno] = (size_t)&buf[i] - (size_t)cmd_output->line[lineno]; - lineno++; i++; } @@ -336,6 +485,138 @@ int cmd_run(const char *cmdstring, output *out, output *err, int flags) { return cmd_run_array(argv, out, err, flags); } +cmd_run_result cmd_run2(const char *cmd_string, int flags) { + cmd_run_result result = { + .cmd_error_code = 0, + .error_code = 0, + .stderr = + { + .buf = NULL, + .buflen = 0, + .line = NULL, + .lines = 0, + }, + .stdout = + { + .buf = NULL, + .buflen = 0, + .line = NULL, + .lines = 0, + }, + }; + + if (cmd_string == NULL) { + result.error_code = -1; + return result; + } + + /* make copy of command string so strtok() doesn't silently modify it */ + /* (the calling program may want to access it later) */ + char *cmd = strdup(cmd_string); + if (cmd == NULL) { + result.error_code = -1; + return result; + } + + /* This is not a shell, so we don't handle "???" */ + if (strstr(cmd, "\"")) { + result.error_code = -1; + return result; + } + + /* allow single quotes, but only if non-whitesapce doesn't occur on both sides */ + if (strstr(cmd, " ' ") || strstr(cmd, "'''")) { + result.error_code = -1; + return result; + } + + /* 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 */ + size_t cmdlen = strlen(cmd_string); + size_t argc = (cmdlen >> 1) + 2; + char **argv = calloc(argc, sizeof(char *)); + + if (argv == NULL) { + printf("%s\n", _("Could not malloc argv array in popen()")); + result.error_code = -1; + return result; + } + + /* get command arguments (stupidly, but fairly quickly) */ + for (int i = 0; cmd; i++) { + char *str = cmd; + str += strspn(str, " \t\r\n"); /* trim any leading whitespace */ + + if (strstr(str, "'") == str) { /* handle SIMPLE quoted strings */ + str++; + if (!strstr(str, "'")) { + result.error_code = -1; + return result; /* balanced? */ + } + + cmd = 1 + strstr(str, "'"); + str[strcspn(str, "'")] = 0; + } else { + if (strpbrk(str, " \t\r\n")) { + cmd = 1 + strpbrk(str, " \t\r\n"); + str[strcspn(str, " \t\r\n")] = 0; + } else { + cmd = NULL; + } + } + + if (cmd && strlen(cmd) == strspn(cmd, " \t\r\n")) { + cmd = NULL; + } + + argv[i++] = str; + } + + result = cmd_run_array2(argv, flags); + + return result; +} + +cmd_run_result cmd_run_array2(char *const *cmd, int flags) { + cmd_run_result result = { + .cmd_error_code = 0, + .error_code = 0, + .stderr = + { + .buf = NULL, + .buflen = 0, + .line = NULL, + .lines = 0, + }, + .stdout = + { + .buf = NULL, + .buflen = 0, + .line = NULL, + .lines = 0, + }, + }; + + int_cmd_open_result cmd_open_result = _cmd_open2(cmd); + if (cmd_open_result.error_code != 0) { + // result.error_code = -1; + // return result; + die(STATE_UNKNOWN, _("Could not open pipe: %s\n"), cmd[0]); + } + + int file_descriptor = cmd_open_result.file_descriptor; + int pfd_out[2] = {cmd_open_result.stdout_pipe_fd[0], cmd_open_result.stdout_pipe_fd[1]}; + int pfd_err[2] = {cmd_open_result.stderr_pipe_fd[0], cmd_open_result.stderr_pipe_fd[1]}; + + int_cmd_fetch_output2 tmp_stdout = _cmd_fetch_output2(pfd_out[0], flags); + result.stdout = tmp_stdout.output_container; + int_cmd_fetch_output2 tmp_stderr = _cmd_fetch_output2(pfd_err[0], flags); + result.stderr = tmp_stderr.output_container; + + result.cmd_error_code = _cmd_close(file_descriptor); + return result; +} + int cmd_run_array(char *const *argv, output *out, output *err, int flags) { /* initialize the structs */ if (out) { diff --git a/lib/utils_cmd.h b/lib/utils_cmd.h index 3672cdc9..d3a8f14f 100644 --- a/lib/utils_cmd.h +++ b/lib/utils_cmd.h @@ -13,7 +13,6 @@ 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 */ } output; @@ -22,6 +21,15 @@ int cmd_run(const char *, output *, output *, int); int cmd_run_array(char *const *, output *, output *, int); int cmd_file_read(const char *, output *, int); +typedef struct { + int error_code; + int cmd_error_code; + output stdout; + output stderr; +} cmd_run_result; +cmd_run_result cmd_run2(const char *cmd, int flags); +cmd_run_result cmd_run_array2(char * const *cmd, int flags); + /* only multi-threaded plugins need to bother with this */ void cmd_init(void); #define CMD_INIT cmd_init() diff --git a/plugins/runcmd.c b/plugins/runcmd.c index 7c583b85..be6691d2 100644 --- a/plugins/runcmd.c +++ b/plugins/runcmd.c @@ -300,7 +300,6 @@ static int np_fetch_output(int fd, output *op, int flags) { } op->line = NULL; - op->lens = NULL; i = 0; while (i < op->buflen) { /* make sure we have enough memory */ @@ -311,7 +310,6 @@ static int np_fetch_output(int fd, output *op, int flags) { } while (!ary_size); op->line = realloc(op->line, ary_size * sizeof(char *)); - op->lens = realloc(op->lens, ary_size * sizeof(size_t)); } /* set the pointer to the string */ @@ -323,9 +321,6 @@ static int np_fetch_output(int fd, output *op, int flags) { } buf[i] = '\0'; - /* calculate the string length using pointer difference */ - op->lens[lineno] = (size_t)&buf[i] - (size_t)op->line[lineno]; - lineno++; i++; } -- cgit v1.2.3-74-g34f1 From 6ce11bc44f5fe2344083a94175a1667ca02e016c Mon Sep 17 00:00:00 2001 From: Alvar Penning Date: Thu, 11 Dec 2025 10:53:07 +0100 Subject: lib/utils_cmd: Rename stdout, stderr in cmd_run_result On OpenBSD's "stdio.h", stdin, stdout, and stderr are not directly FILE*, but #defines. Thus, naming the output struct fields stdout and stderr resulted in compiler errors, after replacing the #define. https://codeberg.org/OpenBSD/src/src/commit/a762189c5efbb2811f3c853bc0e5578fd5fb919d/include/stdio.h#L75-L77 --- lib/utils_cmd.c | 12 ++++++------ lib/utils_cmd.h | 4 ++-- plugins/check_by_ssh.c | 34 +++++++++++++++++----------------- 3 files changed, 25 insertions(+), 25 deletions(-) (limited to 'lib/utils_cmd.h') diff --git a/lib/utils_cmd.c b/lib/utils_cmd.c index 42c81793..23d42168 100644 --- a/lib/utils_cmd.c +++ b/lib/utils_cmd.c @@ -489,14 +489,14 @@ cmd_run_result cmd_run2(const char *cmd_string, int flags) { cmd_run_result result = { .cmd_error_code = 0, .error_code = 0, - .stderr = + .err = { .buf = NULL, .buflen = 0, .line = NULL, .lines = 0, }, - .stdout = + .out = { .buf = NULL, .buflen = 0, @@ -581,14 +581,14 @@ cmd_run_result cmd_run_array2(char *const *cmd, int flags) { cmd_run_result result = { .cmd_error_code = 0, .error_code = 0, - .stderr = + .err = { .buf = NULL, .buflen = 0, .line = NULL, .lines = 0, }, - .stdout = + .out = { .buf = NULL, .buflen = 0, @@ -610,9 +610,9 @@ cmd_run_result cmd_run_array2(char *const *cmd, int flags) { int pfd_err[2] = {cmd_open_result.stderr_pipe_fd[0], cmd_open_result.stderr_pipe_fd[1]}; int_cmd_fetch_output2 tmp_stdout = _cmd_fetch_output2(pfd_out[0], flags); - result.stdout = tmp_stdout.output_container; + result.out = tmp_stdout.output_container; int_cmd_fetch_output2 tmp_stderr = _cmd_fetch_output2(pfd_err[0], flags); - result.stderr = tmp_stderr.output_container; + result.err = tmp_stderr.output_container; result.cmd_error_code = _cmd_close(file_descriptor); return result; diff --git a/lib/utils_cmd.h b/lib/utils_cmd.h index d3a8f14f..04a624b8 100644 --- a/lib/utils_cmd.h +++ b/lib/utils_cmd.h @@ -24,8 +24,8 @@ int cmd_file_read(const char *, output *, int); typedef struct { int error_code; int cmd_error_code; - output stdout; - output stderr; + output out; + output err; } cmd_run_result; cmd_run_result cmd_run2(const char *cmd, int flags); cmd_run_result cmd_run_array2(char * const *cmd, int flags); diff --git a/plugins/check_by_ssh.c b/plugins/check_by_ssh.c index df8907d9..7ffa0ded 100644 --- a/plugins/check_by_ssh.c +++ b/plugins/check_by_ssh.c @@ -98,7 +98,7 @@ int main(int argc, char **argv) { if (child_result.cmd_error_code == 255 && config.unknown_timeout) { mp_subcheck sc_ssh_execution = mp_subcheck_init(); xasprintf(&sc_ssh_execution.output, "SSH connection failed: %s", - child_result.stderr.lines > 0 ? child_result.stderr.line[0] + child_result.err.lines > 0 ? child_result.err.line[0] : "(no error output)"); sc_ssh_execution = mp_set_subcheck_state(sc_ssh_execution, STATE_UNKNOWN); @@ -107,34 +107,34 @@ int main(int argc, char **argv) { } if (verbose) { - for (size_t i = 0; i < child_result.stdout.lines; i++) { - printf("stdout: %s\n", child_result.stdout.line[i]); + for (size_t i = 0; i < child_result.out.lines; i++) { + printf("stdout: %s\n", child_result.out.line[i]); } - for (size_t i = 0; i < child_result.stderr.lines; i++) { - printf("stderr: %s\n", child_result.stderr.line[i]); + for (size_t i = 0; i < child_result.err.lines; i++) { + printf("stderr: %s\n", child_result.err.line[i]); } } size_t skip_stdout = 0; if (config.skip_stdout) { /* --skip-stdout specified without argument */ - skip_stdout = child_result.stdout.lines; + skip_stdout = child_result.out.lines; } else { skip_stdout = config.stdout_lines_to_ignore; } size_t skip_stderr = 0; if (config.skip_stderr) { /* --skip-stderr specified without argument */ - skip_stderr = child_result.stderr.lines; + skip_stderr = child_result.err.lines; } else { skip_stderr = config.sterr_lines_to_ignore; } /* Allow UNKNOWN or WARNING state for (non-skipped) output found on stderr */ - if (child_result.stderr.lines > skip_stderr && + if (child_result.err.lines > skip_stderr && (config.unknown_on_stderr || config.warn_on_stderr)) { mp_subcheck sc_stderr = mp_subcheck_init(); xasprintf(&sc_stderr.output, "remote command execution failed: %s", - child_result.stderr.line[skip_stderr]); + child_result.err.line[skip_stderr]); if (config.unknown_on_stderr) { sc_stderr = mp_set_subcheck_state(sc_stderr, STATE_UNKNOWN); @@ -154,10 +154,10 @@ int main(int argc, char **argv) { mp_subcheck sc_active_check = mp_subcheck_init(); xasprintf(&sc_active_check.output, "command stdout:"); - if (child_result.stdout.lines > skip_stdout) { - for (size_t i = skip_stdout; i < child_result.stdout.lines; i++) { + if (child_result.out.lines > skip_stdout) { + for (size_t i = skip_stdout; i < child_result.out.lines; i++) { xasprintf(&sc_active_check.output, "%s\n%s", sc_active_check.output, - child_result.stdout.line[i]); + child_result.out.line[i]); } } else { xasprintf(&sc_active_check.output, "remote command '%s' returned status %d", @@ -209,10 +209,10 @@ int main(int argc, char **argv) { char *status_text; int cresult; mp_subcheck sc_parse_passive = mp_subcheck_init(); - for (size_t i = skip_stdout; i < child_result.stdout.lines; i++) { - status_text = child_result.stdout.line[i++]; - if (i == child_result.stdout.lines || - strstr(child_result.stdout.line[i], "STATUS CODE: ") == NULL) { + for (size_t i = skip_stdout; i < child_result.out.lines; i++) { + status_text = child_result.out.line[i++]; + if (i == child_result.out.lines || + strstr(child_result.out.line[i], "STATUS CODE: ") == NULL) { sc_parse_passive = mp_set_subcheck_state(sc_parse_passive, STATE_UNKNOWN); xasprintf(&sc_parse_passive.output, "failed to parse output"); @@ -221,7 +221,7 @@ int main(int argc, char **argv) { } if (config.service[commands] && status_text && - sscanf(child_result.stdout.line[i], "STATUS CODE: %d", &cresult) == 1) { + sscanf(child_result.out.line[i], "STATUS CODE: %d", &cresult) == 1) { fprintf(output_file, "[%d] PROCESS_SERVICE_CHECK_RESULT;%s;%s;%d;%s\n", (int)local_time, config.host_shortname, config.service[commands++], cresult, status_text); } -- cgit v1.2.3-74-g34f1