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