Debian Codegraph @

src/cronolog.c

raw symbols ◀ ▶ SCIP
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 { NULL, 0, NULL, 0 }
144 };
145 #endif
146
147 /* Main function.
148 */
149 int
150 main(int argc, char **argv)
151 {
152 PERIODICITY periodicity = UNKNOWN;
153 PERIODICITY period_delay_units = UNKNOWN;
154 int period_multiple = 1;
155 int period_delay = 0;
156 int use_american_date_formats = 0;
157 char read_buf[BUFSIZE];
158 char tzbuf[BUFSIZE];
159 char filename[MAX_PATH];
160 char *start_time = NULL;
161 char *template;
162 char *linkname = NULL;
163 char *prevlinkname = NULL;
164 mode_t linktype = 0;
165 int n_bytes_read;
166 int ch;
167 time_t time_now;
168 time_t time_offset = 0;
169 time_t next_period = 0;
170 int log_fd = -1;
171
172 #ifndef _WIN32
173 while ((ch = getopt_long(argc, argv, short_options, long_options, NULL)) != EOF)
174 #else
175 while ((ch = getopt(argc, argv, short_options)) != EOF)
176 #endif
177 {
178 switch (ch)
179 {
180 case 'a':
181 use_american_date_formats = 1;
182 break;
183
184 case 'e':
185 use_american_date_formats = 0;
186 break;
187
188 case 's':
189 start_time = optarg;
190 break;
191
192 case 'z':
193 sprintf(tzbuf, "TZ=%s", optarg);
194 putenv(tzbuf);
195 break;
196
197 case 'H':
198 linkname = optarg;
199 linktype = S_IFREG;
200 break;
201
202 case 'l':
203 case 'S':
204 linkname = optarg;
205 #ifndef _WIN32
206 linktype = S_IFLNK;
207 #endif
208 break;
209
210 case 'P':
211 if (linkname == NULL)
212 {
213 fprintf(stderr, "A current log symlink is needed to mantain a symlink to the previous log\n", argv[0]);
214 exit(1);
215 }
216 prevlinkname = optarg;
217 break;
218
219
220 case 'd':
221 period_delay_units = parse_timespec(optarg, &period_delay);
222 break;
223
224 case 'p':
225 periodicity = parse_timespec(optarg, &period_multiple);
226 if ( (periodicity == INVALID_PERIOD)
227 || (periodicity == PER_SECOND) && (60 % period_multiple)
228 || (periodicity == PER_MINUTE) && (60 % period_multiple)
229 || (periodicity == HOURLY) && (24 % period_multiple)
230 || (periodicity == DAILY) && (period_multiple > 365)
231 || (periodicity == WEEKLY) && (period_multiple > 52)
232 || (periodicity == MONTHLY) && (12 % period_multiple)) {
233 fprintf(stderr, "%s: invalid explicit period specification (%s)\n", argv[0], start_time);
234 exit(1);
235 }
236 break;
237
238 case 'o':
239 periodicity = ONCE_ONLY;
240 break;
241
242 case 'x':
243 if (strcmp(optarg, "-") == 0)
244 {
245 debug_file = stderr;
246 }
247 else
248 {
249 debug_file = fopen(optarg, "a+");
250 }
251 break;
252
253 case 'V':
254 fprintf(stderr, VERSION_MSG);
255 exit(0);
256
257 case 'h':
258 case '?':
259 fprintf(stderr, USAGE_MSG, argv[0]);
260 exit(1);
261 }
262 }
263
264 if ((argc - optind) != 1)
265 {
266 fprintf(stderr, USAGE_MSG, argv[0]);
267 exit(1);
268 }
269
270 DEBUG((VERSION_MSG "\n"));
271
272 if (start_time)
273 {
274 time_now = parse_time(start_time, use_american_date_formats);
275 if (time_now == -1)
276 {
277 fprintf(stderr, "%s: invalid start time (%s)\n", argv[0], start_time);
278 exit(1);
279 }
280 time_offset = time_now - time(NULL);
281 DEBUG(("Using offset of %d seconds from real time\n", time_offset));
282 }
283
284 /* The template should be the only argument.
285 * Unless the -o option was specified, determine the periodicity.
286 */
287
288 template = argv[optind];
289 if (periodicity == UNKNOWN)
290 {
291 periodicity = determine_periodicity(template);
292 }
293
294
295 DEBUG(("periodicity = %d %s\n", period_multiple, periods[periodicity]));
296
297 if (period_delay) {
298 if ( (period_delay_units > periodicity)
299 || ( period_delay_units == periodicity
300 && abs(period_delay) >= period_multiple)) {
301 fprintf(stderr, "%s: period delay cannot be larger than the rollover period\n", argv[0], start_time);
302 exit(1);
303 }
304 period_delay *= period_seconds[period_delay_units];
305 }
306
307 DEBUG(("Rotation period is per %d %s\n", period_multiple, periods[periodicity]));
308
309
310 /* Loop, waiting for data on standard input */
311
312 for (;;)
313 {
314 /* Read a buffer's worth of log file data, exiting on errors
315 * or end of file.
316 */
317 n_bytes_read = read(0, read_buf, sizeof read_buf);
318 if (n_bytes_read == 0)
319 {
320 exit(3);
321 }
322 if (errno == EINTR)
323 {
324 continue;
325 }
326 else if (n_bytes_read < 0)
327 {
328 exit(4);
329 }
330
331 time_now = time(NULL) + time_offset;
332
333 /* If the current period has finished and there is a log file
334 * open, close the log file
335 */
336 if ((time_now >= next_period) && (log_fd >= 0))
337 {
338 close(log_fd);
339 log_fd = -1;
340 }
341
342 /* If there is no log file open then open a new one.
343 */
344 if (log_fd < 0)
345 {
346 log_fd = new_log_file(template, linkname, linktype, prevlinkname,
347 periodicity, period_multiple, period_delay,
348 filename, sizeof (filename), time_now, &next_period);
349 }
350
351 DEBUG(("%s (%d): wrote message; next period starts at %s (%d) in %d secs\n",
352 timestamp(time_now), time_now,
353 timestamp(next_period), next_period,
354 next_period - time_now));
355
356 /* Write out the log data to the current log file.
357 */
358 if (write(log_fd, read_buf, n_bytes_read) != n_bytes_read)
359 {
360 perror(filename);
361 exit(5);
362 }
363 }
364
365 /* NOTREACHED */
366 return 1;
367 }
368
369 /* Open a new log file: determine the start of the current
370 * period, generate the log file name from the template,
371 * determine the end of the period and open the new log file.
372 *
373 * Returns the file descriptor of the new log file and also sets the
374 * name of the file and the start time of the next period via pointers
375 * supplied.
376 */
377 int
378 new_log_file(const char *template, const char *linkname, mode_t linktype, const char *prevlinkname,
379 PERIODICITY periodicity, int period_multiple, int period_delay,
380 char *pfilename, size_t pfilename_len,
381 time_t time_now, time_t *pnext_period)
382 {
383 time_t start_of_period;
384 struct tm *tm;
385 int log_fd;
386
387 start_of_period = start_of_this_period(time_now, periodicity, period_multiple);
388 tm = localtime(&start_of_period);
389 strftime(pfilename, BUFSIZE, template, tm);
390 *pnext_period = start_of_next_period(start_of_period, periodicity, period_multiple) + period_delay;
391
392 DEBUG(("%s (%d): using log file \"%s\" from %s (%d) until %s (%d) (for %d secs)\n",
393 timestamp(time_now), time_now, pfilename,
394 timestamp(start_of_period), start_of_period,
395 timestamp(*pnext_period), *pnext_period,
396 *pnext_period - time_now));
397
398 log_fd = open(pfilename, O_WRONLY|O_CREAT|O_APPEND, FILE_MODE);
399
400 #ifndef DONT_CREATE_SUBDIRS
401 if ((log_fd < 0) && (errno == ENOENT))
402 {
403 create_subdirs(pfilename);
404 log_fd = open(pfilename, O_WRONLY|O_CREAT|O_APPEND, FILE_MODE);
405 }
406 #endif
407
408 if (log_fd < 0)
409 {
410 perror(pfilename);
411 exit(2);
412 }
413
414 if (linkname)
415 {
416 create_link(pfilename, linkname, linktype, prevlinkname);
417 }
418 return log_fd;
419 }