stat_proc.c revision 1.5 1 1.5 bouyer /* $NetBSD: stat_proc.c,v 1.5 2000/06/06 18:19:56 bouyer Exp $ */
2 1.1 scottr
3 1.1 scottr /*
4 1.1 scottr * Copyright (c) 1995
5 1.1 scottr * A.R. Gordon (andrew.gordon (at) net-tel.co.uk). All rights reserved.
6 1.1 scottr *
7 1.1 scottr * Redistribution and use in source and binary forms, with or without
8 1.1 scottr * modification, are permitted provided that the following conditions
9 1.1 scottr * are met:
10 1.1 scottr * 1. Redistributions of source code must retain the above copyright
11 1.1 scottr * notice, this list of conditions and the following disclaimer.
12 1.1 scottr * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 scottr * notice, this list of conditions and the following disclaimer in the
14 1.1 scottr * documentation and/or other materials provided with the distribution.
15 1.1 scottr * 3. All advertising materials mentioning features or use of this software
16 1.1 scottr * must display the following acknowledgement:
17 1.1 scottr * This product includes software developed for the FreeBSD project
18 1.1 scottr * 4. Neither the name of the author nor the names of any co-contributors
19 1.1 scottr * may be used to endorse or promote products derived from this software
20 1.1 scottr * without specific prior written permission.
21 1.1 scottr *
22 1.1 scottr * THIS SOFTWARE IS PROVIDED BY ANDREW GORDON AND CONTRIBUTORS ``AS IS'' AND
23 1.1 scottr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 scottr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 scottr * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26 1.1 scottr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1 scottr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1 scottr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1 scottr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1 scottr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 scottr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 scottr * SUCH DAMAGE.
33 1.1 scottr *
34 1.1 scottr */
35 1.2 lukem
36 1.2 lukem #include <sys/cdefs.h>
37 1.2 lukem #ifndef lint
38 1.5 bouyer __RCSID("$NetBSD: stat_proc.c,v 1.5 2000/06/06 18:19:56 bouyer Exp $");
39 1.2 lukem #endif
40 1.1 scottr
41 1.3 lukem #include <errno.h>
42 1.3 lukem #include <netdb.h>
43 1.1 scottr #include <stdio.h>
44 1.1 scottr #include <stdlib.h>
45 1.1 scottr #include <string.h>
46 1.3 lukem #include <syslog.h>
47 1.4 christos #include <signal.h>
48 1.3 lukem #include <unistd.h>
49 1.3 lukem
50 1.1 scottr #include <rpc/rpc.h>
51 1.1 scottr
52 1.1 scottr #include "statd.h"
53 1.1 scottr
54 1.1 scottr /* sm_stat_1 --------------------------------------------------------------- */
55 1.1 scottr /*
56 1.1 scottr * Purpose: RPC call to enquire if a host can be monitored
57 1.1 scottr * Returns: TRUE for any hostname that can be looked up to give
58 1.1 scottr * an address.
59 1.1 scottr */
60 1.1 scottr struct sm_stat_res *
61 1.1 scottr sm_stat_1_svc(arg, req)
62 1.1 scottr sm_name *arg;
63 1.1 scottr struct svc_req *req;
64 1.1 scottr {
65 1.1 scottr static sm_stat_res res;
66 1.1 scottr
67 1.4 christos NO_ALARM;
68 1.1 scottr if (debug)
69 1.1 scottr syslog(LOG_DEBUG, "stat called for host %s", arg->mon_name);
70 1.1 scottr
71 1.1 scottr if (gethostbyname(arg->mon_name))
72 1.1 scottr res.res_stat = stat_succ;
73 1.1 scottr else {
74 1.1 scottr syslog(LOG_ERR, "invalid hostname to sm_stat: %s",
75 1.1 scottr arg->mon_name);
76 1.1 scottr res.res_stat = stat_fail;
77 1.1 scottr }
78 1.1 scottr
79 1.4 christos res.state = status_info.ourState;
80 1.4 christos ALARM;
81 1.1 scottr return (&res);
82 1.1 scottr }
83 1.1 scottr
84 1.1 scottr /* sm_mon_1 ---------------------------------------------------------------- */
85 1.1 scottr /*
86 1.1 scottr * Purpose: RPC procedure to establish a monitor request
87 1.1 scottr * Returns: Success, unless lack of resources prevents
88 1.1 scottr * the necessary structures from being set up
89 1.1 scottr * to record the request, or if the hostname is not
90 1.1 scottr * valid (as judged by gethostbyname())
91 1.1 scottr */
92 1.1 scottr struct sm_stat_res *
93 1.1 scottr sm_mon_1_svc(arg, req)
94 1.1 scottr mon *arg;
95 1.1 scottr struct svc_req *req;
96 1.1 scottr {
97 1.1 scottr static sm_stat_res res;
98 1.4 christos HostInfo *hp, h;
99 1.1 scottr MonList *lp;
100 1.1 scottr
101 1.4 christos NO_ALARM;
102 1.1 scottr if (debug) {
103 1.1 scottr syslog(LOG_DEBUG, "monitor request for host %s",
104 1.1 scottr arg->mon_id.mon_name);
105 1.1 scottr syslog(LOG_DEBUG, "recall host: %s prog: %d ver: %d proc: %d",
106 1.3 lukem arg->mon_id.my_id.my_name, arg->mon_id.my_id.my_prog,
107 1.3 lukem arg->mon_id.my_id.my_vers, arg->mon_id.my_id.my_proc);
108 1.1 scottr }
109 1.1 scottr res.res_stat = stat_fail; /* Assume fail until set otherwise */
110 1.4 christos res.state = status_info.ourState;
111 1.1 scottr
112 1.1 scottr /*
113 1.1 scottr * Find existing host entry, or create one if not found. If
114 1.1 scottr * find_host() fails, it will have logged the error already.
115 1.1 scottr */
116 1.4 christos if (!gethostbyname(arg->mon_id.mon_name)) {
117 1.1 scottr syslog(LOG_ERR, "Invalid hostname to sm_mon: %s",
118 1.1 scottr arg->mon_id.mon_name);
119 1.4 christos return &res;
120 1.4 christos }
121 1.1 scottr
122 1.4 christos if ((hp = find_host(arg->mon_id.mon_name, &h)) == NULL)
123 1.4 christos memset(hp = &h, 0, sizeof(h));
124 1.1 scottr
125 1.4 christos lp = (MonList *)malloc(sizeof(MonList));
126 1.4 christos if (!lp)
127 1.4 christos syslog(LOG_ERR, "Out of memory");
128 1.4 christos else {
129 1.4 christos strncpy(lp->notifyHost, arg->mon_id.my_id.my_name,
130 1.4 christos SM_MAXSTRLEN);
131 1.4 christos lp->notifyProg = arg->mon_id.my_id.my_prog;
132 1.4 christos lp->notifyVers = arg->mon_id.my_id.my_vers;
133 1.4 christos lp->notifyProc = arg->mon_id.my_id.my_proc;
134 1.4 christos memcpy(lp->notifyData, arg->priv,
135 1.4 christos sizeof(lp->notifyData));
136 1.4 christos
137 1.4 christos lp->next = hp->monList;
138 1.4 christos hp->monList = lp;
139 1.4 christos change_host(arg->mon_id.mon_name, hp);
140 1.4 christos sync_file();
141 1.4 christos res.res_stat = stat_succ; /* Report success */
142 1.1 scottr }
143 1.4 christos ALARM;
144 1.1 scottr return (&res);
145 1.1 scottr }
146 1.1 scottr
147 1.1 scottr /* do_unmon ---------------------------------------------------------------- */
148 1.1 scottr /*
149 1.1 scottr * Purpose: Remove a monitor request from a host
150 1.1 scottr * Returns: TRUE if found, FALSE if not found.
151 1.1 scottr * Notes: Common code from sm_unmon_1_svc and sm_unmon_all_1_svc
152 1.1 scottr * In the unlikely event of more than one identical monitor
153 1.1 scottr * request, all are removed.
154 1.1 scottr */
155 1.4 christos int
156 1.4 christos do_unmon(name, hp, ptr)
157 1.4 christos char *name;
158 1.1 scottr HostInfo *hp;
159 1.4 christos void *ptr;
160 1.1 scottr {
161 1.4 christos my_id *idp = ptr;
162 1.1 scottr MonList *lp, *next;
163 1.1 scottr MonList *last = NULL;
164 1.1 scottr int result = FALSE;
165 1.1 scottr
166 1.1 scottr lp = hp->monList;
167 1.1 scottr while (lp) {
168 1.1 scottr if (!strncasecmp(idp->my_name, lp->notifyHost, SM_MAXSTRLEN)
169 1.1 scottr && (idp->my_prog == lp->notifyProg)
170 1.1 scottr && (idp->my_proc == lp->notifyProc)
171 1.1 scottr && (idp->my_vers == lp->notifyVers)) {
172 1.1 scottr /* found one. Unhook from chain and free. */
173 1.1 scottr next = lp->next;
174 1.1 scottr if (last)
175 1.1 scottr last->next = next;
176 1.1 scottr else
177 1.1 scottr hp->monList = next;
178 1.1 scottr free(lp);
179 1.1 scottr lp = next;
180 1.1 scottr result = TRUE;
181 1.1 scottr } else {
182 1.1 scottr last = lp;
183 1.1 scottr lp = lp->next;
184 1.1 scottr }
185 1.1 scottr }
186 1.1 scottr return (result);
187 1.1 scottr }
188 1.1 scottr
189 1.1 scottr /* sm_unmon_1 -------------------------------------------------------------- */
190 1.1 scottr /*
191 1.1 scottr * Purpose: RPC procedure to release a monitor request.
192 1.1 scottr * Returns: Local machine's status number
193 1.1 scottr * Notes: The supplied mon_id should match the value passed in an
194 1.1 scottr * earlier call to sm_mon_1
195 1.1 scottr */
196 1.1 scottr struct sm_stat *
197 1.1 scottr sm_unmon_1_svc(arg, req)
198 1.1 scottr mon_id *arg;
199 1.1 scottr struct svc_req *req;
200 1.1 scottr {
201 1.1 scottr static sm_stat res;
202 1.4 christos HostInfo *hp, h;
203 1.1 scottr
204 1.4 christos NO_ALARM;
205 1.1 scottr if (debug) {
206 1.1 scottr syslog(LOG_DEBUG, "un-monitor request for host %s",
207 1.1 scottr arg->mon_name);
208 1.1 scottr syslog(LOG_DEBUG, "recall host: %s prog: %d ver: %d proc: %d",
209 1.3 lukem arg->my_id.my_name, arg->my_id.my_prog,
210 1.1 scottr arg->my_id.my_vers, arg->my_id.my_proc);
211 1.1 scottr }
212 1.4 christos if ((hp = find_host(arg->mon_name, &h)) != NULL) {
213 1.4 christos if (do_unmon(arg->mon_name, hp, &arg->my_id)) {
214 1.4 christos change_host(arg->mon_name, hp);
215 1.1 scottr sync_file();
216 1.4 christos }
217 1.1 scottr else
218 1.1 scottr syslog(LOG_ERR,
219 1.1 scottr "unmon request from %s, no matching monitor",
220 1.1 scottr arg->my_id.my_name);
221 1.1 scottr } else
222 1.1 scottr syslog(LOG_ERR, "unmon request from %s for unknown host %s",
223 1.1 scottr arg->my_id.my_name, arg->mon_name);
224 1.1 scottr
225 1.4 christos res.state = status_info.ourState;
226 1.4 christos ALARM;
227 1.1 scottr
228 1.1 scottr return (&res);
229 1.1 scottr }
230 1.1 scottr
231 1.1 scottr /* sm_unmon_all_1 ---------------------------------------------------------- */
232 1.1 scottr /*
233 1.1 scottr * Purpose: RPC procedure to release monitor requests.
234 1.1 scottr * Returns: Local machine's status number
235 1.1 scottr * Notes: Releases all monitor requests (if any) from the specified
236 1.1 scottr * host and program number.
237 1.1 scottr */
238 1.1 scottr struct sm_stat *
239 1.1 scottr sm_unmon_all_1_svc(arg, req)
240 1.1 scottr my_id *arg;
241 1.1 scottr struct svc_req *req;
242 1.1 scottr {
243 1.1 scottr static sm_stat res;
244 1.1 scottr
245 1.4 christos NO_ALARM;
246 1.1 scottr if (debug) {
247 1.1 scottr syslog(LOG_DEBUG,
248 1.1 scottr "unmon_all for host: %s prog: %d ver: %d proc: %d",
249 1.1 scottr arg->my_name, arg->my_prog, arg->my_vers, arg->my_proc);
250 1.1 scottr }
251 1.1 scottr
252 1.4 christos unmon_hosts();
253 1.1 scottr sync_file();
254 1.1 scottr
255 1.4 christos res.state = status_info.ourState;
256 1.4 christos ALARM;
257 1.1 scottr
258 1.1 scottr return (&res);
259 1.1 scottr }
260 1.1 scottr
261 1.1 scottr /* sm_simu_crash_1 --------------------------------------------------------- */
262 1.1 scottr /*
263 1.1 scottr * Purpose: RPC procedure to simulate a crash
264 1.1 scottr * Returns: Nothing
265 1.1 scottr * Notes: Standardised mechanism for debug purposes
266 1.1 scottr * The specification says that we should drop all of our
267 1.1 scottr * status information (apart from the list of monitored hosts
268 1.1 scottr * on disc). However, this would confuse the rpc.lockd
269 1.1 scottr * which would be unaware that all of its monitor requests
270 1.1 scottr * had been silently junked. Hence we in fact retain all
271 1.1 scottr * current requests and simply increment the status counter
272 1.1 scottr * and inform all hosts on the monitor list.
273 1.1 scottr */
274 1.1 scottr void *
275 1.1 scottr sm_simu_crash_1_svc(v, req)
276 1.1 scottr void *v;
277 1.1 scottr struct svc_req *req;
278 1.1 scottr {
279 1.1 scottr static char dummy;
280 1.1 scottr
281 1.4 christos NO_ALARM;
282 1.1 scottr if (debug)
283 1.1 scottr syslog(LOG_DEBUG, "simu_crash called!!");
284 1.1 scottr
285 1.4 christos reset_database();
286 1.4 christos ALARM;
287 1.4 christos notify_handler(0);
288 1.1 scottr
289 1.1 scottr return (&dummy);
290 1.1 scottr }
291 1.1 scottr
292 1.1 scottr /* sm_notify_1 ------------------------------------------------------------- */
293 1.1 scottr /*
294 1.1 scottr * Purpose: RPC procedure notifying local statd of the crash of another
295 1.1 scottr * Returns: Nothing
296 1.1 scottr * Notes: There is danger of deadlock, since it is quite likely that
297 1.1 scottr * the client procedure that we call will in turn call us
298 1.1 scottr * to remove or adjust the monitor request.
299 1.1 scottr * We therefore fork() a process to do the notifications.
300 1.1 scottr * Note that the main HostInfo structure is in a mmap()
301 1.1 scottr * region and so will be shared with the child, but the
302 1.1 scottr * monList pointed to by the HostInfo is in normal memory.
303 1.1 scottr * Hence if we read the monList before forking, we are
304 1.1 scottr * protected from the parent servicing other requests
305 1.1 scottr * that modify the list.
306 1.1 scottr */
307 1.1 scottr void *
308 1.1 scottr sm_notify_1_svc(arg, req)
309 1.1 scottr stat_chge *arg;
310 1.1 scottr struct svc_req *req;
311 1.1 scottr {
312 1.1 scottr struct timeval timeout = {20, 0}; /* 20 secs timeout */
313 1.1 scottr CLIENT *cli;
314 1.1 scottr static char dummy;
315 1.1 scottr status tx_arg; /* arg sent to callback procedure */
316 1.1 scottr MonList *lp;
317 1.4 christos HostInfo *hp, h;
318 1.1 scottr pid_t pid;
319 1.1 scottr
320 1.1 scottr if (debug)
321 1.1 scottr syslog(LOG_DEBUG, "notify from host %s, new state %d",
322 1.1 scottr arg->mon_name, arg->state);
323 1.1 scottr
324 1.4 christos hp = find_host(arg->mon_name, &h);
325 1.1 scottr if (!hp) {
326 1.1 scottr /* Never heard of this host - why is it notifying us? */
327 1.5 bouyer syslog(LOG_DEBUG, "Unsolicited notification from host %s",
328 1.1 scottr arg->mon_name);
329 1.5 bouyer return (&dummy);
330 1.1 scottr }
331 1.1 scottr lp = hp->monList;
332 1.1 scottr if (!lp) /* We know this host, but have no outstanding requests. */
333 1.5 bouyer return (&dummy);
334 1.1 scottr
335 1.1 scottr pid = fork();
336 1.1 scottr if (pid == -1) {
337 1.1 scottr syslog(LOG_ERR, "Unable to fork notify process - %s",
338 1.1 scottr strerror(errno));
339 1.3 lukem return (FALSE);
340 1.1 scottr }
341 1.1 scottr if (pid)
342 1.1 scottr return (&dummy); /* Parent returns */
343 1.1 scottr
344 1.1 scottr while (lp) {
345 1.1 scottr tx_arg.mon_name = arg->mon_name;
346 1.1 scottr tx_arg.state = arg->state;
347 1.1 scottr memcpy(tx_arg.priv, lp->notifyData, sizeof(tx_arg.priv));
348 1.1 scottr cli = clnt_create(lp->notifyHost, lp->notifyProg,
349 1.1 scottr lp->notifyVers, "udp");
350 1.1 scottr if (!cli)
351 1.1 scottr syslog(LOG_ERR, "Failed to contact host %s%s",
352 1.1 scottr lp->notifyHost, clnt_spcreateerror(""));
353 1.1 scottr else {
354 1.1 scottr if (clnt_call(cli, lp->notifyProc, xdr_status, &tx_arg,
355 1.1 scottr xdr_void, &dummy, timeout) != RPC_SUCCESS)
356 1.1 scottr syslog(LOG_ERR,
357 1.1 scottr "Failed to call rpc.statd client at host %s",
358 1.1 scottr lp->notifyHost);
359 1.1 scottr clnt_destroy(cli);
360 1.1 scottr }
361 1.1 scottr lp = lp->next;
362 1.1 scottr }
363 1.1 scottr
364 1.1 scottr exit(0); /* Child quits */
365 1.1 scottr }
366