xref: /netbsd/usr.sbin/rpc.lockd/lockd.c (revision c4a72b64)
1 /*	$NetBSD: lockd.c,v 1.8 2002/11/08 00:16:39 fvdl 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.8 2002/11/08 00:16:39 fvdl 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 lock_proc.c
46  */
47 
48 #include <sys/types.h>
49 #include <sys/socket.h>
50 
51 #include <err.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <errno.h>
55 #include <syslog.h>
56 #include <signal.h>
57 #include <string.h>
58 #include <unistd.h>
59 #include <util.h>
60 #include <netconfig.h>
61 
62 #include <rpc/rpc.h>
63 #include <rpcsvc/sm_inter.h>
64 
65 #include "lockd.h"
66 #include <rpcsvc/nlm_prot.h>
67 
68 int		debug_level = 0;	/* 0 = no debugging syslog() calls */
69 int		_rpcsvcdirty = 0;
70 
71 int grace_expired;
72 
73 int	main __P((int, char **));
74 void	nlm_prog_0 __P((struct svc_req *, SVCXPRT *));
75 void	nlm_prog_1 __P((struct svc_req *, SVCXPRT *));
76 void	nlm_prog_3 __P((struct svc_req *, SVCXPRT *));
77 void	nlm_prog_4 __P((struct svc_req *, SVCXPRT *));
78 void	usage __P((void));
79 
80 void sigalarm_handler __P((int));
81 
82 char *transports[] = { "udp", "tcp", "udp6", "tcp6" };
83 
84 int
85 main(argc, argv)
86 	int argc;
87 	char **argv;
88 {
89 	SVCXPRT *transp;
90 	int ch, i, maxindex, s;
91 	struct sigaction sigchild, sigalarm;
92 	int grace_period = 30;
93 	struct netconfig *nconf;
94 	int maxrec = RPC_MAXDATASIZE;
95 
96 	while ((ch = getopt(argc, argv, "d:g:")) != (-1)) {
97 		switch (ch) {
98 		case 'd':
99 			debug_level = atoi(optarg);
100 			if (!debug_level) {
101 				usage();
102 				/* NOTREACHED */
103 			}
104 			break;
105 		case 'g':
106 			grace_period = atoi(optarg);
107 			if (!grace_period) {
108 				usage();
109 				/* NOTREACHED */
110 			}
111 			break;
112 		default:
113 		case '?':
114 			usage();
115 			/* NOTREACHED */
116 		}
117 	}
118 
119 	(void)rpcb_unset(NLM_PROG, NLM_SM, NULL);
120 	(void)rpcb_unset(NLM_PROG, NLM_VERS, NULL);
121 	(void)rpcb_unset(NLM_PROG, NLM_VERSX, NULL);
122 	(void)rpcb_unset(NLM_PROG, NLM_VERS4, NULL);
123 
124 	/*
125 	 * Check if IPv6 support is present.
126 	 */
127 	s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
128 	if (s < 0)
129 		maxindex = 2;
130 	else {
131 		close(s);
132 		maxindex = 4;
133 	}
134 
135 	rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec);
136 
137 	for (i = 0; i < maxindex; i++) {
138 		nconf = getnetconfigent(transports[i]);
139 		if (nconf == NULL)
140 			errx(1, "cannot get udp netconf.");
141 
142 		transp = svc_tli_create(RPC_ANYFD, nconf, NULL, RPC_MAXDATASIZE,
143 		    RPC_MAXDATASIZE);
144 		if (transp == NULL) {
145 			errx(1, "cannot create %s service.", transports[i]);
146 			/* NOTREACHED */
147 		}
148 		if (!svc_reg(transp, NLM_PROG, NLM_SM, nlm_prog_0, nconf)) {
149 			errx(1, "unable to register (NLM_PROG, NLM_SM, %s)",
150 			    transports[i]);
151 			/* NOTREACHED */
152 		}
153 		if (!svc_reg(transp, NLM_PROG, NLM_VERS, nlm_prog_1, nconf)) {
154 			errx(1, "unable to register (NLM_PROG, NLM_VERS, %s)",
155 			    transports[i]);
156 			/* NOTREACHED */
157 		}
158 		if (!svc_reg(transp, NLM_PROG, NLM_VERSX, nlm_prog_3, nconf)) {
159 			errx(1, "unable to register (NLM_PROG, NLM_VERSX, %s)",
160 			    transports[i]);
161 			/* NOTREACHED */
162 		}
163 		if (!svc_reg(transp, NLM_PROG, NLM_VERS4, nlm_prog_4, nconf)) {
164 			errx(1, "unable to register (NLM_PROG, NLM_VERS4, %s)",
165 			    transports[i]);
166 			/* NOTREACHED */
167 		}
168 		freenetconfigent(nconf);
169 	}
170 
171 	/*
172 	 * Note that it is NOT sensible to run this program from inetd - the
173 	 * protocol assumes that it will run immediately at boot time.
174 	 */
175 	if (daemon(0, 0)) {
176 		err(1, "cannot fork");
177 		/* NOTREACHED */
178 	}
179 	pidfile(NULL);
180 
181 	openlog("rpc.lockd", 0, LOG_DAEMON);
182 	if (debug_level)
183 		syslog(LOG_INFO, "Starting, debug level %d", debug_level);
184 	else
185 		syslog(LOG_INFO, "Starting");
186 
187 	sigchild.sa_handler = sigchild_handler;
188 	sigemptyset(&sigchild.sa_mask);
189 	sigchild.sa_flags = SA_RESTART;
190 	if (sigaction(SIGCHLD, &sigchild, NULL) != 0) {
191 		syslog(LOG_WARNING, "sigaction(SIGCHLD) failed: %s",
192 		    strerror(errno));
193 		exit(1);
194 	}
195 	sigalarm.sa_handler = sigalarm_handler;
196 	sigemptyset(&sigalarm.sa_mask);
197 	sigalarm.sa_flags = SA_RESETHAND; /* should only happen once */
198 	sigalarm.sa_flags |= SA_RESTART;
199 	if (sigaction(SIGALRM, &sigalarm, NULL) != 0) {
200 		syslog(LOG_WARNING, "sigaction(SIGALRM) failed: %s",
201 		    strerror(errno));
202 		exit(1);
203 	}
204 	grace_expired = 0;
205 	if (alarm(10) < 0) {
206 		syslog(LOG_WARNING, "alarm failed: %s",
207 		    strerror(errno));
208 		exit(1);
209 	}
210 
211 	svc_run();		/* Should never return */
212 	exit(1);
213 }
214 
215 void
216 sigalarm_handler(s)
217 	int s;
218 {
219 	grace_expired = 1;
220 }
221 
222 void
223 usage()
224 {
225 	errx(1, "usage: rpc.lockd [-d <debuglevel>] [-g <grace period>]");
226 }
227