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