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). |
Dan Albert | 1c78cb0 | 2017-10-11 11:25:25 -0700 | [diff] [blame] | 30 | #include <errno.h> |
Elliott Hughes | 0e8616a | 2017-04-11 14:44:51 -0700 | [diff] [blame] | 31 | #include <fcntl.h> |
| 32 | #include <stdint.h> |
| 33 | #include <stdlib.h> |
| 34 | #include <unistd.h> |
| 35 | |
| 36 | #include "private/CachedProperty.h" |
| 37 | |
| 38 | extern "C" void tzset_unlocked(void); |
| 39 | extern "C" int __bionic_open_tzdata(const char*, int32_t*); |
| 40 | |
| 41 | extern "C" void tzsetlcl(char const*); |
| 42 | |
| 43 | void tzset_unlocked() { |
| 44 | // The TZ environment variable is meant to override the system-wide setting. |
| 45 | const char* name = getenv("TZ"); |
| 46 | char buf[PROP_VALUE_MAX]; |
| 47 | |
| 48 | // If that's not set, look at the "persist.sys.timezone" system property. |
| 49 | if (name == nullptr) { |
| 50 | static CachedProperty persist_sys_timezone("persist.sys.timezone"); |
| 51 | |
| 52 | if ((name = persist_sys_timezone.Get()) != nullptr && strlen(name) > 3) { |
| 53 | // POSIX and Java disagree about the sign in a timezone string. For POSIX, "GMT+3" means |
| 54 | // "3 hours west/behind", but for Java it means "3 hours east/ahead". Since (a) Java is |
| 55 | // the one that matches human expectations and (b) this system property is used directly |
| 56 | // by Java, we flip the sign here to translate from Java to POSIX. http://b/25463955. |
| 57 | char sign = name[3]; |
| 58 | if (sign == '-' || sign == '+') { |
| 59 | strlcpy(buf, name, sizeof(buf)); |
| 60 | buf[3] = (sign == '-') ? '+' : '-'; |
| 61 | name = buf; |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | // If the system property is also not available (because you're running AOSP on a WiFi-only |
| 67 | // device, say), fall back to GMT. |
| 68 | if (name == nullptr) name = "GMT"; |
| 69 | |
| 70 | tzsetlcl(name); |
| 71 | } |
| 72 | |
| 73 | #if !defined(__ANDROID__) |
| 74 | static char* make_path(const char* path_prefix_variable, |
| 75 | const char* path_suffix) { |
| 76 | const char* path_prefix = getenv(path_prefix_variable); |
| 77 | if (path_prefix == nullptr) { |
| 78 | fprintf(stderr, "%s: %s not set!\n", __FUNCTION__, path_prefix_variable); |
Elliott Hughes | a01cbaa | 2017-04-12 12:03:44 -0700 | [diff] [blame] | 79 | abort(); |
Elliott Hughes | 0e8616a | 2017-04-11 14:44:51 -0700 | [diff] [blame] | 80 | } |
| 81 | char* path; |
| 82 | if (asprintf(&path, "%s/%s", path_prefix, path_suffix) == -1) { |
| 83 | fprintf(stderr, "%s: couldn't allocate \"%s/%s\"\n", __FUNCTION__, path_prefix, path_suffix); |
Elliott Hughes | a01cbaa | 2017-04-12 12:03:44 -0700 | [diff] [blame] | 84 | abort(); |
Elliott Hughes | 0e8616a | 2017-04-11 14:44:51 -0700 | [diff] [blame] | 85 | } |
| 86 | return path; |
| 87 | } |
| 88 | #endif |
| 89 | |
Elliott Hughes | a01cbaa | 2017-04-12 12:03:44 -0700 | [diff] [blame] | 90 | // byte[12] tzdata_version -- "tzdata2012f\0" |
| 91 | // int index_offset |
| 92 | // int data_offset |
Neil Fuller | 2b76a94 | 2020-05-21 14:53:58 +0100 | [diff] [blame] | 93 | // int final_offset |
Elliott Hughes | a01cbaa | 2017-04-12 12:03:44 -0700 | [diff] [blame] | 94 | struct bionic_tzdata_header_t { |
| 95 | char tzdata_version[12]; |
| 96 | int32_t index_offset; |
| 97 | int32_t data_offset; |
Neil Fuller | 2b76a94 | 2020-05-21 14:53:58 +0100 | [diff] [blame] | 98 | int32_t final_offset; |
Elliott Hughes | a01cbaa | 2017-04-12 12:03:44 -0700 | [diff] [blame] | 99 | }; |
| 100 | static constexpr size_t NAME_LENGTH = 40; |
| 101 | struct index_entry_t { |
| 102 | char buf[NAME_LENGTH]; |
| 103 | int32_t start; |
| 104 | int32_t length; |
| 105 | int32_t unused; // Was raw GMT offset; always 0 since tzdata2014f (L). |
| 106 | }; |
| 107 | |
Elliott Hughes | 528e29d | 2020-06-22 12:55:12 -0700 | [diff] [blame] | 108 | // Returns -2 for a soft failure (where the caller should try another file), |
| 109 | // -1 for a hard failure (where the caller should give up), and >= 0 is a |
| 110 | // file descriptor whose offset points to the data for the given olson id in |
| 111 | // the given file (and *entry_length is the size of the data). |
Elliott Hughes | 0e8616a | 2017-04-11 14:44:51 -0700 | [diff] [blame] | 112 | static int __bionic_open_tzdata_path(const char* path, |
| 113 | const char* olson_id, |
| 114 | int32_t* entry_length) { |
| 115 | int fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_CLOEXEC)); |
| 116 | if (fd == -1) { |
Elliott Hughes | 528e29d | 2020-06-22 12:55:12 -0700 | [diff] [blame] | 117 | // We don't log here, because this is quite common --- current devices |
| 118 | // aren't expected to have the old APK tzdata, for example. |
| 119 | return -2; |
Elliott Hughes | 0e8616a | 2017-04-11 14:44:51 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Elliott Hughes | a01cbaa | 2017-04-12 12:03:44 -0700 | [diff] [blame] | 122 | bionic_tzdata_header_t header = {}; |
Elliott Hughes | 0e8616a | 2017-04-11 14:44:51 -0700 | [diff] [blame] | 123 | ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd, &header, sizeof(header))); |
| 124 | if (bytes_read != sizeof(header)) { |
| 125 | fprintf(stderr, "%s: could not read header of \"%s\": %s\n", |
| 126 | __FUNCTION__, path, (bytes_read == -1) ? strerror(errno) : "short read"); |
| 127 | close(fd); |
Elliott Hughes | 528e29d | 2020-06-22 12:55:12 -0700 | [diff] [blame] | 128 | return -2; |
Elliott Hughes | 0e8616a | 2017-04-11 14:44:51 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | if (strncmp(header.tzdata_version, "tzdata", 6) != 0 || header.tzdata_version[11] != 0) { |
Elliott Hughes | a01cbaa | 2017-04-12 12:03:44 -0700 | [diff] [blame] | 132 | fprintf(stderr, "%s: bad magic in \"%s\": \"%.6s\"\n", __FUNCTION__, path, header.tzdata_version); |
Elliott Hughes | 0e8616a | 2017-04-11 14:44:51 -0700 | [diff] [blame] | 133 | close(fd); |
Elliott Hughes | 528e29d | 2020-06-22 12:55:12 -0700 | [diff] [blame] | 134 | return -2; |
Elliott Hughes | 0e8616a | 2017-04-11 14:44:51 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Elliott Hughes | 0e8616a | 2017-04-11 14:44:51 -0700 | [diff] [blame] | 137 | if (TEMP_FAILURE_RETRY(lseek(fd, ntohl(header.index_offset), SEEK_SET)) == -1) { |
Elliott Hughes | a01cbaa | 2017-04-12 12:03:44 -0700 | [diff] [blame] | 138 | fprintf(stderr, "%s: couldn't seek to index in \"%s\": %s\n", __FUNCTION__, path, strerror(errno)); |
| 139 | close(fd); |
Elliott Hughes | 528e29d | 2020-06-22 12:55:12 -0700 | [diff] [blame] | 140 | return -2; |
Elliott Hughes | a01cbaa | 2017-04-12 12:03:44 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Christopher Ferris | 35dfaa8 | 2017-04-14 12:58:24 -0700 | [diff] [blame] | 143 | if (ntohl(header.index_offset) > ntohl(header.data_offset)) { |
Elliott Hughes | a01cbaa | 2017-04-12 12:03:44 -0700 | [diff] [blame] | 144 | fprintf(stderr, "%s: invalid data and index offsets in \"%s\": %u %u\n", |
| 145 | __FUNCTION__, path, ntohl(header.data_offset), ntohl(header.index_offset)); |
| 146 | close(fd); |
Elliott Hughes | 528e29d | 2020-06-22 12:55:12 -0700 | [diff] [blame] | 147 | return -2; |
Elliott Hughes | a01cbaa | 2017-04-12 12:03:44 -0700 | [diff] [blame] | 148 | } |
| 149 | const size_t index_size = ntohl(header.data_offset) - ntohl(header.index_offset); |
| 150 | if ((index_size % sizeof(index_entry_t)) != 0) { |
| 151 | fprintf(stderr, "%s: invalid index size in \"%s\": %zd\n", __FUNCTION__, path, index_size); |
Elliott Hughes | 0e8616a | 2017-04-11 14:44:51 -0700 | [diff] [blame] | 152 | close(fd); |
Elliott Hughes | 528e29d | 2020-06-22 12:55:12 -0700 | [diff] [blame] | 153 | return -2; |
Elliott Hughes | 0e8616a | 2017-04-11 14:44:51 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Elliott Hughes | a01cbaa | 2017-04-12 12:03:44 -0700 | [diff] [blame] | 156 | char* index = reinterpret_cast<char*>(malloc(index_size)); |
Elliott Hughes | 0e8616a | 2017-04-11 14:44:51 -0700 | [diff] [blame] | 157 | if (index == nullptr) { |
Elliott Hughes | a01cbaa | 2017-04-12 12:03:44 -0700 | [diff] [blame] | 158 | fprintf(stderr, "%s: couldn't allocate %zd-byte index for \"%s\"\n", __FUNCTION__, index_size, path); |
Elliott Hughes | 0e8616a | 2017-04-11 14:44:51 -0700 | [diff] [blame] | 159 | close(fd); |
Elliott Hughes | 528e29d | 2020-06-22 12:55:12 -0700 | [diff] [blame] | 160 | return -2; |
Elliott Hughes | 0e8616a | 2017-04-11 14:44:51 -0700 | [diff] [blame] | 161 | } |
Elliott Hughes | a01cbaa | 2017-04-12 12:03:44 -0700 | [diff] [blame] | 162 | if (TEMP_FAILURE_RETRY(read(fd, index, index_size)) != static_cast<ssize_t>(index_size)) { |
Elliott Hughes | 0e8616a | 2017-04-11 14:44:51 -0700 | [diff] [blame] | 163 | fprintf(stderr, "%s: could not read index of \"%s\": %s\n", |
| 164 | __FUNCTION__, path, (bytes_read == -1) ? strerror(errno) : "short read"); |
Elliott Hughes | a01cbaa | 2017-04-12 12:03:44 -0700 | [diff] [blame] | 165 | free(index); |
Elliott Hughes | 0e8616a | 2017-04-11 14:44:51 -0700 | [diff] [blame] | 166 | close(fd); |
Elliott Hughes | 528e29d | 2020-06-22 12:55:12 -0700 | [diff] [blame] | 167 | return -2; |
Elliott Hughes | 0e8616a | 2017-04-11 14:44:51 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Elliott Hughes | 528e29d | 2020-06-22 12:55:12 -0700 | [diff] [blame] | 170 | off_t specific_zone_offset = -1; |
Elliott Hughes | a01cbaa | 2017-04-12 12:03:44 -0700 | [diff] [blame] | 171 | size_t id_count = index_size / sizeof(index_entry_t); |
| 172 | index_entry_t* entry = reinterpret_cast<index_entry_t*>(index); |
Elliott Hughes | 0e8616a | 2017-04-11 14:44:51 -0700 | [diff] [blame] | 173 | for (size_t i = 0; i < id_count; ++i) { |
| 174 | char this_id[NAME_LENGTH + 1]; |
| 175 | memcpy(this_id, entry->buf, NAME_LENGTH); |
| 176 | this_id[NAME_LENGTH] = '\0'; |
| 177 | |
| 178 | if (strcmp(this_id, olson_id) == 0) { |
| 179 | specific_zone_offset = ntohl(entry->start) + ntohl(header.data_offset); |
| 180 | *entry_length = ntohl(entry->length); |
| 181 | break; |
| 182 | } |
| 183 | |
| 184 | ++entry; |
| 185 | } |
Elliott Hughes | a01cbaa | 2017-04-12 12:03:44 -0700 | [diff] [blame] | 186 | free(index); |
Elliott Hughes | 0e8616a | 2017-04-11 14:44:51 -0700 | [diff] [blame] | 187 | |
| 188 | if (specific_zone_offset == -1) { |
Elliott Hughes | 528e29d | 2020-06-22 12:55:12 -0700 | [diff] [blame] | 189 | // We found a valid tzdata file, but didn't find the requested id in it. |
| 190 | // Give up now, and don't try fallback tzdata files. We don't log here |
| 191 | // because for all we know the given olson id was nonsense. |
Elliott Hughes | 0e8616a | 2017-04-11 14:44:51 -0700 | [diff] [blame] | 192 | close(fd); |
| 193 | return -1; |
| 194 | } |
| 195 | |
| 196 | if (TEMP_FAILURE_RETRY(lseek(fd, specific_zone_offset, SEEK_SET)) == -1) { |
| 197 | fprintf(stderr, "%s: could not seek to %ld in \"%s\": %s\n", |
| 198 | __FUNCTION__, specific_zone_offset, path, strerror(errno)); |
| 199 | close(fd); |
Elliott Hughes | 528e29d | 2020-06-22 12:55:12 -0700 | [diff] [blame] | 200 | return -2; |
Elliott Hughes | 0e8616a | 2017-04-11 14:44:51 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Elliott Hughes | 0e8616a | 2017-04-11 14:44:51 -0700 | [diff] [blame] | 203 | return fd; |
| 204 | } |
| 205 | |
| 206 | int __bionic_open_tzdata(const char* olson_id, int32_t* entry_length) { |
| 207 | int fd; |
| 208 | |
Neil Fuller | 41636ca | 2019-06-10 12:20:21 +0100 | [diff] [blame] | 209 | // Try the three locations for the tzdata file in a strict order: |
Neil Fuller | a50e51a | 2018-11-12 18:45:57 +0000 | [diff] [blame] | 210 | // 1: The O-MR1 time zone updates via APK update mechanism. This is |
Neil Fuller | 41636ca | 2019-06-10 12:20:21 +0100 | [diff] [blame] | 211 | // tried first because it allows us to test that the time zone updates |
| 212 | // via APK mechanism still works even on devices with the time zone |
| 213 | // module. |
Elliott Hughes | 528e29d | 2020-06-22 12:55:12 -0700 | [diff] [blame] | 214 | // TODO: remove this when those devices are no longer supported. |
Neil Fuller | 41636ca | 2019-06-10 12:20:21 +0100 | [diff] [blame] | 215 | // 2: The time zone data module which contains the main copy. This is the |
Elliott Hughes | 528e29d | 2020-06-22 12:55:12 -0700 | [diff] [blame] | 216 | // common case for current devices. |
Neil Fuller | 41636ca | 2019-06-10 12:20:21 +0100 | [diff] [blame] | 217 | // 3: The ultimate fallback: the non-updatable copy in /system. |
| 218 | |
| 219 | #if defined(__ANDROID__) |
| 220 | // On Android devices, bionic has to work even if exec takes place without |
| 221 | // environment variables set. So, all paths are hardcoded here. |
| 222 | |
Elliott Hughes | 0e8616a | 2017-04-11 14:44:51 -0700 | [diff] [blame] | 223 | fd = __bionic_open_tzdata_path("/data/misc/zoneinfo/current/tzdata", |
| 224 | olson_id, entry_length); |
Elliott Hughes | 528e29d | 2020-06-22 12:55:12 -0700 | [diff] [blame] | 225 | if (fd >= -1) return fd; |
Elliott Hughes | 0e8616a | 2017-04-11 14:44:51 -0700 | [diff] [blame] | 226 | |
Neil Fuller | 9268977 | 2018-12-06 14:33:12 +0000 | [diff] [blame] | 227 | fd = __bionic_open_tzdata_path("/apex/com.android.tzdata/etc/tz/tzdata", |
Neil Fuller | 7dfeaf8 | 2018-10-30 20:19:35 +0000 | [diff] [blame] | 228 | olson_id, entry_length); |
Elliott Hughes | 528e29d | 2020-06-22 12:55:12 -0700 | [diff] [blame] | 229 | if (fd >= -1) return fd; |
Neil Fuller | 7dfeaf8 | 2018-10-30 20:19:35 +0000 | [diff] [blame] | 230 | |
Elliott Hughes | 0e8616a | 2017-04-11 14:44:51 -0700 | [diff] [blame] | 231 | fd = __bionic_open_tzdata_path("/system/usr/share/zoneinfo/tzdata", |
| 232 | olson_id, entry_length); |
Elliott Hughes | 528e29d | 2020-06-22 12:55:12 -0700 | [diff] [blame] | 233 | if (fd >= -1) return fd; |
Elliott Hughes | 0e8616a | 2017-04-11 14:44:51 -0700 | [diff] [blame] | 234 | #else |
Neil Fuller | 41636ca | 2019-06-10 12:20:21 +0100 | [diff] [blame] | 235 | // On the host, we don't expect the hard-coded locations above to exist, and |
| 236 | // we're not worried about security so we trust $ANDROID_DATA, |
| 237 | // $ANDROID_TZDATA_ROOT, and $ANDROID_ROOT to point us in the right direction |
| 238 | // instead. |
| 239 | |
Elliott Hughes | 0e8616a | 2017-04-11 14:44:51 -0700 | [diff] [blame] | 240 | char* path = make_path("ANDROID_DATA", "/misc/zoneinfo/current/tzdata"); |
| 241 | fd = __bionic_open_tzdata_path(path, olson_id, entry_length); |
| 242 | free(path); |
Elliott Hughes | 528e29d | 2020-06-22 12:55:12 -0700 | [diff] [blame] | 243 | if (fd >= -1) return fd; |
Elliott Hughes | 0e8616a | 2017-04-11 14:44:51 -0700 | [diff] [blame] | 244 | |
Neil Fuller | 81b3bdd | 2019-03-13 15:22:47 +0000 | [diff] [blame] | 245 | path = make_path("ANDROID_TZDATA_ROOT", "/etc/tz/tzdata"); |
| 246 | fd = __bionic_open_tzdata_path(path, olson_id, entry_length); |
| 247 | free(path); |
Elliott Hughes | 528e29d | 2020-06-22 12:55:12 -0700 | [diff] [blame] | 248 | if (fd >= -1) return fd; |
Neil Fuller | 81b3bdd | 2019-03-13 15:22:47 +0000 | [diff] [blame] | 249 | |
Elliott Hughes | 0e8616a | 2017-04-11 14:44:51 -0700 | [diff] [blame] | 250 | path = make_path("ANDROID_ROOT", "/usr/share/zoneinfo/tzdata"); |
| 251 | fd = __bionic_open_tzdata_path(path, olson_id, entry_length); |
| 252 | free(path); |
Elliott Hughes | 528e29d | 2020-06-22 12:55:12 -0700 | [diff] [blame] | 253 | if (fd >= -1) return fd; |
Elliott Hughes | 0e8616a | 2017-04-11 14:44:51 -0700 | [diff] [blame] | 254 | #endif |
| 255 | |
| 256 | // Not finding any tzdata is more serious that not finding a specific zone, |
| 257 | // and worth logging. |
| 258 | if (fd == -2) { |
| 259 | // The first thing that 'recovery' does is try to format the current time. It doesn't have |
| 260 | // any tzdata available, so we must not abort here --- doing so breaks the recovery image! |
| 261 | fprintf(stderr, "%s: couldn't find any tzdata when looking for %s!\n", __FUNCTION__, olson_id); |
| 262 | } |
| 263 | |
Elliott Hughes | 528e29d | 2020-06-22 12:55:12 -0700 | [diff] [blame] | 264 | // Otherwise we were successful. |
Elliott Hughes | 0e8616a | 2017-04-11 14:44:51 -0700 | [diff] [blame] | 265 | return fd; |
| 266 | } |