]> granicus.if.org Git - apache/blob - server/util_time.c
fr doc rebuild.
[apache] / server / util_time.c
1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2  * contributor license agreements.  See the NOTICE file distributed with
3  * this work for additional information regarding copyright ownership.
4  * The ASF licenses this file to You under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with
6  * the License.  You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "util_time.h"
18 #include "apr_env.h"
19
20
21
22 /* Number of characters needed to format the microsecond part of a timestamp.
23  * Microseconds have 6 digits plus one separator character makes 7.
24  *   */
25 #define AP_CTIME_USEC_LENGTH      7
26
27 /* Length of ISO 8601 date/time */
28 #define AP_CTIME_COMPACT_LEN      20
29
30
31 /* Cache for exploded values of recent timestamps
32  */
33
34 struct exploded_time_cache_element {
35     apr_int64_t t;
36     apr_time_exp_t xt;
37     apr_int64_t t_validate; /* please see comments in cached_explode() */
38 };
39
40 /* the "+ 1" is for the current second: */
41 #define TIME_CACHE_SIZE (AP_TIME_RECENT_THRESHOLD + 1)
42
43 /* Note that AP_TIME_RECENT_THRESHOLD is defined to
44  * be a power of two minus one in util_time.h, so that
45  * we can replace a modulo operation with a bitwise AND
46  * when hashing items into a cache of size
47  * AP_TIME_RECENT_THRESHOLD+1
48  */
49 #define TIME_CACHE_MASK (AP_TIME_RECENT_THRESHOLD)
50
51 static struct exploded_time_cache_element exploded_cache_localtime[TIME_CACHE_SIZE];
52 static struct exploded_time_cache_element exploded_cache_gmt[TIME_CACHE_SIZE];
53
54
55 static apr_status_t cached_explode(apr_time_exp_t *xt, apr_time_t t,
56                                    struct exploded_time_cache_element *cache,
57                                    int use_gmt)
58 {
59     apr_int64_t seconds = apr_time_sec(t);
60     struct exploded_time_cache_element *cache_element =
61         &(cache[seconds & TIME_CACHE_MASK]);
62     struct exploded_time_cache_element cache_element_snapshot;
63
64     /* The cache is implemented as a ring buffer.  Each second,
65      * it uses a different element in the buffer.  The timestamp
66      * in the element indicates whether the element contains the
67      * exploded time for the current second (vs the time
68      * 'now - AP_TIME_RECENT_THRESHOLD' seconds ago).  If the
69      * cached value is for the current time, we use it.  Otherwise,
70      * we compute the apr_time_exp_t and store it in this
71      * cache element. Note that the timestamp in the cache
72      * element is updated only after the exploded time.  Thus
73      * if two threads hit this cache element simultaneously
74      * at the start of a new second, they'll both explode the
75      * time and store it.  I.e., the writers will collide, but
76      * they'll be writing the same value.
77      */
78     if (cache_element->t >= seconds) {
79         /* There is an intentional race condition in this design:
80          * in a multithreaded app, one thread might be reading
81          * from this cache_element to resolve a timestamp from
82          * TIME_CACHE_SIZE seconds ago at the same time that
83          * another thread is copying the exploded form of the
84          * current time into the same cache_element.  (I.e., the
85          * first thread might hit this element of the ring buffer
86          * just as the element is being recycled.)  This can
87          * also happen at the start of a new second, if a
88          * reader accesses the cache_element after a writer
89          * has updated cache_element.t but before the writer
90          * has finished updating the whole cache_element.
91          *
92          * Rather than trying to prevent this race condition
93          * with locks, we allow it to happen and then detect
94          * and correct it.  The detection works like this:
95          *   Step 1: Take a "snapshot" of the cache element by
96          *           copying it into a temporary buffer.
97          *   Step 2: Check whether the snapshot contains consistent
98          *           data: the timestamps at the start and end of
99          *           the cache_element should both match the 'seconds'
100          *           value that we computed from the input time.
101          *           If these three don't match, then the snapshot
102          *           shows the cache_element in the middle of an
103          *           update, and its contents are invalid.
104          *   Step 3: If the snapshot is valid, use it.  Otherwise,
105          *           just give up on the cache and explode the
106          *           input time.
107          */
108         memcpy(&cache_element_snapshot, cache_element,
109                sizeof(struct exploded_time_cache_element));
110         if ((seconds != cache_element_snapshot.t) ||
111             (seconds != cache_element_snapshot.t_validate)) {
112             /* Invalid snapshot */
113             if (use_gmt) {
114                 return apr_time_exp_gmt(xt, t);
115             }
116             else {
117                 return apr_time_exp_lt(xt, t);
118             }
119         }
120         else {
121             /* Valid snapshot */
122             memcpy(xt, &(cache_element_snapshot.xt),
123                    sizeof(apr_time_exp_t));
124         }
125     }
126     else {
127         apr_status_t r;
128         if (use_gmt) {
129             r = apr_time_exp_gmt(xt, t);
130         }
131         else {
132             r = apr_time_exp_lt(xt, t);
133         }
134         if (r != APR_SUCCESS) {
135             return r;
136         }
137         cache_element->t = seconds;
138         memcpy(&(cache_element->xt), xt, sizeof(apr_time_exp_t));
139         cache_element->t_validate = seconds;
140     }
141     xt->tm_usec = (int)apr_time_usec(t);
142     return APR_SUCCESS;
143 }
144
145
146 AP_DECLARE(apr_status_t) ap_explode_recent_localtime(apr_time_exp_t * tm,
147                                                      apr_time_t t)
148 {
149     return cached_explode(tm, t, exploded_cache_localtime, 0);
150 }
151
152 AP_DECLARE(apr_status_t) ap_explode_recent_gmt(apr_time_exp_t * tm,
153                                                apr_time_t t)
154 {
155     return cached_explode(tm, t, exploded_cache_gmt, 1);
156 }
157
158 AP_DECLARE(apr_status_t) ap_recent_ctime(char *date_str, apr_time_t t)
159 {
160     int len = APR_CTIME_LEN;
161     return ap_recent_ctime_ex(date_str, t, AP_CTIME_OPTION_NONE, &len);
162 }
163
164 AP_DECLARE(apr_status_t) ap_recent_ctime_ex(char *date_str, apr_time_t t,
165                                             int option, int *len)
166 {
167     /* ### This code is a clone of apr_ctime(), except that it
168      * uses ap_explode_recent_localtime() instead of apr_time_exp_lt().
169      */
170     apr_time_exp_t xt;
171     const char *s;
172     int real_year;
173     int needed;
174
175
176     /* Calculate the needed buffer length */
177     if (option & AP_CTIME_OPTION_COMPACT)
178         needed = AP_CTIME_COMPACT_LEN;
179     else
180         needed = APR_CTIME_LEN;
181
182     if (option & AP_CTIME_OPTION_USEC) {
183         needed += AP_CTIME_USEC_LENGTH;
184     }
185
186     /* Check the provided buffer length */
187     if (len && *len >= needed) {
188         *len = needed;
189     }
190     else {
191         if (len != NULL) {
192             *len = 0;
193         }
194         return APR_ENOMEM;
195     }
196
197     /* example without options: "Wed Jun 30 21:49:08 1993" */
198     /*                           123456789012345678901234  */
199     /* example for compact format: "1993-06-30 21:49:08" */
200     /*                              1234567890123456789  */
201
202     ap_explode_recent_localtime(&xt, t);
203     real_year = 1900 + xt.tm_year;
204     if (option & AP_CTIME_OPTION_COMPACT) {
205         int real_month = xt.tm_mon + 1;
206         *date_str++ = real_year / 1000 + '0';
207         *date_str++ = real_year % 1000 / 100 + '0';
208         *date_str++ = real_year % 100 / 10 + '0';
209         *date_str++ = real_year % 10 + '0';
210         *date_str++ = '-';
211         *date_str++ = real_month / 10 + '0';
212         *date_str++ = real_month % 10 + '0';
213         *date_str++ = '-';
214     }
215     else {
216         s = &apr_day_snames[xt.tm_wday][0];
217         *date_str++ = *s++;
218         *date_str++ = *s++;
219         *date_str++ = *s++;
220         *date_str++ = ' ';
221         s = &apr_month_snames[xt.tm_mon][0];
222         *date_str++ = *s++;
223         *date_str++ = *s++;
224         *date_str++ = *s++;
225         *date_str++ = ' ';
226     }
227     *date_str++ = xt.tm_mday / 10 + '0';
228     *date_str++ = xt.tm_mday % 10 + '0';
229     *date_str++ = ' ';
230     *date_str++ = xt.tm_hour / 10 + '0';
231     *date_str++ = xt.tm_hour % 10 + '0';
232     *date_str++ = ':';
233     *date_str++ = xt.tm_min / 10 + '0';
234     *date_str++ = xt.tm_min % 10 + '0';
235     *date_str++ = ':';
236     *date_str++ = xt.tm_sec / 10 + '0';
237     *date_str++ = xt.tm_sec % 10 + '0';
238     if (option & AP_CTIME_OPTION_USEC) {
239         int div;
240         int usec = (int)xt.tm_usec;
241         *date_str++ = '.';
242         for (div=100000; div>0; div=div/10) {
243             *date_str++ = usec / div + '0';
244             usec = usec % div;
245         }
246     }
247     if (!(option & AP_CTIME_OPTION_COMPACT)) {
248         *date_str++ = ' ';
249         *date_str++ = real_year / 1000 + '0';
250         *date_str++ = real_year % 1000 / 100 + '0';
251         *date_str++ = real_year % 100 / 10 + '0';
252         *date_str++ = real_year % 10 + '0';
253     }
254     *date_str++ = 0;
255
256     return APR_SUCCESS;
257 }
258
259 AP_DECLARE(apr_status_t) ap_recent_rfc822_date(char *date_str, apr_time_t t)
260 {
261     /* ### This code is a clone of apr_rfc822_date(), except that it
262      * uses ap_explode_recent_gmt() instead of apr_time_exp_gmt().
263      */
264     apr_time_exp_t xt;
265     const char *s;
266     int real_year;
267
268     ap_explode_recent_gmt(&xt, t);
269
270     /* example: "Sat, 08 Jan 2000 18:31:41 GMT" */
271     /*           12345678901234567890123456789  */
272
273     s = &apr_day_snames[xt.tm_wday][0];
274     *date_str++ = *s++;
275     *date_str++ = *s++;
276     *date_str++ = *s++;
277     *date_str++ = ',';
278     *date_str++ = ' ';
279     *date_str++ = xt.tm_mday / 10 + '0';
280     *date_str++ = xt.tm_mday % 10 + '0';
281     *date_str++ = ' ';
282     s = &apr_month_snames[xt.tm_mon][0];
283     *date_str++ = *s++;
284     *date_str++ = *s++;
285     *date_str++ = *s++;
286     *date_str++ = ' ';
287     real_year = 1900 + xt.tm_year;
288     /* This routine isn't y10k ready. */
289     *date_str++ = real_year / 1000 + '0';
290     *date_str++ = real_year % 1000 / 100 + '0';
291     *date_str++ = real_year % 100 / 10 + '0';
292     *date_str++ = real_year % 10 + '0';
293     *date_str++ = ' ';
294     *date_str++ = xt.tm_hour / 10 + '0';
295     *date_str++ = xt.tm_hour % 10 + '0';
296     *date_str++ = ':';
297     *date_str++ = xt.tm_min / 10 + '0';
298     *date_str++ = xt.tm_min % 10 + '0';
299     *date_str++ = ':';
300     *date_str++ = xt.tm_sec / 10 + '0';
301     *date_str++ = xt.tm_sec % 10 + '0';
302     *date_str++ = ' ';
303     *date_str++ = 'G';
304     *date_str++ = 'M';
305     *date_str++ = 'T';
306     *date_str++ = 0;
307     return APR_SUCCESS;
308 }
309
310 AP_DECLARE(void) ap_force_set_tz(apr_pool_t *p) {
311     /* If the TZ variable is unset, many operating systems,
312      * such as Linux, will at runtime read from /etc/localtime
313      * and call fstat on it.
314      *
315      * By forcing the time zone to UTC if it is unset, we gain
316      * about 2% in raw requests/second (since we format log files
317      * in the local time, if present)
318      *
319      * For more info, see:
320      *   <http://www.gnu.org/s/hello/manual/libc/TZ-Variable.html>
321      */
322     char *v = NULL;
323
324     if (apr_env_get(&v, "TZ", p) != APR_SUCCESS) {
325         apr_env_set("TZ", "UTC+0", p);
326     }
327 }