ntp_filegen.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
/*
* Copyright (c) 1996,1999 by Sun Microsystems, Inc.
* All rights reserved.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* ntp_filegen.c,v 3.12 1994/01/25 19:06:11 kardel Exp
*
* implements file generations support for NTP
* logfiles and statistic files
*
*
* Copyright (C) 1992, 1996 by Rainer Pruy
* Friedrich-Alexander Universit�t Erlangen-N�rnberg, Germany
*
* This code may be modified and used freely
* provided credits remain intact.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <errno.h>
#include "ntpd.h"
#include "ntp_io.h"
#include "ntp_string.h"
#include "ntp_calendar.h"
#include "ntp_filegen.h"
#include "ntp_stdlib.h"
/*
* 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
*/
/*
* to check reason on open failure
*/
#ifndef SYS_WINNT
extern int errno;
#endif /* SYS_WINNT */
/*
* imported from timer
*/
extern u_long current_time;
/*
* redefine this if your system dislikes filename suffixes like
* X.19910101 or X.1992W50 or ....
*/
#define SUFFIX_SEP '.'
/*
* other constants
*/
#ifdef DEBUG
extern int debug;
#endif
static int valid_fileref P((char *, char *));
#ifdef UNUSED
static FILEGEN *filegen_unregister P((char *));
#endif /* UNUSED */
/*
* open a file generation according to the current settings of gen
* will also provide a link to basename if requested to do so
*/
static void
{
char *filename;
char *basename;
default:
/*FALLTHROUGH*/
case FILEGEN_NONE:
break;
case FILEGEN_PID:
break;
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.
*/
break;
case FILEGEN_WEEK:
/*
* This is still a hack
* - the term week is not correlated to week as it is used
* normally - it just refers to a period of 7 days
* starting at Jan 1 - 'weeks' are counted starting from zero
*/
break;
case FILEGEN_MONTH:
break;
case FILEGEN_YEAR:
break;
case FILEGEN_AGE:
break;
}
/*
* check for existence of a file with name 'basename'
* as we disallow such a file
* if FGEN_FLAG_LINK is set create a link
*/
/*
* try to resolve name collisions
*/
#ifndef S_ISREG
#endif
/* Hm, file exists... */
/*
* Oh, it is not linked - try to save it
*/
(int) getpid(),
} else {
/*
* there is at least a second link tpo this file
* just remove the conflicting one
*/
#if !defined(VMS)
#else
#endif
}
} else {
/*
* Ehh? Not a regular file ?? strange !!!!
*/
}
} else {
/*
* stat(..) failed, but it is absolutely correct for
* 'basename' not to exist
*/
}
}
/*
* now, try to open new file generation...
*/
#ifdef DEBUG
if (debug > 3)
#endif
/* 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 loosing output
*
* ignore errors due to missing directories
*/
} else {
}
/*
* 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 filename
* 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 */
#else
{
#ifndef SYS_WINNT
errno = 0; /* On VMS, don't support FGEN_FLAG_LINK */
#else
SetLastError(0); /* On WinNT, don't support FGEN_FLAG_LINK */
#endif /* SYS_WINNT */
#endif /* VMS */
}
} /* flags & FGEN_FLAG_LINK */
} /* else fp == NULL */
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
{
return;
}
case FILEGEN_NONE:
break;
case FILEGEN_PID:
break;
case FILEGEN_DAY:
break;
case FILEGEN_WEEK:
/* Would be nice to have a calweekstart() routine */
/* so just use a hack ... */
/* just round time to integral 7 days period for actual year */
+ 60;
/*
* just to be sure -
* the computation above would fail in the presence of leap seconds
* so at least carry the date to the next day (+60 (seconds))
* and go back to the start of the day via calendar computations
*/
break;
case FILEGEN_MONTH:
break;
case FILEGEN_YEAR:
break;
case FILEGEN_AGE:
break;
}
/*
* try to open file if not yet open
* reopen new file generation file on change of generation id
*/
}
}
/*
* change settings for filegen files
*/
void
char *basename;
{
/*
* if nothing would be changed...
*/
return;
/*
* validate parameters
*/
return;
#ifdef DEBUG
if (debug > 2)
printf("configuring filegen:\n\tprefix:\t%s\n\tbasename:\t%s -> %s\n\ttype:\t%d -> %d\n\tflag: %x -> %x\n",
#endif
}
/*
* 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
*/
get_systime(&now);
}
}
/*
* check whether concatenating prefix and basename
* yields a legal filename
*/
static int
{
/*
* prefix cannot be changed dynamically
* (within the context of filegen)
* so just reject basenames containing '..'
*
* ASSUMPTION:
* file system parts 'below' prefix may be
* specified without infringement of security
*
* restricing prefix to legal values
* has to be ensured by other means
* (however, it would be possible to perform some checks here...)
*/
register char *p = basename;
/*
* Just to catch, dumb errors opening up the world...
*/
return 0;
return 0;
if (*p == '.' && *(p+1) == '.' && (*(p+2) == '\0' || *(p+2) == '/'))
return 0;
}
return 1;
}
/*
* filegen registry
*/
static struct filegen_entry {
char *name;
struct filegen_entry *next;
} *filegen_registry = NULL;
FILEGEN *
char *name;
{
struct filegen_entry *f = filegen_registry;
while(f) {
#ifdef XXX /* this gives the Alpha compiler fits */
if (debug > 3)
#endif
return f->filegen;
}
f = f->next;
}
#ifdef DEBUG
if (debug > 3)
#endif
return NULL;
}
void
char *name;
{
struct filegen_entry **f = &filegen_registry;
#ifdef XXX /* this gives the Alpha compiler fits */
if (debug > 3)
#endif
while (*f) {
#ifdef XXX /* this gives the Alpha compiler fits */
if (debug > 4) {
}
#endif
return;
}
f = &((*f)->next);
}
if (*f) {
#ifdef DEBUG
if (debug > 5) {
printf("adding new filegen\n");
}
#endif
}
return;
}
#ifdef UNUSED
static FILEGEN *
char *name;
{
struct filegen_entry **f = &filegen_registry;
#ifdef DEBUG
if (debug > 3)
#endif
while (*f) {
struct filegen_entry *ff = *f;
*f = (*f)->next;
return fg;
}
f = &((*f)->next);
}
return NULL;
}
#endif /* UNUSED */