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