xref: /onnv-gate/usr/src/cmd/ypcmd/shared/lockmap.c (revision 1533:913e2955e72f)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*1533Ssm26363  * Common Development and Distribution License (the "License").
6*1533Ssm26363  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*1533Ssm26363  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <unistd.h>
290Sstevel@tonic-gate #include <syslog.h>
300Sstevel@tonic-gate #include <sys/mman.h>
310Sstevel@tonic-gate #include <thread.h>
320Sstevel@tonic-gate #include <synch.h>
330Sstevel@tonic-gate #include <ndbm.h>
340Sstevel@tonic-gate #include "../ypsym.h"
350Sstevel@tonic-gate #include "../ypdefs.h"
360Sstevel@tonic-gate 
370Sstevel@tonic-gate /*
380Sstevel@tonic-gate  *  These routines provide mutual exclusion between ypserv and ypxfr.
390Sstevel@tonic-gate  *  Mutual exclusion is needed so that ypxfr doesn't try to rename
400Sstevel@tonic-gate  *  dbm files while ypserv is trying to open them.  After ypserv has
410Sstevel@tonic-gate  *  opened a dbm file, it is safe to rename it because ypserv still
420Sstevel@tonic-gate  *  has access to the file through its file descriptor.
430Sstevel@tonic-gate  */
440Sstevel@tonic-gate 
450Sstevel@tonic-gate #define	LOCKFILE "/var/run/yp_maplock"
460Sstevel@tonic-gate struct lockarray {
470Sstevel@tonic-gate 	mutex_t		locknode[MAXHASH];
480Sstevel@tonic-gate };
490Sstevel@tonic-gate typedef struct lockarray lockarray;
500Sstevel@tonic-gate 
510Sstevel@tonic-gate /*
520Sstevel@tonic-gate  * Cross-process robust mutex locks.
530Sstevel@tonic-gate  * Provide synchronization between YP processes
540Sstevel@tonic-gate  * by implementing an exclusive locking mechanism
550Sstevel@tonic-gate  * via a memory-mapped file.
560Sstevel@tonic-gate  */
570Sstevel@tonic-gate static struct lockarray	*shmlockarray;
580Sstevel@tonic-gate static int	lockfile;
590Sstevel@tonic-gate 
600Sstevel@tonic-gate int
610Sstevel@tonic-gate hash(char *s)
620Sstevel@tonic-gate {
63*1533Ssm26363 	unsigned int n = 0;
640Sstevel@tonic-gate 	int i;
650Sstevel@tonic-gate 
660Sstevel@tonic-gate 	for (i = 1; *s; i += 10, s++) {
670Sstevel@tonic-gate 		n += i * (*s);
680Sstevel@tonic-gate 	}
690Sstevel@tonic-gate 	n %= MAXHASH;
700Sstevel@tonic-gate 	return (n);
710Sstevel@tonic-gate }
720Sstevel@tonic-gate 
730Sstevel@tonic-gate bool
740Sstevel@tonic-gate init_locks_mem()
750Sstevel@tonic-gate {
760Sstevel@tonic-gate 	int iiter, rc;
770Sstevel@tonic-gate 	int ebusy_cnt = 0;
780Sstevel@tonic-gate 
790Sstevel@tonic-gate 	/*
800Sstevel@tonic-gate 	 * Initialize cross-process locks in memory-mapped file.
810Sstevel@tonic-gate 	 */
820Sstevel@tonic-gate 	for (iiter = 0; iiter < MAXHASH; iiter++) {
830Sstevel@tonic-gate 		if (rc = mutex_init(&(shmlockarray->locknode[iiter]),
840Sstevel@tonic-gate 		    USYNC_PROCESS_ROBUST, 0)) {
850Sstevel@tonic-gate 			if (rc == EBUSY) {
860Sstevel@tonic-gate 				ebusy_cnt++;
870Sstevel@tonic-gate 			} else {
880Sstevel@tonic-gate 				syslog(LOG_ERR,
890Sstevel@tonic-gate 				    "init_locks_mem():mutex_init():error=%d",
900Sstevel@tonic-gate 				    rc);
910Sstevel@tonic-gate 				return (FALSE);
920Sstevel@tonic-gate 			}
930Sstevel@tonic-gate 		}
940Sstevel@tonic-gate 	}
950Sstevel@tonic-gate 
960Sstevel@tonic-gate 	/*
970Sstevel@tonic-gate 	 * EBUSY for all locks OK, it means another process
980Sstevel@tonic-gate 	 * has already initialized locks.
990Sstevel@tonic-gate 	 */
1000Sstevel@tonic-gate 	if ((ebusy_cnt > 0) && (ebusy_cnt != MAXHASH)) {
1010Sstevel@tonic-gate 		syslog(LOG_ERR,
1020Sstevel@tonic-gate 		    "%s inconsistent. Remove and restart NIS (YP).", LOCKFILE);
1030Sstevel@tonic-gate 		return (FALSE);
1040Sstevel@tonic-gate 	}
1050Sstevel@tonic-gate 	return (TRUE);
1060Sstevel@tonic-gate }
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate bool
1090Sstevel@tonic-gate init_lock_map()
1100Sstevel@tonic-gate {
1110Sstevel@tonic-gate 	char buff[ sizeof (lockarray) ];
1120Sstevel@tonic-gate 	int write_cnt, lf_size;
1130Sstevel@tonic-gate 	struct stat fdata;
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate 	/*
1160Sstevel@tonic-gate 	 * Locking file initialization algorithm, with recovery mechanism.
1170Sstevel@tonic-gate 	 * This mechanism has been devised to ensure proper creation
1180Sstevel@tonic-gate 	 * of a memory-mapped lock file containing mutexes for robust,
1190Sstevel@tonic-gate 	 * inter-process communication.
1200Sstevel@tonic-gate 	 * File name is /var/run/yp_maplock (LOCKFILE).  It might or might
1210Sstevel@tonic-gate 	 * not exist.
1220Sstevel@tonic-gate 	 *
1230Sstevel@tonic-gate 	 * Algorithm:
1240Sstevel@tonic-gate 	 * Try to open the file. If file doesn't exist, or size is too small,
1250Sstevel@tonic-gate 	 * create/rewrite the file, m-map it into memory and initialize the
1260Sstevel@tonic-gate 	 * mutexes in it.
1270Sstevel@tonic-gate 	 * If file exists and size is at least large enough, assume it's a
1280Sstevel@tonic-gate 	 * good file, and m-map the lock structure directly to it.
1290Sstevel@tonic-gate 	 *
1300Sstevel@tonic-gate 	 * Recovery from inconsistent state is easy - simply delete the file
1310Sstevel@tonic-gate 	 * and restart NIS (YP).
1320Sstevel@tonic-gate 	 */
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 	lockfile = open(LOCKFILE, O_RDWR|O_CREAT, 0600);
1350Sstevel@tonic-gate 	if (lockfile != -1) {
1360Sstevel@tonic-gate 		if (lockf(lockfile, F_LOCK, 0) == 0) {
1370Sstevel@tonic-gate 			if (fstat(lockfile, &fdata) == 0) {
1380Sstevel@tonic-gate 				lf_size = fdata.st_size;
1390Sstevel@tonic-gate 				if (lf_size < sizeof (lockarray)) {
1400Sstevel@tonic-gate 					bzero(buff, sizeof (buff));
1410Sstevel@tonic-gate 					if ((write_cnt = write(lockfile, buff,
1420Sstevel@tonic-gate 					    sizeof (buff)) != sizeof (buff))) {
1430Sstevel@tonic-gate 						if (write_cnt < 0) {
1440Sstevel@tonic-gate 							syslog(LOG_ERR,
1450Sstevel@tonic-gate 						    "write(%s) => errno=%d",
1460Sstevel@tonic-gate 							    LOCKFILE, errno);
1470Sstevel@tonic-gate 						} else {
1480Sstevel@tonic-gate 							syslog(LOG_ERR,
1490Sstevel@tonic-gate 		    "write(%s) => %d!=%d: wrong number of bytes written.",
1500Sstevel@tonic-gate 							    LOCKFILE,
1510Sstevel@tonic-gate 							    write_cnt,
1520Sstevel@tonic-gate 							    sizeof (buff));
1530Sstevel@tonic-gate 						}
1540Sstevel@tonic-gate 						lockf(lockfile, F_ULOCK, 0);
1550Sstevel@tonic-gate 						close(lockfile);
1560Sstevel@tonic-gate 						return (FALSE);
1570Sstevel@tonic-gate 					}
1580Sstevel@tonic-gate 				}
1590Sstevel@tonic-gate 			} else {
1600Sstevel@tonic-gate 				syslog(LOG_ERR,
1610Sstevel@tonic-gate 				    "fstat(%s) => errno=%d", LOCKFILE, errno);
1620Sstevel@tonic-gate 				lockf(lockfile, F_ULOCK, 0);
1630Sstevel@tonic-gate 				close(lockfile);
1640Sstevel@tonic-gate 				return (FALSE);
1650Sstevel@tonic-gate 			}
1660Sstevel@tonic-gate 		} else {
1670Sstevel@tonic-gate 			syslog(LOG_ERR,
1680Sstevel@tonic-gate 			    "lockf(%s,F_LOCK) => errno=%d", LOCKFILE, errno);
1690Sstevel@tonic-gate 			close(lockfile);
1700Sstevel@tonic-gate 			return (FALSE);
1710Sstevel@tonic-gate 		}
1720Sstevel@tonic-gate 	} else {
1730Sstevel@tonic-gate 		syslog(LOG_ERR,
1740Sstevel@tonic-gate 		    "open(%s) => errno=%d", LOCKFILE, errno);
1750Sstevel@tonic-gate 		return (FALSE);
1760Sstevel@tonic-gate 	}
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate 	/*
1790Sstevel@tonic-gate 	 * File exists with correct size, is open, and we're holding
1800Sstevel@tonic-gate 	 * the file lock.
1810Sstevel@tonic-gate 	 */
182*1533Ssm26363 	shmlockarray = (lockarray *)mmap((caddr_t)0, sizeof (lockarray),
1830Sstevel@tonic-gate 	    PROT_READ | PROT_WRITE, MAP_SHARED, lockfile, 0);
1840Sstevel@tonic-gate 	if (shmlockarray == MAP_FAILED) {
1850Sstevel@tonic-gate 		syslog(LOG_ERR, "mmap(%s) => errno=%d", LOCKFILE, errno);
1860Sstevel@tonic-gate 		lockf(lockfile, F_ULOCK, 0);
1870Sstevel@tonic-gate 		close(lockfile);
1880Sstevel@tonic-gate 		return (FALSE);
1890Sstevel@tonic-gate 	}
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 	/*
1920Sstevel@tonic-gate 	 * If we wrote zeroes to the file, we also need to initialize
1930Sstevel@tonic-gate 	 * the mutex locks.
1940Sstevel@tonic-gate 	 */
1950Sstevel@tonic-gate 	if (lf_size < sizeof (lockarray)) {
1960Sstevel@tonic-gate 		if (init_locks_mem() == FALSE) {
1970Sstevel@tonic-gate 			lockf(lockfile, F_ULOCK, 0);
1980Sstevel@tonic-gate 			close(lockfile);
1990Sstevel@tonic-gate 			if (remove(LOCKFILE) != 0) {
2000Sstevel@tonic-gate 				syslog(LOG_ERR,
2010Sstevel@tonic-gate 			    "remove(%s) => errno=%d: Please delete file.",
2020Sstevel@tonic-gate 				    LOCKFILE, errno);
2030Sstevel@tonic-gate 			}
2040Sstevel@tonic-gate 			return (FALSE);
2050Sstevel@tonic-gate 		}
2060Sstevel@tonic-gate 	}
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 	if (lockf(lockfile, F_ULOCK, 0) != 0) {
2090Sstevel@tonic-gate 		syslog(LOG_ERR,
2100Sstevel@tonic-gate 		    "lockf(%s,F_ULOCK) => errno=%d",
2110Sstevel@tonic-gate 		    LOCKFILE, errno);
2120Sstevel@tonic-gate 		close(lockfile);
2130Sstevel@tonic-gate 		return (FALSE);
2140Sstevel@tonic-gate 	}
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 	if (close(lockfile) == 0) {
2170Sstevel@tonic-gate 		return (TRUE);
2180Sstevel@tonic-gate 	} else {
2190Sstevel@tonic-gate 		syslog(LOG_ERR,
2200Sstevel@tonic-gate 		    "close(%s) => errno=%d", LOCKFILE, errno);
2210Sstevel@tonic-gate 		return (FALSE);
2220Sstevel@tonic-gate 	}
2230Sstevel@tonic-gate }
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate /*
2260Sstevel@tonic-gate  * FUNCTION : 	lock_map()
2270Sstevel@tonic-gate  *
2280Sstevel@tonic-gate  * DESCRIPTION: Front end to the lock routine taking map name as argument.
2290Sstevel@tonic-gate  *
2300Sstevel@tonic-gate  * GIVEN :	Map name.
2310Sstevel@tonic-gate  *
2320Sstevel@tonic-gate  * RETURNS :	Same as lock_core
2330Sstevel@tonic-gate  */
2340Sstevel@tonic-gate int
2350Sstevel@tonic-gate lock_map(char *mapname)
2360Sstevel@tonic-gate {
2370Sstevel@tonic-gate 	int hashval;
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 	hashval = hash(mapname);
2400Sstevel@tonic-gate 
241*1533Ssm26363 	return (lock_core(hashval));
2420Sstevel@tonic-gate }
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate /*
2450Sstevel@tonic-gate  * FUNCTION : 	lock_core()
2460Sstevel@tonic-gate  *
2470Sstevel@tonic-gate  * DESCRIPTION: The core map locking function
2480Sstevel@tonic-gate  *
2490Sstevel@tonic-gate  * GIVEN :	Map hash value
2500Sstevel@tonic-gate  *
2510Sstevel@tonic-gate  * RETURNS :	0 = Failure
2520Sstevel@tonic-gate  *		1 = Success
2530Sstevel@tonic-gate  */
2540Sstevel@tonic-gate int
2550Sstevel@tonic-gate lock_core(int hashval)
2560Sstevel@tonic-gate {
2570Sstevel@tonic-gate 	int rc;
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 	/*
260*1533Ssm26363 	 * Robust, cross-process lock implementation
2610Sstevel@tonic-gate 	 */
2620Sstevel@tonic-gate 	rc = mutex_lock(&(shmlockarray->locknode[hashval]));
2630Sstevel@tonic-gate 	while (rc != 0) {
2640Sstevel@tonic-gate 		switch (rc) {
2650Sstevel@tonic-gate 		case EOWNERDEAD:
2660Sstevel@tonic-gate 			/*
2670Sstevel@tonic-gate 			 * Previows lock owner died, resetting lock
2680Sstevel@tonic-gate 			 * to recover from error.
2690Sstevel@tonic-gate 			 */
2700Sstevel@tonic-gate 			rc = mutex_init(&(shmlockarray->locknode[hashval]),
2710Sstevel@tonic-gate 			    USYNC_PROCESS_ROBUST, 0);
2720Sstevel@tonic-gate 			if (rc != 0) {
2730Sstevel@tonic-gate 				syslog(LOG_ERR,
2740Sstevel@tonic-gate 				    "mutex_init(): error=%d", rc);
2750Sstevel@tonic-gate 				return (0);
2760Sstevel@tonic-gate 			}
2770Sstevel@tonic-gate 			rc = mutex_unlock(&(shmlockarray->locknode[hashval]));
2780Sstevel@tonic-gate 			if (rc != 0) {
2790Sstevel@tonic-gate 				syslog(LOG_ERR,
2800Sstevel@tonic-gate 				    "mutex_unlock(): error=%d", rc);
2810Sstevel@tonic-gate 				return (0);
2820Sstevel@tonic-gate 			}
2830Sstevel@tonic-gate 			break;
2840Sstevel@tonic-gate 		default:
2850Sstevel@tonic-gate 			/*
2860Sstevel@tonic-gate 			 * Unrecoverable problem - nothing to do
2870Sstevel@tonic-gate 			 * but exit YP and delete lock file.
2880Sstevel@tonic-gate 			 */
2890Sstevel@tonic-gate 			syslog(LOG_ERR,
2900Sstevel@tonic-gate 			    "mutex_lock(): error=%d", rc);
2910Sstevel@tonic-gate 			syslog(LOG_ERR,
2920Sstevel@tonic-gate 			    "Please restart NIS (ypstop/ypstart).");
2930Sstevel@tonic-gate 			if (remove(LOCKFILE) != 0) {
2940Sstevel@tonic-gate 				syslog(LOG_ERR,
2950Sstevel@tonic-gate 			    "remove(%s) => errno=%d: Please delete file.",
2960Sstevel@tonic-gate 				    LOCKFILE, errno);
2970Sstevel@tonic-gate 			}
2980Sstevel@tonic-gate 			return (0);
2990Sstevel@tonic-gate 		}
3000Sstevel@tonic-gate 		rc = mutex_lock(&(shmlockarray->locknode[hashval]));
3010Sstevel@tonic-gate 	}
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 	/* Success */
3040Sstevel@tonic-gate 	return (1);
3050Sstevel@tonic-gate }
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate /*
3090Sstevel@tonic-gate  * FUNCTION : 	unlock_map()
3100Sstevel@tonic-gate  *
3110Sstevel@tonic-gate  * DESCRIPTION: Front end to the unlock routine taking map name as argument.
3120Sstevel@tonic-gate  *
3130Sstevel@tonic-gate  * GIVEN :	Map name.
3140Sstevel@tonic-gate  *
3150Sstevel@tonic-gate  * RETURNS :	Same as unlock_core
3160Sstevel@tonic-gate  */
3170Sstevel@tonic-gate int
3180Sstevel@tonic-gate unlock_map(char *mapname)
3190Sstevel@tonic-gate {
3200Sstevel@tonic-gate 	int hashval;
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate 	hashval = hash(mapname);
3230Sstevel@tonic-gate 
324*1533Ssm26363 	return (unlock_core(hashval));
3250Sstevel@tonic-gate }
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate /*
3280Sstevel@tonic-gate  * FUNCTION : 	unlock_core()
3290Sstevel@tonic-gate  *
3300Sstevel@tonic-gate  * DESCRIPTION: The core map locking function
3310Sstevel@tonic-gate  *
3320Sstevel@tonic-gate  * GIVEN :	Map hash value
3330Sstevel@tonic-gate  *
3340Sstevel@tonic-gate  * RETURNS :	0 = Failure
3350Sstevel@tonic-gate  *		1 = Success
3360Sstevel@tonic-gate  */
3370Sstevel@tonic-gate int
3380Sstevel@tonic-gate unlock_core(int hashval)
3390Sstevel@tonic-gate {
3400Sstevel@tonic-gate 	int rc;
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate 	rc = mutex_unlock(&(shmlockarray->locknode[hashval]));
3430Sstevel@tonic-gate 	if (rc != 0) {
3440Sstevel@tonic-gate 		syslog(LOG_ERR,
3450Sstevel@tonic-gate 		    "mutex_unlock(): error=%d", rc);
3460Sstevel@tonic-gate 		syslog(LOG_ERR,
3470Sstevel@tonic-gate 		    "Please restart NIS (ypstop/ypstart).");
3480Sstevel@tonic-gate 		if (remove(LOCKFILE) != 0) {
3490Sstevel@tonic-gate 			syslog(LOG_ERR,
3500Sstevel@tonic-gate 			    "remove(%s) => errno=%d: Please delete file.",
3510Sstevel@tonic-gate 			    LOCKFILE, errno);
3520Sstevel@tonic-gate 		}
3530Sstevel@tonic-gate 		return (0);
3540Sstevel@tonic-gate 	}
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate 	/* Success */
3570Sstevel@tonic-gate 	return (1);
3580Sstevel@tonic-gate }
359