module.c

Go to the documentation of this file.
00001 /*----------------------------------------------------------------------------
00002  secwatch - Copyright (C) 2006 Nic Stevens -- See COPYING for license details
00003 ------------------------------------------------------------------------------
00004  module.c - implement module loading and initialization 
00005 -----------------------------------------------------------------------------*/
00006 #ifdef HAVE_CONFIG_H
00007 #include "config.h"
00008 #endif
00009 #include <stdio.h>
00010 #include <string.h>
00011 #include <unistd.h>
00012 #include <stdlib.h>
00013 #include <dlfcn.h>
00014 #include <strutil.h>
00015 #include <configfile.h>
00016 #include <hooks.h>
00017 #include <log.h>
00018 
00019 typedef int (*modfn_t)(void *,int, char **);
00020 
00021 typedef struct _moditem {
00022      char * modname;
00023      char ** modargs;
00024      void * modhand;
00025      modfn_t entry;
00026 } moditem_t;
00027 
00028 moditem_t **mods = NULL;
00029 int modcnt = 0;
00030 
00035 void * moddata[] = {config,writeLog,inform,addLogHook,addLoopHook,NULL};
00036 
00042 int loadModule(char * aptr,char *ebuf, size_t ebsiz) {
00043      void *handle;
00044      modfn_t modfunc;
00045      char **argv, * modpath, *module, *error;
00046      int argc, mp;
00047      moditem_t x;
00048      
00049      if((argv = split(" ",aptr)) == NULL) 
00050           return -1;
00051      argc = count(argv);
00052      module = argv[0];
00053      if((handle = dlopen(module,RTLD_LAZY)) == NULL) {
00054           snprintf(ebuf,ebsiz,"module=%s, dlerror: %s",module,dlerror());
00055           return -1;
00056      }
00057      dlerror();
00058      *(void **)(&modfunc) = dlsym(handle,"secwmodinit");
00059      if((error = dlerror()) != NULL) {
00060           snprintf(ebuf,ebsiz,"module=%s, dlerror: %s",module,dlerror());
00061           return -1;
00062      }
00063      x.modname = module;
00064      x.modargs = argv;
00065      x.modhand = handle;
00066      x.entry = modfunc;
00067 
00068      mp = modcnt++;
00069      if((mods = realloc(mods,(sizeof(moditem_t *)*modcnt))) == NULL) {
00070           snprintf(ebuf,ebsiz,"Cannot load module %s: No space for module table",module);
00071           return -1;
00072      }
00073      if((mods[mp] = (moditem_t *) calloc(sizeof(moditem_t),1)) == NULL) {
00074           snprintf(ebuf,ebsiz,"Cannot load module %s: No space for module table entry",module);
00075           return -1;
00076      }
00077      *mods[mp] = x;
00078 
00079      modfunc(moddata,argc,argv);
00080      return 0;
00081 }
00082 
00083      

Generated on Tue Oct 31 10:17:23 2006 for secwatch by  doxygen 1.4.6