2 * ntp_filegen.c,v 3.12 1994/01/25 19:06:11 kardel Exp
4 * implements file generations support for NTP
5 * logfiles and statistic files
8 * Copyright (C) 1992, 1996 by Rainer Pruy
9 * Friedrich-Alexander Universität Erlangen-Nürnberg, Germany
11 * This code may be modified and used freely
12 * provided credits remain intact.
20 #include <sys/types.h>
25 #include "ntp_string.h"
26 #include "ntp_calendar.h"
27 #include "ntp_filegen.h"
28 #include "ntp_stdlib.h"
31 * NTP is intended to run long periods of time without restart.
32 * Thus log and statistic files generated by NTP will grow large.
34 * this set of routines provides a central interface
35 * to generating files using file generations
37 * the generation of a file is changed according to file generation type
42 * redefine this if your system dislikes filename suffixes like
43 * X.19910101 or X.1992W50 or ....
45 #define SUFFIX_SEP '.'
50 #define FGEN_AGE_SECS (24*60*60) /* life time of FILEGEN_AGE in seconds */
52 static void filegen_open P((FILEGEN *, u_long));
53 static int valid_fileref P((char *, char *));
55 static FILEGEN *filegen_unregister P((char *));
58 static void filegen_init P((char *, const char *, FILEGEN *));
65 filegen_init(char *prefix, const char *basename, FILEGEN *fp)
68 fp->prefix = prefix; /* Yes, this is TOTALLY lame! */
69 fp->basename = (char*)emalloc(strlen(basename) + 1);
70 strcpy(fp->basename, basename);
72 fp->type = FILEGEN_DAY;
73 fp->flag = FGEN_FLAG_LINK; /* not yet enabled !!*/
78 * open a file generation according to the current settings of gen
79 * will also provide a link to basename if requested to do so
94 len = strlen(gen->prefix) + strlen(gen->basename) + 1;
95 basename = (char*)emalloc(len);
96 sprintf(basename, "%s%s", gen->prefix, gen->basename);
100 msyslog(LOG_ERR, "unsupported file generations type %d for \"%s\" - reverting to FILEGEN_NONE",
101 gen->type, basename);
102 gen->type = FILEGEN_NONE;
106 filename = (char*)emalloc(len);
107 sprintf(filename,"%s", basename);
111 filename = (char*)emalloc(len + 1 + 1 + 10);
112 sprintf(filename,"%s%c#%ld", basename, SUFFIX_SEP, newid);
116 /* You can argue here in favor of using MJD, but
117 * I would assume it to be easier for humans to interpret dates
118 * in a format they are used to in everyday life.
120 caljulian(newid,&cal);
121 filename = (char*)emalloc(len + 1 + 4 + 2 + 2);
122 sprintf(filename, "%s%c%04d%02d%02d",
123 basename, SUFFIX_SEP, cal.year, cal.month, cal.monthday);
128 * This is still a hack
129 * - the term week is not correlated to week as it is used
130 * normally - it just refers to a period of 7 days
131 * starting at Jan 1 - 'weeks' are counted starting from zero
133 caljulian(newid,&cal);
134 filename = (char*)emalloc(len + 1 + 4 + 1 + 2);
135 sprintf(filename, "%s%c%04dw%02d",
136 basename, SUFFIX_SEP, cal.year, cal.yearday / 7);
140 caljulian(newid,&cal);
141 filename = (char*)emalloc(len + 1 + 4 + 2);
142 sprintf(filename, "%s%c%04d%02d",
143 basename, SUFFIX_SEP, cal.year, cal.month);
147 caljulian(newid,&cal);
148 filename = (char*)emalloc(len + 1 + 4);
149 sprintf(filename, "%s%c%04d", basename, SUFFIX_SEP, cal.year);
153 filename = (char*)emalloc(len + 1 + 2 + 10);
154 sprintf(filename, "%s%ca%08ld", basename, SUFFIX_SEP, newid);
158 if (gen->type != FILEGEN_NONE) {
160 * check for existence of a file with name 'basename'
161 * as we disallow such a file
162 * if FGEN_FLAG_LINK is set create a link
166 * try to resolve name collisions
168 static u_long conflicts = 0;
171 #define S_ISREG(mode) (((mode) & S_IFREG) == S_IFREG)
173 if (stat(basename, &stats) == 0) {
174 /* Hm, file exists... */
175 if (S_ISREG(stats.st_mode)) {
176 if (stats.st_nlink <= 1) {
178 * Oh, it is not linked - try to save it
180 char *savename = (char*)emalloc(len + 1 + 1 + 10 + 10);
181 sprintf(savename, "%s%c%dC%lu",
185 (u_long)conflicts++);
186 if (rename(basename, savename) != 0)
187 msyslog(LOG_ERR," couldn't save %s: %m", basename);
191 * there is at least a second link to
193 * just remove the conflicting one
197 unlink(basename) != 0
199 delete(basename) != 0
202 msyslog(LOG_ERR, "couldn't unlink %s: %m", basename);
206 * Ehh? Not a regular file ?? strange !!!!
208 msyslog(LOG_ERR, "expected regular file for %s (found mode 0%lo)",
209 basename, (unsigned long)stats.st_mode);
213 * stat(..) failed, but it is absolutely correct for
214 * 'basename' not to exist
217 msyslog(LOG_ERR,"stat(%s) failed: %m", basename);
222 * now, try to open new file generation...
224 fp = fopen(filename, "a");
228 printf("opening filegen (type=%d/id=%lu) \"%s\"\n",
229 gen->type, (u_long)newid, filename);
233 /* open failed -- keep previous state
235 * If the file was open before keep the previous generation.
236 * This will cause output to end up in the 'wrong' file,
237 * but I think this is still better than losing output
239 * ignore errors due to missing directories
243 msyslog(LOG_ERR, "can't open %s: %m", filename);
245 if (gen->fp != NULL) {
251 if (gen->flag & FGEN_FLAG_LINK) {
253 * need to link file to basename
254 * have to use hardlink for now as I want to allow
255 * gen->basename spanning directory levels
256 * this would make it more complex to get the correct
257 * filename for symlink
259 * Ok, it would just mean taking the part following
260 * the last '/' in the name.... Should add it later....
263 /* Windows NT does not support file links -Greg Schueman 1/18/97 */
265 #if defined SYS_WINNT || defined SYS_VXWORKS
266 SetLastError(0); /* On WinNT, don't support FGEN_FLAG_LINK */
268 errno = 0; /* On VMS, don't support FGEN_FLAG_LINK */
269 #else /* not (VMS) / VXWORKS / WINNT ; DO THE LINK) */
270 if (link(filename, basename) != 0)
272 msyslog(LOG_ERR, "can't link(%s, %s): %m", filename, basename);
273 #endif /* SYS_WINNT || VXWORKS */
274 } /* flags & FGEN_FLAG_LINK */
275 } /* else fp == NULL */
283 * this function sets up gen->fp to point to the correct
284 * generation of the file for the time specified by 'now'
286 * 'now' usually is interpreted as second part of a l_fp as is in the cal...
296 u_long new_gen = ~ (u_long) 0;
299 if (!(gen->flag & FGEN_FLAG_ENABLED)) {
307 if (gen->fp != NULL) return; /* file already open */
315 caljulian(now, &cal);
316 cal.hour = cal.minute = cal.second = 0;
317 new_gen = caltontp(&cal);
321 /* Would be nice to have a calweekstart() routine */
322 /* so just use a hack ... */
323 /* just round time to integral 7 day period for actual year */
324 new_gen = now - (now - calyearstart(now)) % TIMES7(SECSPERDAY)
328 * the computation above would fail in the presence of leap seconds
329 * so at least carry the date to the next day (+60 (seconds))
330 * and go back to the start of the day via calendar computations
332 caljulian(new_gen, &cal);
333 cal.hour = cal.minute = cal.second = 0;
334 new_gen = caltontp(&cal);
338 caljulian(now, &cal);
339 cal.yearday = (u_short) (cal.yearday - cal.monthday + 1);
341 cal.hour = cal.minute = cal.second = 0;
342 new_gen = caltontp(&cal);
346 new_gen = calyearstart(now);
350 new_gen = current_time - (current_time % FGEN_AGE_SECS);
354 * try to open file if not yet open
355 * reopen new file generation file on change of generation id
357 if (gen->fp == NULL || gen->id != new_gen) {
360 printf("filegen %0x %lu %lu %lu\n", gen->type, now,
363 filegen_open(gen, new_gen);
369 * change settings for filegen files
380 * if nothing would be changed...
382 if ((basename == gen->basename || strcmp(basename,gen->basename) == 0) &&
388 * validate parameters
390 if (!valid_fileref(gen->prefix,basename))
398 printf("configuring filegen:\n\tprefix:\t%s\n\tbasename:\t%s -> %s\n\ttype:\t%d -> %d\n\tflag: %x -> %x\n",
399 gen->prefix, gen->basename, basename, gen->type, type, gen->flag, flag);
401 if (gen->basename != basename || strcmp(gen->basename, basename) != 0) {
403 gen->basename = (char*)emalloc(strlen(basename) + 1);
404 strcpy(gen->basename, basename);
406 gen->type = (u_char) type;
407 gen->flag = (u_char) flag;
410 * make filegen use the new settings
411 * special action is only required when a generation file
413 * otherwise the new settings will be used anyway at the next open
415 if (gen->fp != NULL) {
419 filegen_setup(gen, now.l_ui);
425 * check whether concatenating prefix and basename
426 * yields a legal filename
435 * prefix cannot be changed dynamically
436 * (within the context of filegen)
437 * so just reject basenames containing '..'
440 * file system parts 'below' prefix may be
441 * specified without infringement of security
443 * restricing prefix to legal values
444 * has to be ensured by other means
445 * (however, it would be possible to perform some checks here...)
447 register char *p = basename;
450 * Just to catch, dumb errors opening up the world...
452 if (prefix == NULL || *prefix == '\0')
455 if (basename == NULL)
458 for (p = basename; p; p = strchr(p, '/')) {
459 if (*p == '.' && *(p+1) == '.' && (*(p+2) == '\0' || *(p+2) == '/'))
471 static struct filegen_entry {
474 struct filegen_entry *next;
475 } *filegen_registry = NULL;
483 struct filegen_entry *f = filegen_registry;
486 if (f->name == name || strcmp(name, f->name) == 0) {
487 #ifdef XXX /* this gives the Alpha compiler fits */
489 printf("filegen_get(\"%s\") = %x\n", name,
498 printf("filegen_get(\"%s\") = NULL\n", name);
510 struct filegen_entry **f = &filegen_registry;
512 #ifdef XXX /* this gives the Alpha compiler fits */
514 printf("filegen_register(\"%s\",%x)\n", name, (u_int)filegen);
517 filegen_init(prefix, name, filegen);
520 if ((*f)->name == name || strcmp(name, (*f)->name) == 0) {
521 #ifdef XXX /* this gives the Alpha compiler fits */
523 printf("replacing filegen %x\n", (u_int)(*f)->filegen);
526 (*f)->filegen = filegen;
532 *f = (struct filegen_entry *) emalloc(sizeof(struct filegen_entry));
535 (*f)->name = (char*)emalloc(strlen(name) + 1);
536 strcpy((*f)->name, name);
537 (*f)->filegen = filegen;
540 printf("adding new filegen\n");
554 struct filegen_entry **f = &filegen_registry;
558 printf("filegen_unregister(\"%s\")\n", name);
562 if (strcmp((*f)->name,name) == 0) {
563 struct filegen_entry *ff = *f;