Home | History | Annotate | Line # | Download | only in rpc.lockd
lockd.c revision 1.1
      1  1.1  scottr /*	$NetBSD: lockd.c,v 1.1 1997/03/10 06:26:20 scottr 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.1  scottr 
     37  1.1  scottr /*
     38  1.1  scottr  * main() function for NFS lock daemon.  Most of the code in this
     39  1.1  scottr  * file was generated by running rpcgen /usr/include/rpcsvc/nlm_prot.x.
     40  1.1  scottr  *
     41  1.1  scottr  * The actual program logic is in the file procs.c
     42  1.1  scottr  */
     43  1.1  scottr 
     44  1.1  scottr #include <stdio.h>
     45  1.1  scottr #include <syslog.h>
     46  1.1  scottr 
     47  1.1  scottr #include <rpc/rpc.h>
     48  1.1  scottr #include <rpcsvc/sm_inter.h>
     49  1.1  scottr 
     50  1.1  scottr #include "lockd.h"
     51  1.1  scottr #include "nlm_prot.h"
     52  1.1  scottr 
     53  1.1  scottr /*
     54  1.1  scottr  * XXX - Actual locking is not implemented.  Some notes from the FreeBSD
     55  1.1  scottr  * rpc.lockd/handles.c:
     56  1.1  scottr  *
     57  1.1  scottr  * - Need to find all fds in use by a host and free them when host crashes
     58  1.1  scottr  *   (need not be efficient)
     59  1.1  scottr  * - Need to find fd corresponding to <inode, device>
     60  1.1  scottr  */
     61  1.1  scottr 
     62  1.1  scottr typedef struct fdinfo {
     63  1.1  scottr 	int	fd;		/* The file descriptor itself */
     64  1.1  scottr 	int	ref_count;	/* Count of hosts using the fd - fd is
     65  1.1  scottr 				   closed when this reaches zero */
     66  1.1  scottr 	ino_t	inode_no;	/* The inode number of this file. */
     67  1.1  scottr 	dev_t	device;		/* device on which the file lives. */
     68  1.1  scottr 	struct fdinfo	*next;	/* Chain of FdInfo structures */
     69  1.1  scottr 	struct fdinfo	*prev;
     70  1.1  scottr }       FdInfo;
     71  1.1  scottr 
     72  1.1  scottr int		debug_level = 0;	/* 0 = no debugging syslog() calls */
     73  1.1  scottr int		_rpcsvcdirty = 0;
     74  1.1  scottr 
     75  1.1  scottr void	nlm_prog_1 __P((struct svc_req *, SVCXPRT *));
     76  1.1  scottr void	nlm_prog_3 __P((struct svc_req *, SVCXPRT *));
     77  1.1  scottr 
     78  1.1  scottr main(argc, argv)
     79  1.1  scottr 	int argc;
     80  1.1  scottr 	char **argv;
     81  1.1  scottr {
     82  1.1  scottr 	extern char *optarg;
     83  1.1  scottr 	SVCXPRT *transp;
     84  1.1  scottr 	int ch;
     85  1.1  scottr 
     86  1.1  scottr 	while ((ch = getopt(argc, argv, "d:")) != (-1)) {
     87  1.1  scottr 		switch (ch) {
     88  1.1  scottr 		case 'd':
     89  1.1  scottr 			debug_level = atoi(optarg);
     90  1.1  scottr 			if (!debug_level) {
     91  1.1  scottr 				errx(1, "usage: rpc.lockd [-d <debuglevel>]");
     92  1.1  scottr 				/* NOTREACHED */
     93  1.1  scottr 			}
     94  1.1  scottr 			break;
     95  1.1  scottr 		default:
     96  1.1  scottr 		case '?':
     97  1.1  scottr 			errx(1, "usage: rpc.lockd [-d <debuglevel>]");
     98  1.1  scottr 			/* NOTREACHED */
     99  1.1  scottr 		}
    100  1.1  scottr 	}
    101  1.1  scottr 
    102  1.1  scottr 	(void)pmap_unset(NLM_PROG, NLM_VERS);
    103  1.1  scottr 	(void)pmap_unset(NLM_PROG, NLM_VERSX);
    104  1.1  scottr 
    105  1.1  scottr 	transp = svcudp_create(RPC_ANYSOCK);
    106  1.1  scottr 	if (transp == NULL) {
    107  1.1  scottr 		errx(1, "cannot create udp service.");
    108  1.1  scottr 		/* NOTREACHED */
    109  1.1  scottr 	}
    110  1.1  scottr 	if (!svc_register(transp, NLM_PROG, NLM_VERS,
    111  1.1  scottr 	    nlm_prog_1, IPPROTO_UDP)) {
    112  1.1  scottr 		errx(1, "unable to register (NLM_PROG, NLM_VERS, udp).");
    113  1.1  scottr 		/* NOTREACHED */
    114  1.1  scottr 	}
    115  1.1  scottr 	if (!svc_register(transp, NLM_PROG, NLM_VERSX,
    116  1.1  scottr 	    nlm_prog_3, IPPROTO_UDP)) {
    117  1.1  scottr 		errx(1, "unable to register (NLM_PROG, NLM_VERSX, udp).");
    118  1.1  scottr 		/* NOTREACHED */
    119  1.1  scottr 	}
    120  1.1  scottr 	transp = svctcp_create(RPC_ANYSOCK, 0, 0);
    121  1.1  scottr 	if (transp == NULL) {
    122  1.1  scottr 		errx(1, "cannot create tcp service.");
    123  1.1  scottr 		/* NOTREACHED */
    124  1.1  scottr 	}
    125  1.1  scottr 	if (!svc_register(transp, NLM_PROG, NLM_VERS,
    126  1.1  scottr 	    nlm_prog_1, IPPROTO_TCP)) {
    127  1.1  scottr 		errx(1, "unable to register (NLM_PROG, NLM_VERS, tcp).");
    128  1.1  scottr 		/* NOTREACHED */
    129  1.1  scottr 	}
    130  1.1  scottr 	if (!svc_register(transp, NLM_PROG, NLM_VERSX,
    131  1.1  scottr 	    nlm_prog_3, IPPROTO_TCP)) {
    132  1.1  scottr 		errx(1, "unable to register (NLM_PROG, NLM_VERSX, tcp).");
    133  1.1  scottr 		/* NOTREACHED */
    134  1.1  scottr 	}
    135  1.1  scottr 
    136  1.1  scottr 	/*
    137  1.1  scottr 	 * Note that it is NOT sensible to run this program from inetd - the
    138  1.1  scottr 	 * protocol assumes that it will run immediately at boot time.
    139  1.1  scottr 	 */
    140  1.1  scottr 	if (daemon(0, 0)) {
    141  1.1  scottr 		err(1, "cannot fork");
    142  1.1  scottr 		/* NOTREACHED */
    143  1.1  scottr 	}
    144  1.1  scottr 
    145  1.1  scottr 	openlog("rpc.lockd", 0, LOG_DAEMON);
    146  1.1  scottr 	if (debug_level)
    147  1.1  scottr 		syslog(LOG_INFO, "Starting, debug level %d", debug_level);
    148  1.1  scottr 	else
    149  1.1  scottr 		syslog(LOG_INFO, "Starting");
    150  1.1  scottr 
    151  1.1  scottr 	svc_run();		/* Should never return */
    152  1.1  scottr 	exit(1);
    153  1.1  scottr }
    154