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_dictionary.cc
247c478bd9Sstevel@tonic-gate  *
257c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
267c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
27*8e0c8248SAndrew Stormont  *
28*8e0c8248SAndrew Stormont  * Copyright 2019 RackTop Systems.
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include "db_headers.h"
327c478bd9Sstevel@tonic-gate #include "db_entry.h"
337c478bd9Sstevel@tonic-gate #include "db_dictionary.h"
347c478bd9Sstevel@tonic-gate #include "db_dictlog.h"
357c478bd9Sstevel@tonic-gate #include "db_vers.h"
367c478bd9Sstevel@tonic-gate #include "nisdb_mt.h"
377c478bd9Sstevel@tonic-gate #include "nisdb_rw.h"
387c478bd9Sstevel@tonic-gate #include "ldap_parse.h"
397c478bd9Sstevel@tonic-gate #include "ldap_map.h"
407c478bd9Sstevel@tonic-gate #include "nis_hashitem.h"
417c478bd9Sstevel@tonic-gate #include "ldap_util.h"
427c478bd9Sstevel@tonic-gate #include "nis_db.h"
437c478bd9Sstevel@tonic-gate #include <rpcsvc/nis.h>
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #include <stdio.h>
467c478bd9Sstevel@tonic-gate #include <string.h>
477c478bd9Sstevel@tonic-gate #include <malloc.h>
487c478bd9Sstevel@tonic-gate #ifdef TDRPC
497c478bd9Sstevel@tonic-gate #include <sysent.h>
507c478bd9Sstevel@tonic-gate #endif
517c478bd9Sstevel@tonic-gate #include <unistd.h>
527c478bd9Sstevel@tonic-gate #include <syslog.h>
537c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate typedef bool_t	(*db_func)(XDR *, db_table_desc *);
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate extern db_dictionary *InUseDictionary;
587c478bd9Sstevel@tonic-gate extern db_dictionary *FreeDictionary;
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate /* *************** dictionary version ****************** */
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate #define	DB_MAGIC 0x12340000
637c478bd9Sstevel@tonic-gate #define	DB_MAJOR 0
647c478bd9Sstevel@tonic-gate #define	DB_MINOR 10
657c478bd9Sstevel@tonic-gate #define	DB_VERSION_0_9	(DB_MAGIC|(DB_MAJOR<<8)|9)
667c478bd9Sstevel@tonic-gate #define	DB_ORIG_VERSION	DB_VERSION_0_9
677c478bd9Sstevel@tonic-gate #define	DB_CURRENT_VERSION (DB_MAGIC|DB_MAJOR<<8|DB_MINOR)
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate vers db_update_version;   /* Note 'global' for all dbs. */
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate #define	INMEMORY_ONLY 1
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate /*
747c478bd9Sstevel@tonic-gate  * Checks for valid version.  For now, there are two:
757c478bd9Sstevel@tonic-gate  * DB_VERSION_ORIG was the ORIGINAL one with major = 0, minor = 9
767c478bd9Sstevel@tonic-gate  * DB_CURRENT_VERSION is the latest one with changes in the database format
777c478bd9Sstevel@tonic-gate  *	for entry objects and the change in the dictionary format.
787c478bd9Sstevel@tonic-gate  *
797c478bd9Sstevel@tonic-gate  * Our current implementation can support both versions.
807c478bd9Sstevel@tonic-gate  */
817c478bd9Sstevel@tonic-gate static inline bool_t
db_valid_version(u_int vers)827c478bd9Sstevel@tonic-gate db_valid_version(u_int vers)
837c478bd9Sstevel@tonic-gate {
847c478bd9Sstevel@tonic-gate 	return ((vers == DB_CURRENT_VERSION) || (vers == DB_ORIG_VERSION));
857c478bd9Sstevel@tonic-gate }
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate static char *
db_version_str(u_int vers)887c478bd9Sstevel@tonic-gate db_version_str(u_int vers)
897c478bd9Sstevel@tonic-gate {
907c478bd9Sstevel@tonic-gate 	static char vstr[128];
917c478bd9Sstevel@tonic-gate 	u_int d_major =  (vers&0x0000ff00)>>8;
927c478bd9Sstevel@tonic-gate 	u_int d_minor =  (vers&0x000000ff);
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 	sprintf(vstr, "SunSoft, SSM, Version %d.%d", d_major, d_minor);
957c478bd9Sstevel@tonic-gate 	return (vstr);
967c478bd9Sstevel@tonic-gate }
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate /*
997c478bd9Sstevel@tonic-gate  * Special XDR version that checks for a valid version number.
1007c478bd9Sstevel@tonic-gate  * If we don't find an acceptable version, exit immediately instead
1017c478bd9Sstevel@tonic-gate  * of continuing to xdr rest of dictionary, which might lead to
1027c478bd9Sstevel@tonic-gate  * a core dump if the formats between versions are incompatible.
1037c478bd9Sstevel@tonic-gate  * In the future, there might be a switch to determine versions
1047c478bd9Sstevel@tonic-gate  * and their corresponding XDR routines for the rest of the dictionary.
1057c478bd9Sstevel@tonic-gate  */
1067c478bd9Sstevel@tonic-gate extern "C" {
1077c478bd9Sstevel@tonic-gate bool_t
xdr_db_dict_version(XDR * xdrs,db_dict_version * objp)1087c478bd9Sstevel@tonic-gate xdr_db_dict_version(XDR *xdrs, db_dict_version *objp)
1097c478bd9Sstevel@tonic-gate {
1107c478bd9Sstevel@tonic-gate 	if (xdrs->x_op == XDR_DECODE) {
1117c478bd9Sstevel@tonic-gate 		if (!xdr_u_int(xdrs, (u_int*) objp) ||
1127c478bd9Sstevel@tonic-gate 		    !db_valid_version(((u_int) *objp))) {
1137c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR,
1147c478bd9Sstevel@tonic-gate 	"db_dictionary: invalid dictionary format! Expecting %s",
1157c478bd9Sstevel@tonic-gate 				db_version_str(DB_CURRENT_VERSION));
1167c478bd9Sstevel@tonic-gate 			fprintf(stderr,
1177c478bd9Sstevel@tonic-gate 	"db_dictionary: invalid dictionary format! Expecting %s\n",
1187c478bd9Sstevel@tonic-gate 				db_version_str(DB_CURRENT_VERSION));
1197c478bd9Sstevel@tonic-gate 			exit(1);
1207c478bd9Sstevel@tonic-gate 		}
1217c478bd9Sstevel@tonic-gate 	} else if (!xdr_u_int(xdrs, (u_int*) objp))
1227c478bd9Sstevel@tonic-gate 		return (FALSE);
1237c478bd9Sstevel@tonic-gate 	return (TRUE);
1247c478bd9Sstevel@tonic-gate }
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate void
make_zero(vers * v)1277c478bd9Sstevel@tonic-gate make_zero(vers* v)
1287c478bd9Sstevel@tonic-gate {
1297c478bd9Sstevel@tonic-gate 	v->zero();
1307c478bd9Sstevel@tonic-gate }
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate };
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate /* ******************* dictionary data structures *************** */
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate /* Delete contents of single db_table_desc pointed to by 'current.' */
1397c478bd9Sstevel@tonic-gate static void
delete_table_desc(db_table_desc * current)1407c478bd9Sstevel@tonic-gate delete_table_desc(db_table_desc *current)
1417c478bd9Sstevel@tonic-gate {
1427c478bd9Sstevel@tonic-gate 	if (current->table_name != NULL) delete current->table_name;
1437c478bd9Sstevel@tonic-gate 	if (current->scheme != NULL) delete current->scheme;
1447c478bd9Sstevel@tonic-gate 	if (current->database != NULL) delete current->database;
1457c478bd9Sstevel@tonic-gate 	delete current;
1467c478bd9Sstevel@tonic-gate }
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate /* Create new table descriptor using given table name and table_object. */
1497c478bd9Sstevel@tonic-gate db_status
create_table_desc(char * tab,table_obj * zdesc,db_table_desc ** answer)1507c478bd9Sstevel@tonic-gate db_dictionary::create_table_desc(char *tab, table_obj* zdesc,
1517c478bd9Sstevel@tonic-gate 				db_table_desc** answer)
1527c478bd9Sstevel@tonic-gate {
1537c478bd9Sstevel@tonic-gate 	db_table_desc *newtab;
1547c478bd9Sstevel@tonic-gate 	if ((newtab = new db_table_desc) == NULL) {
1557c478bd9Sstevel@tonic-gate 		FATAL3(
1567c478bd9Sstevel@tonic-gate 	    "db_dictionary::add_table: could not allocate space for new table",
1577c478bd9Sstevel@tonic-gate 		DB_MEMORY_LIMIT, DB_MEMORY_LIMIT);
1587c478bd9Sstevel@tonic-gate 	}
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 	newtab->database = NULL;
1617c478bd9Sstevel@tonic-gate 	newtab->table_name = NULL;
1627c478bd9Sstevel@tonic-gate 	newtab->next = NULL;
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	if ((newtab->scheme = new db_scheme(zdesc)) == NULL) {
1657c478bd9Sstevel@tonic-gate 		delete_table_desc(newtab);
1667c478bd9Sstevel@tonic-gate 		FATAL3(
1677c478bd9Sstevel@tonic-gate 	"db_dictionary::add_table: could not allocate space for scheme",
1687c478bd9Sstevel@tonic-gate 		DB_MEMORY_LIMIT, DB_MEMORY_LIMIT);
1697c478bd9Sstevel@tonic-gate 	}
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	if (newtab->scheme->numkeys() == 0) {
1727c478bd9Sstevel@tonic-gate 		WARNING(
1737c478bd9Sstevel@tonic-gate 	"db_dictionary::add_table: could not translate table_obj to scheme");
1747c478bd9Sstevel@tonic-gate 		delete_table_desc(newtab);
1757c478bd9Sstevel@tonic-gate 		return (DB_BADOBJECT);
1767c478bd9Sstevel@tonic-gate 	}
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	if ((newtab->table_name = strdup(tab)) == NULL) {
1797c478bd9Sstevel@tonic-gate 		delete_table_desc(newtab);
1807c478bd9Sstevel@tonic-gate 		FATAL3(
1817c478bd9Sstevel@tonic-gate 	    "db_dictionary::add_table: could not allocate space for table name",
1827c478bd9Sstevel@tonic-gate 		DB_MEMORY_LIMIT, DB_MEMORY_LIMIT);
1837c478bd9Sstevel@tonic-gate 	}
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	if (answer)
1867c478bd9Sstevel@tonic-gate 		*answer = newtab;
1877c478bd9Sstevel@tonic-gate 	return (DB_SUCCESS);
1887c478bd9Sstevel@tonic-gate }
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate /* Delete list of db_table_desc pointed to by 'head.' */
1927c478bd9Sstevel@tonic-gate static void
delete_bucket(db_table_desc * head)1937c478bd9Sstevel@tonic-gate delete_bucket(db_table_desc *head)
1947c478bd9Sstevel@tonic-gate {
1957c478bd9Sstevel@tonic-gate 	db_table_desc * nextone, *current;
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	for (current = head; current != NULL; current = nextone) {
1987c478bd9Sstevel@tonic-gate 		nextone = current->next;	// remember next
1997c478bd9Sstevel@tonic-gate 		delete_table_desc(current);
2007c478bd9Sstevel@tonic-gate 	}
2017c478bd9Sstevel@tonic-gate }
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate static void
delete_dictionary(db_dict_desc * dict)2047c478bd9Sstevel@tonic-gate delete_dictionary(db_dict_desc *dict)
2057c478bd9Sstevel@tonic-gate {
2067c478bd9Sstevel@tonic-gate 	db_table_desc* bucket;
2077c478bd9Sstevel@tonic-gate 	int i;
2087c478bd9Sstevel@tonic-gate 	if (dict) {
2097c478bd9Sstevel@tonic-gate 		if (dict->tables.tables_val) {
2107c478bd9Sstevel@tonic-gate 			/* delete each bucket */
211*8e0c8248SAndrew Stormont 			for (i = 0; i < dict->tables.tables_len; i++) {
2127c478bd9Sstevel@tonic-gate 				bucket = dict->tables.tables_val[i];
2137c478bd9Sstevel@tonic-gate 				if (bucket)
2147c478bd9Sstevel@tonic-gate 					delete_bucket(bucket);
215*8e0c8248SAndrew Stormont 			}
2167c478bd9Sstevel@tonic-gate 			/* delete table */
2177c478bd9Sstevel@tonic-gate 			delete dict->tables.tables_val;
2187c478bd9Sstevel@tonic-gate 		}
2197c478bd9Sstevel@tonic-gate 		/* delete dictionary */
2207c478bd9Sstevel@tonic-gate 		delete dict;
2217c478bd9Sstevel@tonic-gate 	}
2227c478bd9Sstevel@tonic-gate }
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate /* Relocate bucket starting with this entry to new hashtable 'new_tab'. */
2257c478bd9Sstevel@tonic-gate static void
relocate_bucket(db_table_desc * bucket,db_table_desc_p * new_tab,unsigned long hashsize)2267c478bd9Sstevel@tonic-gate relocate_bucket(db_table_desc* bucket,
2277c478bd9Sstevel@tonic-gate 		db_table_desc_p *new_tab, unsigned long hashsize)
2287c478bd9Sstevel@tonic-gate {
2297c478bd9Sstevel@tonic-gate 	db_table_desc_p np, next_np, *hp;
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	for (np = bucket; np != NULL; np = next_np) {
2327c478bd9Sstevel@tonic-gate 		next_np = np->next;
2337c478bd9Sstevel@tonic-gate 		hp = &new_tab[np->hashval % hashsize];
2347c478bd9Sstevel@tonic-gate 		np->next = *hp;
2357c478bd9Sstevel@tonic-gate 		*hp = np;
2367c478bd9Sstevel@tonic-gate 	}
2377c478bd9Sstevel@tonic-gate }
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate /*
2407c478bd9Sstevel@tonic-gate  * Return pointer to entry with same hash value and table_name
2417c478bd9Sstevel@tonic-gate  * as those supplied.  Returns NULL if not found.
2427c478bd9Sstevel@tonic-gate  */
2437c478bd9Sstevel@tonic-gate static db_status
enumerate_bucket(db_table_desc * bucket,db_status (* func)(db_table_desc *))2447c478bd9Sstevel@tonic-gate enumerate_bucket(db_table_desc* bucket, db_status(*func)(db_table_desc *))
2457c478bd9Sstevel@tonic-gate {
2467c478bd9Sstevel@tonic-gate 	db_table_desc_p np;
2477c478bd9Sstevel@tonic-gate 	db_status status;
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	for (np = bucket; np != NULL; np = np->next) {
2507c478bd9Sstevel@tonic-gate 		status = (func)(np);
2517c478bd9Sstevel@tonic-gate 		if (status != DB_SUCCESS)
2527c478bd9Sstevel@tonic-gate 			return (status);
2537c478bd9Sstevel@tonic-gate 	}
2547c478bd9Sstevel@tonic-gate 	return (DB_SUCCESS);
2557c478bd9Sstevel@tonic-gate }
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate /*
2597c478bd9Sstevel@tonic-gate  * Return pointer to entry with same hash value and table_name
2607c478bd9Sstevel@tonic-gate  * as those supplied.  Returns NULL if not found.
2617c478bd9Sstevel@tonic-gate  */
2627c478bd9Sstevel@tonic-gate static db_table_desc_p
search_bucket(db_table_desc * bucket,unsigned long hval,char * target)2637c478bd9Sstevel@tonic-gate search_bucket(db_table_desc* bucket, unsigned long hval, char *target)
2647c478bd9Sstevel@tonic-gate {
2657c478bd9Sstevel@tonic-gate 	db_table_desc_p np;
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	for (np = bucket; np != NULL; np = np->next) {
2687c478bd9Sstevel@tonic-gate 		if (np->hashval == hval &&
2697c478bd9Sstevel@tonic-gate 		    strcmp(np->table_name, target) == 0) {
2707c478bd9Sstevel@tonic-gate 			break;
2717c478bd9Sstevel@tonic-gate 		}
2727c478bd9Sstevel@tonic-gate 	}
2737c478bd9Sstevel@tonic-gate 	return (np);
2747c478bd9Sstevel@tonic-gate }
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate /*
2787c478bd9Sstevel@tonic-gate  * Remove entry with the specified hashvalue and target table name.
2797c478bd9Sstevel@tonic-gate  * Returns 'TRUE' if successful, FALSE otherwise.
2807c478bd9Sstevel@tonic-gate  * If the entry being removed is at the head of the list, then
2817c478bd9Sstevel@tonic-gate  * the head is updated to reflect the removal. The storage for the
2827c478bd9Sstevel@tonic-gate  * entry is freed if desired.
2837c478bd9Sstevel@tonic-gate  */
2847c478bd9Sstevel@tonic-gate static bool_t
remove_from_bucket(db_table_desc_p bucket,db_table_desc_p * head,unsigned long hval,char * target,bool_t free_storage)2857c478bd9Sstevel@tonic-gate remove_from_bucket(db_table_desc_p bucket,
2867c478bd9Sstevel@tonic-gate 		db_table_desc_p *head, unsigned long hval, char *target,
2877c478bd9Sstevel@tonic-gate 		bool_t free_storage)
2887c478bd9Sstevel@tonic-gate {
2897c478bd9Sstevel@tonic-gate 	db_table_desc_p np, dp;
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 	/* Search for it in the bucket */
2927c478bd9Sstevel@tonic-gate 	for (dp = np = bucket; np != NULL; np = np->next) {
2937c478bd9Sstevel@tonic-gate 		if (np->hashval == hval &&
2947c478bd9Sstevel@tonic-gate 		    strcmp(np->table_name, target) == 0) {
2957c478bd9Sstevel@tonic-gate 			break;
2967c478bd9Sstevel@tonic-gate 		} else {
2977c478bd9Sstevel@tonic-gate 			dp = np;
2987c478bd9Sstevel@tonic-gate 		}
2997c478bd9Sstevel@tonic-gate 	}
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	if (np == NULL)
3027c478bd9Sstevel@tonic-gate 		return (FALSE);	// cannot delete if it is not there
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 	if (dp == np) {
3057c478bd9Sstevel@tonic-gate 		*head = np->next;	// deleting head of bucket
3067c478bd9Sstevel@tonic-gate 	} else {
3077c478bd9Sstevel@tonic-gate 		dp->next = np->next;	// deleting interior link
3087c478bd9Sstevel@tonic-gate 	}
3097c478bd9Sstevel@tonic-gate 	if (free_storage)
3107c478bd9Sstevel@tonic-gate 		delete_table_desc(np);
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 	return (TRUE);
3137c478bd9Sstevel@tonic-gate }
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate /*
3177c478bd9Sstevel@tonic-gate  * Add given entry to the bucket pointed to by 'bucket'.
3187c478bd9Sstevel@tonic-gate  * If an entry with the same table_name is found, no addition
3197c478bd9Sstevel@tonic-gate  * is done.  The entry is added to the head of the bucket.
3207c478bd9Sstevel@tonic-gate  */
3217c478bd9Sstevel@tonic-gate static bool_t
add_to_bucket(db_table_desc_p bucket,db_table_desc ** head,db_table_desc_p td)3227c478bd9Sstevel@tonic-gate add_to_bucket(db_table_desc_p bucket, db_table_desc **head, db_table_desc_p td)
3237c478bd9Sstevel@tonic-gate {
324*8e0c8248SAndrew Stormont 	db_table_desc_p curr;
3257c478bd9Sstevel@tonic-gate 	register char *target_name;
3267c478bd9Sstevel@tonic-gate 	unsigned long target_hval;
3277c478bd9Sstevel@tonic-gate 	target_name = td->table_name;
3287c478bd9Sstevel@tonic-gate 	target_hval = td->hashval;
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	/* Search for it in the bucket */
331*8e0c8248SAndrew Stormont 	for (curr = bucket; curr != NULL; curr = curr->next) {
3327c478bd9Sstevel@tonic-gate 		if (curr->hashval == target_hval &&
3337c478bd9Sstevel@tonic-gate 		    strcmp(curr->table_name, target_name) == 0) {
3347c478bd9Sstevel@tonic-gate 			break;
3357c478bd9Sstevel@tonic-gate 		}
3367c478bd9Sstevel@tonic-gate 	}
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate 	if (curr != NULL)
3397c478bd9Sstevel@tonic-gate 		return (FALSE);  /* duplicates not allowed */
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	curr = *head;
3427c478bd9Sstevel@tonic-gate 	*head = td;
3437c478bd9Sstevel@tonic-gate 	td->next = curr;
3447c478bd9Sstevel@tonic-gate 	return (TRUE);
3457c478bd9Sstevel@tonic-gate }
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate /* Print bucket starting with this entry. */
3497c478bd9Sstevel@tonic-gate static void
print_bucket(db_table_desc * head)3507c478bd9Sstevel@tonic-gate print_bucket(db_table_desc *head)
3517c478bd9Sstevel@tonic-gate {
3527c478bd9Sstevel@tonic-gate 	db_table_desc *np;
3537c478bd9Sstevel@tonic-gate 	for (np = head; np != NULL; np = np->next) {
3547c478bd9Sstevel@tonic-gate 		printf("%s: %d\n", np->table_name, np->hashval);
3557c478bd9Sstevel@tonic-gate 	}
3567c478bd9Sstevel@tonic-gate }
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate static db_status
print_table(db_table_desc * tbl)3597c478bd9Sstevel@tonic-gate print_table(db_table_desc *tbl)
3607c478bd9Sstevel@tonic-gate {
3617c478bd9Sstevel@tonic-gate 	if (tbl == NULL)
3627c478bd9Sstevel@tonic-gate 		return (DB_BADTABLE);
3637c478bd9Sstevel@tonic-gate 	printf("%s: %d\n", tbl->table_name, tbl->hashval);
3647c478bd9Sstevel@tonic-gate 	return (DB_SUCCESS);
3657c478bd9Sstevel@tonic-gate }
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate static int hashsizes[] = {		/* hashtable sizes */
3697c478bd9Sstevel@tonic-gate 	11,
3707c478bd9Sstevel@tonic-gate 	53,
3717c478bd9Sstevel@tonic-gate 	113,
3727c478bd9Sstevel@tonic-gate 	337,
3737c478bd9Sstevel@tonic-gate 	977,
3747c478bd9Sstevel@tonic-gate 	2053,
3757c478bd9Sstevel@tonic-gate 	4073,
3767c478bd9Sstevel@tonic-gate 	8011,
3777c478bd9Sstevel@tonic-gate 	16001,
3787c478bd9Sstevel@tonic-gate 	0
3797c478bd9Sstevel@tonic-gate };
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate // prevents wrap around numbers from being passed
3827c478bd9Sstevel@tonic-gate #define	CALLOC_LIMIT 536870911
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate /* Returns the next size to use for the hash table */
3857c478bd9Sstevel@tonic-gate static unsigned int
get_next_hashsize(long unsigned oldsize)3867c478bd9Sstevel@tonic-gate get_next_hashsize(long unsigned oldsize)
3877c478bd9Sstevel@tonic-gate {
3887c478bd9Sstevel@tonic-gate 	long unsigned newsize, n;
3897c478bd9Sstevel@tonic-gate 	if (oldsize == 0)
3907c478bd9Sstevel@tonic-gate 		newsize = hashsizes[0];
3917c478bd9Sstevel@tonic-gate 	else {
3927c478bd9Sstevel@tonic-gate 		for (n = 0; newsize = hashsizes[n++]; )
3937c478bd9Sstevel@tonic-gate 			if (oldsize == newsize) {
3947c478bd9Sstevel@tonic-gate 				newsize = hashsizes[n];	/* get next size */
3957c478bd9Sstevel@tonic-gate 				break;
3967c478bd9Sstevel@tonic-gate 			}
3977c478bd9Sstevel@tonic-gate 		if (newsize == 0)
3987c478bd9Sstevel@tonic-gate 			newsize = oldsize * 2 + 1;	/* just double */
3997c478bd9Sstevel@tonic-gate 	}
4007c478bd9Sstevel@tonic-gate 	return (newsize);
4017c478bd9Sstevel@tonic-gate }
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate /*
4047c478bd9Sstevel@tonic-gate  * Grow the current hashtable upto the next size.
4057c478bd9Sstevel@tonic-gate  * The contents of the existing hashtable is copied to the new one and
4067c478bd9Sstevel@tonic-gate  * relocated according to its hashvalue relative to the new size.
4077c478bd9Sstevel@tonic-gate  * Old table is deleted after the relocation.
4087c478bd9Sstevel@tonic-gate  */
4097c478bd9Sstevel@tonic-gate static void
grow_dictionary(db_dict_desc_p dd)4107c478bd9Sstevel@tonic-gate grow_dictionary(db_dict_desc_p dd)
4117c478bd9Sstevel@tonic-gate {
4127c478bd9Sstevel@tonic-gate 	unsigned int oldsize, i, new_size;
4137c478bd9Sstevel@tonic-gate 	db_table_desc_p * oldtab, *newtab;
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 	oldsize = dd->tables.tables_len;
4167c478bd9Sstevel@tonic-gate 	oldtab = dd->tables.tables_val;
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 	new_size = get_next_hashsize(oldsize);
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 	if (new_size > CALLOC_LIMIT) {
4217c478bd9Sstevel@tonic-gate 		FATAL("db_dictionary::grow: table size exceeds calloc limit",
4227c478bd9Sstevel@tonic-gate 			DB_MEMORY_LIMIT);
4237c478bd9Sstevel@tonic-gate 	}
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 	if ((newtab = (db_table_desc_p*)
4267c478bd9Sstevel@tonic-gate 		calloc((unsigned int) new_size,
4277c478bd9Sstevel@tonic-gate 			sizeof (db_table_desc_p))) == NULL) {
4287c478bd9Sstevel@tonic-gate 		FATAL("db_dictionary::grow: cannot allocate space",
4297c478bd9Sstevel@tonic-gate 			DB_MEMORY_LIMIT);
4307c478bd9Sstevel@tonic-gate 	}
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate 	if (oldtab != NULL) {		// must transfer contents of old to new
4337c478bd9Sstevel@tonic-gate 		for (i = 0; i < oldsize; i++) {
4347c478bd9Sstevel@tonic-gate 			relocate_bucket(oldtab[i], newtab, new_size);
4357c478bd9Sstevel@tonic-gate 		}
4367c478bd9Sstevel@tonic-gate 		delete oldtab;		// delete old hashtable
4377c478bd9Sstevel@tonic-gate 	}
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate 	dd->tables.tables_val = newtab;
4407c478bd9Sstevel@tonic-gate 	dd->tables.tables_len = new_size;
4417c478bd9Sstevel@tonic-gate }
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate #define	HASHSHIFT	3
4447c478bd9Sstevel@tonic-gate #define	HASHMASK	0x1f
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate static u_int
get_hashval(char * value)4477c478bd9Sstevel@tonic-gate get_hashval(char *value)
4487c478bd9Sstevel@tonic-gate {
4497c478bd9Sstevel@tonic-gate 	int i, len;
4507c478bd9Sstevel@tonic-gate 	u_int hval = 0;
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate 	len = strlen(value);
4537c478bd9Sstevel@tonic-gate 	for (i = 0; i < len; i++) {
4547c478bd9Sstevel@tonic-gate 		hval = ((hval<<HASHSHIFT)^hval);
4557c478bd9Sstevel@tonic-gate 		hval += (value[i] & HASHMASK);
4567c478bd9Sstevel@tonic-gate 	}
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 	return (hval);
4597c478bd9Sstevel@tonic-gate }
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate static db_status
enumerate_dictionary(db_dict_desc * dd,db_status (* func)(db_table_desc *))4627c478bd9Sstevel@tonic-gate enumerate_dictionary(db_dict_desc *dd, db_status (*func) (db_table_desc*))
4637c478bd9Sstevel@tonic-gate {
4647c478bd9Sstevel@tonic-gate 	int i;
4657c478bd9Sstevel@tonic-gate 	db_table_desc *bucket;
4667c478bd9Sstevel@tonic-gate 	db_status status;
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate 	if (dd == NULL)
4697c478bd9Sstevel@tonic-gate 		return (DB_SUCCESS);
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate 	for (i = 0; i < dd->tables.tables_len; i++) {
4727c478bd9Sstevel@tonic-gate 		bucket = dd->tables.tables_val[i];
4737c478bd9Sstevel@tonic-gate 		if (bucket) {
4747c478bd9Sstevel@tonic-gate 			status = enumerate_bucket(bucket, func);
4757c478bd9Sstevel@tonic-gate 			if (status != DB_SUCCESS)
4767c478bd9Sstevel@tonic-gate 				return (status);
4777c478bd9Sstevel@tonic-gate 		}
4787c478bd9Sstevel@tonic-gate 	}
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 	return (DB_SUCCESS);
4817c478bd9Sstevel@tonic-gate }
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate /*
4857c478bd9Sstevel@tonic-gate  * Look up target table_name in hashtable and return its db_table_desc.
4867c478bd9Sstevel@tonic-gate  * Return NULL if not found.
4877c478bd9Sstevel@tonic-gate  */
4887c478bd9Sstevel@tonic-gate static db_table_desc *
search_dictionary(db_dict_desc * dd,char * target)4897c478bd9Sstevel@tonic-gate search_dictionary(db_dict_desc *dd, char *target)
4907c478bd9Sstevel@tonic-gate {
4917c478bd9Sstevel@tonic-gate 	register unsigned long hval;
4927c478bd9Sstevel@tonic-gate 	unsigned long bucket;
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate 	if (target == NULL || dd == NULL || dd->tables.tables_len == 0)
4957c478bd9Sstevel@tonic-gate 		return (NULL);
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 	hval = get_hashval(target);
4987c478bd9Sstevel@tonic-gate 	bucket = hval % dd->tables.tables_len;
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate 	db_table_desc_p fst = dd->tables.tables_val[bucket];
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate 	if (fst != NULL)
5037c478bd9Sstevel@tonic-gate 		return (search_bucket(fst, hval, target));
5047c478bd9Sstevel@tonic-gate 	else
5057c478bd9Sstevel@tonic-gate 		return (NULL);
5067c478bd9Sstevel@tonic-gate }
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate /*
5097c478bd9Sstevel@tonic-gate  * Remove the entry with the target table_name from the dictionary.
5107c478bd9Sstevel@tonic-gate  * If successful, return DB_SUCCESS; otherwise DB_NOTUNIQUE if target
5117c478bd9Sstevel@tonic-gate  * is null; DB_NOTFOUND if entry is not found.
5127c478bd9Sstevel@tonic-gate  * If successful, decrement count of number of entries in hash table.
5137c478bd9Sstevel@tonic-gate  */
5147c478bd9Sstevel@tonic-gate static db_status
remove_from_dictionary(db_dict_desc * dd,char * target,bool_t remove_storage)5157c478bd9Sstevel@tonic-gate remove_from_dictionary(db_dict_desc *dd, char *target, bool_t remove_storage)
5167c478bd9Sstevel@tonic-gate {
5177c478bd9Sstevel@tonic-gate 	register unsigned long hval;
5187c478bd9Sstevel@tonic-gate 	unsigned long bucket;
5197c478bd9Sstevel@tonic-gate 	register db_table_desc *fst;
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 	if (target == NULL)
5227c478bd9Sstevel@tonic-gate 		return (DB_NOTUNIQUE);
5237c478bd9Sstevel@tonic-gate 	if (dd == NULL || dd->tables.tables_len == 0)
5247c478bd9Sstevel@tonic-gate 		return (DB_NOTFOUND);
5257c478bd9Sstevel@tonic-gate 	hval = get_hashval(target);
5267c478bd9Sstevel@tonic-gate 	bucket = hval % dd->tables.tables_len;
5277c478bd9Sstevel@tonic-gate 	fst = dd->tables.tables_val[bucket];
5287c478bd9Sstevel@tonic-gate 	if (fst == NULL)
5297c478bd9Sstevel@tonic-gate 		return (DB_NOTFOUND);
5307c478bd9Sstevel@tonic-gate 	if (remove_from_bucket(fst, &dd->tables.tables_val[bucket],
5317c478bd9Sstevel@tonic-gate 			hval, target, remove_storage)) {
5327c478bd9Sstevel@tonic-gate 		--(dd->count);
5337c478bd9Sstevel@tonic-gate 		return (DB_SUCCESS);
5347c478bd9Sstevel@tonic-gate 	} else
5357c478bd9Sstevel@tonic-gate 		return (DB_NOTFOUND);
5367c478bd9Sstevel@tonic-gate }
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate /*
5397c478bd9Sstevel@tonic-gate  * Add a new db_table_desc to the dictionary.
5407c478bd9Sstevel@tonic-gate  * Return DB_NOTUNIQUE, if entry with identical table_name
5417c478bd9Sstevel@tonic-gate  * already exists.  If entry is added, return DB_SUCCESS.
5427c478bd9Sstevel@tonic-gate  * Increment count of number of entries in index table and grow table
5437c478bd9Sstevel@tonic-gate  * if number of entries equals size of table.
5447c478bd9Sstevel@tonic-gate  *
5457c478bd9Sstevel@tonic-gate  * Inputs: db_dict_desc_p dd	pointer to dictionary to add to.
5467c478bd9Sstevel@tonic-gate  *	   db_table_desc *td	pointer to table entry to be added. The
5477c478bd9Sstevel@tonic-gate  * 				db_table_desc.next field will be altered
5487c478bd9Sstevel@tonic-gate  *				without regard to it's current setting.
5497c478bd9Sstevel@tonic-gate  *				This means that if next points to a list of
5507c478bd9Sstevel@tonic-gate  *				table entries, they may be either linked into
5517c478bd9Sstevel@tonic-gate  *				the dictionary unexpectly or cut off (leaked).
5527c478bd9Sstevel@tonic-gate  */
5537c478bd9Sstevel@tonic-gate static db_status
add_to_dictionary(db_dict_desc_p dd,db_table_desc * td)5547c478bd9Sstevel@tonic-gate add_to_dictionary(db_dict_desc_p dd, db_table_desc *td)
5557c478bd9Sstevel@tonic-gate {
5567c478bd9Sstevel@tonic-gate 	register unsigned long hval;
5577c478bd9Sstevel@tonic-gate 	char *target;
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate 	if (dd == NULL)
5607c478bd9Sstevel@tonic-gate 		return (DB_NOTFOUND);
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 	if (td == NULL)
5637c478bd9Sstevel@tonic-gate 		return (DB_NOTFOUND);
5647c478bd9Sstevel@tonic-gate 	target = td->table_name;
5657c478bd9Sstevel@tonic-gate 	if (target == NULL)
5667c478bd9Sstevel@tonic-gate 		return (DB_NOTUNIQUE);
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate 	hval = get_hashval(target);
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate 	if (dd->tables.tables_val == NULL)
5717c478bd9Sstevel@tonic-gate 		grow_dictionary(dd);
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 	db_table_desc_p fst;
5747c478bd9Sstevel@tonic-gate 	unsigned long bucket;
5757c478bd9Sstevel@tonic-gate 	bucket = hval % dd->tables.tables_len;
5767c478bd9Sstevel@tonic-gate 	fst = dd->tables.tables_val[bucket];
5777c478bd9Sstevel@tonic-gate 	td->hashval = hval;
5787c478bd9Sstevel@tonic-gate 	if (fst == NULL)  { /* Empty bucket */
5797c478bd9Sstevel@tonic-gate 		dd->tables.tables_val[bucket] = td;
5807c478bd9Sstevel@tonic-gate 	} else if (!add_to_bucket(fst, &dd->tables.tables_val[bucket], td)) {
5817c478bd9Sstevel@tonic-gate 			return (DB_NOTUNIQUE);
5827c478bd9Sstevel@tonic-gate 		}
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 	/* increase hash table size if number of entries equals table size */
5857c478bd9Sstevel@tonic-gate 	if (++(dd->count) > dd->tables.tables_len)
5867c478bd9Sstevel@tonic-gate 		grow_dictionary(dd);
5877c478bd9Sstevel@tonic-gate 
5887c478bd9Sstevel@tonic-gate 	return (DB_SUCCESS);
5897c478bd9Sstevel@tonic-gate }
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate /* ******************* pickling routines for dictionary ******************* */
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate /* Does the actual writing to/from file specific for db_dict_desc structure. */
5957c478bd9Sstevel@tonic-gate static bool_t
transfer_aux(XDR * x,pptr tbl)5967c478bd9Sstevel@tonic-gate transfer_aux(XDR* x, pptr tbl)
5977c478bd9Sstevel@tonic-gate {
5987c478bd9Sstevel@tonic-gate 	return (xdr_db_dict_desc_p(x, (db_dict_desc_p *) tbl));
5997c478bd9Sstevel@tonic-gate }
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate class pickle_dict_desc: public pickle_file {
6027c478bd9Sstevel@tonic-gate     public:
pickle_dict_desc(char * f,pickle_mode m)6037c478bd9Sstevel@tonic-gate 	pickle_dict_desc(char *f, pickle_mode m) : pickle_file(f, m) {}
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate 	/* Transfers db_dict_desc structure pointed to by dp to/from file. */
transfer(db_dict_desc_p * dp)6067c478bd9Sstevel@tonic-gate 	int transfer(db_dict_desc_p * dp)
6077c478bd9Sstevel@tonic-gate 		{ return (pickle_file::transfer((pptr) dp, &transfer_aux)); }
6087c478bd9Sstevel@tonic-gate };
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate /* ************************ dictionary methods *************************** */
6117c478bd9Sstevel@tonic-gate 
db_dictionary()6127c478bd9Sstevel@tonic-gate db_dictionary::db_dictionary()
6137c478bd9Sstevel@tonic-gate {
6147c478bd9Sstevel@tonic-gate 	dictionary = NULL;
6157c478bd9Sstevel@tonic-gate 	initialized = FALSE;
6167c478bd9Sstevel@tonic-gate 	filename = NULL;
6177c478bd9Sstevel@tonic-gate 	tmpfilename = NULL;
6187c478bd9Sstevel@tonic-gate 	logfilename = NULL;
6197c478bd9Sstevel@tonic-gate 	logfile = NULL;
6207c478bd9Sstevel@tonic-gate 	logfile_opened = FALSE;
6217c478bd9Sstevel@tonic-gate 	changed = FALSE;
6227c478bd9Sstevel@tonic-gate 	INITRW(dict);
6237c478bd9Sstevel@tonic-gate 	READLOCKOK(dict);
6247c478bd9Sstevel@tonic-gate }
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate /*
6277c478bd9Sstevel@tonic-gate  * This routine clones an entire hash bucket chain. If you clone a
6287c478bd9Sstevel@tonic-gate  * data dictionary entry with the ->next pointer set, you will get a
6297c478bd9Sstevel@tonic-gate  * clone of that entry, as well as the entire linked list. This can cause
6307c478bd9Sstevel@tonic-gate  * pain if you then pass the cloned bucket to routines such as
6317c478bd9Sstevel@tonic-gate  * add_to_dictionary(), which do not expect nor handle dictionary hash
6327c478bd9Sstevel@tonic-gate  * entries with the ->next pointer set. You might either get duplicate
6337c478bd9Sstevel@tonic-gate  * entires or lose entries. If you wish to clone the entire bucket chain
6347c478bd9Sstevel@tonic-gate  * and add it to a new dictionary, loop through the db_table_desc->next list
6357c478bd9Sstevel@tonic-gate  * and call add_to_dictionary() for each item.
6367c478bd9Sstevel@tonic-gate  */
6377c478bd9Sstevel@tonic-gate int
db_clone_bucket(db_table_desc * bucket,db_table_desc ** clone)6387c478bd9Sstevel@tonic-gate db_dictionary::db_clone_bucket(db_table_desc *bucket, db_table_desc **clone)
6397c478bd9Sstevel@tonic-gate {
6407c478bd9Sstevel@tonic-gate 	u_long		size;
6417c478bd9Sstevel@tonic-gate 	XDR		xdrs;
6427c478bd9Sstevel@tonic-gate 	char		*bufin = NULL;
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate 	READLOCK(this, DB_LOCK_ERROR, "r db_dictionary::db_clone_bucket");
6457c478bd9Sstevel@tonic-gate 	db_func use_this = xdr_db_table_desc;
6467c478bd9Sstevel@tonic-gate 	size = xdr_sizeof((xdrproc_t) use_this, (void *) bucket);
6477c478bd9Sstevel@tonic-gate 	bufin = (char *) calloc(1, (size_t) size * sizeof (char));
6487c478bd9Sstevel@tonic-gate 	if (!bufin) {
6497c478bd9Sstevel@tonic-gate 		READUNLOCK(this, DB_MEMORY_LIMIT,
6507c478bd9Sstevel@tonic-gate 			"db_dictionary::insert_modified_table: out of memory");
6517c478bd9Sstevel@tonic-gate 		FATAL3("db_dictionary::insert_modified_table: out of memory",
6527c478bd9Sstevel@tonic-gate 			DB_MEMORY_LIMIT, 0);
6537c478bd9Sstevel@tonic-gate 	}
6547c478bd9Sstevel@tonic-gate 	xdrmem_create(&xdrs, bufin, (size_t) size, XDR_ENCODE);
6557c478bd9Sstevel@tonic-gate 	if (!xdr_db_table_desc(&xdrs, bucket)) {
6567c478bd9Sstevel@tonic-gate 		free(bufin);
6577c478bd9Sstevel@tonic-gate 		xdr_destroy(&xdrs);
6587c478bd9Sstevel@tonic-gate 		READUNLOCK(this, DB_MEMORY_LIMIT,
6597c478bd9Sstevel@tonic-gate 		"db_dictionary::insert_modified_table: xdr encode failed");
6607c478bd9Sstevel@tonic-gate 		FATAL3(
6617c478bd9Sstevel@tonic-gate 		"db_dictionary::insert_modified_table: xdr encode failed.",
6627c478bd9Sstevel@tonic-gate 		DB_MEMORY_LIMIT, 0);
6637c478bd9Sstevel@tonic-gate 	}
6647c478bd9Sstevel@tonic-gate 	*clone = (db_table_desc *) calloc(1, (size_t) size * sizeof (char));
6657c478bd9Sstevel@tonic-gate 	if (!*clone) {
6667c478bd9Sstevel@tonic-gate 		xdr_destroy(&xdrs);
6677c478bd9Sstevel@tonic-gate 		free(bufin);
6687c478bd9Sstevel@tonic-gate 		READUNLOCK(this, DB_MEMORY_LIMIT,
6697c478bd9Sstevel@tonic-gate 			"db_dictionary::insert_modified_table: out of memory");
6707c478bd9Sstevel@tonic-gate 		FATAL3("db_dictionary::insert_modified_table: out of memory",
6717c478bd9Sstevel@tonic-gate 			DB_MEMORY_LIMIT, 0);
6727c478bd9Sstevel@tonic-gate 	}
6737c478bd9Sstevel@tonic-gate 
6747c478bd9Sstevel@tonic-gate 	xdrmem_create(&xdrs, bufin, (size_t) size, XDR_DECODE);
6757c478bd9Sstevel@tonic-gate 	if (!xdr_db_table_desc(&xdrs, *clone)) {
6767c478bd9Sstevel@tonic-gate 		free(bufin);
6777c478bd9Sstevel@tonic-gate 		free(*clone);
6787c478bd9Sstevel@tonic-gate 		xdr_destroy(&xdrs);
6797c478bd9Sstevel@tonic-gate 		READUNLOCK(this, DB_MEMORY_LIMIT,
6807c478bd9Sstevel@tonic-gate 		"db_dictionary::insert_modified_table: xdr encode failed");
6817c478bd9Sstevel@tonic-gate 		FATAL3(
6827c478bd9Sstevel@tonic-gate 		"db_dictionary::insert_modified_table: xdr encode failed.",
6837c478bd9Sstevel@tonic-gate 		DB_MEMORY_LIMIT, 0);
6847c478bd9Sstevel@tonic-gate 	}
6857c478bd9Sstevel@tonic-gate 	free(bufin);
6867c478bd9Sstevel@tonic-gate 	xdr_destroy(&xdrs);
6877c478bd9Sstevel@tonic-gate 	READUNLOCK(this, DB_LOCK_ERROR, "ru db_dictionary::db_clone_bucket");
6887c478bd9Sstevel@tonic-gate 	return (1);
6897c478bd9Sstevel@tonic-gate }
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate int
change_table_name(db_table_desc * clone,char * tok,char * repl)6937c478bd9Sstevel@tonic-gate db_dictionary::change_table_name(db_table_desc *clone, char *tok, char *repl)
6947c478bd9Sstevel@tonic-gate {
6957c478bd9Sstevel@tonic-gate 	char 	*newname;
6967c478bd9Sstevel@tonic-gate 	char	*loc_end, *loc_beg;
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate 	WRITELOCK(this, DB_LOCK_ERROR, "w db_dictionary::change_table_name");
6997c478bd9Sstevel@tonic-gate 	while (clone) {
7007c478bd9Sstevel@tonic-gate 		/*
7017c478bd9Sstevel@tonic-gate 		 * Special case for a tok="". This is used for the
7027c478bd9Sstevel@tonic-gate 		 * nisrestore(1M), when restoring a replica in another
7037c478bd9Sstevel@tonic-gate 		 * domain. This routine is used to change the datafile
7047c478bd9Sstevel@tonic-gate 		 * names in the data.dict (see bugid #4031273). This will not
7057c478bd9Sstevel@tonic-gate 		 * effect massage_dict(), since it never generates an empty
7067c478bd9Sstevel@tonic-gate 		 * string for tok.
7077c478bd9Sstevel@tonic-gate 		 */
7087c478bd9Sstevel@tonic-gate 		if (strlen(tok) == 0) {
7097c478bd9Sstevel@tonic-gate 			strcat(clone->table_name, repl);
7107c478bd9Sstevel@tonic-gate 			clone = clone->next;
7117c478bd9Sstevel@tonic-gate 			continue;
7127c478bd9Sstevel@tonic-gate 		}
7137c478bd9Sstevel@tonic-gate 		newname = (char *) calloc(1, sizeof (char) *
7147c478bd9Sstevel@tonic-gate 				strlen(clone->table_name) +
7157c478bd9Sstevel@tonic-gate 				strlen(repl) - strlen(tok) + 1);
7167c478bd9Sstevel@tonic-gate 		if (!newname) {
7177c478bd9Sstevel@tonic-gate 			WRITEUNLOCK(this, DB_MEMORY_LIMIT,
7187c478bd9Sstevel@tonic-gate 			"db_dictionary::change_table_name: out of memory");
7197c478bd9Sstevel@tonic-gate 		    FATAL3("db_dictionary::change_table_name: out of memory.",
7207c478bd9Sstevel@tonic-gate 				DB_MEMORY_LIMIT, 0);
7217c478bd9Sstevel@tonic-gate 		}
7227c478bd9Sstevel@tonic-gate 		if (loc_beg = strstr(clone->table_name, tok)) {
7237c478bd9Sstevel@tonic-gate 			loc_end = loc_beg + strlen(tok);
7247c478bd9Sstevel@tonic-gate 			int s = loc_beg - clone->table_name;
7257c478bd9Sstevel@tonic-gate 			memcpy(newname, clone->table_name, s);
7267c478bd9Sstevel@tonic-gate 			strcat(newname + s, repl);
7277c478bd9Sstevel@tonic-gate 			strcat(newname, loc_end);
7287c478bd9Sstevel@tonic-gate 			free(clone->table_name);
7297c478bd9Sstevel@tonic-gate 			clone->table_name = newname;
7307c478bd9Sstevel@tonic-gate 		} else {
7317c478bd9Sstevel@tonic-gate 			free(newname);
7327c478bd9Sstevel@tonic-gate 		}
7337c478bd9Sstevel@tonic-gate 		clone = clone->next;
7347c478bd9Sstevel@tonic-gate 	}
7357c478bd9Sstevel@tonic-gate 	WRITEUNLOCK(this, DB_LOCK_ERROR,
7367c478bd9Sstevel@tonic-gate 			"wu db_dictionary::change_table_name");
7377c478bd9Sstevel@tonic-gate 	return (1);
7387c478bd9Sstevel@tonic-gate }
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate #ifdef	curdict
7427c478bd9Sstevel@tonic-gate #undef	curdict
7437c478bd9Sstevel@tonic-gate #endif
7447c478bd9Sstevel@tonic-gate /*
7457c478bd9Sstevel@tonic-gate  * A function to initialize the temporary dictionary from the real
7467c478bd9Sstevel@tonic-gate  * dictionary.
7477c478bd9Sstevel@tonic-gate  */
7487c478bd9Sstevel@tonic-gate bool_t
inittemp(char * dictname,db_dictionary & curdict)7497c478bd9Sstevel@tonic-gate db_dictionary::inittemp(char *dictname, db_dictionary& curdict)
7507c478bd9Sstevel@tonic-gate {
7517c478bd9Sstevel@tonic-gate 	int status;
7527c478bd9Sstevel@tonic-gate 	db_table_desc_p	*newtab;
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate 	db_shutdown();
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate 	WRITELOCK(this, FALSE, "w db_dictionary::inittemp");
7577c478bd9Sstevel@tonic-gate 	if (initialized) {
7587c478bd9Sstevel@tonic-gate 		/* Someone else got in between db_shutdown() and lock() */
7597c478bd9Sstevel@tonic-gate 		WRITEUNLOCK(this, FALSE, "wu db_dictionary::inittemp");
7607c478bd9Sstevel@tonic-gate 		return (TRUE);
7617c478bd9Sstevel@tonic-gate 	}
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate 	pickle_dict_desc f(dictname, PICKLE_READ);
7647c478bd9Sstevel@tonic-gate 	filename = strdup(dictname);
7657c478bd9Sstevel@tonic-gate 	if (filename == NULL) {
7667c478bd9Sstevel@tonic-gate 		WRITEUNLOCK(this, FALSE,
7677c478bd9Sstevel@tonic-gate 			"db_dictionary::inittemp: could not allocate space");
7687c478bd9Sstevel@tonic-gate 		FATAL3("db_dictionary::inittemp: could not allocate space",
7697c478bd9Sstevel@tonic-gate 			DB_MEMORY_LIMIT, FALSE);
7707c478bd9Sstevel@tonic-gate 	}
7717c478bd9Sstevel@tonic-gate 	int len = strlen(filename);
7727c478bd9Sstevel@tonic-gate 	tmpfilename = new char[len+5];
7737c478bd9Sstevel@tonic-gate 	if (tmpfilename == NULL) {
7747c478bd9Sstevel@tonic-gate 		delete filename;
7757c478bd9Sstevel@tonic-gate 		WRITEUNLOCK(this, FALSE,
7767c478bd9Sstevel@tonic-gate 			"db_dictionary::inittemp: could not allocate space");
7777c478bd9Sstevel@tonic-gate 		FATAL3("db_dictionary::inittemp: could not allocate space",
7787c478bd9Sstevel@tonic-gate 			DB_MEMORY_LIMIT, FALSE);
7797c478bd9Sstevel@tonic-gate 	}
7807c478bd9Sstevel@tonic-gate 	logfilename = new char[len+5];
7817c478bd9Sstevel@tonic-gate 	if (logfilename == NULL) {
7827c478bd9Sstevel@tonic-gate 		delete filename;
7837c478bd9Sstevel@tonic-gate 		delete tmpfilename;
7847c478bd9Sstevel@tonic-gate 		WRITEUNLOCK(this, FALSE,
7857c478bd9Sstevel@tonic-gate 			"db_dictionary::inittemp: cannot allocate space");
7867c478bd9Sstevel@tonic-gate 		FATAL3("db_dictionary::inittemp: cannot allocate space",
7877c478bd9Sstevel@tonic-gate 			DB_MEMORY_LIMIT, FALSE);
7887c478bd9Sstevel@tonic-gate 	}
7897c478bd9Sstevel@tonic-gate 
7907c478bd9Sstevel@tonic-gate 	sprintf(tmpfilename, "%s.tmp", filename);
7917c478bd9Sstevel@tonic-gate 	sprintf(logfilename, "%s.log", filename);
7927c478bd9Sstevel@tonic-gate 	unlink(tmpfilename);  /* get rid of partial checkpoints */
7937c478bd9Sstevel@tonic-gate 	dictionary = NULL;
7947c478bd9Sstevel@tonic-gate 
7957c478bd9Sstevel@tonic-gate 	if ((status = f.transfer(&dictionary)) < 0) {
7967c478bd9Sstevel@tonic-gate 		initialized = FALSE;
7977c478bd9Sstevel@tonic-gate 	} else if (status == 1) { /* no dictionary exists, create one */
7987c478bd9Sstevel@tonic-gate 		dictionary = new db_dict_desc;
7997c478bd9Sstevel@tonic-gate 		if (dictionary == NULL) {
8007c478bd9Sstevel@tonic-gate 			WRITEUNLOCK(this, FALSE,
8017c478bd9Sstevel@tonic-gate 			"db_dictionary::inittemp: could not allocate space");
8027c478bd9Sstevel@tonic-gate 			FATAL3(
8037c478bd9Sstevel@tonic-gate 			"db_dictionary::inittemp: could not allocate space",
8047c478bd9Sstevel@tonic-gate 			DB_MEMORY_LIMIT, FALSE);
8057c478bd9Sstevel@tonic-gate 		}
8067c478bd9Sstevel@tonic-gate 		dictionary->tables.tables_len =
8077c478bd9Sstevel@tonic-gate 				curdict.dictionary->tables.tables_len;
8087c478bd9Sstevel@tonic-gate 		if ((newtab = (db_table_desc_p *) calloc(
8097c478bd9Sstevel@tonic-gate 			(unsigned int) dictionary->tables.tables_len,
8107c478bd9Sstevel@tonic-gate 			sizeof (db_table_desc_p))) == NULL) {
8117c478bd9Sstevel@tonic-gate 			WRITEUNLOCK(this, FALSE,
8127c478bd9Sstevel@tonic-gate 			"db_dictionary::inittemp: cannot allocate space");
8137c478bd9Sstevel@tonic-gate 			FATAL3(
8147c478bd9Sstevel@tonic-gate 			"db_dictionary::inittemp: cannot allocate space",
8157c478bd9Sstevel@tonic-gate 			DB_MEMORY_LIMIT, 0);
8167c478bd9Sstevel@tonic-gate 		}
8177c478bd9Sstevel@tonic-gate 		dictionary->tables.tables_val = newtab;
8187c478bd9Sstevel@tonic-gate 		dictionary->count = 0;
8197c478bd9Sstevel@tonic-gate 		dictionary->impl_vers = curdict.dictionary->impl_vers;
8207c478bd9Sstevel@tonic-gate 		initialized = TRUE;
8217c478bd9Sstevel@tonic-gate 	} else	/* dictionary loaded successfully */
8227c478bd9Sstevel@tonic-gate 		initialized = TRUE;
8237c478bd9Sstevel@tonic-gate 
8247c478bd9Sstevel@tonic-gate 	if (initialized == TRUE) {
8257c478bd9Sstevel@tonic-gate 		changed = FALSE;
8267c478bd9Sstevel@tonic-gate 		reset_log();
8277c478bd9Sstevel@tonic-gate 	}
8287c478bd9Sstevel@tonic-gate 
8297c478bd9Sstevel@tonic-gate 	WRITEUNLOCK(this, FALSE, "wu db_dictionary::inittemp");
8307c478bd9Sstevel@tonic-gate 	return (initialized);
8317c478bd9Sstevel@tonic-gate }
8327c478bd9Sstevel@tonic-gate 
8337c478bd9Sstevel@tonic-gate 
8347c478bd9Sstevel@tonic-gate /*
8357c478bd9Sstevel@tonic-gate  * This method replaces the token string specified with the replacment
8367c478bd9Sstevel@tonic-gate  * string specified. It assumes that at least one and only one instance of
8377c478bd9Sstevel@tonic-gate  * the token exists. It is the responsibility of the caller to ensure that
8387c478bd9Sstevel@tonic-gate  * the above assumption stays valid.
8397c478bd9Sstevel@tonic-gate  */
8407c478bd9Sstevel@tonic-gate db_status
massage_dict(char * newdictname,char * tok,char * repl)8417c478bd9Sstevel@tonic-gate db_dictionary::massage_dict(char *newdictname, char *tok, char *repl)
8427c478bd9Sstevel@tonic-gate {
8437c478bd9Sstevel@tonic-gate 	int		retval;
8447c478bd9Sstevel@tonic-gate 	u_int		i, tbl_count;
8457c478bd9Sstevel@tonic-gate 	db_status	status;
8467c478bd9Sstevel@tonic-gate 	db_table_desc 	*bucket, *np, *clone, *next_np;
8477c478bd9Sstevel@tonic-gate 	char		tail[NIS_MAXNAMELEN];
8487c478bd9Sstevel@tonic-gate 	db_dictionary	*tmpptr;
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate 	WRITELOCK(this, DB_LOCK_ERROR, "w db_dictionary::massage_dict");
8517c478bd9Sstevel@tonic-gate 	if (dictionary == NULL) {
8527c478bd9Sstevel@tonic-gate 		WRITEUNLOCK(this, DB_INTERNAL_ERROR,
8537c478bd9Sstevel@tonic-gate 		"db_dictionary::massage_dict: uninitialized dictionary file");
8547c478bd9Sstevel@tonic-gate 		FATAL3(
8557c478bd9Sstevel@tonic-gate 		"db_dictionary::massage_dict: uninitialized dictionary file.",
8567c478bd9Sstevel@tonic-gate 		DB_INTERNAL_ERROR, DB_INTERNAL_ERROR);
8577c478bd9Sstevel@tonic-gate 	}
8587c478bd9Sstevel@tonic-gate 
8597c478bd9Sstevel@tonic-gate 	if ((tbl_count = dictionary->count) == 0) {
8607c478bd9Sstevel@tonic-gate 		WRITEUNLOCK(this, DB_SUCCESS,
8617c478bd9Sstevel@tonic-gate 				"wu db_dictionary::massage_dict");
8627c478bd9Sstevel@tonic-gate 		return (DB_SUCCESS);
8637c478bd9Sstevel@tonic-gate 	}
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate 	/* First checkpoint */
8667c478bd9Sstevel@tonic-gate 	if ((status = checkpoint()) != DB_SUCCESS) {
8677c478bd9Sstevel@tonic-gate 		WRITEUNLOCK(this, status, "wu db_dictionary::massage_dict");
8687c478bd9Sstevel@tonic-gate 		return (status);
8697c478bd9Sstevel@tonic-gate 	}
8707c478bd9Sstevel@tonic-gate 
8717c478bd9Sstevel@tonic-gate #ifdef DEBUG
8727c478bd9Sstevel@tonic-gate 	enumerate_dictionary(dictionary, &print_table);
8737c478bd9Sstevel@tonic-gate #endif
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate 	/* Initialize the free dictionary so that we can start populating it */
8767c478bd9Sstevel@tonic-gate 	FreeDictionary->inittemp(newdictname, *this);
8777c478bd9Sstevel@tonic-gate 
8787c478bd9Sstevel@tonic-gate 	for (i = 0; i < dictionary->tables.tables_len; i++) {
8797c478bd9Sstevel@tonic-gate 		bucket = dictionary->tables.tables_val[i];
8807c478bd9Sstevel@tonic-gate 		if (bucket) {
8817c478bd9Sstevel@tonic-gate 			np = bucket;
8827c478bd9Sstevel@tonic-gate 			while (np != NULL) {
8837c478bd9Sstevel@tonic-gate 				next_np = np->next;
8847c478bd9Sstevel@tonic-gate 				retval = db_clone_bucket(np, &clone);
8857c478bd9Sstevel@tonic-gate 				if (retval != 1) {
8867c478bd9Sstevel@tonic-gate 					WRITEUNLOCK(this, DB_INTERNAL_ERROR,
8877c478bd9Sstevel@tonic-gate 					"wu db_dictionary::massage_dict");
8887c478bd9Sstevel@tonic-gate 					return (DB_INTERNAL_ERROR);
8897c478bd9Sstevel@tonic-gate 				}
8907c478bd9Sstevel@tonic-gate 				if (change_table_name(clone, tok, repl) == -1) {
8917c478bd9Sstevel@tonic-gate 					delete_table_desc(clone);
8927c478bd9Sstevel@tonic-gate 					WRITEUNLOCK(this, DB_INTERNAL_ERROR,
8937c478bd9Sstevel@tonic-gate 					"wu db_dictionary::massage_dict");
8947c478bd9Sstevel@tonic-gate 					return (DB_INTERNAL_ERROR);
8957c478bd9Sstevel@tonic-gate 				}
8967c478bd9Sstevel@tonic-gate 				/*
8977c478bd9Sstevel@tonic-gate 				 * We know we don't have a log file, so we will
8987c478bd9Sstevel@tonic-gate 				 * just add to the in-memory database and dump
8997c478bd9Sstevel@tonic-gate 				 * all of it once we are done.
9007c478bd9Sstevel@tonic-gate 				 */
9017c478bd9Sstevel@tonic-gate 				status = add_to_dictionary
9027c478bd9Sstevel@tonic-gate 						(FreeDictionary->dictionary,
9037c478bd9Sstevel@tonic-gate 						clone);
9047c478bd9Sstevel@tonic-gate 				if (status != DB_SUCCESS) {
9057c478bd9Sstevel@tonic-gate 					delete_table_desc(clone);
9067c478bd9Sstevel@tonic-gate 					WRITEUNLOCK(this, DB_INTERNAL_ERROR,
9077c478bd9Sstevel@tonic-gate 					"wu db_dictionary::massage_dict");
9087c478bd9Sstevel@tonic-gate 					return (DB_INTERNAL_ERROR);
9097c478bd9Sstevel@tonic-gate 				}
9107c478bd9Sstevel@tonic-gate 				status = remove_from_dictionary(dictionary,
9117c478bd9Sstevel@tonic-gate 							np->table_name, TRUE);
9127c478bd9Sstevel@tonic-gate 				if (status != DB_SUCCESS) {
9137c478bd9Sstevel@tonic-gate 					delete_table_desc(clone);
9147c478bd9Sstevel@tonic-gate 					WRITEUNLOCK(this, DB_INTERNAL_ERROR,
9157c478bd9Sstevel@tonic-gate 					"wu db_dictionary::massage_dict");
9167c478bd9Sstevel@tonic-gate 					return (DB_INTERNAL_ERROR);
9177c478bd9Sstevel@tonic-gate 				}
9187c478bd9Sstevel@tonic-gate 				np = next_np;
9197c478bd9Sstevel@tonic-gate 			}
9207c478bd9Sstevel@tonic-gate 		}
9217c478bd9Sstevel@tonic-gate 	}
9227c478bd9Sstevel@tonic-gate 
9237c478bd9Sstevel@tonic-gate 	if (FreeDictionary->dump() != DB_SUCCESS) {
9247c478bd9Sstevel@tonic-gate 		WRITEUNLOCK(this, DB_INTERNAL_ERROR,
9257c478bd9Sstevel@tonic-gate 				"wu db_dictionary::massage_dict");
9267c478bd9Sstevel@tonic-gate 		FATAL3(
9277c478bd9Sstevel@tonic-gate 		"db_dictionary::massage_dict: Unable to dump new dictionary.",
9287c478bd9Sstevel@tonic-gate 		DB_INTERNAL_ERROR, DB_INTERNAL_ERROR);
9297c478bd9Sstevel@tonic-gate 	}
9307c478bd9Sstevel@tonic-gate 
9317c478bd9Sstevel@tonic-gate 	/*
9327c478bd9Sstevel@tonic-gate 	 * Now, shutdown the inuse dictionary and update the FreeDictionary
9337c478bd9Sstevel@tonic-gate 	 * and InUseDictionary pointers as well. Also, delete the old dictionary
9347c478bd9Sstevel@tonic-gate 	 * file.
9357c478bd9Sstevel@tonic-gate 	 */
9367c478bd9Sstevel@tonic-gate 	unlink(filename); /* There shouldn't be a tmpfile or logfile */
9377c478bd9Sstevel@tonic-gate 	db_shutdown();
9387c478bd9Sstevel@tonic-gate 	tmpptr = InUseDictionary;
9397c478bd9Sstevel@tonic-gate 	InUseDictionary = FreeDictionary;
9407c478bd9Sstevel@tonic-gate 	FreeDictionary = tmpptr;
9417c478bd9Sstevel@tonic-gate 	WRITEUNLOCK(this, DB_LOCK_ERROR, "wu db_dictionary::massage_dict");
9427c478bd9Sstevel@tonic-gate 	return (DB_SUCCESS);
9437c478bd9Sstevel@tonic-gate }
9447c478bd9Sstevel@tonic-gate 
9457c478bd9Sstevel@tonic-gate 
9467c478bd9Sstevel@tonic-gate db_status
merge_dict(db_dictionary & tempdict,char * tok,char * repl)9477c478bd9Sstevel@tonic-gate db_dictionary::merge_dict(db_dictionary& tempdict, char *tok, char *repl)
9487c478bd9Sstevel@tonic-gate {
9497c478bd9Sstevel@tonic-gate 
9507c478bd9Sstevel@tonic-gate 	db_status	dbstat = DB_SUCCESS;
9517c478bd9Sstevel@tonic-gate 
9527c478bd9Sstevel@tonic-gate 	db_table_desc	*tbl = NULL, *clone = NULL, *next_td = NULL;
9537c478bd9Sstevel@tonic-gate 	int		retval, i;
9547c478bd9Sstevel@tonic-gate 
9557c478bd9Sstevel@tonic-gate 	WRITELOCK(this, DB_LOCK_ERROR, "w db_dictionary::merge_dict");
9567c478bd9Sstevel@tonic-gate 
9577c478bd9Sstevel@tonic-gate 	for (i = 0; i < tempdict.dictionary->tables.tables_len; ++i) {
9587c478bd9Sstevel@tonic-gate 		tbl = tempdict.dictionary->tables.tables_val[i];
9597c478bd9Sstevel@tonic-gate 		if (!tbl)
9607c478bd9Sstevel@tonic-gate 			continue;
9617c478bd9Sstevel@tonic-gate 		retval = db_clone_bucket(tbl, &clone);
9627c478bd9Sstevel@tonic-gate 		if (retval != 1) {
9637c478bd9Sstevel@tonic-gate 			WRITEUNLOCK(this, DB_INTERNAL_ERROR,
9647c478bd9Sstevel@tonic-gate 					"wu db_dictionary::merge_dict");
9657c478bd9Sstevel@tonic-gate 			return (DB_INTERNAL_ERROR);
9667c478bd9Sstevel@tonic-gate 		}
9677c478bd9Sstevel@tonic-gate 		while (clone) {
9687c478bd9Sstevel@tonic-gate 			next_td = clone->next;
9697c478bd9Sstevel@tonic-gate 			clone->next = NULL;
9707c478bd9Sstevel@tonic-gate 			if ((tok) &&
9717c478bd9Sstevel@tonic-gate 				(change_table_name(clone, tok, repl) == -1)) {
9727c478bd9Sstevel@tonic-gate 				delete_table_desc(clone);
9737c478bd9Sstevel@tonic-gate 				if (next_td)
9747c478bd9Sstevel@tonic-gate 					delete_table_desc(next_td);
9757c478bd9Sstevel@tonic-gate 				WRITEUNLOCK(this, DB_INTERNAL_ERROR,
9767c478bd9Sstevel@tonic-gate 					"wu db_dictionary::merge_dict");
9777c478bd9Sstevel@tonic-gate 				return (DB_INTERNAL_ERROR);
9787c478bd9Sstevel@tonic-gate 			}
9797c478bd9Sstevel@tonic-gate 
9807c478bd9Sstevel@tonic-gate 			dbstat = add_to_dictionary(dictionary, clone);
9817c478bd9Sstevel@tonic-gate 			if (dbstat == DB_NOTUNIQUE) {
9827c478bd9Sstevel@tonic-gate 				/* Overide */
9837c478bd9Sstevel@tonic-gate 				dbstat = remove_from_dictionary(dictionary,
9847c478bd9Sstevel@tonic-gate 						clone->table_name, TRUE);
9857c478bd9Sstevel@tonic-gate 				if (dbstat != DB_SUCCESS) {
9867c478bd9Sstevel@tonic-gate 					WRITEUNLOCK(this, dbstat,
9877c478bd9Sstevel@tonic-gate 					"wu db_dictionary::merge_dict");
9887c478bd9Sstevel@tonic-gate 					return (dbstat);
9897c478bd9Sstevel@tonic-gate 				}
9907c478bd9Sstevel@tonic-gate 				dbstat = add_to_dictionary(dictionary,
9917c478bd9Sstevel@tonic-gate 								clone);
9927c478bd9Sstevel@tonic-gate 			} else {
9937c478bd9Sstevel@tonic-gate 				if (dbstat != DB_SUCCESS) {
9947c478bd9Sstevel@tonic-gate 					WRITEUNLOCK(this, dbstat,
9957c478bd9Sstevel@tonic-gate 					"wu db_dictionary::merge_dict");
9967c478bd9Sstevel@tonic-gate 					return (dbstat);
9977c478bd9Sstevel@tonic-gate 				}
9987c478bd9Sstevel@tonic-gate 			}
9997c478bd9Sstevel@tonic-gate 			clone = next_td;
10007c478bd9Sstevel@tonic-gate 		}
10017c478bd9Sstevel@tonic-gate 	}
10027c478bd9Sstevel@tonic-gate /*
10037c478bd9Sstevel@tonic-gate  * If we were successful in merging the dictionaries, then mark the
10047c478bd9Sstevel@tonic-gate  * dictionary changed, so that it will be properly checkpointed and
10057c478bd9Sstevel@tonic-gate  * dumped to disk.
10067c478bd9Sstevel@tonic-gate  */
10077c478bd9Sstevel@tonic-gate 	if (dbstat == DB_SUCCESS)
10087c478bd9Sstevel@tonic-gate 		changed = TRUE;
10097c478bd9Sstevel@tonic-gate 	WRITEUNLOCK(this, DB_LOCK_ERROR, "wu db_dictionary::merge_dict");
10107c478bd9Sstevel@tonic-gate 	return (dbstat);
10117c478bd9Sstevel@tonic-gate }
10127c478bd9Sstevel@tonic-gate 
10137c478bd9Sstevel@tonic-gate int
copyfile(char * infile,char * outfile)10147c478bd9Sstevel@tonic-gate db_dictionary::copyfile(char *infile, char *outfile)
10157c478bd9Sstevel@tonic-gate {
10167c478bd9Sstevel@tonic-gate 	db_table_desc	*tbl = NULL;
10177c478bd9Sstevel@tonic-gate 	db	*dbase;
10187c478bd9Sstevel@tonic-gate 	int	ret;
10197c478bd9Sstevel@tonic-gate 
10207c478bd9Sstevel@tonic-gate 	READLOCK(this, DB_LOCK_ERROR, "r db_dictionary::copyfile");
10217c478bd9Sstevel@tonic-gate 	/*
10227c478bd9Sstevel@tonic-gate 	 * We need to hold the read-lock until the dump() is done.
10237c478bd9Sstevel@tonic-gate 	 * However, we must avoid the lock migration (read -> write)
10247c478bd9Sstevel@tonic-gate 	 * that would happen in find_table() if the db must be loaded.
10257c478bd9Sstevel@tonic-gate 	 * Hence, look first look for an already loaded db.
10267c478bd9Sstevel@tonic-gate 	 */
10277c478bd9Sstevel@tonic-gate 	dbase  = find_table(infile, &tbl, TRUE, TRUE, FALSE);
10287c478bd9Sstevel@tonic-gate 	if (dbase == NULL) {
10297c478bd9Sstevel@tonic-gate 		/* Release the read-lock, and try again, allowing load */
10307c478bd9Sstevel@tonic-gate 		READUNLOCK(this, DB_LOCK_ERROR, "ru db_dictionary::copyfile");
10317c478bd9Sstevel@tonic-gate 		dbase  = find_table(infile, &tbl, TRUE, TRUE, TRUE);
10327c478bd9Sstevel@tonic-gate 		if (dbase == NULL)
10337c478bd9Sstevel@tonic-gate 			return (DB_NOTFOUND);
10347c478bd9Sstevel@tonic-gate 		/*
10357c478bd9Sstevel@tonic-gate 		 * Read-lock again, and get a 'tbl' we can use since we're
10367c478bd9Sstevel@tonic-gate 		 * still holding the lock.
10377c478bd9Sstevel@tonic-gate 		 */
10387c478bd9Sstevel@tonic-gate 		READLOCK(this, DB_LOCK_ERROR, "r db_dictionary::copyfile");
10397c478bd9Sstevel@tonic-gate 		dbase  = find_table(infile, &tbl, TRUE, TRUE, FALSE);
10407c478bd9Sstevel@tonic-gate 		if (dbase == NULL) {
10417c478bd9Sstevel@tonic-gate 			READUNLOCK(this, DB_NOTFOUND,
10427c478bd9Sstevel@tonic-gate 					"ru db_dictionary::copyfile");
10437c478bd9Sstevel@tonic-gate 			return (DB_NOTFOUND);
10447c478bd9Sstevel@tonic-gate 		}
10457c478bd9Sstevel@tonic-gate 	}
10467c478bd9Sstevel@tonic-gate 	ret = tbl->database->dump(outfile) ? DB_SUCCESS : DB_INTERNAL_ERROR;
10477c478bd9Sstevel@tonic-gate 	READUNLOCK(this, ret, "ru db_dictionary::copyfile");
10487c478bd9Sstevel@tonic-gate 	return (ret);
10497c478bd9Sstevel@tonic-gate }
10507c478bd9Sstevel@tonic-gate 
10517c478bd9Sstevel@tonic-gate 
10527c478bd9Sstevel@tonic-gate bool_t
extract_entries(db_dictionary & tempdict,char ** fs,int fscnt)10537c478bd9Sstevel@tonic-gate db_dictionary::extract_entries(db_dictionary& tempdict, char **fs, int fscnt)
10547c478bd9Sstevel@tonic-gate {
10557c478bd9Sstevel@tonic-gate 	int		i, retval;
10567c478bd9Sstevel@tonic-gate 	db_table_desc	*tbl, *clone;
10577c478bd9Sstevel@tonic-gate 	db_table_desc	tbl_ent;
10587c478bd9Sstevel@tonic-gate 	db_status	dbstat;
10597c478bd9Sstevel@tonic-gate 
10607c478bd9Sstevel@tonic-gate 	READLOCK(this, FALSE, "r db_dictionary::extract_entries");
10617c478bd9Sstevel@tonic-gate 	for (i = 0; i < fscnt; ++i) {
10627c478bd9Sstevel@tonic-gate 		tbl = find_table_desc(fs[i]);
10637c478bd9Sstevel@tonic-gate 		if (!tbl) {
10647c478bd9Sstevel@tonic-gate 			syslog(LOG_DEBUG,
10657c478bd9Sstevel@tonic-gate 				"extract_entries: no dictionary entry for %s",
10667c478bd9Sstevel@tonic-gate 				fs[i]);
10677c478bd9Sstevel@tonic-gate 			READUNLOCK(this, FALSE,
10687c478bd9Sstevel@tonic-gate 					"ru db_dictionary::extract_entries");
10697c478bd9Sstevel@tonic-gate 			return (FALSE);
10707c478bd9Sstevel@tonic-gate 		} else {
10717c478bd9Sstevel@tonic-gate 			tbl_ent.table_name = tbl->table_name;
10727c478bd9Sstevel@tonic-gate 			tbl_ent.hashval = tbl->hashval;
10737c478bd9Sstevel@tonic-gate 			tbl_ent.scheme = tbl->scheme;
10747c478bd9Sstevel@tonic-gate 			tbl_ent.database = tbl->database;
10757c478bd9Sstevel@tonic-gate 			tbl_ent.next = NULL;
10767c478bd9Sstevel@tonic-gate 		}
10777c478bd9Sstevel@tonic-gate 		retval = db_clone_bucket(&tbl_ent, &clone);
10787c478bd9Sstevel@tonic-gate 		if (retval != 1) {
10797c478bd9Sstevel@tonic-gate 			syslog(LOG_DEBUG,
10807c478bd9Sstevel@tonic-gate 			"extract_entries: unable to clone entry for %s",
10817c478bd9Sstevel@tonic-gate 			fs[i]);
10827c478bd9Sstevel@tonic-gate 			READUNLOCK(this, FALSE,
10837c478bd9Sstevel@tonic-gate 					"ru db_dictionary::extract_entries");
10847c478bd9Sstevel@tonic-gate 			return (FALSE);
10857c478bd9Sstevel@tonic-gate 		}
10867c478bd9Sstevel@tonic-gate 		dbstat = add_to_dictionary(tempdict.dictionary, clone);
10877c478bd9Sstevel@tonic-gate 		if (dbstat != DB_SUCCESS) {
10887c478bd9Sstevel@tonic-gate 			delete_table_desc(clone);
10897c478bd9Sstevel@tonic-gate 			READUNLOCK(this, FALSE,
10907c478bd9Sstevel@tonic-gate 					"ru db_dictionary::extract_entries");
10917c478bd9Sstevel@tonic-gate 			return (FALSE);
10927c478bd9Sstevel@tonic-gate 		}
10937c478bd9Sstevel@tonic-gate 	}
10947c478bd9Sstevel@tonic-gate 	if (tempdict.dump() != DB_SUCCESS) {
10957c478bd9Sstevel@tonic-gate 		READUNLOCK(this, FALSE,
10967c478bd9Sstevel@tonic-gate 				"ru db_dictionary::extract_entries");
10977c478bd9Sstevel@tonic-gate 		return (FALSE);
10987c478bd9Sstevel@tonic-gate 	}
10997c478bd9Sstevel@tonic-gate 	READUNLOCK(this, FALSE,
11007c478bd9Sstevel@tonic-gate 			"ru db_dictionary::extract_entries");
11017c478bd9Sstevel@tonic-gate 	return (TRUE);
11027c478bd9Sstevel@tonic-gate }
11037c478bd9Sstevel@tonic-gate 
11047c478bd9Sstevel@tonic-gate 
11057c478bd9Sstevel@tonic-gate /*
11067c478bd9Sstevel@tonic-gate  * Initialize dictionary from contents in 'file'.
11077c478bd9Sstevel@tonic-gate  * If there is already information in this dictionary, it is removed.
11087c478bd9Sstevel@tonic-gate  * Therefore, regardless of whether the load from the file succeeds,
11097c478bd9Sstevel@tonic-gate  * the contents of this dictionary will be altered.  Returns
11107c478bd9Sstevel@tonic-gate  * whether table has been initialized successfully.
11117c478bd9Sstevel@tonic-gate  */
11127c478bd9Sstevel@tonic-gate bool_t
init(char * file)11137c478bd9Sstevel@tonic-gate db_dictionary::init(char *file)
11147c478bd9Sstevel@tonic-gate {
11157c478bd9Sstevel@tonic-gate 	int status;
11167c478bd9Sstevel@tonic-gate 
11177c478bd9Sstevel@tonic-gate 	WRITELOCK(this, FALSE, "w db_dictionary::init");
11187c478bd9Sstevel@tonic-gate 	db_shutdown();
11197c478bd9Sstevel@tonic-gate 
11207c478bd9Sstevel@tonic-gate 	pickle_dict_desc f(file, PICKLE_READ);
11217c478bd9Sstevel@tonic-gate 	filename = strdup(file);
11227c478bd9Sstevel@tonic-gate 	if (filename == NULL) {
11237c478bd9Sstevel@tonic-gate 		WRITEUNLOCK(this, FALSE,
11247c478bd9Sstevel@tonic-gate 			"db_dictionary::init: could not allocate space");
11257c478bd9Sstevel@tonic-gate 		FATAL3("db_dictionary::init: could not allocate space",
11267c478bd9Sstevel@tonic-gate 			DB_MEMORY_LIMIT, FALSE);
11277c478bd9Sstevel@tonic-gate 	}
11287c478bd9Sstevel@tonic-gate 	int len = strlen(filename);
11297c478bd9Sstevel@tonic-gate 	tmpfilename = new char[len+5];
11307c478bd9Sstevel@tonic-gate 	if (tmpfilename == NULL) {
11317c478bd9Sstevel@tonic-gate 		delete filename;
11327c478bd9Sstevel@tonic-gate 		WRITEUNLOCK(this, FALSE,
11337c478bd9Sstevel@tonic-gate 			"db_dictionary::init: could not allocate space");
11347c478bd9Sstevel@tonic-gate 		FATAL3("db_dictionary::init: could not allocate space",
11357c478bd9Sstevel@tonic-gate 			DB_MEMORY_LIMIT, FALSE);
11367c478bd9Sstevel@tonic-gate 	}
11377c478bd9Sstevel@tonic-gate 	logfilename = new char[len+5];
11387c478bd9Sstevel@tonic-gate 	if (logfilename == NULL) {
11397c478bd9Sstevel@tonic-gate 		delete filename;
11407c478bd9Sstevel@tonic-gate 		delete tmpfilename;
11417c478bd9Sstevel@tonic-gate 		WRITEUNLOCK(this, FALSE,
11427c478bd9Sstevel@tonic-gate 				"db_dictionary::init: cannot allocate space");
11437c478bd9Sstevel@tonic-gate 		FATAL3("db_dictionary::init: cannot allocate space",
11447c478bd9Sstevel@tonic-gate 			DB_MEMORY_LIMIT, FALSE);
11457c478bd9Sstevel@tonic-gate 	}
11467c478bd9Sstevel@tonic-gate 
11477c478bd9Sstevel@tonic-gate 	sprintf(tmpfilename, "%s.tmp", filename);
11487c478bd9Sstevel@tonic-gate 	sprintf(logfilename, "%s.log", filename);
11497c478bd9Sstevel@tonic-gate 	unlink(tmpfilename);  /* get rid of partial checkpoints */
11507c478bd9Sstevel@tonic-gate 	dictionary = NULL;
11517c478bd9Sstevel@tonic-gate 
11527c478bd9Sstevel@tonic-gate 	/* load dictionary */
11537c478bd9Sstevel@tonic-gate 	if ((status = f.transfer(&dictionary)) < 0) {
11547c478bd9Sstevel@tonic-gate 	    initialized = FALSE;
11557c478bd9Sstevel@tonic-gate 	} else if (status == 1) {  /* no dictionary exists, create one */
11567c478bd9Sstevel@tonic-gate 	    dictionary = new db_dict_desc;
11577c478bd9Sstevel@tonic-gate 	    if (dictionary == NULL) {
11587c478bd9Sstevel@tonic-gate 		WRITEUNLOCK(this, FALSE,
11597c478bd9Sstevel@tonic-gate 			"db_dictionary::init: could not allocate space");
11607c478bd9Sstevel@tonic-gate 		FATAL3("db_dictionary::init: could not allocate space",
11617c478bd9Sstevel@tonic-gate 			DB_MEMORY_LIMIT, FALSE);
11627c478bd9Sstevel@tonic-gate 	    }
11637c478bd9Sstevel@tonic-gate 	    dictionary->tables.tables_len = 0;
1164