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 */ 210Sstevel@tonic-gate /* 221533Ssm26363 * 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> 33*2189Ssdussud #include <strings.h> 340Sstevel@tonic-gate #include <ndbm.h> 350Sstevel@tonic-gate #include "../ypsym.h" 360Sstevel@tonic-gate #include "../ypdefs.h" 37*2189Ssdussud #include "shim.h" 380Sstevel@tonic-gate 390Sstevel@tonic-gate /* 400Sstevel@tonic-gate * These routines provide mutual exclusion between ypserv and ypxfr. 410Sstevel@tonic-gate * Mutual exclusion is needed so that ypxfr doesn't try to rename 420Sstevel@tonic-gate * dbm files while ypserv is trying to open them. After ypserv has 430Sstevel@tonic-gate * opened a dbm file, it is safe to rename it because ypserv still 440Sstevel@tonic-gate * has access to the file through its file descriptor. 450Sstevel@tonic-gate */ 460Sstevel@tonic-gate 470Sstevel@tonic-gate #define LOCKFILE "/var/run/yp_maplock" 480Sstevel@tonic-gate struct lockarray { 490Sstevel@tonic-gate mutex_t locknode[MAXHASH]; 500Sstevel@tonic-gate }; 510Sstevel@tonic-gate typedef struct lockarray lockarray; 520Sstevel@tonic-gate 530Sstevel@tonic-gate /* 540Sstevel@tonic-gate * Cross-process robust mutex locks. 550Sstevel@tonic-gate * Provide synchronization between YP processes 560Sstevel@tonic-gate * by implementing an exclusive locking mechanism 570Sstevel@tonic-gate * via a memory-mapped file. 580Sstevel@tonic-gate */ 590Sstevel@tonic-gate static struct lockarray *shmlockarray; 600Sstevel@tonic-gate static int lockfile; 610Sstevel@tonic-gate 62*2189Ssdussud /* 63*2189Ssdussud * Hash functions, used for by the locking mechanism. 64*2189Ssdussud * 65*2189Ssdussud * - hash() is the front-end function that gets called. 66*2189Ssdussud * - get_map_id() returns a unique int value per map. 67*2189Ssdussud * It is used in N2L mode only. 68*2189Ssdussud * It is called by hash() in N2L mode. 69*2189Ssdussud */ 70*2189Ssdussud int 71*2189Ssdussud get_map_id(char *map_name, int index) 72*2189Ssdussud { 73*2189Ssdussud map_id_elt_t *cur_elt; 74*2189Ssdussud /* 75*2189Ssdussud * Local references to hash table for map lists 76*2189Ssdussud * and to max number of maps 77*2189Ssdussud */ 78*2189Ssdussud map_id_elt_t **map_list_p; 79*2189Ssdussud int max_map; 80*2189Ssdussud 81*2189Ssdussud /* initializes map_list_p & max_map */ 82*2189Ssdussud get_list_max(&map_list_p, &max_map); 83*2189Ssdussud 84*2189Ssdussud cur_elt = map_list_p[index]; 85*2189Ssdussud while (cur_elt != NULL) { 86*2189Ssdussud if (strcmp(map_name, cur_elt->map_name) == 0) { 87*2189Ssdussud /* found */ 88*2189Ssdussud return (cur_elt->map_id); 89*2189Ssdussud } 90*2189Ssdussud cur_elt = cur_elt->next; 91*2189Ssdussud } 92*2189Ssdussud syslog(LOG_WARNING, "get_map_id: no hash id found for %s" 93*2189Ssdussud ", giving max_map value (%d)", 94*2189Ssdussud map_name, max_map); 95*2189Ssdussud /* 96*2189Ssdussud * max_map does not match any map id, hence 97*2189Ssdussud * will not trigger any lock collision 98*2189Ssdussud * with existing maps. 99*2189Ssdussud * Needed for yp regular locking mechanism. 100*2189Ssdussud */ 101*2189Ssdussud return (max_map); 102*2189Ssdussud } 103*2189Ssdussud 1040Sstevel@tonic-gate int 1050Sstevel@tonic-gate hash(char *s) 1060Sstevel@tonic-gate { 1071533Ssm26363 unsigned int n = 0; 1080Sstevel@tonic-gate int i; 109*2189Ssdussud char *map_name = s; 1100Sstevel@tonic-gate 1110Sstevel@tonic-gate for (i = 1; *s; i += 10, s++) { 1120Sstevel@tonic-gate n += i * (*s); 1130Sstevel@tonic-gate } 1140Sstevel@tonic-gate n %= MAXHASH; 115*2189Ssdussud 116*2189Ssdussud if (yptol_mode & yptol_newlock) { 117*2189Ssdussud return (get_map_id(map_name, n)); 118*2189Ssdussud } else { 119*2189Ssdussud return (n); 120*2189Ssdussud } 1210Sstevel@tonic-gate } 1220Sstevel@tonic-gate 1230Sstevel@tonic-gate bool 1240Sstevel@tonic-gate init_locks_mem() 1250Sstevel@tonic-gate { 1260Sstevel@tonic-gate int iiter, rc; 1270Sstevel@tonic-gate int ebusy_cnt = 0; 1280Sstevel@tonic-gate 1290Sstevel@tonic-gate /* 1300Sstevel@tonic-gate * Initialize cross-process locks in memory-mapped file. 1310Sstevel@tonic-gate */ 1320Sstevel@tonic-gate for (iiter = 0; iiter < MAXHASH; iiter++) { 1330Sstevel@tonic-gate if (rc = mutex_init(&(shmlockarray->locknode[iiter]), 1340Sstevel@tonic-gate USYNC_PROCESS_ROBUST, 0)) { 1350Sstevel@tonic-gate if (rc == EBUSY) { 1360Sstevel@tonic-gate ebusy_cnt++; 1370Sstevel@tonic-gate } else { 1380Sstevel@tonic-gate syslog(LOG_ERR, 1390Sstevel@tonic-gate "init_locks_mem():mutex_init():error=%d", 1400Sstevel@tonic-gate rc); 1410Sstevel@tonic-gate return (FALSE); 1420Sstevel@tonic-gate } 1430Sstevel@tonic-gate } 1440Sstevel@tonic-gate } 1450Sstevel@tonic-gate 1460Sstevel@tonic-gate /* 1470Sstevel@tonic-gate * EBUSY for all locks OK, it means another process 1480Sstevel@tonic-gate * has already initialized locks. 1490Sstevel@tonic-gate */ 1500Sstevel@tonic-gate if ((ebusy_cnt > 0) && (ebusy_cnt != MAXHASH)) { 1510Sstevel@tonic-gate syslog(LOG_ERR, 1520Sstevel@tonic-gate "%s inconsistent. Remove and restart NIS (YP).", LOCKFILE); 1530Sstevel@tonic-gate return (FALSE); 1540Sstevel@tonic-gate } 1550Sstevel@tonic-gate return (TRUE); 1560Sstevel@tonic-gate } 1570Sstevel@tonic-gate 1580Sstevel@tonic-gate bool 1590Sstevel@tonic-gate init_lock_map() 1600Sstevel@tonic-gate { 1610Sstevel@tonic-gate char buff[ sizeof (lockarray) ]; 1620Sstevel@tonic-gate int write_cnt, lf_size; 1630Sstevel@tonic-gate struct stat fdata; 1640Sstevel@tonic-gate 1650Sstevel@tonic-gate /* 1660Sstevel@tonic-gate * Locking file initialization algorithm, with recovery mechanism. 1670Sstevel@tonic-gate * This mechanism has been devised to ensure proper creation 1680Sstevel@tonic-gate * of a memory-mapped lock file containing mutexes for robust, 1690Sstevel@tonic-gate * inter-process communication. 1700Sstevel@tonic-gate * File name is /var/run/yp_maplock (LOCKFILE). It might or might 1710Sstevel@tonic-gate * not exist. 1720Sstevel@tonic-gate * 1730Sstevel@tonic-gate * Algorithm: 1740Sstevel@tonic-gate * Try to open the file. If file doesn't exist, or size is too small, 1750Sstevel@tonic-gate * create/rewrite the file, m-map it into memory and initialize the 1760Sstevel@tonic-gate * mutexes in it. 1770Sstevel@tonic-gate * If file exists and size is at least large enough, assume it's a 1780Sstevel@tonic-gate * good file, and m-map the lock structure directly to it. 1790Sstevel@tonic-gate * 1800Sstevel@tonic-gate * Recovery from inconsistent state is easy - simply delete the file 1810Sstevel@tonic-gate * and restart NIS (YP). 1820Sstevel@tonic-gate */ 1830Sstevel@tonic-gate 1840Sstevel@tonic-gate lockfile = open(LOCKFILE, O_RDWR|O_CREAT, 0600); 1850Sstevel@tonic-gate if (lockfile != -1) { 1860Sstevel@tonic-gate if (lockf(lockfile, F_LOCK, 0) == 0) { 1870Sstevel@tonic-gate if (fstat(lockfile, &fdata) == 0) { 1880Sstevel@tonic-gate lf_size = fdata.st_size; 1890Sstevel@tonic-gate if (lf_size < sizeof (lockarray)) { 1900Sstevel@tonic-gate bzero(buff, sizeof (buff)); 1910Sstevel@tonic-gate if ((write_cnt = write(lockfile, buff, 1920Sstevel@tonic-gate sizeof (buff)) != sizeof (buff))) { 1930Sstevel@tonic-gate if (write_cnt < 0) { 1940Sstevel@tonic-gate syslog(LOG_ERR, 1950Sstevel@tonic-gate "write(%s) => errno=%d", 1960Sstevel@tonic-gate LOCKFILE, errno); 1970Sstevel@tonic-gate } else { 1980Sstevel@tonic-gate syslog(LOG_ERR, 1990Sstevel@tonic-gate "write(%s) => %d!=%d: wrong number of bytes written.", 2000Sstevel@tonic-gate LOCKFILE, 2010Sstevel@tonic-gate write_cnt, 2020Sstevel@tonic-gate sizeof (buff)); 2030Sstevel@tonic-gate } 2040Sstevel@tonic-gate lockf(lockfile, F_ULOCK, 0); 2050Sstevel@tonic-gate close(lockfile); 2060Sstevel@tonic-gate return (FALSE); 2070Sstevel@tonic-gate } 2080Sstevel@tonic-gate } 2090Sstevel@tonic-gate } else { 2100Sstevel@tonic-gate syslog(LOG_ERR, 2110Sstevel@tonic-gate "fstat(%s) => errno=%d", LOCKFILE, errno); 2120Sstevel@tonic-gate lockf(lockfile, F_ULOCK, 0); 2130Sstevel@tonic-gate close(lockfile); 2140Sstevel@tonic-gate return (FALSE); 2150Sstevel@tonic-gate } 2160Sstevel@tonic-gate } else { 2170Sstevel@tonic-gate syslog(LOG_ERR, 2180Sstevel@tonic-gate "lockf(%s,F_LOCK) => errno=%d", LOCKFILE, errno); 2190Sstevel@tonic-gate close(lockfile); 2200Sstevel@tonic-gate return (FALSE); 2210Sstevel@tonic-gate } 2220Sstevel@tonic-gate } else { 2230Sstevel@tonic-gate syslog(LOG_ERR, 2240Sstevel@tonic-gate "open(%s) => errno=%d", LOCKFILE, errno); 2250Sstevel@tonic-gate return (FALSE); 2260Sstevel@tonic-gate } 2270Sstevel@tonic-gate 2280Sstevel@tonic-gate /* 2290Sstevel@tonic-gate * File exists with correct size, is open, and we're holding 2300Sstevel@tonic-gate * the file lock. 2310Sstevel@tonic-gate */ 2321533Ssm26363 shmlockarray = (lockarray *)mmap((caddr_t)0, sizeof (lockarray), 2330Sstevel@tonic-gate PROT_READ | PROT_WRITE, MAP_SHARED, lockfile, 0); 2340Sstevel@tonic-gate if (shmlockarray == MAP_FAILED) { 2350Sstevel@tonic-gate syslog(LOG_ERR, "mmap(%s) => errno=%d", LOCKFILE, errno); 2360Sstevel@tonic-gate lockf(lockfile, F_ULOCK, 0); 2370Sstevel@tonic-gate close(lockfile); 2380Sstevel@tonic-gate return (FALSE); 2390Sstevel@tonic-gate } 2400Sstevel@tonic-gate 2410Sstevel@tonic-gate /* 2420Sstevel@tonic-gate * If we wrote zeroes to the file, we also need to initialize 2430Sstevel@tonic-gate * the mutex locks. 2440Sstevel@tonic-gate */ 2450Sstevel@tonic-gate if (lf_size < sizeof (lockarray)) { 2460Sstevel@tonic-gate if (init_locks_mem() == FALSE) { 2470Sstevel@tonic-gate lockf(lockfile, F_ULOCK, 0); 2480Sstevel@tonic-gate close(lockfile); 2490Sstevel@tonic-gate if (remove(LOCKFILE) != 0) { 2500Sstevel@tonic-gate syslog(LOG_ERR, 2510Sstevel@tonic-gate "remove(%s) => errno=%d: Please delete file.", 2520Sstevel@tonic-gate LOCKFILE, errno); 2530Sstevel@tonic-gate } 2540Sstevel@tonic-gate return (FALSE); 2550Sstevel@tonic-gate } 2560Sstevel@tonic-gate } 2570Sstevel@tonic-gate 2580Sstevel@tonic-gate if (lockf(lockfile, F_ULOCK, 0) != 0) { 2590Sstevel@tonic-gate syslog(LOG_ERR, 2600Sstevel@tonic-gate "lockf(%s,F_ULOCK) => errno=%d", 2610Sstevel@tonic-gate LOCKFILE, errno); 2620Sstevel@tonic-gate close(lockfile); 2630Sstevel@tonic-gate return (FALSE); 2640Sstevel@tonic-gate } 2650Sstevel@tonic-gate 2660Sstevel@tonic-gate if (close(lockfile) == 0) { 2670Sstevel@tonic-gate return (TRUE); 2680Sstevel@tonic-gate } else { 2690Sstevel@tonic-gate syslog(LOG_ERR, 2700Sstevel@tonic-gate "close(%s) => errno=%d", LOCKFILE, errno); 2710Sstevel@tonic-gate return (FALSE); 2720Sstevel@tonic-gate } 2730Sstevel@tonic-gate } 2740Sstevel@tonic-gate 2750Sstevel@tonic-gate /* 2760Sstevel@tonic-gate * FUNCTION : lock_map() 2770Sstevel@tonic-gate * 2780Sstevel@tonic-gate * DESCRIPTION: Front end to the lock routine taking map name as argument. 2790Sstevel@tonic-gate * 2800Sstevel@tonic-gate * GIVEN : Map name. 2810Sstevel@tonic-gate * 2820Sstevel@tonic-gate * RETURNS : Same as lock_core 2830Sstevel@tonic-gate */ 2840Sstevel@tonic-gate int 2850Sstevel@tonic-gate lock_map(char *mapname) 2860Sstevel@tonic-gate { 2870Sstevel@tonic-gate int hashval; 2880Sstevel@tonic-gate 2890Sstevel@tonic-gate hashval = hash(mapname); 2900Sstevel@tonic-gate 2911533Ssm26363 return (lock_core(hashval)); 2920Sstevel@tonic-gate } 2930Sstevel@tonic-gate 2940Sstevel@tonic-gate /* 2950Sstevel@tonic-gate * FUNCTION : lock_core() 2960Sstevel@tonic-gate * 2970Sstevel@tonic-gate * DESCRIPTION: The core map locking function 2980Sstevel@tonic-gate * 2990Sstevel@tonic-gate * GIVEN : Map hash value 3000Sstevel@tonic-gate * 3010Sstevel@tonic-gate * RETURNS : 0 = Failure 3020Sstevel@tonic-gate * 1 = Success 3030Sstevel@tonic-gate */ 3040Sstevel@tonic-gate int 3050Sstevel@tonic-gate lock_core(int hashval) 3060Sstevel@tonic-gate { 3070Sstevel@tonic-gate int rc; 3080Sstevel@tonic-gate 3090Sstevel@tonic-gate /* 3101533Ssm26363 * Robust, cross-process lock implementation 3110Sstevel@tonic-gate */ 3120Sstevel@tonic-gate rc = mutex_lock(&(shmlockarray->locknode[hashval])); 3130Sstevel@tonic-gate while (rc != 0) { 3140Sstevel@tonic-gate switch (rc) { 3150Sstevel@tonic-gate case EOWNERDEAD: 3160Sstevel@tonic-gate /* 3170Sstevel@tonic-gate * Previows lock owner died, resetting lock 3180Sstevel@tonic-gate * to recover from error. 3190Sstevel@tonic-gate */ 3200Sstevel@tonic-gate rc = mutex_init(&(shmlockarray->locknode[hashval]), 3210Sstevel@tonic-gate USYNC_PROCESS_ROBUST, 0); 3220Sstevel@tonic-gate if (rc != 0) { 3230Sstevel@tonic-gate syslog(LOG_ERR, 3240Sstevel@tonic-gate "mutex_init(): error=%d", rc); 3250Sstevel@tonic-gate return (0); 3260Sstevel@tonic-gate } 3270Sstevel@tonic-gate rc = mutex_unlock(&(shmlockarray->locknode[hashval])); 3280Sstevel@tonic-gate if (rc != 0) { 3290Sstevel@tonic-gate syslog(LOG_ERR, 3300Sstevel@tonic-gate "mutex_unlock(): error=%d", rc); 3310Sstevel@tonic-gate return (0); 3320Sstevel@tonic-gate } 3330Sstevel@tonic-gate break; 3340Sstevel@tonic-gate default: 3350Sstevel@tonic-gate /* 3360Sstevel@tonic-gate * Unrecoverable problem - nothing to do 3370Sstevel@tonic-gate * but exit YP and delete lock file. 3380Sstevel@tonic-gate */ 3390Sstevel@tonic-gate syslog(LOG_ERR, 3400Sstevel@tonic-gate "mutex_lock(): error=%d", rc); 3410Sstevel@tonic-gate syslog(LOG_ERR, 3420Sstevel@tonic-gate "Please restart NIS (ypstop/ypstart)."); 3430Sstevel@tonic-gate if (remove(LOCKFILE) != 0) { 3440Sstevel@tonic-gate syslog(LOG_ERR, 3450Sstevel@tonic-gate "remove(%s) => errno=%d: Please delete file.", 3460Sstevel@tonic-gate LOCKFILE, errno); 3470Sstevel@tonic-gate } 3480Sstevel@tonic-gate return (0); 3490Sstevel@tonic-gate } 3500Sstevel@tonic-gate rc = mutex_lock(&(shmlockarray->locknode[hashval])); 3510Sstevel@tonic-gate } 3520Sstevel@tonic-gate 3530Sstevel@tonic-gate /* Success */ 3540Sstevel@tonic-gate return (1); 3550Sstevel@tonic-gate } 3560Sstevel@tonic-gate 3570Sstevel@tonic-gate 3580Sstevel@tonic-gate /* 3590Sstevel@tonic-gate * FUNCTION : unlock_map() 3600Sstevel@tonic-gate * 3610Sstevel@tonic-gate * DESCRIPTION: Front end to the unlock routine taking map name as argument. 3620Sstevel@tonic-gate * 3630Sstevel@tonic-gate * GIVEN : Map name. 3640Sstevel@tonic-gate * 3650Sstevel@tonic-gate * RETURNS : Same as unlock_core 3660Sstevel@tonic-gate */ 3670Sstevel@tonic-gate int 3680Sstevel@tonic-gate unlock_map(char *mapname) 3690Sstevel@tonic-gate { 3700Sstevel@tonic-gate int hashval; 3710Sstevel@tonic-gate 3720Sstevel@tonic-gate hashval = hash(mapname); 3730Sstevel@tonic-gate 3741533Ssm26363 return (unlock_core(hashval)); 3750Sstevel@tonic-gate } 3760Sstevel@tonic-gate 3770Sstevel@tonic-gate /* 3780Sstevel@tonic-gate * FUNCTION : unlock_core() 3790Sstevel@tonic-gate * 3800Sstevel@tonic-gate * DESCRIPTION: The core map locking function 3810Sstevel@tonic-gate * 3820Sstevel@tonic-gate * GIVEN : Map hash value 3830Sstevel@tonic-gate * 3840Sstevel@tonic-gate * RETURNS : 0 = Failure 3850Sstevel@tonic-gate * 1 = Success 3860Sstevel@tonic-gate */ 3870Sstevel@tonic-gate int 3880Sstevel@tonic-gate unlock_core(int hashval) 3890Sstevel@tonic-gate { 3900Sstevel@tonic-gate int rc; 3910Sstevel@tonic-gate 3920Sstevel@tonic-gate rc = mutex_unlock(&(shmlockarray->locknode[hashval])); 3930Sstevel@tonic-gate if (rc != 0) { 3940Sstevel@tonic-gate syslog(LOG_ERR, 3950Sstevel@tonic-gate "mutex_unlock(): error=%d", rc); 3960Sstevel@tonic-gate syslog(LOG_ERR, 3970Sstevel@tonic-gate "Please restart NIS (ypstop/ypstart)."); 3980Sstevel@tonic-gate if (remove(LOCKFILE) != 0) { 3990Sstevel@tonic-gate syslog(LOG_ERR, 4000Sstevel@tonic-gate "remove(%s) => errno=%d: Please delete file.", 4010Sstevel@tonic-gate LOCKFILE, errno); 4020Sstevel@tonic-gate } 4030Sstevel@tonic-gate return (0); 4040Sstevel@tonic-gate } 4050Sstevel@tonic-gate 4060Sstevel@tonic-gate /* Success */ 4070Sstevel@tonic-gate return (1); 4080Sstevel@tonic-gate } 409