lockd_lock.c revision 1.13 1 /* $NetBSD: lockd_lock.c,v 1.13 2003/03/14 13:53:08 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];
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 for (fl = LIST_FIRST(&lcklst_head); fl != NULL;
119 fl = LIST_NEXT(fl, lcklst)) {
120 if (fl->status != LKST_LOCKED)
121 continue;
122 if (memcmp(&fl->filehandle, &filehandle, sizeof(filehandle)))
123 continue;
124 /* got it ! */
125 syslog(LOG_DEBUG, "test for %s: found lock held by %s",
126 lock->caller_name, fl->client_name);
127 sigunlock();
128 return (&fl->client);
129 }
130 /* not found */
131 sigunlock();
132 syslog(LOG_DEBUG, "test for %s: no lock found", lock->caller_name);
133 return NULL;
134 }
135
136 /*
137 * getlock: try to acquire the lock.
138 * If file is already locked and we can sleep, put the lock in the list with
139 * status LKST_WAITING; it'll be processed later.
140 * Otherwise try to lock. If we're allowed to block, fork a child which
141 * will do the blocking lock.
142 */
143 enum nlm_stats
144 getlock(lckarg, rqstp, flags)
145 nlm4_lockargs * lckarg;
146 struct svc_req *rqstp;
147 int flags;
148 {
149 struct file_lock *fl, *newfl;
150 enum nlm_stats retval;
151
152 if (grace_expired == 0 && lckarg->reclaim == 0)
153 return (flags & LOCK_V4) ?
154 nlm4_denied_grace_period : nlm_denied_grace_period;
155
156 /* allocate new file_lock for this request */
157 newfl = malloc(sizeof(struct file_lock));
158 if (newfl == NULL) {
159 syslog(LOG_NOTICE, "malloc failed: %s", strerror(errno));
160 /* failed */
161 return (flags & LOCK_V4) ?
162 nlm4_denied_nolock : nlm_denied_nolocks;
163 }
164 if (lckarg->alock.fh.n_len != sizeof(fhandle_t)) {
165 syslog(LOG_DEBUG, "received fhandle size %d, local size %d",
166 lckarg->alock.fh.n_len, (int)sizeof(fhandle_t));
167 }
168 memcpy(&newfl->filehandle, lckarg->alock.fh.n_bytes, sizeof(fhandle_t));
169 newfl->addr = (struct sockaddr *)svc_getrpccaller(rqstp->rq_xprt)->buf;
170 newfl->client.exclusive = lckarg->exclusive;
171 newfl->client.svid = lckarg->alock.svid;
172 newfl->client.oh.n_bytes = malloc(lckarg->alock.oh.n_len);
173 if (newfl->client.oh.n_bytes == NULL) {
174 syslog(LOG_NOTICE, "malloc failed: %s", strerror(errno));
175 free(newfl);
176 return (flags & LOCK_V4) ?
177 nlm4_denied_nolock : nlm_denied_nolocks;
178 }
179 newfl->client.oh.n_len = lckarg->alock.oh.n_len;
180 memcpy(newfl->client.oh.n_bytes, lckarg->alock.oh.n_bytes,
181 lckarg->alock.oh.n_len);
182 newfl->client.l_offset = lckarg->alock.l_offset;
183 newfl->client.l_len = lckarg->alock.l_len;
184 newfl->client_cookie.n_len = lckarg->cookie.n_len;
185 newfl->client_cookie.n_bytes = malloc(lckarg->cookie.n_len);
186 if (newfl->client_cookie.n_bytes == NULL) {
187 syslog(LOG_NOTICE, "malloc failed: %s", strerror(errno));
188 free(newfl->client.oh.n_bytes);
189 free(newfl);
190 return (flags & LOCK_V4) ?
191 nlm4_denied_nolock : nlm_denied_nolocks;
192 }
193 memcpy(newfl->client_cookie.n_bytes, lckarg->cookie.n_bytes,
194 lckarg->cookie.n_len);
195 strncpy(newfl->client_name, lckarg->alock.caller_name, 128);
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 for (fl = LIST_FIRST(&lcklst_head); fl != NULL;
202 fl = LIST_NEXT(fl, lcklst)) {
203 if (memcmp(&newfl->filehandle, &fl->filehandle,
204 sizeof(fhandle_t)) == 0) {
205 if (strcmp(newfl->client_name, fl->client_name) == 0 &&
206 newfl->client.svid == fl->client.svid) {
207 /* already locked by this host ??? */
208 sigunlock();
209 syslog(LOG_NOTICE, "duplicate lock from %s",
210 newfl->client_name);
211 lfree(newfl);
212 switch(fl->status) {
213 case LKST_LOCKED:
214 return (flags & LOCK_V4) ?
215 nlm4_granted : nlm_granted;
216 case LKST_WAITING:
217 case LKST_PROCESSING:
218 return (flags & LOCK_V4) ?
219 nlm4_blocked : nlm_blocked;
220 case LKST_DYING:
221 return (flags & LOCK_V4) ?
222 nlm4_denied : nlm_denied;
223 default:
224 syslog(LOG_NOTICE, "bad status %d",
225 fl->status);
226 return (flags & LOCK_V4) ?
227 nlm4_failed : nlm_denied;
228 }
229 }
230 /*
231 * We already have a lock for this file. Put this one
232 * in waiting state if allowed to block
233 */
234 if (lckarg->block) {
235 syslog(LOG_DEBUG, "lock from %s: already "
236 "locked, waiting",
237 lckarg->alock.caller_name);
238 newfl->status = LKST_WAITING;
239 LIST_INSERT_HEAD(&lcklst_head, newfl, lcklst);
240 do_mon(lckarg->alock.caller_name);
241 sigunlock();
242 return (flags & LOCK_V4) ?
243 nlm4_blocked : nlm_blocked;
244 } else {
245 sigunlock();
246 syslog(LOG_DEBUG, "lock from %s: already "
247 "locked, failed",
248 lckarg->alock.caller_name);
249 lfree(newfl);
250 return (flags & LOCK_V4) ?
251 nlm4_denied : nlm_denied;
252 }
253 }
254 }
255 /* no entry for this file yet; add to list */
256 LIST_INSERT_HEAD(&lcklst_head, newfl, lcklst);
257 /* do the lock */
258 retval = do_lock(newfl, lckarg->block);
259 switch (retval) {
260 case nlm4_granted:
261 /* case nlm_granted: is the same as nlm4_granted */
262 case nlm4_blocked:
263 /* case nlm_blocked: is the same as nlm4_blocked */
264 do_mon(lckarg->alock.caller_name);
265 break;
266 default:
267 lfree(newfl);
268 break;
269 }
270 sigunlock();
271 return retval;
272 }
273
274 /* unlock a filehandle */
275 enum nlm_stats
276 unlock(lck, flags)
277 nlm4_lock *lck;
278 int flags;
279 {
280 struct file_lock *fl;
281 fhandle_t filehandle;
282 int err = (flags & LOCK_V4) ? nlm4_granted : nlm_granted;
283
284 memcpy(&filehandle, lck->fh.n_bytes, sizeof(fhandle_t));
285 siglock();
286 for (fl = LIST_FIRST(&lcklst_head); fl != NULL;
287 fl = LIST_NEXT(fl, lcklst)) {
288 if (strcmp(fl->client_name, lck->caller_name) ||
289 memcmp(&filehandle, &fl->filehandle, sizeof(fhandle_t)) ||
290 fl->client.oh.n_len != lck->oh.n_len ||
291 memcmp(fl->client.oh.n_bytes, lck->oh.n_bytes,
292 fl->client.oh.n_len) != 0 ||
293 fl->client.svid != lck->svid)
294 continue;
295 /* Got it, unlock and remove from the queue */
296 syslog(LOG_DEBUG, "unlock from %s: found struct, status %d",
297 lck->caller_name, fl->status);
298 switch (fl->status) {
299 case LKST_LOCKED:
300 err = do_unlock(fl);
301 break;
302 case LKST_WAITING:
303 /* remove from the list */
304 LIST_REMOVE(fl, lcklst);
305 lfree(fl);
306 break;
307 case LKST_PROCESSING:
308 /*
309 * being handled by a child; will clean up
310 * when the child exits
311 */
312 fl->status = LKST_DYING;
313 break;
314 case LKST_DYING:
315 /* nothing to do */
316 break;
317 default:
318 syslog(LOG_NOTICE, "unknow status %d for %s",
319 fl->status, fl->client_name);
320 }
321 sigunlock();
322 return err;
323 }
324 sigunlock();
325 /* didn't find a matching entry; log anyway */
326 syslog(LOG_NOTICE, "no matching entry for %s",
327 lck->caller_name);
328 return (flags & LOCK_V4) ? nlm4_granted : nlm_granted;
329 }
330
331 void
332 lfree(fl)
333 struct file_lock *fl;
334 {
335 free(fl->client.oh.n_bytes);
336 free(fl->client_cookie.n_bytes);
337 free(fl);
338 }
339
340 void
341 sigchild_handler(sig)
342 int sig;
343 {
344 int status;
345 pid_t pid;
346 struct file_lock *fl;
347
348 while (1) {
349 pid = wait4(-1, &status, WNOHANG, NULL);
350 if (pid == -1) {
351 if (errno != ECHILD)
352 syslog(LOG_NOTICE, "wait failed: %s",
353 strerror(errno));
354 else
355 syslog(LOG_DEBUG, "wait failed: %s",
356 strerror(errno));
357 return;
358 }
359 if (pid == 0) {
360 /* no more child to handle yet */
361 return;
362 }
363 /*
364 * if we're here we have a child that exited
365 * Find the associated file_lock.
366 */
367 for (fl = LIST_FIRST(&lcklst_head); fl != NULL;
368 fl = LIST_NEXT(fl, lcklst)) {
369 if (pid == fl->locker)
370 break;
371 }
372 if (fl == NULL) {
373 syslog(LOG_NOTICE, "unknow child %d", pid);
374 } else {
375 /*
376 * protect from pid reusing.
377 */
378 fl->locker = 0;
379 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
380 syslog(LOG_NOTICE, "child %d failed", pid);
381 /*
382 * can't do much here; we can't reply
383 * anything but OK for blocked locks
384 * Eventually the client will time out
385 * and retry.
386 */
387 do_unlock(fl);
388 return;
389 }
390
391 /* check lock status */
392 syslog(LOG_DEBUG, "processing child %d, status %d",
393 pid, fl->status);
394 switch(fl->status) {
395 case LKST_PROCESSING:
396 fl->status = LKST_LOCKED;
397 send_granted(fl, (fl->flags & LOCK_V4) ?
398 nlm4_granted : nlm_granted);
399 break;
400 case LKST_DYING:
401 do_unlock(fl);
402 break;
403 default:
404 syslog(LOG_NOTICE, "bad lock status (%d) for"
405 " child %d", fl->status, pid);
406 }
407 }
408 }
409 }
410
411 /*
412 *
413 * try to acquire the lock described by fl. Eventually fork a child to do a
414 * blocking lock if allowed and required.
415 */
416
417 enum nlm_stats
418 do_lock(fl, block)
419 struct file_lock *fl;
420 int block;
421 {
422 int lflags, error;
423 struct stat st;
424
425 fl->fd = fhopen(&fl->filehandle, O_RDWR);
426 if (fl->fd < 0) {
427 switch (errno) {
428 case ESTALE:
429 error = nlm4_stale_fh;
430 break;
431 case EROFS:
432 error = nlm4_rofs;
433 break;
434 default:
435 error = nlm4_failed;
436 }
437 if ((fl->flags & LOCK_V4) == 0)
438 error = nlm_denied;
439 syslog(LOG_NOTICE, "fhopen failed (from %s): %s",
440 fl->client_name, strerror(errno));
441 LIST_REMOVE(fl, lcklst);
442 return error;
443 }
444 if (fstat(fl->fd, &st) < 0) {
445 syslog(LOG_NOTICE, "fstat failed (from %s): %s",
446 fl->client_name, strerror(errno));
447 }
448 syslog(LOG_DEBUG, "lock from %s for file%s%s: dev %d ino %d (uid %d), "
449 "flags %d",
450 fl->client_name, fl->client.exclusive ? " (exclusive)":"",
451 block ? " (block)":"",
452 st.st_dev, st.st_ino, st.st_uid, fl->flags);
453 lflags = LOCK_NB;
454 if (fl->client.exclusive == 0)
455 lflags |= LOCK_SH;
456 else
457 lflags |= LOCK_EX;
458 error = flock(fl->fd, lflags);
459 if (error != 0 && errno == EAGAIN && block) {
460 switch (fl->locker = fork()) {
461 case -1: /* fork failed */
462 syslog(LOG_NOTICE, "fork failed: %s", strerror(errno));
463 LIST_REMOVE(fl, lcklst);
464 close(fl->fd);
465 return (fl->flags & LOCK_V4) ?
466 nlm4_denied_nolock : nlm_denied_nolocks;
467 case 0:
468 /*
469 * Attempt a blocking lock. Will have to call
470 * NLM_GRANTED later.
471 */
472 setproctitle("%s", fl->client_name);
473 lflags &= ~LOCK_NB;
474 if(flock(fl->fd, lflags) != 0) {
475 syslog(LOG_NOTICE, "flock failed: %s",
476 strerror(errno));
477 _exit(1);
478 }
479 /* lock granted */
480 _exit(0);
481 default:
482 syslog(LOG_DEBUG, "lock request from %s: forked %d",
483 fl->client_name, fl->locker);
484 fl->status = LKST_PROCESSING;
485 return (fl->flags & LOCK_V4) ?
486 nlm4_blocked : nlm_blocked;
487 }
488 }
489 /* non block case */
490 if (error != 0) {
491 switch (errno) {
492 case EAGAIN:
493 error = nlm4_denied;
494 break;
495 case ESTALE:
496 error = nlm4_stale_fh;
497 break;
498 case EROFS:
499 error = nlm4_rofs;
500 break;
501 default:
502 error = nlm4_failed;
503 }
504 if ((fl->flags & LOCK_V4) == 0)
505 error = nlm_denied;
506 if (errno != EAGAIN)
507 syslog(LOG_NOTICE, "flock for %s failed: %s",
508 fl->client_name, strerror(errno));
509 else syslog(LOG_DEBUG, "flock for %s failed: %s",
510 fl->client_name, strerror(errno));
511 LIST_REMOVE(fl, lcklst);
512 close(fl->fd);
513 return error;
514 }
515 fl->status = LKST_LOCKED;
516 return (fl->flags & LOCK_V4) ? nlm4_granted : nlm_granted;
517 }
518
519 void
520 send_granted(fl, opcode)
521 struct file_lock *fl;
522 int opcode;
523 {
524 CLIENT *cli;
525 static char dummy;
526 struct timeval timeo;
527 int success;
528 static struct nlm_res retval;
529 static struct nlm4_res retval4;
530
531 cli = get_client(fl->addr,
532 (fl->flags & LOCK_V4) ? NLM_VERS4 : NLM_VERS);
533 if (cli == NULL) {
534 syslog(LOG_NOTICE, "failed to get CLIENT for %s",
535 fl->client_name);
536 /*
537 * We fail to notify remote that the lock has been granted.
538 * The client will timeout and retry, the lock will be
539 * granted at this time.
540 */
541 return;
542 }
543 timeo.tv_sec = 0;
544 timeo.tv_usec = (fl->flags & LOCK_ASYNC) ? 0 : 500000; /* 0.5s */
545
546 if (fl->flags & LOCK_V4) {
547 static nlm4_testargs res;
548 res.cookie = fl->client_cookie;
549 res.exclusive = fl->client.exclusive;
550 res.alock.caller_name = fl->client_name;
551 res.alock.fh.n_len = sizeof(fhandle_t);
552 res.alock.fh.n_bytes = (char*)&fl->filehandle;
553 res.alock.oh = fl->client.oh;
554 res.alock.svid = fl->client.svid;
555 res.alock.l_offset = fl->client.l_offset;
556 res.alock.l_len = fl->client.l_len;
557 syslog(LOG_DEBUG, "sending v4 reply%s",
558 (fl->flags & LOCK_ASYNC) ? " (async)":"");
559 if (fl->flags & LOCK_ASYNC) {
560 success = clnt_call(cli, NLM4_GRANTED_MSG,
561 xdr_nlm4_testargs, &res, xdr_void, &dummy, timeo);
562 } else {
563 success = clnt_call(cli, NLM4_GRANTED,
564 xdr_nlm4_testargs, &res, xdr_nlm4_res,
565 &retval4, timeo);
566 }
567 } else {
568 static nlm_testargs res;
569
570 res.cookie = fl->client_cookie;
571 res.exclusive = fl->client.exclusive;
572 res.alock.caller_name = fl->client_name;
573 res.alock.fh.n_len = sizeof(fhandle_t);
574 res.alock.fh.n_bytes = (char*)&fl->filehandle;
575 res.alock.oh = fl->client.oh;
576 res.alock.svid = fl->client.svid;
577 res.alock.l_offset = fl->client.l_offset;
578 res.alock.l_len = fl->client.l_len;
579 syslog(LOG_DEBUG, "sending v1 reply%s",
580 (fl->flags & LOCK_ASYNC) ? " (async)":"");
581 if (fl->flags & LOCK_ASYNC) {
582 success = clnt_call(cli, NLM_GRANTED_MSG,
583 xdr_nlm_testargs, &res, xdr_void, &dummy, timeo);
584 } else {
585 success = clnt_call(cli, NLM_GRANTED,
586 xdr_nlm_testargs, &res, xdr_nlm_res,
587 &retval, timeo);
588 }
589 }
590 if (debug_level > 2)
591 syslog(LOG_DEBUG, "clnt_call returns %d(%s) for granted",
592 success, clnt_sperrno(success));
593
594 }
595
596 enum nlm_stats
597 do_unlock(rfl)
598 struct file_lock *rfl;
599 {
600 struct file_lock *fl;
601 int error;
602 int lockst;
603
604 /* unlock the file: closing is enough ! */
605 if (close(rfl->fd) < 0) {
606 if (errno == ESTALE)
607 error = nlm4_stale_fh;
608 else
609 error = nlm4_failed;
610 if ((fl->flags & LOCK_V4) == 0)
611 error = nlm_denied;
612 syslog(LOG_NOTICE,
613 "close failed (from %s): %s",
614 rfl->client_name, strerror(errno));
615 } else {
616 error = (fl->flags & LOCK_V4) ?
617 nlm4_granted : nlm_granted;
618 }
619 LIST_REMOVE(rfl, lcklst);
620
621 /* process the next LKST_WAITING lock request for this fh */
622 for (fl = LIST_FIRST(&lcklst_head); fl != NULL;
623 fl = LIST_NEXT(fl, lcklst)) {
624 if (fl->status != LKST_WAITING ||
625 memcmp(&rfl->filehandle, &fl->filehandle,
626 sizeof(fhandle_t)) != 0)
627 continue;
628
629 lockst = do_lock(fl, 1); /* If it's LKST_WAITING we can block */
630 switch (lockst) {
631 case nlm4_granted:
632 /* case nlm_granted: same as nlm4_granted */
633 send_granted(fl, (fl->flags & LOCK_V4) ?
634 nlm4_granted : nlm_granted);
635 break;
636 case nlm4_blocked:
637 /* case nlm_blocked: same as nlm4_blocked */
638 break;
639 default:
640 lfree(fl);
641 break;
642 }
643 break;
644 }
645 lfree(rfl);
646 return error;
647 }
648
649 void
650 siglock()
651 {
652 sigset_t block;
653
654 sigemptyset(&block);
655 sigaddset(&block, SIGCHLD);
656
657 if (sigprocmask(SIG_BLOCK, &block, NULL) < 0) {
658 syslog(LOG_WARNING, "siglock failed: %s", strerror(errno));
659 }
660 }
661
662 void
663 sigunlock()
664 {
665 sigset_t block;
666
667 sigemptyset(&block);
668 sigaddset(&block, SIGCHLD);
669
670 if (sigprocmask(SIG_UNBLOCK, &block, NULL) < 0) {
671 syslog(LOG_WARNING, "sigunlock failed: %s", strerror(errno));
672 }
673 }
674
675 /* monitor a host through rpc.statd, and keep a ref count */
676 void
677 do_mon(hostname)
678 char *hostname;
679 {
680 struct host *hp;
681 struct mon my_mon;
682 struct sm_stat_res res;
683 int retval;
684
685 for (hp = LIST_FIRST(&hostlst_head); hp != NULL;
686 hp = LIST_NEXT(hp, hostlst)) {
687 if (strcmp(hostname, hp->name) == 0) {
688 /* already monitored, just bump refcnt */
689 hp->refcnt++;
690 return;
691 }
692 }
693 /* not found, have to create an entry for it */
694 hp = malloc(sizeof(struct host));
695 strncpy(hp->name, hostname, SM_MAXSTRLEN);
696 hp->refcnt = 1;
697 syslog(LOG_DEBUG, "monitoring host %s",
698 hostname);
699 memset(&my_mon, 0, sizeof(my_mon));
700 my_mon.mon_id.mon_name = hp->name;
701 my_mon.mon_id.my_id.my_name = "localhost";
702 my_mon.mon_id.my_id.my_prog = NLM_PROG;
703 my_mon.mon_id.my_id.my_vers = NLM_SM;
704 my_mon.mon_id.my_id.my_proc = NLM_SM_NOTIFY;
705 if ((retval =
706 callrpc("localhost", SM_PROG, SM_VERS, SM_MON, xdr_mon,
707 (char*)&my_mon, xdr_sm_stat_res, (char*)&res)) != 0) {
708 syslog(LOG_WARNING, "rpc to statd failed: %s",
709 clnt_sperrno((enum clnt_stat)retval));
710 free(hp);
711 return;
712 }
713 if (res.res_stat == stat_fail) {
714 syslog(LOG_WARNING, "statd failed");
715 free(hp);
716 return;
717 }
718 LIST_INSERT_HEAD(&hostlst_head, hp, hostlst);
719 }
720
721 void
722 notify(hostname, state)
723 char *hostname;
724 int state;
725 {
726 struct file_lock *fl, *next_fl;
727 int err;
728 syslog(LOG_DEBUG, "notify from %s, new state %d", hostname, state);
729 /* search all lock for this host; if status changed, release the lock */
730 siglock();
731 for (fl = LIST_FIRST(&lcklst_head); fl != NULL; fl = next_fl) {
732 next_fl = LIST_NEXT(fl, lcklst);
733 if (strcmp(hostname, fl->client_name) == 0 &&
734 fl->nsm_status != state) {
735 syslog(LOG_DEBUG, "state %d, nsm_state %d, unlocking",
736 fl->status, fl->nsm_status);
737 switch(fl->status) {
738 case LKST_LOCKED:
739 err = do_unlock(fl);
740 if (err != nlm_granted)
741 syslog(LOG_DEBUG,
742 "notify: unlock failed for %s (%d)",
743 hostname, err);
744 break;
745 case LKST_WAITING:
746 LIST_REMOVE(fl, lcklst);
747 lfree(fl);
748 break;
749 case LKST_PROCESSING:
750 fl->status = LKST_DYING;
751 break;
752 case LKST_DYING:
753 break;
754 default:
755 syslog(LOG_NOTICE, "unknow status %d for %s",
756 fl->status, fl->client_name);
757 }
758 }
759 }
760 sigunlock();
761 }
762