bozo: avoid canceling the sigkill timer for hung processes
[openafs.git] / src / bozo / fsbnodeops.c
index 5b44ba0..e93a6d7 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Copyright 2000, International Business Machines Corporation and others.
  * All Rights Reserved.
- * 
+ *
  * This software has been released under the terms of the IBM Public
  * License.  For details, see the LICENSE file in the top-level source
  * directory or online at http://www.openafs.org/dl/license10.html
 #include <afsconfig.h>
 #include <afs/param.h>
 
-RCSID
-    ("$Header$");
+#include <afs/procmgmt.h>
+#include <roken.h>
+#include <afs/opr.h>
 
-#include <sys/types.h>
 #include <lwp.h>
 #include <rx/rx.h>
-#include <errno.h>
-#include <stdio.h>
-#ifdef AFS_SUN5_ENV
-#include <fcntl.h>
-#endif
-#ifdef AFS_NT40_ENV
-#include <io.h>
-#include <fcntl.h>
-#else
-#include <sys/file.h>
-
-#include <string.h>
-#include <stdlib.h>
-
-#endif /* AFS_NT40_ENV */
-#include <sys/stat.h>
-#include <afs/procmgmt.h>      /* signal(), kill(), wait(), etc. */
 #include <afs/afsutil.h>
+
 #include "bnode.h"
 #include "bosprototypes.h"
 
+extern char *DoPidFiles;
 static int emergency = 0;
 
 /* if this file exists, then we have to salvage the file system */
@@ -47,20 +32,20 @@ static int emergency = 0;
 
 /*  basic rules:
     Normal operation involves having the file server and the vol server both running.
-    
+
     If the vol server terminates, it can simply be restarted.
-    
+
     If the file server terminates, the disk must salvaged before the file server
     can be restarted.  In order to restart either the file server or the salvager,
     the vol server must be shut down.
-    
+
     If the file server terminates *normally* (exits after receiving a SIGQUIT)
     then we don't have to salvage it.
-    
+
     The needsSalvage flag is set when the file server is started.  It is cleared
     if the file server exits when fileSDW is true but fileKillSent is false,
     indicating that it exited after receiving a quit, but before we sent it a kill.
-    
+
     The needsSalvage flag is cleared when the salvager exits.
 */
 
@@ -100,9 +85,9 @@ struct fsbnode {
     char needsClock;           /* do we need clock ticks */
 };
 
