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 51533Ssm26363 * Common Development and Distribution License (the "License"). 61533Ssm26363 * 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 */ 21*4574Sraf 220Sstevel@tonic-gate /* 23*4574Sraf * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 280Sstevel@tonic-gate 290Sstevel@tonic-gate #include <unistd.h> 300Sstevel@tonic-gate #include <syslog.h> 310Sstevel@tonic-gate #include <sys/mman.h> 320Sstevel@tonic-gate #include <thread.h> 330Sstevel@tonic-gate #include <synch.h> 342189Ssdussud #include <strings.h> 350Sstevel@tonic-gate #include <ndbm.h> 360Sstevel@tonic-gate #include "../ypsym.h" 370Sstevel@tonic-gate #include "../ypdefs.h" 382189Ssdussud #include "shim.h" 390Sstevel@tonic-gate 400Sstevel@tonic-gate /* 410Sstevel@tonic-gate * These routines provide mutual exclusion between ypserv and ypxfr. 420Sstevel@tonic-gate * Mutual exclusion is needed so that ypxfr doesn't try to rename 430Sstevel@tonic-gate * dbm files while ypserv is trying to open them. After ypserv has 440Sstevel@tonic-gate * opened a dbm file, it is safe to rename it because ypserv still 450Sstevel@tonic-gate * has access to the file through its file descriptor. 460Sstevel@tonic-gate */ 470Sstevel@tonic-gate 480Sstevel@tonic-gate #define LOCKFILE "/var/run/yp_maplock" 490Sstevel@tonic-gate struct lockarray { 500Sstevel@tonic-gate mutex_t locknode[MAXHASH]; 510Sstevel@tonic-gate }; 520Sstevel@tonic-gate typedef struct lockarray lockarray; 530Sstevel@tonic-gate 540Sstevel@tonic-gate /* 550Sstevel@tonic-gate * Cross-process robust mutex locks. 560Sstevel@tonic-gate * Provide synchronization between YP processes 570Sstevel@tonic-gate * by implementing an exclusive locking mechanism 580Sstevel@tonic-gate * via a memory-mapped file. 590Sstevel@tonic-gate */ 600Sstevel@tonic-gate static struct lockarray *shmlockarray; 610Sstevel@tonic-gate static int lockfile; 620Sstevel@tonic-gate 632189Ssdussud /* 642189Ssdussud * Hash functions, used for by the locking mechanism. 652189Ssdussud * 662189Ssdussud * - hash() is the front-end function that gets called. 672189Ssdussud * - get_map_id() returns a unique int value per map. 682189Ssdussud * It is used in N2L mode only. 692189Ssdussud * It is called by hash() in N2L mode. 702189Ssdussud */ 712189Ssdussud int 722189Ssdussud get_map_id(char *map_name, int index) 732189Ssdussud { 742189Ssdussud map_id_elt_t *cur_elt; 752189Ssdussud /* 762189Ssdussud * Local references to hash table for map lists 772189Ssdussud * and to max number of maps 782189Ssdussud */ 792189Ssdussud map_id_elt_t **map_list_p; 802189Ssdussud int max_map; 812189Ssdussud 822189Ssdussud /* initializes map_list_p & max_map */ 832189Ssdussud get_list_max(&map_list_p, &max_map); 842189Ssdussud 852189Ssdussud cur_elt = map_list_p[index]; 862189Ssdussud while (cur_elt != NULL) { 872189Ssdussud if (strcmp(map_name, cur_elt->map_name) == 0) { 882189Ssdussud /* found */ 892189Ssdussud return (cur_elt->map_id); 902189Ssdussud } 912189Ssdussud cur_elt = cur_elt->next; 922189Ssdussud } 932189Ssdussud syslog(LOG_WARNING, "get_map_id: no hash id found for %s" 942189Ssdussud ", giving max_map value (%d)", 952189Ssdussud map_name, max_map); 962189Ssdussud /* 972189Ssdussud * max_map does not match any map id, hence 982189Ssdussud * will not trigger any lock collision 992189Ssdussud * with existing maps. 1002189Ssdussud * Needed for yp regular locking mechanism. 1012189Ssdussud */ 1022189Ssdussud return (max_map); 1032189Ssdussud } 1042189Ssdussud 1050Sstevel@tonic-gate int 1060Sstevel@tonic-gate hash(char *s) 1070Sstevel@tonic-gate { 1081533Ssm26363 unsigned int n = 0; 1090Sstevel@tonic-gate int i; 1102189Ssdussud char *map_name = s; 1110Sstevel@tonic-gate 1120Sstevel@tonic-gate for (i = 1; *s; i += 10, s++) { 1130Sstevel@tonic-gate n += i * (*s); 1140Sstevel@tonic-gate } 1150Sstevel@tonic-gate n %= MAXHASH; 1162189Ssdussud 1172189Ssdussud if (yptol_mode & yptol_newlock) { 1182189Ssdussud return (get_map_id(map_name, n)); 1192189Ssdussud } else { 1202189Ssdussud return (n); 1212189Ssdussud } 1220Sstevel@tonic-gate } 1230Sstevel@tonic-gate 1240Sstevel@tonic-gate bool 1250Sstevel@tonic-gate init_locks_mem() 1260Sstevel@tonic-gate { 1270Sstevel@tonic-gate int iiter, rc; 1280Sstevel@tonic-gate int ebusy_cnt = 0; 1290Sstevel@tonic-gate 1300Sstevel@tonic-gate /* 1310Sstevel@tonic-gate * Initialize cross-process locks in memory-mapped file. 1320Sstevel@tonic-gate */ 1330Sstevel@tonic-gate for (iiter = 0; iiter < MAXHASH; iiter++) { 1340Sstevel@tonic-gate if (rc = mutex_init(&(shmlockarray->locknode[iiter]), 135*4574Sraf USYNC_PROCESS | LOCK_ROBUST, 0)) { 1360Sstevel@tonic-gate if (rc == EBUSY) { 1370Sstevel@tonic-gate ebusy_cnt++; 1380Sstevel@tonic-gate } else { 1390Sstevel@tonic-gate syslog(LOG_ERR, 1400Sstevel@tonic-gate "init_locks_mem():mutex_init():error=%d", 1410Sstevel@tonic-gate rc); 1420Sstevel@tonic-gate return (FALSE); 1430Sstevel@tonic-gate } 1440Sstevel@tonic-gate } 1450Sstevel@tonic-gate } 1460Sstevel@tonic-gate 1470Sstevel@tonic-gate /* 1480Sstevel@tonic-gate * EBUSY for all locks OK, it means another process 1490Sstevel@tonic-gate * has already initialized locks. 1500Sstevel@tonic-gate */ 1510Sstevel@tonic-gate if ((ebusy_cnt > 0) && (ebusy_cnt != MAXHASH)) { 1520Sstevel@tonic-gate syslog(LOG_ERR, 1530Sstevel@tonic-gate "%s inconsistent. Remove and restart NIS (YP).", LOCKFILE); 1540Sstevel@tonic-gate return (FALSE); 1550Sstevel@tonic-gate } 1560Sstevel@tonic-gate return (TRUE); 1570Sstevel@tonic-gate } 1580Sstevel@tonic-gate 1590Sstevel@tonic-gate bool 1600Sstevel@tonic-gate init_lock_map() 1610Sstevel@tonic-gate { 1620Sstevel@tonic-gate char buff[ sizeof (lockarray) ]; 1630Sstevel@tonic-gate int write_cnt, lf_size; 1640Sstevel@tonic-gate struct stat fdata; 1650Sstevel@tonic-gate 1660Sstevel@tonic-gate /* 1670Sstevel@tonic-gate * Locking file initialization algorithm, with recovery mechanism. 1680Sstevel@tonic-gate * This mechanism has been devised to ensure proper creation 1690Sstevel@tonic-gate * of a memory-mapped lock file containing mutexes for robust, 1700Sstevel@tonic-gate * inter-process communication. 1710Sstevel@tonic-gate * File name is /var/run/yp_maplock (LOCKFILE). It might or might 1720Sstevel@tonic-gate * not exist. 1730Sstevel@tonic-gate * 1740Sstevel@tonic-gate * Algorithm: 1750Sstevel@tonic-gate * Try to open the file. If file doesn't exist, or size is too small, 1760Sstevel@tonic-gate * create/rewrite the file, m-map it into memory and initialize the 1770Sstevel@tonic-gate * mutexes in it. 1780Sstevel@tonic-gate * If file exists and size is at least large enough, assume it's a 1790Sstevel@tonic-gate * good file, and m-map the lock structure directly to it. 1800Sstevel@tonic-gate * 1810Sstevel@tonic-gate * Recovery from inconsistent state is easy - simply delete the file 1820Sstevel@tonic-gate * and restart NIS (YP). 1830Sstevel@tonic-gate */ 1840Sstevel@tonic-gate 1850Sstevel@tonic-gate lockfile = open(LOCKFILE, O_RDWR|O_CREAT, 0600); 1860Sstevel@tonic-gate if (lockfile != -1) { 1870Sstevel@tonic-gate if (lockf(lockfile, F_LOCK, 0) == 0) { 1880Sstevel@tonic-gate if (fstat(lockfile, &fdata) == 0) { 1890Sstevel@tonic-gate lf_size = fdata.st_size; 1900Sstevel@tonic-gate if (lf_size < sizeof (lockarray)) { 1910Sstevel@tonic-gate bzero(buff, sizeof (buff)); 1920Sstevel@tonic-gate if ((write_cnt = write(lockfile, buff, 1930Sstevel@tonic-gate sizeof (buff)) != sizeof (buff))) { 1940Sstevel@tonic-gate if (write_cnt < 0) { 1950Sstevel@tonic-gate syslog(LOG_ERR, 1960Sstevel@tonic-gate "write(%s) => errno=%d", 1970Sstevel@tonic-gate LOCKFILE, errno); 1980Sstevel@tonic-gate } else { 1990Sstevel@tonic-gate syslog(LOG_ERR, 2000Sstevel@tonic-gate "write(%s) => %d!=%d: wrong number of bytes written.", 2010Sstevel@tonic-gate LOCKFILE, 2020Sstevel@tonic-gate write_cnt, 2030Sstevel@tonic-gate sizeof (buff)); 2040Sstevel@tonic-gate } 2050Sstevel@tonic-gate lockf(lockfile, F_ULOCK, 0); 2060Sstevel@tonic-gate close(lockfile); 2070Sstevel@tonic-gate return (FALSE); 2080Sstevel@tonic-gate } 2090Sstevel@tonic-gate } 2100Sstevel@tonic-gate } else { 2110Sstevel@tonic-gate syslog(LOG_ERR, 2120Sstevel@tonic-gate "fstat(%s) => errno=%d", LOCKFILE, errno); 2130Sstevel@tonic-gate lockf(lockfile, F_ULOCK, 0); 2140Sstevel@tonic-gate close(lockfile); 2150Sstevel@tonic-gate return (FALSE); 2160Sstevel@tonic-gate } 2170Sstevel@tonic-gate } else { 2180Sstevel@tonic-gate syslog(LOG_ERR, 2190Sstevel@tonic-gate "lockf(%s,F_LOCK) => errno=%d", LOCKFILE, errno); 2200Sstevel@tonic-gate close(lockfile); 2210Sstevel@tonic-gate return (FALSE); 2220Sstevel@tonic-gate } 2230Sstevel@tonic-gate } else { 2240Sstevel@tonic-gate syslog(LOG_ERR, 2250Sstevel@tonic-gate "open(%s) => errno=%d", LOCKFILE, errno); 2260Sstevel@tonic-gate return (FALSE); 2270Sstevel@tonic-gate } 2280Sstevel@tonic-gate 2290Sstevel@tonic-gate /* 2300Sstevel@tonic-gate * File exists with correct size, is open, and we're holding 2310Sstevel@tonic-gate * the file lock. 2320Sstevel@tonic-gate */ 2331533Ssm26363 shmlockarray = (lockarray *)mmap((caddr_t)0, sizeof (lockarray), 2340Sstevel@tonic-gate PROT_READ | PROT_WRITE, MAP_SHARED, lockfile, 0); 2350Sstevel@tonic-gate if (shmlockarray == MAP_FAILED) { 2360Sstevel@tonic-gate syslog(LOG_ERR, "mmap(%s) => errno=%d", LOCKFILE, errno); 2370Sstevel@tonic-gate lockf(lockfile, F_ULOCK, 0); 2380Sstevel@tonic-gate close(lockfile); 2390Sstevel@tonic-gate return (FALSE); 2400Sstevel@tonic-gate } 2410Sstevel@tonic-gate 2420Sstevel@tonic-gate /* 2430Sstevel@tonic-gate * If we wrote zeroes to the file, we also need to initialize 2440Sstevel@tonic-gate * the mutex locks. 2450Sstevel@tonic-gate */ 2460Sstevel@tonic-gate if (lf_size < sizeof (lockarray)) { 2470Sstevel@tonic-gate if (init_locks_mem() == FALSE) { 2480Sstevel@tonic-gate lockf(lockfile, F_ULOCK, 0); 2490Sstevel@tonic-gate close(lockfile); 2500Sstevel@tonic-gate if (remove(LOCKFILE) != 0) { 2510Sstevel@tonic-gate syslog(LOG_ERR, 2520Sstevel@tonic-gate "remove(%s) => errno=%d: Please delete file.", 2530Sstevel@tonic-gate LOCKFILE, errno); 2540Sstevel@tonic-gate } 2550Sstevel@tonic-gate return (FALSE); 2560Sstevel@tonic-gate } 2570Sstevel@tonic-gate } 2580Sstevel@tonic-gate 2590Sstevel@tonic-gate if (lockf(lockfile, F_ULOCK, 0) != 0) { 2600Sstevel@tonic-gate syslog(LOG_ERR, 2610Sstevel@tonic-gate "lockf(%s,F_ULOCK) => errno=%d", 2620Sstevel@tonic-gate LOCKFILE, errno); 2630Sstevel@tonic-gate close(lockfile); 2640Sstevel@tonic-gate return (FALSE); 2650Sstevel@tonic-gate } 2660Sstevel@tonic-gate 2670Sstevel@tonic-gate if (close(lockfile) == 0) { 2680Sstevel@tonic-gate return (TRUE); 2690Sstevel@tonic-gate } else { 2700Sstevel@tonic-gate syslog(LOG_ERR, 2710Sstevel@tonic-gate "close(%s) => errno=%d", LOCKFILE, errno); 2720Sstevel@tonic-gate return (FALSE); 2730Sstevel@tonic-gate } 2740Sstevel@tonic-gate } 2750Sstevel@tonic-gate 2760Sstevel@tonic-gate /* 2770Sstevel@tonic-gate * FUNCTION : lock_map() 2780Sstevel@tonic-gate * 2790Sstevel@tonic-gate * DESCRIPTION: Front end to the lock routine taking map name as argument. 2800Sstevel@tonic-gate * 2810Sstevel@tonic-gate * GIVEN : Map name. 2820Sstevel@tonic-gate * 2830Sstevel@tonic-gate * RETURNS : Same as lock_core 2840Sstevel@tonic-gate */ 2850Sstevel@tonic-gate int 2860Sstevel@tonic-gate lock_map(char *mapname) 2870Sstevel@tonic-gate { 2880Sstevel@tonic-gate int hashval; 2890Sstevel@tonic-gate 2900Sstevel@tonic-gate hashval = hash(mapname); 2910Sstevel@tonic-gate 2921533Ssm26363 return (lock_core(hashval)); 2930Sstevel@tonic-gate } 2940Sstevel@tonic-gate 2950Sstevel@tonic-gate /* 2960Sstevel@tonic-gate * FUNCTION : lock_core() 2970Sstevel@tonic-gate * 2980Sstevel@tonic-gate * DESCRIPTION: The core map locking function 2990Sstevel@tonic-gate * 3000Sstevel@tonic-gate * GIVEN : Map hash value 3010Sstevel@tonic-gate * 3020Sstevel@tonic-gate * RETURNS : 0 = Failure 3030Sstevel@tonic-gate * 1 = Success 3040Sstevel@tonic-gate */ 3050Sstevel@tonic-gate int 3060Sstevel@tonic-gate lock_core(int hashval) 3070Sstevel@tonic-gate { 3080Sstevel@tonic-gate int rc; 3090Sstevel@tonic-gate 3100Sstevel@tonic-gate /* 3111533Ssm26363 * Robust, cross-process lock implementation 3120Sstevel@tonic-gate */ 3130Sstevel@tonic-gate rc = mutex_lock(&(shmlockarray->locknode[hashval])); 3140Sstevel@tonic-gate while (rc != 0) { 3150Sstevel@tonic-gate switch (rc) { 3160Sstevel@tonic-gate case EOWNERDEAD: 3170Sstevel@tonic-gate /* 318*4574Sraf * Previous lock owner died, resetting lock 3190Sstevel@tonic-gate * to recover from error. 3200Sstevel@tonic-gate */ 321*4574Sraf rc = mutex_consistent( 322*4574Sraf &(shmlockarray->locknode[hashval])); 3230Sstevel@tonic-gate if (rc != 0) { 3240Sstevel@tonic-gate syslog(LOG_ERR, 325*4574Sraf "mutex_consistent(): error=%d", rc); 3260Sstevel@tonic-gate return (0); 3270Sstevel@tonic-gate } 3280Sstevel@tonic-gate rc = mutex_unlock(&(shmlockarray->locknode[hashval])); 3290Sstevel@tonic-gate if (rc != 0) { 3300Sstevel@tonic-gate syslog(LOG_ERR, 3310Sstevel@tonic-gate "mutex_unlock(): error=%d", rc); 3320Sstevel@tonic-gate return (0); 3330Sstevel@tonic-gate } 3340Sstevel@tonic-gate break; 3350Sstevel@tonic-gate default: 3360Sstevel@tonic-gate /* 3370Sstevel@tonic-gate * Unrecoverable problem - nothing to do 3380Sstevel@tonic-gate * but exit YP and delete lock file. 3390Sstevel@tonic-gate */ 3400Sstevel@tonic-gate syslog(LOG_ERR, 3410Sstevel@tonic-gate "mutex_lock(): error=%d", rc); 3420Sstevel@tonic-gate syslog(LOG_ERR, 3430Sstevel@tonic-gate "Please restart NIS (ypstop/ypstart)."); 3440Sstevel@tonic-gate if (remove(LOCKFILE) != 0) { 3450Sstevel@tonic-gate syslog(LOG_ERR, 3460Sstevel@tonic-gate "remove(%s) => errno=%d: Please delete file.", 3470Sstevel@tonic-gate LOCKFILE, errno); 3480Sstevel@tonic-gate } 3490Sstevel@tonic-gate return (0); 3500Sstevel@tonic-gate } 3510Sstevel@tonic-gate rc = mutex_lock(&(shmlockarray->locknode[hashval])); 3520Sstevel@tonic-gate } 3530Sstevel@tonic-gate 3540Sstevel@tonic-gate /* Success */ 3550Sstevel@tonic-gate return (1); 3560Sstevel@tonic-gate } 3570Sstevel@tonic-gate 3580Sstevel@tonic-gate 3590Sstevel@tonic-gate /* 3600Sstevel@tonic-gate * FUNCTION : unlock_map() 3610Sstevel@tonic-gate * 3620Sstevel@tonic-gate * DESCRIPTION: Front end to the unlock routine taking map name as argument. 3630Sstevel@tonic-gate * 3640Sstevel@tonic-gate * GIVEN : Map name. 3650Sstevel@tonic-gate * 3660Sstevel@tonic-gate * RETURNS : Same as unlock_core 3670Sstevel@tonic-gate */ 3680Sstevel@tonic-gate int 3690Sstevel@tonic-gate unlock_map(char *mapname) 3700Sstevel@tonic-gate { 3710Sstevel@tonic-gate int hashval; 3720Sstevel@tonic-gate 3730Sstevel@tonic-gate hashval = hash(mapname); 3740Sstevel@tonic-gate 3751533Ssm26363 return (unlock_core(hashval)); 3760Sstevel@tonic-gate } 3770Sstevel@tonic-gate 3780Sstevel@tonic-gate /* 3790Sstevel@tonic-gate * FUNCTION : unlock_core() 3800Sstevel@tonic-gate * 3810Sstevel@tonic-gate * DESCRIPTION: The core map locking function 3820Sstevel@tonic-gate * 3830Sstevel@tonic-gate * GIVEN : Map hash value 3840Sstevel@tonic-gate * 3850Sstevel@tonic-gate * RETURNS : 0 = Failure 3860Sstevel@tonic-gate * 1 = Success 3870Sstevel@tonic-gate */ 3880Sstevel@tonic-gate int 3890Sstevel@tonic-gate unlock_core(int hashval) 3900Sstevel@tonic-gate { 3910Sstevel@tonic-gate int rc; 3920Sstevel@tonic-gate 3930Sstevel@tonic-gate rc = mutex_unlock(&(shmlockarray->locknode[hashval])); 3940Sstevel@tonic-gate if (rc != 0) { 3950Sstevel@tonic-gate syslog(LOG_ERR, 3960Sstevel@tonic-gate "mutex_unlock(): error=%d", rc); 3970Sstevel@tonic-gate syslog(LOG_ERR, 3980Sstevel@tonic-gate "Please restart NIS (ypstop/ypstart)."); 3990Sstevel@tonic-gate if (remove(LOCKFILE) != 0) { 4000Sstevel@tonic-gate syslog(LOG_ERR, 4010Sstevel@tonic-gate "remove(%s) => errno=%d: Please delete file.", 4020Sstevel@tonic-gate LOCKFILE, errno); 4030Sstevel@tonic-gate } 4040Sstevel@tonic-gate return (0); 4050Sstevel@tonic-gate } 4060Sstevel@tonic-gate 4070Sstevel@tonic-gate /* Success */ 4080Sstevel@tonic-gate return (1); 4090Sstevel@tonic-gate } 410