]> git.saurik.com Git - apple/network_cmds.git/blob - rpc_lockd.tproj/lockd.c
network_cmds-201.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 #include <sys/resource.h>
63
64 #include <rpc/rpc.h>
65 #include <rpc/pmap_clnt.h>
66 #include <rpcsvc/sm_inter.h>
67
68 #include "lockd.h"
69 #include <rpcsvc/nlm_prot.h>
70
71 int debug_level = 0; /* 0 = no debugging syslog() calls */
72 int _rpcsvcdirty = 0;
73 int waitkern = 0; /* 1 = wait for kernel to say start */
74
75 const char *pid_file = NULL;
76
77 int grace_expired;
78 int nsm_state;
79 pid_t client_pid = -1;
80 struct mon mon_host;
81
82 void init_nsm(void);
83 void nlm_prog_0(struct svc_req *, SVCXPRT *);
84 void nlm_prog_1(struct svc_req *, SVCXPRT *);
85 void nlm_prog_3(struct svc_req *, SVCXPRT *);
86 void nlm_prog_4(struct svc_req *, SVCXPRT *);
87 void usage(void);
88
89 int claim_pid_file(const char *, int);
90 void cleanup_pid_file(void);
91 void handle_sig_cleanup(int);
92
93 void sigalarm_handler(void);
94 void my_svc_run(void);
95
96 const char *transports[] = { "udp", "tcp", "udp6", "tcp6" };
97
98 int
99 main(argc, argv)
100 int argc;
101 char **argv;
102 {
103 SVCXPRT *transp;
104 int ch;
105 struct sigaction sigalarm;
106 int grace_period = 30;
107 struct rlimit rlp;
108
109 while ((ch = getopt(argc, argv, "d:g:wx:")) != (-1)) {
110 switch (ch) {
111 case 'd':
112 debug_level = atoi(optarg);
113 if (!debug_level) {
114 usage();
115 /* NOTREACHED */
116 }
117 break;
118 case 'g':
119 grace_period = atoi(optarg);
120 if (!grace_period) {
121 usage();
122 /* NOTREACHED */
123 }
124 break;
125 case 'w':
126 waitkern = 1;
127 break;
128 case 'x':
129 host_expire = atoi(optarg);
130 break;
131 default:
132 case '?':
133 usage();
134 /* NOTREACHED */
135 }
136 }
137 if (geteuid()) { /* This command allowed only to root */
138 fprintf(stderr, "Sorry. You are not superuser\n");
139 exit(1);
140 }
141
142 /*
143 * Note that it is NOT sensible to run this program from inetd - the
144 * protocol assumes that it will run immediately at boot time.
145 */
146 if (debug_level != 99 && daemon(0, debug_level > 0)) {
147 err(1, "cannot fork");
148 /* NOTREACHED */
149 }
150
151 /* Install signal handler to remove any pid file */
152 signal(SIGINT, handle_sig_cleanup);
153 signal(SIGTERM, handle_sig_cleanup);
154 signal(SIGHUP, handle_sig_cleanup);
155 signal(SIGQUIT, handle_sig_cleanup);
156
157 openlog("rpc.lockd", debug_level == 99 ? LOG_PERROR : 0, LOG_DAEMON);
158
159 if (claim_pid_file("/var/run/lockd.pid", 0) < 0) {
160 syslog(LOG_ERR, "cannot claim pid file");
161 exit(1);
162 }
163
164 if (waitkern) {
165 struct timespec ts;
166 /* wait for kernel to get first lock request */
167 client_kern_wait();
168 /* start statd now, in case it isn't already */
169 system("rpc.statd");
170 /* sleep a little to give statd/portmap a chance to start */
171 /* (better to sleep 100ms than to timeout on portmap calls) */
172 ts.tv_sec = 0;
173 ts.tv_nsec = 100*1000*1000;
174 nanosleep(&ts, NULL);
175 }
176
177 if (debug_level)
178 syslog(LOG_INFO, "Starting, debug level %d", debug_level);
179 else
180 syslog(LOG_INFO, "Starting");
181
182 (void)pmap_unset(NLM_PROG, NLM_SM);
183 (void)pmap_unset(NLM_PROG, NLM_VERS);
184 (void)pmap_unset(NLM_PROG, NLM_VERSX);
185 (void)pmap_unset(NLM_PROG, NLM_VERS4);
186
187 transp = svcudp_create(RPC_ANYSOCK);
188 if (transp == NULL) {
189 syslog(LOG_ERR, "cannot create udp service");
190 exit(1);
191 }
192 if (!svc_register(transp, NLM_PROG, NLM_SM, nlm_prog_0, IPPROTO_UDP)) {
193 syslog(LOG_ERR, "unable to register (NLM_PROG, NLM_SM, udp)");
194 exit(1);
195 }
196 if (!svc_register(transp, NLM_PROG, NLM_VERS, nlm_prog_1, IPPROTO_UDP)) {
197 syslog(LOG_ERR, "unable to register (NLM_PROG, NLM_VERS, udp)");
198 exit(1);
199 }
200 if (!svc_register(transp, NLM_PROG, NLM_VERSX, nlm_prog_3, IPPROTO_UDP)) {
201 syslog(LOG_ERR, "unable to register (NLM_PROG, NLM_VERSX, udp)");
202 exit(1);
203 }
204 if (!svc_register(transp, NLM_PROG, NLM_VERS4, nlm_prog_4, IPPROTO_UDP)) {
205 syslog(LOG_ERR, "unable to register (NLM_PROG, NLM_VERS4, udp)");
206 exit(1);
207 }
208
209 transp = svctcp_create(RPC_ANYSOCK, 0, 0);
210 if (transp == NULL) {
211 syslog(LOG_ERR, "cannot create tcp service");
212 exit(1);
213 }
214 if (!svc_register(transp, NLM_PROG, NLM_SM, nlm_prog_0, IPPROTO_TCP)) {
215 syslog(LOG_ERR, "unable to register (NLM_PROG, NLM_SM, tcp)");
216 exit(1);
217 }
218 if (!svc_register(transp, NLM_PROG, NLM_VERS, nlm_prog_1, IPPROTO_TCP)) {
219 syslog(LOG_ERR, "unable to register (NLM_PROG, NLM_VERS, tcp)");
220 exit(1);
221 }
222 if (!svc_register(transp, NLM_PROG, NLM_VERSX, nlm_prog_3, IPPROTO_TCP)) {
223 syslog(LOG_ERR, "unable to register (NLM_PROG, NLM_VERSX, tcp)");
224 exit(1);
225 }
226 if (!svc_register(transp, NLM_PROG, NLM_VERS4, nlm_prog_4, IPPROTO_TCP)) {
227 syslog(LOG_ERR, "unable to register (NLM_PROG, NLM_VERS4, tcp)");
228 exit(1);
229 }
230
231 sigalarm.sa_handler = (sig_t) sigalarm_handler;
232 sigemptyset(&sigalarm.sa_mask);
233 sigalarm.sa_flags = SA_RESETHAND; /* should only happen once */
234 sigalarm.sa_flags |= SA_RESTART;
235 if (sigaction(SIGALRM, &sigalarm, NULL) != 0) {
236 syslog(LOG_WARNING, "sigaction(SIGALRM) failed: %s",
237 strerror(errno));
238 exit(1);
239 }
240 grace_expired = 0;
241 alarm(grace_period);
242
243 init_nsm();
244
245 client_pid = client_request();
246
247 /* raise our resource limits as far as they can go */
248 if (getrlimit(RLIMIT_NOFILE, &rlp)) {
249 syslog(LOG_WARNING, "getrlimit(RLIMIT_NOFILE) failed: %s",
250 strerror(errno));
251 } else {
252 rlp.rlim_cur = rlp.rlim_max;
253 if (setrlimit(RLIMIT_NOFILE, &rlp)) {
254 syslog(LOG_WARNING, "setrlimit(RLIMIT_NOFILE) failed: %s",
255 strerror(errno));
256 }
257 }
258
259 my_svc_run(); /* Should never return */
260 exit(1);
261 }
262
263 void
264 sigalarm_handler(void)
265 {
266
267 grace_expired = 1;
268 }
269
270 void
271 usage()
272 {
273 errx(1, "usage: rpc.lockd [-d <debuglevel>] [-g <grace period>] "
274 " [-x <statd cache timeout>] [-w]");
275 }
276
277 /*
278 * init_nsm --
279 * Reset the NSM state-of-the-world and acquire its state.
280 */
281 void
282 init_nsm(void)
283 {
284 enum clnt_stat ret;
285 my_id id;
286 sm_stat stat;
287 char name[] = "NFS NLM";
288 char localhost[] = "localhost";
289 int attempt = 0;
290
291 /*
292 * !!!
293 * The my_id structure isn't used by the SM_UNMON_ALL call, as far
294 * as I know. Leave it empty for now.
295 */
296 memset(&id, 0, sizeof(id));
297 id.my_name = name;
298
299 /*
300 * !!!
301 * The statd program must already be registered when lockd runs.
302 * If we have a problem contacting statd, pause and try again a
303 * number of times in case statd is just slow in coming up.
304 */
305 do {
306 ret = callrpc("localhost", SM_PROG, SM_VERS, SM_UNMON_ALL,
307 xdr_my_id, &id, xdr_sm_stat, &stat);
308 if (ret) {
309 syslog(LOG_WARNING, "%lu %s", SM_PROG, clnt_sperrno(ret));
310 if (++attempt < 20) {
311 sleep(attempt);
312 continue;
313 }
314 }
315 break;
316 } while (1);
317
318 if (ret != 0) {
319 syslog(LOG_ERR, "%lu %s", SM_PROG, clnt_sperrno(ret));
320 exit(1);
321 }
322
323 nsm_state = stat.state;
324
325 /* setup constant data for SM_MON calls */
326 mon_host.mon_id.my_id.my_name = localhost;
327 mon_host.mon_id.my_id.my_prog = NLM_PROG;
328 mon_host.mon_id.my_id.my_vers = NLM_SM;
329 mon_host.mon_id.my_id.my_proc = NLM_SM_NOTIFY; /* bsdi addition */
330 }
331
332 /*
333 * claim_pid_file
334 *
335 * Purpose: take ownership of and store pid in given pid_file
336 * Returns: 0 on success or -1 on failure
337 * Notes: force parameter requests that current owner (if any) of
338 * pid file be terminated.
339 */
340 int
341 claim_pid_file(const char *name, int force)
342 {
343 int pidfd, rv, retried = 0;
344 FILE *pidfile;
345
346 try_again:
347
348 /* attempt exclusive open of pid file */
349 pidfd = open(name, O_EXCL|O_CREAT|O_WRONLY,
350 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
351 if (pidfd < 0) {
352 char buf[16];
353 pid_t pid;
354 if (retried)
355 return -1;
356 bzero(buf, 16);
357 retried = 1;
358 /* pid file busy, check validity */
359 pidfd = open(name, O_RDONLY);
360 if (pidfd < 0)
361 goto try_again;
362 rv = read(pidfd, buf, 15);
363 close(pidfd);
364 if (rv <= 0)
365 goto try_again;
366 pid = atoi(buf);
367 if (pid <= 0)
368 goto try_again;
369 rv = kill(pid, force ? SIGKILL : 0);
370 /* if can't signal, assume stale pid file */
371 if ((rv < 0) || force)
372 unlink(name);
373 goto try_again;
374 }
375 pid_file = name;
376 atexit(cleanup_pid_file);
377
378 pidfile = fdopen(pidfd, "w");
379 if (pidfile) {
380 fchmod(pidfd, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
381 fprintf(pidfile, "%d\n", getpid());
382 fclose(pidfile);
383 } else
384 perror("fdopen");
385 close(pidfd);
386 return 0;
387 }
388
389 /*
390 * cleanup_pid_file
391 *
392 * Purpose: delete any pid_file that has been claimed
393 * Returns: Nothing
394 */
395 void
396 cleanup_pid_file(void)
397 {
398 if (pid_file) {
399 unlink(pid_file);
400 pid_file = NULL;
401 }
402 }
403
404 /*
405 * handle_sig_cleanup
406 *
407 * Purpose: on signal, kill client child and do pid file cleanup
408 * Returns: Nothing
409 */
410 void
411 handle_sig_cleanup(int sig __unused)
412 {
413 if (client_pid != -1)
414 kill(client_pid, SIGTERM);
415 cleanup_pid_file();
416 exit(1);
417 }
418
419 void
420 my_svc_run(void)
421 {
422 fd_set readfds;
423 struct timeval timeout;
424 struct timeval now;
425 int error;
426 int hashosts = 0;
427 int tsize = 0;
428 struct timeval *top;
429
430
431 for( ;; ) {
432 timeout.tv_sec = host_expire + 1;
433 timeout.tv_usec = 0;
434
435 tsize = getdtablesize();
436 bcopy(&svc_fdset, &readfds, sizeof(svc_fdset));
437 /*
438 * If there are any expired hosts then sleep with a
439 * timeout to expire them.
440 */
441 if (hashosts && (timeout.tv_sec >= 0))
442 top = &timeout;
443 else
444 top = NULL;
445 error = select(tsize, &readfds, NULL, NULL, top);
446 if (error == -1) {
447 if (errno == EINTR)
448 continue;
449 perror("rpc.lockd: my_svc_run: select failed");
450 return;
451 }
452 gettimeofday(&now, NULL);
453 currsec = now.tv_sec;
454 if (error > 0)
455 svc_getreqset(&readfds);
456 if (debug_level > 3 && error == 0)
457 fprintf(stderr, "my_svc_run: select timeout\n");
458 hashosts = expire_lock_hosts();
459 }
460 }