Removed race condition from CheckIfAlreadySeen()
[citadel.git] / citadel / sysdep.c
index 42323b163c9350a3048773c95827cef5caaf67b8..e5a829fecef005d3b5b34c099617966708759287 100644 (file)
@@ -2,12 +2,9 @@
  * Citadel "system dependent" stuff.
  *
  * Here's where we (hopefully) have most parts of the Citadel server that
- * would need to be altered to run the server in a non-POSIX environment.
- * 
- * If we ever port to a different platform and either have multiple
- * variants of this file or simply load it up with #ifdefs.
+ * might need tweaking when run on different operating system variants.
  *
- * Copyright (c) 1987-2011 by the citadel.org team
+ * Copyright (c) 1987-2017 by the citadel.org team
  *
  * This program is open source software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License, version 3.
@@ -39,6 +36,7 @@
 #include <libcitadel.h>
 
 #include "citserver.h"
+#include "config.h"
 #include "ctdl_module.h"
 
 #include "sysdep_decls.h"
@@ -263,35 +261,27 @@ int ctdl_uds_server(char *sockpath, int queue_len, char *errormessage)
 
        s = socket(AF_UNIX, SOCK_STREAM, 0);
        if (s < 0) {
-               snprintf(errormessage, SIZ, 
-                        "citserver: Can't create a socket: %s",
-                        strerror(errno));
+               snprintf(errormessage, SIZ, "citserver: Can't create a socket: %s", strerror(errno));
                syslog(LOG_EMERG, "%s", errormessage);
                return(-1);
        }
 
        if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
-               snprintf(errormessage, SIZ, 
-                        "citserver: Can't bind: %s",
-                        strerror(errno));
+               snprintf(errormessage, SIZ, "citserver: Can't bind: %s", strerror(errno));
                syslog(LOG_EMERG, "%s", errormessage);
                return(-1);
        }
 
        /* set to nonblock - we need this for some obscure situations */
        if (fcntl(s, F_SETFL, O_NONBLOCK) < 0) {
-               snprintf(errormessage, SIZ, 
-                        "citserver: Can't set socket to non-blocking: %s",
-                        strerror(errno));
+               snprintf(errormessage, SIZ, "citserver: Can't set socket to non-blocking: %s", strerror(errno));
                syslog(LOG_EMERG, "%s", errormessage);
                close(s);
                return(-1);
        }
 
        if (listen(s, actual_queue_len) < 0) {
-               snprintf(errormessage, SIZ, 
-                        "citserver: Can't listen: %s",
-                        strerror(errno));
+               snprintf(errormessage, SIZ, "citserver: Can't listen: %s", strerror(errno));
                syslog(LOG_EMERG, "%s", errormessage);
                return(-1);
        }
@@ -405,14 +395,16 @@ int client_write(const char *buf, int nbytes)
                snprintf(fn, SIZ, "/tmp/foolog_%s.%d", Ctx->ServiceName, Ctx->cs_pid);
                
                fd = fopen(fn, "a+");
-               if (fd)
-               {
-                   fprintf(fd, "Sending: BufSize: %d BufContent: [",
-                           nbytes);
-                   rv = fwrite(buf, nbytes, 1, fd);
-                   fprintf(fd, "]\n");
-                   fclose(fd);
+               if (fd == NULL) {
+                       syslog(LOG_EMERG, "failed to open file %s: %s", fn, strerror(errno));
+                       cit_backtrace();
+                       exit(1);
                }
+               fprintf(fd, "Sending: BufSize: %d BufContent: [",
+                       nbytes);
+               rv = fwrite(buf, nbytes, 1, fd);
+               fprintf(fd, "]\n");
+               fclose(fd);
        }
 #endif
 //     flush_client_inbuf();
@@ -530,8 +522,12 @@ int client_read_blob(StrBuf *Target, int bytes, int timeout)
                snprintf(fn, SIZ, "/tmp/foolog_%s.%d", CCC->ServiceName, CCC->cs_pid);
                        
                fd = fopen(fn, "a+");
-               fprintf(fd, "Reading BLOB: BufSize: %d ",
-                       bytes);
+               if (fd == NULL) {
+                       syslog(LOG_EMERG, "failed to open file %s: %s", fn, strerror(errno));
+                       cit_backtrace();
+                       exit(1);
+               }
+               fprintf(fd, "Reading BLOB: BufSize: %d ", bytes);
                rv = fwrite(ChrPtr(Target), StrLength(Target), 1, fd);
                fprintf(fd, "]\n");
                
