bosserver force corefiles
[openafs.git] / src / bozo / bnode.c
index 9d32684..7596cad 100644 (file)
@@ -10,8 +10,6 @@
 #include <afsconfig.h>
 #include <afs/param.h>
 
-RCSID
-    ("$Header$");
 
 #include <stddef.h>
 #include <stdlib.h>
@@ -19,6 +17,9 @@ RCSID
 #include <dirent.h>
 #include <errno.h>
 #include <sys/types.h>
+#ifdef HAVE_STDINT_H
+# include <stdint.h>
+#endif
 #ifdef AFS_NT40_ENV
 #include <io.h>
 #else
@@ -33,15 +34,15 @@ RCSID
 
 #include <afs/procmgmt.h>      /* signal(), kill(), wait(), etc. */
 #include <lwp.h>
+#include <rx/rx.h>
 #include <afs/audit.h>
 #include <afs/afsutil.h>
 #include <afs/fileutil.h>
 #include "bnode.h"
 #include "bosprototypes.h"
 
-#if defined(AFS_AIX_ENV) || defined(AFS_SUN4_ENV)
-/* All known versions of AIX lack WCOREDUMP but this works */
-#define WCOREDUMP(x) ((x) & 0x80)
+#ifndef WCOREDUMP
+#define WCOREDUMP(x) ((x) & 0200)
 #endif
 
 #define BNODE_LWP_STACKSIZE    (16 * 1024)
@@ -56,10 +57,13 @@ static struct bnode_stats {
     int weirdPids;
 } bnode_stats;
 
+extern const char *DoCore;
 #ifndef AFS_NT40_ENV
 extern char **environ;         /* env structure */
 #endif
 
+int hdl_notifier(struct bnode_proc *tp);
+
 /* Remember the name of the process, if any, that failed last */
 static void
 RememberProcName(register struct bnode_proc *ap)
