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 *));
59 * open a file generation according to the current settings of gen
60 * will also provide a link to basename if requested to do so
75 len = strlen(gen->prefix) + strlen(gen->basename) + 1;
76 basename = (char*)emalloc(len);
77 sprintf(basename, "%s%s", gen->prefix, gen->basename);
81 msyslog(LOG_ERR, "unsupported file generations type %d for \"%s\" - reverting to FILEGEN_NONE",
83 gen->type = FILEGEN_NONE;
87 filename = (char*)emalloc(len);
88 sprintf(filename,"%s", basename);
92 filename = (char*)emalloc(len + 1 + 1 + 10);
93 sprintf(filename,"%s%c#%ld", basename, SUFFIX_SEP, newid);
97 /* You can argue here in favor of using MJD, but
98 * I would assume it to be easier for humans to interpret dates
99 * in a format they are used to in everyday life.
101 caljulian(newid,&cal);
102 filename = (char*)emalloc(len + 1 + 4 + 2 + 2);
103 sprintf(filename, "%s%c%04d%02d%02d",
104 basename, SUFFIX_SEP, cal.year, cal.month, cal.monthday);
109 * This is still a hack
110 * - the term week is not correlated to week as it is used
111 * normally - it just refers to a period of 7 days
112 * starting at Jan 1 - 'weeks' are counted starting from zero
114 caljulian(newid,&cal);
115 filename = (char*)emalloc(len + 1 + 4 + 1 + 2);
116 sprintf(filename, "%s%c%04dw%02d",
117 basename, SUFFIX_SEP, cal.year, cal.yearday / 7);
121 caljulian(newid,&cal);
122 filename = (char*)emalloc(len + 1 + 4 + 2);
123 sprintf(filename, "%s%c%04d%02d",
124 basename, SUFFIX_SEP, cal.year, cal.month);
128 caljulian(newid,&cal);
129 filename = (char*)emalloc(len + 1 + 4);
130 sprintf(filename, "%s%c%04d", basename, SUFFIX_SEP, cal.year);
134 filename = (char*)emalloc(len + 1 + 2 + 10);
135 sprintf(filename, "%s%ca%08ld", basename, SUFFIX_SEP, newid);
139 if (gen->type != FILEGEN_NONE) {
141 * check for existence of a file with name 'basename'
142 * as we disallow such a file
143 * if FGEN_FLAG_LINK is set create a link
147 * try to resolve name collisions
149 static u_long conflicts = 0;
152 #define S_ISREG(mode) (((mode) & S_IFREG) == S_IFREG)
154 if (stat(basename, &stats) == 0) {
155 /* Hm, file exists... */
156 if (S_ISREG(stats.st_mode)) {
157 if (stats.st_nlink <= 1) {
159 * Oh, it is not linked - try to save it
161 char *savename = (char*)emalloc(len + 1 + 1 + 10 + 10);
162 sprintf(savename, "%s%c%dC%lu",
166 (u_long)conflicts++);
167 if (rename(basename, savename) != 0)
168 msyslog(LOG_ERR," couldn't save %s: %m", basename);
172 * there is at least a second link tpo this file
173 * just remove the conflicting one
177 unlink(basename) != 0
179 delete(basename) != 0
182 msyslog(LOG_ERR, "couldn't unlink %s: %m", basename);
186 * Ehh? Not a regular file ?? strange !!!!
188 msyslog(LOG_ERR, "expected regular file for %s (found mode 0%lo)",
189 basename, (unsigned long)stats.st_mode);
193 * stat(..) failed, but it is absolutely correct for
194 * 'basename' not to exist
197 msyslog(LOG_ERR,"stat(%s) failed: %m", basename);
202 * now, try to open new file generation...
204 fp = fopen(filename, "a");
208 printf("opening filegen (type=%d/id=%lu) \"%s\"\n",
209 gen->type, (u_long)newid, filename);
213 /* open failed -- keep previous state
215 * If the file was open before keep the previous generation.
216 * This will cause output to end up in the 'wrong' file,
217 * but I think this is still better than loosing output
219 * ignore errors due to missing directories
223 msyslog(LOG_ERR, "can't open %s: %m", filename);
225 if (gen->fp != NULL) {
231 if (gen->flag & FGEN_FLAG_LINK) {
233 * need to link file to basename
234 * have to use hardlink for now as I want to allow
235 * gen->basename spanning directory levels
236 * this would make it more complex to get the correct
237 * filename for symlink
239 * Ok, it would just mean taking the part following
240 * the last '/' in the name.... Should add it later....
243 /* Windows NT does not support file links -Greg Schueman 1/18/97 */
245 #if defined SYS_WINNT || defined SYS_VXWORKS
246 SetLastError(0); /* On WinNT, don't support FGEN_FLAG_LINK */
248 errno = 0; /* On VMS, don't support FGEN_FLAG_LINK */
249 #else /* not (VMS) / VXWORKS / WINNT ; DO THE LINK) */
250 if (link(filename, basename) != 0)
252 msyslog(LOG_ERR, "can't link(%s, %s): %m", filename, basename);
253 #endif /* SYS_WINNT || VXWORKS */
254 } /* flags & FGEN_FLAG_LINK */
255 } /* else fp == NULL */
263 * this function sets up gen->fp to point to the correct
264 * generation of the file for the time specified by 'now'
266 * 'now' usually is interpreted as second part of a l_fp as is in the cal...
276 u_long new_gen = ~ (u_long) 0;
279 if (!(gen->flag & FGEN_FLAG_ENABLED)) {
287 if (gen->fp != NULL) return; /* file already open */
295 caljulian(now, &cal);
296 cal.hour = cal.minute = cal.second = 0;
297 new_gen = caltontp(&cal);
301 /* Would be nice to have a calweekstart() routine */
302 /* so just use a hack ... */
303 /* just round time to integral 7 day period for actual year */
304 new_gen = now - (now - calyearstart(now)) % TIMES7(SECSPERDAY)
308 * the computation above would fail in the presence of leap seconds
309 * so at least carry the date to the next day (+60 (seconds))
310 * and go back to the start of the day via calendar computations
312 caljulian(new_gen, &cal);
313 cal.hour = cal.minute = cal.second = 0;
314 new_gen = caltontp(&cal);
318 caljulian(now, &cal);
319 cal.yearday -= cal.monthday - 1;
321 cal.hour = cal.minute = cal.second = 0;
322 new_gen = caltontp(&cal);
326 new_gen = calyearstart(now);
330 new_gen = current_time - (current_time % FGEN_AGE_SECS);
334 * try to open file if not yet open
335 * reopen new file generation file on change of generation id
337 if (gen->fp == NULL || gen->id != new_gen) {
338 filegen_open(gen, new_gen);
344 * change settings for filegen files
355 * if nothing would be changed...
357 if ((basename == gen->basename || strcmp(basename,gen->basename) == 0) &&
363 * validate parameters
365 if (!valid_fileref(gen->prefix,basename))
373 printf("configuring filegen:\n\tprefix:\t%s\n\tbasename:\t%s -> %s\n\ttype:\t%d -> %d\n\tflag: %x -> %x\n",
374 gen->prefix, gen->basename, basename, gen->type, type, gen->flag, flag);
376 if (gen->basename != basename || strcmp(gen->basename, basename) != 0) {
378 gen->basename = (char*)emalloc(strlen(basename) + 1);
379 strcpy(gen->basename, basename);
385 * make filegen use the new settings
386 * special action is only required when a generation file
388 * otherwise the new settings will be used anyway at the next open
390 if (gen->fp != NULL) {
394 filegen_setup(gen, now.l_ui);
400 * check whether concatenating prefix and basename
401 * yields a legal filename
410 * prefix cannot be changed dynamically
411 * (within the context of filegen)
412 * so just reject basenames containing '..'
415 * file system parts 'below' prefix may be
416 * specified without infringement of security
418 * restricing prefix to legal values
419 * has to be ensured by other means
420 * (however, it would be possible to perform some checks here...)
422 register char *p = basename;
425 * Just to catch, dumb errors opening up the world...
427 if (prefix == NULL || *prefix == '\0')
430 if (basename == NULL)
433 for (p = basename; p; p = strchr(p, '/')) {
434 if (*p == '.' && *(p+1) == '.' && (*(p+2) == '\0' || *(p+2) == '/'))
446 static struct filegen_entry {
449 struct filegen_entry *next;
450 } *filegen_registry = NULL;
458 struct filegen_entry *f = filegen_registry;
461 if (f->name == name || strcmp(name, f->name) == 0) {
462 #ifdef XXX /* this gives the Alpha compiler fits */
464 printf("filegen_get(\"%s\") = %x\n", name,
473 printf("filegen_get(\"%s\") = NULL\n", name);
484 struct filegen_entry **f = &filegen_registry;
486 #ifdef XXX /* this gives the Alpha compiler fits */
488 printf("filegen_register(\"%s\",%x)\n", name, (u_int)filegen);
491 if ((*f)->name == name || strcmp(name, (*f)->name) == 0) {
492 #ifdef XXX /* this gives the Alpha compiler fits */
494 printf("replacing filegen %x\n", (u_int)(*f)->filegen);
497 (*f)->filegen = filegen;
503 *f = (struct filegen_entry *) emalloc(sizeof(struct filegen_entry));
506 (*f)->name = (char*)emalloc(strlen(name) + 1);
507 strcpy((*f)->name, name);
508 (*f)->filegen = filegen;
511 printf("adding new filegen\n");
525 struct filegen_entry **f = &filegen_registry;
529 printf("filegen_unregister(\"%s\")\n", name);
533 if (strcmp((*f)->name,name) == 0) {
534 struct filegen_entry *ff = *f;