summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorMatthias Eble <psychotrahe@gmx.de>2009-06-11 15:28:14 (GMT)
committerMatthias Eble <psychotrahe@gmx.de>2009-06-11 15:28:14 (GMT)
commit5a6268f72953d7d0aa0f22f278ac929b1f183686 (patch)
tree2653c52c368a8cee0110265f98ed662698901249 /lib
parentc66d2c6b3b6d948ff36126f5d3a7629279f47878 (diff)
parent6e5d12b5d0345e61425399aff22c5a54fbebaf9e (diff)
downloadmonitoring-plugins-5a6268f72953d7d0aa0f22f278ac929b1f183686.tar.gz
Merge branch 'master' of ssh://PsychoTrahe@repo.or.cz/srv/git/nagiosplugins
Diffstat (limited to 'lib')
-rw-r--r--lib/extra_opts.h5
-rw-r--r--lib/tests/test_opts1.c159
-rw-r--r--lib/tests/test_opts2.c146
-rw-r--r--lib/utils_base.c4
4 files changed, 140 insertions, 174 deletions
diff --git a/lib/extra_opts.h b/lib/extra_opts.h
index 0cb47fb..4bb7b73 100644
--- a/lib/extra_opts.h
+++ b/lib/extra_opts.h
@@ -8,11 +8,14 @@
8 8
9/* np_extra_opts: Process the --extra-opts arguments and create a new argument 9/* np_extra_opts: Process the --extra-opts arguments and create a new argument
10 * array with ini-processed and argument-passed arguments together. The 10 * array with ini-processed and argument-passed arguments together. The
11 * ini-procesed arguments always come first (in the ord of --extra-opts 11 * ini-procesed arguments always come first (in the order of --extra-opts
12 * arguments). If no --extra-opts arguments are provided or returned nothing 12 * arguments). If no --extra-opts arguments are provided or returned nothing
13 * it returns **argv otherwise the new array is returned. --extra-opts are 13 * it returns **argv otherwise the new array is returned. --extra-opts are
14 * always removed from **argv. The original pointers from **argv are kept in 14 * always removed from **argv. The original pointers from **argv are kept in
15 * the new array to preserve ability to overwrite arguments in processlist. 15 * the new array to preserve ability to overwrite arguments in processlist.
16 *
17 * The new array can be easily freed as long as a pointer to the original one
18 * is kept. See my_free() in lib/tests/test_opts1.c for an example.
16 */ 19 */
17char **np_extra_opts(int *argc, char **argv, const char *plugin_name); 20char **np_extra_opts(int *argc, char **argv, const char *plugin_name);
18 21
diff --git a/lib/tests/test_opts1.c b/lib/tests/test_opts1.c
index 0b91622..077c5b6 100644
--- a/lib/tests/test_opts1.c
+++ b/lib/tests/test_opts1.c
@@ -21,14 +21,44 @@
21 21
22#include "tap.h" 22#include "tap.h"
23 23
24void my_free(int *argc, char **argv) { 24/* This my_free would be the most efficient way of freeing an extra-opts array,
25 * provided as an example */
26#if 0
27void my_free(int *argc, char **newargv, char **argv) {
25 int i; 28 int i;
26 printf (" Arg(%i): ", *argc); 29 /* Return if both arrays are the same */
27 for (i=1; i<*argc; i++) printf ("'%s' ", argv[i]); 30 if (newargv == argv) return;
31
32 for (i=1; i<*argc; i++) {
33 /* Free array items until we reach the start of the original argv */
34 if (newargv[i] == argv[1]) break;
35 free(newargv[i]);
36 }
37 /* Free the array itself */
38 free(newargv);
39}
40#else
41void my_free(int *argc, char **newargv, char **argv) {
42 /* Free stuff (and print while we're at it) */
43 int i, freeflag=1;
44 printf (" Arg(%i): ", *argc+1);
45 printf ("'%s' ", newargv[0]);
46 for (i=1; i<*argc; i++) {
47 printf ("'%s' ", newargv[i]);
48 /* Stop freeing when we get to the start of the original array */
49 if (freeflag) {
50 if (newargv[i] == argv[1])
51 freeflag=0;
52 else
53 free(newargv[i]);
54 }
55 }
28 printf ("\n"); 56 printf ("\n");
29 free(argv); 57 /* Free only if it's a different array */
58 if (newargv != argv) free(newargv);
30 *argc=0; 59 *argc=0;
31} 60}
61#endif
32 62
33int array_diff(int i1, char **a1, int i2, char **a2) { 63int array_diff(int i1, char **a1, int i2, char **a2) {
34 int i; 64 int i;
@@ -54,94 +84,55 @@ int array_diff(int i1, char **a1, int i2, char **a2) {
54int 84int
55main (int argc, char **argv) 85main (int argc, char **argv)
56{ 86{
57 char **argv_test=NULL, **argv_known=NULL; 87 char **argv_new=NULL;
58 int i, argc_test; 88 int i, argc_test;
59 89
60 plan_tests(5); 90 plan_tests(5);
61 91
62 argv_test=(char **)malloc(2*sizeof(char **)); 92 {
63 argv_test[0] = "prog_name"; 93 char *argv_test[] = {"prog_name", (char *) NULL};
64 argv_test[1] = NULL; 94 argc_test=1;
65 argc_test=1; 95 char *argv_known[] = {"prog_name", (char *) NULL};
66 argv_known=(char **)realloc(argv_known, 2*sizeof(char **)); 96 argv_new=np_extra_opts(&argc_test, argv_test, "check_disk");
67 argv_known[0] = "prog_name"; 97 ok(array_diff(argc_test, argv_new, 1, argv_known), "No opts, returns correct argv/argc");
68 argv_known[1] = NULL; 98 my_free(&argc_test, argv_new, argv_test);
69 argv_test=np_extra_opts(&argc_test, argv_test, "check_disk"); 99 }
70 ok(array_diff(argc_test, argv_test, 1, argv_known), "No opts, returns correct argv/argc");
71 my_free(&argc_test, argv_test);
72 100
73 argv_test=(char **)malloc(6*sizeof(char **)); 101 {
74 argv_test[0] = "prog_name"; 102 char *argv_test[] = {"prog_name", "arg1", "--arg2=val1", "--arg3", "val2", (char *) NULL};
75 argv_test[1] = "arg1"; 103 argc_test=5;
76 argv_test[2] = "--arg2=val1"; 104 char *argv_known[] = {"prog_name", "arg1", "--arg2=val1", "--arg3", "val2", (char *) NULL};
77 argv_test[3] = "--arg3"; 105 argv_new=np_extra_opts(&argc_test, argv_test, "check_disk");
78 argv_test[4] = "val2"; 106 ok(array_diff(argc_test, argv_new, 5, argv_known), "No extra opts, verbatim copy of argv");
79 argv_test[5] = NULL; 107 my_free(&argc_test, argv_new, argv_test);
80 argc_test=5; 108 }
81 argv_known=(char **)realloc(argv_known, 6*sizeof(char **));
82 argv_known[0] = "prog_name";
83 argv_known[1] = "arg1";
84 argv_known[2] = "--arg2=val1";
85 argv_known[3] = "--arg3";
86 argv_known[4] = "val2";
87 argv_known[5] = NULL;
88 argv_test=np_extra_opts(&argc_test, argv_test, "check_disk");
89 ok(array_diff(argc_test, argv_test, 5, argv_known), "No extra opts, verbatim copy of argv");
90 my_free(&argc_test,argv_test);
91 109
92 argv_test=(char **)malloc(3*sizeof(char **)); 110 {
93 argv_test[0] = "prog_name"; 111 char *argv_test[] = {"prog_name", "--extra-opts=@./config-opts.ini", (char *) NULL};
94 argv_test[1] = "--extra-opts=@./config-opts.ini"; 112 argc_test=2;
95 argv_test[2] = NULL; 113 char *argv_known[] = {"prog_name", "--foo=Bar", "--this=Your Mother!", "--blank", (char *) NULL};
96 argc_test=2; 114 argv_new=np_extra_opts(&argc_test, argv_test, "check_disk");
97 argv_known=(char **)realloc(argv_known, 5*sizeof(char **)); 115 ok(array_diff(argc_test, argv_new, 4, argv_known), "Only extra opts using default section");
98 argv_known[0] = "prog_name"; 116 my_free(&argc_test, argv_new, argv_test);
99 argv_known[1] = "--foo=Bar"; 117 }
100 argv_known[2] = "--this=Your Mother!";
101 argv_known[3] = "--blank";
102 argv_known[4] = NULL;
103 argv_test=np_extra_opts(&argc_test, argv_test, "check_disk");
104 ok(array_diff(argc_test, argv_test, 4, argv_known), "Only extra opts using default section");
105 my_free(&argc_test,argv_test);
106 118
107 argv_test=(char **)malloc(5*sizeof(char **)); 119 {
108 argv_test[0] = "prog_name"; 120 char *argv_test[] = {"prog_name", "--extra-opts=sect1@./config-opts.ini", "--extra-opts", "sect2@./config-opts.ini", (char *) NULL};
109 argv_test[1] = "--extra-opts=sect1@./config-opts.ini"; 121 argc_test=4;
110 argv_test[2] = "--extra-opts"; 122 char *argv_known[] = {"prog_name", "--one=two", "--something else=oops", "--this=that", (char *) NULL};
111 argv_test[3] = "sect2@./config-opts.ini"; 123 argv_new=np_extra_opts(&argc_test, argv_test, "check_disk");
112 argv_test[4] = NULL; 124 ok(array_diff(argc_test, argv_new, 4, argv_known), "Only extra opts specified twice");
113 argc_test=4; 125 my_free(&argc_test, argv_new, argv_test);
114 argv_known=(char **)realloc(argv_known, 5*sizeof(char **)); 126 }
115 argv_known[0] = "prog_name";
116 argv_known[1] = "--one=two";
117 argv_known[2] = "--something else=oops";
118 argv_known[3] = "--this=that";
119 argv_known[4] = NULL;
120 argv_test=np_extra_opts(&argc_test, argv_test, "check_disk");
121 ok(array_diff(argc_test, argv_test, 4, argv_known), "Only extra opts specified twice");
122 my_free(&argc_test,argv_test);
123 127
124 argv_test=(char **)malloc(7*sizeof(char **)); 128 {
125 argv_test[0] = "prog_name"; 129 char *argv_test[] = {"prog_name", "--arg1=val1", "--extra-opts=@./config-opts.ini", "--extra-opts", "sect1@./config-opts.ini", "--arg2", (char *) NULL};
126 argv_test[1] = "--arg1=val1"; 130 argc_test=6;
127 argv_test[2] = "--extra-opts=@./config-opts.ini"; 131 char *argv_known[] = {"prog_name", "--foo=Bar", "--this=Your Mother!", "--blank", "--one=two", "--arg1=val1", "--arg2", (char *) NULL};
128 argv_test[3] = "--extra-opts"; 132 argv_new=np_extra_opts(&argc_test, argv_test, "check_disk");
129 argv_test[4] = "sect1@./config-opts.ini"; 133 ok(array_diff(argc_test, argv_new, 7, argv_known), "twice extra opts using two sections");
130 argv_test[5] = "--arg2"; 134 my_free(&argc_test, argv_new, argv_test);
131 argv_test[6] = NULL; 135 }
132 argc_test=6;
133 argv_known=(char **)realloc(argv_known, 8*sizeof(char **));
134 argv_known[0] = "prog_name";
135 argv_known[1] = "--foo=Bar";
136 argv_known[2] = "--this=Your Mother!";
137 argv_known[3] = "--blank";
138 argv_known[4] = "--one=two";
139 argv_known[5] = "--arg1=val1";
140 argv_known[6] = "--arg2";
141 argv_known[7] = NULL;
142 argv_test=np_extra_opts(&argc_test, argv_test, "check_disk");
143 ok(array_diff(argc_test, argv_test, 7, argv_known), "twice extra opts using two sections");
144 my_free(&argc_test,argv_test);
145 136
146 return exit_status(); 137 return exit_status();
147} 138}
diff --git a/lib/tests/test_opts2.c b/lib/tests/test_opts2.c
index e50f23e..c3d2067 100644
--- a/lib/tests/test_opts2.c
+++ b/lib/tests/test_opts2.c
@@ -21,12 +21,24 @@
21 21
22#include "tap.h" 22#include "tap.h"
23 23
24void my_free(int *argc, char **argv) { 24void my_free(int *argc, char **newargv, char **argv) {
25 int i; 25 /* Free stuff (and print while we're at it) */
26 printf (" Arg(%i): ", *argc); 26 int i, freeflag=1;
27 for (i=1; i<*argc; i++) printf ("'%s' ", argv[i]); 27 printf (" Arg(%i): ", *argc+1);
28 printf ("'%s' ", newargv[0]);
29 for (i=1; i<*argc; i++) {
30 printf ("'%s' ", newargv[i]);
31 /* Stop freeing when we get to the start of the original array */
32 if (freeflag) {
33 if (newargv[i] == argv[1])
34 freeflag=0;
35 else
36 free(newargv[i]);
37 }
38 }
28 printf ("\n"); 39 printf ("\n");
29 free(argv); 40 /* Free only if it's a different array */
41 if (newargv != argv) free(newargv);
30 *argc=0; 42 *argc=0;
31} 43}
32 44
@@ -54,97 +66,55 @@ int array_diff(int i1, char **a1, int i2, char **a2) {
54int 66int
55main (int argc, char **argv) 67main (int argc, char **argv)
56{ 68{
57 char **argv_test=NULL, **argv_known=NULL; 69 char **argv_new=NULL;
58 int i, argc_test; 70 int i, argc_test;
59 71
60 plan_tests(5); 72 plan_tests(5);
61 73
62 argv_test=(char **)malloc(6*sizeof(char **)); 74 {
63 argv_test[0] = "prog_name"; 75 char *argv_test[] = {"prog_name", "arg1", "--extra-opts", "--arg3", "val2", (char *) NULL};
64 argv_test[1] = "arg1"; 76 argc_test=5;
65 argv_test[2] = "--extra-opts"; 77 char *argv_known[] = {"prog_name", "--foo=bar", "arg1", "--arg3", "val2", (char *) NULL};
66 argv_test[3] = "--arg3"; 78 argv_new=np_extra_opts(&argc_test, argv_test, "check_disk");
67 argv_test[4] = "val2"; 79 ok(array_diff(argc_test, argv_new, 5, argv_known), "Default section 1");
68 argv_test[5] = NULL; 80 my_free(&argc_test, argv_new, argv_test);
69 argc_test=5; 81 }
70 argv_known=(char **)realloc(argv_known, 6*sizeof(char **));
71 argv_known[0] = "prog_name";
72 argv_known[1] = "--foo=bar";
73 argv_known[2] = "arg1";
74 argv_known[3] = "--arg3";
75 argv_known[4] = "val2";
76 argv_known[5] = NULL;
77 argv_test=np_extra_opts(&argc_test, argv_test, "check_disk");
78 ok(array_diff(argc_test, argv_test, 5, argv_known), "Default section 1");
79 82
80 argv_test=(char **)malloc(3*sizeof(char **)); 83 {
81 argv_test[0] = "prog_name"; 84 char *argv_test[] = {"prog_name", "--extra-opts", (char *) NULL};
82 argv_test[1] = "--extra-opts"; 85 argc_test=2;
83 argv_test[2] = NULL; 86 char *argv_known[] = {"prog_name", "--foo=bar", (char *) NULL};
84 argc_test=2; 87 argv_new=np_extra_opts(&argc_test, argv_test, "check_disk");
85 argv_known=(char **)realloc(argv_known, 3*sizeof(char **)); 88 ok(array_diff(argc_test, argv_new, 2, argv_known), "Default section 2");
86 argv_known[0] = "prog_name"; 89 my_free(&argc_test, argv_new, argv_test);
87 argv_known[1] = "--foo=bar"; 90 }
88 argv_known[2] = NULL;
89 argv_test=np_extra_opts(&argc_test, argv_test, "check_disk");
90 ok(array_diff(argc_test, argv_test, 2, argv_known), "Default section 2");
91 my_free(&argc_test,argv_test);
92 91
93 argv_test=(char **)malloc(6*sizeof(char **)); 92 {
94 argv_test[0] = "prog_name"; 93 char *argv_test[] = {"prog_name", "arg1", "--extra-opts=section1", "--arg3", "val2", (char *) NULL};
95 argv_test[1] = "arg1"; 94 argc_test=5;
96 argv_test[2] = "--extra-opts=section1"; 95 char *argv_known[] = {"prog_name", "--foobar=baz", "arg1", "--arg3", "val2", (char *) NULL};
97 argv_test[3] = "--arg3"; 96 argv_new=np_extra_opts(&argc_test, argv_test, "check_disk");
98 argv_test[4] = "val2"; 97 ok(array_diff(argc_test, argv_new, 5, argv_known), "Default section 3");
99 argv_test[5] = NULL; 98 my_free(&argc_test, argv_new, argv_test);
100 argc_test=5; 99 }
101 argv_known=(char **)realloc(argv_known, 6*sizeof(char **));
102 argv_known[0] = "prog_name";
103 argv_known[1] = "--foobar=baz";
104 argv_known[2] = "arg1";
105 argv_known[3] = "--arg3";
106 argv_known[4] = "val2";
107 argv_known[5] = NULL;
108 argv_test=np_extra_opts(&argc_test, argv_test, "check_disk");
109 ok(array_diff(argc_test, argv_test, 5, argv_known), "Default section 3");
110 my_free(&argc_test,argv_test);
111 100
112 argv_test=(char **)malloc(6*sizeof(char **)); 101 {
113 argv_test[0] = "prog_name"; 102 char *argv_test[] = {"prog_name", "arg1", "--extra-opts", "-arg3", "val2", (char *) NULL};
114 argv_test[1] = "arg1"; 103 argc_test=5;
115 argv_test[2] = "--extra-opts"; 104 char *argv_known[] = {"prog_name", "--foo=bar", "arg1", "-arg3", "val2", (char *) NULL};
116 argv_test[3] = "-arg3"; 105 argv_new=np_extra_opts(&argc_test, argv_test, "check_disk");
117 argv_test[4] = "val2"; 106 ok(array_diff(argc_test, argv_new, 5, argv_known), "Default section 4");
118 argv_test[5] = NULL; 107 my_free(&argc_test, argv_new, argv_test);
119 argc_test=5; 108 }
120 argv_known=(char **)realloc(argv_known, 6*sizeof(char **));
121 argv_known[0] = "prog_name";
122 argv_known[1] = "--foo=bar";
123 argv_known[2] = "arg1";
124 argv_known[3] = "-arg3";
125 argv_known[4] = "val2";
126 argv_known[5] = NULL;
127 argv_test=np_extra_opts(&argc_test, argv_test, "check_disk");
128 ok(array_diff(argc_test, argv_test, 5, argv_known), "Default section 4");
129 my_free(&argc_test,argv_test);
130 109
131 argv_test=(char **)malloc(4*sizeof(char **)); 110 {
132 argv_test[0] = "check_tcp"; 111 char *argv_test[] = {"check_tcp", "--extra-opts", "--extra-opts=tcp_long_lines", (char *) NULL};
133 argv_test[1] = "--extra-opts"; 112 argc_test=3;
134 argv_test[2] = "--extra-opts=tcp_long_lines"; 113 char *argv_known[] = {"check_tcp", "--timeout=10", "--escape", "--send=Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda", "--expect=Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda", "--jail", (char *) NULL};
135 argv_test[3] = NULL; 114 argv_new=np_extra_opts(&argc_test, argv_test, "check_tcp");
136 argc_test=3; 115 ok(array_diff(argc_test, argv_new, 6, argv_known), "Long lines test");
137 argv_known=(char **)realloc(argv_known, 7*sizeof(char **)); 116 my_free(&argc_test, argv_new, argv_test);
138 argv_known[0] = "check_tcp"; 117 }
139 argv_known[1] = "--timeout=10";
140 argv_known[2] = "--escape";
141 argv_known[3] = "--send=Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda";
142 argv_known[4] = "--expect=Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda Foo bar BAZ yadda yadda yadda";
143 argv_known[5] = "--jail";
144 argv_known[6] = NULL;
145 argv_test=np_extra_opts(&argc_test, argv_test, "check_tcp");
146 ok(array_diff(argc_test, argv_test, 6, argv_known), "Long lines test");
147 my_free(&argc_test,argv_test);
148 118
149 return exit_status(); 119 return exit_status();
150} 120}
diff --git a/lib/utils_base.c b/lib/utils_base.c
index 77700f5..4303e15 100644
--- a/lib/utils_base.c
+++ b/lib/utils_base.c
@@ -101,7 +101,9 @@ _set_thresholds(thresholds **my_thresholds, char *warn_string, char *critical_st
101{ 101{
102 thresholds *temp_thresholds = NULL; 102 thresholds *temp_thresholds = NULL;
103 103
104 temp_thresholds = malloc(sizeof(temp_thresholds)); 104 if ((temp_thresholds = malloc(sizeof(thresholds))) == NULL)
105 die(STATE_UNKNOWN, _("Cannot allocate memory: %s\n"),
106 strerror(errno));
105 107
106 temp_thresholds->warning = NULL; 108 temp_thresholds->warning = NULL;
107 temp_thresholds->critical = NULL; 109 temp_thresholds->critical = NULL;