/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
*/
/*
* DESCRIPTION: Contains code supporting the 'update in progress' flag. This is
* a near copy of lock flag code (in
* version of the locking code this file will probably disappear.
*
* These locks are held while a map is being updated from the
* DIT. They prevent a second update being started while this is
* in progress. This is independant from the `lockmap` mechanism
* which protects maps, generally for a much shorter period,
* while their control structures are modified.
*/
#include <unistd.h>
#include <syslog.h>
#include <thread.h>
#include <synch.h>
#include <ndbm.h>
#include <strings.h>
#include "ypsym.h"
#include "shim.h"
#include "yptol.h"
#include "../ldap_util.h"
struct updatearray {
};
/*
* Cross-process robust mutex locks.
* Provide synchronization between YP processes
* by implementing an exclusive locking mechanism
* via a memory-mapped file.
*/
static int lockfile;
{
int ebusy_cnt = 0;
/*
* Initialize cross-process locks in memory-mapped file.
*/
USYNC_PROCESS | LOCK_ROBUST, 0)) {
ebusy_cnt++;
} else {
"init_update_locks_mem():mutex_init():"
"error=%d", rc);
return (FALSE);
}
}
}
/*
* EBUSY for all locks OK, it means another process
* has already initialized locks.
*/
"%s inconsistent. Remove this file and restart NIS (YP)",
LOCKFILE);
return (FALSE);
}
return (TRUE);
}
{
/*
* Locking file initialization algorithm, with recovery mechanism.
* This mechanism has been devised to ensure proper creation
* of a memory-mapped lock file containing mutexes for robust,
* inter-process communication.
* File name is $_PATH_SYSVOL/yp_mapupate (LOCKFILE). It might or might
* not exist.
*
* Algorithm:
* Try to open the file. If file doesn't exist, or size is too small,
* mutexes in it.
* If file exists and size is at least large enough, assume it's a
* good file, and m-map the lock structure directly to it.
*
* Recovery from inconsistent state is easy - simply delete the file
* and restart NIS (YP).
*/
if (lockfile != -1) {
if (lf_size < sizeof (updatearray)) {
if (write_cnt < 0) {
"write(%s) => errno=%d",
} else {
"write(%s) => %d!=%d: wrong number of bytes written",
sizeof (buff));
}
return (FALSE);
}
}
} else {
return (FALSE);
}
} else {
return (FALSE);
}
} else {
return (FALSE);
}
/*
* File exists with correct size, is open, and we're holding
* the file lock.
*/
if (shmupdatearray == MAP_FAILED) {
return (FALSE);
}
/*
* If we wrote zeroes to the file, we also need to initialize
* the mutex locks.
*/
if (lf_size < sizeof (updatearray)) {
if (init_update_locks_mem() == FALSE) {
"remove(%s) => errno=%d: Please delete file",
}
return (FALSE);
}
}
return (FALSE);
}
return (TRUE);
} else {
return (FALSE);
}
}
{
int rc;
/*
* Robust, cross-process lock implementation
*/
while (rc != 0) {
switch (rc) {
case EOWNERDEAD:
/*
* Previous lock owner died, resetting lock
* to recover from error.
*/
if (rc != 0) {
"mutex_consistent(): error=%d", rc);
return (FAILURE);
}
rc = mutex_unlock(
if (rc != 0) {
"mutex_unlock(): error=%d", rc);
return (FAILURE);
}
break;
default:
/*
* Unrecoverable problem - nothing to do
* but exit YP and delete lock file.
*/
"mutex_lock(): error=%d", rc);
"remove(%s) => errno=%d: Please delete file",
}
return (FAILURE);
}
}
/* Success */
return (SUCCESS);
}
{
int rc;
if (rc != 0) {
"mutex_unlock(): error=%d", rc);
"remove(%s) => errno=%d: Please delete file",
}
return (FAILURE);
}
/* Success */
return (SUCCESS);
}
/*
* FUNCTION : is_map_updating()
*
* DESCRIPTION: Determines if a map is currently locked for update
*
* GIVEN : Pointer to map_ctrl structure
*
* RETURNS : TRUE = Map is locked
* FALSE = Map is not locked
*/
{
int ret;
/* It appears not to be possible to just read a mutex. Try to lock it */
if (0 != ret) {
/* Didn't get the lock ... was already locked */
return (TRUE);
}
/* Didn't need the lock so free it again */
return (FALSE);
}
/*
* FUNCTION : try_lock_map_update()
*
* DESCRIPTION: Tries to to lock a map for update.
*
* GIVEN : Pointer to the map to lock
*
* RETURNS : 0 = The map is now locked
* EBUSY = The map was already locked lock not obtained.
* Other = There was an error
*/
int
{
int rc;
/*
* Robust, cross-process lock implementation
*
* Keep trying until either lock is obtained or somebody else gets it.
*/
while (1) {
switch (rc) {
case 0:
case EBUSY:
/* Either got it or somebody else has it */
return (rc);
case EOWNERDEAD:
/*
* Previous lock owner died, resetting lock
* to recover from error.
*/
if (rc != 0) {
"mutex_consistent(): error=%d", rc);
return (rc);
}
rc = mutex_unlock(
if (rc != 0) {
"mutex_unlock(): error=%d", rc);
return (rc);
}
break;
default:
/*
* Unrecoverable problem - nothing to do
* but exit YP and delete lock file.
*/
"mutex_lock(): error=%d", rc);
"remove(%s) => errno=%d: Please delete file",
}
return (rc);
}
}
}