/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: 2014-06-07 21:58:56 UTC
  • Revision ID: teddy@recompile.se-20140607215856-9q31dfsc68uuvzo4
Make mandos-client use scandirat() if it exists.

* plugins.d/mandos-client.d (hookdir_fd): File descriptor for hookdir.
  (set_cloexec_flag): Define this function if O_CLOEXEC is undefined.
  (run_network_hooks): Open hookdir_fd.  Use scandirat() if it exists.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
/*
3
3
 * Mandos plugin runner - Run Mandos plugins
4
4
 *
5
 
 * Copyright © 2008-2021 Teddy Hogeborn
6
 
 * Copyright © 2008-2021 Björn Påhlsson
7
 
 * 
8
 
 * This file is part of Mandos.
9
 
 * 
10
 
 * Mandos is free software: you can redistribute it and/or modify it
11
 
 * under the terms of the GNU General Public License as published by
12
 
 * the Free Software Foundation, either version 3 of the License, or
13
 
 * (at your option) any later version.
14
 
 * 
15
 
 * Mandos is distributed in the hope that it will be useful, but
 
5
 * Copyright © 2008-2014 Teddy Hogeborn
 
6
 * Copyright © 2008-2014 Björn Påhlsson
 
7
 * 
 
8
 * This program is free software: you can redistribute it and/or
 
9
 * modify it under the terms of the GNU General Public License as
 
10
 * published by the Free Software Foundation, either version 3 of the
 
11
 * License, or (at your option) any later version.
 
12
 * 
 
13
 * This program is distributed in the hope that it will be useful, but
16
14
 * WITHOUT ANY WARRANTY; without even the implied warranty of
17
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18
16
 * General Public License for more details.
19
17
 * 
20
18
 * You should have received a copy of the GNU General Public License
21
 
 * along with Mandos.  If not, see <http://www.gnu.org/licenses/>.
 
19
 * along with this program.  If not, see
 
20
 * <http://www.gnu.org/licenses/>.
22
21
 * 
23
22
 * Contact the authors at <mandos@recompile.se>.
24
23
 */
25
24
 
26
 
#define _GNU_SOURCE             /* strchrnul(), TEMP_FAILURE_RETRY(),
27
 
                                   getline(), asprintf(), O_CLOEXEC,
28
 
                                   scandirat(), pipe2() */
29
 
#include <argp.h>               /* argp_program_version,
30
 
                                   argp_program_bug_address,
31
 
                                   struct argp_option,
32
 
                                   struct argp_state, argp_error(),
33
 
                                   ARGP_NO_EXIT, argp_state_help,
34
 
                                   ARGP_HELP_STD_HELP,
35
 
                                   ARGP_HELP_USAGE, ARGP_HELP_EXIT_OK,
36
 
                                   ARGP_KEY_ARG, ARGP_ERR_UNKNOWN,
37
 
                                   struct argp, argp_parse(),
38
 
                                   ARGP_IN_ORDER, ARGP_NO_HELP */
39
 
#include <stdbool.h>            /* bool, false, true */
40
 
#include <sys/types.h>          /* pid_t, sig_atomic_t, uid_t, gid_t,
41
 
                                   getuid(), setgid(), setuid() */
 
25
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY(), getline(),
 
26
                                   O_CLOEXEC */
42
27
#include <stddef.h>             /* size_t, NULL */
43
 
#include <iso646.h>             /* or, and, not */
44
 
#include <string.h>             /* strcmp(), strdup(), strchrnul(),
45
 
                                   strncmp(), strlen(), strcpy(),
46
 
                                   strsep(), strchr(), strsignal() */
47
 
#include <stdlib.h>             /* malloc(), free(), reallocarray(),
48
 
                                   realloc(), EXIT_SUCCESS */
49
 
#include <errno.h>              /* errno, EINTR, ENOMEM, ECHILD,
50
 
                                   error_t, EINVAL, EMFILE, ENFILE,
51
 
                                   ENOENT, ESRCH */
52
 
#include <stdint.h>             /* SIZE_MAX */
53
 
#define _GNU_SOURCE             /* strchrnul(), TEMP_FAILURE_RETRY(),
54
 
                                   getline(), asprintf(), O_CLOEXEC,
55
 
                                   scandirat(), pipe2() */
56
 
#include <unistd.h>             /* TEMP_FAILURE_RETRY(), ssize_t,
57
 
                                   write(), STDOUT_FILENO, uid_t,
58
 
                                   gid_t, getuid(), fchown(), close(),
59
 
                                   symlink(), setgid(), setuid(),
60
 
                                   faccessat(), X_OK, pipe(), pipe2(),
