]> code.citadel.org Git - citadel.git/blobdiff - citadel/sysdep.c
* Cleaned up some things that generated compiler warnings
[citadel.git] / citadel / sysdep.c
index cee6c1fd583be5e5ddf9eaee6340088d3625877f..d52b920bea8659ee4897e66950fedb766fe050d4 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * $Id$
  *
- * Citadel/UX "system dependent" stuff.
+ * Citadel "system dependent" stuff.
  * See copyright.txt for copyright information.
  *
  * Here's where we (hopefully) have most parts of the Citadel server that
@@ -42,6 +42,7 @@
 
 #include <limits.h>
 #include <netinet/in.h>
+#include <arpa/inet.h>
 #include <netdb.h>
 #include <sys/un.h>
 #include <string.h>
@@ -94,9 +95,6 @@ struct CitContext masterCC;
 time_t last_purge = 0;                         /* Last dead session purge */
 static int num_threads = 0;                    /* Current number of threads */
 int num_sessions = 0;                          /* Current number of sessions */
-static int rescan[2];                          /* The rescan pipe */
-
-pthread_t initial_thread;              /* tid for main() thread */
 
 int syslog_facility = (-1);
 
@@ -108,12 +106,12 @@ int syslog_facility = (-1);
  * log data sent through this function.  BE CAREFUL!
  */
 void lprintf(enum LogLevel loglevel, const char *format, ...) {   
-        va_list arg_ptr;
+       va_list arg_ptr;
        char buf[SIZ];
  
-        va_start(arg_ptr, format);   
-        vsnprintf(buf, sizeof(buf), format, arg_ptr);   
-        va_end(arg_ptr);   
+       va_start(arg_ptr, format);   
+       vsnprintf(buf, sizeof(buf), format, arg_ptr);   
+       va_end(arg_ptr);   
 
        if (syslog_facility >= 0) {
                if (loglevel <= verbosity) {
@@ -128,13 +126,13 @@ void lprintf(enum LogLevel loglevel, const char *format, ...) {
        }
        else if (loglevel <= verbosity) { 
                struct timeval tv;
-               struct tm *tim;
+               struct tm tim;
                time_t unixtime;
 
                gettimeofday(&tv, NULL);
                /* Promote to time_t; types differ on some OSes (like darwin) */
                unixtime = tv.tv_sec;
-               tim = localtime(&unixtime);
+               localtime_r(&unixtime, &tim);
                /*
                 * Log provides millisecond accuracy.  If you need
                 * microsecond accuracy and your OS supports it, change
@@ -145,33 +143,33 @@ void lprintf(enum LogLevel loglevel, const char *format, ...) {
                        /* Millisecond display */
                        fprintf(stderr,
                                "%04d/%02d/%02d %2d:%02d:%02d.%03ld [%3d] %s",
-                               tim->tm_year + 1900, tim->tm_mon + 1,
-                               tim->tm_mday, tim->tm_hour, tim->tm_min,
-                               tim->tm_sec, (long)tv.tv_usec / 1000,
+                               tim.tm_year + 1900, tim.tm_mon + 1,
+                               tim.tm_mday, tim.tm_hour, tim.tm_min,
+                               tim.tm_sec, (long)tv.tv_usec / 1000,
                                CC->cs_pid, buf);
 #endif
                        /* Microsecond display */
                        fprintf(stderr,
                                "%04d/%02d/%02d %2d:%02d:%02d.%06ld [%3d] %s",
-                               tim->tm_year + 1900, tim->tm_mon + 1,
-                               tim->tm_mday, tim->tm_hour, tim->tm_min,
-                               tim->tm_sec, (long)tv.tv_usec,
+                               tim.tm_year + 1900, tim.tm_mon + 1,
+                               tim.tm_mday, tim.tm_hour, tim.tm_min,
+                               tim.tm_sec, (long)tv.tv_usec,
                                CC->cs_pid, buf);
                } else {
 #if 0
                        /* Millisecond display */
                        fprintf(stderr,
                                "%04d/%02d/%02d %2d:%02d:%02d.%03ld %s",
-                               tim->tm_year + 1900, tim->tm_mon + 1,
-                               tim->tm_mday, tim->tm_hour, tim->tm_min,
-                               tim->tm_sec, (long)tv.tv_usec / 1000, buf);
+                               tim.tm_year + 1900, tim.tm_mon + 1,
+                               tim.tm_mday, tim.tm_hour, tim.tm_min,
+                               tim.tm_sec, (long)tv.tv_usec / 1000, buf);
 #endif
                        /* Microsecond display */
                        fprintf(stderr,
                                "%04d/%02d/%02d %2d:%02d:%02d.%06ld %s",
-                               tim->tm_year + 1900, tim->tm_mon + 1,
-                               tim->tm_mday, tim->tm_hour, tim->tm_min,
-                               tim->tm_sec, (long)tv.tv_usec, buf);
+                               tim.tm_year + 1900, tim.tm_mon + 1,
+                               tim.tm_mday, tim.tm_hour, tim.tm_min,
+                               tim.tm_sec, (long)tv.tv_usec, buf);
                }
                fflush(stderr);
        }
