diff options
Diffstat (limited to 'lib/utils_cmd.c')
| -rw-r--r-- | lib/utils_cmd.c | 484 |
1 files changed, 394 insertions, 90 deletions
diff --git a/lib/utils_cmd.c b/lib/utils_cmd.c index 18350ac0..42c81793 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. |
| @@ -57,21 +56,19 @@ static pid_t *_cmd_pids = NULL; | |||
| 57 | #include "./maxfd.h" | 56 | #include "./maxfd.h" |
| 58 | 57 | ||
| 59 | #include <fcntl.h> | 58 | #include <fcntl.h> |
| 59 | #include <stddef.h> | ||
| 60 | 60 | ||
| 61 | #ifdef HAVE_SYS_WAIT_H | 61 | #ifdef HAVE_SYS_WAIT_H |
| 62 | # include <sys/wait.h> | 62 | # include <sys/wait.h> |
| 63 | #endif | 63 | #endif |
| 64 | 64 | ||
| 65 | /* used in _cmd_open to pass the environment to commands */ | ||
| 66 | extern char **environ; | ||
| 67 | |||
| 68 | /** macros **/ | 65 | /** macros **/ |
| 69 | #ifndef WEXITSTATUS | 66 | #ifndef WEXITSTATUS |
| 70 | # define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8) | 67 | # define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8) |
| 71 | #endif | 68 | #endif |
| 72 | 69 | ||
| 73 | #ifndef WIFEXITED | 70 | #ifndef WIFEXITED |
| 74 | # define WIFEXITED(stat_val) (((stat_val)&255) == 0) | 71 | # define WIFEXITED(stat_val) (((stat_val) & 255) == 0) |
| 75 | #endif | 72 | #endif |
| 76 | 73 | ||
| 77 | /* 4.3BSD Reno <signal.h> doesn't define SIG_ERR */ | 74 | /* 4.3BSD Reno <signal.h> doesn't define SIG_ERR */ |
| @@ -80,14 +77,13 @@ extern char **environ; | |||
| 80 | #endif | 77 | #endif |
| 81 | 78 | ||
| 82 | /** prototypes **/ | 79 | /** prototypes **/ |
| 83 | static int _cmd_open(char *const *, int *, int *) __attribute__((__nonnull__(1, 2, 3))); | 80 | static int _cmd_open(char *const *argv, int *pfd, int *pfderr) |
| 84 | 81 | __attribute__((__nonnull__(1, 2, 3))); | |
| 85 | static int _cmd_fetch_output(int, output *, int) __attribute__((__nonnull__(2))); | ||
| 86 | 82 | ||
| 87 | static int _cmd_close(int); | 83 | static int _cmd_fetch_output(int fileDescriptor, output *cmd_output, int flags) |
| 84 | __attribute__((__nonnull__(2))); | ||
| 88 | 85 | ||
| 89 | /* prototype imported from utils.h */ | 86 | static int _cmd_close(int fileDescriptor); |
| 90 | extern void die(int, const char *, ...) __attribute__((__noreturn__, __format__(__printf__, 2, 3))); | ||
| 91 | 87 | ||
| 92 | /* this function is NOT async-safe. It is exported so multithreaded | 88 | /* this function is NOT async-safe. It is exported so multithreaded |
| 93 | * plugins (or other apps) can call it prior to running any commands | 89 | * plugins (or other apps) can call it prior to running any commands |
| @@ -103,26 +99,100 @@ void cmd_init(void) { | |||
| 103 | maxfd = MAXFD_LIMIT; | 99 | maxfd = MAXFD_LIMIT; |
| 104 | } | 100 | } |
| 105 | 101 | ||
| 106 | if (!_cmd_pids) | 102 | if (!_cmd_pids) { |
| 107 | _cmd_pids = calloc(maxfd, sizeof(pid_t)); | 103 | _cmd_pids = calloc(maxfd, sizeof(pid_t)); |
| 104 | } | ||
| 105 | } | ||
| 106 | |||
| 107 | typedef struct { | ||
| 108 | int stdout_pipe_fd[2]; | ||
| 109 | int stderr_pipe_fd[2]; | ||
| 110 | int file_descriptor; | ||
| 111 | int error_code; | ||
| 112 | } int_cmd_open_result; | ||
| 113 | static int_cmd_open_result _cmd_open2(char *const *argv) { | ||
| 114 | #ifdef RLIMIT_CORE | ||
| 115 | struct rlimit limit; | ||
| 116 | #endif | ||
| 117 | |||
| 118 | if (!_cmd_pids) { | ||
| 119 | CMD_INIT; | ||
| 120 | } | ||
| 121 | |||
| 122 | setenv("LC_ALL", "C", 1); | ||
| 123 | |||
| 124 | int_cmd_open_result result = { | ||
| 125 | .error_code = 0, | ||
| 126 | .stdout_pipe_fd = {0, 0}, | ||
| 127 | .stderr_pipe_fd = {0, 0}, | ||
| 128 | }; | ||
| 129 | pid_t pid; | ||
| 130 | if (pipe(result.stdout_pipe_fd) < 0 || pipe(result.stderr_pipe_fd) < 0 || (pid = fork()) < 0) { | ||
| 131 | result.error_code = -1; | ||
| 132 | return result; /* errno set by the failing function */ | ||
| 133 | } | ||
| 134 | |||
| 135 | /* child runs exceve() and _exit. */ | ||
| 136 | if (pid == 0) { | ||
| 137 | #ifdef RLIMIT_CORE | ||
| 138 | /* the program we execve shouldn't leave core files */ | ||
| 139 | getrlimit(RLIMIT_CORE, &limit); | ||
| 140 | limit.rlim_cur = 0; | ||
| 141 | setrlimit(RLIMIT_CORE, &limit); | ||
| 142 | #endif | ||
| 143 | close(result.stdout_pipe_fd[0]); | ||
| 144 | if (result.stdout_pipe_fd[1] != STDOUT_FILENO) { | ||
| 145 | dup2(result.stdout_pipe_fd[1], STDOUT_FILENO); | ||
| 146 | close(result.stdout_pipe_fd[1]); | ||
| 147 | } | ||
| 148 | close(result.stderr_pipe_fd[0]); | ||
| 149 | if (result.stderr_pipe_fd[1] != STDERR_FILENO) { | ||
| 150 | dup2(result.stderr_pipe_fd[1], STDERR_FILENO); | ||
| 151 | close(result.stderr_pipe_fd[1]); | ||
| 152 | } | ||
| 153 | |||
| 154 | /* close all descriptors in _cmd_pids[] | ||
| 155 | * This is executed in a separate address space (pure child), | ||
| 156 | * so we don't have to worry about async safety */ | ||
| 157 | long maxfd = mp_open_max(); | ||
| 158 | for (int i = 0; i < maxfd; i++) { | ||
| 159 | if (_cmd_pids[i] > 0) { | ||
| 160 | close(i); | ||
| 161 | } | ||
| 162 | } | ||
| 163 | |||
| 164 | execve(argv[0], argv, environ); | ||
| 165 | _exit(STATE_UNKNOWN); | ||
| 166 | } | ||
| 167 | |||
| 168 | /* parent picks up execution here */ | ||
| 169 | /* close children descriptors in our address space */ | ||
| 170 | close(result.stdout_pipe_fd[1]); | ||
| 171 | close(result.stderr_pipe_fd[1]); | ||
| 172 | |||
| 173 | /* tag our file's entry in the pid-list and return it */ | ||
| 174 | _cmd_pids[result.stdout_pipe_fd[0]] = pid; | ||
| 175 | |||
| 176 | result.file_descriptor = result.stdout_pipe_fd[0]; | ||
| 177 | return result; | ||
| 108 | } | 178 | } |
| 109 | 179 | ||
| 110 | /* Start running a command, array style */ | 180 | /* Start running a command, array style */ |
| 111 | static int _cmd_open(char *const *argv, int *pfd, int *pfderr) { | 181 | static int _cmd_open(char *const *argv, int *pfd, int *pfderr) { |
| 112 | pid_t pid; | ||
| 113 | #ifdef RLIMIT_CORE | 182 | #ifdef RLIMIT_CORE |
| 114 | struct rlimit limit; | 183 | struct rlimit limit; |
| 115 | #endif | 184 | #endif |
| 116 | 185 | ||
| 117 | int i = 0; | 186 | if (!_cmd_pids) { |
| 118 | |||
| 119 | if (!_cmd_pids) | ||
| 120 | CMD_INIT; | 187 | CMD_INIT; |
| 188 | } | ||
| 121 | 189 | ||
| 122 | setenv("LC_ALL", "C", 1); | 190 | setenv("LC_ALL", "C", 1); |
| 123 | 191 | ||
| 124 | if (pipe(pfd) < 0 || pipe(pfderr) < 0 || (pid = fork()) < 0) | 192 | pid_t pid; |
| 193 | if (pipe(pfd) < 0 || pipe(pfderr) < 0 || (pid = fork()) < 0) { | ||
| 125 | return -1; /* errno set by the failing function */ | 194 | return -1; /* errno set by the failing function */ |
| 195 | } | ||
| 126 | 196 | ||
| 127 | /* child runs exceve() and _exit. */ | 197 | /* child runs exceve() and _exit. */ |
| 128 | if (pid == 0) { | 198 | if (pid == 0) { |
| @@ -147,9 +217,11 @@ static int _cmd_open(char *const *argv, int *pfd, int *pfderr) { | |||
| 147 | * This is executed in a separate address space (pure child), | 217 | * This is executed in a separate address space (pure child), |
| 148 | * so we don't have to worry about async safety */ | 218 | * so we don't have to worry about async safety */ |
| 149 | long maxfd = mp_open_max(); | 219 | long maxfd = mp_open_max(); |
| 150 | for (i = 0; i < maxfd; i++) | 220 | for (int i = 0; i < maxfd; i++) { |
| 151 | if (_cmd_pids[i] > 0) | 221 | if (_cmd_pids[i] > 0) { |
| 152 | close(i); | 222 | close(i); |
| 223 | } | ||
| 224 | } | ||
| 153 | 225 | ||
| 154 | execve(argv[0], argv, environ); | 226 | execve(argv[0], argv, environ); |
| 155 | _exit(STATE_UNKNOWN); | 227 | _exit(STATE_UNKNOWN); |
| @@ -166,88 +238,171 @@ static int _cmd_open(char *const *argv, int *pfd, int *pfderr) { | |||
| 166 | return pfd[0]; | 238 | return pfd[0]; |
| 167 | } | 239 | } |
| 168 | 240 | ||
| 169 | static int _cmd_close(int fd) { | 241 | static int _cmd_close(int fileDescriptor) { |
| 170 | int status; | ||
| 171 | pid_t pid; | 242 | pid_t pid; |
| 172 | 243 | ||
| 173 | /* make sure the provided fd was opened */ | 244 | /* make sure the provided fd was opened */ |
| 174 | long maxfd = mp_open_max(); | 245 | long maxfd = mp_open_max(); |
| 175 | if (fd < 0 || fd > maxfd || !_cmd_pids || (pid = _cmd_pids[fd]) == 0) | 246 | if (fileDescriptor < 0 || fileDescriptor > maxfd || !_cmd_pids || |
| 247 | (pid = _cmd_pids[fileDescriptor]) == 0) { | ||
| 176 | return -1; | 248 | return -1; |
| 249 | } | ||
| 177 | 250 | ||
| 178 | _cmd_pids[fd] = 0; | 251 | _cmd_pids[fileDescriptor] = 0; |
| 179 | if (close(fd) == -1) | 252 | if (close(fileDescriptor) == -1) { |
| 180 | return -1; | 253 | return -1; |
| 254 | } | ||
| 181 | 255 | ||
| 182 | /* EINTR is ok (sort of), everything else is bad */ | 256 | /* EINTR is ok (sort of), everything else is bad */ |
| 183 | while (waitpid(pid, &status, 0) < 0) | 257 | int status; |
| 184 | if (errno != EINTR) | 258 | while (waitpid(pid, &status, 0) < 0) { |
| 259 | if (errno != EINTR) { | ||
| 185 | return -1; | 260 | return -1; |
| 261 | } | ||
| 262 | } | ||
| 186 | 263 | ||
| 187 | /* return child's termination status */ | 264 | /* return child's termination status */ |
| 188 | return (WIFEXITED(status)) ? WEXITSTATUS(status) : -1; | 265 | return (WIFEXITED(status)) ? WEXITSTATUS(status) : -1; |
| 189 | } | 266 | } |
| 190 | 267 | ||
| 191 | static int _cmd_fetch_output(int fd, output *op, int flags) { | 268 | typedef struct { |
| 192 | size_t len = 0, i = 0, lineno = 0; | 269 | int error_code; |
| 193 | size_t rsf = 6, ary_size = 0; /* rsf = right shift factor, dec'ed uncond once */ | 270 | output output_container; |
| 194 | char *buf = NULL; | 271 | } int_cmd_fetch_output2; |
| 195 | int ret; | 272 | static int_cmd_fetch_output2 _cmd_fetch_output2(int fileDescriptor, int flags) { |
| 196 | char tmpbuf[4096]; | 273 | char tmpbuf[4096]; |
| 197 | 274 | ||
| 198 | op->buf = NULL; | 275 | int_cmd_fetch_output2 result = { |
| 199 | op->buflen = 0; | 276 | .error_code = 0, |
| 200 | while ((ret = read(fd, tmpbuf, sizeof(tmpbuf))) > 0) { | 277 | .output_container = |
| 201 | len = (size_t)ret; | 278 | { |
| 202 | op->buf = realloc(op->buf, op->buflen + len + 1); | 279 | .buf = NULL, |
| 203 | memcpy(op->buf + op->buflen, tmpbuf, len); | 280 | .buflen = 0, |
| 204 | op->buflen += len; | 281 | .line = NULL, |
| 282 | .lines = 0, | ||
| 283 | }, | ||
| 284 | }; | ||
| 285 | ssize_t ret; | ||
| 286 | while ((ret = read(fileDescriptor, tmpbuf, sizeof(tmpbuf))) > 0) { | ||
| 287 | size_t len = (size_t)ret; | ||
| 288 | result.output_container.buf = | ||
| 289 | realloc(result.output_container.buf, result.output_container.buflen + len + 1); | ||
| 290 | memcpy(result.output_container.buf + result.output_container.buflen, tmpbuf, len); | ||
| 291 | result.output_container.buflen += len; | ||
| 292 | } | ||
| 293 | |||
| 294 | if (ret < 0) { | ||
| 295 | printf("read() returned %zd: %s\n", ret, strerror(errno)); | ||
| 296 | result.error_code = -1; | ||
| 297 | return result; | ||
| 298 | } | ||
| 299 | |||
| 300 | /* some plugins may want to keep output unbroken, and some commands | ||
| 301 | * will yield no output, so return here for those */ | ||
| 302 | if (flags & CMD_NO_ARRAYS || !result.output_container.buf || !result.output_container.buflen) { | ||
| 303 | return result; | ||
| 304 | } | ||
| 305 | |||
| 306 | /* and some may want both */ | ||
| 307 | char *buf = NULL; | ||
| 308 | if (flags & CMD_NO_ASSOC) { | ||
| 309 | buf = malloc(result.output_container.buflen); | ||
| 310 | memcpy(buf, result.output_container.buf, result.output_container.buflen); | ||
| 311 | } else { | ||
| 312 | buf = result.output_container.buf; | ||
| 313 | } | ||
| 314 | |||
| 315 | result.output_container.line = NULL; | ||
| 316 | size_t ary_size = 0; /* rsf = right shift factor, dec'ed uncond once */ | ||
| 317 | size_t rsf = 6; | ||
| 318 | size_t lineno = 0; | ||
| 319 | for (size_t i = 0; i < result.output_container.buflen;) { | ||
| 320 | /* make sure we have enough memory */ | ||
| 321 | if (lineno >= ary_size) { | ||
| 322 | /* ary_size must never be zero */ | ||
| 323 | do { | ||
| 324 | ary_size = result.output_container.buflen >> --rsf; | ||
| 325 | } while (!ary_size); | ||
| 326 | |||
| 327 | result.output_container.line = | ||
| 328 | realloc(result.output_container.line, ary_size * sizeof(char *)); | ||
| 329 | } | ||
| 330 | |||
| 331 | /* set the pointer to the string */ | ||
| 332 | result.output_container.line[lineno] = &buf[i]; | ||
| 333 | |||
| 334 | /* hop to next newline or end of buffer */ | ||
| 335 | while (buf[i] != '\n' && i < result.output_container.buflen) { | ||
| 336 | i++; | ||
| 337 | } | ||
| 338 | buf[i] = '\0'; | ||
| 339 | |||
| 340 | lineno++; | ||
| 205 | i++; | 341 | i++; |
| 206 | } | 342 | } |
| 207 | 343 | ||
| 344 | result.output_container.lines = lineno; | ||
| 345 | |||
| 346 | return result; | ||
| 347 | } | ||
| 348 | |||
| 349 | static int _cmd_fetch_output(int fileDescriptor, output *cmd_output, int flags) { | ||
| 350 | char tmpbuf[4096]; | ||
| 351 | cmd_output->buf = NULL; | ||
| 352 | cmd_output->buflen = 0; | ||
| 353 | ssize_t ret; | ||
| 354 | while ((ret = read(fileDescriptor, tmpbuf, sizeof(tmpbuf))) > 0) { | ||
| 355 | size_t len = (size_t)ret; | ||
| 356 | cmd_output->buf = realloc(cmd_output->buf, cmd_output->buflen + len + 1); | ||
| 357 | memcpy(cmd_output->buf + cmd_output->buflen, tmpbuf, len); | ||
| 358 | cmd_output->buflen += len; | ||
| 359 | } | ||
| 360 | |||
| 208 | if (ret < 0) { | 361 | if (ret < 0) { |
| 209 | printf("read() returned %d: %s\n", ret, strerror(errno)); | 362 | printf("read() returned %zd: %s\n", ret, strerror(errno)); |
| 210 | return ret; | 363 | return ret; |
| 211 | } | 364 | } |
| 212 | 365 | ||
| 213 | /* some plugins may want to keep output unbroken, and some commands | 366 | /* some plugins may want to keep output unbroken, and some commands |
| 214 | * will yield no output, so return here for those */ | 367 | * will yield no output, so return here for those */ |
| 215 | if (flags & CMD_NO_ARRAYS || !op->buf || !op->buflen) | 368 | if (flags & CMD_NO_ARRAYS || !cmd_output->buf || !cmd_output->buflen) { |
| 216 | return op->buflen; | 369 | return cmd_output->buflen; |
| 370 | } | ||
| 217 | 371 | ||
| 218 | /* and some may want both */ | 372 | /* and some may want both */ |
| 373 | char *buf = NULL; | ||
| 219 | if (flags & CMD_NO_ASSOC) { | 374 | if (flags & CMD_NO_ASSOC) { |
| 220 | buf = malloc(op->buflen); | 375 | buf = malloc(cmd_output->buflen); |
| 221 | memcpy(buf, op->buf, op->buflen); | 376 | memcpy(buf, cmd_output->buf, cmd_output->buflen); |
| 222 | } else | 377 | } else { |
| 223 | buf = op->buf; | 378 | buf = cmd_output->buf; |
| 224 | 379 | } | |
| 225 | op->line = NULL; | 380 | |
| 226 | op->lens = NULL; | 381 | cmd_output->line = NULL; |
| 227 | i = 0; | 382 | size_t i = 0; |
| 228 | while (i < op->buflen) { | 383 | size_t ary_size = 0; /* rsf = right shift factor, dec'ed uncond once */ |
| 384 | size_t rsf = 6; | ||
| 385 | size_t lineno = 0; | ||
| 386 | while (i < cmd_output->buflen) { | ||
| 229 | /* make sure we have enough memory */ | 387 | /* make sure we have enough memory */ |
| 230 | if (lineno >= ary_size) { | 388 | if (lineno >= ary_size) { |
| 231 | /* ary_size must never be zero */ | 389 | /* ary_size must never be zero */ |
| 232 | do { | 390 | do { |
| 233 | ary_size = op->buflen >> --rsf; | 391 | ary_size = cmd_output->buflen >> --rsf; |
| 234 | } while (!ary_size); | 392 | } while (!ary_size); |
| 235 | 393 | ||
| 236 | op->line = realloc(op->line, ary_size * sizeof(char *)); | 394 | cmd_output->line = realloc(cmd_output->line, ary_size * sizeof(char *)); |
| 237 | op->lens = realloc(op->lens, ary_size * sizeof(size_t)); | ||
| 238 | } | 395 | } |
| 239 | 396 | ||
| 240 | /* set the pointer to the string */ | 397 | /* set the pointer to the string */ |
| 241 | op->line[lineno] = &buf[i]; | 398 | cmd_output->line[lineno] = &buf[i]; |
| 242 | 399 | ||
| 243 | /* hop to next newline or end of buffer */ | 400 | /* hop to next newline or end of buffer */ |
| 244 | while (buf[i] != '\n' && i < op->buflen) | 401 | while (buf[i] != '\n' && i < cmd_output->buflen) { |
| 245 | i++; | 402 | i++; |
| 403 | } | ||
| 246 | buf[i] = '\0'; | 404 | buf[i] = '\0'; |
| 247 | 405 | ||
| 248 | /* calculate the string length using pointer difference */ | ||
| 249 | op->lens[lineno] = (size_t)&buf[i] - (size_t)op->line[lineno]; | ||
| 250 | |||
| 251 | lineno++; | 406 | lineno++; |
| 252 | i++; | 407 | i++; |
| 253 | } | 408 | } |
| @@ -256,41 +411,42 @@ static int _cmd_fetch_output(int fd, output *op, int flags) { | |||
| 256 | } | 411 | } |
| 257 | 412 | ||
| 258 | int cmd_run(const char *cmdstring, output *out, output *err, int flags) { | 413 | int cmd_run(const char *cmdstring, output *out, output *err, int flags) { |
| 259 | int i = 0, argc; | 414 | 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; | 415 | return -1; |
| 416 | } | ||
| 267 | 417 | ||
| 268 | /* initialize the structs */ | 418 | /* initialize the structs */ |
| 269 | if (out) | 419 | if (out) { |
| 270 | memset(out, 0, sizeof(output)); | 420 | memset(out, 0, sizeof(output)); |
| 271 | if (err) | 421 | } |
| 422 | if (err) { | ||
| 272 | memset(err, 0, sizeof(output)); | 423 | memset(err, 0, sizeof(output)); |
| 424 | } | ||
| 273 | 425 | ||
| 274 | /* make copy of command string so strtok() doesn't silently modify it */ | 426 | /* make copy of command string so strtok() doesn't silently modify it */ |
| 275 | /* (the calling program may want to access it later) */ | 427 | /* (the calling program may want to access it later) */ |
| 276 | cmdlen = strlen(cmdstring); | 428 | size_t cmdlen = strlen(cmdstring); |
| 277 | if ((cmd = malloc(cmdlen + 1)) == NULL) | 429 | char *cmd = NULL; |
| 430 | if ((cmd = malloc(cmdlen + 1)) == NULL) { | ||
| 278 | return -1; | 431 | return -1; |
| 432 | } | ||
| 279 | memcpy(cmd, cmdstring, cmdlen); | 433 | memcpy(cmd, cmdstring, cmdlen); |
| 280 | cmd[cmdlen] = '\0'; | 434 | cmd[cmdlen] = '\0'; |
| 281 | 435 | ||
| 282 | /* This is not a shell, so we don't handle "???" */ | 436 | /* This is not a shell, so we don't handle "???" */ |
| 283 | if (strstr(cmdstring, "\"")) | 437 | if (strstr(cmdstring, "\"")) { |
| 284 | return -1; | 438 | return -1; |
| 439 | } | ||
| 285 | 440 | ||
| 286 | /* allow single quotes, but only if non-whitesapce doesn't occur on both sides */ | 441 | /* allow single quotes, but only if non-whitesapce doesn't occur on both sides */ |
| 287 | if (strstr(cmdstring, " ' ") || strstr(cmdstring, "'''")) | 442 | if (strstr(cmdstring, " ' ") || strstr(cmdstring, "'''")) { |
| 288 | return -1; | 443 | return -1; |
| 444 | } | ||
| 289 | 445 | ||
| 290 | /* each arg must be whitespace-separated, so args can be a maximum | 446 | /* 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 */ | 447 | * of (len / 2) + 1. We add 1 extra to the mix for NULL termination */ |
| 292 | argc = (cmdlen >> 1) + 2; | 448 | int argc = (cmdlen >> 1) + 2; |
| 293 | argv = calloc((size_t)argc, sizeof(char *)); | 449 | char **argv = calloc((size_t)argc, sizeof(char *)); |
| 294 | 450 | ||
| 295 | if (argv == NULL) { | 451 | if (argv == NULL) { |
| 296 | printf("%s\n", _("Could not malloc argv array in popen()")); | 452 | printf("%s\n", _("Could not malloc argv array in popen()")); |
| @@ -298,14 +454,16 @@ int cmd_run(const char *cmdstring, output *out, output *err, int flags) { | |||
| 298 | } | 454 | } |
| 299 | 455 | ||
| 300 | /* get command arguments (stupidly, but fairly quickly) */ | 456 | /* get command arguments (stupidly, but fairly quickly) */ |
| 457 | int i = 0; | ||
| 301 | while (cmd) { | 458 | while (cmd) { |
| 302 | str = cmd; | 459 | char *str = cmd; |
| 303 | str += strspn(str, " \t\r\n"); /* trim any leading whitespace */ | 460 | str += strspn(str, " \t\r\n"); /* trim any leading whitespace */ |
| 304 | 461 | ||
| 305 | if (strstr(str, "'") == str) { /* handle SIMPLE quoted strings */ | 462 | if (strstr(str, "'") == str) { /* handle SIMPLE quoted strings */ |
| 306 | str++; | 463 | str++; |
| 307 | if (!strstr(str, "'")) | 464 | if (!strstr(str, "'")) { |
| 308 | return -1; /* balanced? */ | 465 | return -1; /* balanced? */ |
| 466 | } | ||
| 309 | cmd = 1 + strstr(str, "'"); | 467 | cmd = 1 + strstr(str, "'"); |
| 310 | str[strcspn(str, "'")] = 0; | 468 | str[strcspn(str, "'")] = 0; |
| 311 | } else { | 469 | } else { |
| @@ -317,8 +475,9 @@ int cmd_run(const char *cmdstring, output *out, output *err, int flags) { | |||
| 317 | } | 475 | } |
| 318 | } | 476 | } |
| 319 | 477 | ||
| 320 | if (cmd && strlen(cmd) == strspn(cmd, " \t\r\n")) | 478 | if (cmd && strlen(cmd) == strspn(cmd, " \t\r\n")) { |
| 321 | cmd = NULL; | 479 | cmd = NULL; |
| 480 | } | ||
| 322 | 481 | ||
| 323 | argv[i++] = str; | 482 | argv[i++] = str; |
| 324 | } | 483 | } |
| @@ -326,54 +485,199 @@ int cmd_run(const char *cmdstring, output *out, output *err, int flags) { | |||
| 326 | return cmd_run_array(argv, out, err, flags); | 485 | return cmd_run_array(argv, out, err, flags); |
| 327 | } | 486 | } |
| 328 | 487 | ||
| 329 | int cmd_run_array(char *const *argv, output *out, output *err, int flags) { | 488 | cmd_run_result cmd_run2(const char *cmd_string, int flags) { |
| 330 | int fd, pfd_out[2], pfd_err[2]; | 489 | cmd_run_result result = { |
| 490 | .cmd_error_code = 0, | ||
| 491 | .error_code = 0, | ||
| 492 | .stderr = | ||
| 493 | { | ||
| 494 | .buf = NULL, | ||
| 495 | .buflen = 0, | ||
| 496 | .line = NULL, | ||
| 497 | .lines = 0, | ||
| 498 | }, | ||
| 499 | .stdout = | ||
| 500 | { | ||
| 501 | .buf = NULL, | ||
| 502 | .buflen = 0, | ||
| 503 | .line = NULL, | ||
| 504 | .lines = 0, | ||
| 505 | }, | ||
| 506 | }; | ||
| 507 | |||
| 508 | if (cmd_string == NULL) { | ||
| 509 | result.error_code = -1; | ||
| 510 | return result; | ||
| 511 | } | ||
| 512 | |||
| 513 | /* make copy of command string so strtok() doesn't silently modify it */ | ||
| 514 | /* (the calling program may want to access it later) */ | ||
| 515 | char *cmd = strdup(cmd_string); | ||
| 516 | if (cmd == NULL) { | ||
| 517 | result.error_code = -1; | ||
| 518 | return result; | ||
| 519 | } | ||
| 520 | |||
| 521 | /* This is not a shell, so we don't handle "???" */ | ||
| 522 | if (strstr(cmd, "\"")) { | ||
| 523 | result.error_code = -1; | ||
| 524 | return result; | ||
| 525 | } | ||
| 526 | |||
| 527 | /* allow single quotes, but only if non-whitesapce doesn't occur on both sides */ | ||
| 528 | if (strstr(cmd, " ' ") || strstr(cmd, "'''")) { | ||
| 529 | result.error_code = -1; | ||
| 530 | return result; | ||
| 531 | } | ||
| 532 | |||
| 533 | /* each arg must be whitespace-separated, so args can be a maximum | ||
| 534 | * of (len / 2) + 1. We add 1 extra to the mix for NULL termination */ | ||
| 535 | size_t cmdlen = strlen(cmd_string); | ||
| 536 | size_t argc = (cmdlen >> 1) + 2; | ||
| 537 | char **argv = calloc(argc, sizeof(char *)); | ||
| 538 | |||
| 539 | if (argv == NULL) { | ||
| 540 | printf("%s\n", _("Could not malloc argv array in popen()")); | ||
| 541 | result.error_code = -1; | ||
| 542 | return result; | ||
| 543 | } | ||
| 331 | 544 | ||
| 545 | /* get command arguments (stupidly, but fairly quickly) */ | ||
| 546 | for (int i = 0; cmd; i++) { | ||
| 547 | char *str = cmd; | ||
| 548 | str += strspn(str, " \t\r\n"); /* trim any leading whitespace */ | ||
| 549 | |||
| 550 | if (strstr(str, "'") == str) { /* handle SIMPLE quoted strings */ | ||
| 551 | str++; | ||
| 552 | if (!strstr(str, "'")) { | ||
| 553 | result.error_code = -1; | ||
| 554 | return result; /* balanced? */ | ||
| 555 | } | ||
| 556 | |||
| 557 | cmd = 1 + strstr(str, "'"); | ||
| 558 | str[strcspn(str, "'")] = 0; | ||
| 559 | } else { | ||
| 560 | if (strpbrk(str, " \t\r\n")) { | ||
| 561 | cmd = 1 + strpbrk(str, " \t\r\n"); | ||
| 562 | str[strcspn(str, " \t\r\n")] = 0; | ||
| 563 | } else { | ||
| 564 | cmd = NULL; | ||
| 565 | } | ||
| 566 | } | ||
| 567 | |||
| 568 | if (cmd && strlen(cmd) == strspn(cmd, " \t\r\n")) { | ||
| 569 | cmd = NULL; | ||
| 570 | } | ||
| 571 | |||
| 572 | argv[i++] = str; | ||
| 573 | } | ||
| 574 | |||
| 575 | result = cmd_run_array2(argv, flags); | ||
| 576 | |||
| 577 | return result; | ||
| 578 | } | ||
| 579 | |||
| 580 | cmd_run_result cmd_run_array2(char *const *cmd, int flags) { | ||
| 581 | cmd_run_result result = { | ||
| 582 | .cmd_error_code = 0, | ||
| 583 | .error_code = 0, | ||
| 584 | .stderr = | ||
| 585 | { | ||
| 586 | .buf = NULL, | ||
| 587 | .buflen = 0, | ||
| 588 | .line = NULL, | ||
| 589 | .lines = 0, | ||
| 590 | }, | ||
| 591 | .stdout = | ||
| 592 | { | ||
| 593 | .buf = NULL, | ||
| 594 | .buflen = 0, | ||
| 595 | .line = NULL, | ||
| 596 | .lines = 0, | ||
| 597 | }, | ||
| 598 | }; | ||
| 599 | |||
| 600 | int_cmd_open_result cmd_open_result = _cmd_open2(cmd); | ||
| 601 | if (cmd_open_result.error_code != 0) { | ||
| 602 | // result.error_code = -1; | ||
| 603 | // return result; | ||
| 604 | // TODO properly handle this without dying | ||
| 605 | die(STATE_UNKNOWN, _("Could not open pipe: %s\n"), cmd[0]); | ||
| 606 | } | ||
| 607 | |||
| 608 | int file_descriptor = cmd_open_result.file_descriptor; | ||
| 609 | int pfd_out[2] = {cmd_open_result.stdout_pipe_fd[0], cmd_open_result.stdout_pipe_fd[1]}; | ||
| 610 | int pfd_err[2] = {cmd_open_result.stderr_pipe_fd[0], cmd_open_result.stderr_pipe_fd[1]}; | ||
| 611 | |||
| 612 | int_cmd_fetch_output2 tmp_stdout = _cmd_fetch_output2(pfd_out[0], flags); | ||
| 613 | result.stdout = tmp_stdout.output_container; | ||
| 614 | int_cmd_fetch_output2 tmp_stderr = _cmd_fetch_output2(pfd_err[0], flags); | ||
| 615 | result.stderr = tmp_stderr.output_container; | ||
| 616 | |||
| 617 | result.cmd_error_code = _cmd_close(file_descriptor); | ||
| 618 | return result; | ||
| 619 | } | ||
| 620 | |||
| 621 | int cmd_run_array(char *const *argv, output *out, output *err, int flags) { | ||
| 332 | /* initialize the structs */ | 622 | /* initialize the structs */ |
| 333 | if (out) | 623 | if (out) { |
| 334 | memset(out, 0, sizeof(output)); | 624 | memset(out, 0, sizeof(output)); |
| 335 | if (err) | 625 | } |
| 626 | if (err) { | ||
| 336 | memset(err, 0, sizeof(output)); | 627 | memset(err, 0, sizeof(output)); |
| 628 | } | ||
| 337 | 629 | ||
| 338 | if ((fd = _cmd_open(argv, pfd_out, pfd_err)) == -1) | 630 | int fd; |
| 631 | int pfd_out[2]; | ||
| 632 | int pfd_err[2]; | ||
| 633 | if ((fd = _cmd_open(argv, pfd_out, pfd_err)) == -1) { | ||
| 339 | die(STATE_UNKNOWN, _("Could not open pipe: %s\n"), argv[0]); | 634 | die(STATE_UNKNOWN, _("Could not open pipe: %s\n"), argv[0]); |
| 635 | } | ||
| 340 | 636 | ||
| 341 | if (out) | 637 | if (out) { |
| 342 | out->lines = _cmd_fetch_output(pfd_out[0], out, flags); | 638 | out->lines = _cmd_fetch_output(pfd_out[0], out, flags); |
| 343 | if (err) | 639 | } |
| 640 | if (err) { | ||
| 344 | err->lines = _cmd_fetch_output(pfd_err[0], err, flags); | 641 | err->lines = _cmd_fetch_output(pfd_err[0], err, flags); |
| 642 | } | ||
| 345 | 643 | ||
| 346 | return _cmd_close(fd); | 644 | return _cmd_close(fd); |
| 347 | } | 645 | } |
| 348 | 646 | ||
| 349 | int cmd_file_read(char *filename, output *out, int flags) { | 647 | int cmd_file_read(const char *filename, output *out, int flags) { |
| 350 | int fd; | 648 | int fd; |
| 351 | if (out) | 649 | if (out) { |
| 352 | memset(out, 0, sizeof(output)); | 650 | memset(out, 0, sizeof(output)); |
| 651 | } | ||
| 353 | 652 | ||
| 354 | if ((fd = open(filename, O_RDONLY)) == -1) { | 653 | if ((fd = open(filename, O_RDONLY)) == -1) { |
| 355 | die(STATE_UNKNOWN, _("Error opening %s: %s"), filename, strerror(errno)); | 654 | die(STATE_UNKNOWN, _("Error opening %s: %s"), filename, strerror(errno)); |
| 356 | } | 655 | } |
| 357 | 656 | ||
| 358 | if (out) | 657 | if (out) { |
| 359 | out->lines = _cmd_fetch_output(fd, out, flags); | 658 | out->lines = _cmd_fetch_output(fd, out, flags); |
| 659 | } | ||
| 360 | 660 | ||
| 361 | if (close(fd) == -1) | 661 | if (close(fd) == -1) { |
| 362 | die(STATE_UNKNOWN, _("Error closing %s: %s"), filename, strerror(errno)); | 662 | die(STATE_UNKNOWN, _("Error closing %s: %s"), filename, strerror(errno)); |
| 663 | } | ||
| 363 | 664 | ||
| 364 | return 0; | 665 | return 0; |
| 365 | } | 666 | } |
| 366 | 667 | ||
| 367 | void timeout_alarm_handler(int signo) { | 668 | void timeout_alarm_handler(int signo) { |
| 368 | if (signo == SIGALRM) { | 669 | if (signo == SIGALRM) { |
| 369 | printf(_("%s - Plugin timed out after %d seconds\n"), state_text(timeout_state), timeout_interval); | 670 | printf(_("%s - Plugin timed out after %d seconds\n"), state_text(timeout_state), |
| 671 | timeout_interval); | ||
| 370 | 672 | ||
| 371 | long maxfd = mp_open_max(); | 673 | long maxfd = mp_open_max(); |
| 372 | if (_cmd_pids) | 674 | if (_cmd_pids) { |
| 373 | for (long int i = 0; i < maxfd; i++) { | 675 | for (long int i = 0; i < maxfd; i++) { |
| 374 | if (_cmd_pids[i] != 0) | 676 | if (_cmd_pids[i] != 0) { |
| 375 | kill(_cmd_pids[i], SIGKILL); | 677 | kill(_cmd_pids[i], SIGKILL); |
| 678 | } | ||
| 376 | } | 679 | } |
| 680 | } | ||
| 377 | 681 | ||
| 378 | exit(timeout_state); | 682 | exit(timeout_state); |
| 379 | } | 683 | } |
