]> code.citadel.org Git - citadel.git/blobdiff - citadel/sysdep.c
* Got bounce messages working (mostly ... testers, please beat this up!)
[citadel.git] / citadel / sysdep.c
index 0cd19053a53ab064009324bbe667c9e225ca615b..904528aa7149042995bb4c2a708aa44336a5067c 100644 (file)
@@ -75,6 +75,8 @@ int num_sessions = 0;                         /* Current number of sessions */
 fd_set masterfds;                              /* Master sockets etc. */
 int masterhighest;
 
+time_t last_timer = 0L;                                /* Last timer hook processing */
+
 
 /*
  * lprintf()  ...   Write logging information
@@ -363,13 +365,27 @@ void client_write(char *buf, int nbytes)
 {
        int bytes_written = 0;
        int retval;
+       int sock;
+
+       if (CC->redirect_fp != NULL) {
+               fwrite(buf, nbytes, 1, CC->redirect_fp);
+               return;
+       }
+
+       if (CC->redirect_sock > 0) {
+               sock = CC->redirect_sock;       /* and continue below... */
+       }
+       else {
+               sock = CC->client_socket;
+       }
+
        while (bytes_written < nbytes) {
-               retval = write(CC->client_socket, &buf[bytes_written],
+               retval = write(sock, &buf[bytes_written],
                        nbytes - bytes_written);
                if (retval < 1) {
                        lprintf(2, "client_write() failed: %s\n",
                                strerror(errno));
-                       CC->kill_me = 1;
+                       if (sock == CC->client_socket) CC->kill_me = 1;
                        return;
                }
                bytes_written = bytes_written + retval;
@@ -483,7 +499,7 @@ int client_gets(char *buf)
  * The system-dependent part of master_cleanup() - close the master socket.
  */
 void sysdep_master_cleanup(void) {
-       /* FIX close all protocol master sockets here */
+       /* FIXME close all protocol master sockets here */
 }
 
 
@@ -678,7 +694,35 @@ void dead_session_purge(void) {
 }
 
 
-       
+
+
+
+/*
+ * Redirect a session's output to a file or socket.
+ * This function may be called with a file handle *or* a socket (but not
+ * both).  Call with neither to return output to its normal client socket.
+ */
+void CtdlRedirectOutput(FILE *fp, int sock) {
+
+       if (fp != NULL) CC->redirect_fp = fp;
+       else CC->redirect_fp = NULL;
+
+       if (sock > 0) CC->redirect_sock = sock;
+       else CC->redirect_sock = (-1);
+
+}
+
+
+/*
+ * masterCC is the context we use when not attached to a session.  This
+ * function initializes it.
+ */
+void InitializeMasterCC(void) {
+       memset(&masterCC, 0, sizeof(struct CitContext));
+       masterCC.internal_pgm = 1;
+}
+
+
 
 /*
  * Here's where it all begins.
@@ -697,6 +741,9 @@ int main(int argc, char **argv)
        /* specify default port name and trace file */
        strcpy(tracefile, "");
 
+       /* initialize the master context */
+       InitializeMasterCC();
+
        /* parse command-line arguments */
        for (a=1; a<argc; ++a) {
 
@@ -809,19 +856,9 @@ int main(int argc, char **argv)
 
        for (serviceptr = ServiceHookTable; serviceptr != NULL;
            serviceptr = serviceptr->next ) {
-               serviceptr->msock = ig_tcp_server(
-                       serviceptr->tcp_port, config.c_maxsessions);
-               if (serviceptr->msock >= 0) {
-                       FD_SET(serviceptr->msock, &masterfds);
-                       if (serviceptr->msock > masterhighest)
-                               masterhighest = serviceptr->msock;
-                       lprintf(7, "Bound to port %-5d (socket %d)\n",
-                               serviceptr->tcp_port,
-                               serviceptr->msock);
-               }
-               else {
-                       lprintf(1, "Unable to bind to port %d\n",
-                               serviceptr->tcp_port);
+               FD_SET(serviceptr->msock, &masterfds);
+               if (serviceptr->msock > masterhighest) {
+                       masterhighest = serviceptr->msock;
                }
        }
 
@@ -880,8 +917,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 );
+}
 
 
 
@@ -901,8 +942,10 @@ void worker_thread(void) {
        struct sockaddr_in fsin;        /* Data for master socket */
        int alen;                       /* Data for master socket */
        int ssock;                      /* Descriptor for client socket */
+       struct timeval tv;
 
        ++num_threads;
+
        while (!time_to_die) {
 
                /* 
@@ -928,7 +971,9 @@ SETUP_FD:   memcpy(&readfds, &masterfds, sizeof(fd_set) );
                }
                end_critical_section(S_SESSION_TABLE);
 
-               retval = select(highest + 1, &readfds, NULL, NULL, NULL);
+               tv.tv_sec = 60;         /* wake up every minute if no input */
+               tv.tv_usec = 0;
+               retval = select(highest + 1, &readfds, NULL, NULL, &tv);
 
                /* Now figure out who made this select() unblock.
                 * First, check for an error or exit condition.
@@ -974,12 +1019,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;
                                }
@@ -1021,9 +1064,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);
@@ -1036,6 +1079,10 @@ SETUP_FD:        memcpy(&readfds, &masterfds, sizeof(fd_set) );
 
                }
                dead_session_purge();
+               if ((time(NULL) - last_timer) > 60L) {
+                       last_timer = time(NULL);
+                       PerformSessionHooks(EVT_TIMER);
+               }
        }
 
        /* If control reaches this point, the server is shutting down */