@@ -237,17 +235,6 @@ void init_sysdep(void) {
         * socket breaks.
         */
        signal(SIGPIPE, SIG_IGN);
-
-       /*
-        * Set up the rescan pipe.  When a session goes idle, it writes a
-        * single byte to this pipe to wake up the thread calling select(),
-        * which tells it to rescan the list of session sockets.
-        */
-       if (pipe(rescan) != 0) {
-               lprintf(CTDL_EMERG, "Cannot create rescan pipe: %s\n",
-                       strerror(errno));
-               abort();
-       }
 }
 
 
@@ -287,7 +274,7 @@ void end_critical_section(int which_one)
  * a TCP port.  The server shuts down if the bind fails.
  *
  */
-int ig_tcp_server(int port_number, int queue_len)
+int ig_tcp_server(char *ip_addr, int port_number, int queue_len)
 {
        struct sockaddr_in sin;
        int s, i;
@@ -298,8 +285,17 @@ int ig_tcp_server(int port_number, int queue_len)
 
        memset(&sin, 0, sizeof(sin));
        sin.sin_family = AF_INET;
-       sin.sin_addr.s_addr = INADDR_ANY;
        sin.sin_port = htons((u_short)port_number);
+       if (ip_addr == NULL) {
+               sin.sin_addr.s_addr = INADDR_ANY;
+       }
+       else {
+               sin.sin_addr.s_addr = inet_addr(ip_addr);
+       }
+                                                                               
+       if (sin.sin_addr.s_addr == INADDR_NONE) {
+               sin.sin_addr.s_addr = INADDR_ANY;
+       }
 
        s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
 
@@ -383,12 +379,14 @@ int ig_uds_server(char *sockpath, int queue_len)
  * called this function.  If there's no such binding (for example, if it's
  * called by the housekeeper thread) then a generic 'master' CC is returned.
  *
- * It's inlined because it's used *VERY* frequently.
+ * This function is used *VERY* frequently and must be kept small.
  */
