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