]> git.saurik.com Git - apple/network_cmds.git/blob - rpc_lockd.tproj/lockd.c
f8e46db5ef62334797bbba5118d03e98c419a8d2
[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
63 #include <rpc/rpc.h>
64 #include <rpc/pmap_clnt.h>
65 #include <rpcsvc/sm_inter.h>
66
67 #include "lockd.h"
68 #include <rpcsvc/nlm_prot.h>
69
70 int debug_level = 0; /* 0 = no debugging syslog() calls */
71 int _rpcsvcdirty = 0;
72 int waitkern = 0; /* 1 = wait for kernel to say start */
73
74 const char *pid_file = NULL;
75
76 int grace_expired;
77 int nsm_state;
78 pid_t client_pid = -1;
79 struct mon mon_host;
80
81 void init_nsm(void);
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 *);
86 void usage(void);
87
88 int claim_pid_file(const char *, int);
89 void cleanup_pid_file(void);
90 void handle_sig_cleanup(int);
91
92 void sigalarm_handler(void);
93
94 const char *transports[] = { "udp", "tcp", "udp6", "tcp6" };
95
96 int
97 main(argc, argv)
98 int argc;
99 char **argv;
100 {
101 SVCXPRT *transp;
102 int ch;
103 struct sigaction sigalarm;
104 int grace_period = 30;
105
106 while ((ch = getopt(argc, argv, "d:g:w")) != (-1)) {
107 switch (ch) {
108 case 'd':
109 debug_level = atoi(optarg);
110 if (!debug_level) {
111 usage();
112 /* NOTREACHED */
113 }
114 break;
115 case 'g':
116 grace_period = atoi(optarg);
117 if (!grace_period) {
118 usage();
119 /* NOTREACHED */
120 }
121 break;
122 case 'w':
123 waitkern = 1;
124 break;
125 default:
126 case '?':
127 usage();
128 /* NOTREACHED */
129 }
130 }
131 if (geteuid()) { /* This command allowed only to root */
132 fprintf(stderr, "Sorry. You are not superuser\n");
133 exit(1);
134 }
135
136 /*
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.
139 */
140 if (debug_level != 99 && daemon(0, 0)) {
141 err(1, "cannot fork");
142 /* NOTREACHED */
143 }
144
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);
150
151 if (claim_pid_file("/var/run/lockd.pid", 0) < 0)
152 errx(1, "cannot claim pid file");
153
154 if (waitkern) {
155 struct timespec ts;
156 /* wait for kernel to get first lock request */
157 client_kern_wait();
158 /* start statd now, in case it isn't already */
159 system("rpc.statd");
160 /* sleep a little to give statd/portmap a chance to start */
161 /* (better to sleep 100ms than to timeout on portmap calls) */
162 ts.tv_sec = 0;
163 ts.tv_nsec = 100*1000*1000;
164 nanosleep(&ts, NULL);
165 }
166
167 openlog("rpc.lockd", 0, LOG_DAEMON);
168 if (debug_level)
169 syslog(LOG_INFO, "Starting, debug level %d", debug_level);
170 else
171 syslog(LOG_INFO, "Starting");
172
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);
177
178 transp = svcudp_create(RPC_ANYSOCK);
179 if (transp == NULL)
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)");
187
188 transp = svctcp_create(RPC_ANYSOCK, 0, 0);
189 if (transp == NULL)
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)");
197
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",
204 strerror(errno));
205 exit(1);
206 }
207 grace_expired = 0;
208 alarm(grace_period);
209
210 init_nsm();
211
212 client_pid = client_request();
213
214 svc_run(); /* Should never return */
215 exit(1);
216 }
217
218 void
219 sigalarm_handler(void)
220 {
221
222 grace_expired = 1;
223 }
224
225 void
226 usage()
227 {
228 errx(1, "usage: rpc.lockd [-d <debuglevel>] [-g <grace period>] [-w]");
229 }
230
231 /*
232 * init_nsm --
233 * Reset the NSM state-of-the-world and acquire its state.
234 */
235 void
236 init_nsm(void)
237 {
238 enum clnt_stat ret;
239 my_id id;
240 sm_stat stat;
241 char name[] = "NFS NLM";
242 char localhost[] = "localhost";
243 int attempt = 0;
244
245 /*
246 * !!!
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.
249 */
250 memset(&id, 0, sizeof(id));
251 id.my_name = name;
252
253 /*
254 * !!!
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.
258 */
259 do {
260 ret = callrpc("localhost", SM_PROG, SM_VERS, SM_UNMON_ALL,
261 xdr_my_id, &id, xdr_sm_stat, &stat);
262 if (ret) {
263 warnx("%lu %s", SM_PROG, clnt_sperrno(ret));
264 if (++attempt < 20) {
265 sleep(attempt);
266 continue;
267 }
268 }
269 break;
270 } while (1);
271
272 if (ret != 0) {
273 errx(1, "%lu %s", SM_PROG, clnt_sperrno(ret));
274 }
275
276 nsm_state = stat.state;
277
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 */
283 }
284
285 /*
286 * claim_pid_file
287 *
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.
292 */
293 int
294 claim_pid_file(const char *name, int force)
295 {
296 int pidfd, rv, retried = 0;
297 FILE *pidfile;
298
299 try_again:
300
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);
304 if (pidfd < 0) {
305 char buf[16];
306 pid_t pid;
307 if (retried)
308 return -1;
309 bzero(buf, 16);
310 retried = 1;
311 /* pid file busy, check validity */
312 pidfd = open(name, O_RDONLY);
313 if (pidfd < 0)
314 goto try_again;
315 rv = read(pidfd, buf, 15);
316 close(pidfd);
317 if (rv <= 0)
318 goto try_again;
319 pid = atoi(buf);
320 if (pid <= 0)
321 goto try_again;
322 rv = kill(pid, force ? SIGKILL : 0);
323 /* if can't signal, assume stale pid file */
324 if ((rv < 0) || force)
325 unlink(name);
326 goto try_again;
327 }
328 pid_file = name;
329 atexit(cleanup_pid_file);
330
331 pidfile = fdopen(pidfd, "w");
332 if (pidfile) {
333 fchmod(pidfd, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
334 fprintf(pidfile, "%d\n", getpid());
335 fclose(pidfile);
336 } else
337 perror("fdopen");
338 close(pidfd);
339 return 0;
340 }
341
342 /*
343 * cleanup_pid_file
344 *
345 * Purpose: delete any pid_file that has been claimed
346 * Returns: Nothing
347 */
348 void
349 cleanup_pid_file(void)
350 {
351 if (pid_file) {
352 unlink(pid_file);
353 pid_file = NULL;
354 }
355 }
356
357 /*
358 * handle_sig_cleanup
359 *
360 * Purpose: on signal, kill client child and do pid file cleanup
361 * Returns: Nothing
362 */
363 void
364 handle_sig_cleanup(int sig __unused)
365 {
366 if (client_pid != -1)
367 kill(client_pid, SIGTERM);
368 cleanup_pid_file();
369 exit(1);
370 }
371