]> code.citadel.org Git - citadel.git/blobdiff - citadel/sysdep.c
* Temporary hack to ig_tcp_server() to listen on an arbitrary port if the
[citadel.git] / citadel / sysdep.c
index 63b89bebf4680dea07d76ec05fea4d80a0c64a47..6bde9f3961d93b173dd2d96490dd4630a8f9f4a8 100644 (file)
@@ -285,7 +285,16 @@ int ig_tcp_server(int port_number, int queue_len)
 
        if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
                lprintf(1, "citserver: Can't bind: %s\n", strerror(errno));
-               return(-1);
+               sin.sin_port = 0;
+               if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
+                       lprintf(1, "citserver: Can't bind: %s\n",
+                               strerror(errno));
+                       return(-1);
+               }
+               else {
+                       lprintf(1, "bind to alternate port %d ok\n",
+                               htons(sin.sin_port) );
+               }
        }
 
        if (listen(s, queue_len) < 0) {
@@ -784,16 +793,6 @@ int main(int argc, char **argv)
                free(moddir);
        }
 
-       lprintf(7, "Starting housekeeper thread\n");
-       pthread_attr_init(&attr);
-               pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
-       if (pthread_create(&HousekeepingThread, &attr,
-          (void* (*)(void*)) housekeeping_loop, NULL) != 0) {
-               lprintf(1, "Can't create housekeeping thead: %s\n",
-                       strerror(errno));
-       }
-
-
        /*
         * The rescan pipe exists so that worker threads can be woken up and
         * told to re-scan the context list for fd's to listen on.  This is
@@ -857,6 +856,19 @@ int main(int argc, char **argv)
                }
        }
 
+       /*
+        * Create the housekeeper thread
+        */
+       lprintf(7, "Starting housekeeper thread\n");
+       pthread_attr_init(&attr);
+               pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
+       if (pthread_create(&HousekeepingThread, &attr,
+          (void* (*)(void*)) housekeeping_loop, NULL) != 0) {
+               lprintf(1, "Can't create housekeeping thead: %s\n",
+                       strerror(errno));
+       }
+
+
        /*
         * Now create a bunch of worker threads.
         */
@@ -877,8 +889,12 @@ int main(int argc, char **argv)
 }
 
 
-
-
+/*
+ * Bind a thread to a context.
+ */
+inline void become_session(struct CitContext *which_con) {
+       pthread_setspecific(MyConKey, (void *)which_con );
+}
 
 
 
@@ -971,12 +987,10 @@ SETUP_FD: memcpy(&readfds, &masterfds, sizeof(fd_set) );
                                                SO_REUSEADDR,
                                                &i, sizeof(i));
 
-                                       pthread_setspecific(MyConKey,
-                                               (void *)con);
+                                       become_session(con);
                                        begin_session(con);
                                        serviceptr->h_greeting_function();
-                                       pthread_setspecific(MyConKey,
-                                               (void *)NULL);
+                                       become_session(NULL);
                                        con->state = CON_IDLE;
                                        goto SETUP_FD;
                                }
@@ -987,7 +1001,7 @@ SETUP_FD:  memcpy(&readfds, &masterfds, sizeof(fd_set) );
                 * thread that the &readfds needs to be refreshed with more
                 * current data.
                 */
-               if (FD_ISSET(rescan[0], &readfds)) {
+               if (!time_to_die) if (FD_ISSET(rescan[0], &readfds)) {
                        read(rescan[0], &junk, 1);
                        goto SETUP_FD;
                }
@@ -1018,9 +1032,9 @@ SETUP_FD: memcpy(&readfds, &masterfds, sizeof(fd_set) );
 
                        /* We're bound to a session, now do *one* command */
                        if (bind_me != NULL) {
-                               pthread_setspecific(MyConKey, (void *)bind_me);
+                               become_session(bind_me);
                                CC->h_command_function();
-                               pthread_setspecific(MyConKey, (void *)NULL);
+                               become_session(NULL);
                                bind_me->state = CON_IDLE;
                                if (bind_me->kill_me == 1) {
                                        RemoveContext(bind_me);