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
115
118
if(name != NULL){
116
119
copy_name = strdup(name);
117
120
if(copy_name == NULL){
122
*new_plugin = (plugin) { .name = copy_name,
125
.next = plugin_list };
126
*new_plugin = (plugin){ .name = copy_name,
129
.next = plugin_list };
127
131
new_plugin->argv = malloc(sizeof(char *) * 2);
128
132
if(new_plugin->argv == NULL){
207
211
* Based on the example in the GNU LibC manual chapter 13.13 "File
208
212
* Descriptor Flags".
209
* *Note File Descriptor Flags:(libc)Descriptor Flags.
213
| [[info:libc:Descriptor%20Flags][File Descriptor Flags]] |
211
215
static int set_cloexec_flag(int fd){
212
216
int ret = fcntl(fd, F_GETFD, 0);
377
382
error_t parse_opt(int key, char *arg, __attribute__((unused))
378
struct argp_state *state) {
383
struct argp_state *state){
380
387
case 'g': /* --global-options */
383
while((p = strsep(&arg, ",")) != NULL){
390
while((plugin_option = strsep(&arg, ",")) != NULL){
391
if(plugin_option[0] == '\0'){
387
if(not add_argument(getplugin(NULL), p)){
394
if(not add_argument(getplugin(NULL), plugin_option)){
388
395
perror("add_argument");
389
396
return ARGP_ERR_UNKNOWN;
402
409
case 'o': /* --options-for */
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)){
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)){
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 */
468
ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
469
if(ret < 1 or tmpmax != (uid_t)tmpmax
470
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){
471
472
fprintf(stderr, "Bad user ID number: \"%s\", using %"
472
473
PRIdMAX "\n", arg, (intmax_t)uid);
477
478
case 131: /* --groupid */
478
ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
479
if(ret < 1 or tmpmax != (gid_t)tmpmax
480
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){
481
483
fprintf(stderr, "Bad group ID number: \"%s\", using %"
482
484
PRIdMAX "\n", arg, (intmax_t)gid);
511
513
ignores everything but the --config-file option. */
512
514
error_t parse_opt_config_file(int key, char *arg,
513
515
__attribute__((unused))
514
struct argp_state *state) {
516
struct argp_state *state){
516
518
case 'g': /* --global-options */
517
519
case 'G': /* --global-env */
518
520
case 'o': /* --options-for */
562
564
conffp = fopen(AFILE, "r");
564
566
conffp = fopen(argfile, "r");
566
568
if(conffp != NULL){
567
569
char *org_line = NULL;
568
570
char *p, *arg, *new_arg, *line;
570
572
const char whitespace_delims[] = " \r\t\f\v\n";
571
573
const char comment_delim[] = "#";
574
576
custom_argv = malloc(sizeof(char*) * 2);
575
577
if(custom_argv == NULL){
614
616
custom_argv[custom_argc-1] = new_arg;
615
custom_argv[custom_argc] = NULL;
617
custom_argv[custom_argc] = NULL;
621
ret = fclose(conffp);
622
}while(ret == EOF and errno == EINTR);
625
exitstatus = EXIT_FAILURE;
620
630
/* Check for harmful errors and go to fallback. Other errors might
958
969
from one of them */
959
970
for(plugin *proc = plugin_list; proc != NULL;){
960
971
/* Is this process completely done? */
961
if(proc->eof and proc->completed){
972
if(proc->completed and proc->eof){
962
973
/* Only accept the plugin output if it exited cleanly */
963
974
if(not WIFEXITED(proc->status)
964
975
or WEXITSTATUS(proc->status) != 0){
965
976
/* Bad exit by plugin */
968
979
if(WIFEXITED(proc->status)){
969
fprintf(stderr, "Plugin %" PRIdMAX " exited with status"
970
" %d\n", (intmax_t) (proc->pid),
980
fprintf(stderr, "Plugin %s [%" PRIdMAX "] exited with"
981
" status %d\n", proc->name,
982
(intmax_t) (proc->pid),
971
983
WEXITSTATUS(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));
984
} else if(WIFSIGNALED(proc->status)){
985
fprintf(stderr, "Plugin %s [%" PRIdMAX "] killed by"
986
" signal %d: %s\n", proc->name,
987
(intmax_t) (proc->pid),
988
WTERMSIG(proc->status),
989
strsignal(WTERMSIG(proc->status)));
976
990
} else if(WCOREDUMP(proc->status)){
977
fprintf(stderr, "Plugin %" PRIdMAX " dumped core\n",
978
(intmax_t) (proc->pid));
991
fprintf(stderr, "Plugin %s [%" PRIdMAX "] dumped"
992
" core\n", proc->name, (intmax_t) (proc->pid));
982
996
/* Remove the plugin */
983
997
FD_CLR(proc->fd, &rfds_all);
985
999
/* Block signal while modifying process_list */
986
1000
ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);