diff options
author | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2024-10-31 13:27:16 (GMT) |
---|---|---|
committer | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2024-10-31 13:27:16 (GMT) |
commit | fc90673afcdc6e4107161391d9ad62d40d4b558f (patch) | |
tree | 5aa7ae3e8df98e5af6d9bb54d67e4b385f386f53 /plugins | |
parent | baa2208a1daa3bf12c6bd7f9bc3646fcff36121f (diff) | |
download | monitoring-plugins-fc90673afcdc6e4107161391d9ad62d40d4b558f.tar.gz |
popen.c: style + linter fixes
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/popen.c | 41 |
1 files changed, 20 insertions, 21 deletions
diff --git a/plugins/popen.c b/plugins/popen.c index 7aa18bf..2b9824b 100644 --- a/plugins/popen.c +++ b/plugins/popen.c | |||
@@ -47,12 +47,12 @@ extern pid_t *childpid; | |||
47 | extern int *child_stderr_array; | 47 | extern int *child_stderr_array; |
48 | extern FILE *child_process; | 48 | extern FILE *child_process; |
49 | 49 | ||
50 | FILE *spopen(const char *); | 50 | FILE *spopen(const char * /*cmdstring*/); |
51 | int spclose(FILE *); | 51 | int spclose(FILE * /*fp*/); |
52 | #ifdef REDHAT_SPOPEN_ERROR | 52 | #ifdef REDHAT_SPOPEN_ERROR |
53 | void popen_sigchld_handler(int); | 53 | void popen_sigchld_handler(int); |
54 | #endif | 54 | #endif |
55 | void popen_timeout_alarm_handler(int); | 55 | void popen_timeout_alarm_handler(int /*signo*/); |
56 | 56 | ||
57 | #include <stdarg.h> /* ANSI C header file */ | 57 | #include <stdarg.h> /* ANSI C header file */ |
58 | #include <fcntl.h> | 58 | #include <fcntl.h> |
@@ -84,15 +84,6 @@ static volatile int childtermd = 0; | |||
84 | #endif | 84 | #endif |
85 | 85 | ||
86 | FILE *spopen(const char *cmdstring) { | 86 | FILE *spopen(const char *cmdstring) { |
87 | char *env[2]; | ||
88 | char *cmd = NULL; | ||
89 | char **argv = NULL; | ||
90 | char *str, *tmp; | ||
91 | int argc; | ||
92 | |||
93 | int i = 0, pfd[2], pfderr[2]; | ||
94 | pid_t pid; | ||
95 | |||
96 | #ifdef RLIMIT_CORE | 87 | #ifdef RLIMIT_CORE |
97 | /* do not leave core files */ | 88 | /* do not leave core files */ |
98 | struct rlimit limit; | 89 | struct rlimit limit; |
@@ -101,6 +92,7 @@ FILE *spopen(const char *cmdstring) { | |||
101 | setrlimit(RLIMIT_CORE, &limit); | 92 | setrlimit(RLIMIT_CORE, &limit); |
102 | #endif | 93 | #endif |
103 | 94 | ||
95 | char *env[2]; | ||
104 | env[0] = strdup("LC_ALL=C"); | 96 | env[0] = strdup("LC_ALL=C"); |
105 | env[1] = NULL; | 97 | env[1] = NULL; |
106 | 98 | ||
@@ -108,6 +100,7 @@ FILE *spopen(const char *cmdstring) { | |||
108 | if (cmdstring == NULL) | 100 | if (cmdstring == NULL) |
109 | return (NULL); | 101 | return (NULL); |
110 | 102 | ||
103 | char *cmd = NULL; | ||
111 | /* make copy of command string so strtok() doesn't silently modify it */ | 104 | /* make copy of command string so strtok() doesn't silently modify it */ |
112 | /* (the calling program may want to access it later) */ | 105 | /* (the calling program may want to access it later) */ |
113 | cmd = malloc(strlen(cmdstring) + 1); | 106 | cmd = malloc(strlen(cmdstring) + 1); |
@@ -123,6 +116,8 @@ FILE *spopen(const char *cmdstring) { | |||
123 | if (strstr(cmdstring, " ' ") || strstr(cmdstring, "'''")) | 116 | if (strstr(cmdstring, " ' ") || strstr(cmdstring, "'''")) |
124 | return NULL; | 117 | return NULL; |
125 | 118 | ||
119 | int argc; | ||
120 | char **argv = NULL; | ||
126 | /* there cannot be more args than characters */ | 121 | /* there cannot be more args than characters */ |
127 | argc = strlen(cmdstring) + 1; /* add 1 for NULL termination */ | 122 | argc = strlen(cmdstring) + 1; /* add 1 for NULL termination */ |
128 | argv = malloc(sizeof(char *) * argc); | 123 | argv = malloc(sizeof(char *) * argc); |
@@ -132,6 +127,8 @@ FILE *spopen(const char *cmdstring) { | |||
132 | return NULL; | 127 | return NULL; |
133 | } | 128 | } |
134 | 129 | ||
130 | int i = 0; | ||
131 | char *str; | ||
135 | /* loop to get arguments to command */ | 132 | /* loop to get arguments to command */ |
136 | while (cmd) { | 133 | while (cmd) { |
137 | str = cmd; | 134 | str = cmd; |
@@ -150,7 +147,7 @@ FILE *spopen(const char *cmdstring) { | |||
150 | str[strcspn(str, "'")] = 0; | 147 | str[strcspn(str, "'")] = 0; |
151 | } else if (strcspn(str, "'") < strcspn(str, " \t\r\n")) { | 148 | } else if (strcspn(str, "'") < strcspn(str, " \t\r\n")) { |
152 | /* handle --option='foo bar' strings */ | 149 | /* handle --option='foo bar' strings */ |
153 | tmp = str + strcspn(str, "'") + 1; | 150 | char *tmp = str + strcspn(str, "'") + 1; |
154 | if (!strstr(tmp, "'")) | 151 | if (!strstr(tmp, "'")) |
155 | return NULL; /* balanced? */ | 152 | return NULL; /* balanced? */ |
156 | tmp += strcspn(tmp, "'") + 1; | 153 | tmp += strcspn(tmp, "'") + 1; |
@@ -184,9 +181,11 @@ FILE *spopen(const char *cmdstring) { | |||
184 | return (NULL); | 181 | return (NULL); |
185 | } | 182 | } |
186 | 183 | ||
184 | int pfd[2]; | ||
187 | if (pipe(pfd) < 0) | 185 | if (pipe(pfd) < 0) |
188 | return (NULL); /* errno set by pipe() */ | 186 | return (NULL); /* errno set by pipe() */ |
189 | 187 | ||
188 | int pfderr[2]; | ||
190 | if (pipe(pfderr) < 0) | 189 | if (pipe(pfderr) < 0) |
191 | return (NULL); /* errno set by pipe() */ | 190 | return (NULL); /* errno set by pipe() */ |
192 | 191 | ||
@@ -196,9 +195,11 @@ FILE *spopen(const char *cmdstring) { | |||
196 | } | 195 | } |
197 | #endif | 196 | #endif |
198 | 197 | ||
198 | pid_t pid; | ||
199 | if ((pid = fork()) < 0) | 199 | if ((pid = fork()) < 0) |
200 | return (NULL); /* errno set by fork() */ | 200 | return (NULL); /* errno set by fork() */ |
201 | else if (pid == 0) { /* child */ | 201 | |
202 | if (pid == 0) { /* child */ | ||
202 | close(pfd[0]); | 203 | close(pfd[0]); |
203 | if (pfd[1] != STDOUT_FILENO) { | 204 | if (pfd[1] != STDOUT_FILENO) { |
204 | dup2(pfd[1], STDOUT_FILENO); | 205 | dup2(pfd[1], STDOUT_FILENO); |
@@ -229,13 +230,11 @@ FILE *spopen(const char *cmdstring) { | |||
229 | } | 230 | } |
230 | 231 | ||
231 | int spclose(FILE *fp) { | 232 | int spclose(FILE *fp) { |
232 | int fd, status; | ||
233 | pid_t pid; | ||
234 | |||
235 | if (childpid == NULL) | 233 | if (childpid == NULL) |
236 | return (1); /* popen() has never been called */ | 234 | return (1); /* popen() has never been called */ |
237 | 235 | ||
238 | fd = fileno(fp); | 236 | pid_t pid; |
237 | int fd = fileno(fp); | ||
239 | if ((pid = childpid[fd]) == 0) | 238 | if ((pid = childpid[fd]) == 0) |
240 | return (1); /* fp wasn't opened by popen() */ | 239 | return (1); /* fp wasn't opened by popen() */ |
241 | 240 | ||
@@ -248,6 +247,7 @@ int spclose(FILE *fp) { | |||
248 | ; /* wait until SIGCHLD */ | 247 | ; /* wait until SIGCHLD */ |
249 | #endif | 248 | #endif |
250 | 249 | ||
250 | int status; | ||
251 | while (waitpid(pid, &status, 0) < 0) | 251 | while (waitpid(pid, &status, 0) < 0) |
252 | if (errno != EINTR) | 252 | if (errno != EINTR) |
253 | return (1); /* error other than EINTR from waitpid() */ | 253 | return (1); /* error other than EINTR from waitpid() */ |
@@ -266,10 +266,9 @@ void popen_sigchld_handler(int signo) { | |||
266 | #endif | 266 | #endif |
267 | 267 | ||
268 | void popen_timeout_alarm_handler(int signo) { | 268 | void popen_timeout_alarm_handler(int signo) { |
269 | int fh; | ||
270 | if (signo == SIGALRM) { | 269 | if (signo == SIGALRM) { |
271 | if (child_process != NULL) { | 270 | if (child_process != NULL) { |
272 | fh = fileno(child_process); | 271 | int fh = fileno(child_process); |
273 | if (fh >= 0) { | 272 | if (fh >= 0) { |
274 | kill(childpid[fh], SIGKILL); | 273 | kill(childpid[fh], SIGKILL); |
275 | } | 274 | } |