]> jfr.im git - irc/quakenet/newserv.git/blobdiff - core/modules.c
CHANSERV: remove E type escapes
[irc/quakenet/newserv.git] / core / modules.c
index af40bdc4ed8abc433895623e0b4a4ba9724c96c7..6bdd7a2ef100bf56d52694794e20687156608e9f 100644 (file)
@@ -11,6 +11,8 @@
 #include "../lib/sstring.h"
 #include "../lib/irc_string.h"
 #include "../lib/splitline.h"
+#include "../lib/strlfunc.h"
+#include "../lib/valgrind.h"
 #include "config.h"
 #include "error.h"
 #include <stdio.h>
@@ -21,7 +23,7 @@
 
 #define DEPFILE        "modules.dep"
 
-#define MAXMODULES 100
+#define MAXMODULES 200
 
 /* Module dependency stuff.
  *
@@ -70,6 +72,7 @@ void clearmoduledeps() {
   unsigned int i;
   
   for (i=0;i<knownmodules;i++) {
+    freesstring(moduledeps[i].name);
     if (moduledeps[i].parents) {
       free(moduledeps[i].parents);
       moduledeps[i].parents=NULL;
@@ -137,6 +140,7 @@ void initmoduledeps() {
         Error("core",ERR_ERROR,
                "Too many modules in dependency file; rebuild with higher MAXMODULES.  Module dependencies disabled.\n");
         clearmoduledeps();
+        fclose(fp);
         return;
       }
       
@@ -153,11 +157,14 @@ void initmoduledeps() {
           Error("core",ERR_WARNING,"Couldn't find parent module %s of %s.  Module dependencies disabled.",
                   largv[i+1],largv[0]);
           clearmoduledeps();
+          fclose(fp);
           return;
         }
         mdp->parents[i]->numchildren++; /* break the bad news */
       }
     }
