[monitoring-plugins] lib/parse_ini.[ch]: Simplify code
Holger Weiss
git at monitoring-plugins.org
Wed Jun 18 01:40:08 CEST 2014
Module: monitoring-plugins
Branch: master
Commit: f02b3f6b2866e2ccb8cdfc2257c1f9a540ddc3e9
Author: Holger Weiss <holger at zedat.fu-berlin.de>
Date: Wed Jun 18 00:37:45 2014 +0200
URL: https://www.monitoring-plugins.org/repositories/monitoring-plugins/commit/?id=f02b3f6
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.
---
lib/parse_ini.c | 119 +++++++++++++++++++++++---------------------------------
lib/parse_ini.h | 40 -------------------
2 files changed, 49 insertions(+), 110 deletions(-)
diff --git a/lib/parse_ini.c b/lib/parse_ini.c
index 76953e9..b6d8056 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;
}
-
diff --git a/lib/parse_ini.h b/lib/parse_ini.h
index a3a494e..8b67ea3 100644
--- a/lib/parse_ini.h
+++ b/lib/parse_ini.h
@@ -13,46 +13,6 @@ typedef struct np_arg_el {
struct np_arg_el *next;
} np_arg_list;
-/* 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
-# define NP_DEFAULT_INI_FILENAME1 "plugins.ini"
-#endif /* NP_DEFAULT_INI_FILENAME1 */
-#ifndef NP_DEFAULT_INI_FILENAME2
-# define NP_DEFAULT_INI_FILENAME2 "nagios-plugins.ini"
-#endif /* NP_DEFAULT_INI_FILENAME2 */
-
-/* Config paths ending in nagios (search for NP_DEFAULT_INI_FILENAME1) */
-#ifndef NP_DEFAULT_INI_NAGIOS_PATH1
-# define NP_DEFAULT_INI_NAGIOS_PATH1 "/etc/nagios"
-#endif /* NP_DEFAULT_INI_NAGIOS_PATH1 */
-#ifndef NP_DEFAULT_INI_NAGIOS_PATH2
-# define NP_DEFAULT_INI_NAGIOS_PATH2 "/usr/local/nagios/etc"
-#endif /* NP_DEFAULT_INI_NAGIOS_PATH2 */
-#ifndef NP_DEFAULT_INI_NAGIOS_PATH3
-# define NP_DEFAULT_INI_NAGIOS_PATH3 "/usr/local/etc/nagios"
-#endif /* NP_DEFAULT_INI_NAGIOS_PATH3 */
-#ifndef NP_DEFAULT_INI_NAGIOS_PATH4
-# define NP_DEFAULT_INI_NAGIOS_PATH4 "/etc/opt/nagios"
-#endif /* NP_DEFAULT_INI_NAGIOS_PATH4 */
-
-/* Config paths not ending in nagios (search for NP_DEFAULT_INI_FILENAME2) */
-#ifndef NP_DEFAULT_INI_PATH1
-# define NP_DEFAULT_INI_PATH1 "/etc"
-#endif /* NP_DEFAULT_INI_PATH1 */
-#ifndef NP_DEFAULT_INI_PATH2
-# define NP_DEFAULT_INI_PATH2 "/usr/local/etc"
-#endif /* NP_DEFAULT_INI_PATH2 */
-#ifndef NP_DEFAULT_INI_PATH3
-# define NP_DEFAULT_INI_PATH3 "/etc/opt"
-#endif /* NP_DEFAULT_INI_PATH3 */
-
/* np_load_defaults: load the default configuration (if present) for
* a plugin from the ini file
*/
More information about the Commits
mailing list