1 /* 2 * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 #if defined(__linux__) || defined(_ALLBSD_SOURCE) 27 #include <stdio.h> 28 #include <ctype.h> 29 #endif 30 #include <pwd.h> 31 #include <locale.h> 32 #ifndef ARCHPROPNAME 33 #error "The macro ARCHPROPNAME has not been defined" 34 #endif 35 #include <sys/utsname.h> /* For os_name and os_version */ 36 #include <langinfo.h> /* For nl_langinfo */ 37 #include <stdlib.h> 38 #include <string.h> 39 #include <sys/types.h> 40 #include <unistd.h> 41 #include <sys/param.h> 42 #include <time.h> 43 #include <errno.h> 44 45 #ifdef MACOSX 46 #include "java_props_macosx.h" 47 #endif 48 49 #if defined(_ALLBSD_SOURCE) 50 #if !defined(P_tmpdir) 51 #include <paths.h> 52 #define P_tmpdir _PATH_VARTMP 53 #endif 54 #endif 55 56 #include "locale_str.h" 57 #include "java_props.h" 58 59 #if !defined(_ALLBSD_SOURCE) 60 #ifdef __linux__ 61 #ifndef CODESET 62 #define CODESET _NL_CTYPE_CODESET_NAME 63 #endif 64 #else 65 #ifdef ALT_CODESET_KEY 66 #define CODESET ALT_CODESET_KEY 67 #endif 68 #endif 69 #endif /* !_ALLBSD_SOURCE */ 70 71 #ifdef JAVASE_EMBEDDED 72 #include <dlfcn.h> 73 #include <sys/stat.h> 74 #endif 75 76 /* Take an array of string pairs (map of key->value) and a string (key). 77 * Examine each pair in the map to see if the first string (key) matches the 78 * string. If so, store the second string of the pair (value) in the value and 79 * return 1. Otherwise do nothing and return 0. The end of the map is 80 * indicated by an empty string at the start of a pair (key of ""). 81 */ 82 static int 83 mapLookup(char* map[], const char* key, char** value) { 84 int i; 85 for (i = 0; strcmp(map[i], ""); i += 2){ 86 if (!strcmp(key, map[i])){ 87 *value = map[i + 1]; 88 return 1; 89 } 90 } 91 return 0; 92 } 93 94 /* This function sets an environment variable using envstring. 95 * The format of envstring is "name=value". 96 * If the name has already existed, it will append value to the name. 97 */ 98 static void 99 setPathEnvironment(char *envstring) 100 { 101 char name[20], *value, *current; 102 103 value = strchr(envstring, '='); /* locate name and value separator */ 104 105 if (! value) 106 return; /* not a valid environment setting */ 107 108 /* copy first part as environment name */ 109 strncpy(name, envstring, value - envstring); 110 name[value-envstring] = '\0'; 111 112 value++; /* set value point to value of the envstring */ 113 114 current = getenv(name); 115 if (current) { 116 if (! strstr(current, value)) { 117 /* value is not found in current environment, append it */ 118 char *temp = malloc(strlen(envstring) + strlen(current) + 2); 119 strcpy(temp, name); 120 strcat(temp, "="); 121 strcat(temp, current); 122 strcat(temp, ":"); 123 strcat(temp, value); 124 putenv(temp); 125 } 126 /* else the value has already been set, do nothing */ 127 } 128 else { 129 /* environment variable is not found */ 130 putenv(envstring); 131 } 132 } 133 134 #ifndef P_tmpdir 135 #define P_tmpdir "/var/tmp" 136 #endif 137 138 static int ParseLocale(JNIEnv* env, int cat, char ** std_language, char ** std_script, 139 char ** std_country, char ** std_variant, char ** std_encoding) { 140 char *temp = NULL; 141 char *language = NULL, *country = NULL, *variant = NULL, 142 *encoding = NULL; 143 char *p, *encoding_variant, *old_temp, *old_ev; 144 char *lc; 145 146 /* Query the locale set for the category */ 147 148 #ifdef MACOSX 149 lc = setupMacOSXLocale(cat); // malloc'd memory, need to free 150 #else 151 lc = setlocale(cat, NULL); 152 #endif 153 154 #ifndef __linux__ 155 if (lc == NULL) { 156 return 0; 157 } 158 159 temp = malloc(strlen(lc) + 1); 160 if (temp == NULL) { 161 #ifdef MACOSX 162 free(lc); // malloced memory 163 #endif 164 JNU_ThrowOutOfMemoryError(env, NULL); 165 return 0; 166 } 167 168 if (cat == LC_CTYPE) { 169 /* 170 * Workaround for Solaris bug 4201684: Xlib doesn't like @euro 171 * locales. Since we don't depend on the libc @euro behavior, 172 * we just remove the qualifier. 173 * On Linux, the bug doesn't occur; on the other hand, @euro 174 * is needed there because it's a shortcut that also determines 175 * the encoding - without it, we wouldn't get ISO-8859-15. 176 * Therefore, this code section is Solaris-specific. 177 */ 178 strcpy(temp, lc); 179 p = strstr(temp, "@euro"); 180 if (p != NULL) { 181 *p = '\0'; 182 setlocale(LC_ALL, temp); 183 } 184 } 185 #else 186 if (lc == NULL || !strcmp(lc, "C") || !strcmp(lc, "POSIX")) { 187 lc = "en_US"; 188 } 189 190 temp = malloc(strlen(lc) + 1); 191 if (temp == NULL) { 192 JNU_ThrowOutOfMemoryError(env, NULL); 193 return 0; 194 } 195 196 #endif 197 198 /* 199 * locale string format in Solaris is 200 * <language name>_<country name>.<encoding name>@<variant name> 201 * <country name>, <encoding name>, and <variant name> are optional. 202 */ 203 204 strcpy(temp, lc); 205 #ifdef MACOSX 206 free(lc); // malloced memory 207 #endif 208 /* Parse the language, country, encoding, and variant from the 209 * locale. Any of the elements may be missing, but they must occur 210 * in the order language_country.encoding@variant, and must be 211 * preceded by their delimiter (except for language). 212 * 213 * If the locale name (without .encoding@variant, if any) matches 214 * any of the names in the locale_aliases list, map it to the 215 * corresponding full locale name. Most of the entries in the 216 * locale_aliases list are locales that include a language name but 217 * no country name, and this facility is used to map each language 218 * to a default country if that's possible. It's also used to map 219 * the Solaris locale aliases to their proper Java locale IDs. 220 */ 221 222 encoding_variant = malloc(strlen(temp)+1); 223 if (encoding_variant == NULL) { 224 free(temp); 225 JNU_ThrowOutOfMemoryError(env, NULL); 226 return 0; 227 } 228 229 if ((p = strchr(temp, '.')) != NULL) { 230 strcpy(encoding_variant, p); /* Copy the leading '.' */ 231 *p = '\0'; 232 } else if ((p = strchr(temp, '@')) != NULL) { 233 strcpy(encoding_variant, p); /* Copy the leading '@' */ 234 *p = '\0'; 235 } else { 236 *encoding_variant = '\0'; 237 } 238 239 if (mapLookup(locale_aliases, temp, &p)) { 240 old_temp = temp; 241 temp = realloc(temp, strlen(p)+1); 242 if (temp == NULL) { 243 free(old_temp); 244 free(encoding_variant); 245 JNU_ThrowOutOfMemoryError(env, NULL); 246 return 0; 247 } 248 strcpy(temp, p); 249 old_ev = encoding_variant; 250 encoding_variant = realloc(encoding_variant, strlen(temp)+1); 251 if (encoding_variant == NULL) { 252 free(old_ev); 253 free(temp); 254 JNU_ThrowOutOfMemoryError(env, NULL); 255 return 0; 256 } 257 // check the "encoding_variant" again, if any. 258 if ((p = strchr(temp, '.')) != NULL) { 259 strcpy(encoding_variant, p); /* Copy the leading '.' */ 260 *p = '\0'; 261 } else if ((p = strchr(temp, '@')) != NULL) { 262 strcpy(encoding_variant, p); /* Copy the leading '@' */ 263 *p = '\0'; 264 } 265 } 266 267 language = temp; 268 if ((country = strchr(temp, '_')) != NULL) { 269 *country++ = '\0'; 270 } 271 272 p = encoding_variant; 273 if ((encoding = strchr(p, '.')) != NULL) { 274 p[encoding++ - p] = '\0'; 275 p = encoding; 276 } 277 if ((variant = strchr(p, '@')) != NULL) { 278 p[variant++ - p] = '\0'; 279 } 280 281 /* Normalize the language name */ 282 if (std_language != NULL) { 283 *std_language = "en"; 284 if (language != NULL && mapLookup(language_names, language, std_language) == 0) { 285 *std_language = malloc(strlen(language)+1); 286 strcpy(*std_language, language); 287 } 288 } 289 290 /* Normalize the country name */ 291 if (std_country != NULL && country != NULL) { 292 if (mapLookup(country_names, country, std_country) == 0) { 293 *std_country = malloc(strlen(country)+1); 294 strcpy(*std_country, country); 295 } 296 } 297 298 /* Normalize the script and variant name. Note that we only use 299 * variants listed in the mapping array; others are ignored. 300 */ 301 if (variant != NULL) { 302 if (std_script != NULL) { 303 mapLookup(script_names, variant, std_script); 304 } 305 306 if (std_variant != NULL) { 307 mapLookup(variant_names, variant, std_variant); 308 } 309 } 310 311 /* Normalize the encoding name. Note that we IGNORE the string 312 * 'encoding' extracted from the locale name above. Instead, we use the 313 * more reliable method of calling nl_langinfo(CODESET). This function 314 * returns an empty string if no encoding is set for the given locale 315 * (e.g., the C or POSIX locales); we use the default ISO 8859-1 316 * converter for such locales. 317 */ 318 if (std_encoding != NULL) { 319 /* OK, not so reliable - nl_langinfo() gives wrong answers on 320 * Euro locales, in particular. */ 321 if (strcmp(p, "ISO8859-15") == 0) 322 p = "ISO8859-15"; 323 else 324 p = nl_langinfo(CODESET); 325 326 /* Convert the bare "646" used on Solaris to a proper IANA name */ 327 if (strcmp(p, "646") == 0) 328 p = "ISO646-US"; 329 330 /* return same result nl_langinfo would return for en_UK, 331 * in order to use optimizations. */ 332 *std_encoding = (*p != '\0') ? p : "ISO8859-1"; 333 334 #ifdef __linux__ 335 /* 336 * Remap the encoding string to a different value for japanese 337 * locales on linux so that customized converters are used instead 338 * of the default converter for "EUC-JP". The customized converters 339 * omit support for the JIS0212 encoding which is not supported by 340 * the variant of "EUC-JP" encoding used on linux 341 */ 342 if (strcmp(p, "EUC-JP") == 0) { 343 *std_encoding = "EUC-JP-LINUX"; 344 } 345 #else 346 if (strcmp(p,"eucJP") == 0) { 347 /* For Solaris use customized vendor defined character 348 * customized EUC-JP converter 349 */ 350 *std_encoding = "eucJP-open"; 351 } else if (strcmp(p, "Big5") == 0 || strcmp(p, "BIG5") == 0) { 352 /* 353 * Remap the encoding string to Big5_Solaris which augments 354 * the default converter for Solaris Big5 locales to include 355 * seven additional ideographic characters beyond those included 356 * in the Java "Big5" converter. 357 */ 358 *std_encoding = "Big5_Solaris"; 359 } else if (strcmp(p, "Big5-HKSCS") == 0) { 360 /* 361 * Solaris uses HKSCS2001 362 */ 363 *std_encoding = "Big5-HKSCS-2001"; 364 } 365 #endif 366 #ifdef MACOSX 367 /* 368 * For the case on MacOS X where encoding is set to US-ASCII, but we 369 * don't have any encoding hints from LANG/LC_ALL/LC_CTYPE, use UTF-8 370 * instead. 371 * 372 * The contents of ASCII files will still be read and displayed 373 * correctly, but so will files containing UTF-8 characters beyond the 374 * standard ASCII range. 375 * 376 * Specifically, this allows apps launched by double-clicking a .jar 377 * file to correctly read UTF-8 files using the default encoding (see 378 * 8011194). 379 */ 380 if (strcmp(p,"US-ASCII") == 0 && getenv("LANG") == NULL && 381 getenv("LC_ALL") == NULL && getenv("LC_CTYPE") == NULL) { 382 *std_encoding = "UTF-8"; 383 } 384 #endif 385 } 386 387 free(temp); 388 free(encoding_variant); 389 390 return 1; 391 } 392 393 #ifdef JAVASE_EMBEDDED 394 /* Determine the default embedded toolkit based on whether libawt_xawt 395 * exists in the JRE. This can still be overridden by -Dawt.toolkit=XXX 396 */ 397 static char* getEmbeddedToolkit() { 398 Dl_info dlinfo; 399 char buf[MAXPATHLEN]; 400 int32_t len; 401 char *p; 402 struct stat statbuf; 403 404 /* Get address of this library and the directory containing it. */ 405 dladdr((void *)getEmbeddedToolkit, &dlinfo); 406 realpath((char *)dlinfo.dli_fname, buf); 407 len = strlen(buf); 408 p = strrchr(buf, '/'); 409 /* Default AWT Toolkit on Linux and Solaris is XAWT (libawt_xawt.so). */ 410 strncpy(p, "/libawt_xawt.so", MAXPATHLEN-len-1); 411 /* Check if it exists */ 412 if (stat(buf, &statbuf) == -1 && errno == ENOENT) { 413 /* No - this is a reduced-headless-jre so use special HToolkit */ 414 return "sun.awt.HToolkit"; 415 } 416 else { 417 /* Yes - this is a headful JRE so fallback to SE defaults */ 418 return NULL; 419 } 420 } 421 #endif 422 423 /* This function gets called very early, before VM_CALLS are setup. 424 * Do not use any of the VM_CALLS entries!!! 425 */ 426 java_props_t * 427 GetJavaProperties(JNIEnv *env) 428 { 429 static java_props_t sprops; 430 char *v; /* tmp var */ 431 432 if (sprops.user_dir) { 433 return &sprops; 434 } 435 436 /* tmp dir */ 437 sprops.tmp_dir = P_tmpdir; 438 #ifdef MACOSX 439 /* darwin has a per-user temp dir */ 440 static char tmp_path[PATH_MAX]; 441 int pathSize = confstr(_CS_DARWIN_USER_TEMP_DIR, tmp_path, PATH_MAX); 442 if (pathSize > 0 && pathSize <= PATH_MAX) { 443 sprops.tmp_dir = tmp_path; 444 } 445 #endif /* MACOSX */ 446 447 /* Printing properties */ 448 #ifdef MACOSX 449 sprops.printerJob = "sun.lwawt.macosx.CPrinterJob"; 450 #else 451 sprops.printerJob = "sun.print.PSPrinterJob"; 452 #endif 453 454 /* patches/service packs installed */ 455 sprops.patch_level = "unknown"; 456 457 /* Java 2D/AWT properties */ 458 #ifdef MACOSX 459 // Always the same GraphicsEnvironment and Toolkit on Mac OS X 460 sprops.graphics_env = "sun.awt.CGraphicsEnvironment"; 461 sprops.awt_toolkit = "sun.lwawt.macosx.LWCToolkit"; 462 463 // check if we're in a GUI login session and set java.awt.headless=true if not 464 sprops.awt_headless = isInAquaSession() ? NULL : "true"; 465 #else 466 sprops.graphics_env = "sun.awt.X11GraphicsEnvironment"; 467 #ifdef JAVASE_EMBEDDED 468 sprops.awt_toolkit = getEmbeddedToolkit(); 469 if (sprops.awt_toolkit == NULL) // default as below 470 #endif 471 sprops.awt_toolkit = "sun.awt.X11.XToolkit"; 472 #endif 473 474 /* This is used only for debugging of font problems. */ 475 v = getenv("JAVA2D_FONTPATH"); 476 sprops.font_dir = v ? v : NULL; 477 478 #ifdef SI_ISALIST 479 /* supported instruction sets */ 480 { 481 char list[258]; 482 sysinfo(SI_ISALIST, list, sizeof(list)); 483 sprops.cpu_isalist = strdup(list); 484 } 485 #else 486 sprops.cpu_isalist = NULL; 487 #endif 488 489 /* endianness of platform */ 490 { 491 unsigned int endianTest = 0xff000000; 492 if (((char*)(&endianTest))[0] != 0) 493 sprops.cpu_endian = "big"; 494 else 495 sprops.cpu_endian = "little"; 496 } 497 498 /* os properties */ 499 { 500 #ifdef MACOSX 501 setOSNameAndVersion(&sprops); 502 #else 503 struct utsname name; 504 uname(&name); 505 sprops.os_name = strdup(name.sysname); 506 #ifdef _AIX 507 { 508 char *os_version = malloc(strlen(name.version) + 509 strlen(name.release) + 2); 510 if (os_version != NULL) { 511 strcpy(os_version, name.version); 512 strcat(os_version, "."); 513 strcat(os_version, name.release); 514 } 515 sprops.os_version = os_version; 516 } 517 #else 518 sprops.os_version = strdup(name.release); 519 #endif /* _AIX */ 520 #endif /* MACOSX */ 521 522 sprops.os_arch = ARCHPROPNAME; 523 524 if (getenv("GNOME_DESKTOP_SESSION_ID") != NULL) { 525 sprops.desktop = "gnome"; 526 } 527 else { 528 sprops.desktop = NULL; 529 } 530 } 531 532 /* ABI property (optional) */ 533 #ifdef JDK_ARCH_ABI_PROP_NAME 534 sprops.sun_arch_abi = JDK_ARCH_ABI_PROP_NAME; 535 #endif 536 537 /* Determine the language, country, variant, and encoding from the host, 538 * and store these in the user.language, user.country, user.variant and 539 * file.encoding system properties. */ 540 setlocale(LC_ALL, ""); 541 if (ParseLocale(env, LC_CTYPE, 542 &(sprops.format_language), 543 &(sprops.format_script), 544 &(sprops.format_country), 545 &(sprops.format_variant), 546 &(sprops.encoding))) { 547 ParseLocale(env, LC_MESSAGES, 548 &(sprops.language), 549 &(sprops.script), 550 &(sprops.country), 551 &(sprops.variant), 552 NULL); 553 } else { 554 sprops.language = "en"; 555 sprops.encoding = "ISO8859-1"; 556 } 557 sprops.display_language = sprops.language; 558 sprops.display_script = sprops.script; 559 sprops.display_country = sprops.country; 560 sprops.display_variant = sprops.variant; 561 562 /* ParseLocale failed with OOME */ 563 JNU_CHECK_EXCEPTION_RETURN(env, NULL); 564 565 #ifdef MACOSX 566 sprops.sun_jnu_encoding = "UTF-8"; 567 #else 568 sprops.sun_jnu_encoding = sprops.encoding; 569 #endif 570 571 #ifdef _ALLBSD_SOURCE 572 #if BYTE_ORDER == _LITTLE_ENDIAN 573 sprops.unicode_encoding = "UnicodeLittle"; 574 #else 575 sprops.unicode_encoding = "UnicodeBig"; 576 #endif 577 #else /* !_ALLBSD_SOURCE */ 578 #ifdef __linux__ 579 #if __BYTE_ORDER == __LITTLE_ENDIAN 580 sprops.unicode_encoding = "UnicodeLittle"; 581 #else 582 sprops.unicode_encoding = "UnicodeBig"; 583 #endif 584 #else 585 sprops.unicode_encoding = "UnicodeBig"; 586 #endif 587 #endif /* _ALLBSD_SOURCE */ 588 589 /* user properties */ 590 { 591 struct passwd *pwent = getpwuid(getuid()); 592 sprops.user_name = pwent ? strdup(pwent->pw_name) : "?"; 593 #ifdef MACOSX 594 setUserHome(&sprops); 595 #else 596 sprops.user_home = pwent ? strdup(pwent->pw_dir) : NULL; 597 #endif 598 if (sprops.user_home == NULL) { 599 sprops.user_home = "?"; 600 } 601 } 602 603 /* User TIMEZONE */ 604 { 605 /* 606 * We defer setting up timezone until it's actually necessary. 607 * Refer to TimeZone.getDefault(). However, the system 608 * property is necessary to be able to be set by the command 609 * line interface -D. Here temporarily set a null string to 610 * timezone. 611 */ 612 tzset(); /* for compatibility */ 613 sprops.timezone = ""; 614 } 615 616 /* Current directory */ 617 { 618 char buf[MAXPATHLEN]; 619 errno = 0; 620 if (getcwd(buf, sizeof(buf)) == NULL) 621 JNU_ThrowByName(env, "java/lang/Error", 622 "Properties init: Could not determine current working directory."); 623 else 624 sprops.user_dir = strdup(buf); 625 } 626 627 sprops.file_separator = "/"; 628 sprops.path_separator = ":"; 629 sprops.line_separator = "\n"; 630 631 #if !defined(_ALLBSD_SOURCE) 632 /* Append CDE message and resource search path to NLSPATH and 633 * XFILESEARCHPATH, in order to pick localized message for 634 * FileSelectionDialog window (Bug 4173641). 635 */ 636 setPathEnvironment("NLSPATH=/usr/dt/lib/nls/msg/%L/%N.cat"); 637 setPathEnvironment("XFILESEARCHPATH=/usr/dt/app-defaults/%L/Dt"); 638 #endif 639 640 641 #ifdef MACOSX 642 setProxyProperties(&sprops); 643 #endif 644 645 return &sprops; 646 } 647 648 jstring 649 GetStringPlatform(JNIEnv *env, nchar* cstr) 650 { 651 return JNU_NewStringPlatform(env, cstr); 652 }