~ubuntu-branches/ubuntu/feisty/apache2/feisty

« back to all changes in this revision

Viewing changes to server/util_time.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Barth
  • Date: 2006-12-09 21:05:45 UTC
  • mfrom: (0.6.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20061209210545-h70s0xaqc2v8vqr2
Tags: 2.2.3-3.2
* Non-maintainer upload.
* 043_ajp_connection_reuse: Patch from upstream Bugzilla, fixing a critical
  issue with regard to connection reuse in mod_proxy_ajp.
  Closes: #396265

Show diffs side-by-side

added added

removed removed

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