/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-02-09 19:26:19 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090209192619-7rse82x9pypq4ihk
* plugin-runner.c: Comment change.

* plugins.d/mandos-client.c: Comment changes.
  (quit_now): New global flag.
  (handle_sigterm): Only call avahi_simple_poll_quit once.
  (main): Also handle SIGINT and SIGHUP.

Show diffs side-by-side

added added

removed removed

Lines of Context:
62
62
#include <signal.h>             /* struct sigaction, sigemptyset(),
63
63
                                   sigaddset(), sigaction(),
64
64
                                   sigprocmask(), SIG_BLOCK, SIGCHLD,
65
 
                                   SIG_UNBLOCK, kill() */
 
65
                                   SIG_UNBLOCK, kill(), sig_atomic_t
 
66
                                */
66
67
#include <errno.h>              /* errno, EBADF */
67
68
#include <inttypes.h>           /* intmax_t, SCNdMAX, PRIdMAX,  */
68
69
 
89
90
  size_t buffer_size;
90
91
  size_t buffer_length;
91
92
  bool eof;
92
 
  volatile bool completed;
93
 
  volatile int status;
 
93
  volatile sig_atomic_t completed;
 
94
  int status;
94
95
  struct plugin *next;
95
96
} plugin;
96
97
 
223
224
/* Mark processes as completed when they exit, and save their exit
224
225
   status. */
225
226
static void handle_sigchld(__attribute__((unused)) int sig){
 
227
  int old_errno = errno;
226
228
  while(true){
227
229
    plugin *proc = plugin_list;
228
230
    int status;
248
250
      continue;
249
251
    }
250
252
    proc->status = status;
251
 
    proc->completed = true;
 
253
    proc->completed = 1;
252
254
  }
 
255
  errno = old_errno;
253
256
}
254
257
 
255
258
/* Prints out a password to stdout */
380
383
    switch(key){
381
384
    case 'g':                   /* --global-options */
382
385
      if(arg != NULL){
383
 
        char *p;
384
 
        while((p = strsep(&arg, ",")) != NULL){
385
 
          if(p[0] == '\0'){
 
386
        char *plugin_option;
 
387
        while((plugin_option = strsep(&arg, ",")) != NULL){
 
388
          if(plugin_option[0] == '\0'){
386
389
            continue;
387
390
          }
388
 
          if(not add_argument(getplugin(NULL), p)){
 
391
          if(not add_argument(getplugin(NULL), plugin_option)){
389
392
            perror("add_argument");
390
393
            return ARGP_ERR_UNKNOWN;
391
394
          }
402
405
      break;
403
406
    case 'o':                   /* --options-for */
404
407
      if(arg != NULL){
405
 
        char *p_name = strsep(&arg, ":");
406
 
        if(p_name[0] == '\0' or arg == NULL){
407
 
          break;
408
 
        }
409
 
        char *opt = strsep(&arg, ":");
410
 
        if(opt[0] == '\0' or opt == NULL){
411
 
          break;
412
 
        }
413
 
        char *p;
414
 
        while((p = strsep(&opt, ",")) != NULL){
415
 
          if(p[0] == '\0'){
416
 
            continue;
417
 
          }
418
 
          if(not add_argument(getplugin(p_name), p)){
 
408
        char *plugin_name = strsep(&arg, ":");
 
409
        if(plugin_name[0] == '\0'){
 
410
          break;
 
411
        }
 
412
        char *plugin_option;
 
413
        while((plugin_option = strsep(&arg, ",")) != NULL){
 
414
          if(not add_argument(getplugin(plugin_name), plugin_option)){
419
415
            perror("add_argument");
420
416
            return ARGP_ERR_UNKNOWN;
421
417
          }
662
658
  }
663
659
  
664
660
  /* Strip permissions down to nobody */
 
661
  setgid(gid);
 
662
  if(ret == -1){
 
663
    perror("setgid");
 
664
  }
665
665
  ret = setuid(uid);
666
666
  if(ret == -1){
667
667
    perror("setuid");
668
 
  }  
669
 
  setgid(gid);
670
 
  if(ret == -1){
671
 
    perror("setgid");
672
668
  }
673
669
  
674
670
  if(plugindir == NULL){
934
930
  
935
931
  closedir(dir);
936
932
  dir = NULL;
 
933
  free_plugin(getplugin(NULL));
937
934
  
938
935
  for(plugin *p = plugin_list; p != NULL; p = p->next){
939
936
    if(p->pid != 0){
959
956
       from one of them */
960
957
    for(plugin *proc = plugin_list; proc != NULL;){
961
958
      /* Is this process completely done? */
962
 
      if(proc->eof and proc->completed){
 
959
      if(proc->completed and proc->eof){
963
960
        /* Only accept the plugin output if it exited cleanly */
964
961
        if(not WIFEXITED(proc->status)
965
962
           or WEXITSTATUS(proc->status) != 0){
967
964
 
968
965
          if(debug){
969
966
            if(WIFEXITED(proc->status)){
970
 
              fprintf(stderr, "Plugin %" PRIdMAX " exited with status"
971
 
                      " %d\n", (intmax_t) (proc->pid),
 
967
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] exited with"
 
968
                      " status %d\n", proc->name,
 
969
                      (intmax_t) (proc->pid),
972
970
                      WEXITSTATUS(proc->status));
973
971
            } else if(WIFSIGNALED(proc->status)){
974
 
              fprintf(stderr, "Plugin %" PRIdMAX " killed by signal"
975
 
                      " %d\n", (intmax_t) (proc->pid),
 
972
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] killed by"
 
973
                      " signal %d\n", proc->name,
 
974
                      (intmax_t) (proc->pid),
976
975
                      WTERMSIG(proc->status));
977
976
            } else if(WCOREDUMP(proc->status)){
978
 
              fprintf(stderr, "Plugin %" PRIdMAX " dumped core\n",
979
 
                      (intmax_t) (proc->pid));
 
977
              fprintf(stderr, "Plugin %s [%" PRIdMAX "] dumped"
 
978
                      " core\n", proc->name, (intmax_t) (proc->pid));
980
979
            }
981
980
          }
982
981