xref: /titanic_44/usr/src/lib/libnisdb/yptol/lock_update.c (revision 883492d5a933deb34cd27521e7f2756773cd27af)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*883492d5Sraf  * Common Development and Distribution License (the "License").
6*883492d5Sraf  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21*883492d5Sraf 
227c478bd9Sstevel@tonic-gate /*
23*883492d5Sraf  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * DESCRIPTION: Contains code supporting the 'update in progress' flag. This is
317c478bd9Sstevel@tonic-gate  *		a near copy of lock flag code (in
327c478bd9Sstevel@tonic-gate  *		usr/src/cmd/ypcmd/shared/lockmp.c) If we implement a clean
337c478bd9Sstevel@tonic-gate  *		version	of the locking code this file will probably disappear.
347c478bd9Sstevel@tonic-gate  *
357c478bd9Sstevel@tonic-gate  *		These locks are held while a map is being updated from the
367c478bd9Sstevel@tonic-gate  *		DIT. They prevent a second update being started while this is
377c478bd9Sstevel@tonic-gate  *		in progress. This is independant from the `lockmap` mechanism
387c478bd9Sstevel@tonic-gate  *		which protects maps, generally for a much shorter period,
397c478bd9Sstevel@tonic-gate  *		while their control structures are modified.
407c478bd9Sstevel@tonic-gate  */
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #include <unistd.h>
437c478bd9Sstevel@tonic-gate #include <syslog.h>
447c478bd9Sstevel@tonic-gate #include <sys/mman.h>
457c478bd9Sstevel@tonic-gate #include <thread.h>
467c478bd9Sstevel@tonic-gate #include <synch.h>
477c478bd9Sstevel@tonic-gate #include <ndbm.h>
487c478bd9Sstevel@tonic-gate #include <strings.h>
497c478bd9Sstevel@tonic-gate #include "ypsym.h"
507c478bd9Sstevel@tonic-gate #include "shim.h"
517c478bd9Sstevel@tonic-gate #include "yptol.h"
527c478bd9Sstevel@tonic-gate #include "../ldap_util.h"
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate #define	LOCKFILE "/var/run/yp_mapupdate"
557c478bd9Sstevel@tonic-gate struct updatearray {
567c478bd9Sstevel@tonic-gate 	mutex_t		updatenode[MAXHASH];
577c478bd9Sstevel@tonic-gate };
587c478bd9Sstevel@tonic-gate typedef struct updatearray updatearray;
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate /*
617c478bd9Sstevel@tonic-gate  * Cross-process robust mutex locks.
627c478bd9Sstevel@tonic-gate  * Provide synchronization between YP processes
637c478bd9Sstevel@tonic-gate  * by implementing an exclusive locking mechanism
647c478bd9Sstevel@tonic-gate  * via a memory-mapped file.
657c478bd9Sstevel@tonic-gate  */
667c478bd9Sstevel@tonic-gate static struct updatearray	*shmupdatearray;
677c478bd9Sstevel@tonic-gate static int	lockfile;
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate bool_t
init_update_locks_mem()707c478bd9Sstevel@tonic-gate init_update_locks_mem()
717c478bd9Sstevel@tonic-gate {
727c478bd9Sstevel@tonic-gate 	int iiter, rc;
737c478bd9Sstevel@tonic-gate 	int ebusy_cnt = 0;
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate 	/*
767c478bd9Sstevel@tonic-gate 	 * Initialize cross-process locks in memory-mapped file.
777c478bd9Sstevel@tonic-gate 	 */
787c478bd9Sstevel@tonic-gate 	for (iiter = 0; iiter < MAXHASH; iiter++) {
797c478bd9Sstevel@tonic-gate 		if (rc = mutex_init(&(shmupdatearray->updatenode[iiter]),
80*883492d5Sraf 		    USYNC_PROCESS | LOCK_ROBUST, 0)) {
817c478bd9Sstevel@tonic-gate 			if (rc == EBUSY) {
827c478bd9Sstevel@tonic-gate 				ebusy_cnt++;
837c478bd9Sstevel@tonic-gate 			} else {
847c478bd9Sstevel@tonic-gate 				logmsg(MSG_NOTIMECHECK, LOG_ERR,
857c478bd9Sstevel@tonic-gate 					"init_update_locks_mem():mutex_init():"
867c478bd9Sstevel@tonic-gate 					"error=%d", rc);
877c478bd9Sstevel@tonic-gate 				return (FALSE);
887c478bd9Sstevel@tonic-gate 			}
897c478bd9Sstevel@tonic-gate 		}
907c478bd9Sstevel@tonic-gate 	}
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 	/*
937c478bd9Sstevel@tonic-gate 	 * EBUSY for all locks OK, it means another process
947c478bd9Sstevel@tonic-gate 	 * has already initialized locks.
957c478bd9Sstevel@tonic-gate 	 */
967c478bd9Sstevel@tonic-gate 	if ((ebusy_cnt > 0) && (ebusy_cnt != MAXHASH)) {
977c478bd9Sstevel@tonic-gate 		logmsg(MSG_NOTIMECHECK, LOG_ERR,
987c478bd9Sstevel@tonic-gate 		"%s inconsistent. Remove this file and restart NIS (YP)",
997c478bd9Sstevel@tonic-gate 								LOCKFILE);
1007c478bd9Sstevel@tonic-gate 		return (FALSE);
1017c478bd9Sstevel@tonic-gate 	}
1027c478bd9Sstevel@tonic-gate 	return (TRUE);
1037c478bd9Sstevel@tonic-gate }
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate bool_t
init_update_lock_map()1067c478bd9Sstevel@tonic-gate init_update_lock_map()
1077c478bd9Sstevel@tonic-gate {
1087c478bd9Sstevel@tonic-gate 	char buff[ sizeof (updatearray) ];
1097c478bd9Sstevel@tonic-gate 	int write_cnt, lf_size;
1107c478bd9Sstevel@tonic-gate 	struct stat fdata;
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	/*
1137c478bd9Sstevel@tonic-gate 	 * Locking file initialization algorithm, with recovery mechanism.
1147c478bd9Sstevel@tonic-gate 	 * This mechanism has been devised to ensure proper creation
1157c478bd9Sstevel@tonic-gate 	 * of a memory-mapped lock file containing mutexes for robust,
1167c478bd9Sstevel@tonic-gate 	 * inter-process communication.
1177c478bd9Sstevel@tonic-gate 	 * File name is /var/run/yp_mapupate (LOCKFILE).  It might or might
1187c478bd9Sstevel@tonic-gate 	 * not exist.
1197c478bd9Sstevel@tonic-gate 	 *
1207c478bd9Sstevel@tonic-gate 	 * Algorithm:
1217c478bd9Sstevel@tonic-gate 	 * Try to open the file. If file doesn't exist, or size is too small,
1227c478bd9Sstevel@tonic-gate 	 * create/rewrite the file, m-map it into memory and initialize the
1237c478bd9Sstevel@tonic-gate 	 * mutexes in it.
1247c478bd9Sstevel@tonic-gate 	 * If file exists and size is at least large enough, assume it's a
1257c478bd9Sstevel@tonic-gate 	 * good file, and m-map the lock structure directly to it.
1267c478bd9Sstevel@tonic-gate 	 *
1277c478bd9Sstevel@tonic-gate 	 * Recovery from inconsistent state is easy - simply delete the file
1287c478bd9Sstevel@tonic-gate 	 * and restart NIS (YP).
1297c478bd9Sstevel@tonic-gate 	 */
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	lockfile = open(LOCKFILE, O_RDWR|O_CREAT, 0600);
1327c478bd9Sstevel@tonic-gate 	if (lockfile != -1) {
1337c478bd9Sstevel@tonic-gate 		if (lockf(lockfile, F_LOCK, 0) == 0) {
1347c478bd9Sstevel@tonic-gate 			if (fstat(lockfile, &fdata) == 0) {
1357c478bd9Sstevel@tonic-gate 				lf_size = fdata.st_size;
1367c478bd9Sstevel@tonic-gate 				if (lf_size < sizeof (updatearray)) {
1377c478bd9Sstevel@tonic-gate 					bzero(buff, sizeof (buff));
1387c478bd9Sstevel@tonic-gate 					if ((write_cnt = write(lockfile, buff,
1397c478bd9Sstevel@tonic-gate 					    sizeof (buff)) != sizeof (buff))) {
1407c478bd9Sstevel@tonic-gate 						if (write_cnt < 0) {
1417c478bd9Sstevel@tonic-gate 							logmsg(MSG_NOTIMECHECK,
1427c478bd9Sstevel@tonic-gate 								LOG_ERR,
1437c478bd9Sstevel@tonic-gate 						"write(%s) => errno=%d",
1447c478bd9Sstevel@tonic-gate 							    LOCKFILE, errno);
1457c478bd9Sstevel@tonic-gate 						} else {
1467c478bd9Sstevel@tonic-gate 							logmsg(MSG_NOTIMECHECK,
1477c478bd9Sstevel@tonic-gate 								LOG_ERR,
1487c478bd9Sstevel@tonic-gate 		    "write(%s) => %d!=%d: wrong number of bytes written",
1497c478bd9Sstevel@tonic-gate 							    LOCKFILE,
1507c478bd9Sstevel@tonic-gate 							    write_cnt,
1517c478bd9Sstevel@tonic-gate 							    sizeof (buff));
1527c478bd9Sstevel@tonic-gate 						}
1537c478bd9Sstevel@tonic-gate 						lockf(lockfile, F_ULOCK, 0);
1547c478bd9Sstevel@tonic-gate 						close(lockfile);
1557c478bd9Sstevel@tonic-gate 						return (FALSE);
1567c478bd9Sstevel@tonic-gate 					}
1577c478bd9Sstevel@tonic-gate 				}
1587c478bd9Sstevel@tonic-gate 			} else {
1597c478bd9Sstevel@tonic-gate 				logmsg(MSG_NOTIMECHECK, LOG_ERR,
1607c478bd9Sstevel@tonic-gate 				    "fstat(%s) => errno=%d", LOCKFILE, errno);
1617c478bd9Sstevel@tonic-gate 				lockf(lockfile, F_ULOCK, 0);
1627c478bd9Sstevel@tonic-gate 				close(lockfile);
1637c478bd9Sstevel@tonic-gate 				return (FALSE);
1647c478bd9Sstevel@tonic-gate 			}
1657c478bd9Sstevel@tonic-gate 		} else {
1667c478bd9Sstevel@tonic-gate 			logmsg(MSG_NOTIMECHECK, LOG_ERR,
1677c478bd9Sstevel@tonic-gate 			    "lockf(%s,F_LOCK) => errno=%d", LOCKFILE, errno);
1687c478bd9Sstevel@tonic-gate 			close(lockfile);
1697c478bd9Sstevel@tonic-gate 			return (FALSE);
1707c478bd9Sstevel@tonic-gate 		}
1717c478bd9Sstevel@tonic-gate 	} else {
1727c478bd9Sstevel@tonic-gate 		logmsg(MSG_NOTIMECHECK, LOG_ERR,
1737c478bd9Sstevel@tonic-gate 			"open(%s) => errno=%d", LOCKFILE, errno);
1747c478bd9Sstevel@tonic-gate 		return (FALSE);
1757c478bd9Sstevel@tonic-gate 	}
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	/*
1787c478bd9Sstevel@tonic-gate 	 * File exists with correct size, is open, and we're holding
1797c478bd9Sstevel@tonic-gate 	 * the file lock.
1807c478bd9Sstevel@tonic-gate 	 */
1817c478bd9Sstevel@tonic-gate 	shmupdatearray = (updatearray *)mmap((caddr_t)0, sizeof (updatearray),
1827c478bd9Sstevel@tonic-gate 	    PROT_READ | PROT_WRITE, MAP_SHARED, lockfile, 0);
1837c478bd9Sstevel@tonic-gate 	if (shmupdatearray == MAP_FAILED) {
1847c478bd9Sstevel@tonic-gate 		logmsg(MSG_NOTIMECHECK, LOG_ERR,
1857c478bd9Sstevel@tonic-gate 				"mmap(%s) => errno=%d", LOCKFILE, errno);
1867c478bd9Sstevel@tonic-gate 		lockf(lockfile, F_ULOCK, 0);
1877c478bd9Sstevel@tonic-gate 		close(lockfile);
1887c478bd9Sstevel@tonic-gate 		return (FALSE);
1897c478bd9Sstevel@tonic-gate 	}
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	/*
1927c478bd9Sstevel@tonic-gate 	 * If we wrote zeroes to the file, we also need to initialize
1937c478bd9Sstevel@tonic-gate 	 * the mutex locks.
1947c478bd9Sstevel@tonic-gate 	 */
1957c478bd9Sstevel@tonic-gate 	if (lf_size < sizeof (updatearray)) {
1967c478bd9Sstevel@tonic-gate 		if (init_update_locks_mem() == FALSE) {
1977c478bd9Sstevel@tonic-gate 			lockf(lockfile, F_ULOCK, 0);
1987c478bd9Sstevel@tonic-gate 			close(lockfile);
1997c478bd9Sstevel@tonic-gate 			if (remove(LOCKFILE) != 0) {
2007c478bd9Sstevel@tonic-gate 				logmsg(MSG_NOTIMECHECK, LOG_ERR,
2017c478bd9Sstevel@tonic-gate 				"remove(%s) => errno=%d: Please delete file",
2027c478bd9Sstevel@tonic-gate 							LOCKFILE, errno);
2037c478bd9Sstevel@tonic-gate 			}
2047c478bd9Sstevel@tonic-gate 			return (FALSE);
2057c478bd9Sstevel@tonic-gate 		}
2067c478bd9Sstevel@tonic-gate 	}
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	if (lockf(lockfile, F_ULOCK, 0) != 0) {
2097c478bd9Sstevel@tonic-gate 		logmsg(MSG_NOTIMECHECK, LOG_ERR,
2107c478bd9Sstevel@tonic-gate 			"lockf(%s,F_ULOCK) => errno=%d", LOCKFILE, errno);
2117c478bd9Sstevel@tonic-gate 		close(lockfile);
2127c478bd9Sstevel@tonic-gate 		return (FALSE);
2137c478bd9Sstevel@tonic-gate 	}
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	if (close(lockfile) == 0) {
2167c478bd9Sstevel@tonic-gate 		return (TRUE);
2177c478bd9Sstevel@tonic-gate 	} else {
2187c478bd9Sstevel@tonic-gate 		logmsg(MSG_NOTIMECHECK, LOG_ERR,
2197c478bd9Sstevel@tonic-gate 				"close(%s) => errno=%d", LOCKFILE, errno);
2207c478bd9Sstevel@tonic-gate 		return (FALSE);
2217c478bd9Sstevel@tonic-gate 	}
2227c478bd9Sstevel@tonic-gate }
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate suc_code
lock_map_update(map_ctrl * map)2257c478bd9Sstevel@tonic-gate lock_map_update(map_ctrl *map)
2267c478bd9Sstevel@tonic-gate {
2277c478bd9Sstevel@tonic-gate 	int hashval = map->hash_val;
2287c478bd9Sstevel@tonic-gate 	int rc;
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	/*
2317c478bd9Sstevel@tonic-gate 	 * Robust, cross-process lock implementation
2327c478bd9Sstevel@tonic-gate 	 */
2337c478bd9Sstevel@tonic-gate 	rc = mutex_lock(&(shmupdatearray->updatenode[hashval]));
2347c478bd9Sstevel@tonic-gate 	while (rc != 0) {
2357c478bd9Sstevel@tonic-gate 		switch (rc) {
2367c478bd9Sstevel@tonic-gate 		case EOWNERDEAD:
2377c478bd9Sstevel@tonic-gate 			/*
2387c478bd9Sstevel@tonic-gate 			 * Previous lock owner died, resetting lock
2397c478bd9Sstevel@tonic-gate 			 * to recover from error.
2407c478bd9Sstevel@tonic-gate 			 */
241*883492d5Sraf 			rc = mutex_consistent(
242*883492d5Sraf 			    &(shmupdatearray->updatenode[hashval]));
2437c478bd9Sstevel@tonic-gate 			if (rc != 0) {
2447c478bd9Sstevel@tonic-gate 				logmsg(MSG_NOTIMECHECK, LOG_ERR,
245*883492d5Sraf 					"mutex_consistent(): error=%d", rc);
2467c478bd9Sstevel@tonic-gate 				return (FAILURE);
2477c478bd9Sstevel@tonic-gate 			}
2487c478bd9Sstevel@tonic-gate 			rc = mutex_unlock(
2497c478bd9Sstevel@tonic-gate 			    &(shmupdatearray->updatenode[hashval]));
2507c478bd9Sstevel@tonic-gate 			if (rc != 0) {
2517c478bd9Sstevel@tonic-gate 				logmsg(MSG_NOTIMECHECK, LOG_ERR,
2527c478bd9Sstevel@tonic-gate 					"mutex_unlock(): error=%d", rc);
2537c478bd9Sstevel@tonic-gate 				return (FAILURE);
2547c478bd9Sstevel@tonic-gate 			}
2557c478bd9Sstevel@tonic-gate 			break;
2567c478bd9Sstevel@tonic-gate 		default:
2577c478bd9Sstevel@tonic-gate 			/*
2587c478bd9Sstevel@tonic-gate 			 * Unrecoverable problem - nothing to do
2597c478bd9Sstevel@tonic-gate 			 * but exit YP and delete lock file.
2607c478bd9Sstevel@tonic-gate 			 */
2617c478bd9Sstevel@tonic-gate 			logmsg(MSG_NOTIMECHECK, LOG_ERR,
2627c478bd9Sstevel@tonic-gate 						"mutex_lock(): error=%d", rc);
2637c478bd9Sstevel@tonic-gate 			logmsg(MSG_NOTIMECHECK, LOG_ERR,
2647c478bd9Sstevel@tonic-gate 					"Please restart NIS (ypstop/ypstart)");
2657c478bd9Sstevel@tonic-gate 			if (remove(LOCKFILE) != 0) {
2667c478bd9Sstevel@tonic-gate 				logmsg(MSG_NOTIMECHECK, LOG_ERR,
2677c478bd9Sstevel@tonic-gate 				"remove(%s) => errno=%d: Please delete file",
2687c478bd9Sstevel@tonic-gate 							LOCKFILE, errno);
2697c478bd9Sstevel@tonic-gate 			}
2707c478bd9Sstevel@tonic-gate 			return (FAILURE);
2717c478bd9Sstevel@tonic-gate 		}
2727c478bd9Sstevel@tonic-gate 		rc = mutex_lock(&(shmupdatearray->updatenode[hashval]));
2737c478bd9Sstevel@tonic-gate 	}
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 	/* Success */
2767c478bd9Sstevel@tonic-gate 	return (SUCCESS);
2777c478bd9Sstevel@tonic-gate }
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate suc_code
unlock_map_update(map_ctrl * map)2817c478bd9Sstevel@tonic-gate unlock_map_update(map_ctrl *map)
2827c478bd9Sstevel@tonic-gate {
2837c478bd9Sstevel@tonic-gate 	int hashval = map->hash_val;
2847c478bd9Sstevel@tonic-gate 	int rc;
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 	rc = mutex_unlock(&(shmupdatearray->updatenode[hashval]));
2877c478bd9Sstevel@tonic-gate 	if (rc != 0) {
2887c478bd9Sstevel@tonic-gate 		logmsg(MSG_NOTIMECHECK, LOG_ERR,
2897c478bd9Sstevel@tonic-gate 						"mutex_unlock(): error=%d", rc);
2907c478bd9Sstevel@tonic-gate 		logmsg(MSG_NOTIMECHECK, LOG_ERR,
2917c478bd9Sstevel@tonic-gate 					"Please restart NIS (ypstop/ypstart)");
2927c478bd9Sstevel@tonic-gate 		if (remove(LOCKFILE) != 0) {
2937c478bd9Sstevel@tonic-gate 			logmsg(MSG_NOTIMECHECK, LOG_ERR,
2947c478bd9Sstevel@tonic-gate 			    "remove(%s) => errno=%d: Please delete file",
2957c478bd9Sstevel@tonic-gate 			    LOCKFILE, errno);
2967c478bd9Sstevel@tonic-gate 		}
2977c478bd9Sstevel@tonic-gate 		return (FAILURE);
2987c478bd9Sstevel@tonic-gate 	}
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 	/* Success */
3017c478bd9Sstevel@tonic-gate 	return (SUCCESS);
3027c478bd9Sstevel@tonic-gate }
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate /*
3057c478bd9Sstevel@tonic-gate  * FUNCTION :   is_map_updating()
3067c478bd9Sstevel@tonic-gate  *
3077c478bd9Sstevel@tonic-gate  * DESCRIPTION: Determines if a map is currently locked for update
3087c478bd9Sstevel@tonic-gate  *
3097c478bd9Sstevel@tonic-gate  * GIVEN :      Pointer to map_ctrl structure
3107c478bd9Sstevel@tonic-gate  *
3117c478bd9Sstevel@tonic-gate  * RETURNS :    TRUE = Map is locked
3127c478bd9Sstevel@tonic-gate  *              FALSE = Map is not locked
3137c478bd9Sstevel@tonic-gate  */
3147c478bd9Sstevel@tonic-gate bool_t
is_map_updating(map_ctrl * map)3157c478bd9Sstevel@tonic-gate is_map_updating(map_ctrl *map)
3167c478bd9Sstevel@tonic-gate {
3177c478bd9Sstevel@tonic-gate 	int ret;
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 	/* It appears not to be possible to just read a mutex. Try to lock it */
3207c478bd9Sstevel@tonic-gate 	ret = mutex_trylock(&(shmupdatearray->updatenode[map->hash_val]));
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	if (0 != ret) {
3237c478bd9Sstevel@tonic-gate 		/* Didn't get the lock ... was already locked */
3247c478bd9Sstevel@tonic-gate 		return (TRUE);
3257c478bd9Sstevel@tonic-gate 	}
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 	/* Didn't need the lock so free it again */
3287c478bd9Sstevel@tonic-gate 	mutex_unlock(&(shmupdatearray->updatenode[map->hash_val]));
3297c478bd9Sstevel@tonic-gate 	return (FALSE);
3307c478bd9Sstevel@tonic-gate }
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate /*
3337c478bd9Sstevel@tonic-gate  * FUNCTION :	try_lock_map_update()
3347c478bd9Sstevel@tonic-gate  *
3357c478bd9Sstevel@tonic-gate  * DESCRIPTION: Tries to to lock a map for update.
3367c478bd9Sstevel@tonic-gate  *
3377c478bd9Sstevel@tonic-gate  * GIVEN :	Pointer to the map to lock
3387c478bd9Sstevel@tonic-gate  *
3397c478bd9Sstevel@tonic-gate  * RETURNS :	0 = The map is now locked
3407c478bd9Sstevel@tonic-gate  *		EBUSY = The map was already locked lock not obtained.
3417c478bd9Sstevel@tonic-gate  *		Other = There was an error
3427c478bd9Sstevel@tonic-gate  */
3437c478bd9Sstevel@tonic-gate int
try_lock_map_update(map_ctrl * map)3447c478bd9Sstevel@tonic-gate try_lock_map_update(map_ctrl *map)
3457c478bd9Sstevel@tonic-gate {
3467c478bd9Sstevel@tonic-gate 	int hashval = map->hash_val;
3477c478bd9Sstevel@tonic-gate 	int rc;
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 	/*
3507c478bd9Sstevel@tonic-gate 	 * Robust, cross-process lock implementation
3517c478bd9Sstevel@tonic-gate 	 *
3527c478bd9Sstevel@tonic-gate 	 * Keep trying until either lock is obtained or somebody else gets it.
3537c478bd9Sstevel@tonic-gate 	 */
3547c478bd9Sstevel@tonic-gate 	while (1) {
3557c478bd9Sstevel@tonic-gate 		rc = mutex_trylock(&(shmupdatearray->updatenode[hashval]));
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 		switch (rc) {
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 		case 0:
3607c478bd9Sstevel@tonic-gate 		case EBUSY:
3617c478bd9Sstevel@tonic-gate 			/* Either got it or somebody else has it */
3627c478bd9Sstevel@tonic-gate 			return (rc);
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 		case EOWNERDEAD:
3657c478bd9Sstevel@tonic-gate 			/*
3667c478bd9Sstevel@tonic-gate 			 * Previous lock owner died, resetting lock
3677c478bd9Sstevel@tonic-gate 			 * to recover from error.
3687c478bd9Sstevel@tonic-gate 			 */
369*883492d5Sraf 			rc = mutex_consistent(
370*883492d5Sraf 			    &(shmupdatearray->updatenode[hashval]));
3717c478bd9Sstevel@tonic-gate 			if (rc != 0) {
3727c478bd9Sstevel@tonic-gate 				logmsg(MSG_NOTIMECHECK, LOG_ERR,
373*883492d5Sraf 					"mutex_consistent(): error=%d", rc);
3747c478bd9Sstevel@tonic-gate 				return (rc);
3757c478bd9Sstevel@tonic-gate 			}
3767c478bd9Sstevel@tonic-gate 			rc = mutex_unlock(
3777c478bd9Sstevel@tonic-gate 			    &(shmupdatearray->updatenode[hashval]));
3787c478bd9Sstevel@tonic-gate 			if (rc != 0) {
3797c478bd9Sstevel@tonic-gate 				logmsg(MSG_NOTIMECHECK, LOG_ERR,
3807c478bd9Sstevel@tonic-gate 					"mutex_unlock(): error=%d", rc);
3817c478bd9Sstevel@tonic-gate 				return (rc);
3827c478bd9Sstevel@tonic-gate 			}
3837c478bd9Sstevel@tonic-gate 			break;
3847c478bd9Sstevel@tonic-gate 		default:
3857c478bd9Sstevel@tonic-gate 			/*
3867c478bd9Sstevel@tonic-gate 			 * Unrecoverable problem - nothing to do
3877c478bd9Sstevel@tonic-gate 			 * but exit YP and delete lock file.
3887c478bd9Sstevel@tonic-gate 			 */
3897c478bd9Sstevel@tonic-gate 			logmsg(MSG_NOTIMECHECK, LOG_ERR,
3907c478bd9Sstevel@tonic-gate 						"mutex_lock(): error=%d", rc);
3917c478bd9Sstevel@tonic-gate 			logmsg(MSG_NOTIMECHECK, LOG_ERR,
3927c478bd9Sstevel@tonic-gate 					"Please restart NIS (ypstop/ypstart)");
3937c478bd9Sstevel@tonic-gate 			if (remove(LOCKFILE) != 0) {
3947c478bd9Sstevel@tonic-gate 				logmsg(MSG_NOTIMECHECK, LOG_ERR,
3957c478bd9Sstevel@tonic-gate 				"remove(%s) => errno=%d: Please delete file",
3967c478bd9Sstevel@tonic-gate 							LOCKFILE, errno);
3977c478bd9Sstevel@tonic-gate 			}
3987c478bd9Sstevel@tonic-gate 			return (rc);
3997c478bd9Sstevel@tonic-gate 		}
4007c478bd9Sstevel@tonic-gate 	}
4017c478bd9Sstevel@tonic-gate }
402