]> jfr.im git - irc/quakenet/newserv.git/blobdiff - lua/lua.c
Add malloc checking to lua/nterfacer/trojanscan.
[irc/quakenet/newserv.git] / lua / lua.c
index c5754e24641506a5325c2f04a33c2bedcac3320d..a3d54f3b12359b20917256128f3e6fb5d606fa6e 100644 (file)
--- a/lua/lua.c
+++ b/lua/lua.c
 #include "../lib/array.h"
 #include "../lib/irc_string.h"
 #include "../core/schedule.h"
+#include "../lib/version.h"
 
 #include "lua.h"
 
+MODULE_VERSION(LUA_FULLVERSION);
+
 #ifdef LUA_DEBUGSOCKET
 
 #include <sys/types.h>
@@ -28,6 +31,9 @@
 void lua_startup(void *arg);
 void lua_loadscripts(void);
 void lua_registercommands(lua_State *l);
+void lua_registerdbcommands(lua_State *l);
+void lua_initnickpusher(void);
+void lua_initchanpusher(void);
 void lua_loadlibs(lua_State *l);
 void lua_require(lua_State *l, char *module);
 
@@ -44,6 +50,13 @@ void lua_setpath(lua_State *l);
 void lua_setupdebugsocket(void);
 void lua_freedebugsocket(void);
 
+void lua_deregisternicks(lua_list *l);
+void lua_registerlocalcommands(lua_State *ps);
+void lua_registerdebug(lua_State *ps);
+void lua_socket_closeall(lua_list *l);
+void lua_registersocketcommands(lua_State *ps);
+void lua_registercryptocommands(lua_State *ps);
+
 #ifdef LUA_DEBUGSOCKET
 
 struct sockaddr_in debugsocketdest;
@@ -59,6 +72,9 @@ void *startsched = NULL;
 
 int loaded = 0;
 
