cb5caa98562cf06753163f558cbcfe30b8f4673adjl * CDDL HEADER START
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * The contents of this file are subject to the terms of the
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * Common Development and Distribution License (the "License").
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * You may not use this file except in compliance with the License.
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * See the License for the specific language governing permissions
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * and limitations under the License.
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * When distributing Covered Code, include this CDDL HEADER in each
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * If applicable, add the following below this CDDL HEADER, with the
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * fields enclosed by brackets "[]" replaced with your own identifying
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * information: Portions Copyright [yyyy] [name of copyright owner]
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * CDDL HEADER END
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * Use is subject to license terms.
cb5caa98562cf06753163f558cbcfe30b8f4673adjl#pragma ident "%Z%%M% %I% %E% SMI"
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * This file implements the database functionality used by the
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * switch and configuration components. The implementation is
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * based on hash and has table. If the need arises in the future,
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * the code in this file can be changed to use other algorithms.
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * The following lists the functions implemented:
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * _nscd_add_db_entry
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * _nscd_delete_db_entry
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * _nscd_get_db_entry
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * _nscd_alloc_db
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * _nscd_free_db
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * _nscd_walk_db
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * _nscd_delete_db_entry_cookie
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * This structure defines an instance of the hash entry
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * which implements the nscd database entry. The
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * db_entry field should always be the first one in
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * the structure.
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * This structure defines a nscd database which
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * is implemented as an array of nscd_hash_t.
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * This cookie structure is used to iterate through the
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * database entries contained in a nscd database.
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * FUNCTION: calc_hash
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * Calculate a hash for a string based on the elf_hash
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * algorithm, hash is case insensitive. Uses tolower
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * instead of _tolower because of I18N.
cb5caa98562cf06753163f558cbcfe30b8f4673adjlstatic unsigned long
cb5caa98562cf06753163f558cbcfe30b8f4673adjl const char *str)
cb5caa98562cf06753163f558cbcfe30b8f4673adjl unsigned int g;
cb5caa98562cf06753163f558cbcfe30b8f4673adjl return ((unsigned long)hval);
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * FUNCTION: scan_hash
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * Scan a hash table for a matching hash entry. Assume 'str' is
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * not NULL. If option is NSCD_GET_NEXT_DB_ENTRY and id_num
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * is less than zero, then treats the option as NSCD_GET_FIRST_DB_ENTRY.
cb5caa98562cf06753163f558cbcfe30b8f4673adjlstatic const nscd_hash_t *
cb5caa98562cf06753163f558cbcfe30b8f4673adjl const char *str,
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * FUNCTION: _nscd_get_db_entry
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * Find a nscd database entry from a nscd database.
cb5caa98562cf06753163f558cbcfe30b8f4673adjl const char *str,
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * FUNCTION: _nscd_add_db_entry
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * Add a nscd database entry to a nscd database. This function
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * is not MT safe. The caller should lock the database to
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * prevent concurrent updates done by other threads.
cb5caa98562cf06753163f558cbcfe30b8f4673adjl const char *str,
cb5caa98562cf06753163f558cbcfe30b8f4673adjl /* find the bucket */
cb5caa98562cf06753163f558cbcfe30b8f4673adjl /* can not replace nothing */
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * the nscd_entry_t field should be the first field
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * in a nscd_hash_t
cb5caa98562cf06753163f558cbcfe30b8f4673adjl /* update the prev link list */
cb5caa98562cf06753163f558cbcfe30b8f4673adjl /* update the next link list */
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * FUNCTION: _nscd_delete_db_entry
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * Delete a nscd database entry from a nscd database. This
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * function is not MT safe. The caller should lock the
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * database to prevent concurrent updates done by other
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * threads.
cb5caa98562cf06753163f558cbcfe30b8f4673adjl const char *str,
cb5caa98562cf06753163f558cbcfe30b8f4673adjl /* find the bucket */
cb5caa98562cf06753163f558cbcfe30b8f4673adjl /* delete nothing always works */
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * only when option == NSCD_DEL_ALL_DB_ENTRY
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * will we get here. next_p should be set to
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * idx_p->next_p beforehand
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * FUNCTION: _nscd_alloc_db_entry
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * Allocate and return the memory for a database entry
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * so the caller can insert data and then add it to the
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * database.
cb5caa98562cf06753163f558cbcfe30b8f4673adjl const char *name,
cb5caa98562cf06753163f558cbcfe30b8f4673adjl /* first part: hash data structure and name string */
cb5caa98562cf06753163f558cbcfe30b8f4673adjl /* second part: pointer array to data */
cb5caa98562cf06753163f558cbcfe30b8f4673adjl /* third part: actual data */
cb5caa98562cf06753163f558cbcfe30b8f4673adjl /* allocate the memory */
cb5caa98562cf06753163f558cbcfe30b8f4673adjl /* init the entry based on caller's input */
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * FUNCTION: _nscd_delete_db_entry_cookie
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * Delete a database entry using the information from
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * the input 'cookie'.
cb5caa98562cf06753163f558cbcfe30b8f4673adjl /* snaity check */
cb5caa98562cf06753163f558cbcfe30b8f4673adjl /* more snaity check */
cb5caa98562cf06753163f558cbcfe30b8f4673adjl /* retrieve the hash entry from the cookie */
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * Update the next/previous link list.
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * Need to update c->hash as well, in case
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * the cookie is also used in a walk-db
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * loop. This is to make sure that the
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * next _nscd_walk_db() call will
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * find the (updated) next hash entry in line.
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * make sure the current bucket will be
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * walked again if _nscd_walk_db is
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * called next
cb5caa98562cf06753163f558cbcfe30b8f4673adjl /* delete the entry */
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * FUNCTION: _nscd_alloc_db
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * Allocate the space for a nscd database.
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * The input argument, size, indicates the size of the database.
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * NSCD_DB_SIZE_LARGE specifies an bucket array of size 67,
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * NSCD_DB_SIZE_MEDIUM specifies an bucket array of size 37,
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * NSCD_DB_SIZE_SMALL specifies an bucket array of size 13,
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * NSCD_DB_SIZE_TINY specifies an bucket array of size 3.
cb5caa98562cf06753163f558cbcfe30b8f4673adjl /* allocate the database */
cb5caa98562cf06753163f558cbcfe30b8f4673adjl /* allocate the bucket array */
cb5caa98562cf06753163f558cbcfe30b8f4673adjl sizeof (nscd_hash_t *));
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * FUNCTION: _nscd_free_db
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * Delete a nscd database.
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * find non-empty buckets and for each one of them,
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * delete all the database entries in it's link list
cb5caa98562cf06753163f558cbcfe30b8f4673adjl /* free the bucket array */
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * FUNCTION: _nscd_walk_db
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * Iterate through the database entries contained in
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * a nscd database and return one entry at a time.
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * The cookie structure is used to indicate the
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * location of the returned entry for the next call
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * to this function. For the very first call, *cookie
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * should be set to NULL. For subsequent calls, the one
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * returned by the previous call sould be used.
cb5caa98562cf06753163f558cbcfe30b8f4673adjl /* sanity check */
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * More sanity check. _nscd_delete_db_entry_cookie()
cb5caa98562cf06753163f558cbcfe30b8f4673adjl * could change c->idx to -1.
cb5caa98562cf06753163f558cbcfe30b8f4673adjl /* is there a next entry ? */
cb5caa98562cf06753163f558cbcfe30b8f4673adjl /* yes, return it */
cb5caa98562cf06753163f558cbcfe30b8f4673adjl /* find the first non-empty bucket */
cb5caa98562cf06753163f558cbcfe30b8f4673adjl /* no (more) non-empty bucket, we are done */