]> git.saurik.com Git - apple/libc.git/blob - stdtime/FreeBSD/localtime.c.patch
11d2cc12b82cde0ab0226ae5566fd1600c9a2f96
[apple/libc.git] / stdtime / FreeBSD / localtime.c.patch
1 --- localtime.c.orig Thu Aug 7 18:34:10 2003
2 +++ localtime.c Thu Aug 7 16:57:55 2003
3 @@ -24,6 +24,18 @@
4 #include <sys/stat.h>
5 #include <fcntl.h>
6 #include <pthread.h>
7 +#ifdef NOTIFY_TZ
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 */
12 +#ifndef ALL_STATE
13 +#define ALL_STATE
14 +#endif /* ALL_STATE */
15 +#include <mach/mach_init.h>
16 +#include <notify.h>
17 +#include <alloca.h>
18 +#endif /* NOTIFY_TZ */
19 #include "private.h"
20 #include "un-namespace.h"
21
22 @@ -119,6 +131,16 @@
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 */
25
26 +#ifdef NOTIFY_TZ
27 +typedef struct {
28 + int token;
29 + int notify_was_off;
30 + int is_set;
31 +} notify_tz_t;
32 +
33 +#define NOTIFY_TZ_NAME "com.apple.system.timezone"
34 +#endif /* NOTIFY_TZ */
35 +
36 /*
37 ** Prototypes for static functions.
38 */
39 @@ -138,6 +160,10 @@
40 static int increment_overflow(int * number, int delta);
41 static int normalize_overflow(int * tensptr, int * unitsptr,
42 int base);
43 +#ifdef NOTIFY_TZ
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 *,
50 @@ -174,8 +200,13 @@
51 #endif /* !defined TZ_STRLEN_MAX */
52
53 static char lcl_TZname[TZ_STRLEN_MAX + 1];
54 +#ifdef NOTIFY_TZ
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;
63
64 @@ -203,6 +234,50 @@
65 time_t altzone = 0;
66 #endif /* defined ALTZONE */
67
68 +#ifdef NOTIFY_TZ
69 +#ifdef NOTIFY_TZ_DEBUG
70 +#ifdef NOTIFY_TZ_DEBUG_FILE
71 +#define NOTIFY_TZ_PRINTF(fmt, args...) \
72 +{ \
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_); \
77 + } \
78 +}
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 */
83 +#ifdef NOTIFY_TZ_LOG
84 +#define NOTIFY_LOG(fmt, args...) \
85 +{ \
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_); \
90 + } \
91 +}
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__;
99 +
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
103 + * is being used.
104 + *-------------------------------------------------------------------- */
105 +static char * fullname = NULL;
106 +
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 */
111 +
112 static long
113 detzcode(codep)
114 const char * const codep;
115 @@ -233,7 +308,7 @@
116 #endif /* defined ALTZONE */
117 #ifdef ALL_STATE
118 if (sp == NULL) {
119 - tzname[0] = tzname[1] = gmt;
120 + tzname[0] = tzname[1] = (char *)gmt;
121 return;
122 }
123 #endif /* defined ALL_STATE */
124 @@ -266,6 +341,117 @@
125 }
126 }
127
128 +#ifdef NOTIFY_TZ
129 +static void
130 +notify_check_tz(notify_tz_t *p)
131 +{
132 + unsigned int nstat;
133 + int ncheck;
134 +
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;
140 + return;
141 + }
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 */
147 + p->is_set = 0;
148 + p->notify_was_off = 0;
149 + return;
150 + }
151 + if (p->token < 0)
152 + return;
153 + nstat = notify_check(p->token, &ncheck);
154 + if (nstat || ncheck) {
155 + p->is_set = 0;
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 */
159 + }
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 */
163 +}
164 +
165 +static void
166 +notify_register_tz(char *file, notify_tz_t *p)
167 +{
168 + char *name;
169 + unsigned int nstat;
170 + int ncheck;
171 +
172 + if (__notify_78945668_info__ < 0)
173 + return;
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 + *----------------------------------------------------------------*/
178 + if (p->token >= 0)
179 + notify_cancel(p->token);
180 + if (*file == 0) {
181 + /* no time zone file to monitor */
182 + p->token = -1;
183 + return;
184 + }
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;
191 + else {
192 + name = alloca(sizeof(notify_tz_name) + strlen(file) + 1);
193 + if (name == NULL) {
194 + p->token = -1;
195 + return;
196 + }
197 + strcpy(name, notify_tz_name);
198 + strcat(name, ".");
199 + strcat(name, file);
200 + }
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);
205 + if (nstat != 0) {
206 + p->token = -1;
207 + p->is_set = 0;
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 */
214 + return;
215 + }
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);
222 + if (nstat != 0) {
223 + notify_cancel(p->token);
224 + p->token = -1;
225 + p->is_set = 0;
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 */
232 + return;
233 + }
234 + }
235 + notify_check(p->token, &ncheck); /* this always returns true */
236 +}
237 +#endif /* NOTIFY_TZ */
238 +
239 static int
240 tzload(name, sp)
241 const char * name;
242 @@ -275,6 +461,9 @@
243 int i;
244 int fid;
245
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] == '/') ||
252 @@ -292,7 +481,15 @@
253 ** to hold the longest file name string that the implementation
254 ** guarantees can be opened."
255 */
256 +#ifdef NOTIFY_TZ
257 + if (!fullname) {
258 + fullname = malloc(FILENAME_MAX + 1);
259 + if (!fullname)
260 + return -1;
261 + }
262 +#else /* ! NOTIFY_TZ */
263 char fullname[FILENAME_MAX + 1];
264 +#endif /* NOTIFY_TZ */
265
266 if (name[0] == ':')
267 ++name;
268 @@ -300,7 +497,11 @@
269 if (!doaccess) {
270 if ((p = TZDIR) == NULL)
271 return -1;
272 +#ifdef NOTIFY_TZ
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 */
277 return -1;
278 (void) strcpy(fullname, p);
279 (void) strcat(fullname, "/");
280 @@ -312,6 +513,10 @@
281 doaccess = TRUE;
282 name = fullname;
283 }
284 +#ifdef NOTIFY_TZ
285 + else
286 + strcpy(fullname, name);
287 +#endif /* NOTIFY_TZ */
288 if (doaccess && access(name, R_OK) != 0)
289 return -1;
290 if ((fid = _open(name, OPEN_MODE)) == -1)
291 @@ -327,6 +532,9 @@
292 int ttisstdcnt;
293 int ttisgmtcnt;
294
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)
300 return -1;
301 @@ -748,6 +956,9 @@
302 }
303 }
304 load_result = tzload(TZDEFRULES, sp);
305 +#ifdef NOTIFY_TZ
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 */
310 if (*name != '\0') {
311 @@ -934,8 +1145,19 @@
312 static void
313 tzsetwall_basic(void)
314 {
315 +#ifdef NOTIFY_TZ
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");
321 + return;
322 + }
323 + NOTIFY_TZ_PRINTF("tzsetwall_basic not set\n");
324 +#else /* ! NOTIFY_TZ_DEBUG */
325 if (lcl_is_set < 0)
326 return;
327 +#endif /* NOTIFY_TZ_DEBUG */
328 lcl_is_set = -1;
329
330 #ifdef ALL_STATE
331 @@ -949,12 +1171,18 @@
332 #endif /* defined ALL_STATE */
333 if (tzload((char *) NULL, lclptr) != 0)
334 gmtload(lclptr);
335 +#ifdef NOTIFY_TZ
336 + notify_register_tz(fullname, &lcl_notify);
337 +#endif /* NOTIFY_TZ */
338 settzname();
339 }
340
341 void
342 tzsetwall(void)
343 {
344 +#ifdef NOTIFY_TZ_DEBUG
345 + NOTIFY_TZ_PRINTF("tzsetwall called\n");
346 +#endif /* NOTIFY_TZ_DEBUG */
347 _MUTEX_LOCK(&lcl_mutex);
348 tzsetwall_basic();
349 _MUTEX_UNLOCK(&lcl_mutex);
350 @@ -971,8 +1199,19 @@
351 return;
352 }
353
354 +#ifdef NOTIFY_TZ
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);
360 + return;
361 + }
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)
365 return;
366 +#endif /* NOTIFY_TZ_DEBUG */
367 lcl_is_set = (strlen(name) < sizeof(lcl_TZname));
368 if (lcl_is_set)
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);
374 +#ifdef NOTIFY_TZ
375 + *fullname = 0;
376 +#endif /* NOTIFY_TZ */
377 } else if (tzload(name, lclptr) != 0)
378 if (name[0] == ':' || tzparse(name, lclptr, FALSE) != 0)
379 (void) gmtload(lclptr);
380 +#ifdef NOTIFY_TZ
381 + notify_register_tz(fullname, &lcl_notify);
382 +#endif /* NOTIFY_TZ */
383 settzname();
384 }
385
386 void
387 tzset(void)
388 {
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);
393 tzset_basic();
394 _MUTEX_UNLOCK(&lcl_mutex);
395 @@ -1030,6 +1278,9 @@
396 int i;
397 const time_t t = *timep;
398
399 +#ifdef NOTIFY_TZ_DEBUG
400 + NOTIFY_TZ_PRINTF("localsub called\n");
401 +#endif /* NOTIFY_TZ_DEBUG */
402 sp = lclptr;
403 #ifdef ALL_STATE
404 if (sp == NULL) {
405 @@ -1087,7 +1338,7 @@
406
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);
413 return(NULL);
414 @@ -1123,14 +1374,30 @@
415 const long offset;
416 struct tm * const tmp;
417 {
418 +#ifdef NOTIFY_TZ_DEBUG
419 + NOTIFY_TZ_PRINTF("gmtsub called\n");
420 +#endif /* NOTIFY_TZ_DEBUG */
421 _MUTEX_LOCK(&gmt_mutex);
422 +#ifdef NOTIFY_TZ
423 + notify_check_tz(&gmt_notify);
424 +#endif /* NOTIFY_TZ */
425 if (!gmt_is_set) {
426 gmt_is_set = TRUE;
427 #ifdef ALL_STATE
428 - gmtptr = (struct state *) malloc(sizeof *gmtptr);
429 +#ifdef NOTIFY_TZ
430 + if (gmtptr == NULL)
431 +#endif /* NOTIFY_TZ */
432 + gmtptr = (struct state *) malloc(sizeof *gmtptr);
433 if (gmtptr != NULL)
434 +#ifdef NOTIFY_TZ
435 + {
436 +#endif /* NOTIFY_TZ */
437 #endif /* defined ALL_STATE */
438 gmtload(gmtptr);
439 +#ifdef NOTIFY_TZ
440 + notify_register_tz(fullname, &gmt_notify);
441 + }
442 +#endif /* NOTIFY_TZ */
443 }
444 _MUTEX_UNLOCK(&gmt_mutex);
445 timesub(timep, offset, gmtptr, tmp);
446 @@ -1145,7 +1412,7 @@
447 else {
448 #ifdef ALL_STATE
449 if (gmtptr == NULL)
450 - tmp->TM_ZONE = gmt;
451 + tmp->TM_ZONE = (char *)gmt;
452 else tmp->TM_ZONE = gmtptr->chars;
453 #endif /* defined ALL_STATE */
454 #ifndef ALL_STATE
455 @@ -1165,7 +1432,7 @@
456
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);
463 return(NULL);