61
 
                                   fork(), _exit(), dup2(), fexecve(),
62
 
                                   read(), getpass() */
 
28
#include <stdlib.h>             /* malloc(), exit(), EXIT_SUCCESS,
 
29
                                   realloc() */
 
30
#include <stdbool.h>            /* bool, true, false */
 
31
#include <stdio.h>              /* fileno(), fprintf(),
 
32
                                   stderr, STDOUT_FILENO, fclose() */
 
33
#include <sys/types.h>          /* DIR, fdopendir(), fstat(), struct
 
34
                                   stat, waitpid(), WIFEXITED(),
 
35
                                   WEXITSTATUS(), wait(), pid_t,
 
36
                                   uid_t, gid_t, getuid(), getgid(),
 
37
                                   dirfd() */
 
38
#include <sys/select.h>         /* fd_set, select(), FD_ZERO(),
 
39
                                   FD_SET(), FD_ISSET(), FD_CLR */
 
40
#include <sys/wait.h>           /* wait(), waitpid(), WIFEXITED(),
 
41
                                   WEXITSTATUS(), WTERMSIG(),
 
42
                                   WCOREDUMP() */
 
43
#include <sys/stat.h>           /* struct stat, fstat(), S_ISREG() */
 
44
#include <iso646.h>             /* and, or, not */
 
45
#include <dirent.h>             /* DIR, struct dirent, fdopendir(),
 
46
                                   readdir(), closedir(), dirfd() */
 
47
#include <unistd.h>             /* fcntl(), F_GETFD, F_SETFD,
 
48
                                   FD_CLOEXEC, write(), STDOUT_FILENO,
 
49
                                   struct stat, fstat(), close(),
 
50
                                   setgid(), setuid(), S_ISREG(),
 
51
                                   faccessat() pipe(), fork(),
 
52
                                   _exit(), dup2(), fexecve(), read()
 
53
                                */
63
54
#include <fcntl.h>              /* fcntl(), F_GETFD, F_SETFD,
64
 
                                   FD_CLOEXEC, open(), O_RDONLY,
65
 
                                   O_CLOEXEC, openat() */
66
 
#include <sys/wait.h>           /* waitpid(), WNOHANG, WIFEXITED(),
67
 
                                   WEXITSTATUS(), WIFSIGNALED(),
68
 
                                   WTERMSIG(), wait() */
69
 
#include <error.h>              /* error() */
70
 
#include <stdio.h>              /* FILE, fprintf(), fopen(),
71
 
                                   getline(), fclose(), EOF,
72
 
                                   asprintf(), stderr */
73
 
#include <dirent.h>             /* struct dirent, scandirat(),
74
 
                                   alphasort() */
75
 
#include <sys/stat.h>           /* struct stat, fstat(), S_ISDIR(),
76
 
                                   lstat(), S_ISREG() */
77
 
#include <sys/select.h>         /* fd_set, FD_ZERO(), FD_SETSIZE,
78
 
                                   FD_SET(), select(), FD_CLR(),
79
 
                                   FD_ISSET() */
80
 
#include <signal.h>             /* struct sigaction, SA_NOCLDSTOP,
81
 
                                   sigemptyset(), sigaddset(),
82
 
                                   SIGCHLD, sigprocmask(), SIG_BLOCK,
83
 
                                   SIG_UNBLOCK, kill(), SIGTERM */
 
55
                                   FD_CLOEXEC, openat() */
 
56
#include <string.h>             /* strsep, strlen(), strsignal(),
 
57
                                   strcmp(), strncmp() */
 
58
#include <errno.h>              /* errno */
 
59
#include <argp.h>               /* struct argp_option, struct
 
60
                                   argp_state, struct argp,
 
61
                                   argp_parse(), ARGP_ERR_UNKNOWN,
 
62
                                   ARGP_KEY_END, ARGP_KEY_ARG,
 
63
                                   error_t */
 
64
#include <signal.h>             /* struct sigaction, sigemptyset(),
 
65
                                   sigaddset(), sigaction(),
 
66
                                   sigprocmask(), SIG_BLOCK, SIGCHLD,
 
67
                                   SIG_UNBLOCK, kill(), sig_atomic_t
 
68
                                */
 
69
#include <errno.h>              /* errno, EBADF */
 
70
#include <inttypes.h>           /* intmax_t, PRIdMAX, strtoimax() */
84
71
#include <sysexits.h>           /* EX_OSERR, EX_USAGE, EX_IOERR,
85
72
                                   EX_CONFIG, EX_UNAVAILABLE, EX_OK */
86
 
