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