38
38
#include <sys/select.h> /* fd_set, select(), FD_ZERO(),
39
39
FD_SET(), FD_ISSET(), FD_CLR */
40
40
#include <sys/wait.h> /* wait(), waitpid(), WIFEXITED(),
41
WEXITSTATUS(), WTERMSIG(),
42
43
#include <sys/stat.h> /* struct stat, stat(), S_ISREG() */
43
44
#include <iso646.h> /* and, or, not */
44
45
#include <dirent.h> /* DIR, struct dirent, opendir(),
53
54
#include <fcntl.h> /* fcntl(), F_GETFD, F_SETFD,
55
#include <string.h> /* strsep, strlen(), asprintf() */
56
#include <string.h> /* strsep, strlen(), asprintf(),
56
58
#include <errno.h> /* errno */
57
59
#include <argp.h> /* struct argp_option, struct
58
60
argp_state, struct argp,
62
64
#include <signal.h> /* struct sigaction, sigemptyset(),
63
65
sigaddset(), sigaction(),
64
66
sigprocmask(), SIG_BLOCK, SIGCHLD,
65
SIG_UNBLOCK, kill() */
67
SIG_UNBLOCK, kill(), sig_atomic_t
66
69
#include <errno.h> /* errno, EBADF */
67
#include <inttypes.h> /* intmax_t, SCNdMAX, PRIdMAX, */
70
#include <inttypes.h> /* intmax_t, PRIdMAX, strtoimax() */
69
72
#define BUFFER_SIZE 256
123
*new_plugin = (plugin) { .name = copy_name,
126
.next = plugin_list };
126
*new_plugin = (plugin){ .name = copy_name,
129
.next = plugin_list };
128
131
new_plugin->argv = malloc(sizeof(char *) * 2);
129
132
if(new_plugin->argv == NULL){
378
383
error_t parse_opt(int key, char *arg, __attribute__((unused))
379
struct argp_state *state) {
384
struct argp_state *state){
381
387
case 'g': /* --global-options */
384
while((p = strsep(&arg, ",")) != NULL){
390
while((plugin_option = strsep(&arg, ",")) != NULL){
391
if(plugin_option[0] == '\0'){
388
if(not add_argument(getplugin(NULL), p)){
394
if(not add_argument(getplugin(NULL), plugin_option)){
389
395
perror("add_argument");
390
396
return ARGP_ERR_UNKNOWN;
403
409
case 'o': /* --options-for */
405
char *p_name = strsep(&arg, ":");
406
if(p_name[0] == '\0' or arg == NULL){
409
char *opt = strsep(&arg, ":");
410
if(opt[0] == '\0' or opt == NULL){
414
while((p = strsep(&opt, ",")) != NULL){
418
if(not add_argument(getplugin(p_name), p)){
411
char *plugin_name = strsep(&arg, ":");
412
if(plugin_name[0] == '\0'){
416
while((plugin_option = strsep(&arg, ",")) != NULL){
417
if(not add_argument(getplugin(plugin_name), plugin_option)){
419
418
perror("add_argument");
420
419
return ARGP_ERR_UNKNOWN;
460
459
plugindir = strdup(arg);
461
460
if(plugindir == NULL){
462
461
perror("strdup");
465
464
case 129: /* --config-file */
466
465
/* This is already done by parse_opt_config_file() */
468
467
case 130: /* --userid */
469
ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
470
if(ret < 1 or tmpmax != (uid_t)tmpmax
471
or arg[numchars] != '\0'){
469
tmpmax = strtoimax(arg, &tmp, 10);
470
if(errno != 0 or tmp == arg or *tmp != '\0'
471
or tmpmax != (uid_t)tmpmax){
472
472
fprintf(stderr, "Bad user ID number: \"%s\", using %"
473
473
PRIdMAX "\n", arg, (intmax_t)uid);
478
478
case 131: /* --groupid */
479
ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
480
if(ret < 1 or tmpmax != (gid_t)tmpmax
481
or arg[numchars] != '\0'){
480
tmpmax = strtoimax(arg, &tmp, 10);
481
if(errno != 0 or tmp == arg or *tmp != '\0'
482
or tmpmax != (gid_t)tmpmax){
482
483
fprintf(stderr, "Bad group ID number: \"%s\", using %"
483
484
PRIdMAX "\n", arg, (intmax_t)gid);
512
513
ignores everything but the --config-file option. */
513
514
error_t parse_opt_config_file(int key, char *arg,
514
515
__attribute__((unused))
515
struct argp_state *state) {
516
struct argp_state *state){
517
518
case 'g': /* --global-options */
518
519
case 'G': /* --global-env */
519
520
case 'o': /* --options-for */
563
564
conffp = fopen(AFILE, "r");
565
566
conffp = fopen(argfile, "r");
567
568
if(conffp != NULL){
568
569
char *org_line = NULL;
569
570
char *p, *arg, *new_arg, *line;
571
572
const char whitespace_delims[] = " \r\t\f\v\n";
572
573
const char comment_delim[] = "#";
575
576
custom_argv = malloc(sizeof(char*) * 2);
576
577
if(custom_argv == NULL){
959
961
from one of them */
960
962
for(plugin *proc = plugin_list; proc != NULL;){
961
963
/* Is this process completely done? */
962
if(proc->eof and proc->completed){
964
if(proc->completed and proc->eof){
963
965
/* Only accept the plugin output if it exited cleanly */
964
966
if(not WIFEXITED(proc->status)
965
967
or WEXITSTATUS(proc->status) != 0){
966
968
/* Bad exit by plugin */
969
971
if(WIFEXITED(proc->status)){
970
fprintf(stderr, "Plugin %" PRIdMAX " exited with status"
971
" %d\n", (intmax_t) (proc->pid),
972
fprintf(stderr, "Plugin %s [%" PRIdMAX "] exited with"
973
" status %d\n", proc->name,
974
(intmax_t) (proc->pid),
972
975
WEXITSTATUS(proc->status));
973
} else if(WIFSIGNALED(proc->status)) {
974
fprintf(stderr, "Plugin %" PRIdMAX " killed by signal"
975
" %d\n", (intmax_t) (proc->pid),
976
WTERMSIG(proc->status));
976
} else if(WIFSIGNALED(proc->status)){
977
fprintf(stderr, "Plugin %s [%" PRIdMAX "] killed by"
978
" signal %d: %s\n", proc->name,
979
(intmax_t) (proc->pid),
980
WTERMSIG(proc->status),
981
strsignal(WTERMSIG(proc->status)));
977
982
} else if(WCOREDUMP(proc->status)){
978
fprintf(stderr, "Plugin %" PRIdMAX " dumped core\n",
979
(intmax_t) (proc->pid));
983
fprintf(stderr, "Plugin %s [%" PRIdMAX "] dumped"
984
" core\n", proc->name, (intmax_t) (proc->pid));
983
988
/* Remove the plugin */
984
989
FD_CLR(proc->fd, &rfds_all);
986
991
/* Block signal while modifying process_list */
987
992
ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);