xref: /aosp_15_r20/bionic/libc/tzcode/bionic.cpp (revision 8d67ca893c1523eb926b9080dbe4e2ffd2a27ba1)
1*8d67ca89SAndroid Build Coastguard Worker /*
2*8d67ca89SAndroid Build Coastguard Worker  * Copyright (C) 2017 The Android Open Source Project
3*8d67ca89SAndroid Build Coastguard Worker  * All rights reserved.
4*8d67ca89SAndroid Build Coastguard Worker  *
5*8d67ca89SAndroid Build Coastguard Worker  * Redistribution and use in source and binary forms, with or without
6*8d67ca89SAndroid Build Coastguard Worker  * modification, are permitted provided that the following conditions
7*8d67ca89SAndroid Build Coastguard Worker  * are met:
8*8d67ca89SAndroid Build Coastguard Worker  *  * Redistributions of source code must retain the above copyright
9*8d67ca89SAndroid Build Coastguard Worker  *    notice, this list of conditions and the following disclaimer.
10*8d67ca89SAndroid Build Coastguard Worker  *  * Redistributions in binary form must reproduce the above copyright
11*8d67ca89SAndroid Build Coastguard Worker  *    notice, this list of conditions and the following disclaimer in
12*8d67ca89SAndroid Build Coastguard Worker  *    the documentation and/or other materials provided with the
13*8d67ca89SAndroid Build Coastguard Worker  *    distribution.
14*8d67ca89SAndroid Build Coastguard Worker  *
15*8d67ca89SAndroid Build Coastguard Worker  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16*8d67ca89SAndroid Build Coastguard Worker  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17*8d67ca89SAndroid Build Coastguard Worker  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18*8d67ca89SAndroid Build Coastguard Worker  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19*8d67ca89SAndroid Build Coastguard Worker  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20*8d67ca89SAndroid Build Coastguard Worker  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21*8d67ca89SAndroid Build Coastguard Worker  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22*8d67ca89SAndroid Build Coastguard Worker  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23*8d67ca89SAndroid Build Coastguard Worker  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24*8d67ca89SAndroid Build Coastguard Worker  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25*8d67ca89SAndroid Build Coastguard Worker  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*8d67ca89SAndroid Build Coastguard Worker  * SUCH DAMAGE.
27*8d67ca89SAndroid Build Coastguard Worker  */
28*8d67ca89SAndroid Build Coastguard Worker 
29*8d67ca89SAndroid Build Coastguard Worker #include <arpa/inet.h> // For ntohl(3).
30*8d67ca89SAndroid Build Coastguard Worker #include <errno.h>
31*8d67ca89SAndroid Build Coastguard Worker #include <fcntl.h>
32*8d67ca89SAndroid Build Coastguard Worker #include <stdint.h>
33*8d67ca89SAndroid Build Coastguard Worker #include <stdlib.h>
34*8d67ca89SAndroid Build Coastguard Worker #include <unistd.h>
35*8d67ca89SAndroid Build Coastguard Worker 
36*8d67ca89SAndroid Build Coastguard Worker #include "private/CachedProperty.h"
37*8d67ca89SAndroid Build Coastguard Worker 
38*8d67ca89SAndroid Build Coastguard Worker extern "C" void tzset_unlocked(void);
39*8d67ca89SAndroid Build Coastguard Worker extern "C" void __bionic_get_system_tz(char* buf, size_t n);
40*8d67ca89SAndroid Build Coastguard Worker extern "C" int __bionic_open_tzdata(const char*, int32_t*);
41*8d67ca89SAndroid Build Coastguard Worker 
42*8d67ca89SAndroid Build Coastguard Worker extern "C" void tzsetlcl(char const*);
43*8d67ca89SAndroid Build Coastguard Worker 
__bionic_get_system_tz(char * buf,size_t n)44*8d67ca89SAndroid Build Coastguard Worker void __bionic_get_system_tz(char* buf, size_t n) {
45*8d67ca89SAndroid Build Coastguard Worker   static CachedProperty persist_sys_timezone("persist.sys.timezone");
46*8d67ca89SAndroid Build Coastguard Worker   const char* name = persist_sys_timezone.Get();
47*8d67ca89SAndroid Build Coastguard Worker 
48*8d67ca89SAndroid Build Coastguard Worker   // If the system property is not set, perhaps because this is called
49*8d67ca89SAndroid Build Coastguard Worker   // before the default value has been set (the recovery image being a
50*8d67ca89SAndroid Build Coastguard Worker   // classic example), fall back to GMT.
51*8d67ca89SAndroid Build Coastguard Worker   if (name == nullptr) name = "GMT";
52*8d67ca89SAndroid Build Coastguard Worker 
53*8d67ca89SAndroid Build Coastguard Worker   strlcpy(buf, name, n);
54*8d67ca89SAndroid Build Coastguard Worker 
55*8d67ca89SAndroid Build Coastguard Worker   if (!strcmp(buf, "GMT")) {
56*8d67ca89SAndroid Build Coastguard Worker     // Typically we'll set the system property to an Olson ID, but
57*8d67ca89SAndroid Build Coastguard Worker     // java.util.TimeZone also supports the "GMT+xxxx" style, and at
58*8d67ca89SAndroid Build Coastguard Worker     // least historically (see http://b/25463955) some Android-based set
59*8d67ca89SAndroid Build Coastguard Worker     // top boxes would get the timezone from the TV network in this format
60*8d67ca89SAndroid Build Coastguard Worker     // and use it directly in the system property. This caused trouble
61*8d67ca89SAndroid Build Coastguard Worker     // for native code because POSIX and Java disagree about the sign in
62*8d67ca89SAndroid Build Coastguard Worker     // a timezone string. For POSIX, "GMT+3" means "3 hours west/behind",
63*8d67ca89SAndroid Build Coastguard Worker     // but for Java it means "3 hours east/ahead". Since (a) Java is the
64*8d67ca89SAndroid Build Coastguard Worker     // one that matches human expectations and (b) this system property is
65*8d67ca89SAndroid Build Coastguard Worker     // used directly by Java, we flip the sign here to translate from Java
66*8d67ca89SAndroid Build Coastguard Worker     // to POSIX. We only need to worry about the "GMT+xxxx" case because
67*8d67ca89SAndroid Build Coastguard Worker     // the expectation is that these are valid java.util.TimeZone ids,
68*8d67ca89SAndroid Build Coastguard Worker     // not general POSIX custom timezone specifications (which is why this
69*8d67ca89SAndroid Build Coastguard Worker     // code only applies to the system property, and not to the environment
70*8d67ca89SAndroid Build Coastguard Worker     // variable).
71*8d67ca89SAndroid Build Coastguard Worker     char sign = buf[3];
72*8d67ca89SAndroid Build Coastguard Worker     if (sign == '-' || sign == '+') {
73*8d67ca89SAndroid Build Coastguard Worker       buf[3] = (sign == '-') ? '+' : '-';
74*8d67ca89SAndroid Build Coastguard Worker     }
75*8d67ca89SAndroid Build Coastguard Worker   }
76*8d67ca89SAndroid Build Coastguard Worker }
77*8d67ca89SAndroid Build Coastguard Worker 
tzset_unlocked()78*8d67ca89SAndroid Build Coastguard Worker void tzset_unlocked() {
79*8d67ca89SAndroid Build Coastguard Worker   // The TZ environment variable is meant to override the system-wide setting.
80*8d67ca89SAndroid Build Coastguard Worker   const char* name = getenv("TZ");
81*8d67ca89SAndroid Build Coastguard Worker   char buf[PROP_VALUE_MAX];
82*8d67ca89SAndroid Build Coastguard Worker 
83*8d67ca89SAndroid Build Coastguard Worker   // If that's not set, look at the "persist.sys.timezone" system property.
84*8d67ca89SAndroid Build Coastguard Worker   if (name == nullptr) {
85*8d67ca89SAndroid Build Coastguard Worker     __bionic_get_system_tz(buf, sizeof(buf));
86*8d67ca89SAndroid Build Coastguard Worker     name = buf;
87*8d67ca89SAndroid Build Coastguard Worker   }
88*8d67ca89SAndroid Build Coastguard Worker 
89*8d67ca89SAndroid Build Coastguard Worker   tzsetlcl(name);
90*8d67ca89SAndroid Build Coastguard Worker }
91*8d67ca89SAndroid Build Coastguard Worker 
92*8d67ca89SAndroid Build Coastguard Worker #if !defined(__ANDROID__)
make_path(const char * path_prefix_variable,const char * path_suffix)93*8d67ca89SAndroid Build Coastguard Worker static char* make_path(const char* path_prefix_variable,
94*8d67ca89SAndroid Build Coastguard Worker                        const char* path_suffix) {
95*8d67ca89SAndroid Build Coastguard Worker   const char* path_prefix = getenv(path_prefix_variable);
96*8d67ca89SAndroid Build Coastguard Worker   if (path_prefix == nullptr) {
97*8d67ca89SAndroid Build Coastguard Worker     fprintf(stderr, "%s: %s not set!\n", __FUNCTION__, path_prefix_variable);
98*8d67ca89SAndroid Build Coastguard Worker     abort();
99*8d67ca89SAndroid Build Coastguard Worker   }
100*8d67ca89SAndroid Build Coastguard Worker   char* path;
101*8d67ca89SAndroid Build Coastguard Worker   if (asprintf(&path, "%s/%s", path_prefix, path_suffix) == -1) {
102*8d67ca89SAndroid Build Coastguard Worker     fprintf(stderr, "%s: couldn't allocate \"%s/%s\"\n", __FUNCTION__, path_prefix, path_suffix);
103*8d67ca89SAndroid Build Coastguard Worker     abort();
104*8d67ca89SAndroid Build Coastguard Worker   }
105*8d67ca89SAndroid Build Coastguard Worker   return path;
106*8d67ca89SAndroid Build Coastguard Worker }
107*8d67ca89SAndroid Build Coastguard Worker #endif
108*8d67ca89SAndroid Build Coastguard Worker 
109*8d67ca89SAndroid Build Coastguard Worker // byte[12] tzdata_version  -- "tzdata2012f\0"
110*8d67ca89SAndroid Build Coastguard Worker // int index_offset
111*8d67ca89SAndroid Build Coastguard Worker // int data_offset
112*8d67ca89SAndroid Build Coastguard Worker // int final_offset
113*8d67ca89SAndroid Build Coastguard Worker struct bionic_tzdata_header_t {
114*8d67ca89SAndroid Build Coastguard Worker   char tzdata_version[12];
115*8d67ca89SAndroid Build Coastguard Worker   int32_t index_offset;
116*8d67ca89SAndroid Build Coastguard Worker   int32_t data_offset;
117*8d67ca89SAndroid Build Coastguard Worker   int32_t final_offset;
118*8d67ca89SAndroid Build Coastguard Worker };
119*8d67ca89SAndroid Build Coastguard Worker static constexpr size_t NAME_LENGTH = 40;
120*8d67ca89SAndroid Build Coastguard Worker struct index_entry_t {
121*8d67ca89SAndroid Build Coastguard Worker   char buf[NAME_LENGTH];
122*8d67ca89SAndroid Build Coastguard Worker   int32_t start;
123*8d67ca89SAndroid Build Coastguard Worker   int32_t length;
124*8d67ca89SAndroid Build Coastguard Worker   int32_t unused; // Was raw GMT offset; always 0 since tzdata2014f (L).
125*8d67ca89SAndroid Build Coastguard Worker };
126*8d67ca89SAndroid Build Coastguard Worker 
127*8d67ca89SAndroid Build Coastguard Worker // Returns -2 for a soft failure (where the caller should try another file),
128*8d67ca89SAndroid Build Coastguard Worker // -1 for a hard failure (where the caller should give up), and >= 0 is a
129*8d67ca89SAndroid Build Coastguard Worker // file descriptor whose offset points to the data for the given olson id in
130*8d67ca89SAndroid Build Coastguard Worker // the given file (and *entry_length is the size of the data).
__bionic_open_tzdata_path(const char * path,const char * olson_id,int32_t * entry_length)131*8d67ca89SAndroid Build Coastguard Worker static int __bionic_open_tzdata_path(const char* path,
132*8d67ca89SAndroid Build Coastguard Worker                                      const char* olson_id,
133*8d67ca89SAndroid Build Coastguard Worker                                      int32_t* entry_length) {
134*8d67ca89SAndroid Build Coastguard Worker   int fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_CLOEXEC));
135*8d67ca89SAndroid Build Coastguard Worker   if (fd == -1) {
136*8d67ca89SAndroid Build Coastguard Worker     // We don't log here, because this is quite common --- current devices
137*8d67ca89SAndroid Build Coastguard Worker     // aren't expected to have the old APK tzdata, for example.
138*8d67ca89SAndroid Build Coastguard Worker     return -2;
139*8d67ca89SAndroid Build Coastguard Worker   }
140*8d67ca89SAndroid Build Coastguard Worker 
141*8d67ca89SAndroid Build Coastguard Worker   bionic_tzdata_header_t header = {};
142*8d67ca89SAndroid Build Coastguard Worker   ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd, &header, sizeof(header)));
143*8d67ca89SAndroid Build Coastguard Worker   if (bytes_read != sizeof(header)) {
144*8d67ca89SAndroid Build Coastguard Worker     fprintf(stderr, "%s: could not read header of \"%s\": %s\n",
145*8d67ca89SAndroid Build Coastguard Worker             __FUNCTION__, path, (bytes_read == -1) ? strerror(errno) : "short read");
146*8d67ca89SAndroid Build Coastguard Worker     close(fd);
147*8d67ca89SAndroid Build Coastguard Worker     return -2;
148*8d67ca89SAndroid Build Coastguard Worker   }
149*8d67ca89SAndroid Build Coastguard Worker 
150*8d67ca89SAndroid Build Coastguard Worker   if (strncmp(header.tzdata_version, "tzdata", 6) != 0 || header.tzdata_version[11] != 0) {
151*8d67ca89SAndroid Build Coastguard Worker     fprintf(stderr, "%s: bad magic in \"%s\": \"%.6s\"\n", __FUNCTION__, path, header.tzdata_version);
152*8d67ca89SAndroid Build Coastguard Worker     close(fd);
153*8d67ca89SAndroid Build Coastguard Worker     return -2;
154*8d67ca89SAndroid Build Coastguard Worker   }
155*8d67ca89SAndroid Build Coastguard Worker 
156*8d67ca89SAndroid Build Coastguard Worker   if (TEMP_FAILURE_RETRY(lseek(fd, ntohl(header.index_offset), SEEK_SET)) == -1) {
157*8d67ca89SAndroid Build Coastguard Worker     fprintf(stderr, "%s: couldn't seek to index in \"%s\": %s\n", __FUNCTION__, path, strerror(errno));
158*8d67ca89SAndroid Build Coastguard Worker     close(fd);
159*8d67ca89SAndroid Build Coastguard Worker     return -2;
160*8d67ca89SAndroid Build Coastguard Worker   }
161*8d67ca89SAndroid Build Coastguard Worker 
162*8d67ca89SAndroid Build Coastguard Worker   if (ntohl(header.index_offset) > ntohl(header.data_offset)) {
163*8d67ca89SAndroid Build Coastguard Worker     fprintf(stderr, "%s: invalid data and index offsets in \"%s\": %u %u\n",
164*8d67ca89SAndroid Build Coastguard Worker             __FUNCTION__, path, ntohl(header.data_offset), ntohl(header.index_offset));
165*8d67ca89SAndroid Build Coastguard Worker     close(fd);
166*8d67ca89SAndroid Build Coastguard Worker     return -2;
167*8d67ca89SAndroid Build Coastguard Worker   }
168*8d67ca89SAndroid Build Coastguard Worker   const size_t index_size = ntohl(header.data_offset) - ntohl(header.index_offset);
169*8d67ca89SAndroid Build Coastguard Worker   if ((index_size % sizeof(index_entry_t)) != 0) {
170*8d67ca89SAndroid Build Coastguard Worker     fprintf(stderr, "%s: invalid index size in \"%s\": %zd\n", __FUNCTION__, path, index_size);
171*8d67ca89SAndroid Build Coastguard Worker     close(fd);
172*8d67ca89SAndroid Build Coastguard Worker     return -2;
173*8d67ca89SAndroid Build Coastguard Worker   }
174*8d67ca89SAndroid Build Coastguard Worker 
175*8d67ca89SAndroid Build Coastguard Worker   char* index = reinterpret_cast<char*>(malloc(index_size));
176*8d67ca89SAndroid Build Coastguard Worker   if (index == nullptr) {
177*8d67ca89SAndroid Build Coastguard Worker     fprintf(stderr, "%s: couldn't allocate %zd-byte index for \"%s\"\n", __FUNCTION__, index_size, path);
178*8d67ca89SAndroid Build Coastguard Worker     close(fd);
179*8d67ca89SAndroid Build Coastguard Worker     return -2;
180*8d67ca89SAndroid Build Coastguard Worker   }
181*8d67ca89SAndroid Build Coastguard Worker   if (TEMP_FAILURE_RETRY(read(fd, index, index_size)) != static_cast<ssize_t>(index_size)) {
182*8d67ca89SAndroid Build Coastguard Worker     fprintf(stderr, "%s: could not read index of \"%s\": %s\n",
183*8d67ca89SAndroid Build Coastguard Worker             __FUNCTION__, path, (bytes_read == -1) ? strerror(errno) : "short read");
184*8d67ca89SAndroid Build Coastguard Worker     free(index);
185*8d67ca89SAndroid Build Coastguard Worker     close(fd);
186*8d67ca89SAndroid Build Coastguard Worker     return -2;
187*8d67ca89SAndroid Build Coastguard Worker   }
188*8d67ca89SAndroid Build Coastguard Worker 
189*8d67ca89SAndroid Build Coastguard Worker   off_t specific_zone_offset = -1;
190*8d67ca89SAndroid Build Coastguard Worker   size_t id_count = index_size / sizeof(index_entry_t);
191*8d67ca89SAndroid Build Coastguard Worker   index_entry_t* entry = reinterpret_cast<index_entry_t*>(index);
192*8d67ca89SAndroid Build Coastguard Worker   for (size_t i = 0; i < id_count; ++i) {
193*8d67ca89SAndroid Build Coastguard Worker     char this_id[NAME_LENGTH + 1];
194*8d67ca89SAndroid Build Coastguard Worker     memcpy(this_id, entry->buf, NAME_LENGTH);
195*8d67ca89SAndroid Build Coastguard Worker     this_id[NAME_LENGTH] = '\0';
196*8d67ca89SAndroid Build Coastguard Worker 
197*8d67ca89SAndroid Build Coastguard Worker     if (strcmp(this_id, olson_id) == 0) {
198*8d67ca89SAndroid Build Coastguard Worker       specific_zone_offset = ntohl(entry->start) + ntohl(header.data_offset);
199*8d67ca89SAndroid Build Coastguard Worker       *entry_length = ntohl(entry->length);
200*8d67ca89SAndroid Build Coastguard Worker       break;
201*8d67ca89SAndroid Build Coastguard Worker     }
202*8d67ca89SAndroid Build Coastguard Worker 
203*8d67ca89SAndroid Build Coastguard Worker     ++entry;
204*8d67ca89SAndroid Build Coastguard Worker   }
205*8d67ca89SAndroid Build Coastguard Worker   free(index);
206*8d67ca89SAndroid Build Coastguard Worker 
207*8d67ca89SAndroid Build Coastguard Worker   if (specific_zone_offset == -1) {
208*8d67ca89SAndroid Build Coastguard Worker     // We found a valid tzdata file, but didn't find the requested id in it.
209*8d67ca89SAndroid Build Coastguard Worker     // Give up now, and don't try fallback tzdata files. We don't log here
210*8d67ca89SAndroid Build Coastguard Worker     // because for all we know the given olson id was nonsense.
211*8d67ca89SAndroid Build Coastguard Worker     close(fd);
212*8d67ca89SAndroid Build Coastguard Worker     // This file descriptor (-1) is passed to localtime.c. In invalid fd case
213*8d67ca89SAndroid Build Coastguard Worker     // upstream passes errno value around methods and having 0 there will
214*8d67ca89SAndroid Build Coastguard Worker     // indicate that timezone was found and read successfully and localtime's
215*8d67ca89SAndroid Build Coastguard Worker     // internal state was properly initialized (which wasn't as we couldn't find
216*8d67ca89SAndroid Build Coastguard Worker     // requested timezone in the tzdata file).
217*8d67ca89SAndroid Build Coastguard Worker     // If we reached this point errno is unlikely to be touched. It is only
218*8d67ca89SAndroid Build Coastguard Worker     // close(fd) which can do it, but that is very unlikely to happen. And
219*8d67ca89SAndroid Build Coastguard Worker     // even if it happens we can't extract any useful insights from it.
220*8d67ca89SAndroid Build Coastguard Worker     // We are overriding it to ENOENT as it matches upstream expectations -
221*8d67ca89SAndroid Build Coastguard Worker     // timezone is absent in the tzdata file == there is no TZif file in
222*8d67ca89SAndroid Build Coastguard Worker     // /usr/share/zoneinfo.
223*8d67ca89SAndroid Build Coastguard Worker     errno = ENOENT;
224*8d67ca89SAndroid Build Coastguard Worker     return -1;
225*8d67ca89SAndroid Build Coastguard Worker   }
226*8d67ca89SAndroid Build Coastguard Worker 
227*8d67ca89SAndroid Build Coastguard Worker   if (TEMP_FAILURE_RETRY(lseek(fd, specific_zone_offset, SEEK_SET)) == -1) {
228*8d67ca89SAndroid Build Coastguard Worker     fprintf(stderr, "%s: could not seek to %ld in \"%s\": %s\n",
229*8d67ca89SAndroid Build Coastguard Worker             __FUNCTION__, specific_zone_offset, path, strerror(errno));
230*8d67ca89SAndroid Build Coastguard Worker     close(fd);
231*8d67ca89SAndroid Build Coastguard Worker     return -2;
232*8d67ca89SAndroid Build Coastguard Worker   }
233*8d67ca89SAndroid Build Coastguard Worker 
234*8d67ca89SAndroid Build Coastguard Worker   return fd;
235*8d67ca89SAndroid Build Coastguard Worker }
236*8d67ca89SAndroid Build Coastguard Worker 
__bionic_open_tzdata(const char * olson_id,int32_t * entry_length)237*8d67ca89SAndroid Build Coastguard Worker int __bionic_open_tzdata(const char* olson_id, int32_t* entry_length) {
238*8d67ca89SAndroid Build Coastguard Worker   int fd;
239*8d67ca89SAndroid Build Coastguard Worker 
240*8d67ca89SAndroid Build Coastguard Worker   // Try the two locations for the tzdata file in a strict order:
241*8d67ca89SAndroid Build Coastguard Worker   // 1: The timezone data module which contains the main copy. This is the
242*8d67ca89SAndroid Build Coastguard Worker   //    common case for current devices.
243*8d67ca89SAndroid Build Coastguard Worker   // 2: The ultimate fallback: the non-updatable copy in /system.
244*8d67ca89SAndroid Build Coastguard Worker 
245*8d67ca89SAndroid Build Coastguard Worker #if defined(__ANDROID__)
246*8d67ca89SAndroid Build Coastguard Worker   // On Android devices, bionic has to work even if exec takes place without
247*8d67ca89SAndroid Build Coastguard Worker   // environment variables set. So, all paths are hardcoded here.
248*8d67ca89SAndroid Build Coastguard Worker   fd = __bionic_open_tzdata_path("/apex/com.android.tzdata/etc/tz/tzdata",
249*8d67ca89SAndroid Build Coastguard Worker                                  olson_id, entry_length);
250*8d67ca89SAndroid Build Coastguard Worker   if (fd >= -1) return fd;
251*8d67ca89SAndroid Build Coastguard Worker 
252*8d67ca89SAndroid Build Coastguard Worker   fd = __bionic_open_tzdata_path("/system/usr/share/zoneinfo/tzdata",
253*8d67ca89SAndroid Build Coastguard Worker                                  olson_id, entry_length);
254*8d67ca89SAndroid Build Coastguard Worker   if (fd >= -1) return fd;
255*8d67ca89SAndroid Build Coastguard Worker #else
256*8d67ca89SAndroid Build Coastguard Worker   // On the host, we don't expect the hard-coded locations above to exist, and
257*8d67ca89SAndroid Build Coastguard Worker   // we're not worried about security so we trust $ANDROID_TZDATA_ROOT, and
258*8d67ca89SAndroid Build Coastguard Worker   // $ANDROID_ROOT to point us in the right direction instead.
259*8d67ca89SAndroid Build Coastguard Worker 
260*8d67ca89SAndroid Build Coastguard Worker   char* path = make_path("ANDROID_TZDATA_ROOT", "/etc/tz/tzdata");
261*8d67ca89SAndroid Build Coastguard Worker   fd = __bionic_open_tzdata_path(path, olson_id, entry_length);
262*8d67ca89SAndroid Build Coastguard Worker   free(path);
263*8d67ca89SAndroid Build Coastguard Worker   if (fd >= -1) return fd;
264*8d67ca89SAndroid Build Coastguard Worker 
265*8d67ca89SAndroid Build Coastguard Worker   path = make_path("ANDROID_ROOT", "/usr/share/zoneinfo/tzdata");
266*8d67ca89SAndroid Build Coastguard Worker   fd = __bionic_open_tzdata_path(path, olson_id, entry_length);
267*8d67ca89SAndroid Build Coastguard Worker   free(path);
268*8d67ca89SAndroid Build Coastguard Worker   if (fd >= -1) return fd;
269*8d67ca89SAndroid Build Coastguard Worker #endif
270*8d67ca89SAndroid Build Coastguard Worker 
271*8d67ca89SAndroid Build Coastguard Worker   // Not finding any tzdata is more serious that not finding a specific zone,
272*8d67ca89SAndroid Build Coastguard Worker   // and worth logging.
273*8d67ca89SAndroid Build Coastguard Worker   if (fd == -2) {
274*8d67ca89SAndroid Build Coastguard Worker     // The first thing that 'recovery' does is try to format the current time. It doesn't have
275*8d67ca89SAndroid Build Coastguard Worker     // any tzdata available, so we must not abort here --- doing so breaks the recovery image!
276*8d67ca89SAndroid Build Coastguard Worker     fprintf(stderr, "%s: couldn't find any tzdata when looking for %s!\n", __FUNCTION__, olson_id);
277*8d67ca89SAndroid Build Coastguard Worker   }
278*8d67ca89SAndroid Build Coastguard Worker 
279*8d67ca89SAndroid Build Coastguard Worker   // Otherwise we were successful.
280*8d67ca89SAndroid Build Coastguard Worker   return fd;
281*8d67ca89SAndroid Build Coastguard Worker }
282