diff --git a/lib/libc/nls/catopen.3 b/lib/libc/nls/catopen.3 index 6fd50956be1f..6fb2e2522e85 100644 --- a/lib/libc/nls/catopen.3 +++ b/lib/libc/nls/catopen.3 @@ -1,155 +1,156 @@ .\" Copyright (c) 1994 Winning Strategies, Inc. .\" 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. .\" 3. All advertising materials mentioning features or use of this software .\" must display the following acknowledgement: .\" This product includes software developed by Winning Strategies, Inc. .\" 4. The name of the author may not be used to endorse or promote products .\" derived from this software without specific prior written permission. .\" .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 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. .Dd February 12, 2005 .Dt CATOPEN 3 .Os .Sh NAME .Nm catopen .Nd open message catalog .Sh LIBRARY .Lb libc .Sh SYNOPSIS .In nl_types.h .Ft nl_catd .Fn catopen "const char *name" "int oflag" .Sh DESCRIPTION The .Fn catopen function opens the message catalog specified by .Fa name and returns a message catalog descriptor. If .Fa name contains a .Sq / then .Fa name specifies the full pathname for the message catalog, otherwise the value of the environment variable .Ev NLSPATH is used with the following substitutions: .Bl -tag -width XXX .It \&%N The value of the .Fa name argument. .It \&%L The value of the .Ev LANG environment variable or the .Dv LC_MESSAGES category (see below). .It \&%l The language element from the .Ev LANG environment variable or from the .Dv LC_MESSAGES category. .It \&%t The territory element from the .Ev LANG environment variable or from the .Dv LC_MESSAGES category. .It \&%c The codeset element from the .Ev LANG environment variable or from the .Dv LC_MESSAGES category. .It \&%% A single % character. .El .Pp An empty string is substituted for undefined values. .Pp Path names templates defined in .Ev NLSPATH are separated by colons .Pq Sq \&: . A leading or two adjacent colons is equivalent to specifying %N. .Pp If the .Fa oflag argument is set to the .Dv NL_CAT_LOCALE constant, .Dv LC_MESSAGES locale category used to open the message catalog; using .Dv NL_CAT_LOCALE conforms to the .St -xpg4 standard. You can specify 0 for compatibility with .St -xpg3 ; when .Fa oflag is set to 0, the .Ev LANG environment variable determines the message catalog locale. .Pp A message catalog descriptor remains valid in a process until that process closes it, or until a successful call to one of the .Xr exec 3 function. .Sh RETURN VALUES Upon successful completion, .Fn catopen returns a message catalog descriptor. Otherwise, (nl_catd) -1 is returned and .Va errno is set to indicate the error. .Sh ERRORS .Bl -tag -width Er -.It Bq Er EINVAL -Argument -.Fa name -does not point to a valid message catalog, or catalog is corrupt. .It Bq Er ENAMETOOLONG An entire path to the message catalog exceeded 1024 characters. .It Bq Er ENOENT -The named message catalog does not exists, or the +Argument .Fa name -argument points to an empty string. +does not point to a valid message catalog name, +or it points to an empty string. +.It Bq Er ENOENT +The named message catalog does not exist. +.It Bq Er ENOENT +The named message catalog file is in wrong format. .It Bq Er ENOMEM Insufficient memory is available. .El .Sh SEE ALSO .Xr gencat 1 , .Xr catclose 3 , .Xr catgets 3 , .Xr setlocale 3 .Sh STANDARDS The .Fn catopen function conforms to .St -p1003.1-2001 . diff --git a/lib/libc/nls/msgcat.c b/lib/libc/nls/msgcat.c index 32fa191c4c67..61c09b65f434 100644 --- a/lib/libc/nls/msgcat.c +++ b/lib/libc/nls/msgcat.c @@ -1,481 +1,489 @@ /*********************************************************** Copyright 1990, by Alfalfa Software Incorporated, Cambridge, Massachusetts. Copyright 2010, Gabor Kovesdan All Rights Reserved Permission to use, copy, modify, and distribute this software and its documentation for any purpose and without fee is hereby granted, provided that the above copyright notice appear in all copies and that both that copyright notice and this permission notice appear in supporting documentation, and that Alfalfa's name not be used in advertising or publicity pertaining to distribution of the software without specific, written prior permission. ALPHALPHA DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL ALPHALPHA BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. If you make any modifications, bugfixes or other changes to this software we'd appreciate it if you could send a copy to us so we can keep things up-to-date. Many thanks. Kee Hinckley Alfalfa Software, Inc. 267 Allston St., #3 Cambridge, MA 02139 USA nazgul@alfalfa.com ******************************************************************/ #define _NLS_PRIVATE #include "namespace.h" #include #include #include #include #include /* for ntohl() */ #include #include #include #include #include #include #include #include #include #include #include #include "un-namespace.h" #include "../locale/xlocale_private.h" #include "libc_private.h" #define _DEFAULT_NLS_PATH "/usr/share/nls/%L/%N.cat:/usr/share/nls/%N/%L:" \ _PATH_LOCALBASE "/share/nls/%L/%N.cat:" \ _PATH_LOCALBASE "/share/nls/%N/%L" #define RLOCK(fail) { int ret; \ if (__isthreaded && \ ((ret = _pthread_rwlock_rdlock(&rwlock)) != 0)) { \ errno = ret; \ return (fail); \ }} #define WLOCK(fail) { int ret; \ if (__isthreaded && \ ((ret = _pthread_rwlock_wrlock(&rwlock)) != 0)) { \ errno = ret; \ return (fail); \ }} #define UNLOCK { if (__isthreaded) \ _pthread_rwlock_unlock(&rwlock); } #define NLERR ((nl_catd) -1) #define NLRETERR(errc) { errno = errc; return (NLERR); } #define SAVEFAIL(n, l, e) { np = calloc(1, sizeof(struct catentry)); \ if (np != NULL) { \ np->name = strdup(n); \ np->catd = NLERR; \ np->lang = (l == NULL) ? NULL : \ strdup(l); \ np->caterrno = e; \ if (np->name == NULL || \ (l != NULL && np->lang == NULL)) { \ free(np->name); \ free(np->lang); \ free(np); \ } else { \ WLOCK(NLERR); \ SLIST_INSERT_HEAD(&cache, np, \ list); \ UNLOCK; \ } \ } \ errno = e; \ } static nl_catd load_msgcat(const char *, const char *, const char *); static pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER; struct catentry { SLIST_ENTRY(catentry) list; char *name; char *path; int caterrno; nl_catd catd; char *lang; int refcount; }; SLIST_HEAD(listhead, catentry) cache = SLIST_HEAD_INITIALIZER(cache); nl_catd catopen(const char *name, int type) { return (__catopen_l(name, type, __get_locale())); } nl_catd __catopen_l(const char *name, int type, locale_t locale) { struct stat sbuf; struct catentry *np; char *base, *cptr, *cptr1, *nlspath, *pathP, *pcode; char *plang, *pter; int saverr, spcleft; const char *lang, *tmpptr; char path[PATH_MAX]; /* sanity checking */ if (name == NULL || *name == '\0') - NLRETERR(EINVAL); + NLRETERR(ENOENT); if (strchr(name, '/') != NULL) /* have a pathname */ lang = NULL; else { if (type == NL_CAT_LOCALE) lang = querylocale(LC_MESSAGES_MASK, locale); else lang = getenv("LANG"); if (lang == NULL || *lang == '\0' || strlen(lang) > ENCODING_LEN || (lang[0] == '.' && (lang[1] == '\0' || (lang[1] == '.' && lang[2] == '\0'))) || strchr(lang, '/') != NULL) lang = "C"; } /* Try to get it from the cache first */ RLOCK(NLERR); SLIST_FOREACH(np, &cache, list) { if ((strcmp(np->name, name) == 0) && ((lang != NULL && np->lang != NULL && strcmp(np->lang, lang) == 0) || (np->lang == lang))) { if (np->caterrno != 0) { /* Found cached failing entry */ UNLOCK; NLRETERR(np->caterrno); } else { /* Found cached successful entry */ atomic_add_int(&np->refcount, 1); UNLOCK; return (np->catd); } } } UNLOCK; /* is it absolute path ? if yes, load immediately */ if (strchr(name, '/') != NULL) return (load_msgcat(name, name, lang)); /* sanity checking */ if ((plang = cptr1 = strdup(lang)) == NULL) return (NLERR); if ((cptr = strchr(cptr1, '@')) != NULL) *cptr = '\0'; pter = pcode = ""; if ((cptr = strchr(cptr1, '_')) != NULL) { *cptr++ = '\0'; pter = cptr1 = cptr; } if ((cptr = strchr(cptr1, '.')) != NULL) { *cptr++ = '\0'; pcode = cptr; } if ((nlspath = secure_getenv("NLSPATH")) == NULL) nlspath = _DEFAULT_NLS_PATH; if ((base = cptr = strdup(nlspath)) == NULL) { saverr = errno; free(plang); errno = saverr; return (NLERR); } while ((nlspath = strsep(&cptr, ":")) != NULL) { pathP = path; if (*nlspath) { for (; *nlspath; ++nlspath) { if (*nlspath == '%') { switch (*(nlspath + 1)) { case 'l': tmpptr = plang; break; case 't': tmpptr = pter; break; case 'c': tmpptr = pcode; break; case 'L': tmpptr = lang; break; case 'N': tmpptr = (char *)name; break; case '%': ++nlspath; /* FALLTHROUGH */ default: if (pathP - path >= sizeof(path) - 1) goto too_long; *(pathP++) = *nlspath; continue; } ++nlspath; put_tmpptr: spcleft = sizeof(path) - (pathP - path) - 1; if (strlcpy(pathP, tmpptr, spcleft) >= spcleft) { too_long: free(plang); free(base); SAVEFAIL(name, lang, ENAMETOOLONG); NLRETERR(ENAMETOOLONG); } pathP += strlen(tmpptr); } else { if (pathP - path >= sizeof(path) - 1) goto too_long; *(pathP++) = *nlspath; } } *pathP = '\0'; if (stat(path, &sbuf) == 0) { free(plang); free(base); return (load_msgcat(path, name, lang)); } } else { tmpptr = (char *)name; --nlspath; goto put_tmpptr; } } free(plang); free(base); SAVEFAIL(name, lang, ENOENT); NLRETERR(ENOENT); } char * catgets(nl_catd catd, int set_id, int msg_id, const char *s) { struct _nls_cat_hdr *cat_hdr; struct _nls_msg_hdr *msg_hdr; struct _nls_set_hdr *set_hdr; int i, l, r, u; if (catd == NULL || catd == NLERR) { errno = EBADF; /* LINTED interface problem */ return ((char *)s); } cat_hdr = (struct _nls_cat_hdr *)catd->__data; set_hdr = (struct _nls_set_hdr *)(void *)((char *)catd->__data + sizeof(struct _nls_cat_hdr)); /* binary search, see knuth algorithm b */ l = 0; u = ntohl((u_int32_t)cat_hdr->__nsets) - 1; while (l <= u) { i = (l + u) / 2; r = set_id - ntohl((u_int32_t)set_hdr[i].__setno); if (r == 0) { msg_hdr = (struct _nls_msg_hdr *) (void *)((char *)catd->__data + sizeof(struct _nls_cat_hdr) + ntohl((u_int32_t)cat_hdr->__msg_hdr_offset)); l = ntohl((u_int32_t)set_hdr[i].__index); u = l + ntohl((u_int32_t)set_hdr[i].__nmsgs) - 1; while (l <= u) { i = (l + u) / 2; r = msg_id - ntohl((u_int32_t)msg_hdr[i].__msgno); if (r == 0) { return ((char *) catd->__data + sizeof(struct _nls_cat_hdr) + ntohl((u_int32_t) cat_hdr->__msg_txt_offset) + ntohl((u_int32_t) msg_hdr[i].__offset)); } else if (r < 0) { u = i - 1; } else { l = i + 1; } } /* not found */ goto notfound; } else if (r < 0) { u = i - 1; } else { l = i + 1; } } notfound: /* not found */ errno = ENOMSG; /* LINTED interface problem */ return ((char *)s); } static void catfree(struct catentry *np) { if (np->catd != NULL && np->catd != NLERR) { munmap(np->catd->__data, (size_t)np->catd->__size); free(np->catd); } SLIST_REMOVE(&cache, np, catentry, list); free(np->name); free(np->path); free(np->lang); free(np); } int catclose(nl_catd catd) { struct catentry *np; /* sanity checking */ if (catd == NULL || catd == NLERR) { errno = EBADF; return (-1); } /* Remove from cache if not referenced any more */ WLOCK(-1); SLIST_FOREACH(np, &cache, list) { if (catd == np->catd) { if (atomic_fetchadd_int(&np->refcount, -1) == 1) catfree(np); break; } } UNLOCK; return (0); } /* * Internal support functions */ static nl_catd load_msgcat(const char *path, const char *name, const char *lang) { struct stat st; nl_catd catd; struct catentry *np; void *data; char *copy_path, *copy_name, *copy_lang; - int fd; + int fd, saved_errno; /* path/name will never be NULL here */ /* * One more try in cache; if it was not found by name, * it might still be found by absolute path. */ RLOCK(NLERR); SLIST_FOREACH(np, &cache, list) { if ((np->path != NULL) && (strcmp(np->path, path) == 0)) { atomic_add_int(&np->refcount, 1); UNLOCK; return (np->catd); } } UNLOCK; if ((fd = _open(path, O_RDONLY | O_CLOEXEC)) == -1) { SAVEFAIL(name, lang, errno); NLRETERR(errno); } if (_fstat(fd, &st) != 0) { + saved_errno = errno; _close(fd); - SAVEFAIL(name, lang, EFTYPE); - NLRETERR(EFTYPE); + SAVEFAIL(name, lang, saved_errno); + NLRETERR(saved_errno); + } + + /* The file is too small to contain a _NLS_MAGIC. */ + if (st.st_size < sizeof(u_int32_t)) { + _close(fd); + SAVEFAIL(name, lang, ENOENT); + NLRETERR(ENOENT); } /* * If the file size cannot be held in size_t we cannot mmap() * it to the memory. Probably, this will not be a problem given * that catalog files are usually small. */ if (st.st_size > SIZE_T_MAX) { _close(fd); - SAVEFAIL(name, lang, EFBIG); - NLRETERR(EFBIG); + SAVEFAIL(name, lang, ENOENT); + NLRETERR(ENOENT); } - if ((data = mmap(0, (size_t)st.st_size, PROT_READ, - MAP_FILE|MAP_SHARED, fd, (off_t)0)) == MAP_FAILED) { - int saved_errno = errno; + data = mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0); + if (data == MAP_FAILED) { + saved_errno = errno; _close(fd); SAVEFAIL(name, lang, saved_errno); NLRETERR(saved_errno); } _close(fd); if (ntohl((u_int32_t)((struct _nls_cat_hdr *)data)->__magic) != _NLS_MAGIC) { munmap(data, (size_t)st.st_size); - SAVEFAIL(name, lang, EFTYPE); - NLRETERR(EFTYPE); + SAVEFAIL(name, lang, ENOENT); + NLRETERR(ENOENT); } copy_name = strdup(name); copy_path = strdup(path); copy_lang = (lang == NULL) ? NULL : strdup(lang); catd = malloc(sizeof (*catd)); np = calloc(1, sizeof(struct catentry)); if (copy_name == NULL || copy_path == NULL || (lang != NULL && copy_lang == NULL) || catd == NULL || np == NULL) { free(copy_name); free(copy_path); free(copy_lang); free(catd); free(np); munmap(data, (size_t)st.st_size); SAVEFAIL(name, lang, ENOMEM); NLRETERR(ENOMEM); } catd->__data = data; catd->__size = (int)st.st_size; /* Caching opened catalog */ np->name = copy_name; np->path = copy_path; np->catd = catd; np->lang = copy_lang; atomic_store_int(&np->refcount, 1); WLOCK(NLERR); SLIST_INSERT_HEAD(&cache, np, list); UNLOCK; return (catd); }