* vCtdlLogPrintf(): combine the server data plus the application format string into...
[citadel.git] / citadel / sysdep.c
index ce3523d98fb47f95096acb65146ecef7642a5eb8..2e43b66bb0c472ddfae6558e3e7661cb7b911e80 100644 (file)
@@ -1,6 +1,4 @@
 /*
- * $Id$
- *
  * Citadel "system dependent" stuff.
  * See COPYING for copyright information.
  *
@@ -49,6 +47,7 @@
 #include <errno.h>
 #include <stdarg.h>
 #include <grp.h>
+#define SHOW_ME_VAPPEND_PRINTF
 #include <libcitadel.h>
 #include "citadel.h"
 #include "server.h"
@@ -92,7 +91,7 @@ int verbosity = DEFAULT_VERBOSITY;            /* Logging level */
 
 int syslog_facility = LOG_DAEMON;
 int enable_syslog = 0;
-
+int print_to_logfile = 1;
 
 /*
  * CtdlLogPrintf()  ...   Write logging information
@@ -106,14 +105,13 @@ void CtdlLogPrintf(enum LogLevel loglevel, const char *format, ...) {
 
 void vCtdlLogPrintf(enum LogLevel loglevel, const char *format, va_list arg_ptr)
 {
-       char buf[SIZ], buf2[SIZ];
 
        if (enable_syslog) {
                vsyslog((syslog_facility | loglevel), format, arg_ptr);
        }
 
        /* stderr output code */
-       if (enable_syslog || running_as_daemon) return;
+       if (enable_syslog || !print_to_logfile) return;
 
        /* if we run in forground and syslog is disabled, log to terminal */
        if (loglevel <= verbosity) { 
@@ -121,28 +119,51 @@ void vCtdlLogPrintf(enum LogLevel loglevel, const char *format, va_list arg_ptr)
                struct tm tim;
                time_t unixtime;
                CitContext *CCC = CC;
+               ThreadTSD *cTSD = CTP;
+               CtdlThreadNode *node = NULL;
+               long lwpid = 0;
+               char formatbuf[SIZ];
+               char LWP[64];
+               char SESS[64];
+
+               if (cTSD != NULL) {
+                       node = cTSD->self;
+               }
+
+               if ((node != NULL) && (node->reltid != 0)) {
+                       lwpid = node->reltid;
+               }
 
                gettimeofday(&tv, NULL);
+
                /* Promote to time_t; types differ on some OSes (like darwin) */
                unixtime = tv.tv_sec;
                localtime_r(&unixtime, &tim);
-               if ((CCC != NULL) && (CCC->cs_pid != 0)) {
-                       sprintf(buf,
-                               "%04d/%02d/%02d %2d:%02d:%02d.%06ld [%3d] ",
-                               tim.tm_year + 1900, tim.tm_mon + 1,
-                               tim.tm_mday, tim.tm_hour, tim.tm_min,
-                               tim.tm_sec, (long)tv.tv_usec,
-                               CCC->cs_pid);
-               } else {
-                       sprintf(buf,
-                               "%04d/%02d/%02d %2d:%02d:%02d.%06ld ",
-                               tim.tm_year + 1900, tim.tm_mon + 1,
-                               tim.tm_mday, tim.tm_hour, tim.tm_min,
-                               tim.tm_sec, (long)tv.tv_usec);
+
+               *LWP = '\0';
+               if (lwpid != 0) {
+                       snprintf(LWP, 64, "[LWP:%ld] ", lwpid);
                }
-               vsnprintf(buf2, SIZ, format, arg_ptr);   
+                       
+               *SESS = '\0';
+               if (CCC != NULL) {
+                       if (CCC->cs_pid != 0) {
+                               snprintf(SESS, 64, " [%3d] ", CCC->cs_pid);
+                       }
+                       else if (CCC->user.usernum != 0) {
+                               snprintf(SESS, 64, " [:%ld] ", CCC->user.usernum);
+                       }
+               }
+
+               snprintf(formatbuf, SIZ, 
+                        "%04d/%02d/%02d %2d:%02d:%02d.%06ld %s%s%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, 
+                        LWP, SESS, format
+               );
 
-               fprintf(stderr, "%s%s", buf, buf2);
+               vfprintf(stderr, formatbuf, arg_ptr);   
                fflush(stderr);
        }
 }   
