]>
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>
64 #include <rpc/pmap_clnt.h>
65 #include <rpcsvc/sm_inter.h>
68 #include <rpcsvc/nlm_prot.h>
70 int debug_level
= 0; /* 0 = no debugging syslog() calls */
72 int waitkern
= 0; /* 1 = wait for kernel to say start */
74 const char *pid_file
= NULL
;
78 pid_t client_pid
= -1;
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
*);
88 int claim_pid_file(const char *, int);
89 void cleanup_pid_file(void);
90 void handle_sig_cleanup(int);
92 void sigalarm_handler(void);
93 void my_svc_run(void);
95 const char *transports
[] = { "udp", "tcp", "udp6", "tcp6" };
104 struct sigaction sigalarm
;
105 int grace_period
= 30;
107 while ((ch
= getopt(argc
, argv
, "d:g:wx:")) != (-1)) {
110 debug_level
= atoi(optarg
);
117 grace_period
= atoi(optarg
);
127 host_expire
= atoi(optarg
);
135 if (geteuid()) { /* This command allowed only to root */
136 fprintf(stderr
, "Sorry. You are not superuser\n");
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.
144 if (debug_level
!= 99 && daemon(0, 0)) {
145 err(1, "cannot fork");
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
);
155 if (claim_pid_file("/var/run/lockd.pid", 0) < 0)
156 errx(1, "cannot claim pid file");
160 /* wait for kernel to get first lock request */
162 /* start statd now, in case it isn't already */
164 /* sleep a little to give statd/portmap a chance to start */
165 /* (better to sleep 100ms than to timeout on portmap calls) */
167 ts
.tv_nsec
= 100*1000*1000;
168 nanosleep(&ts
, NULL
);
171 openlog("rpc.lockd", debug_level
== 99 ? LOG_PERROR
: 0, LOG_DAEMON
);
173 syslog(LOG_INFO
, "Starting, debug level %d", debug_level
);
175 syslog(LOG_INFO
, "Starting");
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
);
182 transp
= svcudp_create(RPC_ANYSOCK
);
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)");
192 transp
= svctcp_create(RPC_ANYSOCK
, 0, 0);
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)");
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",
216 client_pid
= client_request();
218 my_svc_run(); /* Should never return */
223 sigalarm_handler(void)
232 errx(1, "usage: rpc.lockd [-d <debuglevel>] [-g <grace period>] "
233 " [-x <statd cache timeout>] [-w]");
238 * Reset the NSM state-of-the-world and acquire its state.
246 char name
[] = "NFS NLM";
247 char localhost
[] = "localhost";
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.
255 memset(&id
, 0, sizeof(id
));
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.
265 ret
= callrpc("localhost", SM_PROG
, SM_VERS
, SM_UNMON_ALL
,
266 xdr_my_id
, &id
, xdr_sm_stat
, &stat
);
268 warnx("%lu %s", SM_PROG
, clnt_sperrno(ret
));
269 if (++attempt
< 20) {
278 errx(1, "%lu %s", SM_PROG
, clnt_sperrno(ret
));
281 nsm_state
= stat
.state
;
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 */
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.
299 claim_pid_file(const char *name
, int force
)
301 int pidfd
, rv
, retried
= 0;
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
);
316 /* pid file busy, check validity */
317 pidfd
= open(name
, O_RDONLY
);
320 rv
= read(pidfd
, buf
, 15);
327 rv
= kill(pid
, force
? SIGKILL
: 0);
328 /* if can't signal, assume stale pid file */
329 if ((rv
< 0) || force
)
334 atexit(cleanup_pid_file
);
336 pidfile
= fdopen(pidfd
, "w");
338 fchmod(pidfd
, S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IROTH
);
339 fprintf(pidfile
, "%d\n", getpid());
350 * Purpose: delete any pid_file that has been claimed
354 cleanup_pid_file(void)
365 * Purpose: on signal, kill client child and do pid file cleanup
369 handle_sig_cleanup(int sig __unused
)
371 if (client_pid
!= -1)
372 kill(client_pid
, SIGTERM
);
381 struct timeval timeout
;
390 timeout
.tv_sec
= host_expire
+ 1;
393 tsize
= getdtablesize();
394 bcopy(&svc_fdset
, &readfds
, sizeof(svc_fdset
));
396 * If there are any expired hosts then sleep with a
397 * timeout to expire them.
399 if (hashosts
&& (timeout
.tv_sec
>= 0))
403 error
= select(tsize
, &readfds
, NULL
, NULL
, top
);
407 perror("rpc.lockd: my_svc_run: select failed");
410 gettimeofday(&now
, NULL
);
411 currsec
= now
.tv_sec
;
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();