/*
* NTP is intended to run long periods of time without restart.
* Thus log and statistic files generated by NTP will grow large.
*
* this set of routines provides a central interface
* to generating files using file generations
*
* the generation of a file is changed according to file generation type
*/
/*
* redefine this if your system dislikes filename suffixes like
* X.19910101 or X.1992W50 or ....
*/
#define SUFFIX_SEP '.'
case FILEGEN_DAY:
/*
* You can argue here in favor of using MJD, but I
* would assume it to be easier for humans to interpret
* dates in a format they are used to in everyday life.
*/
ntpcal_ntp_to_date(&cal, stamp, pivot);
snprintf(suffix, suflen, "%c%04d%02d%02d",
SUFFIX_SEP, cal.year, cal.month, cal.monthday);
cal.hour = cal.minute = cal.second = 0;
gen->id_lo = ntpcal_date_to_ntp(&cal);
gen->id_hi = (u_int32)(gen->id_lo + SECSPERDAY);
break;
/* check possible truncation */
if ('\0' != fullname[len - 1]) {
fullname[len - 1] = '\0';
msyslog(LOG_ERR, "logfile name truncated: \"%s\"",
fullname);
}
if (FILEGEN_NONE != gen->type) {
/*
* check for existence of a file with name 'basename'
* as we disallow such a file
* if FGEN_FLAG_LINK is set create a link
*/
struct stat stats;
/*
* try to resolve name collisions
*/
static u_long conflicts = 0;
#ifndef S_ISREG
#define S_ISREG(mode) (((mode) & S_IFREG) == S_IFREG)
#endif
if (stat(filename, &stats) == 0) {
/* Hm, file exists... */
if (S_ISREG(stats.st_mode)) {
if (stats.st_nlink <= 1) {
/*
* Oh, it is not linked - try to save it
*/
savename = emalloc(len);
snprintf(savename, len,
"%s%c%dC%lu",
filename, SUFFIX_SEP,
(int)getpid(), conflicts++);
if (rename(filename, savename) != 0)
msyslog(LOG_ERR,
"couldn't save %s: %m",
filename);
free(savename);
} else {
/*
* there is at least a second link to
* this file.
* just remove the conflicting one
*/
if (
#if !defined(VMS)
unlink(filename) != 0
#else
delete(filename) != 0
#endif
)
msyslog(LOG_ERR,
"couldn't unlink %s: %m",
filename);
}
} else {
/*
* Ehh? Not a regular file ?? strange !!!!
*/
msyslog(LOG_ERR,
"expected regular file for %s "
"(found mode 0%lo)",
filename,
(unsigned long)stats.st_mode);
}
} else {
/*
* stat(..) failed, but it is absolutely correct for
* 'basename' not to exist
*/
if (ENOENT != errno)
msyslog(LOG_ERR, "stat(%s) failed: %m",
filename);
}
}
/*
* now, try to open new file generation...
*/
DPRINTF(4, ("opening filegen (type=%d/stamp=%u) \"%s\"\n",
gen->type, stamp, fullname));
fp = fopen(fullname, "a");
if (NULL == fp) {
/* open failed -- keep previous state
*
* If the file was open before keep the previous generation.
* This will cause output to end up in the 'wrong' file,
* but I think this is still better than losing output
*
* ignore errors due to missing directories
*/
if (ENOENT != errno)
msyslog(LOG_ERR, "can't open %s: %m", fullname);
} else {
if (NULL != gen->fp) {
fclose(gen->fp);
gen->fp = NULL;
}
gen->fp = fp;
if (gen->flag & FGEN_FLAG_LINK) {
/*
* need to link file to basename
* have to use hardlink for now as I want to allow
* gen->basename spanning directory levels
* this would make it more complex to get the correct
* fullname for symlink
*
* Ok, it would just mean taking the part following
* the last '/' in the name.... Should add it later....
*/
/* Windows NT does not support file links -Greg Schueman 1/18/97 */
#if defined(SYS_WINNT) || defined(SYS_VXWORKS)
SetLastError(0); /* On WinNT, don't support FGEN_FLAG_LINK */
#elif defined(VMS)
errno = 0; /* On VMS, don't support FGEN_FLAG_LINK */
#else /* not (VMS) / VXWORKS / WINNT ; DO THE LINK) */
if (link(fullname, filename) != 0)
if (EEXIST != errno)
msyslog(LOG_ERR,
"can't link(%s, %s): %m",
fullname, filename);
#endif /* SYS_WINNT || VXWORKS */
} /* flags & FGEN_FLAG_LINK */
} /* else fp == NULL */
free(filename);
free(fullname);
return;
}
/*
* this function sets up gen->fp to point to the correct
* generation of the file for the time specified by 'now'
*
* 'now' usually is interpreted as second part of a l_fp as is in the cal...
* library routines
*/
void
filegen_setup(
FILEGEN * gen,
u_int32 now
)
{
int current;
time_t pivot;
if (!(gen->flag & FGEN_FLAG_ENABLED)) {
if (NULL != gen->fp) {
fclose(gen->fp);
gen->fp = NULL;
}
return;
}
switch (gen->type) {
default:
case FILEGEN_NONE:
current = TRUE;
break;
case FILEGEN_PID:
current = ((int)gen->id_lo == getpid());
break;
case FILEGEN_AGE:
current = (gen->id_lo <= current_time) &&
(gen->id_hi > current_time);
break;
case FILEGEN_DAY:
case FILEGEN_WEEK:
case FILEGEN_MONTH:
case FILEGEN_YEAR:
current = (gen->id_lo <= now) &&
(gen->id_hi > now);
break;
}
/*
* try to open file if not yet open
* reopen new file generation file on change of generation id
*/
if (NULL == gen->fp || !current) {
DPRINTF(1, ("filegen %0x %u\n", gen->type, now));
pivot = time(NULL);
filegen_open(gen, now, &pivot);
}
}
/*
* if nothing would be changed...
*/
if (strcmp(dir, gen->dir) == 0 && strcmp(fname, gen->fname) == 0
&& type == gen->type && flag == gen->flag)
return;
/*
* validate parameters
*/
if (!valid_fileref(dir, fname))
return;
/*
* make filegen use the new settings
* special action is only required when a generation file
* is currently open
* otherwise the new settings will be used anyway at the next open
*/
if (file_existed) {
get_systime(&now);
filegen_setup(gen, now.l_ui);
}
}
/*
* check whether concatenating prefix and basename
* yields a legal filename
*/
static int
valid_fileref(
const char * dir,
const char * fname
)
{
/*
* dir cannot be changed dynamically
* (within the context of filegen)
* so just reject basenames containing '..'
*
* ASSUMPTION:
* file system parts 'below' dir may be
* specified without infringement of security
*
* restricting dir to legal values
* has to be ensured by other means
* (however, it would be possible to perform some checks here...)
*/
const char *p;
/*
* Just to catch, dumb errors opening up the world...
*/
if (NULL == dir || '\0' == dir[0])
return FALSE;
if (NULL == fname)
return FALSE;
#ifdef SYS_WINNT
/*
* Windows treats / equivalent to \, reject any / to ensure
* check below for DIR_SEP (\ on windows) are adequate.
*/
if (strchr(fname, '/')) {
msyslog(LOG_ERR,
"filegen filenames must not contain '/': %s",
fname);
return FALSE;
}
#endif
for (p = fname; p != NULL; p = strchr(p, DIR_SEP)) {
if ('.' == p[0] && '.' == p[1]
&& ('\0' == p[2] || DIR_SEP == p[2]))
return FALSE;
}