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
112
109
/* Create a new plugin */
113
plugin *new_plugin = NULL;
115
new_plugin = malloc(sizeof(plugin));
116
} while(new_plugin == NULL and errno == EINTR);
110
plugin *new_plugin = malloc(sizeof(plugin));
117
111
if(new_plugin == NULL){
120
114
char *copy_name = NULL;
121
115
if(name != NULL){
123
copy_name = strdup(name);
124
} while(copy_name == NULL and errno == EINTR);
116
copy_name = strdup(name);
125
117
if(copy_name == NULL){
131
*new_plugin = (plugin){ .name = copy_name,
134
.next = plugin_list };
122
*new_plugin = (plugin) { .name = copy_name,
125
.next = plugin_list };
137
new_plugin->argv = malloc(sizeof(char *) * 2);
138
} while(new_plugin->argv == NULL and errno == EINTR);
127
new_plugin->argv = malloc(sizeof(char *) * 2);
139
128
if(new_plugin->argv == NULL){
141
130
free(new_plugin);
144
133
new_plugin->argv[0] = copy_name;
145
134
new_plugin->argv[1] = NULL;
148
new_plugin->environ = malloc(sizeof(char *));
149
} while(new_plugin->environ == NULL and errno == EINTR);
136
new_plugin->environ = malloc(sizeof(char *));
150
137
if(new_plugin->environ == NULL){
152
139
free(new_plugin->argv);
164
151
static bool add_to_char_array(const char *new, char ***array,
166
153
/* Resize the pointed-to array to hold one more pointer */
168
*array = realloc(*array, sizeof(char *)
169
* (size_t) ((*len) + 2));
170
} while(*array == NULL and errno == EINTR);
154
*array = realloc(*array, sizeof(char *)
155
* (size_t) ((*len) + 2));
171
156
/* Malloc check */
172
157
if(*array == NULL){
175
160
/* Make a copy of the new string */
179
} while(copy == NULL and errno == EINTR);
161
char *copy = strdup(new);
180
162
if(copy == NULL){
208
190
if(strncmp(*e, def, namelen + 1) == 0){
209
191
/* It already exists */
213
new = realloc(*e, strlen(def) + 1);
214
} while(new == NULL and errno == EINTR);
193
char *new = realloc(*e, strlen(def) + 1);
228
207
* Based on the example in the GNU LibC manual chapter 13.13 "File
229
208
* Descriptor Flags".
230
| [[info:libc:Descriptor%20Flags][File Descriptor Flags]] |
209
* *Note File Descriptor Flags:(libc)Descriptor Flags.
232
211
static int set_cloexec_flag(int fd){
233
int ret = TEMP_FAILURE_RETRY(fcntl(fd, F_GETFD, 0));
212
int ret = fcntl(fd, F_GETFD, 0);
234
213
/* If reading the flags failed, return error indication now. */
238
217
/* Store modified flag word in the descriptor. */
239
return TEMP_FAILURE_RETRY(fcntl(fd, F_SETFD, ret | FD_CLOEXEC));
218
return fcntl(fd, F_SETFD, ret | FD_CLOEXEC);
243
222
/* Mark processes as completed when they exit, and save their exit
245
224
static void handle_sigchld(__attribute__((unused)) int sig){
246
int old_errno = errno;
248
226
plugin *proc = plugin_list;
399
377
error_t parse_opt(int key, char *arg, __attribute__((unused))
400
struct argp_state *state){
378
struct argp_state *state) {
404
380
case 'g': /* --global-options */
407
while((plugin_option = strsep(&arg, ",")) != NULL){
408
if(plugin_option[0] == '\0'){
383
while((p = strsep(&arg, ",")) != NULL){
411
if(not add_argument(getplugin(NULL), plugin_option)){
387
if(not add_argument(getplugin(NULL), p)){
412
388
perror("add_argument");
413
389
return ARGP_ERR_UNKNOWN;
426
402
case 'o': /* --options-for */
428
char *plugin_name = strsep(&arg, ":");
429
if(plugin_name[0] == '\0'){
433
while((plugin_option = strsep(&arg, ",")) != NULL){
434
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)){
435
418
perror("add_argument");
436
419
return ARGP_ERR_UNKNOWN;
476
459
plugindir = strdup(arg);
477
460
if(plugindir == NULL){
478
461
perror("strdup");
481
464
case 129: /* --config-file */
482
465
/* This is already done by parse_opt_config_file() */
484
467
case 130: /* --userid */
486
tmpmax = strtoimax(arg, &tmp, 10);
487
if(errno != 0 or tmp == arg or *tmp != '\0'
488
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'){
489
471
fprintf(stderr, "Bad user ID number: \"%s\", using %"
490
472
PRIdMAX "\n", arg, (intmax_t)uid);
495
477
case 131: /* --groupid */
497
tmpmax = strtoimax(arg, &tmp, 10);
498
if(errno != 0 or tmp == arg or *tmp != '\0'
499
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'){
500
481
fprintf(stderr, "Bad group ID number: \"%s\", using %"
501
482
PRIdMAX "\n", arg, (intmax_t)gid);
530
511
ignores everything but the --config-file option. */
531
512
error_t parse_opt_config_file(int key, char *arg,
532
513
__attribute__((unused))
533
struct argp_state *state){
514
struct argp_state *state) {
535
516
case 'g': /* --global-options */
536
517
case 'G': /* --global-env */
537
518
case 'o': /* --options-for */
581
562
conffp = fopen(AFILE, "r");
583
564
conffp = fopen(argfile, "r");
585
566
if(conffp != NULL){
586
567
char *org_line = NULL;
587
568
char *p, *arg, *new_arg, *line;
589
570
const char whitespace_delims[] = " \r\t\f\v\n";
590
571
const char comment_delim[] = "#";
593
574
custom_argv = malloc(sizeof(char*) * 2);
594
575
if(custom_argv == NULL){
633
614
custom_argv[custom_argc-1] = new_arg;
634
custom_argv[custom_argc] = NULL;
615
custom_argv[custom_argc] = NULL;
638
ret = fclose(conffp);
639
} while(ret == EOF and errno == EINTR);
642
exitstatus = EXIT_FAILURE;
647
620
/* Check for harmful errors and go to fallback. Other errors might
790
761
if(plugindir == NULL){
791
ret = TEMP_FAILURE_RETRY(asprintf(&filename, PDIR "/%s",
762
ret = asprintf(&filename, PDIR "/%s", dirst->d_name);
794
ret = TEMP_FAILURE_RETRY(asprintf(&filename, "%s/%s", plugindir,
764
ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
798
767
perror("asprintf");
802
ret = TEMP_FAILURE_RETRY(stat(filename, &st));
771
ret = stat(filename, &st);
809
778
/* Ignore non-executable files */
810
if(not S_ISREG(st.st_mode)
811
or (TEMP_FAILURE_RETRY(access(filename, X_OK)) != 0)){
779
if(not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
813
781
fprintf(stderr, "Ignoring plugin dir entry \"%s\""
814
782
" with bad type or mode\n", filename);
882
850
/* Block SIGCHLD until process is safely in process list */
883
ret = TEMP_FAILURE_RETRY(sigprocmask(SIG_BLOCK,
884
&sigchld_action.sa_mask,
851
ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
887
853
perror("sigprocmask");
888
854
exitstatus = EXIT_FAILURE;
891
857
/* Starting a new process to be watched */
895
} while(pid == -1 and errno == EINTR);
898
861
exitstatus = EXIT_FAILURE;
938
901
/* Parent process */
939
TEMP_FAILURE_RETRY(close(pipefd[1])); /* Close unused write end of
902
close(pipefd[1]); /* Close unused write end of pipe */
942
904
plugin *new_plugin = getplugin(dirst->d_name);
943
905
if(new_plugin == NULL){
944
906
perror("getplugin");
945
ret = TEMP_FAILURE_RETRY(sigprocmask(SIG_UNBLOCK,
946
&sigchld_action.sa_mask,
907
ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
949
909
perror("sigprocmask");
958
918
/* Unblock SIGCHLD so signal handler can be run if this process
959
919
has already completed */
960
ret = TEMP_FAILURE_RETRY(sigprocmask(SIG_UNBLOCK,
961
&sigchld_action.sa_mask,
920
ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
964
922
perror("sigprocmask");
965
923
exitstatus = EXIT_FAILURE;
992
949
while(plugin_list){
993
950
fd_set rfds = rfds_all;
994
951
int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
995
if(select_ret == -1 and errno != EINTR){
952
if(select_ret == -1){
996
953
perror("select");
997
954
exitstatus = EXIT_FAILURE;
1001
958
from one of them */
1002
959
for(plugin *proc = plugin_list; proc != NULL;){
1003
960
/* Is this process completely done? */
1004
if(proc->completed and proc->eof){
961
if(proc->eof and proc->completed){
1005
962
/* Only accept the plugin output if it exited cleanly */
1006
963
if(not WIFEXITED(proc->status)
1007
964
or WEXITSTATUS(proc->status) != 0){
1008
965
/* Bad exit by plugin */
1011
968
if(WIFEXITED(proc->status)){
1012
fprintf(stderr, "Plugin %s [%" PRIdMAX "] exited with"
1013
" status %d\n", proc->name,
1014
(intmax_t) (proc->pid),
969
fprintf(stderr, "Plugin %" PRIdMAX " exited with status"
970
" %d\n", (intmax_t) (proc->pid),
1015
971
WEXITSTATUS(proc->status));
1016
} else if(WIFSIGNALED(proc->status)){
1017
fprintf(stderr, "Plugin %s [%" PRIdMAX "] killed by"
1018
" signal %d: %s\n", proc->name,
1019
(intmax_t) (proc->pid),
1020
WTERMSIG(proc->status),
1021
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));
1022
976
} else if(WCOREDUMP(proc->status)){
1023
fprintf(stderr, "Plugin %s [%" PRIdMAX "] dumped"
1024
" core\n", proc->name, (intmax_t) (proc->pid));
977
fprintf(stderr, "Plugin %" PRIdMAX " dumped core\n",
978
(intmax_t) (proc->pid));
1028
982
/* Remove the plugin */
1029
983
FD_CLR(proc->fd, &rfds_all);
1031
985
/* Block signal while modifying process_list */
1032
ret = TEMP_FAILURE_RETRY(sigprocmask(SIG_BLOCK,
1033
&sigchld_action.sa_mask,
986
ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
1036
988
perror("sigprocmask");
1037
989
exitstatus = EXIT_FAILURE;
1043
995
proc = next_plugin;
1045
997
/* We are done modifying process list, so unblock signal */
1046
ret = TEMP_FAILURE_RETRY(sigprocmask(SIG_UNBLOCK,
1047
&sigchld_action.sa_mask,
998
ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
1050
1001
perror("sigprocmask");
1051
1002
exitstatus = EXIT_FAILURE;
1088
1039
proc->buffer_size += BUFFER_SIZE;
1090
1041
/* Read from the process */
1091
sret = TEMP_FAILURE_RETRY(read(proc->fd,
1093
+ proc->buffer_length,
1042
sret = read(proc->fd, proc->buffer + proc->buffer_length,
1096
1045
/* Read error from this process; ignore the error */
1097
1046
proc = proc->next;