]> code.citadel.org Git - citadel.git/blobdiff - citadel/sysdep.c
Fix race condition that caused segfaults in imap and xmpp as seen on
[citadel.git] / citadel / sysdep.c
index 137e9c59b06075ca824ab855cc2e389649b4e0fc..3d2db7da8d565170816a83fdd35054d4ce094d09 100644 (file)
@@ -441,7 +441,7 @@ void flush_output(void) {
 /*
  * client_write()   ...    Send binary data to the client.
  */
-int client_write(char *buf, int nbytes)
+int client_write(const char *buf, int nbytes)
 {
        int bytes_written = 0;
        int retval;
@@ -745,7 +745,10 @@ void start_daemon(int unused) {
         * We don't just call close() because we don't want these fd's
         * to be reused for other files.
         */
-       chdir(ctdl_run_dir);
+       if (chdir(ctdl_run_dir) != 0)
+               CtdlLogPrintf(CTDL_EMERG, 
+                             "unable to change into directory [%s]: %s", 
+                             ctdl_run_dir, strerror(errno));
 
        child = fork();
        if (child != 0) {
@@ -758,9 +761,13 @@ void start_daemon(int unused) {
 
        setsid();
        umask(0);
-        freopen("/dev/null", "r", stdin);
-        freopen("/dev/null", "w", stdout);
-        freopen("/dev/null", "w", stderr);
+        if ((freopen("/dev/null", "r", stdin) != stdin) || 
+           (freopen("/dev/null", "w", stdout) != stdout) || 
+           (freopen("/dev/null", "w", stderr) != stderr))
+               CtdlLogPrintf(CTDL_EMERG, 
+                             "unable to reopen stdin/out/err %s", 
+                             strerror(errno));
+               
 
        do {
                current_child = fork();
@@ -928,8 +935,9 @@ do_select:  force_purge = 0;
                                ptr->state = CON_EXECUTING;
                                break;
                        }
-                       if ((bind_me == NULL) && (ptr->state == CON_STARTING)) {
+                       if ((bind_me == NULL) && (ptr->state == CON_GREETING)) {
                                bind_me = ptr;
+                               ptr->state = CON_STARTING;
                                break;
                        }
                }
@@ -1146,7 +1154,7 @@ void *select_on_master (void *arg)
                                                SO_REUSEADDR,
                                                &i, sizeof(i));
 
-                                       con->state = CON_STARTING;
+                                       con->state = CON_GREETING;
 
                                        retval--;
                                        if (retval == 0)