__tz.c 9.5 KB

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