s390x: Align child stack while clone. [BZ #27968]
[glibc.git] / iconv / gconv_conf.c
blobc8ad8099a42d8f30650eb8a26b2d654db825d840
1 /* Handle configuration data.
2 Copyright (C) 1997-2021 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
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 <https://www.gnu.org/licenses/>. */
20 #include <assert.h>
21 #include <ctype.h>
22 #include <dirent.h>
23 #include <errno.h>
24 #include <limits.h>
25 #include <locale.h>
26 #include <search.h>
27 #include <stddef.h>
28 #include <stdio.h>
29 #include <stdio_ext.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <sys/param.h>
34 #include <sys/types.h>
36 #include <libc-lock.h>
37 #include <gconv_int.h>
40 /* This is the default path where we look for module lists. */
41 static const char default_gconv_path[] = GCONV_PATH;
43 /* Type to represent search path. */
44 struct path_elem
46 const char *name;
47 size_t len;
50 /* The path elements, as determined by the __gconv_get_path function.
51 All path elements end in a slash. */
52 struct path_elem *__gconv_path_elem;
53 /* Maximum length of a single path element in __gconv_path_elem. */
54 size_t __gconv_max_path_elem_len;
56 /* We use the following struct if we couldn't allocate memory. */
57 static const struct path_elem empty_path_elem = { NULL, 0 };
59 /* Name of the file containing the module information in the directories
60 along the path. */
61 static const char gconv_conf_filename[] = "gconv-modules";
62 static const char gconv_conf_dirname[] = "gconv-modules.d";
64 /* Filename extension for the modules. */
65 #ifndef MODULE_EXT
66 # define MODULE_EXT ".so"
67 #endif
68 static const char gconv_module_ext[] = MODULE_EXT;
70 /* We have a few builtin transformations. */
71 static struct gconv_module builtin_modules[] =
73 #define BUILTIN_TRANSFORMATION(From, To, Cost, Name, Fct, BtowcFct, \
74 MinF, MaxF, MinT, MaxT) \
75 { \
76 .from_string = From, \
77 .to_string = To, \
78 .cost_hi = Cost, \
79 .cost_lo = INT_MAX, \
80 .module_name = Name \
82 #define BUILTIN_ALIAS(From, To)
84 #include "gconv_builtin.h"
86 #undef BUILTIN_TRANSFORMATION
87 #undef BUILTIN_ALIAS
90 static const char builtin_aliases[] =
92 #define BUILTIN_TRANSFORMATION(From, To, Cost, Name, Fct, BtowcFct, \
93 MinF, MaxF, MinT, MaxT)
94 #define BUILTIN_ALIAS(From, To) From "\0" To "\0"
96 #include "gconv_builtin.h"
98 #undef BUILTIN_TRANSFORMATION
99 #undef BUILTIN_ALIAS
102 #include <libio/libioP.h>
103 #define __getdelim(line, len, c, fp) _IO_getdelim (line, len, c, fp)
106 /* Value of the GCONV_PATH environment variable. */
107 const char *__gconv_path_envvar;
110 /* Test whether there is already a matching module known. */
111 static int
112 detect_conflict (const char *alias)
114 struct gconv_module *node = __gconv_modules_db;
116 while (node != NULL)
118 int cmpres = strcmp (alias, node->from_string);
120 if (cmpres == 0)
121 /* We have a conflict. */
122 return 1;
123 else if (cmpres < 0)
124 node = node->left;
125 else
126 node = node->right;
129 return node != NULL;
133 /* The actual code to add aliases. */
134 static void
135 add_alias2 (const char *from, const char *to, const char *wp, void *modules)
137 /* Test whether this alias conflicts with any available module. */
138 if (detect_conflict (from))
139 /* It does conflict, don't add the alias. */
140 return;
142 struct gconv_alias *new_alias = (struct gconv_alias *)
143 malloc (sizeof (struct gconv_alias) + (wp - from));
144 if (new_alias != NULL)
146 void **inserted;
148 new_alias->fromname = memcpy ((char *) new_alias
149 + sizeof (struct gconv_alias),
150 from, wp - from);
151 new_alias->toname = new_alias->fromname + (to - from);
153 inserted = (void **) __tsearch (new_alias, &__gconv_alias_db,
154 __gconv_alias_compare);
155 if (inserted == NULL || *inserted != new_alias)
156 /* Something went wrong, free this entry. */
157 free (new_alias);
162 /* Add new alias. */
163 static void
164 add_alias (char *rp, void *modules)
166 /* We now expect two more string. The strings are normalized
167 (converted to UPPER case) and strored in the alias database. */
168 char *from, *to, *wp;
170 while (__isspace_l (*rp, _nl_C_locobj_ptr))
171 ++rp;
172 from = wp = rp;
173 while (*rp != '\0' && !__isspace_l (*rp, _nl_C_locobj_ptr))
174 *wp++ = __toupper_l (*rp++, _nl_C_locobj_ptr);
175 if (*rp == '\0')
176 /* There is no `to' string on the line. Ignore it. */
177 return;
178 *wp++ = '\0';
179 to = ++rp;
180 while (__isspace_l (*rp, _nl_C_locobj_ptr))
181 ++rp;
182 while (*rp != '\0' && !__isspace_l (*rp, _nl_C_locobj_ptr))
183 *wp++ = __toupper_l (*rp++, _nl_C_locobj_ptr);
184 if (to == wp)
185 /* No `to' string, ignore the line. */
186 return;
187 *wp++ = '\0';
189 add_alias2 (from, to, wp, modules);
193 /* Insert a data structure for a new module in the search tree. */
194 static void
195 insert_module (struct gconv_module *newp, int tobefreed)
197 struct gconv_module **rootp = &__gconv_modules_db;
199 while (*rootp != NULL)
201 struct gconv_module *root = *rootp;
202 int cmpres;
204 cmpres = strcmp (newp->from_string, root->from_string);
205 if (cmpres == 0)
207 /* Both strings are identical. Insert the string at the
208 end of the `same' list if it is not already there. */
209 while (strcmp (newp->from_string, root->from_string) != 0
210 || strcmp (newp->to_string, root->to_string) != 0)
212 rootp = &root->same;
213 root = *rootp;
214 if (root == NULL)
215 break;
218 if (root != NULL)
220 /* This is a no new conversion. But maybe the cost is
221 better. */
222 if (newp->cost_hi < root->cost_hi
223 || (newp->cost_hi == root->cost_hi
224 && newp->cost_lo < root->cost_lo))
226 newp->left = root->left;
227 newp->right = root->right;
228 newp->same = root->same;
229 *rootp = newp;
231 free (root);
233 else if (tobefreed)
234 free (newp);
235 return;
238 break;
240 else if (cmpres < 0)
241 rootp = &root->left;
242 else
243 rootp = &root->right;
246 /* Plug in the new node here. */
247 *rootp = newp;
251 /* Add new module. */
252 static void
253 add_module (char *rp, const char *directory, size_t dir_len, void **modules,
254 size_t *nmodules, int modcounter)
256 /* We expect now
257 1. `from' name
258 2. `to' name
259 3. filename of the module
260 4. an optional cost value
262 struct gconv_alias fake_alias;
263 struct gconv_module *new_module;
264 char *from, *to, *module, *wp;
265 int need_ext;
266 int cost_hi;
268 while (__isspace_l (*rp, _nl_C_locobj_ptr))
269 ++rp;
270 from = rp;
271 while (*rp != '\0' && !__isspace_l (*rp, _nl_C_locobj_ptr))
273 *rp = __toupper_l (*rp, _nl_C_locobj_ptr);
274 ++rp;
276 if (*rp == '\0')
277 return;
278 *rp++ = '\0';
279 to = wp = rp;
280 while (__isspace_l (*rp, _nl_C_locobj_ptr))
281 ++rp;
282 while (*rp != '\0' && !__isspace_l (*rp, _nl_C_locobj_ptr))
283 *wp++ = __toupper_l (*rp++, _nl_C_locobj_ptr);
284 if (*rp == '\0')
285 return;
286 *wp++ = '\0';
288 ++rp;
289 while (__isspace_l (*rp, _nl_C_locobj_ptr));
290 module = wp;
291 while (*rp != '\0' && !__isspace_l (*rp, _nl_C_locobj_ptr))
292 *wp++ = *rp++;
293 if (*rp == '\0')
295 /* There is no cost, use one by default. */
296 *wp++ = '\0';
297 cost_hi = 1;
299 else
301 /* There might be a cost value. */
302 char *endp;
304 *wp++ = '\0';
305 cost_hi = strtol (rp, &endp, 10);
306 if (rp == endp || cost_hi < 1)
307 /* No useful information. */
308 cost_hi = 1;
311 if (module[0] == '\0')
312 /* No module name given. */
313 return;
314 if (module[0] == '/')
315 dir_len = 0;
317 /* See whether we must add the ending. */
318 need_ext = 0;
319 if (wp - module < (ptrdiff_t) sizeof (gconv_module_ext)
320 || memcmp (wp - sizeof (gconv_module_ext), gconv_module_ext,
321 sizeof (gconv_module_ext)) != 0)
322 /* We must add the module extension. */
323 need_ext = sizeof (gconv_module_ext) - 1;
325 /* See whether we have already an alias with this name defined. */
326 fake_alias.fromname = strndupa (from, to - from);
328 if (__tfind (&fake_alias, &__gconv_alias_db, __gconv_alias_compare) != NULL)
329 /* This module duplicates an alias. */
330 return;
332 new_module = (struct gconv_module *) calloc (1,
333 sizeof (struct gconv_module)
334 + (wp - from)
335 + dir_len + need_ext);
336 if (new_module != NULL)
338 char *tmp;
340 new_module->from_string = tmp = (char *) (new_module + 1);
341 tmp = __mempcpy (tmp, from, to - from);
343 new_module->to_string = tmp;
344 tmp = __mempcpy (tmp, to, module - to);
346 new_module->cost_hi = cost_hi;
347 new_module->cost_lo = modcounter;
349 new_module->module_name = tmp;
351 if (dir_len != 0)
352 tmp = __mempcpy (tmp, directory, dir_len);
354 tmp = __mempcpy (tmp, module, wp - module);
356 if (need_ext)
357 memcpy (tmp - 1, gconv_module_ext, sizeof (gconv_module_ext));
359 /* Now insert the new module data structure in our search tree. */
360 insert_module (new_module, 1);
365 /* Read the next configuration file. */
366 static void
367 read_conf_file (const char *filename, const char *directory, size_t dir_len,
368 void **modules, size_t *nmodules)
370 /* Note the file is opened with cancellation in the I/O functions
371 disabled. */
372 FILE *fp = fopen (filename, "rce");
373 char *line = NULL;
374 size_t line_len = 0;
375 static int modcounter;
377 /* Don't complain if a file is not present or readable, simply silently
378 ignore it. */
379 if (fp == NULL)
380 return;
382 /* No threads reading from this stream. */
383 __fsetlocking (fp, FSETLOCKING_BYCALLER);
385 /* Process the known entries of the file. Comments start with `#' and
386 end with the end of the line. Empty lines are ignored. */
387 while (!__feof_unlocked (fp))
389 char *rp, *endp, *word;
390 ssize_t n = __getdelim (&line, &line_len, '\n', fp);
391 if (n < 0)
392 /* An error occurred. */
393 break;
395 rp = line;
396 /* Terminate the line (excluding comments or newline) by an NUL byte
397 to simplify the following code. */
398 endp = strchr (rp, '#');
399 if (endp != NULL)
400 *endp = '\0';
401 else
402 if (rp[n - 1] == '\n')
403 rp[n - 1] = '\0';
405 while (__isspace_l (*rp, _nl_C_locobj_ptr))
406 ++rp;
408 /* If this is an empty line go on with the next one. */
409 if (rp == endp)
410 continue;
412 word = rp;
413 while (*rp != '\0' && !__isspace_l (*rp, _nl_C_locobj_ptr))
414 ++rp;
416 if (rp - word == sizeof ("alias") - 1
417 && memcmp (word, "alias", sizeof ("alias") - 1) == 0)
418 add_alias (rp, *modules);
419 else if (rp - word == sizeof ("module") - 1
420 && memcmp (word, "module", sizeof ("module") - 1) == 0)
421 add_module (rp, directory, dir_len, modules, nmodules, modcounter++);
422 /* else */
423 /* Otherwise ignore the line. */
426 free (line);
428 fclose (fp);
432 /* Determine the directories we are looking for data in. This function should
433 only be called from __gconv_read_conf. */
434 static void
435 __gconv_get_path (void)
437 struct path_elem *result;
439 /* This function is only ever called when __gconv_path_elem is NULL. */
440 result = __gconv_path_elem;
441 assert (result == NULL);
443 /* Determine the complete path first. */
444 char *gconv_path;
445 size_t gconv_path_len;
446 char *elem;
447 char *oldp;
448 char *cp;
449 int nelems;
450 char *cwd;
451 size_t cwdlen;
453 if (__gconv_path_envvar == NULL)
455 /* No user-defined path. Make a modifiable copy of the
456 default path. */
457 gconv_path = strdupa (default_gconv_path);
458 gconv_path_len = sizeof (default_gconv_path);
459 cwd = NULL;
460 cwdlen = 0;
462 else
464 /* Append the default path to the user-defined path. */
465 size_t user_len = strlen (__gconv_path_envvar);
467 gconv_path_len = user_len + 1 + sizeof (default_gconv_path);
468 gconv_path = alloca (gconv_path_len);
469 __mempcpy (__mempcpy (__mempcpy (gconv_path, __gconv_path_envvar,
470 user_len),
471 ":", 1),
472 default_gconv_path, sizeof (default_gconv_path));
473 cwd = __getcwd (NULL, 0);
474 cwdlen = __glibc_unlikely (cwd == NULL) ? 0 : strlen (cwd);
476 assert (default_gconv_path[0] == '/');
478 /* In a first pass we calculate the number of elements. */
479 oldp = NULL;
480 cp = strchr (gconv_path, ':');
481 nelems = 1;
482 while (cp != NULL)
484 if (cp != oldp + 1)
485 ++nelems;
486 oldp = cp;
487 cp = strchr (cp + 1, ':');
490 /* Allocate the memory for the result. */
491 result = malloc ((nelems + 1)
492 * sizeof (struct path_elem)
493 + gconv_path_len + nelems
494 + (nelems - 1) * (cwdlen + 1));
495 if (result != NULL)
497 char *strspace = (char *) &result[nelems + 1];
498 int n = 0;
500 /* Separate the individual parts. */
501 __gconv_max_path_elem_len = 0;
502 elem = __strtok_r (gconv_path, ":", &gconv_path);
503 assert (elem != NULL);
506 result[n].name = strspace;
507 if (elem[0] != '/')
509 assert (cwd != NULL);
510 strspace = __mempcpy (strspace, cwd, cwdlen);
511 *strspace++ = '/';
513 strspace = __stpcpy (strspace, elem);
514 if (strspace[-1] != '/')
515 *strspace++ = '/';
517 result[n].len = strspace - result[n].name;
518 if (result[n].len > __gconv_max_path_elem_len)
519 __gconv_max_path_elem_len = result[n].len;
521 *strspace++ = '\0';
522 ++n;
524 while ((elem = __strtok_r (NULL, ":", &gconv_path)) != NULL);
526 result[n].name = NULL;
527 result[n].len = 0;
530 __gconv_path_elem = result ?: (struct path_elem *) &empty_path_elem;
532 free (cwd);
536 /* Read all configuration files found in the user-specified and the default
537 path. This function should only be called once during the program's
538 lifetime. It disregards locking and synchronization because its only
539 caller, __gconv_load_conf, handles this. */
540 static void
541 __gconv_read_conf (void)
543 void *modules = NULL;
544 size_t nmodules = 0;
545 int save_errno = errno;
546 size_t cnt;
548 /* First see whether we should use the cache. */
549 if (__gconv_load_cache () == 0)
551 /* Yes, we are done. */
552 __set_errno (save_errno);
553 return;
556 #ifndef STATIC_GCONV
557 /* Find out where we have to look. */
558 __gconv_get_path ();
560 for (cnt = 0; __gconv_path_elem[cnt].name != NULL; ++cnt)
562 #define BUF_LEN elem_len + sizeof (gconv_conf_dirname)
564 const char *elem = __gconv_path_elem[cnt].name;
565 size_t elem_len = __gconv_path_elem[cnt].len;
566 char *buf;
568 /* No slash needs to be inserted between elem and gconv_conf_filename;
569 elem already ends in a slash. */
570 buf = alloca (BUF_LEN);
571 char *cp = __mempcpy (__mempcpy (buf, elem, elem_len),
572 gconv_conf_filename, sizeof (gconv_conf_filename));
574 /* Read the gconv-modules configuration file first. */
575 read_conf_file (buf, elem, elem_len, &modules, &nmodules);
577 /* Next, see if there is a gconv-modules.d directory containing
578 configuration files and if it is non-empty. */
579 cp--;
580 cp[0] = '.';
581 cp[1] = 'd';
582 cp[2] = '\0';
584 DIR *confdir = __opendir (buf);
585 if (confdir != NULL)
587 struct dirent *ent;
588 while ((ent = __readdir (confdir)) != NULL)
590 if (ent->d_type != DT_REG)
591 continue;
593 size_t len = strlen (ent->d_name);
594 const char *suffix = ".conf";
596 if (len > strlen (suffix)
597 && strcmp (ent->d_name + len - strlen (suffix), suffix) == 0)
599 /* LEN <= PATH_MAX so this alloca is not unbounded. */
600 char *conf = alloca (BUF_LEN + len + 1);
601 cp = stpcpy (conf, buf);
602 sprintf (cp, "/%s", ent->d_name);
603 read_conf_file (conf, elem, elem_len, &modules, &nmodules);
606 __closedir (confdir);
609 #endif
611 /* Add the internal modules. */
612 for (cnt = 0; cnt < sizeof (builtin_modules) / sizeof (builtin_modules[0]);
613 ++cnt)
615 struct gconv_alias fake_alias;
617 fake_alias.fromname = (char *) builtin_modules[cnt].from_string;
619 if (__tfind (&fake_alias, &__gconv_alias_db, __gconv_alias_compare)
620 != NULL)
621 /* It'll conflict so don't add it. */
622 continue;
624 insert_module (&builtin_modules[cnt], 0);
627 /* Add aliases for builtin conversions. */
628 const char *cp = builtin_aliases;
631 const char *from = cp;
632 const char *to = __rawmemchr (from, '\0') + 1;
633 cp = __rawmemchr (to, '\0') + 1;
635 add_alias2 (from, to, cp, modules);
637 while (*cp != '\0');
639 /* Restore the error number. */
640 __set_errno (save_errno);
644 /* This "once" variable is used to do a one-time load of the configuration. */
645 __libc_once_define (static, once);
648 /* Read all configuration files found in the user-specified and the default
649 path, but do it only "once" using __gconv_read_conf to do the actual
650 work. This is the function that must be called when reading iconv
651 configuration. */
652 void
653 __gconv_load_conf (void)
655 __libc_once (once, __gconv_read_conf);
659 /* Free all resources if necessary. */
660 libc_freeres_fn (free_mem)
662 if (__gconv_path_elem != NULL && __gconv_path_elem != &empty_path_elem)
663 free ((void *) __gconv_path_elem);