#include <inttypes.h>           /* intmax_t, strtoimax(), PRIdMAX */
87
 
#include <fnmatch.h>            /* fnmatch(), FNM_FILE_NAME,
88
 
                                   FNM_PERIOD, FNM_NOMATCH */
 
73
#include <errno.h>              /* errno */
 
74
#include <error.h>              /* error() */
 
75
#include <fnmatch.h>            /* fnmatch() */
89
76
 
90
77
#define BUFFER_SIZE 256
91
78
 
92
79
#define PDIR "/lib/mandos/plugins.d"
93
 
#define PHDIR "/lib/mandos/plugin-helpers"
94
80
#define AFILE "/conf/conf.d/mandos/plugin-runner.conf"
95
81
 
96
82
const char *argp_program_version = "plugin-runner " VERSION;
193
179
  /* Resize the pointed-to array to hold one more pointer */
194
180
  char **new_array = NULL;
195
181
  do {
196
 
#if defined(__GLIBC_PREREQ) and __GLIBC_PREREQ(2, 26)
197
 
    new_array = reallocarray(*array, (size_t)((*len) + 2),
198
 
                             sizeof(char *));
199
 
#else
200
 
    if(((size_t)((*len) + 2)) > (SIZE_MAX / sizeof(char *))){
201
 
      /* overflow */
202
 
      new_array = NULL;
203
 
      errno = ENOMEM;
204
 
    } else {
205
 
      new_array = realloc(*array, (size_t)((*len) + 2)
206
 
                          * sizeof(char *));
207
 
    }
208
 
#endif
 
182
    new_array = realloc(*array, sizeof(char *)
 
183
                        * (size_t) ((*len) + 2));
209
184
  } while(new_array == NULL and errno == EINTR);
210
185
  /* Malloc check */
211
186
  if(new_array == NULL){
266
241
  return add_to_char_array(def, &(p->environ), &(p->envc));
267
242
}
268
243
 
269
 
#ifndef O_CLOEXEC
270
244
/*
271
245
 * Based on the example in the GNU LibC manual chapter 13.13 "File
272
246
 * Descriptor Flags".
283
257
  return (int)TEMP_FAILURE_RETRY(fcntl(fd, F_SETFD,
284
258
                                       ret | FD_CLOEXEC));
285
259
}
286
 
#endif  /* not O_CLOEXEC */
287
260
 
288
261
 
289
262
/* Mark processes as completed when they exit, and save their exit
338
311
__attribute__((nonnull))
339
312
static void free_plugin(plugin *plugin_node){
340
313
  
341
 
  for(char **arg = (plugin_node->argv)+1; *arg != NULL; arg++){
 
314
  for(char **arg = plugin_node->argv; *arg != NULL; arg++){
342
315
    free(*arg);
343
316
  }
344
 
  free(plugin_node->name);
345
317
  free(plugin_node->argv);
346
318
  for(char **env = plugin_node->environ; *env != NULL; env++){
347
319
    free(*env);
374
346
 
375
347
int main(int argc, char *argv[]){
376
348
  char *plugindir = NULL;
377
 
  char *pluginhelperdir = NULL;
378
349
  char *argfile = NULL;
379
350
  FILE *conffp;
380
 
  struct dirent **direntries = NULL;
 
351
  DIR *dir = NULL;
 
352
  struct dirent *dirst;
381
353
  struct stat st;
382
354
  fd_set rfds_all;
383
355
  int ret, maxfd = 0;
391
363
                                      .sa_flags = SA_NOCLDSTOP };
392
364
  char **custom_argv = NULL;
393
365
  int custom_argc = 0;
394
 
  int dir_fd = -1;
 
366
  int dir_fd;
395
367
  
396
368
  /* Establish a signal handler */
397
369
  sigemptyset(&sigchld_action.sa_mask);
442
414
      .doc = "Group ID the plugins will run as", .group = 3 },
443
415
    { .name = "debug", .key = 132,
444
416
      .doc = "Debug mode", .group = 4 },
445
 
    { .name = "plugin-helper-dir", .key = 133,
446
 
      .arg = "DIRECTORY",
447
 
      .doc = "Specify a different plugin helper directory",
448
 
      .group = 2 },
449
417
    /*
450
418
     * These reproduce what we would get without ARGP_NO_HELP
451
419
     */
577
545
    case 132:                   /* --debug */
578
546
      debug = true;
579
547
      break;
580
 
    case 133:                   /* --plugin-helper-dir */
581
 
      free(pluginhelperdir);
582
 
      pluginhelperdir = strdup(arg);
583
 
      if(pluginhelperdir != NULL){
584
 
        errno = 0;
585
 
      }
