Add support for saving frequency skew to a file (#1320)
[openwrt/svn-archive/archive.git] / net / openntpd / patches / 02-save_freq_drift.patch
1 --- openntpd-3.9p1/ntpd.h 2006-05-14 07:29:21.000000000 +0200
2 +++ openntpd-3.9p1new/ntpd.h 2007-02-03 23:51:56.000000000 +0100
3 @@ -34,6 +34,7 @@
4 #define NTPD_USER "_ntp"
5 #endif
6 #define CONFFILE SYSCONFDIR "/ntpd.conf"
7 +#define DRIFTFILE "/var/db/ntpd.drift"
8
9 #define READ_BUF_SIZE 4096
10
11 --- openntpd-3.9p1/ntpd.c 2006-05-14 07:29:23.000000000 +0200
12 +++ openntpd-3.9p1new/ntpd.c 2007-02-04 00:00:36.000000000 +0100
13 @@ -45,6 +45,7 @@
14 int dispatch_imsg(struct ntpd_conf *);
15 int ntpd_adjtime(double);
16 void ntpd_settime(double);
17 +void readfreq(void);
18
19 volatile sig_atomic_t quit = 0;
20 volatile sig_atomic_t reconfig = 0;
21 @@ -153,7 +154,8 @@
22 chld_pid = ntp_main(pipe_chld, &conf);
23
24 setproctitle("[priv]");
25 -
26 + readfreq();
27 +
28 signal(SIGTERM, sighdlr);
29 signal(SIGINT, sighdlr);
30 signal(SIGCHLD, sighdlr);
31
32 --- openntpd-3.9p1/openbsd-compat/port-linux.c 2007-02-03 20:52:20.000000000 +0100
33 +++ openntpd-3.9p1new/openbsd-compat/port-linux.c 2007-02-04 01:02:16.000000000 +0100
34 @@ -33,6 +33,49 @@
35 /* maximum change to skew per adjustment, in PPM */
36 #define MAX_SKEW_DELTA 5.0
37
38 +void
39 +writefreq(double d)
40 +{
41 + int r;
42 + FILE *fp;
43 +
44 + fp = fopen(DRIFTFILE, "w");
45 + if (fp == NULL)
46 + {
47 + log_warn("opening drift file for writing failed");
48 + return;
49 + }
50 +
51 + fprintf(fp, "%e\n", d);
52 + r = ferror(fp);
53 + if (fclose(fp) != 0 || r != 0)
54 + {
55 + unlink(DRIFTFILE);
56 + log_warn("saving freq drift failed");
57 + }
58 +}
59 +
60 +void
61 +readfreq(void)
62 +{
63 + FILE *fp;
64 + double d;
65 + struct timex tmx;
66 +
67 + fp = fopen(DRIFTFILE, "r");
68 + if (fp == NULL)
69 + return;
70 +
71 + if (fscanf(fp, "%le", &d) == 1)
72 + {
73 + tmx.freq = (long)(d * ADJTIMEX_FREQ_SCALE);
74 + tmx.modes = ADJ_FREQUENCY;
75 + if (adjtimex(&tmx) == -1)
76 + log_warn("adjtimex set freq failed");
77 + }
78 + fclose(fp);
79 +}
80 +
81 int
82 _compat_adjtime(const struct timeval *delta, struct timeval *olddelta)
83 {
84 @@ -93,6 +136,8 @@
85 tmx.modes = ADJ_FREQUENCY;
86 if (adjtimex(&tmx) == -1)
87 log_warn("adjtimex set freq failed");
88 +
89 + writefreq( tskew );
90 }
91
92 log_debug("interval %0.3lf skew %0.3lf total skew %0.3lf", interval,