xref: /titanic_50/usr/src/lib/libc/port/locale/localeimpl.c (revision 24bb1048b1197ebc9afe761e4098573aeedfe8c9)
1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright 2014 Garrett D'Amore <garrett@damore.org>
14  */
15 
16 /*
17  * This file implements the 2008 newlocale and friends handling.
18  */
19 
20 #ifndef	_LCONV_C99
21 #define	_LCONV_C99
22 #endif
23 
24 #include "lint.h"
25 #include <atomic.h>
26 #include <locale.h>
27 #include <sys/types.h>
28 #include <sys/mman.h>
29 #include <errno.h>
30 #include <string.h>
31 #include "libc.h"
32 #include "mtlib.h"
33 #include "tsd.h"
34 #include "localeimpl.h"
35 #include "lctype.h"
36 
37 /*
38  * Big Theory of Locales:
39  *
40  * (It is recommended that readers familiarize themselves with the POSIX
41  * 2008 (XPG Issue 7) specifications for locales, first.)
42  *
43  * Historically, we had a bunch of global variables that stored locale
44  * data.  While this worked well, it limited applications to a single locale
45  * at a time.  This doesn't work well in certain server applications.
46  *
47  * Issue 7, X/Open introduced the concept of a locale_t object, along with
48  * versions of functions that can take this object as a parameter, along
49  * with functions to clone and manipulate these locale objects.  The new
50  * functions are named with a _l() suffix.
51  *
52  * Additionally uselocale() is introduced which can change the locale of
53  * of a single thread.  However, setlocale() can still be used to change
54  * the global locale.
55  *
56  * In our implementation, we use libc's TSD to store the locale data that
57  * was previously global.  We still have global data because some applications
58  * have had those global objects compiled into them.  (Such applications will
59  * be unable to benefit from uselocale(), btw.)  The legacy routines are
60  * reimplemented as wrappers that use the appropriate locale object by
61  * calling uselocale().  uselocale() when passed a NULL pointer returns the
62  * thread-specific locale object if one is present, or the global locale
63  * object otherwise.  Note that once the TSD data is set, the only way
64  * to revert to the global locale is to pass the global locale LC_GLOBAL_LOCALE
65  * to uselocale().
66  *
67  * We are careful to minimize performance impact of multiple calls to
68  * uselocale() or setlocale() by using a cache of locale data whenever possible.
69  * As a consequence of this, applications that iterate over all possible
70  * locales will burn through a lot of virtual memory, but we find such
71  * applications rare.  (locale -a might be an exception, but it is short lived.)
72  *
73  * Category data is never released (although enclosing locale objects might be),
74  * in order to guarantee thread-safety.  Calling freelocale() on an object
75  * while it is in use by another thread is a programmer error (use-after-free)
76  * and we don't bother to note it further.
77  *
78  * Locale objects (global locales) established by setlocale() are also
79  * never freed (for MT safety), but we will save previous locale objects
80  * and reuse them when we can.
81  */
82 
83 typedef struct locdata *(*loadfn_t)(const char *);
84 
85 static const loadfn_t loaders[LC_ALL] = {
86 	__lc_ctype_load,
87 	__lc_numeric_load,
88 	__lc_time_load,
89 	__lc_collate_load,
90 	__lc_monetary_load,
91 	__lc_messages_load,
92 };
93 
94 extern struct lc_monetary lc_monetary_posix;
95 extern struct lc_numeric lc_numeric_posix;
96 extern struct lc_messages lc_messages_posix;
97 extern struct lc_time lc_time_posix;
98 extern struct lc_ctype lc_ctype_posix;
99 extern struct lc_collate lc_collate_posix;
100 extern struct _RuneLocale _DefaultRuneLocale;
101 
102 static struct _locale posix_locale = {
103 	/* locdata */
104 	.locdata = {
105 		&__posix_ctype_locdata,
106 		&__posix_numeric_locdata,
107 		&__posix_time_locdata,
108 		&__posix_collate_locdata,
109 		&__posix_monetary_locdata,
110 		&__posix_messages_locdata,
111 	},
112 	.locname = "C",
113 	.ctype = &lc_ctype_posix,
114 	.numeric = &lc_numeric_posix,
115 	.collate = &lc_collate_posix,
116 	.monetary = &lc_monetary_posix,
117 	.messages = &lc_messages_posix,
118 	.time = &lc_time_posix,
119 	.runelocale = &_DefaultRuneLocale,
120 };
121 
122 locale_t ___global_locale = &posix_locale;
123 
124 locale_t
125 __global_locale(void)
126 {
127 	return (___global_locale);
128 }
129 
130 /*
131  * Category names for getenv()  Note that this was modified
132  * for Solaris.  See <iso/locale_iso.h>.
133  */
134 #define	NUM_CATS	7
135 static char *categories[7] = {
136 	"LC_CTYPE",
137 	"LC_NUMERIC",
138 	"LC_TIME",
139 	"LC_COLLATE",
140 	"LC_MONETARY",
141 	"LC_MESSAGES",
142 	"LC_ALL",
143 };
144 
145 /*
146  * Prototypes.
147  */
148 static const char *get_locale_env(int);
149 static struct locdata *locdata_get(int, const const char *);
150 static struct locdata *locdata_get_cache(int, const char *);
151 static locale_t mklocname(locale_t);
152 
153 /*
154  * Some utility routines.
155  */
156 
157 struct locdata *
158 __locdata_alloc(const char *name, size_t memsz)
159 {
160 	struct locdata *ldata;
161 
162 	if ((ldata = lmalloc(sizeof (*ldata))) == NULL) {
163 		return (NULL);
164 	}
165 	if ((ldata->l_data[0] = libc_malloc(memsz)) == NULL) {
166 		lfree(ldata, sizeof (*ldata));
167 		errno = ENOMEM;
168 		return (NULL);
169 	}
170 	(void) strlcpy(ldata->l_lname, name, sizeof (ldata->l_lname));
171 
172 	return (ldata);
173 }
174 
175 /*
176  * Normally we never free locale data truly, but if we failed to load it
177  * for some reason, this routine is used to cleanup the partial mess.
178  */
179 void
180 __locdata_free(struct locdata *ldata)
181 {
182 	for (int i = 0; i < NLOCDATA; i++)
183 		libc_free(ldata->l_data[i]);
184 	if (ldata->l_map != NULL && ldata->l_map_len)
185 		(void) munmap(ldata->l_map, ldata->l_map_len);
186 	lfree(ldata, sizeof (*ldata));
187 }
188 
189 /*
190  * It turns out that for performance reasons we would really like to
191  * cache the most recently referenced locale data to avoid wasteful
192  * loading from files.
193  */
194 
195 static struct locdata *cache_data[LC_ALL];
196 static struct locdata *cat_data[LC_ALL];
197 static mutex_t cache_lock = DEFAULTMUTEX;
198 
199 /*
200  * Returns the cached data if the locale name is the same.  If not,
201  * returns NULL (cache miss).  The locdata is returned with a hold on
202  * it, taken on behalf of the caller.  The caller should drop the hold
203  * when it is finished.
204  */
205 static struct locdata *
206 locdata_get_cache(int category, const char *locname)
207 {
208 	struct locdata *loc;
209 
210 	if (category < 0 || category >= LC_ALL)
211 		return (NULL);
212 
213 	/* Try cache first. */
214 	lmutex_lock(&cache_lock);
215 	loc = cache_data[category];
216 
217 	if ((loc != NULL) && (strcmp(loc->l_lname, locname) == 0)) {
218 		lmutex_unlock(&cache_lock);
219 		return (loc);
220 	}
221 
222 	/*
223 	 * Failing that try previously loaded locales (linear search) --
224 	 * this could be optimized to a hash, but its unlikely that a single
225 	 * application will ever need to work with more than a few locales.
226 	 */
227 	for (loc = cat_data[category]; loc != NULL; loc = loc->l_next) {
228 		if (strcmp(locname, loc->l_lname) == 0) {
229 			break;
230 		}
231 	}
232 
233 	/*
234 	 * Finally, if we still don't have one, try loading the locale
235 	 * data from the actual on-disk data.
236 	 *
237 	 * We drop the lock (libc wants to ensure no internal locks
238 	 * are held when we call other routines required to read from
239 	 * files, allocate memory, etc.)  There is a small race here,
240 	 * but the consequences of the race are benign -- if multiple
241 	 * threads hit this at precisely the same point, we could
242 	 * wind up with duplicates of the locale data in the cache.
243 	 *
244 	 * This wastes the memory for an extra copy of the locale
245 	 * data, but there is no further harm beyond that.  Its not
246 	 * worth the effort to recode this to something "safe"
247 	 * (which would require rescanning the list, etc.), given
248 	 * that this race will probably never actually occur.
249 	 */
250 	if (loc == NULL) {
251 		lmutex_unlock(&cache_lock);
252 		loc = (*loaders[category])(locname);
253 		lmutex_lock(&cache_lock);
254 		if (loc != NULL)
255 			(void) strlcpy(loc->l_lname, locname,
256 			    sizeof (loc->l_lname));
257 	}
258 
259 	/*
260 	 * Assuming we got one, update the cache, and stick us on the list
261 	 * of loaded locale data.  We insert into the head (more recent
262 	 * use is likely to win.)
263 	 */
264 	if (loc != NULL) {
265 		cache_data[category] = loc;
266 		if (!loc->l_cached) {
267 			loc->l_cached = 1;
268 			loc->l_next = cat_data[category];
269 			cat_data[category] = loc;
270 		}
271 	}
272 
273 	lmutex_unlock(&cache_lock);
274 	return (loc);
275 }
276 
277 /*
278  * Routine to get the locdata for a given category and locale.
279  * This includes retrieving it from cache, retrieving it from
280  * a file, etc.
281  */
282 static struct locdata *
283 locdata_get(int category, const char *locname)
284 {
285 	char scratch[ENCODING_LEN + 1];
286 	char *slash;
287 	int cnt;
288 	int len;
289 
290 	if (locname == NULL || *locname == 0) {
291 		locname = get_locale_env(category);
292 	}
293 
294 	/*
295 	 * Extract the locale name for the category if it is a composite
296 	 * locale.
297 	 */
298 	if ((slash = strchr(locname, '/')) != NULL) {
299 		for (cnt = category; cnt && slash != NULL; cnt--) {
300 			locname = slash + 1;
301 			slash = strchr(locname, '/');
302 		}
303 		if (slash) {
304 			len = slash - locname + 1;
305 			if (len >= sizeof (scratch)) {
306 				len = sizeof (scratch);
307 			}
308 		} else {
309 			len = sizeof (scratch);
310 		}
311 		(void) strlcpy(scratch, locname, len);
312 		locname = scratch;
313 	}
314 
315 	if ((strcmp(locname, "C") == 0) || (strcmp(locname, "POSIX") == 0))
316 		return (posix_locale.locdata[category]);
317 
318 	return (locdata_get_cache(category, locname));
319 }
320 
321 /* tsd destructor */
322 static void
323 freelocptr(void *arg)
324 {
325 	locale_t *locptr = arg;
326 	if (*locptr != NULL)
327 		freelocale(*locptr);
328 }
329 
330 static const char *
331 get_locale_env(int category)
332 {
333 	const char *env;
334 
335 	/* 1. check LC_ALL. */
336 	env = getenv(categories[LC_ALL]);
337 
338 	/* 2. check LC_* */
339 	if (env == NULL || *env == '\0')
340 		env = getenv(categories[category]);
341 
342 	/* 3. check LANG */
343 	if (env == NULL || *env == '\0')
344 		env = getenv("LANG");
345 
346 	/* 4. if none is set, fall to "C" */
347 	if (env == NULL || *env == '\0')
348 		env = "C";
349 
350 	return (env);
351 }
352 
353 
354 /*
355  * This routine is exposed via the MB_CUR_MAX macro.  Note that legacy
356  * code will continue to use _ctype[520], but we prefer this function as
357  * it is the only way to get thread-specific information.
358  */
359 unsigned char
360 __mb_cur_max_l(locale_t loc)
361 {
362 	return (loc->ctype->lc_max_mblen);
363 }
364 
365 unsigned char
366 __mb_cur_max(void)
367 {
368 	return (__mb_cur_max_l(uselocale(NULL)));
369 }
370 
371 /*
372  * Public interfaces.
373  */
374 
375 locale_t
376 duplocale(locale_t src)
377 {
378 	locale_t	loc;
379 	int		i;
380 
381 	loc = lmalloc(sizeof (*loc));
382 	if (loc == NULL) {
383 		return (NULL);
384 	}
385 	if (src == NULL) {
386 		/* illumos extension: POSIX says LC_GLOBAL_LOCALE here */
387 		src = ___global_locale;
388 	}
389 	for (i = 0; i < LC_ALL; i++) {
390 		loc->locdata[i] = src->locdata[i];
391 		loc->loaded[i] = 0;
392 	}
393 	loc->collate = loc->locdata[LC_COLLATE]->l_data[0];
394 	loc->ctype = loc->locdata[LC_CTYPE]->l_data[0];
395 	loc->runelocale = loc->locdata[LC_CTYPE]->l_data[1];
396 	loc->messages = loc->locdata[LC_MESSAGES]->l_data[0];
397 	loc->monetary = loc->locdata[LC_MONETARY]->l_data[0];
398 	loc->numeric = loc->locdata[LC_NUMERIC]->l_data[0];
399 	loc->time = loc->locdata[LC_TIME]->l_data[0];
400 	return (loc);
401 }
402 
403 void
404 freelocale(locale_t loc)
405 {
406 	/*
407 	 * We take extra care never to free a saved locale created by
408 	 * setlocale().  This shouldn't be strictly necessary, but a little
409 	 * extra safety doesn't hurt here.
410 	 */
411 	if ((loc != NULL) && (loc != &posix_locale) && (!loc->on_list))
412 		lfree(loc, sizeof (*loc));
413 }
414 
415 locale_t
416 newlocale(int catmask, const char *locname, locale_t base)
417 {
418 	locale_t loc;
419 	int i, e;
420 
421 	if (catmask & ~(LC_ALL_MASK)) {
422 		errno = EINVAL;
423 		return (NULL);
424 	}
425 
426 	/*
427 	 * Technically passing LC_GLOBAL_LOCALE here is illegal,
428 	 * but we allow it.
429 	 */
430 	if (base == NULL || base == ___global_locale) {
431 		loc = duplocale(___global_locale);
432 	} else {
433 		loc = duplocale(base);
434 	}
435 	if (loc == NULL) {
436 		return (NULL);
437 	}
438 
439 	for (i = 0; i < LC_ALL; i++) {
440 		struct locdata *ldata;
441 		loc->loaded[i] = 0;
442 		if (((1 << i) & catmask) == 0) {
443 			/* Default to base locale if not overriding */
444 			continue;
445 		}
446 		ldata = locdata_get(i, locname);
447 		if (ldata == NULL) {
448 			e = errno;
449 			freelocale(loc);
450 			errno = e;
451 			return (NULL);
452 		}
453 		loc->locdata[i] = ldata;
454 	}
455 	loc->collate = loc->locdata[LC_COLLATE]->l_data[0];
456 	loc->ctype = loc->locdata[LC_CTYPE]->l_data[0];
457 	loc->runelocale = loc->locdata[LC_CTYPE]->l_data[1];
458 	loc->messages = loc->locdata[LC_MESSAGES]->l_data[0];
459 	loc->monetary = loc->locdata[LC_MONETARY]->l_data[0];
460 	loc->numeric = loc->locdata[LC_NUMERIC]->l_data[0];
461 	loc->time = loc->locdata[LC_TIME]->l_data[0];
462 	freelocale(base);
463 
464 	return (mklocname(loc));
465 }
466 
467 locale_t
468 uselocale(locale_t loc)
469 {
470 	locale_t lastloc = ___global_locale;
471 	locale_t *locptr;
472 
473 	locptr = tsdalloc(_T_SETLOCALE, sizeof (locale_t), freelocptr);
474 	/* Should never occur */
475 	if (locptr == NULL) {
476 		errno = EINVAL;
477 		return (NULL);
478 	}
479 
480 	if (*locptr != NULL)
481 		lastloc = *locptr;
482 
483 	/* Argument loc is NULL if we are just querying. */
484 	if (loc != NULL) {
485 		/*
486 		 * Set it to LC_GLOBAL_LOCAL to return to using
487 		 * the global locale (setlocale).
488 		 */
489 		if (loc == ___global_locale) {
490 			*locptr = NULL;
491 		} else {
492 			/* No validation of the provided locale at present */
493 			*locptr = loc;
494 		}
495 	}
496 
497 	/*
498 	 * The caller is responsible for freeing, of course it would be
499 	 * gross error to call freelocale() on a locale object that is still
500 	 * in use.
501 	 */
502 	return (lastloc);
503 }
504 
505 static locale_t
506 mklocname(locale_t loc)
507 {
508 	int composite = 0;
509 
510 	/* Look to see if any category is different */
511 	for (int i = 1; i < LC_ALL; ++i) {
512 		if (strcmp(loc->locdata[0]->l_lname,
513 		    loc->locdata[i]->l_lname) != 0) {
514 			composite = 1;
515 			break;
516 		}
517 	}
518 
519 	if (composite) {
520 		/*
521 		 * Note ordering of these follows the numeric order,
522 		 * if the order is changed, then setlocale() will need
523 		 * to be changed as well.
524 		 */
525 		(void) snprintf(loc->locname, sizeof (loc->locname),
526 		    "%s/%s/%s/%s/%s/%s",
527 		    loc->locdata[LC_CTYPE]->l_lname,
528 		    loc->locdata[LC_NUMERIC]->l_lname,
529 		    loc->locdata[LC_TIME]->l_lname,
530 		    loc->locdata[LC_COLLATE]->l_lname,
531 		    loc->locdata[LC_MONETARY]->l_lname,
532 		    loc->locdata[LC_MESSAGES]->l_lname);
533 	} else {
534 		(void) strlcpy(loc->locname, loc->locdata[LC_CTYPE]->l_lname,
535 		    sizeof (loc->locname));
536 	}
537 	return (loc);
538 }
539