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