xref: /titanic_50/usr/src/cmd/nscd/nscd_getentctx.c (revision 9660e5cb810c5efc22db0f1459461c1f1eec15b9)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <stdlib.h>
27 #include <assert.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <fcntl.h>
31 #include "nscd_db.h"
32 #include "nscd_log.h"
33 #include "nscd_switch.h"
34 #include "nscd_door.h"
35 
36 extern int		_whoami;
37 static mutex_t		getent_monitor_mutex = DEFAULTMUTEX;
38 static int		getent_monitor_started = 0;
39 
40 static rwlock_t		getent_ctxDB_rwlock = DEFAULTRWLOCK;
41 static nscd_db_t	*getent_ctxDB = NULL;
42 
43 /*
44  * internal structure representing a nscd getent context
45  */
46 typedef struct nscd_getent_ctx {
47 	int			to_delete; /* this ctx no longer valid */
48 	nscd_getent_context_t	*ptr;
49 	nscd_cookie_num_t	cookie_num;
50 } nscd_getent_ctx_t;
51 
52 /*
53  * nscd_getent_context_t list for each nss database. Protected
54  * by the readers/writer lock nscd_getent_ctx_lock.
55  */
56 nscd_getent_ctx_base_t **nscd_getent_ctx_base;
57 static rwlock_t nscd_getent_ctx_base_lock = DEFAULTRWLOCK;
58 
59 extern nscd_db_entry_t *_nscd_walk_db(nscd_db_t *db, void **cookie);
60 
61 static nscd_rc_t _nscd_init_getent_ctx_monitor();
62 
63 /*
64  * FUNCTION: _nscd_create_getent_ctxDB
65  *
66  * Create the internal getent context database to keep track of the
67  * getent contexts currently being used.
68  */
69 nscd_db_t *
70 _nscd_create_getent_ctxDB()
71 {
72 
73 	nscd_db_t	*ret;
74 
75 	(void) rw_wrlock(&getent_ctxDB_rwlock);
76 
77 	if (getent_ctxDB != NULL) {
78 		(void) rw_unlock(&getent_ctxDB_rwlock);
79 		return (getent_ctxDB);
80 	}
81 
82 	ret = _nscd_alloc_db(NSCD_DB_SIZE_LARGE);
83 
84 	if (ret != NULL)
85 		getent_ctxDB = ret;
86 
87 	(void) rw_unlock(&getent_ctxDB_rwlock);
88 
89 	return (ret);
90 }
91 
92 /*
93  * FUNCTION: _nscd_add_getent_ctx
94  *
95  * Add a getent context to the internal context database.
96  */
97 static nscd_rc_t
98 _nscd_add_getent_ctx(
99 	nscd_getent_context_t	*ptr,
100 	nscd_cookie_num_t	cookie_num)
101 {
102 	int			size;
103 	char			buf[32];
104 	nscd_db_entry_t		*db_entry;
105 	nscd_getent_ctx_t	*gnctx;
106 
107 	if (ptr == NULL)
108 		return (NSCD_INVALID_ARGUMENT);
109 
110 	(void) snprintf(buf, sizeof (buf), "%lld", cookie_num);
111 
112 	size = sizeof (*gnctx);
113 
114 	db_entry = _nscd_alloc_db_entry(NSCD_DATA_CTX_ADDR,
115 	    (const char *)buf, size, 1, 1);
116 	if (db_entry == NULL)
117 		return (NSCD_NO_MEMORY);
118 
119 	gnctx = (nscd_getent_ctx_t *)*(db_entry->data_array);
120 	gnctx->ptr = ptr;
121 	gnctx->cookie_num = cookie_num;
122 
123 	(void) rw_wrlock(&getent_ctxDB_rwlock);
124 	(void) _nscd_add_db_entry(getent_ctxDB, buf, db_entry,
125 	    NSCD_ADD_DB_ENTRY_FIRST);
126 	(void) rw_unlock(&getent_ctxDB_rwlock);
127 
128 	return (NSCD_SUCCESS);
129 }
130 
131 /*
132  * FUNCTION: _nscd_is_getent_ctx
133  *
134  * Check to see if a getent context can be found in the internal
135  * getent context database.
136  */
137 nscd_getent_context_t *
138 _nscd_is_getent_ctx(
139 	nscd_cookie_num_t	cookie_num)
140 {
141 	char			ptrstr[32];
142 	const nscd_db_entry_t	*db_entry;
143 	nscd_getent_context_t	*ret = NULL;
144 	char			*me = "_nscd_is_getent_ctx";
145 
146 	(void) snprintf(ptrstr, sizeof (ptrstr), "%lld", cookie_num);
147 
148 	(void) rw_rdlock(&getent_ctxDB_rwlock);
149 
150 	db_entry = _nscd_get_db_entry(getent_ctxDB, NSCD_DATA_CTX_ADDR,
151 	    (const char *)ptrstr, NSCD_GET_FIRST_DB_ENTRY, 0);
152 
153 	if (db_entry != NULL) {
154 		nscd_getent_ctx_t *gnctx;
155 
156 		gnctx = (nscd_getent_ctx_t *)*(db_entry->data_array);
157 		_NSCD_LOG(NSCD_LOG_GETENT_CTX, NSCD_LOG_LEVEL_DEBUG)
158 		(me, "getent context %p, cookie# %lld, to_delete %d\n",
159 		    gnctx->ptr, gnctx->cookie_num, gnctx->to_delete);
160 
161 		/*
162 		 * If the ctx is not to be deleted and the cookie numbers
163 		 * match, return the ctx if not aborted and not in use,
164 		 * Otherwise return NULL.
165 		 */
166 		if (gnctx->to_delete == 0 && gnctx->cookie_num == cookie_num) {
167 			ret = gnctx->ptr;
168 			(void) mutex_lock(&gnctx->ptr->getent_mutex);
169 			if (ret->aborted == 1 || ret->in_use == 1)
170 				ret = NULL;
171 			else
172 				ret->in_use = 1;
173 			(void) mutex_unlock(&gnctx->ptr->getent_mutex);
174 		}
175 	}
176 
177 	(void) rw_unlock(&getent_ctxDB_rwlock);
178 
179 	return (ret);
180 }
181 
182 int
183 _nscd_is_getent_ctx_in_use(
184 	nscd_getent_context_t	*ctx)
185 {
186 	int	in_use;
187 	char	*me = "_nscd_getent_ctx_in_use";
188 
189 	(void) mutex_lock(&ctx->getent_mutex);
190 
191 	_NSCD_LOG(NSCD_LOG_GETENT_CTX, NSCD_LOG_LEVEL_DEBUG)
192 	(me, "in_use = %d, ctx->thr_id = %d, thread id = %d\n",
193 	    ctx->in_use, ctx->thr_id, thr_self());
194 
195 	in_use = ctx->in_use;
196 	if (in_use == 1 && ctx->thr_id == thr_self())
197 		in_use = 0;
198 	(void) mutex_unlock(&ctx->getent_mutex);
199 	return (in_use);
200 }
201 
202 /*
203  * FUNCTION: _nscd_free_ctx_if_aborted
204  *
205  * Check to see if the getent session associated with a getent context had
206  * been aborted. If so, return the getent context back to the pool.
207  */
208 void
209 _nscd_free_ctx_if_aborted(
210 	nscd_getent_context_t	*ctx)
211 {
212 	int	aborted;
213 	char	*me = "_nscd_free_ctx_if_aborted";
214 
215 	(void) mutex_lock(&ctx->getent_mutex);
216 
217 	_NSCD_LOG(NSCD_LOG_GETENT_CTX, NSCD_LOG_LEVEL_DEBUG)
218 	(me, "in_use = %d, aborted = %d\n", ctx->in_use, ctx->aborted);
219 
220 	if (ctx->in_use != 1) {
221 		(void) mutex_unlock(&ctx->getent_mutex);
222 		return;
223 	}
224 	aborted = ctx->aborted;
225 	ctx->in_use = 0;
226 	(void) mutex_unlock(&ctx->getent_mutex);
227 
228 	if (aborted == 1) {
229 		_NSCD_LOG(NSCD_LOG_GETENT_CTX, NSCD_LOG_LEVEL_DEBUG)
230 		(me, "getent session aborted, return the getent context\n");
231 		_nscd_put_getent_ctx(ctx);
232 	}
233 }
234 
235 /*
236  * FUNCTION: _nscd_del_getent_ctx
237  *
238  * Delete a getent context from the internal getent context database.
239  */
240 static void
241 _nscd_del_getent_ctx(
242 	nscd_getent_context_t	*ptr,
243 	nscd_cookie_num_t	cookie_num)
244 {
245 	char			ptrstr[32];
246 	nscd_getent_ctx_t	*gnctx;
247 	const nscd_db_entry_t	*db_entry;
248 
249 	if (ptr == NULL)
250 		return;
251 
252 	(void) snprintf(ptrstr, sizeof (ptrstr), "%lld", cookie_num);
253 
254 	(void) rw_rdlock(&getent_ctxDB_rwlock);
255 	/*
256 	 * first find the db entry and make sure the
257 	 * sequence number matched, then delete it from
258 	 * the database.
259 	 */
260 	db_entry = _nscd_get_db_entry(getent_ctxDB,
261 	    NSCD_DATA_CTX_ADDR,
262 	    (const char *)ptrstr,
263 	    NSCD_GET_FIRST_DB_ENTRY, 0);
264 	if (db_entry != NULL) {
265 		gnctx = (nscd_getent_ctx_t *)*(db_entry->data_array);
266 		if (gnctx->ptr == ptr && gnctx->cookie_num  == cookie_num) {
267 
268 			(void) rw_unlock(&getent_ctxDB_rwlock);
269 			(void) rw_wrlock(&getent_ctxDB_rwlock);
270 
271 			(void) _nscd_delete_db_entry(getent_ctxDB,
272 			    NSCD_DATA_CTX_ADDR,
273 			    (const char *)ptrstr,
274 			    NSCD_DEL_FIRST_DB_ENTRY, 0);
275 		}
276 	}
277 	(void) rw_unlock(&getent_ctxDB_rwlock);
278 }
279 
280 static void
281 _nscd_free_getent_ctx(
282 	nscd_getent_context_t	*gnctx)
283 {
284 
285 	char			*me = "_nscd_free_getent_ctx";
286 
287 	_NSCD_LOG(NSCD_LOG_GETENT_CTX, NSCD_LOG_LEVEL_DEBUG)
288 	(me, "getent context %p\n", gnctx);
289 
290 	_nscd_put_nsw_state(gnctx->nsw_state);
291 	_nscd_del_getent_ctx(gnctx, gnctx->cookie_num);
292 	free(gnctx);
293 }
294 
295 
296 static void
297 _nscd_free_getent_ctx_base(
298 	nscd_acc_data_t		*data)
299 {
300 	nscd_getent_ctx_base_t	*base = (nscd_getent_ctx_base_t *)data;
301 	nscd_getent_context_t	*c, *tc;
302 	char			*me = "_nscd_free_getent_ctx_base";
303 
304 	_NSCD_LOG(NSCD_LOG_GETENT_CTX | NSCD_LOG_CONFIG, NSCD_LOG_LEVEL_DEBUG)
305 	(me, "getent context base %p\n", base);
306 
307 	if (base == NULL)
308 		return;
309 
310 	c = base->first;
311 	while (c != NULL) {
312 		tc = c->next;
313 		_nscd_free_getent_ctx(c);
314 		c = tc;
315 	}
316 }
317 
318 void
319 _nscd_free_all_getent_ctx_base()
320 {
321 	nscd_getent_ctx_base_t	*base;
322 	int			i;
323 	char			*me = "_nscd_free_all_getent_ctx_base";
324 
325 	_NSCD_LOG(NSCD_LOG_GETENT_CTX | NSCD_LOG_CONFIG, NSCD_LOG_LEVEL_DEBUG)
326 	(me, "entering ..\n");
327 
328 	(void) rw_wrlock(&nscd_getent_ctx_base_lock);
329 
330 	for (i = 0; i < NSCD_NUM_DB; i++) {
331 
332 		base = nscd_getent_ctx_base[i];
333 		if (base == NULL)
334 			continue;
335 
336 		nscd_getent_ctx_base[i] = (nscd_getent_ctx_base_t *)
337 		    _nscd_set((nscd_acc_data_t *)base, NULL);
338 	}
339 	(void) rw_unlock(&nscd_getent_ctx_base_lock);
340 }
341 
342 static nscd_getent_context_t *
343 _nscd_create_getent_ctx(
344 	nscd_nsw_params_t	*params)
345 {
346 	nscd_getent_context_t	*gnctx;
347 	nss_db_root_t		db_root;
348 	char			*me = "_nscd_create_getent_ctx";
349 
350 	gnctx = calloc(1, sizeof (nscd_getent_context_t));
351 	if (gnctx == NULL)
352 		return (NULL);
353 	else {
354 		_NSCD_LOG(NSCD_LOG_GETENT_CTX, NSCD_LOG_LEVEL_DEBUG)
355 		(me, "getent context allocated %p\n", gnctx);
356 	}
357 
358 	gnctx->dbi = params->dbi;
359 	gnctx->cookie_num = _nscd_get_cookie_num();
360 	gnctx->pid = -1;
361 	(void) mutex_init(&gnctx->getent_mutex, USYNC_THREAD, NULL);
362 
363 	if (_nscd_get_nsw_state(&db_root, params) != NSCD_SUCCESS) {
364 		free(gnctx);
365 		return (NULL);
366 	}
367 	gnctx->nsw_state = (nscd_nsw_state_t *)db_root.s;
368 	/* this is a nsw_state used for getent processing */
369 	gnctx->nsw_state->getent = 1;
370 
371 	_NSCD_LOG(NSCD_LOG_GETENT_CTX, NSCD_LOG_LEVEL_DEBUG)
372 	(me, "got nsw_state %p\n", gnctx->nsw_state);
373 
374 	return (gnctx);
375 }
376 
377 
378 nscd_rc_t
379 _nscd_get_getent_ctx(
380 	nss_getent_t		*contextpp,
381 	nscd_nsw_params_t	*params)
382 {
383 
384 	nscd_getent_context_t	*c;
385 	nscd_getent_ctx_base_t	*base, *tmp;
386 	nscd_rc_t		rc;
387 	char			*me = "_nscd_get_getent_ctx";
388 
389 	_NSCD_LOG(NSCD_LOG_GETENT_CTX, NSCD_LOG_LEVEL_DEBUG)
390 	(me, "entering ...\n");
391 
392 	(void) rw_rdlock(&nscd_getent_ctx_base_lock);
393 	base = nscd_getent_ctx_base[params->dbi];
394 	(void) rw_unlock(&nscd_getent_ctx_base_lock);
395 	assert(base != NULL);
396 
397 	/*
398 	 * If the context list is not empty, return the first one
399 	 * on the list. Otherwise, create and return a new one if
400 	 * limit is not reached. if reacehed, wait for the 'one is
401 	 * available' signal.
402 	 */
403 	tmp = (nscd_getent_ctx_base_t *)_nscd_mutex_lock(
404 	    (nscd_acc_data_t *)base);
405 	assert(base == tmp);
406 	if (base->first == NULL) {
407 		if (base->num_getent_ctx == base->max_getent_ctx) {
408 			base->num_waiter++;
409 			while (base->first == NULL) {
410 
411 				_NSCD_LOG(NSCD_LOG_GETENT_CTX,
412 				    NSCD_LOG_LEVEL_DEBUG)
413 				(me, "waiting for signal\n");
414 
415 				_nscd_cond_wait((nscd_acc_data_t *)base, NULL);
416 
417 				_NSCD_LOG(NSCD_LOG_GETENT_CTX,
418 				    NSCD_LOG_LEVEL_DEBUG)
419 				(me, "woke up\n");
420 			}
421 			base->num_waiter--;
422 		} else {
423 			base->first = _nscd_create_getent_ctx(params);
424 			if (base->first != NULL) {
425 				base->first->base = base;
426 				base->num_getent_ctx++;
427 			} else {
428 				/* not able to create an getent ctx */
429 
430 				_NSCD_LOG(NSCD_LOG_GETENT_CTX,
431 				    NSCD_LOG_LEVEL_ERROR)
432 				(me, "create getent ctx failed\n");
433 
434 				_nscd_mutex_unlock((nscd_acc_data_t *)base);
435 				return (NSCD_CREATE_GETENT_CTX_FAILED);
436 			}
437 
438 			_NSCD_LOG(NSCD_LOG_GETENT_CTX, NSCD_LOG_LEVEL_DEBUG)
439 			(me, "got a new getent ctx %p\n", base->first);
440 		}
441 	}
442 
443 	assert(base->first != NULL);
444 
445 	c = base->first;
446 	base->first = c->next;
447 	c->next = NULL;
448 	c->seq_num = 1;
449 	c->cookie_num = _nscd_get_cookie_num();
450 	c->in_use = 1;
451 	c->thr_id = thr_self();
452 
453 	_NSCD_LOG(NSCD_LOG_GETENT_CTX, NSCD_LOG_LEVEL_DEBUG)
454 	(me, "got a getent ctx %p\n", c);
455 
456 	_nscd_mutex_unlock((nscd_acc_data_t *)base);
457 
458 	_NSCD_LOG(NSCD_LOG_GETENT_CTX, NSCD_LOG_LEVEL_DEBUG)
459 	(me, "adding new ctx %p, cookie # = %lld\n", c, c->cookie_num);
460 
461 	if ((rc = _nscd_add_getent_ctx(c, c->cookie_num)) != NSCD_SUCCESS) {
462 		_nscd_put_getent_ctx(c);
463 		return (rc);
464 	}
465 	contextpp->ctx = (struct nss_getent_context *)c;
466 
467 	/* start monitor and reclaim orphan getent context */
468 	if (getent_monitor_started == 0) {
469 		(void) mutex_lock(&getent_monitor_mutex);
470 		if (getent_monitor_started == 0) {
471 			getent_monitor_started = 1;
472 			(void) _nscd_init_getent_ctx_monitor();
473 		}
474 		(void) mutex_unlock(&getent_monitor_mutex);
475 	}
476 
477 	return (NSCD_SUCCESS);
478 }
479 
480 void
481 _nscd_put_getent_ctx(
482 	nscd_getent_context_t	*gnctx)
483 {
484 
485 	nscd_getent_ctx_base_t	*base;
486 	char			*me = "_nscd_put_getent_ctx";
487 
488 	base = gnctx->base;
489 
490 	/* if context base is gone, so should this context */
491 	if ((_nscd_mutex_lock((nscd_acc_data_t *)base)) == NULL) {
492 		_nscd_free_getent_ctx(gnctx);
493 		return;
494 	}
495 
496 	if (base->first != NULL) {
497 		gnctx->next = base->first;
498 		base->first = gnctx;
499 	} else
500 		base->first = gnctx;
501 
502 	/* put back the db state */
503 	_NSCD_LOG(NSCD_LOG_GETENT_CTX, NSCD_LOG_LEVEL_DEBUG)
504 	(me, "putting back nsw state %p\n", gnctx->nsw_state);
505 
506 	/* this nsw_state is no longer used for getent processing */
507 	if (gnctx->nsw_state != NULL) {
508 		gnctx->nsw_state->getent = 0;
509 		_nscd_put_nsw_state(gnctx->nsw_state);
510 		gnctx->nsw_state = NULL;
511 	}
512 
513 	gnctx->aborted = 0;
514 	gnctx->in_use = 0;
515 	gnctx->thr_id = (thread_t)-1;
516 	_nscd_del_getent_ctx(gnctx, gnctx->cookie_num);
517 
518 	_NSCD_LOG(NSCD_LOG_GETENT_CTX, NSCD_LOG_LEVEL_DEBUG)
519 	(me, "ctx (%p, cookie # = %lld) removed from getent ctx DB\n",
520 	    gnctx, gnctx->cookie_num);
521 
522 	if (base->num_waiter > 0) {
523 		_NSCD_LOG(NSCD_LOG_GETENT_CTX, NSCD_LOG_LEVEL_DEBUG)
524 		(me, "signaling (waiter = %d)\n", base->num_waiter);
525 
526 		_nscd_cond_signal((nscd_acc_data_t *)base);
527 	}
528 
529 	gnctx->seq_num = 0;
530 	gnctx->cookie_num = 0;
531 	gnctx->pid = -1;
532 	gnctx->thr_id = (thread_t)-1;
533 	gnctx->n_src = 0;
534 	gnctx->be = NULL;
535 
536 	_nscd_mutex_unlock((nscd_acc_data_t *)base);
537 }
538 
539 nscd_rc_t
540 _nscd_init_getent_ctx_base(
541 	int			dbi,
542 	int			lock)
543 {
544 	nscd_getent_ctx_base_t	*base = NULL;
545 	char			*me = "_nscd_init_getent_ctx_base";
546 
547 	if (lock)
548 		(void) rw_rdlock(&nscd_getent_ctx_base_lock);
549 
550 	base = (nscd_getent_ctx_base_t *)_nscd_alloc(
551 	    NSCD_DATA_GETENT_CTX_BASE,
552 	    sizeof (nscd_getent_ctx_base_t),
553 	    _nscd_free_getent_ctx_base,
554 	    NSCD_ALLOC_MUTEX | NSCD_ALLOC_COND);
555 
556 	if (base == NULL) {
557 		if (lock)
558 			(void) rw_unlock(&nscd_getent_ctx_base_lock);
559 		return (NSCD_NO_MEMORY);
560 	}
561 	_NSCD_LOG(NSCD_LOG_GETENT_CTX | NSCD_LOG_CONFIG, NSCD_LOG_LEVEL_DEBUG)
562 	(me, "base %p allocated\n", base);
563 
564 	/*
565 	 * initialize and activate the new getent_ctx base
566 	 */
567 	base->dbi = dbi;
568 	base->max_getent_ctx = NSCD_SW_CFG(dbi).max_getent_ctx_per_db;
569 	nscd_getent_ctx_base[dbi] =
570 	    (nscd_getent_ctx_base_t *)_nscd_set(
571 	    (nscd_acc_data_t *)nscd_getent_ctx_base[dbi],
572 	    (nscd_acc_data_t *)base);
573 
574 	if (lock)
575 		(void) rw_unlock(&nscd_getent_ctx_base_lock);
576 
577 	return (NSCD_SUCCESS);
578 }
579 
580 nscd_rc_t
581 _nscd_init_all_getent_ctx_base()
582 {
583 	int			i;
584 	nscd_rc_t		rc;
585 	char			*me = "_nscd_init_all_getent_ctx_base";
586 
587 	(void) rw_wrlock(&nscd_getent_ctx_base_lock);
588 
589 	for (i = 0; i < NSCD_NUM_DB; i++) {
590 
591 		rc = _nscd_init_getent_ctx_base(i, 0);
592 
593 		if (rc != NSCD_SUCCESS) {
594 			(void) rw_unlock(&nscd_getent_ctx_base_lock);
595 			return (rc);
596 		}
597 	}
598 
599 	_NSCD_LOG(NSCD_LOG_GETENT_CTX | NSCD_LOG_CONFIG, NSCD_LOG_LEVEL_DEBUG)
600 	(me, "all getent context base initialized\n");
601 
602 	(void) rw_unlock(&nscd_getent_ctx_base_lock);
603 
604 	return (NSCD_SUCCESS);
605 }
606 nscd_rc_t
607 _nscd_alloc_getent_ctx_base()
608 {
609 
610 	(void) rw_wrlock(&nscd_getent_ctx_base_lock);
611 
612 	nscd_getent_ctx_base = calloc(NSCD_NUM_DB,
613 	    sizeof (nscd_getent_ctx_base_t *));
614 	if (nscd_getent_ctx_base == NULL) {
615 		(void) rw_unlock(&nscd_getent_ctx_base_lock);
616 		return (NSCD_NO_MEMORY);
617 	}
618 
619 	(void) rw_unlock(&nscd_getent_ctx_base_lock);
620 
621 	return (NSCD_SUCCESS);
622 }
623 
624 static int
625 process_exited(pid_t pid)
626 {
627 	char	pname[PATH_MAX];
628 	int	fd;
629 
630 	(void) snprintf(pname, sizeof (pname), "/proc/%d/psinfo", pid);
631 	if ((fd = open(pname, O_RDONLY)) == -1)
632 		return (1);
633 	else {
634 		(void) close(fd);
635 		return (0);
636 	}
637 }
638 
639 /*
640  * FUNCTION: reclaim_getent_ctx
641  */
642 /*ARGSUSED*/
643 static void *
644 reclaim_getent_ctx(void *arg)
645 {
646 	void			*cookie = NULL;
647 	nscd_db_entry_t		*ep;
648 	nscd_getent_ctx_t	*ctx;
649 	nscd_getent_context_t	*gctx, *c;
650 	nscd_getent_context_t	*first = NULL, *last = NULL;
651 	nss_getent_t		nssctx = { 0 };
652 	char			*me = "reclaim_getent_ctx";
653 
654 	/*CONSTCOND*/
655 	while (1) {
656 
657 		(void) sleep(60);
658 
659 		(void) rw_rdlock(&getent_ctxDB_rwlock);
660 
661 		for (ep = _nscd_walk_db(getent_ctxDB, &cookie); ep != NULL;
662 		    ep = _nscd_walk_db(getent_ctxDB, &cookie)) {
663 
664 			ctx = (nscd_getent_ctx_t *)*(ep->data_array);
665 
666 			gctx = ctx->ptr;
667 
668 			/*
669 			 * if the client process, which did the setent,
670 			 * exited, add the context to the orphan list
671 			 */
672 			if (gctx->pid != -1 && process_exited(gctx->pid)) {
673 
674 				_NSCD_LOG(NSCD_LOG_GETENT_CTX,
675 				    NSCD_LOG_LEVEL_DEBUG)
676 				(me, "process  %d exited, "
677 				    "getent context = %p, "
678 				    "db index = %d, cookie # = %lld, "
679 				    "sequence # = %lld\n",
680 				    gctx->pid, gctx, gctx->dbi,
681 				    gctx->cookie_num, gctx->seq_num);
682 
683 				if (first != NULL) {
684 					/* add to list if not in already */
685 					for (c = first; c != NULL;
686 					    c = c->next_to_reclaim) {
687 						if (gctx == c)
688 							break;
689 					}
690 					if (c == NULL) {
691 						last->next_to_reclaim = gctx;
692 						last = gctx;
693 					}
694 				} else {
695 					first = gctx;
696 					last = gctx;
697 				}
698 			}
699 		}
700 
701 		(void) rw_unlock(&getent_ctxDB_rwlock);
702 
703 
704 		/*
705 		 * return all the orphan getent contexts to the pool if not
706 		 * in use
707 		 */
708 		for (gctx = first; gctx; ) {
709 			int in_use, num_reclaim_check;
710 
711 			c = gctx->next_to_reclaim;
712 			gctx->next_to_reclaim = NULL;
713 			gctx->aborted = 1;
714 
715 			(void) mutex_lock(&gctx->getent_mutex);
716 			num_reclaim_check = gctx->num_reclaim_check++;
717 			if (num_reclaim_check > 1)
718 				gctx->in_use = 0;
719 			in_use = gctx->in_use;
720 			(void) mutex_unlock(&gctx->getent_mutex);
721 
722 			if (in_use == 0) {
723 				_NSCD_LOG(NSCD_LOG_GETENT_CTX,
724 				    NSCD_LOG_LEVEL_DEBUG)
725 				(me, "process  %d exited, "
726 				    "freeing getent context = %p\n",
727 				    gctx->pid, gctx);
728 				nssctx.ctx = (struct nss_getent_context *)gctx;
729 				nss_endent(NULL, NULL, &nssctx);
730 			}
731 			gctx = c;
732 		}
733 		first = last = NULL;
734 	}
735 	/*NOTREACHED*/
736 	/*LINTED E_FUNC_HAS_NO_RETURN_STMT*/
737 }
738 
739 static nscd_rc_t
740 _nscd_init_getent_ctx_monitor() {
741 
742 	int	errnum;
743 	char	*me = "_nscd_init_getent_ctx_monitor";
744 
745 	_NSCD_LOG(NSCD_LOG_GETENT_CTX, NSCD_LOG_LEVEL_DEBUG)
746 	(me, "initializing the getent context monitor\n");
747 
748 	/*
749 	 * the forker nscd does not process getent requests
750 	 * so no need to monitor orphan getent contexts
751 	 */
752 	if (_whoami == NSCD_FORKER)
753 		return (NSCD_SUCCESS);
754 
755 	/*
756 	 * start a thread to reclaim unused getent contexts
757 	 */
758 	if (thr_create(NULL, NULL, reclaim_getent_ctx,
759 		NULL, THR_DETACHED, NULL) != 0) {
760 		errnum = errno;
761 		_NSCD_LOG(NSCD_LOG_GETENT_CTX, NSCD_LOG_LEVEL_ERROR)
762 		(me, "thr_create: %s\n", strerror(errnum));
763 		return (NSCD_THREAD_CREATE_ERROR);
764 	}
765 
766 	return (NSCD_SUCCESS);
767 }
768