586
 
      break;
587
548
      /*
588
549
       * These reproduce what we would get without ARGP_NO_HELP
589
550
       */
590
551
    case '?':                   /* --help */
591
552
      state->flags &= ~(unsigned int)ARGP_NO_EXIT; /* force exit */
592
553
      argp_state_help(state, state->out_stream, ARGP_HELP_STD_HELP);
593
 
      __builtin_unreachable();
594
554
    case -3:                    /* --usage */
595
555
      state->flags &= ~(unsigned int)ARGP_NO_EXIT; /* force exit */
596
556
      argp_state_help(state, state->out_stream,
597
557
                      ARGP_HELP_USAGE | ARGP_HELP_EXIT_OK);
598
 
      __builtin_unreachable();
599
558
    case 'V':                   /* --version */
600
559
      fprintf(state->out_stream, "%s\n", argp_program_version);
601
560
      exit(EXIT_SUCCESS);
611
570
      if(arg[0] == '\0'){
612
571
        break;
613
572
      }
614
 
#if __GNUC__ >= 7
615
 
      __attribute__((fallthrough));
616
 
#else
617
 
          /* FALLTHROUGH */
618
 
#endif
619
573
    default:
620
574
      return ARGP_ERR_UNKNOWN;
621
575
    }
647
601
    case 130:                   /* --userid */
648
602
    case 131:                   /* --groupid */
649
603
    case 132:                   /* --debug */
650
 
    case 133:                   /* --plugin-helper-dir */
651
604
    case '?':                   /* --help */
652
605
    case -3:                    /* --usage */
653
606
    case 'V':                   /* --version */
733
686
        
734
687
        custom_argc += 1;
