Add an option to add a slight time skew to the execution of scripts to
[dragonfly.git] / usr.sbin / ntpd / patches / ntp.c.patch
1 $DragonFly: src/usr.sbin/ntpd/patches/Attic/ntp.c.patch,v 1.8 2005/04/23 20:43:34 joerg Exp $
2
3 --- ntp.c       25 Feb 2005 16:43:19 -0000      1.6
4 +++ ntp.c       12 Apr 2005 20:13:45 -0000
5 @@ -139,6 +139,11 @@
6                 client_peer_init(p);
7  
8         bzero(&conf->status, sizeof(conf->status));
9 +       conf->freq_samples = -1;
10 +       conf->freq_x = 0.0;
11 +       conf->freq_xx = 0.0;
12 +       conf->freq_xy = 0.0;
13 +       conf->freq_y = 0.0;
14         conf->status.leap = LI_ALARM;
15         clock_getres(CLOCK_REALTIME, &tp);
16         b = 1000000000 / tp.tv_nsec;    /* convert to Hz */
17 @@ -206,7 +211,11 @@
18                         if (p->deadline > 0 && p->deadline < nextaction)
19                                 nextaction = p->deadline;
20                         if (p->deadline > 0 && p->deadline <= time(NULL)) {
21 -                               timeout = error_interval();
22 +                               if (p->trustlevel > TRUSTLEVEL_BADPEER)
23 +                                       timeout = scale_interval(
24 +                                           INTERVAL_QUERY_AGRESSIVE);
25 +                               else
26 +                                       timeout = error_interval();
27                                 log_debug("no reply from %s received in time, "
28                                     "next query %ds", log_sockaddr(
29                                     (struct sockaddr *)&p->addr->ss), timeout);
30 @@ -369,6 +378,45 @@
31         peer_cnt--;
32  }
33  
34 +static void
35 +priv_adjfreq(double offset)
36 +{
37 +       double curtime, freq;
38 +
39 +       conf->freq_samples++;
40 +
41 +       if (conf->freq_samples <= 0)
42 +               return;
43 +
44 +       offset += conf->overall_offset; 
45 +       curtime = gettime_corrected();
46 +       conf->freq_xy += offset * curtime;
47 +       conf->freq_x += curtime;
48 +       conf->freq_y += offset;
49 +       conf->freq_xx += curtime * curtime;
50 +
51 +       if (conf->freq_samples % FREQUENCY_SAMPLES != 0)
52 +               return;
53 +
54 +       freq =
55 +           (conf->freq_xy - conf->freq_x * conf->freq_y / conf->freq_samples)
56 +           /
57 +           (conf->freq_xx - conf->freq_x * conf->freq_x / conf->freq_samples);
58 +
59 +       if (freq > MAX_FREQUENCY_ADJUST)
60 +               freq = MAX_FREQUENCY_ADJUST;
61 +       else if (freq < -MAX_FREQUENCY_ADJUST)
62 +               freq = -MAX_FREQUENCY_ADJUST;
63 +
64 +       imsg_compose(ibuf_main, IMSG_ADJFREQ, 0, 0, &freq, sizeof(freq));
65 +       log_info("adjusting freqency by %f ppm", freq * 1e6);
66 +       conf->freq_xy = 0.0;
67 +       conf->freq_x = 0.0;
68 +       conf->freq_y = 0.0;
69 +       conf->freq_xx = 0.0;
70 +       conf->freq_samples = 0;
71 +}
72 +
73  void
74  priv_adjtime(void)
75  {
76 @@ -418,6 +464,9 @@
77                 imsg_compose(ibuf_main, IMSG_ADJTIME, 0, 0,
78                     &offset_median, sizeof(offset_median));
79  
80 +               conf->overall_offset += offset_median;
81 +               priv_adjfreq(offset_median);
82 +
83                 conf->status.reftime = gettime();
84                 conf->status.leap = LI_NOWARNING;
85                 conf->status.stratum++; /* one more than selected peer */
86 @@ -426,12 +475,15 @@
87                 if (peers[offset_cnt / 2]->addr->ss.ss_family == AF_INET)
88                         conf->status.refid = ((struct sockaddr_in *)
89                             &peers[offset_cnt / 2]->addr->ss)->sin_addr.s_addr;
90 +
91 +               TAILQ_FOREACH(p, &conf->ntp_peers, entry) {
92 +                       for (i = 0; i < OFFSET_ARRAY_SIZE; i++)
93 +                               p->reply[i].offset -= offset_median;
94 +                       p->update.good = 0;
95 +               }
96         }
97  
98         free(peers);
99 -
100 -       TAILQ_FOREACH(p, &conf->ntp_peers, entry)
101 -               p->update.good = 0;
102  }
103  
104  int