lockd_lock.c revision 1.16 1 /* $NetBSD: lockd_lock.c,v 1.16 2003/03/14 14:10:00 yamt Exp $ */
2
3 /*
4 * Copyright (c) 2000 Manuel Bouyer.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by the University of
17 * California, Berkeley and its contributors.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 */
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <unistd.h>
39 #include <fcntl.h>
40 #include <syslog.h>
41 #include <errno.h>
42 #include <string.h>
43 #include <signal.h>
44 #include <rpc/rpc.h>
45 #include <sys/socket.h>
46 #include <sys/param.h>
47 #include <sys/mount.h>
48 #include <sys/wait.h>
49 #include <rpcsvc/sm_inter.h>
50 #include <rpcsvc/nlm_prot.h>
51 #include "lockd_lock.h"
52 #include "lockd.h"
53
54 /* A set of utilities for managing file locking */
55 LIST_HEAD(lcklst_head, file_lock);
56 struct lcklst_head lcklst_head = LIST_HEAD_INITIALIZER(lcklst_head);
57
58 /* struct describing a lock */
59 struct file_lock {
60 LIST_ENTRY(file_lock) lcklst;
61 fhandle_t filehandle; /* NFS filehandle */
62 struct sockaddr *addr;
63 struct nlm4_holder client; /* lock holder */
64 netobj client_cookie; /* cookie sent by the client */
65 char client_name[128];
66 int nsm_status; /* status from the remote lock manager */
67 int status; /* lock status, see below */
68 int flags; /* lock flags, see lockd_lock.h */
69 pid_t locker; /* pid of the child process trying to get the lock */
70 int fd; /* file descriptor for this lock */
71 };
72
73 /* lock status */
74 #define LKST_LOCKED 1 /* lock is locked */
75 #define LKST_WAITING 2 /* file is already locked by another host */
76 #define LKST_PROCESSING 3 /* child is trying to acquire the lock */
77 #define LKST_DYING 4 /* must dies when we get news from the child */
78
79 void lfree __P((struct file_lock *));
80 enum nlm_stats do_lock __P((struct file_lock *, int));
81 enum nlm_stats do_unlock __P((struct file_lock *));
82 void send_granted __P((struct file_lock *, int));
83 void siglock __P((void));
84 void sigunlock __P((void));
85
86 /* list of hosts we monitor */
87 LIST_HEAD(hostlst_head, host);
88 struct hostlst_head hostlst_head = LIST_HEAD_INITIALIZER(hostlst_head);
89
90 /* struct describing a lock */
91 struct host {
92 LIST_ENTRY(host) hostlst;
93 char name[SM_MAXSTRLEN+1];
94 int refcnt;
95 };
96
97 void do_mon __P((char *));
98
99 /*
100 * testlock(): inform the caller if the requested lock would be granted or not
101 * returns NULL if lock would granted, or pointer to the current nlm4_holder
102 * otherwise.
103 */
104
105 struct nlm4_holder *
106 testlock(lock, flags)
107 struct nlm4_lock *lock;
108 int flags;
109 {
110 struct file_lock *fl;
111 fhandle_t filehandle;
112
113 /* convert lock to a local filehandle */
114 memcpy(&filehandle, lock->fh.n_bytes, sizeof(filehandle));
115
116 siglock();
117 /* search through the list for lock holder */
118 LIST_FOREACH(fl, &lcklst_head, lcklst) {
119 if (fl->status != LKST_LOCKED)
120 continue;
121 if (memcmp(&fl->filehandle, &filehandle, sizeof(filehandle)))
122 continue;
123 /* got it ! */
124 syslog(LOG_DEBUG, "test for %s: found lock held by %s",
125 lock->caller_name, fl->client_name);
126 sigunlock();
127 return (&fl->client);
128 }
129 /* not found */
130 sigunlock();
131 syslog(LOG_DEBUG, "test for %s: no lock found", lock->caller_name);
132 return NULL;
133 }
134
135 /*
136 * getlock: try to acquire the lock.
137 * If file is already locked and we can sleep, put the lock in the list with
138 * status LKST_WAITING; it'll be processed later.
139 * Otherwise try to lock. If we're allowed to block, fork a child which
140 * will do the blocking lock.
141 */
142 enum nlm_stats
143 getlock(lckarg, rqstp, flags)
144 nlm4_lockargs * lckarg;
145 struct svc_req *rqstp;
146 int flags;
147 {
148 struct file_lock *fl, *newfl;
149 enum nlm_stats retval;
150
151 if (grace_expired == 0 && lckarg->reclaim == 0)
152 return (flags & LOCK_V4) ?
153 nlm4_denied_grace_period : nlm_denied_grace_period;
154
155 /* allocate new file_lock for this request */
156 newfl = malloc(sizeof(struct file_lock));
157 if (newfl == NULL) {
158 syslog(LOG_NOTICE, "malloc failed: %s", strerror(errno));
159 /* failed */
160 return (flags & LOCK_V4) ?
161 nlm4_denied_nolock : nlm_denied_nolocks;
162 }
163 if (lckarg->alock.fh.n_len != sizeof(fhandle_t)) {
164 syslog(LOG_DEBUG, "received fhandle size %d, local size %d",
165 lckarg->alock.fh.n_len, (int)sizeof(fhandle_t));
166 }
167 memcpy(&newfl->filehandle, lckarg->alock.fh.n_bytes, sizeof(fhandle_t));
168 newfl->addr = (struct sockaddr *)svc_getrpccaller(rqstp->rq_xprt)->buf;
169 newfl->client.exclusive = lckarg->exclusive;
170 newfl->client.svid = lckarg->alock.svid;
171 newfl->client.oh.n_bytes = malloc(lckarg->alock.oh.n_len);
172 if (newfl->client.oh.n_bytes == NULL) {
173 syslog(LOG_NOTICE, "malloc failed: %s", strerror(errno));
174 free(newfl);
175 return (flags & LOCK_V4) ?
176 nlm4_denied_nolock : nlm_denied_nolocks;
177 }
178 newfl->client.oh.n_len = lckarg->alock.oh.n_len;
179 memcpy(newfl->client.oh.n_bytes, lckarg->alock.oh.n_bytes,
180 lckarg->alock.oh.n_len);
181 newfl->client.l_offset = lckarg->alock.l_offset;
182 newfl->client.l_len = lckarg->alock.l_len;
183 newfl->client_cookie.n_len = lckarg->cookie.n_len;
184 newfl->client_cookie.n_bytes = malloc(lckarg->cookie.n_len);
185 if (newfl->client_cookie.n_bytes == NULL) {
186 syslog(LOG_NOTICE, "malloc failed: %s", strerror(errno));
187 free(newfl->client.oh.n_bytes);
188 free(newfl);
189 return (flags & LOCK_V4) ?
190 nlm4_denied_nolock : nlm_denied_nolocks;
191 }
192 memcpy(newfl->client_cookie.n_bytes, lckarg->cookie.n_bytes,
193 lckarg->cookie.n_len);
194 strlcpy(newfl->client_name, lckarg->alock.caller_name,
195 sizeof(newfl->client_name));
196 newfl->nsm_status = lckarg->state;
197 newfl->status = 0;
198 newfl->flags = flags;
199 siglock();
200 /* look for a lock rq from this host for this fh */
201 LIST_FOREACH(fl, &lcklst_head, lcklst) {
202 if (memcmp(&newfl->filehandle, &fl->filehandle,
203 sizeof(fhandle_t)) == 0) {
204 if (strcmp(newfl->client_name, fl->client_name) == 0 &&
205 newfl->client.svid == fl->client.svid) {
206 /* already locked by this host ??? */
207 sigunlock();
208 syslog(LOG_NOTICE, "duplicate lock from %s",
209 newfl->client_name);
210 lfree(newfl);
211 switch(fl->status) {
212 case LKST_LOCKED:
213 return (flags & LOCK_V4) ?
214 nlm4_granted : nlm_granted;
215 case LKST_WAITING:
216 case LKST_PROCESSING:
217 return (flags & LOCK_V4) ?
218 nlm4_blocked : nlm_blocked;
219 case LKST_DYING:
220 return (flags & LOCK_V4) ?
221 nlm4_denied : nlm_denied;
222 default:
223 syslog(LOG_NOTICE, "bad status %d",
224 fl->status);
225 return (flags & LOCK_V4) ?
226 nlm4_failed : nlm_denied;
227 }
228 }
229 /*
230 * We already have a lock for this file. Put this one
231 * in waiting state if allowed to block
232 */
233 if (lckarg->block) {
234 syslog(LOG_DEBUG, "lock from %s: already "
235 "locked, waiting",
236 lckarg->alock.caller_name);
237 newfl->status = LKST_WAITING;
238 LIST_INSERT_HEAD(&lcklst_head, newfl, lcklst);
239 do_mon(lckarg->alock.caller_name);
240 sigunlock();
241 return (flags & LOCK_V4) ?
242 nlm4_blocked : nlm_blocked;
243 } else {
244 sigunlock();
245 syslog(LOG_DEBUG, "lock from %s: already "
246 "locked, failed",
247 lckarg->alock.caller_name);
248 lfree(newfl);
249 return (flags & LOCK_V4) ?
250 nlm4_denied : nlm_denied;
251 }
252 }
253 }
254 /* no entry for this file yet; add to list */
255 LIST_INSERT_HEAD(&lcklst_head, newfl, lcklst);
256 /* do the lock */
257 retval = do_lock(newfl, lckarg->block);
258 switch (retval) {
259 case nlm4_granted:
260 /* case nlm_granted: is the same as nlm4_granted */
261 case nlm4_blocked:
262 /* case nlm_blocked: is the same as nlm4_blocked */
263 do_mon(lckarg->alock.caller_name);
264 break;
265 default:
266 lfree(newfl);
267 break;
268 }
269 sigunlock();
270 return retval;
271 }
272
273 /* unlock a filehandle */
274 enum nlm_stats
275 unlock(lck, flags)
276 nlm4_lock *lck;
277 int flags;
278 {
279 struct file_lock *fl;
280 fhandle_t filehandle;
281 int err = (flags & LOCK_V4) ? nlm4_granted : nlm_granted;
282
283 memcpy(&filehandle, lck->fh.n_bytes, sizeof(fhandle_t));
284 siglock();
285 LIST_FOREACH(fl, &lcklst_head, lcklst) {
286 if (strcmp(fl->client_name, lck->caller_name) ||
287 memcmp(&filehandle, &fl->filehandle, sizeof(fhandle_t)) ||
288 fl->client.oh.n_len != lck->oh.n_len ||
289 memcmp(fl->client.oh.n_bytes, lck->oh.n_bytes,
290 fl->client.oh.n_len) != 0 ||
291 fl->client.svid != lck->svid)
292 continue;
293 /* Got it, unlock and remove from the queue */
294 syslog(LOG_DEBUG, "unlock from %s: found struct, status %d",
295 lck->caller_name, fl->status);
296 switch (fl->status) {
297 case LKST_LOCKED:
298 err = do_unlock(fl);
299 break;
300 case LKST_WAITING:
301 /* remove from the list */
302 LIST_REMOVE(fl, lcklst);
303 lfree(fl);
304 break;
305 case LKST_PROCESSING:
306 /*
307 * being handled by a child; will clean up
308 * when the child exits
309 */
310 fl->status = LKST_DYING;
311 break;
312 case LKST_DYING:
313 /* nothing to do */
314 break;
315 default:
316 syslog(LOG_NOTICE, "unknow status %d for %s",
317 fl->status, fl->client_name);
318 }
319 sigunlock();
320 return err;
321 }
322 sigunlock();
323 /* didn't find a matching entry; log anyway */
324 syslog(LOG_NOTICE, "no matching entry for %s",
325 lck->caller_name);
326 return (flags & LOCK_V4) ? nlm4_granted : nlm_granted;
327 }
328
329 void
330 lfree(fl)
331 struct file_lock *fl;
332 {
333 free(fl->client.oh.n_bytes);
334 free(fl->client_cookie.n_bytes);
335 free(fl);
336 }
337
338 void
339 sigchild_handler(sig)
340 int sig;
341 {
342 int status;
343 pid_t pid;
344 struct file_lock *fl;
345
346 while (1) {
347 pid = wait4(-1, &status, WNOHANG, NULL);
348 if (pid == -1) {
349 if (errno != ECHILD)
350 syslog(LOG_NOTICE, "wait failed: %s",
351 strerror(errno));
352 else
353 syslog(LOG_DEBUG, "wait failed: %s",
354 strerror(errno));
355 return;
356 }
357 if (pid == 0) {
358 /* no more child to handle yet */
359 return;
360 }
361 /*
362 * if we're here we have a child that exited
363 * Find the associated file_lock.
364 */
365 LIST_FOREACH(fl, &lcklst_head, lcklst) {
366 if (pid == fl->locker)
367 break;
368 }
369 if (fl == NULL) {
370 syslog(LOG_NOTICE, "unknow child %d", pid);
371 } else {
372 /*
373 * protect from pid reusing.
374 */
375 fl->locker = 0;
376 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
377 syslog(LOG_NOTICE, "child %d failed", pid);
378 /*
379 * can't do much here; we can't reply
380 * anything but OK for blocked locks
381 * Eventually the client will time out
382 * and retry.
383 */
384 do_unlock(fl);
385 return;
386 }
387
388 /* check lock status */
389 syslog(LOG_DEBUG, "processing child %d, status %d",
390 pid, fl->status);
391 switch(fl->status) {
392 case LKST_PROCESSING:
393 fl->status = LKST_LOCKED;
394 send_granted(fl, (fl->flags & LOCK_V4) ?
395 nlm4_granted : nlm_granted);
396 break;
397 case LKST_DYING:
398 do_unlock(fl);
399 break;
400 default:
401 syslog(LOG_NOTICE, "bad lock status (%d) for"
402 " child %d", fl->status, pid);
403 }
404 }
405 }
406 }
407
408 /*
409 *
410 * try to acquire the lock described by fl. Eventually fork a child to do a
411 * blocking lock if allowed and required.
412 */
413
414 enum nlm_stats
415 do_lock(fl, block)
416 struct file_lock *fl;
417 int block;
418 {
419 int lflags, error;
420 struct stat st;
421
422 fl->fd = fhopen(&fl->filehandle, O_RDWR);
423 if (fl->fd < 0) {
424 switch (errno) {
425 case ESTALE:
426 error = nlm4_stale_fh;
427 break;
428 case EROFS:
429 error = nlm4_rofs;
430 break;
431 default:
432 error = nlm4_failed;
433 }
434 if ((fl->flags & LOCK_V4) == 0)
435 error = nlm_denied;
436 syslog(LOG_NOTICE, "fhopen failed (from %s): %s",
437 fl->client_name, strerror(errno));
438 LIST_REMOVE(fl, lcklst);
439 return error;
440 }
441 if (fstat(fl->fd, &st) < 0) {
442 syslog(LOG_NOTICE, "fstat failed (from %s): %s",
443 fl->client_name, strerror(errno));
444 }
445 syslog(LOG_DEBUG, "lock from %s for file%s%s: dev %d ino %d (uid %d), "
446 "flags %d",
447 fl->client_name, fl->client.exclusive ? " (exclusive)":"",
448 block ? " (block)":"",
449 st.st_dev, st.st_ino, st.st_uid, fl->flags);
450 lflags = LOCK_NB;
451 if (fl->client.exclusive == 0)
452 lflags |= LOCK_SH;
453 else
454 lflags |= LOCK_EX;
455 error = flock(fl->fd, lflags);
456 if (error != 0 && errno == EAGAIN && block) {
457 switch (fl->locker = fork()) {
458 case -1: /* fork failed */
459 syslog(LOG_NOTICE, "fork failed: %s", strerror(errno));
460 LIST_REMOVE(fl, lcklst);
461 close(fl->fd);
462 return (fl->flags & LOCK_V4) ?
463 nlm4_denied_nolock : nlm_denied_nolocks;
464 case 0:
465 /*
466 * Attempt a blocking lock. Will have to call
467 * NLM_GRANTED later.
468 */
469 setproctitle("%s", fl->client_name);
470 lflags &= ~LOCK_NB;
471 if(flock(fl->fd, lflags) != 0) {
472 syslog(LOG_NOTICE, "flock failed: %s",
473 strerror(errno));
474 _exit(1);
475 }
476 /* lock granted */
477 _exit(0);
478 default:
479 syslog(LOG_DEBUG, "lock request from %s: forked %d",
480 fl->client_name, fl->locker);
481 fl->status = LKST_PROCESSING;
482 return (fl->flags & LOCK_V4) ?
483 nlm4_blocked : nlm_blocked;
484 }
485 }
486 /* non block case */
487 if (error != 0) {
488 switch (errno) {
489 case EAGAIN:
490 error = nlm4_denied;
491 break;
492 case ESTALE:
493 error = nlm4_stale_fh;
494 break;
495 case EROFS:
496 error = nlm4_rofs;
497 break;
498 default:
499 error = nlm4_failed;
500 }
501 if ((fl->flags & LOCK_V4) == 0)
502 error = nlm_denied;
503 if (errno != EAGAIN)
504 syslog(LOG_NOTICE, "flock for %s failed: %s",
505 fl->client_name, strerror(errno));
506 else syslog(LOG_DEBUG, "flock for %s failed: %s",
507 fl->client_name, strerror(errno));
508 LIST_REMOVE(fl, lcklst);
509 close(fl->fd);
510 return error;
511 }
512 fl->status = LKST_LOCKED;
513 return (fl->flags & LOCK_V4) ? nlm4_granted : nlm_granted;
514 }
515
516 void
517 send_granted(fl, opcode)
518 struct file_lock *fl;
519 int opcode;
520 {
521 CLIENT *cli;
522 static char dummy;
523 struct timeval timeo;
524 int success;
525 static struct nlm_res retval;
526 static struct nlm4_res retval4;
527
528 cli = get_client(fl->addr,
529 (fl->flags & LOCK_V4) ? NLM_VERS4 : NLM_VERS);
530 if (cli == NULL) {
531 syslog(LOG_NOTICE, "failed to get CLIENT for %s",
532 fl->client_name);
533 /*
534 * We fail to notify remote that the lock has been granted.
535 * The client will timeout and retry, the lock will be
536 * granted at this time.
537 */
538 return;
539 }
540 timeo.tv_sec = 0;
541 timeo.tv_usec = (fl->flags & LOCK_ASYNC) ? 0 : 500000; /* 0.5s */
542
543 if (fl->flags & LOCK_V4) {
544 static nlm4_testargs res;
545 res.cookie = fl->client_cookie;
546 res.exclusive = fl->client.exclusive;
547 res.alock.caller_name = fl->client_name;
548 res.alock.fh.n_len = sizeof(fhandle_t);
549 res.alock.fh.n_bytes = (char*)&fl->filehandle;
550 res.alock.oh = fl->client.oh;
551 res.alock.svid = fl->client.svid;
552 res.alock.l_offset = fl->client.l_offset;
553 res.alock.l_len = fl->client.l_len;
554 syslog(LOG_DEBUG, "sending v4 reply%s",
555 (fl->flags & LOCK_ASYNC) ? " (async)":"");
556 if (fl->flags & LOCK_ASYNC) {
557 success = clnt_call(cli, NLM4_GRANTED_MSG,
558 xdr_nlm4_testargs, &res, xdr_void, &dummy, timeo);
559 } else {
560 success = clnt_call(cli, NLM4_GRANTED,
561 xdr_nlm4_testargs, &res, xdr_nlm4_res,
562 &retval4, timeo);
563 }
564 } else {
565 static nlm_testargs res;
566
567 res.cookie = fl->client_cookie;
568 res.exclusive = fl->client.exclusive;
569 res.alock.caller_name = fl->client_name;
570 res.alock.fh.n_len = sizeof(fhandle_t);
571 res.alock.fh.n_bytes = (char*)&fl->filehandle;
572 res.alock.oh = fl->client.oh;
573 res.alock.svid = fl->client.svid;
574 res.alock.l_offset = fl->client.l_offset;
575 res.alock.l_len = fl->client.l_len;
576 syslog(LOG_DEBUG, "sending v1 reply%s",
577 (fl->flags & LOCK_ASYNC) ? " (async)":"");
578 if (fl->flags & LOCK_ASYNC) {
579 success = clnt_call(cli, NLM_GRANTED_MSG,
580 xdr_nlm_testargs, &res, xdr_void, &dummy, timeo);
581 } else {
582 success = clnt_call(cli, NLM_GRANTED,
583 xdr_nlm_testargs, &res, xdr_nlm_res,
584 &retval, timeo);
585 }
586 }
587 if (debug_level > 2)
588 syslog(LOG_DEBUG, "clnt_call returns %d(%s) for granted",
589 success, clnt_sperrno(success));
590
591 }
592
593 enum nlm_stats
594 do_unlock(rfl)
595 struct file_lock *rfl;
596 {
597 struct file_lock *fl;
598 int error;
599 int lockst;
600
601 /* unlock the file: closing is enough ! */
602 if (close(rfl->fd) < 0) {
603 if (errno == ESTALE)
604 error = nlm4_stale_fh;
605 else
606 error = nlm4_failed;
607 if ((fl->flags & LOCK_V4) == 0)
608 error = nlm_denied;
609 syslog(LOG_NOTICE,
610 "close failed (from %s): %s",
611 rfl->client_name, strerror(errno));
612 } else {
613 error = (fl->flags & LOCK_V4) ?
614 nlm4_granted : nlm_granted;
615 }
616 LIST_REMOVE(rfl, lcklst);
617
618 /* process the next LKST_WAITING lock request for this fh */
619 LIST_FOREACH(fl, &lcklst_head, lcklst) {
620 if (fl->status != LKST_WAITING ||
621 memcmp(&rfl->filehandle, &fl->filehandle,
622 sizeof(fhandle_t)) != 0)
623 continue;
624
625 lockst = do_lock(fl, 1); /* If it's LKST_WAITING we can block */
626 switch (lockst) {
627 case nlm4_granted:
628 /* case nlm_granted: same as nlm4_granted */
629 send_granted(fl, (fl->flags & LOCK_V4) ?
630 nlm4_granted : nlm_granted);
631 break;
632 case nlm4_blocked:
633 /* case nlm_blocked: same as nlm4_blocked */
634 break;
635 default:
636 lfree(fl);
637 break;
638 }
639 break;
640 }
641 lfree(rfl);
642 return error;
643 }
644
645 void
646 siglock()
647 {
648 sigset_t block;
649
650 sigemptyset(&block);
651 sigaddset(&block, SIGCHLD);
652
653 if (sigprocmask(SIG_BLOCK, &block, NULL) < 0) {
654 syslog(LOG_WARNING, "siglock failed: %s", strerror(errno));
655 }
656 }
657
658 void
659 sigunlock()
660 {
661 sigset_t block;
662
663 sigemptyset(&block);
664 sigaddset(&block, SIGCHLD);
665
666 if (sigprocmask(SIG_UNBLOCK, &block, NULL) < 0) {
667 syslog(LOG_WARNING, "sigunlock failed: %s", strerror(errno));
668 }
669 }
670
671 /* monitor a host through rpc.statd, and keep a ref count */
672 void
673 do_mon(hostname)
674 char *hostname;
675 {
676 struct host *hp;
677 struct mon my_mon;
678 struct sm_stat_res res;
679 int retval;
680
681 LIST_FOREACH(hp, &hostlst_head, hostlst) {
682 if (strcmp(hostname, hp->name) == 0) {
683 /* already monitored, just bump refcnt */
684 hp->refcnt++;
685 return;
686 }
687 }
688 /* not found, have to create an entry for it */
689 hp = malloc(sizeof(struct host));
690 strlcpy(hp->name, hostname, sizeof(hp->name));
691 hp->refcnt = 1;
692 syslog(LOG_DEBUG, "monitoring host %s",
693 hostname);
694 memset(&my_mon, 0, sizeof(my_mon));
695 my_mon.mon_id.mon_name = hp->name;
696 my_mon.mon_id.my_id.my_name = "localhost";
697 my_mon.mon_id.my_id.my_prog = NLM_PROG;
698 my_mon.mon_id.my_id.my_vers = NLM_SM;
699 my_mon.mon_id.my_id.my_proc = NLM_SM_NOTIFY;
700 if ((retval =
701 callrpc("localhost", SM_PROG, SM_VERS, SM_MON, xdr_mon,
702 (char*)&my_mon, xdr_sm_stat_res, (char*)&res)) != 0) {
703 syslog(LOG_WARNING, "rpc to statd failed: %s",
704 clnt_sperrno((enum clnt_stat)retval));
705 free(hp);
706 return;
707 }
708 if (res.res_stat == stat_fail) {
709 syslog(LOG_WARNING, "statd failed");
710 free(hp);
711 return;
712 }
713 LIST_INSERT_HEAD(&hostlst_head, hp, hostlst);
714 }
715
716 void
717 notify(hostname, state)
718 char *hostname;
719 int state;
720 {
721 struct file_lock *fl, *next_fl;
722 int err;
723 syslog(LOG_DEBUG, "notify from %s, new state %d", hostname, state);
724 /* search all lock for this host; if status changed, release the lock */
725 siglock();
726 for (fl = LIST_FIRST(&lcklst_head); fl != NULL; fl = next_fl) {
727 next_fl = LIST_NEXT(fl, lcklst);
728 if (strcmp(hostname, fl->client_name) == 0 &&
729 fl->nsm_status != state) {
730 syslog(LOG_DEBUG, "state %d, nsm_state %d, unlocking",
731 fl->status, fl->nsm_status);
732 switch(fl->status) {
733 case LKST_LOCKED:
734 err = do_unlock(fl);
735 if (err != nlm_granted)
736 syslog(LOG_DEBUG,
737 "notify: unlock failed for %s (%d)",
738 hostname, err);
739 break;
740 case LKST_WAITING:
741 LIST_REMOVE(fl, lcklst);
742 lfree(fl);
743 break;
744 case LKST_PROCESSING:
745 fl->status = LKST_DYING;
746 break;
747 case LKST_DYING:
748 break;
749 default:
750 syslog(LOG_NOTICE, "unknow status %d for %s",
751 fl->status, fl->client_name);
752 }
753 }
754 }
755 sigunlock();
756 }
757