xref: /openbsd/usr.sbin/rpc.statd/statd.c (revision e4453397)
1*e4453397Sjsg /*	$OpenBSD: statd.c,v 1.6 2023/01/04 14:42:46 jsg Exp $	*/
2b0c57e47Ssturm 
3b0c57e47Ssturm /*
4b0c57e47Ssturm  * Copyright (c) 1995
5b0c57e47Ssturm  *	A.R. Gordon (andrew.gordon@net-tel.co.uk).  All rights reserved.
6b0c57e47Ssturm  *
7b0c57e47Ssturm  * Redistribution and use in source and binary forms, with or without
8b0c57e47Ssturm  * modification, are permitted provided that the following conditions
9b0c57e47Ssturm  * are met:
10b0c57e47Ssturm  * 1. Redistributions of source code must retain the above copyright
11b0c57e47Ssturm  *    notice, this list of conditions and the following disclaimer.
12b0c57e47Ssturm  * 2. Redistributions in binary form must reproduce the above copyright
13b0c57e47Ssturm  *    notice, this list of conditions and the following disclaimer in the
14b0c57e47Ssturm  *    documentation and/or other materials provided with the distribution.
15b0c57e47Ssturm  * 3. All advertising materials mentioning features or use of this software
16b0c57e47Ssturm  *    must display the following acknowledgement:
17b0c57e47Ssturm  *	This product includes software developed for the FreeBSD project
18b0c57e47Ssturm  * 4. Neither the name of the author nor the names of any co-contributors
19b0c57e47Ssturm  *    may be used to endorse or promote products derived from this software
20b0c57e47Ssturm  *    without specific prior written permission.
21b0c57e47Ssturm  *
22b0c57e47Ssturm  * THIS SOFTWARE IS PROVIDED BY ANDREW GORDON AND CONTRIBUTORS ``AS IS'' AND
23b0c57e47Ssturm  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24b0c57e47Ssturm  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25b0c57e47Ssturm  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26b0c57e47Ssturm  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27b0c57e47Ssturm  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28b0c57e47Ssturm  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29b0c57e47Ssturm  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30b0c57e47Ssturm  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31b0c57e47Ssturm  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32b0c57e47Ssturm  * SUCH DAMAGE.
33b0c57e47Ssturm  *
34b0c57e47Ssturm  */
35b0c57e47Ssturm 
36b0c57e47Ssturm /* main() function for status monitor daemon.  Some of the code in this	*/
37b0c57e47Ssturm /* file was generated by running rpcgen /usr/include/rpcsvc/sm_inter.x	*/
38b0c57e47Ssturm /* The actual program logic is in the file procs.c			*/
39b0c57e47Ssturm 
40b0c57e47Ssturm #include <sys/wait.h>
41b0c57e47Ssturm 
42b0c57e47Ssturm #include <err.h>
43b0c57e47Ssturm #include <ctype.h>
44b0c57e47Ssturm #include <errno.h>
45b0c57e47Ssturm #include <fcntl.h>
46b0c57e47Ssturm #include <signal.h>
47b0c57e47Ssturm #include <stdio.h>
48b0c57e47Ssturm #include <stdlib.h>
49b0c57e47Ssturm #include <string.h>
50b0c57e47Ssturm #include <syslog.h>
51b0c57e47Ssturm #include <unistd.h>
52b0c57e47Ssturm #include <db.h>
53b0c57e47Ssturm 
54b0c57e47Ssturm #include <rpc/rpc.h>
55b0c57e47Ssturm 
56b0c57e47Ssturm #include "statd.h"
57b0c57e47Ssturm 
58b0c57e47Ssturm struct sigaction sa;
59b0c57e47Ssturm int		debug = 0;		/* Controls syslog() for debug msgs */
60b0c57e47Ssturm int		_rpcsvcdirty = 0;	/* XXX ??? */
61b0c57e47Ssturm static DB	*db;			/* Database file */
62b0c57e47Ssturm 
63b0c57e47Ssturm Header		 status_info;
64b0c57e47Ssturm 
65b0c57e47Ssturm static char undefdata[] = "\0\1\2\3\4\5\6\7";
66b0c57e47Ssturm static DBT undefkey = {
67b0c57e47Ssturm 	undefdata,
68b0c57e47Ssturm 	sizeof(undefdata)
69b0c57e47Ssturm };
70b0c57e47Ssturm extern char *__progname;
71b0c57e47Ssturm 
72b0c57e47Ssturm /* statd.c */
73b0c57e47Ssturm static int walk_one(int (*fun )(DBT *, HostInfo *, void *), DBT *, DBT *, void *);
74b0c57e47Ssturm static int walk_db(int (*fun )(DBT *, HostInfo *, void *), void *);
75b0c57e47Ssturm static int reset_host(DBT *, HostInfo *, void *);
76b0c57e47Ssturm static int check_work(DBT *, HostInfo *, void *);
77b0c57e47Ssturm static int unmon_host(DBT *, HostInfo *, void *);
78b0c57e47Ssturm static int notify_one(DBT *, HostInfo *, void *);
79b0c57e47Ssturm static void init_file(char *);
80b0c57e47Ssturm static int notify_one_host(char *);
8125bf4423Skrw static __dead void die(int);
82b0c57e47Ssturm 
83b0c57e47Ssturm int main(int, char **);
84b0c57e47Ssturm 
85b0c57e47Ssturm int
main(int argc,char ** argv)86b0c57e47Ssturm main(int argc, char **argv)
87b0c57e47Ssturm {
88b0c57e47Ssturm 	SVCXPRT *transp;
89b0c57e47Ssturm 	int ch;
90b0c57e47Ssturm 	struct sigaction nsa;
91b0c57e47Ssturm 
92b0c57e47Ssturm 	while ((ch = getopt(argc, argv, "d")) != (-1)) {
93b0c57e47Ssturm 		switch (ch) {
94b0c57e47Ssturm 		case 'd':
95b0c57e47Ssturm 			debug = 1;
96b0c57e47Ssturm 			break;
97b0c57e47Ssturm 		default:
98b0c57e47Ssturm 			fprintf(stderr, "usage: %s [-d]\n", __progname);
99b0c57e47Ssturm 			exit(1);
100b0c57e47Ssturm 			/* NOTREACHED */
101b0c57e47Ssturm 		}
102b0c57e47Ssturm 	}
103b0c57e47Ssturm 	pmap_unset(SM_PROG, SM_VERS);
104b0c57e47Ssturm 
105b0c57e47Ssturm 	transp = svcudp_create(RPC_ANYSOCK);
106b0c57e47Ssturm 	if (transp == NULL) {
107b0c57e47Ssturm 		errx(1, "cannot create udp service.");
108b0c57e47Ssturm 		/* NOTREACHED */
109b0c57e47Ssturm 	}
110b0c57e47Ssturm 	if (!svc_register(transp, SM_PROG, SM_VERS, sm_prog_1, IPPROTO_UDP)) {
111b0c57e47Ssturm 		errx(1, "unable to register (SM_PROG, SM_VERS, udp).");
112b0c57e47Ssturm 		/* NOTREACHED */
113b0c57e47Ssturm 	}
114b0c57e47Ssturm 	transp = svctcp_create(RPC_ANYSOCK, 0, 0);
115b0c57e47Ssturm 	if (transp == NULL) {
116b0c57e47Ssturm 		errx(1, "cannot create tcp service.");
117b0c57e47Ssturm 		/* NOTREACHED */
118b0c57e47Ssturm 	}
119b0c57e47Ssturm 	if (!svc_register(transp, SM_PROG, SM_VERS, sm_prog_1, IPPROTO_TCP)) {
120b0c57e47Ssturm 		errx(1, "unable to register (SM_PROG, SM_VERS, tcp).");
121b0c57e47Ssturm 		/* NOTREACHED */
122b0c57e47Ssturm 	}
123b0c57e47Ssturm 
124b0c57e47Ssturm 	init_file("/var/db/statd.status");
125b0c57e47Ssturm 
126b0c57e47Ssturm 	/*
127b0c57e47Ssturm 	 * Note that it is NOT sensible to run this program from inetd - the
128b0c57e47Ssturm 	 * protocol assumes that it will run immediately at boot time.
129b0c57e47Ssturm 	 */
130b0c57e47Ssturm 	daemon(0, 0);
131b0c57e47Ssturm 
132b0c57e47Ssturm 	sigemptyset(&nsa.sa_mask);
133b0c57e47Ssturm 	nsa.sa_flags = SA_NOCLDSTOP|SA_NOCLDWAIT;
134b0c57e47Ssturm 	nsa.sa_handler = SIG_IGN;
135b0c57e47Ssturm 	sigaction(SIGCHLD, &nsa, NULL);
136b0c57e47Ssturm 
137b0c57e47Ssturm 	openlog("rpc.statd", 0, LOG_DAEMON);
138b0c57e47Ssturm 	if (debug)
139b0c57e47Ssturm 		syslog(LOG_INFO, "Starting - debug enabled");
140b0c57e47Ssturm 	else
141b0c57e47Ssturm 		syslog(LOG_INFO, "Starting");
142b0c57e47Ssturm 
143b0c57e47Ssturm 	sa.sa_handler = die;
144b0c57e47Ssturm 	sa.sa_flags = 0;
145b0c57e47Ssturm 	sigemptyset(&sa.sa_mask);
146b0c57e47Ssturm 	sigaction(SIGTERM, &sa, NULL);
147b0c57e47Ssturm 	sigaction(SIGQUIT, &sa, NULL);
148b0c57e47Ssturm 	sigaction(SIGHUP, &sa, NULL);
149b0c57e47Ssturm 	sigaction(SIGINT, &sa, NULL);
150b0c57e47Ssturm 
151b0c57e47Ssturm 	sa.sa_handler = SIG_IGN;
152b0c57e47Ssturm 	sa.sa_flags = SA_RESTART;
153b0c57e47Ssturm 	sigemptyset(&sa.sa_mask);
154b0c57e47Ssturm 	sigaddset(&sa.sa_mask, SIGALRM);
155b0c57e47Ssturm 
156b0c57e47Ssturm 	/* Initialisation now complete - start operating */
157b0c57e47Ssturm 
158b0c57e47Ssturm 	/* Notify hosts that need it */
159b0c57e47Ssturm 	notify_handler(0);
160b0c57e47Ssturm 
161b0c57e47Ssturm 	while (1)
162b0c57e47Ssturm 		svc_run();		/* Should never return */
163b0c57e47Ssturm 	die(0);
164b0c57e47Ssturm }
165b0c57e47Ssturm 
166b0c57e47Ssturm /* notify_handler ---------------------------------------------------------- */
167b0c57e47Ssturm /*
168b0c57e47Ssturm  * Purpose:	Catch SIGALRM and collect process status
169b0c57e47Ssturm  * Returns:	Nothing.
170b0c57e47Ssturm  * Notes:	No special action required, other than to collect the
171b0c57e47Ssturm  *		process status and hence allow the child to die:
172b0c57e47Ssturm  *		we only use child processes for asynchronous transmission
173b0c57e47Ssturm  *		of SM_NOTIFY to other systems, so it is normal for the
174b0c57e47Ssturm  *		children to exit when they have done their work.
175b0c57e47Ssturm  */
176b0c57e47Ssturm void
notify_handler(int sig)177b0c57e47Ssturm notify_handler(int sig)
178b0c57e47Ssturm {
179b0c57e47Ssturm 	time_t now;
180b0c57e47Ssturm 
181b0c57e47Ssturm 	NO_ALARM;
182b0c57e47Ssturm 	sa.sa_handler = SIG_IGN;
183b0c57e47Ssturm 	sigaction(SIGALRM, &sa, NULL);
184b0c57e47Ssturm 
185b0c57e47Ssturm 	now = time(NULL);
186b0c57e47Ssturm 
187b0c57e47Ssturm 	walk_db(notify_one, &now);
188b0c57e47Ssturm 
189b0c57e47Ssturm 	if (walk_db(check_work, &now) == 0) {
190b0c57e47Ssturm 		/*
191b0c57e47Ssturm 		 * No more work to be done.
192b0c57e47Ssturm 		 */
193b0c57e47Ssturm 		CLR_ALARM;
194b0c57e47Ssturm 		return;
195b0c57e47Ssturm 	}
196b0c57e47Ssturm 	sync_file();
197b0c57e47Ssturm 	ALARM;
198b0c57e47Ssturm 	alarm(5);
199b0c57e47Ssturm }
200b0c57e47Ssturm 
201b0c57e47Ssturm /* sync_file --------------------------------------------------------------- */
202b0c57e47Ssturm /*
203b0c57e47Ssturm  * Purpose:	Packaged call of msync() to flush changes to mmap()ed file
204b0c57e47Ssturm  * Returns:	Nothing.  Errors to syslog.
205b0c57e47Ssturm  */
206b0c57e47Ssturm void
sync_file()207b0c57e47Ssturm sync_file()
208b0c57e47Ssturm {
209b0c57e47Ssturm 	DBT data;
210b0c57e47Ssturm 
211b0c57e47Ssturm 	data.data = &status_info;
212b0c57e47Ssturm 	data.size = sizeof(status_info);
213b0c57e47Ssturm 	switch ((*db->put)(db, &undefkey, &data, 0)) {
214b0c57e47Ssturm 	case 0:
215b0c57e47Ssturm 		return;
216b0c57e47Ssturm 	case -1:
217b0c57e47Ssturm 		goto bad;
218b0c57e47Ssturm 	default:
219b0c57e47Ssturm 		abort();
220b0c57e47Ssturm 	}
221b0c57e47Ssturm 	if ((*db->sync)(db, 0) == -1) {
222b0c57e47Ssturm bad:
223b0c57e47Ssturm 		syslog(LOG_ERR, "database corrupted %m");
224b0c57e47Ssturm 		die(1);
225b0c57e47Ssturm 	}
226b0c57e47Ssturm }
227b0c57e47Ssturm 
228b0c57e47Ssturm /* change_host -------------------------------------------------------------- */
229b0c57e47Ssturm /*
230b0c57e47Ssturm  * Purpose:	Update/Create an entry for host
231b0c57e47Ssturm  * Returns:	Nothing
232b0c57e47Ssturm  * Notes:
233b0c57e47Ssturm  *
234b0c57e47Ssturm  */
235b0c57e47Ssturm void
change_host(char * hostnamep,HostInfo * hp)236b0c57e47Ssturm change_host(char *hostnamep, HostInfo *hp)
237b0c57e47Ssturm {
238b0c57e47Ssturm 	DBT key, data;
239b0c57e47Ssturm 	char *ptr;
240b9fc9a72Sderaadt 	char hostname[HOST_NAME_MAX+1 + 1];
241b0c57e47Ssturm 	HostInfo h;
242b0c57e47Ssturm 
243b0c57e47Ssturm 	strlcpy(hostname, hostnamep, sizeof(hostname));
244b0c57e47Ssturm 	h = *hp;
245b0c57e47Ssturm 
246b0c57e47Ssturm 	for (ptr = hostname; *ptr; ptr++)
247b0c57e47Ssturm 		if (isupper((unsigned char) *ptr))
248b0c57e47Ssturm 			*ptr = tolower((unsigned char) *ptr);
249b0c57e47Ssturm 
250b0c57e47Ssturm 	key.data = hostname;
251b0c57e47Ssturm 	key.size = ptr - hostname + 1;
252b0c57e47Ssturm 	data.data = &h;
253b0c57e47Ssturm 	data.size = sizeof(h);
254b0c57e47Ssturm 
255b0c57e47Ssturm 	switch ((*db->put)(db, &key, &data, 0)) {
256b0c57e47Ssturm 	case -1:
257b0c57e47Ssturm 		syslog(LOG_ERR, "database corrupted %m");
258b0c57e47Ssturm 		die(1);
259b0c57e47Ssturm 	case 0:
260b0c57e47Ssturm 		return;
261b0c57e47Ssturm 	default:
262b0c57e47Ssturm 		abort();
263b0c57e47Ssturm 	}
264b0c57e47Ssturm }
265b0c57e47Ssturm 
266b0c57e47Ssturm 
267b0c57e47Ssturm /* find_host -------------------------------------------------------------- */
268b0c57e47Ssturm /*
269b0c57e47Ssturm  * Purpose:	Find the entry in the status file for a given host
270b0c57e47Ssturm  * Returns:	Copy of entry in hd, or NULL
271b0c57e47Ssturm  * Notes:
272b0c57e47Ssturm  *
273b0c57e47Ssturm  */
274b0c57e47Ssturm HostInfo *
find_host(char * hostname,HostInfo * hp)275b0c57e47Ssturm find_host(char *hostname, HostInfo *hp)
276b0c57e47Ssturm {
277b0c57e47Ssturm 	DBT key, data;
278b0c57e47Ssturm 	char *ptr;
279b0c57e47Ssturm 
280b0c57e47Ssturm 	for (ptr = hostname; *ptr; ptr++)
281b0c57e47Ssturm 		if (isupper((unsigned char) *ptr))
282b0c57e47Ssturm 			*ptr = tolower((unsigned char) *ptr);
283b0c57e47Ssturm 
284b0c57e47Ssturm 	key.data = hostname;
285b0c57e47Ssturm 	key.size = ptr - hostname + 1;
286b0c57e47Ssturm 	switch ((*db->get)(db, &key, &data, 0)) {
287b0c57e47Ssturm 	case 0:
288b0c57e47Ssturm 		if (data.size != sizeof(*hp))
289b0c57e47Ssturm 			goto bad;
290b0c57e47Ssturm 		return memcpy(hp, data.data, sizeof(*hp));
291b0c57e47Ssturm 	case 1:
292b0c57e47Ssturm 		return NULL;
293b0c57e47Ssturm 	case -1:
294b0c57e47Ssturm 		goto bad;
295b0c57e47Ssturm 	default:
296b0c57e47Ssturm 		abort();
297b0c57e47Ssturm 	}
298b0c57e47Ssturm 
299b0c57e47Ssturm bad:
300b0c57e47Ssturm 	syslog(LOG_ERR, "Database corrupted %m");
301b0c57e47Ssturm 	return NULL;
302b0c57e47Ssturm }
303b0c57e47Ssturm 
304b0c57e47Ssturm /* walk_one ------------------------------------------------------------- */
305b0c57e47Ssturm /*
306b0c57e47Ssturm  * Purpose:	Call the given function if the element is valid
307b0c57e47Ssturm  * Returns:	Nothing - exits on error
308b0c57e47Ssturm  * Notes:
309b0c57e47Ssturm  */
310b0c57e47Ssturm static int
walk_one(int (* fun)(DBT *,HostInfo *,void *),DBT * key,DBT * data,void * ptr)311b0c57e47Ssturm walk_one(int (*fun)(DBT *, HostInfo *, void *), DBT *key, DBT *data, void *ptr)
312b0c57e47Ssturm {
313b0c57e47Ssturm 	HostInfo h;
314b0c57e47Ssturm 	if (key->size == undefkey.size &&
315b0c57e47Ssturm 	    memcmp(key->data, undefkey.data, key->size) == 0)
316b0c57e47Ssturm 		return 0;
317b0c57e47Ssturm 	if (data->size != sizeof(HostInfo)) {
318b0c57e47Ssturm 		syslog(LOG_ERR, "Bad data in database");
319b0c57e47Ssturm 		die(1);
320b0c57e47Ssturm 	}
321b0c57e47Ssturm 	memcpy(&h, data->data, sizeof(h));
322b0c57e47Ssturm 	return (*fun)(key, &h, ptr);
323b0c57e47Ssturm }
324b0c57e47Ssturm 
325b0c57e47Ssturm /* walk_db -------------------------------------------------------------- */
326b0c57e47Ssturm /*
327b0c57e47Ssturm  * Purpose:	Iterate over all elements calling the given function
328b0c57e47Ssturm  * Returns:	-1 if function failed, 0 on success
329b0c57e47Ssturm  * Notes:
330b0c57e47Ssturm  */
331b0c57e47Ssturm static int
walk_db(int (* fun)(DBT *,HostInfo *,void *),void * ptr)332b0c57e47Ssturm walk_db(int (*fun)(DBT *, HostInfo *, void *), void *ptr)
333b0c57e47Ssturm {
334b0c57e47Ssturm 	DBT key, data;
335b0c57e47Ssturm 
336b0c57e47Ssturm 	switch ((*db->seq)(db, &key, &data, R_FIRST)) {
337b0c57e47Ssturm 	case -1:
338b0c57e47Ssturm 		goto bad;
339b0c57e47Ssturm 	case 1:
340b0c57e47Ssturm 		/* We should have at least the magic entry at this point */
341b0c57e47Ssturm 		abort();
342b0c57e47Ssturm 	case 0:
343b0c57e47Ssturm 		if (walk_one(fun, &key, &data, ptr) == -1)
344b0c57e47Ssturm 			return -1;
345b0c57e47Ssturm 		break;
346b0c57e47Ssturm 	default:
347b0c57e47Ssturm 		abort();
348b0c57e47Ssturm 	}
349b0c57e47Ssturm 
350b0c57e47Ssturm 	for (;;)
351b0c57e47Ssturm 		switch ((*db->seq)(db, &key, &data, R_NEXT)) {
352b0c57e47Ssturm 		case -1:
353b0c57e47Ssturm 			goto bad;
354b0c57e47Ssturm 		case 0:
355b0c57e47Ssturm 			if (walk_one(fun, &key, &data, ptr) == -1)
356b0c57e47Ssturm 				return -1;
357b0c57e47Ssturm 			break;
358b0c57e47Ssturm 		case 1:
359b0c57e47Ssturm 			return 0;
360b0c57e47Ssturm 		default:
361b0c57e47Ssturm 			abort();
362b0c57e47Ssturm 		}
363b0c57e47Ssturm bad:
364b0c57e47Ssturm 	syslog(LOG_ERR, "Corrupted database %m");
365b0c57e47Ssturm 	die(1);
366b0c57e47Ssturm }
367b0c57e47Ssturm 
368b0c57e47Ssturm /* reset_host ------------------------------------------------------------ */
369b0c57e47Ssturm /*
370b0c57e47Ssturm  * Purpose:	Clean up existing hosts in file.
371b0c57e47Ssturm  * Returns:	Always success 0.
372b0c57e47Ssturm  * Notes:	Clean-up of existing file - monitored hosts will have a
373b0c57e47Ssturm  *		pointer to a list of clients, which refers to memory in
374b0c57e47Ssturm  *		the previous incarnation of the program and so are
375b0c57e47Ssturm  *		meaningless now.  These pointers are zeroed and the fact
376b0c57e47Ssturm  *		that the host was previously monitored is recorded by
377b0c57e47Ssturm  *		setting the notifyReqd flag, which will in due course
378b0c57e47Ssturm  *		cause a SM_NOTIFY to be sent.
379b0c57e47Ssturm  *
380b0c57e47Ssturm  *		Note that if we crash twice in quick succession, some hosts
381b0c57e47Ssturm  *		may already have notifyReqd set, where we didn't manage to
382b0c57e47Ssturm  *		notify them before the second crash occurred.
383b0c57e47Ssturm  */
384b0c57e47Ssturm static int
reset_host(DBT * key,HostInfo * hi,void * ptr)385b0c57e47Ssturm reset_host(DBT *key, HostInfo *hi, void *ptr)
386b0c57e47Ssturm {
387b0c57e47Ssturm 	if (hi->monList) {
388b0c57e47Ssturm 		hi->notifyReqd = *(time_t *) ptr;
389b0c57e47Ssturm 		hi->attempts = 0;
390b0c57e47Ssturm 		hi->monList = NULL;
391b0c57e47Ssturm 		change_host((char *)key->data, hi);
392b0c57e47Ssturm 	}
393b0c57e47Ssturm 	return 0;
394b0c57e47Ssturm }
395b0c57e47Ssturm 
396b0c57e47Ssturm /* check_work ------------------------------------------------------------ */
397b0c57e47Ssturm /*
398b0c57e47Ssturm  * Purpose:	Check if there is work to be done.
399b0c57e47Ssturm  * Returns:	0 if there is no work to be done -1 if there is.
400b0c57e47Ssturm  * Notes:
401b0c57e47Ssturm  */
402b0c57e47Ssturm static int
check_work(DBT * key,HostInfo * hi,void * ptr)403b0c57e47Ssturm check_work(DBT *key, HostInfo *hi, void *ptr)
404b0c57e47Ssturm {
405b0c57e47Ssturm 	return hi->notifyReqd ? -1 : 0;
406b0c57e47Ssturm }
407b0c57e47Ssturm 
408b0c57e47Ssturm /* unmon_host ------------------------------------------------------------ */
409b0c57e47Ssturm /*
410b0c57e47Ssturm  * Purpose:	Unmonitor a host
411b0c57e47Ssturm  * Returns:	0
412b0c57e47Ssturm  * Notes:
413b0c57e47Ssturm  */
414b0c57e47Ssturm static int
unmon_host(DBT * key,HostInfo * hi,void * ptr)415b0c57e47Ssturm unmon_host(DBT *key, HostInfo *hi, void *ptr)
416b0c57e47Ssturm {
417b0c57e47Ssturm 	char *name = key->data;
418b0c57e47Ssturm 
419b0c57e47Ssturm 	if (do_unmon(name, hi, ptr))
420b0c57e47Ssturm 		change_host(name, hi);
421b0c57e47Ssturm 	return 0;
422b0c57e47Ssturm }
423b0c57e47Ssturm 
424b0c57e47Ssturm /* notify_one ------------------------------------------------------------ */
425b0c57e47Ssturm /*
426b0c57e47Ssturm  * Purpose:	Notify one host.
427b0c57e47Ssturm  * Returns:	0 if success -1 on failure
428b0c57e47Ssturm  * Notes:
429b0c57e47Ssturm  */
430b0c57e47Ssturm static int
notify_one(DBT * key,HostInfo * hi,void * ptr)431b0c57e47Ssturm notify_one(DBT *key, HostInfo *hi, void *ptr)
432b0c57e47Ssturm {
433b0c57e47Ssturm 	time_t now = *(time_t *) ptr;
434b0c57e47Ssturm 	char *name = key->data;
435b0c57e47Ssturm 	int error;
436b0c57e47Ssturm 
437b0c57e47Ssturm 	if (hi->notifyReqd == 0 || hi->notifyReqd > now)
438b0c57e47Ssturm 		return 0;
439b0c57e47Ssturm 
440b0c57e47Ssturm 	/*
441b0c57e47Ssturm 	 * If one of the initial attempts fails, we wait
442b0c57e47Ssturm 	 * for a while and have another go.  This is necessary
443b0c57e47Ssturm 	 * because when we have crashed, (eg. a power outage)
444b0c57e47Ssturm 	 * it is quite possible that we won't be able to
445b0c57e47Ssturm 	 * contact all monitored hosts immediately on restart,
446b0c57e47Ssturm 	 * either because they crashed too and take longer
447b0c57e47Ssturm 	 * to come up (in which case the notification isn't
448b0c57e47Ssturm 	 * really required), or more importantly if some
449b0c57e47Ssturm 	 * router etc. needed to reach the monitored host
450b0c57e47Ssturm 	 * has not come back up yet.  In this case, we will
451b0c57e47Ssturm 	 * be a bit late in re-establishing locks (after the
452b0c57e47Ssturm 	 * grace period) but that is the best we can do.  We
453b0c57e47Ssturm 	 * try 10 times at 5 sec intervals, 10 more times at
454b0c57e47Ssturm 	 * 1 minute intervals, then 24 more times at hourly
455b0c57e47Ssturm 	 * intervals, finally giving up altogether if the
456b0c57e47Ssturm 	 * host hasn't come back to life after 24 hours.
457b0c57e47Ssturm 	 */
458b0c57e47Ssturm 	if (notify_one_host(name) || hi->attempts++ >= 44) {
459b0c57e47Ssturm 		error = 0;
460b0c57e47Ssturm 		hi->notifyReqd = 0;
461b0c57e47Ssturm 		hi->attempts = 0;
462b0c57e47Ssturm 	} else {
463b0c57e47Ssturm 		error = -1;
464b0c57e47Ssturm 		if (hi->attempts < 10)
465b0c57e47Ssturm 			hi->notifyReqd += 5;
466b0c57e47Ssturm 		else if (hi->attempts < 20)
467b0c57e47Ssturm 			hi->notifyReqd += 60;
468b0c57e47Ssturm 		else
469b0c57e47Ssturm 			hi->notifyReqd += 60 * 60;
470b0c57e47Ssturm 	}
471b0c57e47Ssturm 	change_host(name, hi);
472b0c57e47Ssturm 	return error;
473b0c57e47Ssturm }
474b0c57e47Ssturm 
475b0c57e47Ssturm /* init_file -------------------------------------------------------------- */
476b0c57e47Ssturm /*
477b0c57e47Ssturm  * Purpose:	Open file, create if necessary, initialise it.
478b0c57e47Ssturm  * Returns:	Nothing - exits on error
479b0c57e47Ssturm  * Notes:	Called before process becomes daemon, hence logs to
480b0c57e47Ssturm  *		stderr rather than syslog.
481b0c57e47Ssturm  *		Opens the file, then mmap()s it for ease of access.
482b0c57e47Ssturm  *		Also performs initial clean-up of the file, zeroing
483b0c57e47Ssturm  *		monitor list pointers, setting the notifyReqd flag in
484b0c57e47Ssturm  *		all hosts that had a monitor list, and incrementing
485b0c57e47Ssturm  *		the state number to the next even value.
486b0c57e47Ssturm  */
487b0c57e47Ssturm static void
init_file(char * filename)488b0c57e47Ssturm init_file(char *filename)
489b0c57e47Ssturm {
490b0c57e47Ssturm 	DBT data;
491b0c57e47Ssturm 
492b0c57e47Ssturm 	db = dbopen(filename, O_RDWR|O_CREAT|O_NDELAY|O_EXLOCK, 0644, DB_HASH,
493b0c57e47Ssturm 	    NULL);
494b0c57e47Ssturm 	if (db == NULL)
495b0c57e47Ssturm 		err(1, "Cannot open `%s'", filename);
496b0c57e47Ssturm 
497b0c57e47Ssturm 	switch ((*db->get)(db, &undefkey, &data, 0)) {
498b0c57e47Ssturm 	case 1:
499b0c57e47Ssturm 		/* New database */
500b0c57e47Ssturm 		memset(&status_info, 0, sizeof(status_info));
501b0c57e47Ssturm 		sync_file();
502b0c57e47Ssturm 		return;
503b0c57e47Ssturm 	case -1:
504b0c57e47Ssturm 		err(1, "error accessing database (%m)");
505b0c57e47Ssturm 	case 0:
506b0c57e47Ssturm 		/* Existing database */
507b0c57e47Ssturm 		if (data.size != sizeof(status_info))
508b0c57e47Ssturm 			errx(1, "database corrupted %lu != %lu",
509b0c57e47Ssturm 			    (u_long)data.size, (u_long)sizeof(status_info));
510b0c57e47Ssturm 		memcpy(&status_info, data.data, data.size);
511b0c57e47Ssturm 		break;
512b0c57e47Ssturm 	default:
513b0c57e47Ssturm 		abort();
514b0c57e47Ssturm 	}
515b0c57e47Ssturm 
516b0c57e47Ssturm 	reset_database();
517b0c57e47Ssturm 	return;
518b0c57e47Ssturm }
519b0c57e47Ssturm 
520b0c57e47Ssturm /* reset_database --------------------------------------------------------- */
521b0c57e47Ssturm /*
522b0c57e47Ssturm  * Purpose:	Clears the statd database
523b0c57e47Ssturm  * Returns:	Nothing
524b0c57e47Ssturm  * Notes:	If this is not called on reset, it will leak memory.
525b0c57e47Ssturm  */
526b0c57e47Ssturm void
reset_database(void)527b0c57e47Ssturm reset_database(void)
528b0c57e47Ssturm {
529b0c57e47Ssturm 	time_t now = time(NULL);
530b0c57e47Ssturm 	walk_db(reset_host, &now);
531b0c57e47Ssturm 
532b0c57e47Ssturm 	/* Select the next higher even number for the state counter */
533b0c57e47Ssturm 	status_info.ourState =
534b0c57e47Ssturm 	    (status_info.ourState + 2) & 0xfffffffe;
535b0c57e47Ssturm 	status_info.ourState++;	/* XXX - ??? */
536b0c57e47Ssturm 	sync_file();
537b0c57e47Ssturm }
538b0c57e47Ssturm 
539b0c57e47Ssturm /* unmon_hosts --------------------------------------------------------- */
540b0c57e47Ssturm /*
541b0c57e47Ssturm  * Purpose:	Unmonitor all the hosts
542b0c57e47Ssturm  * Returns:	Nothing
543b0c57e47Ssturm  * Notes:
544b0c57e47Ssturm  */
545b0c57e47Ssturm void
unmon_hosts(void)546b0c57e47Ssturm unmon_hosts(void)
547b0c57e47Ssturm {
548b0c57e47Ssturm 	time_t now = time(NULL);
549b0c57e47Ssturm 	walk_db(unmon_host, &now);
550b0c57e47Ssturm 	sync_file();
551b0c57e47Ssturm }
552b0c57e47Ssturm 
553b0c57e47Ssturm static int
notify_one_host(char * hostname)554b0c57e47Ssturm notify_one_host(char *hostname)
555b0c57e47Ssturm {
556b0c57e47Ssturm 	struct timeval timeout = {20, 0};	/* 20 secs timeout */
557b0c57e47Ssturm 	CLIENT *cli;
558b0c57e47Ssturm 	char dummy;
559b0c57e47Ssturm 	stat_chge arg;
560b9fc9a72Sderaadt 	char our_hostname[HOST_NAME_MAX+1 + 1];
561b0c57e47Ssturm 
562b0c57e47Ssturm 	gethostname(our_hostname, sizeof(our_hostname));
563b0c57e47Ssturm 	our_hostname[sizeof(our_hostname) - 1] = '\0';
564b0c57e47Ssturm 	arg.mon_name = our_hostname;
565b0c57e47Ssturm 	arg.state = status_info.ourState;
566b0c57e47Ssturm 
567b0c57e47Ssturm 	if (debug)
568b0c57e47Ssturm 		syslog(LOG_DEBUG, "Sending SM_NOTIFY to host %s from %s",
569b0c57e47Ssturm 		    hostname, our_hostname);
570b0c57e47Ssturm 
571b0c57e47Ssturm 	cli = clnt_create(hostname, SM_PROG, SM_VERS, "udp");
572b0c57e47Ssturm 	if (!cli) {
573b0c57e47Ssturm 		syslog(LOG_ERR, "Failed to contact host %s%s", hostname,
574b0c57e47Ssturm 		    clnt_spcreateerror(""));
575b0c57e47Ssturm 		return (FALSE);
576b0c57e47Ssturm 	}
577b0c57e47Ssturm 	if (clnt_call(cli, SM_NOTIFY, xdr_stat_chge, &arg, xdr_void,
578b0c57e47Ssturm 	    &dummy, timeout) != RPC_SUCCESS) {
579b0c57e47Ssturm 		syslog(LOG_ERR, "Failed to contact rpc.statd at host %s",
580b0c57e47Ssturm 		    hostname);
581b0c57e47Ssturm 		clnt_destroy(cli);
582b0c57e47Ssturm 		return (FALSE);
583b0c57e47Ssturm 	}
584b0c57e47Ssturm 	clnt_destroy(cli);
585b0c57e47Ssturm 	return (TRUE);
586b0c57e47Ssturm }
587b0c57e47Ssturm 
58825bf4423Skrw static __dead void
die(int n)589b0c57e47Ssturm die(int n)
590b0c57e47Ssturm {
591b0c57e47Ssturm 	(*db->close)(db);
592b0c57e47Ssturm 	exit(n);
593b0c57e47Ssturm }
594