125
128
return fcntl(fd, F_SETFD, ret | FD_CLOEXEC);
129
131
#define BUFFER_SIZE 256
131
const char *argp_program_version =
132
"plugbasedclient 0.9";
133
const char *argp_program_bug_address =
134
"<mandos@fukt.bsnet.se>";
133
const char *argp_program_version = "plugbasedclient 0.9";
134
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
136
process *process_list = NULL;
138
/* Mark a process as completed when it exits, and save its exit
140
void handle_sigchld(__attribute__((unused)) int sig){
141
process *proc = process_list;
143
pid_t pid = wait(&status);
144
while(proc != NULL and proc->pid != pid){
148
/* Process not found in process list */
151
proc->status = status;
152
proc->completed = true;
136
155
int main(int argc, char *argv[]){
137
156
const char *plugindir = "/conf/conf.d/mandos/plugins.d";
138
157
size_t d_name_len;
140
159
struct dirent *dirst;
143
162
int ret, maxfd = 0;
144
process *process_list = NULL;
145
163
bool debug = false;
146
164
int exitstatus = EXIT_SUCCESS;
166
/* Establish a signal handler */
167
struct sigaction old_sigchld_action,
168
sigchld_action = { .sa_handler = handle_sigchld,
169
.sa_flags = SA_NOCLDSTOP };
170
sigemptyset(&sigchld_action.sa_mask);
171
ret = sigaddset(&sigchld_action.sa_mask, SIGCHLD);
176
ret = sigaction(SIGCHLD, &sigchld_action, &old_sigchld_action);
148
182
/* The options we understand. */
149
183
struct argp_option options[] = {
150
184
{ .name = "global-options", .key = 'g',
167
201
error_t parse_opt (int key, char *arg, struct argp_state *state) {
168
/* Get the INPUT argument from `argp_parse', which we
169
know is a pointer to our plugin list pointer. */
202
/* Get the INPUT argument from `argp_parse', which we know is a
203
pointer to our plugin list pointer. */
170
204
plugin **plugins = state->input;
318
348
exitstatus = EXIT_FAILURE;
321
strcpy(filename, plugindir);
322
strcat(filename, "/");
323
strcat(filename, dirst->d_name);
351
strcpy(filename, plugindir); /* Spurious warning */
352
strcat(filename, "/"); /* Spurious warning */
353
strcat(filename, dirst->d_name); /* Spurious warning */
325
355
stat(filename, &st);
327
357
if (not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
329
359
fprintf(stderr, "Ignoring plugin dir entry \"%s\""
330
360
" with bad type or mode\n", filename);
334
365
if(getplugin(dirst->d_name, &plugin_list)->disabled){
365
397
exitstatus = EXIT_FAILURE;
400
/* Block SIGCHLD until process is safely in process list */
401
ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
403
perror("sigprocmask");
404
exitstatus = EXIT_FAILURE;
368
407
// Starting a new process to be watched
369
408
pid_t pid = fork();
371
410
/* this is the child process */
411
ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
416
ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
418
perror("sigprocmask");
372
421
dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
374
423
if(dirfd(dir) < 0){
375
424
/* If dir has no file descriptor, we could not set FD_CLOEXEC
376
and must close it manually */
425
above and must now close it manually here. */
379
428
if(execv(filename, p->argv) < 0){
394
448
*new_process = (struct process){ .pid = pid,
396
450
.next = process_list };
452
process_list = new_process;
453
/* Unblock SIGCHLD so signal handler can be run if this process
454
has already completed */
455
ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
457
perror("sigprocmask");
458
exitstatus = EXIT_FAILURE;
397
462
FD_SET(new_process->fd, &rfds_all);
399
464
if (maxfd < new_process->fd){
400
465
maxfd = new_process->fd;
404
process_list = new_process;
407
470
/* Free the plugin list */
425
490
exitstatus = EXIT_FAILURE;
493
/* OK, now either a process completed, or something can be read
428
495
for(process *proc = process_list; proc ; proc = proc->next){
429
if(not FD_ISSET(proc->fd, &rfds)){
496
/* Is this process completely done? */
497
if(proc->eof and proc->completed){
498
/* Only accept the plugin output if it exited cleanly */
499
if(not WIFEXITED(proc->status)
500
or WEXITSTATUS(proc->status) != 0){
501
/* Bad exit by plugin */
503
if(WIFEXITED(proc->status)){
504
fprintf(stderr, "Plugin %d exited with status %d\n",
505
proc->pid, WEXITSTATUS(proc->status));
506
} else if(WIFSIGNALED(proc->status)) {
507
fprintf(stderr, "Plugin %d killed by signal %d\n",
508
proc->pid, WTERMSIG(proc->status));
509
} else if(WCOREDUMP(proc->status)){
510
fprintf(stderr, "Plugin %d dumped core\n", proc->pid);
513
/* Remove the plugin */
514
FD_CLR(proc->fd, &rfds_all);
515
/* Block signal while modifying process_list */
516
ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
518
perror("sigprocmask");
519
exitstatus = EXIT_FAILURE;
522
/* Delete this process entry from the list */
523
if(process_list == proc){
524
/* First one - simple */
525
process_list = proc->next;
527
/* Second one or later */
528
for(process *p = process_list; p != NULL; p = p->next){
530
p->next = proc->next;
535
/* We are done modifying process list, so unblock signal */
536
ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
539
perror("sigprocmask");
543
/* We deleted this process from the list, so we can't go
544
proc->next. Therefore, start over from the beginning of
548
/* This process exited nicely, so print its buffer */
549
for(size_t written = 0; written < proc->buffer_length;
550
written += (size_t)ret){
551
ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO,
552
proc->buffer + written,
557
exitstatus = EXIT_FAILURE;
563
/* This process has not completed. Does it have any output? */
564
if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
565
/* This process had nothing to say at this time */
568
/* Before reading, make the process' data buffer large enough */
432
569
if(proc->buffer_length + BUFFER_SIZE > proc->buffer_size){
433
570
proc->buffer = realloc(proc->buffer, proc->buffer_size
434
571
+ (size_t) BUFFER_SIZE);
440
577
proc->buffer_size += BUFFER_SIZE;
579
/* Read from the process */
442
580
ret = read(proc->fd, proc->buffer + proc->buffer_length,
445
/* Read error from this process; ignore it */
583
/* Read error from this process; ignore the error */
448
proc->buffer_length += (size_t) ret;
451
/* wait for process exit */
453
waitpid(proc->pid, &status, 0);
454
if(not WIFEXITED(status) or WEXITSTATUS(status) != 0){
455
FD_CLR(proc->fd, &rfds_all);
458
for(size_t written = 0;
459
written < proc->buffer_length; written += (size_t)ret){
460
ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO,
461
proc->buffer + written,
466
exitstatus = EXIT_FAILURE;
590
proc->buffer_length += (size_t) ret;
594
if(process_list == NULL){
595
fprintf(stderr, "All plugin processes failed, exiting\n");
596
exitstatus = EXIT_FAILURE;
600
/* Restore old signal handler */
601
sigaction(SIGCHLD, &old_sigchld_action, NULL);
603
/* Free the plugin list */
604
for(plugin *next; plugin_list != NULL; plugin_list = next){
605
next = plugin_list->next;
606
free(plugin_list->argv);
614
/* Free the process list and kill the processes */
476
615
for(process *next; process_list != NULL; process_list = next){
616
next = process_list->next;
477
617
close(process_list->fd);
478
618
kill(process_list->pid, SIGTERM);
479
619
free(process_list->buffer);
480
620
free(process_list);
623
/* Wait for any remaining child processes to terminate */
493
631
return exitstatus;