]> git.ipfire.org Git - thirdparty/glibc.git/blob - iconv/gconv_parseconfdir.h
gconv_parseconfdir: Fix memory leak
[thirdparty/glibc.git] / iconv / gconv_parseconfdir.h
1 /* Handle configuration data.
2 Copyright (C) 2021 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19 #include <dirent.h>
20 #include <libc-symbols.h>
21 #include <locale.h>
22 #include <sys/types.h>
23
24 #if IS_IN (libc)
25 # include <libio/libioP.h>
26 # define __getdelim(line, len, c, fp) __getdelim (line, len, c, fp)
27
28 # undef isspace
29 # define isspace(__c) __isspace_l ((__c), _nl_C_locobj_ptr)
30 # define asprintf __asprintf
31 # define opendir __opendir
32 # define readdir __readdir
33 # define closedir __closedir
34 # define mempcpy __mempcpy
35 # define lstat64 __lstat64
36 # define feof_unlocked __feof_unlocked
37 #endif
38
39 /* Name of the file containing the module information in the directories
40 along the path. */
41 static const char gconv_conf_filename[] = "gconv-modules";
42 static const char gconv_conf_dirname[] = "gconv-modules.d";
43
44 static void add_alias (char *);
45 static void add_module (char *, const char *, size_t, int);
46
47 /* Read the next configuration file. */
48 static bool
49 read_conf_file (const char *filename, const char *directory, size_t dir_len)
50 {
51 /* Note the file is opened with cancellation in the I/O functions
52 disabled. */
53 FILE *fp = fopen (filename, "rce");
54 char *line = NULL;
55 size_t line_len = 0;
56 static int modcounter;
57
58 /* Don't complain if a file is not present or readable, simply silently
59 ignore it. */
60 if (fp == NULL)
61 return false;
62
63 /* No threads reading from this stream. */
64 __fsetlocking (fp, FSETLOCKING_BYCALLER);
65
66 /* Process the known entries of the file. Comments start with `#' and
67 end with the end of the line. Empty lines are ignored. */
68 while (!feof_unlocked (fp))
69 {
70 char *rp, *endp, *word;
71 ssize_t n = __getdelim (&line, &line_len, '\n', fp);
72 if (n < 0)
73 /* An error occurred. */
74 break;
75
76 rp = line;
77 /* Terminate the line (excluding comments or newline) by an NUL byte
78 to simplify the following code. */
79 endp = strchr (rp, '#');
80 if (endp != NULL)
81 *endp = '\0';
82 else
83 if (rp[n - 1] == '\n')
84 rp[n - 1] = '\0';
85
86 while (isspace (*rp))
87 ++rp;
88
89 /* If this is an empty line go on with the next one. */
90 if (rp == endp)
91 continue;
92
93 word = rp;
94 while (*rp != '\0' && !isspace (*rp))
95 ++rp;
96
97 if (rp - word == sizeof ("alias") - 1
98 && memcmp (word, "alias", sizeof ("alias") - 1) == 0)
99 add_alias (rp);
100 else if (rp - word == sizeof ("module") - 1
101 && memcmp (word, "module", sizeof ("module") - 1) == 0)
102 add_module (rp, directory, dir_len, modcounter++);
103 /* else */
104 /* Otherwise ignore the line. */
105 }
106
107 free (line);
108
109 fclose (fp);
110 return true;
111 }
112
113 static __always_inline bool
114 gconv_parseconfdir (const char *dir, size_t dir_len)
115 {
116 /* No slash needs to be inserted between dir and gconv_conf_filename;
117 dir already ends in a slash. */
118 char *buf = malloc (dir_len + sizeof (gconv_conf_dirname));
119 bool found = false;
120
121 if (buf == NULL)
122 return false;
123
124 char *cp = mempcpy (mempcpy (buf, dir, dir_len), gconv_conf_filename,
125 sizeof (gconv_conf_filename));
126
127 /* Read the gconv-modules configuration file first. */
128 found = read_conf_file (buf, dir, dir_len);
129
130 /* Next, see if there is a gconv-modules.d directory containing
131 configuration files and if it is non-empty. */
132 cp--;
133 cp[0] = '.';
134 cp[1] = 'd';
135 cp[2] = '\0';
136
137 DIR *confdir = opendir (buf);
138 if (confdir != NULL)
139 {
140 struct dirent *ent;
141 while ((ent = readdir (confdir)) != NULL)
142 {
143 if (ent->d_type != DT_REG && ent->d_type != DT_UNKNOWN)
144 continue;
145
146 size_t len = strlen (ent->d_name);
147 const char *suffix = ".conf";
148
149 if (len > strlen (suffix)
150 && strcmp (ent->d_name + len - strlen (suffix), suffix) == 0)
151 {
152 char *conf;
153 struct stat64 st;
154 if (asprintf (&conf, "%s/%s", buf, ent->d_name) < 0)
155 continue;
156
157 if (ent->d_type != DT_UNKNOWN
158 || (lstat64 (conf, &st) != -1 && S_ISREG (st.st_mode)))
159 found |= read_conf_file (conf, dir, dir_len);
160
161 free (conf);
162 }
163 }
164 closedir (confdir);
165 }
166 free (buf);
167 return found;
168 }