Send crashmessage just once per day.
[citadel.git] / citadel / sysdep.c
index f8ff6df46eb0b0c06aa7132e6c80ecdfd12c0bd7..787a93a3df8eae8cb16b3ab76232e1af009f8fea 100644 (file)
  */
 
 #include "sysdep.h"
-#include <stdlib.h>
-#include <unistd.h>
-#include <stdio.h>
-#include <fcntl.h>
-#include <ctype.h>
+
+#include <errno.h>
 #include <signal.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <sys/wait.h>
-#include <sys/socket.h>
+#include <stdio.h>
 #include <syslog.h>
 #include <sys/syslog.h>
 
-#if TIME_WITH_SYS_TIME
-# include <sys/time.h>
-# include <time.h>
-#else
-# if HAVE_SYS_TIME_H
-#  include <sys/time.h>
-# else
-#  include <time.h>
-# endif
-#endif
 
-#include <limits.h>
-#include <sys/resource.h>
+#include <sys/un.h>
+#include <sys/types.h>
+#include <sys/socket.h>
 #include <netinet/in.h>
+#include <arpa/inet.h>
 #include <netinet/tcp.h>
 #include <arpa/inet.h>
-#include <netdb.h>
-#include <sys/un.h>
-#include <string.h>
-#include <pwd.h>
-#include <errno.h>
-#include <stdarg.h>
-#include <grp.h>
+
 #define SHOW_ME_VAPPEND_PRINTF
 #include <libcitadel.h>
-#include "citadel.h"
-#include "server.h"
-#include "sysdep_decls.h"
-#include "citserver.h"
-#include "support.h"
-#include "config.h"
-#include "database.h"
-#include "housekeeping.h"
-#include "modules/crypto/serv_crypto.h"        /* Needed for init_ssl, client_write_ssl, client_read_ssl, destruct_ssl */
-#include "ecrash.h"
-#include "context.h"
-
-#ifdef HAVE_SYS_SELECT_H
-#include <sys/select.h>
-#endif
 
+#include "citserver.h"
 #include "ctdl_module.h"
-#include "threads.h"
-#include "user_ops.h"
-#include "control.h"
 
+#include "sysdep_decls.h"
+#include "modules/crypto/serv_crypto.h"        /* Needed for init_ssl, client_write_ssl, client_read_ssl, destruct_ssl */
 
+#include "housekeeping.h"
+#include "context.h"
 /*
  * Signal handler to shut down the server.
  */
@@ -438,14 +405,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();
@@ -563,6 +532,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, "Reading BLOB: BufSize: %d ",
                        bytes);
                rv = fwrite(ChrPtr(Target), StrLength(Target), 1, fd);
@@ -579,6 +553,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);
@@ -599,6 +578,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, "Reading BLOB: BufSize: %d ",
                        bytes);
                rv = fwrite(ChrPtr(Target), StrLength(Target), 1, fd);
@@ -624,6 +608,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);
@@ -678,6 +667,11 @@ 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);
@@ -761,6 +755,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)
@@ -814,6 +813,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)
@@ -1096,7 +1100,8 @@ void checkcrash(void)
        if (nFireUpsNonRestart != nFireUps)
        {
                StrBuf *CrashMail;
-
+               const char *msgs[1] = {"crash"};
+               const long lens[1] = {sizeof("crash") - 1};
                CrashMail = NewStrBuf();
                syslog(LOG_ALERT, "Posting crash message\n");
                StrBufPrintf(CrashMail, 
@@ -1113,7 +1118,11 @@ void checkcrash(void)
                        " If you have already done this, the core dump is likely to be found at %score.%d\n"
                        ,
                        ctdl_run_dir, ForkedPid);
-               CtdlAideMessage(ChrPtr(CrashMail), "Citadel server process terminated unexpectedly");
+               CtdlAideFPMessage(ChrPtr(CrashMail),
+                                 "Citadel server process terminated unexpectedly",
+                                 1, msgs, lens,
+                                 0, 0,
+                                 time(NULL));
                FreeStrBuf(&CrashMail);
        }
 }
@@ -1125,7 +1134,7 @@ void checkcrash(void)
  */
 int convert_login(char NameToConvert[]) {
        struct passwd *pw;
-       int a;
+       unsigned int a;
 
        pw = getpwnam(NameToConvert);
        if (pw == NULL) {
@@ -1222,7 +1231,7 @@ void HuntBadSession(void)
 
 }
 
-
+const char *WorkerLogStr = "W";
 /* 
  * This loop just keeps going and going and going...
  */
@@ -1243,6 +1252,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