xref: /illumos-gate/usr/src/lib/libnisdb/db_headers.h (revision 7c478bd9)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  *	db_headers.h
24*7c478bd9Sstevel@tonic-gate  *
25*7c478bd9Sstevel@tonic-gate  *	Copyright (c) 1988-2001 by Sun Microsystems, Inc.
26*7c478bd9Sstevel@tonic-gate  *	All Rights Reserved.
27*7c478bd9Sstevel@tonic-gate  */
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #ifndef _DB_HEADERS_H
32*7c478bd9Sstevel@tonic-gate #define	_DB_HEADERS_H
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
35*7c478bd9Sstevel@tonic-gate #include <syslog.h>
36*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
37*7c478bd9Sstevel@tonic-gate #include <setjmp.h>
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate extern int verbose;
40*7c478bd9Sstevel@tonic-gate extern jmp_buf dbenv;
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate #define	FATAL(msg, fcode) \
43*7c478bd9Sstevel@tonic-gate 	{ \
44*7c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "ERROR: %s", (msg)); \
45*7c478bd9Sstevel@tonic-gate 		__nisdb_get_tsd()->fatalcode = (int)(fcode); \
46*7c478bd9Sstevel@tonic-gate 		__nisdb_get_tsd()->fatalmsg = msg; \
47*7c478bd9Sstevel@tonic-gate 		return; \
48*7c478bd9Sstevel@tonic-gate 	}
49*7c478bd9Sstevel@tonic-gate #define	FATAL3(msg, fcode, retval) \
50*7c478bd9Sstevel@tonic-gate 	{ \
51*7c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "ERROR: %s", (msg)); \
52*7c478bd9Sstevel@tonic-gate 		__nisdb_get_tsd()->fatalcode = (int)(fcode); \
53*7c478bd9Sstevel@tonic-gate 		__nisdb_get_tsd()->fatalmsg = msg; \
54*7c478bd9Sstevel@tonic-gate 		return (retval); \
55*7c478bd9Sstevel@tonic-gate 	}
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate #ifdef	NISDB_MT_DEBUG
58*7c478bd9Sstevel@tonic-gate #define	LOCKVAL(lockcall, msg, lockcode) \
59*7c478bd9Sstevel@tonic-gate 	{ \
60*7c478bd9Sstevel@tonic-gate 		lockcode = lockcall(); \
61*7c478bd9Sstevel@tonic-gate 		if (lockcode != 0) { \
62*7c478bd9Sstevel@tonic-gate 			__nisdb_get_tsd()->fatalcode = lockcode; \
63*7c478bd9Sstevel@tonic-gate 			__nisdb_get_tsd()->fatalmsg = msg; \
64*7c478bd9Sstevel@tonic-gate 			abort(); \
65*7c478bd9Sstevel@tonic-gate 		} \
66*7c478bd9Sstevel@tonic-gate 	}
67*7c478bd9Sstevel@tonic-gate #else
68*7c478bd9Sstevel@tonic-gate #define	LOCKVAL(lockcall, msg, lockcode) \
69*7c478bd9Sstevel@tonic-gate 	{ \
70*7c478bd9Sstevel@tonic-gate 		lockcode = lockcall(); \
71*7c478bd9Sstevel@tonic-gate 		if (lockcode != 0) { \
72*7c478bd9Sstevel@tonic-gate 			__nisdb_get_tsd()->fatalcode = lockcode; \
73*7c478bd9Sstevel@tonic-gate 			__nisdb_get_tsd()->fatalmsg = msg; \
74*7c478bd9Sstevel@tonic-gate 		} \
75*7c478bd9Sstevel@tonic-gate 	}
76*7c478bd9Sstevel@tonic-gate #endif	/* NISDB_MT_DEBUG */
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate #define	LOCKV(lockcall, msg) \
79*7c478bd9Sstevel@tonic-gate 	{ \
80*7c478bd9Sstevel@tonic-gate 		int	lockcode; \
81*7c478bd9Sstevel@tonic-gate 		LOCKVAL(lockcall, msg, lockcode); \
82*7c478bd9Sstevel@tonic-gate 		if (lockcode != 0) \
83*7c478bd9Sstevel@tonic-gate 			return; \
84*7c478bd9Sstevel@tonic-gate 	}
85*7c478bd9Sstevel@tonic-gate #define	LOCK(lockcall, retval, msg) \
86*7c478bd9Sstevel@tonic-gate 	{ \
87*7c478bd9Sstevel@tonic-gate 		int	lockcode; \
88*7c478bd9Sstevel@tonic-gate 		LOCKVAL(lockcall, msg, lockcode); \
89*7c478bd9Sstevel@tonic-gate 		if (lockcode != 0) \
90*7c478bd9Sstevel@tonic-gate 			return (retval); \
91*7c478bd9Sstevel@tonic-gate 	}
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate /* Read lock/unlock 'this', return 'retval' is unsuccessful, and save 'msg' */
94*7c478bd9Sstevel@tonic-gate #define	READLOCK(this, retval, msg) \
95*7c478bd9Sstevel@tonic-gate 	LOCK(this->acqnonexcl, retval, msg)
96*7c478bd9Sstevel@tonic-gate #define	READUNLOCK(this, retval, msg) \
97*7c478bd9Sstevel@tonic-gate 	LOCK(this->relnonexcl, retval, msg)
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate /* Ditto, but return without a value (i.e., a "void" function */
100*7c478bd9Sstevel@tonic-gate #define	READLOCKV(this, msg) \
101*7c478bd9Sstevel@tonic-gate 	LOCKV(this->acqnonexcl, msg)
102*7c478bd9Sstevel@tonic-gate #define	READUNLOCKV(this, msg) \
103*7c478bd9Sstevel@tonic-gate 	LOCKV(this->relnonexcl, msg)
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate /* As READLOCK/READUNLOCK, but set rescode instead of returning on failure */
106*7c478bd9Sstevel@tonic-gate #define	READLOCKNR(this, rescode, msg) \
107*7c478bd9Sstevel@tonic-gate 	LOCKVAL(this->acqnonexcl, msg, rescode)
108*7c478bd9Sstevel@tonic-gate #define	READUNLOCKNR(this, rescode, msg) \
109*7c478bd9Sstevel@tonic-gate 	LOCKVAL(this->relnonexcl, msg, rescode)
110*7c478bd9Sstevel@tonic-gate 
111*7c478bd9Sstevel@tonic-gate /* As READLOCK/READUNLOCK, but use a write lock */
112*7c478bd9Sstevel@tonic-gate #define	WRITELOCK(this, retval, msg) \
113*7c478bd9Sstevel@tonic-gate 	LOCK(this->acqexcl, retval, msg)
114*7c478bd9Sstevel@tonic-gate #define	WRITEUNLOCK(this, retval, msg) \
115*7c478bd9Sstevel@tonic-gate 	LOCK(this->relexcl, retval, msg)
116*7c478bd9Sstevel@tonic-gate 
117*7c478bd9Sstevel@tonic-gate /* Non-blocking write lock */
118*7c478bd9Sstevel@tonic-gate #define	TRYWRITELOCK(this, rescode, msg) \
119*7c478bd9Sstevel@tonic-gate 	LOCKVAL(this->tryacqexcl, msg, rescode)
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate /* Ditto, but return without a value */
122*7c478bd9Sstevel@tonic-gate #define	WRITELOCKV(this, msg) \
123*7c478bd9Sstevel@tonic-gate 	LOCKV(this->acqexcl, msg)
124*7c478bd9Sstevel@tonic-gate #define	WRITEUNLOCKV(this, msg) \
125*7c478bd9Sstevel@tonic-gate 	LOCKV(this->relexcl, msg)
126*7c478bd9Sstevel@tonic-gate 
127*7c478bd9Sstevel@tonic-gate /* As WRITELOCK/WRITEUNLOCK, but set rescode instead of returning on failure */
128*7c478bd9Sstevel@tonic-gate #define	WRITELOCKNR(this, rescode, msg) \
129*7c478bd9Sstevel@tonic-gate 	LOCKVAL(this->acqexcl, msg, rescode)
130*7c478bd9Sstevel@tonic-gate #define	WRITEUNLOCKNR(this, rescode, msg) \
131*7c478bd9Sstevel@tonic-gate 	LOCKVAL(this->relexcl, msg, rescode)
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate /* Apply a second write lock when already holding another write lock */
134*7c478bd9Sstevel@tonic-gate #define	WRITELOCK2(this, retval, msg, that) \
135*7c478bd9Sstevel@tonic-gate 	if (this != 0) { \
136*7c478bd9Sstevel@tonic-gate 		int	lockcode1, lockcode2; \
137*7c478bd9Sstevel@tonic-gate 		WRITELOCKNR(this, lockcode2, msg); \
138*7c478bd9Sstevel@tonic-gate 		if (lockcode2 != 0) { \
139*7c478bd9Sstevel@tonic-gate 			if (that != 0) { \
140*7c478bd9Sstevel@tonic-gate 				WRITEUNLOCKNR(that, lockcode1, msg); \
141*7c478bd9Sstevel@tonic-gate 			} \
142*7c478bd9Sstevel@tonic-gate 			return (retval); \
143*7c478bd9Sstevel@tonic-gate 		} \
144*7c478bd9Sstevel@tonic-gate 	}
145*7c478bd9Sstevel@tonic-gate /* Release two write locks */
146*7c478bd9Sstevel@tonic-gate #define	WRITEUNLOCK2(this, that, retval1, retval2, msg1, msg2) \
147*7c478bd9Sstevel@tonic-gate 	{ \
148*7c478bd9Sstevel@tonic-gate 		int	lockcode1 = 0, lockcode2 = 0; \
149*7c478bd9Sstevel@tonic-gate 		if (this != 0) { \
150*7c478bd9Sstevel@tonic-gate 			WRITEUNLOCKNR(this, lockcode1, msg1); \
151*7c478bd9Sstevel@tonic-gate 		} \
152*7c478bd9Sstevel@tonic-gate 		if (that != 0) { \
153*7c478bd9Sstevel@tonic-gate 			WRITEUNLOCKNR(that, lockcode2, msg2); \
154*7c478bd9Sstevel@tonic-gate 		} \
155*7c478bd9Sstevel@tonic-gate 		if (lockcode2 != 0) { \
156*7c478bd9Sstevel@tonic-gate 			return (retval2); \
157*7c478bd9Sstevel@tonic-gate 		} else if (lockcode1 != 0) { \
158*7c478bd9Sstevel@tonic-gate 			return (retval1); \
159*7c478bd9Sstevel@tonic-gate 		} \
160*7c478bd9Sstevel@tonic-gate 	}
161*7c478bd9Sstevel@tonic-gate 
162*7c478bd9Sstevel@tonic-gate /* Apply a second read lock when already holding another read lock */
163*7c478bd9Sstevel@tonic-gate #define	READLOCK2(this, retval, msg, that) \
164*7c478bd9Sstevel@tonic-gate 	if (this != 0) { \
165*7c478bd9Sstevel@tonic-gate 		int	lockcode1, lockcode2; \
166*7c478bd9Sstevel@tonic-gate 		READLOCKNR(this, lockcode2, msg); \
167*7c478bd9Sstevel@tonic-gate 		if (lockcode2 != 0) { \
168*7c478bd9Sstevel@tonic-gate 			if (that != 0) { \
169*7c478bd9Sstevel@tonic-gate 				READUNLOCKNR(that, lockcode1, msg); \
170*7c478bd9Sstevel@tonic-gate 			} \
171*7c478bd9Sstevel@tonic-gate 			return (retval); \
172*7c478bd9Sstevel@tonic-gate 		} \
173*7c478bd9Sstevel@tonic-gate 	}
174*7c478bd9Sstevel@tonic-gate /* Release two read locks */
175*7c478bd9Sstevel@tonic-gate #define	READUNLOCK2(this, that, retval1, retval2, msg1, msg2) \
176*7c478bd9Sstevel@tonic-gate 	{ \
177*7c478bd9Sstevel@tonic-gate 		int	lockcode1 = 0, lockcode2 = 0; \
178*7c478bd9Sstevel@tonic-gate 		if (this != 0) { \
179*7c478bd9Sstevel@tonic-gate 			READUNLOCKNR(this, lockcode1, msg1); \
180*7c478bd9Sstevel@tonic-gate 		} \
181*7c478bd9Sstevel@tonic-gate 		if (that != 0) { \
182*7c478bd9Sstevel@tonic-gate 			READUNLOCKNR(that, lockcode2, msg2); \
183*7c478bd9Sstevel@tonic-gate 		} \
184*7c478bd9Sstevel@tonic-gate 		if (lockcode2 != 0) { \
185*7c478bd9Sstevel@tonic-gate 			return (retval2); \
186*7c478bd9Sstevel@tonic-gate 		} else if (lockcode1 != 0) { \
187*7c478bd9Sstevel@tonic-gate 			return (retval1); \
188*7c478bd9Sstevel@tonic-gate 		} \
189*7c478bd9Sstevel@tonic-gate 	}
190*7c478bd9Sstevel@tonic-gate 
191*7c478bd9Sstevel@tonic-gate #define	ASSERTWRITELOCKHELD(lvar, retval, msg) \
192*7c478bd9Sstevel@tonic-gate 	{ \
193*7c478bd9Sstevel@tonic-gate 		int	lc; \
194*7c478bd9Sstevel@tonic-gate 		if ((lc = __nisdb_assert_wheld(&lvar ## _rwlock)) != 0) { \
195*7c478bd9Sstevel@tonic-gate 			__nisdb_get_tsd()->fatalcode = lc; \
196*7c478bd9Sstevel@tonic-gate 			__nisdb_get_tsd()->fatalmsg = msg; \
197*7c478bd9Sstevel@tonic-gate 			return (retval); \
198*7c478bd9Sstevel@tonic-gate 		} \
199*7c478bd9Sstevel@tonic-gate 	}
200*7c478bd9Sstevel@tonic-gate 
201*7c478bd9Sstevel@tonic-gate #define	WARNING(x) { syslog(LOG_ERR, "WARNING: %s", (x)); }
202*7c478bd9Sstevel@tonic-gate 
203*7c478bd9Sstevel@tonic-gate #define	WARNING_M(x) { syslog(LOG_ERR, "WARNING: %s: %m", (x)); }
204*7c478bd9Sstevel@tonic-gate 
205*7c478bd9Sstevel@tonic-gate 
206*7c478bd9Sstevel@tonic-gate enum db_status {DB_SUCCESS, DB_NOTFOUND, DB_NOTUNIQUE,
207*7c478bd9Sstevel@tonic-gate 		    DB_BADTABLE, DB_BADQUERY, DB_BADOBJECT,
208*7c478bd9Sstevel@tonic-gate 		DB_MEMORY_LIMIT, DB_STORAGE_LIMIT, DB_INTERNAL_ERROR,
209*7c478bd9Sstevel@tonic-gate 		DB_BADDICTIONARY, DB_SYNC_FAILED, DB_LOCK_ERROR};
210*7c478bd9Sstevel@tonic-gate typedef enum db_status db_status;
211*7c478bd9Sstevel@tonic-gate 
212*7c478bd9Sstevel@tonic-gate enum db_action {DB_LOOKUP, DB_REMOVE, DB_ADD, DB_FIRST, DB_NEXT, DB_ALL,
213*7c478bd9Sstevel@tonic-gate 			DB_RESET_NEXT, DB_ADD_NOLOG,
214*7c478bd9Sstevel@tonic-gate 			DB_ADD_NOSYNC, DB_REMOVE_NOSYNC };
215*7c478bd9Sstevel@tonic-gate typedef enum db_action db_action;
216*7c478bd9Sstevel@tonic-gate 
217*7c478bd9Sstevel@tonic-gate #endif _DB_HEADERS_H
218