/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

plugin-runner
        Freeing up memory during run-time.

Show diffs side-by-side

added added

removed removed

Lines of Context:
105
105
  if (new_plugin == NULL){
106
106
    return NULL;
107
107
  }
108
 
  char *copy_name = NULL;
109
 
  if(name != NULL){
110
 
    copy_name = strdup(name);
111
 
    if(copy_name == NULL){
112
 
      return NULL;
113
 
    }
 
108
  char *copy_name = strdup(name);
 
109
  if(copy_name == NULL){
 
110
    return NULL;
114
111
  }
115
112
  
116
113
  *new_plugin = (plugin) { .name = copy_name,
121
118
  
122
119
  new_plugin->argv = malloc(sizeof(char *) * 2);
123
120
  if (new_plugin->argv == NULL){
124
 
    free(copy_name);
125
121
    free(new_plugin);
126
122
    return NULL;
127
123
  }
130
126
 
131
127
  new_plugin->environ = malloc(sizeof(char *));
132
128
  if(new_plugin->environ == NULL){
133
 
    free(copy_name);
134
129
    free(new_plugin->argv);
135
130
    free(new_plugin);
136
131
    return NULL;
199
194
 
200
195
process *process_list = NULL;
201
196
 
202
 
/* Mark processes as completed when it exits, and save its exit
 
197
/* Mark a process as completed when it exits, and save its exit
203
198
   status. */
204
199
void handle_sigchld(__attribute__((unused)) int sig){
205
200
  process *proc = process_list;
206
 
  while(true){
207
 
    int status;
208
 
    pid_t pid = waitpid(-1, &status, WNOHANG);
209
 
    if(pid == 0){
210
 
      break;
211
 
    }
212
 
    if(pid == -1){
213
 
      if (errno != ECHILD){
214
 
        perror("waitpid");
215
 
      }
216
 
      return;
217
 
    }
218
 
 
219
 
    while(proc != NULL and proc->pid != pid){
220
 
      proc = proc->next;
221
 
    }
222
 
    if(proc == NULL){
223
 
      /* Process not found in process list */
224
 
      continue;
225
 
    }
226
 
    proc->status = status;
227
 
    proc->completed = true;
228
 
  }
 
201
  int status;
 
202
  pid_t pid = wait(&status);
 
203
  if(pid == -1){
 
204
    perror("wait");
 
205
    return;
 
206
  }
 
207
  while(proc != NULL and proc->pid != pid){
 
208
    proc = proc->next;
 
209
  }
 
210
  if(proc == NULL){
 
211
    /* Process not found in process list */
 
212
    return;
 
213
  }
 
214
  proc->status = status;
 
215
  proc->completed = true;
229
216
}
230
217
 
231
218
bool print_out_password(const char *buffer, size_t length){
260
247
    return NULL;
261
248
  }
262
249
  argv[*argc-1] = arg;
263
 
  argv[*argc] = NULL;
 
250
  argv[*argc] = NULL;   
264
251
  return argv;
265
252
}
266
253
 
267
254
static void free_plugin_list(plugin *plugin_list){
268
 
  for(plugin *next; plugin_list != NULL; plugin_list = next){
 
255
  for(plugin *next = plugin_list; plugin_list != NULL; plugin_list = next){
269
256
    next = plugin_list->next;
 
257
    free(plugin_list->name);
270
258
    for(char **arg = plugin_list->argv; *arg != NULL; arg++){
271
259
      free(*arg);
272
 
    }
 
260
    }    
273
261
    free(plugin_list->argv);
274
262
    for(char **env = plugin_list->environ; *env != NULL; env++){
275
263
      free(*env);
276
264
    }
277
265
    free(plugin_list->environ);
278
266
    free(plugin_list);
279
 
  }
 
267
  }  
280
268
}
281
269
 
282
270
int main(int argc, char *argv[]){
310
298
  ret = sigaction(SIGCHLD, &sigchld_action, &old_sigchld_action);
311
299
  if(ret == -1){
312
300
    perror("sigaction");
313
 
    exitstatus = EXIT_FAILURE;
 
301
    exitstatus = EXIT_FAILURE;    
314
302
    goto fallback;
315
303
  }
316
304
  
692
680
      }
693
681
    }
694
682
    
695
 
    int pipefd[2];
 
683
    int pipefd[2]; 
696
684
    ret = pipe(pipefd);
697
685
    if (ret == -1){
698
686
      perror("pipe");
799
787
  }
800
788
  
801
789
  free_plugin_list(plugin_list);
802
 
  plugin_list = NULL;
803
790
  
804
791
  closedir(dir);
805
792
  dir = NULL;