xref: /illumos-gate/usr/src/lib/libnisdb/nisdb_mt.cc (revision 2aeafac3612e19716bf8164f89c3c9196342979c)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include "nisdb_mt.h"
30 #include "nisdb_rw.h"
31 
32 #include "db_headers.h"
33 #include "db_entry.h"
34 #include "db.h"
35 #include "db_dictionary.h"
36 
37 
38 static nisdb_tsd_t	nisdb_shared_tsd;
39 static pthread_key_t	nisdb_tsd_key;
40 
41 void
42 __nisdb_tsd_destroy(void *key) {
43 
44 	nisdb_tsd_t	*tsd = (nisdb_tsd_t *)key;
45 
46 	if (tsd != 0) {
47 		free(tsd);
48 	}
49 }
50 
51 extern "C" {
52 static void
53 __nisdb_init_tsd_key(void)
54 {
55 	(void) pthread_key_create(&nisdb_tsd_key, __nisdb_tsd_destroy);
56 }
57 #pragma init(__nisdb_init_tsd_key)
58 }
59 
60 nisdb_tsd_t *
61 __nisdb_get_tsd(void) {
62 
63 	nisdb_tsd_t	*tsd;
64 
65 	if ((tsd = (nisdb_tsd_t *)pthread_getspecific(nisdb_tsd_key)) == 0) {
66 		/* No TSD; create it */
67 		if ((tsd = (nisdb_tsd_t *)malloc(sizeof (*tsd))) != 0) {
68 			/* Initialize TSD */
69 			memset(tsd, 0, sizeof (*tsd));
70 			/* Register TSD */
71 			if (pthread_setspecific(nisdb_tsd_key, tsd) != 0) {
72 				/* Can't store key */
73 #ifdef	NISDB_MT_DEBUG
74 				abort();
75 #endif
76 				free(tsd);
77 				tsd = &nisdb_shared_tsd;
78 			}
79 		} else {
80 			/* No memory ? */
81 #ifdef	NISDB_MT_DEBUG
82 			abort();
83 #endif
84 			tsd = &nisdb_shared_tsd;
85 		}
86 	}
87 
88 	return (tsd);
89 }
90 
91 void
92 setMappingStatus(int nisPlusStat, int ldapStat) {
93 	nisdb_tsd_t	*tsd = __nisdb_get_tsd();
94 
95 	if (tsd != 0) {
96 		tsd->nisPlusStat = nisPlusStat;
97 		tsd->ldapStat = ldapStat;
98 	}
99 }
100 
101 /*
102  * Save a copy of 'obj' in the TSD. If the TSD already holds an old object,
103  * delete it before saving the new one.
104  *
105  * On successful exit, '*storedP' indicates whether or not the entry was
106  * stored, and hence whether or not we're perfroming a modify operation.
107  *
108  * Return 1 if successful, 0 otherwise.
109  */
110 int
111 saveOldObjForModify(entry_obj *obj, int *storedP) {
112 	nisdb_tsd_t	*tsd = __nisdb_get_tsd();
113 	int		stored;
114 
115 	if (tsd == 0)
116 		return (0);
117 
118 	if ((stored = tsd->doingModify) != 0) {
119 		entry_object	*eObj = tsd->oldObj;
120 
121 		if (eObj != 0) {
122 			free_entry(eObj);
123 			tsd->oldObj = 0;
124 		}
125 
126 		if (obj != 0) {
127 			eObj = new_entry((entry_object *)obj);
128 			if (eObj == 0)
129 				return (0);
130 		} else {
131 			eObj = 0;
132 		}
133 
134 		tsd->oldObj = (entry_obj *)eObj;
135 	}
136 
137 	if (storedP != 0)
138 		*storedP = stored;
139 
140 	return (1);
141 }
142 
143 /*
144  * Retrieve (and remove) the old object (if any) from the TSD. Returns 1
145  * if successful ('*oldObjP' might be NULL), 0 otherwise ('*oldObjP'
146  * unchanged).
147  */
148 int
149 retrieveOldObjForModify(entry_obj **oldObjP) {
150 	nisdb_tsd_t	*tsd = __nisdb_get_tsd();
151 
152 	if (tsd == 0 || oldObjP == 0)
153 		return (0);
154 
155 	if (tsd->doingModify) {
156 		*oldObjP = tsd->oldObj;
157 		tsd->oldObj = 0;
158 	} else {
159 		*oldObjP = 0;
160 	}
161 
162 	return (1);
163 }
164