xref: /illumos-gate/usr/src/cmd/nscd/cache.c (revision 606f6aa3)
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 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * Cache routines for nscd
30  */
31 #include <assert.h>
32 #include <errno.h>
33 #include <memory.h>
34 #include <signal.h>
35 #include <stdlib.h>
36 #include <stddef.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include <sys/stat.h>
40 #include <sys/time.h>
41 #include <sys/types.h>
42 #include <sys/wait.h>
43 #include <unistd.h>
44 #include <ucred.h>
45 #include <nss_common.h>
46 #include <locale.h>
47 #include <ctype.h>
48 #include <strings.h>
49 #include <string.h>
50 #include <umem.h>
51 #include <fcntl.h>
52 #include "cache.h"
53 #include "nscd_door.h"
54 #include "nscd_log.h"
55 #include "nscd_config.h"
56 #include "nscd_frontend.h"
57 #include "nscd_switch.h"
58 
59 #define	SUCCESS		0
60 #define	NOTFOUND	-1
61 #define	SERVERERROR	-2
62 #define	NOSERVER	-3
63 #define	CONTINUE	-4
64 
65 static nsc_db_t *nsc_get_db(nsc_ctx_t *, int);
66 static nscd_rc_t lookup_cache(nsc_lookup_args_t *, nscd_cfg_cache_t *,
67 		nss_XbyY_args_t *, char *, nsc_entry_t **);
68 static uint_t reap_cache(nsc_ctx_t *, uint_t, uint_t);
69 static void delete_entry(nsc_db_t *, nsc_ctx_t *, nsc_entry_t *);
70 static void print_stats(nscd_cfg_stat_cache_t *);
71 static void print_cfg(nscd_cfg_cache_t *);
72 static int lookup_int(nsc_lookup_args_t *, int);
73 
74 #ifdef	NSCD_DEBUG
75 static void print_entry(nsc_db_t *, time_t, nsc_entry_t *);
76 static void avl_dump(nsc_db_t *, time_t);
77 static void hash_dump(nsc_db_t *, time_t);
78 #endif	/* NSCD_DEBUG */
79 static nsc_entry_t *hash_find(nsc_db_t *, nsc_entry_t *, uint_t *, nscd_bool_t);
80 
81 static void queue_adjust(nsc_db_t *, nsc_entry_t *);
82 static void queue_remove(nsc_db_t *, nsc_entry_t *);
83 #ifdef	NSCD_DEBUG
84 static void queue_dump(nsc_db_t *, time_t);
85 #endif	/* NSCD_DEBUG */
86 
87 static int launch_update(nsc_lookup_args_t *);
88 static void do_update(nsc_lookup_args_t *);
89 static void getxy_keepalive(nsc_ctx_t *, nsc_db_t *, int, int);
90 
91 static void ctx_info(nsc_ctx_t *);
92 static void ctx_info_nolock(nsc_ctx_t *);
93 static void ctx_invalidate(nsc_ctx_t *);
94 
95 static void nsc_db_str_key_getlogstr(char *, char *, size_t, nss_XbyY_args_t *);
96 static void nsc_db_int_key_getlogstr(char *, char *, size_t, nss_XbyY_args_t *);
97 static void nsc_db_any_key_getlogstr(char *, char *, size_t, nss_XbyY_args_t *);
98 
99 static int nsc_db_cis_key_compar(const void *, const void *);
100 static int nsc_db_ces_key_compar(const void *, const void *);
101 static int nsc_db_int_key_compar(const void *, const void *);
102 
103 static uint_t nsc_db_cis_key_gethash(nss_XbyY_key_t *, int);
104 static uint_t nsc_db_ces_key_gethash(nss_XbyY_key_t *, int);
105 static uint_t nsc_db_int_key_gethash(nss_XbyY_key_t *, int);
106 
107 static umem_cache_t	*nsc_entry_cache;
108 
109 static nsc_ctx_t *init_cache_ctx(int);
110 static void reaper(nsc_ctx_t *);
111 static void revalidate(nsc_ctx_t *);
112 
113 static nss_status_t
114 dup_packed_buffer(void *src, void *dst) {
115 	nsc_lookup_args_t	*s = (nsc_lookup_args_t *)src;
116 	nsc_entry_t		*d = (nsc_entry_t *)dst;
117 	nss_pheader_t		*sphdr = (nss_pheader_t *)s->buffer;
118 	nss_pheader_t		*dphdr = (nss_pheader_t *)d->buffer;
119 	int			slen, new_pbufsiz = 0;
120 
121 	if (NSCD_GET_STATUS(sphdr) != NSS_SUCCESS) {
122 
123 		/* no result, copy header only (status, errno, etc) */
124 		slen = sphdr->data_off;
125 	} else {
126 		/*
127 		 * lookup result returned, data to copy is the packed
128 		 * header plus result (add 1 for the terminating NULL
129 		 * just in case)
130 		 */
131 		slen = sphdr->data_off + sphdr->data_len + 1;
132 	}
133 
134 	/* allocate cache packed buffer */
135 	if (dphdr != NULL && d->bufsize <= slen && d->bufsize != 0) {
136 		/* old buffer too small, free it */
137 		free(dphdr);
138 		d->buffer = NULL;
139 		d->bufsize = 0;
140 		dphdr = NULL;
141 	}
142 	if (dphdr == NULL) {
143 		/* get new buffer */
144 		dphdr = calloc(1, slen + 1);
145 		if (dphdr == NULL)
146 			return (NSS_ERROR);
147 		d->buffer = dphdr;
148 		d->bufsize = slen + 1;
149 		new_pbufsiz = slen + 1;
150 	}
151 
152 	(void) memcpy(dphdr, sphdr, slen);
153 	if (new_pbufsiz != 0)
154 		dphdr->pbufsiz = new_pbufsiz;
155 
156 	return (NSS_SUCCESS);
157 }
158 
159 char *cache_name[CACHE_CTX_COUNT] = {
160 	NSS_DBNAM_PASSWD,
161 	NSS_DBNAM_GROUP,
162 	NSS_DBNAM_HOSTS,
163 	NSS_DBNAM_IPNODES,
164 	NSS_DBNAM_EXECATTR,
165 	NSS_DBNAM_PROFATTR,
166 	NSS_DBNAM_USERATTR,
167 	NSS_DBNAM_ETHERS,
168 	NSS_DBNAM_RPC,
169 	NSS_DBNAM_PROTOCOLS,
170 	NSS_DBNAM_NETWORKS,
171 	NSS_DBNAM_BOOTPARAMS,
172 	NSS_DBNAM_AUDITUSER,
173 	NSS_DBNAM_AUTHATTR,
174 	NSS_DBNAM_SERVICES,
175 	NSS_DBNAM_NETMASKS,
176 	NSS_DBNAM_PRINTERS,
177 	NSS_DBNAM_PROJECT,
178 	NSS_DBNAM_TSOL_TP,
179 	NSS_DBNAM_TSOL_RH
180 };
181 
182 typedef void (*cache_init_ctx_t)(nsc_ctx_t *);
183 static cache_init_ctx_t cache_init_ctx[CACHE_CTX_COUNT] = {
184 	passwd_init_ctx,
185 	group_init_ctx,
186 	host_init_ctx,
187 	ipnode_init_ctx,
188 	exec_init_ctx,
189 	prof_init_ctx,
190 	user_init_ctx,
191 	ether_init_ctx,
192 	rpc_init_ctx,
193 	proto_init_ctx,
194 	net_init_ctx,
195 	bootp_init_ctx,
196 	auuser_init_ctx,
197 	auth_init_ctx,
198 	serv_init_ctx,
199 	netmask_init_ctx,
200 	printer_init_ctx,
201 	project_init_ctx,
202 	tnrhtp_init_ctx,
203 	tnrhdb_init_ctx
204 };
205 
206 nsc_ctx_t *cache_ctx_p[CACHE_CTX_COUNT] = { 0 };
207 static nscd_cfg_stat_cache_t	null_stats = { 0 };
208 static nscd_cfg_global_cache_t	global_cfg;
209 
210 /*
211  * Given database name 'dbname' find cache index
212  */
213 int
214 get_cache_idx(char *dbname) {
215 	int	i;
216 	char	*nsc_name;
217 
218 	for (i = 0; i < CACHE_CTX_COUNT; i++) {
219 		nsc_name = cache_name[i];
220 		if (strcmp(nsc_name, dbname) == 0)
221 			return (i);
222 	}
223 	return (-1);
224 }
225 
226 /*
227  * Given database name 'dbname' retrieve cache context,
228  * if not created yet, allocate and initialize it.
229  */
230 static nscd_rc_t
231 get_cache_ctx(char *dbname, nsc_ctx_t **ctx) {
232 	int	i;
233 
234 	*ctx = NULL;
235 
236 	i = get_cache_idx(dbname);
237 	if (i == -1)
238 		return (NSCD_INVALID_ARGUMENT);
239 	if ((*ctx = cache_ctx_p[i]) == NULL) {
240 		*ctx = init_cache_ctx(i);
241 		if (*ctx == NULL)
242 			return (NSCD_NO_MEMORY);
243 	}
244 
245 	return (NSCD_SUCCESS);
246 }
247 
248 /*
249  * Generate a log string to identify backend operation in debug logs
250  */
251 static void
252 nsc_db_str_key_getlogstr(char *name, char *whoami, size_t len,
253 		nss_XbyY_args_t *argp) {
254 	(void) snprintf(whoami, len, "%s [key=%s]", name, argp->key.name);
255 }
256 
257 
258 static void
259 nsc_db_int_key_getlogstr(char *name, char *whoami, size_t len,
260 		nss_XbyY_args_t *argp) {
261 	(void) snprintf(whoami, len, "%s [key=%d]", name, argp->key.number);
262 }
263 
264 /*ARGSUSED*/
265 static void
266 nsc_db_any_key_getlogstr(char *name, char *whoami, size_t len,
267 		nss_XbyY_args_t *argp) {
268 	(void) snprintf(whoami, len, "%s", name);
269 }
270 
271 
272 /*
273  * Returns cache based on dbop
274  */
275 static nsc_db_t *
276 nsc_get_db(nsc_ctx_t *ctx, int dbop) {
277 	int	i;
278 
279 	for (i = 0; i < ctx->db_count; i++) {
280 		if (ctx->nsc_db[i] && dbop == ctx->nsc_db[i]->dbop)
281 			return (ctx->nsc_db[i]);
282 	}
283 	return (NULL);
284 }
285 
286 
287 /*
288  * integer compare routine for _NSC_DB_INT_KEY
289  */
290 static int
291 nsc_db_int_key_compar(const void *n1, const void *n2) {
292 	nsc_entry_t	*e1, *e2;
293 
294 	e1 = (nsc_entry_t *)n1;
295 	e2 = (nsc_entry_t *)n2;
296 	return (_NSC_INT_KEY_CMP(e1->key.number, e2->key.number));
297 }
298 
299 
300 /*
301  * case sensitive name compare routine for _NSC_DB_CES_KEY
302  */
303 static int
304 nsc_db_ces_key_compar(const void *n1, const void *n2) {
305 	nsc_entry_t	*e1, *e2;
306 	int		res, l1, l2;
307 
308 	e1 = (nsc_entry_t *)n1;
309 	e2 = (nsc_entry_t *)n2;
310 	l1 = strlen(e1->key.name);
311 	l2 = strlen(e2->key.name);
312 	res = strncmp(e1->key.name, e2->key.name, (l1 > l2)?l1:l2);
313 	return (_NSC_INT_KEY_CMP(res, 0));
314 }
315 
316 
317 /*
318  * case insensitive name compare routine _NSC_DB_CIS_KEY
319  */
320 static int
321 nsc_db_cis_key_compar(const void *n1, const void *n2) {
322 	nsc_entry_t	*e1, *e2;
323 	int		res, l1, l2;
324 
325 	e1 = (nsc_entry_t *)n1;
326 	e2 = (nsc_entry_t *)n2;
327 	l1 = strlen(e1->key.name);
328 	l2 = strlen(e2->key.name);
329 	res = strncasecmp(e1->key.name, e2->key.name, (l1 > l2)?l1:l2);
330 	return (_NSC_INT_KEY_CMP(res, 0));
331 }
332 
333 /*
334  * macro used to generate elf hashes for strings
335  */
336 #define	_NSC_ELF_STR_GETHASH(func, str, htsize, hval) \
337 	hval = 0; \
338 	while (*str) { \
339 		uint_t  g; \
340 		hval = (hval << 4) + func(*str++); \
341 		if ((g = (hval & 0xf0000000)) != 0) \
342 			hval ^= g >> 24; \
343 		hval &= ~g; \
344 	} \
345 	hval %= htsize;
346 
347 
348 /*
349  * cis hash function
350  */
351 uint_t
352 cis_gethash(const char *key, int htsize) {
353 	uint_t	hval;
354 	if (key == NULL)
355 		return (0);
356 	_NSC_ELF_STR_GETHASH(tolower, key, htsize, hval);
357 	return (hval);
358 }
359 
360 
361 /*
362  * ces hash function
363  */
364 uint_t
365 ces_gethash(const char *key, int htsize) {
366 	uint_t	hval;
367 	if (key == NULL)
368 		return (0);
369 	_NSC_ELF_STR_GETHASH(, key, htsize, hval);
370 	return (hval);
371 }
372 
373 
374 /*
375  * one-at-a-time hash function
376  */
377 uint_t
378 db_gethash(const void *key, int len, int htsize) {
379 	uint_t	hval, i;
380 	const char *str = key;
381 
382 	if (str == NULL)
383 		return (0);
384 
385 	for (hval = 0, i = 0; i < len; i++) {
386 		hval += str[i];
387 		hval += (hval << 10);
388 		hval ^= (hval >> 6);
389 	}
390 	hval += (hval << 3);
391 	hval ^= (hval >> 11);
392 	hval += (hval << 15);
393 	return (hval % htsize);
394 }
395 
396 
397 /*
398  * case insensitive name gethash routine _NSC_DB_CIS_KEY
399  */
400 static uint_t
401 nsc_db_cis_key_gethash(nss_XbyY_key_t *key, int htsize) {
402 	return (cis_gethash(key->name, htsize));
403 }
404 
405 
406 /*
407  * case sensitive name gethash routine _NSC_DB_CES_KEY
408  */
409 static uint_t
410 nsc_db_ces_key_gethash(nss_XbyY_key_t *key, int htsize) {
411 	return (ces_gethash(key->name, htsize));
412 }
413 
414 
415 /*
416  * integer gethash routine _NSC_DB_INT_KEY
417  */
418 static uint_t
419 nsc_db_int_key_gethash(nss_XbyY_key_t *key, int htsize) {
420 	return (db_gethash(&key->number, sizeof (key->number), htsize));
421 }
422 
423 
424 /*
425  * Find entry in the hash table
426  * if cmp == nscd_true)
427  *	return entry only if the keys match
428  * else
429  *	return entry in the hash location without checking the keys
430  *
431  */
432 static nsc_entry_t *
433 hash_find(nsc_db_t *nscdb, nsc_entry_t *entry, uint_t *hash,
434 			nscd_bool_t cmp) {
435 
436 	nsc_entry_t	*hashentry;
437 
438 	if (nscdb->gethash)
439 		*hash = nscdb->gethash(&entry->key, nscdb->htsize);
440 	else
441 		return (NULL);
442 
443 	hashentry = nscdb->htable[*hash];
444 	if (cmp == nscd_false || hashentry == NULL)
445 		return (hashentry);
446 	if (nscdb->compar) {
447 		if (nscdb->compar(entry, hashentry) == 0)
448 			return (hashentry);
449 	}
450 	return (NULL);
451 }
452 
453 
454 #define	HASH_REMOVE(nscdb, entry, hash, cmp) \
455 	if (nscdb->htable) { \
456 		if (entry == hash_find(nscdb, entry, &hash, cmp)) \
457 			nscdb->htable[hash] = NULL; \
458 	}
459 
460 
461 #define	HASH_INSERT(nscdb, entry, hash, cmp) \
462 	if (nscdb->htable) { \
463 		(void) hash_find(nscdb, entry, &hash, cmp); \
464 		nscdb->htable[hash] = entry; \
465 	}
466 
467 
468 #ifdef	NSCD_DEBUG
469 static void
470 print_entry(nsc_db_t *nscdb, time_t now, nsc_entry_t *entry) {
471 	nss_XbyY_args_t args;
472 	char		whoami[512];
473 
474 	switch (entry->stats.status) {
475 	case ST_NEW_ENTRY:
476 		(void) fprintf(stdout, gettext("\t status: new entry\n"));
477 		return;
478 	case ST_UPDATE_PENDING:
479 		(void) fprintf(stdout, gettext("\t status: update pending\n"));
480 		return;
481 	case ST_LOOKUP_PENDING:
482 		(void) fprintf(stdout, gettext("\t status: lookup pending\n"));
483 		return;
484 	case ST_DISCARD:
485 		(void) fprintf(stdout, gettext("\t status: discarded entry\n"));
486 		return;
487 	default:
488 		if (entry->stats.timestamp < now)
489 			(void) fprintf(stdout,
490 			gettext("\t status: expired (%d seconds ago)\n"),
491 			now - entry->stats.timestamp);
492 		else
493 			(void) fprintf(stdout,
494 			gettext("\t status: valid (expiry in %d seconds)\n"),
495 			entry->stats.timestamp - now);
496 		break;
497 	}
498 	(void) fprintf(stdout, gettext("\t hits: %u\n"), entry->stats.hits);
499 	args.key = entry->key;
500 	(void) nscdb->getlogstr(nscdb->name, whoami, sizeof (whoami), &args);
501 	(void) fprintf(stdout, "\t %s\n", whoami);
502 }
503 #endif	/* NSCD_DEBUG */
504 
505 static void
506 print_stats(nscd_cfg_stat_cache_t *statsp) {
507 
508 	(void) fprintf(stdout, gettext("\n\t STATISTICS:\n"));
509 	(void) fprintf(stdout, gettext("\t positive hits: %lu\n"),
510 			statsp->pos_hits);
511 	(void) fprintf(stdout, gettext("\t negative hits: %lu\n"),
512 			statsp->neg_hits);
513 	(void) fprintf(stdout, gettext("\t positive misses: %lu\n"),
514 			statsp->pos_misses);
515 	(void) fprintf(stdout, gettext("\t negative misses: %lu\n"),
516 			statsp->neg_misses);
517 	(void) fprintf(stdout, gettext("\t total entries: %lu\n"),
518 			statsp->entries);
519 	(void) fprintf(stdout, gettext("\t queries queued: %lu\n"),
520 			statsp->wait_count);
521 	(void) fprintf(stdout, gettext("\t queries dropped: %lu\n"),
522 			statsp->drop_count);
523 	(void) fprintf(stdout, gettext("\t cache invalidations: %lu\n"),
524 			statsp->invalidate_count);
525 
526 	_NSC_GET_HITRATE(statsp);
527 	(void) fprintf(stdout, gettext("\t cache hit rate: %10.1f\n"),
528 			statsp->hitrate);
529 }
530 
531 
532 static void
533 print_cfg(nscd_cfg_cache_t *cfgp) {
534 	(void) fprintf(stdout, gettext("\n\t CONFIG:\n"));
535 	(void) fprintf(stdout, gettext("\t enabled: %s\n"),
536 			yes_no(cfgp->enable));
537 	(void) fprintf(stdout, gettext("\t per user cache: %s\n"),
538 			yes_no(cfgp->per_user));
539 	(void) fprintf(stdout, gettext("\t avoid name service: %s\n"),
540 			yes_no(cfgp->avoid_ns));
541 	(void) fprintf(stdout, gettext("\t check file: %s\n"),
542 			yes_no(cfgp->check_files));
543 	(void) fprintf(stdout, gettext("\t check file interval: %d\n"),
544 			cfgp->check_interval);
545 	(void) fprintf(stdout, gettext("\t positive ttl: %d\n"),
546 			cfgp->pos_ttl);
547 	(void) fprintf(stdout, gettext("\t negative ttl: %d\n"),
548 			cfgp->neg_ttl);
549 	(void) fprintf(stdout, gettext("\t keep hot count: %d\n"),
550 			cfgp->keephot);
551 	(void) fprintf(stdout, gettext("\t hint size: %d\n"),
552 			cfgp->hint_size);
553 	(void) fprintf(stdout, gettext("\t max entries: %lu%s"),
554 			cfgp->maxentries,
555 			cfgp->maxentries?"\n":" (unlimited)\n");
556 }
557 
558 
559 #ifdef	NSCD_DEBUG
560 static void
561 hash_dump(nsc_db_t *nscdb, time_t now) {
562 	nsc_entry_t	*entry;
563 	int		i;
564 
565 	(void) fprintf(stdout, gettext("\n\nHASH TABLE:\n"));
566 	for (i = 0; i < nscdb->htsize; i++) {
567 		if ((entry = nscdb->htable[i]) != NULL) {
568 			(void) fprintf(stdout, "hash[%d]:\n", i);
569 			print_entry(nscdb, now, entry);
570 		}
571 	}
572 }
573 #endif	/* NSCD_DEBUG */
574 
575 
576 #ifdef	NSCD_DEBUG
577 static void
578 avl_dump(nsc_db_t *nscdb, time_t now) {
579 	nsc_entry_t	*entry;
580 	int		i;
581 
582 	(void) fprintf(stdout, gettext("\n\nAVL TREE:\n"));
583 	for (entry = avl_first(&nscdb->tree), i = 0; entry != NULL;
584 			entry = avl_walk(&nscdb->tree, entry, AVL_AFTER)) {
585 		(void) fprintf(stdout, "avl node[%d]:\n", i++);
586 		print_entry(nscdb, now, entry);
587 	}
588 }
589 #endif	/* NSCD_DEBUG */
590 
591 
592 #ifdef	NSCD_DEBUG
593 static void
594 queue_dump(nsc_db_t *nscdb, time_t now) {
595 	nsc_entry_t	*entry;
596 	int		i;
597 
598 	(void) fprintf(stdout,
599 		gettext("\n\nCACHE [name=%s, nodes=%lu]:\n"),
600 		nscdb->name, avl_numnodes(&nscdb->tree));
601 
602 	(void) fprintf(stdout,
603 		gettext("Starting with the most recently accessed:\n"));
604 
605 	for (entry = nscdb->qtail, i = 0; entry; entry = entry->qnext) {
606 		(void) fprintf(stdout, "entry[%d]:\n", i++);
607 		print_entry(nscdb, now, entry);
608 	}
609 }
610 #endif	/* NSCD_DEBUG */
611 
612 static void
613 queue_remove(nsc_db_t *nscdb, nsc_entry_t *entry) {
614 
615 	if (nscdb->qtail == entry)
616 		nscdb->qtail = entry->qnext;
617 	else
618 		entry->qprev->qnext = entry->qnext;
619 
620 	if (nscdb->qhead == entry)
621 		nscdb->qhead = entry->qprev;
622 	else
623 		entry->qnext->qprev = entry->qprev;
624 
625 	if (nscdb->reap_node == entry)
626 		nscdb->reap_node = entry->qnext;
627 	entry->qnext = entry->qprev = NULL;
628 }
629 
630 
631 static void
632 queue_adjust(nsc_db_t *nscdb, nsc_entry_t *entry) {
633 
634 #ifdef NSCD_DEBUG
635 	assert(nscdb->qtail || entry->qnext == NULL &&
636 			entry->qprev == NULL);
637 
638 	assert(nscdb->qtail && nscdb->qhead ||
639 		nscdb->qtail == NULL && nscdb->qhead == NULL);
640 
641 	assert(entry->qprev || entry->qnext == NULL ||
642 		nscdb->qtail == entry);
643 #endif /* NSCD_DEBUG */
644 
645 	/* already in the desired position */
646 	if (nscdb->qtail == entry)
647 		return;
648 
649 	/* new queue */
650 	if (nscdb->qtail == NULL) {
651 		nscdb->qhead = nscdb->qtail = entry;
652 		return;
653 	}
654 
655 	/* new entry (prev == NULL AND tail != entry) */
656 	if (entry->qprev == NULL) {
657 		nscdb->qtail->qprev = entry;
658 		entry->qnext = nscdb->qtail;
659 		nscdb->qtail = entry;
660 		return;
661 	}
662 
663 	/* existing entry */
664 	if (nscdb->reap_node == entry)
665 		nscdb->reap_node = entry->qnext;
666 	if (nscdb->qhead == entry)
667 		nscdb->qhead = entry->qprev;
668 	else
669 		entry->qnext->qprev = entry->qprev;
670 	entry->qprev->qnext = entry->qnext;
671 	entry->qprev = NULL;
672 	entry->qnext = nscdb->qtail;
673 	nscdb->qtail->qprev = entry;
674 	nscdb->qtail = entry;
675 }
676 
677 
678 /*
679  * Init cache
680  */
681 nscd_rc_t
682 init_cache(int debug_level) {
683 	int cflags;
684 
685 	cflags = (debug_level > 0)?0:UMC_NODEBUG;
686 	nsc_entry_cache = umem_cache_create("nsc_entry_cache",
687 				sizeof (nsc_entry_t), 0, NULL, NULL, NULL,
688 				NULL, NULL, cflags);
689 	if (nsc_entry_cache == NULL)
690 		return (NSCD_NO_MEMORY);
691 	return (NSCD_SUCCESS);
692 }
693 
694 
695 /*
696  * Create cache
697  */
698 nsc_db_t *
699 make_cache(enum db_type dbtype, int dbop, char *name,
700 		int (*compar) (const void *, const void *),
701 		void (*getlogstr)(char *, char *, size_t, nss_XbyY_args_t *),
702 		uint_t (*gethash)(nss_XbyY_key_t *, int),
703 		enum hash_type httype, int htsize) {
704 
705 	nsc_db_t	*nscdb;
706 	char		*me = "make_cache";
707 
708 	nscdb = (nsc_db_t *)malloc(sizeof (*nscdb));
709 	if (nscdb == NULL) {
710 		_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_ERROR)
711 		(me, "%s: memory allocation failure\n", name);
712 		goto out;
713 	}
714 	(void) memset(nscdb, 0, sizeof (*nscdb));
715 
716 	nscdb->dbop = dbop;
717 	nscdb->name = name;
718 	nscdb->db_type = dbtype;
719 
720 	/* Assign compare routine */
721 	if (compar == NULL) {
722 		if (_NSC_DB_CES_KEY(nscdb))
723 			nscdb->compar = nsc_db_ces_key_compar;
724 		else if (_NSC_DB_CIS_KEY(nscdb))
725 			nscdb->compar = nsc_db_cis_key_compar;
726 		else if (_NSC_DB_INT_KEY(nscdb))
727 			nscdb->compar = nsc_db_int_key_compar;
728 		else
729 			assert(0);
730 	} else {
731 		nscdb->compar = compar;
732 	}
733 
734 	/* The cache is an AVL tree */
735 	avl_create(&nscdb->tree, nscdb->compar, sizeof (nsc_entry_t),
736 			offsetof(nsc_entry_t, avl_link));
737 
738 	/* Assign log routine */
739 	if (getlogstr == NULL) {
740 		if (_NSC_DB_STR_KEY(nscdb))
741 			nscdb->getlogstr = nsc_db_str_key_getlogstr;
742 		else if (_NSC_DB_INT_KEY(nscdb))
743 			nscdb->getlogstr = nsc_db_int_key_getlogstr;
744 		else
745 			nscdb->getlogstr = nsc_db_any_key_getlogstr;
746 	} else {
747 		nscdb->getlogstr = getlogstr;
748 	}
749 
750 	/* The AVL tree based cache uses a hash table for quick access */
751 	if (htsize != 0) {
752 		/* Determine hash table size based on type */
753 		nscdb->hash_type = httype;
754 		if (htsize < 0) {
755 			switch (httype) {
756 			case nsc_ht_power2:
757 				htsize = _NSC_INIT_HTSIZE_POWER2;
758 				break;
759 			case nsc_ht_prime:
760 			case nsc_ht_default:
761 			default:
762 				htsize = _NSC_INIT_HTSIZE_PRIME;
763 			}
764 		}
765 		nscdb->htsize = htsize;
766 
767 		/* Create the hash table */
768 		nscdb->htable = calloc(htsize, sizeof (*(nscdb->htable)));
769 		if (nscdb->htable == NULL) {
770 			_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_ERROR)
771 			(me, "%s: memory allocation failure\n", name);
772 			goto out;
773 		}
774 
775 		/* Assign gethash routine */
776 		if (gethash == NULL) {
777 			if (_NSC_DB_CES_KEY(nscdb))
778 				nscdb->gethash = nsc_db_ces_key_gethash;
779 			else if (_NSC_DB_CIS_KEY(nscdb))
780 				nscdb->gethash = nsc_db_cis_key_gethash;
781 			else if (_NSC_DB_INT_KEY(nscdb))
782 				nscdb->gethash = nsc_db_int_key_gethash;
783 			else
784 				assert(0);
785 		} else {
786 			nscdb->gethash = gethash;
787 		}
788 	}
789 
790 	(void) mutex_init(&nscdb->db_mutex, USYNC_THREAD, NULL);
791 	return (nscdb);
792 
793 out:
794 	if (nscdb->htable)
795 		free(nscdb->htable);
796 	if (nscdb)
797 		free(nscdb);
798 	return (NULL);
799 }
800 
801 
802 /*
803  * verify
804  */
805 /* ARGSUSED */
806 nscd_rc_t
807 _nscd_cfg_cache_verify(
808 	void				*data,
809 	struct nscd_cfg_param_desc	*pdesc,
810 	nscd_cfg_id_t			*nswdb,
811 	nscd_cfg_flag_t			dflag,
812 	nscd_cfg_error_t		**errorp,
813 	void				**cookie)
814 {
815 
816 	return (NSCD_SUCCESS);
817 }
818 
819 /*
820  * notify
821  */
822 /* ARGSUSED */
823 nscd_rc_t
824 _nscd_cfg_cache_notify(
825 	void				*data,
826 	struct nscd_cfg_param_desc	*pdesc,
827 	nscd_cfg_id_t			*nswdb,
828 	nscd_cfg_flag_t			dflag,
829 	nscd_cfg_error_t		**errorp,
830 	void				**cookie)
831 {
832 	nsc_ctx_t	*ctx;
833 	void		*dp;
834 	int		i;
835 
836 	/* group data */
837 	if (_nscd_cfg_flag_is_set(dflag, NSCD_CFG_DFLAG_GROUP)) {
838 		if (_nscd_cfg_flag_is_set(pdesc->pflag,
839 				NSCD_CFG_PFLAG_GLOBAL)) {
840 			/* global config */
841 			global_cfg = *(nscd_cfg_global_cache_t *)data;
842 		} else if (_nscd_cfg_flag_is_set(dflag,
843 				NSCD_CFG_DFLAG_SET_ALL_DB)) {
844 			/* non-global config for all dbs */
845 			for (i = 0; i < CACHE_CTX_COUNT; i++) {
846 				ctx = cache_ctx_p[i];
847 				if (ctx == NULL)
848 					return (NSCD_CTX_NOT_FOUND);
849 				(void) rw_wrlock(&ctx->cfg_rwlp);
850 				ctx->cfg = *(nscd_cfg_cache_t *)data;
851 				ctx->cfg_mtime = time(NULL);
852 				(void) rw_unlock(&ctx->cfg_rwlp);
853 			}
854 		} else {
855 			/* non-global config for a specific db */
856 
857 			/* ignore non-caching databases */
858 			if (get_cache_ctx(nswdb->name, &ctx) !=
859 				NSCD_SUCCESS)
860 				return (NSCD_SUCCESS);
861 			(void) rw_wrlock(&ctx->cfg_rwlp);
862 			ctx->cfg = *(nscd_cfg_cache_t *)data;
863 			ctx->cfg_mtime = time(NULL);
864 			(void) rw_unlock(&ctx->cfg_rwlp);
865 		}
866 		return (NSCD_SUCCESS);
867 	}
868 
869 	/* individual data */
870 	if (_nscd_cfg_flag_is_set(pdesc->pflag,
871 				NSCD_CFG_PFLAG_GLOBAL)) {
872 		/* global config */
873 		dp = (char *)&global_cfg + pdesc->p_offset;
874 		(void) memcpy(dp, data, pdesc->p_size);
875 	} else if (_nscd_cfg_flag_is_set(dflag,
876 			NSCD_CFG_DFLAG_SET_ALL_DB)) {
877 		/* non-global config for all dbs */
878 		for (i = 0; i < CACHE_CTX_COUNT; i++) {
879 			ctx = cache_ctx_p[i];
880 			if (ctx == NULL)
881 				return (NSCD_CTX_NOT_FOUND);
882 			dp = (char *)&ctx->cfg + pdesc->p_offset;
883 			(void) rw_wrlock(&ctx->cfg_rwlp);
884 			(void) memcpy(dp, data, pdesc->p_size);
885 			ctx->cfg_mtime = time(NULL);
886 			(void) rw_unlock(&ctx->cfg_rwlp);
887 		}
888 	} else {
889 		/* non-global config for a specific db */
890 
891 		/* ignore non-caching databases */
892 		if (get_cache_ctx(nswdb->name, &ctx) != NSCD_SUCCESS)
893 			return (NSCD_SUCCESS);
894 		dp = (char *)&ctx->cfg + pdesc->p_offset;
895 		(void) rw_wrlock(&ctx->cfg_rwlp);
896 		(void) memcpy(dp, data, pdesc->p_size);
897 		ctx->cfg_mtime = time(NULL);
898 		(void) rw_unlock(&ctx->cfg_rwlp);
899 	}
900 	return (NSCD_SUCCESS);
901 }
902 
903 
904 /*
905  * get stat
906  */
907 /* ARGSUSED */
908 nscd_rc_t
909 _nscd_cfg_cache_get_stat(
910 	void				**stat,
911 	struct nscd_cfg_stat_desc	*sdesc,
912 	nscd_cfg_id_t			*nswdb,
913 	nscd_cfg_flag_t			*dflag,
914 	void				(**free_stat)(void *stat),
915 	nscd_cfg_error_t		**errorp)
916 {
917 	nscd_cfg_stat_cache_t	*statsp, stats;
918 	nsc_ctx_t		*ctx;
919 	int			i;
920 	nscd_rc_t		rc;
921 
922 	statsp = calloc(1, sizeof (*statsp));
923 	if (statsp == NULL)
924 		return (NSCD_NO_MEMORY);
925 
926 	if (_nscd_cfg_flag_is_set(sdesc->sflag, NSCD_CFG_SFLAG_GLOBAL)) {
927 		for (i = 0; i < CACHE_CTX_COUNT; i++) {
928 			if (cache_ctx_p[i] == NULL)
929 				stats = null_stats;
930 			else {
931 				(void) mutex_lock(&cache_ctx_p[i]->stats_mutex);
932 				stats = cache_ctx_p[i]->stats;
933 				(void) mutex_unlock(
934 					&cache_ctx_p[i]->stats_mutex);
935 			}
936 			statsp->pos_hits += stats.pos_hits;
937 			statsp->neg_hits += stats.neg_hits;
938 			statsp->pos_misses += stats.pos_misses;
939 			statsp->neg_misses += stats.neg_misses;
940 			statsp->entries += stats.entries;
941 			statsp->drop_count += stats.drop_count;
942 			statsp->wait_count += stats.wait_count;
943 			statsp->invalidate_count +=
944 				stats.invalidate_count;
945 		}
946 	} else {
947 		if ((rc = get_cache_ctx(nswdb->name, &ctx)) != NSCD_SUCCESS) {
948 			free(statsp);
949 			return (rc);
950 		}
951 		(void) mutex_lock(&ctx->stats_mutex);
952 		*statsp = ctx->stats;
953 		(void) mutex_unlock(&ctx->stats_mutex);
954 	}
955 
956 	_NSC_GET_HITRATE(statsp);
957 	*stat = statsp;
958 	return (NSCD_SUCCESS);
959 }
960 
961 /*
962  * This function should only be called when nscd is
963  * not a daemon.
964  */
965 void
966 nsc_info(nsc_ctx_t *ctx, char *dbname, nscd_cfg_cache_t cfg[],
967 	nscd_cfg_stat_cache_t stats[])
968 {
969 	int		i;
970 	char		*me = "nsc_info";
971 	nsc_ctx_t	*ctx1;
972 	nsc_ctx_t	ctx2;
973 	nscd_rc_t	rc;
974 
975 	if (ctx) {
976 		ctx_info(ctx);
977 		return;
978 	}
979 
980 	if (dbname) {
981 		rc = get_cache_ctx(dbname, &ctx1);
982 		if (rc == NSCD_INVALID_ARGUMENT) {
983 			_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_WARNING)
984 			(me, "%s: no cache context found\n", dbname);
985 			return;
986 		} else if (rc == NSCD_NO_MEMORY) {
987 			_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_WARNING)
988 	(me, "%s: unable to create cache context - no memory\n",
989 				dbname);
990 			return;
991 		}
992 		ctx_info(ctx1);
993 		return;
994 	}
995 
996 	if (cfg == NULL || stats == NULL)
997 		return;
998 
999 	for (i = 0; i < CACHE_CTX_COUNT; i++) {
1000 
1001 		ctx2.dbname = cache_name[i];
1002 		ctx2.cfg = cfg[i];
1003 		ctx2.stats = stats[i];
1004 		ctx_info_nolock(&ctx2);
1005 	}
1006 }
1007 
1008 static void
1009 ctx_info_nolock(nsc_ctx_t *ctx) {
1010 	nscd_cfg_cache_t	cfg;
1011 	nscd_cfg_stat_cache_t	stats;
1012 
1013 	cfg = ctx->cfg;
1014 	(void) fprintf(stdout, gettext("\n\nCACHE: %s\n"), ctx->dbname);
1015 	(void) print_cfg(&cfg);
1016 
1017 	if (cfg.enable == nscd_false)
1018 		return;
1019 
1020 	stats = ctx->stats;
1021 	(void) print_stats(&stats);
1022 }
1023 
1024 static void
1025 ctx_info(nsc_ctx_t *ctx) {
1026 	nscd_cfg_cache_t	cfg;
1027 	nscd_cfg_stat_cache_t	stats;
1028 
1029 	(void) rw_rdlock(&ctx->cfg_rwlp);
1030 	cfg = ctx->cfg;
1031 	(void) rw_unlock(&ctx->cfg_rwlp);
1032 	(void) fprintf(stdout, gettext("\n\nCACHE: %s\n"), ctx->dbname);
1033 	(void) print_cfg(&cfg);
1034 
1035 	if (cfg.enable == nscd_false)
1036 		return;
1037 
1038 	(void) mutex_lock(&ctx->stats_mutex);
1039 	stats = ctx->stats;
1040 	(void) mutex_unlock(&ctx->stats_mutex);
1041 	(void) print_stats(&stats);
1042 }
1043 
1044 #ifdef	NSCD_DEBUG
1045 /*
1046  * This function should only be called when nscd is
1047  * not a daemon.
1048  */
1049 int
1050 nsc_dump(char *dbname, int dbop) {
1051 	nsc_ctx_t	*ctx;
1052 	nsc_db_t	*nscdb;
1053 	nscd_bool_t	enabled;
1054 	time_t		now;
1055 	char		*me = "nsc_dump";
1056 	int		i;
1057 
1058 	if ((i = get_cache_idx(dbname)) == -1) {
1059 		(void) fprintf(stdout, gettext("invalid cache name\n"));
1060 
1061 		_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_WARNING)
1062 		(me, "%s: invalid cache name\n", dbname);
1063 		return (NSCD_CACHE_INVALID_CACHE_NAME);
1064 	}
1065 
1066 	if ((ctx = cache_ctx_p[i]) == NULL)  {
1067 		(void) fprintf(stdout, gettext("no cache context\n"));
1068 
1069 		_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_WARNING)
1070 		(me, "%s: no cache context\n", dbname);
1071 		return (NSCD_CACHE_NO_CACHE_CTX);
1072 	}
1073 
1074 	now = time(NULL);
1075 	(void) rw_rdlock(&ctx->cfg_rwlp);
1076 	enabled = ctx->cfg.enable;
1077 	(void) rw_unlock(&ctx->cfg_rwlp);
1078 
1079 	if (enabled == nscd_false)
1080 		return (NSCD_CACHE_DISABLED);
1081 
1082 	nscdb = nsc_get_db(ctx, dbop);
1083 	if (nscdb == NULL) {
1084 		_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_WARNING)
1085 		(me, "%s:%d: no cache found\n", dbname, dbop);
1086 		return (NSCD_CACHE_NO_CACHE_FOUND);
1087 	}
1088 
1089 	(void) mutex_lock(&nscdb->db_mutex);
1090 	(void) queue_dump(nscdb, now);
1091 	(void) hash_dump(nscdb, now);
1092 	(void) avl_dump(nscdb, now);
1093 	(void) mutex_unlock(&nscdb->db_mutex);
1094 	return (NSCD_SUCCESS);
1095 }
1096 #endif	/* NSCD_DEBUG */
1097 
1098 /*
1099  * These macros are for exclusive use of nsc_lookup
1100  */
1101 #define	NSC_LOOKUP_RETURN(retcode, loglevel, fmt) \
1102 	(void) mutex_unlock(&nscdb->db_mutex); \
1103 	_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_##loglevel) \
1104 		(me, fmt, whoami); \
1105 	return (retcode);
1106 
1107 #define	NSC_LOOKUP_NO_CACHE(str) \
1108 	_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_DEBUG) \
1109 		(me, "%s: name service lookup (bypassing cache\n", \
1110 		str); \
1111 	nss_psearch(largs->buffer, largs->bufsize); \
1112 	status = NSCD_GET_STATUS(largs->buffer); \
1113 	_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_DEBUG) \
1114 		(me, "%s: name service lookup status = %d\n", \
1115 		str, status); \
1116 	if (status == NSS_SUCCESS) { \
1117 		return (SUCCESS); \
1118 	} else if (status == NSS_NOTFOUND) \
1119 		return (NOTFOUND); \
1120 	else \
1121 		return (SERVERERROR);
1122 
1123 /*
1124  * This function starts the revalidation and reaper threads
1125  * for a cache
1126  */
1127 static void
1128 start_threads(nsc_ctx_t *ctx) {
1129 
1130 	int	errnum;
1131 	char	*me = "start_threads";
1132 
1133 	/*
1134 	 *  kick off the revalidate thread (if necessary)
1135 	 */
1136 	if (ctx->revalidate_on != nscd_true) {
1137 		if (thr_create(NULL, NULL, (void *(*)(void *))revalidate,
1138 			ctx, 0, NULL) != 0) {
1139 			errnum = errno;
1140 			_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_ERROR)
1141 		(me, "thr_create (revalidate thread for %s): %s\n",
1142 			ctx->dbname, strerror(errnum));
1143 			exit(1);
1144 		}
1145 		ctx->revalidate_on = nscd_true;
1146 	}
1147 
1148 	/*
1149 	 *  kick off the reaper thread (if necessary)
1150 	 */
1151 	if (ctx->reaper_on != nscd_true) {
1152 		if (thr_create(NULL, NULL, (void *(*)(void *))reaper,
1153 			ctx, 0, NULL) != 0) {
1154 			errnum = errno;
1155 			_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_ERROR)
1156 		(me, "thr_create (reaper thread for %s): %s\n",
1157 			ctx->dbname, strerror(errnum));
1158 			exit(1);
1159 		}
1160 		ctx->reaper_on = nscd_true;
1161 	}
1162 }
1163 
1164 /*
1165  * Examine the packed buffer, see if the front-end parameters
1166  * indicate that the caller specified nsswitch config should be
1167  * used for the lookup. Return 1 if yes, otherwise 0.
1168  */
1169 static int
1170 nsw_config_in_phdr(void *buf)
1171 {
1172 	nss_pheader_t		*pbuf = (nss_pheader_t *)buf;
1173 	nssuint_t		off;
1174 	nss_dbd_t		*pdbd;
1175 	char			*me = "nsw_config_in_phdr";
1176 
1177 	off = pbuf->dbd_off;
1178 	if (off == 0)
1179 		return (0);
1180 	pdbd = (nss_dbd_t *)((void *)((char *)pbuf + off));
1181 	if (pdbd->o_default_config == 0)
1182 		return (0);
1183 
1184 	if ((enum nss_dbp_flags)pdbd->flags & NSS_USE_DEFAULT_CONFIG) {
1185 		_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_DEBUG)
1186 		(me, "use caller specified nsswitch config\n");
1187 		return (1);
1188 	} else
1189 		return (0);
1190 }
1191 
1192 static nss_status_t
1193 copy_result(void *rbuf, void *cbuf)
1194 {
1195 	nss_pheader_t	*rphdr = (nss_pheader_t *)rbuf;
1196 	nss_pheader_t	*cphdr = (nss_pheader_t *)cbuf;
1197 	char		*me = "copy_result";
1198 
1199 	/* return NSS_ERROR if not enough room to copy result */
1200 	if (cphdr->data_len + 1 > rphdr->data_len) {
1201 		NSCD_SET_STATUS(rphdr, NSS_ERROR, ERANGE);
1202 		return (NSS_ERROR);
1203 	} else {
1204 		char	*dst;
1205 
1206 		if (cphdr->data_len == 0)
1207 			return (NSS_SUCCESS);
1208 
1209 		dst = (char *)rphdr + rphdr->data_off;
1210 		(void) memcpy(dst, (char *)cphdr + cphdr->data_off,
1211 			cphdr->data_len);
1212 		rphdr->data_len = cphdr->data_len;
1213 		/* some frontend code expects a terminating NULL char */
1214 		*(dst + rphdr->data_len) = '\0';
1215 
1216 		_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_DEBUG)
1217 		(me, "cache data (len = %lld): >>%s<<\n",
1218 		cphdr->data_len, (char *)cphdr + cphdr->data_off);
1219 
1220 		return (NSS_SUCCESS);
1221 	}
1222 }
1223 
1224 static int
1225 get_dns_ttl(void *pbuf, char *dbname)
1226 {
1227 	nss_pheader_t	*phdr = (nss_pheader_t *)pbuf;
1228 	int		ttl;
1229 	char		*me = "get_dns_ttl";
1230 
1231 	/* if returned, dns ttl is stored in the extended data area */
1232 	if (phdr->ext_off == 0)
1233 		return (-1);
1234 
1235 	if (strcmp(dbname, NSS_DBNAM_HOSTS) != 0 &&
1236 		strcmp(dbname, NSS_DBNAM_IPNODES) != 0)
1237 		return (-1);
1238 
1239 	ttl = *(nssuint_t *)((void *)((char *)pbuf + phdr->ext_off));
1240 
1241 	_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_DEBUG)
1242 	(me, "dns ttl is %d seconds\n", ttl);
1243 
1244 	return (ttl);
1245 }
1246 
1247 static int
1248 check_config(nsc_lookup_args_t *largs, nscd_cfg_cache_t *cfgp,
1249 	char *whoami, int flag)
1250 {
1251 	nsc_db_t	*nscdb;
1252 	nsc_ctx_t	*ctx;
1253 	nss_status_t	status;
1254 	char		*me = "check_config";
1255 
1256 	ctx = largs->ctx;
1257 	nscdb = largs->nscdb;
1258 
1259 	/* see if the cached config needs update */
1260 	if (nscdb->cfg_mtime != ctx->cfg_mtime) {
1261 		(void) rw_rdlock(&ctx->cfg_rwlp);
1262 		nscdb->cfg = ctx->cfg;
1263 		nscdb->cfg_mtime = ctx->cfg_mtime;
1264 		(void) rw_unlock(&ctx->cfg_rwlp);
1265 		_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_DEBUG)
1266 		(me, "config for context %s, database %s updated\n",
1267 			ctx->dbname, nscdb->name);
1268 	}
1269 	*cfgp = nscdb->cfg;
1270 
1271 	if (cfgp->enable == nscd_false) {
1272 		_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_DEBUG)
1273 			(me, "%s: cache disabled\n", ctx->dbname);
1274 
1275 		if (UPDATEBIT & flag)
1276 			return (NOTFOUND);
1277 		else {
1278 			NSC_LOOKUP_NO_CACHE(whoami);
1279 		}
1280 	}
1281 
1282 	/*
1283 	 * if caller requests lookup using his
1284 	 * own nsswitch config, bypass cache
1285 	 */
1286 	if (nsw_config_in_phdr(largs->buffer)) {
1287 		NSC_LOOKUP_NO_CACHE(whoami);
1288 	}
1289 
1290 	/* no need of cache if we are dealing with 0 ttls */
1291 	if (cfgp->pos_ttl <= 0 && cfgp->neg_ttl <= 0) {
1292 		if (flag & UPDATEBIT)
1293 			return (NOTFOUND);
1294 		else if (cfgp->avoid_ns == nscd_true)
1295 			return (SERVERERROR);
1296 		NSC_LOOKUP_NO_CACHE(whoami);
1297 	}
1298 
1299 	return (CONTINUE);
1300 }
1301 
1302 /*
1303  * Invalidate cache if database file has been modified.
1304  * See check_files config param for details.
1305  */
1306 static void
1307 check_db_file(nsc_ctx_t *ctx, nscd_cfg_cache_t cfg,
1308 	char *whoami, time_t now)
1309 {
1310 	struct stat	buf;
1311 	nscd_bool_t	file_modified = nscd_false;
1312 	char		*me = "check_db_file";
1313 
1314 	if (cfg.check_interval != 0 &&
1315 	    (now - ctx->file_chktime) < cfg.check_interval)
1316 		return;
1317 
1318 	ctx->file_chktime = now;
1319 	if (stat(ctx->file_name, &buf) == 0) {
1320 		if (ctx->file_mtime == 0) {
1321 			(void) mutex_lock(&ctx->file_mutex);
1322 			if (ctx->file_mtime == 0) {
1323 				ctx->file_mtime = buf.st_mtime;
1324 				ctx->file_size = buf.st_size;
1325 				ctx->file_ino = buf.st_ino;
1326 			}
1327 			(void) mutex_unlock(&ctx->file_mutex);
1328 		} else if (ctx->file_mtime < buf.st_mtime ||
1329 				ctx->file_size != buf.st_size ||
1330 				ctx->file_ino != buf.st_ino) {
1331 			(void) mutex_lock(&ctx->file_mutex);
1332 			if (ctx->file_mtime < buf.st_mtime ||
1333 				ctx->file_size != buf.st_size ||
1334 				ctx->file_ino != buf.st_ino) {
1335 				file_modified = nscd_true;
1336 				ctx->file_mtime = buf.st_mtime;
1337 				ctx->file_size = buf.st_size;
1338 				ctx->file_ino = buf.st_ino;
1339 			}
1340 			(void) mutex_unlock(&ctx->file_mutex);
1341 		}
1342 	}
1343 
1344 	if (file_modified == nscd_true) {
1345 		_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_DEBUG)
1346 	(me, "%s: file %s has been modified - invalidating cache\n",
1347 		whoami, ctx->file_name);
1348 		ctx_invalidate(ctx);
1349 	}
1350 }
1351 
1352 static int
1353 lookup_int(nsc_lookup_args_t *largs, int flag) {
1354 
1355 	nsc_ctx_t		*ctx;
1356 	nsc_db_t		*nscdb;
1357 	nscd_cfg_cache_t	cfg;
1358 	nsc_entry_t		*this_entry;
1359 	nsc_entry_stat_t	*this_stats;
1360 	nsc_action_t		next_action;
1361 	nss_status_t		status;
1362 	nscd_bool_t		delete;
1363 	nscd_rc_t		rc;
1364 	char			*dbname;
1365 	int			dbop, errnum;
1366 	int			cfg_rc;
1367 	nss_XbyY_args_t		args;
1368 	char			whoami[128];
1369 	time_t			now = time(NULL); /* current time */
1370 	char			*me = "lookup_int";
1371 
1372 	/* extract dbop, dbname, key and cred */
1373 	status = nss_packed_getkey(largs->buffer, largs->bufsize, &dbname,
1374 				&dbop, &args);
1375 	if (status != NSS_SUCCESS) {
1376 		_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_ERROR)
1377 			(me, "nss_packed_getkey failure (%d)\n", status);
1378 		return (SERVERERROR);
1379 	}
1380 
1381 	/* get the cache context */
1382 	if (largs->ctx == NULL) {
1383 		if (get_cache_ctx(dbname, &largs->ctx) != NSCD_SUCCESS) {
1384 			_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_WARNING)
1385 				(me, "%s: no cache context found\n", dbname);
1386 
1387 			if (UPDATEBIT & flag)
1388 				return (NOTFOUND);
1389 			else {
1390 				NSC_LOOKUP_NO_CACHE(dbname);
1391 			}
1392 		}
1393 	}
1394 	ctx = largs->ctx;
1395 
1396 	if (largs->nscdb == NULL) {
1397 		if ((largs->nscdb = nsc_get_db(ctx, dbop)) == NULL) {
1398 			_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_WARNING)
1399 				(me, "%s:%d: no cache found\n",
1400 				dbname, dbop);
1401 
1402 			if (UPDATEBIT & flag)
1403 				return (NOTFOUND);
1404 			else {
1405 				NSC_LOOKUP_NO_CACHE(dbname);
1406 			}
1407 		}
1408 	}
1409 
1410 	nscdb = largs->nscdb;
1411 
1412 	_NSCD_LOG_IF(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_ALL) {
1413 		(void) nscdb->getlogstr(nscdb->name, whoami,
1414 			sizeof (whoami), &args);
1415 	}
1416 
1417 	if (UPDATEBIT & flag) {
1418 		_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_DEBUG)
1419 			(me, "%s: refresh start\n", whoami);
1420 	} else {
1421 		_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_DEBUG)
1422 			(me, "%s: lookup start\n", whoami);
1423 	}
1424 
1425 	cfg_rc = check_config(largs, &cfg, whoami, flag);
1426 	if (cfg_rc != CONTINUE)
1427 		return (cfg_rc);
1428 
1429 	/*
1430 	 * Invalidate cache if file has been modified.
1431 	 */
1432 	if (cfg.check_files == nscd_true)
1433 		check_db_file(ctx, cfg, whoami, now);
1434 
1435 	(void) mutex_lock(&nscdb->db_mutex);
1436 
1437 	/* Lookup the cache table */
1438 	for (;;) {
1439 		delete = nscd_false;
1440 		rc = lookup_cache(largs, &cfg, &args, whoami, &this_entry);
1441 		if (rc != NSCD_SUCCESS) {
1442 			(void) mutex_unlock(&nscdb->db_mutex);
1443 
1444 			/* Either no entry and avoid name service */
1445 			if (rc == NSCD_DB_ENTRY_NOT_FOUND ||
1446 					rc == NSCD_INVALID_ARGUMENT)
1447 				return (NOTFOUND);
1448 
1449 			/* OR memory error */
1450 			return (SERVERERROR);
1451 		}
1452 
1453 		/* get the stats from the entry */
1454 		this_stats = &this_entry->stats;
1455 
1456 		/*
1457 		 * What should we do next ?
1458 		 */
1459 		switch (this_stats->status) {
1460 		case ST_NEW_ENTRY:
1461 			delete = nscd_true;
1462 			next_action = _NSC_NSLOOKUP;
1463 			break;
1464 		case ST_UPDATE_PENDING:
1465 			if (flag & UPDATEBIT) {
1466 				(void) mutex_unlock(&nscdb->db_mutex);
1467 				return (NOTFOUND);
1468 			} else if (this_stats->timestamp < now)
1469 				next_action = _NSC_WAIT;
1470 			else
1471 				next_action = _NSC_USECACHED;
1472 			break;
1473 		case ST_LOOKUP_PENDING:
1474 			if (flag & UPDATEBIT) {
1475 				(void) mutex_unlock(&nscdb->db_mutex);
1476 				return (NOTFOUND);
1477 			}
1478 			next_action = _NSC_WAIT;
1479 			break;
1480 		case ST_DISCARD:
1481 			if (cfg.avoid_ns == nscd_true) {
1482 				(void) mutex_unlock(&nscdb->db_mutex);
1483 				return (NOTFOUND);
1484 			}
1485 			/* otherwise reuse the entry */
1486 			(void) memset(this_stats, 0, sizeof (*this_stats));
1487 			next_action = _NSC_NSLOOKUP;
1488 			break;
1489 		default:
1490 			if (cfg.avoid_ns == nscd_true)
1491 				next_action = _NSC_USECACHED;
1492 			else if ((flag & UPDATEBIT) ||
1493 					(this_stats->timestamp < now)) {
1494 				_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_DEBUG)
1495 			(me, "%s: cached entry needs to be updated\n",
1496 				whoami);
1497 				next_action = _NSC_NSLOOKUP;
1498 			} else
1499 				next_action = _NSC_USECACHED;
1500 			break;
1501 		}
1502 
1503 		if (next_action == _NSC_WAIT) {
1504 			_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_DEBUG)
1505 			(me, "%s: need to wait\n", whoami);
1506 
1507 			/* do we have clearance ? */
1508 			if (_nscd_get_clearance(&ctx->throttle_sema) != 0) {
1509 				/* nope. quit */
1510 				(void) mutex_lock(&ctx->stats_mutex);
1511 				ctx->stats.drop_count++;
1512 				(void) mutex_unlock(&ctx->stats_mutex);
1513 				NSC_LOOKUP_RETURN(NOSERVER, WARNING,
1514 				"%s: no clearance to wait\n");
1515 			}
1516 			/* yes can wait */
1517 			(void) nscd_wait(ctx, nscdb, this_entry);
1518 			(void) _nscd_release_clearance(&ctx->throttle_sema);
1519 			continue;
1520 		}
1521 
1522 		break;
1523 	}
1524 
1525 
1526 	if (!(UPDATEBIT & flag))
1527 		this_stats->hits++;		/* update hit count */
1528 
1529 	if (next_action == _NSC_NSLOOKUP) {
1530 
1531 		_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_DEBUG)
1532 		(me, "%s: name service lookup required\n", whoami);
1533 
1534 		if (_nscd_get_clearance(&ctx->throttle_sema) != 0) {
1535 			if (delete == nscd_true)
1536 				delete_entry(nscdb, ctx, this_entry);
1537 			else
1538 				this_stats->status = ST_DISCARD;
1539 			(void) mutex_lock(&ctx->stats_mutex);
1540 			ctx->stats.drop_count++;
1541 			(void) mutex_unlock(&ctx->stats_mutex);
1542 			NSC_LOOKUP_RETURN(NOSERVER, WARNING,
1543 			"%s: no clearance for lookup\n");
1544 		}
1545 
1546 		/* block any threads accessing this entry */
1547 		this_stats->status = (flag & UPDATEBIT)?
1548 				ST_UPDATE_PENDING:ST_LOOKUP_PENDING;
1549 
1550 		/* release lock and do name service lookup */
1551 		(void) mutex_unlock(&nscdb->db_mutex);
1552 		nss_psearch(largs->buffer, largs->bufsize);
1553 		status = NSCD_GET_STATUS(largs->buffer);
1554 		(void) mutex_lock(&nscdb->db_mutex);
1555 		this_stats->status = 0;
1556 		(void) _nscd_release_clearance(&ctx->throttle_sema);
1557 
1558 		/* signal waiting threads */
1559 		(void) nscd_signal(ctx, nscdb, this_entry);
1560 
1561 		_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_DEBUG)
1562 		(me, "%s: name service lookup status = %d\n",
1563 			whoami, status);
1564 
1565 		if (status == NSS_SUCCESS) {
1566 			int ttl;
1567 
1568 			/*
1569 			 * data found in name service
1570 			 * update cache
1571 			 */
1572 			status = dup_packed_buffer(largs, this_entry);
1573 			if (status != NSS_SUCCESS) {
1574 				delete_entry(nscdb, ctx, this_entry);
1575 				NSC_LOOKUP_RETURN(SERVERERROR, ERROR,
1576 				"%s: failed to update cache\n");
1577 			}
1578 
1579 			/*
1580 			 * store unpacked key in cache
1581 			 */
1582 			status = nss_packed_getkey(this_entry->buffer,
1583 					this_entry->bufsize,
1584 					&dbname, &dbop, &args);
1585 			if (status != NSS_SUCCESS) {
1586 				delete_entry(nscdb, ctx, this_entry);
1587 				NSC_LOOKUP_RETURN(SERVERERROR, ERROR,
1588 				"%s: failed to extract key\n");
1589 			}
1590 			this_entry->key = args.key; /* struct copy */
1591 
1592 			/* update +ve miss count */
1593 			if (!(UPDATEBIT & flag)) {
1594 				(void) mutex_lock(&ctx->stats_mutex);
1595 				ctx->stats.pos_misses++;
1596 				(void) mutex_unlock(&ctx->stats_mutex);
1597 			}
1598 
1599 			/* update +ve ttl */
1600 			ttl = get_dns_ttl(largs->buffer, dbname);
1601 			/* honor the dns ttl less than postive ttl */
1602 			if (ttl < 0 || ttl > cfg.pos_ttl)
1603 				ttl = cfg.pos_ttl;
1604 			this_stats->timestamp = time(NULL) + ttl;
1605 
1606 			/*
1607 			 * start the revalidation and reaper threads
1608 			 * if not already started
1609 			 */
1610 			start_threads(ctx);
1611 
1612 			NSC_LOOKUP_RETURN(SUCCESS, DEBUG,
1613 			"%s: cache updated with positive entry\n");
1614 		} else if (status == NSS_NOTFOUND) {
1615 			/*
1616 			 * data not found in name service
1617 			 * update cache
1618 			 */
1619 
1620 			if (NSCD_GET_ERRNO(largs->buffer) == ERANGE) {
1621 				delete_entry(nscdb, ctx, this_entry);
1622 				NSC_LOOKUP_RETURN(NOTFOUND, DEBUG,
1623 		"%s: ERANGE, cache not updated with negative entry\n");
1624 			}
1625 
1626 			status = dup_packed_buffer(largs, this_entry);
1627 			if (status != NSS_SUCCESS) {
1628 				delete_entry(nscdb, ctx, this_entry);
1629 				NSC_LOOKUP_RETURN(SERVERERROR, ERROR,
1630 				"%s: failed to update cache\n");
1631 			}
1632 
1633 			/* store unpacked key in cache */
1634 			status = nss_packed_getkey(this_entry->buffer,
1635 					this_entry->bufsize,
1636 					&dbname, &dbop, &args);
1637 			if (status != NSS_SUCCESS) {
1638 				delete_entry(nscdb, ctx, this_entry);
1639 				NSC_LOOKUP_RETURN(SERVERERROR, ERROR,
1640 				"%s: failed to extract key\n");
1641 			}
1642 			this_entry->key = args.key; /* struct copy */
1643 
1644 			/* update -ve ttl */
1645 			this_stats->timestamp = time(NULL) + cfg.neg_ttl;
1646 
1647 			/* update -ve miss count */
1648 			if (!(UPDATEBIT & flag)) {
1649 				(void) mutex_lock(&ctx->stats_mutex);
1650 				ctx->stats.neg_misses++;
1651 				(void) mutex_unlock(&ctx->stats_mutex);
1652 			}
1653 
1654 			/*
1655 			 * start the revalidation and reaper threads
1656 			 * if not already started
1657 			 */
1658 			start_threads(ctx);
1659 
1660 			NSC_LOOKUP_RETURN(NOTFOUND, DEBUG,
1661 			"%s: cache updated with negative entry\n");
1662 		} else {
1663 			/*
1664 			 * name service lookup failed
1665 			 */
1666 			errnum = NSCD_GET_ERRNO(largs->buffer);
1667 			if (delete == nscd_true)
1668 				delete_entry(nscdb, ctx, this_entry);
1669 			else
1670 				this_stats->status = ST_DISCARD;
1671 
1672 			(void) mutex_unlock(&nscdb->db_mutex);
1673 			_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_WARNING)
1674 	(me, "%s: name service lookup failed (status=%d, errno=%d)\n",
1675 				whoami, status, errnum);
1676 
1677 			return (SERVERERROR);
1678 		}
1679 	} else if (next_action == _NSC_USECACHED) {
1680 		/*
1681 		 * found entry in cache
1682 		 */
1683 		if (UPDATEBIT & flag) {
1684 			NSC_LOOKUP_RETURN(SUCCESS, DEBUG,
1685 			"%s: no need to update\n");
1686 		}
1687 
1688 		if (NSCD_GET_STATUS((nss_pheader_t *)this_entry->buffer) ==
1689 			NSS_SUCCESS) {
1690 			/* positive hit */
1691 			(void) mutex_lock(&ctx->stats_mutex);
1692 			ctx->stats.pos_hits++;
1693 			(void) mutex_unlock(&ctx->stats_mutex);
1694 
1695 			/* update response buffer */
1696 			if (copy_result(largs->buffer,
1697 				this_entry->buffer) != NSS_SUCCESS) {
1698 				NSC_LOOKUP_RETURN(SERVERERROR, ERROR,
1699 				"%s: response buffer insufficient\n");
1700 			}
1701 
1702 			NSC_LOOKUP_RETURN(SUCCESS, DEBUG,
1703 			"%s: positive entry in cache\n");
1704 		} else {
1705 			/* negative hit */
1706 			(void) mutex_lock(&ctx->stats_mutex);
1707 			ctx->stats.neg_hits++;
1708 			(void) mutex_unlock(&ctx->stats_mutex);
1709 
1710 			NSCD_SET_STATUS((nss_pheader_t *)largs->buffer,
1711 				NSCD_GET_STATUS(this_entry->buffer),
1712 				NSCD_GET_ERRNO(this_entry->buffer));
1713 			NSCD_SET_HERRNO((nss_pheader_t *)largs->buffer,
1714 				NSCD_GET_HERRNO(this_entry->buffer));
1715 
1716 			NSC_LOOKUP_RETURN(NOTFOUND, DEBUG,
1717 			"%s: negative entry in cache\n");
1718 		}
1719 	}
1720 
1721 	NSC_LOOKUP_RETURN(SERVERERROR, ERROR,
1722 	"%s: cache backend failure\n");
1723 }
1724 
1725 /*
1726  * NSCD cache backend lookup function
1727  */
1728 /*ARGSUSED*/
1729 void
1730 nsc_lookup(nsc_lookup_args_t *largs, int flag) {
1731 
1732 	nss_pheader_t	*phdr = (nss_pheader_t *)largs->buffer;
1733 	int		rc;
1734 
1735 	rc = lookup_int(largs, 0);
1736 
1737 	if (NSCD_GET_STATUS(phdr) == NSS_TRYLOCAL)
1738 		return;
1739 
1740 	switch (rc) {
1741 
1742 	case SUCCESS:
1743 		NSCD_RETURN_STATUS(phdr, NSS_SUCCESS, 0);
1744 		break;
1745 
1746 	case NOTFOUND:
1747 		NSCD_RETURN_STATUS(phdr, NSS_NOTFOUND, -1);
1748 		break;
1749 
1750 	case SERVERERROR:
1751 		/*
1752 		 * status and errno should have been set in the phdr,
1753 		 * if not, set status to NSS_ERROR
1754 		 */
1755 		if (NSCD_STATUS_IS_OK(phdr)) {
1756 			NSCD_SET_STATUS(phdr, NSS_ERROR, 0);
1757 		}
1758 		break;
1759 
1760 	case NOSERVER:
1761 		NSCD_RETURN_STATUS(phdr, NSS_TRYLOCAL, -1);
1762 		break;
1763 	}
1764 }
1765 
1766 
1767 static nsc_ctx_t *
1768 init_cache_ctx(int i) {
1769 	nsc_ctx_t	*ctx;
1770 
1771 	ctx = calloc(1, sizeof (nsc_ctx_t));
1772 	if (ctx == NULL)
1773 		return (NULL);
1774 
1775 	/* init locks and semaphores */
1776 	(void) mutex_init(&ctx->file_mutex, USYNC_THREAD, NULL);
1777 	(void) rwlock_init(&ctx->cfg_rwlp, USYNC_THREAD, NULL);
1778 	(void) mutex_init(&ctx->stats_mutex, USYNC_THREAD, NULL);
1779 	(void) _nscd_init_cache_sema(&ctx->throttle_sema, cache_name[i]);
1780 	cache_init_ctx[i](ctx);
1781 	cache_ctx_p[i] = ctx;
1782 
1783 	return (ctx);
1784 }
1785 
1786 
1787 static void
1788 revalidate(nsc_ctx_t *ctx)
1789 {
1790 	for (;;) {
1791 		int 		i, slp, interval, count;
1792 
1793 		(void) rw_rdlock(&ctx->cfg_rwlp);
1794 		slp = ctx->cfg.pos_ttl;
1795 		count = ctx->cfg.keephot;
1796 		(void) rw_unlock(&ctx->cfg_rwlp);
1797 
1798 		if (slp < 60)
1799 			slp = 60;
1800 		if (count != 0) {
1801 			interval = (slp/2)/count;
1802 			if (interval == 0)
1803 				interval = 1;
1804 			(void) sleep(slp*2/3);
1805 			for (i = 0; i < ctx->db_count; i++) {
1806 				getxy_keepalive(ctx, ctx->nsc_db[i],
1807 						count, interval);
1808 			}
1809 		} else {
1810 			(void) sleep(slp);
1811 		}
1812 	}
1813 }
1814 
1815 
1816 static void
1817 getxy_keepalive(nsc_ctx_t *ctx, nsc_db_t *nscdb, int keep, int interval)
1818 {
1819 	nsc_keephot_t		*table;
1820 	nsc_entry_t		*entry, *ptr;
1821 	int			i;
1822 	nsc_lookup_args_t	*largs;
1823 	nss_pheader_t		*phdr;
1824 	int			bufsiz;
1825 	char			*me = "getxy_keepalive";
1826 
1827 	/* we won't be here if keep == 0 so need to check that */
1828 
1829 	_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_DEBUG)
1830 	(me, "%s: keep alive\n", nscdb->name);
1831 
1832 	if ((table = maken(keep)) == NULL) {
1833 		_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_ERROR)
1834 			(me, "memory allocation failure\n");
1835 		exit(1);
1836 	}
1837 
1838 	(void) mutex_lock(&nscdb->db_mutex);
1839 	entry = nscdb->qtail;
1840 	while (entry != NULL) {
1841 		/* leave pending calls alone */
1842 		if (!(entry->stats.status & ST_PENDING)) {
1843 			/* do_revalidate */
1844 			(void) insertn(table, entry->stats.hits,
1845 					entry);
1846 		}
1847 		entry = entry->qnext;
1848 	}
1849 	for (i = 1; i <= keep; i++) {
1850 		if (table[i].ptr == NULL)
1851 			continue;
1852 		ptr = (nsc_entry_t *)table[i].ptr;
1853 		phdr = (nss_pheader_t *)ptr->buffer;
1854 		if (NSCD_GET_STATUS(phdr) == NSS_SUCCESS)
1855 			/*
1856 			 * for positive cache, in addition to the packed
1857 			 * header size, allocate twice the size of the
1858 			 * existing result (in case the result grows
1859 			 * larger) plus 2K (for the file/compat backend to
1860 			 * process a possible large entry in the /etc files)
1861 			 */
1862 			bufsiz = phdr->data_off + 2 * phdr->data_len + 2048;
1863 		else
1864 			/*
1865 			 * for negative cache, allocate 8K buffer to
1866 			 * hold result in case the next lookup may
1867 			 * return something (in addition to the
1868 			 * packed header size)
1869 			 */
1870 			bufsiz = phdr->data_off + 8096;
1871 		table[i].ptr = malloc(bufsiz);
1872 		if (table[i].ptr == NULL) {
1873 			(void) mutex_unlock(&nscdb->db_mutex);
1874 			_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_ERROR)
1875 				(me, "memory allocation failure\n");
1876 			exit(1);
1877 		}
1878 		(void) memcpy(table[i].ptr, ptr->buffer,  ptr->bufsize);
1879 		((nss_pheader_t *)table[i].ptr)->pbufsiz = bufsiz;
1880 		table[i].num = bufsiz;
1881 	}
1882 	(void) mutex_unlock(&nscdb->db_mutex);
1883 
1884 	/* launch update thread for each keep hot entry */
1885 	for (i = keep; i > 0; i--) {
1886 		if (table[i].ptr == NULL)
1887 			continue; /* unused slot in table */
1888 		_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_DEBUG)
1889 		(me, "%s: launching update\n", nscdb->name);
1890 		largs = (nsc_lookup_args_t *)malloc(sizeof (*largs));
1891 		if (largs == NULL) {
1892 			_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_ERROR)
1893 				(me, "memory allocation failure\n");
1894 			exit(1);
1895 		}
1896 		largs->buffer = table[i].ptr;
1897 		largs->bufsize = table[i].num;
1898 		largs->ctx = ctx;
1899 		largs->nscdb = nscdb;
1900 		if (launch_update(largs) < 0)
1901 			exit(1);
1902 		(void) sleep(interval);
1903 	}
1904 
1905 	/*
1906 	 * The update thread will handle freeing of buffer and largs.
1907 	 * Free the table here.
1908 	 */
1909 	free(table);
1910 }
1911 
1912 
1913 static int
1914 launch_update(nsc_lookup_args_t *in)
1915 {
1916 	char	*me = "launch_update";
1917 	int	errnum;
1918 
1919 	errnum = thr_create(NULL, NULL, (void *(*)(void*))do_update,
1920 			in, 0|THR_DETACHED, NULL);
1921 	if (errnum != 0) {
1922 		_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_ERROR)
1923 			(me, "%s: thread creation failure (%d)\n",
1924 			in->nscdb->name, errnum);
1925 		return (-1);
1926 	}
1927 	return (0);
1928 }
1929 
1930 
1931 static void
1932 do_update(nsc_lookup_args_t *in) {
1933 	nss_pheader_t	*phdr = (nss_pheader_t *)in->buffer;
1934 
1935 	/* update the length of the data buffer */
1936 	phdr->data_len = phdr->pbufsiz - phdr->data_off;
1937 
1938 	(void) lookup_int(in, UPDATEBIT);
1939 	if (in->buffer)
1940 		free(in->buffer);
1941 	free(in);
1942 }
1943 
1944 
1945 /*
1946  * Invalidate cache
1947  */
1948 void
1949 nsc_invalidate(nsc_ctx_t *ctx, char *dbname, nsc_ctx_t **ctxs) {
1950 	int	i;
1951 	char	*me = "nsc_invalidate";
1952 
1953 	if (ctx) {
1954 		ctx_invalidate(ctx);
1955 		return;
1956 	}
1957 
1958 	if (dbname) {
1959 		if ((i = get_cache_idx(dbname)) == -1) {
1960 			_NSCD_LOG(NSCD_LOG_CACHE,
1961 				NSCD_LOG_LEVEL_WARNING)
1962 			(me, "%s: invalid cache name\n", dbname);
1963 			return;
1964 		}
1965 		if ((ctx = cache_ctx_p[i]) == NULL)  {
1966 			_NSCD_LOG(NSCD_LOG_CACHE,
1967 				NSCD_LOG_LEVEL_WARNING)
1968 			(me, "%s: no cache context found\n",
1969 				dbname);
1970 			return;
1971 		}
1972 		ctx_invalidate(ctx);
1973 		return;
1974 	}
1975 
1976 	if (ctxs == NULL)
1977 		ctxs =  cache_ctx_p;
1978 
1979 	for (i = 0; i < CACHE_CTX_COUNT; i++) {
1980 		if (ctxs[i] != NULL)
1981 		ctx_invalidate(ctxs[i]);
1982 	}
1983 }
1984 
1985 
1986 /*
1987  * Invalidate cache by context
1988  */
1989 static void
1990 ctx_invalidate(nsc_ctx_t *ctx) {
1991 	int 		i;
1992 	nsc_entry_t	*entry;
1993 	char		*me = "ctx_invalidate";
1994 
1995 	_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_DEBUG)
1996 	(me, "%s: invalidate cache\n", ctx->dbname);
1997 
1998 	for (i = 0; i < ctx->db_count; i++) {
1999 		if (ctx->nsc_db[i] == NULL)
2000 			continue;
2001 		(void) mutex_lock(&ctx->nsc_db[i]->db_mutex);
2002 		entry = ctx->nsc_db[i]->qtail;
2003 		while (entry != NULL) {
2004 			/* leave pending calls alone */
2005 			if (!(entry->stats.status & ST_PENDING))
2006 				entry->stats.status = ST_DISCARD;
2007 			entry = entry->qnext;
2008 		}
2009 		(void) mutex_unlock(&ctx->nsc_db[i]->db_mutex);
2010 	}
2011 
2012 	(void) mutex_lock(&ctx->stats_mutex);
2013 	ctx->stats.invalidate_count++;
2014 	(void) mutex_unlock(&ctx->stats_mutex);
2015 }
2016 
2017 
2018 /*
2019  * Free nsc_entry_t
2020  *
2021  * Pre-reqs:
2022  * nscdb->db_mutex lock must be held before calling this function
2023  */
2024 static void
2025 delete_entry(nsc_db_t *nscdb, nsc_ctx_t *ctx, nsc_entry_t *entry) {
2026 	uint_t		hash;
2027 
2028 	avl_remove(&nscdb->tree, entry);
2029 	HASH_REMOVE(nscdb, entry, hash, nscd_false);
2030 	queue_remove(nscdb, entry);
2031 	if (entry->buffer != NULL) {
2032 		free(entry->buffer);
2033 		entry->buffer = NULL;
2034 	}
2035 	umem_cache_free(nsc_entry_cache, entry);
2036 	(void) mutex_lock(&ctx->stats_mutex);
2037 	ctx->stats.entries--;
2038 	(void) mutex_unlock(&ctx->stats_mutex);
2039 }
2040 
2041 
2042 static nscd_rc_t
2043 lookup_cache(nsc_lookup_args_t *largs, nscd_cfg_cache_t *cfgp,
2044 	nss_XbyY_args_t *argp, char *whoami, nsc_entry_t **entry) {
2045 
2046 	nsc_db_t	*nscdb;
2047 	nsc_ctx_t	*ctx;
2048 	uint_t		hash;
2049 	avl_index_t	pos;
2050 	ulong_t		nentries;
2051 	nsc_entry_t	find_entry, *node;
2052 	char		*me = "lookup_cache";
2053 
2054 	ctx = largs->ctx;
2055 	nscdb = largs->nscdb;
2056 
2057 	/* set the search key */
2058 	find_entry.key = argp->key;	/* struct copy (not deep) */
2059 
2060 	/* lookup the hash table ==> O(1) */
2061 	if (nscdb->htable) {
2062 		*entry = hash_find(nscdb, &find_entry, &hash, nscd_true);
2063 		if (*entry != NULL) {
2064 			(void) queue_adjust(nscdb, *entry);
2065 			return (NSCD_SUCCESS);
2066 		}
2067 	}
2068 
2069 	/* if not found, lookup the AVL tree ==> O(log n) */
2070 	*entry = (nsc_entry_t *)avl_find(&nscdb->tree, &find_entry, &pos);
2071 	if (*entry != NULL) {
2072 		(void) queue_adjust(nscdb, *entry);
2073 		/* move it to the hash table */
2074 		if (nscdb->htable) {
2075 			if (nscdb->htable[hash] == NULL ||
2076 					(*entry)->stats.hits >=
2077 					nscdb->htable[hash]->stats.hits) {
2078 				nscdb->htable[hash] = *entry;
2079 			}
2080 		}
2081 		return (NSCD_SUCCESS);
2082 	}
2083 
2084 	/* entry not found in the cache */
2085 	_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_DEBUG)
2086 		(me, "%s: cache miss\n", whoami);
2087 
2088 	if (cfgp->avoid_ns == nscd_true) {
2089 		_NSCD_LOG(NSCD_LOG_CACHE,
2090 			NSCD_LOG_LEVEL_DEBUG)
2091 			(me, "%s: avoid name service\n", whoami);
2092 		return (NSCD_DB_ENTRY_NOT_FOUND);
2093 	}
2094 
2095 	/* allocate memory for new entry (stub) */
2096 	*entry = (nsc_entry_t *)umem_cache_alloc(nsc_entry_cache,
2097 		UMEM_DEFAULT);
2098 	if (*entry == NULL) {
2099 		_NSCD_LOG(NSCD_LOG_CACHE,
2100 			NSCD_LOG_LEVEL_ERROR)
2101 			(me, "%s: memory allocation failure\n", whoami);
2102 		return (NSCD_NO_MEMORY);
2103 	}
2104 	(void) memset(*entry, 0, sizeof (**entry));
2105 
2106 	/*
2107 	 * Note that the actual data for the key is stored within
2108 	 * the largs->buffer (input buffer to nsc_lookup).
2109 	 * find_entry.key only contains pointers to this data.
2110 	 *
2111 	 * If largs->buffer will be re-allocated by nss_psearch
2112 	 * then (*entry)->key will have dangling pointers.
2113 	 * In such case, the following assignment needs to be
2114 	 * replaced by code that duplicates the key.
2115 	 */
2116 	(*entry)->key = find_entry.key;
2117 
2118 	/*
2119 	 * Add the entry to the cache.
2120 	 */
2121 	avl_insert(&nscdb->tree, *entry, pos);	/* O(log n) */
2122 	(void) queue_adjust(nscdb, *entry);	/* constant */
2123 	if (nscdb->htable)			/* constant */
2124 		nscdb->htable[hash] = *entry;
2125 	(*entry)->stats.status = ST_NEW_ENTRY;
2126 
2127 	(void) mutex_lock(&ctx->stats_mutex);
2128 	nentries = ++(ctx->stats.entries);
2129 	(void) mutex_unlock(&ctx->stats_mutex);
2130 
2131 	/* Have we exceeded max entries ? */
2132 	if (cfgp->maxentries > 0 && nentries > cfgp->maxentries) {
2133 		_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_DEBUG)
2134 			(me, "%s: maximum entries exceeded -- "
2135 				"deleting least recently used entry\n",
2136 			whoami);
2137 
2138 		node = nscdb->qhead;
2139 		while (node != NULL && node != *entry) {
2140 			if (node->stats.status == ST_DISCARD ||
2141 					!(node->stats.status & ST_PENDING)) {
2142 				delete_entry(nscdb, ctx, node);
2143 				break;
2144 			}
2145 			node = node->qprev;
2146 		}
2147 
2148 		/*
2149 		 * It's okay if we were not able to find one to delete.
2150 		 * The reaper (when invoked) will return the cache to a
2151 		 * safe level.
2152 		 */
2153 	}
2154 
2155 	return (NSCD_SUCCESS);
2156 }
2157 
2158 static void
2159 reaper(nsc_ctx_t *ctx) {
2160 	uint_t		ttl, extra_sleep, total_sleep, intervals;
2161 	uint_t		nodes_per_interval, seconds_per_interval;
2162 	ulong_t		nsc_entries;
2163 	char		*me = "reaper";
2164 
2165 	for (;;) {
2166 		(void) mutex_lock(&ctx->stats_mutex);
2167 		nsc_entries = ctx->stats.entries;
2168 		(void) mutex_unlock(&ctx->stats_mutex);
2169 
2170 		(void) rw_rdlock(&ctx->cfg_rwlp);
2171 		ttl = ctx->cfg.pos_ttl;
2172 		(void) rw_unlock(&ctx->cfg_rwlp);
2173 
2174 		if (nsc_entries == 0) {
2175 			_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_DEBUG)
2176 				(me, "%s: nothing to reap\n", ctx->dbname);
2177 
2178 			/* sleep for atleast 60 seconds */
2179 			if (ttl < 60)
2180 				ttl = 60;
2181 			_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_DEBUG)
2182 			(me, "%s: sleep %d\n", ctx->dbname, ttl);
2183 			(void) sleep(ttl);
2184 			continue;
2185 		}
2186 
2187 		if (ttl < 32) ttl = 32;
2188 		if (ttl > (1<<28)) ttl = 1<<28;
2189 
2190 		/*
2191 		 * minimum nodes_per_interval = 256 or 1<<8
2192 		 * maximum nodes_per_interval = nsc_entries
2193 		 * minimum seconds_per_interval = 32 or 1<<5
2194 		 * maximum_seconds_per_interval = ttl
2195 		 */
2196 		if (nsc_entries <= ttl) {
2197 			intervals = (nsc_entries >> 8) + 1;
2198 			seconds_per_interval = ttl / intervals;
2199 			nodes_per_interval = 256;
2200 		} else {
2201 			intervals = (ttl >> 5) + 1;
2202 			seconds_per_interval = 32;
2203 			nodes_per_interval = nsc_entries / intervals;
2204 			if (nodes_per_interval < 256)
2205 				nodes_per_interval = 256;
2206 		}
2207 
2208 		_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_DEBUG)
2209 			(me, "%s: total entries = %d, "
2210 			"seconds per interval = %d, "
2211 			"nodes per interval = %d\n",
2212 			ctx->dbname, nsc_entries, seconds_per_interval,
2213 			nodes_per_interval);
2214 		total_sleep = reap_cache(ctx, nodes_per_interval,
2215 				seconds_per_interval);
2216 		extra_sleep = 1 + ttl - total_sleep;
2217 		if (extra_sleep > 0)
2218 			(void) sleep(extra_sleep);
2219 	}
2220 }
2221 
2222 
2223 static uint_t
2224 reap_cache(nsc_ctx_t *ctx, uint_t nodes_per_interval,
2225 		uint_t seconds_per_interval) {
2226 	uint_t		nodes_togo, total_sleep;
2227 	time_t		now;
2228 	nsc_entry_t	*node, *next_node;
2229 	nsc_db_t	*nscdb;
2230 	uint_t		primes[] = {_NSC_HTSIZE_PRIMES};
2231 	ulong_t		count, nentries, maxentries;
2232 	int		i, slot, value, newhtsize;
2233 	char		*me = "reap_cache";
2234 
2235 	count = 0;
2236 	total_sleep = 0;
2237 	nodes_togo = nodes_per_interval;
2238 	now = time(NULL);
2239 
2240 	for (i = 0; i < ctx->db_count; i++) {
2241 		nscdb = ctx->nsc_db[i];
2242 		(void) mutex_lock(&nscdb->db_mutex);
2243 		nscdb->reap_node = nscdb->qtail;
2244 		while (nscdb->reap_node != NULL) {
2245 			if (nodes_togo == 0) {
2246 				(void) mutex_unlock(&nscdb->db_mutex);
2247 				(void) sleep(seconds_per_interval);
2248 				total_sleep += seconds_per_interval;
2249 				nodes_togo = nodes_per_interval;
2250 				now = time(NULL);
2251 				(void) mutex_lock(&nscdb->db_mutex);
2252 			}
2253 			/* delete ST_DISCARD and expired nodes */
2254 			if ((node = nscdb->reap_node) == NULL)
2255 				break;
2256 			if (node->stats.status == ST_DISCARD ||
2257 					(!(node->stats.status & ST_PENDING) &&
2258 					node->stats.timestamp < now)) {
2259 				/*
2260 				 * Delete entry if its discard flag is
2261 				 * set OR if it has expired. Entries
2262 				 * with pending updates are not
2263 				 * deleted.
2264 				 * nscdb->reap_node will be adjusted
2265 				 * by delete_entry()
2266 				 */
2267 				delete_entry(nscdb, ctx, node);
2268 				count++;
2269 			} else {
2270 				nscdb->reap_node = node->qnext;
2271 			}
2272 			nodes_togo--;
2273 		}
2274 
2275 		if (nscdb->htsize == 0) {
2276 			(void) mutex_unlock(&nscdb->db_mutex);
2277 			continue;
2278 		}
2279 
2280 		/*
2281 		 * Dynamic adjustment of hash table size.
2282 		 *
2283 		 * Hash table size is roughly 1/8th of the
2284 		 * total entries. However the size is changed
2285 		 * only when the number of entries double or
2286 		 * reduced by half
2287 		 */
2288 		nentries = avl_numnodes(&nscdb->tree);
2289 		for (slot = 0, value = _NSC_INIT_HTSIZE_SLOT_VALUE;
2290 			slot < _NSC_HTSIZE_NUM_SLOTS && nentries > value;
2291 			value = (value << 1) + 1, slot++);
2292 		if (nscdb->hash_type == nsc_ht_power2)
2293 			newhtsize = _NSC_INIT_HTSIZE_POWER2 << slot;
2294 		else
2295 			newhtsize = primes[slot];
2296 
2297 		/* Recommended size is same as the current size. Done */
2298 		if (nscdb->htsize == newhtsize) {
2299 			(void) mutex_unlock(&nscdb->db_mutex);
2300 			continue;
2301 		}
2302 
2303 		_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_DEBUG)
2304 			(me, "%s: resizing hash table from %d to %d\n",
2305 			nscdb->name, nscdb->htsize, newhtsize);
2306 
2307 		/*
2308 		 * Dump old hashes because it would be time
2309 		 * consuming to rehash them.
2310 		 */
2311 		(void) free(nscdb->htable);
2312 		nscdb->htable = calloc(newhtsize, sizeof (*(nscdb->htable)));
2313 		if (nscdb->htable == NULL) {
2314 			_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_ERROR)
2315 				(me,
2316 				"%s: memory allocation failure\n",
2317 				nscdb->name);
2318 			/* -1 to try later */
2319 			nscdb->htsize = -1;
2320 		} else {
2321 			nscdb->htsize = newhtsize;
2322 		}
2323 		(void) mutex_unlock(&nscdb->db_mutex);
2324 	}
2325 
2326 	_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_DEBUG)
2327 		(me, "%s: reaped %lu entries\n", ctx->dbname, count);
2328 
2329 	/*
2330 	 * if cache is almost full then reduce it to a safe level by
2331 	 * evicting LRU entries
2332 	 */
2333 
2334 	(void) rw_rdlock(&ctx->cfg_rwlp);
2335 	maxentries = ctx->cfg.maxentries;
2336 	(void) rw_unlock(&ctx->cfg_rwlp);
2337 
2338 	/* No limit on number of entries. Done */
2339 	if (maxentries == 0)
2340 		goto out;
2341 
2342 	(void) mutex_lock(&ctx->stats_mutex);
2343 	nentries = ctx->stats.entries;
2344 	(void) mutex_unlock(&ctx->stats_mutex);
2345 
2346 	/* what is the percentage of cache used ? */
2347 	value = (nentries * 100) / maxentries;
2348 	if (value < _NSC_EVICTION_START_LEVEL)
2349 		goto out;
2350 
2351 	/*
2352 	 * cache needs to be reduced to a safe level
2353 	 */
2354 	value -= _NSC_EVICTION_SAFE_LEVEL;
2355 	for (i = 0, count = 0; i < ctx->db_count; i++) {
2356 		/*
2357 		 * Reduce each subcache by 'value' percent
2358 		 */
2359 		nscdb = ctx->nsc_db[i];
2360 		(void) mutex_lock(&nscdb->db_mutex);
2361 		nodes_togo = (value * avl_numnodes(&nscdb->tree)) / 100;
2362 
2363 		/* Start from LRU entry i.e queue head */
2364 		next_node = nscdb->qhead;
2365 		while (nodes_togo > 0 && next_node != NULL) {
2366 			node = next_node;
2367 			next_node = next_node->qprev;
2368 			if (node->stats.status == ST_DISCARD ||
2369 					!(node->stats.status & ST_PENDING)) {
2370 				/* Leave nodes with pending updates alone  */
2371 				delete_entry(nscdb, ctx, node);
2372 				count++;
2373 				nodes_togo--;
2374 			}
2375 		}
2376 		(void) mutex_unlock(&nscdb->db_mutex);
2377 	}
2378 
2379 	_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_DEBUG)
2380 		(me, "%s: evicted %lu LRU entries\n", ctx->dbname, count);
2381 
2382 out:
2383 	return (total_sleep);
2384 }
2385