/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-09-05 17:39:34 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090905173934-gs0r858omhudc71m
* plugin-runner.c (main): Move variables "tmpmax" and "tmp" into
                          the innermost scope possible.

Show diffs side-by-side

added added

removed removed

Lines of Context:
54
54
#include <fcntl.h>              /* fcntl(), F_GETFD, F_SETFD,
55
55
                                   FD_CLOEXEC */
56
56
#include <string.h>             /* strsep, strlen(), asprintf(),
57
 
                                   strsignal(), strcmp(), strncmp() */
 
57
                                   strsignal() */
58
58
#include <errno.h>              /* errno */
59
59
#include <argp.h>               /* struct argp_option, struct
60
60
                                   argp_state, struct argp,
110
110
    }
111
111
  }
112
112
  /* Create a new plugin */
113
 
  plugin *new_plugin = NULL;
114
 
  do {
115
 
    new_plugin = malloc(sizeof(plugin));
116
 
  } while(new_plugin == NULL and errno == EINTR);
 
113
  plugin *new_plugin = malloc(sizeof(plugin));
117
114
  if(new_plugin == NULL){
118
115
    return NULL;
119
116
  }
120
117
  char *copy_name = NULL;
121
118
  if(name != NULL){
122
 
    do {
123
 
      copy_name = strdup(name);
124
 
    } while(copy_name == NULL and errno == EINTR);
 
119
    copy_name = strdup(name);
125
120
    if(copy_name == NULL){
126
121
      free(new_plugin);
127
122
      return NULL;
133
128
                          .disabled = false,
134
129
                          .next = plugin_list };
135
130
  
136
 
  do {
137
 
    new_plugin->argv = malloc(sizeof(char *) * 2);
138
 
  } while(new_plugin->argv == NULL and errno == EINTR);
 
131
  new_plugin->argv = malloc(sizeof(char *) * 2);
139
132
  if(new_plugin->argv == NULL){
140
133
    free(copy_name);
141
134
    free(new_plugin);
144
137
  new_plugin->argv[0] = copy_name;
145
138
  new_plugin->argv[1] = NULL;
146
139
  
147
 
  do {
148
 
    new_plugin->environ = malloc(sizeof(char *));
149
 
  } while(new_plugin->environ == NULL and errno == EINTR);
 
140
  new_plugin->environ = malloc(sizeof(char *));
150
141
  if(new_plugin->environ == NULL){
151
142
    free(copy_name);
152
143
    free(new_plugin->argv);
164
155
static bool add_to_char_array(const char *new, char ***array,
165
156
                              int *len){
166
157
  /* Resize the pointed-to array to hold one more pointer */
167
 
  do {
168
 
    *array = realloc(*array, sizeof(char *)
169
 
                     * (size_t) ((*len) + 2));
170
 
  } while(*array == NULL and errno == EINTR);
 
158
  *array = realloc(*array, sizeof(char *)
 
159
                   * (size_t) ((*len) + 2));
171
160
  /* Malloc check */
172
161
  if(*array == NULL){
173
162
    return false;
174
163
  }
175
164
  /* Make a copy of the new string */
176
 
  char *copy;
177
 
  do {
178
 
    copy = strdup(new);
179
 
  } while(copy == NULL and errno == EINTR);
 
165
  char *copy = strdup(new);
180
166
  if(copy == NULL){
181
167
    return false;
182
168
  }
208
194
    if(strncmp(*e, def, namelen + 1) == 0){
209
195
      /* It already exists */
210
196
      if(replace){
211
 
        char *new;
212
 
        do {
213
 
          new = realloc(*e, strlen(def) + 1);
214
 
        } while(new == NULL and errno == EINTR);
 
197
        char *new = realloc(*e, strlen(def) + 1);
215
198
        if(new == NULL){
216
199
          return false;
217
200
        }
230
213
 | [[info:libc:Descriptor%20Flags][File Descriptor Flags]] |
231
214
 */
232
215
static int set_cloexec_flag(int fd){
233
 
  int ret = (int)TEMP_FAILURE_RETRY(fcntl(fd, F_GETFD, 0));
 
216
  int ret = fcntl(fd, F_GETFD, 0);
234
217
  /* If reading the flags failed, return error indication now. */
235
218
  if(ret < 0){
236
219
    return ret;
237
220
  }
238
221
  /* Store modified flag word in the descriptor. */
239
 
  return (int)TEMP_FAILURE_RETRY(fcntl(fd, F_SETFD,
240
 
                                       ret | FD_CLOEXEC));
 
222
  return fcntl(fd, F_SETFD, ret | FD_CLOEXEC);
241
223
}
242
224
 
243
225
 
635
617
        custom_argv[custom_argc] = NULL;
636
618
      }
637
619
    }
638
 
    do {
 
620
    do{
639
621
      ret = fclose(conffp);
640
 
    } while(ret == EOF and errno == EINTR);
 
622
    }while(ret == EOF and errno == EINTR);
641
623
    if(ret == EOF){
642
624
      perror("fclose");
643
625
      exitstatus = EXIT_FAILURE;
727
709
  
728
710
  /* Read and execute any executable in the plugin directory*/
729
711
  while(true){
730
 
    do {
731
 
      dirst = readdir(dir);
732
 
    } while(dirst == NULL and errno == EINTR);
 
712
    dirst = readdir(dir);
733
713
    
734
714
    /* All directory entries have been processed */
735
715
    if(dirst == NULL){
771
751
      for(const char **suf = bad_suffixes; *suf != NULL; suf++){
772
752
        size_t suf_len = strlen(*suf);
773
753
        if((d_name_len >= suf_len)
774
 
           and (strcmp((dirst->d_name) + d_name_len-suf_len, *suf)
 
754
           and (strcmp((dirst->d_name)+d_name_len-suf_len, *suf)
775
755
                == 0)){
776
756
          if(debug){
777
757
            fprintf(stderr, "Ignoring plugin dir entry \"%s\""
789
769
    
790
770
    char *filename;
791
771
    if(plugindir == NULL){
792
 
      ret = (int)TEMP_FAILURE_RETRY(asprintf(&filename, PDIR "/%s",
793
 
                                             dirst->d_name));
 
772
      ret = asprintf(&filename, PDIR "/%s", dirst->d_name);
794
773
    } else {
795
 
      ret = (int)TEMP_FAILURE_RETRY(asprintf(&filename, "%s/%s",
796
 
                                             plugindir,
797
 
                                             dirst->d_name));
 
774
      ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
798
775
    }
799
776
    if(ret < 0){
800
777
      perror("asprintf");
801
778
      continue;
802
779
    }
803
780
    
804
 
    ret = (int)TEMP_FAILURE_RETRY(stat(filename, &st));
 
781
    ret = stat(filename, &st);
805
782
    if(ret == -1){
806
783
      perror("stat");
807
784
      free(filename);
809
786
    }
810
787
    
811
788
    /* Ignore non-executable files */
812
 
    if(not S_ISREG(st.st_mode)
813
 
       or (TEMP_FAILURE_RETRY(access(filename, X_OK)) != 0)){
 
789
    if(not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
814
790
      if(debug){
815
791
        fprintf(stderr, "Ignoring plugin dir entry \"%s\""
816
792
                " with bad type or mode\n", filename);
862
838
    }
863
839
    
864
840
    int pipefd[2];
865
 
    ret = (int)TEMP_FAILURE_RETRY(pipe(pipefd));
 
841
    ret = pipe(pipefd);
866
842
    if(ret == -1){
867
843
      perror("pipe");
868
844
      exitstatus = EXIT_FAILURE;
882
858
      goto fallback;
883
859
    }
884
860
    /* Block SIGCHLD until process is safely in process list */
885
 
    ret = (int)TEMP_FAILURE_RETRY(sigprocmask(SIG_BLOCK,
886
 
                                              &sigchld_action.sa_mask,
887
 
                                              NULL));
 
861
    ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
888
862
    if(ret < 0){
889
863
      perror("sigprocmask");
890
864
      exitstatus = EXIT_FAILURE;
891
865
      goto fallback;
892
866
    }
893
867
    /* Starting a new process to be watched */
894
 
    pid_t pid;
895
 
    do {
896
 
      pid = fork();
897
 
    } while(pid == -1 and errno == EINTR);
 
868
    pid_t pid = fork();
898
869
    if(pid == -1){
899
870
      perror("fork");
900
871
      exitstatus = EXIT_FAILURE;
938
909
      /* no return */
939
910
    }
940
911
    /* Parent process */
941
 
    TEMP_FAILURE_RETRY(close(pipefd[1])); /* Close unused write end of
942
 
                                             pipe */
 
912
    close(pipefd[1]);           /* Close unused write end of pipe */
943
913
    free(filename);
944
914
    plugin *new_plugin = getplugin(dirst->d_name);
945
915
    if(new_plugin == NULL){
946
916
      perror("getplugin");
947
 
      ret = (int)(TEMP_FAILURE_RETRY
948
 
                  (sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
949
 
                               NULL)));
 
917
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
950
918
      if(ret < 0){
951
919
        perror("sigprocmask");
952
920
      }
959
927
    
960
928
    /* Unblock SIGCHLD so signal handler can be run if this process
961
929
       has already completed */
962
 
    ret = (int)TEMP_FAILURE_RETRY(sigprocmask(SIG_UNBLOCK,
963
 
                                              &sigchld_action.sa_mask,
964
 
                                              NULL));
 
930
    ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
965
931
    if(ret < 0){
966
932
      perror("sigprocmask");
967
933
      exitstatus = EXIT_FAILURE;
968
934
      goto fallback;
969
935
    }
970
936
    
971
 
    FD_SET(new_plugin->fd, &rfds_all); /* Spurious warning from
972
 
                                          -Wconversion */
 
937
    FD_SET(new_plugin->fd, &rfds_all);
973
938
    
974
939
    if(maxfd < new_plugin->fd){
975
940
      maxfd = new_plugin->fd;
976
941
    }
977
942
  }
978
943
  
979
 
  TEMP_FAILURE_RETRY(closedir(dir));
 
944
  closedir(dir);
980
945
  dir = NULL;
981
946
  free_plugin(getplugin(NULL));
982
947
  
995
960
  while(plugin_list){
996
961
    fd_set rfds = rfds_all;
997
962
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
998
 
    if(select_ret == -1 and errno != EINTR){
 
963
    if(select_ret == -1){
999
964
      perror("select");
1000
965
      exitstatus = EXIT_FAILURE;
1001
966
      goto fallback;
1029
994
          }
1030
995
          
1031
996
          /* Remove the plugin */
1032
 
          FD_CLR(proc->fd, &rfds_all); /* Spurious warning from
1033
 
                                          -Wconversion */
 
997
          FD_CLR(proc->fd, &rfds_all);
1034
998
          
1035
999
          /* Block signal while modifying process_list */
1036
 
          ret = (int)TEMP_FAILURE_RETRY(sigprocmask
1037
 
                                        (SIG_BLOCK,
1038
 
                                         &sigchld_action.sa_mask,
1039
 
                                         NULL));
 
1000
          ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
1040
1001
          if(ret < 0){
1041
1002
            perror("sigprocmask");
1042
1003
            exitstatus = EXIT_FAILURE;
1048
1009
          proc = next_plugin;
1049
1010
          
1050
1011
          /* We are done modifying process list, so unblock signal */
1051
 
          ret = (int)(TEMP_FAILURE_RETRY
1052
 
                      (sigprocmask(SIG_UNBLOCK,
1053
 
                                   &sigchld_action.sa_mask, NULL)));
 
1012
          ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
 
1013
                            NULL);
1054
1014
          if(ret < 0){
1055
1015
            perror("sigprocmask");
1056
1016
            exitstatus = EXIT_FAILURE;
1076
1036
      }
1077
1037
      
1078
1038
      /* This process has not completed.  Does it have any output? */
1079
 
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){ /* Spurious
1080
 
                                                         warning from
1081
 
                                                         -Wconversion */
 
1039
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
1082
1040
        /* This process had nothing to say at this time */
1083
1041
        proc = proc->next;
1084
1042
        continue;
1095
1053
        proc->buffer_size += BUFFER_SIZE;
1096
1054
      }
1097
1055
      /* Read from the process */
1098
 
      sret = TEMP_FAILURE_RETRY(read(proc->fd,
1099
 
                                     proc->buffer
1100
 
                                     + proc->buffer_length,
1101
 
                                     BUFFER_SIZE));
 
1056
      sret = read(proc->fd, proc->buffer + proc->buffer_length,
 
1057
                  BUFFER_SIZE);
1102
1058
      if(sret < 0){
1103
1059
        /* Read error from this process; ignore the error */
1104
1060
        proc = proc->next;
1166
1122
  }
1167
1123
  
1168
1124
  /* Wait for any remaining child processes to terminate */
1169
 
  do {
 
1125
  do{
1170
1126
    ret = wait(NULL);
1171
1127
  } while(ret >= 0);
1172
1128
  if(errno != ECHILD){