citrus_mapper.c revision 1.3 1 /* $NetBSD: citrus_mapper.c,v 1.3 2003/07/01 08:34:03 tshiozak Exp $ */
2
3 /*-
4 * Copyright (c)2003 Citrus Project,
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 #if defined(LIBC_SCCS) && !defined(lint)
31 __RCSID("$NetBSD: citrus_mapper.c,v 1.3 2003/07/01 08:34:03 tshiozak Exp $");
32 #endif /* LIBC_SCCS and not lint */
33
34 #include "namespace.h"
35 #include "reentrant.h"
36 #include <assert.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <errno.h>
41 #include <limits.h>
42 #include <sys/types.h>
43 #include <sys/stat.h>
44 #include <sys/queue.h>
45
46 #include "citrus_namespace.h"
47 #include "citrus_types.h"
48 #include "citrus_region.h"
49 #include "citrus_memstream.h"
50 #include "citrus_bcs.h"
51 #include "citrus_mmap.h"
52 #include "citrus_module.h"
53 #include "citrus_hash.h"
54 #include "citrus_mapper.h"
55
56 #define _CITRUS_MAPPER_DIR "mapper.dir"
57
58 #define CM_HASH_SIZE 101
59 #define REFCOUNT_PERSISTENT -1
60
61 #ifdef _REENTRANT
62 static rwlock_t lock = RWLOCK_INITIALIZER;
63 #endif
64
65 struct _citrus_mapper_area {
66 _CITRUS_HASH_HEAD(, _citrus_mapper, CM_HASH_SIZE) ma_cache;
67 char *ma_dir;
68 };
69
70 /*
71 * _citrus_mapper_create_area:
72 * create mapper area
73 */
74
75 int
76 _citrus_mapper_create_area(
77 struct _citrus_mapper_area *__restrict *__restrict rma,
78 const char *__restrict area)
79 {
80 struct stat st;
81 int ret;
82 char path[PATH_MAX];
83 struct _citrus_mapper_area *ma;
84
85 rwlock_wrlock(&lock);
86
87 if (*rma != NULL) {
88 ret = 0;
89 goto quit;
90 }
91
92 snprintf(path, PATH_MAX, "%s/%s", area, _CITRUS_MAPPER_DIR);
93
94 ret = stat(path, &st);
95 if (ret)
96 goto quit;
97
98 ma = malloc(sizeof(*ma));
99 if (ma == NULL) {
100 ret = errno;
101 goto quit;
102 }
103 ma->ma_dir = strdup(area);
104 if (ma->ma_dir == NULL) {
105 ret = errno;
106 free(ma->ma_dir);
107 goto quit;
108 }
109 _CITRUS_HASH_INIT(&ma->ma_cache, CM_HASH_SIZE);
110
111 *rma = ma;
112 ret = 0;
113 quit:
114 rwlock_unlock(&lock);
115
116 return ret;
117 }
118
119
120 /*
121 * lookup_mapper_entry:
122 * lookup mapper.dir entry in the specified directory.
123 *
124 * line format of iconv.dir file:
125 * mapper module arg
126 * mapper : mapper name.
127 * module : mapper module name.
128 * arg : argument for the module (generally, description file name)
129 */
130
131 static int
132 lookup_mapper_entry(const char *dir, const char *mapname,
133 void *linebuf, size_t linebufsize,
134 const char **module, const char **variable)
135 {
136 struct _region r;
137 struct _memstream ms;
138 int ret;
139 const char *cp, *cq;
140 char *p;
141 size_t len;
142 char path[PATH_MAX];
143
144 /* create mapper.dir path */
145 snprintf(path, PATH_MAX, "%s/%s", dir, _CITRUS_MAPPER_DIR);
146
147 /* open read stream */
148 ret = _map_file(&r, path);
149 if (ret)
150 return ret;
151
152 _memstream_bind(&ms, &r);
153
154 /* search the line matching to the map name */
155 cp = _memstream_matchline(&ms, mapname, &len, 0);
156 if (!cp) {
157 ret = ENOENT;
158 goto quit;
159 }
160 if (!len || len>linebufsize-1) {
161 ret = EINVAL;
162 goto quit;
163 }
164
165 p = linebuf;
166 /* get module name */
167 *module = p;
168 cq = _bcs_skip_nonws_len(cp, &len);
169 strlcpy(p, cp, (size_t)(cq-cp+1));
170 p += cq-cp+1;
171
172 /* get variable */
173 *variable = p;
174 cp = _bcs_skip_ws_len(cq, &len);
175 strlcpy(p, cp, len+1);
176
177 ret = 0;
178
179 quit:
180 _unmap_file(&r);
181 return ret;
182 }
183
184 /*
185 * mapper_open:
186 */
187 static int
188 mapper_open(struct _citrus_mapper_area *__restrict ma,
189 struct _citrus_mapper * __restrict * __restrict rcm,
190 const char * __restrict module,
191 const char * __restrict variable)
192 {
193 int ret;
194 struct _citrus_mapper *cm;
195 _citrus_mapper_getops_t getops;
196
197 /* initialize mapper handle */
198 cm = malloc(sizeof(*cm));
199 if (!cm)
200 return errno;
201
202 cm->cm_module = NULL;
203 cm->cm_ops = NULL;
204 cm->cm_closure = NULL;
205 cm->cm_traits = NULL;
206 cm->cm_refcount = 0;
207 cm->cm_key = NULL;
208
209 /* load module */
210 ret = _citrus_load_module(&cm->cm_module, module);
211 if (ret)
212 goto err;
213
214 /* get operators */
215 getops = (_citrus_mapper_getops_t)
216 _citrus_find_getops(cm->cm_module, module, "mapper");
217 if (!getops) {
218 ret = EOPNOTSUPP;
219 goto err;
220 }
221 cm->cm_ops = malloc(sizeof(*cm->cm_ops));
222 if (!cm->cm_ops) {
223 ret = errno;
224 goto err;
225 }
226 ret = (*getops)(cm->cm_ops, sizeof(*cm->cm_ops),
227 _CITRUS_MAPPER_ABI_VERSION);
228 if (ret)
229 goto err;
230
231 if (!cm->cm_ops->mo_init ||
232 !cm->cm_ops->mo_uninit ||
233 !cm->cm_ops->mo_convert ||
234 !cm->cm_ops->mo_init_state)
235 goto err;
236
237 /* allocate traits structure */
238 cm->cm_traits = malloc(sizeof(*cm->cm_traits));
239 if (cm->cm_traits == NULL) {
240 ret = errno;
241 goto err;
242 }
243 /* initialize the mapper */
244 ret = (*cm->cm_ops->mo_init)(ma, cm, ma->ma_dir,
245 (const void *)variable,
246 strlen(variable)+1,
247 cm->cm_traits, sizeof(*cm->cm_traits));
248 if (ret)
249 goto err;
250
251 *rcm = cm;
252
253 return 0;
254 err:
255 _citrus_mapper_close(cm);
256 return ret;
257 }
258
259 /*
260 * _citrus_mapper_open_direct:
261 * open a mapper.
262 */
263 int
264 _citrus_mapper_open_direct(struct _citrus_mapper_area *__restrict ma,
265 struct _citrus_mapper * __restrict * __restrict rcm,
266 const char * __restrict module,
267 const char * __restrict variable)
268 {
269 return mapper_open(ma, rcm, module, variable);
270 }
271
272 /*
273 * hash_func
274 */
275 static __inline int
276 hash_func(const char *key)
277 {
278 return _string_hash_func(key, CM_HASH_SIZE);
279 }
280
281 /*
282 * match_func
283 */
284 static __inline int
285 match_func(struct _citrus_mapper *cm, const char *key)
286 {
287 return strcmp(cm->cm_key, key);
288 }
289
290 /*
291 * _citrus_mapper_open:
292 * open a mapper with looking up "mapper.dir".
293 */
294 int
295 _citrus_mapper_open(struct _citrus_mapper_area *__restrict ma,
296 struct _citrus_mapper * __restrict * __restrict rcm,
297 const char * __restrict mapname)
298 {
299 int ret;
300 char linebuf[PATH_MAX];
301 const char *module, *variable;
302 struct _citrus_mapper *cm;
303 int hashval;
304
305 rwlock_wrlock(&lock);
306
307 /* search in the cache */
308 hashval = hash_func(mapname);
309 _CITRUS_HASH_SEARCH(&ma->ma_cache, cm, cm_entry, match_func, mapname,
310 hashval);
311 if (cm) {
312 /* found */
313 cm->cm_refcount++;
314 *rcm = cm;
315 ret = 0;
316 goto quit;
317 }
318
319 /* search mapper entry */
320 ret = lookup_mapper_entry(ma->ma_dir, mapname, linebuf, PATH_MAX,
321 &module, &variable);
322 if (ret)
323 goto quit;
324
325 /* open mapper */
326 ret = mapper_open(ma, &cm, module, variable);
327 if (ret)
328 goto quit;
329 cm->cm_key = strdup(mapname);
330 if (cm->cm_key == NULL) {
331 ret = errno;
332 rwlock_unlock(&lock);
333 _mapper_close(cm);
334 return ret;
335 }
336
337 /* insert to the cache */
338 cm->cm_refcount = 1;
339 _CITRUS_HASH_INSERT(&ma->ma_cache, cm, cm_entry, hashval);
340
341 *rcm = cm;
342 ret = 0;
343 quit:
344 rwlock_unlock(&lock);
345 return ret;
346 }
347
348 /*
349 * _citrus_mapper_close:
350 * close the specified mapper.
351 */
352 void
353 _citrus_mapper_close(struct _citrus_mapper *cm)
354 {
355 if (cm) {
356 rwlock_wrlock(&lock);
357 if (cm->cm_refcount == REFCOUNT_PERSISTENT) {
358 rwlock_unlock(&lock);
359 return;
360 }
361 if (cm->cm_refcount > 0) {
362 if (--cm->cm_refcount > 0) {
363 rwlock_unlock(&lock);
364 return;
365 } else {
366 _CITRUS_HASH_REMOVE(cm, cm_entry);
367 }
368 }
369 if (cm->cm_module) {
370 if (cm->cm_ops) {
371 if (cm->cm_closure)
372 (*cm->cm_ops->mo_uninit)(cm);
373 free(cm->cm_ops);
374 }
375 _citrus_unload_module(cm->cm_module);
376 }
377 free(cm->cm_key);
378 free(cm->cm_traits);
379 free(cm);
380 rwlock_unlock(&lock);
381 }
382 }
383
384 /*
385 * _citrus_mapper_set_persistent:
386 * set persistent count.
387 */
388 void
389 _citrus_mapper_set_persistent(struct _citrus_mapper * __restrict cm)
390 {
391 rwlock_wrlock(&lock);
392 cm->cm_refcount = REFCOUNT_PERSISTENT;
393 rwlock_unlock(&lock);
394 }
395