/mandos/trunk

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/trunk

« back to all changes in this revision

Viewing changes to plugin-runner.c

  • Committer: Teddy Hogeborn
  • Date: 2009-02-09 19:26:19 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090209192619-7rse82x9pypq4ihk
* plugin-runner.c: Comment change.

* plugins.d/mandos-client.c: Comment changes.
  (quit_now): New global flag.
  (handle_sigterm): Only call avahi_simple_poll_quit once.
  (main): Also handle SIGINT and SIGHUP.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*  -*- coding: utf-8; mode: c; mode: orgtbl -*- */
 
1
/*  -*- coding: utf-8 -*- */
2
2
/*
3
3
 * Mandos plugin runner - Run Mandos plugins
4
4
 *
23
23
 */
24
24
 
25
25
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY(), getline(),
26
 
                                   asprintf(), O_CLOEXEC */
 
26
                                   asprintf() */
27
27
#include <stddef.h>             /* size_t, NULL */
28
28
#include <stdlib.h>             /* malloc(), exit(), EXIT_FAILURE,
29
29
                                   EXIT_SUCCESS, realloc() */
30
30
#include <stdbool.h>            /* bool, true, false */
31
31
#include <stdio.h>              /* perror, fileno(), fprintf(),
32
32
                                   stderr, STDOUT_FILENO */
33
 
#include <sys/types.h>          /* DIR, fdopendir(), stat(), struct
 
33
#include <sys/types.h>          /* DIR, opendir(), stat(), struct
34
34
                                   stat, waitpid(), WIFEXITED(),
35
35
                                   WEXITSTATUS(), wait(), pid_t,
36
36
                                   uid_t, gid_t, getuid(), getgid(),
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
 
                                   WCOREDUMP() */
 
41
                                   WEXITSTATUS() */
43
42
#include <sys/stat.h>           /* struct stat, stat(), S_ISREG() */
44
43
#include <iso646.h>             /* and, or, not */
45
 
#include <dirent.h>             /* DIR, struct dirent, fdopendir(),
 
44
#include <dirent.h>             /* DIR, struct dirent, opendir(),
46
45
                                   readdir(), closedir(), dirfd() */
47
46
#include <unistd.h>             /* struct stat, stat(), S_ISREG(),
48
47
                                   fcntl(), setuid(), setgid(),
53
52
                                   close() */
54
53
#include <fcntl.h>              /* fcntl(), F_GETFD, F_SETFD,
55
54
                                   FD_CLOEXEC */
56
 
#include <string.h>             /* strsep, strlen(), asprintf(),
57
 
                                   strsignal() */
 
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,
67
65
                                   SIG_UNBLOCK, kill(), sig_atomic_t
68
66
                                */
69
67
#include <errno.h>              /* errno, EBADF */
70
 
#include <inttypes.h>           /* intmax_t, PRIdMAX, strtoimax() */
71
 
#include <sysexits.h>           /* EX_OSERR, EX_USAGE */
 
68
#include <inttypes.h>           /* intmax_t, SCNdMAX, PRIdMAX,  */
72
69
 
73
70
#define BUFFER_SIZE 256
74
71
 
85
82
  char **environ;
86
83
  int envc;
87
84
  bool disabled;
88
 
  
 
85
 
89
86
  /* Variables used for running processes*/
90
87
  pid_t pid;
91
88
  int fd;
103
100
/* Gets an existing plugin based on name,
104
101
   or if none is found, creates a new one */
105
102
static plugin *getplugin(char *name){
106
 
  /* Check for existing plugin with that name */
 
103
  /* Check for exiting plugin with that name */
107
104
  for(plugin *p = plugin_list; p != NULL; p = p->next){
108
105
    if((p->name == name)
109
106
       or (p->name and name and (strcmp(p->name, name) == 0))){
111
108
    }
112
109
  }
113
110
  /* Create a new plugin */
114
 
  plugin *new_plugin = NULL;
115
 
  do {
116
 
    new_plugin = malloc(sizeof(plugin));
117
 
  } while(new_plugin == NULL and errno == EINTR);
 
111
  plugin *new_plugin = malloc(sizeof(plugin));
118
112
  if(new_plugin == NULL){
119
113
    return NULL;
120
114
  }
121
115
  char *copy_name = NULL;
122
116
  if(name != NULL){
123
 
    do {
124
 
      copy_name = strdup(name);
125
 
    } while(copy_name == NULL and errno == EINTR);
 
117
    copy_name = strdup(name);
126
118
    if(copy_name == NULL){
127
 
      int e = errno;
128
119
      free(new_plugin);
129
 
      errno = e;
130
120
      return NULL;
131
121
    }
132
122
  }
136
126
                          .disabled = false,
137
127
                          .next = plugin_list };
