/*
* 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 <sys/ccompile.h>
#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 {
/*
* 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.
*/
{
(void) rw_rdlock(&getent_ctxDB_rwlock);
(const char *)ptrstr, NSCD_GET_FIRST_DB_ENTRY, 0);
(me, "getent context %p, cookie# %lld, to_delete %d\n",
/*
* If the ctx is not to be deleted and the cookie numbers
* match, return the ctx if not aborted and not in use.
* Otherwise return NULL.
*/
else
}
}
(void) rw_unlock(&getent_ctxDB_rwlock);
return (ret);
}
int
{
int in_use;
(me, "in_use = %d, ctx->thr_id = %d, thread id = %d\n",
in_use = 0;
return (in_use);
}
/*
* FUNCTION: _nscd_free_ctx_if_aborted
*
* Check to see if the getent session associated with a getent context had
* been aborted. If so, return the getent context back to the pool.
*/
void
{
int aborted;
return;
}
if (aborted == 1) {
(me, "getent session aborted, return the getent context\n");
}
}
/*
* FUNCTION: _nscd_del_getent_ctx
*
* Delete a getent context from the internal getent context database.
*/
static void
{
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
{
/* remove reference to the getent context base */
}
}
static void
{
return;
while (c != NULL) {
c = tc;
}
}
void
{
int i;
(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 *
{
return (NULL);
else {
}
return (NULL);
}
/* this is a nsw_state used for getent processing */
return (gnctx);
}
{
(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 limit is reached return an error
* so that the client can perform the enumeration.
*/
(nscd_acc_data_t *)base);
/* run out of contexts */
(me, "run out of getent ctxs\n");
return (NSCD_CREATE_GETENT_CTX_FAILED);
} else {
base->num_getent_ctx++;
else {
/* not able to create a getent ctx */
(me, "create getent ctx failed\n");
return (NSCD_CREATE_GETENT_CTX_FAILED);
}
}
}
c->seq_num = 1;
c->cookie_num = _nscd_get_cookie_num();
c->in_use = 1;
(me, "got a getent ctx %p\n", c);
/*
* reference count the getent context base bfore handing out
* the getent context
*/
c->base = (nscd_getent_ctx_base_t *)
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
{
/* if context base is gone or no longer current, free this context */
return;
}
} else
/* put back the db state */
/* this nsw_state is no longer used for getent processing */
}
(me, "ctx (%p, cookie # = %lld) removed from getent ctx DB\n",
gnctx->cookie_num = 0;
/* remove reference to the getent context base */
}
int dbi,
int lock)
{
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;
(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 * __NORETURN
{
/*CONSTCOND*/
while (1) {
(void) sleep(60);
(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",
/* add to list if not in already */
c = c->next_to_reclaim) {
if (gctx == c)
break;
}
if (c == NULL) {
}
} else {
}
}
}
(void) rw_unlock(&getent_ctxDB_rwlock);
/*
* return all the orphan getent contexts to the pool if not
* in use
*/
c = gctx->next_to_reclaim;
if (num_reclaim_check > 1)
if (in_use == 0) {
(me, "process %d exited, "
"freeing getent context = %p\n",
}
gctx = c;
}
}
/*NOTREACHED*/
/*LINTED E_FUNC_HAS_NO_RETURN_STMT*/
}
static nscd_rc_t
int errnum;
(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);
}