xref: /netbsd-src/usr.sbin/rpc.lockd/lockd.c (revision 4472dbe5e3bd91ef2540bada7a7ca7384627ff9b)
1 /*	$NetBSD: lockd.c,v 1.4 2000/02/02 18:17:42 bouyer 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.4 2000/02/02 18:17:42 bouyer 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 void	nlm_prog_4 __P((struct svc_req *, SVCXPRT *));
87 
88 int
89 main(argc, argv)
90 	int argc;
91 	char **argv;
92 {
93 	SVCXPRT *transp;
94 	int ch;
95 
96 	while ((ch = getopt(argc, argv, "d:")) != (-1)) {
97 		switch (ch) {
98 		case 'd':
99 			debug_level = atoi(optarg);
100 			if (!debug_level) {
101 				errx(1, "usage: rpc.lockd [-d <debuglevel>]");
102 				/* NOTREACHED */
103 			}
104 			break;
105 		default:
106 		case '?':
107 			errx(1, "usage: rpc.lockd [-d <debuglevel>]");
108 			/* NOTREACHED */
109 		}
110 	}
111 
112 	(void)pmap_unset(NLM_PROG, NLM_VERS);
113 	(void)pmap_unset(NLM_PROG, NLM_VERSX);
114 	(void)pmap_unset(NLM_PROG, NLM_VERS4);
115 
116 	transp = svcudp_create(RPC_ANYSOCK);
117 	if (transp == NULL) {
118 		errx(1, "cannot create udp service.");
119 		/* NOTREACHED */
120 	}
121 	if (!svc_register(transp, NLM_PROG, NLM_VERS,
122 	    nlm_prog_1, IPPROTO_UDP)) {
123 		errx(1, "unable to register (NLM_PROG, NLM_VERS, udp).");
124 		/* NOTREACHED */
125 	}
126 	if (!svc_register(transp, NLM_PROG, NLM_VERSX,
127 	    nlm_prog_3, IPPROTO_UDP)) {
128 		errx(1, "unable to register (NLM_PROG, NLM_VERSX, udp).");
129 		/* NOTREACHED */
130 	}
131 	if (!svc_register(transp, NLM_PROG, NLM_VERS4,
132 	    nlm_prog_4, IPPROTO_UDP)) {
133 		errx(1, "unable to register (NLM_PROG, NLM_VERS4, udp).");
134 		/* NOTREACHED */
135 	}
136 	transp = svctcp_create(RPC_ANYSOCK, 0, 0);
137 	if (transp == NULL) {
138 		errx(1, "cannot create tcp service.");
139 		/* NOTREACHED */
140 	}
141 	if (!svc_register(transp, NLM_PROG, NLM_VERS,
142 	    nlm_prog_1, IPPROTO_TCP)) {
143 		errx(1, "unable to register (NLM_PROG, NLM_VERS, tcp).");
144 		/* NOTREACHED */
145 	}
146 	if (!svc_register(transp, NLM_PROG, NLM_VERSX,
147 	    nlm_prog_3, IPPROTO_TCP)) {
148 		errx(1, "unable to register (NLM_PROG, NLM_VERSX, tcp).");
149 		/* NOTREACHED */
150 	}
151 	if (!svc_register(transp, NLM_PROG, NLM_VERS4,
152 	    nlm_prog_4, IPPROTO_TCP)) {
153 		errx(1, "unable to register (NLM_PROG, NLM_VERS4, tcp).");
154 		/* NOTREACHED */
155 	}
156 
157 	/*
158 	 * Note that it is NOT sensible to run this program from inetd - the
159 	 * protocol assumes that it will run immediately at boot time.
160 	 */
161 	if (daemon(0, 0)) {
162 		err(1, "cannot fork");
163 		/* NOTREACHED */
164 	}
165 	pidfile(NULL);
166 
167 	openlog("rpc.lockd", 0, LOG_DAEMON);
168 	if (debug_level)
169 		syslog(LOG_INFO, "Starting, debug level %d", debug_level);
170 	else
171 		syslog(LOG_INFO, "Starting");
172 
173 	svc_run();		/* Should never return */
174 	exit(1);
175 }
176