9511 printf family isn't aware of multibyte decimal point characters (fix regression)
[unleashed.git] / usr / src / lib / libnisdb / nis_hashitem.c
blobbbfb0953476d5a8460435e2a2b4cf44899fec2ba
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
20 * CDDL HEADER END
23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
29 #include <string.h>
30 #include <pthread.h>
31 #include <syslog.h>
32 #include <rpcsvc/nis.h>
34 #include "nis_hashitem.h"
36 /* We're the magician, so undefine the define-magic */
37 #undef NIS_HASH_ITEM
38 #undef NIS_HASH_TABLE
39 #undef nis_insert_item
40 #undef nis_find_item
41 #undef nis_pop_item
42 #undef nis_remove_item
44 #define set_thread_status(msg, state)
47 * The hash table routines below implement nested (or recursive)
48 * one-writer-or-many-readers locking. The following restrictions
49 * exist:
51 * Unless an item destructor has been established, an item
52 * MUST NOT be removed from a list (__nis_pop_item_mt() or
53 * (__nis_remove_item_mt()) when the thread performing
54 * the deletion is holding a read-only lock on the item.
55 * Doing so will result in the thread blocking in
56 * pthread_cond_wait() waiting for itself to signal on
57 * the condition variable. Deletion when the invoking
58 * thread is holding a write lock (any level of nesting),
59 * or no lock, is OK.
62 void
63 __nis_init_hash_table(__nis_hash_table_mt *table,
64 void (*itemDestructor)(void *)) {
66 int errorcode;
68 if (table != 0) {
69 errorcode = pthread_mutex_init(&table->lock, 0);
70 if (errorcode != 0) {
71 syslog(LOG_WARNING, "__nis_init_hash_table: "
72 "(table->lock) pthread_mutex_init returned %d (%s)",
73 errorcode, strerror(errorcode));
76 errorcode = pthread_cond_init(&table->cond, 0);
77 if (errorcode != 0) {
78 syslog(LOG_WARNING, "__nis_init_hash_table: "
79 "(table->cond) pthread_cond_init returned %d (%s)",
80 errorcode, strerror(errorcode));
83 errorcode = pthread_mutex_init(&table->traverser_id_lock, 0);
84 if (errorcode != 0) {
85 syslog(LOG_WARNING, "__nis_init_hash_table: "
86 "(table->traverser_id_lock) "
87 "pthread_mutex_init returned %d (%s)",
88 errorcode, strerror(errorcode));
91 table->traversed = 0;
92 table->locked_items = 0;
93 (void) memset(table->keys, 0, sizeof (table->keys));
94 table->first = 0;
95 table->destroyItem = itemDestructor;
99 int
100 __nis_lock_hash_table(__nis_hash_table_mt *table, int traverse, char *msg) {
102 pthread_t myself = pthread_self();
104 if (table != 0) {
105 if (traverse) {
107 * We want exclusive access to everything in the
108 * table (list). Wait until there are no threads
109 * either traversing the list, or with exclusive
110 * access to an item.
112 set_thread_status(msg, "table WL");
113 (void) pthread_mutex_lock(&table->lock);
114 set_thread_status(msg, "table L");
115 while ((table->traversed != 0 &&
116 table->traverser_id != myself) ||
117 table->locked_items != 0) {
118 set_thread_status(msg, "traverse cond_wait");
119 MT_LOG(1, (LOG_NOTICE,
120 "%d: lh table 0x%x trav cond wait",
121 myself, table));
122 (void) pthread_cond_wait(&table->cond,
123 &table->lock);
125 set_thread_status(msg, "traverser_id WL");
126 (void) pthread_mutex_lock(&table->traverser_id_lock);
127 set_thread_status(msg, "traverser_id L");
128 table->traversed = 1;
129 table->traverser_id = myself;
130 (void) pthread_mutex_unlock(&table->traverser_id_lock);
131 set_thread_status(msg, "traverser_id U");
132 } else {
134 * Called from the nis_*_item() functions. If no one's
135 * locked the table, lock it. If the table already is
136 * being traversed by us, do nothing. Otherwise, wait
137 * for the lock.
139 set_thread_status(msg, "non-traverse TL");
140 if (pthread_mutex_trylock(&table->lock) == EBUSY) {
141 int dolock = 1;
142 /* Already locked; find out if it's us */
143 set_thread_status(msg, "traverser_id L");
144 (void) pthread_mutex_lock(
145 &table->traverser_id_lock);
146 if (table->traversed != 0 &&
147 table->traverser_id == myself) {
148 /* It's us. No action. */
149 dolock = 0;
151 (void) pthread_mutex_unlock(
152 &table->traverser_id_lock);
153 set_thread_status(msg, "traverser_id U");
154 /* Not us. Wait for the lock */
155 if (dolock) {
156 MT_LOG(1, (LOG_NOTICE,
157 "%d: lh table 0x%x cond wait",
158 myself, table));
159 set_thread_status(msg, "table WL");
160 (void) pthread_mutex_lock(&table->lock);
161 set_thread_status(msg, "table L");
165 MT_LOG(1, (LOG_NOTICE, "%d: lh table %s lock acquired 0x%x",
166 myself, traverse?"traverse":"non-traverse", table));
167 return (1);
168 } else {
169 return (0);
174 __nis_ulock_hash_table(__nis_hash_table_mt *table, int traverse, char *msg) {
176 int dounlock = 0;
178 if (table != 0) {
179 if (traverse) {
181 * Since we're keeping track of who's traversing the
182 * table in order to avoid recursive locking in the
183 * nis_*item() functions, we might as well sanity check
184 * here.
186 set_thread_status(msg, "traverser_id WL");
187 (void) pthread_mutex_lock(&table->traverser_id_lock);
188 set_thread_status(msg, "traverser_id L");
189 if (table->traversed != 0 &&
190 table->traverser_id == pthread_self()) {
191 table->traversed = 0;
193 * Leave traverser_id as it is, so that it
194 * possible to see which thread last held
195 * the traverser lock.
197 dounlock = 1;
198 /* Wake up other traversers-to-be */
199 set_thread_status(msg, "table cond_signal");
200 (void) pthread_cond_signal(&table->cond);
202 (void) pthread_mutex_unlock(&table->traverser_id_lock);
203 set_thread_status(msg, "traverser_id U");
204 } else {
206 * Called from the nis_*_item() functions. If we're
207 * traversing the table, leave it locked.
209 set_thread_status(msg, "traverser_id WL");
210 (void) pthread_mutex_lock(&table->traverser_id_lock);
211 set_thread_status(msg, "traverser_id L");
212 if (table->traversed == 0) {
213 dounlock = 1;
215 (void) pthread_mutex_unlock(&table->traverser_id_lock);
216 set_thread_status(msg, "traverser_id U");
218 if (dounlock) {
219 (void) pthread_mutex_unlock(&table->lock);
220 set_thread_status(msg, "table U");
222 MT_LOG(1, (LOG_NOTICE, "%d: lh table %s release 0x%x (%s)",
223 pthread_self(), traverse?"traverse":"non-traverse", table,
224 dounlock?"unlocked":"still held"));
225 return (1);
226 } else {
227 return (0);
231 static __nis_hash_item_mt **
232 __find_item_mt(nis_name name, __nis_hash_table_mt *table, int *keyp) {
234 int key = 0;
235 unsigned char *s;
236 __nis_hash_item_mt *it, **pp;
238 /* Assume table!=0, table lock held */
240 for (s = (unsigned char *)name; *s != 0; s++) {
241 key += *s;
243 key %= (sizeof (table->keys) / sizeof (table->keys[0]));
245 if (keyp != 0) {
246 *keyp = key;
248 for (pp = &table->keys[key]; (it = *pp) != 0; pp = &it->next) {
249 if (strcmp(name, it->name) == 0) {
250 break;
254 return (pp);
258 * The 'readwrite' argument is interpreted as follows on a successful
259 * return:
261 * < 0 Exclusive access to item
262 * 0 Item must not be used or referenced in any way
263 * > 0 Non-exclusive access (read-only) to item
265 * Except when 'readwrite' == 0, the caller must explicitly release the
266 * item (__nis_release_item()).
269 __nis_insert_item_mt(void *arg, __nis_hash_table_mt *table, int readwrite) {
271 __nis_hash_item_mt *item = arg;
272 int key;
273 __nis_hash_item_mt **pp;
275 if (item == 0 || __nis_lock_hash_table(table, 0, "nitmt") == 0)
276 return (0);
278 if ((*(pp = __find_item_mt(item->name, table, &key))) != 0) {
279 (void) __nis_ulock_hash_table(table, 0, "nitmt");
280 return (0);
283 (void) pthread_cond_init(&item->lock, 0);
284 item->readers = item->writer = 0;
285 item->last_reader_id = item->writer_id = INV_PTHREAD_ID;
286 if (readwrite < 0) {
287 item->writer = 1;
288 item->writer_id = pthread_self();
289 table->locked_items++;
290 } else if (readwrite > 0) {
291 item->readers = 1;
292 item->last_reader_id = pthread_self();
293 table->locked_items++;
295 item->next = *pp;
296 *pp = item;
297 item->keychain = key;
299 if (table->first)
300 table->first->prv_item = item;
302 item->nxt_item = table->first;
303 item->prv_item = NULL;
304 table->first = item;
306 (void) __nis_ulock_hash_table(table, 0, "nitmt");
308 return (1);
311 void
312 __nis_insert_name_mt(nis_name name, __nis_hash_table_mt *table) {
314 __nis_hash_item_mt *item;
316 if (name == 0 || table == 0)
317 return;
319 if ((item = malloc(sizeof (*item))) == 0) {
320 syslog(LOG_WARNING, "__nis_insert_name_mt: malloc failed\n");
321 return;
324 if ((item->name = strdup(name)) == 0) {
325 syslog(LOG_WARNING, "__nis_insert_name_mt: strdup failed\n");
326 free(item);
327 return;
330 if (! __nis_insert_item_mt(item, table, 0)) {
331 free(item->name);
332 free(item);
337 * readwrite: < 0 Exclusive access
338 * 0 No access; must not use returned item in any way,
339 * other than to confirm existence indicated by a non-NULL
340 * return value.
341 * > 0 Non-exclusive (read-only) access
343 * If trylock != 0 and *trylock != 0 and the item exists but the requested
344 * lock type cannot be acquired, set *trylock = -1 and return 0.
346 void *
347 __nis_find_item_mt(nis_name name, __nis_hash_table_mt *table, int readwrite,
348 int *trylock) {
350 __nis_hash_item_mt *item;
351 pthread_t me = pthread_self();
353 if (name == 0 || __nis_lock_hash_table(table, 0, "nfimt") == 0)
354 return (0);
357 * Block waiting for more favorable conditions unless:
359 * The item doesn't exist anymore
361 * 'readwrite' == 0 (verify existence only)
363 * There's a writer, but it's us
365 * There are no writers, and we're satisfied by RO access
367 * A trylock was requested
369 while ((item = *__find_item_mt(name, table, 0)) != 0) {
370 if (readwrite == 0 ||
371 (item->writer == 0 && item->readers == 0))
372 break;
373 if (item->writer == 0 && readwrite > 0)
374 break;
375 if ((item->writer != 0 && item->writer_id == me))
376 break;
377 if (trylock != 0 && *trylock != 0) {
378 *trylock = -1;
379 (void) __nis_ulock_hash_table(table, 0, "nfimt");
380 return (0);
382 (void) pthread_cond_wait(&item->lock, &table->lock);
385 if (item != 0) {
386 if (readwrite < 0) {
387 if (item->writer == 0) {
388 item->writer_id = me;
389 table->locked_items++;
391 item->writer++;
392 } else if (readwrite > 0) {
393 if (item->readers == 0) {
394 table->locked_items++;
396 item->last_reader_id = me;
397 item->readers++;
401 (void) __nis_ulock_hash_table(table, 0, "nfimt");
403 return (item);
406 void *
407 __nis_pop_item_mt(__nis_hash_table_mt *table) {
409 __nis_hash_item_mt *item, *cur, *prev;
410 pthread_t mtid;
412 if (__nis_lock_hash_table(table, 0, "npimt") == 0)
413 return (0);
415 /* Wait until the first item isn't in use by another thread */
416 mtid = pthread_self();
417 while ((item = table->first) != 0) {
418 if (table->destroyItem != 0)
419 break;
420 if (item->readers == 0 && item->writer == 0)
421 break;
422 if (item->writer != 0 && item->writer_id == mtid)
423 break;
424 (void) pthread_cond_wait(&item->lock, &table->lock);
427 /* List might be empty now */
428 if (item == 0) {
429 __nis_ulock_hash_table(table, 0, "npimt");
430 return (0);
433 prev = 0;
434 for (cur = table->keys[item->keychain]; cur;
435 prev = cur, cur = cur->next) {
436 if (cur == item) {
437 if (prev)
438 prev->next = cur->next;
439 else
440 table->keys[cur->keychain] = cur->next;
441 if (cur->prv_item)
442 cur->prv_item->nxt_item = cur->nxt_item;
443 else
444 table->first = cur->nxt_item;
445 if (cur->nxt_item)
446 cur->nxt_item->prv_item = cur->prv_item;
447 break;
452 * We use keychain < 0 to indicate that the item isn't linked
453 * into the table anymore.
455 item->keychain = -1;
457 /* Adjust the count of locked items in the table */
458 if (table->locked_items != 0 &&
459 (item->writer > 0 || item->readers > 0)) {
460 table->locked_items--;
461 if (table->locked_items == 0) {
462 /* Wake up traversers-to-be */
463 (void) pthread_cond_signal(&table->cond);
468 * Wake up any threads that were waiting for this item. Obviously,
469 * such threads must start over scanning the list.
471 (void) pthread_cond_signal(&item->lock);
472 (void) pthread_cond_destroy(&item->lock);
475 * If the item isn't locked, and an item destructor has been
476 * established, invoke the destructor.
478 if (item->readers == 0 && item->writer == 0 &&
479 table->destroyItem != 0) {
480 (*table->destroyItem)(item);
481 item = 0;
482 } else {
483 item->next = 0;
484 item->prv_item = 0;
485 item->nxt_item = 0;
488 (void) __nis_ulock_hash_table(table, 0, "npimt");
491 * If we get here, and the 'item' is NULL, we've popped the
492 * item, but also destroyed it. Returning NULL would make
493 * our caller believe the list is empty, so instead, we invoke
494 * ourselves to pop the next item.
496 return ((item != 0) ? item : __nis_pop_item_mt(table));
499 void *
500 __nis_remove_item_mt(nis_name name, __nis_hash_table_mt *table) {
502 __nis_hash_item_mt *nl, **pp;
503 pthread_t mtid;
505 if (__nis_lock_hash_table(table, 0, "nrimt") == 0)
506 return (0);
508 /* Find the item, and make sure it's not in use */
509 mtid = pthread_self();
510 while ((nl = *(pp = __find_item_mt(name, table, (int *)0))) != 0) {
511 if (table->destroyItem != 0)
512 break;
513 if (nl->readers == 0 && nl->writer == 0)
514 break;
515 if (nl->writer != 0 && nl->writer_id == mtid)
516 break;
517 (void) pthread_cond_wait(&nl->lock, &table->lock);
520 if (nl == 0) {
521 (void) __nis_ulock_hash_table(table, 0, "nrimt");
522 return (0);
525 /* Remove nl from the hash chain */
526 *pp = nl->next;
527 nl->next = 0;
529 /* Remove nl from the linked list of all names */
530 if (nl->prv_item)
531 nl->prv_item->nxt_item = nl->nxt_item;
532 else
533 table->first = nl->nxt_item;
535 if (nl->nxt_item)
536 nl->nxt_item->prv_item = nl->prv_item;
537 nl->prv_item = 0;
538 nl->nxt_item = 0;
540 /* keychain < 0 means not in table anymore */
541 nl->keychain = -1;
544 * If this item was locked, we can now decrement the count of
545 * locked items for the table.
547 if (table->locked_items != 0 &&
548 (nl->writer > 0 || nl->readers > 0)) {
549 table->locked_items--;
550 if (table->locked_items == 0) {
551 /* Wake up traversers-to-be */
552 (void) pthread_cond_signal(&table->cond);
555 (void) pthread_cond_signal(&nl->lock);
556 (void) pthread_cond_destroy(&nl->lock);
559 * If the item isn't locked, and an item destructor has been
560 * established, invoke the destructor. In that case, we return
561 * NULL, so that our caller doesn't try to reference the
562 * deleted item.
564 if (nl->readers == 0 && nl->writer == 0 && table->destroyItem != 0) {
565 (*table->destroyItem)(nl);
566 nl = 0;
569 (void) __nis_ulock_hash_table(table, 0, "nrimt");
571 return (nl);
575 * Release an item that had been acquired exclusively or non-exclusively.
576 * Note that 'readwrite' can assume any integer value, and thus can be
577 * used to release any level of recursive locking. It's the responsibility
578 * of the caller to make sure that proper nesting is maintained.
581 __nis_release_item(void *arg, __nis_hash_table_mt *table, int readwrite) {
583 __nis_hash_item_mt *item = arg;
584 int wakeup = 0;
586 if (item == 0 || __nis_lock_hash_table(table, 0, "nreli") == 0)
587 return (0);
589 if ((readwrite < 0 && abs(readwrite) > item->writer) ||
590 (readwrite < 0 && item->writer > 0 &&
591 item->writer_id != pthread_self()) ||
592 (readwrite > 0 && readwrite > item->readers)) {
593 /* Caller confused; ignore */
594 (void) __nis_ulock_hash_table(table, 0, "nreli");
595 return (0);
598 if (readwrite < 0) {
599 item->writer += readwrite;
600 if (item->writer == 0 && item->keychain >= 0) {
601 if (table->locked_items != 0)
602 table->locked_items--;
603 wakeup = 1;
605 } else if (readwrite > 0) {
606 item->readers -= readwrite;
607 item->last_reader_id = INV_PTHREAD_ID;
608 if (item->readers == 0 && item->keychain >= 0) {
609 if (table->locked_items != 0)
610 table->locked_items--;
611 wakeup = 1;
615 if (table->locked_items == 0) {
616 /* Wake up traversers-to-be */
617 (void) pthread_cond_signal(&table->cond);
619 if (wakeup) {
620 /* Wake up anyone else who wants this item */
621 (void) pthread_cond_signal(&item->lock);
625 * Delete if no references, not linked into list, and destructor
626 * established.
628 if (item->keychain < 0 &&
629 item->readers == 0 && item->writer == 0 &&
630 item->next == 0 &&
631 item->prv_item == 0 && item->nxt_item == 0 &&
632 table->destroyItem != 0)
633 (*table->destroyItem)(item);
635 (void) __nis_ulock_hash_table(table, 0, "nreli");
637 return (1);
641 * Return -1 if item checked out for both reading and writing, 1 if
642 * readonly, and 0 otherwise.
645 __nis_item_access(void *arg) {
647 __nis_hash_item_mt *item = arg;
649 if (item != 0) {
650 if (item->writer > 0) {
651 if (item->writer_id != pthread_self())
652 abort();
653 return (-1);
654 } else if (item->readers > 0) {
655 return (1);
659 return (0);
663 * __nis_scan_table_mt()
665 * Iterate over all items in a __nis_hash_table_mt. We ignore
666 * first/prv_item/nxt_item and scan in hash-chain order. The iterator
667 * function should *not* insert or delete items. If the iterator
668 * function returns TRUE the scan terminates. For compatibility with
669 * the existing non-MT nis_scan_table() this function has no return
670 * value.
672 void
673 __nis_scan_table_mt(
674 __nis_hash_table_mt *table,
675 bool_t (*func)(__nis_hash_item_mt *, void *),
676 void *funcarg)
678 int slot;
680 if (table == 0) {
681 return;
684 if (__nis_lock_hash_table(table, 1, "nstmt") == 0) {
685 syslog(LOG_DEBUG, "__nis_scan_table_mt: mutex lock failed ");
686 return;
689 for (slot = 0;
690 slot < sizeof (table->keys) / sizeof (table->keys[0]);
691 slot++) {
692 __nis_hash_item_mt *it;
694 for (it = table->keys[slot]; it != 0; it = it->next) {
695 if (TRUE == (*func)(it, funcarg)) {
696 break;
700 if (__nis_ulock_hash_table(table, 1, "nstmt") == 0)
701 syslog(LOG_DEBUG, "__nis_scan_table_mt: mutex unlock failed ");