Home | History | Annotate | Line # | Download | only in citrus
citrus_mapper.c revision 1.7.2.1
      1 /*	$NetBSD: citrus_mapper.c,v 1.7.2.1 2012/04/17 00:05:16 yamt 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.7.2.1 2012/04/17 00:05:16 yamt 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, (size_t)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 		goto quit;
    107 	}
    108 	_CITRUS_HASH_INIT(&ma->ma_cache, CM_HASH_SIZE);
    109 
    110 	*rma = ma;
    111 	ret = 0;
    112 quit:
    113 	rwlock_unlock(&lock);
    114 
    115 	return ret;
    116 }
    117 
    118 
    119 /*
    120  * lookup_mapper_entry:
    121  *	lookup mapper.dir entry in the specified directory.
    122  *
    123  * line format of iconv.dir file:
    124  *	mapper	module	arg
    125  * mapper : mapper name.
    126  * module : mapper module name.
    127  * arg    : argument for the module (generally, description file name)
    128  */
    129 
    130 static int
    131 lookup_mapper_entry(const char *dir, const char *mapname,
    132 		    void *linebuf, size_t linebufsize,
    133 		    const char **module, const char **variable)
    134 {
    135 	struct _region r;
    136 	struct _memstream ms;
    137 	int ret;
    138 	const char *cp, *cq;
    139 	char *p;
    140 	size_t len;
    141 	char path[PATH_MAX];
    142 
    143 	/* create mapper.dir path */
    144 	snprintf(path, (size_t)PATH_MAX, "%s/%s", dir, _CITRUS_MAPPER_DIR);
    145 
    146 	/* open read stream */
    147 	ret = _map_file(&r, path);
    148 	if (ret)
    149 		return ret;
    150 
    151 	_memstream_bind(&ms, &r);
    152 
    153 	/* search the line matching to the map name */
    154 	cp = _memstream_matchline(&ms, mapname, &len, 0);
    155 	if (!cp) {
    156 		ret = ENOENT;
    157 		goto quit;
    158 	}
    159 	if (!len || len>linebufsize-1) {
    160 		ret = EINVAL;
    161 		goto quit;
    162 	}
    163 
    164 	p = linebuf;
    165 	/* get module name */
    166 	*module = p;
    167 	cq = _bcs_skip_nonws_len(cp, &len);
    168 	strlcpy(p, cp, (size_t)(cq-cp+1));
    169 	p += cq-cp+1;
    170 
    171 	/* get variable */
    172 	*variable = p;
    173 	cp = _bcs_skip_ws_len(cq, &len);
    174 	strlcpy(p, cp, len+1);
    175 
    176 	ret = 0;
    177 
    178 quit:
    179 	_unmap_file(&r);
    180 	return ret;
    181 }
    182 
    183 /*
    184  * mapper_close:
    185  *	simply close a mapper. (without handling hash)
    186  */
    187 static void
    188 mapper_close(struct _citrus_mapper *cm)
    189 {
    190 	if (cm->cm_module) {
    191 		if (cm->cm_ops) {
    192 			if (cm->cm_closure)
    193 				(*cm->cm_ops->mo_uninit)(cm);
    194 			free(cm->cm_ops);
    195 		}
    196 		_citrus_unload_module(cm->cm_module);
    197 	}
    198 	free(cm->cm_traits);
    199 	free(cm);
    200 }
    201 
    202 /*
    203  * mapper_open:
    204  *	simply open a mapper. (without handling hash)
    205  */
    206 static int
    207 mapper_open(struct _citrus_mapper_area *__restrict ma,
    208 	    struct _citrus_mapper * __restrict * __restrict rcm,
    209 	    const char * __restrict module,
    210 	    const char * __restrict variable)
    211 {
    212 	int ret;
    213 	struct _citrus_mapper *cm;
    214 	_citrus_mapper_getops_t getops;
    215 
    216 	/* initialize mapper handle */
    217 	cm = malloc(sizeof(*cm));
    218 	if (!cm)
    219 		return errno;
    220 
    221 	cm->cm_module = NULL;
    222 	cm->cm_ops = NULL;
    223 	cm->cm_closure = NULL;
    224 	cm->cm_traits = NULL;
    225 	cm->cm_refcount = 0;
    226 	cm->cm_key = NULL;
    227 
    228 	/* load module */
    229 	ret = _citrus_load_module(&cm->cm_module, module);
    230 	if (ret)
    231 		goto err;
    232 
    233 	/* get operators */
    234 	getops = (_citrus_mapper_getops_t)
    235 	    _citrus_find_getops(cm->cm_module, module, "mapper");
    236 	if (!getops) {
    237 		ret = EOPNOTSUPP;
    238 		goto err;
    239 	}
    240 	cm->cm_ops = malloc(sizeof(*cm->cm_ops));
    241 	if (!cm->cm_ops) {
    242 		ret = errno;
    243 		goto err;
    244 	}
    245 	ret = (*getops)(cm->cm_ops, sizeof(*cm->cm_ops),
    246 			_CITRUS_MAPPER_ABI_VERSION);
    247 	if (ret)
    248 		goto err;
    249 
    250 	if (!cm->cm_ops->mo_init ||
    251 	    !cm->cm_ops->mo_uninit ||
    252 	    !cm->cm_ops->mo_convert ||
    253 	    !cm->cm_ops->mo_init_state) {
    254 		ret = EINVAL;
    255 		goto err;
    256 	}
    257 
    258 	/* allocate traits structure */
    259 	cm->cm_traits = malloc(sizeof(*cm->cm_traits));
    260 	if (cm->cm_traits == NULL) {
    261 		ret = errno;
    262 		goto err;
    263 	}
    264 	/* initialize the mapper */
    265 	ret = (*cm->cm_ops->mo_init)(ma, cm, ma->ma_dir,
    266 				     (const void *)variable,
    267 				     strlen(variable)+1,
    268 				     cm->cm_traits, sizeof(*cm->cm_traits));
    269 	if (ret)
    270 		goto err;
    271 
    272 	*rcm = cm;
    273 
    274 	return 0;
    275 
    276 err:
    277 	mapper_close(cm);
    278 	return ret;
    279 }
    280 
    281 /*
    282  * _citrus_mapper_open_direct:
    283  *	open a mapper.
    284  */
    285 int
    286 _citrus_mapper_open_direct(struct _citrus_mapper_area *__restrict ma,
    287 			   struct _citrus_mapper * __restrict * __restrict rcm,
    288 			   const char * __restrict module,
    289 			   const char * __restrict variable)
    290 {
    291 	return mapper_open(ma, rcm, module, variable);
    292 }
    293 
    294 /*
    295  * hash_func
    296  */
    297 static __inline int
    298 hash_func(const char *key)
    299 {
    300 	return _string_hash_func(key, CM_HASH_SIZE);
    301 }
    302 
    303 /*
    304  * match_func
    305  */
    306 static __inline int
    307 match_func(struct _citrus_mapper *cm, const char *key)
    308 {
    309 	return strcmp(cm->cm_key, key);
    310 }
    311 
    312 /*
    313  * _citrus_mapper_open:
    314  *	open a mapper with looking up "mapper.dir".
    315  */
    316 int
    317 _citrus_mapper_open(struct _citrus_mapper_area *__restrict ma,
    318 		    struct _citrus_mapper * __restrict * __restrict rcm,
    319 		    const char * __restrict mapname)
    320 {
    321 	int ret;
    322 	char linebuf[PATH_MAX];
    323 	const char *module, *variable = NULL;
    324 	struct _citrus_mapper *cm;
    325 	int hashval;
    326 
    327 	rwlock_wrlock(&lock);
    328 
    329 	/* search in the cache */
    330 	hashval = hash_func(mapname);
    331 	_CITRUS_HASH_SEARCH(&ma->ma_cache, cm, cm_entry, match_func, mapname,
    332 			    hashval);
    333 	if (cm) {
    334 		/* found */
    335 		cm->cm_refcount++;
    336 		*rcm = cm;
    337 		ret = 0;
    338 		goto quit;
    339 	}
    340 
    341 	/* search mapper entry */
    342 	ret = lookup_mapper_entry(ma->ma_dir, mapname, linebuf,
    343 	    (size_t)PATH_MAX, &module, &variable);
    344 	if (ret)
    345 		goto quit;
    346 
    347 	/* open mapper */
    348 	ret = mapper_open(ma, &cm, module, variable);
    349 	if (ret)
    350 		goto quit;
    351 	cm->cm_key = strdup(mapname);
    352 	if (cm->cm_key == NULL) {
    353 		ret = errno;
    354 		rwlock_unlock(&lock);
    355 		_mapper_close(cm);
    356 		return ret;
    357 	}
    358 
    359 	/* insert to the cache */
    360 	cm->cm_refcount = 1;
    361 	_CITRUS_HASH_INSERT(&ma->ma_cache, cm, cm_entry, hashval);
    362 
    363 	*rcm = cm;
    364 	ret = 0;
    365 quit:
    366 	rwlock_unlock(&lock);
    367 	return ret;
    368 }
    369 
    370 /*
    371  * _citrus_mapper_close:
    372  *	close the specified mapper.
    373  */
    374 void
    375 _citrus_mapper_close(struct _citrus_mapper *cm)
    376 {
    377 	if (cm) {
    378 		rwlock_wrlock(&lock);
    379 		if (cm->cm_refcount == REFCOUNT_PERSISTENT)
    380 			goto quit;
    381 		if (cm->cm_refcount > 0) {
    382 			if (--cm->cm_refcount > 0)
    383 				goto quit;
    384 			_CITRUS_HASH_REMOVE(cm, cm_entry);
    385 			free(cm->cm_key);
    386 		}
    387 		rwlock_unlock(&lock);
    388 		mapper_close(cm);
    389 		return;
    390 quit:
    391 		rwlock_unlock(&lock);
    392 	}
    393 }
    394 
    395 /*
    396  * _citrus_mapper_set_persistent:
    397  *	set persistent count.
    398  */
    399 void
    400 _citrus_mapper_set_persistent(struct _citrus_mapper * __restrict cm)
    401 {
    402 	rwlock_wrlock(&lock);
    403 	cm->cm_refcount = REFCOUNT_PERSISTENT;
    404 	rwlock_unlock(&lock);
    405 }
    406