Elliott Hughes | 0e8616a | 2017-04-11 14:44:51 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
| 12 | * the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | #include <arpa/inet.h> // For ntohl(3). |
| 30 | #include <fcntl.h> |
| 31 | #include <stdint.h> |
| 32 | #include <stdlib.h> |
| 33 | #include <unistd.h> |
| 34 | |
| 35 | #include "private/CachedProperty.h" |
| 36 | |
| 37 | extern "C" void tzset_unlocked(void); |
| 38 | extern "C" int __bionic_open_tzdata(const char*, int32_t*); |
| 39 | |
| 40 | extern "C" void tzsetlcl(char const*); |
| 41 | |
| 42 | void tzset_unlocked() { |
| 43 | // The TZ environment variable is meant to override the system-wide setting. |
| 44 | const char* name = getenv("TZ"); |
| 45 | char buf[PROP_VALUE_MAX]; |
| 46 | |
| 47 | // If that's not set, look at the "persist.sys.timezone" system property. |
| 48 | if (name == nullptr) { |
| 49 | static CachedProperty persist_sys_timezone("persist.sys.timezone"); |
| 50 | |
| 51 | if ((name = persist_sys_timezone.Get()) != nullptr && strlen(name) > 3) { |
| 52 | // POSIX and Java disagree about the sign in a timezone string. For POSIX, "GMT+3" means |
| 53 | // "3 hours west/behind", but for Java it means "3 hours east/ahead". Since (a) Java is |
| 54 | // the one that matches human expectations and (b) this system property is used directly |
| 55 | // by Java, we flip the sign here to translate from Java to POSIX. http://b/25463955. |
| 56 | char sign = name[3]; |
| 57 | if (sign == '-' || sign == '+') { |
| 58 | strlcpy(buf, name, sizeof(buf)); |
| 59 | buf[3] = (sign == '-') ? '+' : '-'; |
| 60 | name = buf; |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | // If the system property is also not available (because you're running AOSP on a WiFi-only |
| 66 | // device, say), fall back to GMT. |
| 67 | if (name == nullptr) name = "GMT"; |
| 68 | |
| 69 | tzsetlcl(name); |
| 70 | } |
| 71 | |
| 72 | #if !defined(__ANDROID__) |
| 73 | static char* make_path(const char* path_prefix_variable, |
| 74 | const char* path_suffix) { |
| 75 | const char* path_prefix = getenv(path_prefix_variable); |
| 76 | if (path_prefix == nullptr) { |
| 77 | fprintf(stderr, "%s: %s not set!\n", __FUNCTION__, path_prefix_variable); |
| 78 | return -1; |
| 79 | } |
| 80 | char* path; |
| 81 | if (asprintf(&path, "%s/%s", path_prefix, path_suffix) == -1) { |
| 82 | fprintf(stderr, "%s: couldn't allocate \"%s/%s\"\n", __FUNCTION__, path_prefix, path_suffix); |
| 83 | return -1; |
| 84 | } |
| 85 | return path; |
| 86 | } |
| 87 | #endif |
| 88 | |
| 89 | static int __bionic_open_tzdata_path(const char* path, |
| 90 | const char* olson_id, |
| 91 | int32_t* entry_length) { |
| 92 | int fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_CLOEXEC)); |
| 93 | if (fd == -1) { |
| 94 | return -2; // Distinguish failure to find any data from failure to find a specific id. |
| 95 | } |
| 96 | |
| 97 | // byte[12] tzdata_version -- "tzdata2012f\0" |
| 98 | // int index_offset |
| 99 | // int data_offset |
| 100 | // int zonetab_offset |
| 101 | struct bionic_tzdata_header { |
| 102 | char tzdata_version[12]; |
| 103 | int32_t index_offset; |
| 104 | int32_t data_offset; |
| 105 | int32_t zonetab_offset; |
| 106 | } header; |
| 107 | memset(&header, 0, sizeof(header)); |
| 108 | ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd, &header, sizeof(header))); |
| 109 | if (bytes_read != sizeof(header)) { |
| 110 | fprintf(stderr, "%s: could not read header of \"%s\": %s\n", |
| 111 | __FUNCTION__, path, (bytes_read == -1) ? strerror(errno) : "short read"); |
| 112 | close(fd); |
| 113 | return -1; |
| 114 | } |
| 115 | |
| 116 | if (strncmp(header.tzdata_version, "tzdata", 6) != 0 || header.tzdata_version[11] != 0) { |
| 117 | fprintf(stderr, "%s: bad magic in \"%s\": \"%.6s\"\n", |
| 118 | __FUNCTION__, path, header.tzdata_version); |
| 119 | close(fd); |
| 120 | return -1; |
| 121 | } |
| 122 | |
| 123 | #if 0 |
| 124 | fprintf(stderr, "version: %s\n", header.tzdata_version); |
| 125 | fprintf(stderr, "index_offset = %d\n", ntohl(header.index_offset)); |
| 126 | fprintf(stderr, "data_offset = %d\n", ntohl(header.data_offset)); |
| 127 | fprintf(stderr, "zonetab_offset = %d\n", ntohl(header.zonetab_offset)); |
| 128 | #endif |
| 129 | |
| 130 | if (TEMP_FAILURE_RETRY(lseek(fd, ntohl(header.index_offset), SEEK_SET)) == -1) { |
| 131 | fprintf(stderr, "%s: couldn't seek to index in \"%s\": %s\n", |
| 132 | __FUNCTION__, path, strerror(errno)); |
| 133 | close(fd); |
| 134 | return -1; |
| 135 | } |
| 136 | |
| 137 | off_t specific_zone_offset = -1; |
| 138 | ssize_t index_size = ntohl(header.data_offset) - ntohl(header.index_offset); |
| 139 | char* index = new char[index_size]; |
| 140 | if (index == nullptr) { |
| 141 | fprintf(stderr, "%s: couldn't allocate %zd-byte index for \"%s\"\n", |
| 142 | __FUNCTION__, index_size, path); |
| 143 | close(fd); |
| 144 | return -1; |
| 145 | } |
| 146 | if (TEMP_FAILURE_RETRY(read(fd, index, index_size)) != index_size) { |
| 147 | fprintf(stderr, "%s: could not read index of \"%s\": %s\n", |
| 148 | __FUNCTION__, path, (bytes_read == -1) ? strerror(errno) : "short read"); |
| 149 | delete[] index; |
| 150 | close(fd); |
| 151 | return -1; |
| 152 | } |
| 153 | |
| 154 | static const size_t NAME_LENGTH = 40; |
| 155 | struct index_entry_t { |
| 156 | char buf[NAME_LENGTH]; |
| 157 | int32_t start; |
| 158 | int32_t length; |
| 159 | int32_t unused; // Was raw GMT offset; always 0 since tzdata2014f (L). |
| 160 | }; |
| 161 | |
| 162 | size_t id_count = (ntohl(header.data_offset) - ntohl(header.index_offset)) / sizeof(struct index_entry_t); |
| 163 | struct index_entry_t* entry = (struct index_entry_t*) index; |
| 164 | for (size_t i = 0; i < id_count; ++i) { |
| 165 | char this_id[NAME_LENGTH + 1]; |
| 166 | memcpy(this_id, entry->buf, NAME_LENGTH); |
| 167 | this_id[NAME_LENGTH] = '\0'; |
| 168 | |
| 169 | if (strcmp(this_id, olson_id) == 0) { |
| 170 | specific_zone_offset = ntohl(entry->start) + ntohl(header.data_offset); |
| 171 | *entry_length = ntohl(entry->length); |
| 172 | break; |
| 173 | } |
| 174 | |
| 175 | ++entry; |
| 176 | } |
| 177 | delete[] index; |
| 178 | |
| 179 | if (specific_zone_offset == -1) { |
| 180 | close(fd); |
| 181 | return -1; |
| 182 | } |
| 183 | |
| 184 | if (TEMP_FAILURE_RETRY(lseek(fd, specific_zone_offset, SEEK_SET)) == -1) { |
| 185 | fprintf(stderr, "%s: could not seek to %ld in \"%s\": %s\n", |
| 186 | __FUNCTION__, specific_zone_offset, path, strerror(errno)); |
| 187 | close(fd); |
| 188 | return -1; |
| 189 | } |
| 190 | |
| 191 | // TODO: check that there's TZ_MAGIC at this offset, so we can fall back to the other file if not. |
| 192 | |
| 193 | return fd; |
| 194 | } |
| 195 | |
| 196 | int __bionic_open_tzdata(const char* olson_id, int32_t* entry_length) { |
| 197 | int fd; |
| 198 | |
| 199 | #if defined(__ANDROID__) |
| 200 | // On Android, try the two hard-coded locations. |
| 201 | fd = __bionic_open_tzdata_path("/data/misc/zoneinfo/current/tzdata", |
| 202 | olson_id, entry_length); |
| 203 | if (fd >= 0) return fd; |
| 204 | |
| 205 | fd = __bionic_open_tzdata_path("/system/usr/share/zoneinfo/tzdata", |
| 206 | olson_id, entry_length); |
| 207 | if (fd >= 0) return fd; |
| 208 | #else |
| 209 | // On the host, we don't expect those locations to exist, and we're not |
| 210 | // worried about security so we trust $ANDROID_DATA and $ANDROID_ROOT to |
| 211 | // point us in the right direction. |
| 212 | char* path = make_path("ANDROID_DATA", "/misc/zoneinfo/current/tzdata"); |
| 213 | fd = __bionic_open_tzdata_path(path, olson_id, entry_length); |
| 214 | free(path); |
| 215 | if (fd >= 0) return fd; |
| 216 | |
| 217 | path = make_path("ANDROID_ROOT", "/usr/share/zoneinfo/tzdata"); |
| 218 | fd = __bionic_open_tzdata_path(path, olson_id, entry_length); |
| 219 | free(path); |
| 220 | if (fd >= 0) return fd; |
| 221 | #endif |
| 222 | |
| 223 | // Not finding any tzdata is more serious that not finding a specific zone, |
| 224 | // and worth logging. |
| 225 | if (fd == -2) { |
| 226 | // The first thing that 'recovery' does is try to format the current time. It doesn't have |
| 227 | // any tzdata available, so we must not abort here --- doing so breaks the recovery image! |
| 228 | fprintf(stderr, "%s: couldn't find any tzdata when looking for %s!\n", __FUNCTION__, olson_id); |
| 229 | } |
| 230 | |
| 231 | return fd; |
| 232 | } |