bottleneck testcase based on rubbos
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / support / rotatelogs.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 /*
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, 
212                           APR_APPEND | APR_WRITE | APR_CREATE | APR_LARGEFILE,
213                           APR_OS_DEFAULT, pool);
214             if (nLogFD == NULL) {
215                 /* Uh-oh. Failed to open the new log file. Try to clear
216                  * the previous log file, note the lost log entries,
217                  * and keep on truckin'. */
218                 if (nLogFDprev == NULL) {
219                     fprintf(stderr, "1 Previous file handle doesn't exists %s\n", buf2);
220                     exit(2);
221                 }
222                 else {
223                     nLogFD = nLogFDprev;
224                     sprintf(errbuf,
225                             "Resetting log file due to error opening "
226                             "new log file. %10d messages lost.\n",
227                             nMessCount);
228                     nWrite = strlen(errbuf);
229                     apr_file_trunc(nLogFD, 0);
230                     if (apr_file_write(nLogFD, errbuf, &nWrite) != APR_SUCCESS) {
231                         fprintf(stderr, "Error writing to the file %s\n", buf2);
232                         exit(2);
233                     }
234                 }
235             }
236             else if (nLogFDprev) {
237                 apr_file_close(nLogFDprev);
238             }
239             nMessCount = 0;
240         }
241         nWrite = nRead;
242         apr_file_write(nLogFD, buf, &nWrite);
243         if (nWrite != nRead) {
244             nMessCount++;
245             sprintf(errbuf,
246                     "Error writing to log file. "
247                     "%10d messages lost.\n",
248                     nMessCount);
249             nWrite = strlen(errbuf);
250             apr_file_trunc(nLogFD, 0);
251             if (apr_file_write(nLogFD, errbuf, &nWrite) != APR_SUCCESS) {
252                 fprintf(stderr, "Error writing to the file %s\n", buf2);
253                 exit(2);
254             }
255         }
256         else {
257             nMessCount++;
258         }
259     }
260     /* Of course we never, but prevent compiler warnings */
261     return 0;
262 }