diff -up -r ../ntp-4.2.2+pre/ntpd/ntp_util.c ./ntpd/ntp_util.c --- ../ntp-4.2.2+pre/ntpd/ntp_util.c 2006-06-06 13:16:46.000000000 -0700 +++ ./ntpd/ntp_util.c 2006-06-07 13:47:33.000000000 -0700 @@ -52,6 +52,7 @@ static char *key_file_name; * The name of the drift_comp file and the temporary. */ static char *stats_drift_file; +static int drift_exists; static char *stats_temp_file; int stats_write_period = 3600; /* # of seconds between writes. */ double stats_write_tolerance = 0; @@ -83,6 +84,43 @@ static FILEGEN sysstats; static FILEGEN cryptostats; #endif /* OPENSSL */ +/* derived from PM tool getsleep.c */ +#include <mach/mach_port.h> +#include <mach/mach_interface.h> +#include <mach/mach_init.h> + +#include <IOKit/pwr_mgt/IOPMLib.h> +#include <IOKit/pwr_mgt/IOPM.h> + +static io_connect_t getFB() { + static io_connect_t fb; + + if (!fb) { + fb = IOPMFindPowerManagement(kIOMasterPortDefault); + } + return fb; +} + +static unsigned long energy_saving() { + IOReturn err; + unsigned long min = 99; + io_connect_t fb = getFB(); + + if (fb) { + err = IOPMGetAggressiveness( fb, kPMMinutesToSleep, &min); + if (err == kIOReturnSuccess) { + if (min > 0) { + return min; + } else { + err = IOPMGetAggressiveness( fb, kPMMinutesToSpinDown, &min); + if (err == kIOReturnSuccess) { + return min; + } + } + } + } + return 0; +} /* * This controls whether stats are written to the fileset. Provided * so that ntpdc can turn off stats when the file system fills up. @@ -120,6 +158,56 @@ init_util(void) } +void +save_drift_file( + int force + ) +{ + FILE *fp; + static int skipped; + + if (!skipped) + force = 0; /* Don't force a write if we never skipped one */ + if (stats_drift_file != 0 && (force || !drift_exists || !energy_saving())) { + skipped = 0; + if ((fp = fopen(stats_temp_file, "w")) == NULL) { + msyslog(LOG_ERR, "can't open %s: %m", + stats_temp_file); + return; + } + fprintf(fp, "%.3f\n", drift_comp * 1e6); + (void)fclose(fp); + /* atomic */ +#ifdef SYS_WINNT + (void) _unlink(stats_drift_file); /* rename semantics differ under NT */ +#endif /* SYS_WINNT */ + +#ifndef NO_RENAME + (void) rename(stats_temp_file, stats_drift_file); +#else + /* we have no rename NFS of ftp in use*/ + if ((fp = fopen(stats_drift_file, "w")) == NULL) { + msyslog(LOG_ERR, "can't open %s: %m", + stats_drift_file); + return; + } + +#endif + drift_exists++; +#if defined(VMS) + /* PURGE */ + { + $DESCRIPTOR(oldvers,";-1"); + struct dsc$descriptor driftdsc = { + strlen(stats_drift_file),0,0,stats_drift_file }; + + while(lib$delete_file(&oldvers,&driftdsc) & 1) ; + } +#endif + } else { + skipped = 1; + } +} /* * hourly_stats - print some interesting stats @@ -127,8 +215,6 @@ init_util(void) void write_stats(void) { - FILE *fp; - #ifdef DOSYNCTODR struct timeval tv; #if !defined(VMS) @@ -214,42 +300,6 @@ write_stats(void) return; } prev_drift_comp = drift_comp; - if (stats_drift_file != 0) { - if ((fp = fopen(stats_temp_file, "w")) == NULL) { - msyslog(LOG_ERR, "can't open %s: %m", - stats_temp_file); - return; - } - fprintf(fp, "%.3f\n", drift_comp * 1e6); - (void)fclose(fp); - /* atomic */ -#ifdef SYS_WINNT - (void) _unlink(stats_drift_file); /* rename semantics differ under NT */ -#endif /* SYS_WINNT */ - -#ifndef NO_RENAME - (void) rename(stats_temp_file, stats_drift_file); -#else - /* we have no rename NFS of ftp in use */ - if ((fp = fopen(stats_drift_file, "w")) == NULL) { - msyslog(LOG_ERR, "can't open %s: %m", - stats_drift_file); - return; - } - -#endif - -#if defined(VMS) - /* PURGE */ - { - $DESCRIPTOR(oldvers,";-1"); - struct dsc$descriptor driftdsc = { - strlen(stats_drift_file),0,0,stats_drift_file }; - - while(lib$delete_file(&oldvers,&driftdsc) & 1) ; - } -#endif - } } @@ -344,6 +394,7 @@ stats_config( break; } fclose(fp); + drift_exists++; prev_drift_comp = old_drift / 1e6; msyslog(LOG_INFO, "frequency initialized %.3f PPM from %s", diff -up -r ../ntp-4.2.2+pre/ntpd/ntpd.c ./ntpd/ntpd.c --- ../ntp-4.2.2+pre/ntpd/ntpd.c 2006-06-07 11:44:38.000000000 -0700 +++ ./ntpd/ntpd.c 2006-06-07 13:45:42.000000000 -0700 @@ -1021,6 +1021,19 @@ finish( # endif case 0: /* Should never happen... */ return; +# if defined(SIGHUP) || defined(SIGINT) || defined(SIGTERM) +# ifdef SIGHUP + case SIGHUP: +# endif +# ifdef SIGINT + case SIGINT: +# endif +# ifdef SIGTERM + case SIGTERM: +# endif + save_drift_file(1); + /* fall-thru */ +# endif default: exit(0); }