1 --- localtime.c.orig Thu Aug 7 18:34:10 2003
2 +++ localtime.c Thu Aug 7 16:57:55 2003
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;
66 #endif /* defined ALTZONE */
69 +#ifdef NOTIFY_TZ_DEBUG
70 +#ifdef NOTIFY_TZ_DEBUG_FILE
71 +#define NOTIFY_TZ_PRINTF(fmt, args...) \
73 + FILE *_notify_tz_fp_; \
74 + if((_notify_tz_fp_ = fopen(NOTIFY_TZ_DEBUG_FILE, "a")) != NULL) { \
75 + fprintf(_notify_tz_fp_, "%d: " fmt, getpid(), ## args); \
76 + fclose(_notify_tz_fp_); \
79 +#else /* ! NOTIFY_TZ_DEBUG_FILE */
80 +#define NOTIFY_TZ_PRINTF(args...) fprintf(stdout, ## args)
81 +#endif /* NOTIFY_TZ_DEBUG_FILE */
82 +#endif /* NOTIFY_TZ_DEBUG */
84 +#define NOTIFY_LOG(fmt, args...) \
86 + FILE *_notify_log_fp_; \
87 + if((_notify_log_fp_ = fopen(NOTIFY_TZ_LOG, "a")) != NULL) { \
88 + fprintf(_notify_log_fp_, "%d: " fmt, getpid(), ## args); \
89 + fclose(_notify_log_fp_); \
92 +#endif /* NOTIFY_TZ_LOG */
93 +/*--------------------------------------------------------------------
94 + * __notify_78945668_info__ is a global variable (defined in Libnotify)
95 + * that can be used to disable the notify mechanism. Set to a negative
96 + * value to disable. It can then be set back to zero to re-enable.
97 + *-------------------------------------------------------------------- */
98 +extern int __notify_78945668_info__;
100 +/*--------------------------------------------------------------------
101 + * fullname is used to pass the actual path of the timezone file to the
102 + * notify routines. If it is a nil string, that means no timezone file
104 + *-------------------------------------------------------------------- */
105 +static char * fullname = NULL;
107 +static notify_tz_t gmt_notify = {-1, 0, 0};
108 +static notify_tz_t lcl_notify = {-1, 0, 0};
109 +static char notify_tz_name[] = NOTIFY_TZ_NAME;
110 +#endif /* NOTIFY_TZ */
114 const char * const codep;
116 #endif /* defined ALTZONE */
119 - tzname[0] = tzname[1] = gmt;
120 + tzname[0] = tzname[1] = (char *)gmt;
123 #endif /* defined ALL_STATE */
124 @@ -266,6 +341,117 @@
130 +notify_check_tz(notify_tz_t *p)
132 + unsigned int nstat;
135 + if (__notify_78945668_info__ < 0) {
136 +#ifdef NOTIFY_TZ_DEBUG
137 + if(!p->notify_was_off) NOTIFY_TZ_PRINTF("notify_check_tz: setting %s_notify->notify_was_off\n", (p == &lcl_notify ? "lcl" : "gmt"));
138 +#endif /* NOTIFY_TZ_DEBUG */
139 + p->notify_was_off = 1;
142 + /* force rereading the timezone file if notify was off */
143 + if (p->notify_was_off) {
144 +#ifdef NOTIFY_TZ_DEBUG
145 + NOTIFY_TZ_PRINTF("notify_check_tz: saw %s_notify->notify_was_off\n", (p == &lcl_notify ? "lcl" : "gmt"));
146 +#endif /* NOTIFY_TZ_DEBUG */
148 + p->notify_was_off = 0;
153 + nstat = notify_check(p->token, &ncheck);
154 + if (nstat || ncheck) {
156 +#ifdef NOTIFY_TZ_DEBUG
157 + NOTIFY_TZ_PRINTF("notify_check_tz: %s changed\n", (p == &lcl_notify) ? "lcl" : "gmt");
158 +#endif /* NOTIFY_TZ_DEBUG */
160 +#ifdef NOTIFY_TZ_DEBUG
161 + NOTIFY_TZ_PRINTF("notify_check_tz: %s unchanged\n", (p == &lcl_notify) ? "lcl" : "gmt");
162 +#endif /* NOTIFY_TZ_DEBUG */
166 +notify_register_tz(char *file, notify_tz_t *p)
169 + unsigned int nstat;
172 + if (__notify_78945668_info__ < 0)
174 + /*----------------------------------------------------------------
175 + * Since we don't record the last time zone filename, just cancel
176 + * (which should remove the file monitor) and setup from scratch
177 + *----------------------------------------------------------------*/
179 + notify_cancel(p->token);
181 + /* no time zone file to monitor */
185 + /*----------------------------------------------------------------
186 + * Just use com.apple.system.timezone if the path is /etc/localtime.
187 + * Otherwise use com.apple.system.timezone.<fullpath>
188 + *----------------------------------------------------------------*/
189 + if (TZDEFAULT && strcmp(file, TZDEFAULT) == 0)
190 + name = notify_tz_name;
192 + name = alloca(sizeof(notify_tz_name) + strlen(file) + 1);
193 + if (name == NULL) {
197 + strcpy(name, notify_tz_name);
199 + strcat(name, file);
201 +#ifdef NOTIFY_TZ_DEBUG
202 + NOTIFY_TZ_PRINTF("notify_register_tz: file=%s name=%s\n", file, name);
203 +#endif /* NOTIFY_TZ_DEBUG */
204 + nstat = notify_register_check(name, &p->token);
208 +#ifdef NOTIFY_TZ_DEBUG
209 + NOTIFY_TZ_PRINTF("***notify_register_tz: notify_register_check failed: %u\n", nstat);
210 +#endif /* NOTIFY_TZ_DEBUG */
211 +#ifdef NOTIFY_TZ_LOG
212 + NOTIFY_LOG("notify_register_check(%s) failed: %u\n", name, nstat);
213 +#endif /* NOTIFY_TZ_LOG */
216 + /* don't need to request monitoring /etc/localtime */
217 + if (name != notify_tz_name) {
218 +#ifdef NOTIFY_TZ_DEBUG
219 + NOTIFY_TZ_PRINTF("notify_register_tz: monitor %s\n", file);
220 +#endif /* NOTIFY_TZ_DEBUG */
221 + nstat = notify_monitor_file(p->token, file, 0);
223 + notify_cancel(p->token);
226 +#ifdef NOTIFY_TZ_DEBUG
227 + NOTIFY_TZ_PRINTF("***notify_register_tz: notify_monitor_file failed: %u\n", nstat);
228 +#endif /* NOTIFY_TZ_DEBUG */
229 +#ifdef NOTIFY_TZ_LOG
230 + NOTIFY_LOG("notify_monitor_file(%s) failed: %u\n", file, nstat);
231 +#endif /* NOTIFY_TZ_LOG */
235 + notify_check(p->token, &ncheck); /* this always returns true */
237 +#endif /* NOTIFY_TZ */
246 +#ifdef NOTIFY_TZ_DEBUG
247 + NOTIFY_TZ_PRINTF("tzload: name=%s\n", name);
248 +#endif /* NOTIFY_TZ_DEBUG */
249 /* XXX The following is from OpenBSD, and I'm not sure it is correct */
250 if (name != NULL && issetugid() != 0)
251 if ((name[0] == ':' && name[1] == '/') ||
253 ** to hold the longest file name string that the implementation
254 ** guarantees can be opened."
258 + fullname = malloc(FILENAME_MAX + 1);
262 +#else /* ! NOTIFY_TZ */
263 char fullname[FILENAME_MAX + 1];
264 +#endif /* NOTIFY_TZ */
270 if ((p = TZDIR) == NULL)
273 + if ((strlen(p) + 1 + strlen(name) + 1) >= (FILENAME_MAX + 1))
274 +#else /* ! NOTIFY_TZ */
275 if ((strlen(p) + 1 + strlen(name) + 1) >= sizeof fullname)
276 +#endif /* NOTIFY_TZ */
278 (void) strcpy(fullname, p);
279 (void) strcat(fullname, "/");
286 + strcpy(fullname, name);
287 +#endif /* NOTIFY_TZ */
288 if (doaccess && access(name, R_OK) != 0)
290 if ((fid = _open(name, OPEN_MODE)) == -1)
295 +#ifdef NOTIFY_TZ_DEBUG
296 + NOTIFY_TZ_PRINTF("tzload: reading %s\n", name);
297 +#endif /* NOTIFY_TZ_DEBUG */
298 i = _read(fid, buf, sizeof buf);
299 if (_close(fid) != 0)
304 load_result = tzload(TZDEFRULES, sp);
306 + *fullname = 0; /* mark fullname as invalid */
307 +#endif /* NOTIFY_TZ */
308 if (load_result != 0)
309 sp->leapcnt = 0; /* so, we're off a little */
311 @@ -934,8 +1145,19 @@
313 tzsetwall_basic(void)
316 + notify_check_tz(&lcl_notify);
317 +#endif /* NOTIFY_TZ */
318 +#ifdef NOTIFY_TZ_DEBUG
319 + if (lcl_is_set < 0) {
320 + NOTIFY_TZ_PRINTF("tzsetwall_basic lcl_is_set < 0\n");
323 + NOTIFY_TZ_PRINTF("tzsetwall_basic not set\n");
324 +#else /* ! NOTIFY_TZ_DEBUG */
327 +#endif /* NOTIFY_TZ_DEBUG */
331 @@ -949,12 +1171,18 @@
332 #endif /* defined ALL_STATE */
333 if (tzload((char *) NULL, lclptr) != 0)
336 + notify_register_tz(fullname, &lcl_notify);
337 +#endif /* NOTIFY_TZ */
344 +#ifdef NOTIFY_TZ_DEBUG
345 + NOTIFY_TZ_PRINTF("tzsetwall called\n");
346 +#endif /* NOTIFY_TZ_DEBUG */
347 _MUTEX_LOCK(&lcl_mutex);
349 _MUTEX_UNLOCK(&lcl_mutex);
350 @@ -971,8 +1199,19 @@
355 + notify_check_tz(&lcl_notify);
356 +#endif /* NOTIFY_TZ */
357 +#ifdef NOTIFY_TZ_DEBUG
358 + if (lcl_is_set > 0 && strcmp(lcl_TZname, name) == 0) {
359 + NOTIFY_TZ_PRINTF("tzset_basic matched %s\n", lcl_TZname);
362 + NOTIFY_TZ_PRINTF("tzset_basic didn't matched %s\n", lcl_TZname);
363 +#else /* ! NOTIFY_TZ_DEBUG */
364 if (lcl_is_set > 0 && strcmp(lcl_TZname, name) == 0)
366 +#endif /* NOTIFY_TZ_DEBUG */
367 lcl_is_set = (strlen(name) < sizeof(lcl_TZname));
369 (void) strcpy(lcl_TZname, name);
370 @@ -995,15 +1234,24 @@
371 lclptr->ttis[0].tt_gmtoff = 0;
372 lclptr->ttis[0].tt_abbrind = 0;
373 (void) strcpy(lclptr->chars, gmt);
376 +#endif /* NOTIFY_TZ */
377 } else if (tzload(name, lclptr) != 0)
378 if (name[0] == ':' || tzparse(name, lclptr, FALSE) != 0)
379 (void) gmtload(lclptr);
381 + notify_register_tz(fullname, &lcl_notify);
382 +#endif /* NOTIFY_TZ */
389 +#ifdef NOTIFY_TZ_DEBUG
390 + NOTIFY_TZ_PRINTF("tzset called TZ=%s\n", getenv("TZ"));
391 +#endif /* NOTIFY_TZ_DEBUG */
392 _MUTEX_LOCK(&lcl_mutex);
394 _MUTEX_UNLOCK(&lcl_mutex);
395 @@ -1030,6 +1278,9 @@
397 const time_t t = *timep;
399 +#ifdef NOTIFY_TZ_DEBUG
400 + NOTIFY_TZ_PRINTF("localsub called\n");
401 +#endif /* NOTIFY_TZ_DEBUG */
405 @@ -1087,7 +1338,7 @@
407 if (__isthreaded != 0) {
408 _pthread_mutex_lock(&localtime_mutex);
409 - if (localtime_key < 0) {
410 + if (localtime_key == (pthread_key_t)-1) {
411 if (_pthread_key_create(&localtime_key, free) < 0) {
412 _pthread_mutex_unlock(&localtime_mutex);
414 @@ -1123,14 +1374,30 @@
416 struct tm * const tmp;
418 +#ifdef NOTIFY_TZ_DEBUG
419 + NOTIFY_TZ_PRINTF("gmtsub called\n");
420 +#endif /* NOTIFY_TZ_DEBUG */
421 _MUTEX_LOCK(&gmt_mutex);
423 + notify_check_tz(&gmt_notify);
424 +#endif /* NOTIFY_TZ */
428 - gmtptr = (struct state *) malloc(sizeof *gmtptr);
430 + if (gmtptr == NULL)
431 +#endif /* NOTIFY_TZ */
432 + gmtptr = (struct state *) malloc(sizeof *gmtptr);
436 +#endif /* NOTIFY_TZ */
437 #endif /* defined ALL_STATE */
440 + notify_register_tz(fullname, &gmt_notify);
442 +#endif /* NOTIFY_TZ */
444 _MUTEX_UNLOCK(&gmt_mutex);
445 timesub(timep, offset, gmtptr, tmp);
446 @@ -1145,7 +1412,7 @@
450 - tmp->TM_ZONE = gmt;
451 + tmp->TM_ZONE = (char *)gmt;
452 else tmp->TM_ZONE = gmtptr->chars;
453 #endif /* defined ALL_STATE */
455 @@ -1165,7 +1432,7 @@
457 if (__isthreaded != 0) {
458 _pthread_mutex_lock(&gmtime_mutex);
459 - if (gmtime_key < 0) {
460 + if (gmtime_key == (pthread_key_t)-1) {
461 if (_pthread_key_create(&gmtime_key, free) < 0) {
462 _pthread_mutex_unlock(&gmtime_mutex);