Debian Codegraph @

src/cronolog.c

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /* ====================================================================
2 * Copyright (c) 1995-1999 The Apache Group. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
14 * distribution.
15 *
16 * 3. All advertising materials mentioning features or use of this
17 * software must display the following acknowledgment:
18 * "This product includes software developed by the Apache Group
19 * for use in the Apache HTTP server project (http://www.apache.org/)."
20 *
21 * 4. The names "Apache Server" and "Apache Group" must not be used to
22 * endorse or promote products derived from this software without
23 * prior written permission. For written permission, please contact
24 * [email protected].
25 *
26 * 5. Products derived from this software may not be called "Apache"
27 * nor may "Apache" appear in their names without prior written
28 * permission of the Apache Group.
29 *
30 * 6. Redistributions of any form whatsoever must retain the following
31 * acknowledgment:
32 * "This product includes software developed by the Apache Group
33 * for use in the Apache HTTP server project (http://www.apache.org/)."
34 *
35 * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
36 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE GROUP OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46 * OF THE POSSIBILITY OF SUCH DAMAGE.
47 * ====================================================================
48 *
49 * This software consists of voluntary contributions made by many
50 * individuals on behalf of the Apache Group and was originally based
51 * on public domain software written at the National Center for
52 * Supercomputing Applications, University of Illinois, Urbana-Champaign.
53 * For more information on the Apache Group and the Apache HTTP server
54 * project, please see <http://www.apache.org/>.
55 *
56 */
57 /*
58 * cronolog -- simple Apache log rotation program
59 *
60 * Copyright (c) 1996-1999 by Ford & Mason Ltd
61 *
62 * This software was submitted by Ford & Mason Ltd to the Apache
63 * Software Foundation in December 1999. Future revisions and
64 * derivatives of this source code must acknowledge Ford & Mason Ltd
65 * as the original contributor of this module. All other licensing
66 * and usage conditions are those of the Apache Software Foundation.
67 *
68 * Originally written by Andrew Ford <[email protected]>
69 *
70 * cronolog is loosly based on the rotatelogs program, which is part of the
71 * Apache package written by Ben Laurie <[email protected]>
72 *
73 * The argument to this program is the log file name template as an
74 * strftime format string. For example to generate new error and
75 * access logs each day stored in subdirectories by year, month and day add
76 * the following lines to the httpd.conf:
77 *
78 * TransferLog "|/www/etc/cronolog /www/logs/%Y/%m/%d/access.log"
79 * ErrorLog "|/www/etc/cronolog /www/logs/%Y/%m/%d/error.log"
80 *
81 * The option "-x file" specifies that debugging messages should be
82 * written to "file" (e.g. /dev/console) or to stderr if "file" is "-".
83 */
84
85 #include "cronoutils.h"
86 #include "getopt.h"
87
88
89 /* Forward function declaration */
90
91 int new_log_file(const char *, const char *, mode_t, const char *,
92 PERIODICITY, int, int, char *, size_t, time_t, time_t *);
93
94
95 /* Definition of version and usage messages */
96
97 #ifndef _WIN32
98 #define VERSION_MSG PACKAGE " version " VERSION "\n"
99 #else
100 #define VERSION_MSG "cronolog version 0.1\n"
101 #endif
102
103
104 #define USAGE_MSG "usage: %s [OPTIONS] logfile-spec\n" \
105 "\n" \
106 " -H NAME, --hardlink=NAME maintain a hard link from NAME to current log\n" \
107 " -S NAME, --symlink=NAME maintain a symbolic link from NAME to current log\n" \
108 " -P NAME, --prev-symlink=NAME maintain a symbolic link from NAME to previous log\n" \
109 " -l NAME, --link=NAME same as -S/--symlink\n" \
110 " -h, --help print this help, then exit\n" \
111 " -p PERIOD, --period=PERIOD set the rotation period explicitly\n" \
112 " -d DELAY, --delay=DELAY set the rotation period delay\n" \
113 " -o, --once-only create single output log from template (not rotated)\n" \
114 " -x FILE, --debug=FILE write debug messages to FILE\n" \
115 " ( or to standard error if FILE is \"-\")\n" \
116 " -a, --american American date formats\n" \
117 " -e, --european European date formats (default)\n" \
118 " -s, --start-time=TIME starting time\n" \
119 " -z TZ, --time-zone=TZ use TZ for timezone\n" \
120 " -V, --version print version number, then exit\n"
121
122
123 /* Definition of the short and long program options */
124
125 char *short_options = "ad:eop:s:z:H:P:S:l:hVx:";
126
127 #ifndef _WIN32
128 struct option long_options[] =
129 {
130 { "american", no_argument, NULL, 'a' },
131 { "european", no_argument, NULL, 'e' },
132 { "start-time", required_argument, NULL, 's' },
133 { "time-zone", required_argument, NULL, 'z' },
134 { "hardlink", required_argument, NULL, 'H' },
135 { "symlink", required_argument, NULL, 'S' },
136 { "prev-symlink", required_argument, NULL, 'P' },
137 { "link", required_argument, NULL, 'l' },
138 { "period", required_argument, NULL, 'p' },
139 { "delay", required_argument, NULL, 'd' },
140 { "once-only", no_argument, NULL, 'o' },
141 { "help", no_argument, NULL, 'h' },
142 { "version", no_argument, NULL, 'V' }
143 };
144 #endif
145
146 /* Main function.
147 */
148 int
149 main(int argc, char **argv)
150 {
151 PERIODICITY periodicity = UNKNOWN;
152 PERIODICITY period_delay_units = UNKNOWN;
153 int period_multiple = 1;
154 int period_delay = 0;
155 int use_american_date_formats = 0;
156 char read_buf[BUFSIZE];
157 char tzbuf[BUFSIZE];
158 char filename[MAX_PATH];
159 char *start_time = NULL;
160 char *template;
161 char *linkname = NULL;
162 char *prevlinkname = NULL;
163 mode_t linktype = 0;
164 int n_bytes_read;
165 int ch;
166 time_t time_now;
167 time_t time_offset = 0;
168 time_t next_period = 0;
169 int log_fd = -1;
170
171 #ifndef _WIN32
172 while ((ch = getopt_long(argc, argv, short_options, long_options, NULL)) != EOF)
173 #else
174 while ((ch = getopt(argc, argv, short_options)) != EOF)
175 #endif
176 {
177 switch (ch)
178 {
179 case 'a':
180 use_american_date_formats = 1;
181 break;
182
183 case 'e':
184 use_american_date_formats = 0;
185 break;
186
187 case 's':
188 start_time = optarg;
189 break;
190
191 case 'z':
192 sprintf(tzbuf, "TZ=%s", optarg);
193 putenv(tzbuf);
194 break;
195
196 case 'H':
197 linkname = optarg;
198 linktype = S_IFREG;
199 break;
200
201 case 'l':
202 case 'S':
203 linkname = optarg;
204 #ifndef _WIN32
205 linktype = S_IFLNK;
206 #endif
207 break;
208
209 case 'P':
210 if (linkname == NULL)
211 {
212 fprintf(stderr, "A current log symlink is needed to mantain a symlink to the previous log\n", argv[0]);
213 exit(1);
214 }
215 prevlinkname = optarg;
216 break;
217
218
219 case 'd':
220 period_delay_units = parse_timespec(optarg, &period_delay);
221 break;
222
223 case 'p':
224 periodicity = parse_timespec(optarg, &period_multiple);
225 if ( (periodicity == INVALID_PERIOD)
226 || (periodicity == PER_SECOND) && (60 % period_multiple)
227 || (periodicity == PER_MINUTE) && (60 % period_multiple)
228 || (periodicity == HOURLY) && (24 % period_multiple)
229 || (periodicity == DAILY) && (period_multiple > 365)
230 || (periodicity == WEEKLY) && (period_multiple > 52)
231 || (periodicity == MONTHLY) && (12 % period_multiple)) {
232 fprintf(stderr, "%s: invalid explicit period specification (%s)\n", argv[0], start_time);
233 exit(1);
234 }
235 break;
236
237 case 'o':
238 periodicity = ONCE_ONLY;
239 break;
240
241 case 'x':
242 if (strcmp(optarg, "-") == 0)
243 {
244 debug_file = stderr;
245 }
246 else
247 {
248 debug_file = fopen(optarg, "a+");
249 }
250 break;
251
252 case 'V':
253 fprintf(stderr, VERSION_MSG);
254 exit(0);
255
256 case 'h':
257 case '?':
258 fprintf(stderr, USAGE_MSG, argv[0]);
259 exit(1);
260 }
261 }
262
263 if ((argc - optind) != 1)
264 {
265 fprintf(stderr, USAGE_MSG, argv[0]);
266 exit(1);
267 }
268
269 DEBUG((VERSION_MSG "\n"));
270
271 if (start_time)
272 {
273 time_now = parse_time(start_time, use_american_date_formats);
274 if (time_now == -1)
275 {
276 fprintf(stderr, "%s: invalid start time (%s)\n", argv[0], start_time);
277 exit(1);
278 }
279 time_offset = time_now - time(NULL);
280 DEBUG(("Using offset of %d seconds from real time\n", time_offset));
281 }
282
283 /* The template should be the only argument.
284 * Unless the -o option was specified, determine the periodicity.
285 */
286
287 template = argv[optind];
288 if (periodicity == UNKNOWN)
289 {
290 periodicity = determine_periodicity(template);
291 }
292
293
294 DEBUG(("periodicity = %d %s\n", period_multiple, periods[periodicity]));
295
296 if (period_delay) {
297 if ( (period_delay_units > periodicity)
298 || ( period_delay_units == periodicity
299 && abs(period_delay) >= period_multiple)) {
300 fprintf(stderr, "%s: period delay cannot be larger than the rollover period\n", argv[0], start_time);
301 exit(1);
302 }
303 period_delay *= period_seconds[period_delay_units];
304 }
305
306 DEBUG(("Rotation period is per %d %s\n", period_multiple, periods[periodicity]));
307
308
309 /* Loop, waiting for data on standard input */
310
311 for (;;)
312 {
313 /* Read a buffer's worth of log file data, exiting on errors
314 * or end of file.
315 */
316 n_bytes_read = read(0, read_buf, sizeof read_buf);
317 if (n_bytes_read == 0)
318 {
319 exit(3);
320 }
321 if (errno == EINTR)
322 {
323 continue;
324 }
325 else if (n_bytes_read < 0)
326 {
327 exit(4);
328 }
329
330 time_now = time(NULL) + time_offset;
331
332 /* If the current period has finished and there is a log file
333 * open, close the log file
334 */
335 if ((time_now >= next_period) && (log_fd >= 0))
336 {
337 close(log_fd);
338 log_fd = -1;
339 }
340
341 /* If there is no log file open then open a new one.
342 */
343 if (log_fd < 0)
344 {
345 log_fd = new_log_file(template, linkname, linktype, prevlinkname,
346 periodicity, period_multiple, period_delay,
347 filename, sizeof (filename), time_now, &next_period);
348 }
349
350 DEBUG(("%s (%d): wrote message; next period starts at %s (%d) in %d secs\n",
351 timestamp(time_now), time_now,
352 timestamp(next_period), next_period,
353 next_period - time_now));
354
355 /* Write out the log data to the current log file.
356 */
357 if (write(log_fd, read_buf, n_bytes_read) != n_bytes_read)
358 {
359 perror(filename);
360 exit(5);
361 }
362 }
363
364 /* NOTREACHED */
365 return 1;
366 }
367
368 /* Open a new log file: determine the start of the current
369 * period, generate the log file name from the template,
370 * determine the end of the period and open the new log file.
371 *
372 * Returns the file descriptor of the new log file and also sets the
373 * name of the file and the start time of the next period via pointers
374 * supplied.
375 */
376 int
377 new_log_file(const char *template, const char *linkname, mode_t linktype, const char *prevlinkname,
378 PERIODICITY periodicity, int period_multiple, int period_delay,
379 char *pfilename, size_t pfilename_len,
380 time_t time_now, time_t *pnext_period)
381 {
382 time_t start_of_period;
383 struct tm *tm;
384 int log_fd;
385
386 start_of_period = start_of_this_period(time_now, periodicity, period_multiple);
387 tm = localtime(&start_of_period);
388 strftime(pfilename, BUFSIZE, template, tm);
389 *pnext_period = start_of_next_period(start_of_period, periodicity, period_multiple) + period_delay;
390
391 DEBUG(("%s (%d): using log file \"%s\" from %s (%d) until %s (%d) (for %d secs)\n",
392 timestamp(time_now), time_now, pfilename,
393 timestamp(start_of_period), start_of_period,
394 timestamp(*pnext_period), *pnext_period,
395 *pnext_period - time_now));
396
397 log_fd = open(pfilename, O_WRONLY|O_CREAT|O_APPEND, FILE_MODE);
398
399 #ifndef DONT_CREATE_SUBDIRS
400 if ((log_fd < 0) && (errno == ENOENT))
401 {
402 create_subdirs(pfilename);
403 log_fd = open(pfilename, O_WRONLY|O_CREAT|O_APPEND, FILE_MODE);
404 }
405 #endif
406
407 if (log_fd < 0)
408 {
409 perror(pfilename);
410 exit(2);
411 }
412
413 if (linkname)
414 {
415 create_link(pfilename, linkname, linktype, prevlinkname);
416 }
417 return log_fd;
418 }