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 Universitaet Erlangen-Nuernberg, 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 '.'
47 static void filegen_open (FILEGEN *, u_int32, const time_t*);
48 static int valid_fileref (const char *, const char *);
49 static void filegen_init (const char *, const char *, FILEGEN *);
51 static void filegen_uninit (FILEGEN *);
67 fgp->dir = estrdup(dir);
68 fgp->fname = estrdup(fname);
71 fgp->type = FILEGEN_DAY;
72 fgp->flag = FGEN_FLAG_LINK; /* not yet enabled !!*/
77 * filegen_uninit - free memory allocated by filegen_init
92 * open a file generation according to the current settings of gen
93 * will also provide a link to basename if requested to do so
103 char *savename; /* temp store for name collision handling */
104 char *fullname; /* name with any designation extension */
105 char *filename; /* name without designation extension */
106 char *suffix; /* where to print suffix extension */
112 /* get basic filename in buffer, leave room for extensions */
113 len = strlen(gen->dir) + strlen(gen->fname) + 65;
114 filename = emalloc(len);
115 fullname = emalloc(len);
117 snprintf(filename, len, "%s%s", gen->dir, gen->fname);
119 /* where to place suffix */
120 suflen = strlcpy(fullname, filename, len);
121 suffix = fullname + suflen;
122 suflen = len - suflen;
124 /* last octet of fullname set to '\0' for truncation check */
125 fullname[len - 1] = '\0';
131 "unsupported file generations type %d for "
132 "\"%s\" - reverting to FILEGEN_NONE",
133 gen->type, filename);
134 gen->type = FILEGEN_NONE;
138 /* no suffix, all set */
142 gen->id_lo = getpid();
144 snprintf(suffix, suflen, "%c#%ld",
145 SUFFIX_SEP, gen->id_lo);
150 * You can argue here in favor of using MJD, but I
151 * would assume it to be easier for humans to interpret
152 * dates in a format they are used to in everyday life.
154 ntpcal_ntp_to_date(&cal, stamp, pivot);
155 snprintf(suffix, suflen, "%c%04d%02d%02d",
156 SUFFIX_SEP, cal.year, cal.month, cal.monthday);
157 cal.hour = cal.minute = cal.second = 0;
158 gen->id_lo = ntpcal_date_to_ntp(&cal);
159 gen->id_hi = (u_int32)(gen->id_lo + SECSPERDAY);
163 isocal_ntp_to_date(&iso, stamp, pivot);
164 snprintf(suffix, suflen, "%c%04dw%02d",
165 SUFFIX_SEP, iso.year, iso.week);
166 iso.hour = iso.minute = iso.second = 0;
168 gen->id_lo = isocal_date_to_ntp(&iso);
169 gen->id_hi = (u_int32)(gen->id_lo + 7 * SECSPERDAY);
173 ntpcal_ntp_to_date(&cal, stamp, pivot);
174 snprintf(suffix, suflen, "%c%04d%02d",
175 SUFFIX_SEP, cal.year, cal.month);
176 cal.hour = cal.minute = cal.second = 0;
178 gen->id_lo = ntpcal_date_to_ntp(&cal);
180 gen->id_hi = ntpcal_date_to_ntp(&cal);
184 ntpcal_ntp_to_date(&cal, stamp, pivot);
185 snprintf(suffix, suflen, "%c%04d",
186 SUFFIX_SEP, cal.year);
187 cal.hour = cal.minute = cal.second = 0;
188 cal.month = cal.monthday = 1;
189 gen->id_lo = ntpcal_date_to_ntp(&cal);
191 gen->id_hi = ntpcal_date_to_ntp(&cal);
195 gen->id_lo = current_time - (current_time % SECSPERDAY);
196 gen->id_hi = gen->id_lo + SECSPERDAY;
197 snprintf(suffix, suflen, "%ca%08ld",
198 SUFFIX_SEP, gen->id_lo);
201 /* check possible truncation */
202 if ('\0' != fullname[len - 1]) {
203 fullname[len - 1] = '\0';
204 msyslog(LOG_ERR, "logfile name truncated: \"%s\"",
208 if (FILEGEN_NONE != gen->type) {
210 * check for existence of a file with name 'basename'
211 * as we disallow such a file
212 * if FGEN_FLAG_LINK is set create a link
216 * try to resolve name collisions
218 static u_long conflicts = 0;
221 #define S_ISREG(mode) (((mode) & S_IFREG) == S_IFREG)
223 if (stat(filename, &stats) == 0) {
224 /* Hm, file exists... */
225 if (S_ISREG(stats.st_mode)) {
226 if (stats.st_nlink <= 1) {
228 * Oh, it is not linked - try to save it
230 savename = emalloc(len);
231 snprintf(savename, len,
233 filename, SUFFIX_SEP,
234 (int)getpid(), conflicts++);
236 if (rename(filename, savename) != 0)
238 "couldn't save %s: %m",
243 * there is at least a second link to
245 * just remove the conflicting one
249 unlink(filename) != 0
251 delete(filename) != 0
255 "couldn't unlink %s: %m",
260 * Ehh? Not a regular file ?? strange !!!!
263 "expected regular file for %s "
266 (unsigned long)stats.st_mode);
270 * stat(..) failed, but it is absolutely correct for
271 * 'basename' not to exist
274 msyslog(LOG_ERR, "stat(%s) failed: %m",
280 * now, try to open new file generation...
282 DPRINTF(4, ("opening filegen (type=%d/stamp=%u) \"%s\"\n",
283 gen->type, stamp, fullname));
285 fp = fopen(fullname, "a");
288 /* open failed -- keep previous state
290 * If the file was open before keep the previous generation.
291 * This will cause output to end up in the 'wrong' file,
292 * but I think this is still better than losing output
294 * ignore errors due to missing directories
298 msyslog(LOG_ERR, "can't open %s: %m", fullname);
300 if (NULL != gen->fp) {
306 if (gen->flag & FGEN_FLAG_LINK) {
308 * need to link file to basename
309 * have to use hardlink for now as I want to allow
310 * gen->basename spanning directory levels
311 * this would make it more complex to get the correct
312 * fullname for symlink
314 * Ok, it would just mean taking the part following
315 * the last '/' in the name.... Should add it later....
318 /* Windows NT does not support file links -Greg Schueman 1/18/97 */
320 #if defined(SYS_WINNT) || defined(SYS_VXWORKS)
321 SetLastError(0); /* On WinNT, don't support FGEN_FLAG_LINK */
323 errno = 0; /* On VMS, don't support FGEN_FLAG_LINK */
324 #else /* not (VMS) / VXWORKS / WINNT ; DO THE LINK) */
325 if (link(fullname, filename) != 0)
328 "can't link(%s, %s): %m",
330 #endif /* SYS_WINNT || VXWORKS */
331 } /* flags & FGEN_FLAG_LINK */
332 } /* else fp == NULL */
340 * this function sets up gen->fp to point to the correct
341 * generation of the file for the time specified by 'now'
343 * 'now' usually is interpreted as second part of a l_fp as is in the cal...
356 if (!(gen->flag & FGEN_FLAG_ENABLED)) {
357 if (NULL != gen->fp) {
372 current = ((int)gen->id_lo == getpid());
376 current = (gen->id_lo <= current_time) &&
377 (gen->id_hi > current_time);
384 current = (gen->id_lo <= now) &&
389 * try to open file if not yet open
390 * reopen new file generation file on change of generation id
392 if (NULL == gen->fp || !current) {
393 DPRINTF(1, ("filegen %0x %u\n", gen->type, now));
395 filegen_open(gen, now, &pivot);
401 * change settings for filegen files
417 * if nothing would be changed...
419 if (strcmp(dir, gen->dir) == 0 && strcmp(fname, gen->fname) == 0
420 && type == gen->type && flag == gen->flag)
424 * validate parameters
426 if (!valid_fileref(dir, fname))
429 if (NULL != gen->fp) {
434 file_existed = FALSE;
437 DPRINTF(3, ("configuring filegen:\n"
439 "\tfname:\t%s -> %s\n"
440 "\ttype:\t%d -> %d\n"
441 "\tflag: %x -> %x\n",
447 if (strcmp(gen->dir, dir) != 0) {
449 gen->dir = estrdup(dir);
452 if (strcmp(gen->fname, fname) != 0) {
454 gen->fname = estrdup(fname);
456 gen->type = (u_char)type;
457 gen->flag = (u_char)flag;
460 * make filegen use the new settings
461 * special action is only required when a generation file
463 * otherwise the new settings will be used anyway at the next open
467 filegen_setup(gen, now.l_ui);
473 * check whether concatenating prefix and basename
474 * yields a legal filename
483 * dir cannot be changed dynamically
484 * (within the context of filegen)
485 * so just reject basenames containing '..'
488 * file system parts 'below' dir may be
489 * specified without infringement of security
491 * restricting dir to legal values
492 * has to be ensured by other means
493 * (however, it would be possible to perform some checks here...)
498 * Just to catch, dumb errors opening up the world...
500 if (NULL == dir || '\0' == dir[0])
508 * Windows treats / equivalent to \, reject any / to ensure
509 * check below for DIR_SEP (\ on windows) are adequate.
511 if (strchr(fname, '/')) {
513 "filegen filenames must not contain '/': %s",
519 for (p = fname; p != NULL; p = strchr(p, DIR_SEP)) {
520 if ('.' == p[0] && '.' == p[1]
521 && ('\0' == p[2] || DIR_SEP == p[2]))
533 static struct filegen_entry {
536 struct filegen_entry * next;
537 } *filegen_registry = NULL;
545 struct filegen_entry *f = filegen_registry;
548 if (f->name == name || strcmp(name, f->name) == 0) {
549 DPRINTF(4, ("filegen_get(%s) = %p\n",
555 DPRINTF(4, ("filegen_get(%s) = NULL\n", name));
567 struct filegen_entry **ppfe;
569 DPRINTF(4, ("filegen_register(%s, %p)\n", name, filegen));
571 filegen_init(dir, name, filegen);
573 ppfe = &filegen_registry;
574 while (NULL != *ppfe) {
575 if ((*ppfe)->name == name
576 || !strcmp((*ppfe)->name, name)) {
578 DPRINTF(5, ("replacing filegen %p\n",
581 (*ppfe)->filegen = filegen;
584 ppfe = &((*ppfe)->next);
587 *ppfe = emalloc(sizeof **ppfe);
589 (*ppfe)->next = NULL;
590 (*ppfe)->name = estrdup(name);
591 (*ppfe)->filegen = filegen;
593 DPRINTF(6, ("adding new filegen\n"));
600 * filegen_statsdir() - reset each filegen entry's dir to statsdir.
603 filegen_statsdir(void)
605 struct filegen_entry *f;
607 for (f = filegen_registry; f != NULL; f = f->next)
608 filegen_config(f->filegen, statsdir, f->filegen->fname,
609 f->filegen->type, f->filegen->flag);
614 * filegen_unregister frees memory allocated by filegen_register for
623 struct filegen_entry ** ppfe;
624 struct filegen_entry * pfe;
627 DPRINTF(4, ("filegen_unregister(%s)\n", name));
629 ppfe = &filegen_registry;
631 while (NULL != *ppfe) {
632 if ((*ppfe)->name == name
633 || !strcmp((*ppfe)->name, name)) {
635 *ppfe = (*ppfe)->next;
642 ppfe = &((*ppfe)->next);