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