]> granicus.if.org Git - apache/blob - support/rotatelogs.c
50194a725afaa6ad39cf4a9039b255a02759c808
[apache] / support / rotatelogs.c
1 /* ====================================================================
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2000-2002 The Apache Software Foundation.  All rights
5  * reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the
17  *    distribution.
18  *
19  * 3. The end-user documentation included with the redistribution,
20  *    if any, must include the following acknowledgment:
21  *       "This product includes software developed by the
22  *        Apache Software Foundation (http://www.apache.org/)."
23  *    Alternately, this acknowledgment may appear in the software itself,
24  *    if and wherever such third-party acknowledgments normally appear.
25  *
26  * 4. The names "Apache" and "Apache Software Foundation" must
27  *    not be used to endorse or promote products derived from this
28  *    software without prior written permission. For written
29  *    permission, please contact apache@apache.org.
30  *
31  * 5. Products derived from this software may not be called "Apache",
32  *    nor may "Apache" appear in their name, without prior written
33  *    permission of the Apache Software Foundation.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of the Apache Software Foundation.  For more
51  * information on the Apache Software Foundation, please see
52  * <http://www.apache.org/>.
53  */
54
55 /*
56  * Simple program to rotate Apache logs without having to kill the server.
57  *
58  * Contributed by Ben Laurie <ben@algroup.co.uk>
59  *
60  * 12 Mar 1996
61  *
62  * Ported to APR by Mladen Turk <mturk@mappingsoft.com>
63  *
64  * 23 Sep 2001
65  */
66
67
68 #include "apr.h"
69 #include "apr_lib.h"
70 #include "apr_strings.h"
71 #include "apr_errno.h"
72 #include "apr_file_io.h"
73 #include "apr_file_info.h"
74 #include "apr_general.h"
75 #include "apr_time.h"
76
77 #if APR_HAVE_STDLIB_H
78 #include <stdlib.h>
79 #endif
80 #if APR_HAVE_STRING_H
81 #include <string.h>
82 #endif
83 #if APR_HAVE_STRINGS_H
84 #include <strings.h>
85 #endif
86
87 #define BUFSIZE         65536
88 #define ERRMSGSZ        82
89
90 #ifndef MAX_PATH
91 #define MAX_PATH        1024
92 #endif
93
94 int main (int argc, const char * const argv[])
95 {
96     char buf[BUFSIZE], buf2[MAX_PATH], errbuf[ERRMSGSZ];
97     int tLogEnd = 0, tRotation = 0, utc_offset = 0;
98     unsigned int sRotation = 0;
99     int nMessCount = 0;
100     apr_size_t nRead, nWrite;
101     int use_strftime = 0;
102     int now = 0;
103     const char *szLogRoot;
104     apr_file_t *f_stdin, *nLogFD = NULL, *nLogFDprev = NULL;
105     apr_pool_t *pool;
106     char *ptr = NULL;
107
108     apr_app_initialize(&argc, &argv, NULL);
109     atexit(apr_terminate);
110
111     apr_pool_create(&pool, NULL);
112     if (argc < 3 || argc > 4) {
113         fprintf(stderr,
114                 "Usage: %s <logfile> <rotation time in seconds> "
115                 "[offset minutes from UTC] or <rotation size in megabytes>\n\n",
116                 argv[0]);
117 #ifdef OS2
118         fprintf(stderr,
119                 "Add this:\n\nTransferLog \"|%s.exe /some/where 86400\"\n\n",
120                 argv[0]);
121 #else
122         fprintf(stderr,
123                 "Add this:\n\nTransferLog \"|%s /some/where 86400\"\n\n",
124                 argv[0]);
125         fprintf(stderr,
126                 "or \n\nTransferLog \"|%s /some/where 5M\"\n\n", argv[0]);
127 #endif
128         fprintf(stderr,
129                 "to httpd.conf. The generated name will be /some/where.nnnn "
130                 "where nnnn is the\nsystem time at which the log nominally "
131                 "starts (N.B. if using a rotation time,\nthe time will always "
132                 "be a multiple of the rotation time, so you can synchronize\n"
133                 "cron scripts with it). At the end of each rotation time or "
134                 "when the file size\nis reached a new log is started.\n");
135         exit(1);
136     }
137
138     szLogRoot = argv[1];
139
140     ptr = strchr (argv[2], 'M');
141     if (ptr) {
142         if (*(ptr+1) == '\0') {
143             sRotation = atoi(argv[2]) * 1048576;
144         }
145         if (sRotation == 0) {
146             fprintf(stderr, "Invalid rotation size parameter\n");
147             exit(1);
148         }
149     }
150     else {
151         if (argc >= 4) {
152             utc_offset = atoi(argv[3]) * 60;
153         }
154         tRotation = atoi(argv[2]);
155         if (tRotation <= 0) {
156             fprintf(stderr, "Rotation time must be > 0\n");
157             exit(6);
158         }
159     }
160
161     use_strftime = (strchr(szLogRoot, '%') != NULL);
162     if (apr_file_open_stdin(&f_stdin, pool) != APR_SUCCESS) {
163         fprintf(stderr, "Unable to open stdin\n");
164         exit(1);
165     }
166
167     for (;;) {
168         nRead = sizeof(buf);
169         if (apr_file_read(f_stdin, buf, &nRead) != APR_SUCCESS)
170             exit(3);
171         if (tRotation) {
172             now = (int)(apr_time_now() / APR_USEC_PER_SEC) + utc_offset;
173             if (nLogFD != NULL && now >= tLogEnd) {
174                 nLogFDprev = nLogFD;
175                 nLogFD = NULL;
176             }
177         }
178         else if (sRotation) {
179             apr_finfo_t finfo;
180             apr_off_t current_size = -1;
181
182             if ((nLogFD != NULL) && 
183                 (apr_file_info_get(&finfo, APR_FINFO_SIZE, nLogFD) == APR_SUCCESS)) {
184                 current_size = finfo.size;
185             }
186
187             if (current_size > sRotation) {
188                 nLogFDprev = nLogFD;
189                 nLogFD = NULL;
190             }
191         }
192         else {
193             fprintf(stderr, "No rotation time or size specified\n");
194             exit(2);
195         }
196
197         if (nLogFD == NULL) {
198             int tLogStart;
199                 
200             if (tRotation)
201                 tLogStart = (now / tRotation) * tRotation;
202             else
203                 tLogStart = (int)apr_time_sec(apr_time_now());
204
205             if (use_strftime) {
206                 apr_time_t tNow = apr_time_from_sec(tLogStart);
207                 apr_time_exp_t e;
208                 apr_size_t rs;
209
210                 apr_time_exp_gmt(&e, tNow);
211                 apr_strftime(buf2, &rs, sizeof(buf2), szLogRoot, &e);
212             }
213             else {
214                 sprintf(buf2, "%s.%010d", szLogRoot, tLogStart);
215             }
216             tLogEnd = tLogStart + tRotation;
217             apr_file_open(&nLogFD, buf2, APR_READ | APR_WRITE | APR_CREATE | APR_APPEND,
218                           APR_OS_DEFAULT, pool);
219             if (nLogFD == NULL) {
220                 /* Uh-oh. Failed to open the new log file. Try to clear
221                  * the previous log file, note the lost log entries,
222                  * and keep on truckin'. */
223                 if (nLogFDprev == NULL) {
224                     fprintf(stderr, "1 Previous file handle doesn't exists %s\n", buf2);
225                     exit(2);
226                 }
227                 else {
228                     nLogFD = nLogFDprev;
229                     sprintf(errbuf,
230                             "Resetting log file due to error opening "
231                             "new log file. %10d messages lost.\n",
232                             nMessCount);
233                     nWrite = strlen(errbuf);
234                     apr_file_trunc(nLogFD, 0);
235                     if (apr_file_write(nLogFD, errbuf, &nWrite) != APR_SUCCESS) {
236                         fprintf(stderr, "Error writing to the file %s\n", buf2);
237                         exit(2);
238                     }
239                 }
240             }
241             else if (nLogFDprev) {
242                 apr_file_close(nLogFDprev);
243             }
244             nMessCount = 0;
245         }
246         nWrite = nRead;
247         apr_file_write(nLogFD, buf, &nWrite);
248         if (nWrite != nRead) {
249             nMessCount++;
250             sprintf(errbuf,
251                     "Error writing to log file. "
252                     "%10d messages lost.\n",
253                     nMessCount);
254             nWrite = strlen(errbuf);
255             apr_file_trunc(nLogFD, 0);
256             if (apr_file_write(nLogFD, errbuf, &nWrite) != APR_SUCCESS) {
257                 fprintf(stderr, "Error writing to the file %s\n", buf2);
258                 exit(2);
259             }
260         }
261         else {
262             nMessCount++;
263         }
264     }
265     /* Of course we never, but prevent compiler warnings */
266     return 0;
267 }