735
688
        {
736
 
#if defined(__GLIBC_PREREQ) and __GLIBC_PREREQ(2, 26)
737
 
          char **new_argv = reallocarray(custom_argv, (size_t)custom_argc + 1,
738
 
                                         sizeof(char *));
739
 
#else
740
 
          char **new_argv = NULL;
741
 
          if(((size_t)custom_argc + 1) > (SIZE_MAX / sizeof(char *))){
742
 
            /* overflow */
743
 
            errno = ENOMEM;
744
 
          } else {
745
 
            new_argv = realloc(custom_argv, ((size_t)custom_argc + 1)
746
 
                               * sizeof(char *));
747
 
          }
748
 
#endif
 
689
          char **new_argv = realloc(custom_argv, sizeof(char *)
 
690
                                    * ((unsigned int)
 
691
                                       custom_argc + 1));
749
692
          if(new_argv == NULL){
750
 
            error(0, errno, "reallocarray");
 
693
            error(0, errno, "realloc");
751
694
            exitstatus = EX_OSERR;
752
695
            free(new_arg);
753
696
            free(org_line);
818
761
    goto fallback;
819
762
  }
820
763
  
821
 
  {
822
 
    char *pluginhelperenv;
823
 
    bool bret = true;
824
 
    ret = asprintf(&pluginhelperenv, "MANDOSPLUGINHELPERDIR=%s",
825
 
                   pluginhelperdir != NULL ? pluginhelperdir : PHDIR);
826
 
    if(ret != -1){
827
 
      bret = add_environment(getplugin(NULL), pluginhelperenv, true);
828
 
    }
829
 
    if(ret == -1 or not bret){
830
 
      error(0, errno, "Failed to set MANDOSPLUGINHELPERDIR"
831
 
            " environment variable to \"%s\" for all plugins\n",
832
 
            pluginhelperdir != NULL ? pluginhelperdir : PHDIR);
833
 
    }
834
 
    if(ret != -1){
835
 
      free(pluginhelperenv);
836
 
    }
837
 
  }
838
 
  
839
764
  if(debug){
840
 
    for(plugin *p = plugin_list; p != NULL; p = p->next){
 
765
    for(plugin *p = plugin_list; p != NULL; p=p->next){
841
766
      fprintf(stderr, "Plugin: %s has %d arguments\n",
842
767
              p->name ? p->name : "Global", p->argc - 1);
843
768
      for(char **a = p->argv; *a != NULL; a++){
852
777
  
853
778
  if(getuid() == 0){
854
779
    /* Work around Debian bug #633582:
855
 
       <https://bugs.debian.org/633582> */
 
780
       <http://bugs.debian.org/633582> */
856
781
    int plugindir_fd = open(/* plugindir or */ PDIR, O_RDONLY);
857
782
    if(plugindir_fd == -1){
858
783
      if(errno != ENOENT){
870
795
          }
871
796
        }
872
797
      }
873
 
      close(plugindir_fd);
874
 
    }
875
 
 
876
 
    /* Work around Debian bug #981302
877
 
       <https://bugs.debian.org/981302> */
878
 
    if(lstat("/dev/fd", &st) != 0 and errno == ENOENT){
879
 
      ret = symlink("/proc/self/fd", "/dev/fd");
880
 
      if(ret == -1){
881
 
        error(0, errno, "Failed to create /dev/fd symlink");
882
 
      }
 
798
      TEMP_FAILURE_RETRY(close(plugindir_fd));
883
799
    }
884
800
  }
885
801
  
913
829
    ret = set_cloexec_flag(dir_fd);
914
830
    if(ret < 0){
915
831
      error(0, errno, "set_cloexec_flag");
 
832
      TEMP_FAILURE_RETRY(close(dir_fd));
916
833
      exitstatus = EX_OSERR;
917
834
      goto fallback;
918
835
    }
919
836
#endif  /* O_CLOEXEC */
 
837
    
 
838
    dir = fdopendir(dir_fd);
 
839
    if(dir == NULL){
 
840
      error(0, errno, "Could not open plugin dir");
 
841
      TEMP_FAILURE_RETRY(close(dir_fd));
 
842
      exitstatus = EX_OSERR;
 
843
      goto fallback;
 
844
    }
920
845
  }
921
846
  
922
 
  int good_name(const struct dirent * const dirent){
923
 
    const char * const patterns[] = { ".*", "#*#", "*~", "*.dpkg-new",
924
 
                                      "*.dpkg-old", "*.dpkg-bak",
925
 
                                      "*.dpkg-divert", NULL };
 
847
  FD_ZERO(&rfds_all);
 
848
  
 
849
  /* Read and execute any executable in the plugin directory*/
 
850
  while(true){
 
851
    do {
 
852
      dirst = readdir(dir);
 
853
    } while(dirst == NULL and errno == EINTR);
 
854
    
 
855
    /* All directory entries have been processed */
 
856
    if(dirst == NULL){
 
857
      if(errno == EBADF){
 
858
        error(0, errno, "readdir");
 
859
        exitstatus = EX_IOERR;
 
860
        goto fallback;
 
861
      }
 
862
      break;
 
863
    }
 
864
    
 
865
    /* Ignore dotfiles, backup files and other junk */
 
866
    {
 
867
      bool bad_name = false;
 
868
      const char * const patterns[] = { ".*", "#*#", "*~",
 
869
                                        "*.dpkg-new", "*.dpkg-old",
 
870
                                        "*.dpkg-bak", "*.dpkg-divert",
 
871
                                        NULL };
926
872
#ifdef __GNUC__
927
873
#pragma GCC diagnostic push
928
874
#pragma GCC diagnostic ignored "-Wcast-qual"
929
875
#endif
930
 
    for(const char **pat = (const char **)patterns;
931
 
        *pat != NULL; pat++){
 
876
      for(const char **pat = (const char **)patterns;
 
877
          *pat != NULL; pat++){
932
878
#ifdef __GNUC__
933
879
#pragma GCC diagnostic pop
934
880
#endif
935
 
      if(fnmatch(*pat, dirent->d_name, FNM_FILE_NAME | FNM_PERIOD)
936
 
         != FNM_NOMATCH){
937
 
        if(debug){
 
881
        if(fnmatch(*pat, dirst->d_name,
 
882
                   FNM_FILE_NAME | FNM_PERIOD) != FNM_NOMATCH){
 
883
          if(debug){
938
884
            fprintf(stderr, "Ignoring plugin dir entry \"%s\""
939
 
                    " matching pattern %s\n", dirent->d_name, *pat);
 
885
                    " matching pattern %s\n", dirst->d_name, *pat);
 
886
          }
 
887
          bad_name = true;
 
888
          break;
940
889
        }
941
 
        return 0;
 
890
      }
 
891
      if(bad_name){
 
892
        continue;
942
893
      }
943
894
    }
944
 
    return 1;
945
 
  }
946
 
  
947
 
  int numplugins = scandirat(dir_fd, ".", &direntries, good_name,
948
 
                             alphasort);
949
 
  if(numplugins == -1){
950
 
    error(0, errno, "Could not scan plugin dir");
951
 
    direntries = NULL;
952
 
    exitstatus = EX_OSERR;
953
 
    goto fallback;
954
 
  }
955
 
  
956
 
  FD_ZERO(&rfds_all);
957
 
  
958
 
  /* Read and execute any executable in the plugin directory*/
959
 
  for(int i = 0; i < numplugins; i++){
960
895
    
961
 
    int plugin_fd = openat(dir_fd, direntries[i]->d_name, O_RDONLY);
 
896
    int plugin_fd = openat(dir_fd, dirst->d_name, O_RDONLY |
 
897
#ifdef O_CLOEXEC
 
898
                            O_CLOEXEC
 
899
#else  /* not O_CLOEXEC */
 
900
                            0
 
901
#endif  /* not O_CLOEXEC */
 
902
                            );
962
903
    if(plugin_fd == -1){
963
904
      error(0, errno, "Could not open plugin");
964
 
      free(direntries[i]);
965
 
      continue;
966
 
    }
 
905
      continue;
 
906
    }
 
907
#ifndef O_CLOEXEC
 
908
  /* Set the FD_CLOEXEC flag on the plugin FD */
 
909
    ret = set_cloexec_flag(plugin_fd);
 
910
    if(ret < 0){
 
911
      error(0, errno, "set_cloexec_flag");
 
912
      TEMP_FAILURE_RETRY(close(plugin_fd));
 
913
      continue;
 
914
    }
 
915
#endif  /* O_CLOEXEC */
967
916
    ret = (int)TEMP_FAILURE_RETRY(fstat(plugin_fd, &st));
968
917
    if(ret == -1){
969
918
      error(0, errno, "stat");
970
 
      close(plugin_fd);
971
 
      free(direntries[i]);
 
919
      TEMP_FAILURE_RETRY(close(plugin_fd));
972
920
      continue;
973
921
    }
974
922
    
975
923
    /* Ignore non-executable files */
976
924
    if(not S_ISREG(st.st_mode)
977
 
       or (TEMP_FAILURE_RETRY(faccessat(dir_fd, direntries[i]->d_name,
978
 
                                        X_OK, 0)) != 0)){
 
925
       or (TEMP_FAILURE_RETRY(faccessat(dir_fd, dirst->d_name, X_OK,
 
926
                                        0)) != 0)){
979
927
      if(debug){
980
928
        fprintf(stderr, "Ignoring plugin dir entry \"%s/%s\""
981
929
                " with bad type or mode\n",
982
 
                plugindir != NULL ? plugindir : PDIR,
983
 
                direntries[i]->d_name);
 
930
                plugindir != NULL ? plugindir : PDIR, dirst->d_name);
984
931
      }
985
 
      close(plugin_fd);
986
 
      free(direntries[i]);
 
932
      TEMP_FAILURE_RETRY(close(plugin_fd));
987
933
      continue;
988
934
    }
989
935
    
990
 
    plugin *p = getplugin(direntries[i]->d_name);
 
936
    plugin *p = getplugin(dirst->d_name);
991
937
    if(p == NULL){
992
938
      error(0, errno, "getplugin");
993
 
      close(plugin_fd);
994
 
      free(direntries[i]);
 
939
      TEMP_FAILURE_RETRY(close(plugin_fd));
995
940
      continue;
996
941
    }
997
942
    if(p->disabled){
998
943
      if(debug){
999
944
        fprintf(stderr, "Ignoring disabled plugin \"%s\"\n",
1000
 
                direntries[i]->d_name);
 
945
                dirst->d_name);
1001
946
      }
1002
 
      close(plugin_fd);
1003
 
      free(direntries[i]);
 
947
      TEMP_FAILURE_RETRY(close(plugin_fd));
1004
948
      continue;
1005
949
    }
1006
950
    {
1031
975
    }
1032
976
    
1033
977
    int pipefd[2];
1034
 
#ifndef O_CLOEXEC
1035
978
    ret = (int)TEMP_FAILURE_RETRY(pipe(pipefd));
1036
 
#else  /* O_CLOEXEC */
1037
 
    ret = (int)TEMP_FAILURE_RETRY(pipe2(pipefd, O_CLOEXEC));
1038
 
#endif  /* O_CLOEXEC */
1039
979
    if(ret == -1){
1040
980
      error(0, errno, "pipe");
1041
981
      exitstatus = EX_OSERR;
1042
 
      free(direntries[i]);
1043
 
      goto fallback;
1044
 
    }
1045
 
    if(pipefd[0] >= FD_SETSIZE){
1046
 
      fprintf(stderr, "pipe()[0] (%d) >= FD_SETSIZE (%d)", pipefd[0],
1047
 
              FD_SETSIZE);
1048
 
      close(pipefd[0]);
1049
 
      close(pipefd[1]);
1050
 
      exitstatus = EX_OSERR;
1051
 
      free(direntries[i]);
1052
 
      goto fallback;
1053
 
    }
1054
 
#ifndef O_CLOEXEC
 
982
      goto fallback;
 
983
    }
1055
984
    /* Ask OS to automatic close the pipe on exec */
1056
985
    ret = set_cloexec_flag(pipefd[0]);
1057
986
    if(ret < 0){
1058
987
      error(0, errno, "set_cloexec_flag");
1059
 
      close(pipefd[0]);
1060
 
      close(pipefd[1]);
1061
988
      exitstatus = EX_OSERR;
1062
 
      free(direntries[i]);
1063
989
      goto fallback;
1064
990
    }
1065
991
    ret = set_cloexec_flag(pipefd[1]);
1066
992
    if(ret < 0){
1067
993
      error(0, errno, "set_cloexec_flag");
1068
 
      close(pipefd[0]);
1069
 
      close(pipefd[1]);
1070
994
      exitstatus = EX_OSERR;
1071
 
      free(direntries[i]);
1072
995
      goto fallback;
1073
996
    }
1074
 
#endif  /* not O_CLOEXEC */
1075
997
    /* Block SIGCHLD until process is safely in process list */
1076
998
    ret = (int)TEMP_FAILURE_RETRY(sigprocmask(SIG_BLOCK,
1077
999
                                              &sigchld_action.sa_mask,
1079
1001
    if(ret < 0){
1080
1002
      error(0, errno, "sigprocmask");
1081
1003
      exitstatus = EX_OSERR;
1082
 
      free(direntries[i]);
1083
1004
      goto fallback;
1084
1005
    }
1085
1006
    /* Starting a new process to be watched */
1089
1010
    } while(pid == -1 and errno == EINTR);
1090
1011
    if(pid == -1){
1091
1012
      error(0, errno, "fork");
1092
 
      TEMP_FAILURE_RETRY(sigprocmask(SIG_UNBLOCK,
1093
 
                                     &sigchld_action.sa_mask, NULL));
1094
 
      close(pipefd[0]);
1095
 
      close(pipefd[1]);
1096
1013
      exitstatus = EX_OSERR;
1097
 
      free(direntries[i]);
1098
1014
      goto fallback;
1099
1015
    }
1100
1016
    if(pid == 0){
1116
1032
        _exit(EX_OSERR);
1117
1033
      }
1118
1034
      
 
1035
      if(dirfd(dir) < 0){
 
1036
        /* If dir has no file descriptor, we could not set FD_CLOEXEC
 
1037
           above and must now close it manually here. */
 
1038
        closedir(dir);
 
1039
      }
1119
1040
      if(fexecve(plugin_fd, p->argv,
1120
1041
                (p->environ[0] != NULL) ? p->environ : environ) < 0){
1121
1042
        error(0, errno, "fexecve for %s/%s",
1122
 
              plugindir != NULL ? plugindir : PDIR,
1123
 
              direntries[i]->d_name);
 
1043
              plugindir != NULL ? plugindir : PDIR, dirst->d_name);
1124
1044
        _exit(EX_OSERR);
1125
1045
      }
1126
1046
      /* no return */
1127
1047
    }
1128
1048
    /* Parent process */
1129
 
    close(pipefd[1]);           /* Close unused write end of pipe */
1130
 
    close(plugin_fd);
1131
 
    plugin *new_plugin = getplugin(direntries[i]->d_name);
 
1049
    TEMP_FAILURE_RETRY(close(pipefd[1])); /* Close unused write end of
 
1050
                                             pipe */
 
1051
    TEMP_FAILURE_RETRY(close(plugin_fd));
 
1052
    plugin *new_plugin = getplugin(dirst->d_name);
1132
1053
    if(new_plugin == NULL){
1133
1054
      error(0, errno, "getplugin");
1134
1055
      ret = (int)(TEMP_FAILURE_RETRY
1138
1059
        error(0, errno, "sigprocmask");
1139
1060
      }
1140
1061
      exitstatus = EX_OSERR;
1141
 
      free(direntries[i]);
1142
1062
      goto fallback;
1143
1063
    }
1144
 
    free(direntries[i]);
1145
1064
    
1146
1065
    new_plugin->pid = pid;
1147
1066
    new_plugin->fd = pipefd[0];
1148
 
 
1149
 
    if(debug){
1150
 
      fprintf(stderr, "Plugin %s started (PID %" PRIdMAX ")\n",
1151
 
              new_plugin->name, (intmax_t) (new_plugin->pid));
1152
 
    }
1153
 
 
 
1067
    
1154
1068
    /* Unblock SIGCHLD so signal handler can be run if this process
1155
1069
       has already completed */
1156
1070
    ret = (int)TEMP_FAILURE_RETRY(sigprocmask(SIG_UNBLOCK,
1162
1076
      goto fallback;
1163
1077
    }
1164
1078
    
1165
 
    FD_SET(new_plugin->fd, &rfds_all);
 
1079
#if defined (__GNUC__) and defined (__GLIBC__)
 
1080
#if not __GLIBC_PREREQ(2, 16)
 
1081
#pragma GCC diagnostic push
 
1082
#pragma GCC diagnostic ignored "-Wsign-conversion"
 
1083
#endif
 
1084
#endif
 
1085
    FD_SET(new_plugin->fd, &rfds_all); /* Spurious warning from
 
1086
                                          -Wconversion in GNU libc
 
1087
                                          before 2.16 */
 
1088
#if defined (__GNUC__) and defined (__GLIBC__)
 
1089
#if not __GLIBC_PREREQ(2, 16)
 
1090
#pragma GCC diagnostic pop
 
1091
#endif
 
1092
#endif
1166
1093
    
1167
1094
    if(maxfd < new_plugin->fd){
1168
1095
      maxfd = new_plugin->fd;
1169
1096
    }
1170
1097
  }
1171
1098
  
1172
 
  free(direntries);
1173
 
  direntries = NULL;
1174
 
  close(dir_fd);
1175
 
  dir_fd = -1;
 
1099
  TEMP_FAILURE_RETRY(closedir(dir));
 
1100
  dir = NULL;
1176
1101
  free_plugin(getplugin(NULL));
1177
1102
  
1178
1103
  for(plugin *p = plugin_list; p != NULL; p = p->next){
1217
1142
                      (intmax_t) (proc->pid),
1218
1143
                      WTERMSIG(proc->status),
1219
1144
                      strsignal(WTERMSIG(proc->status)));
 
1145
            } else if(WCOREDUMP(proc->status)){
 
1146
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] dumped"
 
