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