-INLINE struct CitContext *MyContext(void) {
-       return ((pthread_getspecific(MyConKey) == NULL)
-               ? &masterCC
-               : (struct CitContext *) pthread_getspecific(MyConKey)
+struct CitContext *MyContext(void) {
+
+       register struct CitContext *c;
+
+       return ((c = (struct CitContext *) pthread_getspecific(MyConKey),
+               c == NULL) ? &masterCC : c
        );
 }
 
@@ -460,7 +458,7 @@ DONE:       ++num_sessions;
 
 /*
  * buffer_output() ... tell client_write to buffer all output until
- *                     instructed to dump it all out later
+ *                  instructed to dump it all out later
  */
 void buffer_output(void) {
        if (CC->buffering == 0) {
@@ -471,15 +469,26 @@ void buffer_output(void) {
 }
 
 /*
- * unbuffer_output()  ...  dump out all that output we've been buffering.
+ * flush_output()  ...   dump out all that output we've been buffering.
+ */
+void flush_output(void) {
+       if (CC->buffering == 1) {
+               client_write(CC->output_buffer, CC->buffer_len);
+               CC->buffer_len = 0;
+       }
+}
+
+/*
+ * unbuffer_output()  ...  stop buffering output.
  */
 void unbuffer_output(void) {
        if (CC->buffering == 1) {
                CC->buffering = 0;
+               /* We don't call flush_output because we can't. */
                client_write(CC->output_buffer, CC->buffer_len);
+               CC->buffer_len = 0;
                free(CC->output_buffer);
                CC->output_buffer = NULL;
-               CC->buffer_len = 0;
        }
 }
 
@@ -541,15 +550,15 @@ void client_write(char *buf, int nbytes)
 
 /*
  * cprintf()  ...   Send formatted printable data to the client.   It is
- *                  implemented in terms of client_write() but remains in
- *                  sysdep.c in case we port to somewhere without va_args...
+ *               implemented in terms of client_write() but remains in
+ *               sysdep.c in case we port to somewhere without va_args...
  */
 void cprintf(const char *format, ...) {   
-        va_list arg_ptr;   
-        char buf[SIZ];   
+       va_list arg_ptr;   
+       char buf[SIZ];   
    
-        va_start(arg_ptr, format);   
-        if (vsnprintf(buf, sizeof buf, format, arg_ptr) == -1)
+       va_start(arg_ptr, format);   
+       if (vsnprintf(buf, sizeof buf, format, arg_ptr) == -1)
                buf[sizeof buf - 2] = '\n';
        client_write(buf, strlen(buf)); 
        va_end(arg_ptr);
@@ -765,6 +774,7 @@ void create_worker(void) {
        if ((ret = pthread_attr_setstacksize(&attr, 128 * 1024))) {
                lprintf(CTDL_EMERG, "pthread_attr_setstacksize: %s\n", strerror(ret));
                time_to_die = -1;
+               pthread_attr_destroy(&attr);
                return;
        }
 
@@ -777,6 +787,7 @@ void create_worker(void) {
 
        n->next = worker_list;
        worker_list = n;
+       pthread_attr_destroy(&attr);
 }
 
 
@@ -885,7 +896,7 @@ void *worker_thread(void *arg) {
        struct CitContext *ptr;
        struct CitContext *bind_me = NULL;
        fd_set readfds;
-       int retval;
+       int retval = 0;
        struct CitContext *con= NULL;   /* Temporary context pointer */
        struct ServiceFunctionHook *serviceptr;
        int ssock;                      /* Descriptor for client socket */
@@ -906,10 +917,9 @@ void *worker_thread(void *arg) {
 do_select:     force_purge = 0;
                bind_me = NULL;         /* Which session shall we handle? */
 
-               /* Initialize the fdset.  Start with the rescan pipe. */
+               /* Initialize the fdset. */
                FD_ZERO(&readfds);
-               FD_SET(rescan[0], &readfds);
-               highest = rescan[0] + 1;
+               highest = 0;
 
                begin_critical_section(S_SESSION_TABLE);
                for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
@@ -945,18 +955,11 @@ do_select:        force_purge = 0;
                if (!time_to_die) {
                        tv.tv_sec = 1;          /* wake up every second if no input */
                        tv.tv_usec = 0;
-                       /*
-                        * Avoid the "thundering herd" problem by letting only
-                        * one thread select() at a time.
-                        */
-                       begin_critical_section(S_I_WANNA_SELECT);
                        retval = select(highest + 1, &readfds, NULL, NULL, &tv);
-                       end_critical_section(S_I_WANNA_SELECT);
-               }
-               else {
-                       break;
                }
 
+               if (time_to_die) return(NULL);
+
                /* Now figure out who made this select() unblock.
                 * First, check for an error or exit condition.
                 */
@@ -987,7 +990,7 @@ do_select:  force_purge = 0;
                                                strerror(errno));
                                }
                                else {
-                                       lprintf(CTDL_NOTICE,
+                                       lprintf(CTDL_DEBUG,
                                                "New client socket %d\n",
                                                ssock);
 
@@ -1018,27 +1021,11 @@ do_select:      force_purge = 0;
                                        serviceptr->h_greeting_function();
                                        become_session(NULL);
                                        con->state = CON_IDLE;
-
-                                       /* Wake up the currently blocking select() by
-                                        * writing a dummy byte to the rescan pipe.
-                                        */
-                                       write(rescan[1], &i, 1);
                                        goto do_select;
                                }
                        }
                }
 
-               if (time_to_die) {
-                       end_critical_section(S_I_WANNA_SELECT);
-                       break;
-               }
-
-               /* If the rescan pipe went active, read a dummy byte from it */
-               if (FD_ISSET(rescan[0], &readfds)) {
-                       read(rescan[0], &i, 1);
-                       goto do_select;
-               }
-
                /* It must be a client socket.  Find a context that has data
                 * waiting on its socket *and* is in the CON_IDLE state.  Any
                 * active sockets other than our chosen one are marked as
@@ -1083,11 +1070,6 @@ SKIP_SELECT:
                        force_purge = CC->kill_me;
                        become_session(NULL);
                        bind_me->state = CON_IDLE;
-
-                       /* Wake up the currently blocking select() by writing
-                        * a dummy byte to the rescan pipe.
-                        */
-                       write(rescan[1], &i, 1);
                }
 
                dead_session_purge(force_purge);
@@ -1096,8 +1078,7 @@ SKIP_SELECT:
        }
 
        /* If control reaches this point, the server is shutting down */        
-       --num_threads;
-       return NULL;
+       return(NULL);
 }