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