1 --- localtime.c.orig 2004-11-01 23:24:08.000000000 -0800
2 +++ localtime.c 2004-11-05 23:43:55.000000000 -0800
8 +//#define NOTIFY_TZ_DEBUG
9 +//#define NOTIFY_TZ_DEBUG_FILE "/var/log/localtime.debug"
10 +//#define NOTIFY_TZ_LOG "/var/log/localtime.log"
11 +/* force ALL_STATE if NOTIFY_TZ is set */
14 +#endif /* ALL_STATE */
15 +#include <mach/mach_init.h>
18 +#endif /* NOTIFY_TZ */
20 #include "un-namespace.h"
23 #define DAY_OF_YEAR 1 /* n - day of year */
24 #define MONTH_NTH_DAY_OF_WEEK 2 /* Mm.n.d - month, week, day of week */
33 +#define NOTIFY_TZ_NAME "com.apple.system.timezone"
34 +#endif /* NOTIFY_TZ */
37 ** Prototypes for static functions.
40 static int increment_overflow(int * number, int delta);
41 static int normalize_overflow(int * tensptr, int * unitsptr,
44 +static void notify_check_tz(notify_tz_t *p);
45 +static void notify_register_tz(char *file, notify_tz_t *p);
46 +#endif /* NOTIFY_TZ */
47 static void settzname(void);
48 static time_t time1(struct tm * tmp,
49 void(*funcp) (const time_t *,
51 #endif /* !defined TZ_STRLEN_MAX */
53 static char lcl_TZname[TZ_STRLEN_MAX + 1];
55 +#define lcl_is_set (lcl_notify.is_set)
56 +#define gmt_is_set (gmt_notify.is_set)
57 +#else /* ! NOTIFY_TZ */
58 static int lcl_is_set;
59 static int gmt_is_set;
60 +#endif /* NOTIFY_TZ */
61 static pthread_mutex_t lcl_mutex = PTHREAD_MUTEX_INITIALIZER;
62 static pthread_mutex_t gmt_mutex = PTHREAD_MUTEX_INITIALIZER;
73 +__private_extern__ void _st_set_timezone(long);
74 #endif /* defined USG_COMPAT */
78 +__private_extern__ long __darwin_altzone = 0;
79 +#define altzone __darwin_altzone
80 #endif /* defined ALTZONE */
83 +#ifdef NOTIFY_TZ_DEBUG
84 +#ifdef NOTIFY_TZ_DEBUG_FILE
85 +#define NOTIFY_TZ_PRINTF(fmt, args...) \
87 + FILE *_notify_tz_fp_; \
88 + if((_notify_tz_fp_ = fopen(NOTIFY_TZ_DEBUG_FILE, "a")) != NULL) { \
89 + fprintf(_notify_tz_fp_, "%d: " fmt, getpid(), ## args); \
90 + fclose(_notify_tz_fp_); \
93 +#else /* ! NOTIFY_TZ_DEBUG_FILE */
94 +#define NOTIFY_TZ_PRINTF(args...) fprintf(stdout, ## args)
95 +#endif /* NOTIFY_TZ_DEBUG_FILE */
96 +#endif /* NOTIFY_TZ_DEBUG */
98 +#define NOTIFY_LOG(fmt, args...) \
100 + FILE *_notify_log_fp_; \
101 + if((_notify_log_fp_ = fopen(NOTIFY_TZ_LOG, "a")) != NULL) { \
102 + fprintf(_notify_log_fp_, "%d: " fmt, getpid(), ## args); \
103 + fclose(_notify_log_fp_); \
106 +#endif /* NOTIFY_TZ_LOG */
107 +/*--------------------------------------------------------------------
108 + * __notify_78945668_info__ is a global variable (defined in Libnotify)
109 + * that can be used to disable the notify mechanism. Set to a negative
110 + * value to disable. It can then be set back to zero to re-enable.
111 + *-------------------------------------------------------------------- */
112 +extern int __notify_78945668_info__;
114 +/*--------------------------------------------------------------------
115 + * fullname is used to pass the actual path of the timezone file to the
116 + * notify routines. If it is a nil string, that means no timezone file
118 + *-------------------------------------------------------------------- */
119 +static char * fullname = NULL;
121 +static notify_tz_t gmt_notify = {-1, 0, 0};
122 +static notify_tz_t lcl_notify = {-1, 0, 0};
123 +static char notify_tz_name[] = NOTIFY_TZ_NAME;
124 +#endif /* NOTIFY_TZ */
128 const char * const codep;
129 @@ -246,14 +324,14 @@
130 tzname[1] = wildabbr;
134 + _st_set_timezone(0);
135 #endif /* defined USG_COMPAT */
138 #endif /* defined ALTZONE */
141 - tzname[0] = tzname[1] = gmt;
142 + tzname[0] = tzname[1] = (char *)gmt;
145 #endif /* defined ALL_STATE */
149 if (i == 0 || !ttisp->tt_isdst)
150 - timezone = -(ttisp->tt_gmtoff);
151 + _st_set_timezone(-(ttisp->tt_gmtoff));
152 #endif /* defined USG_COMPAT */
154 if (i == 0 || ttisp->tt_isdst)
155 @@ -286,6 +364,117 @@
161 +notify_check_tz(notify_tz_t *p)
163 + unsigned int nstat;
166 + if (__notify_78945668_info__ < 0) {
167 +#ifdef NOTIFY_TZ_DEBUG
168 + if(!p->notify_was_off) NOTIFY_TZ_PRINTF("notify_check_tz: setting %s_notify->notify_was_off\n", (p == &lcl_notify ? "lcl" : "gmt"));
169 +#endif /* NOTIFY_TZ_DEBUG */
170 + p->notify_was_off = 1;
173 + /* force rereading the timezone file if notify was off */
174 + if (p->notify_was_off) {
175 +#ifdef NOTIFY_TZ_DEBUG
176 + NOTIFY_TZ_PRINTF("notify_check_tz: saw %s_notify->notify_was_off\n", (p == &lcl_notify ? "lcl" : "gmt"));
177 +#endif /* NOTIFY_TZ_DEBUG */
179 + p->notify_was_off = 0;
184 + nstat = notify_check(p->token, &ncheck);
185 + if (nstat || ncheck) {
187 +#ifdef NOTIFY_TZ_DEBUG
188 + NOTIFY_TZ_PRINTF("notify_check_tz: %s changed\n", (p == &lcl_notify) ? "lcl" : "gmt");
189 +#endif /* NOTIFY_TZ_DEBUG */
191 +#ifdef NOTIFY_TZ_DEBUG
192 + NOTIFY_TZ_PRINTF("notify_check_tz: %s unchanged\n", (p == &lcl_notify) ? "lcl" : "gmt");
193 +#endif /* NOTIFY_TZ_DEBUG */
197 +notify_register_tz(char *file, notify_tz_t *p)
200 + unsigned int nstat;
203 + if (__notify_78945668_info__ < 0)
205 + /*----------------------------------------------------------------
206 + * Since we don't record the last time zone filename, just cancel
207 + * (which should remove the file monitor) and setup from scratch
208 + *----------------------------------------------------------------*/
210 + notify_cancel(p->token);
211 + if (!file || *file == 0) {
212 + /* no time zone file to monitor */
216 + /*----------------------------------------------------------------
217 + * Just use com.apple.system.timezone if the path is /etc/localtime.
218 + * Otherwise use com.apple.system.timezone.<fullpath>
219 + *----------------------------------------------------------------*/
220 + if (TZDEFAULT && strcmp(file, TZDEFAULT) == 0)
221 + name = notify_tz_name;
223 + name = alloca(sizeof(notify_tz_name) + strlen(file) + 1);
224 + if (name == NULL) {
228 + strcpy(name, notify_tz_name);
230 + strcat(name, file);
232 +#ifdef NOTIFY_TZ_DEBUG
233 + NOTIFY_TZ_PRINTF("notify_register_tz: file=%s name=%s\n", file, name);
234 +#endif /* NOTIFY_TZ_DEBUG */
235 + nstat = notify_register_check(name, &p->token);
239 +#ifdef NOTIFY_TZ_DEBUG
240 + NOTIFY_TZ_PRINTF("***notify_register_tz: notify_register_check failed: %u\n", nstat);
241 +#endif /* NOTIFY_TZ_DEBUG */
242 +#ifdef NOTIFY_TZ_LOG
243 + NOTIFY_LOG("notify_register_check(%s) failed: %u\n", name, nstat);
244 +#endif /* NOTIFY_TZ_LOG */
247 + /* don't need to request monitoring /etc/localtime */
248 + if (name != notify_tz_name) {
249 +#ifdef NOTIFY_TZ_DEBUG
250 + NOTIFY_TZ_PRINTF("notify_register_tz: monitor %s\n", file);
251 +#endif /* NOTIFY_TZ_DEBUG */
252 + nstat = notify_monitor_file(p->token, file, 0);
254 + notify_cancel(p->token);
257 +#ifdef NOTIFY_TZ_DEBUG
258 + NOTIFY_TZ_PRINTF("***notify_register_tz: notify_monitor_file failed: %u\n", nstat);
259 +#endif /* NOTIFY_TZ_DEBUG */
260 +#ifdef NOTIFY_TZ_LOG
261 + NOTIFY_LOG("notify_monitor_file(%s) failed: %u\n", file, nstat);
262 +#endif /* NOTIFY_TZ_LOG */
266 + notify_check(p->token, &ncheck); /* this always returns true */
268 +#endif /* NOTIFY_TZ */
277 +#ifdef NOTIFY_TZ_DEBUG
278 + NOTIFY_TZ_PRINTF("tzload: name=%s\n", name);
279 +#endif /* NOTIFY_TZ_DEBUG */
280 /* XXX The following is from OpenBSD, and I'm not sure it is correct */
281 if (name != NULL && issetugid() != 0)
282 if ((name[0] == ':' && name[1] == '/') ||
284 ** to hold the longest file name string that the implementation
285 ** guarantees can be opened."
289 + fullname = malloc(FILENAME_MAX + 1);
293 +#else /* ! NOTIFY_TZ */
294 char fullname[FILENAME_MAX + 1];
295 +#endif /* NOTIFY_TZ */
301 if ((p = TZDIR) == NULL)
304 + if ((strlen(p) + 1 + strlen(name) + 1) >= (FILENAME_MAX + 1))
305 +#else /* ! NOTIFY_TZ */
306 if ((strlen(p) + 1 + strlen(name) + 1) >= sizeof fullname)
307 +#endif /* NOTIFY_TZ */
309 (void) strcpy(fullname, p);
310 (void) strcat(fullname, "/");
317 + strcpy(fullname, name);
318 +#endif /* NOTIFY_TZ */
319 if (doaccess && access(name, R_OK) != 0)
321 if ((fid = _open(name, OPEN_MODE)) == -1)
326 +#ifdef NOTIFY_TZ_DEBUG
327 + NOTIFY_TZ_PRINTF("tzload: reading %s\n", name);
328 +#endif /* NOTIFY_TZ_DEBUG */
329 i = _read(fid, u.buf, sizeof u.buf);
330 if (_close(fid) != 0)
335 load_result = tzload(TZDEFRULES, sp);
337 + *fullname = 0; /* mark fullname as invalid */
338 +#endif /* NOTIFY_TZ */
339 if (load_result != 0)
340 sp->leapcnt = 0; /* so, we're off a little */
342 @@ -951,8 +1165,19 @@
344 tzsetwall_basic(void)
347 + notify_check_tz(&lcl_notify);
348 +#endif /* NOTIFY_TZ */
349 +#ifdef NOTIFY_TZ_DEBUG
350 + if (lcl_is_set < 0) {
351 + NOTIFY_TZ_PRINTF("tzsetwall_basic lcl_is_set < 0\n");
354 + NOTIFY_TZ_PRINTF("tzsetwall_basic not set\n");
355 +#else /* ! NOTIFY_TZ_DEBUG */
358 +#endif /* NOTIFY_TZ_DEBUG */
362 @@ -966,12 +1191,18 @@
363 #endif /* defined ALL_STATE */
364 if (tzload((char *) NULL, lclptr) != 0)
367 + notify_register_tz(fullname, &lcl_notify);
368 +#endif /* NOTIFY_TZ */
375 +#ifdef NOTIFY_TZ_DEBUG
376 + NOTIFY_TZ_PRINTF("tzsetwall called\n");
377 +#endif /* NOTIFY_TZ_DEBUG */
378 _MUTEX_LOCK(&lcl_mutex);
380 _MUTEX_UNLOCK(&lcl_mutex);
381 @@ -988,8 +1219,18 @@
386 + notify_check_tz(&lcl_notify);
387 +#endif /* NOTIFY_TZ */
388 +#ifdef NOTIFY_TZ_DEBUG
389 + if (lcl_is_set > 0 && strcmp(lcl_TZname, name) == 0) {
390 + NOTIFY_TZ_PRINTF("tzset_basic matched %s\n", lcl_TZname);
393 +#else /* ! NOTIFY_TZ_DEBUG */
394 if (lcl_is_set > 0 && strcmp(lcl_TZname, name) == 0)
396 +#endif /* NOTIFY_TZ_DEBUG */
397 lcl_is_set = strlen(name) < sizeof lcl_TZname;
399 (void) strcpy(lcl_TZname, name);
400 @@ -1014,15 +1255,25 @@
401 lclptr->ttis[0].tt_gmtoff = 0;
402 lclptr->ttis[0].tt_abbrind = 0;
403 (void) strcpy(lclptr->chars, gmt);
407 +#endif /* NOTIFY_TZ */
408 } else if (tzload(name, lclptr) != 0)
409 if (name[0] == ':' || tzparse(name, lclptr, FALSE) != 0)
410 (void) gmtload(lclptr);
412 + notify_register_tz(fullname, &lcl_notify);
413 +#endif /* NOTIFY_TZ */
420 +#ifdef NOTIFY_TZ_DEBUG
421 + NOTIFY_TZ_PRINTF("tzset called TZ=%s\n", getenv("TZ"));
422 +#endif /* NOTIFY_TZ_DEBUG */
423 _MUTEX_LOCK(&lcl_mutex);
425 _MUTEX_UNLOCK(&lcl_mutex);
426 @@ -1049,6 +1300,9 @@
428 const time_t t = *timep;
430 +#ifdef NOTIFY_TZ_DEBUG
431 + NOTIFY_TZ_PRINTF("localsub called\n");
432 +#endif /* NOTIFY_TZ_DEBUG */
436 @@ -1094,7 +1348,7 @@
438 if (__isthreaded != 0) {
439 _pthread_mutex_lock(&localtime_mutex);
440 - if (localtime_key < 0) {
441 + if (localtime_key == (pthread_key_t)-1) {
442 if (_pthread_key_create(&localtime_key, free) < 0) {
443 _pthread_mutex_unlock(&localtime_mutex);
445 @@ -1146,14 +1400,30 @@
447 struct tm * const tmp;
449 +#ifdef NOTIFY_TZ_DEBUG
450 + NOTIFY_TZ_PRINTF("gmtsub called\n");
451 +#endif /* NOTIFY_TZ_DEBUG */
452 _MUTEX_LOCK(&gmt_mutex);
454 + notify_check_tz(&gmt_notify);
455 +#endif /* NOTIFY_TZ */
459 - gmtptr = (struct state *) malloc(sizeof *gmtptr);
461 + if (gmtptr == NULL)
462 +#endif /* NOTIFY_TZ */
463 + gmtptr = (struct state *) malloc(sizeof *gmtptr);
467 +#endif /* NOTIFY_TZ */
468 #endif /* defined ALL_STATE */
471 + notify_register_tz(fullname, &gmt_notify);
473 +#endif /* NOTIFY_TZ */
475 _MUTEX_UNLOCK(&gmt_mutex);
476 timesub(timep, offset, gmtptr, tmp);
477 @@ -1168,7 +1438,7 @@
481 - tmp->TM_ZONE = gmt;
482 + tmp->TM_ZONE = (char *)gmt;
483 else tmp->TM_ZONE = gmtptr->chars;
484 #endif /* defined ALL_STATE */
486 @@ -1188,7 +1458,7 @@
488 if (__isthreaded != 0) {
489 _pthread_mutex_lock(&gmtime_mutex);
490 - if (gmtime_key < 0) {
491 + if (gmtime_key == (pthread_key_t)-1) {
492 if (_pthread_key_create(&gmtime_key, free) < 0) {
493 _pthread_mutex_unlock(&gmtime_mutex);