1 /* 2 * Copyright (c) 1999, 2011, 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 #include <stdlib.h> 27 #include <stdio.h> 28 #include <strings.h> 29 #include <time.h> 30 #include <limits.h> 31 #include <errno.h> 32 #include <stddef.h> 33 #include <sys/stat.h> 34 #include <sys/types.h> 35 #include <string.h> 36 #include <dirent.h> 37 #include <unistd.h> 38 #ifdef __solaris__ 39 #include <libscf.h> 40 #endif 41 42 #include "jvm.h" 43 44 #define SKIP_SPACE(p) while (*p == ' ' || *p == '\t') p++; 45 46 #if !defined(__solaris__) || defined(__sparcv9) || defined(amd64) 47 #define fileopen fopen 48 #define filegets fgets 49 #define fileclose fclose 50 #endif 51 52 #if defined(__linux__) || defined(_ALLBSD_SOURCE) 53 54 55 static const char *ETC_TIMEZONE_FILE = "/etc/timezone"; 56 static const char *ZONEINFO_DIR = "/usr/share/zoneinfo"; 57 static const char *DEFAULT_ZONEINFO_FILE = "/etc/localtime"; 58 #else 59 static const char *SYS_INIT_FILE = "/etc/default/init"; 60 static const char *ZONEINFO_DIR = "/usr/share/lib/zoneinfo"; 61 static const char *DEFAULT_ZONEINFO_FILE = "/usr/share/lib/zoneinfo/localtime"; 62 #endif /*__linux__*/ 63 64 /* 65 * Returns a pointer to the zone ID portion of the given zoneinfo file 66 * name, or NULL if the given string doesn't contain "zoneinfo/". 67 */ 68 static char * 69 getZoneName(char *str) 70 { 71 static const char *zidir = "zoneinfo/"; 72 73 char *pos = strstr((const char *)str, zidir); 74 if (pos == NULL) { 75 return NULL; 76 } 77 return pos + strlen(zidir); 78 } 79 80 /* 81 * Returns a path name created from the given 'dir' and 'name' under 82 * UNIX. This function allocates memory for the pathname calling 83 * malloc(). NULL is returned if malloc() fails. 84 */ 85 static char * 86 getPathName(const char *dir, const char *name) { 87 char *path; 88 89 path = (char *) malloc(strlen(dir) + strlen(name) + 2); 90 if (path == NULL) { 91 return NULL; 92 } 93 return strcat(strcat(strcpy(path, dir), "/"), name); 94 } 95 96 /* 97 * Scans the specified directory and its subdirectories to find a 98 * zoneinfo file which has the same content as /etc/localtime on Linux 99 * or /usr/share/lib/zoneinfo/localtime on Solaris given in 'buf'. 100 * If file is symbolic link, then the contents it points to are in buf. 101 * Returns a zone ID if found, otherwise, NULL is returned. 102 */ 103 static char * 104 findZoneinfoFile(char *buf, size_t size, const char *dir) 105 { 106 DIR *dirp = NULL; 107 struct stat statbuf; 108 struct dirent *dp = NULL; 109 struct dirent *entry = NULL; 110 char *pathname = NULL; 111 int fd = -1; 112 char *dbuf = NULL; 113 char *tz = NULL; 114 115 dirp = opendir(dir); 116 if (dirp == NULL) { 117 return NULL; 118 } 119 120 entry = (struct dirent *) malloc((size_t) pathconf(dir, _PC_NAME_MAX)); 121 if (entry == NULL) { 122 (void) closedir(dirp); 123 return NULL; 124 } 125 126 #if defined(__linux__) || defined(MACOSX) || (defined(__solaris__) \ 127 && (defined(_POSIX_PTHREAD_SEMANTICS) || defined(_LP64))) 128 while (readdir_r(dirp, entry, &dp) == 0 && dp != NULL) { 129 #else 130 while ((dp = readdir_r(dirp, entry)) != NULL) { 131 #endif 132 133 /* 134 * Skip '.' and '..' (and possibly other .* files) 135 */ 136 if (dp->d_name[0] == '.') { 137 continue; 138 } 139 140 /* 141 * Skip "ROC", "posixrules", and "localtime". 142 */ 143 if ((strcmp(dp->d_name, "ROC") == 0) 144 || (strcmp(dp->d_name, "posixrules") == 0) 145 #ifdef __solaris__ 146 /* 147 * Skip the "src" and "tab" directories on Solaris. 148 */ 149 || (strcmp(dp->d_name, "src") == 0) 150 || (strcmp(dp->d_name, "tab") == 0) 151 #endif 152 || (strcmp(dp->d_name, "localtime") == 0)) { 153 continue; 154 } 155 156 pathname = getPathName(dir, dp->d_name); 157 if (pathname == NULL) { 158 break; 159 } 160 if (stat(pathname, &statbuf) == -1) { 161 break; 162 } 163 164 if (S_ISDIR(statbuf.st_mode)) { 165 tz = findZoneinfoFile(buf, size, pathname); 166 if (tz != NULL) { 167 break; 168 } 169 } else if (S_ISREG(statbuf.st_mode) && (size_t)statbuf.st_size == size) { 170 dbuf = (char *) malloc(size); 171 if (dbuf == NULL) { 172 break; 173 } 174 if ((fd = open(pathname, O_RDONLY)) == -1) { 175 break; 176 } 177 if (read(fd, dbuf, size) != (ssize_t) size) { 178 break; 179 } 180 if (memcmp(buf, dbuf, size) == 0) { 181 tz = getZoneName(pathname); 182 if (tz != NULL) { 183 tz = strdup(tz); 184 } 185 break; 186 } 187 free((void *) dbuf); 188 dbuf = NULL; 189 (void) close(fd); 190 fd = -1; 191 } 192 free((void *) pathname); 193 pathname = NULL; 194 } 195 196 if (entry != NULL) { 197 free((void *) entry); 198 } 199 if (dirp != NULL) { 200 (void) closedir(dirp); 201 } 202 if (pathname != NULL) { 203 free((void *) pathname); 204 } 205 if (fd != -1) { 206 (void) close(fd); 207 } 208 if (dbuf != NULL) { 209 free((void *) dbuf); 210 } 211 return tz; 212 } 213 214 #if defined(__linux__) || defined(MACOSX) 215 216 /* 217 * Performs Linux specific mapping and returns a zone ID 218 * if found. Otherwise, NULL is returned. 219 */ 220 static char * 221 getPlatformTimeZoneID() 222 { 223 struct stat statbuf; 224 char *tz = NULL; 225 FILE *fp; 226 int fd; 227 char *buf; 228 size_t size; 229 char linkbuf[PATH_MAX+1]; 230 231 #ifdef __linux__ 232 /* 233 * Try reading the /etc/timezone file for Debian distros. There's 234 * no spec of the file format available. This parsing assumes that 235 * there's one line of an Olson tzid followed by a '\n', no 236 * leading or trailing spaces, no comments. 237 */ 238 if ((fp = fopen(ETC_TIMEZONE_FILE, "r")) != NULL) { 239 char line[256]; 240 241 if (fgets(line, sizeof(line), fp) != NULL) { 242 char *p = strchr(line, '\n'); 243 if (p != NULL) { 244 *p = '\0'; 245 } 246 if (strlen(line) > 0) { 247 tz = strdup(line); 248 } 249 } 250 (void) fclose(fp); 251 if (tz != NULL) { 252 return tz; 253 } 254 } 255 #endif /* __linux__ */ 256 257 /* 258 * Next, try /etc/localtime to find the zone ID. 259 */ 260 if (lstat(DEFAULT_ZONEINFO_FILE, &statbuf) == -1) { 261 return NULL; 262 } 263 264 /* 265 * If it's a symlink, get the link name and its zone ID part. (The 266 * older versions of timeconfig created a symlink as described in 267 * the Red Hat man page. It was changed in 1999 to create a copy 268 * of a zoneinfo file. It's no longer possible to get the zone ID 269 * from /etc/localtime.) 270 */ 271 strcpy(linkbuf, DEFAULT_ZONEINFO_FILE); 272 linkbuf[strlen(DEFAULT_ZONEINFO_FILE)] = '\0'; 273 if (S_ISLNK(statbuf.st_mode)) { 274 int len; 275 276 if ((len = readlink(DEFAULT_ZONEINFO_FILE, linkbuf, sizeof(linkbuf)-1)) == -1) { 277 jio_fprintf(stderr, (const char *) "can't get a symlink of %s\n", 278 DEFAULT_ZONEINFO_FILE); 279 return NULL; 280 } 281 linkbuf[len] = '\0'; 282 tz = getZoneName(linkbuf); 283 if (tz != NULL) { 284 tz = strdup(tz); 285 return tz; 286 } 287 } 288 289 /* 290 * If it's a regular file, we need to find out the same zoneinfo file 291 * that has been copied as /etc/localtime. 292 * If initial symbolic link resolution failed, we should treat target 293 * file as a regular file. Obtain new statbuf for this (using stat) 294 */ 295 if (stat(linkbuf, &statbuf) == -1) { 296 return NULL; 297 } 298 299 size = (size_t) statbuf.st_size; 300 buf = (char *) malloc(size); 301 if (buf == NULL) { 302 return NULL; 303 } 304 if ((fd = open(linkbuf, O_RDONLY)) == -1) { 305 free((void *) buf); 306 return NULL; 307 } 308 309 if (read(fd, buf, size) != (ssize_t) size) { 310 (void) close(fd); 311 free((void *) buf); 312 return NULL; 313 } 314 (void) close(fd); 315 316 tz = findZoneinfoFile(buf, size, ZONEINFO_DIR); 317 free((void *) buf); 318 return tz; 319 } 320 #else 321 #ifdef __solaris__ 322 #if !defined(__sparcv9) && !defined(amd64) 323 324 /* 325 * Those file* functions mimic the UNIX stream io functions. This is 326 * because of the limitation of the number of open files on Solaris 327 * (32-bit mode only) due to the System V ABI. 328 */ 329 330 #define BUFFER_SIZE 4096 331 332 static struct iobuffer { 333 int magic; /* -1 to distinguish from the real FILE */ 334 int fd; /* file descriptor */ 335 char *buffer; /* pointer to buffer */ 336 char *ptr; /* current read pointer */ 337 char *endptr; /* end pointer */ 338 }; 339 340 static int 341 fileclose(FILE *stream) 342 { 343 struct iobuffer *iop = (struct iobuffer *) stream; 344 345 if (iop->magic != -1) { 346 return fclose(stream); 347 } 348 349 if (iop == NULL) { 350 return 0; 351 } 352 close(iop->fd); 353 free((void *)iop->buffer); 354 free((void *)iop); 355 return 0; 356 } 357 358 static FILE * 359 fileopen(const char *fname, const char *fmode) 360 { 361 FILE *fp; 362 int fd; 363 struct iobuffer *iop; 364 365 if ((fp = fopen(fname, fmode)) != NULL) { 366 return fp; 367 } 368 369 /* 370 * It assumes read open. 371 */ 372 if ((fd = open(fname, O_RDONLY)) == -1) { 373 return NULL; 374 } 375 376 /* 377 * Allocate struct iobuffer and its buffer 378 */ 379 iop = malloc(sizeof(struct iobuffer)); 380 if (iop == NULL) { 381 (void) close(fd); 382 errno = ENOMEM; 383 return NULL; 384 } 385 iop->magic = -1; 386 iop->fd = fd; 387 iop->buffer = malloc(BUFFER_SIZE); 388 if (iop->buffer == NULL) { 389 (void) close(fd); 390 free((void *) iop); 391 errno = ENOMEM; 392 return NULL; 393 } 394 iop->ptr = iop->buffer; 395 iop->endptr = iop->buffer; 396 return (FILE *)iop; 397 } 398 399 /* 400 * This implementation assumes that n is large enough and the line 401 * separator is '\n'. 402 */ 403 static char * 404 filegets(char *s, int n, FILE *stream) 405 { 406 struct iobuffer *iop = (struct iobuffer *) stream; 407 char *p; 408 409 if (iop->magic != -1) { 410 return fgets(s, n, stream); 411 } 412 413 p = s; 414 for (;;) { 415 char c; 416 417 if (iop->ptr == iop->endptr) { 418 ssize_t len; 419 420 if ((len = read(iop->fd, (void *)iop->buffer, BUFFER_SIZE)) == -1) { 421 return NULL; 422 } 423 if (len == 0) { 424 *p = 0; 425 if (s == p) { 426 return NULL; 427 } 428 return s; 429 } 430 iop->ptr = iop->buffer; 431 iop->endptr = iop->buffer + len; 432 } 433 c = *iop->ptr++; 434 *p++ = c; 435 if ((p - s) == (n - 1)) { 436 *p = 0; 437 return s; 438 } 439 if (c == '\n') { 440 *p = 0; 441 return s; 442 } 443 } 444 /*NOTREACHED*/ 445 } 446 #endif /* not __sparcv9 */ 447 448 449 /* 450 * Performs Solaris dependent mapping. Returns a zone ID if 451 * found. Otherwise, NULL is returned. Solaris libc looks up 452 * "/etc/default/init" to get the default TZ value if TZ is not defined 453 * as an environment variable. 454 */ 455 static char * 456 getPlatformTimeZoneID() 457 { 458 char *tz = NULL; 459 FILE *fp; 460 461 /* 462 * Try the TZ entry in /etc/default/init. 463 */ 464 if ((fp = fileopen(SYS_INIT_FILE, "r")) != NULL) { 465 char line[256]; 466 char quote = '\0'; 467 468 while (filegets(line, sizeof(line), fp) != NULL) { 469 char *p = line; 470 char *s; 471 char c; 472 473 /* quick check for comment lines */ 474 if (*p == '#') { 475 continue; 476 } 477 if (strncmp(p, "TZ=", 3) == 0) { 478 p += 3; 479 SKIP_SPACE(p); 480 c = *p; 481 if (c == '"' || c == '\'') { 482 quote = c; 483 p++; 484 } 485 486 /* 487 * PSARC/2001/383: quoted string support 488 */ 489 for (s = p; (c = *s) != '\0' && c != '\n'; s++) { 490 /* No '\\' is supported here. */ 491 if (c == quote) { 492 quote = '\0'; 493 break; 494 } 495 if (c == ' ' && quote == '\0') { 496 break; 497 } 498 } 499 if (quote != '\0') { 500 jio_fprintf(stderr, "ZoneInfo: unterminated time zone name in /etc/TIMEZONE\n"); 501 } 502 *s = '\0'; 503 tz = strdup(p); 504 break; 505 } 506 } 507 (void) fileclose(fp); 508 } 509 return tz; 510 } 511 512 #define TIMEZONE_FMRI "svc:/system/timezone:default" 513 #define TIMEZONE_PG "timezone" 514 #define LOCALTIME_PROP "localtime" 515 516 static void 517 cleanupScf(scf_handle_t *h, 518 scf_snapshot_t *snap, 519 scf_instance_t *inst, 520 scf_propertygroup_t *pg, 521 scf_property_t *prop, 522 scf_value_t *val, 523 char *buf) { 524 if (buf != NULL) { 525 free(buf); 526 } 527 if (snap != NULL) { 528 scf_snapshot_destroy(snap); 529 } 530 if (val != NULL) { 531 scf_value_destroy(val); 532 } 533 if (prop != NULL) { 534 scf_property_destroy(prop); 535 } 536 if (pg != NULL) { 537 scf_pg_destroy(pg); 538 } 539 if (inst != NULL) { 540 scf_instance_destroy(inst); 541 } 542 if (h != NULL) { 543 scf_handle_destroy(h); 544 } 545 } 546 547 /* 548 * Retruns a zone ID of Solaris when the TZ value is "localtime". 549 * First, it tries scf. If scf fails, it looks for the same file as 550 * /usr/share/lib/zoneinfo/localtime under /usr/share/lib/zoneinfo/. 551 */ 552 static char * 553 getSolarisDefaultZoneID() { 554 char *tz = NULL; 555 struct stat statbuf; 556 size_t size; 557 char *buf; 558 int fd; 559 /* scf specific variables */ 560 scf_handle_t *h = NULL; 561 scf_snapshot_t *snap = NULL; 562 scf_instance_t *inst = NULL; 563 scf_propertygroup_t *pg = NULL; 564 scf_property_t *prop = NULL; 565 scf_value_t *val = NULL; 566 567 if ((h = scf_handle_create(SCF_VERSION)) != NULL 568 && scf_handle_bind(h) == 0 569 && (inst = scf_instance_create(h)) != NULL 570 && (snap = scf_snapshot_create(h)) != NULL 571 && (pg = scf_pg_create(h)) != NULL 572 && (prop = scf_property_create(h)) != NULL 573 && (val = scf_value_create(h)) != NULL 574 && scf_handle_decode_fmri(h, TIMEZONE_FMRI, NULL, NULL, inst, 575 NULL, NULL, SCF_DECODE_FMRI_REQUIRE_INSTANCE) == 0 576 && scf_instance_get_snapshot(inst, "running", snap) == 0 577 && scf_instance_get_pg_composed(inst, snap, TIMEZONE_PG, pg) == 0 578 && scf_pg_get_property(pg, LOCALTIME_PROP, prop) == 0 579 && scf_property_get_value(prop, val) == 0) { 580 ssize_t len; 581 582 /* Gets the length of the zone ID string */ 583 len = scf_value_get_astring(val, NULL, 0); 584 if (len != -1) { 585 tz = malloc(++len); /* +1 for a null byte */ 586 if (tz != NULL && scf_value_get_astring(val, tz, len) != -1) { 587 cleanupScf(h, snap, inst, pg, prop, val, NULL); 588 return tz; 589 } 590 } 591 } 592 cleanupScf(h, snap, inst, pg, prop, val, tz); 593 594 if (stat(DEFAULT_ZONEINFO_FILE, &statbuf) == -1) { 595 return NULL; 596 } 597 size = (size_t) statbuf.st_size; 598 buf = malloc(size); 599 if (buf == NULL) { 600 return NULL; 601 } 602 if ((fd = open(DEFAULT_ZONEINFO_FILE, O_RDONLY)) == -1) { 603 free((void *) buf); 604 return NULL; 605 } 606 607 if (read(fd, buf, size) != (ssize_t) size) { 608 (void) close(fd); 609 free((void *) buf); 610 return NULL; 611 } 612 (void) close(fd); 613 tz = findZoneinfoFile(buf, size, ZONEINFO_DIR); 614 free((void *) buf); 615 return tz; 616 } 617 #endif /*__solaris__*/ 618 #endif /*__linux__*/ 619 620 /* 621 * findJavaTZ_md() maps platform time zone ID to Java time zone ID 622 * using <java_home>/lib/tzmappings. If the TZ value is not found, it 623 * trys some libc implementation dependent mappings. If it still 624 * can't map to a Java time zone ID, it falls back to the GMT+/-hh:mm 625 * form. `country', which can be null, is not used for UNIX platforms. 626 */ 627 /*ARGSUSED1*/ 628 char * 629 findJavaTZ_md(const char *java_home_dir, const char *country) 630 { 631 char *tz; 632 char *javatz = NULL; 633 char *freetz = NULL; 634 635 tz = getenv("TZ"); 636 637 #if defined(__linux__) || defined(_ALLBSD_SOURCE) 638 if (tz == NULL) { 639 #else 640 #ifdef __solaris__ 641 if (tz == NULL || *tz == '\0') { 642 #endif 643 #endif 644 tz = getPlatformTimeZoneID(); 645 freetz = tz; 646 } 647 648 /* 649 * Remove any preceding ':' 650 */ 651 if (tz != NULL && *tz == ':') { 652 tz++; 653 } 654 655 #ifdef __solaris__ 656 if (strcmp(tz, "localtime") == 0) { 657 tz = getSolarisDefaultZoneID(); 658 freetz = tz; 659 } 660 #endif 661 662 if (tz != NULL) { 663 #ifdef __linux__ 664 /* 665 * Ignore "posix/" prefix. 666 */ 667 if (strncmp(tz, "posix/", 6) == 0) { 668 tz += 6; 669 } 670 #endif 671 javatz = strdup(tz); 672 if (freetz != NULL) { 673 free((void *) freetz); 674 } 675 } 676 return javatz; 677 } 678 /** 679 * Returns a GMT-offset-based zone ID. (e.g., "GMT-08:00") 680 */ 681 682 #ifdef MACOSX 683 684 char * 685 getGMTOffsetID() 686 { 687 time_t offset; 688 char sign, buf[32]; 689 struct tm *local_tm; 690 time_t clock; 691 time_t currenttime; 692 693 clock = time(NULL); 694 tzset(); 695 local_tm = localtime(&clock); 696 if (local_tm->tm_gmtoff >= 0) { 697 offset = (time_t) local_tm->tm_gmtoff; 698 sign = "+"; 699 } else { 700 offset = (time_t) -local_tm->tm_gmtoff; 701 sign = "-"; 702 } 703 sprintf(buf, (const char *)"GMT%c%02d:%02d", 704 sign, (int)(offset/3600), (int)((offset%3600)/60)); 705 return strdup(buf); 706 } 707 #else 708 709 char * 710 getGMTOffsetID() 711 { 712 time_t offset; 713 char sign, buf[32]; 714 #ifdef __solaris__ 715 struct tm localtm; 716 time_t currenttime; 717 718 currenttime = time(NULL); 719 if (localtime_r(¤ttime, &localtm) == NULL) { 720 return NULL; 721 } 722 723 offset = localtm.tm_isdst ? altzone : timezone; 724 #else 725 offset = timezone; 726 #endif /*__linux__*/ 727 728 if (offset == 0) { 729 return strdup("GMT"); 730 } 731 732 /* Note that the time offset direction is opposite. */ 733 if (offset > 0) { 734 sign = '-'; 735 } else { 736 offset = -offset; 737 sign = '+'; 738 } 739 sprintf(buf, (const char *)"GMT%c%02d:%02d", 740 sign, (int)(offset/3600), (int)((offset%3600)/60)); 741 return strdup(buf); 742 } 743 #endif /* MACOSX */