xref: /titanic_52/usr/src/lib/libnisdb/nisdb_mt.cc (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include "nisdb_mt.h"
30*7c478bd9Sstevel@tonic-gate #include "nisdb_rw.h"
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate #include "db_headers.h"
33*7c478bd9Sstevel@tonic-gate #include "db_entry.h"
34*7c478bd9Sstevel@tonic-gate #include "db.h"
35*7c478bd9Sstevel@tonic-gate #include "db_dictionary.h"
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate static nisdb_tsd_t	nisdb_shared_tsd;
39*7c478bd9Sstevel@tonic-gate static pthread_key_t	nisdb_tsd_key;
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate void
42*7c478bd9Sstevel@tonic-gate __nisdb_tsd_destroy(void *key) {
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate 	nisdb_tsd_t	*tsd = (nisdb_tsd_t *)key;
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate 	if (tsd != 0) {
47*7c478bd9Sstevel@tonic-gate 		free(tsd);
48*7c478bd9Sstevel@tonic-gate 	}
49*7c478bd9Sstevel@tonic-gate }
50*7c478bd9Sstevel@tonic-gate 
51*7c478bd9Sstevel@tonic-gate extern "C" {
52*7c478bd9Sstevel@tonic-gate static void
53*7c478bd9Sstevel@tonic-gate __nisdb_init_tsd_key(void)
54*7c478bd9Sstevel@tonic-gate {
55*7c478bd9Sstevel@tonic-gate 	(void) pthread_key_create(&nisdb_tsd_key, __nisdb_tsd_destroy);
56*7c478bd9Sstevel@tonic-gate }
57*7c478bd9Sstevel@tonic-gate #pragma init(__nisdb_init_tsd_key)
58*7c478bd9Sstevel@tonic-gate }
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate nisdb_tsd_t *
61*7c478bd9Sstevel@tonic-gate __nisdb_get_tsd(void) {
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate 	nisdb_tsd_t	*tsd;
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate 	if ((tsd = (nisdb_tsd_t *)pthread_getspecific(nisdb_tsd_key)) == 0) {
66*7c478bd9Sstevel@tonic-gate 		/* No TSD; create it */
67*7c478bd9Sstevel@tonic-gate 		if ((tsd = (nisdb_tsd_t *)malloc(sizeof (*tsd))) != 0) {
68*7c478bd9Sstevel@tonic-gate 			/* Initialize TSD */
69*7c478bd9Sstevel@tonic-gate 			memset(tsd, 0, sizeof (*tsd));
70*7c478bd9Sstevel@tonic-gate 			/* Register TSD */
71*7c478bd9Sstevel@tonic-gate 			if (pthread_setspecific(nisdb_tsd_key, tsd) != 0) {
72*7c478bd9Sstevel@tonic-gate 				/* Can't store key */
73*7c478bd9Sstevel@tonic-gate #ifdef	NISDB_MT_DEBUG
74*7c478bd9Sstevel@tonic-gate 				abort();
75*7c478bd9Sstevel@tonic-gate #endif
76*7c478bd9Sstevel@tonic-gate 				free(tsd);
77*7c478bd9Sstevel@tonic-gate 				tsd = &nisdb_shared_tsd;
78*7c478bd9Sstevel@tonic-gate 			}
79*7c478bd9Sstevel@tonic-gate 		} else {
80*7c478bd9Sstevel@tonic-gate 			/* No memory ? */
81*7c478bd9Sstevel@tonic-gate #ifdef	NISDB_MT_DEBUG
82*7c478bd9Sstevel@tonic-gate 			abort();
83*7c478bd9Sstevel@tonic-gate #endif
84*7c478bd9Sstevel@tonic-gate 			tsd = &nisdb_shared_tsd;
85*7c478bd9Sstevel@tonic-gate 		}
86*7c478bd9Sstevel@tonic-gate 	}
87*7c478bd9Sstevel@tonic-gate 
88*7c478bd9Sstevel@tonic-gate 	return (tsd);
89*7c478bd9Sstevel@tonic-gate }
90*7c478bd9Sstevel@tonic-gate 
91*7c478bd9Sstevel@tonic-gate void
92*7c478bd9Sstevel@tonic-gate setMappingStatus(int nisPlusStat, int ldapStat) {
93*7c478bd9Sstevel@tonic-gate 	nisdb_tsd_t	*tsd = __nisdb_get_tsd();
94*7c478bd9Sstevel@tonic-gate 
95*7c478bd9Sstevel@tonic-gate 	if (tsd != 0) {
96*7c478bd9Sstevel@tonic-gate 		tsd->nisPlusStat = nisPlusStat;
97*7c478bd9Sstevel@tonic-gate 		tsd->ldapStat = ldapStat;
98*7c478bd9Sstevel@tonic-gate 	}
99*7c478bd9Sstevel@tonic-gate }
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate /*
102*7c478bd9Sstevel@tonic-gate  * Save a copy of 'obj' in the TSD. If the TSD already holds an old object,
103*7c478bd9Sstevel@tonic-gate  * delete it before saving the new one.
104*7c478bd9Sstevel@tonic-gate  *
105*7c478bd9Sstevel@tonic-gate  * On successful exit, '*storedP' indicates whether or not the entry was
106*7c478bd9Sstevel@tonic-gate  * stored, and hence whether or not we're perfroming a modify operation.
107*7c478bd9Sstevel@tonic-gate  *
108*7c478bd9Sstevel@tonic-gate  * Return 1 if successful, 0 otherwise.
109*7c478bd9Sstevel@tonic-gate  */
110*7c478bd9Sstevel@tonic-gate int
111*7c478bd9Sstevel@tonic-gate saveOldObjForModify(entry_obj *obj, int *storedP) {
112*7c478bd9Sstevel@tonic-gate 	nisdb_tsd_t	*tsd = __nisdb_get_tsd();
113*7c478bd9Sstevel@tonic-gate 	int		stored;
114*7c478bd9Sstevel@tonic-gate 
115*7c478bd9Sstevel@tonic-gate 	if (tsd == 0)
116*7c478bd9Sstevel@tonic-gate 		return (0);
117*7c478bd9Sstevel@tonic-gate 
118*7c478bd9Sstevel@tonic-gate 	if ((stored = tsd->doingModify) != 0) {
119*7c478bd9Sstevel@tonic-gate 		entry_object	*eObj = tsd->oldObj;
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate 		if (eObj != 0) {
122*7c478bd9Sstevel@tonic-gate 			free_entry(eObj);
123*7c478bd9Sstevel@tonic-gate 			tsd->oldObj = 0;
124*7c478bd9Sstevel@tonic-gate 		}
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate 		if (obj != 0) {
127*7c478bd9Sstevel@tonic-gate 			eObj = new_entry((entry_object *)obj);
128*7c478bd9Sstevel@tonic-gate 			if (eObj == 0)
129*7c478bd9Sstevel@tonic-gate 				return (0);
130*7c478bd9Sstevel@tonic-gate 		} else {
131*7c478bd9Sstevel@tonic-gate 			eObj = 0;
132*7c478bd9Sstevel@tonic-gate 		}
133*7c478bd9Sstevel@tonic-gate 
134*7c478bd9Sstevel@tonic-gate 		tsd->oldObj = (entry_obj *)eObj;
135*7c478bd9Sstevel@tonic-gate 	}
136*7c478bd9Sstevel@tonic-gate 
137*7c478bd9Sstevel@tonic-gate 	if (storedP != 0)
138*7c478bd9Sstevel@tonic-gate 		*storedP = stored;
139*7c478bd9Sstevel@tonic-gate 
140*7c478bd9Sstevel@tonic-gate 	return (1);
141*7c478bd9Sstevel@tonic-gate }
142*7c478bd9Sstevel@tonic-gate 
143*7c478bd9Sstevel@tonic-gate /*
144*7c478bd9Sstevel@tonic-gate  * Retrieve (and remove) the old object (if any) from the TSD. Returns 1
145*7c478bd9Sstevel@tonic-gate  * if successful ('*oldObjP' might be NULL), 0 otherwise ('*oldObjP'
146*7c478bd9Sstevel@tonic-gate  * unchanged).
147*7c478bd9Sstevel@tonic-gate  */
148*7c478bd9Sstevel@tonic-gate int
149*7c478bd9Sstevel@tonic-gate retrieveOldObjForModify(entry_obj **oldObjP) {
150*7c478bd9Sstevel@tonic-gate 	nisdb_tsd_t	*tsd = __nisdb_get_tsd();
151*7c478bd9Sstevel@tonic-gate 
152*7c478bd9Sstevel@tonic-gate 	if (tsd == 0 || oldObjP == 0)
153*7c478bd9Sstevel@tonic-gate 		return (0);
154*7c478bd9Sstevel@tonic-gate 
155*7c478bd9Sstevel@tonic-gate 	if (tsd->doingModify) {
156*7c478bd9Sstevel@tonic-gate 		*oldObjP = tsd->oldObj;
157*7c478bd9Sstevel@tonic-gate 		tsd->oldObj = 0;
158*7c478bd9Sstevel@tonic-gate 	} else {
159*7c478bd9Sstevel@tonic-gate 		*oldObjP = 0;
160*7c478bd9Sstevel@tonic-gate 	}
161*7c478bd9Sstevel@tonic-gate 
162*7c478bd9Sstevel@tonic-gate 	return (1);
163*7c478bd9Sstevel@tonic-gate }
164