2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 #include <afsconfig.h>
11 #include <afs/param.h>
13 #include <afs/procmgmt.h>
19 #include <afs/afsutil.h>
21 #include "bnode_internal.h"
22 #include "bosprototypes.h"
24 extern char *DoPidFiles;
26 struct bnode *ez_create(char *, char *, char *, char *, char *, char *);
27 static int ez_hascore(struct bnode *bnode);
28 static int ez_restartp(struct bnode *bnode);
29 static int ez_delete(struct bnode *bnode);
30 static int ez_timeout(struct bnode *bnode);
31 static int ez_getstat(struct bnode *bnode, afs_int32 *status);
32 static int ez_setstat(struct bnode *bnode, afs_int32 status);
33 static int ez_procexit(struct bnode *bnode, struct bnode_proc *proc);
34 static int ez_getstring(struct bnode *bnode, char *abuffer, afs_int32 alen);
35 static int ez_getparm(struct bnode *bnode, afs_int32, char *, afs_int32);
36 static int ez_procstarted(struct bnode *bnode, struct bnode_proc *proc);
38 #define SDTIME 60 /* time in seconds given to a process to evaporate */
39 #define ERROR_RESET_TIME 60 /* time in seconds to wait before resetting error count state */
41 struct bnode_ops ezbnode_ops = {
56 ez_hascore(struct bnode *abnode)
60 bnode_CoreName(abnode, NULL, tbuffer);
61 if (access(tbuffer, 0) == 0)
68 ez_restartp(struct bnode *bn)
70 struct ezbnode *abnode = (struct ezbnode *)bn;
71 struct bnode_token *tt;
75 code = bnode_ParseLine(abnode->command, &tt);
80 code = stat(tt->key, &tstat);
85 if (tstat.st_ctime > abnode->lastStart)
94 ez_delete(struct bnode *bn)
96 struct ezbnode *abnode = (struct ezbnode *)bn;
98 free(abnode->command);
104 ez_create(char *ainstance, char *acommand, char *unused1, char *unused2,
105 char *unused3, char *unused4)
110 if (ConstructLocalBinPath(acommand, &cmdpath)) {
111 bozo_Log("BNODE: command path invalid '%s'\n", acommand);
115 te = calloc(1, sizeof(struct ezbnode));
116 if (bnode_InitBnode((struct bnode *)te, &ezbnode_ops, ainstance) != 0) {
120 te->command = cmdpath;
121 return (struct bnode *)te;
124 /* called to SIGKILL a process if it doesn't terminate normally
125 * or to retry start after an error stop. */
127 ez_timeout(struct bnode *bn)
129 struct ezbnode *abnode = (struct ezbnode *)bn;
131 if (abnode->waitingForShutdown) {
132 /* send kill and turn off timer */
133 bnode_StopProc(abnode->proc, SIGKILL);
134 abnode->killSent = 1;
135 bnode_SetTimeout((struct bnode *)abnode, 0);
136 } else if (!abnode->running && abnode->b.flags & BNODE_ERRORSTOP) {
137 /* was stopped for too many errors, retrying */
138 /* reset error count after running for a bit */
139 bnode_SetTimeout(bn, ERROR_RESET_TIME);
140 bnode_SetStat(bn, BSTAT_NORMAL);
142 bnode_SetTimeout(bn, 0); /* one shot timer */
143 bnode_ResetErrorCount(bn);
149 ez_getstat(struct bnode *bn, afs_int32 * astatus)
151 struct ezbnode *abnode = (struct ezbnode *)bn;
154 if (abnode->waitingForShutdown)
155 temp = BSTAT_SHUTTINGDOWN;
156 else if (abnode->running)
158 else if (abnode->b.flags & BNODE_ERRORSTOP)
159 temp = BSTAT_STARTINGUP;
161 temp = BSTAT_SHUTDOWN;
167 ez_setstat(struct bnode *bn, afs_int32 astatus)
169 struct ezbnode *abnode = (struct ezbnode *)bn;
171 struct bnode_proc *tp;
174 if (abnode->waitingForShutdown)
176 if (astatus == BSTAT_NORMAL && !abnode->running) {
178 abnode->lastStart = FT_ApproxTime();
179 code = bnode_NewProc((struct bnode *)abnode, abnode->command, NULL, &tp);
185 } else if (astatus == BSTAT_SHUTDOWN && abnode->running) {
187 bnode_StopProc(abnode->proc, SIGTERM);
188 abnode->waitingForShutdown = 1;
189 bnode_SetTimeout((struct bnode *)abnode, SDTIME);
196 ez_procstarted(struct bnode *bn, struct bnode_proc *aproc)
201 code = bozo_CreatePidFile(bn->name, NULL, aproc->pid);
207 ez_procexit(struct bnode *bn, struct bnode_proc *aproc)
209 struct ezbnode *abnode = (struct ezbnode *)bn;
211 /* process has exited */
215 bozo_DeletePidFile(bn->name, NULL);
218 abnode->waitingForShutdown = 0;
220 abnode->killSent = 0;
222 bnode_SetTimeout((struct bnode *) abnode, 0); /* clear timer */
224 code = ez_setstat((struct bnode *) abnode, BSTAT_NORMAL);
225 else if (abnode->b.flags & BNODE_ERRORSTOP && abnode->b.errorStopDelay) {
226 bozo_Log("%s will retry start in %d seconds\n", abnode->b.name,
227 abnode->b.errorStopDelay);
228 bnode_SetTimeout(bn, abnode->b.errorStopDelay);
234 ez_getstring(struct bnode *abnode, char *abuffer, afs_int32 alen)
236 return -1; /* don't have much to add */
240 ez_getparm(struct bnode *bn, afs_int32 aindex, char *abuffer,
243 struct ezbnode *abnode = (struct ezbnode *) bn;
246 strcpy(abuffer, abnode->command);