17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate * with the License.
87c478bd9Sstevel@tonic-gate *
97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate * and limitations under the License.
137c478bd9Sstevel@tonic-gate *
147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate *
207c478bd9Sstevel@tonic-gate * CDDL HEADER END
217c478bd9Sstevel@tonic-gate */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate * db_table.cc
247c478bd9Sstevel@tonic-gate *
257c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
267c478bd9Sstevel@tonic-gate * Use is subject to license terms.
277c478bd9Sstevel@tonic-gate */
287c478bd9Sstevel@tonic-gate
297c478bd9Sstevel@tonic-gate #include <stdio.h>
307c478bd9Sstevel@tonic-gate #include <malloc.h>
317c478bd9Sstevel@tonic-gate #include <string.h>
327c478bd9Sstevel@tonic-gate #include <stdlib.h> /* srand48() */
337c478bd9Sstevel@tonic-gate #include <lber.h>
347c478bd9Sstevel@tonic-gate #include <ldap.h>
357c478bd9Sstevel@tonic-gate #include "db_headers.h"
367c478bd9Sstevel@tonic-gate #include "db_table.h"
377c478bd9Sstevel@tonic-gate #include "db_pickle.h" /* for dump and load */
387c478bd9Sstevel@tonic-gate #include "db_entry.h"
397c478bd9Sstevel@tonic-gate #include "nisdb_mt.h"
407c478bd9Sstevel@tonic-gate
417c478bd9Sstevel@tonic-gate #include "ldap_parse.h"
427c478bd9Sstevel@tonic-gate #include "ldap_util.h"
437c478bd9Sstevel@tonic-gate #include "ldap_map.h"
447c478bd9Sstevel@tonic-gate #include "ldap_xdr.h"
457c478bd9Sstevel@tonic-gate #include "nis_hashitem.h"
467c478bd9Sstevel@tonic-gate #include "nisdb_ldap.h"
477c478bd9Sstevel@tonic-gate #include "nis_parse_ldap_conf.h"
487c478bd9Sstevel@tonic-gate
497c478bd9Sstevel@tonic-gate static time_t maxTimeT;
507c478bd9Sstevel@tonic-gate
517c478bd9Sstevel@tonic-gate /*
527c478bd9Sstevel@tonic-gate * Find the largest (positive) value of time_t.
537c478bd9Sstevel@tonic-gate *
547c478bd9Sstevel@tonic-gate * If time_t is unsigned, the largest possible value is just ~0.
557c478bd9Sstevel@tonic-gate * However, if it's signed, then ~0 is negative. Since lint (for
567c478bd9Sstevel@tonic-gate * sure), and perhaps the compiler too, dislike comparing an
577c478bd9Sstevel@tonic-gate * unsigned quantity to see if it's less than zero, we compare
587c478bd9Sstevel@tonic-gate * to one instead. If negative, the largest possible value is
597c478bd9Sstevel@tonic-gate * th inverse of 2**(N-1), where N is the number of bits in a
607c478bd9Sstevel@tonic-gate * time_t.
617c478bd9Sstevel@tonic-gate */
627c478bd9Sstevel@tonic-gate extern "C" {
637c478bd9Sstevel@tonic-gate static void
__setMaxTimeT(void)647c478bd9Sstevel@tonic-gate __setMaxTimeT(void)
657c478bd9Sstevel@tonic-gate {
667c478bd9Sstevel@tonic-gate unsigned char b[sizeof (time_t)];
677c478bd9Sstevel@tonic-gate int i;
687c478bd9Sstevel@tonic-gate
697c478bd9Sstevel@tonic-gate /* Compute ~0 for an unknown length integer */
707c478bd9Sstevel@tonic-gate for (i = 0; i < sizeof (time_t); i++) {
717c478bd9Sstevel@tonic-gate b[i] = 0xff;
727c478bd9Sstevel@tonic-gate }
737c478bd9Sstevel@tonic-gate /* Set maxTimeT to ~0 of appropriate length */
747c478bd9Sstevel@tonic-gate (void) memcpy(&maxTimeT, b, sizeof (time_t));
757c478bd9Sstevel@tonic-gate
767c478bd9Sstevel@tonic-gate if (maxTimeT < 1)
777c478bd9Sstevel@tonic-gate maxTimeT = ~(1<<((8*sizeof (maxTimeT))-1));
787c478bd9Sstevel@tonic-gate }
797c478bd9Sstevel@tonic-gate #pragma init(__setMaxTimeT)
807c478bd9Sstevel@tonic-gate }
817c478bd9Sstevel@tonic-gate
827c478bd9Sstevel@tonic-gate /* How much to grow table by */
837c478bd9Sstevel@tonic-gate #define DB_TABLE_GROWTH_INCREMENT 1024
847c478bd9Sstevel@tonic-gate
857c478bd9Sstevel@tonic-gate /* 0'th not used; might be confusing. */
867c478bd9Sstevel@tonic-gate #define DB_TABLE_START 1
877c478bd9Sstevel@tonic-gate
887c478bd9Sstevel@tonic-gate /* prevents wrap around numbers from being passed */
897c478bd9Sstevel@tonic-gate #define CALLOC_LIMIT 536870911
907c478bd9Sstevel@tonic-gate
917c478bd9Sstevel@tonic-gate /* Initial table sizes to use before using 1K increments. */
927c478bd9Sstevel@tonic-gate /* This helps conserve memory usage when there are lots of small tables. */
937c478bd9Sstevel@tonic-gate static int tabsizes[] = {
947c478bd9Sstevel@tonic-gate 16,
957c478bd9Sstevel@tonic-gate 128,
967c478bd9Sstevel@tonic-gate 512,
977c478bd9Sstevel@tonic-gate DB_TABLE_GROWTH_INCREMENT,
987c478bd9Sstevel@tonic-gate 0
997c478bd9Sstevel@tonic-gate };
1007c478bd9Sstevel@tonic-gate
1017c478bd9Sstevel@tonic-gate /* Returns the next size to use for table */
1027c478bd9Sstevel@tonic-gate static long unsigned
get_new_table_size(long unsigned oldsize)1037c478bd9Sstevel@tonic-gate get_new_table_size(long unsigned oldsize)
1047c478bd9Sstevel@tonic-gate {
1057c478bd9Sstevel@tonic-gate long unsigned newsize = 0, n;
1067c478bd9Sstevel@tonic-gate if (oldsize == 0)
1077c478bd9Sstevel@tonic-gate newsize = tabsizes[0];
1087c478bd9Sstevel@tonic-gate else {
1097c478bd9Sstevel@tonic-gate for (n = 0; newsize = tabsizes[n++]; )
1107c478bd9Sstevel@tonic-gate if (oldsize == newsize) {
1117c478bd9Sstevel@tonic-gate newsize = tabsizes[n]; /* get next size */
1127c478bd9Sstevel@tonic-gate break;
1137c478bd9Sstevel@tonic-gate }
1147c478bd9Sstevel@tonic-gate if (newsize == 0)
1157c478bd9Sstevel@tonic-gate newsize = oldsize + DB_TABLE_GROWTH_INCREMENT;
1167c478bd9Sstevel@tonic-gate }
1177c478bd9Sstevel@tonic-gate return (newsize);
1187c478bd9Sstevel@tonic-gate }
1197c478bd9Sstevel@tonic-gate
1207c478bd9Sstevel@tonic-gate
1217c478bd9Sstevel@tonic-gate /* destructor */
~db_free_list()1227c478bd9Sstevel@tonic-gate db_free_list::~db_free_list()
1237c478bd9Sstevel@tonic-gate {
1247c478bd9Sstevel@tonic-gate WRITELOCKV(this, "w db_free_list::~db_free_list");
1257c478bd9Sstevel@tonic-gate reset(); /* free list entries */
1267c478bd9Sstevel@tonic-gate DESTROYRW(free_list);
1277c478bd9Sstevel@tonic-gate }
1287c478bd9Sstevel@tonic-gate
1297c478bd9Sstevel@tonic-gate void
reset()1307c478bd9Sstevel@tonic-gate db_free_list::reset()
1317c478bd9Sstevel@tonic-gate {
1327c478bd9Sstevel@tonic-gate db_free_entry *current, *nextentry;
1337c478bd9Sstevel@tonic-gate
1347c478bd9Sstevel@tonic-gate WRITELOCKV(this, "w db_free_list::reset");
1357c478bd9Sstevel@tonic-gate for (current = head; current != NULL; ) {
1367c478bd9Sstevel@tonic-gate nextentry = current->next;
1377c478bd9Sstevel@tonic-gate delete current;
1387c478bd9Sstevel@tonic-gate current = nextentry;
1397c478bd9Sstevel@tonic-gate }
1407c478bd9Sstevel@tonic-gate head = NULL;
1417c478bd9Sstevel@tonic-gate count = 0;
1427c478bd9Sstevel@tonic-gate WRITEUNLOCKV(this, "wu db_free_list::reset");
1437c478bd9Sstevel@tonic-gate }
1447c478bd9Sstevel@tonic-gate
1457c478bd9Sstevel@tonic-gate /* Returns the location of a free entry, or NULL, if there aren't any. */
1467c478bd9Sstevel@tonic-gate entryp
pop()1477c478bd9Sstevel@tonic-gate db_free_list::pop()
1487c478bd9Sstevel@tonic-gate {
1497c478bd9Sstevel@tonic-gate WRITELOCK(this, NULL, "w db_free_list::pop");
1507c478bd9Sstevel@tonic-gate if (head == NULL) {
1517c478bd9Sstevel@tonic-gate WRITEUNLOCK(this, NULL, "wu db_free_list::pop");
1527c478bd9Sstevel@tonic-gate return (NULL);
1537c478bd9Sstevel@tonic-gate }
1547c478bd9Sstevel@tonic-gate db_free_entry* old_head = head;
1557c478bd9Sstevel@tonic-gate entryp found = head->where;
1567c478bd9Sstevel@tonic-gate head = head->next;
1577c478bd9Sstevel@tonic-gate delete old_head;
1587c478bd9Sstevel@tonic-gate --count;
1597c478bd9Sstevel@tonic-gate WRITEUNLOCK(this, found, "wu db_free_list::pop");
1607c478bd9Sstevel@tonic-gate return (found);
1617c478bd9Sstevel@tonic-gate }
1627c478bd9Sstevel@tonic-gate
1637c478bd9Sstevel@tonic-gate /*
1647c478bd9Sstevel@tonic-gate * Adds given location to the free list.
1657c478bd9Sstevel@tonic-gate * Returns TRUE if successful, FALSE otherwise (when out of memory).
1667c478bd9Sstevel@tonic-gate */
1677c478bd9Sstevel@tonic-gate bool_t
push(entryp tabloc)1687c478bd9Sstevel@tonic-gate db_free_list::push(entryp tabloc)
1697c478bd9Sstevel@tonic-gate {
1707c478bd9Sstevel@tonic-gate db_free_entry * newentry = new db_free_entry;
1717c478bd9Sstevel@tonic-gate
1727c478bd9Sstevel@tonic-gate WRITELOCK(this, FALSE, "w db_free_list::push");
1737c478bd9Sstevel@tonic-gate if (newentry == NULL) {
1747c478bd9Sstevel@tonic-gate WRITEUNLOCK(this, FALSE, "wu db_free_list::push");
1757c478bd9Sstevel@tonic-gate FATAL3("db_free_list::push: cannot allocation space",
1767c478bd9Sstevel@tonic-gate DB_MEMORY_LIMIT, FALSE);
1777c478bd9Sstevel@tonic-gate }
1787c478bd9Sstevel@tonic-gate newentry->where = tabloc;
1797c478bd9Sstevel@tonic-gate newentry->next = head;
1807c478bd9Sstevel@tonic-gate head = newentry;
1817c478bd9Sstevel@tonic-gate ++count;
1827c478bd9Sstevel@tonic-gate WRITEUNLOCK(this, TRUE, "wu db_free_list::push");
1837c478bd9Sstevel@tonic-gate return (TRUE);
1847c478bd9Sstevel@tonic-gate }
1857c478bd9Sstevel@tonic-gate
1867c478bd9Sstevel@tonic-gate /*
1877c478bd9Sstevel@tonic-gate * Returns in a vector the information in the free list.
1887c478bd9Sstevel@tonic-gate * Vector returned is of form: [n free cells][n1][n2][loc1], ..[locn].
1897c478bd9Sstevel@tonic-gate * Leave the first 'n' cells free.
1907c478bd9Sstevel@tonic-gate * n1 is the number of entries that should be in the freelist.
1917c478bd9Sstevel@tonic-gate * n2 is the number of entries actually found in the freelist.
1927c478bd9Sstevel@tonic-gate * [loc1...locn] are the entries. n2 <= n1 because we never count beyond n1.
1937c478bd9Sstevel@tonic-gate * It is up to the caller to free the returned vector when he is through.
1947c478bd9Sstevel@tonic-gate */
1957c478bd9Sstevel@tonic-gate long *
stats(int nslots)1967c478bd9Sstevel@tonic-gate db_free_list::stats(int nslots)
1977c478bd9Sstevel@tonic-gate {
1987c478bd9Sstevel@tonic-gate long realcount = 0,
1997c478bd9Sstevel@tonic-gate i,
2007c478bd9Sstevel@tonic-gate liststart = nslots, // start of freelist
2017c478bd9Sstevel@tonic-gate listend = nslots+count+2; // end of freelist
2027c478bd9Sstevel@tonic-gate db_free_entry_p current = head;
2037c478bd9Sstevel@tonic-gate
2047c478bd9Sstevel@tonic-gate READLOCK(this, NULL, "r db_free_list::stats");
2057c478bd9Sstevel@tonic-gate
2067c478bd9Sstevel@tonic-gate long *answer = (long *)malloc((int)(listend)*sizeof (long));
2077c478bd9Sstevel@tonic-gate if (answer == 0) {
2087c478bd9Sstevel@tonic-gate READUNLOCK(this, NULL, "ru db_free_list::stats");
2097c478bd9Sstevel@tonic-gate FATAL3("db_free_list::stats: cannot allocation space",
2107c478bd9Sstevel@tonic-gate DB_MEMORY_LIMIT, NULL);
2117c478bd9Sstevel@tonic-gate }
2127c478bd9Sstevel@tonic-gate
2137c478bd9Sstevel@tonic-gate answer[liststart] = count; /* size of freelist */
2147c478bd9Sstevel@tonic-gate
2157c478bd9Sstevel@tonic-gate for (i = liststart+2; i < listend && current != NULL; i++) {
2167c478bd9Sstevel@tonic-gate answer[i] = current->where;
2177c478bd9Sstevel@tonic-gate current = current->next;
2187c478bd9Sstevel@tonic-gate ++realcount;
2197c478bd9Sstevel@tonic-gate }
2207c478bd9Sstevel@tonic-gate
2217c478bd9Sstevel@tonic-gate answer[liststart+1] = realcount;
2227c478bd9Sstevel@tonic-gate READUNLOCK(this, answer, "ru db_free_list::stats");
2237c478bd9Sstevel@tonic-gate return (answer);
2247c478bd9Sstevel@tonic-gate }
2257c478bd9Sstevel@tonic-gate
2267c478bd9Sstevel@tonic-gate
2277c478bd9Sstevel@tonic-gate /* Set default values for the mapping structure */
2287c478bd9Sstevel@tonic-gate void
initMappingStruct(__nisdb_table_mapping_t * m)2297c478bd9Sstevel@tonic-gate db_table::initMappingStruct(__nisdb_table_mapping_t *m) {
2307c478bd9Sstevel@tonic-gate if (m == 0)
2317c478bd9Sstevel@tonic-gate return;
2327c478bd9Sstevel@tonic-gate
2337c478bd9Sstevel@tonic-gate m->initTtlLo = (ldapDBTableMapping.initTtlLo > 0) ?
2347c478bd9Sstevel@tonic-gate ldapDBTableMapping.initTtlLo : (3600-1800);
2357c478bd9Sstevel@tonic-gate m->initTtlHi = (ldapDBTableMapping.initTtlHi > 0) ?
2367c478bd9Sstevel@tonic-gate ldapDBTableMapping.initTtlHi : (3600+1800);
2377c478bd9Sstevel@tonic-gate m->ttl = (ldapDBTableMapping.ttl > 0) ?
2387c478bd9Sstevel@tonic-gate ldapDBTableMapping.ttl : 3600;
2397c478bd9Sstevel@tonic-gate m->enumExpire = 0;
2407c478bd9Sstevel@tonic-gate m->fromLDAP = FALSE;
2417c478bd9Sstevel@tonic-gate m->toLDAP = FALSE;
2427c478bd9Sstevel@tonic-gate m->isMaster = FALSE;
2437c478bd9Sstevel@tonic-gate m->retrieveError = ldapDBTableMapping.retrieveError;
2447c478bd9Sstevel@tonic-gate m->retrieveErrorRetry.attempts =
2457c478bd9Sstevel@tonic-gate ldapDBTableMapping.retrieveErrorRetry.attempts;
2467c478bd9Sstevel@tonic-gate m->retrieveErrorRetry.timeout =
2477c478bd9Sstevel@tonic-gate ldapDBTableMapping.retrieveErrorRetry.timeout;
2487c478bd9Sstevel@tonic-gate m->storeError = ldapDBTableMapping.storeError;
2497c478bd9Sstevel@tonic-gate m->storeErrorRetry.attempts =
2507c478bd9Sstevel@tonic-gate ldapDBTableMapping.storeErrorRetry.attempts;
2517c478bd9Sstevel@tonic-gate m->storeErrorRetry.timeout =
2527c478bd9Sstevel@tonic-gate ldapDBTableMapping.storeErrorRetry.timeout;
2537c478bd9Sstevel@tonic-gate m->storeErrorDisp = ldapDBTableMapping.storeErrorDisp;
2547c478bd9Sstevel@tonic-gate m->refreshError = ldapDBTableMapping.refreshError;
2557c478bd9Sstevel@tonic-gate m->refreshErrorRetry.attempts =
2567c478bd9Sstevel@tonic-gate ldapDBTableMapping.refreshErrorRetry.attempts;
2577c478bd9Sstevel@tonic-gate m->refreshErrorRetry.timeout =
2587c478bd9Sstevel@tonic-gate ldapDBTableMapping.refreshErrorRetry.timeout;
2597c478bd9Sstevel@tonic-gate m->matchFetch = ldapDBTableMapping.matchFetch;
2607c478bd9Sstevel@tonic-gate
2617c478bd9Sstevel@tonic-gate if (mapping.expire != 0)
2627c478bd9Sstevel@tonic-gate free(mapping.expire);
2637c478bd9Sstevel@tonic-gate m->expire = 0;
2647c478bd9Sstevel@tonic-gate
2657c478bd9Sstevel@tonic-gate if (m->tm != 0)
2667c478bd9Sstevel@tonic-gate free(m->tm);
2677c478bd9Sstevel@tonic-gate m->tm = 0;
2687c478bd9Sstevel@tonic-gate
2697c478bd9Sstevel@tonic-gate /*
2707c478bd9Sstevel@tonic-gate * The 'objType' field obviously indicates the type of object.
2717c478bd9Sstevel@tonic-gate * However, we also use it to tell us if we've retrieved mapping
2727c478bd9Sstevel@tonic-gate * data from LDAP or not; in the latter case, 'objType' is
2737c478bd9Sstevel@tonic-gate * NIS_BOGUS_OBJ. For purposes of maintaining expiration times,
2747c478bd9Sstevel@tonic-gate * we may need to know if the object is a table or a directory
2757c478bd9Sstevel@tonic-gate * _before_ we've retrieved any mapping data. Hence the 'expireType'
2767c478bd9Sstevel@tonic-gate * field, which starts as NIS_BOGUS_OBJ (meaning, don't know, assume
2777c478bd9Sstevel@tonic-gate * directory for now), and later is set to NIS_DIRECTORY_OBJ
2787c478bd9Sstevel@tonic-gate * (always keep expiration data, in case one of the dir entries
2797c478bd9Sstevel@tonic-gate * is mapped) or NIS_TABLE_OBJ (only need expiration data if
2807c478bd9Sstevel@tonic-gate * tha table is mapped).
2817c478bd9Sstevel@tonic-gate */
2827c478bd9Sstevel@tonic-gate m->objType = NIS_BOGUS_OBJ;
2837c478bd9Sstevel@tonic-gate m->expireType = NIS_BOGUS_OBJ;
2847c478bd9Sstevel@tonic-gate if (m->objName != 0)
2857c478bd9Sstevel@tonic-gate free(m->objName);
2867c478bd9Sstevel@tonic-gate m->objName = 0;
2877c478bd9Sstevel@tonic-gate }
2887c478bd9Sstevel@tonic-gate
2897c478bd9Sstevel@tonic-gate void
db_table_ldap_init(void)2907c478bd9Sstevel@tonic-gate db_table::db_table_ldap_init(void) {
2917c478bd9Sstevel@tonic-gate
2927c478bd9Sstevel@tonic-gate INITRW(table);
2937c478bd9Sstevel@tonic-gate
2947c478bd9Sstevel@tonic-gate enumMode.flag = 0;
2957c478bd9Sstevel@tonic-gate enumCount.flag = 0;
2967c478bd9Sstevel@tonic-gate enumIndex.ptr = 0;
2977c478bd9Sstevel@tonic-gate enumArray.ptr = 0;
2987c478bd9Sstevel@tonic-gate
2997c478bd9Sstevel@tonic-gate mapping.expire = 0;
3007c478bd9Sstevel@tonic-gate mapping.tm = 0;
3017c478bd9Sstevel@tonic-gate mapping.objName = 0;
3027c478bd9Sstevel@tonic-gate mapping.isDeferredTable = FALSE;
3037c478bd9Sstevel@tonic-gate (void) mutex_init(&mapping.enumLock, 0, 0);
3047c478bd9Sstevel@tonic-gate mapping.enumTid = 0;
3057c478bd9Sstevel@tonic-gate mapping.enumStat = -1;
3067c478bd9Sstevel@tonic-gate mapping.enumDeferred = 0;
3077c478bd9Sstevel@tonic-gate mapping.enumEntries = 0;
3087c478bd9Sstevel@tonic-gate mapping.enumTime = 0;
3097c478bd9Sstevel@tonic-gate }
3107c478bd9Sstevel@tonic-gate
3117c478bd9Sstevel@tonic-gate /* db_table constructor */
db_table()3127c478bd9Sstevel@tonic-gate db_table::db_table() : freelist()
3137c478bd9Sstevel@tonic-gate {
3147c478bd9Sstevel@tonic-gate tab = NULL;
3157c478bd9Sstevel@tonic-gate table_size = 0;
3167c478bd9Sstevel@tonic-gate last_used = 0;
3177c478bd9Sstevel@tonic-gate count = 0;
3187c478bd9Sstevel@tonic-gate
3197c478bd9Sstevel@tonic-gate db_table_ldap_init();
3207c478bd9Sstevel@tonic-gate initMappingStruct(&mapping);
3217c478bd9Sstevel@tonic-gate
3227c478bd9Sstevel@tonic-gate /* grow(); */
3237c478bd9Sstevel@tonic-gate }
3247c478bd9Sstevel@tonic-gate
3257c478bd9Sstevel@tonic-gate /*
3267c478bd9Sstevel@tonic-gate * db_table destructor:
3277c478bd9Sstevel@tonic-gate * 1. Get rid of contents of freelist
3287c478bd9Sstevel@tonic-gate * 2. delete all entries hanging off table
3297c478bd9Sstevel@tonic-gate * 3. get rid of table itself
3307c478bd9Sstevel@tonic-gate */
~db_table()3317c478bd9Sstevel@tonic-gate db_table::~db_table()
3327c478bd9Sstevel@tonic-gate {
3337c478bd9Sstevel@tonic-gate WRITELOCKV(this, "w db_table::~db_table");
3347c478bd9Sstevel@tonic-gate reset();
3357c478bd9Sstevel@tonic-gate DESTROYRW(table);
3367c478bd9Sstevel@tonic-gate }
3377c478bd9Sstevel@tonic-gate
3387c478bd9Sstevel@tonic-gate /* reset size and pointers */
3397c478bd9Sstevel@tonic-gate void
reset()3407c478bd9Sstevel@tonic-gate db_table::reset()
3417c478bd9Sstevel@tonic-gate {
3427c478bd9Sstevel@tonic-gate int i, done = 0;
3437c478bd9Sstevel@tonic-gate
3447c478bd9Sstevel@tonic-gate WRITELOCKV(this, "w db_table::reset");
3457c478bd9Sstevel@tonic-gate freelist.reset();
3467c478bd9Sstevel@tonic-gate
3477c478bd9Sstevel@tonic-gate /* Add sanity check in case of table corruption */
3487c478bd9Sstevel@tonic-gate if (tab != NULL) {
3497c478bd9Sstevel@tonic-gate for (i = 0;
3507c478bd9Sstevel@tonic-gate i <= last_used && i < table_size && done < count;
3517c478bd9Sstevel@tonic-gate i++) {
3527c478bd9Sstevel@tonic-gate if (tab[i]) {
3537c478bd9Sstevel@tonic-gate free_entry(tab[i]);
3547c478bd9Sstevel@tonic-gate ++done;
3557c478bd9Sstevel@tonic-gate }
3567c478bd9Sstevel@tonic-gate }
3577c478bd9Sstevel@tonic-gate }
3587c478bd9Sstevel@tonic-gate
3597c478bd9Sstevel@tonic-gate delete tab;
3607c478bd9Sstevel@tonic-gate table_size = last_used = count = 0;
3617c478bd9Sstevel@tonic-gate tab = NULL;
3627c478bd9Sstevel@tonic-gate sfree(mapping.expire);
3637c478bd9Sstevel@tonic-gate mapping.expire = NULL;
3647c478bd9Sstevel@tonic-gate mapping.objType = NIS_BOGUS_OBJ;
3657c478bd9Sstevel@tonic-gate mapping.expireType = NIS_BOGUS_OBJ;
3667c478bd9Sstevel@tonic-gate sfree(mapping.objName);
3677c478bd9Sstevel@tonic-gate mapping.objName = 0;
3687c478bd9Sstevel@tonic-gate /* Leave other values of the mapping structure unchanged */
3697c478bd9Sstevel@tonic-gate enumMode.flag = 0;
3707c478bd9Sstevel@tonic-gate enumCount.flag = 0;
3717c478bd9Sstevel@tonic-gate sfree(enumIndex.ptr);
3727c478bd9Sstevel@tonic-gate enumIndex.ptr = 0;
3737c478bd9Sstevel@tonic-gate sfree(enumArray.ptr);
3747c478bd9Sstevel@tonic-gate enumArray.ptr = 0;
3757c478bd9Sstevel@tonic-gate WRITEUNLOCKV(this, "wu db_table::reset");
3767c478bd9Sstevel@tonic-gate }
3777c478bd9Sstevel@tonic-gate
3787c478bd9Sstevel@tonic-gate db_status
allocateExpire(long oldSize,long newSize)3797c478bd9Sstevel@tonic-gate db_table::allocateExpire(long oldSize, long newSize) {
3807c478bd9Sstevel@tonic-gate time_t *newExpire;
3817c478bd9Sstevel@tonic-gate
3827c478bd9Sstevel@tonic-gate newExpire = (time_t *)realloc(mapping.expire,
3837c478bd9Sstevel@tonic-gate newSize * sizeof (mapping.expire[0]));
3847c478bd9Sstevel@tonic-gate if (newExpire != NULL) {
3857c478bd9Sstevel@tonic-gate /* Initialize new portion */
3867c478bd9Sstevel@tonic-gate (void) memset(&newExpire[oldSize], 0,
3877c478bd9Sstevel@tonic-gate (newSize-oldSize) * sizeof (newExpire[0]));
3887c478bd9Sstevel@tonic-gate mapping.expire = newExpire;
3897c478bd9Sstevel@tonic-gate } else {
3907c478bd9Sstevel@tonic-gate return (DB_MEMORY_LIMIT);
3917c478bd9Sstevel@tonic-gate }
3927c478bd9Sstevel@tonic-gate
3937c478bd9Sstevel@tonic-gate return (DB_SUCCESS);
3947c478bd9Sstevel@tonic-gate }
3957c478bd9Sstevel@tonic-gate
3967c478bd9Sstevel@tonic-gate db_status
allocateEnumArray(long oldSize,long newSize)3977c478bd9Sstevel@tonic-gate db_table::allocateEnumArray(long oldSize, long newSize) {
3987c478bd9Sstevel@tonic-gate entry_object **newEnumArray;
399*8d0852b7SRichard Lowe const char *myself = "db_table::allocateEnumArray";
4007c478bd9Sstevel@tonic-gate
4017c478bd9Sstevel@tonic-gate if (enumCount.flag > 0) {
4027c478bd9Sstevel@tonic-gate if (enumIndex.ptr == 0) {
4037c478bd9Sstevel@tonic-gate enumIndex.ptr = (entryp *)am(myself, enumCount.flag *
4047c478bd9Sstevel@tonic-gate sizeof (entryp));
4057c478bd9Sstevel@tonic-gate if (enumIndex.ptr == 0)
4067c478bd9Sstevel@tonic-gate return (DB_MEMORY_LIMIT);
4077c478bd9Sstevel@tonic-gate }
4087c478bd9Sstevel@tonic-gate oldSize = 0;
4097c478bd9Sstevel@tonic-gate newSize = enumCount.flag;
4107c478bd9Sstevel@tonic-gate }
4117c478bd9Sstevel@tonic-gate newEnumArray = (entry_object **)realloc(enumArray.ptr,
4127c478bd9Sstevel@tonic-gate newSize * sizeof (entry_object *));
4137c478bd9Sstevel@tonic-gate if (newEnumArray != 0 && newSize > oldSize) {
4147c478bd9Sstevel@tonic-gate (void) memcpy(&newEnumArray[oldSize], &tab[oldSize],
4157c478bd9Sstevel@tonic-gate (newSize-oldSize) * sizeof (entry_object *));
4167c478bd9Sstevel@tonic-gate enumArray.ptr = newEnumArray;
4177c478bd9Sstevel@tonic-gate } else if (newEnumArray == 0) {
4187c478bd9Sstevel@tonic-gate return (DB_MEMORY_LIMIT);
4197c478bd9Sstevel@tonic-gate }
4207c478bd9Sstevel@tonic-gate
4217c478bd9Sstevel@tonic-gate return (DB_SUCCESS);
4227c478bd9Sstevel@tonic-gate }
4237c478bd9Sstevel@tonic-gate
4247c478bd9Sstevel@tonic-gate /* Expand the table. Fatal error if insufficient memory. */
4257c478bd9Sstevel@tonic-gate void
grow()4267c478bd9Sstevel@tonic-gate db_table::grow()
4277c478bd9Sstevel@tonic-gate {
4287c478bd9Sstevel@tonic-gate WRITELOCKV(this, "w db_table::grow");
4297c478bd9Sstevel@tonic-gate long oldsize = table_size;
4307c478bd9Sstevel@tonic-gate entry_object_p *oldtab = tab;
4317c478bd9Sstevel@tonic-gate long i;
4327c478bd9Sstevel@tonic-gate
4337c478bd9Sstevel@tonic-gate table_size = get_new_table_size(oldsize);
4347c478bd9Sstevel@tonic-gate
4357c478bd9Sstevel@tonic-gate #ifdef DEBUG
4367c478bd9Sstevel@tonic-gate fprintf(stderr, "db_table GROWING to %d\n", table_size);
4377c478bd9Sstevel@tonic-gate #endif
4387c478bd9Sstevel@tonic-gate
4397c478bd9Sstevel@tonic-gate if (table_size > CALLOC_LIMIT) {
4407c478bd9Sstevel@tonic-gate table_size = oldsize;
4417c478bd9Sstevel@tonic-gate WRITEUNLOCKV(this, "wu db_table::grow");
4427c478bd9Sstevel@tonic-gate FATAL("db_table::grow: table size exceeds calloc limit",
4437c478bd9Sstevel@tonic-gate DB_MEMORY_LIMIT);
4447c478bd9Sstevel@tonic-gate }
4457c478bd9Sstevel@tonic-gate
4467c478bd9Sstevel@tonic-gate // if ((tab = new entry_object_p[table_size]) == NULL)
4477c478bd9Sstevel@tonic-gate if ((tab = (entry_object_p*)
4487c478bd9Sstevel@tonic-gate calloc((unsigned int) table_size,
4497c478bd9Sstevel@tonic-gate sizeof (entry_object_p))) == NULL) {
4507c478bd9Sstevel@tonic-gate tab = oldtab; // restore previous table info
4517c478bd9Sstevel@tonic-gate table_size = oldsize;
4527c478bd9Sstevel@tonic-gate WRITEUNLOCKV(this, "wu db_table::grow");
4537c478bd9Sstevel@tonic-gate FATAL("db_table::grow: cannot allocate space", DB_MEMORY_LIMIT);
4547c478bd9Sstevel@tonic-gate }
4557c478bd9Sstevel@tonic-gate
4567c478bd9Sstevel@tonic-gate /*
4577c478bd9Sstevel@tonic-gate * For directories, we may need the expire time array even if the
4587c478bd9Sstevel@tonic-gate * directory itself isn't mapped. If the objType and expireType both
4597c478bd9Sstevel@tonic-gate * are bogus, we don't know yet if this is a table or a directory,
4607c478bd9Sstevel@tonic-gate * and must proceed accordingly.
4617c478bd9Sstevel@tonic-gate */
4627c478bd9Sstevel@tonic-gate if (mapping.objType == NIS_DIRECTORY_OBJ ||
4637c478bd9Sstevel@tonic-gate mapping.expireType != NIS_TABLE_OBJ ||
4647c478bd9Sstevel@tonic-gate mapping.fromLDAP) {
4657c478bd9Sstevel@tonic-gate db_status stat = allocateExpire(oldsize, table_size);
4667c478bd9Sstevel@tonic-gate if (stat != DB_SUCCESS) {
4677c478bd9Sstevel@tonic-gate free(tab);
4687c478bd9Sstevel@tonic-gate tab = oldtab;
4697c478bd9Sstevel@tonic-gate table_size = oldsize;
4707c478bd9Sstevel@tonic-gate WRITEUNLOCKV(this, "wu db_table::grow expire");
4717c478bd9Sstevel@tonic-gate FATAL(
4727c478bd9Sstevel@tonic-gate "db_table::grow: cannot allocate space for expire", stat);
4737c478bd9Sstevel@tonic-gate }
4747c478bd9Sstevel@tonic-gate }
4757c478bd9Sstevel@tonic-gate
4767c478bd9Sstevel@tonic-gate if (oldtab != NULL) {
4777c478bd9Sstevel@tonic-gate for (i = 0; i < oldsize; i++) { // transfer old to new
4787c478bd9Sstevel@tonic-gate tab[i] = oldtab[i];
4797c478bd9Sstevel@tonic-gate }
4807c478bd9Sstevel@tonic-gate delete oldtab;
4817c478bd9Sstevel@tonic-gate }
4827c478bd9Sstevel@tonic-gate
4837c478bd9Sstevel@tonic-gate if (enumMode.flag) {
4847c478bd9Sstevel@tonic-gate db_status stat = allocateEnumArray(oldsize, table_size);
4857c478bd9Sstevel@tonic-gate if (stat != DB_SUCCESS) {
4867c478bd9Sstevel@tonic-gate free(tab);
4877c478bd9Sstevel@tonic-gate tab = oldtab;
4887c478bd9Sstevel@tonic-gate table_size = oldsize;
4897c478bd9Sstevel@tonic-gate WRITEUNLOCKV(this, "wu db_table::grow enumArray");
4907c478bd9Sstevel@tonic-gate FATAL(
4917c478bd9Sstevel@tonic-gate "db_table::grow: cannot allocate space for enumArray", stat);
4927c478bd9Sstevel@tonic-gate }
4937c478bd9Sstevel@tonic-gate }
4947c478bd9Sstevel@tonic-gate
4957c478bd9Sstevel@tonic-gate WRITEUNLOCKV(this, "wu db_table::grow");
4967c478bd9Sstevel@tonic-gate }
4977c478bd9Sstevel@tonic-gate
4987c478bd9Sstevel@tonic-gate /*
4997c478bd9Sstevel@tonic-gate * Return the first entry in table, also return its position in
5007c478bd9Sstevel@tonic-gate * 'where'. Return NULL in both if no next entry is found.
5017c478bd9Sstevel@tonic-gate */
5027c478bd9Sstevel@tonic-gate entry_object*
first_entry(entryp * where)5037c478bd9Sstevel@tonic-gate db_table::first_entry(entryp * where)
5047c478bd9Sstevel@tonic-gate {
5057c478bd9Sstevel@tonic-gate ASSERTRHELD(table);
5067c478bd9Sstevel@tonic-gate if (count == 0 || tab == NULL) { /* empty table */
5077c478bd9Sstevel@tonic-gate *where = NULL;
5087c478bd9Sstevel@tonic-gate return (NULL);
5097c478bd9Sstevel@tonic-gate } else {
5107c478bd9Sstevel@tonic-gate entryp i;
5117c478bd9Sstevel@tonic-gate for (i = DB_TABLE_START;
5127c478bd9Sstevel@tonic-gate i < table_size && i <= last_used; i++) {
5137c478bd9Sstevel@tonic-gate if (tab[i] != NULL) {
5147c478bd9Sstevel@tonic-gate *where = i;
5157c478bd9Sstevel@tonic-gate return (tab[i]);
5167c478bd9Sstevel@tonic-gate }
5177c478bd9Sstevel@tonic-gate }
5187c478bd9Sstevel@tonic-gate }
5197c478bd9Sstevel@tonic-gate *where = NULL;
5207c478bd9Sstevel@tonic-gate return (NULL);
5217c478bd9Sstevel@tonic-gate }
5227c478bd9Sstevel@tonic-gate
5237c478bd9Sstevel@tonic-gate /*
5247c478bd9Sstevel@tonic-gate * Return the next entry in table from 'prev', also return its position in
5257c478bd9Sstevel@tonic-gate * 'newentry'. Return NULL in both if no next entry is found.
5267c478bd9Sstevel@tonic-gate */
5277c478bd9Sstevel@tonic-gate entry_object *
next_entry(entryp prev,entryp * newentry)5287c478bd9Sstevel@tonic-gate db_table::next_entry(entryp prev, entryp* newentry)
5297c478bd9Sstevel@tonic-gate {
5307c478bd9Sstevel@tonic-gate long i;
5317c478bd9Sstevel@tonic-gate
5327c478bd9Sstevel@tonic-gate ASSERTRHELD(table);
5337c478bd9Sstevel@tonic-gate if (prev >= table_size || tab == NULL || tab[prev] == NULL)
5347c478bd9Sstevel@tonic-gate return (NULL);
5357c478bd9Sstevel@tonic-gate for (i = prev+1; i < table_size && i <= last_used; i++) {
5367c478bd9Sstevel@tonic-gate if (tab[i] != NULL) {
5377c478bd9Sstevel@tonic-gate *newentry = i;
5387c478bd9Sstevel@tonic-gate return (tab[i]);
5397c478bd9Sstevel@tonic-gate }
5407c478bd9Sstevel@tonic-gate }
5417c478bd9Sstevel@tonic-gate *newentry = NULL;
5427c478bd9Sstevel@tonic-gate return (NULL);
5437c478bd9Sstevel@tonic-gate }
5447c478bd9Sstevel@tonic-gate
5457c478bd9Sstevel@tonic-gate /* Return entry at location 'where', NULL if location is invalid. */
5467c478bd9Sstevel@tonic-gate entry_object *
get_entry(entryp where)5477c478bd9Sstevel@tonic-gate db_table::get_entry(entryp where)
5487c478bd9Sstevel@tonic-gate {
5497c478bd9Sstevel@tonic-gate ASSERTRHELD(table);
5507c478bd9Sstevel@tonic-gate if (where < table_size && tab != NULL && tab[where] != NULL)
5517c478bd9Sstevel@tonic-gate return (tab[where]);
5527c478bd9Sstevel@tonic-gate else
5537c478bd9Sstevel@tonic-gate return (NULL);
5547c478bd9Sstevel@tonic-gate }
5557c478bd9Sstevel@tonic-gate
5567c478bd9Sstevel@tonic-gate void
setEntryExp(entryp where,entry_obj * obj,int initialLoad)5577c478bd9Sstevel@tonic-gate db_table::setEntryExp(entryp where, entry_obj *obj, int initialLoad) {
5587c478bd9Sstevel@tonic-gate nis_object *o;
559*8d0852b7SRichard Lowe const char *myself = "db_table::setEntryExp";
5607c478bd9Sstevel@tonic-gate
5617c478bd9Sstevel@tonic-gate /*
5627c478bd9Sstevel@tonic-gate * If we don't know what type of object this is yet, we
5637c478bd9Sstevel@tonic-gate * can find out now. If it's a directory, the pseudo-object
5647c478bd9Sstevel@tonic-gate * in column zero will have the type "IN_DIRECTORY";
5657c478bd9Sstevel@tonic-gate * otherwise, it's a table object.
5667c478bd9Sstevel@tonic-gate */
5677c478bd9Sstevel@tonic-gate if (mapping.expireType == NIS_BOGUS_OBJ) {
5687c478bd9Sstevel@tonic-gate if (obj != 0) {
5697c478bd9Sstevel@tonic-gate if (obj->en_type != 0 &&
5707c478bd9Sstevel@tonic-gate strcmp(obj->en_type, "IN_DIRECTORY") == 0) {
5717c478bd9Sstevel@tonic-gate mapping.expireType = NIS_DIRECTORY_OBJ;
5727c478bd9Sstevel@tonic-gate } else {
5737c478bd9Sstevel@tonic-gate mapping.expireType = NIS_TABLE_OBJ;
5747c478bd9Sstevel@tonic-gate if (!mapping.fromLDAP) {
5757c478bd9Sstevel@tonic-gate free(mapping.expire);
5767c478bd9Sstevel@tonic-gate mapping.expire = 0;
5777c478bd9Sstevel@tonic-gate }
5787c478bd9Sstevel@tonic-gate }
5797c478bd9Sstevel@tonic-gate }
5807c478bd9Sstevel@tonic-gate }
5817c478bd9Sstevel@tonic-gate
5827c478bd9Sstevel@tonic-gate /* Set the entry TTL */
5837c478bd9Sstevel@tonic-gate if (mapping.expire != NULL) {
5847c478bd9Sstevel@tonic-gate struct timeval now;
5857c478bd9Sstevel@tonic-gate time_t lo, hi, ttl;
5867c478bd9Sstevel@tonic-gate
5877c478bd9Sstevel@tonic-gate (void) gettimeofday(&now, NULL);
5887c478bd9Sstevel@tonic-gate if (mapping.expireType == NIS_TABLE_OBJ) {
5897c478bd9Sstevel@tonic-gate lo = mapping.initTtlLo;
5907c478bd9Sstevel@tonic-gate hi = mapping.initTtlHi;
5917c478bd9Sstevel@tonic-gate ttl = mapping.ttl;
5927c478bd9Sstevel@tonic-gate /* TTL == 0 means always expired */
5937c478bd9Sstevel@tonic-gate if (ttl == 0)
5947c478bd9Sstevel@tonic-gate ttl = -1;
5957c478bd9Sstevel@tonic-gate } else {
5967c478bd9Sstevel@tonic-gate __nis_table_mapping_t *t = 0;
5977c478bd9Sstevel@tonic-gate
5987c478bd9Sstevel@tonic-gate o = unmakePseudoEntryObj(obj, 0);
5997c478bd9Sstevel@tonic-gate if (o != 0) {
6007c478bd9Sstevel@tonic-gate __nis_buffer_t b = {0, 0};
6017c478bd9Sstevel@tonic-gate
6027c478bd9Sstevel@tonic-gate bp2buf(myself, &b, "%s.%s",
6037c478bd9Sstevel@tonic-gate o->zo_name, o->zo_domain);
6047c478bd9Sstevel@tonic-gate t = getObjMapping(b.buf, 0, 1, 0, 0);
6057c478bd9Sstevel@tonic-gate sfree(b.buf);
6067c478bd9Sstevel@tonic-gate nis_destroy_object(o);
6077c478bd9Sstevel@tonic-gate }
6087c478bd9Sstevel@tonic-gate
6097c478bd9Sstevel@tonic-gate if (t != 0) {
6107c478bd9Sstevel@tonic-gate lo = t->initTtlLo;
6117c478bd9Sstevel@tonic-gate hi = t->initTtlHi;
6127c478bd9Sstevel@tonic-gate ttl = t->ttl;
6137c478bd9Sstevel@tonic-gate /* TTL == 0 means always expired */
6147c478bd9Sstevel@tonic-gate if (ttl == 0)
6157c478bd9Sstevel@tonic-gate ttl = -1;
6167c478bd9Sstevel@tonic-gate } else {
6177c478bd9Sstevel@tonic-gate /*
6187c478bd9Sstevel@tonic-gate * No expiration time initialization
6197c478bd9Sstevel@tonic-gate * data. Cook up values that will
6207c478bd9Sstevel@tonic-gate * result in mapping.expire[where]
6217c478bd9Sstevel@tonic-gate * set to maxTimeT.
6227c478bd9Sstevel@tonic-gate */
6237c478bd9Sstevel@tonic-gate hi = lo = ttl = maxTimeT - now.tv_sec;
6247c478bd9Sstevel@tonic-gate }
6257c478bd9Sstevel@tonic-gate }
6267c478bd9Sstevel@tonic-gate
6277c478bd9Sstevel@tonic-gate if (initialLoad) {
6287c478bd9Sstevel@tonic-gate int interval = hi - lo + 1;
6297c478bd9Sstevel@tonic-gate if (interval <= 1) {
6307c478bd9Sstevel@tonic-gate mapping.expire[where] = now.tv_sec + lo;
6317c478bd9Sstevel@tonic-gate } else {
6327c478bd9Sstevel@tonic-gate srand48(now.tv_sec);
6337c478bd9Sstevel@tonic-gate mapping.expire[where] = now.tv_sec +
6347c478bd9Sstevel@tonic-gate (lrand48() % interval);
6357c478bd9Sstevel@tonic-gate }
6367c478bd9Sstevel@tonic-gate if (mapping.enumExpire == 0 ||
6377c478bd9Sstevel@tonic-gate mapping.expire[where] <
6387c478bd9Sstevel@tonic-gate mapping.enumExpire)
6397c478bd9Sstevel@tonic-gate mapping.enumExpire = mapping.expire[where];
6407c478bd9Sstevel@tonic-gate } else {
6417c478bd9Sstevel@tonic-gate mapping.expire[where] = now.tv_sec + ttl;
6427c478bd9Sstevel@tonic-gate }
6437c478bd9Sstevel@tonic-gate }
6447c478bd9Sstevel@tonic-gate }
6457c478bd9Sstevel@tonic-gate
6467c478bd9Sstevel@tonic-gate /*
6477c478bd9Sstevel@tonic-gate * Add given entry to table in first available slot (either look in freelist
6487c478bd9Sstevel@tonic-gate * or add to end of table) and return the the position of where the record
6497c478bd9Sstevel@tonic-gate * is placed. 'count' is incremented if entry is added. Table may grow
6507c478bd9Sstevel@tonic-gate * as a side-effect of the addition. Copy is made of input.
6517c478bd9Sstevel@tonic-gate */
6527c478bd9Sstevel@tonic-gate entryp
add_entry(entry_object * obj,int initialLoad)6537c478bd9Sstevel@tonic-gate db_table::add_entry(entry_object *obj, int initialLoad) {
6547c478bd9Sstevel@tonic-gate /*
6557c478bd9Sstevel@tonic-gate * We're returning an index of the table array, so the caller
6567c478bd9Sstevel@tonic-gate * should hold a lock until done with the index. To save us
6577c478bd9Sstevel@tonic-gate * the bother of upgrading to a write lock, it might as well
6587c478bd9Sstevel@tonic-gate * be a write lock to begin with.
6597c478bd9Sstevel@tonic-gate */
6607c478bd9Sstevel@tonic-gate ASSERTWHELD(table);
6617c478bd9Sstevel@tonic-gate entryp where = freelist.pop();
6627c478bd9Sstevel@tonic-gate if (where == NULL) { /* empty freelist */
6637c478bd9Sstevel@tonic-gate if (last_used >= (table_size-1)) /* full (> is for 0) */
6647c478bd9Sstevel@tonic-gate grow();
6657c478bd9Sstevel@tonic-gate where = ++last_used;
6667c478bd9Sstevel@tonic-gate }
6677c478bd9Sstevel@tonic-gate if (tab != NULL) {
6687c478bd9Sstevel@tonic-gate ++count;
6697c478bd9Sstevel@tonic-gate setEntryExp(where, obj, initialLoad);
6707c478bd9Sstevel@tonic-gate
6717c478bd9Sstevel@tonic-gate if (enumMode.flag)
6727c478bd9Sstevel@tonic-gate enumTouch(where);
6737c478bd9Sstevel@tonic-gate tab[where] = new_entry(obj);
6747c478bd9Sstevel@tonic-gate return (where);
6757c478bd9Sstevel@tonic-gate } else {
6767c478bd9Sstevel@tonic-gate return (NULL);
6777c478bd9Sstevel@tonic-gate }
6787c478bd9Sstevel@tonic-gate }
6797c478bd9Sstevel@tonic-gate
6807c478bd9Sstevel@tonic-gate /*
6817c478bd9Sstevel@tonic-gate * Replaces object at specified location by given entry.
6827c478bd9Sstevel@tonic-gate * Returns TRUE if replacement successful; FALSE otherwise.
6837c478bd9Sstevel@tonic-gate * There must something already at the specified location, otherwise,
6847c478bd9Sstevel@tonic-gate * replacement fails. Copy is not made of the input.
6857c478bd9Sstevel@tonic-gate * The pre-existing entry is freed.
6867c478bd9Sstevel@tonic-gate */
6877c478bd9Sstevel@tonic-gate bool_t
replace_entry(entryp where,entry_object * obj)6887c478bd9Sstevel@tonic-gate db_table::replace_entry(entryp where, entry_object * obj)
6897c478bd9Sstevel@tonic-gate {
6907c478bd9Sstevel@tonic-gate ASSERTWHELD(table);
6917c478bd9Sstevel@tonic-gate if (where < DB_TABLE_START || where >= table_size ||
6927c478bd9Sstevel@tonic-gate tab == NULL || tab[where] == NULL)
6937c478bd9Sstevel@tonic-gate return (FALSE);
6947c478bd9Sstevel@tonic-gate /* (Re-)set the entry TTL */
6957c478bd9Sstevel@tonic-gate setEntryExp(where, obj, 0);
6967c478bd9Sstevel@tonic-gate
6977c478bd9Sstevel@tonic-gate if (enumMode.flag)
6987c478bd9Sstevel@tonic-gate enumTouch(where);
6997c478bd9Sstevel@tonic-gate free_entry(tab[where]);
7007c478bd9Sstevel@tonic-gate tab[where] = obj;
7017c478bd9Sstevel@tonic-gate return (TRUE);
7027c478bd9Sstevel@tonic-gate }
7037c478bd9Sstevel@tonic-gate
7047c478bd9Sstevel@tonic-gate /*
7057c478bd9Sstevel@tonic-gate * Deletes entry at specified location. Returns TRUE if location is valid;
7067c478bd9Sstevel@tonic-gate * FALSE if location is invalid, or the freed location cannot be added to
7077c478bd9Sstevel@tonic-gate * the freelist. 'count' is decremented if the deletion occurs. The object
7087c478bd9Sstevel@tonic-gate * at that location is freed.
7097c478bd9Sstevel@tonic-gate */
7107c478bd9Sstevel@tonic-gate bool_t
delete_entry(entryp where)7117c478bd9Sstevel@tonic-gate db_table::delete_entry(entryp where)
7127c478bd9Sstevel@tonic-gate {
7137c478bd9Sstevel@tonic-gate bool_t ret = TRUE;
7147c478bd9Sstevel@tonic-gate
7157c478bd9Sstevel@tonic-gate ASSERTWHELD(table);
7167c478bd9Sstevel@tonic-gate if (where < DB_TABLE_START || where >= table_size ||
7177c478bd9Sstevel@tonic-gate tab == NULL || tab[where] == NULL)
7187c478bd9Sstevel@tonic-gate return (FALSE);
7197c478bd9Sstevel@tonic-gate if (mapping.expire != NULL) {
7207c478bd9Sstevel@tonic-gate mapping.expire[where] = 0;
7217c478bd9Sstevel@tonic-gate }
7227c478bd9Sstevel@tonic-gate if (enumMode.flag)
7237c478bd9Sstevel@tonic-gate enumTouch(where);
7247c478bd9Sstevel@tonic-gate free_entry(tab[where]);
7257c478bd9Sstevel@tonic-gate tab[where] = NULL; /* very important to set it to null */
7267c478bd9Sstevel@tonic-gate --count;
7277c478bd9Sstevel@tonic-gate if (where == last_used) { /* simple case, deleting from end */
7287c478bd9Sstevel@tonic-gate --last_used;
7297c478bd9Sstevel@tonic-gate return (TRUE);
7307c478bd9Sstevel@tonic-gate } else {
7317c478bd9Sstevel@tonic-gate return (freelist.push(where));
7327c478bd9Sstevel@tonic-gate }
7337c478bd9Sstevel@tonic-gate return (ret);
7347c478bd9Sstevel@tonic-gate }
7357c478bd9Sstevel@tonic-gate
7367c478bd9Sstevel@tonic-gate /*
7377c478bd9Sstevel@tonic-gate * Returns statistics of table.
7387c478bd9Sstevel@tonic-gate * [vector_size][table_size][last_used][count][freelist].
7397c478bd9Sstevel@tonic-gate * It is up to the caller to free the returned vector when his is through.
7407c478bd9Sstevel@tonic-gate * The free list is included if 'fl' is TRUE.
7417c478bd9Sstevel@tonic-gate */
7427c478bd9Sstevel@tonic-gate long *
stats(bool_t include_freelist)7437c478bd9Sstevel@tonic-gate db_table::stats(bool_t include_freelist)
7447c478bd9Sstevel@tonic-gate {
7457c478bd9Sstevel@tonic-gate long *answer;
7467c478bd9Sstevel@tonic-gate
7477c478bd9Sstevel@tonic-gate READLOCK(this, NULL, "r db_table::stats");
7487c478bd9Sstevel@tonic-gate if (include_freelist)
7497c478bd9Sstevel@tonic-gate answer = freelist.stats(3);
7507c478bd9Sstevel@tonic-gate else {
7517c478bd9Sstevel@tonic-gate answer = (long *)malloc(3*sizeof (long));
7527c478bd9Sstevel@tonic-gate }
7537c478bd9Sstevel@tonic-gate
7547c478bd9Sstevel@tonic-gate if (answer) {
7557c478bd9Sstevel@tonic-gate answer[0] = table_size;
7567c478bd9Sstevel@tonic-gate answer[1] = last_used;
7577c478bd9Sstevel@tonic-gate answer[2] = count;
7587c478bd9Sstevel@tonic-gate }
7597c478bd9Sstevel@tonic-gate READUNLOCK(this, answer, "ru db_table::stats");
7607c478bd9Sstevel@tonic-gate return (answer);
7617c478bd9Sstevel@tonic-gate }
7627c478bd9Sstevel@tonic-gate
7637c478bd9Sstevel@tonic-gate bool_t
configure(char * tablePath)7647c478bd9Sstevel@tonic-gate db_table::configure(char *tablePath) {
7657c478bd9Sstevel@tonic-gate long i;
7667c478bd9Sstevel@tonic-gate struct timeval now;
767*8d0852b7SRichard Lowe const char *myself = "db_table::configure";
7687c478bd9Sstevel@tonic-gate
7697c478bd9Sstevel@tonic-gate (void) gettimeofday(&now, NULL);
7707c478bd9Sstevel@tonic-gate
7717c478bd9Sstevel@tonic-gate WRITELOCK(this, FALSE, "db_table::configure w");
7727c478bd9Sstevel@tonic-gate
7737c478bd9Sstevel@tonic-gate /* (Re-)initialize from global info */
7747c478bd9Sstevel@tonic-gate initMappingStruct(&mapping);
7757c478bd9Sstevel@tonic-gate
7767c478bd9Sstevel@tonic-gate /* Retrieve table mapping for this table */
7777c478bd9Sstevel@tonic-gate mapping.tm = (__nis_table_mapping_t *)__nis_find_item_mt(
7787c478bd9Sstevel@tonic-gate tablePath, &ldapMappingList, 0, 0);
7797c478bd9Sstevel@tonic-gate if (mapping.tm != 0) {
7807c478bd9Sstevel@tonic-gate __nis_object_dn_t *odn = mapping.tm->objectDN;
7817c478bd9Sstevel@tonic-gate
7827c478bd9Sstevel@tonic-gate /*
7837c478bd9Sstevel@tonic-gate * The mapping.fromLDAP and mapping.toLDAP fields serve as
7847c478bd9Sstevel@tonic-gate * quick-references that tell us if mapping is enabled.
7857c478bd9Sstevel@tonic-gate * Hence, initialize them appropriately from the table
7867c478bd9Sstevel@tonic-gate * mapping objectDN.
7877c478bd9Sstevel@tonic-gate */
7887c478bd9Sstevel@tonic-gate while (odn != 0 && (!mapping.fromLDAP || !mapping.toLDAP)) {
7897c478bd9Sstevel@tonic-gate if (odn->read.scope != LDAP_SCOPE_UNKNOWN)
7907c478bd9Sstevel@tonic-gate mapping.fromLDAP = TRUE;
7917c478bd9Sstevel@tonic-gate if (odn->write.scope != LDAP_SCOPE_UNKNOWN)
7927c478bd9Sstevel@tonic-gate mapping.toLDAP = TRUE;
7937c478bd9Sstevel@tonic-gate odn = (__nis_object_dn_t *)odn->next;
7947c478bd9Sstevel@tonic-gate }
7957c478bd9Sstevel@tonic-gate
7967c478bd9Sstevel@tonic-gate /* Set the timeout values */
7977c478bd9Sstevel@tonic-gate mapping.initTtlLo = mapping.tm->initTtlLo;
7987c478bd9Sstevel@tonic-gate mapping.initTtlHi = mapping.tm->initTtlHi;
7997c478bd9Sstevel@tonic-gate mapping.ttl = mapping.tm->ttl;
8007c478bd9Sstevel@tonic-gate
8017c478bd9Sstevel@tonic-gate mapping.objName = sdup(myself, T, mapping.tm->objName);
8027c478bd9Sstevel@tonic-gate if (mapping.objName == 0 && mapping.tm->objName != 0) {
8037c478bd9Sstevel@tonic-gate WRITEUNLOCK(this, FALSE,
8047c478bd9Sstevel@tonic-gate "db_table::configure wu objName");
8057c478bd9Sstevel@tonic-gate FATAL3("db_table::configure objName",
8067c478bd9Sstevel@tonic-gate DB_MEMORY_LIMIT, FALSE);
8077c478bd9Sstevel@tonic-gate }
8087c478bd9Sstevel@tonic-gate }
8097c478bd9Sstevel@tonic-gate
8107c478bd9Sstevel@tonic-gate /*
8117c478bd9Sstevel@tonic-gate * In order to initialize the expiration times, we need to know
8127c478bd9Sstevel@tonic-gate * if 'this' represents a table or a directory. To that end, we
8137c478bd9Sstevel@tonic-gate * find an entry in the table, and invoke setEntryExp() on it.
8147c478bd9Sstevel@tonic-gate * As a side effect, setEntryExp() will examine the pseudo-object
8157c478bd9Sstevel@tonic-gate * in the entry, and set the expireType accordingly.
8167c478bd9Sstevel@tonic-gate */
8177c478bd9Sstevel@tonic-gate if (tab != 0) {
8187c478bd9Sstevel@tonic-gate for (i = 0; i <= last_used; i++) {
8197c478bd9Sstevel@tonic-gate if (tab[i] != NULL) {
8207c478bd9Sstevel@tonic-gate setEntryExp(i, tab[i], 1);
8217c478bd9Sstevel@tonic-gate break;
8227c478bd9Sstevel@tonic-gate }
8237c478bd9Sstevel@tonic-gate }
8247c478bd9Sstevel@tonic-gate }
8257c478bd9Sstevel@tonic-gate
8267c478bd9Sstevel@tonic-gate /*
8277c478bd9Sstevel@tonic-gate * If mapping from an LDAP repository, make sure we have the
8287c478bd9Sstevel@tonic-gate * expiration time array.
8297c478bd9Sstevel@tonic-gate */
8307c478bd9Sstevel@tonic-gate if ((mapping.expireType != NIS_TABLE_OBJ || mapping.fromLDAP) &&
8317c478bd9Sstevel@tonic-gate mapping.expire == NULL && table_size > 0 && tab != 0) {
8327c478bd9Sstevel@tonic-gate db_status stat = allocateExpire(0, table_size);
8337c478bd9Sstevel@tonic-gate if (stat != DB_SUCCESS) {
8347c478bd9Sstevel@tonic-gate WRITEUNLOCK(this, FALSE,
8357c478bd9Sstevel@tonic-gate "db_table::configure wu expire");
8367c478bd9Sstevel@tonic-gate FATAL3("db_table::configure expire",
8377c478bd9Sstevel@tonic-gate stat, FALSE);
8387c478bd9Sstevel@tonic-gate }
8397c478bd9Sstevel@tonic-gate } else if (mapping.expireType == NIS_TABLE_OBJ && !mapping.fromLDAP &&
8407c478bd9Sstevel@tonic-gate mapping.expire != NULL) {
8417c478bd9Sstevel@tonic-gate /* Not using expiration times */
8427c478bd9Sstevel@tonic-gate free(mapping.expire);
8437c478bd9Sstevel@tonic-gate mapping.expire = NULL;
8447c478bd9Sstevel@tonic-gate }
8457c478bd9Sstevel@tonic-gate
8467c478bd9Sstevel@tonic-gate /*
8477c478bd9Sstevel@tonic-gate * Set initial expire times for entries that don't already have one.
8487c478bd9Sstevel@tonic-gate * Establish the enumeration expiration time to be the minimum of
8497c478bd9Sstevel@tonic-gate * all expiration times in the table, though no larger than current
8507c478bd9Sstevel@tonic-gate * time plus initTtlHi.
8517c478bd9Sstevel@tonic-gate */
8527c478bd9Sstevel@tonic-gate if (mapping.expire != NULL) {
8537c478bd9Sstevel@tonic-gate int interval = mapping.initTtlHi - mapping.initTtlLo + 1;
8547c478bd9Sstevel@tonic-gate time_t enumXp = now.tv_sec + mapping.initTtlHi;
8557c478bd9Sstevel@tonic-gate
8567c478bd9Sstevel@tonic-gate if (interval > 1)
8577c478bd9Sstevel@tonic-gate srand48(now.tv_sec);
8587c478bd9Sstevel@tonic-gate for (i = 0; i <= last_used; i++) {
8597c478bd9Sstevel@tonic-gate if (tab[i] != NULL && mapping.expire[i] == 0) {
8607c478bd9Sstevel@tonic-gate if (mapping.expireType == NIS_TABLE_OBJ) {
8617c478bd9Sstevel@tonic-gate if (interval > 1)
8627c478bd9Sstevel@tonic-gate mapping.expire[i] =
8637c478bd9Sstevel@tonic-gate now.tv_sec +
8647c478bd9Sstevel@tonic-gate (lrand48() % interval);
8657c478bd9Sstevel@tonic-gate else
8667c478bd9Sstevel@tonic-gate mapping.expire[i] =
8677c478bd9Sstevel@tonic-gate now.tv_sec +
8687c478bd9Sstevel@tonic-gate mapping.initTtlLo;
8697c478bd9Sstevel@tonic-gate } else {
8707c478bd9Sstevel@tonic-gate setEntryExp(i, tab[i], 1);
8717c478bd9Sstevel@tonic-gate }
8727c478bd9Sstevel@tonic-gate }
8737c478bd9Sstevel@tonic-gate if (enumXp > mapping.expire[i])
8747c478bd9Sstevel@tonic-gate enumXp = mapping.expire[i];
8757c478bd9Sstevel@tonic-gate }
8767c478bd9Sstevel@tonic-gate mapping.enumExpire = enumXp;
8777c478bd9Sstevel@tonic-gate }
8787c478bd9Sstevel@tonic-gate
8797c478bd9Sstevel@tonic-gate WRITEUNLOCK(this, FALSE, "db_table::configure wu");
8807c478bd9Sstevel@tonic-gate
8817c478bd9Sstevel@tonic-gate return (TRUE);
8827c478bd9Sstevel@tonic-gate }
8837c478bd9Sstevel@tonic-gate
8847c478bd9Sstevel@tonic-gate /* Return TRUE if the entry at 'loc' hasn't expired */
8857c478bd9Sstevel@tonic-gate bool_t
cacheValid(entryp loc)8867c478bd9Sstevel@tonic-gate db_table::cacheValid(entryp loc) {
8877c478bd9Sstevel@tonic-gate bool_t ret;
8887c478bd9Sstevel@tonic-gate struct timeval now;
8897c478bd9Sstevel@tonic-gate
8907c478bd9Sstevel@tonic-gate (void) gettimeofday(&now, 0);
8917c478bd9Sstevel@tonic-gate
8927c478bd9Sstevel@tonic-gate READLOCK(this, FALSE, "db_table::cacheValid r");
8937c478bd9Sstevel@tonic-gate
8947c478bd9Sstevel@tonic-gate if (loc < 0 || loc >= table_size || tab == 0 || tab[loc] == 0)
8957c478bd9Sstevel@tonic-gate ret = FALSE;
8967c478bd9Sstevel@tonic-gate else if (mapping.expire == 0 || mapping.expire[loc] >= now.tv_sec)
8977c478bd9Sstevel@tonic-gate ret = TRUE;
8987c478bd9Sstevel@tonic-gate else
8997c478bd9Sstevel@tonic-gate ret = FALSE;
9007c478bd9Sstevel@tonic-gate
9017c478bd9Sstevel@tonic-gate READUNLOCK(this, ret, "db_table::cacheValid ru");
9027c478bd9Sstevel@tonic-gate
9037c478bd9Sstevel@tonic-gate return (ret);
9047c478bd9Sstevel@tonic-gate }
9057c478bd9Sstevel@tonic-gate
9067c478bd9Sstevel@tonic-gate /*
9077c478bd9Sstevel@tonic-gate * If the supplied object has the same content as the one at 'loc',
9087c478bd9Sstevel@tonic-gate * update the expiration time for the latter, and return TRUE.
9097c478bd9Sstevel@tonic-gate */
9107c478bd9Sstevel@tonic-gate bool_t
dupEntry(entry_object * obj,entryp loc)9117c478bd9Sstevel@tonic-gate db_table::dupEntry(entry_object *obj, entryp loc) {
9127c478bd9Sstevel@tonic-gate if (obj == 0 || loc < 0 || loc >= table_size || tab == 0 ||
9137c478bd9Sstevel@tonic-gate tab[loc] == 0)
9147c478bd9Sstevel@tonic-gate return (FALSE);
9157c478bd9Sstevel@tonic-gate
9167c478bd9Sstevel@tonic-gate if (sameEntry(obj, tab[loc])) {
9177c478bd9Sstevel@tonic-gate setEntryExp(loc, tab[loc], 0);
9187c478bd9Sstevel@tonic-gate
9197c478bd9Sstevel@tonic-gate if (enumMode.flag > 0)
9207c478bd9Sstevel@tonic-gate enumTouch(loc);
9217c478bd9Sstevel@tonic-gate return (TRUE);
9227c478bd9Sstevel@tonic-gate }
9237c478bd9Sstevel@tonic-gate
9247c478bd9Sstevel@tonic-gate return (FALSE);
9257c478bd9Sstevel@tonic-gate }
9267c478bd9Sstevel@tonic-gate
9277c478bd9Sstevel@tonic-gate /*
9287c478bd9Sstevel@tonic-gate * If enumeration mode is enabled, we keep a shadow array that initially
9297c478bd9Sstevel@tonic-gate * starts out the same as 'tab'. Any update activity (add, remove, replace,
9307c478bd9Sstevel@tonic-gate * or update timestamp) for an entry in the table means we delete the shadow
9317c478bd9Sstevel@tonic-gate * array pointer. When ending enumeration mode, we return the shadow array.
9327c478bd9Sstevel@tonic-gate * Any non-NULL entries in the array have not been updated since the start
9337c478bd9Sstevel@tonic-gate * of the enum mode.
9347c478bd9Sstevel@tonic-gate *
9357c478bd9Sstevel@tonic-gate * The indended use is for enumeration of an LDAP container, where we
9367c478bd9Sstevel@tonic-gate * will update all entries that currently exist in LDAP. The entries we
9377c478bd9Sstevel@tonic-gate * don't update are those that don't exist in LDAP, and thus should be
9387c478bd9Sstevel@tonic-gate * removed.
9397c478bd9Sstevel@tonic-gate *
9407c478bd9Sstevel@tonic-gate * Note that any LDAP query strictly speaking can be a partial enumeration
9417c478bd9Sstevel@tonic-gate * (i.e., return more than one match). Since the query might also have
9427c478bd9Sstevel@tonic-gate * matched multiple local DB entries, we need to do the same work as for
9437c478bd9Sstevel@tonic-gate * enumeration for any query. In order to avoid having to work on the
9447c478bd9Sstevel@tonic-gate * whole 'tab' array for simple queries (which we expect usually will
9457c478bd9Sstevel@tonic-gate * match just one or at most a few entries), we have a "reduced" enum mode,
9467c478bd9Sstevel@tonic-gate * where the caller supplies a count of the number of DB entries (derived
9477c478bd9Sstevel@tonic-gate * from db_mindex::satisfy_query() or similar), and then uses enumSetup()
9487c478bd9Sstevel@tonic-gate * to specify which 'tab' entries we're interested in.
9497c478bd9Sstevel@tonic-gate */
9507c478bd9Sstevel@tonic-gate void
setEnumMode(long enumNum)9517c478bd9Sstevel@tonic-gate db_table::setEnumMode(long enumNum) {
952*8d0852b7SRichard Lowe const char *myself = "setEnumMode";
9537c478bd9Sstevel@tonic-gate
9547c478bd9Sstevel@tonic-gate enumMode.flag++;
9557c478bd9Sstevel@tonic-gate if (enumMode.flag == 1) {
9567c478bd9Sstevel@tonic-gate db_status stat;
9577c478bd9Sstevel@tonic-gate
9587c478bd9Sstevel@tonic-gate if (enumNum < 0)
9597c478bd9Sstevel@tonic-gate enumNum = 0;
9607c478bd9Sstevel@tonic-gate else if (enumNum >= table_size)
9617c478bd9Sstevel@tonic-gate enumNum = table_size;
9627c478bd9Sstevel@tonic-gate
9637c478bd9Sstevel@tonic-gate enumCount.flag = enumNum;
9647c478bd9Sstevel@tonic-gate
9657c478bd9Sstevel@tonic-gate stat = allocateEnumArray(0, table_size);
9667c478bd9Sstevel@tonic-gate
9677c478bd9Sstevel@tonic-gate if (stat != DB_SUCCESS) {
9687c478bd9Sstevel@tonic-gate enumMode.flag = 0;
9697c478bd9Sstevel@tonic-gate enumCount.flag = 0;
9707c478bd9Sstevel@tonic-gate logmsg(MSG_NOTIMECHECK, LOG_ERR,
9717c478bd9Sstevel@tonic-gate "%s: No memory for enum check array; entry removal disabled",
9727c478bd9Sstevel@tonic-gate myself);
9737c478bd9Sstevel@tonic-gate }
9747c478bd9Sstevel@tonic-gate }
9757c478bd9Sstevel@tonic-gate }
9767c478bd9Sstevel@tonic-gate
9777c478bd9Sstevel@tonic-gate void
clearEnumMode(void)9787c478bd9Sstevel@tonic-gate db_table::clearEnumMode(void) {
9797c478bd9Sstevel@tonic-gate if (enumMode.flag > 0) {
9807c478bd9Sstevel@tonic-gate enumMode.flag--;
9817c478bd9Sstevel@tonic-gate if (enumMode.flag == 0) {
9827c478bd9Sstevel@tonic-gate sfree(enumArray.ptr);
9837c478bd9Sstevel@tonic-gate enumArray.ptr = 0;
9847c478bd9Sstevel@tonic-gate if (enumCount.flag > 0) {
9857c478bd9Sstevel@tonic-gate sfree(enumIndex.ptr);
9867c478bd9Sstevel@tonic-gate enumIndex.ptr = 0;
9877c478bd9Sstevel@tonic-gate enumCount.flag = 0;
9887c478bd9Sstevel@tonic-gate }
9897c478bd9Sstevel@tonic-gate }
9907c478bd9Sstevel@tonic-gate }
9917c478bd9Sstevel@tonic-gate }
9927c478bd9Sstevel@tonic-gate
9937c478bd9Sstevel@tonic-gate entry_object **
endEnumMode(long * numEa)9947c478bd9Sstevel@tonic-gate db_table::endEnumMode(long *numEa) {
9957c478bd9Sstevel@tonic-gate if (enumMode.flag > 0) {
9967c478bd9Sstevel@tonic-gate enumMode.flag--;
9977c478bd9Sstevel@tonic-gate if (enumMode.flag == 0) {
9987c478bd9Sstevel@tonic-gate entry_obj **ea = (entry_object **)enumArray.ptr;
9997c478bd9Sstevel@tonic-gate long nea;
10007c478bd9Sstevel@tonic-gate
10017c478bd9Sstevel@tonic-gate enumArray.ptr = 0;
10027c478bd9Sstevel@tonic-gate
10037c478bd9Sstevel@tonic-gate if (enumCount.flag > 0) {
10047c478bd9Sstevel@tonic-gate nea = enumCount.flag;
10057c478bd9Sstevel@tonic-gate enumCount.flag = 0;
10067c478bd9Sstevel@tonic-gate sfree(enumIndex.ptr);
10077c478bd9Sstevel@tonic-gate enumIndex.ptr = 0;
10087c478bd9Sstevel@tonic-gate } else {
10097c478bd9Sstevel@tonic-gate nea = table_size;
10107c478bd9Sstevel@tonic-gate }
10117c478bd9Sstevel@tonic-gate
10127c478bd9Sstevel@tonic-gate if (numEa != 0)
10137c478bd9Sstevel@tonic-gate *numEa = nea;
10147c478bd9Sstevel@tonic-gate
10157c478bd9Sstevel@tonic-gate return (ea);
10167c478bd9Sstevel@tonic-gate }
10177c478bd9Sstevel@tonic-gate }
10187c478bd9Sstevel@tonic-gate
10197c478bd9Sstevel@tonic-gate if (numEa != 0)
10207c478bd9Sstevel@tonic-gate *numEa = 0;
10217c478bd9Sstevel@tonic-gate
10227c478bd9Sstevel@tonic-gate return (0);
10237c478bd9Sstevel@tonic-gate }
10247c478bd9Sstevel@tonic-gate
10257c478bd9Sstevel@tonic-gate /*
10267c478bd9Sstevel@tonic-gate * Set the appropriate entry in the enum array to NULL.
10277c478bd9Sstevel@tonic-gate */
10287c478bd9Sstevel@tonic-gate void
enumTouch(entryp loc)10297c478bd9Sstevel@tonic-gate db_table::enumTouch(entryp loc) {
10307c478bd9Sstevel@tonic-gate if (loc < 0 || loc >= table_size)
10317c478bd9Sstevel@tonic-gate return;
10327c478bd9Sstevel@tonic-gate
10337c478bd9Sstevel@tonic-gate if (enumMode.flag > 0) {
10347c478bd9Sstevel@tonic-gate if (enumCount.flag < 1) {
10357c478bd9Sstevel@tonic-gate ((entry_object **)enumArray.ptr)[loc] = 0;
10367c478bd9Sstevel@tonic-gate } else {
10377c478bd9Sstevel@tonic-gate int i;
10387c478bd9Sstevel@tonic-gate
10397c478bd9Sstevel@tonic-gate for (i = 0; i < enumCount.flag; i++) {
10407c478bd9Sstevel@tonic-gate if (loc == ((entryp *)enumIndex.ptr)[i]) {
10417c478bd9Sstevel@tonic-gate ((entry_object **)enumArray.ptr)[i] = 0;
10427c478bd9Sstevel@tonic-gate break;
10437c478bd9Sstevel@tonic-gate }
10447c478bd9Sstevel@tonic-gate }
10457c478bd9Sstevel@tonic-gate }
10467c478bd9Sstevel@tonic-gate }
10477c478bd9Sstevel@tonic-gate }
10487c478bd9Sstevel@tonic-gate
10497c478bd9Sstevel@tonic-gate /*
10507c478bd9Sstevel@tonic-gate * Add the entry indicated by 'loc' to the enumIndex array, at 'index'.
10517c478bd9Sstevel@tonic-gate */
10527c478bd9Sstevel@tonic-gate void
enumSetup(entryp loc,long index)10537c478bd9Sstevel@tonic-gate db_table::enumSetup(entryp loc, long index) {
10547c478bd9Sstevel@tonic-gate if (enumMode.flag == 0 || loc < 0 || loc >= table_size ||
10557c478bd9Sstevel@tonic-gate index < 0 || index >= enumCount.flag)
10567c478bd9Sstevel@tonic-gate return;
10577c478bd9Sstevel@tonic-gate
10587c478bd9Sstevel@tonic-gate ((entryp *)enumIndex.ptr)[index] = loc;
10597c478bd9Sstevel@tonic-gate ((entry_object **)enumArray.ptr)[index] = tab[loc];
10607c478bd9Sstevel@tonic-gate }
10617c478bd9Sstevel@tonic-gate
10627c478bd9Sstevel@tonic-gate /*
10637c478bd9Sstevel@tonic-gate * Touch, i.e., update the expiration time for the entry. Also, if enum
10647c478bd9Sstevel@tonic-gate * mode is in effect, mark the entry used for enum purposes.
10657c478bd9Sstevel@tonic-gate */
10667c478bd9Sstevel@tonic-gate void
touchEntry(entryp loc)10677c478bd9Sstevel@tonic-gate db_table::touchEntry(entryp loc) {
10687c478bd9Sstevel@tonic-gate if (loc < 0 || loc >= table_size || tab == 0 || tab[loc] == 0)
10697c478bd9Sstevel@tonic-gate return;
10707c478bd9Sstevel@tonic-gate
10717c478bd9Sstevel@tonic-gate setEntryExp(loc, tab[loc], 0);
10727c478bd9Sstevel@tonic-gate
10737c478bd9Sstevel@tonic-gate enumTouch(loc);
10747c478bd9Sstevel@tonic-gate }
10757c478bd9Sstevel@tonic-gate
10767c478bd9Sstevel@tonic-gate /* ************************* pickle_table ********************* */
10777c478bd9Sstevel@tonic-gate /* Does the actual writing to/from file specific for db_table structure. */
10787c478bd9Sstevel@tonic-gate /*
10797c478bd9Sstevel@tonic-gate * This was a static earlier with the func name being transfer_aux. The
10807c478bd9Sstevel@tonic-gate * backup and restore project needed this to copy files over.
10817c478bd9Sstevel@tonic-gate */
10827c478bd9Sstevel@tonic-gate bool_t
transfer_aux_table(XDR * x,pptr dp)10837c478bd9Sstevel@tonic-gate transfer_aux_table(XDR* x, pptr dp)
10847c478bd9Sstevel@tonic-gate {
10857c478bd9Sstevel@tonic-gate return (xdr_db_table(x, (db_table*) dp));
10867c478bd9Sstevel@tonic-gate }
10877c478bd9Sstevel@tonic-gate
10887c478bd9Sstevel@tonic-gate class pickle_table: public pickle_file {
10897c478bd9Sstevel@tonic-gate public:
pickle_table(char * f,pickle_mode m)10907c478bd9Sstevel@tonic-gate pickle_table(char *f, pickle_mode m) : pickle_file(f, m) {}
10917c478bd9Sstevel@tonic-gate
10927c478bd9Sstevel@tonic-gate /* Transfers db_table structure pointed to by dp to/from file. */
transfer(db_table * dp)10937c478bd9Sstevel@tonic-gate int transfer(db_table* dp)
10947c478bd9Sstevel@tonic-gate { return (pickle_file::transfer((pptr) dp, &transfer_aux_table)); }
10957c478bd9Sstevel@tonic-gate };
10967c478bd9Sstevel@tonic-gate
10977c478bd9Sstevel@tonic-gate /*
10987c478bd9Sstevel@tonic-gate * Writes the contents of table, including the all the entries, into the
10997c478bd9Sstevel@tonic-gate * specified file in XDR format. May need to change this to use APPEND
11007c478bd9Sstevel@tonic-gate * mode instead.
11017c478bd9Sstevel@tonic-gate */
11027c478bd9Sstevel@tonic-gate int
dump(char * file)11037c478bd9Sstevel@tonic-gate db_table::dump(char *file)
11047c478bd9Sstevel@tonic-gate {
11057c478bd9Sstevel@tonic-gate int ret;
11067c478bd9Sstevel@tonic-gate READLOCK(this, -1, "r db_table::dump");
11077c478bd9Sstevel@tonic-gate pickle_table f(file, PICKLE_WRITE); /* may need to use APPEND mode */
11087c478bd9Sstevel@tonic-gate int status = f.transfer(this);
11097c478bd9Sstevel@tonic-gate
11107c478bd9Sstevel@tonic-gate if (status == 1)
11117c478bd9Sstevel@tonic-gate ret = -1;
11127c478bd9Sstevel@tonic-gate else
11137c478bd9Sstevel@tonic-gate ret = status;
11147c478bd9Sstevel@tonic-gate READUNLOCK(this, ret, "ru db_table::dump");
11157c478bd9Sstevel@tonic-gate }
11167c478bd9Sstevel@tonic-gate
11177c478bd9Sstevel@tonic-gate /* Constructor that loads in the table from the given file */
db_table(char * file)11187c478bd9Sstevel@tonic-gate db_table::db_table(char *file) : freelist()
11197c478bd9Sstevel@tonic-gate {
11207c478bd9Sstevel@tonic-gate pickle_table f(file, PICKLE_READ);
11217c478bd9Sstevel@tonic-gate tab = NULL;
11227c478bd9Sstevel@tonic-gate table_size = last_used = count = 0;
11237c478bd9Sstevel@tonic-gate
11247c478bd9Sstevel@tonic-gate /* load table */
11257c478bd9Sstevel@tonic-gate if (f.transfer(this) < 0) {
11267c478bd9Sstevel@tonic-gate /* fell through, something went wrong, initialize to null */
11277c478bd9Sstevel@tonic-gate tab = NULL;
11287c478bd9Sstevel@tonic-gate table_size = last_used = count = 0;
11297c478bd9Sstevel@tonic-gate freelist.init();
11307c478bd9Sstevel@tonic-gate }
11317c478bd9Sstevel@tonic-gate
11327c478bd9Sstevel@tonic-gate db_table_ldap_init();
11337c478bd9Sstevel@tonic-gate initMappingStruct(&mapping);
11347c478bd9Sstevel@tonic-gate }
11357c478bd9Sstevel@tonic-gate
11367c478bd9Sstevel@tonic-gate /* Returns whether location is valid. */
entry_exists_p(entryp i)11377c478bd9Sstevel@tonic-gate bool_t db_table::entry_exists_p(entryp i) {
11387c478bd9Sstevel@tonic-gate bool_t ret = FALSE;
11397c478bd9Sstevel@tonic-gate READLOCK(this, FALSE, "r db_table::entry_exists_p");
11407c478bd9Sstevel@tonic-gate if (tab != NULL && i < table_size)
11417c478bd9Sstevel@tonic-gate ret = tab[i] != NULL;
11427c478bd9Sstevel@tonic-gate READUNLOCK(this, ret, "ru db_table::entry_exists_p");
11437c478bd9Sstevel@tonic-gate return (ret);
11447c478bd9Sstevel@tonic-gate }
11457c478bd9Sstevel@tonic-gate
11467c478bd9Sstevel@tonic-gate /* Empty free list */
init()11477c478bd9Sstevel@tonic-gate void db_free_list::init() {
11487c478bd9Sstevel@tonic-gate WRITELOCKV(this, "w db_free_list::init");
11497c478bd9Sstevel@tonic-gate head = NULL;
11507c478bd9Sstevel@tonic-gate count = 0;
11517c478bd9Sstevel@tonic-gate WRITEUNLOCKV(this, "wu db_free_list::init");
11527c478bd9Sstevel@tonic-gate }
1153