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