138
128
  
139
 
  do {
140
 
    new_plugin->argv = malloc(sizeof(char *) * 2);
141
 
  } while(new_plugin->argv == NULL and errno == EINTR);
 
129
  new_plugin->argv = malloc(sizeof(char *) * 2);
142
130
  if(new_plugin->argv == NULL){
143
 
    int e = errno;
144
131
    free(copy_name);
145
132
    free(new_plugin);
146
 
    errno = e;
147
133
    return NULL;
148
134
  }
149
135
  new_plugin->argv[0] = copy_name;
150
136
  new_plugin->argv[1] = NULL;
151
137
  
152
 
  do {
153
 
    new_plugin->environ = malloc(sizeof(char *));
154
 
  } while(new_plugin->environ == NULL and errno == EINTR);
 
138
  new_plugin->environ = malloc(sizeof(char *));
155
139
  if(new_plugin->environ == NULL){
156
 
    int e = errno;
157
140
    free(copy_name);
158
141
    free(new_plugin->argv);
159
142
    free(new_plugin);
160
 
    errno = e;
161
143
    return NULL;
162
144
  }
163
145
  new_plugin->environ[0] = NULL;
171
153
static bool add_to_char_array(const char *new, char ***array,
172
154
                              int *len){
173
155
  /* Resize the pointed-to array to hold one more pointer */
174
 
  do {
175
 
    *array = realloc(*array, sizeof(char *)
176
 
                     * (size_t) ((*len) + 2));
177
 
  } while(*array == NULL and errno == EINTR);
 
156
  *array = realloc(*array, sizeof(char *)
 
157
                   * (size_t) ((*len) + 2));
178
158
  /* Malloc check */
179
159
  if(*array == NULL){
180
160
    return false;
181
161
  }
182
162
  /* Make a copy of the new string */
183
 
  char *copy;
184
 
  do {
185
 
    copy = strdup(new);
186
 
  } while(copy == NULL and errno == EINTR);
 
163
  char *copy = strdup(new);
187
164
  if(copy == NULL){
188
165
    return false;
189
166
  }
215
192
    if(strncmp(*e, def, namelen + 1) == 0){
216
193
      /* It already exists */
217
194
      if(replace){
218
 
        char *new;
219
 
        do {
220
 
          new = realloc(*e, strlen(def) + 1);
221
 
        } while(new == NULL and errno == EINTR);
 
195
        char *new = realloc(*e, strlen(def) + 1);
222
196
        if(new == NULL){
223
197
          return false;
224
198
        }
234
208
/*
235
209
 * Based on the example in the GNU LibC manual chapter 13.13 "File
236
210
 * Descriptor Flags".
237
 
 | [[info:libc:Descriptor%20Flags][File Descriptor Flags]] |
 
211
 * *Note File Descriptor Flags:(libc)Descriptor Flags.
238
212
 */
239
213
static int set_cloexec_flag(int fd){
240
 
  int ret = (int)TEMP_FAILURE_RETRY(fcntl(fd, F_GETFD, 0));
 
214
  int ret = fcntl(fd, F_GETFD, 0);
241
215
  /* If reading the flags failed, return error indication now. */
242
216
  if(ret < 0){
243
217
    return ret;
244
218
  }
245
219
  /* Store modified flag word in the descriptor. */
246
 
  return (int)TEMP_FAILURE_RETRY(fcntl(fd, F_SETFD,
247
 
                                       ret | FD_CLOEXEC));
 
220
  return fcntl(fd, F_SETFD, ret | FD_CLOEXEC);
248
221
}
249
222
 
250
223
 
307
280
  }
308
281
  free(plugin_node->environ);
309
282
  free(plugin_node->buffer);
310
 
  
 
283
 
311
284
  /* Removes the plugin from the singly-linked list */
312
285
  if(plugin_node == plugin_list){
313
286
    /* First one - simple */
340
313
  struct dirent *dirst;
341
314
  struct stat st;
342
315
  fd_set rfds_all;
343
 
  int ret, maxfd = 0;
 
316
  int ret, numchars, maxfd = 0;
344
317
  ssize_t sret;
 
318
  intmax_t tmpmax;
345
319
  uid_t uid = 65534;
346
320
  gid_t gid = 65534;
347
321
  bool debug = false;
401
375
      .doc = "Group ID the plugins will run as", .group = 3 },
402
376
    { .name = "debug", .key = 132,
403
377
      .doc = "Debug mode", .group = 4 },
404
 
    /*
405
 
     * These reproduce what we would get without ARGP_NO_HELP
406
 
     */
407
 
    { .name = "help", .key = '?',
408
 
      .doc = "Give this help list", .group = -1 },
409
 
    { .name = "usage", .key = -3,
410
 
      .doc = "Give a short usage message", .group = -1 },
411
 
    { .name = "version", .key = 'V',
412
 
      .doc = "Print program version", .group = -1 },
413
378
    { .name = NULL }
414
379
  };
415
380
  
416
 
  error_t parse_opt(int key, char *arg, struct argp_state *state){
417
 
    errno = 0;
 
381
  error_t parse_opt(int key, char *arg, __attribute__((unused))
 
382
                    struct argp_state *state){
418
383
    switch(key){
419
 
      char *tmp;
420
 
      intmax_t tmpmax;
421
384
    case 'g':                   /* --global-options */
422
 
      {
 
385
      if(arg != NULL){
423
386
        char *plugin_option;
424
387
        while((plugin_option = strsep(&arg, ",")) != NULL){
 
388
          if(plugin_option[0] == '\0'){
 
389
            continue;
 
390
          }
425
391
          if(not add_argument(getplugin(NULL), plugin_option)){
426
 
            break;
 
392
            perror("add_argument");
 
393
            return ARGP_ERR_UNKNOWN;
427
394
          }
428
395
        }
429
396
      }
430
397
      break;
431
398
    case 'G':                   /* --global-env */
432
 
      add_environment(getplugin(NULL), arg, true);
 
399
      if(arg == NULL){
 
400
        break;
 
401
      }
 
402
      if(not add_environment(getplugin(NULL), arg, true)){
 
403
        perror("add_environment");
 
404
      }
433
405
      break;
434
406
    case 'o':                   /* --options-for */
435
 
      {
436
 
        char *option_list = strchr(arg, ':');
437
 
        if(option_list == NULL){
438
 
          argp_error(state, "No colon in \"%s\"", arg);
439
 
          errno = EINVAL;
440
 
          break;
441
 
        }
442
 
        *option_list = '\0';
443
 
        option_list++;
444
 
        if(arg[0] == '\0'){
445
 
          argp_error(state, "Empty plugin name");
446
 
          errno = EINVAL;
447
 
          break;
448
 
        }
449
 
        char *option;
450
 
        while((option = strsep(&option_list, ",")) != NULL){
451
 
          if(not add_argument(getplugin(arg), option)){
452
 
            break;
 
407
      if(arg != NULL){
 
408
        char *plugin_name = strsep(&arg, ":");
 
409
        if(plugin_name[0] == '\0'){
 
410
          break;
 
411
        }
 
412
        char *plugin_option;
 
413
        while((plugin_option = strsep(&arg, ",")) != NULL){
 
414
          if(not add_argument(getplugin(plugin_name), plugin_option)){
 
415
            perror("add_argument");
 
416
            return ARGP_ERR_UNKNOWN;
453
417
          }
454
418
        }
455
419
      }
456
420
      break;
457
421
    case 'E':                   /* --env-for */
 
422
      if(arg == NULL){
 
423
        break;
 
424
      }
458
425
      {
459
426
        char *envdef = strchr(arg, ':');
460
427
        if(envdef == NULL){
461
 
          argp_error(state, "No colon in \"%s\"", arg);
462
 
          errno = EINVAL;
463
428
          break;
464
429
        }
465
430
        *envdef = '\0';
466
 
        envdef++;
467
 
        if(arg[0] == '\0'){
468
 
          argp_error(state, "Empty plugin name");
469
 
          errno = EINVAL;
470
 
          break;
 
431
        if(not add_environment(getplugin(arg), envdef+1, true)){
 
432
          perror("add_environment");
471
433
        }
472
 
        add_environment(getplugin(arg), envdef, true);
473
434
      }
474
435
      break;
475
436
    case 'd':                   /* --disable */
476
 
      {
 
437
      if(arg != NULL){
477
438
        plugin *p = getplugin(arg);
478
 
        if(p != NULL){
479
 
          p->disabled = true;
 
439
        if(p == NULL){
 
440
          return ARGP_ERR_UNKNOWN;
480
441
        }
 
442
        p->disabled = true;
481
443
      }
482
444
      break;
483
445
    case 'e':                   /* --enable */
484
 
      {
 
446
      if(arg != NULL){
485
447
        plugin *p = getplugin(arg);
486
 
        if(p != NULL){
487
 
          p->disabled = false;
 
448
        if(p == NULL){
 
449
          return ARGP_ERR_UNKNOWN;
488
450
        }
 
451
        p->disabled = false;
489
452
      }
490
453
      break;
491
454
    case 128:                   /* --plugin-dir */
492
455
      free(plugindir);
493
456
      plugindir = strdup(arg);
 
457
      if(plugindir == NULL){
 
458
        perror("strdup");
 
459
      }      
494
460
      break;
495
461
    case 129:                   /* --config-file */
496
462
      /* This is already done by parse_opt_config_file() */
497
463
      break;
498
464
    case 130:                   /* --userid */
499
 
      tmpmax = strtoimax(arg, &tmp, 10);
500
 
      if(errno != 0 or tmp == arg or *tmp != '\0'
501
 
         or tmpmax != (uid_t)tmpmax){
502
 
        argp_error(state, "Bad user ID number: \"%s\", using %"
503
 
                   PRIdMAX, arg, (intmax_t)uid);
504
 
        break;
 
465
      ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
 
466
      if(ret < 1 or tmpmax != (uid_t)tmpmax
 
467
         or arg[numchars] != '\0'){
 
468
        fprintf(stderr, "Bad user ID number: \"%s\", using %"
 
469
                PRIdMAX "\n", arg, (intmax_t)uid);
 
470
      } else {
 
471
        uid = (uid_t)tmpmax;
505
472
      }
506
 
      uid = (uid_t)tmpmax;
507
473
      break;
508
474
    case 131:                   /* --groupid */
509
 
      tmpmax = strtoimax(arg, &tmp, 10);
510
 
      if(errno != 0 or tmp == arg or *tmp != '\0'
511
 
         or tmpmax != (gid_t)tmpmax){
512
 
        argp_error(state, "Bad group ID number: \"%s\", using %"
513
 
                   PRIdMAX, arg, (intmax_t)gid);
514
 
        break;
 
475
      ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
 
476
      if(ret < 1 or tmpmax != (gid_t)tmpmax
 
477
         or arg[numchars] != '\0'){
 
478
        fprintf(stderr, "Bad group ID number: \"%s\", using %"
 
479
                PRIdMAX "\n", arg, (intmax_t)gid);
 
480
      } else {
 
481
        gid = (gid_t)tmpmax;
515
482
      }
516
 
      gid = (gid_t)tmpmax;
517
483
      break;
518
484
    case 132:                   /* --debug */
519
485
      debug = true;
520
486
      break;
521
 
      /*
522
 
       * These reproduce what we would get without ARGP_NO_HELP
523
 
       */
524
 
    case '?':                   /* --help */
525
 
      state->flags &= ~(unsigned int)ARGP_NO_EXIT; /* force exit */
526
 
      argp_state_help(state, state->out_stream, ARGP_HELP_STD_HELP);
527
 
    case -3:                    /* --usage */
528
 
      state->flags &= ~(unsigned int)ARGP_NO_EXIT; /* force exit */
529
 
      argp_state_help(state, state->out_stream,
530
 
                      ARGP_HELP_USAGE | ARGP_HELP_EXIT_OK);
531
 
    case 'V':                   /* --version */
532
 
      fprintf(state->out_stream, "%s\n", argp_program_version);
533
 
      exit(EXIT_SUCCESS);
534
 
      break;
535
487
/*
536
488
 * When adding more options before this line, remember to also add a
537
489
 * "case" to the "parse_opt_config_file" function below.
540
492
      /* Cryptsetup always passes an argument, which is an empty
541
493
         string if "none" was specified in /etc/crypttab.  So if
542
494
         argument was empty, we ignore it silently. */
543
 
      if(arg[0] == '\0'){
544
 
        break;
 
495
      if(arg[0] != '\0'){
 
496
        fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
545
497
      }
 
498
      break;
 
499
    case ARGP_KEY_END:
 
500
      break;
546
501
    default:
547
502
      return ARGP_ERR_UNKNOWN;
548
503
    }
549
 
    return errno;               /* Set to 0 at start */
 
504
    return 0;
550
505
  }
551
506
  
552
507
  /* This option parser is the same as parse_opt() above, except it
554
509
  error_t parse_opt_config_file(int key, char *arg,
555
510
                                __attribute__((unused))
556
511
                                struct argp_state *state){
557
 
    errno = 0;
558
512
    switch(key){
559
513
    case 'g':                   /* --global-options */
560
514
    case 'G':                   /* --global-env */
567
521
    case 129:                   /* --config-file */
568
522
      free(argfile);
569
523
      argfile = strdup(arg);
570
 
      break;
 
524
      if(argfile == NULL){
 
525
        perror("strdup");
 
526
      }
 
527
      break;      
571
528
    case 130:                   /* --userid */
572
529
    case 131:                   /* --groupid */
573
530
    case 132:                   /* --debug */
574
 
    case '?':                   /* --help */
575
 
    case -3:                    /* --usage */
576
 
    case 'V':                   /* --version */
577
531
    case ARGP_KEY_ARG:
 
532
    case ARGP_KEY_END:
578
533
      break;
579
534
    default:
580
535
      return ARGP_ERR_UNKNOWN;
581
536
    }
582
 
    return errno;
 
537
    return 0;
583
538
  }
584
539
  
585
540
  struct argp argp = { .options = options,
589
544
  
590
545
  /* Parse using parse_opt_config_file() in order to get the custom
591
546
     config file location, if any. */
592
 
  ret = argp_parse(&argp, argc, argv,
593
 
                   ARGP_IN_ORDER | ARGP_NO_EXIT | ARGP_NO_HELP,
594
 
                   NULL, NULL);
595
 
  switch(ret){
596
 
  case 0:
597
 
    break;
598
 
  case ENOMEM:
599
 
  default:
600
 
    errno = ret;
601
 
    perror("argp_parse");
602
 
    exitstatus = EX_OSERR;
603
 
    goto fallback;
604
 
  case EINVAL:
605
 
    exitstatus = EX_USAGE;
 
547
  ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
 
548
  if(ret == ARGP_ERR_UNKNOWN){
 
549
    fprintf(stderr, "Unknown error while parsing arguments\n");
 
550
    exitstatus = EXIT_FAILURE;
606
551
    goto fallback;
607
552
  }
608
553
  
614
559
    conffp = fopen(AFILE, "r");
615
560
  } else {
616
561
    conffp = fopen(argfile, "r");
617
 
  }
 
562
  }  
618
563
  if(conffp != NULL){
619
564
    char *org_line = NULL;
620
565
    char *p, *arg, *new_arg, *line;
621
566
    size_t size = 0;
622
567
    const char whitespace_delims[] = " \r\t\f\v\n";
623
568
    const char comment_delim[] = "#";
624
 
    
 
569
 
625
570
    custom_argc = 1;
626
571
    custom_argv = malloc(sizeof(char*) * 2);
627
572
    if(custom_argv == NULL){
631
576
    }
632
577
    custom_argv[0] = argv[0];
633
578
    custom_argv[1] = NULL;
634
 
    
 
579
 
635
580
    /* for each line in the config file, strip whitespace and ignore
636
581
       commented text */
637
582
    while(true){
639
584
      if(sret == -1){
640
585
        break;
641
586
      }
642
 
      
 
587
 
643
588
      line = org_line;
644
589
      arg = strsep(&line, comment_delim);
645
590
      while((p = strsep(&arg, whitespace_delims)) != NULL){
664
609
          goto fallback;
665
610
        }
666
611
        custom_argv[custom_argc-1] = new_arg;
667
 
        custom_argv[custom_argc] = NULL;
 
612
        custom_argv[custom_argc] = NULL;        
668
613
      }
669
614
    }
670
 
    do {
671
 
      ret = fclose(conffp);
672
 
    } while(ret == EOF and errno == EINTR);
673
 
    if(ret == EOF){
674
 
      perror("fclose");
675
 
      exitstatus = EXIT_FAILURE;
676
 
      goto fallback;
677
 
    }
678
615
    free(org_line);
679
616
  } else {
680
617
    /* Check for harmful errors and go to fallback. Other errors might
685
622
      goto fallback;
686
623
    }
687
624
  }
688
 
  /* If there were any arguments from the configuration file, pass
689
 
     them to parser as command line arguments */
 
625
  /* If there was any arguments from configuration file,
 
626
     pass them to parser as command arguments */
690
627
  if(custom_argv != NULL){
691
 
    ret = argp_parse(&argp, custom_argc, custom_argv,
692
 
                     ARGP_IN_ORDER | ARGP_NO_EXIT | ARGP_NO_HELP,
693
 
                     NULL, NULL);
694
 
    switch(ret){
695
 
    case 0:
696
 
      break;
697
 
    case ENOMEM:
698
 
    default:
699
 
      errno = ret;
700
 
      perror("argp_parse");
701
 
      exitstatus = EX_OSERR;
702
 
      goto fallback;
703
 
    case EINVAL:
704
 
      exitstatus = EX_CONFIG;
 
628
    ret = argp_parse(&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
 
629
                     0, NULL);
 
630
    if(ret == ARGP_ERR_UNKNOWN){
 
631
      fprintf(stderr, "Unknown error while parsing arguments\n");
 
632
      exitstatus = EXIT_FAILURE;
705
633
      goto fallback;
706
634
    }
707
635
  }
708
636
  
709
637
  /* Parse actual command line arguments, to let them override the
710
638
     config file */
711
 
  ret = argp_parse(&argp, argc, argv,
712
 
                   ARGP_IN_ORDER | ARGP_NO_EXIT | ARGP_NO_HELP,
713
 
                   NULL, NULL);
714
 
  switch(ret){
715
 
  case 0:
716
 
    break;
717
 
  case ENOMEM:
718
 
  default:
719
 
    errno = ret;
720
 
    perror("argp_parse");
721
 
    exitstatus = EX_OSERR;
722
 
    goto fallback;
723
 
  case EINVAL:
724
 
    exitstatus = EX_USAGE;
 
639
  ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
 
640
  if(ret == ARGP_ERR_UNKNOWN){
 
641
    fprintf(stderr, "Unknown error while parsing arguments\n");
 
642
    exitstatus = EXIT_FAILURE;
725
643
    goto fallback;
726
644
  }
727
645
  
749
667
    perror("setuid");
750
668
  }
751
669
  
752
 
  /* Open plugin directory with close_on_exec flag */
 
670
  if(plugindir == NULL){
 
671
    dir = opendir(PDIR);
 
672
  } else {
 
673
    dir = opendir(plugindir);
 
674
  }
 
675
  
 
676
  if(dir == NULL){
 
677
    perror("Could not open plugin dir");
 
678
    exitstatus = EXIT_FAILURE;
 
679
    goto fallback;
 
680
  }
 
681
  
 
682
  /* Set the FD_CLOEXEC flag on the directory, if possible */
753
683
  {
754
 
    int dir_fd = -1;
755
 
    if(plugindir == NULL){
756
 
      dir_fd = open(PDIR, O_RDONLY |
757
 
#ifdef O_CLOEXEC
758
 
                    O_CLOEXEC
759
 
#else  /* not O_CLOEXEC */
760
 
                    0
761
 
#endif  /* not O_CLOEXEC */
762
 
                    );
763
 
    } else {
764
 
      dir_fd = open(plugindir, O_RDONLY |
765
 
#ifdef O_CLOEXEC
766
 
                    O_CLOEXEC
767
 
#else  /* not O_CLOEXEC */
768
 
                    0
769
 
#endif  /* not O_CLOEXEC */
770
 
                    );
771
 
    }
772
 
    if(dir_fd == -1){
773
 
      perror("Could not open plugin dir");
774
 
      exitstatus = EXIT_FAILURE;
775
 
      goto fallback;
776
 
    }
777
 
    
778
 
#ifndef O_CLOEXEC
779
 
  /* Set the FD_CLOEXEC flag on the directory */
780
 
    ret = set_cloexec_flag(dir_fd);
781
 
    if(ret < 0){
782
 
      perror("set_cloexec_flag");
783
 
      TEMP_FAILURE_RETRY(close(dir_fd));
784
 
      exitstatus = EXIT_FAILURE;
785
 
      goto fallback;
786
 
    }
787
 
#endif  /* O_CLOEXEC */
788
 
    
789
 
    dir = fdopendir(dir_fd);
790
 
    if(dir == NULL){
791
 
      perror("Could not open plugin dir");
792
 
      TEMP_FAILURE_RETRY(close(dir_fd));
793
 
      exitstatus = EXIT_FAILURE;
794
 
      goto fallback;
 
684
    int dir_fd = dirfd(dir);
 
685
    if(dir_fd >= 0){
 
686
      ret = set_cloexec_flag(dir_fd);
 
687
      if(ret < 0){
 
688
        perror("set_cloexec_flag");
 
689
        exitstatus = EXIT_FAILURE;
 
690
        goto fallback;
 
691
      }
795
692
    }
796
693
  }
797
694
  
799
696
  
800
697
  /* Read and execute any executable in the plugin directory*/
801
698
  while(true){
802
 
    do {
803
 
      dirst = readdir(dir);
804
 
    } while(dirst == NULL and errno == EINTR);
 
699
    dirst = readdir(dir);
805
700
    
806
701
    /* All directory entries have been processed */
807
702
    if(dirst == NULL){
858
753
        continue;
859
754
      }
860
755
    }
861
 
    
 
756
 
862
757
    char *filename;
863
758
    if(plugindir == NULL){
864
 
      ret = (int)TEMP_FAILURE_RETRY(asprintf(&filename, PDIR "/%s",
865
 
                                             dirst->d_name));
 
759
      ret = asprintf(&filename, PDIR "/%s", dirst->d_name);
866
760
    } else {
867
 
      ret = (int)TEMP_FAILURE_RETRY(asprintf(&filename, "%s/%s",
868
 
                                             plugindir,
869
 
                                             dirst->d_name));
 
761
      ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
870
762
    }
871
763
    if(ret < 0){
872
764
      perror("asprintf");
873
765
      continue;
874
766
    }
875
767
    
876
 
    ret = (int)TEMP_FAILURE_RETRY(stat(filename, &st));
 
768
    ret = stat(filename, &st);
877
769
    if(ret == -1){
878
770
      perror("stat");
879
771
      free(filename);
880
772
      continue;
881
773
    }
882
 
    
 
774
 
883
775
    /* Ignore non-executable files */
884
 
    if(not S_ISREG(st.st_mode)
885
 
       or (TEMP_FAILURE_RETRY(access(filename, X_OK)) != 0)){
 
776
    if(not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
886
777
      if(debug){
887
778
        fprintf(stderr, "Ignoring plugin dir entry \"%s\""
888
779
                " with bad type or mode\n", filename);
934
825
    }
935
826
    
936
827
    int pipefd[2];
937
 
    ret = (int)TEMP_FAILURE_RETRY(pipe(pipefd));
 
828
    ret = pipe(pipefd);
938
829
    if(ret == -1){
939
830
      perror("pipe");
940
831
      exitstatus = EXIT_FAILURE;
954
845
      goto fallback;
955
846
    }
956
847
    /* Block SIGCHLD until process is safely in process list */
957
 
    ret = (int)TEMP_FAILURE_RETRY(sigprocmask(SIG_BLOCK,
958
 
                                              &sigchld_action.sa_mask,
959
 
                                              NULL));
 
848
    ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
960
849
    if(ret < 0){
961
850
      perror("sigprocmask");
962
851
      exitstatus = EXIT_FAILURE;
963
852
      goto fallback;
964
853
    }
965
854
    /* Starting a new process to be watched */
966
 
    pid_t pid;
967
 
    do {
968
 
      pid = fork();
969
 
    } while(pid == -1 and errno == EINTR);
 
855
    pid_t pid = fork();
970
856
    if(pid == -1){
971
857
      perror("fork");
972
858
      exitstatus = EXIT_FAILURE;
1010
896
      /* no return */
1011
897
    }
1012
898
    /* Parent process */
1013
 
    TEMP_FAILURE_RETRY(close(pipefd[1])); /* Close unused write end of
1014
 
                                             pipe */
 
899
    close(pipefd[1]);           /* Close unused write end of pipe */
1015
900
    free(filename);
1016
901
    plugin *new_plugin = getplugin(dirst->d_name);
1017
902
    if(new_plugin == NULL){
1018
903
      perror("getplugin");
1019
 
      ret = (int)(TEMP_FAILURE_RETRY
1020
 
                  (sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
1021
 
                               NULL)));
 
904
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
1022
905
      if(ret < 0){
1023
906
        perror("sigprocmask");
1024
907
      }
1031
914
    
1032
915
    /* Unblock SIGCHLD so signal handler can be run if this process
1033
916
       has already completed */
1034
 
    ret = (int)TEMP_FAILURE_RETRY(sigprocmask(SIG_UNBLOCK,
1035
 
                                              &sigchld_action.sa_mask,
1036
 
                                              NULL));
 
917
    ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
1037
918
    if(ret < 0){
1038
919
      perror("sigprocmask");
1039
920
      exitstatus = EXIT_FAILURE;
1040
921
      goto fallback;
1041
922
    }
1042
923
    
1043
 
    FD_SET(new_plugin->fd, &rfds_all); /* Spurious warning from
1044
 
                                          -Wconversion */
 
924
    FD_SET(new_plugin->fd, &rfds_all);
1045
925
    
1046
926
    if(maxfd < new_plugin->fd){
1047
927
      maxfd = new_plugin->fd;
1048
928
    }
1049
929
  }
1050
930
  
1051
 
  TEMP_FAILURE_RETRY(closedir(dir));
 
931
  closedir(dir);
1052
932
  dir = NULL;
1053
933
  free_plugin(getplugin(NULL));
1054
934
  
1067
947
  while(plugin_list){
1068
948
    fd_set rfds = rfds_all;
1069
949
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
1070
 
    if(select_ret == -1 and errno != EINTR){
 
950
    if(select_ret == -1){
1071
951
      perror("select");
1072
952
      exitstatus = EXIT_FAILURE;
1073
953
      goto fallback;
1081
961
        if(not WIFEXITED(proc->status)
1082
962
           or WEXITSTATUS(proc->status) != 0){
1083
963
          /* Bad exit by plugin */
1084
 
          
 
964
 
1085
965
          if(debug){
1086
966
            if(WIFEXITED(proc->status)){
1087
967
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] exited with"
1090
970
                      WEXITSTATUS(proc->status));
1091
971
            } else if(WIFSIGNALED(proc->status)){
1092
972
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] killed by"
1093
 
                      " signal %d: %s\n", proc->name,
 
973
                      " signal %d\n", proc->name,
1094
974
                      (intmax_t) (proc->pid),
1095
 
                      WTERMSIG(proc->status),
1096
 
                      strsignal(WTERMSIG(proc->status)));
 
975
                      WTERMSIG(proc->status));
1097
976
            } else if(WCOREDUMP(proc->status)){
1098
977
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] dumped"
1099
978
                      " core\n", proc->name, (intmax_t) (proc->pid));
1101
980
          }
1102
981
          
1103
982
          /* Remove the plugin */
1104
 
          FD_CLR(proc->fd, &rfds_all); /* Spurious warning from
1105
 
                                          -Wconversion */
1106
 
          
 
983
          FD_CLR(proc->fd, &rfds_all);
 
984
 
1107
985
          /* Block signal while modifying process_list */
1108
 
          ret = (int)TEMP_FAILURE_RETRY(sigprocmask
1109
 
                                        (SIG_BLOCK,
1110
 
                                         &sigchld_action.sa_mask,
1111
 
                                         NULL));
 
986
          ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
1112
987
          if(ret < 0){
1113
988
            perror("sigprocmask");
1114
989
            exitstatus = EXIT_FAILURE;
1120
995
          proc = next_plugin;
1121
996
          
1122
997
          /* We are done modifying process list, so unblock signal */
1123
 
          ret = (int)(TEMP_FAILURE_RETRY
1124
 
                      (sigprocmask(SIG_UNBLOCK,
1125
 
                                   &sigchld_action.sa_mask, NULL)));
 
998
          ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
 
999
                            NULL);
1126
1000
          if(ret < 0){
1127
1001
            perror("sigprocmask");
1128
1002
            exitstatus = EXIT_FAILURE;
1148
1022
      }
1149
1023
      
1150
1024
      /* This process has not completed.  Does it have any output? */
1151
 
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){ /* Spurious
1152
 
                                                         warning from
1153
 
                                                         -Wconversion */
 
1025
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
1154
1026
        /* This process had nothing to say at this time */
1155
1027
        proc = proc->next;
1156
1028
        continue;
1167
1039
        proc->buffer_size += BUFFER_SIZE;
1168
1040
      }
1169
1041
      /* Read from the process */
1170
 
      sret = TEMP_FAILURE_RETRY(read(proc->fd,
1171
 
                                     proc->buffer
1172
 
                                     + proc->buffer_length,
1173
 
                                     BUFFER_SIZE));
 
1042
      sret = read(proc->fd, proc->buffer + proc->buffer_length,
 
1043
                  BUFFER_SIZE);
1174
1044
      if(sret < 0){
1175
1045
        /* Read error from this process; ignore the error */
1176
1046
        proc = proc->next;
1184
1054
      }
1185
1055
    }
1186
1056
  }
1187
 
  
1188
 
  
 
1057
 
 
1058
 
1189
1059
 fallback:
1190
1060
  
1191
1061
  if(plugin_list == NULL or exitstatus != EXIT_SUCCESS){
1238
1108
  }
1239
1109
  
1240
1110
  /* Wait for any remaining child processes to terminate */
1241
 
  do {
 
1111
  do{
1242
1112
    ret = wait(NULL);
1243
1113
  } while(ret >= 0);
1244
1114
  if(errno != ECHILD){