summaryrefslogtreecommitdiffstats
path: root/lib/utils_cmd.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/utils_cmd.c')
-rw-r--r--lib/utils_cmd.c196
1 files changed, 109 insertions, 87 deletions
diff --git a/lib/utils_cmd.c b/lib/utils_cmd.c
index 9b222409..35b83297 100644
--- a/lib/utils_cmd.c
+++ b/lib/utils_cmd.c
@@ -40,7 +40,6 @@
40 40
41/** includes **/ 41/** includes **/
42#include "common.h" 42#include "common.h"
43#include "utils.h"
44#include "utils_cmd.h" 43#include "utils_cmd.h"
45/* This variable must be global, since there's no way the caller 44/* This variable must be global, since there's no way the caller
46 * can forcibly slay a dead or ungainly running program otherwise. 45 * can forcibly slay a dead or ungainly running program otherwise.
@@ -62,16 +61,13 @@ static pid_t *_cmd_pids = NULL;
62# include <sys/wait.h> 61# include <sys/wait.h>
63#endif 62#endif
64 63
65/* used in _cmd_open to pass the environment to commands */
66extern char **environ;
67
68/** macros **/ 64/** macros **/
69#ifndef WEXITSTATUS 65#ifndef WEXITSTATUS
70# define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8) 66# define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
71#endif 67#endif
72 68
73#ifndef WIFEXITED 69#ifndef WIFEXITED
74# define WIFEXITED(stat_val) (((stat_val)&255) == 0) 70# define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
75#endif 71#endif
76 72
77/* 4.3BSD Reno <signal.h> doesn't define SIG_ERR */ 73/* 4.3BSD Reno <signal.h> doesn't define SIG_ERR */
@@ -80,14 +76,12 @@ extern char **environ;
80#endif 76#endif
81 77
82/** prototypes **/ 78/** prototypes **/
83static int _cmd_open(char *const *, int *, int *) __attribute__((__nonnull__(1, 2, 3))); 79static int _cmd_open(char *const *argv, int *pfd, int *pfderr)
84 80 __attribute__((__nonnull__(1, 2, 3)));
85static int _cmd_fetch_output(int, output *, int) __attribute__((__nonnull__(2)));
86 81
87static int _cmd_close(int); 82static int _cmd_fetch_output(int fileDescriptor, output *cmd_output, int flags) __attribute__((__nonnull__(2)));
88 83
89/* prototype imported from utils.h */ 84static int _cmd_close(int fileDescriptor);
90extern void die(int, const char *, ...) __attribute__((__noreturn__, __format__(__printf__, 2, 3)));
91 85
92/* this function is NOT async-safe. It is exported so multithreaded 86/* this function is NOT async-safe. It is exported so multithreaded
93 * plugins (or other apps) can call it prior to running any commands 87 * plugins (or other apps) can call it prior to running any commands
@@ -103,26 +97,29 @@ void cmd_init(void) {
103 maxfd = MAXFD_LIMIT; 97 maxfd = MAXFD_LIMIT;
104 } 98 }
105 99
106 if (!_cmd_pids) 100 if (!_cmd_pids) {
107 _cmd_pids = calloc(maxfd, sizeof(pid_t)); 101 _cmd_pids = calloc(maxfd, sizeof(pid_t));
102 }
108} 103}
109 104
110/* Start running a command, array style */ 105/* Start running a command, array style */
111static int _cmd_open(char *const *argv, int *pfd, int *pfderr) { 106static int _cmd_open(char *const *argv, int *pfd, int *pfderr) {
112 pid_t pid;
113#ifdef RLIMIT_CORE 107#ifdef RLIMIT_CORE
114 struct rlimit limit; 108 struct rlimit limit;
115#endif 109#endif
116 110
117 int i = 0; 111 int i = 0;
118 112
119 if (!_cmd_pids) 113 if (!_cmd_pids) {
120 CMD_INIT; 114 CMD_INIT;
115 }
121 116
122 setenv("LC_ALL", "C", 1); 117 setenv("LC_ALL", "C", 1);
123 118
124 if (pipe(pfd) < 0 || pipe(pfderr) < 0 || (pid = fork()) < 0) 119 pid_t pid;
120 if (pipe(pfd) < 0 || pipe(pfderr) < 0 || (pid = fork()) < 0) {
125 return -1; /* errno set by the failing function */ 121 return -1; /* errno set by the failing function */
122 }
126 123
127 /* child runs exceve() and _exit. */ 124 /* child runs exceve() and _exit. */
128 if (pid == 0) { 125 if (pid == 0) {
@@ -147,9 +144,11 @@ static int _cmd_open(char *const *argv, int *pfd, int *pfderr) {
147 * This is executed in a separate address space (pure child), 144 * This is executed in a separate address space (pure child),
148 * so we don't have to worry about async safety */ 145 * so we don't have to worry about async safety */
149 long maxfd = mp_open_max(); 146 long maxfd = mp_open_max();
150 for (i = 0; i < maxfd; i++) 147 for (i = 0; i < maxfd; i++) {
151 if (_cmd_pids[i] > 0) 148 if (_cmd_pids[i] > 0) {
152 close(i); 149 close(i);
150 }
151 }
153 152
154 execve(argv[0], argv, environ); 153 execve(argv[0], argv, environ);
155 _exit(STATE_UNKNOWN); 154 _exit(STATE_UNKNOWN);
@@ -166,87 +165,94 @@ static int _cmd_open(char *const *argv, int *pfd, int *pfderr) {
166 return pfd[0]; 165 return pfd[0];
167} 166}
168 167
169static int _cmd_close(int fd) { 168static int _cmd_close(int fileDescriptor) {
170 int status;
171 pid_t pid; 169 pid_t pid;
172 170
173 /* make sure the provided fd was opened */ 171 /* make sure the provided fd was opened */
174 long maxfd = mp_open_max(); 172 long maxfd = mp_open_max();
175 if (fd < 0 || fd > maxfd || !_cmd_pids || (pid = _cmd_pids[fd]) == 0) 173 if (fileDescriptor < 0 || fileDescriptor > maxfd || !_cmd_pids ||
174 (pid = _cmd_pids[fileDescriptor]) == 0) {
176 return -1; 175 return -1;
176 }
177 177
178 _cmd_pids[fd] = 0; 178 _cmd_pids[fileDescriptor] = 0;
179 if (close(fd) == -1) 179 if (close(fileDescriptor) == -1) {
180 return -1; 180 return -1;
181 }
181 182
182 /* EINTR is ok (sort of), everything else is bad */ 183 /* EINTR is ok (sort of), everything else is bad */
183 while (waitpid(pid, &status, 0) < 0) 184 int status;
184 if (errno != EINTR) 185 while (waitpid(pid, &status, 0) < 0) {
186 if (errno != EINTR) {
185 return -1; 187 return -1;
188 }
189 }
186 190
187 /* return child's termination status */ 191 /* return child's termination status */
188 return (WIFEXITED(status)) ? WEXITSTATUS(status) : -1; 192 return (WIFEXITED(status)) ? WEXITSTATUS(status) : -1;
189} 193}
190 194
191static int _cmd_fetch_output(int fd, output *op, int flags) { 195static int _cmd_fetch_output(int fileDescriptor, output *cmd_output, int flags) {
192 size_t len = 0, i = 0, lineno = 0;
193 size_t rsf = 6, ary_size = 0; /* rsf = right shift factor, dec'ed uncond once */
194 char *buf = NULL;
195 int ret;
196 char tmpbuf[4096]; 196 char tmpbuf[4096];
197 197 cmd_output->buf = NULL;
198 op->buf = NULL; 198 cmd_output->buflen = 0;
199 op->buflen = 0; 199 ssize_t ret;
200 while ((ret = read(fd, tmpbuf, sizeof(tmpbuf))) > 0) { 200 while ((ret = read(fileDescriptor, tmpbuf, sizeof(tmpbuf))) > 0) {
201 len = (size_t)ret; 201 size_t len = (size_t)ret;
202 op->buf = realloc(op->buf, op->buflen + len + 1); 202 cmd_output->buf = realloc(cmd_output->buf, cmd_output->buflen + len + 1);
203 memcpy(op->buf + op->buflen, tmpbuf, len); 203 memcpy(cmd_output->buf + cmd_output->buflen, tmpbuf, len);
204 op->buflen += len; 204 cmd_output->buflen += len;
205 i++;
206 } 205 }
207 206
208 if (ret < 0) { 207 if (ret < 0) {
209 printf("read() returned %d: %s\n", ret, strerror(errno)); 208 printf("read() returned %zd: %s\n", ret, strerror(errno));
210 return ret; 209 return ret;
211 } 210 }
212 211
213 /* some plugins may want to keep output unbroken, and some commands 212 /* some plugins may want to keep output unbroken, and some commands
214 * will yield no output, so return here for those */ 213 * will yield no output, so return here for those */
215 if (flags & CMD_NO_ARRAYS || !op->buf || !op->buflen) 214 if (flags & CMD_NO_ARRAYS || !cmd_output->buf || !cmd_output->buflen) {
216 return op->buflen; 215 return cmd_output->buflen;
216 }
217 217
218 /* and some may want both */ 218 /* and some may want both */
219 char *buf = NULL;
219 if (flags & CMD_NO_ASSOC) { 220 if (flags & CMD_NO_ASSOC) {
220 buf = malloc(op->buflen); 221 buf = malloc(cmd_output->buflen);
221 memcpy(buf, op->buf, op->buflen); 222 memcpy(buf, cmd_output->buf, cmd_output->buflen);
222 } else 223 } else {
223 buf = op->buf; 224 buf = cmd_output->buf;
224 225 }
225 op->line = NULL; 226
226 op->lens = NULL; 227 cmd_output->line = NULL;
227 i = 0; 228 cmd_output->lens = NULL;
228 while (i < op->buflen) { 229 size_t i = 0;
230 size_t ary_size = 0; /* rsf = right shift factor, dec'ed uncond once */
231 size_t rsf = 6;
232 size_t lineno = 0;
233 while (i < cmd_output->buflen) {
229 /* make sure we have enough memory */ 234 /* make sure we have enough memory */
230 if (lineno >= ary_size) { 235 if (lineno >= ary_size) {
231 /* ary_size must never be zero */ 236 /* ary_size must never be zero */
232 do { 237 do {
233 ary_size = op->buflen >> --rsf; 238 ary_size = cmd_output->buflen >> --rsf;
234 } while (!ary_size); 239 } while (!ary_size);
235 240
236 op->line = realloc(op->line, ary_size * sizeof(char *)); 241 cmd_output->line = realloc(cmd_output->line, ary_size * sizeof(char *));
237 op->lens = realloc(op->lens, ary_size * sizeof(size_t)); 242 cmd_output->lens = realloc(cmd_output->lens, ary_size * sizeof(size_t));
238 } 243 }
239 244
240 /* set the pointer to the string */ 245 /* set the pointer to the string */
241 op->line[lineno] = &buf[i]; 246 cmd_output->line[lineno] = &buf[i];
242 247
243 /* hop to next newline or end of buffer */ 248 /* hop to next newline or end of buffer */
244 while (buf[i] != '\n' && i < op->buflen) 249 while (buf[i] != '\n' && i < cmd_output->buflen) {
245 i++; 250 i++;
251 }
246 buf[i] = '\0'; 252 buf[i] = '\0';
247 253
248 /* calculate the string length using pointer difference */ 254 /* calculate the string length using pointer difference */
249 op->lens[lineno] = (size_t)&buf[i] - (size_t)op->line[lineno]; 255 cmd_output->lens[lineno] = (size_t)&buf[i] - (size_t)cmd_output->line[lineno];
250 256
251 lineno++; 257 lineno++;
252 i++; 258 i++;
@@ -256,41 +262,42 @@ static int _cmd_fetch_output(int fd, output *op, int flags) {
256} 262}
257 263
258int cmd_run(const char *cmdstring, output *out, output *err, int flags) { 264int cmd_run(const char *cmdstring, output *out, output *err, int flags) {
259 int i = 0, argc; 265 if (cmdstring == NULL) {
260 size_t cmdlen;
261 char **argv = NULL;
262 char *cmd = NULL;
263 char *str = NULL;
264
265 if (cmdstring == NULL)
266 return -1; 266 return -1;
267 }
267 268
268 /* initialize the structs */ 269 /* initialize the structs */
269 if (out) 270 if (out) {
270 memset(out, 0, sizeof(output)); 271 memset(out, 0, sizeof(output));
271 if (err) 272 }
273 if (err) {
272 memset(err, 0, sizeof(output)); 274 memset(err, 0, sizeof(output));
275 }
273 276
274 /* make copy of command string so strtok() doesn't silently modify it */ 277 /* make copy of command string so strtok() doesn't silently modify it */
275 /* (the calling program may want to access it later) */ 278 /* (the calling program may want to access it later) */
276 cmdlen = strlen(cmdstring); 279 size_t cmdlen = strlen(cmdstring);
277 if ((cmd = malloc(cmdlen + 1)) == NULL) 280 char *cmd = NULL;
281 if ((cmd = malloc(cmdlen + 1)) == NULL) {
278 return -1; 282 return -1;
283 }
279 memcpy(cmd, cmdstring, cmdlen); 284 memcpy(cmd, cmdstring, cmdlen);
280 cmd[cmdlen] = '\0'; 285 cmd[cmdlen] = '\0';
281 286
282 /* This is not a shell, so we don't handle "???" */ 287 /* This is not a shell, so we don't handle "???" */
283 if (strstr(cmdstring, "\"")) 288 if (strstr(cmdstring, "\"")) {
284 return -1; 289 return -1;
290 }
285 291
286 /* allow single quotes, but only if non-whitesapce doesn't occur on both sides */ 292 /* allow single quotes, but only if non-whitesapce doesn't occur on both sides */
287 if (strstr(cmdstring, " ' ") || strstr(cmdstring, "'''")) 293 if (strstr(cmdstring, " ' ") || strstr(cmdstring, "'''")) {
288 return -1; 294 return -1;
295 }
289 296
290 /* each arg must be whitespace-separated, so args can be a maximum 297 /* each arg must be whitespace-separated, so args can be a maximum
291 * of (len / 2) + 1. We add 1 extra to the mix for NULL termination */ 298 * of (len / 2) + 1. We add 1 extra to the mix for NULL termination */
292 argc = (cmdlen >> 1) + 2; 299 int argc = (cmdlen >> 1) + 2;
293 argv = calloc((size_t)argc, sizeof(char *)); 300 char **argv = calloc((size_t)argc, sizeof(char *));
294 301
295 if (argv == NULL) { 302 if (argv == NULL) {
296 printf("%s\n", _("Could not malloc argv array in popen()")); 303 printf("%s\n", _("Could not malloc argv array in popen()"));
@@ -298,14 +305,16 @@ int cmd_run(const char *cmdstring, output *out, output *err, int flags) {
298 } 305 }
299 306
300 /* get command arguments (stupidly, but fairly quickly) */ 307 /* get command arguments (stupidly, but fairly quickly) */
308 int i = 0;
301 while (cmd) { 309 while (cmd) {
302 str = cmd; 310 char *str = cmd;
303 str += strspn(str, " \t\r\n"); /* trim any leading whitespace */ 311 str += strspn(str, " \t\r\n"); /* trim any leading whitespace */
304 312
305 if (strstr(str, "'") == str) { /* handle SIMPLE quoted strings */ 313 if (strstr(str, "'") == str) { /* handle SIMPLE quoted strings */
306 str++; 314 str++;
307 if (!strstr(str, "'")) 315 if (!strstr(str, "'")) {
308 return -1; /* balanced? */ 316 return -1; /* balanced? */
317 }
309 cmd = 1 + strstr(str, "'"); 318 cmd = 1 + strstr(str, "'");
310 str[strcspn(str, "'")] = 0; 319 str[strcspn(str, "'")] = 0;
311 } else { 320 } else {
@@ -317,8 +326,9 @@ int cmd_run(const char *cmdstring, output *out, output *err, int flags) {
317 } 326 }
318 } 327 }
319 328
320 if (cmd && strlen(cmd) == strspn(cmd, " \t\r\n")) 329 if (cmd && strlen(cmd) == strspn(cmd, " \t\r\n")) {
321 cmd = NULL; 330 cmd = NULL;
331 }
322 332
323 argv[i++] = str; 333 argv[i++] = str;
324 } 334 }
@@ -327,53 +337,65 @@ int cmd_run(const char *cmdstring, output *out, output *err, int flags) {
327} 337}
328 338
329int cmd_run_array(char *const *argv, output *out, output *err, int flags) { 339int cmd_run_array(char *const *argv, output *out, output *err, int flags) {
330 int fd, pfd_out[2], pfd_err[2];
331
332 /* initialize the structs */ 340 /* initialize the structs */
333 if (out) 341 if (out) {
334 memset(out, 0, sizeof(output)); 342 memset(out, 0, sizeof(output));
335 if (err) 343 }
344 if (err) {
336 memset(err, 0, sizeof(output)); 345 memset(err, 0, sizeof(output));
346 }
337 347
338 if ((fd = _cmd_open(argv, pfd_out, pfd_err)) == -1) 348 int fd;
349 int pfd_out[2];
350 int pfd_err[2];
351 if ((fd = _cmd_open(argv, pfd_out, pfd_err)) == -1) {
339 die(STATE_UNKNOWN, _("Could not open pipe: %s\n"), argv[0]); 352 die(STATE_UNKNOWN, _("Could not open pipe: %s\n"), argv[0]);
353 }
340 354
341 if (out) 355 if (out) {
342 out->lines = _cmd_fetch_output(pfd_out[0], out, flags); 356 out->lines = _cmd_fetch_output(pfd_out[0], out, flags);
343 if (err) 357 }
358 if (err) {
344 err->lines = _cmd_fetch_output(pfd_err[0], err, flags); 359 err->lines = _cmd_fetch_output(pfd_err[0], err, flags);
360 }
345 361
346 return _cmd_close(fd); 362 return _cmd_close(fd);
347} 363}
348 364
349int cmd_file_read(const char *filename, output *out, int flags) { 365int cmd_file_read(const char *filename, output *out, int flags) {
350 int fd; 366 int fd;
351 if (out) 367 if (out) {
352 memset(out, 0, sizeof(output)); 368 memset(out, 0, sizeof(output));
369 }
353 370
354 if ((fd = open(filename, O_RDONLY)) == -1) { 371 if ((fd = open(filename, O_RDONLY)) == -1) {
355 die(STATE_UNKNOWN, _("Error opening %s: %s"), filename, strerror(errno)); 372 die(STATE_UNKNOWN, _("Error opening %s: %s"), filename, strerror(errno));
356 } 373 }
357 374
358 if (out) 375 if (out) {
359 out->lines = _cmd_fetch_output(fd, out, flags); 376 out->lines = _cmd_fetch_output(fd, out, flags);
377 }
360 378
361 if (close(fd) == -1) 379 if (close(fd) == -1) {
362 die(STATE_UNKNOWN, _("Error closing %s: %s"), filename, strerror(errno)); 380 die(STATE_UNKNOWN, _("Error closing %s: %s"), filename, strerror(errno));
381 }
363 382
364 return 0; 383 return 0;
365} 384}
366 385
367void timeout_alarm_handler(int signo) { 386void timeout_alarm_handler(int signo) {
368 if (signo == SIGALRM) { 387 if (signo == SIGALRM) {
369 printf(_("%s - Plugin timed out after %d seconds\n"), state_text(timeout_state), timeout_interval); 388 printf(_("%s - Plugin timed out after %d seconds\n"), state_text(timeout_state),
389 timeout_interval);
370 390
371 long maxfd = mp_open_max(); 391 long maxfd = mp_open_max();
372 if (_cmd_pids) 392 if (_cmd_pids) {
373 for (long int i = 0; i < maxfd; i++) { 393 for (long int i = 0; i < maxfd; i++) {
374 if (_cmd_pids[i] != 0) 394 if (_cmd_pids[i] != 0) {
375 kill(_cmd_pids[i], SIGKILL); 395 kill(_cmd_pids[i], SIGKILL);
396 }
376 } 397 }
398 }
377 399
378 exit(timeout_state); 400 exit(timeout_state);
379 } 401 }