+
+    fclose(fp);
     
     /* Second pass */
     for (i=0;i<knownmodules;i++) {
@@ -220,59 +227,62 @@ void initmodules() {
 int insmod(char *modulename) {
   int i;
   module *mods;
-  char buf[1024];
-  const char *(*verinfo)(void);
+  char buf[1024], modulebuf[1024];
+  const char *(*verinfo)(const char **);
   struct module_dep *mdp;
 
-  delchars(modulename,"./\\;");
+  strlcpy(modulebuf, modulename, sizeof(modulebuf));
+  delchars(modulebuf,"./\\;");
   
-  if (isloaded(modulename)) {
-    Error("core",ERR_DEBUG,"Tried to load already loaded module: %s",modulename);
+  if (isloaded(modulebuf)) {
+    Error("core",ERR_DEBUG,"Tried to load already loaded module: %s",modulebuf);
     return 1;
   }
   
-  if (strlen(modulename)>100) {
-    Error("core",ERR_WARNING,"Module name too long: %s",modulename);  
+  if (strlen(modulebuf)>100) {
+    Error("core",ERR_WARNING,"Module name too long: %s",modulebuf);  
     return 1;
   }
 
-  if ((mdp=getmoduledep(modulename))) {
+  if ((mdp=getmoduledep(modulebuf))) {
     for (i=0;i<mdp->numparents;i++) {
       if (!isloaded(mdp->parents[i]->name->content)) {
         if (insmod(mdp->parents[i]->name->content)) {
           Error("core",ERR_WARNING,"Error loading dependant module %s (needed by %s)",
-                   mdp->parents[i]->name->content,modulename);
+                   mdp->parents[i]->name->content,modulebuf);
           return 1;
         }
       }
     }
   } else {
-    Error("core",ERR_WARNING,"Loading module %s without dependency information.",modulename);
+    Error("core",ERR_WARNING,"Loading module %s without dependency information.",modulebuf);
   }
 
   i=array_getfreeslot(&modules);
   mods=(module *)(modules.content);
 
-  sprintf(buf,"%s/%s%s",moddir->content,modulename,modsuffix->content);
+  sprintf(buf,"%s/%s%s",moddir->content,modulebuf,modsuffix->content);
   
   mods[i].handle=dlopen(buf,RTLD_NOW|RTLD_GLOBAL);
   
   if(mods[i].handle==NULL) {
-    Error("core",ERR_ERROR,"Loading module %s failed: %s",modulename,dlerror());
+    Error("core",ERR_ERROR,"Loading module %s failed: %s",modulebuf,dlerror());
     array_delslot(&modules,i);
     return -1;
   }
 
-  mods[i].name=getsstring(modulename,MODULENAMELEN);
+  mods[i].name=getsstring(modulebuf,MODULENAMELEN);
 
   verinfo=dlsym(mods[i].handle,"_version");
   if(verinfo) {
-    mods[i].version=verinfo();
-  }  else {
+    mods[i].buildid=verinfo(&mods[i].version);
+  } else {
     mods[i].version=NULL;
+    mods[i].buildid=NULL;
   }
 
-  Error("core",ERR_INFO,"Loaded module %s OK.",modulename);
+  mods[i].loadedsince = time(NULL);
+  Error("core",ERR_INFO,"Loaded module %s OK.",modulebuf);
   
   return 0;
 }
@@ -289,24 +299,21 @@ int getindex(char *modulename) {
   return -1;
 }
 
-char *lsmod(int index) {
+char *lsmod(int index, const char **ver, const char **buildid, time_t *t) {
   module *mods;
 
   if (index < 0 || index >= modules.cursi)
     return NULL;
 
   mods=(module *)(modules.content);
-  return mods[index].name->content;
-}
+  if(ver)
+    *ver=mods[index].version;
+  if(buildid)
+    *buildid=mods[index].buildid;
+  if(t)
+    *t=mods[index].loadedsince;
 
-const char *lsmodver(int index) {
-  module *mods;
-
-  if (index < 0 || index >= modules.cursi)
-    return NULL;
-
-  mods=(module *)(modules.content);
-  return mods[index].version;
+  return mods[index].name->content;
 }
 
 int isloaded(char *modulename) {
@@ -317,52 +324,56 @@ int isloaded(char *modulename) {
 }
 
 
-int rmmod(char *modulename) {
+int rmmod(char *modulename, int close) {
   int i,j;
   module *mods;
   struct module_dep *mdp;
+  char modulebuf[1024];
+
+  strlcpy(modulebuf, modulename, sizeof(modulebuf));
+  delchars(modulebuf,"./\\;");
   
-  delchars(modulename,"./\\;");
-  
-  i=getindex(modulename);
+  i=getindex(modulebuf);
   if (i<0)
     return 1;
 
-  if ((mdp=getmoduledep(modulename))) {
+  if ((mdp=getmoduledep(modulebuf))) {
     for (j=0;j<mdp->numchildren;j++) {
       if (isloaded(mdp->children[j]->name->content)) {
-        if (rmmod(mdp->children[j]->name->content)) {
+        if (rmmod(mdp->children[j]->name->content, close)) {
           Error("core",ERR_WARNING,"Unable to remove child module %s (depends on %s)",
-                 mdp->children[j]->name->content, modulename);
+                 mdp->children[j]->name->content, modulebuf);
           return 1;
         }
       }
     }
 
     /* We may have removed other modules - reaquire the index number in case it has changed. */
-    i=getindex(modulename);
+    i=getindex(modulebuf);
     if (i<0)
       return 1;
   } else {
-    Error("core",ERR_WARNING,"Removing module %s without dependency information",modulename);
+    Error("core",ERR_WARNING,"Removing module %s without dependency information",modulebuf);
   }
   
   mods=(module *)(modules.content);
     
+  if (!close
 #ifdef BROKEN_DLCLOSE
-  {
+      || 1
+#endif
+     ) {
     void (*fini)();
     fini = dlsym(mods[i].handle, "__fini");
     if(!dlerror())
       fini();
-  }
-#endif
+  } else
+    dlclose(mods[i].handle);
 
-  dlclose(mods[i].handle);
   freesstring(mods[i].name);
   array_delslot(&modules,i);
 
-  Error("core",ERR_INFO,"Removed module %s.",modulename);
+  Error("core",ERR_INFO,"Removed module %s.",modulebuf);
   
   return 0;
 }    
@@ -386,19 +397,20 @@ void setreloadmark(struct module_dep *mdp) {
 void preparereload(char *modulename) {
   unsigned int i;
   struct module_dep *mdp;
-  
-  delchars(modulename,"./\\;");
+  char modulebuf[1024];
+
+  strlcpy(modulebuf, modulename, sizeof(modulebuf));
+  delchars(modulebuf,"./\\;");
   
   /* First, clear the mark off all dependant modules */
   for (i=0;i<knownmodules;i++)
     moduledeps[i].reloading=0;
 
   /* Do nothing if this module is not loaded */
-  i=getindex(modulename);
-  if (i<0)
+  if (getindex(modulebuf)<0)
     return;
     
-  if ((mdp=getmoduledep(modulename))) {
+  if ((mdp=getmoduledep(modulebuf))) {
     setreloadmark(mdp);
   }
 }
@@ -425,7 +437,7 @@ void safereloadcallback(void *arg) {
     return;
   
   preparereload(safereload_str->content);
-  rmmod(safereload_str->content);
+  rmmod(safereload_str->content, 1);
   insmod(safereload_str->content);
   reloadmarked();
 
@@ -444,3 +456,40 @@ void safereload(char *themodule) {
   
   scheduleoneshot(1, safereloadcallback, NULL);
 }
+
+void newserv_shutdown() {
+  module *mods;
+  char buf[1024];
+
+  while (modules.cursi) {
+    mods=(module *)(modules.content);
+
+    strlcpy(buf, mods[0].name->content, sizeof(buf));
+
+    /* Unload the module unless we're running on Valgrind -
+     * in which case unloading the module would invalidate
+     * stacktraces Valgrind has captured so far. */
+    rmmod(buf, !RUNNING_ON_VALGRIND);
+  }
+  
+  clearmoduledeps();
+
+  if (moddir!=NULL)
+    freesstring(moddir);
+
+  if (modsuffix!=NULL)
+    freesstring(modsuffix);
+
+  Error("core",ERR_INFO,"All modules removed.  Exiting.");
+}
+
+/* very slow, make sure you cache the pointer! */
+void *ndlsym(char *modulename, char *fn) {
+  module *mods=(module *)(modules.content);
+  int i=getindex(modulename);
+
+  if (i<0)
+    return NULL;
+
+  return dlsym(mods[i].handle, fn);
+}