@@ -240,90 +261,121 @@ void init_sysdep(void) {
 }
 
 
-
-
-/*
+/* 
  * This is a generic function to set up a master socket for listening on
- * a TCP port.  The server shuts down if the bind fails.
+ * a TCP port.  The server shuts down if the bind fails.  (IPv4/IPv6 version)
  *
+ * ip_addr     IP address to bind
+ * port_number port number to bind
+ * queue_len   number of incoming connections to allow in the queue
  */
-int ig_tcp_server(char *ip_addr, int port_number, int queue_len, char **errormessage)
+int ctdl_tcp_server(char *ip_addr, int port_number, int queue_len, char *errormessage)
 {
-       struct sockaddr_in sin;
-       int s, i;
-       int actual_queue_len;
-
-       actual_queue_len = queue_len;
-       if (actual_queue_len < 5) actual_queue_len = 5;
-
-       memset(&sin, 0, sizeof(sin));
-       sin.sin_family = AF_INET;
-       sin.sin_port = htons((u_short)port_number);
-       if (ip_addr == NULL) {
-               sin.sin_addr.s_addr = INADDR_ANY;
+       struct protoent *p;
+       struct sockaddr_in6 sin6;
+       struct sockaddr_in sin4;
+       int s, i, b;
+       int ip_version = 6;
+
+       memset(&sin6, 0, sizeof(sin6));
+       memset(&sin4, 0, sizeof(sin4));
+       sin6.sin6_family = AF_INET6;
+       sin4.sin_family = AF_INET;
+
+       if (    (ip_addr == NULL)                                                       /* any IPv6 */
+               || (IsEmptyStr(ip_addr))
+               || (!strcmp(ip_addr, "*"))
+       ) {
+               ip_version = 6;
+               sin6.sin6_addr = in6addr_any;
        }
-       else {
-               sin.sin_addr.s_addr = inet_addr(ip_addr);
+       else if (!strcmp(ip_addr, "0.0.0.0"))                                           /* any IPv4 */
+       {
+               ip_version = 4;
+               sin4.sin_addr.s_addr = INADDR_ANY;
        }
-                                                                               
-       if (sin.sin_addr.s_addr == !INADDR_ANY) {
-               sin.sin_addr.s_addr = INADDR_ANY;
+       else if ((strchr(ip_addr, '.')) && (!strchr(ip_addr, ':')))                     /* specific IPv4 */
+       {
+               ip_version = 4;
+               if (inet_pton(AF_INET, ip_addr, &sin4.sin_addr) <= 0) {
+                       snprintf(errormessage, SIZ,
+                                "Error binding to [%s] : %s", ip_addr, strerror(errno)
+                       );
+                       CtdlLogPrintf(CTDL_ALERT, "%s\n", errormessage);
+                       return (-1);
+               }
+       }
+       else                                                                            /* specific IPv6 */
+       {
+               ip_version = 6;
+               if (inet_pton(AF_INET6, ip_addr, &sin6.sin6_addr) <= 0) {
+                       snprintf(errormessage, SIZ,
+                                "Error binding to [%s] : %s", ip_addr, strerror(errno)
+                       );
+                       CtdlLogPrintf(CTDL_ALERT, "%s\n", errormessage);
+                       return (-1);
+               }
+       }
+
+       if (port_number == 0) {
+               snprintf(errormessage, SIZ,
+                        "Can't start: no port number specified."
+               );
+               CtdlLogPrintf(CTDL_ALERT, "%s\n", errormessage);
+               return (-1);
        }
+       sin6.sin6_port = htons((u_short) port_number);
+       sin4.sin_port = htons((u_short) port_number);
 
-       s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
+       p = getprotobyname("tcp");
 
+       s = socket( ((ip_version == 6) ? PF_INET6 : PF_INET), SOCK_STREAM, (p->p_proto));
        if (s < 0) {
-               *errormessage = (char*) malloc(SIZ + 1);
-               snprintf(*errormessage, SIZ, 
-                                "citserver: Can't create a socket: %s",
-                                strerror(errno));
-               CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
-               return(-1);
+               snprintf(errormessage, SIZ,
+                        "Can't create a listening socket: %s", strerror(errno)
+               );
+               CtdlLogPrintf(CTDL_ALERT, "%s\n", errormessage);
+               return (-1);
        }
-
+       /* Set some socket options that make sense. */
        i = 1;
        setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
 
-       if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
-               *errormessage = (char*) malloc(SIZ + 1);
-               snprintf(*errormessage, SIZ, 
-                                "citserver: Can't bind: %s",
-                                strerror(errno));
-               CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
-               close(s);
-               return(-1);
+       if (ip_version == 6) {
+               b = bind(s, (struct sockaddr *) &sin6, sizeof(sin6));
        }
-
-       /* set to nonblock - we need this for some obscure situations */
-       if (fcntl(s, F_SETFL, O_NONBLOCK) < 0) {
-               *errormessage = (char*) malloc(SIZ + 1);
-               snprintf(*errormessage, SIZ, 
-                                "citserver: Can't set socket to non-blocking: %s",
-                                strerror(errno));
-               CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
-               close(s);
-               return(-1);
+       else {
+               b = bind(s, (struct sockaddr *) &sin4, sizeof(sin4));
        }
 
-       if (listen(s, actual_queue_len) < 0) {
-               *errormessage = (char*) malloc(SIZ + 1);
-               snprintf(*errormessage, SIZ, 
-                                "citserver: Can't listen: %s",
-                                strerror(errno));
-               CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
-               close(s);
-               return(-1);
+       if (b < 0) {
+               snprintf(errormessage, SIZ,
+                        "Can't bind: %s", strerror(errno)
+               );
+               CtdlLogPrintf(CTDL_ALERT, "%s\n", errormessage);
+               return (-1);
        }
 
-       return(s);
+       fcntl(s, F_SETFL, O_NONBLOCK);
+
+       if (listen(s, ((queue_len >= 5) ? queue_len : 5) ) < 0) {
+               snprintf(errormessage, SIZ,
+                        "Can't listen: %s", strerror(errno)
+               );
+               CtdlLogPrintf(CTDL_ALERT, "%s\n", errormessage);
+               return (-1);
+       }
+       return (s);
 }
 
 
 
+
+
 /*
  * Create a Unix domain socket and listen on it
  */
-int ig_uds_server(char *sockpath, int queue_len, char **errormessage)
+int ctdl_uds_server(char *sockpath, int queue_len, char *errormessage)
 {
        struct sockaddr_un addr;
        int s;
@@ -338,10 +390,10 @@ int ig_uds_server(char *sockpath, int queue_len, char **errormessage)
 
        i = unlink(sockpath);
        if ((i != 0) && (errno != ENOENT)) {
-               *errormessage = (char*) malloc(SIZ + 1);
-               snprintf(*errormessage, SIZ, "citserver: can't unlink %s: %s",
-                       sockpath, strerror(errno));
-               CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
+               snprintf(errormessage, SIZ, "citserver: can't unlink %s: %s",
+                       sockpath, strerror(errno)
+               );
+               CtdlLogPrintf(CTDL_EMERG, "%s\n", errormessage);
                return(-1);
        }
 
@@ -351,40 +403,36 @@ int ig_uds_server(char *sockpath, int queue_len, char **errormessage)
 
        s = socket(AF_UNIX, SOCK_STREAM, 0);
        if (s < 0) {
-               *errormessage = (char*) malloc(SIZ + 1);
-               snprintf(*errormessage, SIZ, 
+               snprintf(errormessage, SIZ, 
                         "citserver: Can't create a socket: %s",
                         strerror(errno));
-               CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
+               CtdlLogPrintf(CTDL_EMERG, "%s\n", errormessage);
                return(-1);
        }
 
        if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
-               *errormessage = (char*) malloc(SIZ + 1);
-               snprintf(*errormessage, SIZ, 
+               snprintf(errormessage, SIZ, 
                         "citserver: Can't bind: %s",
                         strerror(errno));
-               CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
+               CtdlLogPrintf(CTDL_EMERG, "%s\n", errormessage);
                return(-1);
        }
 
        /* set to nonblock - we need this for some obscure situations */
        if (fcntl(s, F_SETFL, O_NONBLOCK) < 0) {
-               *errormessage = (char*) malloc(SIZ + 1);
-               snprintf(*errormessage, SIZ, 
+               snprintf(errormessage, SIZ, 
                         "citserver: Can't set socket to non-blocking: %s",
                         strerror(errno));
-               CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
+               CtdlLogPrintf(CTDL_EMERG, "%s\n", errormessage);
                close(s);
                return(-1);
        }
 
        if (listen(s, actual_queue_len) < 0) {
-               *errormessage = (char*) malloc(SIZ + 1);
-               snprintf(*errormessage, SIZ, 
+               snprintf(errormessage, SIZ, 
                         "citserver: Can't listen: %s",
                         strerror(errno));
-               CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
+               CtdlLogPrintf(CTDL_EMERG, "%s\n", errormessage);
                return(-1);
        }
 
@@ -468,8 +516,27 @@ int client_write(const char *buf, int nbytes)
 
        if (nbytes < 1) return(0);
 
-//     flush_client_inbuf();
        Ctx = CC;
+
+#ifdef BIGBAD_IODBG
+       {
+               int rv = 0;
+               char fn [SIZ];
+               FILE *fd;
+               
+               snprintf(fn, SIZ, "/tmp/foolog_%s.%d", Ctx->ServiceName, Ctx->cs_pid);
+               
+               fd = fopen(fn, "a+");
+               fprintf(fd, "Sending: BufSize: %d BufContent: [",
+                       nbytes);
+               rv = fwrite(buf, nbytes, 1, fd);
+               fprintf(fd, "]\n");
+               
+                       
+               fclose(fd);
+       }
+#endif
+//     flush_client_inbuf();
        if (Ctx->redirect_buffer != NULL) {
                StrBufAppendBufPlain(Ctx->redirect_buffer,
                                     buf, nbytes, 0);
@@ -572,16 +639,60 @@ int client_read_blob(StrBuf *Target, int bytes, int timeout)
 
 #ifdef HAVE_OPENSSL
        if (CCC->redirect_ssl) {
+#ifdef BIGBAD_IODBG
+               int rv = 0;
+               char fn [SIZ];
+               FILE *fd;
+               
+               snprintf(fn, SIZ, "/tmp/foolog_%s.%d", CCC->ServiceName, CCC->cs_pid);
+                       
+               fd = fopen(fn, "a+");
+               fprintf(fd, "Reading BLOB: BufSize: %d ",
+                       bytes);
+               rv = fwrite(ChrPtr(Target), StrLength(Target), 1, fd);
+               fprintf(fd, "]\n");
+               
+                       
+               fclose(fd);
+#endif
                retval = client_read_sslblob(Target, bytes, timeout);
                if (retval < 0) {
                        CtdlLogPrintf(CTDL_CRIT, 
                                      "%s failed\n",
                                      __FUNCTION__);
                }
+#ifdef BIGBAD_IODBG
+               snprintf(fn, SIZ, "/tmp/foolog_%s.%d", CCC->ServiceName, CCC->cs_pid);
+               
+               fd = fopen(fn, "a+");
+               fprintf(fd, "Read: %d BufContent: [",
+                       StrLength(Target));
+               rv = fwrite(ChrPtr(Target), StrLength(Target), 1, fd);
+               fprintf(fd, "]\n");
+               
+               
+               fclose(fd);
+#endif
        }
        else 
 #endif
        {
+#ifdef BIGBAD_IODBG
+               int rv = 0;
+               char fn [SIZ];
+               FILE *fd;
+               
+               snprintf(fn, SIZ, "/tmp/foolog_%s.%d", CCC->ServiceName, CCC->cs_pid);
+                       
+               fd = fopen(fn, "a+");
+               fprintf(fd, "Reading BLOB: BufSize: %d ",
+                       bytes);
+               rv = fwrite(ChrPtr(Target), StrLength(Target), 1, fd);
+               fprintf(fd, "]\n");
+               
+                       
+               fclose(fd);
+#endif
                retval = StrBufReadBLOBBuffered(Target, 
                                                CCC->ReadBuf,
                                                &CCC->Pos,
@@ -597,31 +708,33 @@ int client_read_blob(StrBuf *Target, int bytes, int timeout)
                                      Error);
                        return retval;
                }
-               else
-               {
 #ifdef BIGBAD_IODBG
-                       int rv = 0;
-                       char fn [SIZ];
-                       FILE *fd;
-                       
-                       snprintf(fn, SIZ, "/tmp/foolog_%s.%d", CCC->ServiceName, CCC->cs_pid);
-                       
-                       fd = fopen(fn, "a+");
-                       fprintf(fd, "Read: BufSize: %d BufContent: [",
-                               StrLength(Target));
-                       rv = fwrite(ChrPtr(Target), StrLength(Target), 1, fd);
-                       fprintf(fd, "]\n");
-                       
-                       
-                       fclose(fd);
+               snprintf(fn, SIZ, "/tmp/foolog_%s.%d", CCC->ServiceName, CCC->cs_pid);
+               
+               fd = fopen(fn, "a+");
+               fprintf(fd, "Read: %d BufContent: [",
+                       StrLength(Target));
+               rv = fwrite(ChrPtr(Target), StrLength(Target), 1, fd);
+               fprintf(fd, "]\n");
+               
+               
+               fclose(fd);
 #endif
-
-               }
        }
        return retval;
 }
 
 
+/*
+ * to make client_read_random_blob() more efficient, increase buffer size.
+ * just use in greeting function, else your buffer may be flushed
+ */
+void client_set_inbound_buf(long N)
+{
+       FlushStrBuf(CC->ReadBuf);
+       ReAdjustEmptyBuf(CC->ReadBuf, N * SIZ, N * SIZ);
+}
+
 int client_read_random_blob(StrBuf *Target, int timeout)
 {
        CitContext *CCC=CC;
@@ -645,11 +758,30 @@ int client_read_random_blob(StrBuf *Target, int timeout)
                        StrBufAppendBufPlain(Target, pch, len, 0);
                        FlushStrBuf(CCC->ReadBuf);
                        CCC->Pos = NULL;
+#ifdef BIGBAD_IODBG
+                       {
+                               int rv = 0;
+                               char fn [SIZ];
+                               FILE *fd;
+                       
+                               snprintf(fn, SIZ, "/tmp/foolog_%s.%d", CCC->ServiceName, CCC->cs_pid);
+                       
+                               fd = fopen(fn, "a+");
+                               fprintf(fd, "Read: BufSize: %d BufContent: [",
+                                       StrLength(Target));
+                               rv = fwrite(ChrPtr(Target), StrLength(Target), 1, fd);
+                               fprintf(fd, "]\n");
+                       
+                       
+                               fclose(fd);
+                       }
+#endif
+       
                        return StrLength(Target);
                }
                return rc;
        }
-       else 
+       else
                return rc;
 }
 
@@ -1285,6 +1417,10 @@ void *select_on_master (void *arg)
        int m;
        int i;
        int retval;
+       struct CitContext select_on_master_CC;
+
+       CtdlFillSystemContext(&select_on_master_CC, "select_on_master");
+       citthread_setspecific(MyConKey, (void *)&select_on_master_CC);
 
        while (!CtdlThreadCheckStop()) {
                /* Initialize the fdset. */
@@ -1388,6 +1524,8 @@ void *select_on_master (void *arg)
                        }
                }
        }
+       CtdlClearSystemContext();
+
        return NULL;
 }