]> code.citadel.org Git - citadel.git/blobdiff - citadel/sysdep.c
* cleanup ourselfs on exit.
[citadel.git] / citadel / sysdep.c
index df4f714f2e1fab35e8f442f2b20188e2585d7ae6..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...
  */
@@ -477,6 +478,7 @@ struct CitContext *CreateNewContext(void) {
  * native TCP buffering (Linux & *BSD), use that; otherwise, emulate it with
  * user-space buffering.
  */
+#ifndef HAVE_DARWIN
 #ifdef TCP_CORK
 #      define HAVE_TCP_BUFFERING
 #else
@@ -484,8 +486,8 @@ struct CitContext *CreateNewContext(void) {
 #              define HAVE_TCP_BUFFERING
 #              define TCP_CORK TCP_NOPUSH
 #      endif
-#endif
-
+#endif /* TCP_CORK */
+#endif /* HAVE_DARWIN */
 
 #ifdef HAVE_TCP_BUFFERING
 static unsigned on = 1, off = 0;
@@ -506,6 +508,16 @@ void flush_output(void) {
        setsockopt(ctx->client_socket, IPPROTO_TCP, TCP_CORK, &off, 4);
        setsockopt(ctx->client_socket, IPPROTO_TCP, TCP_CORK, &on, 4);
 }
+#elif HAVE_DARWIN
+/* Stub functions for Darwin/OS X where TCP buffering isn't liked at all */
+void buffer_output(void) {
+CC->buffering = 0;
+}
+void unbuffer_output(void) {
+CC->buffering = 0;
+}
+void flush_output(void) {
+}
 #else
 void buffer_output(void) {
        if (CC->buffering == 0) {
@@ -730,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
@@ -765,6 +793,7 @@ void start_daemon(int unused) {
        int status = 0;
        pid_t child = 0;
        FILE *fp;
+       int do_restart = 0;
 
        current_child = 0;
 
@@ -776,11 +805,6 @@ void start_daemon(int unused) {
 
        child = fork();
        if (child != 0) {
-               fp = fopen(file_pid_file, "w");
-               if (fp != NULL) {
-                       fprintf(fp, "%d\n", child);
-                       fclose(fp);
-               }
                exit(0);
        }
        
@@ -789,7 +813,7 @@ void start_daemon(int unused) {
        signal(SIGQUIT, SIG_IGN);
 
        setsid();
-//     umask(0);
+       umask(0);
         freopen("/dev/null", "r", stdin);
         freopen("/dev/null", "w", stdout);
         freopen("/dev/null", "w", stderr);
@@ -809,14 +833,44 @@ void start_daemon(int unused) {
                }
        
                else {
+                       fp = fopen(file_pid_file, "w");
+                       if (fp != NULL) {
+                               fprintf(fp, "%d\n", child);
+                               fclose(fp);
+                       }
                        waitpid(current_child, &status, 0);
                }
 
-       } while (status != 0);
+               do_restart = 0;
 
-       unlink(file_pid_file);
-       exit(0);
+               /* Did the main process exit with an actual exit code? */
+               if (WIFEXITED(status)) {
 
+                       /* Exit code 0 means the watcher should exit */
+                       if (WEXITSTATUS(status) == 0) {
+                               do_restart = 0;
+                       }
+
+                       /* Exit code 101-109 means the watcher should exit */
+                       else if ( (WEXITSTATUS(status) >= 101) && (WEXITSTATUS(status) <= 109) ) {
+                               do_restart = 0;
+                       }
+
+                       /* Any other exit code means we should restart. */
+                       else {
+                               do_restart = 1;
+                       }
+               }
+
+               /* Any other type of termination (signals, etc.) should also restart. */
+               else {
+                       do_restart = 1;
+               }
+
+       } while (do_restart);
+
+       unlink(file_pid_file);
+       exit(WEXITSTATUS(status));
 }
 
 
@@ -891,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.
@@ -1223,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);
 }