]>
git.saurik.com Git - apple/network_cmds.git/blob - rpc_lockd.tproj/kern.c
cd4213dab84124a56031ca1379a209baed243a7e
2 * Copyright (c) 1997 Berkeley Software Design, Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. Berkeley Software Design Inc's name may not be used to endorse or
13 * promote products derived from this software without specific prior
16 * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * from BSDI kern.c,v 1.2 1998/11/25 22:38:27 don Exp
29 * $FreeBSD: src/usr.sbin/rpc.lockd/kern.c,v 1.11 2002/08/15 21:52:21 alfred Exp $
32 #include <sys/param.h>
33 #include <sys/mount.h>
34 #include <sys/queue.h>
35 #include <sys/socket.h>
38 #include <netinet/in.h>
39 #include <arpa/inet.h>
52 #include "rpcsvc/nlm_prot.h"
53 #include <nfs/rpcv2.h>
54 #include <nfs/nfsproto.h>
55 #include <nfs/nfs_lock.h>
59 #include "lockd_lock.h"
61 #define DAEMON_USERNAME "daemon"
63 #define nfslockdans(_v, _ansp) \
64 ((_ansp)->la_vers = (_v), \
65 nfsclnt(NFSCLNT_LOCKDANS, (_ansp)))
68 /* Lock request owner. */
69 typedef struct __owner
{
70 pid_t pid
; /* Process ID. */
71 time_t tod
; /* Time-of-day. */
75 static char hostname
[MAXHOSTNAMELEN
+ 1]; /* Hostname. */
77 static void client_cleanup(void);
78 static void set_auth(CLIENT
*cl
, struct xucred
*ucred
);
79 int lock_request(LOCKD_MSG
*);
80 int test_request(LOCKD_MSG
*);
81 void show(LOCKD_MSG
*);
82 int unlock_request(LOCKD_MSG
*);
85 * will break because fifo needs to be repopened when EOF'd
87 #define lockd_seteuid(uid) seteuid(uid)
89 #define d_calls (debug_level > 1)
90 #define d_args (debug_level > 2)
94 struct sockaddr
*saddr
;
96 static char inet_buf
[INET6_ADDRSTRLEN
];
98 if (getnameinfo(saddr
, saddr
->sa_len
, inet_buf
, sizeof(inet_buf
),
99 NULL
, 0, NI_NUMERICHOST
) == 0)
107 * wait for kernel to signal first lock request before starting
110 client_kern_wait(void)
112 if (nfsclnt(NFSCLNT_LOCKDWAIT
, NULL
))
113 warn("nfsclnt_lockdwait");
119 (void)lockd_seteuid(0);
120 (void) nfsclnt(NFSCLNT_LOCKDFD
, (struct lockd_ans
*)-1);
126 * Loop around messages from the kernel, forwarding them off to
134 * select on FIFOs can currently hang, so we'll use
135 * the nfslockdwait syscall instead for now.
137 #define USE_NFSLOCKDWAIT_INSTEAD_OF_SELECT 1
138 #ifndef USE_NFSLOCKDWAIT_INSTEAD_OF_SELECT
147 /* Recreate the NLM fifo. */
148 (void)unlink(_PATH_LCKFIFO
);
149 old_umask
= umask(S_IXGRP
|S_IXOTH
);
150 if (mkfifo(_PATH_LCKFIFO
, S_IWUSR
| S_IRUSR
)) {
151 syslog(LOG_ERR
, "mkfifo: %s: %m", _PATH_LCKFIFO
);
157 * Create a separate process, the client code is really a separate
158 * daemon that shares a lot of code.
160 switch (child
= fork()) {
169 signal(SIGHUP
, (sig_t
)client_cleanup
);
170 signal(SIGTERM
, (sig_t
)client_cleanup
);
173 (void)time(&owner
.tod
);
174 owner
.pid
= getpid();
175 (void)gethostname(hostname
, sizeof(hostname
) - 1);
177 /* Open the fifo for reading. */
178 if ((fd
= open(_PATH_LCKFIFO
, O_RDONLY
| O_NONBLOCK
)) == -1) {
179 syslog(LOG_ERR
, "open: %s: %m", _PATH_LCKFIFO
);
182 (void)unlink(_PATH_LCKFIFO
);
183 if (nfsclnt(NFSCLNT_LOCKDFD
, (struct lockd_ans
*)fd
)) {
184 syslog(LOG_ERR
, "nfsclnt_fd: %d: %m", fd
);
187 pw
= getpwnam(DAEMON_USERNAME
);
189 syslog(LOG_ERR
, "getpwnam: %s: %m", DAEMON_USERNAME
);
192 daemon_uid
= pw
->pw_uid
;
193 /* drop our root priviledges */
194 (void)lockd_seteuid(daemon_uid
);
197 #ifndef USE_NFSLOCKDWAIT_INSTEAD_OF_SELECT
198 /* Wait for contact... fifo's return EAGAIN when read with
201 /* Set up the select. */
204 (void)select(fd
+ 1, &rdset
, NULL
, NULL
, NULL
);
207 /* Read the fixed length message. */
208 if ((nr
= read(fd
, &msg
, sizeof(msg
))) == sizeof(msg
)) {
212 if (msg
.lm_version
!= LOCKD_MSG_VERSION
) {
214 "unknown msg type: %d", msg
.lm_version
);
217 * Send it to the NLM server and don't grant the lock
218 * if we fail for any reason.
220 switch (msg
.lm_fl
.l_type
) {
224 ret
= test_request(&msg
);
226 ret
= lock_request(&msg
);
229 ret
= unlock_request(&msg
);
234 "unknown lock type: %d", msg
.lm_fl
.l_type
);
238 struct lockd_ans ans
;
240 ans
.la_msg_ident
= msg
.lm_msg_ident
;
241 ans
.la_errno
= ENOTSUP
;
243 if (nfslockdans(LOCKD_ANS_VERSION
, &ans
)) {
244 syslog(LOG_DEBUG
, "process %lu: %m",
245 (u_long
)msg
.lm_msg_ident
.pid
);
248 } else if (nr
== -1) {
249 if (errno
!= EAGAIN
) {
250 syslog(LOG_ERR
, "read: %s: %m", _PATH_LCKFIFO
);
253 #ifdef USE_NFSLOCKDWAIT_INSTEAD_OF_SELECT
255 nfsclnt(NFSCLNT_LOCKDWAIT
, NULL
);
257 } else if (nr
!= 0) {
259 "%s: discard %d bytes", _PATH_LCKFIFO
, nr
);
261 #ifdef USE_NFSLOCKDWAIT_INSTEAD_OF_SELECT
263 nfsclnt(NFSCLNT_LOCKDWAIT
, NULL
);
267 /* Reached only on error. */
269 (void)lockd_seteuid(0);
270 (void) nfsclnt(NFSCLNT_LOCKDFD
, (struct lockd_ans
*)-1);
278 struct xucred
*xucred
;
280 if (cl
->cl_auth
!= NULL
)
281 cl
->cl_auth
->ah_ops
->ah_destroy(cl
->cl_auth
);
282 cl
->cl_auth
= authunix_create(hostname
,
284 xucred
->cr_groups
[0],
285 xucred
->cr_ngroups
- 1,
286 &xucred
->cr_groups
[1]);
292 * Convert a lock LOCKD_MSG into an NLM request, and send it off.
295 test_request(LOCKD_MSG
*msg
)
298 struct timeval timeout
= {0, 0}; /* No timeout, no response. */
302 syslog(LOG_DEBUG
, "test request: %s: %s to %s",
303 msg
->lm_nfsv3
? "V4" : "V1/3",
304 msg
->lm_fl
.l_type
== F_WRLCK
? "write" : "read",
305 from_addr((struct sockaddr
*)&msg
->lm_addr
));
308 struct nlm4_testargs arg4
;
310 arg4
.cookie
.n_bytes
= (char *)&msg
->lm_msg_ident
;
311 arg4
.cookie
.n_len
= sizeof(msg
->lm_msg_ident
);
312 arg4
.exclusive
= msg
->lm_fl
.l_type
== F_WRLCK
? 1 : 0;
313 arg4
.alock
.caller_name
= hostname
;
314 arg4
.alock
.fh
.n_bytes
= (char *)&msg
->lm_fh
;
315 arg4
.alock
.fh
.n_len
= msg
->lm_fh_len
;
316 arg4
.alock
.oh
.n_bytes
= (char *)&owner
;
317 arg4
.alock
.oh
.n_len
= sizeof(owner
);
318 arg4
.alock
.svid
= msg
->lm_msg_ident
.pid
;
319 arg4
.alock
.l_offset
= msg
->lm_fl
.l_start
;
320 arg4
.alock
.l_len
= msg
->lm_fl
.l_len
;
322 if ((cli
= get_client(
323 (struct sockaddr
*)&msg
->lm_addr
,
327 set_auth(cli
, &msg
->lm_cred
);
328 (void)clnt_call(cli
, NLM_TEST_MSG
,
329 xdr_nlm4_testargs
, &arg4
, xdr_void
, &dummy
, timeout
);
331 struct nlm_testargs arg
;
333 arg
.cookie
.n_bytes
= (char *)&msg
->lm_msg_ident
;
334 arg
.cookie
.n_len
= sizeof(msg
->lm_msg_ident
);
335 arg
.exclusive
= msg
->lm_fl
.l_type
== F_WRLCK
? 1 : 0;
336 arg
.alock
.caller_name
= hostname
;
337 arg
.alock
.fh
.n_bytes
= (char *)&msg
->lm_fh
;
338 arg
.alock
.fh
.n_len
= msg
->lm_fh_len
;
339 arg
.alock
.oh
.n_bytes
= (char *)&owner
;
340 arg
.alock
.oh
.n_len
= sizeof(owner
);
341 arg
.alock
.svid
= msg
->lm_msg_ident
.pid
;
342 arg
.alock
.l_offset
= msg
->lm_fl
.l_start
;
343 arg
.alock
.l_len
= msg
->lm_fl
.l_len
;
345 if ((cli
= get_client(
346 (struct sockaddr
*)&msg
->lm_addr
,
350 set_auth(cli
, &msg
->lm_cred
);
351 (void)clnt_call(cli
, NLM_TEST_MSG
,
352 xdr_nlm_testargs
, &arg
, xdr_void
, &dummy
, timeout
);
359 * Convert a lock LOCKD_MSG into an NLM request, and send it off.
362 lock_request(LOCKD_MSG
*msg
)
365 struct nlm4_lockargs arg4
;
366 struct nlm_lockargs arg
;
367 struct timeval timeout
= {0, 0}; /* No timeout, no response. */
371 syslog(LOG_DEBUG
, "lock request: %s: %s to %s",
372 msg
->lm_nfsv3
? "V4" : "V1/3",
373 msg
->lm_fl
.l_type
== F_WRLCK
? "write" : "read",
374 from_addr((struct sockaddr
*)&msg
->lm_addr
));
377 arg4
.cookie
.n_bytes
= (char *)&msg
->lm_msg_ident
;
378 arg4
.cookie
.n_len
= sizeof(msg
->lm_msg_ident
);
379 arg4
.block
= msg
->lm_wait
? 1 : 0;
380 arg4
.exclusive
= msg
->lm_fl
.l_type
== F_WRLCK
? 1 : 0;
381 arg4
.alock
.caller_name
= hostname
;
382 arg4
.alock
.fh
.n_bytes
= (char *)&msg
->lm_fh
;
383 arg4
.alock
.fh
.n_len
= msg
->lm_fh_len
;
384 arg4
.alock
.oh
.n_bytes
= (char *)&owner
;
385 arg4
.alock
.oh
.n_len
= sizeof(owner
);
386 arg4
.alock
.svid
= msg
->lm_msg_ident
.pid
;
387 arg4
.alock
.l_offset
= msg
->lm_fl
.l_start
;
388 arg4
.alock
.l_len
= msg
->lm_fl
.l_len
;
390 arg4
.state
= nsm_state
;
392 if ((cli
= get_client(
393 (struct sockaddr
*)&msg
->lm_addr
,
397 set_auth(cli
, &msg
->lm_cred
);
398 (void)clnt_call(cli
, NLM_LOCK_MSG
,
399 xdr_nlm4_lockargs
, &arg4
, xdr_void
, &dummy
, timeout
);
401 arg
.cookie
.n_bytes
= (char *)&msg
->lm_msg_ident
;
402 arg
.cookie
.n_len
= sizeof(msg
->lm_msg_ident
);
403 arg
.block
= msg
->lm_wait
? 1 : 0;
404 arg
.exclusive
= msg
->lm_fl
.l_type
== F_WRLCK
? 1 : 0;
405 arg
.alock
.caller_name
= hostname
;
406 arg
.alock
.fh
.n_bytes
= (char *)&msg
->lm_fh
;
407 arg
.alock
.fh
.n_len
= msg
->lm_fh_len
;
408 arg
.alock
.oh
.n_bytes
= (char *)&owner
;
409 arg
.alock
.oh
.n_len
= sizeof(owner
);
410 arg
.alock
.svid
= msg
->lm_msg_ident
.pid
;
411 arg
.alock
.l_offset
= msg
->lm_fl
.l_start
;
412 arg
.alock
.l_len
= msg
->lm_fl
.l_len
;
414 arg
.state
= nsm_state
;
416 if ((cli
= get_client(
417 (struct sockaddr
*)&msg
->lm_addr
,
421 set_auth(cli
, &msg
->lm_cred
);
422 (void)clnt_call(cli
, NLM_LOCK_MSG
,
423 xdr_nlm_lockargs
, &arg
, xdr_void
, &dummy
, timeout
);
430 * Convert an unlock LOCKD_MSG into an NLM request, and send it off.
433 unlock_request(LOCKD_MSG
*msg
)
436 struct nlm4_unlockargs arg4
;
437 struct nlm_unlockargs arg
;
438 struct timeval timeout
= {0, 0}; /* No timeout, no response. */
442 syslog(LOG_DEBUG
, "unlock request: %s: to %s",
443 msg
->lm_nfsv3
? "V4" : "V1/3",
444 from_addr((struct sockaddr
*)&msg
->lm_addr
));
447 arg4
.cookie
.n_bytes
= (char *)&msg
->lm_msg_ident
;
448 arg4
.cookie
.n_len
= sizeof(msg
->lm_msg_ident
);
449 arg4
.alock
.caller_name
= hostname
;
450 arg4
.alock
.fh
.n_bytes
= (char *)&msg
->lm_fh
;
451 arg4
.alock
.fh
.n_len
= msg
->lm_fh_len
;
452 arg4
.alock
.oh
.n_bytes
= (char *)&owner
;
453 arg4
.alock
.oh
.n_len
= sizeof(owner
);
454 arg4
.alock
.svid
= msg
->lm_msg_ident
.pid
;
455 arg4
.alock
.l_offset
= msg
->lm_fl
.l_start
;
456 arg4
.alock
.l_len
= msg
->lm_fl
.l_len
;
458 if ((cli
= get_client(
459 (struct sockaddr
*)&msg
->lm_addr
,
463 set_auth(cli
, &msg
->lm_cred
);
464 (void)clnt_call(cli
, NLM_UNLOCK_MSG
,
465 xdr_nlm4_unlockargs
, &arg4
, xdr_void
, &dummy
, timeout
);
467 arg
.cookie
.n_bytes
= (char *)&msg
->lm_msg_ident
;
468 arg
.cookie
.n_len
= sizeof(msg
->lm_msg_ident
);
469 arg
.alock
.caller_name
= hostname
;
470 arg
.alock
.fh
.n_bytes
= (char *)&msg
->lm_fh
;
471 arg
.alock
.fh
.n_len
= msg
->lm_fh_len
;
472 arg
.alock
.oh
.n_bytes
= (char *)&owner
;
473 arg
.alock
.oh
.n_len
= sizeof(owner
);
474 arg
.alock
.svid
= msg
->lm_msg_ident
.pid
;
475 arg
.alock
.l_offset
= msg
->lm_fl
.l_start
;
476 arg
.alock
.l_len
= msg
->lm_fl
.l_len
;
478 if ((cli
= get_client(
479 (struct sockaddr
*)&msg
->lm_addr
,
483 set_auth(cli
, &msg
->lm_cred
);
484 (void)clnt_call(cli
, NLM_UNLOCK_MSG
,
485 xdr_nlm_unlockargs
, &arg
, xdr_void
, &dummy
, timeout
);
492 lock_answer(int pid
, netobj
*netcookie
, int result
, int version
,
493 int *pid_p
, off_t l_start
, off_t l_len
)
495 struct lockd_ans ans
;
497 if (netcookie
->n_len
!= sizeof(ans
.la_msg_ident
)) {
498 if (pid
== -1) { /* we're screwed */
499 syslog(LOG_ERR
, "inedible nlm cookie");
502 ans
.la_msg_ident
.pid
= pid
;
503 ans
.la_msg_ident
.msg_seq
= -1;
505 memcpy(&ans
.la_msg_ident
, netcookie
->n_bytes
,
506 sizeof(ans
.la_msg_ident
));
510 syslog(LOG_DEBUG
, "lock answer: pid %lu: %s %d",
511 (unsigned long)ans
.la_msg_ident
.pid
,
512 version
== NLM_VERS4
? "nlmv4" : "nlmv3",
515 ans
.la_getlk_set
= 0;
516 if (version
== NLM_VERS4
)
522 ans
.la_errno
= EACCES
;
526 ans
.la_errno
= EACCES
;
528 /* this is an answer to a nlm_test msg */
529 ans
.la_getlk_set
= 1;
530 ans
.la_getlk_pid
= *pid_p
;
531 ans
.la_getlk_start
= l_start
;
532 ans
.la_getlk_len
= l_len
;
536 case nlm4_denied_nolocks
:
537 ans
.la_errno
= ENOLCK
;
542 case nlm4_denied_grace_period
:
543 ans
.la_errno
= EAGAIN
;
546 ans
.la_errno
= EDEADLK
;
549 ans
.la_errno
= EROFS
;
552 ans
.la_errno
= ESTALE
;
555 ans
.la_errno
= EFBIG
;
558 ans
.la_errno
= EACCES
;
567 ans
.la_errno
= EACCES
;
571 ans
.la_errno
= EACCES
;
573 /* this is an answer to a nlm_test msg */
574 ans
.la_getlk_set
= 1;
575 ans
.la_getlk_pid
= *pid_p
;
576 ans
.la_getlk_start
= l_start
;
577 ans
.la_getlk_len
= l_len
;
581 case nlm_denied_nolocks
:
582 ans
.la_errno
= ENOLCK
;
587 case nlm_denied_grace_period
:
588 ans
.la_errno
= EAGAIN
;
591 ans
.la_errno
= EDEADLK
;
595 if (nfslockdans(LOCKD_ANS_VERSION
, &ans
)) {
596 syslog(LOG_DEBUG
, "lock_answer(%d): process %lu: %m",
597 result
, (u_long
)ans
.la_msg_ident
.pid
);
605 * Display the contents of a kernel LOCKD_MSG structure.
610 static char hex
[] = "0123456789abcdef";
614 u_int8_t
*p
, *t
, buf
[NFS_SMALLFH
*3+1];
616 syslog(LOG_DEBUG
, "process ID: %lu\n", (long)mp
->lm_msg_ident
.pid
);
618 fsidp
= (fsid_t
*)&mp
->lm_fh
;
619 fidp
= (struct fid
*)((u_int8_t
*)&mp
->lm_fh
+ sizeof(fsid_t
));
621 for (t
= buf
, p
= (u_int8_t
*)mp
->lm_fh
,
623 len
> 0; ++p
, --len
) {
625 *t
++ = hex
[(*p
& 0xf0) >> 4];
626 *t
++ = hex
[*p
& 0x0f];
630 syslog(LOG_DEBUG
, "fh_len %d, fh %s\n", mp
->lm_fh_len
, buf
);
632 /* Show flock structure. */
633 syslog(LOG_DEBUG
, "start %qu; len %qu; pid %lu; type %d; whence %d\n",
634 mp
->lm_fl
.l_start
, mp
->lm_fl
.l_len
, (u_long
)mp
->lm_fl
.l_pid
,
635 mp
->lm_fl
.l_type
, mp
->lm_fl
.l_whence
);
637 /* Show wait flag. */
638 syslog(LOG_DEBUG
, "wait was %s\n", mp
->lm_wait
? "set" : "not set");