include-afsconfig-before-param-h-20010712
[openafs.git] / src / bozo / ezbnodeops.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
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
8  */
9
10 #include <afsconfig.h>
11 #include <afs/param.h>
12
13 RCSID("$Header$");
14
15 #include <sys/types.h>
16 #include <errno.h>
17 #include <sys/stat.h>
18 #include <lwp.h>
19 #ifdef AFS_NT40_ENV
20 #include <io.h>
21 #endif
22 #include <afs/afsutil.h>
23 #include <afs/procmgmt.h>  /* signal(), kill(), wait(), etc. */
24 #include "bnode.h"
25
26 static int ez_timeout(), ez_getstat(), ez_setstat(), ez_delete();
27 static int ez_procexit(), ez_getstring(), ez_getparm(), ez_restartp();
28 static int ez_hascore();
29 struct bnode *ez_create();
30
31 #define SDTIME          60          /* time in seconds given to a process to evaporate */
32
33 struct bnode_ops ezbnode_ops = {
34     ez_create,
35     ez_timeout,
36     ez_getstat,
37     ez_setstat,
38     ez_delete,
39     ez_procexit,
40     ez_getstring,
41     ez_getparm,
42     ez_restartp,
43     ez_hascore,
44 };
45
46 static int ez_hascore(abnode)
47 register struct ezbnode *abnode; {
48     char tbuffer[256];
49
50     bnode_CoreName(abnode, (char *) 0, tbuffer);
51     if (access(tbuffer, 0) == 0) return 1;
52     else return 0;
53 }
54
55 static int ez_restartp (abnode)
56 register struct ezbnode *abnode; {
57     struct bnode_token *tt;
58     register afs_int32 code;
59     struct stat tstat;
60     
61     code = bnode_ParseLine(abnode->command, &tt);
62     if (code) return 0;
63     if (!tt) return 0;
64     code = stat(tt->key, &tstat);
65     if (code) {
66         bnode_FreeTokens(tt);
67         return 0;
68     }
69     if (tstat.st_ctime > abnode->lastStart) code = 1;
70     else code = 0;
71     bnode_FreeTokens(tt);
72     return code;
73 }
74
75 static int ez_delete(abnode)
76 struct ezbnode *abnode; {
77     free(abnode->command);
78     free(abnode);
79     return 0;
80 }
81
82 struct bnode *ez_create(ainstance, acommand)
83 char *ainstance;
84 char *acommand; {
85     struct ezbnode *te;
86     char *cmdpath;
87
88     if (ConstructLocalBinPath(acommand, &cmdpath)) {
89         bozo_Log("BNODE: command path invalid '%s'\n", acommand);
90         return (struct bnode *)0;
91     }
92
93     te = (struct ezbnode *) malloc(sizeof(struct ezbnode));
94     bzero(te, sizeof(struct ezbnode));
95     bnode_InitBnode(te, &ezbnode_ops, ainstance);
96     te->command = cmdpath;
97     return (struct bnode *) te;
98 }
99
100 /* called to SIGKILL a process if it doesn't terminate normally */
101 static int ez_timeout(abnode)
102 struct ezbnode *abnode; {
103     if (!abnode->waitingForShutdown) return 0;  /* spurious */
104     /* send kill and turn off timer */
105     bnode_StopProc(abnode->proc, SIGKILL);
106     abnode->killSent = 1;
107     bnode_SetTimeout(abnode, 0);
108     return 0;
109 }
110
111 static int ez_getstat(abnode, astatus)
112 struct ezbnode *abnode;
113 afs_int32 *astatus; {
114     register afs_int32 temp;
115     if (abnode->waitingForShutdown) temp = BSTAT_SHUTTINGDOWN;
116     else if (abnode->running) temp = BSTAT_NORMAL;
117     else temp = BSTAT_SHUTDOWN;
118     *astatus = temp;
119     return 0;
120 }
121
122 static int ez_setstat(abnode, astatus)
123 register struct ezbnode *abnode;
124 afs_int32 astatus; {
125     struct bnode_proc *tp;
126     register afs_int32 code;
127
128     if (abnode->waitingForShutdown) return BZBUSY;
129     if (astatus == BSTAT_NORMAL && !abnode->running) {
130         /* start up */
131         abnode->lastStart = FT_ApproxTime();
132         code = bnode_NewProc(abnode, abnode->command, (char *) 0, &tp);
133         if (code) return code;
134         abnode->running = 1;
135         abnode->proc = tp;
136         return 0;
137     }
138     else if (astatus == BSTAT_SHUTDOWN && abnode->running) {
139         /* start shutdown */
140         bnode_StopProc(abnode->proc, SIGTERM);
141         abnode->waitingForShutdown = 1;
142         bnode_SetTimeout(abnode, SDTIME);
143         return 0;
144     }
145     return 0;
146 }
147
148 static int ez_procexit(abnode, aproc)
149 struct ezbnode *abnode;
150 struct bnode_proc *aproc; {
151     /* process has exited */
152     register afs_int32 code;
153
154     abnode->waitingForShutdown = 0;
155     abnode->running = 0;
156     abnode->killSent = 0;
157     abnode->proc = (struct bnode_proc *) 0;
158     bnode_SetTimeout(abnode, 0);        /* clear timer */
159     if (abnode->b.goal)
160         code = ez_setstat(abnode, BSTAT_NORMAL);
161     else code = 0;
162     return code;
163 }
164
165 static int ez_getstring(abnode, abuffer, alen)
166 struct ezbnode *abnode;
167 char *abuffer;
168 afs_int32 alen;{
169     return -1;      /* don't have much to add */
170 }
171
172 static ez_getparm(abnode, aindex, abuffer, alen)
173 struct ezbnode *abnode;
174 afs_int32 aindex;
175 char *abuffer;
176 afs_int32 alen; {
177     if (aindex > 0) return BZDOM;
178     strcpy(abuffer, abnode->command);
179     return 0;
180 }