fixed crash management
[NeonServV5.git] / src / main.c
index 0ca3d31ea190c1be70efb3f6eded83e96dfa43d9..f567e93f3e92ec5eaf0cd4b1e54caa1bea401cca 100644 (file)
@@ -1,4 +1,4 @@
-/* main.c - NeonServ v5.3
+/* main.c - NeonServ v5.4
  * Copyright (C) 2011-2012  Philipp Kreil (pk910)
  * 
  * This program is free software: you can redistribute it and/or modify
 #include "ModeNode.h"
 #include "IRCQueue.h"
 #include "DBHelper.h"
-#include "commands.h"
 #include "ConfigParser.h"
 #include "ssl.h"
 #include "QServer.h"
 #include "version.h"
+#include "modules.h"
+#include "module_commands.h"
 
 time_t start_time;
 static int running, hard_restart;
@@ -115,11 +116,13 @@ void * thread_main(void *arg) {
         socket_wait = time(0) + SOCKET_SELECT_TIME;
         do {
             if(!socket_loop(SOCKET_SELECT_TIME)) {
-                putlog(LOGLEVEL_ERROR, "No more active Bots... shutting down.");
+                if(!running) break;
+                putlog(LOGLEVEL_ERROR, "No more active Bots... shutting down.\n");
                 cleanup();
                 exit(0);
             }
         } while(time(0) < socket_wait);
+        if(!running) break;
         clearTempUsers();
         destroyEvents();
         mysql_free();
@@ -146,6 +149,7 @@ int getCurrentThreadID() {
 #endif
 
 void exit_daemon() {
+    running = 0;
     if(daemonized) {
         remove(PID_FILE);
     }
@@ -191,10 +195,15 @@ int main(int argc, char *argv[]) {
             break;
         }
     }
+    #ifndef WIN32
     if(geteuid() == 0 || getuid() == 0) {
         fprintf(stderr, "NeonServ may not be run with super user privileges.\n");
         exit(0);
     }
+    #endif
+    #ifdef ENABLE_MEMORY_DEBUG
+    initMemoryDebug();
+    #endif
     if(!loadConfig(CONF_FILE)) {
         fprintf(stderr, "Unable to load " CONF_FILE "\n");
         exit(0);
@@ -223,8 +232,8 @@ int main(int argc, char *argv[]) {
         atexit(exit_daemon);
         FILE *pidfile = fopen(PID_FILE, "w");
         if (pidfile == NULL) {
-            fprintf(stderr, "Unable to create PID file: %s", strerror(errno));
-            putlog(LOGLEVEL_ERROR, "Unable to create PID file: %s", strerror(errno));
+            fprintf(stderr, "Unable to create PID file: %s\n", strerror(errno));
+            putlog(LOGLEVEL_ERROR, "Unable to create PID file: %s\n", strerror(errno));
         } else {
             fprintf(pidfile, "%i\n", (int)getpid());
             fclose(pidfile);
@@ -243,10 +252,6 @@ main:
     signal(SIGSEGV, sighandler);
     signal(SIGTERM, sighandler);
     
-    #ifdef ENABLE_MEMORY_DEBUG
-    initMemoryDebug();
-    #endif
-    
     start_time = time(0);
     
     #ifdef WIN32
@@ -280,9 +285,10 @@ main:
     init_ModeNode();
     init_bind();
        init_modcmd();
+    register_module_commands();
     init_handleinfohandler();
     init_tools();
-    
+    loadModules();
     init_bots();
     init_DBHelper();
     qserver_init();
@@ -323,7 +329,7 @@ main:
         socket_wait = time(0) + SOCKET_SELECT_TIME;
         do {
             if(!socket_loop(SOCKET_SELECT_TIME)) {
-                putlog(LOGLEVEL_ERROR, "No more active Bots... shutting down.");
+                putlog(LOGLEVEL_ERROR, "No more active Bots... shutting down.\n");
                 cleanup();
                 exit(0);
             }
@@ -457,7 +463,7 @@ TIMEQ_CALLBACK(main_checkauths) {
             if ((row = mysql_fetch_row(res)) != NULL) {
                 lastcheck = atoi(row[1]);
                 if(!lastcheck || unixtime - lastcheck >= min_unckecked) {
-                    lookup_authname(row[0], main_checkauths_callback, NULL);
+                    lookup_authname(row[0], 0, main_checkauths_callback, NULL);
                 } else 
                     next_call = 300;
             }