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