]> granicus.if.org Git - apache/blob - support/rotatelogs.c
describe the recent changes to mod_headers (%%, envclause everywhere)
[apache] / support / rotatelogs.c
1 /* Copyright 1999-2004 The Apache Software Foundation
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 /*
17  * Simple program to rotate Apache logs without having to kill the server.
18  *
19  * Contributed by Ben Laurie <ben algroup.co.uk>
20  *
21  * 12 Mar 1996
22  *
23  * Ported to APR by Mladen Turk <mturk mappingsoft.com>
24  *
25  * 23 Sep 2001
26  */
27
28
29 #include "apr.h"
30 #include "apr_lib.h"
31 #include "apr_strings.h"
32 #include "apr_errno.h"
33 #include "apr_file_io.h"
34 #include "apr_file_info.h"
35 #include "apr_general.h"
36 #include "apr_time.h"
37
38 #if APR_HAVE_STDLIB_H
39 #include <stdlib.h>
40 #endif
41 #if APR_HAVE_STRING_H
42 #include <string.h>
43 #endif
44 #if APR_HAVE_STRINGS_H
45 #include <strings.h>
46 #endif
47
48 #define BUFSIZE         65536
49 #define ERRMSGSZ        82
50
51 #ifndef MAX_PATH
52 #define MAX_PATH        1024
53 #endif
54
55 int main (int argc, const char * const argv[])
56 {
57     char buf[BUFSIZE], buf2[MAX_PATH], errbuf[ERRMSGSZ];
58     int tLogEnd = 0, tRotation = 0, utc_offset = 0;
59     unsigned int sRotation = 0;
60     int nMessCount = 0;
61     apr_size_t nRead, nWrite;
62     int use_strftime = 0;
63     int now = 0;
64     const char *szLogRoot;
65     apr_file_t *f_stdin, *nLogFD = NULL, *nLogFDprev = NULL;
66     apr_pool_t *pool;
67     char *ptr = NULL;
68
69     apr_app_initialize(&argc, &argv, NULL);
70     atexit(apr_terminate);
71
72     apr_pool_create(&pool, NULL);
73     if (argc < 3 || argc > 4) {
74         fprintf(stderr,
75                 "Usage: %s <logfile> <rotation time in seconds> "
76                 "[offset minutes from UTC] or <rotation size in megabytes>\n\n",
77                 argv[0]);
78 #ifdef OS2
79         fprintf(stderr,
80                 "Add this:\n\nTransferLog \"|%s.exe /some/where 86400\"\n\n",
81                 argv[0]);
82 #else
83         fprintf(stderr,
84                 "Add this:\n\nTransferLog \"|%s /some/where 86400\"\n\n",
85                 argv[0]);
86         fprintf(stderr,
87                 "or \n\nTransferLog \"|%s /some/where 5M\"\n\n", argv[0]);
88 #endif
89         fprintf(stderr,
90                 "to httpd.conf. The generated name will be /some/where.nnnn "
91                 "where nnnn is the\nsystem time at which the log nominally "
92                 "starts (N.B. if using a rotation time,\nthe time will always "
93                 "be a multiple of the rotation time, so you can synchronize\n"
94                 "cron scripts with it). At the end of each rotation time or "
95                 "when the file size\nis reached a new log is started.\n");
96         exit(1);
97     }
98
99     szLogRoot = argv[1];
100
101     ptr = strchr (argv[2], 'M');
102     if (ptr) {
103         if (*(ptr+1) == '\0') {
104             sRotation = atoi(argv[2]) * 1048576;
105         }
106         if (sRotation == 0) {
107             fprintf(stderr, "Invalid rotation size parameter\n");
108             exit(1);
109         }
110     }
111     else {
112         if (argc >= 4) {
113             utc_offset = atoi(argv[3]) * 60;
114         }
115         tRotation = atoi(argv[2]);
116         if (tRotation <= 0) {
117             fprintf(stderr, "Rotation time must be > 0\n");
118             exit(6);
119         }
120     }
121
122     use_strftime = (strchr(szLogRoot, '%') != NULL);
123     if (apr_file_open_stdin(&f_stdin, pool) != APR_SUCCESS) {
124         fprintf(stderr, "Unable to open stdin\n");
125         exit(1);
126     }
127
128     for (;;) {
129         nRead = sizeof(buf);
130         if (apr_file_read(f_stdin, buf, &nRead) != APR_SUCCESS)
131             exit(3);
132         if (tRotation) {
133             now = (int)(apr_time_now() / APR_USEC_PER_SEC) + utc_offset;
134             if (nLogFD != NULL && now >= tLogEnd) {
135                 nLogFDprev = nLogFD;
136                 nLogFD = NULL;
137             }
138         }
139         else if (sRotation) {
140             apr_finfo_t finfo;
141             apr_off_t current_size = -1;
142
143             if ((nLogFD != NULL) && 
144                 (apr_file_info_get(&finfo, APR_FINFO_SIZE, nLogFD) == APR_SUCCESS)) {
145                 current_size = finfo.size;
146             }
147
148             if (current_size > sRotation) {
149                 nLogFDprev = nLogFD;
150                 nLogFD = NULL;
151             }
152         }
153         else {
154             fprintf(stderr, "No rotation time or size specified\n");
155             exit(2);
156         }
157
158         if (nLogFD == NULL) {
159             int tLogStart;
160                 
161             if (tRotation)
162                 tLogStart = (now / tRotation) * tRotation;
163             else
164                 tLogStart = (int)apr_time_sec(apr_time_now());
165
166             if (use_strftime) {
167                 apr_time_t tNow = apr_time_from_sec(tLogStart);
168                 apr_time_exp_t e;
169                 apr_size_t rs;
170
171                 apr_time_exp_gmt(&e, tNow);
172                 apr_strftime(buf2, &rs, sizeof(buf2), szLogRoot, &e);
173             }
174             else {
175                 sprintf(buf2, "%s.%010d", szLogRoot, tLogStart);
176             }
177             tLogEnd = tLogStart + tRotation;
178             apr_file_open(&nLogFD, buf2, APR_READ | APR_WRITE | APR_CREATE | APR_APPEND,
179                           APR_OS_DEFAULT, pool);
180             if (nLogFD == NULL) {
181                 /* Uh-oh. Failed to open the new log file. Try to clear
182                  * the previous log file, note the lost log entries,
183                  * and keep on truckin'. */
184                 if (nLogFDprev == NULL) {
185                     fprintf(stderr, "1 Previous file handle doesn't exists %s\n", buf2);
186                     exit(2);
187                 }
188                 else {
189                     nLogFD = nLogFDprev;
190                     sprintf(errbuf,
191                             "Resetting log file due to error opening "
192                             "new log file. %10d messages lost.\n",
193                             nMessCount);
194                     nWrite = strlen(errbuf);
195                     apr_file_trunc(nLogFD, 0);
196                     if (apr_file_write(nLogFD, errbuf, &nWrite) != APR_SUCCESS) {
197                         fprintf(stderr, "Error writing to the file %s\n", buf2);
198                         exit(2);
199                     }
200                 }
201             }
202             else if (nLogFDprev) {
203                 apr_file_close(nLogFDprev);
204             }
205             nMessCount = 0;
206         }
207         nWrite = nRead;
208         apr_file_write(nLogFD, buf, &nWrite);
209         if (nWrite != nRead) {
210             nMessCount++;
211             sprintf(errbuf,
212                     "Error writing to log file. "
213                     "%10d messages lost.\n",
214                     nMessCount);
215             nWrite = strlen(errbuf);
216             apr_file_trunc(nLogFD, 0);
217             if (apr_file_write(nLogFD, errbuf, &nWrite) != APR_SUCCESS) {
218                 fprintf(stderr, "Error writing to the file %s\n", buf2);
219                 exit(2);
220             }
221         }
222         else {
223             nMessCount++;
224         }
225     }
226     /* Of course we never, but prevent compiler warnings */
227     return 0;
228 }