bozo: bosserver -pidfiles option
authorMichael Meffie <mmeffie@sinenomine.net>
Wed, 24 Nov 2010 01:21:50 +0000 (20:21 -0500)
committerDerrick Brashear <shadow@dementix.org>
Mon, 26 Sep 2011 04:09:23 +0000 (21:09 -0700)
Add an option to bosserver to create pidfiles for long running
processes for simple, fs, and dafs bnode types, as well as the
bosserver process. The pidfiles are located in the server local
directory by default, or in the path specifed by the -pidfiles
command-line option.

Change-Id: I3e85b027dc6f630965f84b18b7f849ac481dabe2
Reviewed-on: http://gerrit.openafs.org/5497
Tested-by: BuildBot <buildbot@rampaginggeek.com>
Reviewed-by: Derrick Brashear <shadow@dementix.org>

src/bozo/bnode.c
src/bozo/bnode.p.h
src/bozo/bosprototypes.h
src/bozo/bosserver.c
src/bozo/cronbnodeops.c
src/bozo/ezbnodeops.c
src/bozo/fsbnodeops.c

index 3f10be9..4181a19 100644 (file)
@@ -41,6 +41,7 @@ static struct bnode_stats {
 } bnode_stats;
 
 extern const char *DoCore;
+extern const char *DoPidFiles;
 #ifndef AFS_NT40_ENV
 extern char **environ;         /* env structure */
 #endif
@@ -976,6 +977,7 @@ bnode_NewProc(struct bnode *abnode, char *aexecString, char *coreName,
     tp->pid = cpid;
     tp->flags = BPROC_STARTED;
     tp->flags &= ~BPROC_EXITED;
+    BOP_PROCSTARTED(abnode, tp);
     bnode_Check(abnode);
     return 0;
 }
index 14ab9d7..971d272 100644 (file)
@@ -16,6 +16,7 @@
 #define        BOP_GETPARM(bnode, n, b, l)     ((*(bnode)->ops->getparm)((bnode),(n),(b),(l)))
 #define        BOP_RESTARTP(bnode)     ((*(bnode)->ops->restartp)((bnode)))
 #define BOP_HASCORE(bnode)     ((*(bnode)->ops->hascore)((bnode)))
+#define BOP_PROCSTARTED(bnode,p)       ((*(bnode)->ops->procstarted)((bnode),(p)))
 
 struct bnode_proc;
 
@@ -31,6 +32,7 @@ struct bnode_ops {
                     afs_int32 alen);
     int (*restartp) ( struct bnode *);
     int (*hascore) ( struct bnode *);
