/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

merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
96
96
 
97
97
static plugin *plugin_list = NULL;
98
98
 
99
 
/* Gets a existing plugin based on name,
 
99
/* Gets an existing plugin based on name,
100
100
   or if none is found, creates a new one */
101
101
static plugin *getplugin(char *name){
102
102
  /* Check for exiting plugin with that name */
118
118
      return NULL;
119
119
    }
120
120
  }
121
 
 
 
121
  
122
122
  *new_plugin = (plugin) { .name = copy_name,
123
123
                           .argc = 1,
124
124
                           .disabled = false,
187
187
  size_t namelen = (size_t)(strchrnul(def, '=') - def);
188
188
  /* Search for this environment variable */
189
189
  for(char **e = p->environ; *e != NULL; e++){
190
 
    if(strncmp(*e, def, namelen+1) == 0){
 
190
    if(strncmp(*e, def, namelen + 1) == 0){
191
191
      /* It already exists */
192
192
      if(replace){
193
 
        char *new = realloc(*e, strlen(def));
 
193
        char *new = realloc(*e, strlen(def) + 1);
194
194
        if(new == NULL){
195
195
          return false;
196
196
        }
238
238
      /* No child processes */
239
239
      break;
240
240
    }
241
 
 
 
241
    
242
242
    /* A child exited, find it in process_list */
243
243
    while(proc != NULL and proc->pid != pid){
244
244
      proc = proc->next;
397
397
      if(arg == NULL){
398
398
        break;
399
399
      }
400
 
      {
401
 
        char *envdef = strdup(arg);
402
 
        if(envdef == NULL){
403
 
          break;
404
 
        }
405
 
        if(not add_environment(getplugin(NULL), envdef, true)){
406
 
          perror("add_environment");
407
 
        }
 
400
      if(not add_environment(getplugin(NULL), arg, true)){
 
401
        perror("add_environment");
408
402
      }
409
403
      break;
410
404
    case 'o':                   /* --options-for */
411
405
      if (arg != NULL){
412
406
        char *p_name = strsep(&arg, ":");
413
 
        if(p_name[0] == '\0'){
 
407
        if(p_name[0] == '\0' or arg == NULL){
414
408
          break;
415
409
        }
416
410
        char *opt = strsep(&arg, ":");
417
 
        if(opt[0] == '\0'){
 
411
        if(opt[0] == '\0' or opt == NULL){
418
412
          break;
419
413
        }
420
 
        if(opt != NULL){
421
 
          char *p;
422
 
          while((p = strsep(&opt, ",")) != NULL){
423
 
            if(p[0] == '\0'){
424
 
              continue;
425
 
            }
426
 
            if(not add_argument(getplugin(p_name), p)){
427
 
              perror("add_argument");
428
 
              return ARGP_ERR_UNKNOWN;
429
 
            }
 
414
        char *p;
 
415
        while((p = strsep(&opt, ",")) != NULL){
 
416
          if(p[0] == '\0'){
 
417
            continue;
 
418
          }
 
419
          if(not add_argument(getplugin(p_name), p)){
 
420
            perror("add_argument");
 
421
            return ARGP_ERR_UNKNOWN;
430
422
          }
431
423
        }
432
424
      }
440
432
        if(envdef == NULL){
441
433
          break;
442
434
        }
443
 
        char *p_name = strndup(arg, (size_t) (envdef-arg));
444
 
        if(p_name == NULL){
445
 
          break;
446
 
        }
447
 
        envdef++;
448
 
        if(not add_environment(getplugin(p_name), envdef, true)){
 
435
        *envdef = '\0';
 
436
        if(not add_environment(getplugin(arg), envdef+1, true)){
449
437
          perror("add_environment");
450
438
        }
451
439
      }
469
457
      }
470
458
      break;
471
459
    case 128:                   /* --plugin-dir */
 
460
      free(plugindir);
472
461
      plugindir = strdup(arg);
473
462
      if(plugindir == NULL){
474
463
        perror("strdup");
512
501
    case 128:                   /* --plugin-dir */
513
502
      break;
514
503
    case 129:                   /* --config-file */
 
504
      free(argfile);
515
505
      argfile = strdup(arg);
516
506
      if(argfile == NULL){
517
507
        perror("strdup");
942
932
    }
943
933
    /* OK, now either a process completed, or something can be read
944
934
       from one of them */
945
 
    for(plugin *proc = plugin_list; proc != NULL; proc = proc->next){
 
935
    for(plugin *proc = plugin_list; proc != NULL;){
946
936
      /* Is this process completely done? */
947
937
      if(proc->eof and proc->completed){
948
938
        /* Only accept the plugin output if it exited cleanly */
975
965
            exitstatus = EXIT_FAILURE;
976
966
            goto fallback;
977
967
          }
978
 
          free_plugin(proc);
 
968
          
979
969
          /* We are done modifying process list, so unblock signal */
980
970
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
981
971
                             NULL);
988
978
          if(plugin_list == NULL){
989
979
            break;
990
980
          }
 
981
          
 
982
          plugin *next_plugin = proc->next;
 
983
          free_plugin(proc);
 
984
          proc = next_plugin;
991
985
          continue;
992
986
        }
993
987
        
1005
999
      /* This process has not completed.  Does it have any output? */
1006
1000
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
1007
1001
        /* This process had nothing to say at this time */
 
1002
        proc = proc->next;
1008
1003
        continue;
1009
1004
      }
1010
1005
      /* Before reading, make the process' data buffer large enough */
1023
1018
                 BUFFER_SIZE);
1024
1019
      if(ret < 0){
1025
1020
        /* Read error from this process; ignore the error */
 
1021
        proc = proc->next;
1026
1022
        continue;
1027
1023
      }
1028
1024
      if(ret == 0){