stat_proc.c revision 1.2 1 1.2 lukem /* $NetBSD: stat_proc.c,v 1.2 1997/10/17 16:02:59 lukem 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.2 lukem __RCSID("$NetBSD: stat_proc.c,v 1.2 1997/10/17 16:02:59 lukem Exp $");
39 1.2 lukem #endif
40 1.1 scottr
41 1.1 scottr #include <stdio.h>
42 1.1 scottr #include <stdlib.h>
43 1.1 scottr #include <string.h>
44 1.1 scottr #include <errno.h>
45 1.1 scottr #include <rpc/rpc.h>
46 1.1 scottr #include <syslog.h>
47 1.1 scottr #include <netdb.h>
48 1.1 scottr
49 1.1 scottr #include "statd.h"
50 1.1 scottr
51 1.1 scottr /* sm_stat_1 --------------------------------------------------------------- */
52 1.1 scottr /*
53 1.1 scottr * Purpose: RPC call to enquire if a host can be monitored
54 1.1 scottr * Returns: TRUE for any hostname that can be looked up to give
55 1.1 scottr * an address.
56 1.1 scottr */
57 1.1 scottr struct sm_stat_res *
58 1.1 scottr sm_stat_1_svc(arg, req)
59 1.1 scottr sm_name *arg;
60 1.1 scottr struct svc_req *req;
61 1.1 scottr {
62 1.1 scottr static sm_stat_res res;
63 1.1 scottr
64 1.1 scottr if (debug)
65 1.1 scottr syslog(LOG_DEBUG, "stat called for host %s", arg->mon_name);
66 1.1 scottr
67 1.1 scottr if (gethostbyname(arg->mon_name))
68 1.1 scottr res.res_stat = stat_succ;
69 1.1 scottr else {
70 1.1 scottr syslog(LOG_ERR, "invalid hostname to sm_stat: %s",
71 1.1 scottr arg->mon_name);
72 1.1 scottr res.res_stat = stat_fail;
73 1.1 scottr }
74 1.1 scottr
75 1.1 scottr res.state = status_info->ourState;
76 1.1 scottr return (&res);
77 1.1 scottr }
78 1.1 scottr
79 1.1 scottr /* sm_mon_1 ---------------------------------------------------------------- */
80 1.1 scottr /*
81 1.1 scottr * Purpose: RPC procedure to establish a monitor request
82 1.1 scottr * Returns: Success, unless lack of resources prevents
83 1.1 scottr * the necessary structures from being set up
84 1.1 scottr * to record the request, or if the hostname is not
85 1.1 scottr * valid (as judged by gethostbyname())
86 1.1 scottr */
87 1.1 scottr struct sm_stat_res *
88 1.1 scottr sm_mon_1_svc(arg, req)
89 1.1 scottr mon *arg;
90 1.1 scottr struct svc_req *req;
91 1.1 scottr {
92 1.1 scottr static sm_stat_res res;
93 1.1 scottr HostInfo *hp;
94 1.1 scottr MonList *lp;
95 1.1 scottr
96 1.1 scottr if (debug) {
97 1.1 scottr syslog(LOG_DEBUG, "monitor request for host %s",
98 1.1 scottr arg->mon_id.mon_name);
99 1.1 scottr syslog(LOG_DEBUG, "recall host: %s prog: %d ver: %d proc: %d",
100 1.1 scottr arg->mon_id.mon_name, arg->mon_id.my_id.my_name,
101 1.1 scottr arg->mon_id.my_id.my_prog, arg->mon_id.my_id.my_vers,
102 1.1 scottr arg->mon_id.my_id.my_proc);
103 1.1 scottr }
104 1.1 scottr res.res_stat = stat_fail; /* Assume fail until set otherwise */
105 1.1 scottr res.state = status_info->ourState;
106 1.1 scottr
107 1.1 scottr /*
108 1.1 scottr * Find existing host entry, or create one if not found. If
109 1.1 scottr * find_host() fails, it will have logged the error already.
110 1.1 scottr */
111 1.1 scottr if (!gethostbyname(arg->mon_id.mon_name))
112 1.1 scottr syslog(LOG_ERR, "Invalid hostname to sm_mon: %s",
113 1.1 scottr arg->mon_id.mon_name);
114 1.1 scottr else if (hp = find_host(arg->mon_id.mon_name, TRUE)) {
115 1.1 scottr lp = (MonList *)malloc(sizeof(MonList));
116 1.1 scottr if (!lp)
117 1.1 scottr syslog(LOG_ERR, "Out of memory");
118 1.1 scottr else {
119 1.1 scottr strncpy(lp->notifyHost, arg->mon_id.my_id.my_name,
120 1.1 scottr SM_MAXSTRLEN);
121 1.1 scottr lp->notifyProg = arg->mon_id.my_id.my_prog;
122 1.1 scottr lp->notifyVers = arg->mon_id.my_id.my_vers;
123 1.1 scottr lp->notifyProc = arg->mon_id.my_id.my_proc;
124 1.1 scottr memcpy(lp->notifyData, arg->priv,
125 1.1 scottr sizeof(lp->notifyData));
126 1.1 scottr
127 1.1 scottr lp->next = hp->monList;
128 1.1 scottr hp->monList = lp;
129 1.1 scottr sync_file();
130 1.1 scottr
131 1.1 scottr res.res_stat = stat_succ; /* Report success */
132 1.1 scottr }
133 1.1 scottr }
134 1.1 scottr return (&res);
135 1.1 scottr }
136 1.1 scottr
137 1.1 scottr /* do_unmon ---------------------------------------------------------------- */
138 1.1 scottr /*
139 1.1 scottr * Purpose: Remove a monitor request from a host
140 1.1 scottr * Returns: TRUE if found, FALSE if not found.
141 1.1 scottr * Notes: Common code from sm_unmon_1_svc and sm_unmon_all_1_svc
142 1.1 scottr * In the unlikely event of more than one identical monitor
143 1.1 scottr * request, all are removed.
144 1.1 scottr */
145 1.1 scottr static int
146 1.1 scottr do_unmon(hp, idp)
147 1.1 scottr HostInfo *hp;
148 1.1 scottr my_id *idp;
149 1.1 scottr {
150 1.1 scottr MonList *lp, *next;
151 1.1 scottr MonList *last = NULL;
152 1.1 scottr int result = FALSE;
153 1.1 scottr
154 1.1 scottr lp = hp->monList;
155 1.1 scottr while (lp) {
156 1.1 scottr if (!strncasecmp(idp->my_name, lp->notifyHost, SM_MAXSTRLEN)
157 1.1 scottr && (idp->my_prog == lp->notifyProg)
158 1.1 scottr && (idp->my_proc == lp->notifyProc)
159 1.1 scottr && (idp->my_vers == lp->notifyVers)) {
160 1.1 scottr /* found one. Unhook from chain and free. */
161 1.1 scottr next = lp->next;
162 1.1 scottr if (last)
163 1.1 scottr last->next = next;
164 1.1 scottr else
165 1.1 scottr hp->monList = next;
166 1.1 scottr free(lp);
167 1.1 scottr lp = next;
168 1.1 scottr result = TRUE;
169 1.1 scottr } else {
170 1.1 scottr last = lp;
171 1.1 scottr lp = lp->next;
172 1.1 scottr }
173 1.1 scottr }
174 1.1 scottr return (result);
175 1.1 scottr }
176 1.1 scottr
177 1.1 scottr /* sm_unmon_1 -------------------------------------------------------------- */
178 1.1 scottr /*
179 1.1 scottr * Purpose: RPC procedure to release a monitor request.
180 1.1 scottr * Returns: Local machine's status number
181 1.1 scottr * Notes: The supplied mon_id should match the value passed in an
182 1.1 scottr * earlier call to sm_mon_1
183 1.1 scottr */
184 1.1 scottr struct sm_stat *
185 1.1 scottr sm_unmon_1_svc(arg, req)
186 1.1 scottr mon_id *arg;
187 1.1 scottr struct svc_req *req;
188 1.1 scottr {
189 1.1 scottr static sm_stat res;
190 1.1 scottr HostInfo *hp;
191 1.1 scottr
192 1.1 scottr if (debug) {
193 1.1 scottr syslog(LOG_DEBUG, "un-monitor request for host %s",
194 1.1 scottr arg->mon_name);
195 1.1 scottr syslog(LOG_DEBUG, "recall host: %s prog: %d ver: %d proc: %d",
196 1.1 scottr arg->mon_name, arg->my_id.my_name, arg->my_id.my_prog,
197 1.1 scottr arg->my_id.my_vers, arg->my_id.my_proc);
198 1.1 scottr }
199 1.1 scottr if (hp = find_host(arg->mon_name, FALSE)) {
200 1.1 scottr if (do_unmon(hp, &arg->my_id))
201 1.1 scottr sync_file();
202 1.1 scottr else
203 1.1 scottr syslog(LOG_ERR,
204 1.1 scottr "unmon request from %s, no matching monitor",
205 1.1 scottr arg->my_id.my_name);
206 1.1 scottr } else
207 1.1 scottr syslog(LOG_ERR, "unmon request from %s for unknown host %s",
208 1.1 scottr arg->my_id.my_name, arg->mon_name);
209 1.1 scottr
210 1.1 scottr res.state = status_info->ourState;
211 1.1 scottr
212 1.1 scottr return (&res);
213 1.1 scottr }
214 1.1 scottr
215 1.1 scottr /* sm_unmon_all_1 ---------------------------------------------------------- */
216 1.1 scottr /*
217 1.1 scottr * Purpose: RPC procedure to release monitor requests.
218 1.1 scottr * Returns: Local machine's status number
219 1.1 scottr * Notes: Releases all monitor requests (if any) from the specified
220 1.1 scottr * host and program number.
221 1.1 scottr */
222 1.1 scottr struct sm_stat *
223 1.1 scottr sm_unmon_all_1_svc(arg, req)
224 1.1 scottr my_id *arg;
225 1.1 scottr struct svc_req *req;
226 1.1 scottr {
227 1.1 scottr static sm_stat res;
228 1.1 scottr HostInfo *hp;
229 1.1 scottr MonList *lp;
230 1.1 scottr int i;
231 1.1 scottr
232 1.1 scottr if (debug) {
233 1.1 scottr syslog(LOG_DEBUG,
234 1.1 scottr "unmon_all for host: %s prog: %d ver: %d proc: %d",
235 1.1 scottr arg->my_name, arg->my_prog, arg->my_vers, arg->my_proc);
236 1.1 scottr }
237 1.1 scottr
238 1.1 scottr for (i = status_info->noOfHosts, hp = status_info->hosts; i; i--, hp++)
239 1.1 scottr do_unmon(hp, arg);
240 1.1 scottr
241 1.1 scottr sync_file();
242 1.1 scottr
243 1.1 scottr res.state = status_info->ourState;
244 1.1 scottr
245 1.1 scottr return (&res);
246 1.1 scottr }
247 1.1 scottr
248 1.1 scottr /* sm_simu_crash_1 --------------------------------------------------------- */
249 1.1 scottr /*
250 1.1 scottr * Purpose: RPC procedure to simulate a crash
251 1.1 scottr * Returns: Nothing
252 1.1 scottr * Notes: Standardised mechanism for debug purposes
253 1.1 scottr * The specification says that we should drop all of our
254 1.1 scottr * status information (apart from the list of monitored hosts
255 1.1 scottr * on disc). However, this would confuse the rpc.lockd
256 1.1 scottr * which would be unaware that all of its monitor requests
257 1.1 scottr * had been silently junked. Hence we in fact retain all
258 1.1 scottr * current requests and simply increment the status counter
259 1.1 scottr * and inform all hosts on the monitor list.
260 1.1 scottr */
261 1.1 scottr void *
262 1.1 scottr sm_simu_crash_1_svc(v, req)
263 1.1 scottr void *v;
264 1.1 scottr struct svc_req *req;
265 1.1 scottr {
266 1.1 scottr static char dummy;
267 1.1 scottr int work_to_do;
268 1.1 scottr HostInfo *hp;
269 1.1 scottr int i;
270 1.1 scottr
271 1.1 scottr if (debug)
272 1.1 scottr syslog(LOG_DEBUG, "simu_crash called!!");
273 1.1 scottr
274 1.1 scottr /*
275 1.1 scottr * Simulate crash by setting notify-required flag on all monitored
276 1.1 scottr * hosts, and incrementing our status number. notify_hosts() is
277 1.1 scottr * then called to fork a process to do the notifications.
278 1.1 scottr */
279 1.1 scottr for (i = status_info->noOfHosts, hp = status_info->hosts; i > 0;
280 1.1 scottr i--, hp++) {
281 1.1 scottr if (hp->monList) {
282 1.1 scottr work_to_do = TRUE;
283 1.1 scottr hp->notifyReqd = TRUE;
284 1.1 scottr }
285 1.1 scottr }
286 1.1 scottr status_info->ourState += 2; /* always even numbers if not crashed */
287 1.1 scottr
288 1.1 scottr if (work_to_do)
289 1.1 scottr notify_hosts();
290 1.1 scottr
291 1.1 scottr return (&dummy);
292 1.1 scottr }
293 1.1 scottr
294 1.1 scottr /* sm_notify_1 ------------------------------------------------------------- */
295 1.1 scottr /*
296 1.1 scottr * Purpose: RPC procedure notifying local statd of the crash of another
297 1.1 scottr * Returns: Nothing
298 1.1 scottr * Notes: There is danger of deadlock, since it is quite likely that
299 1.1 scottr * the client procedure that we call will in turn call us
300 1.1 scottr * to remove or adjust the monitor request.
301 1.1 scottr * We therefore fork() a process to do the notifications.
302 1.1 scottr * Note that the main HostInfo structure is in a mmap()
303 1.1 scottr * region and so will be shared with the child, but the
304 1.1 scottr * monList pointed to by the HostInfo is in normal memory.
305 1.1 scottr * Hence if we read the monList before forking, we are
306 1.1 scottr * protected from the parent servicing other requests
307 1.1 scottr * that modify the list.
308 1.1 scottr */
309 1.1 scottr void *
310 1.1 scottr sm_notify_1_svc(arg, req)
311 1.1 scottr stat_chge *arg;
312 1.1 scottr struct svc_req *req;
313 1.1 scottr {
314 1.1 scottr struct timeval timeout = {20, 0}; /* 20 secs timeout */
315 1.1 scottr CLIENT *cli;
316 1.1 scottr static char dummy;
317 1.1 scottr status tx_arg; /* arg sent to callback procedure */
318 1.1 scottr MonList *lp;
319 1.1 scottr HostInfo *hp;
320 1.1 scottr pid_t pid;
321 1.1 scottr
322 1.1 scottr if (debug)
323 1.1 scottr syslog(LOG_DEBUG, "notify from host %s, new state %d",
324 1.1 scottr arg->mon_name, arg->state);
325 1.1 scottr
326 1.1 scottr hp = find_host(arg->mon_name, FALSE);
327 1.1 scottr if (!hp) {
328 1.1 scottr /* Never heard of this host - why is it notifying us? */
329 1.1 scottr syslog(LOG_ERR, "Unsolicited notification from host %s",
330 1.1 scottr arg->mon_name);
331 1.1 scottr return;
332 1.1 scottr }
333 1.1 scottr lp = hp->monList;
334 1.1 scottr if (!lp) /* We know this host, but have no outstanding requests. */
335 1.1 scottr return (FALSE);
336 1.1 scottr
337 1.1 scottr pid = fork();
338 1.1 scottr if (pid == -1) {
339 1.1 scottr syslog(LOG_ERR, "Unable to fork notify process - %s",
340 1.1 scottr strerror(errno));
341 1.1 scottr return;
342 1.1 scottr }
343 1.1 scottr if (pid)
344 1.1 scottr return (&dummy); /* Parent returns */
345 1.1 scottr
346 1.1 scottr while (lp) {
347 1.1 scottr tx_arg.mon_name = arg->mon_name;
348 1.1 scottr tx_arg.state = arg->state;
349 1.1 scottr memcpy(tx_arg.priv, lp->notifyData, sizeof(tx_arg.priv));
350 1.1 scottr cli = clnt_create(lp->notifyHost, lp->notifyProg,
351 1.1 scottr lp->notifyVers, "udp");
352 1.1 scottr if (!cli)
353 1.1 scottr syslog(LOG_ERR, "Failed to contact host %s%s",
354 1.1 scottr lp->notifyHost, clnt_spcreateerror(""));
355 1.1 scottr else {
356 1.1 scottr if (clnt_call(cli, lp->notifyProc, xdr_status, &tx_arg,
357 1.1 scottr xdr_void, &dummy, timeout) != RPC_SUCCESS)
358 1.1 scottr syslog(LOG_ERR,
359 1.1 scottr "Failed to call rpc.statd client at host %s",
360 1.1 scottr lp->notifyHost);
361 1.1 scottr clnt_destroy(cli);
362 1.1 scottr }
363 1.1 scottr lp = lp->next;
364 1.1 scottr }
365 1.1 scottr
366 1.1 scottr exit(0); /* Child quits */
367 1.1 scottr }
368