]> git.saurik.com Git - apple/network_cmds.git/blob - rpc_lockd.tproj/lockd.c
network_cmds-176.3.1.tar.gz
[apple/network_cmds.git] / rpc_lockd.tproj / lockd.c
1 /* $NetBSD: lockd.c,v 1.7 2000/08/12 18:08:44 thorpej Exp $ */
2 /* $FreeBSD: src/usr.sbin/rpc.lockd/lockd.c,v 1.13 2002/04/11 07:19:30 alfred Exp $ */
3
4 /*
5 * Copyright (c) 1995
6 * A.R. Gordon (andrew.gordon@net-tel.co.uk). All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed for the FreeBSD project
19 * 4. Neither the name of the author nor the names of any co-contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY ANDREW GORDON AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 */
36
37 #include <sys/cdefs.h>
38 #ifndef lint
39 __RCSID("$NetBSD: lockd.c,v 1.7 2000/08/12 18:08:44 thorpej Exp $");
40 #endif
41
42 /*
43 * main() function for NFS lock daemon. Most of the code in this
44 * file was generated by running rpcgen /usr/include/rpcsvc/nlm_prot.x.
45 *
46 * The actual program logic is in the file lock_proc.c
47 */
48
49 #include <sys/types.h>
50 #include <sys/socket.h>
51
52 #include <err.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <errno.h>
56 #include <syslog.h>
57 #include <signal.h>
58 #include <string.h>
59 #include <unistd.h>
60 #include <fcntl.h>
61 #include <sys/stat.h>
62
63 #include <rpc/rpc.h>
64 #include <rpc/pmap_clnt.h>
65 #include <rpcsvc/sm_inter.h>
66
67 #include "lockd.h"
68 #include <rpcsvc/nlm_prot.h>
69
70 int debug_level = 0; /* 0 = no debugging syslog() calls */
71 int _rpcsvcdirty = 0;
72 int waitkern = 0; /* 1 = wait for kernel to say start */
73
74 const char *pid_file = NULL;
75
76 int grace_expired;
77 int nsm_state;
78 pid_t client_pid = -1;
79 struct mon mon_host;
80
81 void init_nsm(void);
82 void nlm_prog_0(struct svc_req *, SVCXPRT *);
83 void nlm_prog_1(struct svc_req *, SVCXPRT *);
84 void nlm_prog_3(struct svc_req *, SVCXPRT *);
85 void nlm_prog_4(struct svc_req *, SVCXPRT *);
86 void usage(void);
87
88 int claim_pid_file(const char *, int);
89 void cleanup_pid_file(void);
90 void handle_sig_cleanup(int);
91
92 void sigalarm_handler(void);
93 void my_svc_run(void);
94
95 const char *transports[] = { "udp", "tcp", "udp6", "tcp6" };
96
97 int
98 main(argc, argv)
99 int argc;
100 char **argv;
101 {
102 SVCXPRT *transp;
103 int ch;
104 struct sigaction sigalarm;
105 int grace_period = 30;
106
107 while ((ch = getopt(argc, argv, "d:g:wx:")) != (-1)) {
108 switch (ch) {
109 case 'd':
110 debug_level = atoi(optarg);
111 if (!debug_level) {
112 usage();
113 /* NOTREACHED */
114 }
115 break;
116 case 'g':
117 grace_period = atoi(optarg);
118 if (!grace_period) {
119 usage();
120 /* NOTREACHED */
121 }
122 break;
123 case 'w':
124 waitkern = 1;
125 break;
126 case 'x':
127 host_expire = atoi(optarg);
128 break;
129 default:
130 case '?':
131 usage();
132 /* NOTREACHED */
133 }
134 }
135 if (geteuid()) { /* This command allowed only to root */
136 fprintf(stderr, "Sorry. You are not superuser\n");
137 exit(1);
138 }
139
140 /*
141 * Note that it is NOT sensible to run this program from inetd - the
142 * protocol assumes that it will run immediately at boot time.
143 */
144 if (debug_level != 99 && daemon(0, 0)) {
145 err(1, "cannot fork");
146 /* NOTREACHED */
147 }
148
149 /* Install signal handler to remove any pid file */
150 signal(SIGINT, handle_sig_cleanup);
151 signal(SIGTERM, handle_sig_cleanup);
152 signal(SIGHUP, handle_sig_cleanup);
153 signal(SIGQUIT, handle_sig_cleanup);
154
155 if (claim_pid_file("/var/run/lockd.pid", 0) < 0)
156 errx(1, "cannot claim pid file");
157
158 if (waitkern) {
159 struct timespec ts;
160 /* wait for kernel to get first lock request */
161 client_kern_wait();
162 /* start statd now, in case it isn't already */
163 system("rpc.statd");
164 /* sleep a little to give statd/portmap a chance to start */
165 /* (better to sleep 100ms than to timeout on portmap calls) */
166 ts.tv_sec = 0;
167 ts.tv_nsec = 100*1000*1000;
168 nanosleep(&ts, NULL);
169 }
170
171 openlog("rpc.lockd", debug_level == 99 ? LOG_PERROR : 0, LOG_DAEMON);
172 if (debug_level)
173 syslog(LOG_INFO, "Starting, debug level %d", debug_level);
174 else
175 syslog(LOG_INFO, "Starting");
176
177 (void)pmap_unset(NLM_PROG, NLM_SM);
178 (void)pmap_unset(NLM_PROG, NLM_VERS);
179 (void)pmap_unset(NLM_PROG, NLM_VERSX);
180 (void)pmap_unset(NLM_PROG, NLM_VERS4);
181
182 transp = svcudp_create(RPC_ANYSOCK);
183 if (transp == NULL)
184 errx(1, "cannot create udp service");
185 if (!svc_register(transp, NLM_PROG, NLM_VERS, nlm_prog_1, IPPROTO_UDP))
186 errx(1, "unable to register (NLM_PROG, NLM_VERS, udp)");
187 if (!svc_register(transp, NLM_PROG, NLM_VERSX, nlm_prog_3, IPPROTO_UDP))
188 errx(1, "unable to register (NLM_PROG, NLM_VERSX, udp)");
189 if (!svc_register(transp, NLM_PROG, NLM_VERS4, nlm_prog_4, IPPROTO_UDP))
190 errx(1, "unable to register (NLM_PROG, NLM_VERS4, udp)");
191
192 transp = svctcp_create(RPC_ANYSOCK, 0, 0);
193 if (transp == NULL)
194 errx(1, "cannot create tcp service");
195 if (!svc_register(transp, NLM_PROG, NLM_VERS, nlm_prog_1, IPPROTO_TCP))
196 errx(1, "unable to register (NLM_PROG, NLM_VERS, tcp)");
197 if (!svc_register(transp, NLM_PROG, NLM_VERSX, nlm_prog_3, IPPROTO_TCP))
198 errx(1, "unable to register (NLM_PROG, NLM_VERSX, tcp)");
199 if (!svc_register(transp, NLM_PROG, NLM_VERS4, nlm_prog_4, IPPROTO_TCP))
200 errx(1, "unable to register (NLM_PROG, NLM_VERS4, tcp)");
201
202 sigalarm.sa_handler = (sig_t) sigalarm_handler;
203 sigemptyset(&sigalarm.sa_mask);
204 sigalarm.sa_flags = SA_RESETHAND; /* should only happen once */
205 sigalarm.sa_flags |= SA_RESTART;
206 if (sigaction(SIGALRM, &sigalarm, NULL) != 0) {
207 syslog(LOG_WARNING, "sigaction(SIGALRM) failed: %s",
208 strerror(errno));
209 exit(1);
210 }
211 grace_expired = 0;
212 alarm(grace_period);
213
214 init_nsm();
215
216 client_pid = client_request();
217
218 my_svc_run(); /* Should never return */
219 exit(1);
220 }
221
222 void
223 sigalarm_handler(void)
224 {
225
226 grace_expired = 1;
227 }
228
229 void
230 usage()
231 {
232 errx(1, "usage: rpc.lockd [-d <debuglevel>] [-g <grace period>] "
233 " [-x <statd cache timeout>] [-w]");
234 }
235
236 /*
237 * init_nsm --
238 * Reset the NSM state-of-the-world and acquire its state.
239 */
240 void
241 init_nsm(void)
242 {
243 enum clnt_stat ret;
244 my_id id;
245 sm_stat stat;
246 char name[] = "NFS NLM";
247 char localhost[] = "localhost";
248 int attempt = 0;
249
250 /*
251 * !!!
252 * The my_id structure isn't used by the SM_UNMON_ALL call, as far
253 * as I know. Leave it empty for now.
254 */
255 memset(&id, 0, sizeof(id));
256 id.my_name = name;
257
258 /*
259 * !!!
260 * The statd program must already be registered when lockd runs.
261 * If we have a problem contacting statd, pause and try again a
262 * number of times in case statd is just slow in coming up.
263 */
264 do {
265 ret = callrpc("localhost", SM_PROG, SM_VERS, SM_UNMON_ALL,
266 xdr_my_id, &id, xdr_sm_stat, &stat);
267 if (ret) {
268 warnx("%lu %s", SM_PROG, clnt_sperrno(ret));
269 if (++attempt < 20) {
270 sleep(attempt);
271 continue;
272 }
273 }
274 break;
275 } while (1);
276
277 if (ret != 0) {
278 errx(1, "%lu %s", SM_PROG, clnt_sperrno(ret));
279 }
280
281 nsm_state = stat.state;
282
283 /* setup constant data for SM_MON calls */
284 mon_host.mon_id.my_id.my_name = localhost;
285 mon_host.mon_id.my_id.my_prog = NLM_PROG;
286 mon_host.mon_id.my_id.my_vers = NLM_SM;
287 mon_host.mon_id.my_id.my_proc = NLM_SM_NOTIFY; /* bsdi addition */
288 }
289
290 /*
291 * claim_pid_file
292 *
293 * Purpose: take ownership of and store pid in given pid_file
294 * Returns: 0 on success or -1 on failure
295 * Notes: force parameter requests that current owner (if any) of
296 * pid file be terminated.
297 */
298 int
299 claim_pid_file(const char *name, int force)
300 {
301 int pidfd, rv, retried = 0;
302 FILE *pidfile;
303
304 try_again:
305
306 /* attempt exclusive open of pid file */
307 pidfd = open(name, O_EXCL|O_CREAT|O_WRONLY,
308 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
309 if (pidfd < 0) {
310 char buf[16];
311 pid_t pid;
312 if (retried)
313 return -1;
314 bzero(buf, 16);
315 retried = 1;
316 /* pid file busy, check validity */
317 pidfd = open(name, O_RDONLY);
318 if (pidfd < 0)
319 goto try_again;
320 rv = read(pidfd, buf, 15);
321 close(pidfd);
322 if (rv <= 0)
323 goto try_again;
324 pid = atoi(buf);
325 if (pid <= 0)
326 goto try_again;
327 rv = kill(pid, force ? SIGKILL : 0);
328 /* if can't signal, assume stale pid file */
329 if ((rv < 0) || force)
330 unlink(name);
331 goto try_again;
332 }
333 pid_file = name;
334 atexit(cleanup_pid_file);
335
336 pidfile = fdopen(pidfd, "w");
337 if (pidfile) {
338 fchmod(pidfd, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
339 fprintf(pidfile, "%d\n", getpid());
340 fclose(pidfile);
341 } else
342 perror("fdopen");
343 close(pidfd);
344 return 0;
345 }
346
347 /*
348 * cleanup_pid_file
349 *
350 * Purpose: delete any pid_file that has been claimed
351 * Returns: Nothing
352 */
353 void
354 cleanup_pid_file(void)
355 {
356 if (pid_file) {
357 unlink(pid_file);
358 pid_file = NULL;
359 }
360 }
361
362 /*
363 * handle_sig_cleanup
364 *
365 * Purpose: on signal, kill client child and do pid file cleanup
366 * Returns: Nothing
367 */
368 void
369 handle_sig_cleanup(int sig __unused)
370 {
371 if (client_pid != -1)
372 kill(client_pid, SIGTERM);
373 cleanup_pid_file();
374 exit(1);
375 }
376
377 void
378 my_svc_run(void)
379 {
380 fd_set readfds;
381 struct timeval timeout;
382 struct timeval now;
383 int error;
384 int hashosts = 0;
385 int tsize = 0;
386 struct timeval *top;
387
388
389 for( ;; ) {
390 timeout.tv_sec = host_expire + 1;
391 timeout.tv_usec = 0;
392
393 tsize = getdtablesize();
394 bcopy(&svc_fdset, &readfds, sizeof(svc_fdset));
395 /*
396 * If there are any expired hosts then sleep with a
397 * timeout to expire them.
398 */
399 if (hashosts && (timeout.tv_sec >= 0))
400 top = &timeout;
401 else
402 top = NULL;
403 error = select(tsize, &readfds, NULL, NULL, top);
404 if (error == -1) {
405 if (errno == EINTR)
406 continue;
407 perror("rpc.lockd: my_svc_run: select failed");
408 return;
409 }
410 gettimeofday(&now, NULL);
411 currsec = now.tv_sec;
412 if (error > 0)
413 svc_getreqset(&readfds);
414 if (debug_level > 3 && error == 0)
415 fprintf(stderr, "my_svc_run: select timeout\n");
416 hashosts = expire_lock_hosts();
417 }
418 }