]> jfr.im git - irc/quakenet/newserv.git/blobdiff - core/modules.c
fixes for clang
[irc/quakenet/newserv.git] / core / modules.c
index 688285a480a594fd8c8d060bace4b0476ffe99e3..08a1749710b593ba1e5098fd65164d5f746d81a8 100644 (file)
@@ -11,6 +11,7 @@
 #include "../lib/sstring.h"
 #include "../lib/irc_string.h"
 #include "../lib/splitline.h"
+#include "../lib/strlfunc.h"
 #include "config.h"
 #include "error.h"
 #include <stdio.h>
@@ -70,6 +71,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;
@@ -220,59 +222,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 +294,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;
-}
-
-const char *lsmodver(int index) {
-  module *mods;
-
-  if (index < 0 || index >= modules.cursi)
-    return NULL;
+  if(ver)
+    *ver=mods[index].version;
+  if(buildid)
+    *buildid=mods[index].buildid;
+  if(t)
+    *t=mods[index].loadedsince;
 
-  mods=(module *)(modules.content);
-  return mods[index].version;
+  return mods[index].name->content;
 }
 
 int isloaded(char *modulename) {
@@ -321,30 +323,32 @@ int rmmod(char *modulename) {
   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)) {
           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);
@@ -362,7 +366,7 @@ int rmmod(char *modulename) {
   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 +390,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);
   }
 }
@@ -447,12 +452,23 @@ void safereload(char *themodule) {
 
 void newserv_shutdown() {
   module *mods;
-  
+  char buf[1024];
+
   while (modules.cursi) {
     mods=(module *)(modules.content);
-    rmmod(mods[0].name->content);
+
+    strlcpy(buf, mods[0].name->content, sizeof(buf));
+    rmmod(buf);
   }
   
+  clearmoduledeps();
+
+  if (moddir!=NULL)
+    freesstring(moddir);
+
+  if (modsuffix!=NULL)
+    freesstring(modsuffix);
+
   Error("core",ERR_INFO,"All modules removed.  Exiting.");
 }