+    int (*procstarted) ( struct bnode *, struct bnode_proc * );
 };
 
 struct bnode_type {
@@ -139,3 +141,5 @@ extern afs_int32 bnode_Create(char *atype, char *ainstance, struct bnode ** abp,
 extern struct bnode *bnode_FindInstance(char *aname);
 extern int bnode_WaitStatus(struct bnode *abnode, int astatus);
 extern int bnode_SetStat(struct bnode *abnode, int agoal);
+extern int bnode_CreatePidFile(struct bnode *abnode, struct bnode_proc *aproc, char *name);
+extern int bnode_DestroyPidFile(struct bnode *abnode, struct bnode_proc *aproc);
index a009eb0..116f989 100644 (file)
@@ -43,6 +43,8 @@ int bnode_Deactivate(struct bnode *abnode);
 void bozo_Log(char *format, ... );
 int bozo_ReBozo(void);
 int WriteBozoFile(char *aname);
+int bozo_CreatePidFile(char *ainst, char *aname, pid_t apid);
+int bozo_DeletePidFile(char *ainst, char *aname);
 
 /* bosoprocs.c */
 int GetRequiredDirPerm(const char *path);
index 84ae880..d8db010 100644 (file)
@@ -58,6 +58,7 @@ FILE *bozo_logFile;
 const char *DoCore;
 int DoLogging = 0;
 int DoSyslog = 0;
+const char *DoPidFiles = NULL;
 #ifndef AFS_NT40_ENV
 int DoSyslogFacility = LOG_DAEMON;
 #endif
@@ -714,6 +715,90 @@ background(void)
 #endif /* ! AFS_NT40_ENV */
 #endif
 
+static char *
+make_pid_filename(char *ainst, char *aname)
+{
+    char *buffer = NULL;
+    int length;
+
+    length = strlen(DoPidFiles) + strlen(ainst) + 6;
+    if (aname && *aname) {
+       length += strlen(aname) + 1;
+    }
+    buffer = malloc(length * sizeof(char));
+    if (!buffer) {
+       if (aname) {
+           bozo_Log("Failed to alloc pid filename buffer for %s.%s.\n",
+                    ainst, aname);
+       } else {
+           bozo_Log("Failed to alloc pid filename buffer for %s.\n", ainst);
+       }
+    } else {
+       if (aname && *aname) {
+           snprintf(buffer, length, "%s/%s.%s.pid", DoPidFiles, ainst,
+                    aname);
+       } else {
+           snprintf(buffer, length, "%s/%s.pid", DoPidFiles, ainst);
+       }
+    }
+    return buffer;
+}
+
+/**
+ * Write a file containing the pid of the named process.
+ *
+ * @param ainst instance name
+ * @param aname sub-process name of the instance, may be null
+ * @param apid  process id of the newly started process
+ *
+ * @returns status
+ */
+int
+bozo_CreatePidFile(char *ainst, char *aname, pid_t apid)
+{
+    int code = 0;
+    char *pidfile = NULL;
+    FILE *fp;
+
+    pidfile = make_pid_filename(ainst, aname);
+    if (!pidfile) {
+       return ENOMEM;
+    }
+    if ((fp = fopen(pidfile, "w")) == NULL) {
+       bozo_Log("Failed to open pidfile %s; errno=%d\n", pidfile, errno);
+       free(pidfile);
+       return errno;
+    }
+    if (fprintf(fp, "%ld\n", afs_printable_int32_ld(apid)) < 0) {
+       code = errno;
+    }
+    if (fclose(fp) != 0) {
+       code = errno;
+    }
+    free(pidfile);
+    return code;
+}
+
+/**
+ * Clean a pid file for a process which just exited.
+ *
+ * @param ainst instance name
+ * @param aname sub-process name of the instance, may be null
+ *
+ * @returns status
+ */
+int
+bozo_DeletePidFile(char *ainst, char *aname)
+{
+    char *pidfile = NULL;
+    pidfile = make_pid_filename(ainst, aname);
+    if (pidfile) {
+       unlink(pidfile);
+       free(pidfile);
+    }
+    return 0;
+}
+
 /* start a process and monitor it */
 
 #include "AFS_component_version_number.c"
@@ -858,6 +943,10 @@ main(int argc, char **argv, char **envp)
                printf("Invalid audit interface '%s'\n", interface);
                exit(1);
            }
+       } else if (strncmp(argv[code], "-pidfiles=", 10) == 0) {
+           DoPidFiles = (argv[code]+10);
+       } else if (strncmp(argv[code], "-pidfiles", 9) == 0) {
+           DoPidFiles = AFSDIR_BOSCONFIG_DIR;
        }
        else {
 
@@ -871,6 +960,7 @@ main(int argc, char **argv, char **envp)
                   "[-syslog[=FACILITY]] "
                   "[-enable_peer_stats] [-enable_process_stats] "
                   "[-cores=<none|path>] \n"
+                  "[-pidfiles[=path]] "
                   "[-nofork] " "[-help]\n");
 #else
            printf("Usage: bosserver [-noauth] [-log] "
@@ -879,6 +969,7 @@ main(int argc, char **argv, char **envp)
                   "[-rxmaxmtu <bytes>] [-rxbind] [-allow-dotted-principals]"
                   "[-enable_peer_stats] [-enable_process_stats] "
                   "[-cores=<none|path>] \n"
+                  "[-pidfiles[=path]] "
                   "[-help]\n");
 #endif
            fflush(stdout);
@@ -1060,6 +1151,10 @@ main(int argc, char **argv, char **envp)
     afsconf_SetNoAuthFlag(tdir, noAuth);
     afsconf_BuildServerSecurityObjects(tdir, &securityClasses, &numClasses);
 
