]> git.saurik.com Git - apple/libc.git/blob - stdtime/FreeBSD/localtime.c.patch
Libc-391.4.1.tar.gz
[apple/libc.git] / stdtime / FreeBSD / localtime.c.patch
1 --- localtime.c.orig 2004-11-01 23:24:08.000000000 -0800
2 +++ localtime.c 2004-11-05 23:43:55.000000000 -0800
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 @@ -135,6 +147,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 @@ -154,6 +176,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 @@ -194,8 +220,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 @@ -214,15 +245,62 @@
65
66 static struct tm tm;
67
68 +#define USG_COMPAT
69 +#define ALTZONE
70 #ifdef USG_COMPAT
71 -time_t timezone = 0;
72 int daylight = 0;
73 +__private_extern__ void _st_set_timezone(long);
74 #endif /* defined USG_COMPAT */
75
76 #ifdef ALTZONE
77 -time_t altzone = 0;
78 +__private_extern__ long __darwin_altzone = 0;
79 +#define altzone __darwin_altzone
80 #endif /* defined ALTZONE */
81
82 +#ifdef NOTIFY_TZ
83 +#ifdef NOTIFY_TZ_DEBUG
84 +#ifdef NOTIFY_TZ_DEBUG_FILE
85 +#define NOTIFY_TZ_PRINTF(fmt, args...) \
86 +{ \
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_); \
91 + } \
92 +}
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 */
97 +#ifdef NOTIFY_TZ_LOG
98 +#define NOTIFY_LOG(fmt, args...) \
99 +{ \
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_); \
104 + } \
105 +}
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__;
113 +
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
117 + * is being used.
118 + *-------------------------------------------------------------------- */
119 +static char * fullname = NULL;
120 +
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 */
125 +
126 static long
127 detzcode(codep)
128 const char * const codep;
129 @@ -246,14 +324,14 @@
130 tzname[1] = wildabbr;
131 #ifdef USG_COMPAT
132 daylight = 0;
133 - timezone = 0;
134 + _st_set_timezone(0);
135 #endif /* defined USG_COMPAT */
136 #ifdef ALTZONE
137 altzone = 0;
138 #endif /* defined ALTZONE */
139 #ifdef ALL_STATE
140 if (sp == NULL) {
141 - tzname[0] = tzname[1] = gmt;
142 + tzname[0] = tzname[1] = (char *)gmt;
143 return;
144 }
145 #endif /* defined ALL_STATE */
146 @@ -266,7 +344,7 @@
147 if (ttisp->tt_isdst)
148 daylight = 1;
149 if (i == 0 || !ttisp->tt_isdst)
150 - timezone = -(ttisp->tt_gmtoff);
151 + _st_set_timezone(-(ttisp->tt_gmtoff));
152 #endif /* defined USG_COMPAT */
153 #ifdef ALTZONE
154 if (i == 0 || ttisp->tt_isdst)
155 @@ -286,6 +364,117 @@
156 }
157 }
158
159 +#ifdef NOTIFY_TZ
160 +static void
161 +notify_check_tz(notify_tz_t *p)
162 +{
163 + unsigned int nstat;
164 + int ncheck;
165 +
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;
171 + return;
172 + }
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 */
178 + p->is_set = 0;
179 + p->notify_was_off = 0;
180 + return;
181 + }
182 + if (p->token < 0)
183 + return;
184 + nstat = notify_check(p->token, &ncheck);
185 + if (nstat || ncheck) {
186 + p->is_set = 0;
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 */
190 + }
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 */
194 +}
195 +
196 +static void
197 +notify_register_tz(char *file, notify_tz_t *p)
198 +{
199 + char *name;
200 + unsigned int nstat;
201 + int ncheck;
202 +
203 + if (__notify_78945668_info__ < 0)
204 + return;
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 + *----------------------------------------------------------------*/
209 + if (p->token >= 0)
210 + notify_cancel(p->token);
211 + if (!file || *file == 0) {
212 + /* no time zone file to monitor */
213 + p->token = -1;
214 + return;
215 + }
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;
222 + else {
223 + name = alloca(sizeof(notify_tz_name) + strlen(file) + 1);
224 + if (name == NULL) {
225 + p->token = -1;
226 + return;
227 + }
228 + strcpy(name, notify_tz_name);
229 + strcat(name, ".");
230 + strcat(name, file);
231 + }
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);
236 + if (nstat != 0) {
237 + p->token = -1;
238 + p->is_set = 0;
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 */
245 + return;
246 + }
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);
253 + if (nstat != 0) {
254 + notify_cancel(p->token);
255 + p->token = -1;
256 + p->is_set = 0;
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 */
263 + return;
264 + }
265 + }
266 + notify_check(p->token, &ncheck); /* this always returns true */
267 +}
268 +#endif /* NOTIFY_TZ */
269 +
270 static int
271 tzload(name, sp)
272 const char * name;
273 @@ -295,6 +484,9 @@
274 int i;
275 int fid;
276
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] == '/') ||
283 @@ -312,7 +504,15 @@
284 ** to hold the longest file name string that the implementation
285 ** guarantees can be opened."
286 */
287 +#ifdef NOTIFY_TZ
288 + if (!fullname) {
289 + fullname = malloc(FILENAME_MAX + 1);
290 + if (!fullname)
291 + return -1;
292 + }
293 +#else /* ! NOTIFY_TZ */
294 char fullname[FILENAME_MAX + 1];
295 +#endif /* NOTIFY_TZ */
296
297 if (name[0] == ':')
298 ++name;
299 @@ -320,7 +520,11 @@
300 if (!doaccess) {
301 if ((p = TZDIR) == NULL)
302 return -1;
303 +#ifdef NOTIFY_TZ
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 */
308 return -1;
309 (void) strcpy(fullname, p);
310 (void) strcat(fullname, "/");
311 @@ -332,6 +536,10 @@
312 doaccess = TRUE;
313 name = fullname;
314 }
315 +#ifdef NOTIFY_TZ
316 + else
317 + strcpy(fullname, name);
318 +#endif /* NOTIFY_TZ */
319 if (doaccess && access(name, R_OK) != 0)
320 return -1;
321 if ((fid = _open(name, OPEN_MODE)) == -1)
322 @@ -350,6 +558,9 @@
323 int ttisstdcnt;
324 int ttisgmtcnt;
325
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)
331 return -1;
332 @@ -764,6 +975,9 @@
333 }
334 }
335 load_result = tzload(TZDEFRULES, sp);
336 +#ifdef NOTIFY_TZ
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 */
341 if (*name != '\0') {
342 @@ -951,8 +1165,19 @@
343 static void
344 tzsetwall_basic(void)
345 {
346 +#ifdef NOTIFY_TZ
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");
352 + return;
353 + }
354 + NOTIFY_TZ_PRINTF("tzsetwall_basic not set\n");
355 +#else /* ! NOTIFY_TZ_DEBUG */
356 if (lcl_is_set < 0)
357 return;
358 +#endif /* NOTIFY_TZ_DEBUG */
359 lcl_is_set = -1;
360
361 #ifdef ALL_STATE
362 @@ -966,12 +1191,18 @@
363 #endif /* defined ALL_STATE */
364 if (tzload((char *) NULL, lclptr) != 0)
365 gmtload(lclptr);
366 +#ifdef NOTIFY_TZ
367 + notify_register_tz(fullname, &lcl_notify);
368 +#endif /* NOTIFY_TZ */
369 settzname();
370 }
371
372 void
373 tzsetwall(void)
374 {
375 +#ifdef NOTIFY_TZ_DEBUG
376 + NOTIFY_TZ_PRINTF("tzsetwall called\n");
377 +#endif /* NOTIFY_TZ_DEBUG */
378 _MUTEX_LOCK(&lcl_mutex);
379 tzsetwall_basic();
380 _MUTEX_UNLOCK(&lcl_mutex);
381 @@ -988,8 +1219,18 @@
382 return;
383 }
384
385 +#ifdef NOTIFY_TZ
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);
391 + return;
392 + }
393 +#else /* ! NOTIFY_TZ_DEBUG */
394 if (lcl_is_set > 0 && strcmp(lcl_TZname, name) == 0)
395 return;
396 +#endif /* NOTIFY_TZ_DEBUG */
397 lcl_is_set = strlen(name) < sizeof lcl_TZname;
398 if (lcl_is_set)
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);
404 +#ifdef NOTIFY_TZ
405 + if (fullname)
406 + *fullname = 0;
407 +#endif /* NOTIFY_TZ */
408 } else if (tzload(name, lclptr) != 0)
409 if (name[0] == ':' || tzparse(name, lclptr, FALSE) != 0)
410 (void) gmtload(lclptr);
411 +#ifdef NOTIFY_TZ
412 + notify_register_tz(fullname, &lcl_notify);
413 +#endif /* NOTIFY_TZ */
414 settzname();
415 }
416
417 void
418 tzset(void)
419 {
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);
424 tzset_basic();
425 _MUTEX_UNLOCK(&lcl_mutex);
426 @@ -1049,6 +1300,9 @@
427 int i;
428 const time_t t = *timep;
429
430 +#ifdef NOTIFY_TZ_DEBUG
431 + NOTIFY_TZ_PRINTF("localsub called\n");
432 +#endif /* NOTIFY_TZ_DEBUG */
433 sp = lclptr;
434 #ifdef ALL_STATE
435 if (sp == NULL) {
436 @@ -1094,7 +1348,7 @@
437
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);
444 return(NULL);
445 @@ -1146,14 +1400,30 @@
446 const long offset;
447 struct tm * const tmp;
448 {
449 +#ifdef NOTIFY_TZ_DEBUG
450 + NOTIFY_TZ_PRINTF("gmtsub called\n");
451 +#endif /* NOTIFY_TZ_DEBUG */
452 _MUTEX_LOCK(&gmt_mutex);
453 +#ifdef NOTIFY_TZ
454 + notify_check_tz(&gmt_notify);
455 +#endif /* NOTIFY_TZ */
456 if (!gmt_is_set) {
457 gmt_is_set = TRUE;
458 #ifdef ALL_STATE
459 - gmtptr = (struct state *) malloc(sizeof *gmtptr);
460 +#ifdef NOTIFY_TZ
461 + if (gmtptr == NULL)
462 +#endif /* NOTIFY_TZ */
463 + gmtptr = (struct state *) malloc(sizeof *gmtptr);
464 if (gmtptr != NULL)
465 +#ifdef NOTIFY_TZ
466 + {
467 +#endif /* NOTIFY_TZ */
468 #endif /* defined ALL_STATE */
469 gmtload(gmtptr);
470 +#ifdef NOTIFY_TZ
471 + notify_register_tz(fullname, &gmt_notify);
472 + }
473 +#endif /* NOTIFY_TZ */
474 }
475 _MUTEX_UNLOCK(&gmt_mutex);
476 timesub(timep, offset, gmtptr, tmp);
477 @@ -1168,7 +1438,7 @@
478 else {
479 #ifdef ALL_STATE
480 if (gmtptr == NULL)
481 - tmp->TM_ZONE = gmt;
482 + tmp->TM_ZONE = (char *)gmt;
483 else tmp->TM_ZONE = gmtptr->chars;
484 #endif /* defined ALL_STATE */
485 #ifndef ALL_STATE
486 @@ -1188,7 +1458,7 @@
487
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);
494 return(NULL);