Index: head/lib/libc/iconv/citrus_csmapper.c =================================================================== --- head/lib/libc/iconv/citrus_csmapper.c (revision 252583) +++ head/lib/libc/iconv/citrus_csmapper.c (revision 252584) @@ -1,383 +1,385 @@ /* $FreeBSD$ */ /* $NetBSD: citrus_csmapper.c,v 1.10 2009/01/11 02:46:24 christos Exp $ */ /*- * Copyright (c)2003 Citrus Project, * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include #include #include #include #include #include #include #include #include #include #include #include "citrus_namespace.h" #include "citrus_types.h" #include "citrus_bcs.h" #include "citrus_region.h" #include "citrus_lock.h" #include "citrus_memstream.h" #include "citrus_mmap.h" #include "citrus_module.h" #include "citrus_hash.h" #include "citrus_mapper.h" #include "citrus_csmapper.h" #include "citrus_pivot_file.h" #include "citrus_db.h" #include "citrus_db_hash.h" #include "citrus_lookup.h" static struct _citrus_mapper_area *maparea = NULL; +static pthread_rwlock_t ma_lock = PTHREAD_RWLOCK_INITIALIZER; + #define CS_ALIAS _PATH_CSMAPPER "/charset.alias" #define CS_PIVOT _PATH_CSMAPPER "/charset.pivot" /* ---------------------------------------------------------------------- */ static int get32(struct _region *r, uint32_t *rval) { if (_region_size(r) != 4) return (EFTYPE); memcpy(rval, _region_head(r), (size_t)4); *rval = be32toh(*rval); return (0); } static int open_subdb(struct _citrus_db **subdb, struct _citrus_db *db, const char *src) { struct _region r; int ret; ret = _db_lookup_by_s(db, src, &r, NULL); if (ret) return (ret); ret = _db_open(subdb, &r, _CITRUS_PIVOT_SUB_MAGIC, _db_hash_std, NULL); if (ret) return (ret); return (0); } #define NO_SUCH_FILE EOPNOTSUPP static int find_best_pivot_pvdb(const char *src, const char *dst, char *pivot, size_t pvlen, unsigned long *rnorm) { struct _citrus_db *db1, *db2, *db3; struct _region fr, r1, r2; char buf[LINE_MAX]; uint32_t val32; unsigned long norm; int i, num, ret; ret = _map_file(&fr, CS_PIVOT ".pvdb"); if (ret) { if (ret == ENOENT) ret = NO_SUCH_FILE; return (ret); } ret = _db_open(&db1, &fr, _CITRUS_PIVOT_MAGIC, _db_hash_std, NULL); if (ret) goto quit1; ret = open_subdb(&db2, db1, src); if (ret) goto quit2; num = _db_get_num_entries(db2); *rnorm = ULONG_MAX; for (i = 0; i < num; i++) { /* iterate each pivot */ ret = _db_get_entry(db2, i, &r1, &r2); if (ret) goto quit3; /* r1:pivot name, r2:norm among src and pivot */ ret = get32(&r2, &val32); if (ret) goto quit3; norm = val32; snprintf(buf, sizeof(buf), "%.*s", (int)_region_size(&r1), (char *)_region_head(&r1)); /* buf: pivot name */ ret = open_subdb(&db3, db1, buf); if (ret) goto quit3; if (_db_lookup_by_s(db3, dst, &r2, NULL) != 0) goto quit4; /* r2: norm among pivot and dst */ ret = get32(&r2, &val32); if (ret) goto quit4; norm += val32; /* judge minimum norm */ if (norm < *rnorm) { *rnorm = norm; strlcpy(pivot, buf, pvlen); } quit4: _db_close(db3); if (ret) goto quit3; } quit3: _db_close(db2); quit2: _db_close(db1); quit1: _unmap_file(&fr); if (ret) return (ret); if (*rnorm == ULONG_MAX) return (ENOENT); return (0); } /* ---------------------------------------------------------------------- */ struct zone { const char *begin, *end; }; struct parse_arg { char dst[PATH_MAX]; unsigned long norm; }; static int parse_line(struct parse_arg *pa, struct _region *r) { struct zone z1, z2; char buf[20]; size_t len; len = _region_size(r); z1.begin = _bcs_skip_ws_len(_region_head(r), &len); if (len == 0) return (EFTYPE); z1.end = _bcs_skip_nonws_len(z1.begin, &len); if (len == 0) return (EFTYPE); z2.begin = _bcs_skip_ws_len(z1.end, &len); if (len == 0) return (EFTYPE); z2.end = _bcs_skip_nonws_len(z2.begin, &len); /* z1 : dst name, z2 : norm */ snprintf(pa->dst, sizeof(pa->dst), "%.*s", (int)(z1.end-z1.begin), z1.begin); snprintf(buf, sizeof(buf), "%.*s", (int)(z2.end-z2.begin), z2.begin); pa->norm = _bcs_strtoul(buf, NULL, 0); return (0); } static int find_dst(struct parse_arg *pasrc, const char *dst) { struct _lookup *cl; struct parse_arg padst; struct _region data; int ret; ret = _lookup_seq_open(&cl, CS_PIVOT, _LOOKUP_CASE_IGNORE); if (ret) return (ret); ret = _lookup_seq_lookup(cl, pasrc->dst, &data); while (ret == 0) { ret = parse_line(&padst, &data); if (ret) break; if (strcmp(dst, padst.dst) == 0) { pasrc->norm += padst.norm; break; } ret = _lookup_seq_next(cl, NULL, &data); } _lookup_seq_close(cl); return (ret); } static int find_best_pivot_lookup(const char *src, const char *dst, char *pivot, size_t pvlen, unsigned long *rnorm) { struct _lookup *cl; struct _region data; struct parse_arg pa; char pivot_min[PATH_MAX]; unsigned long norm_min; int ret; ret = _lookup_seq_open(&cl, CS_PIVOT, _LOOKUP_CASE_IGNORE); if (ret) return (ret); norm_min = ULONG_MAX; /* find pivot code */ ret = _lookup_seq_lookup(cl, src, &data); while (ret == 0) { ret = parse_line(&pa, &data); if (ret) break; ret = find_dst(&pa, dst); if (ret) break; if (pa.norm < norm_min) { norm_min = pa.norm; strlcpy(pivot_min, pa.dst, sizeof(pivot_min)); } ret = _lookup_seq_next(cl, NULL, &data); } _lookup_seq_close(cl); if (ret != ENOENT) return (ret); if (norm_min == ULONG_MAX) return (ENOENT); strlcpy(pivot, pivot_min, pvlen); if (rnorm) *rnorm = norm_min; return (0); } static int find_best_pivot(const char *src, const char *dst, char *pivot, size_t pvlen, unsigned long *rnorm) { int ret; ret = find_best_pivot_pvdb(src, dst, pivot, pvlen, rnorm); if (ret == NO_SUCH_FILE) ret = find_best_pivot_lookup(src, dst, pivot, pvlen, rnorm); return (ret); } static __inline int open_serial_mapper(struct _citrus_mapper_area *__restrict ma, struct _citrus_mapper * __restrict * __restrict rcm, const char *src, const char *pivot, const char *dst) { char buf[PATH_MAX]; snprintf(buf, sizeof(buf), "%s/%s,%s/%s", src, pivot, pivot, dst); return (_mapper_open_direct(ma, rcm, "mapper_serial", buf)); } static struct _citrus_csmapper *csm_none = NULL; static int get_none(struct _citrus_mapper_area *__restrict ma, struct _citrus_csmapper *__restrict *__restrict rcsm) { int ret; - WLOCK; + WLOCK(&ma_lock); if (csm_none) { *rcsm = csm_none; ret = 0; goto quit; } ret = _mapper_open_direct(ma, &csm_none, "mapper_none", ""); if (ret) goto quit; _mapper_set_persistent(csm_none); *rcsm = csm_none; ret = 0; quit: - UNLOCK; + UNLOCK(&ma_lock); return (ret); } int _citrus_csmapper_open(struct _citrus_csmapper * __restrict * __restrict rcsm, const char * __restrict src, const char * __restrict dst, uint32_t flags, unsigned long *rnorm) { const char *realsrc, *realdst; char buf1[PATH_MAX], buf2[PATH_MAX], key[PATH_MAX], pivot[PATH_MAX]; unsigned long norm; int ret; norm = 0; ret = _citrus_mapper_create_area(&maparea, _PATH_CSMAPPER); if (ret) return (ret); realsrc = _lookup_alias(CS_ALIAS, src, buf1, sizeof(buf1), _LOOKUP_CASE_IGNORE); realdst = _lookup_alias(CS_ALIAS, dst, buf2, sizeof(buf2), _LOOKUP_CASE_IGNORE); if (!strcmp(realsrc, realdst)) { ret = get_none(maparea, rcsm); if (ret == 0 && rnorm != NULL) *rnorm = 0; return (ret); } snprintf(key, sizeof(key), "%s/%s", realsrc, realdst); ret = _mapper_open(maparea, rcsm, key); if (ret == 0) { if (rnorm != NULL) *rnorm = 0; return (0); } if (ret != ENOENT || (flags & _CSMAPPER_F_PREVENT_PIVOT)!=0) return (ret); ret = find_best_pivot(realsrc, realdst, pivot, sizeof(pivot), &norm); if (ret) return (ret); ret = open_serial_mapper(maparea, rcsm, realsrc, pivot, realdst); if (ret == 0 && rnorm != NULL) *rnorm = norm; return (ret); } Index: head/lib/libc/iconv/citrus_iconv.c =================================================================== --- head/lib/libc/iconv/citrus_iconv.c (revision 252583) +++ head/lib/libc/iconv/citrus_iconv.c (revision 252584) @@ -1,335 +1,337 @@ /* $FreeBSD$ */ /* $NetBSD: citrus_iconv.c,v 1.7 2008/07/25 14:05:25 christos Exp $ */ /*- * Copyright (c)2003 Citrus Project, * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "citrus_namespace.h" #include "citrus_bcs.h" #include "citrus_esdb.h" #include "citrus_region.h" #include "citrus_memstream.h" #include "citrus_mmap.h" #include "citrus_module.h" #include "citrus_lock.h" #include "citrus_lookup.h" #include "citrus_hash.h" #include "citrus_iconv.h" #define _CITRUS_ICONV_DIR "iconv.dir" #define _CITRUS_ICONV_ALIAS "iconv.alias" #define CI_HASH_SIZE 101 #define CI_INITIAL_MAX_REUSE 5 #define CI_ENV_MAX_REUSE "ICONV_MAX_REUSE" static bool isinit = false; static int shared_max_reuse, shared_num_unused; static _CITRUS_HASH_HEAD(, _citrus_iconv_shared, CI_HASH_SIZE) shared_pool; static TAILQ_HEAD(, _citrus_iconv_shared) shared_unused; +static pthread_rwlock_t ci_lock = PTHREAD_RWLOCK_INITIALIZER; + static __inline void init_cache(void) { - WLOCK; + WLOCK(&ci_lock); if (!isinit) { _CITRUS_HASH_INIT(&shared_pool, CI_HASH_SIZE); TAILQ_INIT(&shared_unused); shared_max_reuse = -1; if (!issetugid() && getenv(CI_ENV_MAX_REUSE)) shared_max_reuse = atoi(getenv(CI_ENV_MAX_REUSE)); if (shared_max_reuse < 0) shared_max_reuse = CI_INITIAL_MAX_REUSE; isinit = true; } - UNLOCK; + UNLOCK(&ci_lock); } static __inline void close_shared(struct _citrus_iconv_shared *ci) { if (ci) { if (ci->ci_module) { if (ci->ci_ops) { if (ci->ci_closure) (*ci->ci_ops->io_uninit_shared)(ci); free(ci->ci_ops); } _citrus_unload_module(ci->ci_module); } free(ci); } } static __inline int open_shared(struct _citrus_iconv_shared * __restrict * __restrict rci, const char * __restrict convname, const char * __restrict src, const char * __restrict dst) { struct _citrus_iconv_shared *ci; _citrus_iconv_getops_t getops; const char *module; size_t len_convname; int ret; module = (strcmp(src, dst) != 0) ? "iconv_std" : "iconv_none"; /* initialize iconv handle */ len_convname = strlen(convname); ci = malloc(sizeof(*ci) + len_convname + 1); if (!ci) { ret = errno; goto err; } ci->ci_module = NULL; ci->ci_ops = NULL; ci->ci_closure = NULL; ci->ci_convname = (void *)&ci[1]; memcpy(ci->ci_convname, convname, len_convname + 1); /* load module */ ret = _citrus_load_module(&ci->ci_module, module); if (ret) goto err; /* get operators */ getops = (_citrus_iconv_getops_t)_citrus_find_getops(ci->ci_module, module, "iconv"); if (!getops) { ret = EOPNOTSUPP; goto err; } ci->ci_ops = malloc(sizeof(*ci->ci_ops)); if (!ci->ci_ops) { ret = errno; goto err; } ret = (*getops)(ci->ci_ops); if (ret) goto err; if (ci->ci_ops->io_init_shared == NULL || ci->ci_ops->io_uninit_shared == NULL || ci->ci_ops->io_init_context == NULL || ci->ci_ops->io_uninit_context == NULL || ci->ci_ops->io_convert == NULL) goto err; /* initialize the converter */ ret = (*ci->ci_ops->io_init_shared)(ci, src, dst); if (ret) goto err; *rci = ci; return (0); err: close_shared(ci); return (ret); } static __inline int hash_func(const char *key) { return (_string_hash_func(key, CI_HASH_SIZE)); } static __inline int match_func(struct _citrus_iconv_shared * __restrict ci, const char * __restrict key) { return (strcmp(ci->ci_convname, key)); } static int get_shared(struct _citrus_iconv_shared * __restrict * __restrict rci, const char *src, const char *dst) { struct _citrus_iconv_shared * ci; char convname[PATH_MAX]; int hashval, ret = 0; snprintf(convname, sizeof(convname), "%s/%s", src, dst); - WLOCK; + WLOCK(&ci_lock); /* lookup alread existing entry */ hashval = hash_func(convname); _CITRUS_HASH_SEARCH(&shared_pool, ci, ci_hash_entry, match_func, convname, hashval); if (ci != NULL) { /* found */ if (ci->ci_used_count == 0) { TAILQ_REMOVE(&shared_unused, ci, ci_tailq_entry); shared_num_unused--; } ci->ci_used_count++; *rci = ci; goto quit; } /* create new entry */ ret = open_shared(&ci, convname, src, dst); if (ret) goto quit; _CITRUS_HASH_INSERT(&shared_pool, ci, ci_hash_entry, hashval); ci->ci_used_count = 1; *rci = ci; quit: - UNLOCK; + UNLOCK(&ci_lock); return (ret); } static void release_shared(struct _citrus_iconv_shared * __restrict ci) { - WLOCK; + WLOCK(&ci_lock); ci->ci_used_count--; if (ci->ci_used_count == 0) { /* put it into unused list */ shared_num_unused++; TAILQ_INSERT_TAIL(&shared_unused, ci, ci_tailq_entry); /* flood out */ while (shared_num_unused > shared_max_reuse) { ci = TAILQ_FIRST(&shared_unused); TAILQ_REMOVE(&shared_unused, ci, ci_tailq_entry); _CITRUS_HASH_REMOVE(ci, ci_hash_entry); shared_num_unused--; close_shared(ci); } } - UNLOCK; + UNLOCK(&ci_lock); } /* * _citrus_iconv_open: * open a converter for the specified in/out codes. */ int _citrus_iconv_open(struct _citrus_iconv * __restrict * __restrict rcv, const char * __restrict src, const char * __restrict dst) { struct _citrus_iconv *cv = NULL; struct _citrus_iconv_shared *ci = NULL; char realdst[PATH_MAX], realsrc[PATH_MAX]; char buf[PATH_MAX], path[PATH_MAX]; int ret; init_cache(); /* GNU behaviour, using locale encoding if "" or "char" is specified */ if ((strcmp(src, "") == 0) || (strcmp(src, "char") == 0)) src = nl_langinfo(CODESET); if ((strcmp(dst, "") == 0) || (strcmp(dst, "char") == 0)) dst = nl_langinfo(CODESET); /* resolve codeset name aliases */ strlcpy(realsrc, _lookup_alias(path, src, buf, (size_t)PATH_MAX, _LOOKUP_CASE_IGNORE), (size_t)PATH_MAX); strlcpy(realdst, _lookup_alias(path, dst, buf, (size_t)PATH_MAX, _LOOKUP_CASE_IGNORE), (size_t)PATH_MAX); /* sanity check */ if (strchr(realsrc, '/') != NULL || strchr(realdst, '/')) return (EINVAL); /* get shared record */ ret = get_shared(&ci, realsrc, realdst); if (ret) return (ret); /* create/init context */ if (*rcv == NULL) { cv = malloc(sizeof(*cv)); if (cv == NULL) { ret = errno; release_shared(ci); return (ret); } *rcv = cv; } (*rcv)->cv_shared = ci; ret = (*ci->ci_ops->io_init_context)(*rcv); if (ret) { release_shared(ci); free(cv); return (ret); } return (0); } /* * _citrus_iconv_close: * close the specified converter. */ void _citrus_iconv_close(struct _citrus_iconv *cv) { if (cv) { (*cv->cv_shared->ci_ops->io_uninit_context)(cv); release_shared(cv->cv_shared); free(cv); } } const char *_citrus_iconv_canonicalize(const char *name) { char *buf; if ((buf = malloc((size_t)PATH_MAX)) == NULL) return (NULL); memset((void *)buf, 0, (size_t)PATH_MAX); _citrus_esdb_alias(name, buf, (size_t)PATH_MAX); return (buf); } Index: head/lib/libc/iconv/citrus_lock.h =================================================================== --- head/lib/libc/iconv/citrus_lock.h (revision 252583) +++ head/lib/libc/iconv/citrus_lock.h (revision 252584) @@ -1,36 +1,33 @@ /* $FreeBSD$ */ /*- * Copyright (C) 2010 Gabor Kovesdan * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include -/* XXX Yes, the original code has three separate file-local lock instances */ -static pthread_rwlock_t lock = PTHREAD_RWLOCK_INITIALIZER; - -#define WLOCK if (__isthreaded) \ - pthread_rwlock_wrlock(&lock); -#define UNLOCK if (__isthreaded) \ - pthread_rwlock_unlock(&lock); +#define WLOCK(lock) if (__isthreaded) \ + pthread_rwlock_wrlock(lock); +#define UNLOCK(lock) if (__isthreaded) \ + pthread_rwlock_unlock(lock); Index: head/lib/libc/iconv/citrus_mapper.c =================================================================== --- head/lib/libc/iconv/citrus_mapper.c (revision 252583) +++ head/lib/libc/iconv/citrus_mapper.c (revision 252584) @@ -1,399 +1,401 @@ /* $FreeBSD$ */ /* $NetBSD: citrus_mapper.c,v 1.7 2008/07/25 14:05:25 christos Exp $ */ /*- * Copyright (c)2003 Citrus Project, * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include #include #include #include #include #include #include #include #include #include #include "citrus_namespace.h" #include "citrus_types.h" #include "citrus_region.h" #include "citrus_lock.h" #include "citrus_memstream.h" #include "citrus_bcs.h" #include "citrus_mmap.h" #include "citrus_module.h" #include "citrus_hash.h" #include "citrus_mapper.h" #define _CITRUS_MAPPER_DIR "mapper.dir" #define CM_HASH_SIZE 101 #define REFCOUNT_PERSISTENT -1 +static pthread_rwlock_t cm_lock = PTHREAD_RWLOCK_INITIALIZER; + struct _citrus_mapper_area { _CITRUS_HASH_HEAD(, _citrus_mapper, CM_HASH_SIZE) ma_cache; char *ma_dir; }; /* * _citrus_mapper_create_area: * create mapper area */ int _citrus_mapper_create_area( struct _citrus_mapper_area *__restrict *__restrict rma, const char *__restrict area) { struct _citrus_mapper_area *ma; struct stat st; char path[PATH_MAX]; int ret; - WLOCK; + WLOCK(&cm_lock); if (*rma != NULL) { ret = 0; goto quit; } snprintf(path, (size_t)PATH_MAX, "%s/%s", area, _CITRUS_MAPPER_DIR); ret = stat(path, &st); if (ret) goto quit; ma = malloc(sizeof(*ma)); if (ma == NULL) { ret = errno; goto quit; } ma->ma_dir = strdup(area); if (ma->ma_dir == NULL) { ret = errno; free(ma); goto quit; } _CITRUS_HASH_INIT(&ma->ma_cache, CM_HASH_SIZE); *rma = ma; ret = 0; quit: - UNLOCK; + UNLOCK(&cm_lock); return (ret); } /* * lookup_mapper_entry: * lookup mapper.dir entry in the specified directory. * * line format of iconv.dir file: * mapper module arg * mapper : mapper name. * module : mapper module name. * arg : argument for the module (generally, description file name) */ static int lookup_mapper_entry(const char *dir, const char *mapname, void *linebuf, size_t linebufsize, const char **module, const char **variable) { struct _region r; struct _memstream ms; const char *cp, *cq; char *p; char path[PATH_MAX]; size_t len; int ret; /* create mapper.dir path */ snprintf(path, (size_t)PATH_MAX, "%s/%s", dir, _CITRUS_MAPPER_DIR); /* open read stream */ ret = _map_file(&r, path); if (ret) return (ret); _memstream_bind(&ms, &r); /* search the line matching to the map name */ cp = _memstream_matchline(&ms, mapname, &len, 0); if (!cp) { ret = ENOENT; goto quit; } if (!len || len > linebufsize - 1) { ret = EINVAL; goto quit; } p = linebuf; /* get module name */ *module = p; cq = _bcs_skip_nonws_len(cp, &len); strlcpy(p, cp, (size_t)(cq - cp + 1)); p += cq - cp + 1; /* get variable */ *variable = p; cp = _bcs_skip_ws_len(cq, &len); strlcpy(p, cp, len + 1); ret = 0; quit: _unmap_file(&r); return (ret); } /* * mapper_close: * simply close a mapper. (without handling hash) */ static void mapper_close(struct _citrus_mapper *cm) { if (cm->cm_module) { if (cm->cm_ops) { if (cm->cm_closure) (*cm->cm_ops->mo_uninit)(cm); free(cm->cm_ops); } _citrus_unload_module(cm->cm_module); } free(cm->cm_traits); free(cm); } /* * mapper_open: * simply open a mapper. (without handling hash) */ static int mapper_open(struct _citrus_mapper_area *__restrict ma, struct _citrus_mapper * __restrict * __restrict rcm, const char * __restrict module, const char * __restrict variable) { struct _citrus_mapper *cm; _citrus_mapper_getops_t getops; int ret; /* initialize mapper handle */ cm = malloc(sizeof(*cm)); if (!cm) return (errno); cm->cm_module = NULL; cm->cm_ops = NULL; cm->cm_closure = NULL; cm->cm_traits = NULL; cm->cm_refcount = 0; cm->cm_key = NULL; /* load module */ ret = _citrus_load_module(&cm->cm_module, module); if (ret) goto err; /* get operators */ getops = (_citrus_mapper_getops_t) _citrus_find_getops(cm->cm_module, module, "mapper"); if (!getops) { ret = EOPNOTSUPP; goto err; } cm->cm_ops = malloc(sizeof(*cm->cm_ops)); if (!cm->cm_ops) { ret = errno; goto err; } ret = (*getops)(cm->cm_ops); if (ret) goto err; if (!cm->cm_ops->mo_init || !cm->cm_ops->mo_uninit || !cm->cm_ops->mo_convert || !cm->cm_ops->mo_init_state) goto err; /* allocate traits structure */ cm->cm_traits = malloc(sizeof(*cm->cm_traits)); if (cm->cm_traits == NULL) { ret = errno; goto err; } /* initialize the mapper */ ret = (*cm->cm_ops->mo_init)(ma, cm, ma->ma_dir, (const void *)variable, strlen(variable) + 1, cm->cm_traits, sizeof(*cm->cm_traits)); if (ret) goto err; *rcm = cm; return (0); err: mapper_close(cm); return (ret); } /* * _citrus_mapper_open_direct: * open a mapper. */ int _citrus_mapper_open_direct(struct _citrus_mapper_area *__restrict ma, struct _citrus_mapper * __restrict * __restrict rcm, const char * __restrict module, const char * __restrict variable) { return (mapper_open(ma, rcm, module, variable)); } /* * hash_func */ static __inline int hash_func(const char *key) { return (_string_hash_func(key, CM_HASH_SIZE)); } /* * match_func */ static __inline int match_func(struct _citrus_mapper *cm, const char *key) { return (strcmp(cm->cm_key, key)); } /* * _citrus_mapper_open: * open a mapper with looking up "mapper.dir". */ int _citrus_mapper_open(struct _citrus_mapper_area *__restrict ma, struct _citrus_mapper * __restrict * __restrict rcm, const char * __restrict mapname) { struct _citrus_mapper *cm; char linebuf[PATH_MAX]; const char *module, *variable; int hashval, ret; variable = NULL; - WLOCK; + WLOCK(&cm_lock); /* search in the cache */ hashval = hash_func(mapname); _CITRUS_HASH_SEARCH(&ma->ma_cache, cm, cm_entry, match_func, mapname, hashval); if (cm) { /* found */ cm->cm_refcount++; *rcm = cm; ret = 0; goto quit; } /* search mapper entry */ ret = lookup_mapper_entry(ma->ma_dir, mapname, linebuf, (size_t)PATH_MAX, &module, &variable); if (ret) goto quit; /* open mapper */ - UNLOCK; + UNLOCK(&cm_lock); ret = mapper_open(ma, &cm, module, variable); - WLOCK; + WLOCK(&cm_lock); if (ret) goto quit; cm->cm_key = strdup(mapname); if (cm->cm_key == NULL) { ret = errno; _mapper_close(cm); goto quit; } /* insert to the cache */ cm->cm_refcount = 1; _CITRUS_HASH_INSERT(&ma->ma_cache, cm, cm_entry, hashval); *rcm = cm; ret = 0; quit: - UNLOCK; + UNLOCK(&cm_lock); return (ret); } /* * _citrus_mapper_close: * close the specified mapper. */ void _citrus_mapper_close(struct _citrus_mapper *cm) { if (cm) { - WLOCK; + WLOCK(&cm_lock); if (cm->cm_refcount == REFCOUNT_PERSISTENT) goto quit; if (cm->cm_refcount > 0) { if (--cm->cm_refcount > 0) goto quit; _CITRUS_HASH_REMOVE(cm, cm_entry); free(cm->cm_key); } mapper_close(cm); quit: - UNLOCK; + UNLOCK(&cm_lock); } } /* * _citrus_mapper_set_persistent: * set persistent count. */ void _citrus_mapper_set_persistent(struct _citrus_mapper * __restrict cm) { - WLOCK; + WLOCK(&cm_lock); cm->cm_refcount = REFCOUNT_PERSISTENT; - UNLOCK; + UNLOCK(&cm_lock); }