xref: /illumos-gate/usr/src/cmd/ypcmd/shared/lockmap.c (revision 4a190493)
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
53f95ae75Ssm26363  * Common Development and Distribution License (the "License").
63f95ae75Ssm26363  * 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  */
217c478bd9Sstevel@tonic-gate /*
223f95ae75Ssm26363  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <unistd.h>
297c478bd9Sstevel@tonic-gate #include <syslog.h>
307c478bd9Sstevel@tonic-gate #include <sys/mman.h>
317c478bd9Sstevel@tonic-gate #include <thread.h>
327c478bd9Sstevel@tonic-gate #include <synch.h>
33*4a190493Ssdussud #include <strings.h>
347c478bd9Sstevel@tonic-gate #include <ndbm.h>
357c478bd9Sstevel@tonic-gate #include "../ypsym.h"
367c478bd9Sstevel@tonic-gate #include "../ypdefs.h"
37*4a190493Ssdussud #include "shim.h"
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate /*
407c478bd9Sstevel@tonic-gate  *  These routines provide mutual exclusion between ypserv and ypxfr.
417c478bd9Sstevel@tonic-gate  *  Mutual exclusion is needed so that ypxfr doesn't try to rename
427c478bd9Sstevel@tonic-gate  *  dbm files while ypserv is trying to open them.  After ypserv has
437c478bd9Sstevel@tonic-gate  *  opened a dbm file, it is safe to rename it because ypserv still
447c478bd9Sstevel@tonic-gate  *  has access to the file through its file descriptor.
457c478bd9Sstevel@tonic-gate  */
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate #define	LOCKFILE "/var/run/yp_maplock"
487c478bd9Sstevel@tonic-gate struct lockarray {
497c478bd9Sstevel@tonic-gate 	mutex_t		locknode[MAXHASH];
507c478bd9Sstevel@tonic-gate };
517c478bd9Sstevel@tonic-gate typedef struct lockarray lockarray;
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate /*
547c478bd9Sstevel@tonic-gate  * Cross-process robust mutex locks.
557c478bd9Sstevel@tonic-gate  * Provide synchronization between YP processes
567c478bd9Sstevel@tonic-gate  * by implementing an exclusive locking mechanism
577c478bd9Sstevel@tonic-gate  * via a memory-mapped file.
587c478bd9Sstevel@tonic-gate  */
597c478bd9Sstevel@tonic-gate static struct lockarray	*shmlockarray;
607c478bd9Sstevel@tonic-gate static int	lockfile;
617c478bd9Sstevel@tonic-gate 
62*4a190493Ssdussud /*
63*4a190493Ssdussud  * Hash functions, used for by the locking mechanism.
64*4a190493Ssdussud  *
65*4a190493Ssdussud  * - hash() is the front-end function that gets called.
66*4a190493Ssdussud  * - get_map_id() returns a unique int value per map.
67*4a190493Ssdussud  *      It is used in N2L mode only.
68*4a190493Ssdussud  *      It is called by hash() in N2L mode.
69*4a190493Ssdussud  */
70*4a190493Ssdussud int
71*4a190493Ssdussud get_map_id(char *map_name, int index)
72*4a190493Ssdussud {
73*4a190493Ssdussud 	map_id_elt_t *cur_elt;
74*4a190493Ssdussud 	/*
75*4a190493Ssdussud 	 * Local references to hash table for map lists
76*4a190493Ssdussud 	 * and to max number of maps
77*4a190493Ssdussud 	 */
78*4a190493Ssdussud 	map_id_elt_t **map_list_p;
79*4a190493Ssdussud 	int max_map;
80*4a190493Ssdussud 
81*4a190493Ssdussud 	/* initializes map_list_p & max_map */
82*4a190493Ssdussud 	get_list_max(&map_list_p, &max_map);
83*4a190493Ssdussud 
84*4a190493Ssdussud 	cur_elt = map_list_p[index];
85*4a190493Ssdussud 	while (cur_elt != NULL) {
86*4a190493Ssdussud 		if (strcmp(map_name, cur_elt->map_name) == 0) {
87*4a190493Ssdussud 			/* found */
88*4a190493Ssdussud 			return (cur_elt->map_id);
89*4a190493Ssdussud 		}
90*4a190493Ssdussud 		cur_elt = cur_elt->next;
91*4a190493Ssdussud 	}
92*4a190493Ssdussud 	syslog(LOG_WARNING, "get_map_id: no hash id found for %s"
93*4a190493Ssdussud 		", giving max_map value (%d)",
94*4a190493Ssdussud 		map_name, max_map);
95*4a190493Ssdussud 	/*
96*4a190493Ssdussud 	 * max_map does not match any map id, hence
97*4a190493Ssdussud 	 * will not trigger any lock collision
98*4a190493Ssdussud 	 * with existing maps.
99*4a190493Ssdussud 	 * Needed for yp regular locking mechanism.
100*4a190493Ssdussud 	 */
101*4a190493Ssdussud 	return (max_map);
102*4a190493Ssdussud }
103*4a190493Ssdussud 
1047c478bd9Sstevel@tonic-gate int
1057c478bd9Sstevel@tonic-gate hash(char *s)
1067c478bd9Sstevel@tonic-gate {
1073f95ae75Ssm26363 	unsigned int n = 0;
1087c478bd9Sstevel@tonic-gate 	int i;
109*4a190493Ssdussud 	char *map_name = s;
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 	for (i = 1; *s; i += 10, s++) {
1127c478bd9Sstevel@tonic-gate 		n += i * (*s);
1137c478bd9Sstevel@tonic-gate 	}
1147c478bd9Sstevel@tonic-gate 	n %= MAXHASH;
115*4a190493Ssdussud 
116*4a190493Ssdussud 	if (yptol_mode & yptol_newlock) {
117*4a190493Ssdussud 		return (get_map_id(map_name, n));
118*4a190493Ssdussud 	} else {
1197c478bd9Sstevel@tonic-gate 		return (n);
1207c478bd9Sstevel@tonic-gate 	}
121*4a190493Ssdussud }
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate bool
1247c478bd9Sstevel@tonic-gate init_locks_mem()
1257c478bd9Sstevel@tonic-gate {
1267c478bd9Sstevel@tonic-gate 	int iiter, rc;
1277c478bd9Sstevel@tonic-gate 	int ebusy_cnt = 0;
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	/*
1307c478bd9Sstevel@tonic-gate 	 * Initialize cross-process locks in memory-mapped file.
1317c478bd9Sstevel@tonic-gate 	 */
1327c478bd9Sstevel@tonic-gate 	for (iiter = 0; iiter < MAXHASH; iiter++) {
1337c478bd9Sstevel@tonic-gate 		if (rc = mutex_init(&(shmlockarray->locknode[iiter]),
1347c478bd9Sstevel@tonic-gate 		    USYNC_PROCESS_ROBUST, 0)) {
1357c478bd9Sstevel@tonic-gate 			if (rc == EBUSY) {
1367c478bd9Sstevel@tonic-gate 				ebusy_cnt++;
1377c478bd9Sstevel@tonic-gate 			} else {
1387c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
1397c478bd9Sstevel@tonic-gate 				    "init_locks_mem():mutex_init():error=%d",
1407c478bd9Sstevel@tonic-gate 				    rc);
1417c478bd9Sstevel@tonic-gate 				return (FALSE);
1427c478bd9Sstevel@tonic-gate 			}
1437c478bd9Sstevel@tonic-gate 		}
1447c478bd9Sstevel@tonic-gate 	}
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	/*
1477c478bd9Sstevel@tonic-gate 	 * EBUSY for all locks OK, it means another process
1487c478bd9Sstevel@tonic-gate 	 * has already initialized locks.
1497c478bd9Sstevel@tonic-gate 	 */
1507c478bd9Sstevel@tonic-gate 	if ((ebusy_cnt > 0) && (ebusy_cnt != MAXHASH)) {
1517c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
1527c478bd9Sstevel@tonic-gate 		    "%s inconsistent. Remove and restart NIS (YP).", LOCKFILE);
1537c478bd9Sstevel@tonic-gate 		return (FALSE);
1547c478bd9Sstevel@tonic-gate 	}
1557c478bd9Sstevel@tonic-gate 	return (TRUE);
1567c478bd9Sstevel@tonic-gate }
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate bool
1597c478bd9Sstevel@tonic-gate init_lock_map()
1607c478bd9Sstevel@tonic-gate {
1617c478bd9Sstevel@tonic-gate 	char buff[ sizeof (lockarray) ];
1627c478bd9Sstevel@tonic-gate 	int write_cnt, lf_size;
1637c478bd9Sstevel@tonic-gate 	struct stat fdata;
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	/*
1667c478bd9Sstevel@tonic-gate 	 * Locking file initialization algorithm, with recovery mechanism.
1677c478bd9Sstevel@tonic-gate 	 * This mechanism has been devised to ensure proper creation
1687c478bd9Sstevel@tonic-gate 	 * of a memory-mapped lock file containing mutexes for robust,
1697c478bd9Sstevel@tonic-gate 	 * inter-process communication.
1707c478bd9Sstevel@tonic-gate 	 * File name is /var/run/yp_maplock (LOCKFILE).  It might or might
1717c478bd9Sstevel@tonic-gate 	 * not exist.
1727c478bd9Sstevel@tonic-gate 	 *
1737c478bd9Sstevel@tonic-gate 	 * Algorithm:
1747c478bd9Sstevel@tonic-gate 	 * Try to open the file. If file doesn't exist, or size is too small,
1757c478bd9Sstevel@tonic-gate 	 * create/rewrite the file, m-map it into memory and initialize the
1767c478bd9Sstevel@tonic-gate 	 * mutexes in it.
1777c478bd9Sstevel@tonic-gate 	 * If file exists and size is at least large enough, assume it's a
1787c478bd9Sstevel@tonic-gate 	 * good file, and m-map the lock structure directly to it.
1797c478bd9Sstevel@tonic-gate 	 *
1807c478bd9Sstevel@tonic-gate 	 * Recovery from inconsistent state is easy - simply delete the file
1817c478bd9Sstevel@tonic-gate 	 * and restart NIS (YP).
1827c478bd9Sstevel@tonic-gate 	 */
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	lockfile = open(LOCKFILE, O_RDWR|O_CREAT, 0600);
1857c478bd9Sstevel@tonic-gate 	if (lockfile != -1) {
1867c478bd9Sstevel@tonic-gate 		if (lockf(lockfile, F_LOCK, 0) == 0) {
1877c478bd9Sstevel@tonic-gate 			if (fstat(lockfile, &fdata) == 0) {
1887c478bd9Sstevel@tonic-gate 				lf_size = fdata.st_size;
1897c478bd9Sstevel@tonic-gate 				if (lf_size < sizeof (lockarray)) {
1907c478bd9Sstevel@tonic-gate 					bzero(buff, sizeof (buff));
1917c478bd9Sstevel@tonic-gate 					if ((write_cnt = write(lockfile, buff,
1927c478bd9Sstevel@tonic-gate 					    sizeof (buff)) != sizeof (buff))) {
1937c478bd9Sstevel@tonic-gate 						if (write_cnt < 0) {
1947c478bd9Sstevel@tonic-gate 							syslog(LOG_ERR,
1957c478bd9Sstevel@tonic-gate 						    "write(%s) => errno=%d",
1967c478bd9Sstevel@tonic-gate 							    LOCKFILE, errno);
1977c478bd9Sstevel@tonic-gate 						} else {
1987c478bd9Sstevel@tonic-gate 							syslog(LOG_ERR,
1997c478bd9Sstevel@tonic-gate 		    "write(%s) => %d!=%d: wrong number of bytes written.",
2007c478bd9Sstevel@tonic-gate 							    LOCKFILE,
2017c478bd9Sstevel@tonic-gate 							    write_cnt,
2027c478bd9Sstevel@tonic-gate 							    sizeof (buff));
2037c478bd9Sstevel@tonic-gate 						}
2047c478bd9Sstevel@tonic-gate 						lockf(lockfile, F_ULOCK, 0);
2057c478bd9Sstevel@tonic-gate 						close(lockfile);
2067c478bd9Sstevel@tonic-gate 						return (FALSE);
2077c478bd9Sstevel@tonic-gate 					}
2087c478bd9Sstevel@tonic-gate 				}
2097c478bd9Sstevel@tonic-gate 			} else {
2107c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
2117c478bd9Sstevel@tonic-gate 				    "fstat(%s) => errno=%d", LOCKFILE, errno);
2127c478bd9Sstevel@tonic-gate 				lockf(lockfile, F_ULOCK, 0);
2137c478bd9Sstevel@tonic-gate 				close(lockfile);
2147c478bd9Sstevel@tonic-gate 				return (FALSE);
2157c478bd9Sstevel@tonic-gate 			}
2167c478bd9Sstevel@tonic-gate 		} else {
2177c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR,
2187c478bd9Sstevel@tonic-gate 			    "lockf(%s,F_LOCK) => errno=%d", LOCKFILE, errno);
2197c478bd9Sstevel@tonic-gate 			close(lockfile);
2207c478bd9Sstevel@tonic-gate 			return (FALSE);
2217c478bd9Sstevel@tonic-gate 		}
2227c478bd9Sstevel@tonic-gate 	} else {
2237c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
2247c478bd9Sstevel@tonic-gate 		    "open(%s) => errno=%d", LOCKFILE, errno);
2257c478bd9Sstevel@tonic-gate 		return (FALSE);
2267c478bd9Sstevel@tonic-gate 	}
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 	/*
2297c478bd9Sstevel@tonic-gate 	 * File exists with correct size, is open, and we're holding
2307c478bd9Sstevel@tonic-gate 	 * the file lock.
2317c478bd9Sstevel@tonic-gate 	 */
2327c478bd9Sstevel@tonic-gate 	shmlockarray = (lockarray *)mmap((caddr_t)0, sizeof (lockarray),
2337c478bd9Sstevel@tonic-gate 	    PROT_READ | PROT_WRITE, MAP_SHARED, lockfile, 0);
2347c478bd9Sstevel@tonic-gate 	if (shmlockarray == MAP_FAILED) {
2357c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "mmap(%s) => errno=%d", LOCKFILE, errno);
2367c478bd9Sstevel@tonic-gate 		lockf(lockfile, F_ULOCK, 0);
2377c478bd9Sstevel@tonic-gate 		close(lockfile);
2387c478bd9Sstevel@tonic-gate 		return (FALSE);
2397c478bd9Sstevel@tonic-gate 	}
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	/*
2427c478bd9Sstevel@tonic-gate 	 * If we wrote zeroes to the file, we also need to initialize
2437c478bd9Sstevel@tonic-gate 	 * the mutex locks.
2447c478bd9Sstevel@tonic-gate 	 */
2457c478bd9Sstevel@tonic-gate 	if (lf_size < sizeof (lockarray)) {
2467c478bd9Sstevel@tonic-gate 		if (init_locks_mem() == FALSE) {
2477c478bd9Sstevel@tonic-gate 			lockf(lockfile, F_ULOCK, 0);
2487c478bd9Sstevel@tonic-gate 			close(lockfile);
2497c478bd9Sstevel@tonic-gate 			if (remove(LOCKFILE) != 0) {
2507c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
2517c478bd9Sstevel@tonic-gate 			    "remove(%s) => errno=%d: Please delete file.",
2527c478bd9Sstevel@tonic-gate 				    LOCKFILE, errno);
2537c478bd9Sstevel@tonic-gate 			}
2547c478bd9Sstevel@tonic-gate 			return (FALSE);
2557c478bd9Sstevel@tonic-gate 		}
2567c478bd9Sstevel@tonic-gate 	}
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 	if (lockf(lockfile, F_ULOCK, 0) != 0) {
2597c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
2607c478bd9Sstevel@tonic-gate 		    "lockf(%s,F_ULOCK) => errno=%d",
2617c478bd9Sstevel@tonic-gate 		    LOCKFILE, errno);
2627c478bd9Sstevel@tonic-gate 		close(lockfile);
2637c478bd9Sstevel@tonic-gate 		return (FALSE);
2647c478bd9Sstevel@tonic-gate 	}
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	if (close(lockfile) == 0) {
2677c478bd9Sstevel@tonic-gate 		return (TRUE);
2687c478bd9Sstevel@tonic-gate 	} else {
2697c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
2707c478bd9Sstevel@tonic-gate 		    "close(%s) => errno=%d", LOCKFILE, errno);
2717c478bd9Sstevel@tonic-gate 		return (FALSE);
2727c478bd9Sstevel@tonic-gate 	}
2737c478bd9Sstevel@tonic-gate }
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate /*
2767c478bd9Sstevel@tonic-gate  * FUNCTION : 	lock_map()
2777c478bd9Sstevel@tonic-gate  *
2787c478bd9Sstevel@tonic-gate  * DESCRIPTION: Front end to the lock routine taking map name as argument.
2797c478bd9Sstevel@tonic-gate  *
2807c478bd9Sstevel@tonic-gate  * GIVEN :	Map name.
2817c478bd9Sstevel@tonic-gate  *
2827c478bd9Sstevel@tonic-gate  * RETURNS :	Same as lock_core
2837c478bd9Sstevel@tonic-gate  */
2847c478bd9Sstevel@tonic-gate int
2857c478bd9Sstevel@tonic-gate lock_map(char *mapname)
2867c478bd9Sstevel@tonic-gate {
2877c478bd9Sstevel@tonic-gate 	int hashval;
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 	hashval = hash(mapname);
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 	return (lock_core(hashval));
2927c478bd9Sstevel@tonic-gate }
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate /*
2957c478bd9Sstevel@tonic-gate  * FUNCTION : 	lock_core()
2967c478bd9Sstevel@tonic-gate  *
2977c478bd9Sstevel@tonic-gate  * DESCRIPTION: The core map locking function
2987c478bd9Sstevel@tonic-gate  *
2997c478bd9Sstevel@tonic-gate  * GIVEN :	Map hash value
3007c478bd9Sstevel@tonic-gate  *
3017c478bd9Sstevel@tonic-gate  * RETURNS :	0 = Failure
3027c478bd9Sstevel@tonic-gate  *		1 = Success
3037c478bd9Sstevel@tonic-gate  */
3047c478bd9Sstevel@tonic-gate int
3057c478bd9Sstevel@tonic-gate lock_core(int hashval)
3067c478bd9Sstevel@tonic-gate {
3077c478bd9Sstevel@tonic-gate 	int rc;
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 	/*
3107c478bd9Sstevel@tonic-gate 	 * Robust, cross-process lock implementation
3117c478bd9Sstevel@tonic-gate 	 */
3127c478bd9Sstevel@tonic-gate 	rc = mutex_lock(&(shmlockarray->locknode[hashval]));
3137c478bd9Sstevel@tonic-gate 	while (rc != 0) {
3147c478bd9Sstevel@tonic-gate 		switch (rc) {
3157c478bd9Sstevel@tonic-gate 		case EOWNERDEAD:
3167c478bd9Sstevel@tonic-gate 			/*
3177c478bd9Sstevel@tonic-gate 			 * Previows lock owner died, resetting lock
3187c478bd9Sstevel@tonic-gate 			 * to recover from error.
3197c478bd9Sstevel@tonic-gate 			 */
3207c478bd9Sstevel@tonic-gate 			rc = mutex_init(&(shmlockarray->locknode[hashval]),
3217c478bd9Sstevel@tonic-gate 			    USYNC_PROCESS_ROBUST, 0);
3227c478bd9Sstevel@tonic-gate 			if (rc != 0) {
3237c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
3247c478bd9Sstevel@tonic-gate 				    "mutex_init(): error=%d", rc);
3257c478bd9Sstevel@tonic-gate 				return (0);
3267c478bd9Sstevel@tonic-gate 			}
3277c478bd9Sstevel@tonic-gate 			rc = mutex_unlock(&(shmlockarray->locknode[hashval]));
3287c478bd9Sstevel@tonic-gate 			if (rc != 0) {
3297c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
3307c478bd9Sstevel@tonic-gate 				    "mutex_unlock(): error=%d", rc);
3317c478bd9Sstevel@tonic-gate 				return (0);
3327c478bd9Sstevel@tonic-gate 			}
3337c478bd9Sstevel@tonic-gate 			break;
3347c478bd9Sstevel@tonic-gate 		default:
3357c478bd9Sstevel@tonic-gate 			/*
3367c478bd9Sstevel@tonic-gate 			 * Unrecoverable problem - nothing to do
3377c478bd9Sstevel@tonic-gate 			 * but exit YP and delete lock file.
3387c478bd9Sstevel@tonic-gate 			 */
3397c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR,
3407c478bd9Sstevel@tonic-gate 			    "mutex_lock(): error=%d", rc);
3417c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR,
3427c478bd9Sstevel@tonic-gate 			    "Please restart NIS (ypstop/ypstart).");
3437c478bd9Sstevel@tonic-gate 			if (remove(LOCKFILE) != 0) {
3447c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
3457c478bd9Sstevel@tonic-gate 			    "remove(%s) => errno=%d: Please delete file.",
3467c478bd9Sstevel@tonic-gate 				    LOCKFILE, errno);
3477c478bd9Sstevel@tonic-gate 			}
3487c478bd9Sstevel@tonic-gate 			return (0);
3497c478bd9Sstevel@tonic-gate 		}
3507c478bd9Sstevel@tonic-gate 		rc = mutex_lock(&(shmlockarray->locknode[hashval]));
3517c478bd9Sstevel@tonic-gate 	}
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 	/* Success */
3547c478bd9Sstevel@tonic-gate 	return (1);
3557c478bd9Sstevel@tonic-gate }
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate /*
3597c478bd9Sstevel@tonic-gate  * FUNCTION : 	unlock_map()
3607c478bd9Sstevel@tonic-gate  *
3617c478bd9Sstevel@tonic-gate  * DESCRIPTION: Front end to the unlock routine taking map name as argument.
3627c478bd9Sstevel@tonic-gate  *
3637c478bd9Sstevel@tonic-gate  * GIVEN :	Map name.
3647c478bd9Sstevel@tonic-gate  *
3657c478bd9Sstevel@tonic-gate  * RETURNS :	Same as unlock_core
3667c478bd9Sstevel@tonic-gate  */
3677c478bd9Sstevel@tonic-gate int
3687c478bd9Sstevel@tonic-gate unlock_map(char *mapname)
3697c478bd9Sstevel@tonic-gate {
3707c478bd9Sstevel@tonic-gate 	int hashval;
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 	hashval = hash(mapname);
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 	return (unlock_core(hashval));
3757c478bd9Sstevel@tonic-gate }
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate /*
3787c478bd9Sstevel@tonic-gate  * FUNCTION : 	unlock_core()
3797c478bd9Sstevel@tonic-gate  *
3807c478bd9Sstevel@tonic-gate  * DESCRIPTION: The core map locking function
3817c478bd9Sstevel@tonic-gate  *
3827c478bd9Sstevel@tonic-gate  * GIVEN :	Map hash value
3837c478bd9Sstevel@tonic-gate  *
3847c478bd9Sstevel@tonic-gate  * RETURNS :	0 = Failure
3857c478bd9Sstevel@tonic-gate  *		1 = Success
3867c478bd9Sstevel@tonic-gate  */
3877c478bd9Sstevel@tonic-gate int
3887c478bd9Sstevel@tonic-gate unlock_core(int hashval)
3897c478bd9Sstevel@tonic-gate {
3907c478bd9Sstevel@tonic-gate 	int rc;
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 	rc = mutex_unlock(&(shmlockarray->locknode[hashval]));
3937c478bd9Sstevel@tonic-gate 	if (rc != 0) {
3947c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
3957c478bd9Sstevel@tonic-gate 		    "mutex_unlock(): error=%d", rc);
3967c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
3977c478bd9Sstevel@tonic-gate 		    "Please restart NIS (ypstop/ypstart).");
3987c478bd9Sstevel@tonic-gate 		if (remove(LOCKFILE) != 0) {
3997c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR,
4007c478bd9Sstevel@tonic-gate 			    "remove(%s) => errno=%d: Please delete file.",
4017c478bd9Sstevel@tonic-gate 			    LOCKFILE, errno);
4027c478bd9Sstevel@tonic-gate 		}
4037c478bd9Sstevel@tonic-gate 		return (0);
4047c478bd9Sstevel@tonic-gate 	}
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 	/* Success */
4077c478bd9Sstevel@tonic-gate 	return (1);
4087c478bd9Sstevel@tonic-gate }
409