]>
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>
65 #include <rpc/pmap_clnt.h>
66 #include <rpcsvc/sm_inter.h>
69 #include <rpcsvc/nlm_prot.h>
71 int debug_level
= 0; /* 0 = no debugging syslog() calls */
73 int waitkern
= 0; /* 1 = wait for kernel to say start */
75 const char *pid_file
= NULL
;
79 pid_t client_pid
= -1;
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
*);
89 int claim_pid_file(const char *, int);
90 void cleanup_pid_file(void);
91 void handle_sig_cleanup(int);
93 void sigalarm_handler(void);
94 void my_svc_run(void);
96 const char *transports
[] = { "udp", "tcp", "udp6", "tcp6" };
105 struct sigaction sigalarm
;
106 int grace_period
= 30;
109 while ((ch
= getopt(argc
, argv
, "d:g:wx:")) != (-1)) {
112 debug_level
= atoi(optarg
);
119 grace_period
= atoi(optarg
);
129 host_expire
= atoi(optarg
);
137 if (geteuid()) { /* This command allowed only to root */
138 fprintf(stderr
, "Sorry. You are not superuser\n");
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.
146 if (debug_level
!= 99 && daemon(0, debug_level
> 0)) {
147 err(1, "cannot fork");
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
);
157 openlog("rpc.lockd", debug_level
== 99 ? LOG_PERROR
: 0, LOG_DAEMON
);
159 if (claim_pid_file("/var/run/lockd.pid", 0) < 0) {
160 syslog(LOG_ERR
, "cannot claim pid file");
166 /* wait for kernel to get first lock request */
168 /* start statd now, in case it isn't already */
170 /* sleep a little to give statd/portmap a chance to start */
171 /* (better to sleep 100ms than to timeout on portmap calls) */
173 ts
.tv_nsec
= 100*1000*1000;
174 nanosleep(&ts
, NULL
);
178 syslog(LOG_INFO
, "Starting, debug level %d", debug_level
);
180 syslog(LOG_INFO
, "Starting");
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
);
187 transp
= svcudp_create(RPC_ANYSOCK
);
188 if (transp
== NULL
) {
189 syslog(LOG_ERR
, "cannot create udp service");
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)");
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)");
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)");
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)");
209 transp
= svctcp_create(RPC_ANYSOCK
, 0, 0);
210 if (transp
== NULL
) {
211 syslog(LOG_ERR
, "cannot create tcp service");
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)");
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)");
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)");
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)");
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",
245 client_pid
= client_request();
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",
252 rlp
.rlim_cur
= rlp
.rlim_max
;
253 if (setrlimit(RLIMIT_NOFILE
, &rlp
)) {
254 syslog(LOG_WARNING
, "setrlimit(RLIMIT_NOFILE) failed: %s",
259 my_svc_run(); /* Should never return */
264 sigalarm_handler(void)
273 errx(1, "usage: rpc.lockd [-d <debuglevel>] [-g <grace period>] "
274 " [-x <statd cache timeout>] [-w]");
279 * Reset the NSM state-of-the-world and acquire its state.
287 char name
[] = "NFS NLM";
288 char localhost
[] = "localhost";
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.
296 memset(&id
, 0, sizeof(id
));
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.
306 ret
= callrpc("localhost", SM_PROG
, SM_VERS
, SM_UNMON_ALL
,
307 xdr_my_id
, &id
, xdr_sm_stat
, &stat
);
309 syslog(LOG_WARNING
, "%lu %s", SM_PROG
, clnt_sperrno(ret
));
310 if (++attempt
< 20) {
319 syslog(LOG_ERR
, "%lu %s", SM_PROG
, clnt_sperrno(ret
));
323 nsm_state
= stat
.state
;
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 */
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.
341 claim_pid_file(const char *name
, int force
)
343 int pidfd
, rv
, retried
= 0;
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
);
358 /* pid file busy, check validity */
359 pidfd
= open(name
, O_RDONLY
);
362 rv
= read(pidfd
, buf
, 15);
369 rv
= kill(pid
, force
? SIGKILL
: 0);
370 /* if can't signal, assume stale pid file */
371 if ((rv
< 0) || force
)
376 atexit(cleanup_pid_file
);
378 pidfile
= fdopen(pidfd
, "w");
380 fchmod(pidfd
, S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IROTH
);
381 fprintf(pidfile
, "%d\n", getpid());
392 * Purpose: delete any pid_file that has been claimed
396 cleanup_pid_file(void)
407 * Purpose: on signal, kill client child and do pid file cleanup
411 handle_sig_cleanup(int sig __unused
)
413 if (client_pid
!= -1)
414 kill(client_pid
, SIGTERM
);
423 struct timeval timeout
;
432 timeout
.tv_sec
= host_expire
+ 1;
435 tsize
= getdtablesize();
436 bcopy(&svc_fdset
, &readfds
, sizeof(svc_fdset
));
438 * If there are any expired hosts then sleep with a
439 * timeout to expire them.
441 if (hashosts
&& (timeout
.tv_sec
>= 0))
445 error
= select(tsize
, &readfds
, NULL
, NULL
, top
);
449 perror("rpc.lockd: my_svc_run: select failed");
452 gettimeofday(&now
, NULL
);
453 currsec
= now
.tv_sec
;
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();