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

« back to all changes in this revision

Viewing changes to support/rotatelogs.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
/*
 
18
 * Simple program to rotate Apache logs without having to kill the server.
 
19
 *
 
20
 * Contributed by Ben Laurie <ben algroup.co.uk>
 
21
 *
 
22
 * 12 Mar 1996
 
23
 *
 
24
 * Ported to APR by Mladen Turk <mturk mappingsoft.com>
 
25
 *
 
26
 * 23 Sep 2001
 
27
 *
 
28
 * -l option added 2004-06-11
 
29
 *
 
30
 * -l causes the use of local time rather than GMT as the base for the
 
31
 * interval.  NB: Using -l in an environment which changes the GMT offset
 
32
 * (such as for BST or DST) can lead to unpredictable results!
 
33
 *
 
34
 */
 
35
 
 
36
 
 
37
#include "apr.h"
 
38
#include "apr_lib.h"
 
39
#include "apr_strings.h"
 
40
#include "apr_errno.h"
 
41
#include "apr_file_io.h"
 
42
#include "apr_file_info.h"
 
43
#include "apr_general.h"
 
44
#include "apr_time.h"
 
45
 
 
46
#if APR_HAVE_STDLIB_H
 
47
#include <stdlib.h>
 
48
#endif
 
49
#if APR_HAVE_STRING_H
 
50
#include <string.h>
 
51
#endif
 
52
#if APR_HAVE_STRINGS_H
 
53
#include <strings.h>
 
54
#endif
 
55
 
 
56
#define BUFSIZE         65536
 
57
#define ERRMSGSZ        82
 
58
 
 
59
#ifndef MAX_PATH
 
60
#define MAX_PATH        1024
 
61
#endif
 
62
 
 
63
int main (int argc, const char * const argv[])
 
