lift child restrictions after multi-threaded fork
[musl] / src / locale / dcngettext.c
1 #include <libintl.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <errno.h>
5 #include <limits.h>
6 #include <sys/stat.h>
7 #include <sys/mman.h>
8 #include <ctype.h>
9 #include "locale_impl.h"
10 #include "atomic.h"
11 #include "pleval.h"
12 #include "lock.h"
13 #include "fork_impl.h"
14
15 #define malloc __libc_malloc
16 #define calloc __libc_calloc
17 #define realloc undef
18 #define free undef
19
20 struct binding {
21         struct binding *next;
22         int dirlen;
23         volatile int active;
24         char *domainname;
25         char *dirname;
26         char buf[];
27 };
28
29 static void *volatile bindings;
30
31 static char *gettextdir(const char *domainname, size_t *dirlen)
32 {
33         struct binding *p;
34         for (p=bindings; p; p=p->next) {
35                 if (!strcmp(p->domainname, domainname) && p->active) {
36                         *dirlen = p->dirlen;
37                         return (char *)p->dirname;
38                 }
39         }
40         return 0;
41 }
42
43 static volatile int lock[1];
44 volatile int *const __gettext_lockptr = lock;
45
46 char *bindtextdomain(const char *domainname, const char *dirname)
47 {
48         struct binding *p, *q;
49
50         if (!domainname) return 0;
51         if (!dirname) return gettextdir(domainname, &(size_t){0});
52
53         size_t domlen = strnlen(domainname, NAME_MAX+1);
54         size_t dirlen = strnlen(dirname, PATH_MAX);
55         if (domlen > NAME_MAX || dirlen >= PATH_MAX) {
56                 errno = EINVAL;
57                 return 0;
58         }
59
60         LOCK(lock);
61
62         for (p=bindings; p; p=p->next) {
63                 if (!strcmp(p->domainname, domainname) &&
64                     !strcmp(p->dirname, dirname)) {
65                         break;
66                 }
67         }
68
69         if (!p) {
70                 p = calloc(sizeof *p + domlen + dirlen + 2, 1);
71                 if (!p) {
72                         UNLOCK(lock);
73                         return 0;
74                 }
75                 p->next = bindings;
76                 p->dirlen = dirlen;
77                 p->domainname = p->buf;
78                 p->dirname = p->buf + domlen + 1;
79                 memcpy(p->domainname, domainname, domlen+1);
80                 memcpy(p->dirname, dirname, dirlen+1);
81                 a_cas_p(&bindings, bindings, p);
82         }
83
84         a_store(&p->active, 1);
85
86         for (q=bindings; q; q=q->next) {
87                 if (!strcmp(q->domainname, domainname) && q != p)
88                         a_store(&q->active, 0);
89         }
90
91         UNLOCK(lock);
92         
93         return (char *)p->dirname;
94 }
95
96 static const char catnames[][12] = {
97         "LC_CTYPE",
98         "LC_NUMERIC",
99         "LC_TIME",
100         "LC_COLLATE",
101         "LC_MONETARY",
102         "LC_MESSAGES",
103 };
104
105 static const char catlens[] = { 8, 10, 7, 10, 11, 11 };
106
107 struct msgcat {
108         struct msgcat *next;
109         const void *map;
110         size_t map_size;
111         const char *plural_rule;
112         int nplurals;
113         struct binding *binding;
114         const struct __locale_map *lm;
115         int cat;
116 };
117
118 static char *dummy_gettextdomain()
119 {
120         return "messages";
121 }
122
123 weak_alias(dummy_gettextdomain, __gettextdomain);
124
125 char *dcngettext(const char *domainname, const char *msgid1, const char *msgid2, unsigned long int n, int category)
126 {
127         static struct msgcat *volatile cats;
128         struct msgcat *p;
129         struct __locale_struct *loc = CURRENT_LOCALE;
130         const struct __locale_map *lm;
131         size_t domlen;
132         struct binding *q;
133         int old_errno = errno;
134
135         if ((unsigned)category >= LC_ALL) goto notrans;
136
137         if (!domainname) domainname = __gettextdomain();
138
139         domlen = strnlen(domainname, NAME_MAX+1);
140         if (domlen > NAME_MAX) goto notrans;
141
142         for (q=bindings; q; q=q->next)
143                 if (!strcmp(q->domainname, domainname) && q->active)
144                         break;
145         if (!q) goto notrans;
146
147         lm = loc->cat[category];
148         if (!lm) {
149 notrans:
150                 errno = old_errno;
151                 return (char *) ((n == 1) ? msgid1 : msgid2);
152         }
153
154         for (p=cats; p; p=p->next)
155                 if (p->binding == q && p->lm == lm && p->cat == category)
156                         break;
157
158         if (!p) {
159                 const char *dirname, *locname, *catname, *modname, *locp;
160                 size_t dirlen, loclen, catlen, modlen, alt_modlen;
161                 void *old_cats;
162                 size_t map_size;
163
164                 dirname = q->dirname;
165                 locname = lm->name;
166                 catname = catnames[category];
167
168                 dirlen = q->dirlen;
169                 loclen = strlen(locname);
170                 catlen = catlens[category];
171
172                 /* Logically split @mod suffix from locale name. */
173                 modname = memchr(locname, '@', loclen);
174                 if (!modname) modname = locname + loclen;
175                 alt_modlen = modlen = loclen - (modname-locname);
176                 loclen = modname-locname;
177
178                 /* Drop .charset identifier; it is not used. */
179                 const char *csp = memchr(locname, '.', loclen);
180                 if (csp) loclen = csp-locname;
181
182                 char name[dirlen+1 + loclen+modlen+1 + catlen+1 + domlen+3 + 1];
183                 const void *map;
184
185                 for (;;) {
186                         snprintf(name, sizeof name, "%s/%.*s%.*s/%s/%s.mo\0",
187                                 dirname, (int)loclen, locname,
188                                 (int)alt_modlen, modname, catname, domainname);
189                         if (map = __map_file(name, &map_size)) break;
190
191                         /* Try dropping @mod, _YY, then both. */
192                         if (alt_modlen) {
193                                 alt_modlen = 0;
194                         } else if ((locp = memchr(locname, '_', loclen))) {
195                                 loclen = locp-locname;
196                                 alt_modlen = modlen;
197                         } else {
198                                 break;
199                         }
200                 }
201                 if (!map) goto notrans;
202
203                 p = calloc(sizeof *p, 1);
204                 if (!p) {
205                         __munmap((void *)map, map_size);
206                         goto notrans;
207                 }
208                 p->cat = category;
209                 p->binding = q;
210                 p->lm = lm;
211                 p->map = map;
212                 p->map_size = map_size;
213
214                 const char *rule = "n!=1;";
215                 unsigned long np = 2;
216                 const char *r = __mo_lookup(p->map, p->map_size, "");
217                 char *z;
218                 while (r && strncmp(r, "Plural-Forms:", 13)) {
219                         z = strchr(r, '\n');
220                         r = z ? z+1 : 0;
221                 }
222                 if (r) {
223                         r += 13;
224                         while (isspace(*r)) r++;
225                         if (!strncmp(r, "nplurals=", 9)) {
226                                 np = strtoul(r+9, &z, 10);
227                                 r = z;
228                         }
229                         while (*r && *r != ';') r++;
230                         if (*r) {
231                                 r++;
232                                 while (isspace(*r)) r++;
233                                 if (!strncmp(r, "plural=", 7))
234                                         rule = r+7;
235                         }
236                 }
237                 p->nplurals = np;
238                 p->plural_rule = rule;
239
240                 do {
241                         old_cats = cats;
242                         p->next = old_cats;
243                 } while (a_cas_p(&cats, old_cats, p) != old_cats);
244         }
245
246         const char *trans = __mo_lookup(p->map, p->map_size, msgid1);
247         if (!trans) goto notrans;
248
249         /* Non-plural-processing gettext forms pass a null pointer as
250          * msgid2 to request that dcngettext suppress plural processing. */
251
252         if (msgid2 && p->nplurals) {
253                 unsigned long plural = __pleval(p->plural_rule, n);
254                 if (plural > p->nplurals) goto notrans;
255                 while (plural--) {
256                         size_t rem = p->map_size - (trans - (char *)p->map);
257                         size_t l = strnlen(trans, rem);
258                         if (l+1 >= rem)
259                                 goto notrans;
260                         trans += l+1;
261                 }
262         }
263         errno = old_errno;
264         return (char *)trans;
265 }
266
267 char *dcgettext(const char *domainname, const char *msgid, int category)
268 {
269         return dcngettext(domainname, msgid, 0, 1, category);
270 }
271
272 char *dngettext(const char *domainname, const char *msgid1, const char *msgid2, unsigned long int n)
273 {
274         return dcngettext(domainname, msgid1, msgid2, n, LC_MESSAGES);
275 }
276
277 char *dgettext(const char *domainname, const char *msgid)
278 {
279         return dcngettext(domainname, msgid, 0, 1, LC_MESSAGES);
280 }