From: Michael Meffie Date: Wed, 24 Nov 2010 01:21:50 +0000 (-0500) Subject: bozo: bosserver -pidfiles option X-Git-Tag: upstream/1.6.1.pre1^2~208 X-Git-Url: https://git.michaelhowe.org/gitweb/?a=commitdiff_plain;h=f008d3d5f64eea9a9459641fd91bfff79236d0b4;p=packages%2Fo%2Fopenafs.git bozo: bosserver -pidfiles option 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. Reviewed-on: http://gerrit.openafs.org/5497 Tested-by: BuildBot Reviewed-by: Derrick Brashear (cherry picked from commit bdf86d245fd55c5c7ac7ea81e3d6b6bafdbe1783) Change-Id: Id76530b81e2e92c76a015510d04dc8d5e5fd75ce Reviewed-on: http://gerrit.openafs.org/5537 Tested-by: BuildBot Reviewed-by: Derrick Brashear --- diff --git a/src/bozo/bnode.c b/src/bozo/bnode.c index 891e871e3..6f3e2c36d 100644 --- a/src/bozo/bnode.c +++ b/src/bozo/bnode.c @@ -58,6 +58,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 @@ -994,6 +995,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; } diff --git a/src/bozo/bnode.p.h b/src/bozo/bnode.p.h index 14ab9d7ba..971d27219 100644 --- a/src/bozo/bnode.p.h +++ b/src/bozo/bnode.p.h @@ -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); diff --git a/src/bozo/bosprototypes.h b/src/bozo/bosprototypes.h index 38403aee9..1f666fd01 100644 --- a/src/bozo/bosprototypes.h +++ b/src/bozo/bosprototypes.h @@ -10,6 +10,10 @@ #ifndef _BOSPROTOTYPES_H_ #define _BOSPROTOTYPES_H_ +#ifdef AFS_NT40_ENV +typedef int pid_t; +#endif + #include /* bnode.c */ @@ -43,6 +47,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); diff --git a/src/bozo/bosserver.c b/src/bozo/bosserver.c index e05663be7..dca7ec652 100644 --- a/src/bozo/bosserver.c +++ b/src/bozo/bosserver.c @@ -70,6 +70,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 @@ -727,6 +728,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" @@ -871,6 +956,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 { @@ -884,6 +973,7 @@ main(int argc, char **argv, char **envp) "[-syslog[=FACILITY]] " "[-enable_peer_stats] [-enable_process_stats] " "[-cores=] \n" + "[-pidfiles[=path]] " "[-nofork] " "[-help]\n"); #else printf("Usage: bosserver [-noauth] [-log] " @@ -892,6 +982,7 @@ main(int argc, char **argv, char **envp) "[-rxmaxmtu ] [-rxbind] [-allow-dotted-principals]" "[-enable_peer_stats] [-enable_process_stats] " "[-cores=] \n" + "[-pidfiles[=path]] " "[-help]\n"); #endif fflush(stdout); @@ -1053,6 +1144,10 @@ main(int argc, char **argv, char **envp) afsconf_SetNoAuthFlag(tdir, noAuth); afsconf_BuildServerSecurityObjects(tdir, 0, &securityClasses, &numClasses); + if (DoPidFiles) { + bozo_CreatePidFile("bosserver", NULL, getpid()); + } + /* Disable jumbograms */ rx_SetNoJumbo(); diff --git a/src/bozo/cronbnodeops.c b/src/bozo/cronbnodeops.c index 01026e345..d1adc5acb 100644 --- a/src/bozo/cronbnodeops.c +++ b/src/bozo/cronbnodeops.c @@ -37,6 +37,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); @@ -54,6 +55,7 @@ struct bnode_ops cronbnode_ops = { cron_getparm, cron_restartp, cron_hascore, + cron_procstarted, }; struct cronbnode { @@ -275,6 +277,12 @@ cron_setstat(struct bnode *bn, afs_int32 astatus) return 0; } +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) { diff --git a/src/bozo/ezbnodeops.c b/src/bozo/ezbnodeops.c index b7916465e..8a8494f39 100644 --- a/src/bozo/ezbnodeops.c +++ b/src/bozo/ezbnodeops.c @@ -28,6 +28,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); @@ -39,6 +40,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 */ @@ -53,6 +55,7 @@ struct bnode_ops ezbnode_ops = { ez_getparm, ez_restartp, ez_hascore, + ez_procstarted }; static int @@ -185,6 +188,17 @@ ez_setstat(struct bnode *bn, afs_int32 astatus) return 0; } +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) { @@ -193,6 +207,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; diff --git a/src/bozo/fsbnodeops.c b/src/bozo/fsbnodeops.c index a81374d7f..285786179 100644 --- a/src/bozo/fsbnodeops.c +++ b/src/bozo/fsbnodeops.c @@ -35,6 +35,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 */ @@ -109,6 +110,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, @@ -138,6 +140,7 @@ struct bnode_ops fsbnode_ops = { fs_getparm, fs_restartp, fs_hascore, + fs_procstarted, }; /* demand attach fs bnode ops */ @@ -152,6 +155,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 @@ -711,6 +715,17 @@ fs_setstat(struct bnode *abnode, afs_int32 astatus) return NudgeProcs((struct fsbnode *) abnode); } +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) { @@ -718,6 +733,10 @@ fs_procexit(struct bnode *bn, struct bnode_proc *aproc) /* process has exited */ + if (DoPidFiles) { + bozo_DeletePidFile(bn->name, aproc->coreName); + } + if (aproc == abnode->volProc) { abnode->volProc = 0; abnode->volRunning = 0;