openpty: use TIOCGPTPEER to open slave side fd
[glibc.git] / locale / loadlocale.c
blob2bdb39b4b8873ac1a759b20c0d936a7eda3fb4e7
1 /* Functions to read locale data files.
2 Copyright (C) 1996-2017 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
20 #include <assert.h>
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <locale.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #ifdef _POSIX_MAPPED_FILES
28 # include <sys/mman.h>
29 #endif
30 #include <sys/stat.h>
32 #include <not-cancel.h>
33 #include "localeinfo.h"
36 static const size_t _nl_category_num_items[] =
38 #define DEFINE_CATEGORY(category, category_name, items, a) \
39 [category] = _NL_ITEM_INDEX (_NL_NUM_##category),
40 #include "categories.def"
41 #undef DEFINE_CATEGORY
45 #define NO_PAREN(arg, rest...) arg, ##rest
47 #define DEFINE_CATEGORY(category, category_name, items, a) \
48 static const enum value_type _nl_value_type_##category[] = { NO_PAREN items };
49 #define DEFINE_ELEMENT(element, element_name, optstd, type, rest...) \
50 [_NL_ITEM_INDEX (element)] = type,
51 #include "categories.def"
52 #undef DEFINE_CATEGORY
54 static const enum value_type *const _nl_value_types[] =
56 #define DEFINE_CATEGORY(category, category_name, items, a) \
57 [category] = _nl_value_type_##category,
58 #include "categories.def"
59 #undef DEFINE_CATEGORY
63 struct __locale_data *
64 _nl_intern_locale_data (int category, const void *data, size_t datasize)
66 const struct
68 unsigned int magic;
69 unsigned int nstrings;
70 unsigned int strindex[0];
71 } *const filedata = data;
72 struct __locale_data *newdata;
73 size_t cnt;
75 if (__builtin_expect (datasize < sizeof *filedata, 0)
76 || __builtin_expect (filedata->magic != LIMAGIC (category), 0))
78 /* Bad data file. */
79 __set_errno (EINVAL);
80 return NULL;
83 if (__builtin_expect (filedata->nstrings < _nl_category_num_items[category],
85 || (__builtin_expect (sizeof *filedata
86 + filedata->nstrings * sizeof (unsigned int)
87 >= datasize, 0)))
89 /* Insufficient data. */
90 __set_errno (EINVAL);
91 return NULL;
94 newdata = malloc (sizeof *newdata
95 + filedata->nstrings * sizeof (union locale_data_value));
96 if (newdata == NULL)
97 return NULL;
99 newdata->filedata = (void *) filedata;
100 newdata->filesize = datasize;
101 newdata->private.data = NULL;
102 newdata->private.cleanup = NULL;
103 newdata->usage_count = 0;
104 newdata->use_translit = 0;
105 newdata->nstrings = filedata->nstrings;
106 for (cnt = 0; cnt < newdata->nstrings; ++cnt)
108 size_t idx = filedata->strindex[cnt];
109 if (__glibc_unlikely (idx > (size_t) newdata->filesize))
111 puntdata:
112 free (newdata);
113 __set_errno (EINVAL);
114 return NULL;
117 /* Determine the type. There is one special case: the LC_CTYPE
118 category can have more elements than there are in the
119 _nl_value_type_LC_XYZ array. There are all pointers. */
120 switch (category)
122 #define CATTEST(cat) \
123 case LC_##cat: \
124 if (cnt >= (sizeof (_nl_value_type_LC_##cat) \
125 / sizeof (_nl_value_type_LC_##cat[0]))) \
126 goto puntdata; \
127 break
128 CATTEST (NUMERIC);
129 CATTEST (TIME);
130 CATTEST (COLLATE);
131 CATTEST (MONETARY);
132 CATTEST (MESSAGES);
133 CATTEST (PAPER);
134 CATTEST (NAME);
135 CATTEST (ADDRESS);
136 CATTEST (TELEPHONE);
137 CATTEST (MEASUREMENT);
138 CATTEST (IDENTIFICATION);
139 default:
140 assert (category == LC_CTYPE);
141 break;
144 if ((category == LC_CTYPE
145 && cnt >= (sizeof (_nl_value_type_LC_CTYPE)
146 / sizeof (_nl_value_type_LC_CTYPE[0])))
147 || __builtin_expect (_nl_value_types[category][cnt] != word, 1))
148 newdata->values[cnt].string = newdata->filedata + idx;
149 else
151 if (!LOCFILE_ALIGNED_P (idx))
152 goto puntdata;
153 newdata->values[cnt].word =
154 *((const uint32_t *) (newdata->filedata + idx));
158 return newdata;
161 void
162 _nl_load_locale (struct loaded_l10nfile *file, int category)
164 int fd;
165 void *filedata;
166 struct stat64 st;
167 struct __locale_data *newdata;
168 int save_err;
169 int alloc = ld_mapped;
171 file->decided = 1;
172 file->data = NULL;
174 fd = __open_nocancel (file->filename, O_RDONLY | O_CLOEXEC);
175 if (__builtin_expect (fd, 0) < 0)
176 /* Cannot open the file. */
177 return;
179 if (__builtin_expect (__fxstat64 (_STAT_VER, fd, &st), 0) < 0)
181 puntfd:
182 __close_nocancel_nostatus (fd);
183 return;
185 if (__glibc_unlikely (S_ISDIR (st.st_mode)))
187 /* LOCALE/LC_foo is a directory; open LOCALE/LC_foo/SYS_LC_foo
188 instead. */
189 char *newp;
190 size_t filenamelen;
192 __close_nocancel_nostatus (fd);
194 filenamelen = strlen (file->filename);
195 newp = (char *) alloca (filenamelen
196 + 5 + _nl_category_name_sizes[category] + 1);
197 __mempcpy (__mempcpy (__mempcpy (newp, file->filename, filenamelen),
198 "/SYS_", 5),
199 _nl_category_names.str + _nl_category_name_idxs[category],
200 _nl_category_name_sizes[category] + 1);
202 fd = __open_nocancel (newp, O_RDONLY | O_CLOEXEC);
203 if (__builtin_expect (fd, 0) < 0)
204 return;
206 if (__builtin_expect (__fxstat64 (_STAT_VER, fd, &st), 0) < 0)
207 goto puntfd;
210 /* Map in the file's data. */
211 save_err = errno;
212 #ifdef _POSIX_MAPPED_FILES
213 # ifndef MAP_COPY
214 /* Linux seems to lack read-only copy-on-write. */
215 # define MAP_COPY MAP_PRIVATE
216 # endif
217 # ifndef MAP_FILE
218 /* Some systems do not have this flag; it is superfluous. */
219 # define MAP_FILE 0
220 # endif
221 filedata = __mmap ((caddr_t) 0, st.st_size,
222 PROT_READ, MAP_FILE|MAP_COPY, fd, 0);
223 if (__glibc_unlikely (filedata == MAP_FAILED))
225 filedata = NULL;
226 if (__builtin_expect (errno, ENOSYS) == ENOSYS)
228 #endif /* _POSIX_MAPPED_FILES */
229 /* No mmap; allocate a buffer and read from the file. */
230 alloc = ld_malloced;
231 filedata = malloc (st.st_size);
232 if (filedata != NULL)
234 off_t to_read = st.st_size;
235 ssize_t nread;
236 char *p = (char *) filedata;
237 while (to_read > 0)
239 nread = __read_nocancel (fd, p, to_read);
240 if (__builtin_expect (nread, 1) <= 0)
242 free (filedata);
243 if (nread == 0)
244 __set_errno (EINVAL); /* Bizarreness going on. */
245 goto puntfd;
247 p += nread;
248 to_read -= nread;
250 __set_errno (save_err);
252 #ifdef _POSIX_MAPPED_FILES
255 #endif /* _POSIX_MAPPED_FILES */
257 /* We have mapped the data, so we no longer need the descriptor. */
258 __close_nocancel_nostatus (fd);
260 if (__glibc_unlikely (filedata == NULL))
261 /* We failed to map or read the data. */
262 return;
264 newdata = _nl_intern_locale_data (category, filedata, st.st_size);
265 if (__glibc_unlikely (newdata == NULL))
266 /* Bad data. */
268 #ifdef _POSIX_MAPPED_FILES
269 if (alloc == ld_mapped)
270 __munmap ((caddr_t) filedata, st.st_size);
271 #endif
272 return;
275 /* _nl_intern_locale_data leaves us these fields to initialize. */
276 newdata->name = NULL; /* This will be filled if necessary in findlocale.c. */
277 newdata->alloc = alloc;
279 file->data = newdata;
282 void
283 _nl_unload_locale (struct __locale_data *locale)
285 if (locale->private.cleanup)
286 (*locale->private.cleanup) (locale);
288 switch (__builtin_expect (locale->alloc, ld_mapped))
290 case ld_malloced:
291 free ((void *) locale->filedata);
292 break;
293 case ld_mapped:
294 #ifdef _POSIX_MAPPED_FILES
295 __munmap ((caddr_t) locale->filedata, locale->filesize);
296 break;
297 #endif
298 case ld_archive: /* Nothing to do. */
299 break;
302 if (__builtin_expect (locale->alloc, ld_mapped) != ld_archive)
303 free ((char *) locale->name);
305 free (locale);