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