1147
                      " core\n", proc->name, (intmax_t) (proc->pid));
1220
1148
            }
1221
1149
          }
1222
1150
          
1223
1151
          /* Remove the plugin */
1224
 
          FD_CLR(proc->fd, &rfds_all);
 
1152
#if defined (__GNUC__) and defined (__GLIBC__)
 
1153
#if not __GLIBC_PREREQ(2, 16)
 
1154
#pragma GCC diagnostic push
 
1155
#pragma GCC diagnostic ignored "-Wsign-conversion"
 
1156
#endif
 
1157
#endif
 
1158
          FD_CLR(proc->fd, &rfds_all); /* Spurious warning from
 
1159
                                          -Wconversion in GNU libc
 
1160
                                          before 2.16 */
 
1161
#if defined (__GNUC__) and defined (__GLIBC__)
 
1162
#if not __GLIBC_PREREQ(2, 16)
 
1163
#pragma GCC diagnostic pop
 
1164
#endif
 
1165
#endif
1225
1166
          
1226
1167
          /* Block signal while modifying process_list */
1227
1168
          ret = (int)TEMP_FAILURE_RETRY(sigprocmask
1267
1208
      }
1268
1209
      
1269
1210
      /* This process has not completed.  Does it have any output? */
1270
 
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
 
