db_mindex3.cc revision 36e852a172cba914383d7341c988128b2c667fbd
/*
* 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
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <time.h>
#include <lber.h>
#include <ldap.h>
#include <signal.h>
#include <pthread.h>
#include "db_headers.h"
#include "db.h"
#include "db_mindex.h"
#include "db_dictionary.h"
#include "nisdb_mt.h"
#include "ldap_map.h"
#include "ldap_glob.h"
#include "ldap_util.h"
extern db_dictionary *InUseDictionary;
extern "C" {
typedef struct {
db_query *q;
char *dbId;
int isDeferred;
char *tableName;
static void *entriesFromLDAPthread(void *);
}
#ifdef SET_ENTRY_FLAGS
static uint_t
return (ef);
}
#endif /* SET_ENTRY_FLAGS */
/*
* Retrieve container entries from LDAP per 't' and 'qin'/'q'.
* This is a helper function for db_mindex::queryLDAP(); see
* that function for details of the parameters (except doAsynch).
*
* If 'doAsynch' is set, and the retrieval is an enumeration
* (qin == NULL), the retrieval is performed in a detached
* thread. In this case, the return code just reflects the
* setup and launch of the detached thread. Retrieval will
* complete asynchronously.
*/
int
int stat;
char *myself = "db_mindex::entriesFromLDAP";
if (arg == 0) {
freeQuery(q);
if (dirObj != 0)
return (LDAP_NO_MEMORY);
}
arg->t = t;
arg->q = q;
/*
* Check if an enumeration thread is running; if so, then regardless
* of whether or not the current operation is an enumeration, we
* just return success, and let our caller get the data from the
* existing (deferred) DB.
*/
int doReturn = 0;
"%s: Enumeration thread %d not found for \"%s\"; exit status = %d (%s)",
/* Reflect the fact that no enum thread is running */
/* Cleanup deferred mode */
if (dstat == DB_SUCCESS) {
} else {
"%s: DB error %d committing \"%s\"",
}
}
} else if (stat == 0) {
"%s: Enumeration thread %d already running for \"%s\"",
stat = LDAP_SUCCESS;
doReturn = 1;
} else {
"%s: Error %d looking for enumeration thread %d for \"%s\"",
doReturn = 1;
}
if (doReturn) {
freeQuery(q);
if (dirObj != 0)
return (stat);
}
}
/*
* If we're enumerating (and hence expect that retrieving all data,
* and updating the local DB, might take a while), create a deferred-
* update table that clients can use while we are updating the real
* one.
*/
DB_SUCCESS) {
} else {
"%s: Unable to defer updates for \"%s\" (status=%d);"
" updating in place",
arg->isDeferred = 0;
}
} else {
arg->isDeferred = 0;
}
/* If enumerating, perform the operation in a separate thread */
(void) pthread_attr_init(&attr);
#ifdef FORCE_SYNCHRONOUS
#else
(void) pthread_attr_setdetachstate(&attr,
#endif /* FORCE_SYNCHRONOUS */
if (stat != 0) {
"%s: Error %d creating new thread; using current one",
return (stat);
}
/*
* We're now returning to the caller, who will get data
* from:
*
* The deferred DB, if an enumeration thread already
* was running, and deferred mode was on, or
*
* The original DB, if we just started an enumeration
* thread. In this case, our caller (several levels up)
* means that the enum thread will have to wait for
* our caller once it's done the LDAP retrieval, and
* wants to update the DB.
*/
stat = LDAP_SUCCESS;
#ifdef FORCE_SYNCHRONOUS
{
int tstat;
if (stat == 0) {
"%s: thread %d => %d",
} else {
"%s: pthread_join(%d) => %d",
}
}
#endif /* FORCE_SYNCHRONOUS */
} else {
}
return (stat);
}
extern "C" {
/*
* We use this 'extern "C"' function in order to make sure that
* pthread_create() doesn't have any problems trying to invoke a
* C++ function.
*/
static void *
entriesFromLDAPthread(void *voidarg) {
int stat;
db_table_desc *tbl = 0;
char *tableName;
/* Lock to prevent removal */
"entriesFromLDAPthread");
/*
* It's possible that the db_mindex for the table has changed,
* or disappeared, between now and the time when our parent
* thread released its lock on the table. Hence, we search the
* dictionary to re-acquire the 'db', and the db_mindex.
*/
if (tableName != 0) {
#ifdef NISDB_LDAP_DEBUG
#endif /* NISDB_LDAP_DEBUG */
if (dbase != 0)
else
#ifdef NISDB_LDAP_DEBUG
"entriesFromLDAPthread: %s -> %s -> 0x%x (0x%x)",
#endif /* NISDB_LDAP_DEBUG */
tableName = 0;
}
"entriesFromLDAPthread");
return ((void *)stat);
}
}
int
char *dbId;
int lstat;
__nis_obj_attr_t **oa = 0;
entry_object **ea;
long numEa;
char *myself =
"db_mindex::entriesFromLDAPreal";
if (arg == 0)
return (LDAP_PARAM_ERROR);
t = arg->t;
q = arg->q;
/* We haven't done anything, so rollback should be OK */
if (arg->isDeferred && t != 0) {
if (dstat != DB_SUCCESS) {
"%s: DB error %d rolling back \"%s\"",
/*
* Had rollback succeeded, the 'table'
* would have disappeared. However, since
* rollback failed, we need to update the
* table->mapping.enum* fields.
*/
if (table != 0) {
(void) mutex_lock(&table->
(void) mutex_unlock(&table->
}
}
}
return (LDAP_PARAM_ERROR);
}
if (qin == 0)
(void) gettimeofday(&start, 0);
/* Getting table entries */
#ifdef NISDB_LDAP_DEBUG
"%s: mapFromLDAP() => 0x%x, status=%d %s; nq = %d",
#endif /* NISDB_LDAP_DEBUG */
/*
* Keep track of the number of NIS+ entries we got back;
* note that the number of LDAP entries may have been
* smaller or larger.
*/
/*
* If we get LDAP_NO_SUCH_OBJECT, we need to delete the entries
* in the table, so we can't just return.
*/
"%s: mapFromLDAP() => 0x0, status=%d (%s)",
if (arg->isDeferred) {
if (dstat != DB_SUCCESS) {
"%s: DB error %d rolling back \"%s\"",
/*
* Had rollback succeeded, the 'table'
* would have disappeared. However, since
* rollback failed, we need to update the
* table->mapping.enum* fields.
*/
(void) gettimeofday(&end, 0);
}
}
}
return (stat);
}
/*
* Need to disable write-through to LDAP, for which we need a lock
* on our db_mindex ('mindex'); we're also updating the table, so
* we need a write lock on that as well. However, before locking the
* mindex, we need to maintain lock integrity by acquiring the
* trans log lock. Note that actually beginning a transaction is
* expensive, so we defer that until we know that we really need
* to update.
*/
if (lstat != 0) {
"%s: transaction log busy; no LDAP update for \"%s\"",
else
"%s: Error %d locking transaction log; no LDAP update for \"%s\"",
if (arg->isDeferred) {
if (dstat != DB_SUCCESS) {
"%s: DB error %d rolling back \"%s\"",
/*
* Had rollback succeeded, the 'table'
* would have disappeared. However, since
* rollback failed, we need to update the
* table->mapping.enum* fields.
*/
(void) gettimeofday(&end, 0);
}
}
}
return (LDAP_OPERATIONS_ERROR);
}
/*
* If we have any updates, we'll call db::sync_log, which write-
* locks the 'db' instance. In order to avoid a dead-lock with
* threads performing a DB lookup (which will lock the 'db' and
* then the 'db_mindex'), we need hence need to lock in the
* following order:
*
* trans.log (already holding that one)
* db
* db_mindex
* db_table
*/
"w db db_mindex::entriesFromLDAPreal");
if (stat == 0) {
if (stat2 == 0) {
"table w db_mindex::entriesFromLDAPreal");
}
}
if (stat != 0) {
"%s: 'db' busy; no LDAP update for \"%s\"",
else
"%s: 'db' lock error %d; no LDAP update for \"%s\"",
} else if (stat2 != 0) {
"%s: 'db_mindex' busy; no LDAP update for \"%s\"",
else
"%s: 'db_mindex' lock error %d; no LDAP update for \"%s\"",
} else {
"%s: 'db_table' busy; no LDAP update for \"%s\"",
else
"%s: 'db_table' lock error %d; no LDAP update for \"%s\"",
}
if (arg->isDeferred) {
if (dstat != DB_SUCCESS) {
"%s: DB error %d rolling back \"%s\"",
/*
* Had rollback succeeded, the 'table'
* would have disappeared. However, since
* rollback failed, we need to update the
* table->mapping.enum* fields.
*/
}
}
if (stat == 0) {
if (stat2 == 0) {
"db_mindex::entriesFromLDAPreal wu",
"db_mindex::entriesFromLDAPreal wu db");
} else {
"db_mindex::entriesFromLDAPreal wu db");
}
}
return (LDAP_OPERATIONS_ERROR);
}
stat = LDAP_SUCCESS;
mindex->setNoLDAPquery();
if (qin == 0) {
table->setEnumMode(0);
/*
* If there is no non-indexed table mapping, we must filter
* the enum mode (i.e., deletion candidates) array to only
* contain those entries that match the indexes.
*/
if (haveIndexedMapping(t)) {
/*
* Walk through the entry array, and remove any enum
* array entry that _doesn't_ match the index(es).
*/
for (i = 0; i < ntea; i++) {
db_query *q;
int numMatches;
if (tea[i] == 0)
continue;
q = pseudoEntryObj2Query(tea[i], 0, 0);
if (q == 0)
continue;
&numMatches);
if (tp == 0 || numMatches <= 0)
freeQuery(q);
}
}
} else {
long i, count;
/*
* Find the entries in the DB that currently match the
* query, and add them to the enum array. Those that
* remain untouched when we've processed the LDAP data
* don't currently exist in LDAP, and should be deleted
* from the DB.
*/
for (i = 0; i < count; i++) {
if (dbie == 0)
break;
}
} else {
}
}
entry_object eo, *e;
nis_object o, *to;
int j, nc;
if (res[i] == 0)
continue;
#ifdef NISDB_LDAP_DEBUG
printQuery(res[i], t);
printObjAttr(oa[i]);
#endif /* NISDB_LDAP_DEBUG */
/* Assemble an object from the query and attributes */
(void) memset(&o, 0, sizeof (o));
if (oa[i] != 0) {
}
!= t->numColumns)
tc = 0;
if (o.zo_owner == 0)
if (o.zo_group == 0)
if (o.zo_domain == 0)
if (o.zo_access == 0)
if (o.zo_ttl == 0)
} else {
tc = 0;
o.zo_owner = "";
o.zo_group = "";
o.zo_domain = "";
}
t->numColumns + 1;
if (qc == 0) {
continue;
}
for (j = 0; j < nc; j++) {
continue;
#ifdef SET_ENTRY_FLAGS
if (tc != 0)
#else
/*
* In theory, the entry flags should be derived
* from the table flags. However, that doesn't
* seem to be the way that the DB code has done
* things so far, so leave the entry flags unset.
*/
#endif /* SET_ENTRY_FLAGS */
}
setOid(&o);
if (e == 0) {
continue;
}
/*
* 'o' is currently a pseudo-object of type entry, with
* column zero used for an XDR:ed version of the entry_obj,
* column one the real column zero of the entry, etc.
* We now need a real NIS_ENTRY_OBJ object, so move the
* entry_col array one step left.
*/
/*
* LDAP_SUCCESS => Entry added or modified
* LDAP_COMPARE_TRUE => Entry same as existing one
* other => Error
*/
if (stat == LDAP_SUCCESS) {
na++;
} else if (stat == LDAP_COMPARE_TRUE) {
stat = LDAP_SUCCESS;
} else {
"%s: Error adding entry to \"%s\": %s",
}
if (e->en_cols.en_cols_val != 0)
}
/* Take care of deletes if we enumerated the table */
if (doEnum) {
} else
ea = 0;
if (ea != 0) {
for (i = 0; i < numEa; i++) {
int st;
if (ea[i] == 0)
continue;
if (st == LDAP_SUCCESS) {
na++;
} else {
"%s: Error removing directory entry for \"%s\": %s",
if (stat == LDAP_SUCCESS)
}
}
if (stat == LDAP_SUCCESS) {
(void) gettimeofday(&now, 0);
}
if (doEnum)
}
/* If we called log_action() successfully, we need to sync the log */
if (na > 0)
else if (xid != 0)
else
ret = 0;
if (ret != 0) {
"%s: Error %s transaction for \"%s\"",
"ending" : "aborting",
}
#ifdef NISDB_LDAP_DEBUG
printbuf();
#endif /* NISDB_LDAP_DEBUG */
if (doEnum)
if (arg->isDeferred) {
/*
* Rollback doesn't recover data written to disk, so
* we should commit even if we're returning failure.
*/
if (dstat != DB_SUCCESS) {
"%s: DB error %d committing \"%s\"",
}
}
{
(void) gettimeofday(&end, 0);
}
#ifdef NISDB_LDAP_DEBUG
#else
#endif /* NISDB_LDAP_DEBUG */
}
"db db_mindex::entriesFromLDAPreal wu");
if (stat3 != 0)
if (stat2 != 0)
if (lstat != 0)
return (stat);
}
/*
* Sets the oid (i.e., the creation and modification times) for the
* specified object. In order to avoid retrieving the old incarnation
* (if any) from the DB first, we're punting and setting both mtime
* and ctime to the current time.
*/
static void
if (obj != 0) {
}
}