/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-10-30 16:27:17 UTC
  • Revision ID: teddy@fukt.bsnet.se-20091030162717-dun2a9li8bu6jm2w
* Makefile (DOCBOOKTOMAN, DOCBOOKTOHTML, common.ent, mandos,
            mandos-keygen, mandos-ctl, mandos.lsm): Add $(strip) to
                                                    make commands
                                                    easier to read
                                                    when run.
           (plugins.d/mandos-client): Use $(LINK.c) to compile in the
                                      same way as the other programs -
                                      i.e. not via an object file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*  -*- coding: utf-8 -*- */
 
1
/*  -*- coding: utf-8; mode: c; mode: orgtbl -*- */
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() */
 
26
                                   asprintf(), O_CLOEXEC */
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, opendir(), stat(), struct
 
33
#include <sys/types.h>          /* DIR, fdopendir(), stat(), struct
34
34
                                   stat, waitpid(), WIFEXITED(),
35
35
                                   WEXITSTATUS(), wait(), pid_t,
36
36
                                   uid_t, gid_t, getuid(), getgid(),
42
42
                                   WCOREDUMP() */
43
43
#include <sys/stat.h>           /* struct stat, stat(), S_ISREG() */
44
44
#include <iso646.h>             /* and, or, not */
45
 
#include <dirent.h>             /* DIR, struct dirent, opendir(),
 
45
#include <dirent.h>             /* DIR, struct dirent, fdopendir(),
46
46
                                   readdir(), closedir(), dirfd() */
47
47
#include <unistd.h>             /* struct stat, stat(), S_ISREG(),
48
48
                                   fcntl(), setuid(), setgid(),
68
68
                                */
69
69
#include <errno.h>              /* errno, EBADF */
70
70
#include <inttypes.h>           /* intmax_t, PRIdMAX, strtoimax() */
 
71
#include <sysexits.h>           /* EX_OSERR, EX_USAGE */
71
72
 
72
73
#define BUFFER_SIZE 256
73
74
 
102
103
/* Gets an existing plugin based on name,
103
104
   or if none is found, creates a new one */
104
105
static plugin *getplugin(char *name){
105
 
  /* Check for exiting plugin with that name */
 
106
  /* Check for existing plugin with that name */
106
107
  for(plugin *p = plugin_list; p != NULL; p = p->next){
107
108
    if((p->name == name)
108
109
       or (p->name and name and (strcmp(p->name, name) == 0))){
110
111
    }
111
112
  }
112
113
  /* Create a new plugin */
113
 
  plugin *new_plugin = malloc(sizeof(plugin));
 
114
  plugin *new_plugin = NULL;
 
115
  do {
 
116
    new_plugin = malloc(sizeof(plugin));
 
117
  } while(new_plugin == NULL and errno == EINTR);
114
118
  if(new_plugin == NULL){
115
119
    return NULL;
116
120
  }
117
121
  char *copy_name = NULL;
118
122
  if(name != NULL){
119
 
    copy_name = strdup(name);
 
123
    do {
 
124
      copy_name = strdup(name);
 
125
    } while(copy_name == NULL and errno == EINTR);
120
126
    if(copy_name == NULL){
 
127
      int e = errno;
121
128
      free(new_plugin);
 
129
      errno = e;
122
130
      return NULL;
123
131
    }
124
132
  }
128
136
                          .disabled = false,
129
137
                          .next = plugin_list };
130
138
  
131
 
  new_plugin->argv = malloc(sizeof(char *) * 2);
 
139
  do {
 
140
    new_plugin->argv = malloc(sizeof(char *) * 2);
 
141
  } while(new_plugin->argv == NULL and errno == EINTR);
132
142
  if(new_plugin->argv == NULL){
 
143
    int e = errno;
133
144
    free(copy_name);
134
145
    free(new_plugin);
 
146
    errno = e;
135
147
    return NULL;
136
148
  }
137
149
  new_plugin->argv[0] = copy_name;
138
150
  new_plugin->argv[1] = NULL;
139
151
  
140
 
  new_plugin->environ = malloc(sizeof(char *));
 
152
  do {
 
153
    new_plugin->environ = malloc(sizeof(char *));
 
154
  } while(new_plugin->environ == NULL and errno == EINTR);
