/mandos/release

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

« back to all changes in this revision

Viewing changes to plugin-runner.c

Support not using IPv6 in server:

* mandos (AvahiService.__init__): Take new "protocol" parameter.  All
                                  callers changed.
  (IPv6_TCPServer.__init__): Take new "use_ipv6" parameter.  All
                             callers changed.
  (IPv6_TCPServer.server_bind): Create IPv4 socket if not using IPv6.
  (main): New "--no-ipv6" command line option.  New "use_ipv6" config
          option.
* mandos-options.xml ([@id="address"]): Document conditional IPv4
                                        address support.
  ([@id="ipv6"]): New paragraph.
* mandos.conf (use_ipv6): New config option.
* mandos.conf.xml (OPTIONS): Document new "use_dbus" option.
  (EXAMPLE): Changed to use IPv6 link-local address.  Added "use_ipv6"
             option.
* mandos.xml (SYNOPSIS): New "--no-ipv6" option.
  (OPTIONS): Document new "--no-ipv6" option.

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,
68
66
                                */
69
67
#include <errno.h>              /* errno, EBADF */
70
68
#include <inttypes.h>           /* intmax_t, PRIdMAX, strtoimax() */
71
 
#include <sysexits.h>           /* EX_OSERR, EX_USAGE */
72
69
 
73
70
#define BUFFER_SIZE 256
74
71
 
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
 
342
315
  fd_set rfds_all;