64
{
 
65
    char buf[BUFSIZE], buf2[MAX_PATH], errbuf[ERRMSGSZ];
 
66
    int tLogEnd = 0, tRotation = 0, utc_offset = 0;
 
67
    unsigned int sRotation = 0;
 
68
    int nMessCount = 0;
 
69
    apr_size_t nRead, nWrite;
 
70
    int use_strftime = 0;
 
71
    int use_localtime = 0;
 
72
    int now = 0;
 
73
    const char *szLogRoot;
 
74
    apr_file_t *f_stdin, *nLogFD = NULL, *nLogFDprev = NULL;
 
75
    apr_pool_t *pool;
 
76
    char *ptr = NULL;
 
77
    int argBase = 0;
 
78
    int argFile = 1;
 
79
    int argIntv = 2;
 
80
    int argOffset = 3;
 
81
 
 
82
    apr_app_initialize(&argc, &argv, NULL);
 
83
    atexit(apr_terminate);
 
84
 
 
85
    apr_pool_create(&pool, NULL);
 
86
    if ((argc > 2) && (strcmp(argv[1], "-l") == 0)) {
 
87
        argBase++;
 
88
        argFile += argBase;
 
89
        argIntv += argBase;
 
90
        argOffset += argBase;
 
91
        use_localtime = 1;
 
92
    }
 
93
    if (argc < (argBase + 3) || argc > (argBase + 4)) {
 
94
        fprintf(stderr,
 
95
                "Usage: %s [-l] <logfile> <rotation time in seconds> "
 
96
                "[offset minutes from UTC] or <rotation size in megabytes>\n\n",
 
97
                argv[0]);
 
98
#ifdef OS2
 
99
        fprintf(stderr,
 
100
                "Add this:\n\nTransferLog \"|%s.exe /some/where 86400\"\n\n",
 
101
                argv[0]);
 
102
#else
 
103
        fprintf(stderr,
 
104
                "Add this:\n\nTransferLog \"|%s /some/where 86400\"\n\n",
 
105
                argv[0]);
 
106
        fprintf(stderr,
 
107
                "or \n\nTransferLog \"|%s /some/where 5M\"\n\n", argv[0]);
 
108
#endif
 
109
        fprintf(stderr,
 
110
                "to httpd.conf. The generated name will be /some/where.nnnn "
 
111
                "where nnnn is the\nsystem time at which the log nominally "
 
112
                "starts (N.B. if using a rotation time,\nthe time will always "
 
113
                "be a multiple of the rotation time, so you can synchronize\n"
 
114
                "cron scripts with it). At the end of each rotation time or "
 
115
                "when the file size\nis reached a new log is started.\n");
 
116
        exit(1);
 
117
    }
 
118
 
 
119
    szLogRoot = argv[argFile];
 
120
 
 
121
    ptr = strchr(argv[argIntv], 'M');
 
122
    if (ptr) {
 
123
        if (*(ptr+1) == '\0') {
 
124
            sRotation = atoi(argv[argIntv]) * 1048576;
 
125
        }
 
126
        if (sRotation == 0) {
 
127
            fprintf(stderr, "Invalid rotation size parameter\n");
 
128
            exit(1);
 
129
        }
 
130
    }
 
131
    else {
 
132
        if (argc >= (argBase + 4)) {
 
133
            utc_offset = atoi(argv[argOffset]) * 60;
 
134
        }
 
135
        tRotation = atoi(argv[argIntv]);
 
136
        if (tRotation <= 0) {
 
137
            fprintf(stderr, "Rotation time must be > 0\n");
 
138
            exit(6);
 
139
        }
 
140
    }
 
141
 
 
142
    use_strftime = (strchr(szLogRoot, '%') != NULL);
 
143
    if (apr_file_open_stdin(&f_stdin, pool) != APR_SUCCESS) {
 
144
        fprintf(stderr, "Unable to open stdin\n");
 
145
        exit(1);
 
146
    }
 
147
 
 
148
    for (;;) {
 
149
        nRead = sizeof(buf);
 
150
        if (apr_file_read(f_stdin, buf, &nRead) != APR_SUCCESS) {
 
151
            exit(3);
 
152
        }
 
153
        if (tRotation) {
 
154
            /*
 
155
             * Check for our UTC offset every time through the loop, since
 
156
             * it might change if there's a switch between standard and
 
157
             * daylight savings time.
 
158
             */
 
159
            if (use_localtime) {
 
160
                apr_time_exp_t lt;
 
161
                apr_time_exp_lt(&lt, apr_time_now());
 
162
                utc_offset = lt.tm_gmtoff;
 
163
            }
 
164
            now = (int)(apr_time_now() / APR_USEC_PER_SEC) + utc_offset;
 
165
            if (nLogFD != NULL && now >= tLogEnd) {
 
166
                nLogFDprev = nLogFD;
 
167
                nLogFD = NULL;
 
168
            }
 
169
        }
 
170
        else if (sRotation) {
 
171
            apr_finfo_t finfo;
 
172
            apr_off_t current_size = -1;
 
173
 
 
174
            if ((nLogFD != NULL) &&
 
175
                (apr_file_info_get(&finfo, APR_FINFO_SIZE, nLogFD) == APR_SUCCESS)) {
 
176
                current_size = finfo.size;
 
177
            }
 
178
 
 
179
            if (current_size > sRotation) {
 
180
                nLogFDprev = nLogFD;
 
181
                nLogFD = NULL;
 
182
            }
 
183
        }
 
184
        else {
 
185
            fprintf(stderr, "No rotation time or size specified\n");
 
186
            exit(2);
 
187
        }
 
188
 
 
189
        if (nLogFD == NULL) {
 
190
            int tLogStart;
 
191
 
 
192
            if (tRotation) {
 
193
                tLogStart = (now / tRotation) * tRotation;
 
194
            }
 
195
            else {
 
196
                tLogStart = (int)apr_time_sec(apr_time_now());
 
197
            }
 
198
 
 
199
            if (use_strftime) {
 
200
                apr_time_t tNow = apr_time_from_sec(tLogStart);
 
201
                apr_time_exp_t e;
 
202
                apr_size_t rs;
 
203
 
 
204
                apr_time_exp_gmt(&e, tNow);
 
205
                apr_strftime(buf2, &rs, sizeof(buf2), szLogRoot, &e);
 
206
            }
 
207
            else {
 
208
                sprintf(buf2, "%s.%010d", szLogRoot, tLogStart);
 
209
            }
 
210
            tLogEnd = tLogStart + tRotation;
 
211
            apr_file_open(&nLogFD, buf2, APR_READ | APR_WRITE | APR_CREATE | APR_APPEND,
 
212
                          APR_OS_DEFAULT, pool);
 
213
            if (nLogFD == NULL) {
 
214
                /* Uh-oh. Failed to open the new log file. Try to clear
 
215
                 * the previous log file, note the lost log entries,
 
216
                 * and keep on truckin'. */
 
217
                if (nLogFDprev == NULL) {
 
218
                    fprintf(stderr, "1 Previous file handle doesn't exists %s\n", buf2);
 
219
                    exit(2);
 
220
                }
 
221
                else {
 
222
                    nLogFD = nLogFDprev;
 
223
                    sprintf(errbuf,
 
224
                            "Resetting log file due to error opening "
 
225
                            "new log file. %10d messages lost.\n",
 
226
                            nMessCount);
 
227
                    nWrite = strlen(errbuf);
 
228
                    apr_file_trunc(nLogFD, 0);
 
229
                    if (apr_file_write(nLogFD, errbuf, &nWrite) != APR_SUCCESS) {
 
230
                        fprintf(stderr, "Error writing to the file %s\n", buf2);
 
231
                        exit(2);
 
232
                    }
 
233
                }
 
234
            }
 
235
            else if (nLogFDprev) {
 
236
                apr_file_close(nLogFDprev);
 
237
            }
 
238
            nMessCount = 0;
 
239
        }
 
240
        nWrite = nRead;
 
241
        apr_file_write(nLogFD, buf, &nWrite);
 
242
        if (nWrite != nRead) {
 
243
            nMessCount++;
 
244
            sprintf(errbuf,
 
245
                    "Error writing to log file. "
 
246
                    "%10d messages lost.\n",
 
247
                    nMessCount);
 
248
            nWrite = strlen(errbuf);
 
249
            apr_file_trunc(nLogFD, 0);
 
250
            if (apr_file_write(nLogFD, errbuf, &nWrite) != APR_SUCCESS) {
 
251
                fprintf(stderr, "Error writing to the file %s\n", buf2);
 
252
                exit(2);
 
253
            }
 
254
        }
 
255
        else {
 
256
            nMessCount++;
 
257
        }
 
258
    }
 
259
    /* Of course we never, but prevent compiler warnings */
 
260
    return 0;
 
261
}