xref: /illumos-gate/usr/src/lib/libnisdb/db_headers.h (revision a506a34c)
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_headers.h
247c478bd9Sstevel@tonic-gate  *
25*a506a34cSth  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
26*a506a34cSth  * Use is subject to license terms.
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #ifndef _DB_HEADERS_H
327c478bd9Sstevel@tonic-gate #define	_DB_HEADERS_H
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
357c478bd9Sstevel@tonic-gate #include <syslog.h>
367c478bd9Sstevel@tonic-gate #include <stdlib.h>
377c478bd9Sstevel@tonic-gate #include <setjmp.h>
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate extern int verbose;
407c478bd9Sstevel@tonic-gate extern jmp_buf dbenv;
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #define	FATAL(msg, fcode) \
437c478bd9Sstevel@tonic-gate 	{ \
447c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "ERROR: %s", (msg)); \
457c478bd9Sstevel@tonic-gate 		__nisdb_get_tsd()->fatalcode = (int)(fcode); \
467c478bd9Sstevel@tonic-gate 		__nisdb_get_tsd()->fatalmsg = msg; \
477c478bd9Sstevel@tonic-gate 		return; \
487c478bd9Sstevel@tonic-gate 	}
497c478bd9Sstevel@tonic-gate #define	FATAL3(msg, fcode, retval) \
507c478bd9Sstevel@tonic-gate 	{ \
517c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "ERROR: %s", (msg)); \
527c478bd9Sstevel@tonic-gate 		__nisdb_get_tsd()->fatalcode = (int)(fcode); \
537c478bd9Sstevel@tonic-gate 		__nisdb_get_tsd()->fatalmsg = msg; \
547c478bd9Sstevel@tonic-gate 		return (retval); \
557c478bd9Sstevel@tonic-gate 	}
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate #ifdef	NISDB_MT_DEBUG
587c478bd9Sstevel@tonic-gate #define	LOCKVAL(lockcall, msg, lockcode) \
597c478bd9Sstevel@tonic-gate 	{ \
607c478bd9Sstevel@tonic-gate 		lockcode = lockcall(); \
617c478bd9Sstevel@tonic-gate 		if (lockcode != 0) { \
627c478bd9Sstevel@tonic-gate 			__nisdb_get_tsd()->fatalcode = lockcode; \
637c478bd9Sstevel@tonic-gate 			__nisdb_get_tsd()->fatalmsg = msg; \
647c478bd9Sstevel@tonic-gate 			abort(); \
657c478bd9Sstevel@tonic-gate 		} \
667c478bd9Sstevel@tonic-gate 	}
677c478bd9Sstevel@tonic-gate #else
687c478bd9Sstevel@tonic-gate #define	LOCKVAL(lockcall, msg, lockcode) \
697c478bd9Sstevel@tonic-gate 	{ \
707c478bd9Sstevel@tonic-gate 		lockcode = lockcall(); \
717c478bd9Sstevel@tonic-gate 		if (lockcode != 0) { \
727c478bd9Sstevel@tonic-gate 			__nisdb_get_tsd()->fatalcode = lockcode; \
737c478bd9Sstevel@tonic-gate 			__nisdb_get_tsd()->fatalmsg = msg; \
747c478bd9Sstevel@tonic-gate 		} \
757c478bd9Sstevel@tonic-gate 	}
767c478bd9Sstevel@tonic-gate #endif	/* NISDB_MT_DEBUG */
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate #define	LOCKV(lockcall, msg) \
797c478bd9Sstevel@tonic-gate 	{ \
807c478bd9Sstevel@tonic-gate 		int	lockcode; \
817c478bd9Sstevel@tonic-gate 		LOCKVAL(lockcall, msg, lockcode); \
827c478bd9Sstevel@tonic-gate 		if (lockcode != 0) \
837c478bd9Sstevel@tonic-gate 			return; \
847c478bd9Sstevel@tonic-gate 	}
857c478bd9Sstevel@tonic-gate #define	LOCK(lockcall, retval, msg) \
867c478bd9Sstevel@tonic-gate 	{ \
877c478bd9Sstevel@tonic-gate 		int	lockcode; \
887c478bd9Sstevel@tonic-gate 		LOCKVAL(lockcall, msg, lockcode); \
897c478bd9Sstevel@tonic-gate 		if (lockcode != 0) \
907c478bd9Sstevel@tonic-gate 			return (retval); \
917c478bd9Sstevel@tonic-gate 	}
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate /* Read lock/unlock 'this', return 'retval' is unsuccessful, and save 'msg' */
947c478bd9Sstevel@tonic-gate #define	READLOCK(this, retval, msg) \
957c478bd9Sstevel@tonic-gate 	LOCK(this->acqnonexcl, retval, msg)
967c478bd9Sstevel@tonic-gate #define	READUNLOCK(this, retval, msg) \
977c478bd9Sstevel@tonic-gate 	LOCK(this->relnonexcl, retval, msg)
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate /* Ditto, but return without a value (i.e., a "void" function */
1007c478bd9Sstevel@tonic-gate #define	READLOCKV(this, msg) \
1017c478bd9Sstevel@tonic-gate 	LOCKV(this->acqnonexcl, msg)
1027c478bd9Sstevel@tonic-gate #define	READUNLOCKV(this, msg) \
1037c478bd9Sstevel@tonic-gate 	LOCKV(this->relnonexcl, msg)
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate /* As READLOCK/READUNLOCK, but set rescode instead of returning on failure */
1067c478bd9Sstevel@tonic-gate #define	READLOCKNR(this, rescode, msg) \
1077c478bd9Sstevel@tonic-gate 	LOCKVAL(this->acqnonexcl, msg, rescode)
1087c478bd9Sstevel@tonic-gate #define	READUNLOCKNR(this, rescode, msg) \
1097c478bd9Sstevel@tonic-gate 	LOCKVAL(this->relnonexcl, msg, rescode)
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate /* As READLOCK/READUNLOCK, but use a write lock */
1127c478bd9Sstevel@tonic-gate #define	WRITELOCK(this, retval, msg) \
1137c478bd9Sstevel@tonic-gate 	LOCK(this->acqexcl, retval, msg)
1147c478bd9Sstevel@tonic-gate #define	WRITEUNLOCK(this, retval, msg) \
1157c478bd9Sstevel@tonic-gate 	LOCK(this->relexcl, retval, msg)
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate /* Non-blocking write lock */
1187c478bd9Sstevel@tonic-gate #define	TRYWRITELOCK(this, rescode, msg) \
1197c478bd9Sstevel@tonic-gate 	LOCKVAL(this->tryacqexcl, msg, rescode)
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate /* Ditto, but return without a value */
1227c478bd9Sstevel@tonic-gate #define	WRITELOCKV(this, msg) \
1237c478bd9Sstevel@tonic-gate 	LOCKV(this->acqexcl, msg)
1247c478bd9Sstevel@tonic-gate #define	WRITEUNLOCKV(this, msg) \
1257c478bd9Sstevel@tonic-gate 	LOCKV(this->relexcl, msg)
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate /* As WRITELOCK/WRITEUNLOCK, but set rescode instead of returning on failure */
1287c478bd9Sstevel@tonic-gate #define	WRITELOCKNR(this, rescode, msg) \
1297c478bd9Sstevel@tonic-gate 	LOCKVAL(this->acqexcl, msg, rescode)
1307c478bd9Sstevel@tonic-gate #define	WRITEUNLOCKNR(this, rescode, msg) \
1317c478bd9Sstevel@tonic-gate 	LOCKVAL(this->relexcl, msg, rescode)
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate /* Apply a second write lock when already holding another write lock */
1347c478bd9Sstevel@tonic-gate #define	WRITELOCK2(this, retval, msg, that) \
1357c478bd9Sstevel@tonic-gate 	if (this != 0) { \
1367c478bd9Sstevel@tonic-gate 		int	lockcode1, lockcode2; \
1377c478bd9Sstevel@tonic-gate 		WRITELOCKNR(this, lockcode2, msg); \
1387c478bd9Sstevel@tonic-gate 		if (lockcode2 != 0) { \
1397c478bd9Sstevel@tonic-gate 			if (that != 0) { \
1407c478bd9Sstevel@tonic-gate 				WRITEUNLOCKNR(that, lockcode1, msg); \
1417c478bd9Sstevel@tonic-gate 			} \
1427c478bd9Sstevel@tonic-gate 			return (retval); \
1437c478bd9Sstevel@tonic-gate 		} \
1447c478bd9Sstevel@tonic-gate 	}
1457c478bd9Sstevel@tonic-gate /* Release two write locks */
1467c478bd9Sstevel@tonic-gate #define	WRITEUNLOCK2(this, that, retval1, retval2, msg1, msg2) \
1477c478bd9Sstevel@tonic-gate 	{ \
1487c478bd9Sstevel@tonic-gate 		int	lockcode1 = 0, lockcode2 = 0; \
1497c478bd9Sstevel@tonic-gate 		if (this != 0) { \
1507c478bd9Sstevel@tonic-gate 			WRITEUNLOCKNR(this, lockcode1, msg1); \
1517c478bd9Sstevel@tonic-gate 		} \
1527c478bd9Sstevel@tonic-gate 		if (that != 0) { \
1537c478bd9Sstevel@tonic-gate 			WRITEUNLOCKNR(that, lockcode2, msg2); \
1547c478bd9Sstevel@tonic-gate 		} \
1557c478bd9Sstevel@tonic-gate 		if (lockcode2 != 0) { \
1567c478bd9Sstevel@tonic-gate 			return (retval2); \
1577c478bd9Sstevel@tonic-gate 		} else if (lockcode1 != 0) { \
1587c478bd9Sstevel@tonic-gate 			return (retval1); \
1597c478bd9Sstevel@tonic-gate 		} \
1607c478bd9Sstevel@tonic-gate 	}
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate /* Apply a second read lock when already holding another read lock */
1637c478bd9Sstevel@tonic-gate #define	READLOCK2(this, retval, msg, that) \
1647c478bd9Sstevel@tonic-gate 	if (this != 0) { \
1657c478bd9Sstevel@tonic-gate 		int	lockcode1, lockcode2; \
1667c478bd9Sstevel@tonic-gate 		READLOCKNR(this, lockcode2, msg); \
1677c478bd9Sstevel@tonic-gate 		if (lockcode2 != 0) { \
1687c478bd9Sstevel@tonic-gate 			if (that != 0) { \
1697c478bd9Sstevel@tonic-gate 				READUNLOCKNR(that, lockcode1, msg); \
1707c478bd9Sstevel@tonic-gate 			} \
1717c478bd9Sstevel@tonic-gate 			return (retval); \
1727c478bd9Sstevel@tonic-gate 		} \
1737c478bd9Sstevel@tonic-gate 	}
1747c478bd9Sstevel@tonic-gate /* Release two read locks */
1757c478bd9Sstevel@tonic-gate #define	READUNLOCK2(this, that, retval1, retval2, msg1, msg2) \
1767c478bd9Sstevel@tonic-gate 	{ \
1777c478bd9Sstevel@tonic-gate 		int	lockcode1 = 0, lockcode2 = 0; \
1787c478bd9Sstevel@tonic-gate 		if (this != 0) { \
1797c478bd9Sstevel@tonic-gate 			READUNLOCKNR(this, lockcode1, msg1); \
1807c478bd9Sstevel@tonic-gate 		} \
1817c478bd9Sstevel@tonic-gate 		if (that != 0) { \
1827c478bd9Sstevel@tonic-gate 			READUNLOCKNR(that, lockcode2, msg2); \
1837c478bd9Sstevel@tonic-gate 		} \
1847c478bd9Sstevel@tonic-gate 		if (lockcode2 != 0) { \
1857c478bd9Sstevel@tonic-gate 			return (retval2); \
1867c478bd9Sstevel@tonic-gate 		} else if (lockcode1 != 0) { \
1877c478bd9Sstevel@tonic-gate 			return (retval1); \
1887c478bd9Sstevel@tonic-gate 		} \
1897c478bd9Sstevel@tonic-gate 	}
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate #define	ASSERTWRITELOCKHELD(lvar, retval, msg) \
1927c478bd9Sstevel@tonic-gate 	{ \
1937c478bd9Sstevel@tonic-gate 		int	lc; \
1947c478bd9Sstevel@tonic-gate 		if ((lc = __nisdb_assert_wheld(&lvar ## _rwlock)) != 0) { \
1957c478bd9Sstevel@tonic-gate 			__nisdb_get_tsd()->fatalcode = lc; \
1967c478bd9Sstevel@tonic-gate 			__nisdb_get_tsd()->fatalmsg = msg; \
1977c478bd9Sstevel@tonic-gate 			return (retval); \
1987c478bd9Sstevel@tonic-gate 		} \
1997c478bd9Sstevel@tonic-gate 	}
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate #define	WARNING(x) { syslog(LOG_ERR, "WARNING: %s", (x)); }
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate #define	WARNING_M(x) { syslog(LOG_ERR, "WARNING: %s: %m", (x)); }
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate enum db_status {DB_SUCCESS, DB_NOTFOUND, DB_NOTUNIQUE,
2077c478bd9Sstevel@tonic-gate 		    DB_BADTABLE, DB_BADQUERY, DB_BADOBJECT,
2087c478bd9Sstevel@tonic-gate 		DB_MEMORY_LIMIT, DB_STORAGE_LIMIT, DB_INTERNAL_ERROR,
2097c478bd9Sstevel@tonic-gate 		DB_BADDICTIONARY, DB_SYNC_FAILED, DB_LOCK_ERROR};
2107c478bd9Sstevel@tonic-gate typedef enum db_status db_status;
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate enum db_action {DB_LOOKUP, DB_REMOVE, DB_ADD, DB_FIRST, DB_NEXT, DB_ALL,
2137c478bd9Sstevel@tonic-gate 			DB_RESET_NEXT, DB_ADD_NOLOG,
2147c478bd9Sstevel@tonic-gate 			DB_ADD_NOSYNC, DB_REMOVE_NOSYNC };
2157c478bd9Sstevel@tonic-gate typedef enum db_action db_action;
2167c478bd9Sstevel@tonic-gate 
217*a506a34cSth #endif /* _DB_HEADERS_H */
218