8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / nscd / nscd_dbimpl.c
blob822b4b6aba69f7c5c7e0e381eaf8de2bff707c1d
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <ctype.h>
32 #include "nscd_db.h"
35 * This file implements the database functionality used by the
36 * switch and configuration components. The implementation is
37 * based on hash and has table. If the need arises in the future,
38 * the code in this file can be changed to use other algorithms.
39 * The following lists the functions implemented:
41 * _nscd_add_db_entry
42 * _nscd_delete_db_entry
43 * _nscd_get_db_entry
44 * _nscd_alloc_db
45 * _nscd_free_db
46 * _nscd_walk_db
47 * _nscd_delete_db_entry_cookie
51 * This structure defines an instance of the hash entry
52 * which implements the nscd database entry. The
53 * db_entry field should always be the first one in
54 * the structure.
56 typedef struct nscd_hash {
57 nscd_db_entry_t db_entry;
58 struct nscd_hash *next_p;
59 struct nscd_hash *prev_p;
60 } nscd_hash_t;
63 * This structure defines a nscd database which
64 * is implemented as an array of nscd_hash_t.
66 struct nscd_db_s {
67 int array_size; /* number of elements in hash_tbl_p */
68 nscd_hash_t **hash_tbl_p;
72 * This cookie structure is used to iterate through the
73 * database entries contained in a nscd database.
75 struct cookie {
76 int idx; /* the current bucket */
77 nscd_hash_t *hash; /* the current hash entry */
78 nscd_db_t *db; /* the database */
82 * FUNCTION: calc_hash
84 * Calculate a hash for a string based on the elf_hash
85 * algorithm, hash is case insensitive. Uses tolower
86 * instead of _tolower because of I18N.
88 static unsigned long
89 calc_hash(
90 const char *str)
92 unsigned int hval = 0;
93 char ch;
95 while (*str != NULL) {
96 unsigned int g;
98 ch = (char)*str++;
99 if (isupper(ch))
100 ch = _tolower(ch);
101 hval = (hval << 4) + ch;
102 if ((g = (hval & 0xf0000000)) != 0)
103 hval ^= g >> 24;
104 hval &= ~g;
106 return ((unsigned long)hval);
110 * FUNCTION: scan_hash
112 * Scan a hash table for a matching hash entry. Assume 'str' is
113 * not NULL. If option is NSCD_GET_NEXT_DB_ENTRY and id_num
114 * is less than zero, then treats the option as NSCD_GET_FIRST_DB_ENTRY.
117 static const nscd_hash_t *
118 scan_hash(
119 int type,
120 const char *str,
121 const nscd_hash_t *idx_p,
122 nscd_db_option_t option,
123 int id_num)
125 int id_matched = 0;
126 nscd_db_entry_t *db_entry;
128 while (idx_p != NULL) {
129 db_entry = &((nscd_hash_t *)idx_p)->db_entry;
130 if (db_entry->type == type) {
131 if (strcasecmp(str, db_entry->name) == 0) {
132 switch (option) {
133 case NSCD_GET_FIRST_DB_ENTRY:
134 return (idx_p);
135 case NSCD_GET_EXACT_DB_ENTRY:
136 if (id_num == db_entry->id_num)
137 return (idx_p);
138 break;
139 case NSCD_GET_NEXT_DB_ENTRY:
140 if (id_num < 0)
141 return (idx_p);
142 if (id_matched == 1)
143 return (idx_p);
144 if (id_num == db_entry->id_num)
145 id_matched = 1;
146 break;
150 idx_p = idx_p->next_p;
152 return (NULL);
156 * FUNCTION: _nscd_get_db_entry
158 * Find a nscd database entry from a nscd database.
160 const nscd_db_entry_t *
161 _nscd_get_db_entry(
162 const nscd_db_t *db,
163 int type,
164 const char *str,
165 nscd_db_option_t option,
166 int id_num)
168 unsigned long hash;
169 const nscd_hash_t *idx_p, *hash_p;
171 if (db == NULL || str == NULL)
172 return (NULL);
174 hash = calc_hash(str);
175 idx_p = db->hash_tbl_p[hash % db->array_size];
177 hash_p = scan_hash(type, str, idx_p, option, id_num);
179 return (&hash_p->db_entry);
183 * FUNCTION: _nscd_add_db_entry
185 * Add a nscd database entry to a nscd database. This function
186 * is not MT safe. The caller should lock the database to
187 * prevent concurrent updates done by other threads.
189 nscd_rc_t
190 _nscd_add_db_entry(
191 nscd_db_t *db,
192 const char *str,
193 nscd_db_entry_t *entry,
194 nscd_db_option_t option)
196 int i;
197 unsigned long hash;
198 nscd_hash_t *next_p = NULL, *prev_p = NULL;
199 nscd_hash_t *idx_p, *hash_entry;
200 nscd_db_entry_t *db_entry;
202 /* find the bucket */
203 hash = calc_hash(str);
204 i = hash % db->array_size;
205 idx_p = db->hash_tbl_p[i];
207 /* can not replace nothing */
208 if (idx_p == NULL)
209 if (option == NSCD_ADD_DB_ENTRY_REPLACE)
210 return (NSCD_DB_ENTRY_NOT_FOUND);
212 while (idx_p != NULL) {
213 db_entry = &idx_p->db_entry;
214 switch (option) {
216 case NSCD_ADD_DB_ENTRY_FIRST:
217 next_p = idx_p;
218 goto add_entry;
220 case NSCD_ADD_DB_ENTRY_REPLACE:
221 if (db_entry->type != entry->type)
222 goto cont;
223 if (strcasecmp(db_entry->name, str) != 0)
224 goto cont;
226 if (db_entry->id_num == entry->id_num) {
227 prev_p = idx_p->prev_p;
228 next_p = idx_p->next_p;
229 free(idx_p);
230 goto add_entry;
232 goto cont;
234 case NSCD_ADD_DB_ENTRY_IF_NONE:
235 if (db_entry->type != entry->type)
236 break;
237 if (strcasecmp(db_entry->name, str) != 0)
238 break;
239 return (NSCD_DB_ENTRY_FOUND);
242 if (idx_p->next_p == NULL) {
243 if (option == NSCD_ADD_DB_ENTRY_LAST ||
244 option == NSCD_ADD_DB_ENTRY_IF_NONE) {
245 prev_p = idx_p;
246 goto add_entry;
250 cont:
251 idx_p = idx_p->next_p;
254 add_entry:
257 * the nscd_entry_t field should be the first field
258 * in a nscd_hash_t
260 hash_entry = (nscd_hash_t *)entry;
262 /* update the prev link list */
263 hash_entry->prev_p = prev_p;
264 if (prev_p == NULL)
265 db->hash_tbl_p[i] = hash_entry;
266 else
267 prev_p->next_p = hash_entry;
269 /* update the next link list */
270 hash_entry->next_p = next_p;
271 if (next_p != NULL)
272 next_p->prev_p = hash_entry;
274 return (NSCD_SUCCESS);
278 * FUNCTION: _nscd_delete_db_entry
280 * Delete a nscd database entry from a nscd database. This
281 * function is not MT safe. The caller should lock the
282 * database to prevent concurrent updates done by other
283 * threads.
285 nscd_rc_t
286 _nscd_delete_db_entry(
287 nscd_db_t *db,
288 int type,
289 const char *str,
290 nscd_db_option_t option,
291 int id_num)
293 int i;
294 int del_more = 0;
295 unsigned long hash;
296 nscd_hash_t *idx_p, *next_p = NULL, *prev_p = NULL;
297 nscd_db_entry_t *db_entry;
299 /* find the bucket */
300 hash = calc_hash(str);
301 i = hash % db->array_size;
302 idx_p = db->hash_tbl_p[i];
304 /* delete nothing always works */
305 if (idx_p == NULL)
306 return (NSCD_SUCCESS);
308 while (idx_p != NULL) {
309 db_entry = &idx_p->db_entry;
310 if (db_entry->type != type)
311 goto cont;
312 if (strcasecmp(db_entry->name, str) != 0)
313 goto cont;
315 switch (option) {
317 case NSCD_DEL_FIRST_DB_ENTRY:
318 prev_p = idx_p->prev_p;
319 next_p = idx_p->next_p;
320 del_more = 0;
321 break;
323 case NSCD_DEL_EXACT_DB_ENTRY:
324 if (db_entry->id_num == id_num) {
325 prev_p = idx_p->prev_p;
326 next_p = idx_p->next_p;
327 del_more = 0;
328 } else
329 goto cont;
330 break;
332 case NSCD_DEL_ALL_DB_ENTRY:
333 prev_p = idx_p->prev_p;
334 next_p = idx_p->next_p;
335 break;
338 if (prev_p == NULL)
339 db->hash_tbl_p[i] = next_p;
340 else
341 prev_p->next_p = next_p;
343 if (next_p != NULL)
344 next_p->prev_p = prev_p;
346 free(idx_p);
348 if (del_more == 0)
349 break;
351 * only when option == NSCD_DEL_ALL_DB_ENTRY
352 * will we get here. next_p should be set to
353 * idx_p->next_p beforehand
355 idx_p = next_p;
356 continue;
358 cont:
360 idx_p = idx_p->next_p;
363 return (NSCD_SUCCESS);
367 * FUNCTION: _nscd_alloc_db_entry
369 * Allocate and return the memory for a database entry
370 * so the caller can insert data and then add it to the
371 * database.
373 nscd_db_entry_t *
374 _nscd_alloc_db_entry(
375 int type,
376 const char *name,
377 int dataSize,
378 int num_data,
379 int num_array)
381 int size;
382 int array_o, data_o;
383 nscd_hash_t *hash;
384 void *p;
386 /* first part: hash data structure and name string */
387 size = sizeof (*hash) + strlen(name) + 1;
388 array_o = size = roundup(size);
390 /* second part: pointer array to data */
391 size += (num_data + num_array) * sizeof (void **);
392 size = roundup(size);
394 /* third part: actual data */
395 data_o = size;
396 size += dataSize;
398 /* allocate the memory */
399 hash = (nscd_hash_t *)calloc(1, size);
401 if (hash == NULL)
402 return (NULL);
404 /* init the entry based on caller's input */
405 hash->db_entry.num_data = num_data;
406 hash->db_entry.num_array = num_array;
407 hash->db_entry.type = type;
408 hash->db_entry.name = (char *)hash + sizeof (*hash);
409 p = (char *)hash + array_o;
410 hash->db_entry.data_array = (void **)p;
411 *(hash->db_entry.data_array) = (char *)hash + data_o;
412 (void) strcpy(hash->db_entry.name, name);
414 return (&hash->db_entry);
418 * FUNCTION: _nscd_delete_db_entry_cookie
420 * Delete a database entry using the information from
421 * the input 'cookie'.
423 void
424 _nscd_delete_db_entry_cookie(
425 nscd_db_t *db,
426 void **cookie)
428 nscd_hash_t *hp;
429 struct cookie *c;
431 /* snaity check */
432 if (cookie == NULL || *cookie == NULL || db == NULL)
433 return;
434 c = *cookie;
436 /* more snaity check */
437 if (db != c->db || c->hash == NULL ||
438 c->idx < 0 || c->idx >= db->array_size)
439 return;
441 /* retrieve the hash entry from the cookie */
442 hp = c->hash;
445 * Update the next/previous link list.
446 * Need to update c->hash as well, in case
447 * the cookie is also used in a walk-db
448 * loop. This is to make sure that the
449 * next _nscd_walk_db() call will
450 * find the (updated) next hash entry in line.
452 if (hp->prev_p == NULL) {
454 * make sure the current bucket will be
455 * walked again if _nscd_walk_db is
456 * called next
458 c->hash = NULL;
459 db->hash_tbl_p[c->idx] = hp->next_p;
460 c->idx--;
462 } else {
463 c->hash = hp->prev_p;
464 hp->prev_p->next_p = hp->next_p;
466 if (hp->next_p != NULL)
467 hp->next_p->prev_p = hp->prev_p;
469 /* delete the entry */
470 free(hp);
474 * FUNCTION: _nscd_alloc_db
476 * Allocate the space for a nscd database.
478 * The input argument, size, indicates the size of the database.
479 * NSCD_DB_SIZE_LARGE specifies an bucket array of size 67,
480 * NSCD_DB_SIZE_MEDIUM specifies an bucket array of size 37,
481 * NSCD_DB_SIZE_SMALL specifies an bucket array of size 13,
482 * NSCD_DB_SIZE_TINY specifies an bucket array of size 3.
484 nscd_db_t *
485 _nscd_alloc_db(
486 int size)
488 int sz;
489 nscd_db_t *db;
491 /* allocate the database */
492 db = (nscd_db_t *)calloc(1, sizeof (nscd_db_t));
493 if (db == NULL)
494 return (NULL);
496 /* allocate the bucket array */
497 if (size == NSCD_DB_SIZE_LARGE)
498 sz = 67;
499 else if (size == NSCD_DB_SIZE_MEDIUM)
500 sz = 37;
501 else if (size == NSCD_DB_SIZE_SMALL)
502 sz = 13;
503 else if (size == NSCD_DB_SIZE_TINY)
504 sz = 3;
505 db->hash_tbl_p = (nscd_hash_t **)calloc(sz + 1,
506 sizeof (nscd_hash_t *));
507 if (db->hash_tbl_p == NULL) {
508 free(db);
509 return (NULL);
512 db->array_size = sz;
514 return (db);
518 * FUNCTION: _nscd_free_db
520 * Delete a nscd database.
522 void
523 _nscd_free_db(
524 nscd_db_t *db)
526 int i;
527 nscd_hash_t *hp, *next_p;
530 * find non-empty buckets and for each one of them,
531 * delete all the database entries in it's link list
533 for (i = 0; i < db->array_size; i++) {
535 hp = db->hash_tbl_p[i];
537 while (hp != NULL) {
538 next_p = hp->next_p;
539 free(hp);
540 hp = next_p;
544 /* free the bucket array */
545 free(db->hash_tbl_p);
547 free(db);
551 * FUNCTION: _nscd_walk_db
553 * Iterate through the database entries contained in
554 * a nscd database and return one entry at a time.
555 * The cookie structure is used to indicate the
556 * location of the returned entry for the next call
557 * to this function. For the very first call, *cookie
558 * should be set to NULL. For subsequent calls, the one
559 * returned by the previous call sould be used.
561 nscd_db_entry_t *
562 _nscd_walk_db(
563 nscd_db_t *db,
564 void **cookie)
566 struct cookie *c;
568 /* sanity check */
569 if (cookie == NULL || db == NULL)
570 return (NULL);
572 if (*cookie != NULL) {
574 c = *cookie;
577 * More sanity check. _nscd_delete_db_entry_cookie()
578 * could change c->idx to -1.
580 if (db != c->db ||
581 c->idx < -1 || c->idx >= db->array_size)
582 return (NULL);
584 /* is there a next entry ? */
585 if (c->hash != NULL)
586 c->hash = c->hash->next_p;
588 /* yes, return it */
589 if (c->hash != NULL) {
590 return (&c->hash->db_entry);
592 } else {
593 c = (struct cookie *)calloc(1, sizeof (*c));
594 if (c == NULL)
595 return (NULL);
596 c->idx = -1;
597 c->hash = NULL;
598 c->db = db;
601 /* find the first non-empty bucket */
602 for (c->idx++; c->idx < db->array_size; c->idx++) {
603 c->hash = db->hash_tbl_p[c->idx];
604 if (c->hash != NULL)
605 break;
608 /* no (more) non-empty bucket, we are done */
609 if (c->hash == NULL) {
610 free(c);
611 *cookie = NULL;
612 return (NULL);
615 *cookie = c;
616 return (&c->hash->db_entry);