]> granicus.if.org Git - apache/blob - support/rotatelogs.c
Too many args is a bad thing too, some days
[apache] / support / rotatelogs.c
1 /* ====================================================================
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2000-2001 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, char *argv[])
95 {
96     char buf[BUFSIZE], buf2[MAX_PATH], errbuf[ERRMSGSZ];
97     int tLogEnd = 0, tRotation, utc_offset = 0;
98     int nMessCount = 0;
99     apr_size_t nRead, nWrite;
100     int use_strftime = 0;
101     int now;
102     char *szLogRoot;
103     apr_file_t *f_stdin, *nLogFD = NULL, *nLogFDprev = NULL;
104     apr_pool_t *pool;
105
106     apr_initialize();
107     atexit(apr_terminate);
108
109     apr_pool_create(&pool, NULL);
110     if (argc < 3 || argc > 4) {
111         fprintf(stderr,
112                 "Usage: %s <logfile> <rotation time in seconds> "
113                 "[offset minutes from UTC]\n\n",
114                 argv[0]);
115 #ifdef OS2
116         fprintf(stderr,
117                 "Add this:\n\nTransferLog \"|%s.exe /some/where 86400\"\n\n",
118                 argv[0]);
119 #else
120         fprintf(stderr,
121                 "Add this:\n\nTransferLog \"|%s /some/where 86400\"\n\n",
122                 argv[0]);
123 #endif
124         fprintf(stderr,
125                 "to httpd.conf. The generated name will be /some/where.nnnn "
126                 "where nnnn is the\nsystem time at which the log nominally "
127                 "starts (N.B. this time will always be a\nmultiple of the "
128                 "rotation time, so you can synchronize cron scripts with it).\n"
129                 "At the end of each rotation time a new log is started.\n");
130         exit(1);
131     }
132
133     szLogRoot = argv[1];
134     if (argc >= 4) {
135         utc_offset = atoi(argv[3]) * 60;
136     }
137     tRotation = atoi(argv[2]);
138     if (tRotation <= 0) {
139         fprintf(stderr, "Rotation time must be > 0\n");
140         exit(6);
141     }
142
143     use_strftime = (strchr(szLogRoot, '%') != NULL);
144     if (apr_file_open_stdin(&f_stdin, pool) != APR_SUCCESS) {
145         fprintf(stderr, "Unable to open stdin\n");
146         exit(1);
147     }
148
149     for (;;) {
150         nRead = sizeof(buf);
151         if (apr_file_read(f_stdin, buf, &nRead) != APR_SUCCESS)
152             exit(3);
153         now = (int)(apr_time_now() / APR_USEC_PER_SEC) + utc_offset;
154         if (nRead == 0)
155             exit(3);
156         if (nLogFD != NULL && (now >= tLogEnd || nRead < 0)) {
157             nLogFDprev = nLogFD;
158             nLogFD = NULL;
159         }
160         if (nLogFD == NULL) {
161             int tLogStart = (now / tRotation) * tRotation;
162             if (use_strftime) {
163                 apr_time_t tNow = tLogStart * APR_USEC_PER_SEC;
164                 apr_exploded_time_t e;
165                 apr_size_t rs;
166
167                 apr_explode_gmt(&e, tNow);
168                 apr_strftime(buf2, &rs, sizeof(buf2), szLogRoot, &e);
169             }
170             else {
171                 sprintf(buf2, "%s.%010d", szLogRoot, tLogStart);
172             }
173             tLogEnd = tLogStart + tRotation;
174             apr_file_open(&nLogFD, buf2, APR_READ | APR_WRITE | APR_CREATE | APR_APPEND,
175                           APR_OS_DEFAULT, pool);
176             if (nLogFD == NULL) {
177                 /* Uh-oh. Failed to open the new log file. Try to clear
178                  * the previous log file, note the lost log entries,
179                  * and keep on truckin'. */
180                 if (nLogFDprev == NULL) {
181                     fprintf(stderr, "1 Previous file handle doesn't exists %s\n", buf2);
182                     exit(2);
183                 }
184                 else {
185                     nLogFD = nLogFDprev;
186                     sprintf(errbuf,
187                             "Resetting log file due to error opening "
188                             "new log file. %10d messages lost.\n",
189                             nMessCount);
190                     nWrite = strlen(errbuf);
191                     apr_file_trunc(nLogFD, 0);
192                     if (apr_file_write(nLogFD, errbuf, &nWrite) != APR_SUCCESS) {
193                         fprintf(stderr, "Error witing to the file %s\n", buf2);
194                         exit(2);
195                     }
196                 }
197             }
198             else if (nLogFDprev) {
199                 apr_file_close(nLogFDprev);
200             }
201             nMessCount = 0;
202         }
203         do {
204             nWrite = nRead;
205             apr_file_write(nLogFD, buf, &nWrite);
206         } while (nWrite < 0 && errno == EINTR);
207         if (nWrite != nRead) {
208             nMessCount++;
209             sprintf(errbuf,
210                     "Error writing to log file. "
211                     "%10d messages lost.\n",
212                     nMessCount);
213             nWrite = strlen(errbuf);
214             apr_file_trunc(nLogFD, 0);
215             if (apr_file_write(nLogFD, errbuf, &nWrite) != APR_SUCCESS) {
216                 fprintf(stderr, "Error witing to the file %s\n", buf2);
217                 exit(2);
218             }
219         }
220         else {
221             nMessCount++;
222         }
223     }
224     /* Of course we never, but prevent compiler warnings */
225     return 0;
226 }