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