@@ -546,12 +542,14 @@ int client_read_blob(StrBuf *Target, int bytes, int timeout)
                snprintf(fn, SIZ, "/tmp/foolog_%s.%d", CCC->ServiceName, CCC->cs_pid);
                
                fd = fopen(fn, "a+");
-               fprintf(fd, "Read: %d BufContent: [",
-                       StrLength(Target));
+               if (fd == NULL) {
+                       syslog(LOG_EMERG, "failed to open file %s: %s", fn, strerror(errno));
+                       cit_backtrace();
+                       exit(1);
+               }
+               fprintf(fd, "Read: %d BufContent: [", StrLength(Target));
                rv = fwrite(ChrPtr(Target), StrLength(Target), 1, fd);
                fprintf(fd, "]\n");
-               
-               
                fclose(fd);
 #endif
        }
@@ -566,12 +564,15 @@ int client_read_blob(StrBuf *Target, int bytes, int timeout)
                snprintf(fn, SIZ, "/tmp/foolog_%s.%d", CCC->ServiceName, CCC->cs_pid);
                        
                fd = fopen(fn, "a+");
+               if (fd == NULL) {
+                       syslog(LOG_EMERG, "failed to open file %s: %s", fn, strerror(errno));
+                       cit_backtrace();
+                       exit(1);
+               }
                fprintf(fd, "Reading BLOB: BufSize: %d ",
                        bytes);
                rv = fwrite(ChrPtr(Target), StrLength(Target), 1, fd);
                fprintf(fd, "]\n");
-               
-                       
                fclose(fd);
 #endif
                retval = StrBufReadBLOBBuffered(Target, 
@@ -581,7 +582,8 @@ int client_read_blob(StrBuf *Target, int bytes, int timeout)
                                                1, 
                                                bytes,
                                                O_TERM,
-                                               &Error);
+                                               &Error
+               );
                if (retval < 0) {
                        syslog(LOG_CRIT, "client_read_blob() failed: %s", Error);
                        client_close();
@@ -591,6 +593,11 @@ int client_read_blob(StrBuf *Target, int bytes, int timeout)
                snprintf(fn, SIZ, "/tmp/foolog_%s.%d", CCC->ServiceName, CCC->cs_pid);
                
                fd = fopen(fn, "a+");
+               if (fd == NULL) {
+                       syslog(LOG_EMERG, "failed to open file %s: %s", fn, strerror(errno));
+                       cit_backtrace();
+                       exit(1);
+               }
                fprintf(fd, "Read: %d BufContent: [",
                        StrLength(Target));
                rv = fwrite(ChrPtr(Target), StrLength(Target), 1, fd);
@@ -645,16 +652,18 @@ int client_read_random_blob(StrBuf *Target, int timeout)
                                snprintf(fn, SIZ, "/tmp/foolog_%s.%d", CCC->ServiceName, CCC->cs_pid);
                        
                                fd = fopen(fn, "a+");
+                               if (fd == NULL) {
+                                       syslog(LOG_EMERG, "failed to open file %s: %s", fn, strerror(errno));
+                                       cit_backtrace();
+                                       exit(1);
+                               }
                                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;
@@ -704,7 +713,7 @@ int HaveMoreLinesWaiting(CitContext *CCC)
  */
 INLINE int client_read(char *buf, int bytes)
 {
-       return(client_read_to(buf, bytes, config.c_sleeping));
+       return(client_read_to(buf, bytes, CtdlGetConfigInt("c_sleeping")));
 }
 
 int CtdlClientGetLine(StrBuf *Target)
@@ -728,6 +737,11 @@ int CtdlClientGetLine(StrBuf *Target)
                snprintf(fn, SIZ, "/tmp/foolog_%s.%d", CCC->ServiceName, CCC->cs_pid);
 
                fd = fopen(fn, "a+");
+               if (fd == NULL) {
+                       syslog(LOG_EMERG, "failed to open file %s: %s", fn, strerror(errno));
+                       cit_backtrace();
+                       exit(1);
+               }
                pch = ChrPtr(CCC->RecvBuf.Buf);
                len = StrLength(CCC->RecvBuf.Buf);
                if (CCC->RecvBuf.ReadWritePointer != NULL)
@@ -781,6 +795,11 @@ int CtdlClientGetLine(StrBuf *Target)
                snprintf(fn, SIZ, "/tmp/foolog_%s.%d", CCC->ServiceName, CCC->cs_pid);
 
                fd = fopen(fn, "a+");
+               if (fd == NULL) {
+                       syslog(LOG_EMERG, "failed to open file %s: %s", fn, strerror(errno));
+                       cit_backtrace();
+                       exit(1);
+               }
                pch = ChrPtr(CCC->RecvBuf.Buf);
                len = StrLength(CCC->RecvBuf.Buf);
                if (CCC->RecvBuf.ReadWritePointer != NULL)
@@ -800,7 +819,8 @@ int CtdlClientGetLine(StrBuf *Target)
                                                       &CCC->client_socket,
                                                       5,
                                                       1,
-                                                      &Error);
+                                                      &Error
+               );
 
 #ifdef BIGBAD_IODBG
                 pch = ChrPtr(CCC->RecvBuf.Buf);
