From af71e9fc0ef268b322c4f4677773273c07926a7a Mon Sep 17 00:00:00 2001 From: Thomas Guyot-Sionnest Date: Wed, 26 Mar 2008 09:46:27 +0000 Subject: write ini-file lookup function + tests git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@1962 f882894a-f735-0410-b71e-b25c423dba1c --- lib/parse_ini.c | 78 +++++++++++++++++++++++++++++++++++++++++++-------- lib/parse_ini.h | 10 ++++--- lib/tests/plugins.ini | 7 +++++ lib/tests/test_opts.c | 60 ++++++++++++++++++++++++++++++++++++++- 4 files changed, 138 insertions(+), 17 deletions(-) create mode 100644 lib/tests/plugins.ini diff --git a/lib/parse_ini.c b/lib/parse_ini.c index e318ec32..67d8367f 100644 --- a/lib/parse_ini.c +++ b/lib/parse_ini.c @@ -30,6 +30,10 @@ #include "utils_base.h" #include +#include +#include +#include + /* FIXME: N::P dies if section is not found */ /* FIXME: N::P dies if config file is not found */ @@ -50,6 +54,8 @@ static int read_defaults(FILE *f, const char *stanza, np_arg_list **opts); static int add_option(FILE *f, np_arg_list **optlst); /* internal function to find default file */ static char* default_file(void); +/* internal function to stat() files */ +static int test_file(const char* env, int len, const char* file, char* temp_file); /* parse_locator decomposes a string of the form * [stanza][@filename] @@ -296,23 +302,71 @@ static int add_option(FILE *f, np_arg_list **optlst){ } static char* default_file(void){ - char *np_env=NULL; + struct stat sb; + char *np_env=NULL, *default_file=NULL; + char temp_file[MAX_INPUT_BUFFER]; + size_t len; - /* FIXME: STUB */ - return ""; -#if 0 if((np_env=getenv("NAGIOS_CONFIG_PATH"))!=NULL) { + /* skip ant starting colon... */ + while(*np_env==':') np_env++; /* Look for NP_DEFAULT_INI_FILENAME1 and NP_DEFAULT_INI_FILENAME2 in * every PATHs defined (colon-separated). */ + while((len=strcspn(np_env,":"))>0){ + /* Test NP_DEFAULT_INI_FILENAME[1-2] in current np_env token */ + if(test_file(np_env,len,NP_DEFAULT_INI_FILENAME1,temp_file)==1 || + test_file(np_env,len,NP_DEFAULT_INI_FILENAME2,temp_file)==1){ + default_file=strdup(temp_file); + break; + } + + /* Move on to the next token */ + np_env+=len; + while(*np_env==':') np_env++; + } /* while(...) */ + } /* if(getenv("NAGIOS_CONFIG_PATH")) */ + + /* Look for NP_DEFAULT_INI_FILENAME1 in NP_DEFAULT_INI_NAGIOS_PATH[1-4] */ + if(!default_file){ + if(test_file(NP_DEFAULT_INI_NAGIOS_PATH1,strlen(NP_DEFAULT_INI_NAGIOS_PATH1),NP_DEFAULT_INI_FILENAME1,temp_file)==1 || + test_file(NP_DEFAULT_INI_NAGIOS_PATH2,strlen(NP_DEFAULT_INI_NAGIOS_PATH2),NP_DEFAULT_INI_FILENAME1,temp_file)==1 || + test_file(NP_DEFAULT_INI_NAGIOS_PATH3,strlen(NP_DEFAULT_INI_NAGIOS_PATH3),NP_DEFAULT_INI_FILENAME1,temp_file)==1 || + test_file(NP_DEFAULT_INI_NAGIOS_PATH4,strlen(NP_DEFAULT_INI_NAGIOS_PATH4),NP_DEFAULT_INI_FILENAME1,temp_file)==1) + default_file=strdup(temp_file); + } + + /* Look for NP_DEFAULT_INI_FILENAME2 in NP_DEFAULT_INI_PATH[1-3] */ + if(!default_file){ + if(test_file(NP_DEFAULT_INI_PATH1,strlen(NP_DEFAULT_INI_PATH1),NP_DEFAULT_INI_FILENAME2,temp_file)==1 || + test_file(NP_DEFAULT_INI_PATH2,strlen(NP_DEFAULT_INI_PATH2),NP_DEFAULT_INI_FILENAME2,temp_file)==1 || + test_file(NP_DEFAULT_INI_PATH3,strlen(NP_DEFAULT_INI_PATH3),NP_DEFAULT_INI_FILENAME2,temp_file)==1) + default_file=strdup(temp_file); } - if !file_found - search NP_DEFAULT_INI_NAGIOS_PATH[1-4] for NP_DEFAULT_INI_FILENAME1; - if !file_found - search NP_DEFAULT_INI_PATH[1-3] for NP_DEFAULT_INI_FILENAME2; - if !file_found - return empty string (or null if we want to die); - return file name; -#endif + + /* Return default_file or empty string (should return NULL if we want to + * die there... + */ + if(default_file) + return default_file; + return ""; +} + +/* put together len bytes from env and the filename and test for its + * existence. Returns 1 if found, 0 if not and -1 if test wasn't performed. + */ +static int test_file(const char* env, int len, const char* file, char* temp_file){ + struct stat sb; + + /* test for len + filelen + '/' + '\0' */ + if((len+strlen(file)+2)>MAX_INPUT_BUFFER) return -1; + + strncpy(temp_file,env,len); + temp_file[len]='\0'; + strncat(temp_file,"/",len+1); + strncat(temp_file,file,len+strlen(file)+1); + + if(stat(temp_file, &sb) != -1) return 1; + return 0; } diff --git a/lib/parse_ini.h b/lib/parse_ini.h index 61149a21..309dce52 100644 --- a/lib/parse_ini.h +++ b/lib/parse_ini.h @@ -13,10 +13,12 @@ typedef struct np_arg_el { struct np_arg_el *next; } np_arg_list; -/* NP_DEFAULT_INI_PATH: compile-time default location for ini file -#ifndef NP_DEFAULT_INI_PATH -# define NP_DEFAULT_INI_PATH "/etc/nagios-plugins.ini" -#endif NP_DEFAULT_INI_PATH */ +/* FIXME: This is in plugins/common.c. Should be eventually moved to lib/ + * (although for this particular one a configure settings should be ideal) + */ +#ifndef MAX_INPUT_BUFFER +# define MAX_INPUT_BUFFER 8192 +#endif /* MAX_INPUT_BUFFER */ /* Filenames (see below) */ #ifndef NP_DEFAULT_INI_FILENAME1 diff --git a/lib/tests/plugins.ini b/lib/tests/plugins.ini new file mode 100644 index 00000000..5e20deda --- /dev/null +++ b/lib/tests/plugins.ini @@ -0,0 +1,7 @@ + +[check_disk] +foo=bar + +[section1] +foobar=baz + diff --git a/lib/tests/test_opts.c b/lib/tests/test_opts.c index 12726059..769a2df2 100644 --- a/lib/tests/test_opts.c +++ b/lib/tests/test_opts.c @@ -59,7 +59,7 @@ main (int argc, char **argv) char **argv_test=NULL, **argv_known=NULL; int i, argc_test, argc_new; - plan_tests(8); + plan_tests(11); argv_test=(char **)malloc(2*sizeof(char **)); argv_test[0] = "prog_name"; @@ -203,6 +203,64 @@ main (int argc, char **argv) ok(array_diff(argc_new, argv_test, 4, argv_known), "Missing section 3"); my_free(&argc_new,argv_test); + setenv("NAGIOS_CONFIG_PATH", ".", 1); + argv_test=(char **)malloc(6*sizeof(char **)); + argv_test[0] = "prog_name"; + argv_test[1] = "arg1"; + argv_test[2] = "--extra-opts"; + argv_test[3] = "--arg3"; + argv_test[4] = "val2"; + argv_test[5] = NULL; + argc_test=5; + argv_known=(char **)realloc(argv_known, 6*sizeof(char **)); + argv_known[0] = "prog_name"; + argv_known[1] = "--foo=bar"; + argv_known[2] = "arg1"; + argv_known[3] = "--arg3"; + argv_known[4] = "val2"; + argv_known[5] = NULL; + argv_test=np_extra_opts(argc_test, argv_test, "check_disk", &argc_new); + ok(array_diff(argc_new, argv_test, 5, argv_known), "Default section 1"); + my_free(&argc_new,argv_test); + + argv_test=(char **)malloc(6*sizeof(char **)); + argv_test[0] = "prog_name"; + argv_test[1] = "arg1"; + argv_test[2] = "--extra-opts=section1"; + argv_test[3] = "--arg3"; + argv_test[4] = "val2"; + argv_test[5] = NULL; + argc_test=5; + argv_known=(char **)realloc(argv_known, 6*sizeof(char **)); + argv_known[0] = "prog_name"; + argv_known[1] = "--foobar=baz"; + argv_known[2] = "arg1"; + argv_known[3] = "--arg3"; + argv_known[4] = "val2"; + argv_known[5] = NULL; + argv_test=np_extra_opts(argc_test, argv_test, "check_disk", &argc_new); + ok(array_diff(argc_new, argv_test, 5, argv_known), "Default section 2"); + my_free(&argc_new,argv_test); + + argv_test=(char **)malloc(6*sizeof(char **)); + argv_test[0] = "prog_name"; + argv_test[1] = "arg1"; + argv_test[2] = "--extra-opts=section2"; + argv_test[3] = "--arg3"; + argv_test[4] = "val2"; + argv_test[5] = NULL; + argc_test=5; + argv_known=(char **)realloc(argv_known, 6*sizeof(char **)); + argv_known[0] = "prog_name"; + argv_known[1] = "--foo=bar"; + argv_known[2] = "arg1"; + argv_known[3] = "--arg3"; + argv_known[4] = "val2"; + argv_known[5] = NULL; + argv_test=np_extra_opts(argc_test, argv_test, "check_disk", &argc_new); + ok(array_diff(argc_new, argv_test, 5, argv_known), "Default section 3"); + my_free(&argc_new,argv_test); + return exit_status(); } -- cgit v1.2.3-74-g34f1