summaryrefslogtreecommitdiff
path: root/usr/src/cmd/nscd/nscd_getentctx.c
blob: 2fed9a5c82b24b4b512d1eb8ee1d5a71111b0160 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
/*
 * 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
 * or http://www.opensolaris.org/os/licensing.
 * 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 mutex_t		getent_monitor_mutex = DEFAULTMUTEX;
static int		getent_monitor_started = 0;

static rwlock_t		getent_ctxDB_rwlock = DEFAULTRWLOCK;
static nscd_db_t	*getent_ctxDB = NULL;

/*
 * internal structure representing a nscd getent context
 */
typedef struct nscd_getent_ctx {
	int			to_delete; /* this ctx no longer valid */
	nscd_getent_context_t	*ptr;
	nscd_cookie_num_t	cookie_num;
} nscd_getent_ctx_t;

/*
 * nscd_getent_context_t list for each nss database. Protected
 * by the readers/writer lock nscd_getent_ctx_lock.
 */
nscd_getent_ctx_base_t **nscd_getent_ctx_base;
static rwlock_t nscd_getent_ctx_base_lock = DEFAULTRWLOCK;

extern nscd_db_entry_t *_nscd_walk_db(nscd_db_t *db, void **cookie);

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.
 */
nscd_db_t *
_nscd_create_getent_ctxDB()
{

	nscd_db_t	*ret;

	(void) rw_wrlock(&getent_ctxDB_rwlock);

	if (getent_ctxDB != NULL) {
		(void) rw_unlock(&getent_ctxDB_rwlock);
		return (getent_ctxDB);
	}

	ret = _nscd_alloc_db(NSCD_DB_SIZE_LARGE);

	if (ret != NULL)
		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
_nscd_add_getent_ctx(
	nscd_getent_context_t	*ptr,
	nscd_cookie_num_t	cookie_num)
{
	int			size;
	char			buf[32];
	nscd_db_entry_t		*db_entry;
	nscd_getent_ctx_t	*gnctx;

	if (ptr == NULL)
		return (NSCD_INVALID_ARGUMENT);

	(void) snprintf(buf, sizeof (buf), "%lld", cookie_num);

	size = sizeof (*gnctx);

	db_entry = _nscd_alloc_db_entry(NSCD_DATA_CTX_ADDR,
	    (const char *)buf, size, 1, 1);
	if (db_entry == NULL)
		return (NSCD_NO_MEMORY);

	gnctx = (nscd_getent_ctx_t *)*(db_entry->data_array);
	gnctx->ptr = ptr;
	gnctx->cookie_num = cookie_num;

	(void) rw_wrlock(&getent_ctxDB_rwlock);
	(void) _nscd_add_db_entry(getent_ctxDB, buf, db_entry,
	    NSCD_ADD_DB_ENTRY_FIRST);
	(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.
 */
nscd_getent_context_t *
_nscd_is_getent_ctx(
	nscd_cookie_num_t	cookie_num)
{
	char			ptrstr[32];
	const nscd_db_entry_t	*db_entry;
	nscd_getent_context_t	*ret = NULL;
	char			*me = "_nscd_is_getent_ctx";

	(void) snprintf(ptrstr, sizeof (ptrstr), "%lld", cookie_num);

	(void) rw_rdlock(&getent_ctxDB_rwlock);

	db_entry = _nscd_get_db_entry(getent_ctxDB, NSCD_DATA_CTX_ADDR,
	    (const char *)ptrstr, NSCD_GET_FIRST_DB_ENTRY, 0);

	if (db_entry != NULL) {
		nscd_getent_ctx_t *gnctx;

		gnctx = (nscd_getent_ctx_t *)*(db_entry->data_array);
		_NSCD_LOG(NSCD_LOG_GETENT_CTX, NSCD_LOG_LEVEL_DEBUG)
		(me, "getent context %p, cookie# %lld, to_delete %d\n",
		    gnctx->ptr, gnctx->cookie_num, gnctx->to_delete);

		/*
		 * 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.
		 */
		if (gnctx->to_delete == 0 && gnctx->cookie_num == cookie_num) {
			ret = gnctx->ptr;
			(void) mutex_lock(&gnctx->ptr->getent_mutex);
			if (ret->aborted == 1 || ret->in_use == 1)
				ret = NULL;
			else
				ret->in_use = 1;
			(void) mutex_unlock(&gnctx->ptr->getent_mutex);
		}
	}

	(void) rw_unlock(&getent_ctxDB_rwlock);

	return (ret);
}

int
_nscd_is_getent_ctx_in_use(
	nscd_getent_context_t	*ctx)
{
	int	in_use;
	char	*me = "_nscd_getent_ctx_in_use";

	(void) mutex_lock(&ctx->getent_mutex);

	_NSCD_LOG(NSCD_LOG_GETENT_CTX, NSCD_LOG_LEVEL_DEBUG)
	(me, "in_use = %d, ctx->thr_id = %d, thread id = %d\n",
	    ctx->in_use, ctx->thr_id, thr_self());

	in_use = ctx->in_use;
	if (in_use == 1 && ctx->thr_id == thr_self())
		in_use = 0;
	(void) mutex_unlock(&ctx->getent_mutex);
	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
_nscd_free_ctx_if_aborted(
	nscd_getent_context_t	*ctx)
{
	int	aborted;
	char	*me = "_nscd_free_ctx_if_aborted";

	(void) mutex_lock(&ctx->getent_mutex);

	_NSCD_LOG(NSCD_LOG_GETENT_CTX, NSCD_LOG_LEVEL_DEBUG)
	(me, "in_use = %d, aborted = %d\n", ctx->in_use, ctx->aborted);

	if (ctx->in_use != 1) {
		(void) mutex_unlock(&ctx->getent_mutex);
		return;
	}
	aborted = ctx->aborted;
	ctx->in_use = 0;
	(void) mutex_unlock(&ctx->getent_mutex);

	if (aborted == 1) {
		_NSCD_LOG(NSCD_LOG_GETENT_CTX, NSCD_LOG_LEVEL_DEBUG)
		(me, "getent session aborted, return the getent context\n");
		_nscd_put_getent_ctx(ctx);
	}
}

/*
 * FUNCTION: _nscd_del_getent_ctx
 *
 * Delete a getent context from the internal getent context database.
 */
static void
_nscd_del_getent_ctx(
	nscd_getent_context_t	*ptr,
	nscd_cookie_num_t	cookie_num)
{
	char			ptrstr[32];
	nscd_getent_ctx_t	*gnctx;
	const nscd_db_entry_t	*db_entry;

	if (ptr == NULL)
		return;

	(void) snprintf(ptrstr, sizeof (ptrstr), "%lld", cookie_num);

	(void) rw_rdlock(&getent_ctxDB_rwlock);
	/*
	 * first find the db entry and make sure the
	 * sequence number matched, then delete it from
	 * the database.
	 */
	db_entry = _nscd_get_db_entry(getent_ctxDB,
	    NSCD_DATA_CTX_ADDR,
	    (const char *)ptrstr,
	    NSCD_GET_FIRST_DB_ENTRY, 0);
	if (db_entry != NULL) {
		gnctx = (nscd_getent_ctx_t *)*(db_entry->data_array);
		if (gnctx->ptr == ptr && gnctx->cookie_num  == cookie_num) {

			(void) rw_unlock(&getent_ctxDB_rwlock);
			(void) rw_wrlock(&getent_ctxDB_rwlock);

			(void) _nscd_delete_db_entry(getent_ctxDB,
			    NSCD_DATA_CTX_ADDR,
			    (const char *)ptrstr,
			    NSCD_DEL_FIRST_DB_ENTRY, 0);
		}
	}
	(void) rw_unlock(&getent_ctxDB_rwlock);
}

static void
_nscd_free_getent_ctx(
	nscd_getent_context_t	*gnctx)
{

	char			*me = "_nscd_free_getent_ctx";

	_NSCD_LOG(NSCD_LOG_GETENT_CTX, NSCD_LOG_LEVEL_DEBUG)
	(me, "getent context %p\n", gnctx);

	_nscd_put_nsw_state(gnctx->nsw_state);

	if (gnctx->base != NULL) {
		/* remove reference to the getent context base */
		_nscd_release((nscd_acc_data_t *)gnctx->base);
		gnctx->base = NULL;
	}

	_nscd_del_getent_ctx(gnctx, gnctx->cookie_num);
	free(gnctx);
}


static void
_nscd_free_getent_ctx_base(
	nscd_acc_data_t		*data)
{
	nscd_getent_ctx_base_t	*base = (nscd_getent_ctx_base_t *)data;
	nscd_getent_context_t	*c, *tc;
	char			*me = "_nscd_free_getent_ctx_base";

	_NSCD_LOG(NSCD_LOG_GETENT_CTX | NSCD_LOG_CONFIG, NSCD_LOG_LEVEL_DEBUG)
	(me, "getent context base %p\n", base);

	if (base == NULL)
		return;

	c = base->first;
	while (c != NULL) {
		tc = c->next;
		_nscd_free_getent_ctx(c);
		c = tc;
	}
}

void
_nscd_free_all_getent_ctx_base()
{
	nscd_getent_ctx_base_t	*base;
	int			i;
	char			*me = "_nscd_free_all_getent_ctx_base";

	_NSCD_LOG(NSCD_LOG_GETENT_CTX | NSCD_LOG_CONFIG, NSCD_LOG_LEVEL_DEBUG)
	(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];
		if (base == NULL)
			continue;

		nscd_getent_ctx_base[i] = (nscd_getent_ctx_base_t *)
		    _nscd_set((nscd_acc_data_t *)base, NULL);
	}
	(void) rw_unlock(&nscd_getent_ctx_base_lock);
}

static nscd_getent_context_t *
_nscd_create_getent_ctx(
	nscd_nsw_params_t	*params)
{
	nscd_getent_context_t	*gnctx;
	nss_db_root_t		db_root;
	char			*me = "_nscd_create_getent_ctx";

	gnctx = calloc(1, sizeof (nscd_getent_context_t));
	if (gnctx == NULL)
		return (NULL);
	else {
		_NSCD_LOG(NSCD_LOG_GETENT_CTX, NSCD_LOG_LEVEL_DEBUG)
		(me, "getent context allocated %p\n", gnctx);
	}

	gnctx->dbi = params->dbi;
	gnctx->cookie_num = _nscd_get_cookie_num();
	gnctx->pid = -1;
	(void) mutex_init(&gnctx->getent_mutex, USYNC_THREAD, NULL);

	if (_nscd_get_nsw_state(&db_root, params) != NSCD_SUCCESS) {
		free(gnctx);
		return (NULL);
	}
	gnctx->nsw_state = (nscd_nsw_state_t *)db_root.s;
	/* this is a nsw_state used for getent processing */
	gnctx->nsw_state->getent = 1;

	_NSCD_LOG(NSCD_LOG_GETENT_CTX, NSCD_LOG_LEVEL_DEBUG)
	(me, "got nsw_state %p\n", gnctx->nsw_state);

	return (gnctx);
}


nscd_rc_t
_nscd_get_getent_ctx(
	nss_getent_t		*contextpp,
	nscd_nsw_params_t	*params)
{

	nscd_getent_context_t	*c;
	nscd_getent_ctx_base_t	*base, *tmp;
	nscd_rc_t		rc;
	char			*me = "_nscd_get_getent_ctx";

	_NSCD_LOG(NSCD_LOG_GETENT_CTX, NSCD_LOG_LEVEL_DEBUG)
	(me, "entering ...\n");

	(void) rw_rdlock(&nscd_getent_ctx_base_lock);
	base = nscd_getent_ctx_base[params->dbi];
	(void) rw_unlock(&nscd_getent_ctx_base_lock);
	assert(base != NULL);

	/*
	 * 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.
	 */
	tmp = (nscd_getent_ctx_base_t *)_nscd_mutex_lock(
	    (nscd_acc_data_t *)base);
	assert(base == tmp);
	if (base->first == NULL) {
		if (base->num_getent_ctx >= base->max_getent_ctx) {
			/* run out of contexts */

			_NSCD_LOG(NSCD_LOG_GETENT_CTX,
			    NSCD_LOG_LEVEL_DEBUG)
			(me, "run out of getent ctxs\n");

			_nscd_mutex_unlock((nscd_acc_data_t *)base);
			return (NSCD_CREATE_GETENT_CTX_FAILED);
		} else {
			base->first = _nscd_create_getent_ctx(params);
			if (base->first != NULL)
				base->num_getent_ctx++;
			else {
				/* not able to create a getent ctx */

				_NSCD_LOG(NSCD_LOG_GETENT_CTX,
				    NSCD_LOG_LEVEL_ERROR)
				(me, "create getent ctx failed\n");

				_nscd_mutex_unlock((nscd_acc_data_t *)base);
				return (NSCD_CREATE_GETENT_CTX_FAILED);
			}

			_NSCD_LOG(NSCD_LOG_GETENT_CTX, NSCD_LOG_LEVEL_DEBUG)
			(me, "got a new getent ctx %p\n", base->first);
		}
	}

	assert(base->first != NULL);

	c = base->first;
	base->first = c->next;
	c->next = NULL;
	c->seq_num = 1;
	c->cookie_num = _nscd_get_cookie_num();
	c->in_use = 1;
	c->thr_id = thr_self();

	_NSCD_LOG(NSCD_LOG_GETENT_CTX, NSCD_LOG_LEVEL_DEBUG)
	(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 *)
	    _nscd_get((nscd_acc_data_t *)base);

	_nscd_mutex_unlock((nscd_acc_data_t *)base);

	_NSCD_LOG(NSCD_LOG_GETENT_CTX, NSCD_LOG_LEVEL_DEBUG)
	(me, "adding new ctx %p, cookie # = %lld\n", c, c->cookie_num);

	if ((rc = _nscd_add_getent_ctx(c, c->cookie_num)) != NSCD_SUCCESS) {
		_nscd_put_getent_ctx(c);
		return (rc);
	}
	contextpp->ctx = (struct nss_getent_context *)c;

	/* start monitor and reclaim orphan getent context */
	if (getent_monitor_started == 0) {
		(void) mutex_lock(&getent_monitor_mutex);
		if (getent_monitor_started == 0) {
			getent_monitor_started = 1;
			(void) _nscd_init_getent_ctx_monitor();
		}
		(void) mutex_unlock(&getent_monitor_mutex);
	}

	return (NSCD_SUCCESS);
}

void
_nscd_put_getent_ctx(
	nscd_getent_context_t	*gnctx)
{

	nscd_getent_ctx_base_t	*base;
	char			*me = "_nscd_put_getent_ctx";

	base = gnctx->base;

	/* if context base is gone or no longer current, free this context */
	if ((_nscd_mutex_lock((nscd_acc_data_t *)base)) == NULL) {
		_nscd_free_getent_ctx(gnctx);
		return;
	}

	if (base->first != NULL) {
		gnctx->next = base->first;
		base->first = gnctx;
	} else
		base->first = gnctx;

	/* put back the db state */
	_NSCD_LOG(NSCD_LOG_GETENT_CTX, NSCD_LOG_LEVEL_DEBUG)
	(me, "putting back nsw state %p\n", gnctx->nsw_state);

	/* this nsw_state is no longer used for getent processing */
	if (gnctx->nsw_state != NULL) {
		gnctx->nsw_state->getent = 0;
		_nscd_put_nsw_state(gnctx->nsw_state);
		gnctx->nsw_state = NULL;
	}

	gnctx->aborted = 0;
	gnctx->in_use = 0;
	gnctx->thr_id = (thread_t)-1;
	_nscd_del_getent_ctx(gnctx, gnctx->cookie_num);

	_NSCD_LOG(NSCD_LOG_GETENT_CTX, NSCD_LOG_LEVEL_DEBUG)
	(me, "ctx (%p, cookie # = %lld) removed from getent ctx DB\n",
	    gnctx, gnctx->cookie_num);

	gnctx->seq_num = 0;
	gnctx->cookie_num = 0;
	gnctx->pid = -1;
	gnctx->thr_id = (thread_t)-1;
	gnctx->n_src = 0;
	gnctx->be = NULL;

	/* remove reference to the getent context base */
	_nscd_release((nscd_acc_data_t *)base);
	gnctx->base = NULL;

	_nscd_mutex_unlock((nscd_acc_data_t *)base);
}

nscd_rc_t
_nscd_init_getent_ctx_base(
	int			dbi,
	int			lock)
{
	nscd_getent_ctx_base_t	*base = NULL;
	char			*me = "_nscd_init_getent_ctx_base";

	if (lock)
		(void) rw_rdlock(&nscd_getent_ctx_base_lock);

	base = (nscd_getent_ctx_base_t *)_nscd_alloc(
	    NSCD_DATA_GETENT_CTX_BASE,
	    sizeof (nscd_getent_ctx_base_t),
	    _nscd_free_getent_ctx_base,
	    NSCD_ALLOC_MUTEX | NSCD_ALLOC_COND);

	if (base == NULL) {
		if (lock)
			(void) rw_unlock(&nscd_getent_ctx_base_lock);
		return (NSCD_NO_MEMORY);
	}
	_NSCD_LOG(NSCD_LOG_GETENT_CTX | NSCD_LOG_CONFIG, NSCD_LOG_LEVEL_DEBUG)
	(me, "base %p allocated\n", base);

	/*
	 * initialize and activate the new getent_ctx base
	 */
	base->dbi = dbi;
	base->max_getent_ctx = NSCD_SW_CFG(dbi).max_getent_ctx_per_db;
	nscd_getent_ctx_base[dbi] =
	    (nscd_getent_ctx_base_t *)_nscd_set(
	    (nscd_acc_data_t *)nscd_getent_ctx_base[dbi],
	    (nscd_acc_data_t *)base);

	if (lock)
		(void) rw_unlock(&nscd_getent_ctx_base_lock);

	return (NSCD_SUCCESS);
}

nscd_rc_t
_nscd_init_all_getent_ctx_base()
{
	int			i;
	nscd_rc_t		rc;
	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);
		}
	}

	_NSCD_LOG(NSCD_LOG_GETENT_CTX | NSCD_LOG_CONFIG, NSCD_LOG_LEVEL_DEBUG)
	(me, "all getent context base initialized\n");

	(void) rw_unlock(&nscd_getent_ctx_base_lock);

	return (NSCD_SUCCESS);
}
nscd_rc_t
_nscd_alloc_getent_ctx_base()
{

	(void) rw_wrlock(&nscd_getent_ctx_base_lock);

	nscd_getent_ctx_base = calloc(NSCD_NUM_DB,
	    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
process_exited(pid_t pid)
{
	char	pname[PATH_MAX];
	int	fd;

	(void) snprintf(pname, sizeof (pname), "/proc/%d/psinfo", pid);
	if ((fd = open(pname, O_RDONLY)) == -1)
		return (1);
	else {
		(void) close(fd);
		return (0);
	}
}

/*
 * FUNCTION: reclaim_getent_ctx
 */
/*ARGSUSED*/
static void * __NORETURN
reclaim_getent_ctx(void *arg)
{
	void			*cookie = NULL;
	nscd_db_entry_t		*ep;
	nscd_getent_ctx_t	*ctx;
	nscd_getent_context_t	*gctx, *c;
	nscd_getent_context_t	*first = NULL, *last = NULL;
	nss_getent_t		nssctx = { 0 };
	char			*me = "reclaim_getent_ctx";

	/*CONSTCOND*/
	while (1) {

		(void) sleep(60);

		(void) rw_rdlock(&getent_ctxDB_rwlock);

		for (ep = _nscd_walk_db(getent_ctxDB, &cookie); ep != NULL;
		    ep = _nscd_walk_db(getent_ctxDB, &cookie)) {

			ctx = (nscd_getent_ctx_t *)*(ep->data_array);

			gctx = ctx->ptr;

			/*
			 * if the client process, which did the setent,
			 * exited, add the context to the orphan list
			 */
			if (gctx->pid != -1 && process_exited(gctx->pid)) {

				_NSCD_LOG(NSCD_LOG_GETENT_CTX,
				    NSCD_LOG_LEVEL_DEBUG)
				(me, "process  %d exited, "
				    "getent context = %p, "
				    "db index = %d, cookie # = %lld, "
				    "sequence # = %lld\n",
				    gctx->pid, gctx, gctx->dbi,
				    gctx->cookie_num, gctx->seq_num);

				if (first != NULL) {
					/* add to list if not in already */
					for (c = first; c != NULL;
					    c = c->next_to_reclaim) {
						if (gctx == c)
							break;
					}
					if (c == NULL) {
						last->next_to_reclaim = gctx;
						last = gctx;
					}
				} else {
					first = gctx;
					last = gctx;
				}
			}
		}

		(void) rw_unlock(&getent_ctxDB_rwlock);


		/*
		 * return all the orphan getent contexts to the pool if not
		 * in use
		 */
		for (gctx = first; gctx; ) {
			int in_use, num_reclaim_check;

			c = gctx->next_to_reclaim;
			gctx->next_to_reclaim = NULL;
			gctx->aborted = 1;

			(void) mutex_lock(&gctx->getent_mutex);
			num_reclaim_check = gctx->num_reclaim_check++;
			if (num_reclaim_check > 1)
				gctx->in_use = 0;
			in_use = gctx->in_use;
			(void) mutex_unlock(&gctx->getent_mutex);

			if (in_use == 0) {
				_NSCD_LOG(NSCD_LOG_GETENT_CTX,
				    NSCD_LOG_LEVEL_DEBUG)
				(me, "process  %d exited, "
				    "freeing getent context = %p\n",
				    gctx->pid, gctx);
				nssctx.ctx = (struct nss_getent_context *)gctx;
				nss_endent(NULL, NULL, &nssctx);
			}
			gctx = c;
		}
		first = last = NULL;
	}
	/*NOTREACHED*/
	/*LINTED E_FUNC_HAS_NO_RETURN_STMT*/
}

static nscd_rc_t
_nscd_init_getent_ctx_monitor() {

	int	errnum;
	char	*me = "_nscd_init_getent_ctx_monitor";

	_NSCD_LOG(NSCD_LOG_GETENT_CTX, NSCD_LOG_LEVEL_DEBUG)
	(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
	 */
	if (thr_create(NULL, NULL, reclaim_getent_ctx,
		NULL, THR_DETACHED, NULL) != 0) {
		errnum = errno;
		_NSCD_LOG(NSCD_LOG_GETENT_CTX, NSCD_LOG_LEVEL_ERROR)
		(me, "thr_create: %s\n", strerror(errnum));
		return (NSCD_THREAD_CREATE_ERROR);
	}

	return (NSCD_SUCCESS);
}