@@ -890,10 +910,11 @@ void close_masters (void)
                        else
                                Text = "Closing";
                                        
-                       syslog(LOG_INFO, "%s %d listener on port %d\n",
+                       syslog(LOG_INFO, "%s %d listener on port %d",
                               Text,
                               serviceptr->msock,
-                              serviceptr->tcp_port);
+                              serviceptr->tcp_port
+                       );
                        serviceptr->tcp_port = 0;
                }
                
@@ -904,10 +925,11 @@ void close_masters (void)
                        else
                                Text = "Closing";
 
-                       syslog(LOG_INFO, "%s %d listener on '%s'\n",
+                       syslog(LOG_INFO, "%s %d listener on '%s'",
                               Text,
                               serviceptr->msock,
-                              serviceptr->sockpath);
+                              serviceptr->sockpath
+                       );
                }
 
                 if (serviceptr->msock != -1)
@@ -1063,7 +1085,6 @@ void checkcrash(void)
        if (nFireUpsNonRestart != nFireUps)
        {
                StrBuf *CrashMail;
-
                CrashMail = NewStrBuf();
                syslog(LOG_ALERT, "Posting crash message\n");
                StrBufPrintf(CrashMail, 
@@ -1092,7 +1113,7 @@ void checkcrash(void)
  */
 int convert_login(char NameToConvert[]) {
        struct passwd *pw;
-       int a;
+       unsigned int a;
 
        pw = getpwnam(NameToConvert);
        if (pw == NULL) {
@@ -1189,7 +1210,7 @@ void HuntBadSession(void)
 
 }
 
-
+const char *WorkerLogStr = "W";
 /* 
  * This loop just keeps going and going and going...
  */
@@ -1210,6 +1231,8 @@ void *worker_thread(void *blah) {
        ++num_workers;
        pthread_mutex_unlock(&ThreadCountMutex);
 
+       pthread_setspecific(evConKey, WorkerLogStr);
+
        while (!server_shutting_down) {
 
                /* make doubly sure we're not holding any stale db handles
@@ -1323,9 +1346,7 @@ do_select:        force_purge = 0;
                                         * operations barf on FreeBSD.  Not a fatal error.
                                         */
                                        if (fcntl(ssock, F_SETFL, 0) < 0) {
-                                               syslog(LOG_EMERG,
-                                                       "citserver: Can't set socket to blocking: %s\n",
-                                                       strerror(errno));
+                                               syslog(LOG_EMERG, "citserver: Can't set socket to blocking: %s", strerror(errno));
                                        }
 
                                        /* New context will be created already
@@ -1431,8 +1452,8 @@ SKIP_SELECT:
 
                pthread_mutex_lock(&ThreadCountMutex);
                --active_workers;
-               if ((active_workers + config.c_min_workers < num_workers) &&
-                   (num_workers > config.c_min_workers))
+               if ((active_workers + CtdlGetConfigInt("c_min_workers") < num_workers) &&
+                   (num_workers > CtdlGetConfigInt("c_min_workers")))
                {
                        num_workers--;
                        pthread_mutex_unlock(&ThreadCountMutex);