xref: /illumos-gate/usr/src/lib/libnisdb/db_headers.h (revision 9d8d665c)
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
5d67944fbSScott Rotondo  * Common Development and Distribution License (the "License").
6d67944fbSScott Rotondo  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
227c478bd9Sstevel@tonic-gate  *	db_headers.h
237c478bd9Sstevel@tonic-gate  *
24d67944fbSScott Rotondo  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
25a506a34cSth  * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #ifndef _DB_HEADERS_H
297c478bd9Sstevel@tonic-gate #define	_DB_HEADERS_H
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
327c478bd9Sstevel@tonic-gate #include <syslog.h>
337c478bd9Sstevel@tonic-gate #include <stdlib.h>
347c478bd9Sstevel@tonic-gate #include <setjmp.h>
357c478bd9Sstevel@tonic-gate 
36d67944fbSScott Rotondo #ifdef  __cplusplus
37d67944fbSScott Rotondo extern "C" {
38d67944fbSScott Rotondo #endif
39d67944fbSScott Rotondo 
407c478bd9Sstevel@tonic-gate extern int verbose;
41d67944fbSScott Rotondo 
42d67944fbSScott Rotondo #ifdef  __cplusplus
43d67944fbSScott Rotondo }
44d67944fbSScott Rotondo #endif
45d67944fbSScott Rotondo 
467c478bd9Sstevel@tonic-gate extern jmp_buf dbenv;
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate #define	FATAL(msg, fcode) \
497c478bd9Sstevel@tonic-gate 	{ \
507c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "ERROR: %s", (msg)); \
517c478bd9Sstevel@tonic-gate 		__nisdb_get_tsd()->fatalcode = (int)(fcode); \
527c478bd9Sstevel@tonic-gate 		__nisdb_get_tsd()->fatalmsg = msg; \
537c478bd9Sstevel@tonic-gate 		return; \
547c478bd9Sstevel@tonic-gate 	}
557c478bd9Sstevel@tonic-gate #define	FATAL3(msg, fcode, retval) \
567c478bd9Sstevel@tonic-gate 	{ \
577c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "ERROR: %s", (msg)); \
587c478bd9Sstevel@tonic-gate 		__nisdb_get_tsd()->fatalcode = (int)(fcode); \
597c478bd9Sstevel@tonic-gate 		__nisdb_get_tsd()->fatalmsg = msg; \
607c478bd9Sstevel@tonic-gate 		return (retval); \
617c478bd9Sstevel@tonic-gate 	}
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate #ifdef	NISDB_MT_DEBUG
647c478bd9Sstevel@tonic-gate #define	LOCKVAL(lockcall, msg, lockcode) \
657c478bd9Sstevel@tonic-gate 	{ \
667c478bd9Sstevel@tonic-gate 		lockcode = lockcall(); \
677c478bd9Sstevel@tonic-gate 		if (lockcode != 0) { \
687c478bd9Sstevel@tonic-gate 			__nisdb_get_tsd()->fatalcode = lockcode; \
697c478bd9Sstevel@tonic-gate 			__nisdb_get_tsd()->fatalmsg = msg; \
707c478bd9Sstevel@tonic-gate 			abort(); \
717c478bd9Sstevel@tonic-gate 		} \
727c478bd9Sstevel@tonic-gate 	}
737c478bd9Sstevel@tonic-gate #else
747c478bd9Sstevel@tonic-gate #define	LOCKVAL(lockcall, msg, lockcode) \
757c478bd9Sstevel@tonic-gate 	{ \
767c478bd9Sstevel@tonic-gate 		lockcode = lockcall(); \
777c478bd9Sstevel@tonic-gate 		if (lockcode != 0) { \
787c478bd9Sstevel@tonic-gate 			__nisdb_get_tsd()->fatalcode = lockcode; \
797c478bd9Sstevel@tonic-gate 			__nisdb_get_tsd()->fatalmsg = msg; \
807c478bd9Sstevel@tonic-gate 		} \
817c478bd9Sstevel@tonic-gate 	}
827c478bd9Sstevel@tonic-gate #endif	/* NISDB_MT_DEBUG */
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate #define	LOCKV(lockcall, msg) \
857c478bd9Sstevel@tonic-gate 	{ \
867c478bd9Sstevel@tonic-gate 		int	lockcode; \
877c478bd9Sstevel@tonic-gate 		LOCKVAL(lockcall, msg, lockcode); \
887c478bd9Sstevel@tonic-gate 		if (lockcode != 0) \
897c478bd9Sstevel@tonic-gate 			return; \
907c478bd9Sstevel@tonic-gate 	}
917c478bd9Sstevel@tonic-gate #define	LOCK(lockcall, retval, msg) \
927c478bd9Sstevel@tonic-gate 	{ \
937c478bd9Sstevel@tonic-gate 		int	lockcode; \
947c478bd9Sstevel@tonic-gate 		LOCKVAL(lockcall, msg, lockcode); \
957c478bd9Sstevel@tonic-gate 		if (lockcode != 0) \
967c478bd9Sstevel@tonic-gate 			return (retval); \
977c478bd9Sstevel@tonic-gate 	}
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate /* Read lock/unlock 'this', return 'retval' is unsuccessful, and save 'msg' */
1007c478bd9Sstevel@tonic-gate #define	READLOCK(this, retval, msg) \
1017c478bd9Sstevel@tonic-gate 	LOCK(this->acqnonexcl, retval, msg)
1027c478bd9Sstevel@tonic-gate #define	READUNLOCK(this, retval, msg) \
1037c478bd9Sstevel@tonic-gate 	LOCK(this->relnonexcl, retval, msg)
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate /* Ditto, but return without a value (i.e., a "void" function */
1067c478bd9Sstevel@tonic-gate #define	READLOCKV(this, msg) \
1077c478bd9Sstevel@tonic-gate 	LOCKV(this->acqnonexcl, msg)
1087c478bd9Sstevel@tonic-gate #define	READUNLOCKV(this, msg) \
1097c478bd9Sstevel@tonic-gate 	LOCKV(this->relnonexcl, msg)
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate /* As READLOCK/READUNLOCK, but set rescode instead of returning on failure */
1127c478bd9Sstevel@tonic-gate #define	READLOCKNR(this, rescode, msg) \
1137c478bd9Sstevel@tonic-gate 	LOCKVAL(this->acqnonexcl, msg, rescode)
1147c478bd9Sstevel@tonic-gate #define	READUNLOCKNR(this, rescode, msg) \
1157c478bd9Sstevel@tonic-gate 	LOCKVAL(this->relnonexcl, msg, rescode)
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate /* As READLOCK/READUNLOCK, but use a write lock */
1187c478bd9Sstevel@tonic-gate #define	WRITELOCK(this, retval, msg) \
1197c478bd9Sstevel@tonic-gate 	LOCK(this->acqexcl, retval, msg)
1207c478bd9Sstevel@tonic-gate #define	WRITEUNLOCK(this, retval, msg) \
1217c478bd9Sstevel@tonic-gate 	LOCK(this->relexcl, retval, msg)
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate /* Non-blocking write lock */
1247c478bd9Sstevel@tonic-gate #define	TRYWRITELOCK(this, rescode, msg) \
1257c478bd9Sstevel@tonic-gate 	LOCKVAL(this->tryacqexcl, msg, rescode)
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate /* Ditto, but return without a value */
1287c478bd9Sstevel@tonic-gate #define	WRITELOCKV(this, msg) \
1297c478bd9Sstevel@tonic-gate 	LOCKV(this->acqexcl, msg)
1307c478bd9Sstevel@tonic-gate #define	WRITEUNLOCKV(this, msg) \
1317c478bd9Sstevel@tonic-gate 	LOCKV(this->relexcl, msg)
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate /* As WRITELOCK/WRITEUNLOCK, but set rescode instead of returning on failure */
1347c478bd9Sstevel@tonic-gate #define	WRITELOCKNR(this, rescode, msg) \
1357c478bd9Sstevel@tonic-gate 	LOCKVAL(this->acqexcl, msg, rescode)
1367c478bd9Sstevel@tonic-gate #define	WRITEUNLOCKNR(this, rescode, msg) \
1377c478bd9Sstevel@tonic-gate 	LOCKVAL(this->relexcl, msg, rescode)
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate /* Apply a second write lock when already holding another write lock */
1407c478bd9Sstevel@tonic-gate #define	WRITELOCK2(this, retval, msg, that) \
1417c478bd9Sstevel@tonic-gate 	if (this != 0) { \
1427c478bd9Sstevel@tonic-gate 		int	lockcode1, lockcode2; \
1437c478bd9Sstevel@tonic-gate 		WRITELOCKNR(this, lockcode2, msg); \
1447c478bd9Sstevel@tonic-gate 		if (lockcode2 != 0) { \
145*9d8d665cSToomas Soome 			WRITEUNLOCKNR(that, lockcode1, msg); \
1467c478bd9Sstevel@tonic-gate 			return (retval); \
1477c478bd9Sstevel@tonic-gate 		} \
1487c478bd9Sstevel@tonic-gate 	}
1497c478bd9Sstevel@tonic-gate /* Release two write locks */
1507c478bd9Sstevel@tonic-gate #define	WRITEUNLOCK2(this, that, retval1, retval2, msg1, msg2) \
1517c478bd9Sstevel@tonic-gate 	{ \
1527c478bd9Sstevel@tonic-gate 		int	lockcode1 = 0, lockcode2 = 0; \
153*9d8d665cSToomas Soome 		WRITEUNLOCKNR(this, lockcode1, msg1); \
154*9d8d665cSToomas Soome 		WRITEUNLOCKNR(that, lockcode2, msg2); \
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) { \
168*9d8d665cSToomas Soome 			READUNLOCKNR(that, lockcode1, msg); \
1697c478bd9Sstevel@tonic-gate 			return (retval); \
1707c478bd9Sstevel@tonic-gate 		} \
1717c478bd9Sstevel@tonic-gate 	}
1727c478bd9Sstevel@tonic-gate /* Release two read locks */
1737c478bd9Sstevel@tonic-gate #define	READUNLOCK2(this, that, retval1, retval2, msg1, msg2) \
1747c478bd9Sstevel@tonic-gate 	{ \
1757c478bd9Sstevel@tonic-gate 		int	lockcode1 = 0, lockcode2 = 0; \
176*9d8d665cSToomas Soome 		READUNLOCKNR(this, lockcode1, msg1); \
177*9d8d665cSToomas Soome 		READUNLOCKNR(that, lockcode2, msg2); \
1787c478bd9Sstevel@tonic-gate 		if (lockcode2 != 0) { \
1797c478bd9Sstevel@tonic-gate 			return (retval2); \
1807c478bd9Sstevel@tonic-gate 		} else if (lockcode1 != 0) { \
1817c478bd9Sstevel@tonic-gate 			return (retval1); \
1827c478bd9Sstevel@tonic-gate 		} \
1837c478bd9Sstevel@tonic-gate 	}
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate #define	ASSERTWRITELOCKHELD(lvar, retval, msg) \
1867c478bd9Sstevel@tonic-gate 	{ \
1877c478bd9Sstevel@tonic-gate 		int	lc; \
1887c478bd9Sstevel@tonic-gate 		if ((lc = __nisdb_assert_wheld(&lvar ## _rwlock)) != 0) { \
1897c478bd9Sstevel@tonic-gate 			__nisdb_get_tsd()->fatalcode = lc; \
1907c478bd9Sstevel@tonic-gate 			__nisdb_get_tsd()->fatalmsg = msg; \
1917c478bd9Sstevel@tonic-gate 			return (retval); \
1927c478bd9Sstevel@tonic-gate 		} \
1937c478bd9Sstevel@tonic-gate 	}
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate #define	WARNING(x) { syslog(LOG_ERR, "WARNING: %s", (x)); }
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate #define	WARNING_M(x) { syslog(LOG_ERR, "WARNING: %s: %m", (x)); }
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate enum db_status {DB_SUCCESS, DB_NOTFOUND, DB_NOTUNIQUE,
2017c478bd9Sstevel@tonic-gate 		    DB_BADTABLE, DB_BADQUERY, DB_BADOBJECT,
2027c478bd9Sstevel@tonic-gate 		DB_MEMORY_LIMIT, DB_STORAGE_LIMIT, DB_INTERNAL_ERROR,
2037c478bd9Sstevel@tonic-gate 		DB_BADDICTIONARY, DB_SYNC_FAILED, DB_LOCK_ERROR};
2047c478bd9Sstevel@tonic-gate typedef enum db_status db_status;
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate enum db_action {DB_LOOKUP, DB_REMOVE, DB_ADD, DB_FIRST, DB_NEXT, DB_ALL,
2077c478bd9Sstevel@tonic-gate 			DB_RESET_NEXT, DB_ADD_NOLOG,
2087c478bd9Sstevel@tonic-gate 			DB_ADD_NOSYNC, DB_REMOVE_NOSYNC };
2097c478bd9Sstevel@tonic-gate typedef enum db_action db_action;
2107c478bd9Sstevel@tonic-gate 
211a506a34cSth #endif /* _DB_HEADERS_H */
212