1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <stdlib.h>
#include <stdio.h>
#include "nscd_db.h"
#include "nscd_log.h"
static rwlock_t addrDB_rwlock = DEFAULTRWLOCK;
static nscd_db_t *addrDB = NULL;
/*
* internal structure representing a nscd internal address
*/
typedef struct nscd_int_addr {
int to_delete; /* no longer valid */
int type;
void *ptr;
nscd_seq_num_t seq_num;
rwlock_t rwlock; /* used to serialize get and destroy */
} nscd_int_addr_t;
/*
* FUNCTION: _nscd_create_int_addrDB
*
* Create the internal address database to keep track of the
* memory allocated by _nscd_alloc.
*/
void *
_nscd_create_int_addrDB()
{
nscd_db_t *ret;
char *me = "_nscd_create_int_addrDB";
_NSCD_LOG(NSCD_LOG_INT_ADDR | NSCD_LOG_CONFIG, NSCD_LOG_LEVEL_DEBUG)
(me, "initializing the internal address database\n");
(void) rw_wrlock(&addrDB_rwlock);
if (addrDB != NULL) {
(void) rw_unlock(&addrDB_rwlock);
return (addrDB);
}
ret = _nscd_alloc_db(NSCD_DB_SIZE_LARGE);
if (ret != NULL)
addrDB = ret;
(void) rw_unlock(&addrDB_rwlock);
return (ret);
}
/*
* FUNCTION: _nscd_add_int_addr
*
* Add an address of 'type' to the internal address database.
*/
nscd_rc_t
_nscd_add_int_addr(
void *ptr,
int type,
nscd_seq_num_t seq_num)
{
int size;
char buf[2 * sizeof (ptr) + 1];
nscd_db_entry_t *db_entry;
nscd_int_addr_t *int_addr;
if (ptr == NULL)
return (NSCD_INVALID_ARGUMENT);
(void) snprintf(buf, sizeof (buf), "%p", ptr);
size = sizeof (*int_addr);
db_entry = _nscd_alloc_db_entry(NSCD_DATA_ADDR,
(const char *)buf, size, 1, 1);
if (db_entry == NULL)
return (NSCD_NO_MEMORY);
int_addr = (nscd_int_addr_t *)*(db_entry->data_array);
int_addr->ptr = ptr;
int_addr->type = type;
int_addr->seq_num = seq_num;
(void) rwlock_init(&int_addr->rwlock, USYNC_THREAD, NULL);
(void) rw_wrlock(&addrDB_rwlock);
(void) _nscd_add_db_entry(addrDB, buf, db_entry,
NSCD_ADD_DB_ENTRY_FIRST);
(void) rw_unlock(&addrDB_rwlock);
return (NSCD_SUCCESS);
}
/*
* FUNCTION: _nscd_is_int_addr
*
* Check to see if an address can be found in the internal
* address database, if so, obtain a reader lock on the
* associated rw_lock. The caller needs to unlock it when
* done using the data.
*/
rwlock_t *
_nscd_is_int_addr(
void *ptr,
nscd_seq_num_t seq_num)
{
char *me = "_nscd_is_int_addr";
char ptrstr[1 + 2 * sizeof (ptr)];
rwlock_t *addr_rwlock;
const nscd_db_entry_t *db_entry;
if (ptr == NULL)
return (NULL);
(void) snprintf(ptrstr, sizeof (ptrstr), "%p", ptr);
(void) rw_rdlock(&addrDB_rwlock);
db_entry = _nscd_get_db_entry(addrDB, NSCD_DATA_ADDR,
(const char *)ptrstr, NSCD_GET_FIRST_DB_ENTRY, 0);
if (db_entry != NULL) {
nscd_int_addr_t *int_addr;
int_addr = (nscd_int_addr_t *)*(db_entry->data_array);
addr_rwlock = &int_addr->rwlock;
(void) rw_rdlock(addr_rwlock);
/*
* If the data is marked as to be deleted
* or the sequence number does not match,
* return NULL.
*/
if (int_addr->to_delete == 1 ||
int_addr->seq_num != seq_num) {
(void) rw_unlock(addr_rwlock);
addr_rwlock = NULL;
}
_NSCD_LOG(NSCD_LOG_INT_ADDR, NSCD_LOG_LEVEL_DEBUG)
(me, "found %p, seq# = %lld\n", ptr, int_addr->seq_num);
} else
addr_rwlock = NULL;
(void) rw_unlock(&addrDB_rwlock);
return (addr_rwlock);
}
/*
* FUNCTION: _nscd_del_int_addr
*
* Delete an address from the internal address database.
*/
void
_nscd_del_int_addr(
void *ptr,
nscd_seq_num_t seq_num)
{
char *me = "_nscd_del_int_addr";
char ptrstr[1 + 2 * sizeof (ptr)];
rwlock_t *addr_rwlock;
nscd_int_addr_t *int_addr;
const nscd_db_entry_t *db_entry;
if (ptr == NULL)
return;
_NSCD_LOG(NSCD_LOG_INT_ADDR, NSCD_LOG_LEVEL_DEBUG)
(me, "deleting int addr %p (%d)\n", ptr, seq_num);
(void) snprintf(ptrstr, sizeof (ptrstr), "%p", ptr);
(void) rw_rdlock(&addrDB_rwlock);
/*
* first find the db entry and make sure that
* no one is currently locking it. i.e.,
* no one is waiting to use the same address.
* If it is locked, rw_wrlock() will not return
* until it is unlocked.
*/
db_entry = _nscd_get_db_entry(addrDB,
NSCD_DATA_ADDR,
(const char *)ptrstr,
NSCD_GET_FIRST_DB_ENTRY, 0);
if (db_entry != NULL) {
int_addr = (nscd_int_addr_t *)*(db_entry->data_array);
addr_rwlock = &int_addr->rwlock;
(void) rw_wrlock(addr_rwlock);
} else {
(void) rw_unlock(&addrDB_rwlock);
return;
}
(void) rw_unlock(&addrDB_rwlock);
/*
* delete the db entry if the sequence numbers match
*/
if (int_addr->seq_num == seq_num) {
(void) rw_wrlock(&addrDB_rwlock);
(void) _nscd_delete_db_entry(addrDB,
NSCD_DATA_ADDR,
(const char *)ptrstr,
NSCD_DEL_FIRST_DB_ENTRY, 0);
(void) rw_unlock(&addrDB_rwlock);
}
}
/*
* FUNCTION: _nscd_destroy_int_addrDB
*
* Destroy the internal address database.
*/
void
_nscd_destroy_int_addrDB()
{
(void) rw_wrlock(&addrDB_rwlock);
_nscd_free_db(addrDB);
addrDB = NULL;
(void) rw_unlock(&addrDB_rwlock);
}
|