aboutsummaryrefslogtreecommitdiff
path: root/lib/parse_ini.c
diff options
context:
space:
mode:
authorGravatar Holger Weiss <holger@zedat.fu-berlin.de> 2014-06-18 00:37:45 +0200
committerGravatar Holger Weiss <holger@zedat.fu-berlin.de> 2014-06-18 00:37:45 +0200
commitf02b3f6b2866e2ccb8cdfc2257c1f9a540ddc3e9 (patch)
tree53cd212b63f38c4e70f145fb8773d46c6cdaaec0 /lib/parse_ini.c
parentb2fed383b1a04e64731333957fb098d77627cfdb (diff)
downloadmonitoring-plugins-f02b3f6b2866e2ccb8cdfc2257c1f9a540ddc3e9.tar.gz
lib/parse_ini.[ch]: Simplify code
Rewrite the code that looks up the INI configuration file path (used by the Extra-Opts feature) in order to improve readability. The behaviour should not have changed.
Diffstat (limited to 'lib/parse_ini.c')
-rw-r--r--lib/parse_ini.c119
1 files changed, 49 insertions, 70 deletions
diff --git a/lib/parse_ini.c b/lib/parse_ini.c
index 76953e9e..b6d80562 100644
--- a/lib/parse_ini.c
+++ b/lib/parse_ini.c
@@ -40,6 +40,23 @@ typedef struct {
char *stanza;
} np_ini_info;
+static char *default_ini_file_names[] = {
+ "plugins.ini",
+ "nagios-plugins.ini",
+ NULL
+};
+
+static char *default_ini_path_names[] = {
+ "/etc/nagios/plugins.ini",
+ "/usr/local/nagios/etc/plugins.ini",
+ "/usr/local/etc/nagios/plugins.ini",
+ "/etc/opt/nagios/plugins.ini",
+ "/etc/nagios-plugins.ini",
+ "/usr/local/etc/nagios-plugins.ini",
+ "/etc/opt/nagios-plugins.ini",
+ NULL
+};
+
/* eat all characters from a FILE pointer until n is encountered */
#define GOBBLE_TO(f, c, n) do { (c)=fgetc((f)); } while((c)!=EOF && (c)!=(n))
@@ -49,8 +66,6 @@ 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 test files access */
-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]
@@ -72,18 +87,17 @@ static void parse_locator(const char *locator, const char *def_stanza, np_ini_in
} else { /* otherwise we use the default stanza */
i->stanza=strdup(def_stanza);
}
+ if(i->stanza==NULL){
+ die(STATE_UNKNOWN, _("malloc() failed!\n"));
+ }
/* if there is no @file part */
if(stanza_len==locator_len){
i->file=default_file();
- if(strcmp(i->file, "") == 0){
- die(STATE_UNKNOWN, _("Cannot find '%s' or '%s' in any standard location.\n"), NP_DEFAULT_INI_FILENAME1, NP_DEFAULT_INI_FILENAME2);
- }
} else {
i->file=strdup(&(locator[stanza_len+1]));
}
-
- if(i->file==NULL || i->stanza==NULL){
- die(STATE_UNKNOWN, _("malloc() failed!\n"));
+ if(i->file==NULL || i->file[0]=='\0'){
+ die(STATE_UNKNOWN, _("Cannot find config file in any standard location.\n"));
}
}
@@ -295,71 +309,36 @@ static int add_option(FILE *f, np_arg_list **optlst){
return 0;
}
-static char* default_file(void){
- struct stat sb;
- char *np_env=NULL, *default_file=NULL;
- char temp_file[MAX_INPUT_BUFFER];
- size_t len;
-
- if((np_env=getenv("NAGIOS_CONFIG_PATH"))!=NULL) {
- /* skip any 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;
+static char *default_file_in_path(void){
+ char *config_path, **file;
+ char *dir, *ini_file, *tokens;
+
+ if((config_path=getenv("NAGIOS_CONFIG_PATH"))==NULL)
+ return NULL;
+
+ if((tokens=strdup(config_path))==NULL)
+ die(STATE_UNKNOWN, _("Insufficient Memory"));
+ for(dir=strtok(tokens, ":"); dir!=NULL; dir=strtok(NULL, ":")){
+ for(file=default_ini_file_names; *file!=NULL; file++){
+ if((asprintf(&ini_file, "%s/%s", dir, *file))<0)
+ die(STATE_UNKNOWN, _("Insufficient Memory"));
+ if(access(ini_file, F_OK)==0){
+ free(tokens);
+ return ini_file;
}
-
- /* 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);
+ }
}
-
- /* Return default_file or empty string (should return NULL if we want plugins
- * to die there)...
- */
- if(default_file)
- return default_file;
- return "";
+ free(tokens);
+ return NULL;
}
-/* 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){
-
- /* test if len + filelen + '/' + '\0' fits in temp_file */
- 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);
+static char *default_file(void){
+ char **p, *ini_file;
- if(access(temp_file, F_OK) == 0) return 1;
- return 0;
+ if((ini_file=default_file_in_path())!=NULL)
+ return ini_file;
+ for(p=default_ini_path_names; *p!=NULL; p++)
+ if (access(*p, F_OK)==0)
+ return *p;
+ return NULL;
}
-