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