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