diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/parse_ini.c | 78 | ||||
-rw-r--r-- | lib/parse_ini.h | 10 | ||||
-rw-r--r-- | lib/tests/plugins.ini | 7 | ||||
-rw-r--r-- | lib/tests/test_opts.c | 60 |
4 files changed, 138 insertions, 17 deletions
diff --git a/lib/parse_ini.c b/lib/parse_ini.c index e318ec3..67d8367 100644 --- a/lib/parse_ini.c +++ b/lib/parse_ini.c | |||
@@ -30,6 +30,10 @@ | |||
30 | #include "utils_base.h" | 30 | #include "utils_base.h" |
31 | #include <ctype.h> | 31 | #include <ctype.h> |
32 | 32 | ||
33 | #include <sys/types.h> | ||
34 | #include <sys/stat.h> | ||
35 | #include <unistd.h> | ||
36 | |||
33 | /* FIXME: N::P dies if section is not found */ | 37 | /* FIXME: N::P dies if section is not found */ |
34 | /* FIXME: N::P dies if config file is not found */ | 38 | /* FIXME: N::P dies if config file is not found */ |
35 | 39 | ||
@@ -50,6 +54,8 @@ static int read_defaults(FILE *f, const char *stanza, np_arg_list **opts); | |||
50 | static int add_option(FILE *f, np_arg_list **optlst); | 54 | static int add_option(FILE *f, np_arg_list **optlst); |
51 | /* internal function to find default file */ | 55 | /* internal function to find default file */ |
52 | static char* default_file(void); | 56 | static char* default_file(void); |
57 | /* internal function to stat() files */ | ||
58 | static int test_file(const char* env, int len, const char* file, char* temp_file); | ||
53 | 59 | ||
54 | /* parse_locator decomposes a string of the form | 60 | /* parse_locator decomposes a string of the form |
55 | * [stanza][@filename] | 61 | * [stanza][@filename] |
@@ -296,23 +302,71 @@ static int add_option(FILE *f, np_arg_list **optlst){ | |||
296 | } | 302 | } |
297 | 303 | ||
298 | static char* default_file(void){ | 304 | static char* default_file(void){ |
299 | char *np_env=NULL; | 305 | struct stat sb; |
306 | char *np_env=NULL, *default_file=NULL; | ||
307 | char temp_file[MAX_INPUT_BUFFER]; | ||
308 | size_t len; | ||
300 | 309 | ||
301 | /* FIXME: STUB */ | ||
302 | return ""; | ||
303 | #if 0 | ||
304 | if((np_env=getenv("NAGIOS_CONFIG_PATH"))!=NULL) { | 310 | if((np_env=getenv("NAGIOS_CONFIG_PATH"))!=NULL) { |
311 | /* skip ant starting colon... */ | ||
312 | while(*np_env==':') np_env++; | ||
305 | /* Look for NP_DEFAULT_INI_FILENAME1 and NP_DEFAULT_INI_FILENAME2 in | 313 | /* Look for NP_DEFAULT_INI_FILENAME1 and NP_DEFAULT_INI_FILENAME2 in |
306 | * every PATHs defined (colon-separated). | 314 | * every PATHs defined (colon-separated). |
307 | */ | 315 | */ |
316 | while((len=strcspn(np_env,":"))>0){ | ||
317 | /* Test NP_DEFAULT_INI_FILENAME[1-2] in current np_env token */ | ||
318 | if(test_file(np_env,len,NP_DEFAULT_INI_FILENAME1,temp_file)==1 || | ||
319 | test_file(np_env,len,NP_DEFAULT_INI_FILENAME2,temp_file)==1){ | ||
320 | default_file=strdup(temp_file); | ||
321 | break; | ||
322 | } | ||
323 | |||
324 | /* Move on to the next token */ | ||
325 | np_env+=len; | ||
326 | while(*np_env==':') np_env++; | ||
327 | } /* while(...) */ | ||
328 | } /* if(getenv("NAGIOS_CONFIG_PATH")) */ | ||
329 | |||
330 | /* Look for NP_DEFAULT_INI_FILENAME1 in NP_DEFAULT_INI_NAGIOS_PATH[1-4] */ | ||
331 | if(!default_file){ | ||
332 | if(test_file(NP_DEFAULT_INI_NAGIOS_PATH1,strlen(NP_DEFAULT_INI_NAGIOS_PATH1),NP_DEFAULT_INI_FILENAME1,temp_file)==1 || | ||
333 | test_file(NP_DEFAULT_INI_NAGIOS_PATH2,strlen(NP_DEFAULT_INI_NAGIOS_PATH2),NP_DEFAULT_INI_FILENAME1,temp_file)==1 || | ||
334 | test_file(NP_DEFAULT_INI_NAGIOS_PATH3,strlen(NP_DEFAULT_INI_NAGIOS_PATH3),NP_DEFAULT_INI_FILENAME1,temp_file)==1 || | ||
335 | test_file(NP_DEFAULT_INI_NAGIOS_PATH4,strlen(NP_DEFAULT_INI_NAGIOS_PATH4),NP_DEFAULT_INI_FILENAME1,temp_file)==1) | ||
336 | default_file=strdup(temp_file); | ||
337 | } | ||
338 | |||
339 | /* Look for NP_DEFAULT_INI_FILENAME2 in NP_DEFAULT_INI_PATH[1-3] */ | ||
340 | if(!default_file){ | ||
341 | if(test_file(NP_DEFAULT_INI_PATH1,strlen(NP_DEFAULT_INI_PATH1),NP_DEFAULT_INI_FILENAME2,temp_file)==1 || | ||
342 | test_file(NP_DEFAULT_INI_PATH2,strlen(NP_DEFAULT_INI_PATH2),NP_DEFAULT_INI_FILENAME2,temp_file)==1 || | ||
343 | test_file(NP_DEFAULT_INI_PATH3,strlen(NP_DEFAULT_INI_PATH3),NP_DEFAULT_INI_FILENAME2,temp_file)==1) | ||
344 | default_file=strdup(temp_file); | ||
308 | } | 345 | } |
309 | if !file_found | 346 | |
310 | search NP_DEFAULT_INI_NAGIOS_PATH[1-4] for NP_DEFAULT_INI_FILENAME1; | 347 | /* Return default_file or empty string (should return NULL if we want to |
311 | if !file_found | 348 | * die there... |
312 | search NP_DEFAULT_INI_PATH[1-3] for NP_DEFAULT_INI_FILENAME2; | 349 | */ |
313 | if !file_found | 350 | if(default_file) |
314 | return empty string (or null if we want to die); | 351 | return default_file; |
315 | return file name; | 352 | return ""; |
316 | #endif | 353 | } |
354 | |||
355 | /* put together len bytes from env and the filename and test for its | ||
356 | * existence. Returns 1 if found, 0 if not and -1 if test wasn't performed. | ||
357 | */ | ||
358 | static int test_file(const char* env, int len, const char* file, char* temp_file){ | ||
359 | struct stat sb; | ||
360 | |||
361 | /* test for len + filelen + '/' + '\0' */ | ||
362 | if((len+strlen(file)+2)>MAX_INPUT_BUFFER) return -1; | ||
363 | |||
364 | strncpy(temp_file,env,len); | ||
365 | temp_file[len]='\0'; | ||
366 | strncat(temp_file,"/",len+1); | ||
367 | strncat(temp_file,file,len+strlen(file)+1); | ||
368 | |||
369 | if(stat(temp_file, &sb) != -1) return 1; | ||
370 | return 0; | ||
317 | } | 371 | } |
318 | 372 | ||
diff --git a/lib/parse_ini.h b/lib/parse_ini.h index 61149a2..309dce5 100644 --- a/lib/parse_ini.h +++ b/lib/parse_ini.h | |||
@@ -13,10 +13,12 @@ typedef struct np_arg_el { | |||
13 | struct np_arg_el *next; | 13 | struct np_arg_el *next; |
14 | } np_arg_list; | 14 | } np_arg_list; |
15 | 15 | ||
16 | /* NP_DEFAULT_INI_PATH: compile-time default location for ini file | 16 | /* FIXME: This is in plugins/common.c. Should be eventually moved to lib/ |
17 | #ifndef NP_DEFAULT_INI_PATH | 17 | * (although for this particular one a configure settings should be ideal) |
18 | # define NP_DEFAULT_INI_PATH "/etc/nagios-plugins.ini" | 18 | */ |
19 | #endif NP_DEFAULT_INI_PATH */ | 19 | #ifndef MAX_INPUT_BUFFER |
20 | # define MAX_INPUT_BUFFER 8192 | ||
21 | #endif /* MAX_INPUT_BUFFER */ | ||
20 | 22 | ||
21 | /* Filenames (see below) */ | 23 | /* Filenames (see below) */ |
22 | #ifndef NP_DEFAULT_INI_FILENAME1 | 24 | #ifndef NP_DEFAULT_INI_FILENAME1 |
diff --git a/lib/tests/plugins.ini b/lib/tests/plugins.ini new file mode 100644 index 0000000..5e20ded --- /dev/null +++ b/lib/tests/plugins.ini | |||
@@ -0,0 +1,7 @@ | |||
1 | |||
2 | [check_disk] | ||
3 | foo=bar | ||
4 | |||
5 | [section1] | ||
6 | foobar=baz | ||
7 | |||
diff --git a/lib/tests/test_opts.c b/lib/tests/test_opts.c index 1272605..769a2df 100644 --- a/lib/tests/test_opts.c +++ b/lib/tests/test_opts.c | |||
@@ -59,7 +59,7 @@ main (int argc, char **argv) | |||
59 | char **argv_test=NULL, **argv_known=NULL; | 59 | char **argv_test=NULL, **argv_known=NULL; |
60 | int i, argc_test, argc_new; | 60 | int i, argc_test, argc_new; |
61 | 61 | ||
62 | plan_tests(8); | 62 | plan_tests(11); |
63 | 63 | ||
64 | argv_test=(char **)malloc(2*sizeof(char **)); | 64 | argv_test=(char **)malloc(2*sizeof(char **)); |
65 | argv_test[0] = "prog_name"; | 65 | argv_test[0] = "prog_name"; |
@@ -203,6 +203,64 @@ main (int argc, char **argv) | |||
203 | ok(array_diff(argc_new, argv_test, 4, argv_known), "Missing section 3"); | 203 | ok(array_diff(argc_new, argv_test, 4, argv_known), "Missing section 3"); |
204 | my_free(&argc_new,argv_test); | 204 | my_free(&argc_new,argv_test); |
205 | 205 | ||
206 | setenv("NAGIOS_CONFIG_PATH", ".", 1); | ||
207 | argv_test=(char **)malloc(6*sizeof(char **)); | ||
208 | argv_test[0] = "prog_name"; | ||
209 | argv_test[1] = "arg1"; | ||
210 | argv_test[2] = "--extra-opts"; | ||
211 | argv_test[3] = "--arg3"; | ||
212 | argv_test[4] = "val2"; | ||
213 | argv_test[5] = NULL; | ||
214 | argc_test=5; | ||
215 | argv_known=(char **)realloc(argv_known, 6*sizeof(char **)); | ||
216 | argv_known[0] = "prog_name"; | ||
217 | argv_known[1] = "--foo=bar"; | ||
218 | argv_known[2] = "arg1"; | ||
219 | argv_known[3] = "--arg3"; | ||
220 | argv_known[4] = "val2"; | ||
221 | argv_known[5] = NULL; | ||
222 | argv_test=np_extra_opts(argc_test, argv_test, "check_disk", &argc_new); | ||
223 | ok(array_diff(argc_new, argv_test, 5, argv_known), "Default section 1"); | ||
224 | my_free(&argc_new,argv_test); | ||
225 | |||
226 | argv_test=(char **)malloc(6*sizeof(char **)); | ||
227 | argv_test[0] = "prog_name"; | ||
228 | argv_test[1] = "arg1"; | ||
229 | argv_test[2] = "--extra-opts=section1"; | ||
230 | argv_test[3] = "--arg3"; | ||
231 | argv_test[4] = "val2"; | ||
232 | argv_test[5] = NULL; | ||
233 | argc_test=5; | ||
234 | argv_known=(char **)realloc(argv_known, 6*sizeof(char **)); | ||
235 | argv_known[0] = "prog_name"; | ||
236 | argv_known[1] = "--foobar=baz"; | ||
237 | argv_known[2] = "arg1"; | ||
238 | argv_known[3] = "--arg3"; | ||
239 | argv_known[4] = "val2"; | ||
240 | argv_known[5] = NULL; | ||
241 | argv_test=np_extra_opts(argc_test, argv_test, "check_disk", &argc_new); | ||
242 | ok(array_diff(argc_new, argv_test, 5, argv_known), "Default section 2"); | ||
243 | my_free(&argc_new,argv_test); | ||
244 | |||
245 | argv_test=(char **)malloc(6*sizeof(char **)); | ||
246 | argv_test[0] = "prog_name"; | ||
247 | argv_test[1] = "arg1"; | ||
248 | argv_test[2] = "--extra-opts=section2"; | ||
249 | argv_test[3] = "--arg3"; | ||
250 | argv_test[4] = "val2"; | ||
251 | argv_test[5] = NULL; | ||
252 | argc_test=5; | ||
253 | argv_known=(char **)realloc(argv_known, 6*sizeof(char **)); | ||
254 | argv_known[0] = "prog_name"; | ||
255 | argv_known[1] = "--foo=bar"; | ||
256 | argv_known[2] = "arg1"; | ||
257 | argv_known[3] = "--arg3"; | ||
258 | argv_known[4] = "val2"; | ||
259 | argv_known[5] = NULL; | ||
260 | argv_test=np_extra_opts(argc_test, argv_test, "check_disk", &argc_new); | ||
261 | ok(array_diff(argc_new, argv_test, 5, argv_known), "Default section 3"); | ||
262 | my_free(&argc_new,argv_test); | ||
263 | |||
206 | return exit_status(); | 264 | return exit_status(); |
207 | } | 265 | } |
208 | 266 | ||