1211
#if defined (__GNUC__) and defined (__GLIBC__)
 
1212
#if not __GLIBC_PREREQ(2, 16)
 
1213
#pragma GCC diagnostic push
 
1214
#pragma GCC diagnostic ignored "-Wsign-conversion"
 
1215
#endif
 
1216
#endif
 
1217
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){ /* Spurious
 
1218
                                                         warning from
 
1219
                                                         -Wconversion
 
1220
                                                         in GNU libc
 
1221
                                                         before
 
1222
                                                         2.16 */
 
1223
#if defined (__GNUC__) and defined (__GLIBC__)
 
1224
#if not __GLIBC_PREREQ(2, 16)
 
1225
#pragma GCC diagnostic pop
 
1226
#endif
 
1227
#endif
1271
1228
        /* This process had nothing to say at this time */
1272
1229
        proc = proc->next;
1273
1230
        continue;
1340
1297
    free(custom_argv);
1341
1298
  }
1342
1299
  
1343
 
  free(direntries);
1344
 
  
1345
 
  if(dir_fd != -1){
1346
 
    close(dir_fd);
 
1300
  if(dir != NULL){
 
1301
    closedir(dir);
1347
1302
  }
1348
1303
  
1349
1304
  /* Kill the processes */
1369
1324
  free_plugin_list();
1370
1325
  
1371
1326
  free(plugindir);
1372
 
  free(pluginhelperdir);
1373
1327
  free(argfile);
1374
1328
  
1375
1329
  return exitstatus;