1 #include "time_impl.h"
2 #include <errno.h>
3 #include <fcntl.h>
4 #include <stdint.h>
5 #include <limits.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <sys/mman.h>
9 #include <ctype.h>
10 #include <unistd.h>
11 #include "libc.h"
12 #include "lock.h"
13 #include "fork_impl.h"
14
15 #include "time_impl.h"
16 #ifdef OHOS_ENABLE_PARAMETER
17 #include "sys_param.h"
18 #define SYSPARAM_LENGTH 40
19 #endif
20 #define __TZ_VERSION__ '2'
21
22 #define malloc __libc_malloc
23 #define calloc undef
24 #define realloc undef
25 #define free undef
26
27 long __timezone = 0;
28 int __daylight = 0;
29 char *__tzname[2] = { 0, 0 };
30
31 weak_alias(__timezone, timezone);
32 weak_alias(__daylight, daylight);
33 weak_alias(__tzname, tzname);
34
35 static char std_name[TZNAME_MAX+1];
36 static char dst_name[TZNAME_MAX+1];
37 const char __utc[] = "UTC";
38 const char __gmt[] = "GMT";
39
40 static int dst_off;
41 static int r0[5], r1[5];
42
43 static const unsigned char *zi, *trans, *index, *types, *abbrevs, *abbrevs_end, *tzdata_map;
44 static size_t map_size, tzdata_map_size, map_offset;
45
46 static char old_tz_buf[32];
47 static char *old_tz = old_tz_buf;
48 static size_t old_tz_size = sizeof old_tz_buf;
49 static int cota_accessable = 0;
50
51 #if defined(OHOS_ENABLE_PARAMETER) && (!defined(__LITEOS__))
52 static CachedHandle cota_param_handle = NULL;
53 static CachedHandle tz_param_handle = NULL;
54 #endif
55
56 static volatile int lock[1];
57 volatile int *const __timezone_lockptr = lock;
58
InitTimeZoneParam(void)59 void InitTimeZoneParam(void)
60 {
61 #if defined(OHOS_ENABLE_PARAMETER) && (!defined(__LITEOS__))
62 if (cota_param_handle == NULL) {
63 cota_param_handle = CachedParameterCreate("persist.global.tz_override", "false");
64 }
65 if (tz_param_handle == NULL) {
66 tz_param_handle = CachedParameterCreate("persist.time.timezone", "/etc/localtime");
67 }
68 #endif
69 }
70
getint(const char ** p)71 static int getint(const char **p)
72 {
73 unsigned x;
74 for (x=0; **p-'0'<10U; (*p)++) x = **p-'0' + 10*x;
75 return x;
76 }
77
getoff(const char ** p)78 static int getoff(const char **p)
79 {
80 int neg = 0;
81 if (**p == '-') {
82 ++*p;
83 neg = 1;
84 } else if (**p == '+') {
85 ++*p;
86 }
87 int off = 3600*getint(p);
88 if (**p == ':') {
89 ++*p;
90 off += 60*getint(p);
91 if (**p == ':') {
92 ++*p;
93 off += getint(p);
94 }
95 }
96 return neg ? -off : off;
97 }
98
getrule(const char ** p,int rule[5])99 static void getrule(const char **p, int rule[5])
100 {
101 int r = rule[0] = **p;
102
103 if (r!='M') {
104 if (r=='J') ++*p;
105 else rule[0] = 0;
106 rule[1] = getint(p);
107 } else {
108 ++*p; rule[1] = getint(p);
109 ++*p; rule[2] = getint(p);
110 ++*p; rule[3] = getint(p);
111 }
112
113 if (**p=='/') {
114 ++*p;
115 rule[4] = getoff(p);
116 } else {
117 rule[4] = 7200;
118 }
119 }
120
getname(char * d,const char ** p)121 static void getname(char *d, const char **p)
122 {
123 int i;
124 if (**p == '<') {
125 ++*p;
126 for (i=0; (*p)[i] && (*p)[i]!='>'; i++)
127 if (i<TZNAME_MAX) d[i] = (*p)[i];
128 if ((*p)[i]) ++*p;
129 } else {
130 for (i=0; ((*p)[i]|32)-'a'<26U; i++)
131 if (i<TZNAME_MAX) d[i] = (*p)[i];
132 }
133 *p += i;
134 d[i<TZNAME_MAX?i:TZNAME_MAX] = 0;
135 }
136
137 #define VEC(...) ((const unsigned char[]){__VA_ARGS__})
138
zi_read32(const unsigned char * z)139 static uint32_t zi_read32(const unsigned char *z)
140 {
141 return (unsigned)z[0]<<24 | z[1]<<16 | z[2]<<8 | z[3];
142 }
143
zi_dotprod(const unsigned char * z,const unsigned char * v,size_t n)144 static size_t zi_dotprod(const unsigned char *z, const unsigned char *v, size_t n)
145 {
146 size_t y;
147 uint32_t x;
148 for (y=0; n; n--, z+=4, v++) {
149 x = zi_read32(z);
150 y += x * *v;
151 }
152 return y;
153 }
154
check_cota(void)155 static int check_cota(void)
156 {
157 #if defined(OHOS_ENABLE_PARAMETER) && (!defined(__LITEOS__))
158 static int cota_exist = 0;
159 if (cota_exist) return 1;
160 if (cota_param_handle == NULL) {
161 cota_param_handle = CachedParameterCreate("persist.global.tz_override", "false");
162 }
163 const char *cota_param_value = CachedParameterGet(cota_param_handle);
164 if (cota_param_value != NULL && !strcmp(cota_param_value, "true")) {
165 cota_exist = 1;
166 return 1;
167 }
168 return 0;
169 #else
170 return 0;
171 #endif
172 }
173
do_tzset(int use_env)174 static void do_tzset(int use_env)
175 {
176 char buf[NAME_MAX+25], *pathname=buf+24;
177 const char *try, *p;
178 const char *s = NULL;
179 const unsigned char *map = 0;
180 size_t i;
181 static const char cota_path[] = "/etc/tzdata_distro/tzdata\0";
182
183 /* Cota timezone data needs to be mounted at startup, some app's
184 * startup timezone is before cota timezone data mounted time.
185 * In addition, because of sandbox mechanism, app can't read cota
186 * timezone data at startup. Therefore, it is necessary to read
187 * System param and cota file reading attempt to ensure cota
188 * timezone data is loaded.*/
189 int use_cota = 0;
190 int keep_old_tzid = 1;
191 int old_errno = errno;
192 if (!cota_accessable) {
193 if (check_cota()) {
194 if (access(cota_path, R_OK) == 0) {
195 use_cota = 1;
196 keep_old_tzid = 0;
197 cota_accessable = 1;
198 }
199 }
200 errno = old_errno;
201 } else {
202 use_cota = 1;
203 }
204
205 #ifndef __LITEOS__
206 static const char search[] =
207 "/etc/zoneinfo/tzdata\0/usr/share/zoneinfo/tzdata\0/share/zoneinfo/tzdata\0";
208 #else
209 static const char search[] =
210 "/usr/share/zoneinfo/tzdata\0/share/zoneinfo/tzdata\0/etc/zoneinfo/tzdata\0";
211 #endif
212
213 if (use_env == 1) {
214 s = getenv("TZ");
215 }
216
217 if (!s) {
218 #if defined(OHOS_ENABLE_PARAMETER) && (!defined(__LITEOS__))
219 if (tz_param_handle == NULL) {
220 tz_param_handle = CachedParameterCreate("persist.time.timezone", "/etc/localtime");
221 }
222 const char *tz_param_value = CachedParameterGet(tz_param_handle);
223 if (tz_param_value != NULL) {
224 s = tz_param_value;
225 } else {
226 s = "/etc/localtime";
227 }
228 #else
229 s = "/etc/localtime";
230 #endif
231 }
232 if (!*s) s = __utc;
233
234 if (keep_old_tzid && old_tz && !strcmp(s, old_tz)) return;
235
236 for (i=0; i<5; i++) r0[i] = r1[i] = 0;
237
238 if (tzdata_map && zi) {
239 if (tzdata_map + map_offset == zi) {
240 __munmap((void *)tzdata_map, tzdata_map_size);
241 } else {
242 __munmap((void *)zi, map_size);
243 }
244 }
245
246 /* Cache the old value of TZ to check if it has changed. Avoid
247 * free so as not to pull it into static programs. Growth
248 * strategy makes it so free would have minimal benefit anyway. */
249 i = strlen(s);
250 if (i > PATH_MAX+1) s = __utc, i = 3;
251 if (i >= old_tz_size) {
252 old_tz_size *= 2;
253 if (i >= old_tz_size) old_tz_size = i+1;
254 if (old_tz_size > PATH_MAX+2) old_tz_size = PATH_MAX+2;
255 old_tz = malloc(old_tz_size);
256 }
257 if (old_tz) memcpy(old_tz, s, i+1);
258
259 int posix_form = 0;
260 if (*s != ':') {
261 p = s;
262 char dummy_name[TZNAME_MAX+1];
263 getname(dummy_name, &p);
264 if (p!=s && (*p == '+' || *p == '-' || isdigit(*p)
265 || !strcmp(dummy_name, "UTC")
266 || !strcmp(dummy_name, "GMT")))
267 posix_form = 1;
268 }
269
270 /* Non-suid can use an absolute tzfile pathname or a relative
271 * pathame beginning with "."; in secure mode, only the
272 * standard path will be searched. */
273 #ifndef __LITEOS__
274 int flag = 1;
275 if (!posix_form) {
276 if (*s == ':') s++;
277 if (*s == '/' || *s == '.') {
278 /* The path is invalid, use the default value. */
279 flag = 0;
280 if (!libc.secure || !strcmp(s, "/etc/localtime")) {
281 map = __map_file(s, &map_size);
282 map_offset = 0;
283 }
284 }
285 }
286
287 if (flag) {
288 /* Adapt to time zone names, such as Asia/Shanghai or Shanghai*/
289 size_t l = strlen(s);
290 if (l <= NAME_MAX && !strchr(s, '.')) {
291 memcpy(pathname, s, l+1);
292 pathname[l] = 0;
293 /* Try to load distro timezone data first*/
294 if (use_cota) {
295 tzdata_map = __map_tzdata_file(cota_path, pathname, &tzdata_map_size, &map_offset, &map_size);
296 if (tzdata_map != NULL) {
297 map = tzdata_map + map_offset;
298 }
299 }
300 try = search;
301 while (!map && *try) {
302 tzdata_map = __map_tzdata_file(try, pathname, &tzdata_map_size, &map_offset, &map_size);
303 if (tzdata_map != NULL) {
304 map = tzdata_map + map_offset;
305 }
306 try += strlen(try) + 1;
307 }
308 }
309 }
310 #else
311 if (!posix_form) {
312 if (*s == ':') s++;
313 if (*s == '/' || *s == '.') {
314 if (!libc.secure || !strcmp(s, "/etc/localtime"))
315 map = __map_file(s, &map_size);
316 map_offset = 0;
317 } else {
318 size_t l = strlen(s);
319 if (l <= NAME_MAX && !strchr(s, '.')) {
320 memcpy(pathname, s, l+1);
321 pathname[l] = 0;
322 for (try=search; !map && *try; try+=l+1) {
323 l = strlen(try);
324 memcpy(pathname-l, try, l);
325 map = __map_file(pathname-l, &map_size);
326 map_offset = 0;
327 }
328 }
329 }
330 if (!map) s = __utc;
331 }
332 #endif
333
334 if (map && (map_size < 44 || memcmp(map, "TZif", 4))) {
335 if (tzdata_map + map_offset == map) {
336 __munmap((void *)tzdata_map, tzdata_map_size);
337 tzdata_map = 0;
338 } else {
339 __munmap((void *)map, map_size);
340 }
341 map = 0;
342 s = __utc;
343 }
344
345 zi = map;
346 if (map) {
347 int scale = 2;
348 /*
349 * map[0]-map[3]: magic, it is TZif
350 * map[4]: version, '\0' or '2' or '3' as of 2013
351 * map[5]-map[19]: reserved; must be zero
352 * map[20]-map[23]: The number of UT/local indicators stored in the file.
353 * map[24]-map[27]: The number of standard/wall indicators stored in the file.
354 * map[24]-map[31]: The number of leap seconds for which data entries are stored in the file.
355 * map[32]-map[35]: The number of transition times for which data entries are stored in the file.
356 * map[36]-map[39]: The number of local time types for which data entries are
357 * stored in the file (must not be zero).
358 * map[40]-map[43]: The number of bytes of time zone abbreviation strings stored in the file.
359
360 * If map[4] is '2' or greater, the above is followed by a second instance
361 * of tzhead and a second instance of the data in which each coded transition
362 * time uses 8 rather than 4 chars,
363 * then a POSIX-TZ-environment-variable-style string for use in handling
364 * instants after the last transition time stored in the file
365 * (with nothing between the newlines if there is no POSIX representation for
366 * such instants).
367
368 * If map[4] is '3' or greater, the above is extended as follows.
369 * First, the POSIX TZ string's hour offset may range from -167
370 * through 167 as compared to the POSIX-required 0 through 24.
371 * Second, its DST start time may be January 1 at 00:00 and its stop
372 * time December 31 at 24:00 plus the difference between DST and
373 * standard time, indicating DST all year. */
374 if (map[4]!='1') {
375 size_t skip = zi_dotprod(zi+20, VEC(1,1,8,5,6,1), 6);
376 trans = zi+skip+44+44;
377 scale++;
378 } else {
379 trans = zi+44;
380 }
381 index = trans + (zi_read32(trans-12) << scale);
382 types = index + zi_read32(trans-12);
383 abbrevs = types + 6*zi_read32(trans-8);
384 abbrevs_end = abbrevs + zi_read32(trans-4);
385 if (zi[map_size-1] == '\n') {
386 for (s = (const char *)zi+map_size-2; *s!='\n'; s--);
387 s++;
388 } else {
389 const unsigned char *p;
390 __tzname[0] = __tzname[1] = 0;
391 __daylight = __timezone = dst_off = 0;
392 for (p=types; p<abbrevs; p+=6) {
393 if (!p[4] && !__tzname[0]) {
394 __tzname[0] = (char *)abbrevs + p[5];
395 __timezone = -zi_read32(p);
396 }
397 if (p[4] && !__tzname[1]) {
398 __tzname[1] = (char *)abbrevs + p[5];
399 dst_off = -zi_read32(p);
400 __daylight = 1;
401 }
402 }
403 if (!__tzname[0]) __tzname[0] = __tzname[1];
404 if (!__tzname[0]) __tzname[0] = (char *)__utc;
405 if (!__daylight) {
406 __tzname[1] = __tzname[0];
407 dst_off = __timezone;
408 }
409 return;
410 }
411 }
412
413 if (!s) s = __utc;
414 getname(std_name, &s);
415 __tzname[0] = std_name;
416 __timezone = getoff(&s);
417 getname(dst_name, &s);
418 __tzname[1] = dst_name;
419 if (dst_name[0]) {
420 __daylight = 1;
421 if (*s == '+' || *s=='-' || *s-'0'<10U)
422 dst_off = getoff(&s);
423 else
424 dst_off = __timezone - 3600;
425 } else {
426 __daylight = 0;
427 dst_off = __timezone;
428 }
429
430 if (*s == ',') s++, getrule(&s, r0);
431 if (*s == ',') s++, getrule(&s, r1);
432 }
433
434 /* Search zoneinfo rules to find the one that applies to the given time,
435 * and determine alternate opposite-DST-status rule that may be needed. */
436
scan_trans(long long t,int local,size_t * alt)437 static size_t scan_trans(long long t, int local, size_t *alt)
438 {
439 int scale = 3 - (trans == zi+44);
440 uint64_t x;
441 int off = 0;
442
443 size_t a = 0, n = (index-trans)>>scale, m;
444
445 if (!n) {
446 if (alt) *alt = 0;
447 return 0;
448 }
449
450 /* Binary search for 'most-recent rule before t'. */
451 while (n > 1) {
452 m = a + n/2;
453 x = zi_read32(trans + (m<<scale));
454 if (scale == 3) x = x<<32 | zi_read32(trans + (m<<scale) + 4);
455 else x = (int32_t)x;
456 if (local) off = (int32_t)zi_read32(types + 6 * index[m-1]);
457 if (t - off < (int64_t)x) {
458 n /= 2;
459 } else {
460 a = m;
461 n -= n/2;
462 }
463 }
464
465 /* First and last entry are special. First means to use lowest-index
466 * non-DST type. Last means to apply POSIX-style rule if available. */
467 n = (index-trans)>>scale;
468 if (a == n-1) return -1;
469 if (a == 0) {
470 x = zi_read32(trans);
471 if (scale == 3) x = x<<32 | zi_read32(trans + 4);
472 else x = (int32_t)x;
473 /* Find the lowest non-DST type, or 0 if none. */
474 size_t j = 0;
475 for (size_t i=abbrevs-types; i; i-=6) {
476 if (!types[i-6+4]) j = i-6;
477 }
478 if (local) off = (int32_t)zi_read32(types + j);
479 /* If t is before first transition, use the above-found type
480 * and the index-zero (after transition) type as the alt. */
481 if (t - off < (int64_t)x) {
482 if (alt) *alt = index[0];
483 return j/6;
484 }
485 }
486
487 /* Try to find a neighboring opposite-DST-status rule. */
488 if (alt) {
489 if (a && types[6*index[a-1]+4] != types[6*index[a]+4])
490 *alt = index[a-1];
491 else if (a+1<n && types[6*index[a+1]+4] != types[6*index[a]+4])
492 *alt = index[a+1];
493 else
494 *alt = index[a];
495 }
496
497 return index[a];
498 }
499
days_in_month(int m,int is_leap)500 static int days_in_month(int m, int is_leap)
501 {
502 if (m==2) return 28+is_leap;
503 else return 30+((0xad5>>(m-1))&1);
504 }
505
506 /* Convert a POSIX DST rule plus year to seconds since epoch. */
507
rule_to_secs(const int * rule,int year)508 static long long rule_to_secs(const int *rule, int year)
509 {
510 int is_leap;
511 long long t = __year_to_secs(year, &is_leap);
512 int x, m, n, d;
513 if (rule[0]!='M') {
514 x = rule[1];
515 if (rule[0]=='J' && (x < 60 || !is_leap)) x--;
516 t += 86400 * x;
517 } else {
518 m = rule[1];
519 n = rule[2];
520 d = rule[3];
521 t += __month_to_secs(m-1, is_leap);
522 int wday = (int)((t + 4*86400) % (7*86400)) / 86400;
523 int days = d - wday;
524 if (days < 0) days += 7;
525 if (n == 5 && days+28 >= days_in_month(m, is_leap)) n = 4;
526 t += 86400 * (days + 7*(n-1));
527 }
528 t += rule[4];
529 return t;
530 }
531
532 /* Determine the time zone in effect for a given time in seconds since the
533 * epoch. It can be given in local or universal time. The results will
534 * indicate whether DST is in effect at the queried time, and will give both
535 * the GMT offset for the active zone/DST rule and the opposite DST. This
536 * enables a caller to efficiently adjust for the case where an explicit
537 * DST specification mismatches what would be in effect at the time. */
538
__secs_to_zone(long long t,int local,int * isdst,long * offset,long * oppoff,const char ** zonename,int use_env)539 void __secs_to_zone(long long t, int local, int *isdst, long *offset, long *oppoff, const char **zonename, int use_env)
540 {
541 LOCK(lock);
542 if (use_env == 1) {
543 do_tzset(TZ_USE_ENV);
544 } else {
545 do_tzset(TZ_NO_USE_ENV);
546 }
547
548 if (zi) {
549 size_t alt, i = scan_trans(t, local, &alt);
550 if (i != -1) {
551 *isdst = types[6*i+4];
552 *offset = (int32_t)zi_read32(types+6*i);
553 *zonename = (const char *)abbrevs + types[6*i+5];
554 if (oppoff) *oppoff = (int32_t)zi_read32(types+6*alt);
555 UNLOCK(lock);
556 return;
557 }
558 }
559
560 if (!__daylight) goto std;
561
562 /* FIXME: may be broken if DST changes right at year boundary?
563 * Also, this could be more efficient.*/
564 long long y = t / 31556952 + 70;
565 while (__year_to_secs(y, 0) > t) y--;
566 while (__year_to_secs(y+1, 0) < t) y++;
567
568 long long t0 = rule_to_secs(r0, y);
569 long long t1 = rule_to_secs(r1, y);
570
571 if (!local) {
572 t0 += __timezone;
573 t1 += dst_off;
574 }
575 if (t0 < t1) {
576 if (t >= t0 && t < t1) goto dst;
577 goto std;
578 } else {
579 if (t >= t1 && t < t0) goto std;
580 goto dst;
581 }
582 std:
583 *isdst = 0;
584 *offset = -__timezone;
585 if (oppoff) *oppoff = -dst_off;
586 *zonename = __tzname[0];
587 UNLOCK(lock);
588 return;
589 dst:
590 *isdst = 1;
591 *offset = -dst_off;
592 if (oppoff) *oppoff = -__timezone;
593 *zonename = __tzname[1];
594 UNLOCK(lock);
595 }
596
__tzset()597 static void __tzset()
598 {
599 LOCK(lock);
600 do_tzset(TZ_USE_ENV);
601 UNLOCK(lock);
602 }
603
604 weak_alias(__tzset, tzset);
605
__tm_to_tzname(const struct tm * tm)606 const char *__tm_to_tzname(const struct tm *tm)
607 {
608 const void *p = tm->__tm_zone;
609 LOCK(lock);
610 do_tzset(TZ_USE_ENV);
611 if (p != __utc && p != __tzname[0] && p != __tzname[1] &&
612 (!zi || (uintptr_t)p-(uintptr_t)abbrevs >= abbrevs_end - abbrevs))
613 p = "";
614 UNLOCK(lock);
615 return p;
616 }
617