/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

  • Committer: Teddy Hogeborn
  • Date: 2009-09-17 11:47:22 UTC
  • mfrom: (237.2.138 mandos)
  • Revision ID: teddy@fukt.bsnet.se-20090917114722-azcwe7sdowxc95ba
Merge from trunk.  Lots of bug fixes, including Debian bug #546928.

Show diffs side-by-side

added added

removed removed

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