lockd.c revision 1.5 1 /* $NetBSD: lockd.c,v 1.5 2000/06/07 14:34:40 bouyer Exp $ */
2
3 /*
4 * Copyright (c) 1995
5 * A.R. Gordon (andrew.gordon (at) 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.5 2000/06/07 14:34:40 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 lock_proc.c
46 */
47
48 #include <err.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <errno.h>
52 #include <syslog.h>
53 #include <signal.h>
54 #include <string.h>
55 #include <unistd.h>
56 #include <util.h>
57
58 #include <rpc/rpc.h>
59 #include <rpcsvc/sm_inter.h>
60
61 #include "lockd.h"
62 #include "nlm_prot.h"
63
64 int debug_level = 0; /* 0 = no debugging syslog() calls */
65 int _rpcsvcdirty = 0;
66
67 int grace_expired;
68
69 int main __P((int, char **));
70 void nlm_prog_0 __P((struct svc_req *, SVCXPRT *));
71 void nlm_prog_1 __P((struct svc_req *, SVCXPRT *));
72 void nlm_prog_3 __P((struct svc_req *, SVCXPRT *));
73 void nlm_prog_4 __P((struct svc_req *, SVCXPRT *));
74 void usage __P((void));
75
76 void sigalarm_handler __P((int));
77
78 int
79 main(argc, argv)
80 int argc;
81 char **argv;
82 {
83 SVCXPRT *transp;
84 int ch;
85 struct sigaction sigchild, sigalarm;
86 int grace_period = 30;
87
88 while ((ch = getopt(argc, argv, "d:g:")) != (-1)) {
89 switch (ch) {
90 case 'd':
91 debug_level = atoi(optarg);
92 if (!debug_level) {
93 usage();
94 /* NOTREACHED */
95 }
96 break;
97 case 'g':
98 grace_period = atoi(optarg);
99 if (!grace_period) {
100 usage();
101 /* NOTREACHED */
102 }
103 break;
104 default:
105 case '?':
106 usage();
107 /* NOTREACHED */
108 }
109 }
110
111 (void)pmap_unset(NLM_PROG, NLM_SM);
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_SM,
122 nlm_prog_0, IPPROTO_UDP)) {
123 errx(1, "unable to register (NLM_PROG, NLM_SM, udp).");
124 /* NOTREACHED */
125 }
126 if (!svc_register(transp, NLM_PROG, NLM_VERS,
127 nlm_prog_1, IPPROTO_UDP)) {
128 errx(1, "unable to register (NLM_PROG, NLM_VERS, udp).");
129 /* NOTREACHED */
130 }
131 if (!svc_register(transp, NLM_PROG, NLM_VERSX,
132 nlm_prog_3, IPPROTO_UDP)) {
133 errx(1, "unable to register (NLM_PROG, NLM_VERSX, udp).");
134 /* NOTREACHED */
135 }
136 if (!svc_register(transp, NLM_PROG, NLM_VERS4,
137 nlm_prog_4, IPPROTO_UDP)) {
138 errx(1, "unable to register (NLM_PROG, NLM_VERS4, udp).");
139 /* NOTREACHED */
140 }
141 transp = svctcp_create(RPC_ANYSOCK, 0, 0);
142 if (transp == NULL) {
143 errx(1, "cannot create tcp service.");
144 /* NOTREACHED */
145 }
146 if (!svc_register(transp, NLM_PROG, NLM_VERS,
147 nlm_prog_1, IPPROTO_TCP)) {
148 errx(1, "unable to register (NLM_PROG, NLM_VERS, tcp).");
149 /* NOTREACHED */
150 }
151 if (!svc_register(transp, NLM_PROG, NLM_VERSX,
152 nlm_prog_3, IPPROTO_TCP)) {
153 errx(1, "unable to register (NLM_PROG, NLM_VERSX, tcp).");
154 /* NOTREACHED */
155 }
156 if (!svc_register(transp, NLM_PROG, NLM_VERS4,
157 nlm_prog_4, IPPROTO_TCP)) {
158 errx(1, "unable to register (NLM_PROG, NLM_VERS4, tcp).");
159 /* NOTREACHED */
160 }
161
162 /*
163 * Note that it is NOT sensible to run this program from inetd - the
164 * protocol assumes that it will run immediately at boot time.
165 */
166 if (daemon(0, 0)) {
167 err(1, "cannot fork");
168 /* NOTREACHED */
169 }
170 pidfile(NULL);
171
172 openlog("rpc.lockd", 0, LOG_DAEMON);
173 if (debug_level)
174 syslog(LOG_INFO, "Starting, debug level %d", debug_level);
175 else
176 syslog(LOG_INFO, "Starting");
177
178 sigchild.sa_handler = sigchild_handler;
179 sigemptyset(&sigchild.sa_mask);
180 sigchild.sa_flags = SA_RESTART;
181 if (sigaction(SIGCHLD, &sigchild, NULL) != 0) {
182 syslog(LOG_WARNING, "sigaction(SIGCHLD) failed: %s",
183 strerror(errno));
184 exit(1);
185 }
186 sigalarm.sa_handler = sigalarm_handler;
187 sigemptyset(&sigalarm.sa_mask);
188 sigalarm.sa_flags = SA_RESETHAND; /* should only happen once */
189 sigalarm.sa_flags |= SA_RESTART;
190 if (sigaction(SIGALRM, &sigalarm, NULL) != 0) {
191 syslog(LOG_WARNING, "sigaction(SIGALRM) failed: %s",
192 strerror(errno));
193 exit(1);
194 }
195 grace_expired = 0;
196 if (alarm(10) < 0) {
197 syslog(LOG_WARNING, "alarm failed: %s",
198 strerror(errno));
199 exit(1);
200 }
201
202 svc_run(); /* Should never return */
203 exit(1);
204 }
205
206 void
207 sigalarm_handler(s)
208 int s;
209 {
210 grace_expired = 1;
211 }
212
213 void
214 usage()
215 {
216 errx(1, "usage: rpc.lockd [-d <debuglevel>] [-g <grace period>]");
217 }
218