1 /* $NetBSD: lockd.c,v 1.9 2007/11/04 23:12:50 christos Exp $ */
5 * A.R. Gordon (andrew.gordon@net-tel.co.uk). All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
36 #include <sys/cdefs.h>
38 __RCSID("$NetBSD: lockd.c,v 1.9 2007/11/04 23:12:50 christos Exp $");
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.
45 * The actual program logic is in the file lock_proc.c
48 #include <sys/types.h>
49 #include <sys/socket.h>
60 #include <netconfig.h>
63 #include <rpcsvc/sm_inter.h>
66 #include <rpcsvc/nlm_prot.h>
68 int debug_level
= 0; /* 0 = no debugging syslog() calls */
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
;
79 static void sigalarm_handler(int);
81 static const char *transports
[] = { "udp", "tcp", "udp6", "tcp6" };
84 main(int argc
, char **argv
)
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
;
93 (void)setprogname(*argv
);
94 while ((ch
= getopt(argc
, argv
, "d:g:")) != (-1)) {
97 debug_level
= atoi(optarg
);
104 grace_period
= atoi(optarg
);
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
);
123 * Check if IPv6 support is present.
125 s
= socket(AF_INET6
, SOCK_DGRAM
, IPPROTO_UDP
);
133 (void)rpc_control(RPC_SVC_CONNMAXREC_SET
, &maxrec
);
135 for (i
= 0; i
< maxindex
; i
++) {
136 nconf
= getnetconfigent(transports
[i
]);
138 errx(1, "cannot get udp netconf.");
140 transp
= svc_tli_create(RPC_ANYFD
, nconf
, NULL
, RPC_MAXDATASIZE
,
142 if (transp
== NULL
) {
143 errx(1, "cannot create %s service.", transports
[i
]);
146 if (!svc_reg(transp
, NLM_PROG
, NLM_SM
, nlm_prog_0
, nconf
)) {
147 errx(1, "unable to register (NLM_PROG, NLM_SM, %s)",
151 if (!svc_reg(transp
, NLM_PROG
, NLM_VERS
, nlm_prog_1
, nconf
)) {
152 errx(1, "unable to register (NLM_PROG, NLM_VERS, %s)",
156 if (!svc_reg(transp
, NLM_PROG
, NLM_VERSX
, nlm_prog_3
, nconf
)) {
157 errx(1, "unable to register (NLM_PROG, NLM_VERSX, %s)",
161 if (!svc_reg(transp
, NLM_PROG
, NLM_VERS4
, nlm_prog_4
, nconf
)) {
162 errx(1, "unable to register (NLM_PROG, NLM_VERS4, %s)",
166 freenetconfigent(nconf
);
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.
173 if (daemon(0, 0) == -1) {
174 err(1, "cannot fork");
179 openlog("rpc.lockd", 0, LOG_DAEMON
);
181 syslog(LOG_INFO
, "Starting, debug level %d", debug_level
);
183 syslog(LOG_INFO
, "Starting");
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)");
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)");
201 if (alarm(10) == (unsigned int)-1) {
202 syslog(LOG_WARNING
, "alarm failed (%m)");
206 svc_run(); /* Should never return */
212 sigalarm_handler(int s
)
220 (void)fprintf(stderr
, "Usage: %s[-d <debuglevel>] [-g <grace period>]",