141
155
  if(new_plugin->environ == NULL){
 
156
    int e = errno;
142
157
    free(copy_name);
143
158
    free(new_plugin->argv);
144
159
    free(new_plugin);
 
160
    errno = e;
145
161
    return NULL;
146
162
  }
147
163
  new_plugin->environ[0] = NULL;
155
171
static bool add_to_char_array(const char *new, char ***array,
156
172
                              int *len){
157
173
  /* Resize the pointed-to array to hold one more pointer */
158
 
  *array = realloc(*array, sizeof(char *)
159
 
                   * (size_t) ((*len) + 2));
 
174
  do {
 
175
    *array = realloc(*array, sizeof(char *)
 
176
                     * (size_t) ((*len) + 2));
 
177
  } while(*array == NULL and errno == EINTR);
160
178
  /* Malloc check */
161
179
  if(*array == NULL){
162
180
    return false;
163
181
  }
164
182
  /* Make a copy of the new string */
165
 
  char *copy = strdup(new);
 
183
  char *copy;
 
184
  do {
 
185
    copy = strdup(new);
 
186
  } while(copy == NULL and errno == EINTR);
166
187
  if(copy == NULL){
167
188
    return false;
168
189
  }
194
215
    if(strncmp(*e, def, namelen + 1) == 0){
195
216
      /* It already exists */
196
217
      if(replace){
197
 
        char *new = realloc(*e, strlen(def) + 1);
 
218
        char *new;
 
219
        do {
 
220
          new = realloc(*e, strlen(def) + 1);
 
221
        } while(new == NULL and errno == EINTR);
198
222
        if(new == NULL){
199
223
          return false;
200
224
        }
210
234
/*
211
235
 * Based on the example in the GNU LibC manual chapter 13.13 "File
212
236
 * Descriptor Flags".
213
 
 * *Note File Descriptor Flags:(libc)Descriptor Flags.
 
237
 | [[info:libc:Descriptor%20Flags][File Descriptor Flags]] |
214
238
 */
215
239
static int set_cloexec_flag(int fd){
216
 
  int ret = fcntl(fd, F_GETFD, 0);
 
240
  int ret = (int)TEMP_FAILURE_RETRY(fcntl(fd, F_GETFD, 0));
217
241
  /* If reading the flags failed, return error indication now. */
218
242
  if(ret < 0){
219
243
    return ret;
220
244
  }
221
245
  /* Store modified flag word in the descriptor. */
222
 
  return fcntl(fd, F_SETFD, ret | FD_CLOEXEC);
 
246
  return (int)TEMP_FAILURE_RETRY(fcntl(fd, F_SETFD,
 
247
                                       ret | FD_CLOEXEC));
223
248
}
224
249
 
225
250
 
317
342
  fd_set rfds_all;
318
343
  int ret, maxfd = 0;
319
344
  ssize_t sret;
320
 
  intmax_t tmpmax;
321
345
  uid_t uid = 65534;
322
346
  gid_t gid = 65534;
323
347
  bool debug = false;
377
401
      .doc = "Group ID the plugins will run as", .group = 3 },
378
402
    { .name = "debug", .key = 132,
379
403
      .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 },
380
413
    { .name = NULL }
381
414
  };
382
415
  
383
 
  error_t parse_opt(int key, char *arg, __attribute__((unused))
384
 
                    struct argp_state *state){
385
 
    char *tmp;
 
416
  error_t parse_opt(int key, char *arg, struct argp_state *state){
 
417
    errno = 0;
386
418
    switch(key){
 
419
      char *tmp;
 
420
      intmax_t tmpmax;
387
421
    case 'g':                   /* --global-options */
388
 
      if(arg != NULL){
 
422
      {
389
423
        char *plugin_option;
390
424
        while((plugin_option = strsep(&arg, ",")) != NULL){
391
 
          if(plugin_option[0] == '\0'){
392
 
            continue;
393
 
          }
394
425
          if(not add_argument(getplugin(NULL), plugin_option)){
395
 
            perror("add_argument");
396
 
            return ARGP_ERR_UNKNOWN;
 
426
            break;
397
427
          }
398
428
        }
399
429
      }
400
430
      break;
401
431
    case 'G':                   /* --global-env */
402
 
      if(arg == NULL){
403
 
        break;
404
 
      }
405
 
      if(not add_environment(getplugin(NULL), arg, true)){
406
 
        perror("add_environment");
407
 
      }
 
432
      add_environment(getplugin(NULL), arg, true);
408
433
      break;
409
434
    case 'o':                   /* --options-for */
410
 
      if(arg != NULL){
411
 
        char *plugin_name = strsep(&arg, ":");
412
 
        if(plugin_name[0] == '\0'){
413
 
          break;
414
 
        }
415
 
        char *plugin_option;
416
 
        while((plugin_option = strsep(&arg, ",")) != NULL){
417
 
          if(not add_argument(getplugin(plugin_name), plugin_option)){
418
 
            perror("add_argument");
419
 
            return ARGP_ERR_UNKNOWN;
 
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;
420
453
          }
421
454
        }
422
455
      }
423
456
      break;
424
457
    case 'E':                   /* --env-for */
425
 
      if(arg == NULL){
426
 
        break;
427
 
      }
428
458
      {
429
459
        char *envdef = strchr(arg, ':');
430
460
        if(envdef == NULL){
 
461
          argp_error(state, "No colon in \"%s\"", arg);
 
462
          errno = EINVAL;
431
463
          break;
432
464
        }
433
465
        *envdef = '\0';
434
 
        if(not add_environment(getplugin(arg), envdef+1, true)){
435
 
          perror("add_environment");
 
466
        envdef++;
 
467
        if(arg[0] == '\0'){
 
468
          argp_error(state, "Empty plugin name");
 
469
          errno = EINVAL;
 
470
          break;
436
471
        }
 
472
        add_environment(getplugin(arg), envdef, true);
437
473
      }
438
474
      break;
439
475
    case 'd':                   /* --disable */
440
 
      if(arg != NULL){
 
476
      {
441
477
        plugin *p = getplugin(arg);
442
 
        if(p == NULL){
443
 
          return ARGP_ERR_UNKNOWN;
 
478
        if(p != NULL){
 
479
          p->disabled = true;
444
480
        }
445
 
        p->disabled = true;
446
481
      }
447
482
      break;
448
483
    case 'e':                   /* --enable */
449
 
      if(arg != NULL){
 
484
      {
450
485
        plugin *p = getplugin(arg);
451
 
        if(p == NULL){
452
 
          return ARGP_ERR_UNKNOWN;
 
486
        if(p != NULL){
 
487
          p->disabled = false;
453
488
        }
454
 
        p->disabled = false;
455
489
      }
456
490
      break;
457
491
    case 128:                   /* --plugin-dir */
458
492
      free(plugindir);
459
493
      plugindir = strdup(arg);
460
 
      if(plugindir == NULL){
461
 
        perror("strdup");
462
 
      }
463
494
      break;
464
495
    case 129:                   /* --config-file */
465
496
      /* This is already done by parse_opt_config_file() */
466
497
      break;
467
498
    case 130:                   /* --userid */
468
 
      errno = 0;
469
499
      tmpmax = strtoimax(arg, &tmp, 10);
470
500
      if(errno != 0 or tmp == arg or *tmp != '\0'
471
501
         or tmpmax != (uid_t)tmpmax){
472
 
        fprintf(stderr, "Bad user ID number: \"%s\", using %"
473
 
                PRIdMAX "\n", arg, (intmax_t)uid);
474
 
      } else {
475
 
        uid = (uid_t)tmpmax;
 
502
        argp_error(state, "Bad user ID number: \"%s\", using %"
 
503
                   PRIdMAX, arg, (intmax_t)uid);
 
504
        break;
476
505
      }
 
506
      uid = (uid_t)tmpmax;
477
507
      break;
478
508
    case 131:                   /* --groupid */
479
 
      errno = 0;
480
509
      tmpmax = strtoimax(arg, &tmp, 10);
481
510
      if(errno != 0 or tmp == arg or *tmp != '\0'
482
511
         or tmpmax != (gid_t)tmpmax){
483
 
        fprintf(stderr, "Bad group ID number: \"%s\", using %"
484
 
                PRIdMAX "\n", arg, (intmax_t)gid);
485
 
      } else {
486
 
        gid = (gid_t)tmpmax;
 
512
        argp_error(state, "Bad group ID number: \"%s\", using %"
 
513
                   PRIdMAX, arg, (intmax_t)gid);
 
514
        break;
487
515
      }
 
516
      gid = (gid_t)tmpmax;
488
517
      break;
489
518
    case 132:                   /* --debug */
490
519
      debug = true;
491
520
      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;
492
535
/*
493
536
 * When adding more options before this line, remember to also add a
494
537
 * "case" to the "parse_opt_config_file" function below.
497
540
      /* Cryptsetup always passes an argument, which is an empty
498
541
         string if "none" was specified in /etc/crypttab.  So if
499
542
         argument was empty, we ignore it silently. */
500
 
      if(arg[0] != '\0'){
501
 
        fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
 
543
      if(arg[0] == '\0'){
 
544
        break;
502
545
      }
503
 
      break;
504
 
    case ARGP_KEY_END:
505
 
      break;
506
546
    default:
507
547
      return ARGP_ERR_UNKNOWN;
508
548
    }
509
 
    return 0;
 
549
    return errno;               /* Set to 0 at start */
510
550
  }
511
551
  
512
552
  /* This option parser is the same as parse_opt() above, except it
514
554
  error_t parse_opt_config_file(int key, char *arg,
515
555
                                __attribute__((unused))
516
556
                                struct argp_state *state){
 
557
    errno = 0;
517
558
    switch(key){
518
559
    case 'g':                   /* --global-options */
519
560
    case 'G':                   /* --global-env */
526
567
    case 129:                   /* --config-file */
527
568
      free(argfile);
528
569
      argfile = strdup(arg);
529
 
      if(argfile == NULL){
530
 
        perror("strdup");
531
 
      }
532
570
      break;
533
571
    case 130:                   /* --userid */
534
572
    case 131:                   /* --groupid */
535
573
    case 132:                   /* --debug */
 
574
    case '?':                   /* --help */
 
575
    case -3:                    /* --usage */
 
576
    case 'V':                   /* --version */
536
577
    case ARGP_KEY_ARG:
537
 
    case ARGP_KEY_END:
538
578
      break;
539
579
    default:
540
580
      return ARGP_ERR_UNKNOWN;
541
581
    }
542
 
    return 0;
 
582
    return errno;
543
583
  }
544
584
  
545
585
  struct argp argp = { .options = options,
549
589
  
550
590
  /* Parse using parse_opt_config_file() in order to get the custom
551
591
     config file location, if any. */
552
 
  ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
553
 
  if(ret == ARGP_ERR_UNKNOWN){
554
 
    fprintf(stderr, "Unknown error while parsing arguments\n");
555
 
    exitstatus = EXIT_FAILURE;
 
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;
556
606
    goto fallback;
557
607
  }
558
608
  
617
667
        custom_argv[custom_argc] = NULL;
618
668
      }
619
669
    }
 
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
    }
620
678
    free(org_line);
621
679
  } else {
622
680
    /* Check for harmful errors and go to fallback. Other errors might
627
685
      goto fallback;
628
686
    }
629
687
  }
630
 
  /* If there was any arguments from configuration file,
631
 
     pass them to parser as command arguments */
 
688
  /* If there were any arguments from the configuration file, pass
 
689
     them to parser as command line arguments */
632
690
  if(custom_argv != NULL){
633
 
    ret = argp_parse(&argp, custom_argc, custom_argv, ARGP_IN_ORDER,
634
 
                     0, NULL);
635
 
    if(ret == ARGP_ERR_UNKNOWN){
636
 
      fprintf(stderr, "Unknown error while parsing arguments\n");
637
 
      exitstatus = EXIT_FAILURE;
 
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;
638
705
      goto fallback;
639
706
    }
640
707
  }
641
708
  
642
709
  /* Parse actual command line arguments, to let them override the
643
710
     config file */
644
 
  ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, NULL);
645
 
  if(ret == ARGP_ERR_UNKNOWN){
646
 
    fprintf(stderr, "Unknown error while parsing arguments\n");
647
 
    exitstatus = EXIT_FAILURE;
 
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;
648
725
    goto fallback;
649
726
  }
650
727
  
672
749
    perror("setuid");
673
750
  }
674
751
  
675
 
  if(plugindir == NULL){
676
 
    dir = opendir(PDIR);
677
 
  } else {
678
 
    dir = opendir(plugindir);
679
 
  }
680
 
  
681
 
  if(dir == NULL){
682
 
    perror("Could not open plugin dir");
683
 
    exitstatus = EXIT_FAILURE;
684
 
    goto fallback;
685
 
  }
686
 
  
687
 
  /* Set the FD_CLOEXEC flag on the directory, if possible */
 
752
  /* Open plugin directory with close_on_exec flag */
688
753
  {
689
 
    int dir_fd = dirfd(dir);
690
 
    if(dir_fd >= 0){
691
 
      ret = set_cloexec_flag(dir_fd);
692
 
      if(ret < 0){
693
 
        perror("set_cloexec_flag");
694
 
        exitstatus = EXIT_FAILURE;
695
 
        goto fallback;
696
 
      }
 
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;
697
795
    }
698
796
  }
699
797
  
701
799
  
702
800
  /* Read and execute any executable in the plugin directory*/
703
801
  while(true){
704
 
    dirst = readdir(dir);
 
802
    do {
 
803
      dirst = readdir(dir);
 
804
    } while(dirst == NULL and errno == EINTR);
705
805
    
706
806
    /* All directory entries have been processed */
707
807
    if(dirst == NULL){
761
861
    
762
862
    char *filename;
763
863
    if(plugindir == NULL){
764
 
      ret = asprintf(&filename, PDIR "/%s", dirst->d_name);
 
864
      ret = (int)TEMP_FAILURE_RETRY(asprintf(&filename, PDIR "/%s",
 
865
                                             dirst->d_name));
765
866
    } else {
766
 
      ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
 
867
      ret = (int)TEMP_FAILURE_RETRY(asprintf(&filename, "%s/%s",
 
868
                                             plugindir,
 
869
                                             dirst->d_name));
767
870
    }
768
871
    if(ret < 0){
769
872
      perror("asprintf");
770
873
      continue;
771
874
    }
772
875
    
773
 
    ret = stat(filename, &st);
 
876
    ret = (int)TEMP_FAILURE_RETRY(stat(filename, &st));
774
877
    if(ret == -1){
775
878
      perror("stat");
776
879
      free(filename);
778
881
    }
779
882
    
780
883
    /* Ignore non-executable files */
781
 
    if(not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
 
884
    if(not S_ISREG(st.st_mode)
 
885
       or (TEMP_FAILURE_RETRY(access(filename, X_OK)) != 0)){
782
886
      if(debug){
783
887
        fprintf(stderr, "Ignoring plugin dir entry \"%s\""
784
888
                " with bad type or mode\n", filename);
830
934
    }
831
935
    
832
936
    int pipefd[2];
833
 
    ret = pipe(pipefd);
 
937
    ret = (int)TEMP_FAILURE_RETRY(pipe(pipefd));
834
938
    if(ret == -1){
835
939
      perror("pipe");
836
940
      exitstatus = EXIT_FAILURE;
850
954
      goto fallback;
851
955
    }
852
956
    /* Block SIGCHLD until process is safely in process list */
853
 
    ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
 
957
    ret = (int)TEMP_FAILURE_RETRY(sigprocmask(SIG_BLOCK,
 
958
                                              &sigchld_action.sa_mask,
 
959
                                              NULL));
854
960
    if(ret < 0){
855
961
      perror("sigprocmask");
856
962
      exitstatus = EXIT_FAILURE;
857
963
      goto fallback;
858
964
    }
859
965
    /* Starting a new process to be watched */
860
 
    pid_t pid = fork();
 
966
    pid_t pid;
 
967
    do {
 
968
      pid = fork();
 
969
    } while(pid == -1 and errno == EINTR);
861
970
    if(pid == -1){
862
971
      perror("fork");
863
972
      exitstatus = EXIT_FAILURE;
901
1010
      /* no return */
902
1011
    }
903
1012
    /* Parent process */
904
 
    close(pipefd[1]);           /* Close unused write end of pipe */
 
1013
    TEMP_FAILURE_RETRY(close(pipefd[1])); /* Close unused write end of
 
1014
                                             pipe */
905
1015
    free(filename);
906
1016
    plugin *new_plugin = getplugin(dirst->d_name);
907
1017
    if(new_plugin == NULL){
908
1018
      perror("getplugin");
909
 
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
1019
      ret = (int)(TEMP_FAILURE_RETRY
 
1020
                  (sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
 
1021
                               NULL)));
910
1022
      if(ret < 0){
911
1023
        perror("sigprocmask");
912
1024
      }
919
1031
    
920
1032
    /* Unblock SIGCHLD so signal handler can be run if this process
921
1033
       has already completed */
922
 
    ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
 
1034
    ret = (int)TEMP_FAILURE_RETRY(sigprocmask(SIG_UNBLOCK,
 
1035
                                              &sigchld_action.sa_mask,
 
1036
                                              NULL));
923
1037
    if(ret < 0){
924
1038
      perror("sigprocmask");
925
1039
      exitstatus = EXIT_FAILURE;
926
1040
      goto fallback;
927
1041
    }
928
1042
    
929
 
    FD_SET(new_plugin->fd, &rfds_all);
 
1043
    FD_SET(new_plugin->fd, &rfds_all); /* Spurious warning from
 
1044
                                          -Wconversion */
930
1045
    
931
1046
    if(maxfd < new_plugin->fd){
932
1047
      maxfd = new_plugin->fd;
933
1048
    }
934
1049
  }
935
1050
  
936
 
  closedir(dir);
 
1051
  TEMP_FAILURE_RETRY(closedir(dir));
937
1052
  dir = NULL;
938
1053
  free_plugin(getplugin(NULL));
939
1054
  
952
1067
  while(plugin_list){
953
1068
    fd_set rfds = rfds_all;
954
1069
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
955
 
    if(select_ret == -1){
 
1070
    if(select_ret == -1 and errno != EINTR){
956
1071
      perror("select");
957
1072
      exitstatus = EXIT_FAILURE;
958
1073
      goto fallback;
986
1101
          }
987
1102
          
988
1103
          /* Remove the plugin */
989
 
          FD_CLR(proc->fd, &rfds_all);
 
1104
          FD_CLR(proc->fd, &rfds_all); /* Spurious warning from
 
1105
                                          -Wconversion */
990
1106
          
991
1107
          /* Block signal while modifying process_list */
992
 
          ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
 
1108
          ret = (int)TEMP_FAILURE_RETRY(sigprocmask
 
1109
                                        (SIG_BLOCK,
 
1110
                                         &sigchld_action.sa_mask,
 
1111
                                         NULL));
993
1112
          if(ret < 0){
994
1113
            perror("sigprocmask");
995
1114
            exitstatus = EXIT_FAILURE;
1001
1120
          proc = next_plugin;
1002
1121
          
1003
1122
          /* We are done modifying process list, so unblock signal */
1004
 
          ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
1005
 
                            NULL);
 
1123
          ret = (int)(TEMP_FAILURE_RETRY
 
1124
                      (sigprocmask(SIG_UNBLOCK,
 
1125
                                   &sigchld_action.sa_mask, NULL)));
1006
1126
          if(ret < 0){
1007
1127
            perror("sigprocmask");
1008
1128
            exitstatus = EXIT_FAILURE;
1028
1148
      }
1029
1149
      
1030
1150
      /* This process has not completed.  Does it have any output? */
1031
 
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
 
1151
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){ /* Spurious
 
1152
                                                         warning from
 
1153
                                                         -Wconversion */
1032
1154
        /* This process had nothing to say at this time */
1033
1155
        proc = proc->next;
1034
1156
        continue;
1045
1167
        proc->buffer_size += BUFFER_SIZE;
1046
1168
      }
1047
1169
      /* Read from the process */
1048
 
      sret = read(proc->fd, proc->buffer + proc->buffer_length,
1049
 
                  BUFFER_SIZE);
 
1170
      sret = TEMP_FAILURE_RETRY(read(proc->fd,
 
1171
                                     proc->buffer
 
1172
                                     + proc->buffer_length,
 
1173
                                     BUFFER_SIZE));
1050
1174
      if(sret < 0){
1051
1175
        /* Read error from this process; ignore the error */
1052
1176
        proc = proc->next;
1114
1238
  }
1115
1239
  
1116
1240
  /* Wait for any remaining child processes to terminate */
1117
 
  do{
 
1241
  do {
1118
1242
    ret = wait(NULL);
1119
1243
  } while(ret >= 0);
1120
1244
  if(errno != ECHILD){