Home | History | Annotate | Line # | Download | only in rpc.lockd
lock_proc.c revision 1.4
      1  1.4  bouyer /*	$NetBSD: lock_proc.c,v 1.4 2000/06/07 14:34:40 bouyer 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.1  scottr 
     36  1.2   lukem #include <sys/cdefs.h>
     37  1.2   lukem #ifndef lint
     38  1.4  bouyer __RCSID("$NetBSD: lock_proc.c,v 1.4 2000/06/07 14:34:40 bouyer Exp $");
     39  1.2   lukem #endif
     40  1.2   lukem 
     41  1.1  scottr #include <sys/param.h>
     42  1.1  scottr #include <sys/socket.h>
     43  1.1  scottr 
     44  1.1  scottr #include <netinet/in.h>
     45  1.1  scottr #include <arpa/inet.h>
     46  1.1  scottr 
     47  1.2   lukem #include <netdb.h>
     48  1.2   lukem #include <stdio.h>
     49  1.2   lukem #include <string.h>
     50  1.2   lukem #include <syslog.h>
     51  1.2   lukem 
     52  1.1  scottr #include <rpc/rpc.h>
     53  1.1  scottr #include <rpcsvc/sm_inter.h>
     54  1.1  scottr 
     55  1.1  scottr #include "lockd.h"
     56  1.1  scottr #include "nlm_prot.h"
     57  1.4  bouyer #include "lockd_lock.h"
     58  1.1  scottr 
     59  1.1  scottr 
     60  1.1  scottr #define	CLIENT_CACHE_SIZE	64	/* No. of client sockets cached */
     61  1.1  scottr #define	CLIENT_CACHE_LIFETIME	120	/* In seconds */
     62  1.1  scottr 
     63  1.2   lukem static void	log_from_addr __P((char *, struct svc_req *));
     64  1.2   lukem 
     65  1.1  scottr /* log_from_addr ----------------------------------------------------------- */
     66  1.1  scottr /*
     67  1.1  scottr  * Purpose:	Log name of function called and source address
     68  1.1  scottr  * Returns:	Nothing
     69  1.1  scottr  * Notes:	Extracts the source address from the transport handle
     70  1.1  scottr  *		passed in as part of the called procedure specification
     71  1.1  scottr  */
     72  1.4  bouyer static void
     73  1.1  scottr log_from_addr(fun_name, req)
     74  1.1  scottr 	char *fun_name;
     75  1.1  scottr 	struct svc_req *req;
     76  1.1  scottr {
     77  1.1  scottr 	struct sockaddr_in *addr;
     78  1.1  scottr 	struct hostent *host;
     79  1.1  scottr 	char hostname_buf[40];
     80  1.1  scottr 
     81  1.1  scottr 	addr = svc_getcaller(req->rq_xprt);
     82  1.1  scottr 	host = gethostbyaddr((char *)&(addr->sin_addr), addr->sin_len, AF_INET);
     83  1.1  scottr 	if (host) {
     84  1.1  scottr 		strncpy(hostname_buf, host->h_name, sizeof(hostname_buf));
     85  1.1  scottr 		hostname_buf[sizeof(hostname_buf) - 1] = '\0';
     86  1.1  scottr 	} else		/* No hostname available - print raw address */
     87  1.1  scottr 		strcpy(hostname_buf, inet_ntoa(addr->sin_addr));
     88  1.1  scottr 
     89  1.1  scottr 	syslog(LOG_DEBUG, "%s from %s", fun_name, hostname_buf);
     90  1.1  scottr }
     91  1.1  scottr 
     92  1.1  scottr /* get_client -------------------------------------------------------------- */
     93  1.1  scottr /*
     94  1.1  scottr  * Purpose:	Get a CLIENT* for making RPC calls to lockd on given host
     95  1.1  scottr  * Returns:	CLIENT* pointer, from clnt_udp_create, or NULL if error
     96  1.1  scottr  * Notes:	Creating a CLIENT* is quite expensive, involving a
     97  1.1  scottr  *		conversation with the remote portmapper to get the
     98  1.1  scottr  *		port number.  Since a given client is quite likely
     99  1.1  scottr  *		to make several locking requests in succession, it is
    100  1.1  scottr  *		desirable to cache the created CLIENT*.
    101  1.1  scottr  *
    102  1.1  scottr  *		Since we are using UDP rather than TCP, there is no cost
    103  1.1  scottr  *		to the remote system in keeping these cached indefinitely.
    104  1.1  scottr  *		Unfortunately there is a snag: if the remote system
    105  1.1  scottr  *		reboots, the cached portmapper results will be invalid,
    106  1.1  scottr  *		and we will never detect this since all of the xxx_msg()
    107  1.1  scottr  *		calls return no result - we just fire off a udp packet
    108  1.1  scottr  *		and hope for the best.
    109  1.1  scottr  *
    110  1.1  scottr  *		We solve this by discarding cached values after two
    111  1.1  scottr  *		minutes, regardless of whether they have been used
    112  1.1  scottr  *		in the meanwhile (since a bad one might have been used
    113  1.1  scottr  *		plenty of times, as the host keeps retrying the request
    114  1.1  scottr  *		and we keep sending the reply back to the wrong port).
    115  1.1  scottr  *
    116  1.1  scottr  *		Given that the entries will always expire in the order
    117  1.1  scottr  *		that they were created, there is no point in a LRU
    118  1.1  scottr  *		algorithm for when the cache gets full - entries are
    119  1.1  scottr  *		always re-used in sequence.
    120  1.1  scottr  */
    121  1.1  scottr static CLIENT *clnt_cache_ptr[CLIENT_CACHE_SIZE];
    122  1.1  scottr static long clnt_cache_time[CLIENT_CACHE_SIZE];	/* time entry created */
    123  1.1  scottr static struct in_addr clnt_cache_addr[CLIENT_CACHE_SIZE];
    124  1.1  scottr static int clnt_cache_next_to_use = 0;
    125  1.1  scottr 
    126  1.4  bouyer CLIENT *
    127  1.4  bouyer get_client(host_addr, vers)
    128  1.1  scottr 	struct sockaddr_in *host_addr;
    129  1.4  bouyer 	u_long vers;
    130  1.1  scottr {
    131  1.1  scottr 	CLIENT *client;
    132  1.1  scottr 	struct timeval retry_time, time_now;
    133  1.1  scottr 	int i, sock_no;
    134  1.1  scottr 
    135  1.1  scottr 	gettimeofday(&time_now, NULL);
    136  1.1  scottr 
    137  1.1  scottr 	/*
    138  1.1  scottr 	 * Search for the given client in the cache, zapping any expired
    139  1.1  scottr 	 * entries that we happen to notice in passing.
    140  1.1  scottr 	 */
    141  1.1  scottr 	for (i = 0; i < CLIENT_CACHE_SIZE; i++) {
    142  1.1  scottr 		client = clnt_cache_ptr[i];
    143  1.1  scottr 		if (client && ((clnt_cache_time[i] + CLIENT_CACHE_LIFETIME)
    144  1.1  scottr 		    < time_now.tv_sec)) {
    145  1.1  scottr 			/* Cache entry has expired. */
    146  1.1  scottr 			if (debug_level > 3)
    147  1.1  scottr 				syslog(LOG_DEBUG, "Expired CLIENT* in cache");
    148  1.1  scottr 			clnt_cache_time[i] = 0L;
    149  1.1  scottr 			clnt_destroy(client);
    150  1.1  scottr 			clnt_cache_ptr[i] = NULL;
    151  1.1  scottr 			client = NULL;
    152  1.1  scottr 		}
    153  1.1  scottr 		if (client && !memcmp(&clnt_cache_addr[i],
    154  1.1  scottr 		    &host_addr->sin_addr, sizeof(struct in_addr))) {
    155  1.1  scottr 			/* Found it! */
    156  1.1  scottr 			if (debug_level > 3)
    157  1.1  scottr 				syslog(LOG_DEBUG, "Found CLIENT* in cache");
    158  1.1  scottr 			return (client);
    159  1.1  scottr 		}
    160  1.1  scottr 	}
    161  1.1  scottr 
    162  1.1  scottr 	/* Not found in cache.  Free the next entry if it is in use. */
    163  1.1  scottr 	if (clnt_cache_ptr[clnt_cache_next_to_use]) {
    164  1.1  scottr 		clnt_destroy(clnt_cache_ptr[clnt_cache_next_to_use]);
    165  1.1  scottr 		clnt_cache_ptr[clnt_cache_next_to_use] = NULL;
    166  1.1  scottr 	}
    167  1.1  scottr 
    168  1.1  scottr 	/* Create the new client handle */
    169  1.1  scottr 	sock_no = RPC_ANYSOCK;
    170  1.1  scottr 	retry_time.tv_sec = 5;
    171  1.1  scottr 	retry_time.tv_usec = 0;
    172  1.1  scottr 	host_addr->sin_port = 0; /* Force consultation with portmapper */
    173  1.4  bouyer #if 1
    174  1.4  bouyer 	client = clntudp_create(host_addr, NLM_PROG, vers,
    175  1.1  scottr 	    retry_time, &sock_no);
    176  1.4  bouyer #else
    177  1.4  bouyer 	client = clnttcp_create(host_addr, NLM_PROG, vers,
    178  1.4  bouyer 	    &sock_no, 0, 0);
    179  1.4  bouyer #endif
    180  1.1  scottr 	if (!client) {
    181  1.1  scottr 		syslog(LOG_ERR, clnt_spcreateerror("clntudp_create"));
    182  1.1  scottr 		syslog(LOG_ERR, "Unable to return result to %s",
    183  1.1  scottr 		    inet_ntoa(host_addr->sin_addr));
    184  1.1  scottr 		return NULL;
    185  1.1  scottr 	}
    186  1.1  scottr 
    187  1.1  scottr 	/* Success - update the cache entry */
    188  1.1  scottr 	clnt_cache_ptr[clnt_cache_next_to_use] = client;
    189  1.1  scottr 	clnt_cache_addr[clnt_cache_next_to_use] = host_addr->sin_addr;
    190  1.1  scottr 	clnt_cache_time[clnt_cache_next_to_use] = time_now.tv_sec;
    191  1.1  scottr 	if (++clnt_cache_next_to_use > CLIENT_CACHE_SIZE)
    192  1.1  scottr 		clnt_cache_next_to_use = 0;
    193  1.1  scottr 
    194  1.1  scottr 	/*
    195  1.1  scottr 	 * Disable the default timeout, so we can specify our own in calls
    196  1.1  scottr 	 * to clnt_call().  (Note that the timeout is a different concept
    197  1.1  scottr 	 * from the retry period set in clnt_udp_create() above.)
    198  1.1  scottr 	 */
    199  1.1  scottr 	retry_time.tv_sec = -1;
    200  1.1  scottr 	retry_time.tv_usec = -1;
    201  1.1  scottr 	clnt_control(client, CLSET_TIMEOUT, (char *)&retry_time);
    202  1.1  scottr 
    203  1.1  scottr 	if (debug_level > 3)
    204  1.1  scottr 		syslog(LOG_DEBUG, "Created CLIENT* for %s",
    205  1.1  scottr 		    inet_ntoa(host_addr->sin_addr));
    206  1.1  scottr 	return client;
    207  1.1  scottr }
    208  1.1  scottr 
    209  1.1  scottr 
    210  1.1  scottr /* transmit_result --------------------------------------------------------- */
    211  1.1  scottr /*
    212  1.1  scottr  * Purpose:	Transmit result for nlm_xxx_msg pseudo-RPCs
    213  1.1  scottr  * Returns:	Nothing - we have no idea if the datagram got there
    214  1.1  scottr  * Notes:	clnt_call() will always fail (with timeout) as we are
    215  1.1  scottr  *		calling it with timeout 0 as a hack to just issue a datagram
    216  1.1  scottr  *		without expecting a result
    217  1.1  scottr  */
    218  1.4  bouyer void
    219  1.4  bouyer transmit_result(opcode, result, addr)
    220  1.1  scottr 	int opcode;
    221  1.1  scottr 	nlm_res *result;
    222  1.4  bouyer 	struct sockaddr *addr;
    223  1.1  scottr {
    224  1.1  scottr 	static char dummy;
    225  1.1  scottr 	CLIENT *cli;
    226  1.1  scottr 	struct timeval timeo;
    227  1.1  scottr 	int success;
    228  1.1  scottr 
    229  1.4  bouyer 	if ((cli = get_client((struct sockaddr_in *)addr, NLM_VERS)) != NULL) {
    230  1.1  scottr 		timeo.tv_sec = 0; /* No timeout - not expecting response */
    231  1.1  scottr 		timeo.tv_usec = 0;
    232  1.1  scottr 
    233  1.1  scottr 		success = clnt_call(cli, opcode, xdr_nlm_res, result, xdr_void,
    234  1.1  scottr 		    &dummy, timeo);
    235  1.1  scottr 
    236  1.1  scottr 		if (debug_level > 2)
    237  1.4  bouyer 			syslog(LOG_DEBUG, "clnt_call returns %d(%s)",
    238  1.4  bouyer 			    success, clnt_sperrno(success));
    239  1.1  scottr 	}
    240  1.1  scottr }
    241  1.3  bouyer /* transmit4_result --------------------------------------------------------- */
    242  1.3  bouyer /*
    243  1.3  bouyer  * Purpose:	Transmit result for nlm4_xxx_msg pseudo-RPCs
    244  1.3  bouyer  * Returns:	Nothing - we have no idea if the datagram got there
    245  1.3  bouyer  * Notes:	clnt_call() will always fail (with timeout) as we are
    246  1.3  bouyer  *		calling it with timeout 0 as a hack to just issue a datagram
    247  1.3  bouyer  *		without expecting a result
    248  1.3  bouyer  */
    249  1.4  bouyer void
    250  1.4  bouyer transmit4_result(opcode, result, addr)
    251  1.3  bouyer 	int opcode;
    252  1.3  bouyer 	nlm4_res *result;
    253  1.4  bouyer 	struct sockaddr *addr;
    254  1.3  bouyer {
    255  1.3  bouyer 	static char dummy;
    256  1.3  bouyer 	CLIENT *cli;
    257  1.3  bouyer 	struct timeval timeo;
    258  1.3  bouyer 	int success;
    259  1.3  bouyer 
    260  1.4  bouyer 	if ((cli = get_client((struct sockaddr_in *)addr, NLM_VERS4)) != NULL) {
    261  1.3  bouyer 		timeo.tv_sec = 0; /* No timeout - not expecting response */
    262  1.3  bouyer 		timeo.tv_usec = 0;
    263  1.3  bouyer 
    264  1.3  bouyer 		success = clnt_call(cli, opcode, xdr_nlm4_res, result, xdr_void,
    265  1.3  bouyer 		    &dummy, timeo);
    266  1.3  bouyer 
    267  1.3  bouyer 		if (debug_level > 2)
    268  1.4  bouyer 			syslog(LOG_DEBUG, "clnt_call returns %d(%s)",
    269  1.4  bouyer 			    success, clnt_sperrno(success));
    270  1.3  bouyer 	}
    271  1.3  bouyer }
    272  1.4  bouyer 
    273  1.4  bouyer /*
    274  1.4  bouyer  * converts a struct nlm_lock to struct nlm4_lock
    275  1.4  bouyer  */
    276  1.4  bouyer static void nlmtonlm4 __P((struct nlm_lock *, struct nlm4_lock *));
    277  1.4  bouyer static void
    278  1.4  bouyer nlmtonlm4(arg, arg4)
    279  1.4  bouyer 	struct nlm_lock *arg;
    280  1.4  bouyer 	struct nlm4_lock *arg4;
    281  1.4  bouyer {
    282  1.4  bouyer 	memcpy(arg4, arg, sizeof(nlm_lock));
    283  1.4  bouyer 	arg4->l_offset = arg->l_offset;
    284  1.4  bouyer 	arg4->l_len = arg->l_len;
    285  1.4  bouyer }
    286  1.1  scottr /* ------------------------------------------------------------------------- */
    287  1.1  scottr /*
    288  1.1  scottr  * Functions for Unix<->Unix locking (ie. monitored locking, with rpc.statd
    289  1.1  scottr  * involved to ensure reclaim of locks after a crash of the "stateless"
    290  1.1  scottr  * server.
    291  1.1  scottr  *
    292  1.1  scottr  * These all come in two flavours - nlm_xxx() and nlm_xxx_msg().
    293  1.1  scottr  * The first are standard RPCs with argument and result.
    294  1.1  scottr  * The nlm_xxx_msg() calls implement exactly the same functions, but
    295  1.1  scottr  * use two pseudo-RPCs (one in each direction).  These calls are NOT
    296  1.1  scottr  * standard use of the RPC protocol in that they do not return a result
    297  1.1  scottr  * at all (NB. this is quite different from returning a void result).
    298  1.1  scottr  * The effect of this is to make the nlm_xxx_msg() calls simple unacknowledged
    299  1.1  scottr  * datagrams, requiring higher-level code to perform retries.
    300  1.1  scottr  *
    301  1.1  scottr  * Despite the disadvantages of the nlm_xxx_msg() approach (some of which
    302  1.1  scottr  * are documented in the comments to get_client() above), this is the
    303  1.1  scottr  * interface used by all current commercial NFS implementations
    304  1.1  scottr  * [Solaris, SCO, AIX etc.].  This is presumed to be because these allow
    305  1.1  scottr  * implementations to continue using the standard RPC libraries, while
    306  1.1  scottr  * avoiding the block-until-result nature of the library interface.
    307  1.1  scottr  *
    308  1.1  scottr  * No client implementations have been identified so far that make use
    309  1.1  scottr  * of the true RPC version (early SunOS releases would be a likely candidate
    310  1.1  scottr  * for testing).
    311  1.1  scottr  */
    312  1.1  scottr 
    313  1.1  scottr /* nlm_test ---------------------------------------------------------------- */
    314  1.1  scottr /*
    315  1.1  scottr  * Purpose:	Test whether a specified lock would be granted if requested
    316  1.1  scottr  * Returns:	nlm_granted (or error code)
    317  1.1  scottr  * Notes:
    318  1.1  scottr  */
    319  1.1  scottr nlm_testres *
    320  1.1  scottr nlm_test_1_svc(arg, rqstp)
    321  1.1  scottr 	nlm_testargs *arg;
    322  1.1  scottr 	struct svc_req *rqstp;
    323  1.1  scottr {
    324  1.1  scottr 	static nlm_testres res;
    325  1.4  bouyer 	struct nlm4_lock arg4;
    326  1.4  bouyer 	struct nlm4_holder *holder;
    327  1.4  bouyer 	nlmtonlm4(&arg->alock, &arg4);
    328  1.1  scottr 
    329  1.1  scottr 	if (debug_level)
    330  1.1  scottr 		log_from_addr("nlm_test", rqstp);
    331  1.1  scottr 
    332  1.4  bouyer 	holder = testlock(&arg4, 0);
    333  1.1  scottr 	/*
    334  1.1  scottr 	 * Copy the cookie from the argument into the result.  Note that this
    335  1.1  scottr 	 * is slightly hazardous, as the structure contains a pointer to a
    336  1.1  scottr 	 * malloc()ed buffer that will get freed by the caller.  However, the
    337  1.1  scottr 	 * main function transmits the result before freeing the argument
    338  1.1  scottr 	 * so it is in fact safe.
    339  1.1  scottr 	 */
    340  1.1  scottr 	res.cookie = arg->cookie;
    341  1.4  bouyer 	if (holder == NULL) {
    342  1.4  bouyer 		res.stat.stat = nlm_granted;
    343  1.4  bouyer 	} else {
    344  1.4  bouyer 		res.stat.stat = nlm_denied;
    345  1.4  bouyer 		memcpy(&res.stat.nlm_testrply_u.holder, holder,
    346  1.4  bouyer 		    sizeof(struct nlm_holder));
    347  1.4  bouyer 		res.stat.nlm_testrply_u.holder.l_offset = holder->l_offset;
    348  1.4  bouyer 		res.stat.nlm_testrply_u.holder.l_len = holder->l_len;
    349  1.4  bouyer 	}
    350  1.1  scottr 	return (&res);
    351  1.1  scottr }
    352  1.1  scottr 
    353  1.1  scottr void *
    354  1.1  scottr nlm_test_msg_1_svc(arg, rqstp)
    355  1.1  scottr 	nlm_testargs *arg;
    356  1.1  scottr 	struct svc_req *rqstp;
    357  1.1  scottr {
    358  1.1  scottr 	nlm_testres res;
    359  1.1  scottr 	static char dummy;
    360  1.1  scottr 	struct sockaddr_in *addr;
    361  1.1  scottr 	CLIENT *cli;
    362  1.1  scottr 	int success;
    363  1.1  scottr 	struct timeval timeo;
    364  1.4  bouyer 	struct nlm4_lock arg4;
    365  1.4  bouyer 	struct nlm4_holder *holder;
    366  1.4  bouyer 
    367  1.4  bouyer 	nlmtonlm4(&arg->alock, &arg4);
    368  1.1  scottr 
    369  1.1  scottr 	if (debug_level)
    370  1.1  scottr 		log_from_addr("nlm_test_msg", rqstp);
    371  1.1  scottr 
    372  1.4  bouyer 	holder = testlock(&arg4, 0);
    373  1.4  bouyer 
    374  1.1  scottr 	res.cookie = arg->cookie;
    375  1.4  bouyer 	if (holder == NULL) {
    376  1.4  bouyer 		res.stat.stat = nlm_granted;
    377  1.4  bouyer 	} else {
    378  1.4  bouyer 		res.stat.stat = nlm_denied;
    379  1.4  bouyer 		memcpy(&res.stat.nlm_testrply_u.holder, holder,
    380  1.4  bouyer 		    sizeof(struct nlm_holder));
    381  1.4  bouyer 		res.stat.nlm_testrply_u.holder.l_offset = holder->l_offset;
    382  1.4  bouyer 		res.stat.nlm_testrply_u.holder.l_len = holder->l_len;
    383  1.4  bouyer 	}
    384  1.1  scottr 
    385  1.1  scottr 	/*
    386  1.1  scottr 	 * nlm_test has different result type to the other operations, so
    387  1.1  scottr 	 * can't use transmit_result() in this case
    388  1.1  scottr 	 */
    389  1.1  scottr 	addr = svc_getcaller(rqstp->rq_xprt);
    390  1.4  bouyer 	if ((cli = get_client(addr, NLM_VERS)) != NULL) {
    391  1.1  scottr 		timeo.tv_sec = 0; /* No timeout - not expecting response */
    392  1.1  scottr 		timeo.tv_usec = 0;
    393  1.1  scottr 
    394  1.1  scottr 		success = clnt_call(cli, NLM_TEST_RES, xdr_nlm_testres,
    395  1.1  scottr 		    &res, xdr_void, &dummy, timeo);
    396  1.1  scottr 
    397  1.1  scottr 		if (debug_level > 2)
    398  1.4  bouyer 			syslog(LOG_DEBUG, "clnt_call returns %d", success);
    399  1.1  scottr 	}
    400  1.1  scottr 	return (NULL);
    401  1.1  scottr }
    402  1.1  scottr 
    403  1.1  scottr /* nlm_lock ---------------------------------------------------------------- */
    404  1.1  scottr /*
    405  1.1  scottr  * Purposes:	Establish a lock
    406  1.1  scottr  * Returns:	granted, denied or blocked
    407  1.1  scottr  * Notes:	*** grace period support missing
    408  1.1  scottr  */
    409  1.1  scottr nlm_res *
    410  1.1  scottr nlm_lock_1_svc(arg, rqstp)
    411  1.1  scottr 	nlm_lockargs *arg;
    412  1.1  scottr 	struct svc_req *rqstp;
    413  1.1  scottr {
    414  1.1  scottr 	static nlm_res res;
    415  1.4  bouyer 	struct nlm4_lockargs arg4;
    416  1.4  bouyer 	nlmtonlm4(&arg->alock, &arg4.alock);
    417  1.4  bouyer 	arg4.cookie = arg->cookie;
    418  1.4  bouyer 	arg4.block = arg->block;
    419  1.4  bouyer 	arg4.exclusive = arg->exclusive;
    420  1.4  bouyer 	arg4.reclaim = arg->reclaim;
    421  1.4  bouyer 	arg4.state = arg->state;
    422  1.1  scottr 
    423  1.1  scottr 	if (debug_level)
    424  1.1  scottr 		log_from_addr("nlm_lock", rqstp);
    425  1.1  scottr 
    426  1.1  scottr 	/* copy cookie from arg to result.  See comment in nlm_test_1() */
    427  1.1  scottr 	res.cookie = arg->cookie;
    428  1.1  scottr 
    429  1.4  bouyer 	res.stat.stat = getlock(&arg4, rqstp, LOCK_MON);
    430  1.1  scottr 	return (&res);
    431  1.1  scottr }
    432  1.1  scottr 
    433  1.1  scottr void *
    434  1.1  scottr nlm_lock_msg_1_svc(arg, rqstp)
    435  1.1  scottr 	nlm_lockargs *arg;
    436  1.1  scottr 	struct svc_req *rqstp;
    437  1.1  scottr {
    438  1.1  scottr 	static nlm_res res;
    439  1.4  bouyer 	struct nlm4_lockargs arg4;
    440  1.4  bouyer 
    441  1.4  bouyer 	nlmtonlm4(&arg->alock, &arg4.alock);
    442  1.4  bouyer 	arg4.cookie = arg->cookie;
    443  1.4  bouyer 	arg4.block = arg->block;
    444  1.4  bouyer 	arg4.exclusive = arg->exclusive;
    445  1.4  bouyer 	arg4.reclaim = arg->reclaim;
    446  1.4  bouyer 	arg4.state = arg->state;
    447  1.1  scottr 
    448  1.1  scottr 	if (debug_level)
    449  1.1  scottr 		log_from_addr("nlm_lock_msg", rqstp);
    450  1.1  scottr 
    451  1.1  scottr 	res.cookie = arg->cookie;
    452  1.4  bouyer 	res.stat.stat = getlock(&arg4, rqstp, LOCK_ASYNC | LOCK_MON);
    453  1.4  bouyer 	transmit_result(NLM_LOCK_RES, &res,
    454  1.4  bouyer 	    (struct sockaddr *)svc_getcaller(rqstp->rq_xprt));
    455  1.1  scottr 
    456  1.1  scottr 	return (NULL);
    457  1.1  scottr }
    458  1.1  scottr 
    459  1.1  scottr /* nlm_cancel -------------------------------------------------------------- */
    460  1.1  scottr /*
    461  1.1  scottr  * Purpose:	Cancel a blocked lock request
    462  1.1  scottr  * Returns:	granted or denied
    463  1.1  scottr  * Notes:
    464  1.1  scottr  */
    465  1.1  scottr nlm_res *
    466  1.1  scottr nlm_cancel_1_svc(arg, rqstp)
    467  1.1  scottr 	nlm_cancargs *arg;
    468  1.1  scottr 	struct svc_req *rqstp;
    469  1.1  scottr {
    470  1.1  scottr 	static nlm_res res;
    471  1.4  bouyer 	struct nlm4_lock arg4;
    472  1.4  bouyer 
    473  1.4  bouyer 	nlmtonlm4(&arg->alock, &arg4);
    474  1.1  scottr 
    475  1.1  scottr 	if (debug_level)
    476  1.1  scottr 		log_from_addr("nlm_cancel", rqstp);
    477  1.1  scottr 
    478  1.1  scottr 	/* copy cookie from arg to result.  See comment in nlm_test_1() */
    479  1.1  scottr 	res.cookie = arg->cookie;
    480  1.1  scottr 
    481  1.1  scottr 	/*
    482  1.1  scottr 	 * Since at present we never return 'nlm_blocked', there can never be
    483  1.1  scottr 	 * a lock to cancel, so this call always fails.
    484  1.1  scottr 	 */
    485  1.4  bouyer 	res.stat.stat = unlock(&arg4, LOCK_CANCEL);
    486  1.1  scottr 	return (&res);
    487  1.1  scottr }
    488  1.1  scottr 
    489  1.1  scottr void *
    490  1.1  scottr nlm_cancel_msg_1_svc(arg, rqstp)
    491  1.1  scottr 	nlm_cancargs *arg;
    492  1.1  scottr 	struct svc_req *rqstp;
    493  1.1  scottr {
    494  1.1  scottr 	static nlm_res res;
    495  1.4  bouyer 	struct nlm4_lock arg4;
    496  1.4  bouyer 
    497  1.4  bouyer 	nlmtonlm4(&arg->alock, &arg4);
    498  1.1  scottr 
    499  1.1  scottr 	if (debug_level)
    500  1.1  scottr 		log_from_addr("nlm_cancel_msg", rqstp);
    501  1.1  scottr 
    502  1.1  scottr 	res.cookie = arg->cookie;
    503  1.1  scottr 	/*
    504  1.1  scottr 	 * Since at present we never return 'nlm_blocked', there can never be
    505  1.1  scottr 	 * a lock to cancel, so this call always fails.
    506  1.1  scottr 	 */
    507  1.4  bouyer 	res.stat.stat = unlock(&arg4, LOCK_CANCEL);
    508  1.4  bouyer 	transmit_result(NLM_CANCEL_RES, &res,
    509  1.4  bouyer 	    (struct sockaddr *)svc_getcaller(rqstp->rq_xprt));
    510  1.1  scottr 	return (NULL);
    511  1.1  scottr }
    512  1.1  scottr 
    513  1.1  scottr /* nlm_unlock -------------------------------------------------------------- */
    514  1.1  scottr /*
    515  1.1  scottr  * Purpose:	Release an existing lock
    516  1.1  scottr  * Returns:	Always granted, unless during grace period
    517  1.1  scottr  * Notes:	"no such lock" error condition is ignored, as the
    518  1.1  scottr  *		protocol uses unreliable UDP datagrams, and may well
    519  1.1  scottr  *		re-try an unlock that has already succeeded.
    520  1.1  scottr  */
    521  1.1  scottr nlm_res *
    522  1.1  scottr nlm_unlock_1_svc(arg, rqstp)
    523  1.1  scottr 	nlm_unlockargs *arg;
    524  1.1  scottr 	struct svc_req *rqstp;
    525  1.1  scottr {
    526  1.1  scottr 	static nlm_res res;
    527  1.4  bouyer 	struct nlm4_lock arg4;
    528  1.4  bouyer 
    529  1.4  bouyer 	nlmtonlm4(&arg->alock, &arg4);
    530  1.1  scottr 
    531  1.1  scottr 	if (debug_level)
    532  1.1  scottr 		log_from_addr("nlm_unlock", rqstp);
    533  1.1  scottr 
    534  1.4  bouyer 	res.stat.stat = unlock(&arg4, 0);
    535  1.1  scottr 	res.cookie = arg->cookie;
    536  1.1  scottr 
    537  1.1  scottr 	return (&res);
    538  1.1  scottr }
    539  1.1  scottr 
    540  1.1  scottr void *
    541  1.1  scottr nlm_unlock_msg_1_svc(arg, rqstp)
    542  1.1  scottr 	nlm_unlockargs *arg;
    543  1.1  scottr 	struct svc_req *rqstp;
    544  1.1  scottr {
    545  1.1  scottr 	static nlm_res res;
    546  1.4  bouyer 	struct nlm4_lock arg4;
    547  1.4  bouyer 
    548  1.4  bouyer 	nlmtonlm4(&arg->alock, &arg4);
    549  1.1  scottr 
    550  1.1  scottr 	if (debug_level)
    551  1.1  scottr 		log_from_addr("nlm_unlock_msg", rqstp);
    552  1.1  scottr 
    553  1.4  bouyer 	res.stat.stat = unlock(&arg4, 0);
    554  1.1  scottr 	res.cookie = arg->cookie;
    555  1.1  scottr 
    556  1.4  bouyer 	transmit_result(NLM_UNLOCK_RES, &res,
    557  1.4  bouyer 	    (struct sockaddr *)svc_getcaller(rqstp->rq_xprt));
    558  1.1  scottr 	return (NULL);
    559  1.1  scottr }
    560  1.1  scottr 
    561  1.1  scottr /* ------------------------------------------------------------------------- */
    562  1.1  scottr /*
    563  1.1  scottr  * Client-side pseudo-RPCs for results.  Note that for the client there
    564  1.1  scottr  * are only nlm_xxx_msg() versions of each call, since the 'real RPC'
    565  1.1  scottr  * version returns the results in the RPC result, and so the client
    566  1.1  scottr  * does not normally receive incoming RPCs.
    567  1.1  scottr  *
    568  1.1  scottr  * The exception to this is nlm_granted(), which is genuinely an RPC
    569  1.1  scottr  * call from the server to the client - a 'call-back' in normal procedure
    570  1.1  scottr  * call terms.
    571  1.1  scottr  */
    572  1.1  scottr 
    573  1.1  scottr /* nlm_granted ------------------------------------------------------------- */
    574  1.1  scottr /*
    575  1.1  scottr  * Purpose:	Receive notification that formerly blocked lock now granted
    576  1.1  scottr  * Returns:	always success ('granted')
    577  1.1  scottr  * Notes:
    578  1.1  scottr  */
    579  1.1  scottr nlm_res *
    580  1.1  scottr nlm_granted_1_svc(arg, rqstp)
    581  1.1  scottr 	nlm_testargs *arg;
    582  1.1  scottr 	struct svc_req *rqstp;
    583  1.1  scottr {
    584  1.1  scottr 	static nlm_res res;
    585  1.1  scottr 
    586  1.1  scottr 	if (debug_level)
    587  1.1  scottr 		log_from_addr("nlm_granted", rqstp);
    588  1.1  scottr 
    589  1.1  scottr 	/* copy cookie from arg to result.  See comment in nlm_test_1() */
    590  1.1  scottr 	res.cookie = arg->cookie;
    591  1.1  scottr 
    592  1.1  scottr 	res.stat.stat = nlm_granted;
    593  1.1  scottr 	return (&res);
    594  1.1  scottr }
    595  1.1  scottr 
    596  1.1  scottr void *
    597  1.1  scottr nlm_granted_msg_1_svc(arg, rqstp)
    598  1.1  scottr 	nlm_testargs *arg;
    599  1.1  scottr 	struct svc_req *rqstp;
    600  1.1  scottr {
    601  1.1  scottr 	static nlm_res res;
    602  1.1  scottr 
    603  1.1  scottr 	if (debug_level)
    604  1.1  scottr 		log_from_addr("nlm_granted_msg", rqstp);
    605  1.1  scottr 
    606  1.1  scottr 	res.cookie = arg->cookie;
    607  1.1  scottr 	res.stat.stat = nlm_granted;
    608  1.4  bouyer 	transmit_result(NLM_GRANTED_RES, &res,
    609  1.4  bouyer 	    (struct sockaddr *)svc_getcaller(rqstp->rq_xprt));
    610  1.1  scottr 	return (NULL);
    611  1.1  scottr }
    612  1.1  scottr 
    613  1.1  scottr /* nlm_test_res ------------------------------------------------------------ */
    614  1.1  scottr /*
    615  1.1  scottr  * Purpose:	Accept result from earlier nlm_test_msg() call
    616  1.1  scottr  * Returns:	Nothing
    617  1.1  scottr  */
    618  1.1  scottr void *
    619  1.1  scottr nlm_test_res_1_svc(arg, rqstp)
    620  1.1  scottr 	nlm_testres *arg;
    621  1.1  scottr 	struct svc_req *rqstp;
    622  1.1  scottr {
    623  1.1  scottr 	if (debug_level)
    624  1.1  scottr 		log_from_addr("nlm_test_res", rqstp);
    625  1.1  scottr 	return (NULL);
    626  1.1  scottr }
    627  1.1  scottr 
    628  1.1  scottr /* nlm_lock_res ------------------------------------------------------------ */
    629  1.1  scottr /*
    630  1.1  scottr  * Purpose:	Accept result from earlier nlm_lock_msg() call
    631  1.1  scottr  * Returns:	Nothing
    632  1.1  scottr  */
    633  1.1  scottr void *
    634  1.1  scottr nlm_lock_res_1_svc(arg, rqstp)
    635  1.1  scottr 	nlm_res *arg;
    636  1.1  scottr 	struct svc_req *rqstp;
    637  1.1  scottr {
    638  1.1  scottr 	if (debug_level)
    639  1.1  scottr 		log_from_addr("nlm_lock_res", rqstp);
    640  1.1  scottr 
    641  1.1  scottr 	return (NULL);
    642  1.1  scottr }
    643  1.1  scottr 
    644  1.1  scottr /* nlm_cancel_res ---------------------------------------------------------- */
    645  1.1  scottr /*
    646  1.1  scottr  * Purpose:	Accept result from earlier nlm_cancel_msg() call
    647  1.1  scottr  * Returns:	Nothing
    648  1.1  scottr  */
    649  1.1  scottr void *
    650  1.1  scottr nlm_cancel_res_1_svc(arg, rqstp)
    651  1.1  scottr 	nlm_res *arg;
    652  1.1  scottr 	struct svc_req *rqstp;
    653  1.1  scottr {
    654  1.1  scottr 	if (debug_level)
    655  1.1  scottr 		log_from_addr("nlm_cancel_res", rqstp);
    656  1.1  scottr 	return (NULL);
    657  1.1  scottr }
    658  1.1  scottr 
    659  1.1  scottr /* nlm_unlock_res ---------------------------------------------------------- */
    660  1.1  scottr /*
    661  1.1  scottr  * Purpose:	Accept result from earlier nlm_unlock_msg() call
    662  1.1  scottr  * Returns:	Nothing
    663  1.1  scottr  */
    664  1.1  scottr void *
    665  1.1  scottr nlm_unlock_res_1_svc(arg, rqstp)
    666  1.1  scottr 	nlm_res *arg;
    667  1.1  scottr 	struct svc_req *rqstp;
    668  1.1  scottr {
    669  1.1  scottr 	if (debug_level)
    670  1.1  scottr 		log_from_addr("nlm_unlock_res", rqstp);
    671  1.1  scottr 	return (NULL);
    672  1.1  scottr }
    673  1.1  scottr 
    674  1.1  scottr /* nlm_granted_res --------------------------------------------------------- */
    675  1.1  scottr /*
    676  1.1  scottr  * Purpose:	Accept result from earlier nlm_granted_msg() call
    677  1.1  scottr  * Returns:	Nothing
    678  1.1  scottr  */
    679  1.1  scottr void *
    680  1.1  scottr nlm_granted_res_1_svc(arg, rqstp)
    681  1.1  scottr 	nlm_res *arg;
    682  1.1  scottr 	struct svc_req *rqstp;
    683  1.1  scottr {
    684  1.1  scottr 	if (debug_level)
    685  1.1  scottr 		log_from_addr("nlm_granted_res", rqstp);
    686  1.1  scottr 	return (NULL);
    687  1.1  scottr }
    688  1.1  scottr 
    689  1.1  scottr /* ------------------------------------------------------------------------- */
    690  1.1  scottr /*
    691  1.1  scottr  * Calls for PCNFS locking (aka non-monitored locking, no involvement
    692  1.1  scottr  * of rpc.statd).
    693  1.1  scottr  *
    694  1.1  scottr  * These are all genuine RPCs - no nlm_xxx_msg() nonsense here.
    695  1.1  scottr  */
    696  1.1  scottr 
    697  1.1  scottr /* nlm_share --------------------------------------------------------------- */
    698  1.1  scottr /*
    699  1.1  scottr  * Purpose:	Establish a DOS-style lock
    700  1.1  scottr  * Returns:	success or failure
    701  1.1  scottr  * Notes:	Blocking locks are not supported - client is expected
    702  1.1  scottr  *		to retry if required.
    703  1.1  scottr  */
    704  1.1  scottr nlm_shareres *
    705  1.1  scottr nlm_share_3_svc(arg, rqstp)
    706  1.1  scottr 	nlm_shareargs *arg;
    707  1.1  scottr 	struct svc_req *rqstp;
    708  1.1  scottr {
    709  1.1  scottr 	static nlm_shareres res;
    710  1.1  scottr 
    711  1.1  scottr 	if (debug_level)
    712  1.1  scottr 		log_from_addr("nlm_share", rqstp);
    713  1.1  scottr 
    714  1.1  scottr 	res.cookie = arg->cookie;
    715  1.1  scottr 	res.stat = nlm_granted;
    716  1.1  scottr 	res.sequence = 1234356;	/* X/Open says this field is ignored? */
    717  1.1  scottr 	return (&res);
    718  1.1  scottr }
    719  1.1  scottr 
    720  1.1  scottr /* nlm_unshare ------------------------------------------------------------ */
    721  1.1  scottr /*
    722  1.1  scottr  * Purpose:	Release a DOS-style lock
    723  1.1  scottr  * Returns:	nlm_granted, unless in grace period
    724  1.1  scottr  * Notes:
    725  1.1  scottr  */
    726  1.1  scottr nlm_shareres *
    727  1.1  scottr nlm_unshare_3_svc(arg, rqstp)
    728  1.1  scottr 	nlm_shareargs *arg;
    729  1.1  scottr 	struct svc_req *rqstp;
    730  1.1  scottr {
    731  1.1  scottr 	static nlm_shareres res;
    732  1.1  scottr 
    733  1.1  scottr 	if (debug_level)
    734  1.1  scottr 		log_from_addr("nlm_unshare", rqstp);
    735  1.1  scottr 
    736  1.1  scottr 	res.cookie = arg->cookie;
    737  1.1  scottr 	res.stat = nlm_granted;
    738  1.1  scottr 	res.sequence = 1234356;	/* X/Open says this field is ignored? */
    739  1.1  scottr 	return (&res);
    740  1.1  scottr }
    741  1.1  scottr 
    742  1.1  scottr /* nlm_nm_lock ------------------------------------------------------------ */
    743  1.1  scottr /*
    744  1.1  scottr  * Purpose:	non-monitored version of nlm_lock()
    745  1.1  scottr  * Returns:	as for nlm_lock()
    746  1.1  scottr  * Notes:	These locks are in the same style as the standard nlm_lock,
    747  1.1  scottr  *		but the rpc.statd should not be called to establish a
    748  1.1  scottr  *		monitor for the client machine, since that machine is
    749  1.1  scottr  *		declared not to be running a rpc.statd, and so would not
    750  1.1  scottr  *		respond to the statd protocol.
    751  1.1  scottr  */
    752  1.1  scottr nlm_res *
    753  1.1  scottr nlm_nm_lock_3_svc(arg, rqstp)
    754  1.1  scottr 	nlm_lockargs *arg;
    755  1.1  scottr 	struct svc_req *rqstp;
    756  1.1  scottr {
    757  1.1  scottr 	static nlm_res res;
    758  1.1  scottr 
    759  1.1  scottr 	if (debug_level)
    760  1.1  scottr 		log_from_addr("nlm_nm_lock", rqstp);
    761  1.1  scottr 
    762  1.1  scottr 	/* copy cookie from arg to result.  See comment in nlm_test_1() */
    763  1.1  scottr 	res.cookie = arg->cookie;
    764  1.1  scottr 	res.stat.stat = nlm_granted;
    765  1.1  scottr 	return (&res);
    766  1.1  scottr }
    767  1.1  scottr 
    768  1.1  scottr /* nlm_free_all ------------------------------------------------------------ */
    769  1.1  scottr /*
    770  1.1  scottr  * Purpose:	Release all locks held by a named client
    771  1.1  scottr  * Returns:	Nothing
    772  1.1  scottr  * Notes:	Potential denial of service security problem here - the
    773  1.1  scottr  *		locks to be released are specified by a host name, independent
    774  1.1  scottr  *		of the address from which the request has arrived.
    775  1.1  scottr  *		Should probably be rejected if the named host has been
    776  1.1  scottr  *		using monitored locks.
    777  1.1  scottr  */
    778  1.1  scottr void *
    779  1.1  scottr nlm_free_all_3_svc(arg, rqstp)
    780  1.1  scottr 	nlm_notify *arg;
    781  1.1  scottr 	struct svc_req *rqstp;
    782  1.1  scottr {
    783  1.1  scottr 	static char dummy;
    784  1.1  scottr 
    785  1.1  scottr 	if (debug_level)
    786  1.1  scottr 		log_from_addr("nlm_free_all", rqstp);
    787  1.1  scottr 	return (&dummy);
    788  1.1  scottr }
    789  1.3  bouyer 
    790  1.3  bouyer /* calls for nlm version 4 (NFSv3) */
    791  1.3  bouyer /* nlm_test ---------------------------------------------------------------- */
    792  1.3  bouyer /*
    793  1.3  bouyer  * Purpose:	Test whether a specified lock would be granted if requested
    794  1.3  bouyer  * Returns:	nlm_granted (or error code)
    795  1.3  bouyer  * Notes:
    796  1.3  bouyer  */
    797  1.3  bouyer nlm4_testres *
    798  1.3  bouyer nlm4_test_4_svc(arg, rqstp)
    799  1.3  bouyer 	nlm4_testargs *arg;
    800  1.3  bouyer 	struct svc_req *rqstp;
    801  1.3  bouyer {
    802  1.3  bouyer 	static nlm4_testres res;
    803  1.4  bouyer 	struct nlm4_holder *holder;
    804  1.3  bouyer 
    805  1.3  bouyer 	if (debug_level)
    806  1.3  bouyer 		log_from_addr("nlm4_test", rqstp);
    807  1.3  bouyer 
    808  1.4  bouyer 	holder = testlock(&arg->alock, LOCK_V4);
    809  1.4  bouyer 
    810  1.3  bouyer 	/*
    811  1.3  bouyer 	 * Copy the cookie from the argument into the result.  Note that this
    812  1.3  bouyer 	 * is slightly hazardous, as the structure contains a pointer to a
    813  1.3  bouyer 	 * malloc()ed buffer that will get freed by the caller.  However, the
    814  1.3  bouyer 	 * main function transmits the result before freeing the argument
    815  1.3  bouyer 	 * so it is in fact safe.
    816  1.3  bouyer 	 */
    817  1.3  bouyer 	res.cookie = arg->cookie;
    818  1.4  bouyer 	if (holder == NULL) {
    819  1.4  bouyer 		res.stat.stat = nlm4_granted;
    820  1.4  bouyer 	} else {
    821  1.4  bouyer 		res.stat.stat = nlm4_denied;
    822  1.4  bouyer 		memcpy(&res.stat.nlm4_testrply_u.holder, holder,
    823  1.4  bouyer 		    sizeof(struct nlm4_holder));
    824  1.4  bouyer 	}
    825  1.3  bouyer 	return (&res);
    826  1.3  bouyer }
    827  1.3  bouyer 
    828  1.3  bouyer void *
    829  1.3  bouyer nlm4_test_msg_4_svc(arg, rqstp)
    830  1.3  bouyer 	nlm4_testargs *arg;
    831  1.3  bouyer 	struct svc_req *rqstp;
    832  1.3  bouyer {
    833  1.3  bouyer 	nlm4_testres res;
    834  1.3  bouyer 	static char dummy;
    835  1.3  bouyer 	struct sockaddr_in *addr;
    836  1.3  bouyer 	CLIENT *cli;
    837  1.3  bouyer 	int success;
    838  1.3  bouyer 	struct timeval timeo;
    839  1.4  bouyer 	struct nlm4_holder *holder;
    840  1.3  bouyer 
    841  1.3  bouyer 	if (debug_level)
    842  1.3  bouyer 		log_from_addr("nlm4_test_msg", rqstp);
    843  1.3  bouyer 
    844  1.4  bouyer 	holder = testlock(&arg->alock, LOCK_V4);
    845  1.4  bouyer 
    846  1.3  bouyer 	res.cookie = arg->cookie;
    847  1.4  bouyer 	if (holder == NULL) {
    848  1.4  bouyer 		res.stat.stat = nlm4_granted;
    849  1.4  bouyer 	} else {
    850  1.4  bouyer 		res.stat.stat = nlm4_denied;
    851  1.4  bouyer 		memcpy(&res.stat.nlm4_testrply_u.holder, holder,
    852  1.4  bouyer 		    sizeof(struct nlm4_holder));
    853  1.4  bouyer 	}
    854  1.3  bouyer 
    855  1.3  bouyer 	/*
    856  1.3  bouyer 	 * nlm_test has different result type to the other operations, so
    857  1.3  bouyer 	 * can't use transmit4_result() in this case
    858  1.3  bouyer 	 */
    859  1.3  bouyer 	addr = svc_getcaller(rqstp->rq_xprt);
    860  1.4  bouyer 	if ((cli = get_client(addr, NLM_VERS4)) != NULL) {
    861  1.3  bouyer 		timeo.tv_sec = 0; /* No timeout - not expecting response */
    862  1.3  bouyer 		timeo.tv_usec = 0;
    863  1.3  bouyer 
    864  1.3  bouyer 		success = clnt_call(cli, NLM4_TEST_RES, xdr_nlm4_testres,
    865  1.3  bouyer 		    &res, xdr_void, &dummy, timeo);
    866  1.3  bouyer 
    867  1.3  bouyer 		if (debug_level > 2)
    868  1.4  bouyer 			syslog(LOG_DEBUG, "clnt_call returns %d", success);
    869  1.3  bouyer 	}
    870  1.3  bouyer 	return (NULL);
    871  1.3  bouyer }
    872  1.3  bouyer 
    873  1.3  bouyer /* nlm_lock ---------------------------------------------------------------- */
    874  1.3  bouyer /*
    875  1.3  bouyer  * Purposes:	Establish a lock
    876  1.3  bouyer  * Returns:	granted, denied or blocked
    877  1.3  bouyer  * Notes:	*** grace period support missing
    878  1.3  bouyer  */
    879  1.3  bouyer nlm4_res *
    880  1.3  bouyer nlm4_lock_4_svc(arg, rqstp)
    881  1.3  bouyer 	nlm4_lockargs *arg;
    882  1.3  bouyer 	struct svc_req *rqstp;
    883  1.3  bouyer {
    884  1.3  bouyer 	static nlm4_res res;
    885  1.3  bouyer 
    886  1.3  bouyer 	if (debug_level)
    887  1.3  bouyer 		log_from_addr("nlm4_lock", rqstp);
    888  1.3  bouyer 
    889  1.3  bouyer 	/* copy cookie from arg to result.  See comment in nlm_test_4() */
    890  1.3  bouyer 	res.cookie = arg->cookie;
    891  1.3  bouyer 
    892  1.4  bouyer 	res.stat.stat = getlock(arg, rqstp, LOCK_MON | LOCK_V4);
    893  1.3  bouyer 	return (&res);
    894  1.3  bouyer }
    895  1.3  bouyer 
    896  1.3  bouyer void *
    897  1.3  bouyer nlm4_lock_msg_4_svc(arg, rqstp)
    898  1.3  bouyer 	nlm4_lockargs *arg;
    899  1.3  bouyer 	struct svc_req *rqstp;
    900  1.3  bouyer {
    901  1.3  bouyer 	static nlm4_res res;
    902  1.3  bouyer 
    903  1.3  bouyer 	if (debug_level)
    904  1.3  bouyer 		log_from_addr("nlm4_lock_msg", rqstp);
    905  1.3  bouyer 
    906  1.3  bouyer 	res.cookie = arg->cookie;
    907  1.4  bouyer 	res.stat.stat = getlock(arg, rqstp, LOCK_MON | LOCK_ASYNC | LOCK_V4);
    908  1.4  bouyer 	transmit4_result(NLM4_LOCK_RES, &res,
    909  1.4  bouyer 	    (struct sockaddr *)svc_getcaller(rqstp->rq_xprt));
    910  1.3  bouyer 
    911  1.3  bouyer 	return (NULL);
    912  1.3  bouyer }
    913  1.3  bouyer 
    914  1.3  bouyer /* nlm_cancel -------------------------------------------------------------- */
    915  1.3  bouyer /*
    916  1.3  bouyer  * Purpose:	Cancel a blocked lock request
    917  1.3  bouyer  * Returns:	granted or denied
    918  1.3  bouyer  * Notes:
    919  1.3  bouyer  */
    920  1.3  bouyer nlm4_res *
    921  1.3  bouyer nlm4_cancel_4_svc(arg, rqstp)
    922  1.3  bouyer 	nlm4_cancargs *arg;
    923  1.3  bouyer 	struct svc_req *rqstp;
    924  1.3  bouyer {
    925  1.3  bouyer 	static nlm4_res res;
    926  1.3  bouyer 
    927  1.3  bouyer 	if (debug_level)
    928  1.3  bouyer 		log_from_addr("nlm4_cancel", rqstp);
    929  1.3  bouyer 
    930  1.3  bouyer 	/* copy cookie from arg to result.  See comment in nlm_test_1() */
    931  1.3  bouyer 	res.cookie = arg->cookie;
    932  1.3  bouyer 
    933  1.3  bouyer 	/*
    934  1.3  bouyer 	 * Since at present we never return 'nlm_blocked', there can never be
    935  1.3  bouyer 	 * a lock to cancel, so this call always fails.
    936  1.3  bouyer 	 */
    937  1.4  bouyer 	res.stat.stat = unlock(&arg->alock, LOCK_CANCEL);
    938  1.3  bouyer 	return (&res);
    939  1.3  bouyer }
    940  1.3  bouyer 
    941  1.3  bouyer void *
    942  1.3  bouyer nlm4_cancel_msg_4_svc(arg, rqstp)
    943  1.3  bouyer 	nlm4_cancargs *arg;
    944  1.3  bouyer 	struct svc_req *rqstp;
    945  1.3  bouyer {
    946  1.3  bouyer 	static nlm4_res res;
    947  1.3  bouyer 
    948  1.3  bouyer 	if (debug_level)
    949  1.3  bouyer 		log_from_addr("nlm4_cancel_msg", rqstp);
    950  1.3  bouyer 
    951  1.3  bouyer 	res.cookie = arg->cookie;
    952  1.3  bouyer 	/*
    953  1.3  bouyer 	 * Since at present we never return 'nlm_blocked', there can never be
    954  1.3  bouyer 	 * a lock to cancel, so this call always fails.
    955  1.3  bouyer 	 */
    956  1.4  bouyer 	res.stat.stat = unlock(&arg->alock, LOCK_CANCEL | LOCK_V4);
    957  1.4  bouyer 	transmit4_result(NLM4_CANCEL_RES, &res,
    958  1.4  bouyer 	    (struct sockaddr *)svc_getcaller(rqstp->rq_xprt));
    959  1.3  bouyer 	return (NULL);
    960  1.3  bouyer }
    961  1.3  bouyer 
    962  1.3  bouyer /* nlm_unlock -------------------------------------------------------------- */
    963  1.3  bouyer /*
    964  1.3  bouyer  * Purpose:	Release an existing lock
    965  1.3  bouyer  * Returns:	Always granted, unless during grace period
    966  1.3  bouyer  * Notes:	"no such lock" error condition is ignored, as the
    967  1.3  bouyer  *		protocol uses unreliable UDP datagrams, and may well
    968  1.3  bouyer  *		re-try an unlock that has already succeeded.
    969  1.3  bouyer  */
    970  1.3  bouyer nlm4_res *
    971  1.3  bouyer nlm4_unlock_4_svc(arg, rqstp)
    972  1.3  bouyer 	nlm4_unlockargs *arg;
    973  1.3  bouyer 	struct svc_req *rqstp;
    974  1.3  bouyer {
    975  1.3  bouyer 	static nlm4_res res;
    976  1.3  bouyer 
    977  1.3  bouyer 	if (debug_level)
    978  1.3  bouyer 		log_from_addr("nlm4_unlock", rqstp);
    979  1.3  bouyer 
    980  1.4  bouyer 	res.stat.stat = unlock(&arg->alock, LOCK_V4);
    981  1.3  bouyer 	res.cookie = arg->cookie;
    982  1.3  bouyer 
    983  1.3  bouyer 	return (&res);
    984  1.3  bouyer }
    985  1.3  bouyer 
    986  1.3  bouyer void *
    987  1.3  bouyer nlm4_unlock_msg_4_svc(arg, rqstp)
    988  1.3  bouyer 	nlm4_unlockargs *arg;
    989  1.3  bouyer 	struct svc_req *rqstp;
    990  1.3  bouyer {
    991  1.3  bouyer 	static nlm4_res res;
    992  1.3  bouyer 
    993  1.3  bouyer 	if (debug_level)
    994  1.3  bouyer 		log_from_addr("nlm4_unlock_msg", rqstp);
    995  1.3  bouyer 
    996  1.4  bouyer 	res.stat.stat = unlock(&arg->alock, LOCK_V4);
    997  1.3  bouyer 	res.cookie = arg->cookie;
    998  1.3  bouyer 
    999  1.4  bouyer 	transmit4_result(NLM4_UNLOCK_RES, &res,
   1000  1.4  bouyer 	    (struct sockaddr *)svc_getcaller(rqstp->rq_xprt));
   1001  1.3  bouyer 	return (NULL);
   1002  1.3  bouyer }
   1003  1.3  bouyer 
   1004  1.3  bouyer /* ------------------------------------------------------------------------- */
   1005  1.3  bouyer /*
   1006  1.3  bouyer  * Client-side pseudo-RPCs for results.  Note that for the client there
   1007  1.3  bouyer  * are only nlm_xxx_msg() versions of each call, since the 'real RPC'
   1008  1.3  bouyer  * version returns the results in the RPC result, and so the client
   1009  1.3  bouyer  * does not normally receive incoming RPCs.
   1010  1.3  bouyer  *
   1011  1.3  bouyer  * The exception to this is nlm_granted(), which is genuinely an RPC
   1012  1.3  bouyer  * call from the server to the client - a 'call-back' in normal procedure
   1013  1.3  bouyer  * call terms.
   1014  1.3  bouyer  */
   1015  1.3  bouyer 
   1016  1.3  bouyer /* nlm_granted ------------------------------------------------------------- */
   1017  1.3  bouyer /*
   1018  1.3  bouyer  * Purpose:	Receive notification that formerly blocked lock now granted
   1019  1.3  bouyer  * Returns:	always success ('granted')
   1020  1.3  bouyer  * Notes:
   1021  1.3  bouyer  */
   1022  1.3  bouyer nlm4_res *
   1023  1.3  bouyer nlm4_granted_4_svc(arg, rqstp)
   1024  1.3  bouyer 	nlm4_testargs *arg;
   1025  1.3  bouyer 	struct svc_req *rqstp;
   1026  1.3  bouyer {
   1027  1.3  bouyer 	static nlm4_res res;
   1028  1.3  bouyer 
   1029  1.3  bouyer 	if (debug_level)
   1030  1.3  bouyer 		log_from_addr("nlm4_granted", rqstp);
   1031  1.3  bouyer 
   1032  1.3  bouyer 	/* copy cookie from arg to result.  See comment in nlm_test_1() */
   1033  1.3  bouyer 	res.cookie = arg->cookie;
   1034  1.3  bouyer 
   1035  1.3  bouyer 	res.stat.stat = nlm4_granted;
   1036  1.3  bouyer 	return (&res);
   1037  1.3  bouyer }
   1038  1.3  bouyer 
   1039  1.3  bouyer void *
   1040  1.3  bouyer nlm4_granted_msg_4_svc(arg, rqstp)
   1041  1.3  bouyer 	nlm4_testargs *arg;
   1042  1.3  bouyer 	struct svc_req *rqstp;
   1043  1.3  bouyer {
   1044  1.3  bouyer 	static nlm4_res res;
   1045  1.3  bouyer 
   1046  1.3  bouyer 	if (debug_level)
   1047  1.3  bouyer 		log_from_addr("nlm4_granted_msg", rqstp);
   1048  1.3  bouyer 
   1049  1.3  bouyer 	res.cookie = arg->cookie;
   1050  1.3  bouyer 	res.stat.stat = nlm4_granted;
   1051  1.4  bouyer 	transmit4_result(NLM4_GRANTED_RES, &res,
   1052  1.4  bouyer 	    (struct sockaddr *)svc_getcaller(rqstp->rq_xprt));
   1053  1.3  bouyer 	return (NULL);
   1054  1.3  bouyer }
   1055  1.3  bouyer 
   1056  1.3  bouyer /* nlm_test_res ------------------------------------------------------------ */
   1057  1.3  bouyer /*
   1058  1.3  bouyer  * Purpose:	Accept result from earlier nlm_test_msg() call
   1059  1.3  bouyer  * Returns:	Nothing
   1060  1.3  bouyer  */
   1061  1.3  bouyer void *
   1062  1.3  bouyer nlm4_test_res_4_svc(arg, rqstp)
   1063  1.3  bouyer 	nlm4_testres *arg;
   1064  1.3  bouyer 	struct svc_req *rqstp;
   1065  1.3  bouyer {
   1066  1.3  bouyer 	if (debug_level)
   1067  1.3  bouyer 		log_from_addr("nlm4_test_res", rqstp);
   1068  1.3  bouyer 	return (NULL);
   1069  1.3  bouyer }
   1070  1.3  bouyer 
   1071  1.3  bouyer /* nlm_lock_res ------------------------------------------------------------ */
   1072  1.3  bouyer /*
   1073  1.3  bouyer  * Purpose:	Accept result from earlier nlm_lock_msg() call
   1074  1.3  bouyer  * Returns:	Nothing
   1075  1.3  bouyer  */
   1076  1.3  bouyer void *
   1077  1.3  bouyer nlm4_lock_res_4_svc(arg, rqstp)
   1078  1.3  bouyer 	nlm4_res *arg;
   1079  1.3  bouyer 	struct svc_req *rqstp;
   1080  1.3  bouyer {
   1081  1.3  bouyer 	if (debug_level)
   1082  1.3  bouyer 		log_from_addr("nlm4_lock_res", rqstp);
   1083  1.3  bouyer 
   1084  1.3  bouyer 	return (NULL);
   1085  1.3  bouyer }
   1086  1.3  bouyer 
   1087  1.3  bouyer /* nlm_cancel_res ---------------------------------------------------------- */
   1088  1.3  bouyer /*
   1089  1.3  bouyer  * Purpose:	Accept result from earlier nlm_cancel_msg() call
   1090  1.3  bouyer  * Returns:	Nothing
   1091  1.3  bouyer  */
   1092  1.3  bouyer void *
   1093  1.3  bouyer nlm4_cancel_res_4_svc(arg, rqstp)
   1094  1.3  bouyer 	nlm4_res *arg;
   1095  1.3  bouyer 	struct svc_req *rqstp;
   1096  1.3  bouyer {
   1097  1.3  bouyer 	if (debug_level)
   1098  1.3  bouyer 		log_from_addr("nlm4_cancel_res", rqstp);
   1099  1.3  bouyer 	return (NULL);
   1100  1.3  bouyer }
   1101  1.3  bouyer 
   1102  1.3  bouyer /* nlm_unlock_res ---------------------------------------------------------- */
   1103  1.3  bouyer /*
   1104  1.3  bouyer  * Purpose:	Accept result from earlier nlm_unlock_msg() call
   1105  1.3  bouyer  * Returns:	Nothing
   1106  1.3  bouyer  */
   1107  1.3  bouyer void *
   1108  1.3  bouyer nlm4_unlock_res_4_svc(arg, rqstp)
   1109  1.3  bouyer 	nlm4_res *arg;
   1110  1.3  bouyer 	struct svc_req *rqstp;
   1111  1.3  bouyer {
   1112  1.3  bouyer 	if (debug_level)
   1113  1.3  bouyer 		log_from_addr("nlm4_unlock_res", rqstp);
   1114  1.3  bouyer 	return (NULL);
   1115  1.3  bouyer }
   1116  1.3  bouyer 
   1117  1.3  bouyer /* nlm_granted_res --------------------------------------------------------- */
   1118  1.3  bouyer /*
   1119  1.3  bouyer  * Purpose:	Accept result from earlier nlm_granted_msg() call
   1120  1.3  bouyer  * Returns:	Nothing
   1121  1.3  bouyer  */
   1122  1.3  bouyer void *
   1123  1.3  bouyer nlm4_granted_res_4_svc(arg, rqstp)
   1124  1.3  bouyer 	nlm4_res *arg;
   1125  1.3  bouyer 	struct svc_req *rqstp;
   1126  1.3  bouyer {
   1127  1.3  bouyer 	if (debug_level)
   1128  1.3  bouyer 		log_from_addr("nlm4_granted_res", rqstp);
   1129  1.3  bouyer 	return (NULL);
   1130  1.3  bouyer }
   1131  1.3  bouyer 
   1132  1.3  bouyer /* ------------------------------------------------------------------------- */
   1133  1.3  bouyer /*
   1134  1.3  bouyer  * Calls for PCNFS locking (aka non-monitored locking, no involvement
   1135  1.3  bouyer  * of rpc.statd).
   1136  1.3  bouyer  *
   1137  1.3  bouyer  * These are all genuine RPCs - no nlm_xxx_msg() nonsense here.
   1138  1.3  bouyer  */
   1139  1.3  bouyer 
   1140  1.3  bouyer /* nlm_share --------------------------------------------------------------- */
   1141  1.3  bouyer /*
   1142  1.3  bouyer  * Purpose:	Establish a DOS-style lock
   1143  1.3  bouyer  * Returns:	success or failure
   1144  1.3  bouyer  * Notes:	Blocking locks are not supported - client is expected
   1145  1.3  bouyer  *		to retry if required.
   1146  1.3  bouyer  */
   1147  1.3  bouyer nlm4_shareres *
   1148  1.3  bouyer nlm4_share_4_svc(arg, rqstp)
   1149  1.3  bouyer 	nlm4_shareargs *arg;
   1150  1.3  bouyer 	struct svc_req *rqstp;
   1151  1.3  bouyer {
   1152  1.3  bouyer 	static nlm4_shareres res;
   1153  1.3  bouyer 
   1154  1.3  bouyer 	if (debug_level)
   1155  1.3  bouyer 		log_from_addr("nlm4_share", rqstp);
   1156  1.3  bouyer 
   1157  1.3  bouyer 	res.cookie = arg->cookie;
   1158  1.3  bouyer 	res.stat = nlm4_granted;
   1159  1.3  bouyer 	res.sequence = 1234356;	/* X/Open says this field is ignored? */
   1160  1.3  bouyer 	return (&res);
   1161  1.3  bouyer }
   1162  1.3  bouyer 
   1163  1.3  bouyer /* nlm4_unshare ------------------------------------------------------------ */
   1164  1.3  bouyer /*
   1165  1.3  bouyer  * Purpose:	Release a DOS-style lock
   1166  1.3  bouyer  * Returns:	nlm_granted, unless in grace period
   1167  1.3  bouyer  * Notes:
   1168  1.3  bouyer  */
   1169  1.3  bouyer nlm4_shareres *
   1170  1.3  bouyer nlm4_unshare_4_svc(arg, rqstp)
   1171  1.3  bouyer 	nlm4_shareargs *arg;
   1172  1.3  bouyer 	struct svc_req *rqstp;
   1173  1.3  bouyer {
   1174  1.3  bouyer 	static nlm4_shareres res;
   1175  1.3  bouyer 
   1176  1.3  bouyer 	if (debug_level)
   1177  1.3  bouyer 		log_from_addr("nlm_unshare", rqstp);
   1178  1.3  bouyer 
   1179  1.3  bouyer 	res.cookie = arg->cookie;
   1180  1.3  bouyer 	res.stat = nlm4_granted;
   1181  1.3  bouyer 	res.sequence = 1234356;	/* X/Open says this field is ignored? */
   1182  1.3  bouyer 	return (&res);
   1183  1.3  bouyer }
   1184  1.3  bouyer 
   1185  1.3  bouyer /* nlm4_nm_lock ------------------------------------------------------------ */
   1186  1.3  bouyer /*
   1187  1.3  bouyer  * Purpose:	non-monitored version of nlm4_lock()
   1188  1.3  bouyer  * Returns:	as for nlm4_lock()
   1189  1.3  bouyer  * Notes:	These locks are in the same style as the standard nlm4_lock,
   1190  1.3  bouyer  *		but the rpc.statd should not be called to establish a
   1191  1.3  bouyer  *		monitor for the client machine, since that machine is
   1192  1.3  bouyer  *		declared not to be running a rpc.statd, and so would not
   1193  1.3  bouyer  *		respond to the statd protocol.
   1194  1.3  bouyer  */
   1195  1.3  bouyer nlm4_res *
   1196  1.3  bouyer nlm4_nm_lock_4_svc(arg, rqstp)
   1197  1.3  bouyer 	nlm4_lockargs *arg;
   1198  1.3  bouyer 	struct svc_req *rqstp;
   1199  1.3  bouyer {
   1200  1.3  bouyer 	static nlm4_res res;
   1201  1.3  bouyer 
   1202  1.3  bouyer 	if (debug_level)
   1203  1.3  bouyer 		log_from_addr("nlm4_nm_lock", rqstp);
   1204  1.3  bouyer 
   1205  1.3  bouyer 	/* copy cookie from arg to result.  See comment in nlm4_test_1() */
   1206  1.3  bouyer 	res.cookie = arg->cookie;
   1207  1.3  bouyer 	res.stat.stat = nlm4_granted;
   1208  1.3  bouyer 	return (&res);
   1209  1.3  bouyer }
   1210  1.3  bouyer 
   1211  1.3  bouyer /* nlm4_free_all ------------------------------------------------------------ */
   1212  1.3  bouyer /*
   1213  1.3  bouyer  * Purpose:	Release all locks held by a named client
   1214  1.3  bouyer  * Returns:	Nothing
   1215  1.3  bouyer  * Notes:	Potential denial of service security problem here - the
   1216  1.3  bouyer  *		locks to be released are specified by a host name, independent
   1217  1.3  bouyer  *		of the address from which the request has arrived.
   1218  1.3  bouyer  *		Should probably be rejected if the named host has been
   1219  1.3  bouyer  *		using monitored locks.
   1220  1.3  bouyer  */
   1221  1.3  bouyer void *
   1222  1.3  bouyer nlm4_free_all_4_svc(arg, rqstp)
   1223  1.3  bouyer 	nlm_notify *arg;
   1224  1.3  bouyer 	struct svc_req *rqstp;
   1225  1.3  bouyer {
   1226  1.3  bouyer 	static char dummy;
   1227  1.3  bouyer 
   1228  1.3  bouyer 	if (debug_level)
   1229  1.3  bouyer 		log_from_addr("nlm4_free_all", rqstp);
   1230  1.3  bouyer 	return (&dummy);
   1231  1.3  bouyer }
   1232  1.3  bouyer 
   1233  1.4  bouyer /* nlm_sm_notify --------------------------------------------------------- */
   1234  1.4  bouyer /*
   1235  1.4  bouyer  * Purpose:	called by rpc.statd when a monitored host state changes.
   1236  1.4  bouyer  * Returns:	Nothing
   1237  1.4  bouyer  */
   1238  1.4  bouyer void *
   1239  1.4  bouyer nlm_sm_notify_0_svc(arg, rqstp)
   1240  1.4  bouyer 	struct nlm_sm_status *arg;
   1241  1.4  bouyer 	struct svc_req *rqstp;
   1242  1.4  bouyer {
   1243  1.4  bouyer 	static char dummy;
   1244  1.4  bouyer 	notify(arg->mon_name, arg->state);
   1245  1.4  bouyer 	return (&dummy);
   1246  1.4  bouyer }
   1247