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