+    if (DoPidFiles) {
+       bozo_CreatePidFile("bosserver", NULL, getpid());
+    }
+
     /* Disable jumbograms */
     rx_SetNoJumbo();
 
index f4eeab1..1d5f2af 100644 (file)
@@ -33,6 +33,7 @@ static int cron_delete(struct bnode *bnode);
 static int cron_timeout(struct bnode *bnode);
 static int cron_getstat(struct bnode *bnode, afs_int32 *status);
 static int cron_setstat(struct bnode *bnode, afs_int32 status);
+static int cron_procstarted(struct bnode *bnode, struct bnode_proc *proc);
 static int cron_procexit(struct bnode *bnode, struct bnode_proc *proc);
 static int cron_getstring(struct bnode *bnode, char *abuffer, afs_int32 alen);
 static int cron_getparm(struct bnode *bnode, afs_int32, char *, afs_int32);
@@ -50,6 +51,7 @@ struct bnode_ops cronbnode_ops = {
     cron_getparm,
     cron_restartp,
     cron_hascore,
+    cron_procstarted,
 };
 
 struct cronbnode {
@@ -272,6 +274,12 @@ cron_setstat(struct bnode *bn, afs_int32 astatus)
 }
 
 static int
+cron_procstarted(struct bnode *bn, struct bnode_proc *aproc)
+{
+    return 0;  /* no op */
+}
+
+static int
 cron_procexit(struct bnode *bn, struct bnode_proc *aproc)
 {
     struct cronbnode *abnode = (struct cronbnode *) bn;
index 2a8f397..9094895 100644 (file)
@@ -23,6 +23,7 @@
 #include "bnode.h"
 #include "bosprototypes.h"
 
+extern char *DoPidFiles;
 
 struct bnode *ez_create(char *, char *, char *, char *, char *, char *);
 static int ez_hascore(struct bnode *bnode);
@@ -34,6 +35,7 @@ static int ez_setstat(struct bnode *bnode, afs_int32 status);
 static int ez_procexit(struct bnode *bnode, struct bnode_proc *proc);
 static int ez_getstring(struct bnode *bnode, char *abuffer, afs_int32 alen);
 static int ez_getparm(struct bnode *bnode, afs_int32, char *, afs_int32);
+static int ez_procstarted(struct bnode *bnode, struct bnode_proc *proc);
 
 #define        SDTIME          60      /* time in seconds given to a process to evaporate */
 
@@ -48,6 +50,7 @@ struct bnode_ops ezbnode_ops = {
     ez_getparm,
     ez_restartp,
     ez_hascore,
+    ez_procstarted
 };
 
 static int
@@ -181,6 +184,17 @@ ez_setstat(struct bnode *bn, afs_int32 astatus)
 }
 
 static int
+ez_procstarted(struct bnode *bn, struct bnode_proc *aproc)
+{
+    int code = 0;
+
+    if (DoPidFiles) {
+       code = bozo_CreatePidFile(bn->name, NULL, aproc->pid);
+    }
+    return code;;
+}
+
+static int
 ez_procexit(struct bnode *bn, struct bnode_proc *aproc)
 {
     struct ezbnode *abnode = (struct ezbnode *)bn;
@@ -188,6 +202,10 @@ ez_procexit(struct bnode *bn, struct bnode_proc *aproc)
     /* process has exited */
     afs_int32 code;
 
+    if (DoPidFiles) {
+       bozo_DeletePidFile(bn->name, NULL);
+    }
+
     abnode->waitingForShutdown = 0;
     abnode->running = 0;
     abnode->killSent = 0;
index 14a233a..c477e9f 100644 (file)
@@ -21,6 +21,7 @@
 #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 */
@@ -95,6 +96,7 @@ 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,
@@ -124,6 +126,7 @@ struct bnode_ops fsbnode_ops = {
     fs_getparm,
     fs_restartp,
     fs_hascore,
+    fs_procstarted,
 };
 
 /* demand attach fs bnode ops */
@@ -138,6 +141,7 @@ 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
@@ -698,12 +702,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;