mirror of
https://github.com/monitoring-plugins/monitoring-plugins.git
synced 2026-02-19 02:27:55 -05:00
general refactorin in lib, more local variables, real booleans
This commit is contained in:
parent
8ef825d85f
commit
f855c5b5bb
13 changed files with 198 additions and 182 deletions
|
|
@ -29,26 +29,30 @@
|
|||
bool is_option2(char *str) {
|
||||
if (!str) {
|
||||
return false;
|
||||
} else if (strspn(str, "-") == 1 || strspn(str, "-") == 2) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (strspn(str, "-") == 1 || strspn(str, "-") == 2) {
|
||||
return true;
|
||||
}
|
||||
|
||||
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;
|
||||
|
|
|
|||
|
|
@ -19,7 +19,6 @@
|
|||
*****************************************************************************/
|
||||
|
||||
#include "./maxfd.h"
|
||||
#include <errno.h>
|
||||
|
||||
long mp_open_max(void) {
|
||||
long maxfd = 0L;
|
||||
|
|
|
|||
141
lib/parse_ini.c
141
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 valptr<lineend then we have to also allocate space for "=bar" */
|
||||
if (valptr < lineend) {
|
||||
equals = value = 1;
|
||||
equals = value = true;
|
||||
val_len = (size_t)(1 + valend - valptr);
|
||||
cfg_len += 1 + val_len;
|
||||
}
|
||||
/* if valptr==valend then we have "=" but no "bar" */
|
||||
else if (valptr == lineend) {
|
||||
equals = 1;
|
||||
} else if (valptr == lineend) {
|
||||
/* if valptr==valend then we have "=" but no "bar" */
|
||||
equals = true;
|
||||
cfg_len += 1;
|
||||
}
|
||||
|
||||
/* a line with no equal sign isn't valid */
|
||||
if (equals == 0) {
|
||||
if (!equals) {
|
||||
die(STATE_UNKNOWN, "%s\n", _("Config file error"));
|
||||
}
|
||||
|
||||
/* okay, now we have all the info we need, so we create a new np_arg_list
|
||||
* element and set the argument...
|
||||
*/
|
||||
optnew = malloc(sizeof(np_arg_list));
|
||||
np_arg_list *optnew = malloc(sizeof(np_arg_list));
|
||||
optnew->next = 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;
|
||||
|
|
|
|||
|
|
@ -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");
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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");
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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 */
|
||||
|
|
|
|||
107
lib/utils_cmd.c
107
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 <sys/wait.h>
|
||||
#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_open(char *const *argv, int *pfd, int *pfderr)
|
||||
__attribute__((__nonnull__(1, 2, 3)));
|
||||
|
||||
static int _cmd_fetch_output(int, output *, int) __attribute__((__nonnull__(2)));
|
||||
static int _cmd_fetch_output(int fileDescriptor, output *cmd_output, int flags) __attribute__((__nonnull__(2)));
|
||||
|
||||
static int _cmd_close(int);
|
||||
|
||||
/* 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]);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,17 +5,17 @@
|
|||
* Header file for Monitoring Plugins utils_cmd.c
|
||||
*
|
||||
*/
|
||||
#include "../config.h"
|
||||
#include <stddef.h>
|
||||
|
||||
/** 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);
|
||||
|
|
|
|||
|
|
@ -26,8 +26,10 @@
|
|||
*
|
||||
*****************************************************************************/
|
||||
|
||||
#include "common.h"
|
||||
#include "../config.h"
|
||||
#include "utils_tcp.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#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)) {
|
||||
return NP_MATCH_RETRY;
|
||||
} else {
|
||||
return NP_MATCH_FAILURE;
|
||||
}
|
||||
if (partial > 0 || !(flags & NP_MATCH_EXACT)) {
|
||||
return NP_MATCH_RETRY;
|
||||
}
|
||||
return NP_MATCH_FAILURE;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue