* cleanup ourselfs on exit.
authorWilfried Göesgens <willi@citadel.org>
Sat, 20 Jan 2007 14:47:52 +0000 (14:47 +0000)
committerWilfried Göesgens <willi@citadel.org>
Sat, 20 Jan 2007 14:47:52 +0000 (14:47 +0000)
citadel/serv_crypto.c
citadel/serv_crypto.h
citadel/serv_extensions.c
citadel/serv_extensions.h
citadel/sysdep.c

index ab6bd6f4c0a8fe1958c791cfb5174d4f37f16cdf..0db01ac47b8fdcd0bb8dafadf63f87bf65c2c1e0 100644 (file)
@@ -49,6 +49,16 @@ static unsigned long id_callback(void)
        return (unsigned long) pthread_self();
 }
 
+void destruct_ssl(void)
+{
+       int a;
+       CtdlUnregisterProtoHook(cmd_stls, "STLS");
+       CtdlUnregisterProtoHook(cmd_gtls, "GTLS");
+       for (a = 0; a < CRYPTO_num_locks(); a++) 
+               free(SSLCritters[a]);
+       free (SSLCritters);
+}
+
 void init_ssl(void)
 {
        SSL_METHOD *ssl_method;
index e8e13c2ddd85ed08a6a55a4904d59f8debe44535..501748281cd749f5f89a7c7d76d4a490d0b20364 100644 (file)
@@ -12,6 +12,7 @@
 #define CIT_CIPHERS    "ALL:RC4+RSA:+SSLv2:+TLSv1:!MD5:@STRENGTH"      /* see ciphers(1) */
 
 #ifdef HAVE_OPENSSL
+void destruct_ssl(void);
 void init_ssl(void);
 void client_write_ssl (char *buf, int nbytes);
 int client_read_ssl (char *buf, int bytes, int timeout);
index ca6349c7fad492616c559e0740240149b6c12cd1..5c2ce3867e72c72dc1b6bfcd561f9b7812e8eb32 100644 (file)
@@ -170,9 +170,11 @@ void CtdlRegisterProtoHook(void (*handler) (char *), char *cmd, char *desc)
 
 void CtdlUnregisterProtoHook(void (*handler) (char *), char *cmd)
 {
-       struct ProtoFunctionHook *cur, *p;
+       struct ProtoFunctionHook *cur, *p, *lastcur;
 
-       for (cur = ProtoHookList; cur != NULL; cur = cur->next) {
+       for (cur = ProtoHookList; 
+            cur != NULL; 
+            cur = (cur != NULL)? cur->next: NULL) {
                /* This will also remove duplicates if any */
                while (cur != NULL &&
                                handler == cur->handler &&
@@ -183,10 +185,31 @@ void CtdlUnregisterProtoHook(void (*handler) (char *), char *cmd)
                        if (cur == ProtoHookList) {
                                ProtoHookList = p;
                        }
+                       else if (lastcur != NULL)
+                       {
+                               lastcur->next = p;
+                       }
                        free(cur);
                        cur = p;
                }
+               lastcur = cur;
+       }
+}
+
+void CtdlDestroyProtoHooks(void)
+{
+       struct ProtoFunctionHook *cur, *p;
+
+       cur = ProtoHookList; 
+       while (cur != NULL)
+       {
+               lprintf(CTDL_INFO, "Destroyed server command %s (%s)\n",
+                       cur->cmd, cur->desc);
+               p = cur->next;
+               free(cur);
+               cur = p;
        }
+       ProtoHookList = NULL;
 }
 
 
@@ -276,6 +299,21 @@ void CtdlUnregisterCleanupHook(void (*fcn_ptr) (void))
        }
 }
 
+void CtdlDestroyCleanupHooks(void)
+{
+       struct CleanupFunctionHook *cur, *p;
+
+       cur = CleanupHookTable;
+       while (cur != NULL)
+       {
+               lprintf(CTDL_INFO, "Destroyed cleanup function\n");
+               p = cur->next;
+               free(cur);
+               cur = p;
+       }
+       CleanupHookTable = NULL;
+}
+
 
 void CtdlRegisterSessionHook(void (*fcn_ptr) (void), int EventType)
 {
@@ -315,6 +353,21 @@ void CtdlUnregisterSessionHook(void (*fcn_ptr) (void), int EventType)
        }
 }
 
+void CtdlDestroySessionHooks(void)
+{
+       struct SessionFunctionHook *cur, *p;
+
+       cur = SessionHookTable;
+       while (cur != NULL)
+       {
+               lprintf(CTDL_INFO, "Destroyed session function\n");
+               p = cur->next;
+               free(cur);
+               cur = p;
+       }
+       SessionHookTable = NULL;
+}
+
 
 void CtdlRegisterUserHook(void (*fcn_ptr) (struct ctdluser *), int EventType)
 {
@@ -354,6 +407,21 @@ void CtdlUnregisterUserHook(void (*fcn_ptr) (struct ctdluser *), int EventType)
        }
 }
 
+void CtdlDestroyUserHooks(void)
+{
+       struct UserFunctionHook *cur, *p;
+
+       cur = UserHookTable;
+       while (cur != NULL)
+       {
+               lprintf(CTDL_INFO, "Destroyed user function \n");
+               p = cur->next;
+               free(cur);
+               cur = p;
+       }
+       UserHookTable = NULL;
+}
+
 
 void CtdlRegisterMessageHook(int (*handler)(struct CtdlMessage *),
                                int EventType)
@@ -395,6 +463,21 @@ void CtdlUnregisterMessageHook(int (*handler)(struct CtdlMessage *),
        }
 }
 
+void CtdlDestroyMessageHook(void)
+{
+       struct MessageFunctionHook *cur, *p;
+
+       cur = MessageHookTable; 
+       while (cur != NULL)
+       {
+               lprintf(CTDL_INFO, "Destroyed message function \n");
+               p = cur->next;
+               free(cur);
+               cur = p;
+       }
+       MessageHookTable = NULL;
+}
+
 
 void CtdlRegisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
 {
@@ -429,6 +512,21 @@ void CtdlUnregisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
        }
 }
 
+void CtdlDestroyNetprocHooks(void)
+{
+       struct NetprocFunctionHook *cur, *p;
+
+       cur = NetprocHookTable;
+       while (cur != NULL)
+       {
+               lprintf(CTDL_INFO, "Unregistered netproc function\n");
+               p = cur->next;
+               free(cur);
+               cur = p;
+       }
+       NetprocHookTable = NULL;
+}
+
 
 void CtdlRegisterDeleteHook(void (*handler)(char *, long) )
 {
@@ -462,6 +560,20 @@ void CtdlUnregisterDeleteHook(void (*handler)(char *, long) )
                }
        }
 }
+void CtdlDestroyDeleteHooks(void)
+{
+       struct DeleteFunctionHook *cur, *p;
+
+       cur = DeleteHookTable;
+       while (cur != NULL)
+       {
+               lprintf(CTDL_INFO, "Destroyed netproc function\n");
+               p = cur->next;
+               free(cur);
+               cur = p;                
+       }
+       DeleteHookTable = NULL;
+}
 
 
 
@@ -499,6 +611,22 @@ void CtdlUnregisterFixedOutputHook(char *content_type)
        }
 }
 
+void CtdlDestroyFixedOutputHooks(void)
+{
+       struct FixedOutputHook *cur, *p;
+
+       cur = FixedOutputTable; 
+       while (cur != NULL)
+       {
+               lprintf(CTDL_INFO, "Destroyed fixed output function for %s\n", cur->content_type);
+               p = cur->next;
+               free(cur);
+               cur = p;
+               
+       }
+       FixedOutputTable = NULL;
+}
+
 /* returns nonzero if we found a hook and used it */
 int PerformFixedOutputHooks(char *content_type, char *content, int content_length)
 {
@@ -553,6 +681,23 @@ void CtdlUnregisterXmsgHook(int (*fcn_ptr) (char *, char *, char *), int order)
        }
 }
 
+void CtdlDestroyXmsgHooks(void)
+{
+       struct XmsgFunctionHook *cur, *p;
+
+       cur = XmsgHookTable;
+       while (cur != NULL)
+       {
+               lprintf(CTDL_INFO, "Destroyed x-msg function "
+                       "(priority %d)\n", cur->order);
+               p = cur->next;
+                       
+               free(cur);
+               cur = p;
+       }
+       XmsgHookTable = NULL;
+}
+
 
 void CtdlRegisterServiceHook(int tcp_port,
                        char *sockpath,
@@ -583,6 +728,7 @@ void CtdlRegisterServiceHook(int tcp_port,
        }
        else if (tcp_port <= 0) {       /* port -1 to disable */
                lprintf(CTDL_INFO, "Service has been manually disabled, skipping\n");
+               free (message);
                free(newfcn);
                return;
        }
@@ -603,6 +749,7 @@ void CtdlRegisterServiceHook(int tcp_port,
                AddPortError(message, error);
                strcat(message, "FAILED.");
                lprintf(CTDL_CRIT, "%s\n", message);
+               free (message);
                free(error);
                free(newfcn);
        }
@@ -646,6 +793,29 @@ void CtdlUnregisterServiceHook(int tcp_port, char *sockpath,
        }
 }
 
+void CtdlDestroyServiceHook(void)
+{
+       struct ServiceFunctionHook *cur, *p;
+
+       cur = ServiceHookTable;
+       while (cur != NULL)
+       {
+               close(cur->msock);
+               if (cur->sockpath) {
+                       lprintf(CTDL_INFO, "Closed UNIX domain socket %s\n",
+                               cur->sockpath);
+               } else if (cur->tcp_port) {
+                       lprintf(CTDL_INFO, "Closed TCP port %d\n", cur->tcp_port);
+               } else {
+                       lprintf(CTDL_INFO, "Unregistered unknown service\n");
+               }
+               p = cur->next;
+               free(cur);
+               cur = p;
+       }
+       ServiceHookTable = NULL;
+}
+
 
 void PerformSessionHooks(int EventType)
 {
index 590d05c8e9058427804090912ae94ce4164ba7ca..b4930e622b92874f0f87ec6ce7e08a6ad6b6b4a2 100644 (file)
@@ -47,37 +47,46 @@ char *Dynamic_Module_Init(void);
 
 void CtdlRegisterSessionHook(void (*fcn_ptr)(void), int EventType);
 void CtdlUnregisterSessionHook(void (*fcn_ptr)(void), int EventType);
+void CtdlDestroySessionHooks(void);
 void PerformSessionHooks(int EventType);
 
 void CtdlRegisterUserHook(void (*fcn_ptr)(struct ctdluser *), int EventType);
 void CtdlUnregisterUserHook(void (*fcn_ptr)(struct ctdluser *), int EventType);
+void CtdlDestroyUserHooks(void);
 void PerformUserHooks(struct ctdluser *usbuf, int EventType);
 
 void CtdlRegisterXmsgHook(int (*fcn_ptr)(char *, char *, char *), int order);
 void CtdlUnregisterXmsgHook(int (*fcn_ptr)(char *, char *, char *), int order);
 int PerformXmsgHooks(char *, char *, char *);
+void CtdlDestroyXmsgHooks(void);
+
 
 
 void CtdlRegisterMessageHook(int (*handler)(struct CtdlMessage *),
                                                        int EventType);
 void CtdlUnregisterMessageHook(int (*handler)(struct CtdlMessage *),
                                                        int EventType);
+void CtdlDestroyMessageHook(void);
 int PerformMessageHooks(struct CtdlMessage *, int EventType);
 
 
 void CtdlRegisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) );
 void CtdlUnregisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) );
+void CtdlDestroyNetprocHooks(void);
 int PerformNetprocHooks(struct CtdlMessage *, char *);
 
 void CtdlRegisterDeleteHook(void (*handler)(char *, long) );
 void CtdlUnregisterDeleteHook(void (*handler)(char *, long) );
+void CtdlDestroyDeleteHooks(void);
 void PerformDeleteHooks(char *, long);
 
 
 void CtdlRegisterCleanupHook(void (*fcn_ptr)(void));
 void CtdlUnregisterCleanupHook(void (*fcn_ptr)(void));
+void CtdlDestroyCleanupHooks(void);
 void CtdlRegisterProtoHook(void (*handler)(char *), char *cmd, char *desc);
 void CtdlUnregisterProtoHook(void (*handler)(char *), char *cmd);
+void CtdlDestroyProtoHooks(void);
 void CtdlRegisterServiceHook(int tcp_port,
                        char *sockpath,
                         void (*h_greeting_function) (void),
@@ -90,11 +99,13 @@ void CtdlUnregisterServiceHook(int tcp_port,
                         void (*h_command_function) (void),
                         void (*h_async_function) (void)
 );
+void CtdlDestroyServiceHook(void);
 
 void CtdlRegisterFixedOutputHook(char *content_type,
                        void (*output_function) (char *supplied_data, int len)
 );
 void CtdlUnRegisterFixedOutputHook(char *content_type);
+void CtdlDestroyFixedOutputHooks(void);
 int PerformFixedOutputHooks(char *, char *, int);
 
 #endif /* SERV_EXTENSIONS_H */
index 1d344029edfc1a9116c6b76b31f0a9f7dfc9e8e9..f12c6babcc21b084953b6001d57b8decefcc4e10 100644 (file)
@@ -101,6 +101,8 @@ int syslog_facility = LOG_DAEMON;
 int enable_syslog = 0;
 extern int running_as_daemon;
 
+void DestroyWorkerList(void);
+
 /*
  * lprintf()  ...   Write logging information
  */
@@ -162,7 +164,6 @@ static RETSIGTYPE signal_cleanup(int signum) {
        master_cleanup(signum);
 }
 
-
 /*
  * Some initialization stuff...
  */
@@ -741,9 +742,25 @@ void sysdep_master_cleanup(void) {
                        unlink(serviceptr->sockpath);
                }
        }
+#ifdef HAVE_OPENSSL
+       destruct_ssl();
+#endif
+       CtdlDestroyProtoHooks();
+       CtdlDestroyDeleteHooks();
+       CtdlDestroyXmsgHooks();
+       CtdlDestroyNetprocHooks();
+       CtdlDestroyUserHooks();
+       CtdlDestroyMessageHook();
+       CtdlDestroyCleanupHooks();
+       CtdlDestroyFixedOutputHooks();  
+       CtdlDestroySessionHooks();
+       CtdlDestroyServiceHook();
+       DestroyWorkerList();
 }
 
 
+
+
 /*
  * Terminate another session.
  * (This could justifiably be moved out of sysdep.c because it
@@ -928,6 +945,18 @@ void create_worker(void) {
        pthread_attr_destroy(&attr);
 }
 
+void DestroyWorkerList(void)
+{
+       struct worker_node *cur, *p;
+       cur = worker_list;
+       while (cur != NULL)
+       {
+               p = cur->next;
+               free (cur);
+               cur = p;
+       }
+       worker_list = NULL;
+}
 
 /*
  * Create the indexer thread and begin its operation.
@@ -1260,7 +1289,7 @@ SKIP_SELECT:
                do_housekeeping();
                check_sched_shutdown();
        }
-
+       if (con != NULL) free (con);//// TODO: could this harm other threads? 
        /* If control reaches this point, the server is shutting down */        
        return(NULL);
 }