__tz.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. #include "time_impl.h"
  2. #include <stdint.h>
  3. #include <limits.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <sys/mman.h>
  7. #include "libc.h"
  8. #include "lock.h"
  9. #include "fork_impl.h"
  10. #define malloc __libc_malloc
  11. #define calloc undef
  12. #define realloc undef
  13. #define free undef
  14. long __timezone = 0;
  15. int __daylight = 0;
  16. char *__tzname[2] = { 0, 0 };
  17. weak_alias(__timezone, timezone);
  18. weak_alias(__daylight, daylight);
  19. weak_alias(__tzname, tzname);
  20. static char std_name[TZNAME_MAX+1];
  21. static char dst_name[TZNAME_MAX+1];
  22. const char __utc[] = "UTC";
  23. static int dst_off;
  24. static int r0[5], r1[5];
  25. static const unsigned char *zi, *trans, *index, *types, *abbrevs, *abbrevs_end;
  26. static size_t map_size;
  27. static char old_tz_buf[32];
  28. static char *old_tz = old_tz_buf;
  29. static size_t old_tz_size = sizeof old_tz_buf;
  30. static volatile int lock[1];
  31. volatile int *const __timezone_lockptr = lock;
  32. static int getint(const char **p)
  33. {
  34. unsigned x;
  35. for (x=0; **p-'0'<10U; (*p)++) x = **p-'0' + 10*x;
  36. return x;
  37. }
  38. static int getoff(const char **p)
  39. {
  40. int neg = 0;
  41. if (**p == '-') {
  42. ++*p;
  43. neg = 1;
  44. } else if (**p == '+') {
  45. ++*p;
  46. }
  47. int off = 3600*getint(p);
  48. if (**p == ':') {
  49. ++*p;
  50. off += 60*getint(p);
  51. if (**p == ':') {
  52. ++*p;
  53. off += getint(p);
  54. }
  55. }
  56. return neg ? -off : off;
  57. }
  58. static void getrule(const char **p, int rule[5])
  59. {
  60. int r = rule[0] = **p;
  61. if (r!='M') {
  62. if (r=='J') ++*p;
  63. else rule[0] = 0;
  64. rule[1] = getint(p);
  65. } else {
  66. ++*p; rule[1] = getint(p);
  67. ++*p; rule[2] = getint(p);
  68. ++*p; rule[3] = getint(p);
  69. }
  70. if (**p=='/') {
  71. ++*p;
  72. rule[4] = getoff(p);
  73. } else {
  74. rule[4] = 7200;
  75. }
  76. }
  77. static void getname(char *d, const char **p)
  78. {
  79. int i;
  80. if (**p == '<') {
  81. ++*p;
  82. for (i=0; (*p)[i] && (*p)[i]!='>'; i++)
  83. if (i<TZNAME_MAX) d[i] = (*p)[i];
  84. if ((*p)[i]) ++*p;
  85. } else {
  86. for (i=0; ((*p)[i]|32)-'a'<26U; i++)
  87. if (i<TZNAME_MAX) d[i] = (*p)[i];
  88. }
  89. *p += i;
  90. d[i<TZNAME_MAX?i:TZNAME_MAX] = 0;
  91. }
  92. #define VEC(...) ((const unsigned char[]){__VA_ARGS__})
  93. static uint32_t zi_read32(const unsigned char *z)
  94. {
  95. return (unsigned)z[0]<<24 | z[1]<<16 | z[2]<<8 | z[3];
  96. }
  97. static size_t zi_dotprod(const unsigned char *z, const unsigned char *v, size_t n)
  98. {
  99. size_t y;
  100. uint32_t x;
  101. for (y=0; n; n--, z+=4, v++) {
  102. x = zi_read32(z);
  103. y += x * *v;
  104. }
  105. return y;
  106. }
  107. static void do_tzset()
  108. {
  109. char buf[NAME_MAX+25], *pathname=buf+24;
  110. const char *try, *s, *p;
  111. const unsigned char *map = 0;
  112. size_t i;
  113. static const char search[] =
  114. "/usr/share/zoneinfo/\0/share/zoneinfo/\0/etc/zoneinfo/\0";
  115. s = getenv("TZ");
  116. if (!s) s = "/etc/localtime";
  117. if (!*s) s = __utc;
  118. if (old_tz && !strcmp(s, old_tz)) return;
  119. for (i=0; i<5; i++) r0[i] = r1[i] = 0;
  120. if (zi) __munmap((void *)zi, map_size);
  121. /* Cache the old value of TZ to check if it has changed. Avoid
  122. * free so as not to pull it into static programs. Growth
  123. * strategy makes it so free would have minimal benefit anyway. */
  124. i = strlen(s);
  125. if (i > PATH_MAX+1) s = __utc, i = 3;
  126. if (i >= old_tz_size) {
  127. old_tz_size *= 2;
  128. if (i >= old_tz_size) old_tz_size = i+1;
  129. if (old_tz_size > PATH_MAX+2) old_tz_size = PATH_MAX+2;
  130. old_tz = malloc(old_tz_size);
  131. }
  132. if (old_tz) memcpy(old_tz, s, i+1);
  133. /* Non-suid can use an absolute tzfile pathname or a relative
  134. * pathame beginning with "."; in secure mode, only the
  135. * standard path will be searched. */
  136. if (*s == ':' || ((p=strchr(s, '/')) && !memchr(s, ',', p-s))) {
  137. if (*s == ':') s++;
  138. if (*s == '/' || *s == '.') {
  139. if (!libc.secure || !strcmp(s, "/etc/localtime"))
  140. map = __map_file(s, &map_size);
  141. } else {
  142. size_t l = strlen(s);
  143. if (l <= NAME_MAX && !strchr(s, '.')) {
  144. memcpy(pathname, s, l+1);
  145. pathname[l] = 0;
  146. for (try=search; !map && *try; try+=l+1) {
  147. l = strlen(try);
  148. memcpy(pathname-l, try, l);
  149. map = __map_file(pathname-l, &map_size);
  150. }
  151. }
  152. }
  153. if (!map) s = __utc;
  154. }
  155. if (map && (map_size < 44 || memcmp(map, "TZif", 4))) {
  156. __munmap((void *)map, map_size);
  157. map = 0;
  158. s = __utc;
  159. }
  160. zi = map;
  161. if (map) {
  162. int scale = 2;
  163. if (map[4]!='1') {
  164. size_t skip = zi_dotprod(zi+20, VEC(1,1,8,5,6,1), 6);
  165. trans = zi+skip+44+44;
  166. scale++;
  167. } else {
  168. trans = zi+44;
  169. }
  170. index = trans + (zi_read32(trans-12) << scale);
  171. types = index + zi_read32(trans-12);
  172. abbrevs = types + 6*zi_read32(trans-8);
  173. abbrevs_end = abbrevs + zi_read32(trans-4);
  174. if (zi[map_size-1] == '\n') {
  175. for (s = (const char *)zi+map_size-2; *s!='\n'; s--);
  176. s++;
  177. } else {
  178. const unsigned char *p;
  179. __tzname[0] = __tzname[1] = 0;
  180. __daylight = __timezone = dst_off = 0;
  181. for (p=types; p<abbrevs; p+=6) {
  182. if (!p[4] && !__tzname[0]) {
  183. __tzname[0] = (char *)abbrevs + p[5];
  184. __timezone = -zi_read32(p);
  185. }
  186. if (p[4] && !__tzname[1]) {
  187. __tzname[1] = (char *)abbrevs + p[5];
  188. dst_off = -zi_read32(p);
  189. __daylight = 1;
  190. }
  191. }
  192. if (!__tzname[0]) __tzname[0] = __tzname[1];
  193. if (!__tzname[0]) __tzname[0] = (char *)__utc;
  194. if (!__daylight) {
  195. __tzname[1] = __tzname[0];
  196. dst_off = __timezone;
  197. }
  198. return;
  199. }
  200. }
  201. if (!s) s = __utc;
  202. getname(std_name, &s);
  203. __tzname[0] = std_name;
  204. __timezone = getoff(&s);
  205. getname(dst_name, &s);
  206. __tzname[1] = dst_name;
  207. if (dst_name[0]) {
  208. __daylight = 1;
  209. if (*s == '+' || *s=='-' || *s-'0'<10U)
  210. dst_off = getoff(&s);
  211. else
  212. dst_off = __timezone - 3600;
  213. } else {
  214. __daylight = 0;
  215. dst_off = __timezone;
  216. }
  217. if (*s == ',') s++, getrule(&s, r0);
  218. if (*s == ',') s++, getrule(&s, r1);
  219. }
  220. /* Search zoneinfo rules to find the one that applies to the given time,
  221. * and determine alternate opposite-DST-status rule that may be needed. */
  222. static size_t scan_trans(long long t, int local, size_t *alt)
  223. {
  224. int scale = 3 - (trans == zi+44);
  225. uint64_t x;
  226. int off = 0;
  227. size_t a = 0, n = (index-trans)>>scale, m;
  228. if (!n) {
  229. if (alt) *alt = 0;
  230. return 0;
  231. }
  232. /* Binary search for 'most-recent rule before t'. */
  233. while (n > 1) {
  234. m = a + n/2;
  235. x = zi_read32(trans + (m<<scale));
  236. if (scale == 3) x = x<<32 | zi_read32(trans + (m<<scale) + 4);
  237. else x = (int32_t)x;
  238. if (local) off = (int32_t)zi_read32(types + 6 * index[m-1]);
  239. if (t - off < (int64_t)x) {
  240. n /= 2;
  241. } else {
  242. a = m;
  243. n -= n/2;
  244. }
  245. }
  246. /* First and last entry are special. First means to use lowest-index
  247. * non-DST type. Last means to apply POSIX-style rule if available. */
  248. n = (index-trans)>>scale;
  249. if (a == n-1) return -1;
  250. if (a == 0) {
  251. x = zi_read32(trans + (a<<scale));
  252. if (scale == 3) x = x<<32 | zi_read32(trans + (a<<scale) + 4);
  253. else x = (int32_t)x;
  254. if (local) off = (int32_t)zi_read32(types + 6 * index[a-1]);
  255. if (t - off < (int64_t)x) {
  256. for (a=0; a<(abbrevs-types)/6; a++) {
  257. if (types[6*a+4] != types[4]) break;
  258. }
  259. if (a == (abbrevs-types)/6) a = 0;
  260. if (types[6*a+4]) {
  261. *alt = a;
  262. return 0;
  263. } else {
  264. *alt = 0;
  265. return a;
  266. }
  267. }
  268. }
  269. /* Try to find a neighboring opposite-DST-status rule. */
  270. if (alt) {
  271. if (a && types[6*index[a-1]+4] != types[6*index[a]+4])
  272. *alt = index[a-1];
  273. else if (a+1<n && types[6*index[a+1]+4] != types[6*index[a]+4])
  274. *alt = index[a+1];
  275. else
  276. *alt = index[a];
  277. }
  278. return index[a];
  279. }
  280. static int days_in_month(int m, int is_leap)
  281. {
  282. if (m==2) return 28+is_leap;
  283. else return 30+((0xad5>>(m-1))&1);
  284. }
  285. /* Convert a POSIX DST rule plus year to seconds since epoch. */
  286. static long long rule_to_secs(const int *rule, int year)
  287. {
  288. int is_leap;
  289. long long t = __year_to_secs(year, &is_leap);
  290. int x, m, n, d;
  291. if (rule[0]!='M') {
  292. x = rule[1];
  293. if (rule[0]=='J' && (x < 60 || !is_leap)) x--;
  294. t += 86400 * x;
  295. } else {
  296. m = rule[1];
  297. n = rule[2];
  298. d = rule[3];
  299. t += __month_to_secs(m-1, is_leap);
  300. int wday = (int)((t + 4*86400) % (7*86400)) / 86400;
  301. int days = d - wday;
  302. if (days < 0) days += 7;
  303. if (n == 5 && days+28 >= days_in_month(m, is_leap)) n = 4;
  304. t += 86400 * (days + 7*(n-1));
  305. }
  306. t += rule[4];
  307. return t;
  308. }
  309. /* Determine the time zone in effect for a given time in seconds since the
  310. * epoch. It can be given in local or universal time. The results will
  311. * indicate whether DST is in effect at the queried time, and will give both
  312. * the GMT offset for the active zone/DST rule and the opposite DST. This
  313. * enables a caller to efficiently adjust for the case where an explicit
  314. * DST specification mismatches what would be in effect at the time. */
  315. void __secs_to_zone(long long t, int local, int *isdst, long *offset, long *oppoff, const char **zonename)
  316. {
  317. LOCK(lock);
  318. do_tzset();
  319. if (zi) {
  320. size_t alt, i = scan_trans(t, local, &alt);
  321. if (i != -1) {
  322. *isdst = types[6*i+4];
  323. *offset = (int32_t)zi_read32(types+6*i);
  324. *zonename = (const char *)abbrevs + types[6*i+5];
  325. if (oppoff) *oppoff = (int32_t)zi_read32(types+6*alt);
  326. UNLOCK(lock);
  327. return;
  328. }
  329. }
  330. if (!__daylight) goto std;
  331. /* FIXME: may be broken if DST changes right at year boundary?
  332. * Also, this could be more efficient.*/
  333. long long y = t / 31556952 + 70;
  334. while (__year_to_secs(y, 0) > t) y--;
  335. while (__year_to_secs(y+1, 0) < t) y++;
  336. long long t0 = rule_to_secs(r0, y);
  337. long long t1 = rule_to_secs(r1, y);
  338. if (!local) {
  339. t0 += __timezone;
  340. t1 += dst_off;
  341. }
  342. if (t0 < t1) {
  343. if (t >= t0 && t < t1) goto dst;
  344. goto std;
  345. } else {
  346. if (t >= t1 && t < t0) goto std;
  347. goto dst;
  348. }
  349. std:
  350. *isdst = 0;
  351. *offset = -__timezone;
  352. if (oppoff) *oppoff = -dst_off;
  353. *zonename = __tzname[0];
  354. UNLOCK(lock);
  355. return;
  356. dst:
  357. *isdst = 1;
  358. *offset = -dst_off;
  359. if (oppoff) *oppoff = -__timezone;
  360. *zonename = __tzname[1];
  361. UNLOCK(lock);
  362. }
  363. static void __tzset()
  364. {
  365. LOCK(lock);
  366. do_tzset();
  367. UNLOCK(lock);
  368. }
  369. weak_alias(__tzset, tzset);
  370. const char *__tm_to_tzname(const struct tm *tm)
  371. {
  372. const void *p = tm->__tm_zone;
  373. LOCK(lock);
  374. do_tzset();
  375. if (p != __utc && p != __tzname[0] && p != __tzname[1] &&
  376. (!zi || (uintptr_t)p-(uintptr_t)abbrevs >= abbrevs_end - abbrevs))
  377. p = "";
  378. UNLOCK(lock);
  379. return p;
  380. }