343
316
  int ret, 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){
 
383
    char *tmp;
418
384
    switch(key){
419
 
      char *tmp;
420
 
      intmax_t tmpmax;
421
385
    case 'g':                   /* --global-options */
422
 
      {
 
386
      if(arg != NULL){
423
387
        char *plugin_option;
424
388
        while((plugin_option = strsep(&arg, ",")) != NULL){
 
389
          if(plugin_option[0] == '\0'){
 
390
            continue;
 
391
          }
425
392
          if(not add_argument(getplugin(NULL), plugin_option)){
426
 
            break;
 
393
            perror("add_argument");
 
394
            return ARGP_ERR_UNKNOWN;
427
395
          }
428
396
        }
429
397
      }
430
398
      break;
431
399
    case 'G':                   /* --global-env */
432
 
      add_environment(getplugin(NULL), arg, true);
 
400
      if(arg == NULL){
 
401
        break;
 
402
      }
 
403
      if(not add_environment(getplugin(NULL), arg, true)){
 
404
        perror("add_environment");
 
405
      }
433
406
      break;
434
407
    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;
 
408
      if(arg != NULL){
 
409
        char *plugin_name = strsep(&arg, ":");
 
410
        if(plugin_name[0] == '\0'){
 
411
          break;
 
412
        }
 
413
        char *plugin_option;
 
414
        while((plugin_option = strsep(&arg, ",")) != NULL){
 
415
          if(not add_argument(getplugin(plugin_name), plugin_option)){
 
416
            perror("add_argument");
 
417
            return ARGP_ERR_UNKNOWN;
453
418
          }
454
419
        }
455
420
      }
456
421
      break;
457
422
    case 'E':                   /* --env-for */
 
423
      if(arg == NULL){
 
424
        break;
 
425
      }
458
426
      {
459
427
        char *envdef = strchr(arg, ':');
460
428
        if(envdef == NULL){
461
 
          argp_error(state, "No colon in \"%s\"", arg);
462
 
          errno = EINVAL;
463
429
          break;
464
430
        }
465
431
        *envdef = '\0';
466
 
        envdef++;
467
 
        if(arg[0] == '\0'){
468
 
          argp_error(state, "Empty plugin name");
469
 
          errno = EINVAL;
470
 
          break;
 
432
        if(not add_environment(getplugin(arg), envdef+1, true)){
 
433
          perror("add_environment");
471
434
        }
472
 
        add_environment(getplugin(arg), envdef, true);
473
435
      }
474
436
      break;
475
437
    case 'd':                   /* --disable */
476
 
      {
 
438
      if(arg != NULL){
477
439
        plugin *p = getplugin(arg);
478
 
        if(p != NULL){
479
 
          p->disabled = true;
 
440
        if(p == NULL){
 
441
          return ARGP_ERR_UNKNOWN;
480
442
        }
 
443
        p->disabled = true;
481
444
      }
482
445
      break;
483
446
    case 'e':                   /* --enable */
484
 
      {
 
447
      if(arg != NULL){
485
448
        plugin *p = getplugin(arg);
486
 
        if(p != NULL){
487
 
          p->disabled = false;
 
449
        if(p == NULL){
 
450
          return ARGP_ERR_UNKNOWN;
488
451
        }
 
452
        p->disabled = false;
489
453
      }
490
454
      break;
491
455
    case 128:                   /* --plugin-dir */
492
456
      free(plugindir);
493
457
      plugindir = strdup(arg);
 
458
      if(plugindir == NULL){
 
459
        perror("strdup");
 
460
      }      
494
461
      break;
495
462
    case 129:                   /* --config-file */
496
463
      /* This is already done by parse_opt_config_file() */
497
464
      break;
498
465
    case 130:                   /* --userid */
 
466
      errno = 0;
499
467
      tmpmax = strtoimax(arg, &tmp, 10);
500
468
      if(errno != 0 or tmp == arg or *tmp != '\0'
501
469
         or tmpmax != (uid_t)tmpmax){
502
 
        argp_error(state, "Bad user ID number: \"%s\", using %"
503
 
                   PRIdMAX, arg, (intmax_t)uid);
504
 
        break;
 
470
        fprintf(stderr, "Bad user ID number: \"%s\", using %"
 
471
                PRIdMAX "\n", arg, (intmax_t)uid);
 
472
      } else {
 
473
        uid = (uid_t)tmpmax;
505
474
      }
506
 
      uid = (uid_t)tmpmax;
507
475
      break;
508
476
    case 131:                   /* --groupid */
 
477
      errno = 0;
509
478
      tmpmax = strtoimax(arg, &tmp, 10);
510
479
      if(errno != 0 or tmp == arg or *tmp != '\0'
511
480
         or tmpmax != (gid_t)tmpmax){
512
 
        argp_error(state, "Bad group ID number: \"%s\", using %"
513
 
                   PRIdMAX, arg, (intmax_t)gid);
514
 
        break;
 
481
        fprintf(stderr, "Bad group ID number: \"%s\", using %"
 
482
                PRIdMAX "\n", arg, (intmax_t)gid);
 
483
      } else {
 
484
        gid = (gid_t)tmpmax;
515
485
      }
516
 
      gid = (gid_t)tmpmax;
517
486
      break;
518
487
    case 132:                   /* --debug */
519
488
      debug = true;
520
489
      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
490
/*
536
491
 * When adding more options before this line, remember to also add a
537
492
 * "case" to the "parse_opt_config_file" function below.
540
495
      /* Cryptsetup always passes an argument, which is an empty
541
496
         string if "none" was specified in /etc/crypttab.  So if
542
497
         argument was empty, we ignore it silently. */
543
 
      if(arg[0] == '\0'){
544
 
        break;
 
498
      if(arg[0] != '\0'){
 
499
        fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
545
500
      }
 
501
      break;
 
502
    case ARGP_KEY_END:
 
503
      break;
546
504
    default:
547
505
      return ARGP_ERR_UNKNOWN;
548
506
    }
549
 
    return errno;               /* Set to 0 at start */
 
507
    return 0;
550
508
  }
551
509
  
552
510
  /* This option parser is the same as parse_opt() above, except it
554
512
  error_t parse_opt_config_file(int key, char *arg,
555
513
                                __attribute__((unused))
556
514
                                struct argp_state *state){
557
 
    errno = 0;
558
515
    switch(key){
559
516
    case 'g':                   /* --global-options */
560
517
    case 'G':                   /* --global-env */
567
524
    case 129:                   /* --config-file */
568
525
      free(argfile);
569
526
      argfile = strdup(arg);
570
 
      break;
 
527
      if(argfile == NULL){
 
528
        perror("strdup");
 
529
      }
 
530
      break;      
571
531
    case 130:                   /* --userid */
572
532
    case 131:                   /* --groupid */
573
533
    case 132:                   /* --debug */
574
 
    case '?':                   /* --help */
575
 
    case -3:                    /* --usage */
576
 
    case 'V':                   /* --version */
577
534
    case ARGP_KEY_ARG:
 
535
    case ARGP_KEY_END:
578
536
      break;
579
537
    default:
580
538
      return ARGP_ERR_UNKNOWN;
581
539
    }
582
 
    return errno;
 
540
    return 0;
583
541
  }
584
542
  
585
543
  struct argp argp = { .options = options,
589
547
  
590
548
  /* Parse using parse_opt_config_file() in order to get the custom
591
549
     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;
 
550
  ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
 
551
  if(ret == ARGP_ERR_UNKNOWN){
 
552
    fprintf(stderr, "Unknown error while parsing arguments\n");
 
553
    exitstatus = EXIT_FAILURE;
606
554
    goto fallback;
607
555
  }
608
556
  
614
562
    conffp = fopen(AFILE, "r");
615
563
  } else {
616
564
    conffp = fopen(argfile, "r");
617
 
  }
 
565
  }  
618
566
  if(conffp != NULL){
619
567
    char *org_line = NULL;
620
568
    char *p, *arg, *new_arg, *line;
664
612
          goto fallback;
665
613
        }
666
614
        custom_argv[custom_argc-1] = new_arg;
667
 
        custom_argv[custom_argc] = NULL;
 
615
        custom_argv[custom_argc] = NULL;        
668
616
      }
669
617
    }
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
618
    free(org_line);
679
619
  } else {
680
620
    /* Check for harmful errors and go to fallback. Other errors might
685
625
      goto fallback;
686
626
    }
687
627
  }
688
 
  /* If there were any arguments from the configuration file, pass
689
 
     them to parser as command line arguments */
 
628
  /* If there was any arguments from configuration file,
 
629
     pass them to parser as command arguments */
690
630
  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;
 
631
    ret = argp_parse(&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
 
632
                     0, NULL);
 
633
    if(ret == ARGP_ERR_UNKNOWN){
 
634
      fprintf(stderr, "Unknown error while parsing arguments\n");
 
635
      exitstatus = EXIT_FAILURE;
705
636
      goto fallback;
706
637
    }
707
638
  }
708
639
  
709
640
  /* Parse actual command line arguments, to let them override the
710
641
     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;
 
642
  ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
 
643
  if(ret == ARGP_ERR_UNKNOWN){
 
644
    fprintf(stderr, "Unknown error while parsing arguments\n");
 
645
    exitstatus = EXIT_FAILURE;
725
646
    goto fallback;
726
647
  }
727
648
  
749
670
    perror("setuid");
750
671
  }
751
672
  
752
 
  /* Open plugin directory with close_on_exec flag */
 
673
  if(plugindir == NULL){
 
674
    dir = opendir(PDIR);
 
675
  } else {
 
676
    dir = opendir(plugindir);
 
677
  }
 
678
  
 
679
  if(dir == NULL){
 
680
    perror("Could not open plugin dir");
 
681
    exitstatus = EXIT_FAILURE;
 
682
    goto fallback;
 
683
  }
 
684
  
 
685
  /* Set the FD_CLOEXEC flag on the directory, if possible */
753
686
  {
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;
 
687
    int dir_fd = dirfd(dir);
 
688
    if(dir_fd >= 0){
 
689
      ret = set_cloexec_flag(dir_fd);
 
690
      if(ret < 0){
 
691
        perror("set_cloexec_flag");
 
692
        exitstatus = EXIT_FAILURE;
 
693
        goto fallback;
 
694
      }
795
695
    }
796
696
  }
797
697
  
799
699
  
800
700
  /* Read and execute any executable in the plugin directory*/
801
701
  while(true){
802
 
    do {
803
 
      dirst = readdir(dir);
804
 
    } while(dirst == NULL and errno == EINTR);
 
702
    dirst = readdir(dir);
805
703
    
806
704
    /* All directory entries have been processed */
807
705
    if(dirst == NULL){
861
759
    
862
760
    char *filename;
863
761
    if(plugindir == NULL){
864
 
      ret = (int)TEMP_FAILURE_RETRY(asprintf(&filename, PDIR "/%s",
865
 
                                             dirst->d_name));
 
762
      ret = asprintf(&filename, PDIR "/%s", dirst->d_name);
866
763
    } else {
867
 
      ret = (int)TEMP_FAILURE_RETRY(asprintf(&filename, "%s/%s",
868
 
                                             plugindir,
869
 
                                             dirst->d_name));
 
764
      ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
870
765
    }
871
766
    if(ret < 0){
872
767
      perror("asprintf");
873
768
      continue;
874
769
    }
875
770
    
876
 
    ret = (int)TEMP_FAILURE_RETRY(stat(filename, &st));
 
771
    ret = stat(filename, &st);
877
772
    if(ret == -1){
878
773
      perror("stat");
879
774
      free(filename);
881
776
    }
882
777
    
883
778
    /* Ignore non-executable files */
884
 
    if(not S_ISREG(st.st_mode)
885
 
       or (TEMP_FAILURE_RETRY(access(filename, X_OK)) != 0)){
 
779
    if(not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
886
780
      if(debug){
887
781
        fprintf(stderr, "Ignoring plugin dir entry \"%s\""
888
782
                " with bad type or mode\n", filename);
934
828
    }
935
829
    
936
830
    int pipefd[2];
937
 
    ret = (int)TEMP_FAILURE_RETRY(pipe(pipefd));
 
831
    ret = pipe(pipefd);
938
832
    if(ret == -1){
939
833
      perror("pipe");
940
834
      exitstatus = EXIT_FAILURE;
954
848
      goto fallback;
955
849
    }
956
850
    /* 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));
 
851
    ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
960
852
    if(ret < 0){
961
853
      perror("sigprocmask");
962
854
      exitstatus = EXIT_FAILURE;
963
855
      goto fallback;
964
856
    }
965
857
    /* Starting a new process to be watched */
966
 
    pid_t pid;
967
 
    do {
968
 
      pid = fork();
969
 
    } while(pid == -1 and errno == EINTR);
 
858
    pid_t pid = fork();
970
859
    if(pid == -1){
971
860
      perror("fork");
972
861
      exitstatus = EXIT_FAILURE;
1010
899
      /* no return */
1011
900
    }
1012
901
    /* Parent process */
1013
 
    TEMP_FAILURE_RETRY(close(pipefd[1])); /* Close unused write end of
1014
 
                                             pipe */
 
902
    close(pipefd[1]);           /* Close unused write end of pipe */
1015
903
    free(filename);
1016
904
    plugin *new_plugin = getplugin(dirst->d_name);
1017
905
    if(new_plugin == NULL){
1018
906
      perror("getplugin");
1019
 
      ret = (int)(TEMP_FAILURE_RETRY
1020
 
                  (sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
1021
 
                               NULL)));
 
907
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
1022
908
      if(ret < 0){
1023
909
        perror("sigprocmask");
1024
910
      }
1031
917
    
1032
918
    /* Unblock SIGCHLD so signal handler can be run if this process
1033
919
       has already completed */
1034
 
    ret = (int)TEMP_FAILURE_RETRY(sigprocmask(SIG_UNBLOCK,
1035
 
                                              &sigchld_action.sa_mask,
1036
 
                                              NULL));
 
920
    ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
1037
921
    if(ret < 0){
1038
922
      perror("sigprocmask");
1039
923
      exitstatus = EXIT_FAILURE;
1040
924
      goto fallback;
1041
925
    }
1042
926
    
1043
 
    FD_SET(new_plugin->fd, &rfds_all); /* Spurious warning from
1044
 
                                          -Wconversion */
 
927
    FD_SET(new_plugin->fd, &rfds_all);
1045
928
    
1046
929
    if(maxfd < new_plugin->fd){
1047
930
      maxfd = new_plugin->fd;
1048
931
    }
1049
932
  }
1050
933
  
1051
 
  TEMP_FAILURE_RETRY(closedir(dir));
 
934
  closedir(dir);
1052
935
  dir = NULL;
1053
936
  free_plugin(getplugin(NULL));
1054
937
  
1067
950
  while(plugin_list){
1068
951
    fd_set rfds = rfds_all;
1069
952
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
1070
 
    if(select_ret == -1 and errno != EINTR){
 
953
    if(select_ret == -1){
1071
954
      perror("select");
1072
955
      exitstatus = EXIT_FAILURE;
1073
956
      goto fallback;
1090
973
                      WEXITSTATUS(proc->status));
1091
974
            } else if(WIFSIGNALED(proc->status)){
1092
975
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] killed by"
1093
 
                      " signal %d: %s\n", proc->name,
 
976
                      " signal %d\n", proc->name,
1094
977
                      (intmax_t) (proc->pid),
1095
 
                      WTERMSIG(proc->status),
1096
 
                      strsignal(WTERMSIG(proc->status)));
 
978
                      WTERMSIG(proc->status));
1097
979
            } else if(WCOREDUMP(proc->status)){
1098
980
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] dumped"
1099
981
                      " core\n", proc->name, (intmax_t) (proc->pid));
1101
983
          }
1102
984
          
1103
985
          /* Remove the plugin */
1104
 
          FD_CLR(proc->fd, &rfds_all); /* Spurious warning from
1105
 
                                          -Wconversion */
 
986
          FD_CLR(proc->fd, &rfds_all);
1106
987
          
1107
988
          /* Block signal while modifying process_list */
1108
 
          ret = (int)TEMP_FAILURE_RETRY(sigprocmask
1109
 
                                        (SIG_BLOCK,
1110
 
                                         &sigchld_action.sa_mask,
1111
 
                                         NULL));
 
989
          ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
1112
990
          if(ret < 0){
1113
991
            perror("sigprocmask");
1114
992
            exitstatus = EXIT_FAILURE;
1120
998
          proc = next_plugin;
1121
999
          
1122
1000
          /* 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)));
 
1001
          ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
 
1002
                            NULL);
1126
1003
          if(ret < 0){
1127
1004
            perror("sigprocmask");
1128
1005
            exitstatus = EXIT_FAILURE;
1148
1025
      }
1149
1026
      
1150
1027
      /* 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 */
 
1028
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
1154
1029
        /* This process had nothing to say at this time */
1155
1030
        proc = proc->next;
1156
1031
        continue;
1167
1042
        proc->buffer_size += BUFFER_SIZE;
1168
1043
      }
1169
1044
      /* Read from the process */
1170
 
      sret = TEMP_FAILURE_RETRY(read(proc->fd,
1171
 
                                     proc->buffer
1172
 
                                     + proc->buffer_length,
1173
 
                                     BUFFER_SIZE));
 
1045
      sret = read(proc->fd, proc->buffer + proc->buffer_length,
 
1046
                  BUFFER_SIZE);
1174
1047
      if(sret < 0){
1175
1048
        /* Read error from this process; ignore the error */
1176
1049
        proc = proc->next;
1238
1111
  }
1239
1112
  
1240
1113
  /* Wait for any remaining child processes to terminate */
1241
 
  do {
 
1114
  do{
1242
1115
    ret = wait(NULL);
1243
1116
  } while(ret >= 0);
1244
1117
  if(errno != ECHILD){