/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:40:18 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090905174018-3w6q5uqk8ti5fqq6
* plugins.d/mandos-client.c (init_gpgme): Move variable "ret" into the
                                          innermost scope possible.
  (start_mandos_communication): Move variable "decrypted_buffer_size"
                                into the innermost scope possible.

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 = 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 = 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 TEMP_FAILURE_RETRY(fcntl(fd, F_SETFD, ret | FD_CLOEXEC));
 
222
  return fcntl(fd, F_SETFD, ret | FD_CLOEXEC);
240
223
}
241
224
 
242
225
 
634
617
        custom_argv[custom_argc] = NULL;
635
618
      }
636
619
    }
637
 
    do {
 
620
    do{
638
621
      ret = fclose(conffp);
639
 
    } while(ret == EOF and errno == EINTR);
 
622
    }while(ret == EOF and errno == EINTR);
640
623
    if(ret == EOF){
641
624
      perror("fclose");
642
625
      exitstatus = EXIT_FAILURE;
726
709
  
727
710
  /* Read and execute any executable in the plugin directory*/
728
711
  while(true){
729
 
    do {
730
 
      dirst = readdir(dir);
731
 
    } while(dirst == NULL and errno == EINTR);
 
712
    dirst = readdir(dir);
732
713
    
733
714
    /* All directory entries have been processed */
734
715
    if(dirst == NULL){
788
769
    
789
770
    char *filename;
790
771
    if(plugindir == NULL){
791
 
      ret = TEMP_FAILURE_RETRY(asprintf(&filename, PDIR "/%s",
792
 
                                        dirst->d_name));
 
772
      ret = asprintf(&filename, PDIR "/%s", dirst->d_name);
793
773
    } else {
794
 
      ret = TEMP_FAILURE_RETRY(asprintf(&filename, "%s/%s", plugindir,
795
 
                                        dirst->d_name));
 
774
      ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
796
775
    }
797
776
    if(ret < 0){
798
777
      perror("asprintf");
799
778
      continue;
800
779
    }
801
780
    
802
 
    ret = TEMP_FAILURE_RETRY(stat(filename, &st));
 
781
    ret = stat(filename, &st);
803
782
    if(ret == -1){
804
783
      perror("stat");
805
784
      free(filename);
807
786
    }
808
787
    
809
788
    /* Ignore non-executable files */
810
 
    if(not S_ISREG(st.st_mode)
811
 
       or (TEMP_FAILURE_RETRY(access(filename, X_OK)) != 0)){
 
789
    if(not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
812
790
      if(debug){
813
791
        fprintf(stderr, "Ignoring plugin dir entry \"%s\""
814
792
                " with bad type or mode\n", filename);
860
838
    }
861
839
    
862
840
    int pipefd[2];
863
 
    ret = TEMP_FAILURE_RETRY(pipe(pipefd));
 
841
    ret = pipe(pipefd);
864
842
    if(ret == -1){
865
843
      perror("pipe");
866
844
      exitstatus = EXIT_FAILURE;
880
858
      goto fallback;
881
859
    }
882
860
    /* Block SIGCHLD until process is safely in process list */
883
 
    ret = TEMP_FAILURE_RETRY(sigprocmask(SIG_BLOCK,
884
 
                                         &sigchld_action.sa_mask,
885
 
                                         NULL));
 
861
    ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
886
862
    if(ret < 0){
887
863
      perror("sigprocmask");
888
864
      exitstatus = EXIT_FAILURE;
889
865
      goto fallback;
890
866
    }
891
867
    /* Starting a new process to be watched */
892
 
    pid_t pid;
893
 
    do {
894
 
      pid = fork();
895
 
    } while(pid == -1 and errno == EINTR);
 
868
    pid_t pid = fork();
896
869
    if(pid == -1){
897
870
      perror("fork");
898
871
      exitstatus = EXIT_FAILURE;
936
909
      /* no return */
937
910
    }
938
911
    /* Parent process */
939
 
    TEMP_FAILURE_RETRY(close(pipefd[1])); /* Close unused write end of
940
 
                                             pipe */
 
912
    close(pipefd[1]);           /* Close unused write end of pipe */
941
913
    free(filename);
942
914
    plugin *new_plugin = getplugin(dirst->d_name);
943
915
    if(new_plugin == NULL){
944
916
      perror("getplugin");
945
 
      ret = TEMP_FAILURE_RETRY(sigprocmask(SIG_UNBLOCK,
946
 
                                           &sigchld_action.sa_mask,
947
 
                                           NULL));
 
917
      ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
948
918
      if(ret < 0){
949
919
        perror("sigprocmask");
950
920
      }
957
927
    
958
928
    /* Unblock SIGCHLD so signal handler can be run if this process
959
929
       has already completed */
960
 
    ret = TEMP_FAILURE_RETRY(sigprocmask(SIG_UNBLOCK,
961
 
                                         &sigchld_action.sa_mask,
962
 
                                         NULL));
 
930
    ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
963
931
    if(ret < 0){
964
932
      perror("sigprocmask");
965
933
      exitstatus = EXIT_FAILURE;
973
941
    }
974
942
  }
975
943
  
976
 
  TEMP_FAILURE_RETRY(closedir(dir));
 
944
  closedir(dir);
977
945
  dir = NULL;
978
946
  free_plugin(getplugin(NULL));
979
947
  
992
960
  while(plugin_list){
993
961
    fd_set rfds = rfds_all;
994
962
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
995
 
    if(select_ret == -1 and errno != EINTR){
 
963
    if(select_ret == -1){
996
964
      perror("select");
997
965
      exitstatus = EXIT_FAILURE;
998
966
      goto fallback;
1029
997
          FD_CLR(proc->fd, &rfds_all);
1030
998
          
1031
999
          /* Block signal while modifying process_list */
1032
 
          ret = TEMP_FAILURE_RETRY(sigprocmask(SIG_BLOCK,
1033
 
                                               &sigchld_action.sa_mask,
1034
 
                                               NULL));
 
1000
          ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
1035
1001
          if(ret < 0){
1036
1002
            perror("sigprocmask");
1037
1003
            exitstatus = EXIT_FAILURE;
1043
1009
          proc = next_plugin;
1044
1010
          
1045
1011
          /* We are done modifying process list, so unblock signal */
1046
 
          ret = TEMP_FAILURE_RETRY(sigprocmask(SIG_UNBLOCK,
1047
 
                                               &sigchld_action.sa_mask,
1048
 
                                               NULL));
 
1012
          ret = sigprocmask(SIG_UNBLOCK, &sigchld_action.sa_mask,
 
1013
                            NULL);
1049
1014
          if(ret < 0){
1050
1015
            perror("sigprocmask");
1051
1016
            exitstatus = EXIT_FAILURE;
1088
1053
        proc->buffer_size += BUFFER_SIZE;
1089
1054
      }
1090
1055
      /* Read from the process */
1091
 
      sret = TEMP_FAILURE_RETRY(read(proc->fd,
1092
 
                                     proc->buffer
1093
 
                                     + proc->buffer_length,
1094
 
                                     BUFFER_SIZE));
 
1056
      sret = read(proc->fd, proc->buffer + proc->buffer_length,
 
1057
                  BUFFER_SIZE);
1095
1058
      if(sret < 0){
1096
1059
        /* Read error from this process; ignore the error */
1097
1060
        proc = proc->next;
1159
1122
  }
1160
1123
  
1161
1124
  /* Wait for any remaining child processes to terminate */
1162
 
  do {
 
1125
  do{
1163
1126
    ret = wait(NULL);
1164
1127
  } while(ret >= 0);
1165
1128
  if(errno != ECHILD){