nscd_getentctx.c revision cb5caa98562cf06753163f558cbcfe30b8f4673a
/*
* 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 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include "nscd_db.h"
#include "nscd_log.h"
#include "nscd_switch.h"
#include "nscd_door.h"
extern int _whoami;
static int getent_monitor_started = 0;
/*
* internal structure representing a nscd getent context
*/
typedef struct nscd_getent_ctx {
int to_delete; /* this ctx no longer valid */
/*
* nscd_getent_context_t list for each nss database. Protected
*/
static nscd_rc_t _nscd_init_getent_ctx_monitor();
/*
* FUNCTION: _nscd_create_getent_ctxDB
*
* Create the internal getent context database to keep track of the
* getent contexts currently being used.
*/
{
(void) rw_wrlock(&getent_ctxDB_rwlock);
if (getent_ctxDB != NULL) {
(void) rw_unlock(&getent_ctxDB_rwlock);
return (getent_ctxDB);
}
getent_ctxDB = ret;
(void) rw_unlock(&getent_ctxDB_rwlock);
return (ret);
}
/*
* FUNCTION: _nscd_add_getent_ctx
*
* Add a getent context to the internal context database.
*/
static nscd_rc_t
{
int size;
return (NSCD_INVALID_ARGUMENT);
return (NSCD_NO_MEMORY);
(void) rw_wrlock(&getent_ctxDB_rwlock);
(void) rw_unlock(&getent_ctxDB_rwlock);
return (NSCD_SUCCESS);
}
/*
* FUNCTION: _nscd_is_getent_ctx
*
* Check to see if a getent context can be found in the internal
* getent context database.
*/
{
const nscd_db_entry_t *db_entry;
(void) rw_rdlock(&getent_ctxDB_rwlock);
(const char *)ptrstr, NSCD_GET_FIRST_DB_ENTRY, 0);
/*
* If the ctx is not to be deleted and
* the cookie numbers match, return the ctx.
* Otherwise return NULL.
*/
}
(void) rw_unlock(&getent_ctxDB_rwlock);
return (ret);
}
/*
* FUNCTION: _nscd_del_getent_ctx
*
* Delete a getent context from the internal getent context database.
*/
static void
{
const nscd_db_entry_t *db_entry;
return;
(void) rw_rdlock(&getent_ctxDB_rwlock);
/*
* first find the db entry and make sure the
* sequence number matched, then delete it from
* the database.
*/
(const char *)ptrstr,
(void) rw_unlock(&getent_ctxDB_rwlock);
(void) rw_wrlock(&getent_ctxDB_rwlock);
(void) _nscd_delete_db_entry(getent_ctxDB,
(const char *)ptrstr,
}
}
(void) rw_unlock(&getent_ctxDB_rwlock);
}
static void
{
char *me = "_nscd_free_getent_ctx";
}
static void
{
nscd_getent_context_t *c, *tc;
char *me = "_nscd_free_getent_ctx_base";
return;
while (c != NULL) {
c = tc;
}
}
void
{
int i;
char *me = "_nscd_free_all_getent_ctx_base";
(me, "entering ..\n");
(void) rw_wrlock(&nscd_getent_ctx_base_lock);
for (i = 0; i < NSCD_NUM_DB; i++) {
base = nscd_getent_ctx_base[i];
continue;
}
(void) rw_unlock(&nscd_getent_ctx_base_lock);
}
static nscd_getent_context_t *
{
char *me = "_nscd_create_getent_ctx";
return (NULL);
else {
}
return (NULL);
}
/* this is a nsw_state used for getent processing */
return (gnctx);
}
{
char *me = "_nscd_get_getent_ctx";
(me, "entering ...\n");
(void) rw_rdlock(&nscd_getent_ctx_base_lock);
(void) rw_unlock(&nscd_getent_ctx_base_lock);
/*
* If the context list is not empty, return the first one
* on the list. Otherwise, create and return a new one if
* limit is not reached. if reacehed, wait for the 'one is
* available' signal.
*/
(nscd_acc_data_t *)base);
base->num_waiter++;
(me, "waiting for signal\n");
(me, "woke up\n");
}
base->num_waiter--;
} else {
base->num_getent_ctx++;
} else {
/* not able to create an getent ctx */
(me, "create getent ctx failed\n");
return (NSCD_CREATE_GETENT_CTX_FAILED);
}
}
}
c->seq_num = 1;
(me, "got a getent ctx %p\n", c);
return (rc);
}
/* start monitor and reclaim orphan getent context */
if (getent_monitor_started == 0) {
(void) mutex_lock(&getent_monitor_mutex);
if (getent_monitor_started == 0) {
(void) _nscd_init_getent_ctx_monitor();
}
(void) mutex_unlock(&getent_monitor_mutex);
}
return (NSCD_SUCCESS);
}
void
{
char *me = "_nscd_put_getent_ctx";
/* if context base is gone, so should this context */
return;
}
} else
/* put back the db state */
/* this nsw_state is no longer used for getent processing */
(me, "ctx (%p seq# = %lld) removed from getent ctx DB\n",
if (base->num_waiter > 0) {
}
}
int dbi,
int lock)
{
char *me = "_nscd_init_getent_ctx_base";
if (lock)
(void) rw_rdlock(&nscd_getent_ctx_base_lock);
sizeof (nscd_getent_ctx_base_t),
if (lock)
(void) rw_unlock(&nscd_getent_ctx_base_lock);
return (NSCD_NO_MEMORY);
}
/*
* initialize and activate the new getent_ctx base
*/
(nscd_acc_data_t *)base);
if (lock)
(void) rw_unlock(&nscd_getent_ctx_base_lock);
return (NSCD_SUCCESS);
}
{
int i;
char *me = "_nscd_init_all_getent_ctx_base";
(void) rw_wrlock(&nscd_getent_ctx_base_lock);
for (i = 0; i < NSCD_NUM_DB; i++) {
rc = _nscd_init_getent_ctx_base(i, 0);
if (rc != NSCD_SUCCESS) {
(void) rw_unlock(&nscd_getent_ctx_base_lock);
return (rc);
}
}
(me, "all getent context base initialized\n");
(void) rw_unlock(&nscd_getent_ctx_base_lock);
return (NSCD_SUCCESS);
}
{
(void) rw_wrlock(&nscd_getent_ctx_base_lock);
sizeof (nscd_getent_ctx_base_t *));
if (nscd_getent_ctx_base == NULL) {
(void) rw_unlock(&nscd_getent_ctx_base_lock);
return (NSCD_NO_MEMORY);
}
(void) rw_unlock(&nscd_getent_ctx_base_lock);
return (NSCD_SUCCESS);
}
static int
{
int fd;
return (1);
else {
return (0);
}
}
/*
* FUNCTION: reclaim_getent_ctx
*/
/*ARGSUSED*/
static void *
reclaim_getent_ctx(void *arg)
{
nscd_getent_context_t *gctx, *c;
char *me = "reclaim_getent_ctx";
/*CONSTCOND*/
while (1) {
(void) rw_rdlock(&getent_ctxDB_rwlock);
/*
* if the client process, which did the setent,
* exited, add the context to the orphan list
*/
(me, "process %d exited, "
"getent context = %p, "
"db index = %d, cookie = %lld, "
"sequence # = %lld\n",
} else {
}
}
}
(void) rw_unlock(&getent_ctxDB_rwlock);
/*
* return all the orphan getent contexts to the pool
*/
gctx = c;
}
(void) sleep(60);
}
/*NOTREACHED*/
/*LINTED E_FUNC_HAS_NO_RETURN_STMT*/
}
static nscd_rc_t
int errnum;
char *me = "_nscd_init_getent_ctx_monitor";
(me, "initializing the getent context monitor\n");
/*
* the forker nscd does not process getent requests
* so no need to monitor orphan getent contexts
*/
if (_whoami == NSCD_FORKER)
return (NSCD_SUCCESS);
/*
* start a thread to reclaim unused getent contexts
*/
return (NSCD_THREAD_CREATE_ERROR);
}
return (NSCD_SUCCESS);
}