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