]>
git.saurik.com Git - apple/network_cmds.git/blob - rpc_lockd.tproj/lockd.c
f8e46db5ef62334797bbba5118d03e98c419a8d2
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);
94 const char *transports
[] = { "udp", "tcp", "udp6", "tcp6" };
103 struct sigaction sigalarm
;
104 int grace_period
= 30;
106 while ((ch
= getopt(argc
, argv
, "d:g:w")) != (-1)) {
109 debug_level
= atoi(optarg
);
116 grace_period
= atoi(optarg
);
131 if (geteuid()) { /* This command allowed only to root */
132 fprintf(stderr
, "Sorry. You are not superuser\n");
137 * Note that it is NOT sensible to run this program from inetd - the
138 * protocol assumes that it will run immediately at boot time.
140 if (debug_level
!= 99 && daemon(0, 0)) {
141 err(1, "cannot fork");
145 /* Install signal handler to remove any pid file */
146 signal(SIGINT
, handle_sig_cleanup
);
147 signal(SIGTERM
, handle_sig_cleanup
);
148 signal(SIGHUP
, handle_sig_cleanup
);
149 signal(SIGQUIT
, handle_sig_cleanup
);
151 if (claim_pid_file("/var/run/lockd.pid", 0) < 0)
152 errx(1, "cannot claim pid file");
156 /* wait for kernel to get first lock request */
158 /* start statd now, in case it isn't already */
160 /* sleep a little to give statd/portmap a chance to start */
161 /* (better to sleep 100ms than to timeout on portmap calls) */
163 ts
.tv_nsec
= 100*1000*1000;
164 nanosleep(&ts
, NULL
);
167 openlog("rpc.lockd", 0, LOG_DAEMON
);
169 syslog(LOG_INFO
, "Starting, debug level %d", debug_level
);
171 syslog(LOG_INFO
, "Starting");
173 (void)pmap_unset(NLM_PROG
, NLM_SM
);
174 (void)pmap_unset(NLM_PROG
, NLM_VERS
);
175 (void)pmap_unset(NLM_PROG
, NLM_VERSX
);
176 (void)pmap_unset(NLM_PROG
, NLM_VERS4
);
178 transp
= svcudp_create(RPC_ANYSOCK
);
180 errx(1, "cannot create udp service");
181 if (!svc_register(transp
, NLM_PROG
, NLM_VERS
, nlm_prog_1
, IPPROTO_UDP
))
182 errx(1, "unable to register (NLM_PROG, NLM_VERS, udp)");
183 if (!svc_register(transp
, NLM_PROG
, NLM_VERSX
, nlm_prog_3
, IPPROTO_UDP
))
184 errx(1, "unable to register (NLM_PROG, NLM_VERSX, udp)");
185 if (!svc_register(transp
, NLM_PROG
, NLM_VERS4
, nlm_prog_4
, IPPROTO_UDP
))
186 errx(1, "unable to register (NLM_PROG, NLM_VERS4, udp)");
188 transp
= svctcp_create(RPC_ANYSOCK
, 0, 0);
190 errx(1, "cannot create tcp service");
191 if (!svc_register(transp
, NLM_PROG
, NLM_VERS
, nlm_prog_1
, IPPROTO_TCP
))
192 errx(1, "unable to register (NLM_PROG, NLM_VERS, tcp)");
193 if (!svc_register(transp
, NLM_PROG
, NLM_VERSX
, nlm_prog_3
, IPPROTO_TCP
))
194 errx(1, "unable to register (NLM_PROG, NLM_VERSX, tcp)");
195 if (!svc_register(transp
, NLM_PROG
, NLM_VERS4
, nlm_prog_4
, IPPROTO_TCP
))
196 errx(1, "unable to register (NLM_PROG, NLM_VERS4, tcp)");
198 sigalarm
.sa_handler
= (sig_t
) sigalarm_handler
;
199 sigemptyset(&sigalarm
.sa_mask
);
200 sigalarm
.sa_flags
= SA_RESETHAND
; /* should only happen once */
201 sigalarm
.sa_flags
|= SA_RESTART
;
202 if (sigaction(SIGALRM
, &sigalarm
, NULL
) != 0) {
203 syslog(LOG_WARNING
, "sigaction(SIGALRM) failed: %s",
212 client_pid
= client_request();
214 svc_run(); /* Should never return */
219 sigalarm_handler(void)
228 errx(1, "usage: rpc.lockd [-d <debuglevel>] [-g <grace period>] [-w]");
233 * Reset the NSM state-of-the-world and acquire its state.
241 char name
[] = "NFS NLM";
242 char localhost
[] = "localhost";
247 * The my_id structure isn't used by the SM_UNMON_ALL call, as far
248 * as I know. Leave it empty for now.
250 memset(&id
, 0, sizeof(id
));
255 * The statd program must already be registered when lockd runs.
256 * If we have a problem contacting statd, pause and try again a
257 * number of times in case statd is just slow in coming up.
260 ret
= callrpc("localhost", SM_PROG
, SM_VERS
, SM_UNMON_ALL
,
261 xdr_my_id
, &id
, xdr_sm_stat
, &stat
);
263 warnx("%lu %s", SM_PROG
, clnt_sperrno(ret
));
264 if (++attempt
< 20) {
273 errx(1, "%lu %s", SM_PROG
, clnt_sperrno(ret
));
276 nsm_state
= stat
.state
;
278 /* setup constant data for SM_MON calls */
279 mon_host
.mon_id
.my_id
.my_name
= localhost
;
280 mon_host
.mon_id
.my_id
.my_prog
= NLM_PROG
;
281 mon_host
.mon_id
.my_id
.my_vers
= NLM_SM
;
282 mon_host
.mon_id
.my_id
.my_proc
= NLM_SM_NOTIFY
; /* bsdi addition */
288 * Purpose: take ownership of and store pid in given pid_file
289 * Returns: 0 on success or -1 on failure
290 * Notes: force parameter requests that current owner (if any) of
291 * pid file be terminated.
294 claim_pid_file(const char *name
, int force
)
296 int pidfd
, rv
, retried
= 0;
301 /* attempt exclusive open of pid file */
302 pidfd
= open(name
, O_EXCL
|O_CREAT
|O_WRONLY
,
303 S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IROTH
);
311 /* pid file busy, check validity */
312 pidfd
= open(name
, O_RDONLY
);
315 rv
= read(pidfd
, buf
, 15);
322 rv
= kill(pid
, force
? SIGKILL
: 0);
323 /* if can't signal, assume stale pid file */
324 if ((rv
< 0) || force
)
329 atexit(cleanup_pid_file
);
331 pidfile
= fdopen(pidfd
, "w");
333 fchmod(pidfd
, S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IROTH
);
334 fprintf(pidfile
, "%d\n", getpid());
345 * Purpose: delete any pid_file that has been claimed
349 cleanup_pid_file(void)
360 * Purpose: on signal, kill client child and do pid file cleanup
364 handle_sig_cleanup(int sig __unused
)
366 if (client_pid
!= -1)
367 kill(client_pid
, SIGTERM
);