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