Home | History | Annotate | Line # | Download | only in citrus
citrus_mapper.c revision 1.1
      1  1.1  tshiozak /*	$NetBSD: citrus_mapper.c,v 1.1 2003/06/25 09:51:36 tshiozak 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.1  tshiozak __RCSID("$NetBSD: citrus_mapper.c,v 1.1 2003/06/25 09:51:36 tshiozak 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.1  tshiozak 
     45  1.1  tshiozak #include "citrus_namespace.h"
     46  1.1  tshiozak #include "citrus_types.h"
     47  1.1  tshiozak #include "citrus_region.h"
     48  1.1  tshiozak #include "citrus_memstream.h"
     49  1.1  tshiozak #include "citrus_bcs.h"
     50  1.1  tshiozak #include "citrus_mmap.h"
     51  1.1  tshiozak #include "citrus_module.h"
     52  1.1  tshiozak #include "citrus_hash.h"
     53  1.1  tshiozak #include "citrus_mapper.h"
     54  1.1  tshiozak 
     55  1.1  tshiozak #define _CITRUS_MAPPER_DIR	"mapper.dir"
     56  1.1  tshiozak 
     57  1.1  tshiozak #define CM_HASH_SIZE 101
     58  1.1  tshiozak #define REFCOUNT_PERSISTENT	-1
     59  1.1  tshiozak 
     60  1.1  tshiozak #ifdef _REENTRANT
     61  1.1  tshiozak static rwlock_t lock = RWLOCK_INITIALIZER;
     62  1.1  tshiozak #endif
     63  1.1  tshiozak 
     64  1.1  tshiozak struct _citrus_mapper_area {
     65  1.1  tshiozak 	_CITRUS_HASH_HEAD(, _citrus_mapper, CM_HASH_SIZE)	ma_cache;
     66  1.1  tshiozak 	char							*ma_dir;
     67  1.1  tshiozak };
     68  1.1  tshiozak 
     69  1.1  tshiozak /*
     70  1.1  tshiozak  * _citrus_mapper_create_area:
     71  1.1  tshiozak  *	create mapper area
     72  1.1  tshiozak  */
     73  1.1  tshiozak 
     74  1.1  tshiozak int
     75  1.1  tshiozak _citrus_mapper_create_area(
     76  1.1  tshiozak 	struct _citrus_mapper_area *__restrict *__restrict rma,
     77  1.1  tshiozak 	const char *__restrict area)
     78  1.1  tshiozak {
     79  1.1  tshiozak 	struct stat st;
     80  1.1  tshiozak 	int ret;
     81  1.1  tshiozak 	char path[PATH_MAX];
     82  1.1  tshiozak 	struct _citrus_mapper_area *ma;
     83  1.1  tshiozak 
     84  1.1  tshiozak 	rwlock_wrlock(&lock);
     85  1.1  tshiozak 
     86  1.1  tshiozak 	if (*rma != NULL) {
     87  1.1  tshiozak 		ret = 0;
     88  1.1  tshiozak 		goto quit;
     89  1.1  tshiozak 	}
     90  1.1  tshiozak 
     91  1.1  tshiozak 	snprintf(path, PATH_MAX, "%s/%s", area, _CITRUS_MAPPER_DIR);
     92  1.1  tshiozak 
     93  1.1  tshiozak 	ret = stat(path, &st);
     94  1.1  tshiozak 	if (ret)
     95  1.1  tshiozak 		goto quit;
     96  1.1  tshiozak 
     97  1.1  tshiozak 	ma = malloc(sizeof(*ma));
     98  1.1  tshiozak 	if (ma == NULL) {
     99  1.1  tshiozak 		ret = errno;
    100  1.1  tshiozak 		goto quit;
    101  1.1  tshiozak 	}
    102  1.1  tshiozak 	ma->ma_dir = strdup(area);
    103  1.1  tshiozak 	if (ma->ma_dir == NULL) {
    104  1.1  tshiozak 		ret = errno;
    105  1.1  tshiozak 		free(ma->ma_dir);
    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.1  tshiozak 	snprintf(path, 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.1  tshiozak  * mapper_open:
    185  1.1  tshiozak  */
    186  1.1  tshiozak static int
    187  1.1  tshiozak mapper_open(struct _citrus_mapper_area *__restrict ma,
    188  1.1  tshiozak 	    struct _citrus_mapper * __restrict * __restrict rcm,
    189  1.1  tshiozak 	    const char * __restrict module,
    190  1.1  tshiozak 	    const char * __restrict variable)
    191  1.1  tshiozak {
    192  1.1  tshiozak 	int ret;
    193  1.1  tshiozak 	struct _citrus_mapper *cm;
    194  1.1  tshiozak 	_citrus_mapper_getops_t getops;
    195  1.1  tshiozak 
    196  1.1  tshiozak 	/* initialize mapper handle */
    197  1.1  tshiozak 	cm = malloc(sizeof(*cm));
    198  1.1  tshiozak 	if (!cm)
    199  1.1  tshiozak 		return errno;
    200  1.1  tshiozak 
    201  1.1  tshiozak 	cm->cm_module = NULL;
    202  1.1  tshiozak 	cm->cm_ops = NULL;
    203  1.1  tshiozak 	cm->cm_closure = NULL;
    204  1.1  tshiozak 	cm->cm_traits = NULL;
    205  1.1  tshiozak 	cm->cm_refcount = 0;
    206  1.1  tshiozak 	cm->cm_key = NULL;
    207  1.1  tshiozak 
    208  1.1  tshiozak 	/* load module */
    209  1.1  tshiozak 	ret = _citrus_load_module(&cm->cm_module, module);
    210  1.1  tshiozak 	if (ret)
    211  1.1  tshiozak 		goto err;
    212  1.1  tshiozak 
    213  1.1  tshiozak 	/* get operators */
    214  1.1  tshiozak 	getops = (_citrus_mapper_getops_t)
    215  1.1  tshiozak 	    _citrus_find_getops(cm->cm_module, module, "mapper");
    216  1.1  tshiozak 	if (!getops) {
    217  1.1  tshiozak 		ret = EOPNOTSUPP;
    218  1.1  tshiozak 		goto err;
    219  1.1  tshiozak 	}
    220  1.1  tshiozak 	cm->cm_ops = malloc(sizeof(*cm->cm_ops));
    221  1.1  tshiozak 	if (!cm->cm_ops) {
    222  1.1  tshiozak 		ret = errno;
    223  1.1  tshiozak 		goto err;
    224  1.1  tshiozak 	}
    225  1.1  tshiozak 	ret = (*getops)(cm->cm_ops, sizeof(*cm->cm_ops),
    226  1.1  tshiozak 			_CITRUS_MAPPER_ABI_VERSION);
    227  1.1  tshiozak 	if (ret)
    228  1.1  tshiozak 		goto err;
    229  1.1  tshiozak 
    230  1.1  tshiozak 	if (!cm->cm_ops->mo_init ||
    231  1.1  tshiozak 	    !cm->cm_ops->mo_uninit ||
    232  1.1  tshiozak 	    !cm->cm_ops->mo_convert ||
    233  1.1  tshiozak 	    !cm->cm_ops->mo_init_state)
    234  1.1  tshiozak 		goto err;
    235  1.1  tshiozak 
    236  1.1  tshiozak 	/* allocate traits structure */
    237  1.1  tshiozak 	cm->cm_traits = malloc(sizeof(*cm->cm_traits));
    238  1.1  tshiozak 	if (cm->cm_traits == NULL) {
    239  1.1  tshiozak 		ret = errno;
    240  1.1  tshiozak 		goto err;
    241  1.1  tshiozak 	}
    242  1.1  tshiozak 	/* initialize the mapper */
    243  1.1  tshiozak 	ret = (*cm->cm_ops->mo_init)(ma, cm, ma->ma_dir,
    244  1.1  tshiozak 				     (const void *)variable,
    245  1.1  tshiozak 				     strlen(variable)+1,
    246  1.1  tshiozak 				     cm->cm_traits, sizeof(*cm->cm_traits));
    247  1.1  tshiozak 	if (ret)
    248  1.1  tshiozak 		goto err;
    249  1.1  tshiozak 
    250  1.1  tshiozak 	*rcm = cm;
    251  1.1  tshiozak 
    252  1.1  tshiozak 	return 0;
    253  1.1  tshiozak err:
    254  1.1  tshiozak 	_citrus_mapper_close(cm);
    255  1.1  tshiozak 	return ret;
    256  1.1  tshiozak }
    257  1.1  tshiozak 
    258  1.1  tshiozak /*
    259  1.1  tshiozak  * _citrus_mapper_open_direct:
    260  1.1  tshiozak  *	open a mapper.
    261  1.1  tshiozak  */
    262  1.1  tshiozak int
    263  1.1  tshiozak _citrus_mapper_open_direct(struct _citrus_mapper_area *__restrict ma,
    264  1.1  tshiozak 			   struct _citrus_mapper * __restrict * __restrict rcm,
    265  1.1  tshiozak 			   const char * __restrict module,
    266  1.1  tshiozak 			   const char * __restrict variable)
    267  1.1  tshiozak {
    268  1.1  tshiozak 	return mapper_open(ma, rcm, module, variable);
    269  1.1  tshiozak }
    270  1.1  tshiozak 
    271  1.1  tshiozak /*
    272  1.1  tshiozak  * hash_func
    273  1.1  tshiozak  */
    274  1.1  tshiozak static __inline int
    275  1.1  tshiozak hash_func(const char *key)
    276  1.1  tshiozak {
    277  1.1  tshiozak 	return _string_hash_func(key, CM_HASH_SIZE);
    278  1.1  tshiozak }
    279  1.1  tshiozak 
    280  1.1  tshiozak /*
    281  1.1  tshiozak  * match_func
    282  1.1  tshiozak  */
    283  1.1  tshiozak static __inline int
    284  1.1  tshiozak match_func(struct _citrus_mapper *cm, const char *key)
    285  1.1  tshiozak {
    286  1.1  tshiozak 	return strcmp(cm->cm_key, key);
    287  1.1  tshiozak }
    288  1.1  tshiozak 
    289  1.1  tshiozak /*
    290  1.1  tshiozak  * _citrus_mapper_open:
    291  1.1  tshiozak  *	open a mapper with looking up "mapper.dir".
    292  1.1  tshiozak  */
    293  1.1  tshiozak int
    294  1.1  tshiozak _citrus_mapper_open(struct _citrus_mapper_area *__restrict ma,
    295  1.1  tshiozak 		    struct _citrus_mapper * __restrict * __restrict rcm,
    296  1.1  tshiozak 		    const char * __restrict mapname)
    297  1.1  tshiozak {
    298  1.1  tshiozak 	int ret;
    299  1.1  tshiozak 	char linebuf[PATH_MAX];
    300  1.1  tshiozak 	const char *module, *variable;
    301  1.1  tshiozak 	struct _citrus_mapper *cm;
    302  1.1  tshiozak 	int hashval;
    303  1.1  tshiozak 
    304  1.1  tshiozak 	rwlock_wrlock(&lock);
    305  1.1  tshiozak 
    306  1.1  tshiozak 	/* search in the cache */
    307  1.1  tshiozak 	hashval = hash_func(mapname);
    308  1.1  tshiozak 	_CITRUS_HASH_SEARCH(&ma->ma_cache, cm, cm_entry, match_func, mapname,
    309  1.1  tshiozak 			    hashval);
    310  1.1  tshiozak 	if (cm) {
    311  1.1  tshiozak 		/* found */
    312  1.1  tshiozak 		cm->cm_refcount++;
    313  1.1  tshiozak 		*rcm = cm;
    314  1.1  tshiozak 		ret = 0;
    315  1.1  tshiozak 		goto quit;
    316  1.1  tshiozak 	}
    317  1.1  tshiozak 
    318  1.1  tshiozak 	/* search mapper entry */
    319  1.1  tshiozak 	ret = lookup_mapper_entry(ma->ma_dir, mapname, linebuf, PATH_MAX,
    320  1.1  tshiozak 				  &module, &variable);
    321  1.1  tshiozak 	if (ret)
    322  1.1  tshiozak 		goto quit;
    323  1.1  tshiozak 
    324  1.1  tshiozak 	/* open mapper */
    325  1.1  tshiozak 	ret = mapper_open(ma, &cm, module, variable);
    326  1.1  tshiozak 	if (ret)
    327  1.1  tshiozak 		goto quit;
    328  1.1  tshiozak 	cm->cm_key = strdup(mapname);
    329  1.1  tshiozak 	if (cm->cm_key == NULL) {
    330  1.1  tshiozak 		ret = errno;
    331  1.1  tshiozak 		rwlock_unlock(&lock);
    332  1.1  tshiozak 		_mapper_close(cm);
    333  1.1  tshiozak 		return ret;
    334  1.1  tshiozak 	}
    335  1.1  tshiozak 
    336  1.1  tshiozak 	/* insert to the cache */
    337  1.1  tshiozak 	cm->cm_refcount = 1;
    338  1.1  tshiozak 	_CITRUS_HASH_INSERT(&ma->ma_cache, cm, cm_entry, hashval);
    339  1.1  tshiozak 
    340  1.1  tshiozak 	*rcm = cm;
    341  1.1  tshiozak 	ret = 0;
    342  1.1  tshiozak quit:
    343  1.1  tshiozak 	rwlock_unlock(&lock);
    344  1.1  tshiozak 	return ret;
    345  1.1  tshiozak }
    346  1.1  tshiozak 
    347  1.1  tshiozak /*
    348  1.1  tshiozak  * _citrus_mapper_close:
    349  1.1  tshiozak  *	close the specified mapper.
    350  1.1  tshiozak  */
    351  1.1  tshiozak void
    352  1.1  tshiozak _citrus_mapper_close(struct _citrus_mapper *cm)
    353  1.1  tshiozak {
    354  1.1  tshiozak 	if (cm) {
    355  1.1  tshiozak 		rwlock_wrlock(&lock);
    356  1.1  tshiozak 		if (cm->cm_refcount == REFCOUNT_PERSISTENT) {
    357  1.1  tshiozak 			rwlock_unlock(&lock);
    358  1.1  tshiozak 			return;
    359  1.1  tshiozak 		}
    360  1.1  tshiozak 		if (cm->cm_refcount > 0) {
    361  1.1  tshiozak 			if (--cm->cm_refcount > 0) {
    362  1.1  tshiozak 				rwlock_unlock(&lock);
    363  1.1  tshiozak 				return;
    364  1.1  tshiozak 			} else {
    365  1.1  tshiozak 				_CITRUS_HASH_REMOVE(cm, cm_entry);
    366  1.1  tshiozak 			}
    367  1.1  tshiozak 		}
    368  1.1  tshiozak 		if (cm->cm_module) {
    369  1.1  tshiozak 			if (cm->cm_ops) {
    370  1.1  tshiozak 				if (cm->cm_closure)
    371  1.1  tshiozak 					(*cm->cm_ops->mo_uninit)(cm);
    372  1.1  tshiozak 				free(cm->cm_ops);
    373  1.1  tshiozak 			}
    374  1.1  tshiozak 			_citrus_unload_module(cm->cm_module);
    375  1.1  tshiozak 		}
    376  1.1  tshiozak 		free(cm->cm_key);
    377  1.1  tshiozak 		free(cm->cm_traits);
    378  1.1  tshiozak 		free(cm);
    379  1.1  tshiozak 		rwlock_unlock(&lock);
    380  1.1  tshiozak 	}
    381  1.1  tshiozak }
    382  1.1  tshiozak 
    383  1.1  tshiozak /*
    384  1.1  tshiozak  * _citrus_mapper_set_persistent:
    385  1.1  tshiozak  *	set persistent count.
    386  1.1  tshiozak  */
    387  1.1  tshiozak void
    388  1.1  tshiozak _citrus_mapper_set_persistent(struct _citrus_mapper * __restrict cm)
    389  1.1  tshiozak {
    390  1.1  tshiozak 	rwlock_wrlock(&lock);
    391  1.1  tshiozak 	cm->cm_refcount = REFCOUNT_PERSISTENT;
    392  1.1  tshiozak 	rwlock_unlock(&lock);
    393  1.1  tshiozak }
    394