@ -1,4 +1,4 @@
/* $OpenBSD: ntpd.c,v 1.97 2015/10/12 06:50:08 reyk Exp $ */
/* $OpenBSD: ntpd.c,v 1.98 2015/10/23 16:39:13 deraadt Exp $ */
/*
/*
* Copyright ( c ) 2003 , 2004 Henning Brauer < henning @ openbsd . org >
* Copyright ( c ) 2003 , 2004 Henning Brauer < henning @ openbsd . org >
@ -32,6 +32,7 @@
# include <string.h>
# include <string.h>
# include <time.h>
# include <time.h>
# include <unistd.h>
# include <unistd.h>
# include <fcntl.h>
# include <err.h>
# include <err.h>
# include "ntpd.h"
# include "ntpd.h"
@ -215,6 +216,13 @@ main(int argc, char *argv[])
constraint_cnt = 0 ;
constraint_cnt = 0 ;
/*
* Constraint processes are forked with certificates in memory ,
* then privdrop into chroot before speaking to the outside world .
*/
if ( pledge ( " stdio rpath inet settime proc id " , NULL ) = = - 1 )
err ( 1 , " pledge " ) ;
while ( quit = = 0 ) {
while ( quit = = 0 ) {
new_cnt = PFD_MAX + constraint_cnt ;
new_cnt = PFD_MAX + constraint_cnt ;
if ( new_cnt > pfd_elms ) {
if ( new_cnt > pfd_elms ) {
@ -491,61 +499,59 @@ ntpd_settime(double d)
log_info ( " set local clock to %s (offset %fs) " , buf , d ) ;
log_info ( " set local clock to %s (offset %fs) " , buf , d ) ;
}
}
static FILE * freqfp ;
void
void
readfreq ( void )
readfreq ( void )
{
{
FILE * fp ;
int64_t current ;
int64_t current ;
int fd ;
double d ;
double d ;
fp = f open ( DRIFTFILE , " r " ) ;
if ( fp = = NULL ) {
/* if the drift file has been deleted by the user, reset */
fd = open ( DRIFTFILE , O_RDWR ) ;
if ( fd = = - 1 ) {
log_warnx ( " creating new %s " , DRIFTFILE ) ;
current = 0 ;
current = 0 ;
if ( adjfreq ( & current , NULL ) = = - 1 )
if ( adjfreq ( & current , NULL ) = = - 1 )
log_warn ( " adjfreq reset failed " ) ;
log_warn ( " adjfreq reset failed " ) ;
freqfp = fopen ( DRIFTFILE , " w " ) ;
return ;
return ;
}
}
freqfp = fdopen ( fd , " r+ " ) ;
/* if we're adjusting frequency already, don't override */
/* if we're adjusting frequency already, don't override */
if ( adjfreq ( NULL , & current ) = = - 1 )
if ( adjfreq ( NULL , & current ) = = - 1 )
log_warn ( " adjfreq failed " ) ;
log_warn ( " adjfreq failed " ) ;
else if ( current = = 0 ) {
if ( fscanf ( fp , " %lf " , & d ) = = 1 ) {
else if ( current = = 0 & & freqfp ) {
if ( fscanf ( freqf p , " %lf " , & d ) = = 1 ) {
d / = 1e6 ; /* scale from ppm */
d / = 1e6 ; /* scale from ppm */
ntpd_adjfreq ( d , 0 ) ;
ntpd_adjfreq ( d , 0 ) ;
} else
} else
log_warnx ( " can't read %s" , DRIFTFILE ) ;
log_warnx ( " %s is empty " , DRIFTFILE ) ;
}
}
fclose ( fp ) ;
}
}
int
int
writefreq ( double d )
writefreq ( double d )
{
{
int r ;
int r ;
FILE * fp ;
static int warnonce = 1 ;
static int warnonce = 1 ;
fp = fopen ( DRIFTFILE , " w " ) ;
if ( fp = = NULL ) {
if ( warnonce ) {
log_warn ( " can't open %s " , DRIFTFILE ) ;
warnonce = 0 ;
}
if ( freqfp = = NULL )
return 0 ;
return 0 ;
}
fprintf ( fp , " %.3f \n " , d * 1e6 ) ; /* scale to ppm */
r = ferror ( fp ) ;
if ( fclose ( fp ) ! = 0 | | r ! = 0 ) {
rewind ( freqfp ) ;
fprintf ( freqfp , " %.3f \n " , d * 1e6 ) ; /* scale to ppm */
r = ferror ( freqfp ) ;
if ( r ! = 0 ) {
if ( warnonce ) {
if ( warnonce ) {
log_warnx ( " can't write %s " , DRIFTFILE ) ;
log_warnx ( " can't write %s " , DRIFTFILE ) ;
warnonce = 0 ;
warnonce = 0 ;
}
}
unlink ( DRIFTFILE ) ;
return 0 ;
return 0 ;
}
}
ftruncate ( fileno ( freqfp ) , ftello ( freqfp ) ) ;
fsync ( fileno ( freqfp ) ) ;
return 1 ;
return 1 ;
}
}