128
122
return fcntl(fd, F_SETFD, ret | FD_CLOEXEC);
131
126
#define BUFFER_SIZE 256
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;
128
const char *argp_program_version =
129
"plugbasedclient 0.9";
130
const char *argp_program_bug_address =
131
"<mandos@fukt.bsnet.se>";
155
133
int main(int argc, char *argv[]){
156
134
const char *plugindir = "/conf/conf.d/mandos/plugins.d";
157
135
size_t d_name_len;
159
137
struct dirent *dirst;
162
140
int ret, maxfd = 0;
141
process *process_list = NULL;
163
142
bool debug = false;
164
143
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);
182
145
/* The options we understand. */
183
146
struct argp_option options[] = {
184
147
{ .name = "global-options", .key = 'g',
378
339
addargument(p, *a);
385
exitstatus = EXIT_FAILURE;
388
ret = set_cloexec_flag(pipefd[0]);
390
perror("set_cloexec_flag");
391
exitstatus = EXIT_FAILURE;
394
ret = set_cloexec_flag(pipefd[1]);
396
perror("set_cloexec_flag");
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;
407
// Starting a new process to be watched
408
342
pid_t pid = fork();
410
344
/* 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");
346
close(pipefd[0]); /* close unused read end of pipe */
421
347
dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
424
/* If dir has no file descriptor, we could not set FD_CLOEXEC
425
above and must now close it manually here. */
428
349
if(execv(filename, p->argv) < 0){
430
352
_exit(EXIT_FAILURE);
436
356
close(pipefd[1]); /* close unused write end of pipe */
437
357
process *new_process = malloc(sizeof(process));
438
358
if (new_process == NULL){
439
359
perror("malloc");
440
ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
442
perror("sigprocmask");
444
exitstatus = EXIT_FAILURE;
448
*new_process = (struct process){ .pid = pid,
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;
360
exitstatus = EXIT_FAILURE;
364
new_process->fd = pipefd[0];
365
new_process->buffer = malloc(BUFFER_SIZE);
366
if (new_process->buffer == NULL){
368
exitstatus = EXIT_FAILURE;
371
new_process->buffer_size = BUFFER_SIZE;
372
new_process->buffer_length = 0;
462
373
FD_SET(new_process->fd, &rfds_all);
464
375
if (maxfd < new_process->fd){
465
376
maxfd = new_process->fd;
470
/* Free the plugin list */
471
for(plugin *next; plugin_list != NULL; plugin_list = next){
472
next = plugin_list->next;
473
free(plugin_list->argv);
380
new_process->next = process_list;
381
process_list = new_process;
480
if (process_list == NULL){
481
fprintf(stderr, "No plugin processes started, exiting\n");
482
exitstatus = EXIT_FAILURE;
486
fd_set rfds = rfds_all;
487
int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
488
if (select_ret == -1){
490
exitstatus = EXIT_FAILURE;
493
/* OK, now either a process completed, or something can be read
495
for(process *proc = process_list; proc ; proc = proc->next){
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;
386
if (process_list != NULL){
388
fd_set rfds = rfds_all;
389
int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
390
if (select_ret == -1){
394
for(process *process_itr = process_list; process_itr != NULL;
395
process_itr = process_itr->next){
396
if(FD_ISSET(process_itr->fd, &rfds)){
397
if(process_itr->buffer_length + BUFFER_SIZE
398
> process_itr->buffer_size){
399
process_itr->buffer = realloc(process_itr->buffer,
400
process_itr->buffer_size
401
+ (size_t) BUFFER_SIZE);
402
if (process_itr->buffer == NULL){
406
process_itr->buffer_size += BUFFER_SIZE;
408
ret = read(process_itr->fd, process_itr->buffer
409
+ process_itr->buffer_length, BUFFER_SIZE);
411
/* Read error from this process; ignore it */
414
process_itr->buffer_length += (size_t) ret;
417
/* wait for process exit */
419
waitpid(process_itr->pid, &status, 0);
420
if(WIFEXITED(status) and WEXITSTATUS(status) == 0){
421
for(size_t written = 0;
422
written < process_itr->buffer_length;){
423
ret = write(STDOUT_FILENO,
424
process_itr->buffer + written,
425
process_itr->buffer_length - written);
430
written += (size_t)ret;
434
FD_CLR(process_itr->fd, &rfds_all);
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 */
569
if(proc->buffer_length + BUFFER_SIZE > proc->buffer_size){
570
proc->buffer = realloc(proc->buffer, proc->buffer_size
571
+ (size_t) BUFFER_SIZE);
572
if (proc->buffer == NULL){
574
exitstatus = EXIT_FAILURE;
577
proc->buffer_size += BUFFER_SIZE;
579
/* Read from the process */
580
ret = read(proc->fd, proc->buffer + proc->buffer_length,
583
/* Read error from this process; ignore the error */
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 */
615
for(process *next; process_list != NULL; process_list = next){
616
next = process_list->next;
617
close(process_list->fd);
618
kill(process_list->pid, SIGTERM);
619
free(process_list->buffer);
623
/* Wait for any remaining child processes to terminate */
444
for(process *process_itr = process_list; process_itr != NULL;
445
process_itr = process_itr->next){
446
close(process_itr->fd);
447
kill(process_itr->pid, SIGTERM);
448
free(process_itr->buffer);
631
461
return exitstatus;