]>
git.saurik.com Git - apple/network_cmds.git/blob - 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 $ */
6 * A.R. Gordon (andrew.gordon@net-tel.co.uk). All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
37 #include <sys/cdefs.h>
39 __RCSID("$NetBSD: lockd.c,v 1.7 2000/08/12 18:08:44 thorpej Exp $");
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.
46 * The actual program logic is in the file lock_proc.c
49 #include <sys/types.h>
50 #include <sys/socket.h>
62 #include <sys/resource.h>
63 #include <sys/sysctl.h>
66 #include <rpc/pmap_clnt.h>
67 #include <rpcsvc/sm_inter.h>
70 #include <rpcsvc/nlm_prot.h>
72 int debug_level
= 0; /* 0 = no debugging syslog() calls */
74 int waitkern
= 0; /* 1 = wait for kernel to say start */
76 const char *pid_file
= NULL
;
80 pid_t client_pid
= -1;
84 void nlm_prog_0(struct svc_req
*, SVCXPRT
*);
85 void nlm_prog_1(struct svc_req
*, SVCXPRT
*);
86 void nlm_prog_3(struct svc_req
*, SVCXPRT
*);
87 void nlm_prog_4(struct svc_req
*, SVCXPRT
*);
90 int claim_pid_file(const char *, int);
91 void cleanup_pid_file(void);
92 void handle_sig_cleanup(int);
94 void sigalarm_handler(void);
95 void my_svc_run(void);
97 const char *transports
[] = { "udp", "tcp", "udp6", "tcp6" };
106 struct sigaction sigalarm
;
107 int grace_period
= 30;
114 while ((ch
= getopt(argc
, argv
, "d:g:wx:")) != (-1)) {
117 debug_level
= atoi(optarg
);
124 grace_period
= atoi(optarg
);
134 host_expire
= atoi(optarg
);
142 if (geteuid()) { /* This command allowed only to root */
143 fprintf(stderr
, "Sorry. You are not superuser\n");
148 * Note that it is NOT sensible to run this program from inetd - the
149 * protocol assumes that it will run immediately at boot time.
151 if (debug_level
!= 99 && daemon(0, debug_level
> 0)) {
152 err(1, "cannot fork");
156 /* Install signal handler to remove any pid file */
157 signal(SIGINT
, handle_sig_cleanup
);
158 signal(SIGTERM
, handle_sig_cleanup
);
159 signal(SIGHUP
, handle_sig_cleanup
);
160 signal(SIGQUIT
, handle_sig_cleanup
);
164 openlog("rpc.lockd", debug_level
== 99 ? LOG_PERROR
: 0, LOG_DAEMON
);
167 mib
[1] = KERN_PROCDELAYTERM
;
173 if (sysctl(mib
, 2, &oldstate
, &oldsize
, &newstate
, 4) < 0) {
174 syslog(LOG_INFO
, "cannot mark pid for delayed termination");
177 if (claim_pid_file("/var/run/lockd.pid", 0) < 0) {
178 syslog(LOG_ERR
, "cannot claim pid file");
184 /* wait for kernel to get first lock request */
186 /* start statd now, in case it isn't already */
188 /* sleep a little to give statd/portmap a chance to start */
189 /* (better to sleep 100ms than to timeout on portmap calls) */
191 ts
.tv_nsec
= 100*1000*1000;
192 nanosleep(&ts
, NULL
);
196 syslog(LOG_INFO
, "Starting, debug level %d", debug_level
);
198 syslog(LOG_INFO
, "Starting");
200 (void)pmap_unset(NLM_PROG
, NLM_SM
);
201 (void)pmap_unset(NLM_PROG
, NLM_VERS
);
202 (void)pmap_unset(NLM_PROG
, NLM_VERSX
);
203 (void)pmap_unset(NLM_PROG
, NLM_VERS4
);
205 transp
= svcudp_create(RPC_ANYSOCK
);
206 if (transp
== NULL
) {
207 syslog(LOG_ERR
, "cannot create udp service");
210 if (!svc_register(transp
, NLM_PROG
, NLM_SM
, nlm_prog_0
, IPPROTO_UDP
)) {
211 syslog(LOG_ERR
, "unable to register (NLM_PROG, NLM_SM, udp)");
214 if (!svc_register(transp
, NLM_PROG
, NLM_VERS
, nlm_prog_1
, IPPROTO_UDP
)) {
215 syslog(LOG_ERR
, "unable to register (NLM_PROG, NLM_VERS, udp)");
218 if (!svc_register(transp
, NLM_PROG
, NLM_VERSX
, nlm_prog_3
, IPPROTO_UDP
)) {
219 syslog(LOG_ERR
, "unable to register (NLM_PROG, NLM_VERSX, udp)");
222 if (!svc_register(transp
, NLM_PROG
, NLM_VERS4
, nlm_prog_4
, IPPROTO_UDP
)) {
223 syslog(LOG_ERR
, "unable to register (NLM_PROG, NLM_VERS4, udp)");
227 transp
= svctcp_create(RPC_ANYSOCK
, 0, 0);
228 if (transp
== NULL
) {
229 syslog(LOG_ERR
, "cannot create tcp service");
232 if (!svc_register(transp
, NLM_PROG
, NLM_SM
, nlm_prog_0
, IPPROTO_TCP
)) {
233 syslog(LOG_ERR
, "unable to register (NLM_PROG, NLM_SM, tcp)");
236 if (!svc_register(transp
, NLM_PROG
, NLM_VERS
, nlm_prog_1
, IPPROTO_TCP
)) {
237 syslog(LOG_ERR
, "unable to register (NLM_PROG, NLM_VERS, tcp)");
240 if (!svc_register(transp
, NLM_PROG
, NLM_VERSX
, nlm_prog_3
, IPPROTO_TCP
)) {
241 syslog(LOG_ERR
, "unable to register (NLM_PROG, NLM_VERSX, tcp)");
244 if (!svc_register(transp
, NLM_PROG
, NLM_VERS4
, nlm_prog_4
, IPPROTO_TCP
)) {
245 syslog(LOG_ERR
, "unable to register (NLM_PROG, NLM_VERS4, tcp)");
249 sigalarm
.sa_handler
= (sig_t
) sigalarm_handler
;
250 sigemptyset(&sigalarm
.sa_mask
);
251 sigalarm
.sa_flags
= SA_RESETHAND
; /* should only happen once */
252 sigalarm
.sa_flags
|= SA_RESTART
;
253 if (sigaction(SIGALRM
, &sigalarm
, NULL
) != 0) {
254 syslog(LOG_WARNING
, "sigaction(SIGALRM) failed: %s",
263 client_pid
= client_request();
265 /* raise our resource limits as far as they can go */
266 if (getrlimit(RLIMIT_NOFILE
, &rlp
)) {
267 syslog(LOG_WARNING
, "getrlimit(RLIMIT_NOFILE) failed: %s",
270 rlp
.rlim_cur
= rlp
.rlim_max
;
271 if (setrlimit(RLIMIT_NOFILE
, &rlp
)) {
272 syslog(LOG_WARNING
, "setrlimit(RLIMIT_NOFILE) failed: %s",
277 my_svc_run(); /* Should never return */
282 sigalarm_handler(void)
291 errx(1, "usage: rpc.lockd [-d <debuglevel>] [-g <grace period>] "
292 " [-x <statd cache timeout>] [-w]");
297 * Reset the NSM state-of-the-world and acquire its state.
305 char name
[] = "NFS NLM";
306 char localhost
[] = "localhost";
311 * The my_id structure isn't used by the SM_UNMON_ALL call, as far
312 * as I know. Leave it empty for now.
314 memset(&id
, 0, sizeof(id
));
319 * The statd program must already be registered when lockd runs.
320 * If we have a problem contacting statd, pause and try again a
321 * number of times in case statd is just slow in coming up.
324 ret
= callrpc("localhost", SM_PROG
, SM_VERS
, SM_UNMON_ALL
,
325 xdr_my_id
, &id
, xdr_sm_stat
, &stat
);
327 syslog(LOG_WARNING
, "%lu %s", SM_PROG
, clnt_sperrno(ret
));
328 if (++attempt
< 20) {
337 syslog(LOG_ERR
, "%lu %s", SM_PROG
, clnt_sperrno(ret
));
341 nsm_state
= stat
.state
;
343 /* setup constant data for SM_MON calls */
344 mon_host
.mon_id
.my_id
.my_name
= localhost
;
345 mon_host
.mon_id
.my_id
.my_prog
= NLM_PROG
;
346 mon_host
.mon_id
.my_id
.my_vers
= NLM_SM
;
347 mon_host
.mon_id
.my_id
.my_proc
= NLM_SM_NOTIFY
; /* bsdi addition */
353 * Purpose: take ownership of and store pid in given pid_file
354 * Returns: 0 on success or -1 on failure
355 * Notes: force parameter requests that current owner (if any) of
356 * pid file be terminated.
359 claim_pid_file(const char *name
, int force
)
361 int pidfd
, rv
, retried
= 0;
366 /* attempt exclusive open of pid file */
367 pidfd
= open(name
, O_EXCL
|O_CREAT
|O_WRONLY
,
368 S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IROTH
);
376 /* pid file busy, check validity */
377 pidfd
= open(name
, O_RDONLY
);
380 rv
= read(pidfd
, buf
, 15);
387 rv
= kill(pid
, force
? SIGKILL
: 0);
388 /* if can't signal, assume stale pid file */
389 if ((rv
< 0) || force
)
394 atexit(cleanup_pid_file
);
396 pidfile
= fdopen(pidfd
, "w");
398 fchmod(pidfd
, S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IROTH
);
399 fprintf(pidfile
, "%d\n", getpid());
410 * Purpose: delete any pid_file that has been claimed
414 cleanup_pid_file(void)
425 * Purpose: on signal, kill client child and do pid file cleanup
429 handle_sig_cleanup(int sig __unused
)
431 if (client_pid
!= -1)
432 kill(client_pid
, SIGTERM
);
441 struct timeval timeout
;
450 timeout
.tv_sec
= host_expire
+ 1;
453 tsize
= getdtablesize();
454 bcopy(&svc_fdset
, &readfds
, sizeof(svc_fdset
));
456 * If there are any expired hosts then sleep with a
457 * timeout to expire them.
459 if (hashosts
&& (timeout
.tv_sec
>= 0))
463 error
= select(tsize
, &readfds
, NULL
, NULL
, top
);
467 perror("rpc.lockd: my_svc_run: select failed");
470 gettimeofday(&now
, NULL
);
471 currsec
= now
.tv_sec
;
473 svc_getreqset(&readfds
);
474 if (debug_level
> 3 && error
== 0)
475 fprintf(stderr
, "my_svc_run: select timeout\n");
476 hashosts
= expire_lock_hosts();