@@ -83,7 +87,12 @@ RememberProcName(register struct bnode_proc *ap)
 int
 bnode_CoreName(register struct bnode *abnode, char *acoreName, char *abuffer)
 {
-    strcpy(abuffer, AFSDIR_SERVER_CORELOG_FILEPATH);
+    if (DoCore) {
+       strcpy(abuffer, DoCore);
+       strcat(abuffer, "/");
+       strcat(abuffer, AFSDIR_CORE_FILE);
+    } else
+       strcpy(abuffer, AFSDIR_SERVER_CORELOG_FILEPATH);
     if (acoreName) {
        strcat(abuffer, acoreName);
        strcat(abuffer, ".");
@@ -99,7 +108,7 @@ SaveCore(register struct bnode *abnode, register struct bnode_proc
 {
     char tbuffer[256];
     struct stat tstat;
-    register afs_int32 code;
+    register afs_int32 code = 0;
     char *corefile = NULL;
 #ifdef BOZO_SAVE_CORES
     struct timeval Start;
@@ -109,15 +118,23 @@ SaveCore(register struct bnode *abnode, register struct bnode_proc
 
     /* Linux always appends the PID to core dumps from threaded processes, so
      * we have to scan the directory to find core files under another name. */
-    code = stat(AFSDIR_SERVER_CORELOG_FILEPATH, &tstat);
+    if (DoCore) {
+       strcpy(tbuffer, DoCore);
+       strcat(tbuffer, "/");
+       strcat(tbuffer, AFSDIR_CORE_FILE);
+    } else
+       code = stat(AFSDIR_SERVER_CORELOG_FILEPATH, &tstat);
     if (code) {
         DIR *logdir;
         struct dirent *file;
-        char *p;
         size_t length;
         unsigned long pid;
+       const char *coredir = AFSDIR_LOGS_DIR;
 
-        logdir = opendir(AFSDIR_LOGS_DIR);
+       if (DoCore)
+         coredir = DoCore;
+
+       logdir = opendir(coredir);
         if (logdir == NULL)
             return;
         while ((file = readdir(logdir)) != NULL) {
@@ -125,19 +142,20 @@ SaveCore(register struct bnode *abnode, register struct bnode_proc
                 continue;
             pid = atol(file->d_name + 5);
             if (pid == aproc->pid) {
-                length = strlen(AFSDIR_LOGS_DIR) + strlen(file->d_name) + 2;
+                length = strlen(coredir) + strlen(file->d_name) + 2;
                 corefile = malloc(length);
                 if (corefile == NULL) {
                     closedir(logdir);
                     return;
                 }
-                snprintf(corefile, length, "%s/%s", AFSDIR_LOGS_DIR,
-                         file->d_name);
+                snprintf(corefile, length, "%s/%s", coredir, file->d_name);
                 code = 0;
                 break;
             }
         }
         closedir(logdir);
+    } else {
+       corefile = strdup(tbuffer);
     }
     if (code)
        return;
@@ -151,12 +169,8 @@ SaveCore(register struct bnode *abnode, register struct bnode_proc
            TimeFields->tm_hour, TimeFields->tm_min, TimeFields->tm_sec);
     strcpy(tbuffer, FileName);
 #endif
-    if (corefile == NULL)
-        code = renamefile(AFSDIR_SERVER_CORELOG_FILEPATH, tbuffer);
-    else {
-        code = renamefile(corefile, tbuffer);
-        free(corefile);
-    }
+    code = renamefile(corefile, tbuffer);
+    free(corefile);
 }
 
 int
@@ -204,7 +218,7 @@ bnode_HasCore(register struct bnode *abnode)
 
 /* wait for all bnodes to stabilize */
 int
-bnode_WaitAll()
+bnode_WaitAll(void)
 {
     register struct bnode *tb;
     register afs_int32 code;
@@ -288,7 +302,7 @@ bnode_SetFileGoal(register struct bnode *abnode, register int agoal)
 
 /* apply a function to all bnodes in the system */
 int
-bnode_ApplyInstance(int (*aproc) (), char *arock)
+bnode_ApplyInstance(int (*aproc) (struct bnode *tb, void *), void *arock)
 {
     register struct bnode *tb, *nb;
     register afs_int32 code;
@@ -700,15 +714,15 @@ SendNotifierData(register int fd, register struct bnode_proc *tp)
        buf1 = "(null)";
     (void)sprintf(bufp, "coreName: %s\n", buf1);
     bufp += strlen(bufp);
-    (void)sprintf(bufp, "pid: %ld\n", tp->pid);
+    (void)sprintf(bufp, "pid: %ld\n", afs_printable_int32_ld(tp->pid));
     bufp += strlen(bufp);
-    (void)sprintf(bufp, "lastExit: %ld\n", tp->lastExit);
+    (void)sprintf(bufp, "lastExit: %ld\n", afs_printable_int32_ld(tp->lastExit));
     bufp += strlen(bufp);
 #ifdef notdef
-    (void)sprintf(bufp, "lastSignal: %ld\n", tp->lastSignal);
+    (void)sprintf(bufp, "lastSignal: %ld\n", afs_printable_int32_ld(tp->lastSignal));
     bufp += strlen(bufp);
 #endif
-    (void)sprintf(bufp, "flags: %ld\n", tp->flags);
+    (void)sprintf(bufp, "flags: %ld\n", afs_printable_int32_ld(tp->flags));
     bufp += strlen(bufp);
     (void)sprintf(bufp, "END bnode_proc\n");
     bufp += strlen(bufp);
@@ -725,21 +739,21 @@ SendNotifierData(register int fd, register struct bnode_proc *tp)
     bufp += strlen(bufp);
     (void)sprintf(bufp, "name: %s\n", tb->name);
     bufp += strlen(bufp);
-    (void)sprintf(bufp, "rsTime: %ld\n", tb->rsTime);
+    (void)sprintf(bufp, "rsTime: %ld\n", afs_printable_int32_ld(tb->rsTime));
     bufp += strlen(bufp);
-    (void)sprintf(bufp, "rsCount: %ld\n", tb->rsCount);
+    (void)sprintf(bufp, "rsCount: %ld\n", afs_printable_int32_ld(tb->rsCount));
     bufp += strlen(bufp);
-    (void)sprintf(bufp, "procStartTime: %ld\n", tb->procStartTime);
+    (void)sprintf(bufp, "procStartTime: %ld\n", afs_printable_int32_ld(tb->procStartTime));
     bufp += strlen(bufp);
-    (void)sprintf(bufp, "procStarts: %ld\n", tb->procStarts);
+    (void)sprintf(bufp, "procStarts: %ld\n", afs_printable_int32_ld(tb->procStarts));
     bufp += strlen(bufp);
-    (void)sprintf(bufp, "lastAnyExit: %ld\n", tb->lastAnyExit);
+    (void)sprintf(bufp, "lastAnyExit: %ld\n", afs_printable_int32_ld(tb->lastAnyExit));
     bufp += strlen(bufp);
-    (void)sprintf(bufp, "lastErrorExit: %ld\n", tb->lastErrorExit);
+    (void)sprintf(bufp, "lastErrorExit: %ld\n", afs_printable_int32_ld(tb->lastErrorExit));
     bufp += strlen(bufp);
-    (void)sprintf(bufp, "errorCode: %ld\n", tb->errorCode);
+    (void)sprintf(bufp, "errorCode: %ld\n", afs_printable_int32_ld(tb->errorCode));
     bufp += strlen(bufp);
-    (void)sprintf(bufp, "errorSignal: %ld\n", tb->errorSignal);
+    (void)sprintf(bufp, "errorSignal: %ld\n", afs_printable_int32_ld(tb->errorSignal));
     bufp += strlen(bufp);
 /*
     (void) sprintf(bufp, "lastErrorName: %s\n", tb->lastErrorName);
@@ -753,13 +767,14 @@ SendNotifierData(register int fd, register struct bnode_proc *tp)
     if (write(fd, buffer, len) < 0) {
        return -1;
     }
+    return 0;
 }
 
 int
 hdl_notifier(struct bnode_proc *tp)
 {
 #ifndef AFS_NT40_ENV           /* NT notifier callout not yet implemented */
-    int code, pid, status;
+    int code, pid;
     struct stat tstat;
 
     if (stat(tp->bnode->notifier, &tstat)) {
@@ -817,22 +832,22 @@ bnode_SoftInt(void *param)
 void
 bnode_Int(int asignal)
 {
-    if (asignal == SIGQUIT) {
-       IOMGR_SoftSig(bozo_ShutdownAndExit, (void *) asignal);
+    if (asignal == SIGQUIT || asignal == SIGTERM) {
+       IOMGR_SoftSig(bozo_ShutdownAndExit, (void *)(intptr_t)asignal);
     } else {
-       IOMGR_SoftSig(bnode_SoftInt, (void *) asignal);
+       IOMGR_SoftSig(bnode_SoftInt, (void *)(intptr_t)asignal);
     }
 }
 
 
 /* intialize the whole system */
 int
-bnode_Init()
+bnode_Init(void)
 {
     PROCESS junk;
     register afs_int32 code;
     struct sigaction newaction;
-    static initDone = 0;
+    static int initDone = 0;
 
     if (initDone)
        return 0;
@@ -845,7 +860,7 @@ bnode_Init()
                             "bnode-manager", &bproc_pid);
     if (code)
        return code;
-    memset((char *)&newaction, 0, sizeof(newaction));
+    memset(&newaction, 0, sizeof(newaction));
     newaction.sa_handler = bnode_Int;
     code = sigaction(SIGCHLD, &newaction, NULL);
     if (code)
@@ -853,6 +868,9 @@ bnode_Init()
     code = sigaction(SIGQUIT, &newaction, NULL);
     if (code)
        return errno;
+    code = sigaction(SIGTERM, &newaction, NULL);
+    if (code)
+       return errno;
     return code;
 }
 
@@ -882,7 +900,7 @@ int
 bnode_ParseLine(char *aline, struct bnode_token **alist)
 {
     char tbuffer[256];
-    register char *tptr;
+    register char *tptr = NULL;
     int inToken;
     struct bnode_token *first, *last;
     register struct bnode_token *ttok;
@@ -928,6 +946,7 @@ bnode_ParseLine(char *aline, struct bnode_token **alist)
            return 0;
        }
     }
+    return 0;
 }
 
 #define        MAXVARGS            128