+struct rusage r_usages;
+struct rusage r_usagee;
+
 static const luaL_Reg ourlibs[] = {
   {"", luaopen_base},
   {LUA_LOADLIBNAME, luaopen_package},
@@ -72,9 +88,19 @@ static const luaL_Reg ourlibs[] = {
 #endif
   {NULL, NULL}
 };
+
+lua_list dummy;
   
 void _init() {
   lua_setupdebugsocket();
+  lua_initnickpusher();
+  lua_initchanpusher();
+
+  dummy.name = getsstring("???", 10);
+  if(!dummy.name) {
+    Error("lua", ERR_ERROR, "Cannot set dummy name.");
+    return;
+  }
 
   cpath = getcopyconfigitem("lua", "scriptdir", "", 500);
 
@@ -122,8 +148,10 @@ void _fini() {
 
   freesstring(cpath);
   freesstring(suffix);
+  freesstring(dummy.name);
 
   lua_freedebugsocket();
+  nscheckfreeall(POOL_LUA);
 }
 
 void lua_loadscripts(void) {
@@ -140,6 +168,17 @@ void lua_loadscripts(void) {
   }
 }
 
+/* taken from the lua manual, modified to use nsmalloc */
+static void *lua_nsmalloc(void *ud, void *ptr, size_t osize, size_t nsize) {
+  if(nsize == 0) {
+    if(ptr != NULL)
+      luafree(ptr);
+    return NULL;
+  }
+
+  return luarealloc(ptr, nsize);
+}
+
 lua_State *lua_loadscript(char *file) {
   char fullpath[LUA_PATHLEN];
   int top;
@@ -154,11 +193,11 @@ lua_State *lua_loadscript(char *file) {
   if(lua_scriptloaded(file))
     return NULL;
 
-  l = lua_open();
+  l = lua_newstate(lua_nsmalloc, NULL);
   if(!l)
     return NULL;
 
-  n = (lua_list *)malloc(sizeof(lua_list));;
+  n = (lua_list *)luamalloc(sizeof(lua_list));;
   if(!n) {
     Error("lua", ERR_ERROR, "Error allocing list for %s.", file);
     return NULL;
@@ -167,12 +206,22 @@ lua_State *lua_loadscript(char *file) {
   n->name = getsstring(file, LUA_PATHLEN);
   if(!n->name) {
     Error("lua", ERR_ERROR, "Error allocing name item for %s.", file);
-    free(n);
+    luafree(n);
     return NULL;
   }
+  n->calls = 0;
+
+  timerclear(&n->ru_utime);
+  timerclear(&n->ru_stime);
 
   lua_loadlibs(l);
+  lua_registerdebug(l);
   lua_registercommands(l);
+  lua_registerlocalcommands(l);
+  lua_registerdbcommands(l);
+  lua_registersocketcommands(l);
+  lua_registercryptocommands(l);
+
   lua_setpath(l);
 
 #ifdef LUA_USEJIT
@@ -186,27 +235,16 @@ lua_State *lua_loadscript(char *file) {
     Error("lua", ERR_ERROR, "Error loading %s.", file);
     lua_close(l);
     freesstring(n->name);
-    free(n);
+    luafree(n);
     return NULL;
   }
 
-  top = lua_gettop(l);
-
-  if(lua_pcall(l, 0, 0, 0)) {
-    Error("lua", ERR_ERROR, "Error setting pcall on %s.", file);
-    lua_close(l);
-    freesstring(n->name);
-    free(n);
-    return NULL;
-  }
-
-  lua_settop(l, top);
-
-  Error("lua", ERR_INFO, "Loaded %s.", file);
   n->l = l;
 
   n->next = NULL;
   n->prev = lua_tail;
+  n->nicks = NULL;
+  n->sockets = NULL;
 
   if(!lua_head) { 
     lua_head = n;
@@ -216,6 +254,19 @@ lua_State *lua_loadscript(char *file) {
 
   lua_tail = n;
 
+  top = lua_gettop(l);
+
+  if(lua_pcall(l, 0, 0, 0)) {
+    Error("lua", ERR_ERROR, "Error pcalling: %s.", file);
+    lua_close(l);
+    freesstring(n->name);
+    luafree(n);
+    return NULL;
+  }
+
+  lua_settop(l, top);
+
+  Error("lua", ERR_INFO, "Loaded %s.", file);
   lua_onload(l);
 
   return l;
@@ -223,6 +274,8 @@ lua_State *lua_loadscript(char *file) {
 
 void lua_unloadscript(lua_list *l) {
   lua_onunload(l->l);
+  lua_deregisternicks(l);
+  lua_socket_closeall(l);
   lua_close(l->l);
   freesstring(l->name);
 
@@ -249,7 +302,7 @@ void lua_unloadscript(lua_list *l) {
      }
   }
 
-  free(l);
+  luafree(l);
 }
 
 void lua_setpath(lua_State *l) {
@@ -378,12 +431,49 @@ void lua_debugoutput(char *format, ...) {
 }
 #endif
 
-char *lua_namefromstate(lua_State *l) {
+lua_list *lua_listfromstate(lua_State *l) {
   lua_list *i = lua_head;
   for(;i;i=i->next)
     if(i->l == l)
-      return i->name->content;
+      return i;
+
+  return &dummy;
+}
+
+int lua_listexists(lua_list *l) {
+  lua_list *i;
+
+  for(i=lua_head;i;i=i->next)
+    if(i == l)
+      return 1;
+
+  return 0;
+}
+
+int lua_lineok(const char *data) {
+  if(strchr(data, '\r') || strchr(data, '\n'))
+    return 0;
+  return 1;
+}
+
+INLINE int lua_debugpcall(lua_State *l, char *message, int a, int b, int c) {
+  lua_list *l2 = lua_listfromstate(l);
+  int ret;
+
+#ifdef LUA_DEBUGSOCKET
+  DEBUGOUT("%s: %s\n", l2->name->content, message);
+#endif
+
+#ifdef LUA_PROFILE
+  ACCOUNTING_START(l2);
+#endif
+
+  ret = lua_pcall(l, a, b, c);
+
+#ifdef LUA_PROFILE
+  ACCOUNTING_STOP(l2);
+#endif
 
-  return "???";
+  return ret;
 }