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