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