1*b708da7cSchristos /* $NetBSD: statd.c,v 1.34 2019/12/02 19:23:53 christos Exp $ */
224ce527bSscottr
324ce527bSscottr /*
424ce527bSscottr * Copyright (c) 1995
524ce527bSscottr * A.R. Gordon (andrew.gordon@net-tel.co.uk). All rights reserved.
624ce527bSscottr *
724ce527bSscottr * Redistribution and use in source and binary forms, with or without
824ce527bSscottr * modification, are permitted provided that the following conditions
924ce527bSscottr * are met:
1024ce527bSscottr * 1. Redistributions of source code must retain the above copyright
1124ce527bSscottr * notice, this list of conditions and the following disclaimer.
1224ce527bSscottr * 2. Redistributions in binary form must reproduce the above copyright
1324ce527bSscottr * notice, this list of conditions and the following disclaimer in the
1424ce527bSscottr * documentation and/or other materials provided with the distribution.
1524ce527bSscottr * 3. All advertising materials mentioning features or use of this software
1624ce527bSscottr * must display the following acknowledgement:
1724ce527bSscottr * This product includes software developed for the FreeBSD project
1824ce527bSscottr * 4. Neither the name of the author nor the names of any co-contributors
1924ce527bSscottr * may be used to endorse or promote products derived from this software
2024ce527bSscottr * without specific prior written permission.
2124ce527bSscottr *
2224ce527bSscottr * THIS SOFTWARE IS PROVIDED BY ANDREW GORDON AND CONTRIBUTORS ``AS IS'' AND
2324ce527bSscottr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2424ce527bSscottr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2524ce527bSscottr * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2624ce527bSscottr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2724ce527bSscottr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2824ce527bSscottr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2924ce527bSscottr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3024ce527bSscottr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3124ce527bSscottr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3224ce527bSscottr * SUCH DAMAGE.
3324ce527bSscottr *
3424ce527bSscottr */
3524ce527bSscottr
3690a9b36aSlukem #include <sys/cdefs.h>
3790a9b36aSlukem #ifndef lint
38*b708da7cSchristos __RCSID("$NetBSD: statd.c,v 1.34 2019/12/02 19:23:53 christos Exp $");
3990a9b36aSlukem #endif
4090a9b36aSlukem
4124ce527bSscottr /* main() function for status monitor daemon. Some of the code in this */
4224ce527bSscottr /* file was generated by running rpcgen /usr/include/rpcsvc/sm_inter.x */
4324ce527bSscottr /* The actual program logic is in the file procs.c */
4424ce527bSscottr
4532f51971Smrg #include <sys/param.h>
4659d12b63Stron #include <sys/wait.h>
4732f51971Smrg
4890a9b36aSlukem #include <err.h>
491175f55bSchristos #include <ctype.h>
5090a9b36aSlukem #include <errno.h>
5190a9b36aSlukem #include <fcntl.h>
5290a9b36aSlukem #include <signal.h>
5390a9b36aSlukem #include <stdio.h>
54021be7fdSlukem #include <stdlib.h>
5590a9b36aSlukem #include <string.h>
5690a9b36aSlukem #include <syslog.h>
5790a9b36aSlukem #include <unistd.h>
5856c5efa3Sthorpej #include <util.h>
591175f55bSchristos #include <db.h>
6077044250Sfvdl #include <netconfig.h>
6190a9b36aSlukem
6224ce527bSscottr #include <rpc/rpc.h>
6324ce527bSscottr
6424ce527bSscottr #include "statd.h"
6524ce527bSscottr
661175f55bSchristos struct sigaction sa;
6724ce527bSscottr int debug = 0; /* Controls syslog() for debug msgs */
68bffacbd1Skamil extern int _rpcsvcdirty;
691175f55bSchristos static DB *db; /* Database file */
701175f55bSchristos
711175f55bSchristos Header status_info;
721175f55bSchristos
731175f55bSchristos static char undefdata[] = "\0\1\2\3\4\5\6\7";
741175f55bSchristos static DBT undefkey = {
751175f55bSchristos undefdata,
761175f55bSchristos sizeof(undefdata)
771175f55bSchristos };
781175f55bSchristos
791175f55bSchristos
801175f55bSchristos /* statd.c */
81421949a3Ssevan static int walk_one(int (*fun )(DBT *, HostInfo *, void *), DBT *, DBT *, void *);
82421949a3Ssevan static int walk_db(int (*fun )(DBT *, HostInfo *, void *), void *);
83421949a3Ssevan static int reset_host(DBT *, HostInfo *, void *);
84421949a3Ssevan static int check_work(DBT *, HostInfo *, void *);
85421949a3Ssevan static int unmon_host(DBT *, HostInfo *, void *);
86421949a3Ssevan static int notify_one(DBT *, HostInfo *, void *);
87421949a3Ssevan static void init_file(const char *);
88421949a3Ssevan static int notify_one_host(const char *);
89421949a3Ssevan static void die(int) __dead;
9024ce527bSscottr
9190a9b36aSlukem int
main(int argc,char * argv[])92421949a3Ssevan main(int argc, char *argv[])
9324ce527bSscottr {
9424ce527bSscottr int ch;
95607b156bSchristos struct sigaction nsa;
96d4f83aa2Sfvdl int maxrec = RPC_MAXDATASIZE;
9724ce527bSscottr
9824ce527bSscottr while ((ch = getopt(argc, argv, "d")) != (-1)) {
9924ce527bSscottr switch (ch) {
10024ce527bSscottr case 'd':
10124ce527bSscottr debug = 1;
10224ce527bSscottr break;
10324ce527bSscottr default:
10424ce527bSscottr case '?':
10525bdbb66Scgd (void)fprintf(stderr, "usage: %s [-d]\n",
10625bdbb66Scgd getprogname());
107b2f84794Sscottr exit(1);
10824ce527bSscottr /* NOTREACHED */
10924ce527bSscottr }
11024ce527bSscottr }
11177044250Sfvdl (void)rpcb_unset(SM_PROG, SM_VERS, NULL);
11224ce527bSscottr
113d4f83aa2Sfvdl rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec);
114d4f83aa2Sfvdl
11577044250Sfvdl if (!svc_create(sm_prog_1, SM_PROG, SM_VERS, "udp")) {
116cb8ff824Schristos errx(EXIT_FAILURE, "cannot create udp service.");
11724ce527bSscottr /* NOTREACHED */
11824ce527bSscottr }
11977044250Sfvdl if (!svc_create(sm_prog_1, SM_PROG, SM_VERS, "tcp")) {
120cb8ff824Schristos errx(EXIT_FAILURE, "cannot create udp service.");
12124ce527bSscottr /* NOTREACHED */
12224ce527bSscottr }
12377044250Sfvdl
12424ce527bSscottr init_file("/var/db/statd.status");
12524ce527bSscottr
12624ce527bSscottr /*
12724ce527bSscottr * Note that it is NOT sensible to run this program from inetd - the
12824ce527bSscottr * protocol assumes that it will run immediately at boot time.
12924ce527bSscottr */
1301175f55bSchristos if (!debug)
13124ce527bSscottr daemon(0, 0);
1320b2d4e95Stron
1330b2d4e95Stron sigemptyset(&nsa.sa_mask);
1340b2d4e95Stron nsa.sa_flags = SA_NOCLDSTOP|SA_NOCLDWAIT;
1350b2d4e95Stron nsa.sa_handler = SIG_IGN;
1360b2d4e95Stron (void)sigaction(SIGCHLD, &nsa, NULL);
1370b2d4e95Stron
13856c5efa3Sthorpej pidfile(NULL);
13924ce527bSscottr openlog("rpc.statd", 0, LOG_DAEMON);
14024ce527bSscottr if (debug)
14124ce527bSscottr syslog(LOG_INFO, "Starting - debug enabled");
14224ce527bSscottr else
14324ce527bSscottr syslog(LOG_INFO, "Starting");
14424ce527bSscottr
1451175f55bSchristos sa.sa_handler = die;
1461175f55bSchristos sa.sa_flags = 0;
14724ce527bSscottr sigemptyset(&sa.sa_mask);
1481175f55bSchristos (void)sigaction(SIGTERM, &sa, NULL);
1491175f55bSchristos (void)sigaction(SIGQUIT, &sa, NULL);
1501175f55bSchristos (void)sigaction(SIGHUP, &sa, NULL);
1511175f55bSchristos (void)sigaction(SIGINT, &sa, NULL);
1521175f55bSchristos
1531175f55bSchristos sa.sa_handler = SIG_IGN;
15424ce527bSscottr sa.sa_flags = SA_RESTART;
1551175f55bSchristos sigemptyset(&sa.sa_mask);
1561175f55bSchristos sigaddset(&sa.sa_mask, SIGALRM);
15724ce527bSscottr
15824ce527bSscottr /* Initialisation now complete - start operating */
15924ce527bSscottr
1601175f55bSchristos /* Notify hosts that need it */
1611175f55bSchristos notify_handler(0);
16224ce527bSscottr
1631175f55bSchristos while (1)
16424ce527bSscottr svc_run(); /* Should never return */
1651175f55bSchristos die(0);
16624ce527bSscottr }
16724ce527bSscottr
1681175f55bSchristos /* notify_handler ---------------------------------------------------------- */
16924ce527bSscottr /*
1701175f55bSchristos * Purpose: Catch SIGALRM and collect process status
17124ce527bSscottr * Returns: Nothing.
17224ce527bSscottr * Notes: No special action required, other than to collect the
17324ce527bSscottr * process status and hence allow the child to die:
17424ce527bSscottr * we only use child processes for asynchronous transmission
17524ce527bSscottr * of SM_NOTIFY to other systems, so it is normal for the
17624ce527bSscottr * children to exit when they have done their work.
17724ce527bSscottr */
1781175f55bSchristos void
notify_handler(int sig)179421949a3Ssevan notify_handler(int sig)
18024ce527bSscottr {
1811175f55bSchristos time_t now;
1821175f55bSchristos
1831175f55bSchristos NO_ALARM;
1841175f55bSchristos sa.sa_handler = SIG_IGN;
1851175f55bSchristos (void)sigaction(SIGALRM, &sa, NULL);
1861175f55bSchristos
1871175f55bSchristos now = time(NULL);
1881175f55bSchristos
1891175f55bSchristos (void) walk_db(notify_one, &now);
1901175f55bSchristos
1911175f55bSchristos if (walk_db(check_work, &now) == 0) {
1921175f55bSchristos /*
1931175f55bSchristos * No more work to be done.
1941175f55bSchristos */
1951175f55bSchristos CLR_ALARM;
1961175f55bSchristos return;
1971175f55bSchristos }
1981175f55bSchristos sync_file();
1991175f55bSchristos ALARM;
2001175f55bSchristos alarm(5);
20124ce527bSscottr }
20224ce527bSscottr
20324ce527bSscottr /* sync_file --------------------------------------------------------------- */
20424ce527bSscottr /*
20524ce527bSscottr * Purpose: Packaged call of msync() to flush changes to mmap()ed file
20624ce527bSscottr * Returns: Nothing. Errors to syslog.
20724ce527bSscottr */
20824ce527bSscottr void
sync_file()20924ce527bSscottr sync_file()
21024ce527bSscottr {
2111175f55bSchristos DBT data;
2121175f55bSchristos
2131175f55bSchristos data.data = &status_info;
2141175f55bSchristos data.size = sizeof(status_info);
2151175f55bSchristos switch ((*db->put)(db, &undefkey, &data, 0)) {
2161175f55bSchristos case 0:
2171175f55bSchristos return;
2181175f55bSchristos case -1:
2191175f55bSchristos goto bad;
2201175f55bSchristos default:
2211175f55bSchristos abort();
22224ce527bSscottr }
2231175f55bSchristos if ((*db->sync)(db, 0) == -1) {
2241175f55bSchristos bad:
2251175f55bSchristos syslog(LOG_ERR, "database corrupted %m");
2261175f55bSchristos die(1);
2271175f55bSchristos }
2281175f55bSchristos }
2291175f55bSchristos
2301175f55bSchristos /* change_host -------------------------------------------------------------- */
2311175f55bSchristos /*
2321175f55bSchristos * Purpose: Update/Create an entry for host
2331175f55bSchristos * Returns: Nothing
2341175f55bSchristos * Notes:
2351175f55bSchristos *
2361175f55bSchristos */
2371175f55bSchristos void
change_host(char * hostnamep,HostInfo * hp)238421949a3Ssevan change_host(char *hostnamep, HostInfo *hp)
2391175f55bSchristos {
2401175f55bSchristos DBT key, data;
2411175f55bSchristos char *ptr;
2423cce18ceSbouyer char hostname[MAXHOSTNAMELEN + 1];
2433cce18ceSbouyer HostInfo h;
2443cce18ceSbouyer
2453cce18ceSbouyer strncpy(hostname, hostnamep, MAXHOSTNAMELEN + 1);
2463cce18ceSbouyer h = *hp;
2471175f55bSchristos
2481175f55bSchristos for (ptr = hostname; *ptr; ptr++)
2491175f55bSchristos if (isupper((unsigned char) *ptr))
2501175f55bSchristos *ptr = tolower((unsigned char) *ptr);
2511175f55bSchristos
2521175f55bSchristos key.data = hostname;
2531175f55bSchristos key.size = ptr - hostname + 1;
2543cce18ceSbouyer data.data = &h;
2553cce18ceSbouyer data.size = sizeof(h);
2561175f55bSchristos
2571175f55bSchristos switch ((*db->put)(db, &key, &data, 0)) {
2581175f55bSchristos case -1:
2591175f55bSchristos syslog(LOG_ERR, "database corrupted %m");
2601175f55bSchristos die(1);
2611175f55bSchristos case 0:
2621175f55bSchristos return;
2631175f55bSchristos default:
2641175f55bSchristos abort();
2651175f55bSchristos }
2661175f55bSchristos }
2671175f55bSchristos
26824ce527bSscottr
26924ce527bSscottr /* find_host -------------------------------------------------------------- */
27024ce527bSscottr /*
27124ce527bSscottr * Purpose: Find the entry in the status file for a given host
2721175f55bSchristos * Returns: Copy of entry in hd, or NULL
2731175f55bSchristos * Notes:
2741175f55bSchristos *
27524ce527bSscottr */
27624ce527bSscottr HostInfo *
find_host(char * hostname,HostInfo * hp)277421949a3Ssevan find_host(char *hostname, HostInfo *hp)
2781175f55bSchristos {
2791175f55bSchristos DBT key, data;
2801175f55bSchristos char *ptr;
28124ce527bSscottr
2821175f55bSchristos for (ptr = hostname; *ptr; ptr++)
2831175f55bSchristos if (isupper((unsigned char) *ptr))
2841175f55bSchristos *ptr = tolower((unsigned char) *ptr);
2851175f55bSchristos
2861175f55bSchristos key.data = hostname;
2871175f55bSchristos key.size = ptr - hostname + 1;
2881175f55bSchristos switch ((*db->get)(db, &key, &data, 0)) {
2891175f55bSchristos case 0:
2901175f55bSchristos if (data.size != sizeof(*hp))
2911175f55bSchristos goto bad;
2921175f55bSchristos return memcpy(hp, data.data, sizeof(*hp));
2931175f55bSchristos case 1:
2941175f55bSchristos return NULL;
2951175f55bSchristos case -1:
2961175f55bSchristos goto bad;
2971175f55bSchristos default:
2981175f55bSchristos abort();
2991175f55bSchristos }
3001175f55bSchristos
3011175f55bSchristos bad:
3021175f55bSchristos syslog(LOG_ERR, "Database corrupted %m");
3031175f55bSchristos return NULL;
3041175f55bSchristos }
3051175f55bSchristos
3061175f55bSchristos /* walk_one ------------------------------------------------------------- */
3071175f55bSchristos /*
3081175f55bSchristos * Purpose: Call the given function if the element is valid
3091175f55bSchristos * Returns: Nothing - exits on error
3101175f55bSchristos * Notes:
3111175f55bSchristos */
3121175f55bSchristos static int
walk_one(int (* fun)(DBT *,HostInfo *,void *),DBT * key,DBT * data,void * ptr)313421949a3Ssevan walk_one(int (*fun)(DBT *, HostInfo *, void *), DBT *key, DBT *data, void *ptr)
3141175f55bSchristos {
315491268e2Sbouyer HostInfo h;
3161175f55bSchristos if (key->size == undefkey.size &&
3171175f55bSchristos memcmp(key->data, undefkey.data, key->size) == 0)
3181175f55bSchristos return 0;
3191175f55bSchristos if (data->size != sizeof(HostInfo)) {
3201175f55bSchristos syslog(LOG_ERR, "Bad data in database");
3211175f55bSchristos die(1);
3221175f55bSchristos }
323491268e2Sbouyer memcpy(&h, data->data, sizeof(h));
324491268e2Sbouyer return (*fun)(key, &h, ptr);
3251175f55bSchristos }
3261175f55bSchristos
3271175f55bSchristos /* walk_db -------------------------------------------------------------- */
3281175f55bSchristos /*
3291175f55bSchristos * Purpose: Iterate over all elements calling the given function
3301175f55bSchristos * Returns: -1 if function failed, 0 on success
3311175f55bSchristos * Notes:
3321175f55bSchristos */
3331175f55bSchristos static int
walk_db(int (* fun)(DBT *,HostInfo *,void *),void * ptr)334421949a3Ssevan walk_db(int (*fun)(DBT *, HostInfo *, void *), void *ptr)
3351175f55bSchristos {
3361175f55bSchristos DBT key, data;
3371175f55bSchristos
3381175f55bSchristos switch ((*db->seq)(db, &key, &data, R_FIRST)) {
3391175f55bSchristos case -1:
3401175f55bSchristos goto bad;
3411175f55bSchristos case 1:
3421175f55bSchristos /* We should have at least the magic entry at this point */
3431175f55bSchristos abort();
3441175f55bSchristos case 0:
3451175f55bSchristos if (walk_one(fun, &key, &data, ptr) == -1)
3461175f55bSchristos return -1;
34724ce527bSscottr break;
3481175f55bSchristos default:
3491175f55bSchristos abort();
35024ce527bSscottr }
35124ce527bSscottr
35224ce527bSscottr
3531175f55bSchristos for (;;)
3541175f55bSchristos switch ((*db->seq)(db, &key, &data, R_NEXT)) {
3551175f55bSchristos case -1:
3561175f55bSchristos goto bad;
357e85966b4Sbouyer case 0:
3581175f55bSchristos if (walk_one(fun, &key, &data, ptr) == -1)
3591175f55bSchristos return -1;
3601175f55bSchristos break;
361e85966b4Sbouyer case 1:
3621175f55bSchristos return 0;
3631175f55bSchristos default:
3641175f55bSchristos abort();
3651175f55bSchristos }
3661175f55bSchristos bad:
3671175f55bSchristos syslog(LOG_ERR, "Corrupted database %m");
3681175f55bSchristos die(1);
3691175f55bSchristos }
3701175f55bSchristos
3711175f55bSchristos /* reset_host ------------------------------------------------------------ */
37224ce527bSscottr /*
3731175f55bSchristos * Purpose: Clean up existing hosts in file.
3741175f55bSchristos * Returns: Always success 0.
3751175f55bSchristos * Notes: Clean-up of existing file - monitored hosts will have a
3761175f55bSchristos * pointer to a list of clients, which refers to memory in
3771175f55bSchristos * the previous incarnation of the program and so are
3781175f55bSchristos * meaningless now. These pointers are zeroed and the fact
3791175f55bSchristos * that the host was previously monitored is recorded by
3801175f55bSchristos * setting the notifyReqd flag, which will in due course
3811175f55bSchristos * cause a SM_NOTIFY to be sent.
3821175f55bSchristos *
3831175f55bSchristos * Note that if we crash twice in quick succession, some hosts
3841175f55bSchristos * may already have notifyReqd set, where we didn't manage to
3851175f55bSchristos * notify them before the second crash occurred.
38624ce527bSscottr */
3871175f55bSchristos static int
reset_host(DBT * key,HostInfo * hi,void * ptr)388421949a3Ssevan reset_host(DBT *key, HostInfo *hi, void *ptr)
3891175f55bSchristos {
39024ce527bSscottr
3911175f55bSchristos if (hi->monList) {
392491268e2Sbouyer hi->notifyReqd = *(time_t *) ptr;
3931175f55bSchristos hi->attempts = 0;
3941175f55bSchristos hi->monList = NULL;
39565c4e197Syamt change_host((char *)key->data, hi);
39624ce527bSscottr }
3971175f55bSchristos return 0;
39824ce527bSscottr }
3991175f55bSchristos
4001175f55bSchristos /* check_work ------------------------------------------------------------ */
40124ce527bSscottr /*
4021175f55bSchristos * Purpose: Check if there is work to be done.
4031175f55bSchristos * Returns: 0 if there is no work to be done -1 if there is.
4041175f55bSchristos * Notes:
40524ce527bSscottr */
4061175f55bSchristos static int
check_work(DBT * key,HostInfo * hi,void * ptr)407421949a3Ssevan check_work(DBT *key, HostInfo *hi, void *ptr)
4081175f55bSchristos {
4091175f55bSchristos return hi->notifyReqd ? -1 : 0;
4101175f55bSchristos }
4111175f55bSchristos
4121175f55bSchristos /* unmon_host ------------------------------------------------------------ */
4131175f55bSchristos /*
4141175f55bSchristos * Purpose: Unmonitor a host
4151175f55bSchristos * Returns: 0
4161175f55bSchristos * Notes:
4171175f55bSchristos */
4181175f55bSchristos static int
unmon_host(DBT * key,HostInfo * hi,void * ptr)419421949a3Ssevan unmon_host(DBT *key, HostInfo *hi, void *ptr)
4201175f55bSchristos {
4211175f55bSchristos char *name = key->data;
4221175f55bSchristos
4231175f55bSchristos if (do_unmon(name, hi, ptr))
4241175f55bSchristos change_host(name, hi);
4251175f55bSchristos return 0;
4261175f55bSchristos }
4271175f55bSchristos
4281175f55bSchristos /* notify_one ------------------------------------------------------------ */
4291175f55bSchristos /*
4301175f55bSchristos * Purpose: Notify one host.
4311175f55bSchristos * Returns: 0 if success -1 on failure
4321175f55bSchristos * Notes:
4331175f55bSchristos */
4341175f55bSchristos static int
notify_one(DBT * key,HostInfo * hi,void * ptr)435421949a3Ssevan notify_one(DBT *key, HostInfo *hi, void *ptr)
4361175f55bSchristos {
4371175f55bSchristos time_t now = *(time_t *) ptr;
4381175f55bSchristos char *name = key->data;
4399f17a05fSchristos int error;
4401175f55bSchristos
4411175f55bSchristos if (hi->notifyReqd == 0 || hi->notifyReqd > now)
4421175f55bSchristos return 0;
4431175f55bSchristos
4441175f55bSchristos /*
4451175f55bSchristos * If one of the initial attempts fails, we wait
4461175f55bSchristos * for a while and have another go. This is necessary
4471175f55bSchristos * because when we have crashed, (eg. a power outage)
4481175f55bSchristos * it is quite possible that we won't be able to
4491175f55bSchristos * contact all monitored hosts immediately on restart,
4501175f55bSchristos * either because they crashed too and take longer
4511175f55bSchristos * to come up (in which case the notification isn't
4521175f55bSchristos * really required), or more importantly if some
4531175f55bSchristos * router etc. needed to reach the monitored host
4541175f55bSchristos * has not come back up yet. In this case, we will
4551175f55bSchristos * be a bit late in re-establishing locks (after the
4561175f55bSchristos * grace period) but that is the best we can do. We
4571175f55bSchristos * try 10 times at 5 sec intervals, 10 more times at
4581175f55bSchristos * 1 minute intervals, then 24 more times at hourly
4591175f55bSchristos * intervals, finally giving up altogether if the
4601175f55bSchristos * host hasn't come back to life after 24 hours.
4611175f55bSchristos */
4629f17a05fSchristos if (notify_one_host(name) || hi->attempts++ >= 44) {
4639f17a05fSchristos error = 0;
4649f17a05fSchristos hi->notifyReqd = 0;
4659f17a05fSchristos hi->attempts = 0;
4669f17a05fSchristos } else {
4679f17a05fSchristos error = -1;
4689f17a05fSchristos if (hi->attempts < 10)
4691175f55bSchristos hi->notifyReqd += 5;
4701175f55bSchristos else if (hi->attempts < 20)
4711175f55bSchristos hi->notifyReqd += 60;
4721175f55bSchristos else
4731175f55bSchristos hi->notifyReqd += 60 * 60;
4749f17a05fSchristos }
4753cce18ceSbouyer change_host(name, hi);
4769f17a05fSchristos return error;
47724ce527bSscottr }
47824ce527bSscottr
47924ce527bSscottr /* init_file -------------------------------------------------------------- */
48024ce527bSscottr /*
48124ce527bSscottr * Purpose: Open file, create if necessary, initialise it.
48224ce527bSscottr * Returns: Nothing - exits on error
48324ce527bSscottr * Notes: Called before process becomes daemon, hence logs to
48424ce527bSscottr * stderr rather than syslog.
48524ce527bSscottr * Opens the file, then mmap()s it for ease of access.
48624ce527bSscottr * Also performs initial clean-up of the file, zeroing
48724ce527bSscottr * monitor list pointers, setting the notifyReqd flag in
48824ce527bSscottr * all hosts that had a monitor list, and incrementing
48924ce527bSscottr * the state number to the next even value.
49024ce527bSscottr */
4911175f55bSchristos static void
init_file(const char * filename)492421949a3Ssevan init_file(const char *filename)
49324ce527bSscottr {
4941175f55bSchristos DBT data;
49524ce527bSscottr
49600d19847Stron db = dbopen(filename, O_RDWR|O_CREAT|O_NDELAY|O_EXLOCK, 0644, DB_HASH,
4971175f55bSchristos NULL);
4981175f55bSchristos if (db == NULL)
499cb8ff824Schristos err(EXIT_FAILURE, "Cannot open `%s'", filename);
5001175f55bSchristos
5011175f55bSchristos switch ((*db->get)(db, &undefkey, &data, 0)) {
5021175f55bSchristos case 1:
5031175f55bSchristos /* New database */
5041175f55bSchristos (void)memset(&status_info, 0, sizeof(status_info));
5051175f55bSchristos sync_file();
5061175f55bSchristos return;
5071175f55bSchristos
5081175f55bSchristos case -1:
509cb8ff824Schristos err(EXIT_FAILURE, "error accessing database (%s)", strerror(errno));
5101175f55bSchristos case 0:
5111175f55bSchristos /* Existing database */
5121175f55bSchristos if (data.size != sizeof(status_info))
513cb8ff824Schristos errx(EXIT_FAILURE, "database corrupted %lu != %lu",
514bab116cfSthorpej (u_long)data.size, (u_long)sizeof(status_info));
51545717cbcSyamt memcpy(&status_info, data.data, data.size);
5161175f55bSchristos break;
5171175f55bSchristos default:
5181175f55bSchristos abort();
51924ce527bSscottr }
52024ce527bSscottr
5211175f55bSchristos reset_database();
5221175f55bSchristos return;
5231175f55bSchristos }
5241175f55bSchristos
5251175f55bSchristos /* reset_database --------------------------------------------------------- */
52624ce527bSscottr /*
5271175f55bSchristos * Purpose: Clears the statd database
5281175f55bSchristos * Returns: Nothing
5291175f55bSchristos * Notes: If this is not called on reset, it will leak memory.
53024ce527bSscottr */
5311175f55bSchristos void
reset_database()5321175f55bSchristos reset_database()
5331175f55bSchristos {
5341175f55bSchristos time_t now = time(NULL);
5351175f55bSchristos walk_db(reset_host, &now);
53624ce527bSscottr
53724ce527bSscottr /* Select the next higher even number for the state counter */
5381175f55bSchristos status_info.ourState =
5391175f55bSchristos (status_info.ourState + 2) & 0xfffffffe;
5401175f55bSchristos status_info.ourState++; /* XXX - ??? */
5411175f55bSchristos sync_file();
54224ce527bSscottr }
54324ce527bSscottr
5441175f55bSchristos /* unmon_hosts --------------------------------------------------------- */
54524ce527bSscottr /*
5461175f55bSchristos * Purpose: Unmonitor all the hosts
5471175f55bSchristos * Returns: Nothing
5481175f55bSchristos * Notes:
54924ce527bSscottr */
5501175f55bSchristos void
unmon_hosts()5511175f55bSchristos unmon_hosts()
5521175f55bSchristos {
5531175f55bSchristos time_t now = time(NULL);
5541175f55bSchristos walk_db(unmon_host, &now);
5551175f55bSchristos sync_file();
5561175f55bSchristos }
5571175f55bSchristos
55824ce527bSscottr static int
notify_one_host(const char * hostname)559421949a3Ssevan notify_one_host(const char *hostname)
56024ce527bSscottr {
56124ce527bSscottr struct timeval timeout = {20, 0}; /* 20 secs timeout */
56224ce527bSscottr CLIENT *cli;
56324ce527bSscottr char dummy;
56424ce527bSscottr stat_chge arg;
56532f51971Smrg char our_hostname[MAXHOSTNAMELEN + 1];
56624ce527bSscottr
56724ce527bSscottr gethostname(our_hostname, sizeof(our_hostname));
56832f51971Smrg our_hostname[sizeof(our_hostname) - 1] = '\0';
56924ce527bSscottr arg.mon_name = our_hostname;
5701175f55bSchristos arg.state = status_info.ourState;
57124ce527bSscottr
57224ce527bSscottr if (debug)
57324ce527bSscottr syslog(LOG_DEBUG, "Sending SM_NOTIFY to host %s from %s",
57424ce527bSscottr hostname, our_hostname);
57524ce527bSscottr
57624ce527bSscottr cli = clnt_create(hostname, SM_PROG, SM_VERS, "udp");
57724ce527bSscottr if (!cli) {
57824ce527bSscottr syslog(LOG_ERR, "Failed to contact host %s%s", hostname,
57924ce527bSscottr clnt_spcreateerror(""));
58024ce527bSscottr return (FALSE);
58124ce527bSscottr }
58224ce527bSscottr if (clnt_call(cli, SM_NOTIFY, xdr_stat_chge, &arg, xdr_void,
58324ce527bSscottr &dummy, timeout) != RPC_SUCCESS) {
58424ce527bSscottr syslog(LOG_ERR, "Failed to contact rpc.statd at host %s",
58524ce527bSscottr hostname);
58624ce527bSscottr clnt_destroy(cli);
58724ce527bSscottr return (FALSE);
58824ce527bSscottr }
58924ce527bSscottr clnt_destroy(cli);
59024ce527bSscottr return (TRUE);
59124ce527bSscottr }
59224ce527bSscottr
5931175f55bSchristos
5941175f55bSchristos static void
die(int n)595421949a3Ssevan die(int n)
59624ce527bSscottr {
5971175f55bSchristos (*db->close)(db);
5981175f55bSchristos exit(n);
59924ce527bSscottr }
600