-struct bnode * fs_create(char *ainstance, char *afilecmd, char *avolcmd, 
-                        char *asalcmd, char *ascancmd);
-struct bnode * dafs_create(char *ainstance, char *afilecmd, char *avolcmd, 
+struct bnode * fs_create(char *ainstance, char *afilecmd, char *avolcmd,
+                        char *asalcmd, char *ascancmd, char *dummy);
+struct bnode * dafs_create(char *ainstance, char *afilecmd, char *avolcmd,
                           char * asalsrvcmd, char *asalcmd, char *ascancmd);
 
 static int fs_hascore(struct bnode *abnode);
@@ -111,11 +96,12 @@ static int fs_delete(struct bnode *abnode);
 static int fs_timeout(struct bnode *abnode);
 static int fs_getstat(struct bnode *abnode, afs_int32 * astatus);
 static int fs_setstat(struct bnode *abnode, afs_int32 astatus);
+static int fs_procstarted(struct bnode *abnode, struct bnode_proc *aproc);
 static int fs_procexit(struct bnode *abnode, struct bnode_proc *aproc);
 static int fs_getstring(struct bnode *abnode, char *abuffer, afs_int32 alen);
-static int fs_getparm(struct bnode *abnode, afs_int32 aindex, 
+static int fs_getparm(struct bnode *abnode, afs_int32 aindex,
                      char *abuffer, afs_int32 alen);
-static int dafs_getparm(struct bnode *abnode, afs_int32 aindex, 
+static int dafs_getparm(struct bnode *abnode, afs_int32 aindex,
                        char *abuffer, afs_int32 alen);
 
 static int SetSalFlag(struct fsbnode *abnode, int aflag);
@@ -140,6 +126,7 @@ struct bnode_ops fsbnode_ops = {
     fs_getparm,
     fs_restartp,
     fs_hascore,
+    fs_procstarted,
 };
 
 /* demand attach fs bnode ops */
@@ -154,13 +141,15 @@ struct bnode_ops dafsbnode_ops = {
     dafs_getparm,
     fs_restartp,
     fs_hascore,
+    fs_procstarted,
 };
 
 /* Quick inline function to safely convert a fsbnode to a bnode without
  * dropping type information
  */
 
-static_inline struct bnode * fsbnode2bnode(struct fsbnode *abnode) {
+static_inline struct bnode *
+fsbnode2bnode(struct fsbnode *abnode) {
     return (struct bnode *) abnode;
 }
 
@@ -209,7 +198,7 @@ fs_restartp(struct bnode *bn)
 {
     struct fsbnode *abnode = (struct fsbnode *)bn;
     struct bnode_token *tt;
-    register afs_int32 code;
+    afs_int32 code;
     struct stat tstat;
 
     code = bnode_ParseLine(abnode->filecmd, &tt);
@@ -261,7 +250,7 @@ fs_restartp(struct bnode *bn)
            bnode_FreeTokens(tt);
            return 0;
        }
-       if (tstat.st_ctime > abnode->lastScanStart)
+       if (tstat.st_ctime > abnode->lastSalsrvStart)
            code = 1;
        else
            code = 0;
@@ -293,7 +282,7 @@ fs_restartp(struct bnode *bn)
 /* set needsSalvage flag, creating file SALVAGE.<instancename> if
     we need to salvage the file system (so we can tell over panic reboots */
 static int
-SetSalFlag(register struct fsbnode *abnode, register int aflag)
+SetSalFlag(struct fsbnode *abnode, int aflag)
 {
     char tbuffer[AFSDIR_PATH_MAX];
     int fd;
@@ -302,7 +291,7 @@ SetSalFlag(register struct fsbnode *abnode, register int aflag)
     if (abnode->salsrvcmd == NULL) {
        abnode->needsSalvage = aflag;
        strcompose(tbuffer, AFSDIR_PATH_MAX, AFSDIR_SERVER_LOCAL_DIRPATH, "/",
-                  SALFILE, abnode->b.name, NULL);
+                  SALFILE, abnode->b.name, (char *)NULL);
        if (aflag) {
            fd = open(tbuffer, O_CREAT | O_TRUNC | O_RDWR, 0666);
            close(fd);
@@ -315,7 +304,7 @@ SetSalFlag(register struct fsbnode *abnode, register int aflag)
 
 /* set the needsSalvage flag according to the existence of the salvage file */
 static int
-RestoreSalFlag(register struct fsbnode *abnode)
+RestoreSalFlag(struct fsbnode *abnode)
 {
     char tbuffer[AFSDIR_PATH_MAX];
 
@@ -324,7 +313,7 @@ RestoreSalFlag(register struct fsbnode *abnode)
        abnode->needsSalvage = 0;
     } else {
        strcompose(tbuffer, AFSDIR_PATH_MAX, AFSDIR_SERVER_LOCAL_DIRPATH, "/",
-                  SALFILE, abnode->b.name, NULL);
+                  SALFILE, abnode->b.name, (char *)NULL);
        if (access(tbuffer, 0) == 0) {
            /* file exists, so need to salvage */
            abnode->needsSalvage = 1;
@@ -335,20 +324,11 @@ RestoreSalFlag(register struct fsbnode *abnode)
     return 0;
 }
 
-char *
-copystr(register char *a)
-{
-    register char *b;
-    b = (char *)malloc(strlen(a) + 1);
-    strcpy(b, a);
-    return b;
-}
-
 static int
 fs_delete(struct bnode *bn)
 {
     struct fsbnode *abnode = (struct fsbnode *)bn;
-    
+
     free(abnode->filecmd);
     free(abnode->volcmd);
     free(abnode->salcmd);
@@ -378,17 +358,17 @@ AppendExecutableExtension(char *cmd)
 
 struct bnode *
 fs_create(char *ainstance, char *afilecmd, char *avolcmd, char *asalcmd,
-         char *ascancmd)
+         char *ascancmd, char *dummy)
 {
     struct stat tstat;
-    register struct fsbnode *te;
+    struct fsbnode *te;
     char cmdname[AFSDIR_PATH_MAX];
     char *fileCmdpath, *volCmdpath, *salCmdpath, *scanCmdpath;
     int bailout = 0;
 
     fileCmdpath = volCmdpath = salCmdpath = scanCmdpath = NULL;
-    te = NULL; 
-    
+    te = NULL;
+
     /* construct local paths from canonical (wire-format) paths */
     if (ConstructLocalBinPath(afilecmd, &fileCmdpath)) {
        bozo_Log("BNODE: command path invalid '%s'\n", afilecmd);
@@ -450,12 +430,11 @@ fs_create(char *ainstance, char *afilecmd, char *avolcmd, char *asalcmd,
        }
     }
 
-    te = (struct fsbnode *)malloc(sizeof(struct fsbnode));
+    te = calloc(1, sizeof(struct fsbnode));
     if (te == NULL) {
        bailout = 1;
        goto done;
     }
-    memset(te, 0, sizeof(struct fsbnode));
     te->filecmd = fileCmdpath;
     te->volcmd = volCmdpath;
     te->salsrvcmd = NULL;
@@ -468,8 +447,8 @@ fs_create(char *ainstance, char *afilecmd, char *avolcmd, char *asalcmd,
        bailout = 1;
        goto done;
     }
-    bnode_SetTimeout(fsbnode2bnode(te), POLLTIME);     
-               /* ask for timeout activations every 10 seconds */
+    bnode_SetTimeout(fsbnode2bnode(te), POLLTIME);
+               /* ask for timeout activations every 20 seconds */
     RestoreSalFlag(te);                /* restore needsSalvage flag based on file's existence */
     SetNeedsClock(te);         /* compute needsClock field */
 
@@ -493,18 +472,18 @@ fs_create(char *ainstance, char *afilecmd, char *avolcmd, char *asalcmd,
 
 /* create a demand attach fs bnode */
 struct bnode *
-dafs_create(char *ainstance, char *afilecmd, char *avolcmd, 
+dafs_create(char *ainstance, char *afilecmd, char *avolcmd,
            char * asalsrvcmd, char *asalcmd, char *ascancmd)
 {
     struct stat tstat;
-    register struct fsbnode *te;
+    struct fsbnode *te;
     char cmdname[AFSDIR_PATH_MAX];
     char *fileCmdpath, *volCmdpath, *salsrvCmdpath, *salCmdpath, *scanCmdpath;
     int bailout = 0;
 
     fileCmdpath = volCmdpath = salsrvCmdpath = salCmdpath = scanCmdpath = NULL;
     te = NULL;
-    
+
     /* construct local paths from canonical (wire-format) paths */
     if (ConstructLocalBinPath(afilecmd, &fileCmdpath)) {
        bozo_Log("BNODE: command path invalid '%s'\n", afilecmd);
@@ -579,12 +558,11 @@ dafs_create(char *ainstance, char *afilecmd, char *avolcmd,
        }
     }
 
-    te = (struct fsbnode *)malloc(sizeof(struct fsbnode));
+    te = calloc(1, sizeof(struct fsbnode));
     if (te == NULL) {
        bailout = 1;
        goto done;
     }
-    memset(te, 0, sizeof(struct fsbnode));
     te->filecmd = fileCmdpath;
     te->volcmd = volCmdpath;
     te->salsrvcmd = salsrvCmdpath;
@@ -597,8 +575,8 @@ dafs_create(char *ainstance, char *afilecmd, char *avolcmd,
        bailout = 1;
        goto done;
     }
-    bnode_SetTimeout(fsbnode2bnode(te), POLLTIME);     
-               /* ask for timeout activations every 10 seconds */
+    bnode_SetTimeout(fsbnode2bnode(te), POLLTIME);
+               /* ask for timeout activations every 20 seconds */
     RestoreSalFlag(te);                /* restore needsSalvage flag based on file's existence */
     SetNeedsClock(te);         /* compute needsClock field */
 
@@ -627,8 +605,8 @@ static int
 fs_timeout(struct bnode *bn)
 {
     struct fsbnode *abnode = (struct fsbnode *)bn;
-    
-    register afs_int32 now;
+
+    afs_int32 now;
 
     now = FT_ApproxTime();
     /* shutting down */
@@ -677,6 +655,16 @@ fs_timeout(struct bnode *bn)
                 SDTIME);
        }
     }
+
+    if ((abnode->b.flags & BNODE_ERRORSTOP) && !abnode->salRunning
+       && !abnode->volRunning && !abnode->fileRunning && !abnode->scanRunning
+       && !abnode->salsrvRunning) {
+       bnode_SetStat(bn, BSTAT_NORMAL);
+    }
+    else {
+       bnode_ResetErrorCount(bn);
+    }
+
     SetNeedsClock(abnode);
     return 0;
 }
@@ -685,8 +673,8 @@ static int
 fs_getstat(struct bnode *bn, afs_int32 * astatus)
 {
     struct fsbnode *abnode = (struct fsbnode *) bn;
-    
-    register afs_int32 temp;
+
+    afs_int32 temp;
     if (abnode->volSDW || abnode->fileSDW || abnode->salSDW
        || abnode->scanSDW || abnode->salsrvSDW)
        temp = BSTAT_SHUTTINGDOWN;
@@ -713,12 +701,27 @@ fs_setstat(struct bnode *abnode, afs_int32 astatus)
 }
 
 static int
+fs_procstarted(struct bnode *bn, struct bnode_proc *aproc)
+{
+    int code = 0;
+
+    if (DoPidFiles) {
+       code = bozo_CreatePidFile(bn->name, aproc->coreName, aproc->pid);
+    }
+    return code;
+}
+
+static int
 fs_procexit(struct bnode *bn, struct bnode_proc *aproc)
 {
    struct fsbnode *abnode = (struct fsbnode *)bn;
-   
+
     /* process has exited */
 
+    if (DoPidFiles) {
+       bozo_DeletePidFile(bn->name, aproc->coreName);
+    }
+
     if (aproc == abnode->volProc) {
        abnode->volProc = 0;
        abnode->volRunning = 0;
@@ -764,28 +767,49 @@ fs_procexit(struct bnode *bn, struct bnode_proc *aproc)
 
 /* make sure we're periodically checking the state if we need to */
 static void
-SetNeedsClock(register struct fsbnode *ab)
+SetNeedsClock(struct fsbnode *ab)
 {
-    if (ab->b.goal == 1 && ab->fileRunning && ab->volRunning
+    afs_int32 timeout = POLLTIME;
+
+    if ((ab->fileSDW && !ab->fileKillSent) || (ab->volSDW && !ab->volKillSent)
+       || (ab->scanSDW && !ab->scanKillSent) || (ab->salSDW && !ab->salKillSent)
+       || (ab->salsrvSDW && !ab->salsrvKillSent)) {
+       /* SIGQUIT sent, will send SIGKILL if process does not exit */
+       ab->needsClock = 1;
+    } else if (ab->b.goal == 1 && ab->fileRunning && ab->volRunning
        && (!ab->scancmd || ab->scanRunning)
-       && (!ab->salsrvcmd || ab->salsrvRunning))
-       ab->needsClock = 0;     /* running normally */
-    else if (ab->b.goal == 0 && !ab->fileRunning && !ab->volRunning
-            && !ab->salRunning && !ab->scanRunning && !ab->salsrvRunning)
-       ab->needsClock = 0;     /* halted normally */
-    else
+       && (!ab->salsrvcmd || ab->salsrvRunning)) {
+       if (ab->b.errorStopCount) {
+           /* reset error count after running for a bit */
+           ab->needsClock = 1;
+       } else {
+           ab->needsClock = 0; /* running normally */
+       }
+    } else if ((ab->b.goal == 0) && !ab->fileRunning && !ab->volRunning
+              && !ab->salRunning && !ab->scanRunning && !ab->salsrvRunning) {
+       if (ab->b.flags & BNODE_ERRORSTOP && ab->b.errorStopDelay) {
+           bozo_Log("%s will retry start in %d seconds\n", ab->b.name,
+                    ab->b.errorStopDelay);
+           ab->needsClock = 1; /* halted for errors, retry later */
+           timeout = ab->b.errorStopDelay;
+       } else {
+           ab->needsClock = 0; /* halted normally */
+       }
+    } else
        ab->needsClock = 1;     /* other */
-    if (ab->needsClock && !bnode_PendingTimeout(fsbnode2bnode(ab)))
-       bnode_SetTimeout(fsbnode2bnode(ab), POLLTIME);
+
+    if (ab->needsClock && (!bnode_PendingTimeout(fsbnode2bnode(ab))
+                          || ab->b.period != timeout))
+       bnode_SetTimeout(fsbnode2bnode(ab), timeout);
     if (!ab->needsClock)
        bnode_SetTimeout(fsbnode2bnode(ab), 0);
 }
 
 static int
-NudgeProcs(register struct fsbnode *abnode)
+NudgeProcs(struct fsbnode *abnode)
 {
     struct bnode_proc *tp;     /* not register */
-    register afs_int32 code;
+    afs_int32 code;
     afs_int32 now;
 
     now = FT_ApproxTime();
@@ -947,7 +971,7 @@ static int
 fs_getstring(struct bnode *bn, char *abuffer, afs_int32 alen)
 {
     struct fsbnode *abnode = (struct fsbnode *)bn;
-    
+
     if (alen < 40)
        return -1;
     if (abnode->b.goal == 1) {
@@ -992,7 +1016,7 @@ fs_getparm(struct bnode *bn, afs_int32 aindex, char *abuffer,
           afs_int32 alen)
 {
     struct fsbnode *abnode = (struct fsbnode *)bn;
-    
+
     if (aindex == 0)
        strcpy(abuffer, abnode->filecmd);
     else if (aindex == 1)
@@ -1011,7 +1035,7 @@ dafs_getparm(struct bnode *bn, afs_int32 aindex, char *abuffer,
             afs_int32 alen)
 {
     struct fsbnode *abnode = (struct fsbnode *)bn;
-    
+
     if (aindex == 0)
        strcpy(abuffer, abnode->filecmd);
     else if (aindex == 1)