Bump to version 1.22.1
[platform/upstream/busybox.git] / networking / ntpd.c
index e86f72f..c4b0187 100644 (file)
@@ -46,6 +46,7 @@
 #include "libbb.h"
 #include <math.h>
 #include <netinet/ip.h> /* For IPTOS_LOWDELAY definition */
+#include <sys/resource.h> /* setpriority */
 #include <sys/timex.h>
 #ifndef IPTOS_LOWDELAY
 # define IPTOS_LOWDELAY 0x10
 
 
 /* Verbosity control (max level of -dddd options accepted).
- * max 5 is very talkative (and bloated). 2 is non-bloated,
+ * max 6 is very talkative (and bloated). 3 is non-bloated,
  * production level setting.
  */
-#define MAX_VERBOSE     2
+#define MAX_VERBOSE     3
 
 
 /* High-level description of the algorithm:
  * was hibernated, someone set totally wrong date, etc),
  * then the time is stepped, all datapoints are discarded,
  * and we go back to steady state.
+ *
+ * Made some changes to speed up re-syncing after our clock goes bad
+ * (tested with suspending my laptop):
+ * - if largish offset (>= STEP_THRESHOLD * 8 == 1 sec) is seen
+ *   from a peer, schedule next query for this peer soon
+ *   without drastically lowering poll interval for everybody.
+ *   This makes us collect enough data for step much faster:
+ *   e.g. at poll = 10 (1024 secs), step was done within 5 minutes
+ *   after first reply which indicated that our clock is 14 seconds off.
+ * - on step, do not discard d_dispersion data of the existing datapoints,
+ *   do not clear reachable_bits. This prevents discarding first ~8
+ *   datapoints after the step.
  */
 
-#define RETRY_INTERVAL  5       /* on error, retry in N secs */
+#define RETRY_INTERVAL     5    /* on error, retry in N secs */
 #define RESPONSE_INTERVAL 15    /* wait for reply up to N secs */
-#define INITIAL_SAMPLES 4       /* how many samples do we want for init */
+#define INITIAL_SAMPLES    4    /* how many samples do we want for init */
+#define BAD_DELAY_GROWTH   4    /* drop packet if its delay grew by more than this */
 
 /* Clock discipline parameters and constants */
 
 #define FREQ_TOLERANCE  0.000015 /* frequency tolerance (15 PPM) */
 #define BURSTPOLL       0       /* initial poll */
 #define MINPOLL         5       /* minimum poll interval. std ntpd uses 6 (6: 64 sec) */
-#define BIGPOLL         10      /* drop to lower poll at any trouble (10: 17 min) */
+/* If we got largish offset from a peer, cap next query interval
+ * for this peer by this many seconds:
+ */
+#define BIGOFF_INTERVAL (1 << 6)
+/* If offset > discipline_jitter * POLLADJ_GATE, and poll interval is >= 2^BIGPOLL,
+ * then it is decreased _at once_. (If < 2^BIGPOLL, it will be decreased _eventually_).
+ */
+#define BIGPOLL         10      /* 2^10 sec ~= 17 min */
 #define MAXPOLL         12      /* maximum poll interval (12: 1.1h, 17: 36.4h). std ntpd uses 17 */
 /* Actively lower poll when we see such big offsets.
  * With STEP_THRESHOLD = 0.125, it means we try to sync more aggressively
 
 /* Poll-adjust threshold.
  * When we see that offset is small enough compared to discipline jitter,
- * we grow a counter: += MINPOLL. When it goes over POLLADJ_LIMIT,
+ * we grow a counter: += MINPOLL. When counter goes over POLLADJ_LIMIT,
  * we poll_exp++. If offset isn't small, counter -= poll_exp*2,
- * and when it goes below -POLLADJ_LIMIT, we poll_exp--
- * (bumped from 30 to 40 since otherwise I often see poll_exp going *2* steps down)
+ * and when it goes below -POLLADJ_LIMIT, we poll_exp--.
+ * (Bumped from 30 to 40 since otherwise I often see poll_exp going *2* steps down)
  */
 #define POLLADJ_LIMIT   40
-/* If offset < POLLADJ_GATE * discipline_jitter, then we can increase
+/* If offset < discipline_jitter * POLLADJ_GATE, then we decide to increase
  * poll interval (we think we can't improve timekeeping
  * by staying at smaller poll).
  */
 #define POLLADJ_GATE    4
+#define TIMECONST_HACK_GATE 2
 /* Compromise Allan intercept (sec). doc uses 1500, std ntpd uses 512 */
 #define ALLAN           512
 /* PLL loop gain */
@@ -208,22 +230,22 @@ typedef struct {
 } msg_t;
 
 typedef struct {
-       double d_recv_time;
        double d_offset;
+       double d_recv_time;
        double d_dispersion;
 } datapoint_t;
 
 typedef struct {
        len_and_sockaddr *p_lsa;
        char             *p_dotted;
-       /* when to send new query (if p_fd == -1)
-        * or when receive times out (if p_fd >= 0): */
        int              p_fd;
        int              datapoint_idx;
        uint32_t         lastpkt_refid;
        uint8_t          lastpkt_status;
        uint8_t          lastpkt_stratum;
        uint8_t          reachable_bits;
+       /* when to send new query (if p_fd == -1)
+        * or when receive times out (if p_fd >= 0): */
        double           next_action_time;
        double           p_xmttime;
        double           lastpkt_recv_time;
@@ -272,6 +294,9 @@ struct globals {
        llist_t  *ntp_peers;
 #if ENABLE_FEATURE_NTPD_SERVER
        int      listen_fd;
+# define G_listen_fd (G.listen_fd)
+#else
+# define G_listen_fd (-1)
 #endif
        unsigned verbose;
        unsigned peer_cnt;
@@ -325,7 +350,7 @@ struct globals {
 
 #define STATE_NSET      0       /* initial state, "nothing is set" */
 //#define STATE_FSET    1       /* frequency set from file */
-#define STATE_SPIK      2       /* spike detected */
+//#define STATE_SPIK    2       /* spike detected */
 //#define STATE_FREQ    3       /* initial frequency */
 #define STATE_SYNC      4       /* clock synchronized (normal operation) */
        uint8_t  discipline_state;      // doc calls it c.state
@@ -336,6 +361,10 @@ struct globals {
        double   last_update_offset;    // c.last
        double   last_update_recv_time; // s.t
        double   discipline_jitter;     // c.jitter
+       /* Since we only compare it with ints, can simplify code
+        * by not making this variable floating point:
+        */
+       unsigned offset_to_jitter_ratio;
        //double   cluster_offset;        // s.offset
        //double   cluster_jitter;        // s.jitter
 #if !USING_KERNEL_PLL_LOOP
@@ -354,6 +383,7 @@ static const int const_IPTOS_LOWDELAY = IPTOS_LOWDELAY;
 #define VERB3 if (MAX_VERBOSE >= 3 && G.verbose >= 3)
 #define VERB4 if (MAX_VERBOSE >= 4 && G.verbose >= 4)
 #define VERB5 if (MAX_VERBOSE >= 5 && G.verbose >= 5)
+#define VERB6 if (MAX_VERBOSE >= 6 && G.verbose >= 6)
 
 
 static double LOG2D(int a)
@@ -509,23 +539,34 @@ static void
 filter_datapoints(peer_t *p)
 {
        int i, idx;
+       double sum, wavg;
+       datapoint_t *fdp;
+
+#if 0
+/* Simulations have shown that use of *averaged* offset for p->filter_offset
+ * is in fact worse than simply using last received one: with large poll intervals
+ * (>= 2048) averaging code uses offset values which are outdated by hours,
+ * and time/frequency correction goes totally wrong when fed essentially bogus offsets.
+ */
        int got_newest;
-       double minoff, maxoff, wavg, sum, w;
+       double minoff, maxoff, w;
        double x = x; /* for compiler */
        double oldest_off = oldest_off;
        double oldest_age = oldest_age;
        double newest_off = newest_off;
        double newest_age = newest_age;
 
-       minoff = maxoff = p->filter_datapoint[0].d_offset;
+       fdp = p->filter_datapoint;
+
+       minoff = maxoff = fdp[0].d_offset;
        for (i = 1; i < NUM_DATAPOINTS; i++) {
-               if (minoff > p->filter_datapoint[i].d_offset)
-                       minoff = p->filter_datapoint[i].d_offset;
-               if (maxoff < p->filter_datapoint[i].d_offset)
-                       maxoff = p->filter_datapoint[i].d_offset;
+               if (minoff > fdp[i].d_offset)
+                       minoff = fdp[i].d_offset;
+               if (maxoff < fdp[i].d_offset)
+                       maxoff = fdp[i].d_offset;
        }
 
-       idx = p->datapoint_idx; /* most recent datapoint */
+       idx = p->datapoint_idx; /* most recent datapoint's index */
        /* Average offset:
         * Drop two outliers and take weighted average of the rest:
         * most_recent/2 + older1/4 + older2/8 ... + older5/32 + older6/32
@@ -544,27 +585,27 @@ filter_datapoints(peer_t *p)
        got_newest = 0;
        sum = 0;
        for (i = 0; i < NUM_DATAPOINTS; i++) {
-               VERB4 {
+               VERB5 {
                        bb_error_msg("datapoint[%d]: off:%f disp:%f(%f) age:%f%s",
                                i,
-                               p->filter_datapoint[idx].d_offset,
-                               p->filter_datapoint[idx].d_dispersion, dispersion(&p->filter_datapoint[idx]),
-                               G.cur_time - p->filter_datapoint[idx].d_recv_time,
-                               (minoff == p->filter_datapoint[idx].d_offset || maxoff == p->filter_datapoint[idx].d_offset)
+                               fdp[idx].d_offset,
+                               fdp[idx].d_dispersion, dispersion(&fdp[idx]),
+                               G.cur_time - fdp[idx].d_recv_time,
+                               (minoff == fdp[idx].d_offset || maxoff == fdp[idx].d_offset)
                                        ? " (outlier by offset)" : ""
                        );
                }
 
-               sum += dispersion(&p->filter_datapoint[idx]) / (2 << i);
+               sum += dispersion(&fdp[idx]) / (2 << i);
 
-               if (minoff == p->filter_datapoint[idx].d_offset) {
+               if (minoff == fdp[idx].d_offset) {
                        minoff -= 1; /* so that we don't match it ever again */
                } else
-               if (maxoff == p->filter_datapoint[idx].d_offset) {
+               if (maxoff == fdp[idx].d_offset) {
                        maxoff += 1;
                } else {
-                       oldest_off = p->filter_datapoint[idx].d_offset;
-                       oldest_age = G.cur_time - p->filter_datapoint[idx].d_recv_time;
+                       oldest_off = fdp[idx].d_offset;
+                       oldest_age = G.cur_time - fdp[idx].d_recv_time;
                        if (!got_newest) {
                                got_newest = 1;
                                newest_off = oldest_off;
@@ -597,6 +638,32 @@ filter_datapoints(peer_t *p)
        }
        p->filter_offset = wavg;
 
+#else
+
+       fdp = p->filter_datapoint;
+       idx = p->datapoint_idx; /* most recent datapoint's index */
+
+       /* filter_offset: simply use the most recent value */
+       p->filter_offset = fdp[idx].d_offset;
+
+       /*                     n-1
+        *                     ---    dispersion(i)
+        * filter_dispersion =  \     -------------
+        *                      /       (i+1)
+        *                     ---     2
+        *                     i=0
+        */
+       wavg = 0;
+       sum = 0;
+       for (i = 0; i < NUM_DATAPOINTS; i++) {
+               sum += dispersion(&fdp[idx]) / (2 << i);
+               wavg += fdp[idx].d_offset;
+               idx = (idx - 1) & (NUM_DATAPOINTS - 1);
+       }
+       wavg /= NUM_DATAPOINTS;
+       p->filter_dispersion = sum;
+#endif
+
        /*                  +-----                 -----+ ^ 1/2
         *                  |       n-1                 |
         *                  |       ---                 |
@@ -610,13 +677,13 @@ filter_datapoints(peer_t *p)
         */
        sum = 0;
        for (i = 0; i < NUM_DATAPOINTS; i++) {
-               sum += SQUARE(wavg - p->filter_datapoint[i].d_offset);
+               sum += SQUARE(wavg - fdp[i].d_offset);
        }
        sum = SQRT(sum / NUM_DATAPOINTS);
        p->filter_jitter = sum > G_precision_sec ? sum : G_precision_sec;
 
-       VERB3 bb_error_msg("filter offset:%+f(corr:%e) disp:%f jitter:%f",
-                       p->filter_offset, x,
+       VERB4 bb_error_msg("filter offset:%+f disp:%f jitter:%f",
+                       p->filter_offset,
                        p->filter_dispersion,
                        p->filter_jitter);
 }
@@ -627,6 +694,18 @@ reset_peer_stats(peer_t *p, double offset)
        int i;
        bool small_ofs = fabs(offset) < 16 * STEP_THRESHOLD;
 
+       /* Used to set p->filter_datapoint[i].d_dispersion = MAXDISP
+        * and clear reachable bits, but this proved to be too agressive:
+        * after step (tested with suspinding laptop for ~30 secs),
+        * this caused all previous data to be considered invalid,
+        * making us needing to collect full ~8 datapoins per peer
+        * after step in order to start trusting them.
+        * In turn, this was making poll interval decrease even after
+        * step was done. (Poll interval decreases already before step
+        * in this scenario, because we see large offsets and end up with
+        * no good peer to select).
+        */
+
        for (i = 0; i < NUM_DATAPOINTS; i++) {
                if (small_ofs) {
                        p->filter_datapoint[i].d_recv_time += offset;
@@ -640,17 +719,17 @@ reset_peer_stats(peer_t *p, double offset)
                } else {
                        p->filter_datapoint[i].d_recv_time  = G.cur_time;
                        p->filter_datapoint[i].d_offset     = 0;
-                       p->filter_datapoint[i].d_dispersion = MAXDISP;
+                       /*p->filter_datapoint[i].d_dispersion = MAXDISP;*/
                }
        }
        if (small_ofs) {
                p->lastpkt_recv_time += offset;
        } else {
-               p->reachable_bits = 0;
+               /*p->reachable_bits = 0;*/
                p->lastpkt_recv_time = G.cur_time;
        }
        filter_datapoints(p); /* recalc p->filter_xxx */
-       VERB5 bb_error_msg("%s->lastpkt_recv_time=%f", p->p_dotted, p->lastpkt_recv_time);
+       VERB6 bb_error_msg("%s->lastpkt_recv_time=%f", p->p_dotted, p->lastpkt_recv_time);
 }
 
 static void
@@ -732,6 +811,12 @@ send_query_to_peer(peer_t *p)
                free(local_lsa);
        }
 
+       /* Emit message _before_ attempted send. Think of a very short
+        * roundtrip networks: we need to go back to recv loop ASAP,
+        * to reduce delay. Printing messages after send works against that.
+        */
+       VERB1 bb_error_msg("sending query to %s", p->p_dotted);
+
        /*
         * Send out a random 64-bit number as our transmit time.  The NTP
         * server will copy said number into the originate field on the
@@ -749,23 +834,34 @@ send_query_to_peer(peer_t *p)
        p->p_xmt_msg.m_xmttime.fractionl = random();
        p->p_xmttime = gettime1900d();
 
+       /* Were doing it only if sendto worked, but
+        * loss of sync detection needs reachable_bits updated
+        * even if sending fails *locally*:
+        * "network is unreachable" because cable was pulled?
+        * We still need to declare "unsync" if this condition persists.
+        */
+       p->reachable_bits <<= 1;
+
        if (do_sendto(p->p_fd, /*from:*/ NULL, /*to:*/ &p->p_lsa->u.sa, /*addrlen:*/ p->p_lsa->len,
                        &p->p_xmt_msg, NTP_MSGSIZE_NOAUTH) == -1
        ) {
                close(p->p_fd);
                p->p_fd = -1;
+               /*
+                * We know that we sent nothing.
+                * We can retry *soon* without fearing
+                * that we are flooding the peer.
+                */
                set_next(p, RETRY_INTERVAL);
                return;
        }
 
-       p->reachable_bits <<= 1;
-       VERB1 bb_error_msg("sent query to %s", p->p_dotted);
        set_next(p, RESPONSE_INTERVAL);
 }
 
 
 /* Note that there is no provision to prevent several run_scripts
- * to be done in quick succession. In fact, it happens rather often
+ * to be started in quick succession. In fact, it happens rather often
  * if initial syncronization results in a step.
  * You will see "step" and then "stratum" script runs, sometimes
  * as close as only 0.002 seconds apart.
@@ -776,6 +872,8 @@ static void run_script(const char *action, double offset)
        char *argv[3];
        char *env1, *env2, *env3, *env4;
 
+       G.last_script_run = G.cur_time;
+
        if (!G.script_name)
                return;
 
@@ -812,8 +910,6 @@ static void run_script(const char *action, double offset)
        free(env2);
        free(env3);
        free(env4);
-
-       G.last_script_run = G.cur_time;
 }
 
 static NOINLINE void
@@ -833,15 +929,20 @@ step_time(double offset)
 
        VERB2 {
                tval = tvc.tv_sec;
-               strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", localtime(&tval));
+               strftime_YYYYMMDDHHMMSS(buf, sizeof(buf), &tval);
                bb_error_msg("current time is %s.%06u", buf, (unsigned)tvc.tv_usec);
        }
        tval = tvn.tv_sec;
-       strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", localtime(&tval));
+       strftime_YYYYMMDDHHMMSS(buf, sizeof(buf), &tval);
        bb_error_msg("setting time to %s.%06u (offset %+fs)", buf, (unsigned)tvn.tv_usec, offset);
 
        /* Correct various fields which contain time-relative values: */
 
+       /* Globals: */
+       G.cur_time += offset;
+       G.last_update_recv_time += offset;
+       G.last_script_run += offset;
+
        /* p->lastpkt_recv_time, p->next_action_time and such: */
        for (item = G.ntp_peers; item != NULL; item = item->link) {
                peer_t *pp = (peer_t *) item->data;
@@ -849,11 +950,16 @@ step_time(double offset)
                //bb_error_msg("offset:%+f pp->next_action_time:%f -> %f",
                //      offset, pp->next_action_time, pp->next_action_time + offset);
                pp->next_action_time += offset;
+               if (pp->p_fd >= 0) {
+                       /* We wait for reply from this peer too.
+                        * But due to step we are doing, reply's data is no longer
+                        * useful (in fact, it'll be bogus). Stop waiting for it.
+                        */
+                       close(pp->p_fd);
+                       pp->p_fd = -1;
+                       set_next(pp, RETRY_INTERVAL);
+               }
        }
-       /* Globals: */
-       G.cur_time += offset;
-       G.last_update_recv_time += offset;
-       G.last_script_run += offset;
 }
 
 
@@ -895,20 +1001,20 @@ fit(peer_t *p, double rd)
 {
        if ((p->reachable_bits & (p->reachable_bits-1)) == 0) {
                /* One or zero bits in reachable_bits */
-               VERB3 bb_error_msg("peer %s unfit for selection: unreachable", p->p_dotted);
+               VERB4 bb_error_msg("peer %s unfit for selection: unreachable", p->p_dotted);
                return 0;
        }
 #if 0 /* we filter out such packets earlier */
        if ((p->lastpkt_status & LI_ALARM) == LI_ALARM
         || p->lastpkt_stratum >= MAXSTRAT
        ) {
-               VERB3 bb_error_msg("peer %s unfit for selection: bad status/stratum", p->p_dotted);
+               VERB4 bb_error_msg("peer %s unfit for selection: bad status/stratum", p->p_dotted);
                return 0;
        }
 #endif
        /* rd is root_distance(p) */
        if (rd > MAXDIST + FREQ_TOLERANCE * (1 << G.poll_exp)) {
-               VERB3 bb_error_msg("peer %s unfit for selection: root distance too high", p->p_dotted);
+               VERB4 bb_error_msg("peer %s unfit for selection: root distance too high", p->p_dotted);
                return 0;
        }
 //TODO
@@ -948,7 +1054,7 @@ select_and_cluster(void)
                        continue;
                }
 
-               VERB4 bb_error_msg("interval: [%f %f %f] %s",
+               VERB5 bb_error_msg("interval: [%f %f %f] %s",
                                offset - rd,
                                offset,
                                offset + rd,
@@ -973,7 +1079,7 @@ select_and_cluster(void)
        }
        num_candidates = num_points / 3;
        if (num_candidates == 0) {
-               VERB3 bb_error_msg("no valid datapoints, no peer selected");
+               VERB3 bb_error_msg("no valid datapoints%s", ", no peer selected");
                return NULL;
        }
 //TODO: sorting does not seem to be done in reference code
@@ -1031,12 +1137,13 @@ select_and_cluster(void)
                        break;
                num_falsetickers++;
                if (num_falsetickers * 2 >= num_candidates) {
-                       VERB3 bb_error_msg("too many falsetickers:%d (candidates:%d), no peer selected",
-                                       num_falsetickers, num_candidates);
+                       VERB3 bb_error_msg("falsetickers:%d, candidates:%d%s",
+                                       num_falsetickers, num_candidates,
+                                       ", no peer selected");
                        return NULL;
                }
        }
-       VERB3 bb_error_msg("selected interval: [%f, %f]; candidates:%d falsetickers:%d",
+       VERB4 bb_error_msg("selected interval: [%f, %f]; candidates:%d falsetickers:%d",
                        low, high, num_candidates, num_falsetickers);
 
        /* Clustering */
@@ -1054,7 +1161,7 @@ select_and_cluster(void)
                survivor[num_survivors].p = p;
                /* x.opt_rd == root_distance(p); */
                survivor[num_survivors].metric = MAXDIST * p->lastpkt_stratum + point[i].opt_rd;
-               VERB4 bb_error_msg("survivor[%d] metric:%f peer:%s",
+               VERB5 bb_error_msg("survivor[%d] metric:%f peer:%s",
                        num_survivors, survivor[num_survivors].metric, p->p_dotted);
                num_survivors++;
        }
@@ -1064,8 +1171,9 @@ select_and_cluster(void)
         * is acceptable.
         */
        if (num_survivors < MIN_SELECTED) {
-               VERB3 bb_error_msg("num_survivors %d < %d, no peer selected",
-                               num_survivors, MIN_SELECTED);
+               VERB3 bb_error_msg("survivors:%d%s",
+                               num_survivors,
+                               ", no peer selected");
                return NULL;
        }
 
@@ -1085,7 +1193,7 @@ select_and_cluster(void)
                double min_jitter = min_jitter;
 
                if (num_survivors <= MIN_CLUSTERED) {
-                       VERB3 bb_error_msg("num_survivors %d <= %d, not discarding more",
+                       VERB4 bb_error_msg("num_survivors %d <= %d, not discarding more",
                                        num_survivors, MIN_CLUSTERED);
                        break;
                }
@@ -1111,11 +1219,11 @@ select_and_cluster(void)
                                max_selection_jitter = selection_jitter_sq;
                                max_idx = i;
                        }
-                       VERB5 bb_error_msg("survivor %d selection_jitter^2:%f",
+                       VERB6 bb_error_msg("survivor %d selection_jitter^2:%f",
                                        i, selection_jitter_sq);
                }
                max_selection_jitter = SQRT(max_selection_jitter / num_survivors);
-               VERB4 bb_error_msg("max_selection_jitter (at %d):%f min_jitter:%f",
+               VERB5 bb_error_msg("max_selection_jitter (at %d):%f min_jitter:%f",
                                max_idx, max_selection_jitter, min_jitter);
 
                /* If the maximum selection jitter is less than the
@@ -1124,7 +1232,7 @@ select_and_cluster(void)
                 * as well stop.
                 */
                if (max_selection_jitter < min_jitter) {
-                       VERB3 bb_error_msg("max_selection_jitter:%f < min_jitter:%f, num_survivors:%d, not discarding more",
+                       VERB4 bb_error_msg("max_selection_jitter:%f < min_jitter:%f, num_survivors:%d, not discarding more",
                                        max_selection_jitter, min_jitter, num_survivors);
                        break;
                }
@@ -1132,7 +1240,7 @@ select_and_cluster(void)
                /* Delete survivor[max_idx] from the list
                 * and go around again.
                 */
-               VERB5 bb_error_msg("dropping survivor %d", max_idx);
+               VERB6 bb_error_msg("dropping survivor %d", max_idx);
                num_survivors--;
                while (max_idx < num_survivors) {
                        survivor[max_idx] = survivor[max_idx + 1];
@@ -1174,7 +1282,7 @@ select_and_cluster(void)
                /* Starting from 1 is ok here */
                for (i = 1; i < num_survivors; i++) {
                        if (G.last_update_peer == survivor[i].p) {
-                               VERB4 bb_error_msg("keeping old synced peer");
+                               VERB5 bb_error_msg("keeping old synced peer");
                                p = G.last_update_peer;
                                goto keep_old;
                        }
@@ -1182,7 +1290,7 @@ select_and_cluster(void)
        }
        G.last_update_peer = p;
  keep_old:
-       VERB3 bb_error_msg("selected peer %s filter_offset:%+f age:%f",
+       VERB4 bb_error_msg("selected peer %s filter_offset:%+f age:%f",
                        p->p_dotted,
                        p->filter_offset,
                        G.cur_time - p->lastpkt_recv_time
@@ -1201,7 +1309,7 @@ set_new_values(int disc_state, double offset, double recv_time)
         * of the last clock filter sample, which must be earlier than
         * the current time.
         */
-       VERB3 bb_error_msg("disc_state=%d last update offset=%f recv_time=%f",
+       VERB4 bb_error_msg("disc_state=%d last update offset=%f recv_time=%f",
                        disc_state, offset, recv_time);
        G.discipline_state = disc_state;
        G.last_update_offset = offset;
@@ -1239,8 +1347,8 @@ update_local_clock(peer_t *p)
         * an old sample or the same sample twice.
         */
        if (recv_time <= G.last_update_recv_time) {
-               VERB3 bb_error_msg("same or older datapoint: %f >= %f, not using it",
-                               G.last_update_recv_time, recv_time);
+               VERB3 bb_error_msg("update from %s: same or older datapoint, not using it",
+                       p->p_dotted);
                return 0; /* "leave poll interval as is" */
        }
 
@@ -1256,7 +1364,7 @@ update_local_clock(peer_t *p)
        if (G.discipline_state == STATE_FREQ) {
                /* Ignore updates until the stepout threshold */
                if (since_last_update < WATCH_THRESHOLD) {
-                       VERB3 bb_error_msg("measuring drift, datapoint ignored, %f sec remains",
+                       VERB4 bb_error_msg("measuring drift, datapoint ignored, %f sec remains",
                                        WATCH_THRESHOLD - since_last_update);
                        return 0; /* "leave poll interval as is" */
                }
@@ -1270,10 +1378,21 @@ update_local_clock(peer_t *p)
         * offset exceeds the step threshold and when it does not.
         */
        if (abs_offset > STEP_THRESHOLD) {
+#if 0
+               double remains;
+
+// This "spike state" seems to be useless, peer selection already drops
+// occassional "bad" datapoints. If we are here, there were _many_
+// large offsets. When a few first large offsets are seen,
+// we end up in "no valid datapoints, no peer selected" state.
+// Only when enough of them are seen (which means it's not a fluke),
+// we end up here. Looks like _our_ clock is off.
                switch (G.discipline_state) {
                case STATE_SYNC:
                        /* The first outlyer: ignore it, switch to SPIK state */
-                       VERB3 bb_error_msg("offset:%+f - spike detected", offset);
+                       VERB3 bb_error_msg("update from %s: offset:%+f, spike%s",
+                               p->p_dotted, offset,
+                               "");
                        G.discipline_state = STATE_SPIK;
                        return -1; /* "decrease poll interval" */
 
@@ -1281,13 +1400,16 @@ update_local_clock(peer_t *p)
                        /* Ignore succeeding outlyers until either an inlyer
                         * is found or the stepout threshold is exceeded.
                         */
-                       if (since_last_update < WATCH_THRESHOLD) {
-                               VERB3 bb_error_msg("spike detected, datapoint ignored, %f sec remains",
-                                               WATCH_THRESHOLD - since_last_update);
+                       remains = WATCH_THRESHOLD - since_last_update;
+                       if (remains > 0) {
+                               VERB3 bb_error_msg("update from %s: offset:%+f, spike%s",
+                                       p->p_dotted, offset,
+                                       ", datapoint ignored");
                                return -1; /* "decrease poll interval" */
                        }
                        /* fall through: we need to step */
                } /* switch */
+#endif
 
                /* Step the time and clamp down the poll interval.
                 *
@@ -1310,7 +1432,7 @@ update_local_clock(peer_t *p)
                 * is always suppressed, even at the longer poll
                 * intervals.
                 */
-               VERB3 bb_error_msg("stepping time by %+f; poll_exp=MINPOLL", offset);
+               VERB4 bb_error_msg("stepping time by %+f; poll_exp=MINPOLL", offset);
                step_time(offset);
                if (option_mask32 & OPT_q) {
                        /* We were only asked to set time once. Done. */
@@ -1323,18 +1445,21 @@ update_local_clock(peer_t *p)
 
                run_script("step", offset);
 
+               recv_time += offset;
+
 #if USING_INITIAL_FREQ_ESTIMATION
                if (G.discipline_state == STATE_NSET) {
                        set_new_values(STATE_FREQ, /*offset:*/ 0, recv_time);
                        return 1; /* "ok to increase poll interval" */
                }
 #endif
-               set_new_values(STATE_SYNC, /*offset:*/ 0, recv_time);
+               abs_offset = offset = 0;
+               set_new_values(STATE_SYNC, offset, recv_time);
 
        } else { /* abs_offset <= STEP_THRESHOLD */
 
                if (G.poll_exp < MINPOLL && G.initial_poll_complete) {
-                       VERB3 bb_error_msg("small offset:%+f, disabling burst mode", offset);
+                       VERB4 bb_error_msg("small offset:%+f, disabling burst mode", offset);
                        G.polladj_count = 0;
                        G.poll_exp = MINPOLL;
                }
@@ -1345,9 +1470,6 @@ update_local_clock(peer_t *p)
                etemp = SQUARE(G.discipline_jitter);
                dtemp = SQUARE(offset - G.last_update_offset);
                G.discipline_jitter = SQRT(etemp + (dtemp - etemp) / AVG);
-               if (G.discipline_jitter < G_precision_sec)
-                       G.discipline_jitter = G_precision_sec;
-               VERB3 bb_error_msg("discipline jitter=%f", G.discipline_jitter);
 
                switch (G.discipline_state) {
                case STATE_NSET:
@@ -1366,7 +1488,7 @@ update_local_clock(peer_t *p)
 #else
                        set_new_values(STATE_SYNC, offset, recv_time);
 #endif
-                       VERB3 bb_error_msg("transitioning to FREQ, datapoint ignored");
+                       VERB4 bb_error_msg("transitioning to FREQ, datapoint ignored");
                        return 0; /* "leave poll interval as is" */
 
 #if 0 /* this is dead code for now */
@@ -1424,6 +1546,10 @@ update_local_clock(peer_t *p)
                }
        }
 
+       if (G.discipline_jitter < G_precision_sec)
+               G.discipline_jitter = G_precision_sec;
+       G.offset_to_jitter_ratio = abs_offset / G.discipline_jitter;
+
        G.reftime = G.cur_time;
        G.ntp_status = p->lastpkt_status;
        G.refid = p->lastpkt_refid;
@@ -1431,11 +1557,11 @@ update_local_clock(peer_t *p)
        dtemp = p->filter_jitter; // SQRT(SQUARE(p->filter_jitter) + SQUARE(G.cluster_jitter));
        dtemp += MAXD(p->filter_dispersion + FREQ_TOLERANCE * (G.cur_time - p->lastpkt_recv_time) + abs_offset, MINDISP);
        G.rootdisp = p->lastpkt_rootdisp + dtemp;
-       VERB3 bb_error_msg("updating leap/refid/reftime/rootdisp from peer %s", p->p_dotted);
+       VERB4 bb_error_msg("updating leap/refid/reftime/rootdisp from peer %s", p->p_dotted);
 
        /* We are in STATE_SYNC now, but did not do adjtimex yet.
         * (Any other state does not reach this, they all return earlier)
-        * By this time, freq_drift and G.last_update_offset are set
+        * By this time, freq_drift and offset are set
         * to values suitable for adjtimex.
         */
 #if !USING_KERNEL_PLL_LOOP
@@ -1451,18 +1577,18 @@ update_local_clock(peer_t *p)
        dtemp = SQUARE(dtemp);
        G.discipline_wander = SQRT(etemp + (dtemp - etemp) / AVG);
 
-       VERB3 bb_error_msg("discipline freq_drift=%.9f(int:%ld corr:%e) wander=%f",
+       VERB4 bb_error_msg("discipline freq_drift=%.9f(int:%ld corr:%e) wander=%f",
                        G.discipline_freq_drift,
                        (long)(G.discipline_freq_drift * 65536e6),
                        freq_drift,
                        G.discipline_wander);
 #endif
-       VERB3 {
+       VERB4 {
                memset(&tmx, 0, sizeof(tmx));
                if (adjtimex(&tmx) < 0)
                        bb_perror_msg_and_die("adjtimex");
-               VERB3 bb_error_msg("p adjtimex freq:%ld offset:%+ld constant:%ld status:0x%x",
-                               tmx.freq, tmx.offset, tmx.constant, tmx.status);
+               bb_error_msg("p adjtimex freq:%ld offset:%+ld status:0x%x tc:%ld",
+                               tmx.freq, tmx.offset, tmx.status, tmx.constant);
        }
 
        memset(&tmx, 0, sizeof(tmx));
@@ -1474,40 +1600,42 @@ update_local_clock(peer_t *p)
        tmx.modes = ADJ_FREQUENCY | ADJ_OFFSET;
        /* 65536 is one ppm */
        tmx.freq = G.discipline_freq_drift * 65536e6;
-       tmx.offset = G.last_update_offset * 1000000; /* usec */
 #endif
        tmx.modes = ADJ_OFFSET | ADJ_STATUS | ADJ_TIMECONST;// | ADJ_MAXERROR | ADJ_ESTERROR;
-       tmx.offset = (G.last_update_offset * 1000000); /* usec */
-                       /* + (G.last_update_offset < 0 ? -0.5 : 0.5) - too small to bother */
+       tmx.offset = (offset * 1000000); /* usec */
        tmx.status = STA_PLL;
        if (G.ntp_status & LI_PLUSSEC)
                tmx.status |= STA_INS;
        if (G.ntp_status & LI_MINUSSEC)
                tmx.status |= STA_DEL;
+
        tmx.constant = G.poll_exp - 4;
-       //tmx.esterror = (u_int32)(clock_jitter * 1e6);
-       //tmx.maxerror = (u_int32)((sys_rootdelay / 2 + sys_rootdisp) * 1e6);
+       /* EXPERIMENTAL.
+        * The below if statement should be unnecessary, but...
+        * It looks like Linux kernel's PLL is far too gentle in changing
+        * tmx.freq in response to clock offset. Offset keeps growing
+        * and eventually we fall back to smaller poll intervals.
+        * We can make correction more agressive (about x2) by supplying
+        * PLL time constant which is one less than the real one.
+        * To be on a safe side, let's do it only if offset is significantly
+        * larger than jitter.
+        */
+       if (tmx.constant > 0 && G.offset_to_jitter_ratio >= TIMECONST_HACK_GATE)
+               tmx.constant--;
+
+       //tmx.esterror = (uint32_t)(clock_jitter * 1e6);
+       //tmx.maxerror = (uint32_t)((sys_rootdelay / 2 + sys_rootdisp) * 1e6);
        rc = adjtimex(&tmx);
        if (rc < 0)
                bb_perror_msg_and_die("adjtimex");
        /* NB: here kernel returns constant == G.poll_exp, not == G.poll_exp - 4.
         * Not sure why. Perhaps it is normal.
         */
-       VERB3 bb_error_msg("adjtimex:%d freq:%ld offset:%+ld constant:%ld status:0x%x",
-                               rc, tmx.freq, tmx.offset, tmx.constant, tmx.status);
-#if 0
-       VERB3 {
-               /* always gives the same output as above msg */
-               memset(&tmx, 0, sizeof(tmx));
-               if (adjtimex(&tmx) < 0)
-                       bb_perror_msg_and_die("adjtimex");
-               VERB3 bb_error_msg("c adjtimex freq:%ld offset:%+ld constant:%ld status:0x%x",
-                               tmx.freq, tmx.offset, tmx.constant, tmx.status);
-       }
-#endif
+       VERB4 bb_error_msg("adjtimex:%d freq:%ld offset:%+ld status:0x%x",
+                               rc, tmx.freq, tmx.offset, tmx.status);
        G.kernel_freq_drift = tmx.freq / 65536;
-       VERB2 bb_error_msg("update peer:%s, offset:%+f, jitter:%f, clock drift:%+.3f ppm",
-                       p->p_dotted, G.last_update_offset, G.discipline_jitter, (double)tmx.freq / 65536);
+       VERB2 bb_error_msg("update from:%s offset:%+f jitter:%f clock drift:%+.3fppm tc:%d",
+                       p->p_dotted, offset, G.discipline_jitter, (double)tmx.freq / 65536, (int)tmx.constant);
 
        return 1; /* "ok to increase poll interval" */
 }
@@ -1525,7 +1653,7 @@ retry_interval(void)
        interval = RETRY_INTERVAL;
        r = random();
        interval += r % (unsigned)(RETRY_INTERVAL / 4);
-       VERB3 bb_error_msg("chose retry interval:%u", interval);
+       VERB4 bb_error_msg("chose retry interval:%u", interval);
        return interval;
 }
 static unsigned
@@ -1538,7 +1666,7 @@ poll_interval(int exponent)
        interval = 1 << exponent;
        r = random();
        interval += ((r & (interval-1)) >> 4) + ((r >> 8) & 1); /* + 1/16 of interval, max */
-       VERB3 bb_error_msg("chose poll interval:%u (poll_exp:%d exp:%d)", interval, G.poll_exp, exponent);
+       VERB4 bb_error_msg("chose poll interval:%u (poll_exp:%d exp:%d)", interval, G.poll_exp, exponent);
        return interval;
 }
 static NOINLINE void
@@ -1548,10 +1676,13 @@ recv_and_process_peer_pkt(peer_t *p)
        ssize_t     size;
        msg_t       msg;
        double      T1, T2, T3, T4;
+       double      dv, offset;
        unsigned    interval;
        datapoint_t *datapoint;
        peer_t      *q;
 
+       offset = 0;
+
        /* We can recvfrom here and check from.IP, but some multihomed
         * ntp servers reply from their *other IP*.
         * TODO: maybe we should check at least what we can: from.port == 123?
@@ -1566,22 +1697,30 @@ recv_and_process_peer_pkt(peer_t *p)
                ) {
 //TODO: always do this?
                        interval = retry_interval();
-                       goto set_next_and_close_sock;
+                       goto set_next_and_ret;
                }
                xfunc_die();
        }
 
        if (size != NTP_MSGSIZE_NOAUTH && size != NTP_MSGSIZE) {
                bb_error_msg("malformed packet received from %s", p->p_dotted);
-               goto bail;
+               return;
        }
 
        if (msg.m_orgtime.int_partl != p->p_xmt_msg.m_xmttime.int_partl
         || msg.m_orgtime.fractionl != p->p_xmt_msg.m_xmttime.fractionl
        ) {
-               goto bail;
+               /* Somebody else's packet */
+               return;
        }
 
+       /* We do not expect any more packets from this peer for now.
+        * Closing the socket informs kernel about it.
+        * We open a new socket when we send a new query.
+        */
+       close(p->p_fd);
+       p->p_fd = -1;
+
        if ((msg.m_status & LI_ALARM) == LI_ALARM
         || msg.m_stratum == 0
         || msg.m_stratum > NTP_MAXSTRATUM
@@ -1589,9 +1728,8 @@ recv_and_process_peer_pkt(peer_t *p)
 // TODO: stratum 0 responses may have commands in 32-bit m_refid field:
 // "DENY", "RSTR" - peer does not like us at all
 // "RATE" - peer is overloaded, reduce polling freq
-               interval = poll_interval(0);
-               bb_error_msg("reply from %s: not synced, next query in %us", p->p_dotted, interval);
-               goto set_next_and_close_sock;
+               bb_error_msg("reply from %s: peer is unsynced", p->p_dotted);
+               goto pick_normal_interval;
        }
 
 //     /* Verify valid root distance */
@@ -1624,41 +1762,51 @@ recv_and_process_peer_pkt(peer_t *p)
        T4 = G.cur_time;
 
        p->lastpkt_recv_time = T4;
+       VERB6 bb_error_msg("%s->lastpkt_recv_time=%f", p->p_dotted, p->lastpkt_recv_time);
 
-       VERB5 bb_error_msg("%s->lastpkt_recv_time=%f", p->p_dotted, p->lastpkt_recv_time);
-       p->datapoint_idx = p->reachable_bits ? (p->datapoint_idx + 1) % NUM_DATAPOINTS : 0;
-       datapoint = &p->filter_datapoint[p->datapoint_idx];
-       datapoint->d_recv_time = T4;
-       datapoint->d_offset    = ((T2 - T1) + (T3 - T4)) / 2;
        /* The delay calculation is a special case. In cases where the
         * server and client clocks are running at different rates and
         * with very fast networks, the delay can appear negative. In
         * order to avoid violating the Principle of Least Astonishment,
         * the delay is clamped not less than the system precision.
         */
+       dv = p->lastpkt_delay;
        p->lastpkt_delay = (T4 - T1) - (T3 - T2);
        if (p->lastpkt_delay < G_precision_sec)
                p->lastpkt_delay = G_precision_sec;
+       /*
+        * If this packet's delay is much bigger than the last one,
+        * it's better to just ignore it than use its much less precise value.
+        */
+       if (p->reachable_bits && p->lastpkt_delay > dv * BAD_DELAY_GROWTH) {
+               bb_error_msg("reply from %s: delay %f is too high, ignoring", p->p_dotted, p->lastpkt_delay);
+               goto pick_normal_interval;
+       }
+
+       p->datapoint_idx = p->reachable_bits ? (p->datapoint_idx + 1) % NUM_DATAPOINTS : 0;
+       datapoint = &p->filter_datapoint[p->datapoint_idx];
+       datapoint->d_recv_time = T4;
+       datapoint->d_offset    = offset = ((T2 - T1) + (T3 - T4)) / 2;
        datapoint->d_dispersion = LOG2D(msg.m_precision_exp) + G_precision_sec;
        if (!p->reachable_bits) {
                /* 1st datapoint ever - replicate offset in every element */
                int i;
-               for (i = 1; i < NUM_DATAPOINTS; i++) {
-                       p->filter_datapoint[i].d_offset = datapoint->d_offset;
+               for (i = 0; i < NUM_DATAPOINTS; i++) {
+                       p->filter_datapoint[i].d_offset = offset;
                }
        }
 
        p->reachable_bits |= 1;
        if ((MAX_VERBOSE && G.verbose) || (option_mask32 & OPT_w)) {
-               bb_error_msg("reply from %s: reach 0x%02x offset %+f delay %f status 0x%02x strat %d refid 0x%08x rootdelay %f",
+               bb_error_msg("reply from %s: offset:%+f delay:%f status:0x%02x strat:%d refid:0x%08x rootdelay:%f reach:0x%02x",
                        p->p_dotted,
-                       p->reachable_bits,
-                       datapoint->d_offset,
+                       offset,
                        p->lastpkt_delay,
                        p->lastpkt_status,
                        p->lastpkt_stratum,
                        p->lastpkt_refid,
-                       p->lastpkt_rootdelay
+                       p->lastpkt_rootdelay,
+                       p->reachable_bits
                        /* not shown: m_ppoll, m_precision_exp, m_rootdisp,
                         * m_reftime, m_orgtime, m_rectime, m_xmttime
                         */
@@ -1677,7 +1825,7 @@ recv_and_process_peer_pkt(peer_t *p)
                         * drop poll interval one step down.
                         */
                        if (fabs(q->filter_offset) >= POLLDOWN_OFFSET) {
-                               VERB3 bb_error_msg("offset:%+f > POLLDOWN_OFFSET", q->filter_offset);
+                               VERB4 bb_error_msg("offset:%+f > POLLDOWN_OFFSET", q->filter_offset);
                                goto poll_down;
                        }
                }
@@ -1691,14 +1839,7 @@ recv_and_process_peer_pkt(peer_t *p)
                 * is increased, otherwise it is decreased. A bit of hysteresis
                 * helps calm the dance. Works best using burst mode.
                 */
-               VERB4 if (rc > 0) {
-                       bb_error_msg("offset:%+f POLLADJ_GATE*discipline_jitter:%f poll:%s",
-                               q->filter_offset, POLLADJ_GATE * G.discipline_jitter,
-                               fabs(q->filter_offset) < POLLADJ_GATE * G.discipline_jitter
-                                       ? "grows" : "falls"
-                       );
-               }
-               if (rc > 0 && fabs(q->filter_offset) < POLLADJ_GATE * G.discipline_jitter) {
+               if (rc > 0 && G.offset_to_jitter_ratio <= POLLADJ_GATE) {
                        /* was += G.poll_exp but it is a bit
                         * too optimistic for my taste at high poll_exp's */
                        G.polladj_count += MINPOLL;
@@ -1706,11 +1847,11 @@ recv_and_process_peer_pkt(peer_t *p)
                                G.polladj_count = 0;
                                if (G.poll_exp < MAXPOLL) {
                                        G.poll_exp++;
-                                       VERB3 bb_error_msg("polladj: discipline_jitter:%f ++poll_exp=%d",
+                                       VERB4 bb_error_msg("polladj: discipline_jitter:%f ++poll_exp=%d",
                                                        G.discipline_jitter, G.poll_exp);
                                }
                        } else {
-                               VERB3 bb_error_msg("polladj: incr:%d", G.polladj_count);
+                               VERB4 bb_error_msg("polladj: incr:%d", G.polladj_count);
                        }
                } else {
                        G.polladj_count -= G.poll_exp * 2;
@@ -1732,28 +1873,35 @@ recv_and_process_peer_pkt(peer_t *p)
                                                if (pp->p_fd < 0)
                                                        pp->next_action_time -= (1 << G.poll_exp);
                                        }
-                                       VERB3 bb_error_msg("polladj: discipline_jitter:%f --poll_exp=%d",
+                                       VERB4 bb_error_msg("polladj: discipline_jitter:%f --poll_exp=%d",
                                                        G.discipline_jitter, G.poll_exp);
                                }
                        } else {
-                               VERB3 bb_error_msg("polladj: decr:%d", G.polladj_count);
+                               VERB4 bb_error_msg("polladj: decr:%d", G.polladj_count);
                        }
                }
        }
 
        /* Decide when to send new query for this peer */
+ pick_normal_interval:
        interval = poll_interval(0);
+       if (fabs(offset) >= STEP_THRESHOLD * 8 && interval > BIGOFF_INTERVAL) {
+               /* If we are synced, offsets are less than STEP_THRESHOLD,
+                * or at the very least not much larger than it.
+                * Now we see a largish one.
+                * Either this peer is feeling bad, or packet got corrupted,
+                * or _our_ clock is wrong now and _all_ peers will show similar
+                * largish offsets too.
+                * I observed this with laptop suspend stopping clock.
+                * In any case, it makes sense to make next request soonish:
+                * cases 1 and 2: get a better datapoint,
+                * case 3: allows to resync faster.
+                */
+               interval = BIGOFF_INTERVAL;
+       }
 
- set_next_and_close_sock:
+ set_next_and_ret:
        set_next(p, interval);
-       /* We do not expect any more packets from this peer for now.
-        * Closing the socket informs kernel about it.
-        * We open a new socket when we send a new query.
-        */
-       close(p->p_fd);
-       p->p_fd = -1;
- bail:
-       return;
 }
 
 #if ENABLE_FEATURE_NTPD_SERVER
@@ -1768,10 +1916,10 @@ recv_and_process_client_pkt(void /*int fd*/)
        uint8_t          query_status;
        l_fixedpt_t      query_xmttime;
 
-       to = get_sock_lsa(G.listen_fd);
+       to = get_sock_lsa(G_listen_fd);
        from = xzalloc(to->len);
 
-       size = recv_from_to(G.listen_fd, &msg, sizeof(msg), MSG_DONTWAIT, from, &to->u.sa, to->len);
+       size = recv_from_to(G_listen_fd, &msg, sizeof(msg), MSG_DONTWAIT, from, &to->u.sa, to->len);
        if (size != NTP_MSGSIZE_NOAUTH && size != NTP_MSGSIZE) {
                char *addr;
                if (size < 0) {
@@ -1790,10 +1938,10 @@ recv_and_process_client_pkt(void /*int fd*/)
 
        /* Build a reply packet */
        memset(&msg, 0, sizeof(msg));
-       msg.m_status = G.stratum < MAXSTRAT ? G.ntp_status : LI_ALARM;
+       msg.m_status = G.stratum < MAXSTRAT ? (G.ntp_status & LI_MASK) : LI_ALARM;
        msg.m_status |= (query_status & VERSION_MASK);
        msg.m_status |= ((query_status & MODE_MASK) == MODE_CLIENT) ?
-                        MODE_SERVER : MODE_SYM_PAS;
+                       MODE_SERVER : MODE_SYM_PAS;
        msg.m_stratum = G.stratum;
        msg.m_ppoll = G.poll_exp;
        msg.m_precision_exp = G_precision_exp;
@@ -1814,7 +1962,7 @@ recv_and_process_client_pkt(void /*int fd*/)
 
        /* We reply from the local address packet was sent to,
         * this makes to/from look swapped here: */
-       do_sendto(G.listen_fd,
+       do_sendto(G_listen_fd,
                /*from:*/ &to->u.sa, /*to:*/ from, /*addrlen:*/ to->len,
                &msg, size);
 
@@ -1953,11 +2101,11 @@ static NOINLINE void ntp_init(char **argv)
                logmode = LOGMODE_NONE;
        }
 #if ENABLE_FEATURE_NTPD_SERVER
-       G.listen_fd = -1;
+       G_listen_fd = -1;
        if (opts & OPT_l) {
-               G.listen_fd = create_and_bind_dgram_or_die(NULL, 123);
-               socket_want_pktinfo(G.listen_fd);
-               setsockopt(G.listen_fd, IPPROTO_IP, IP_TOS, &const_IPTOS_LOWDELAY, sizeof(const_IPTOS_LOWDELAY));
+               G_listen_fd = create_and_bind_dgram_or_die(NULL, 123);
+               socket_want_pktinfo(G_listen_fd);
+               setsockopt(G_listen_fd, IPPROTO_IP, IP_TOS, &const_IPTOS_LOWDELAY, sizeof(const_IPTOS_LOWDELAY));
        }
 #endif
        /* I hesitate to set -20 prio. -15 should be high enough for timekeeping */
@@ -2019,6 +2167,8 @@ int ntpd_main(int argc UNUSED_PARAM, char **argv)
         */
        cnt = G.peer_cnt * (INITIAL_SAMPLES + 1);
 
+       write_pidfile(CONFIG_PID_FILE_PATH "/ntpd.pid");
+
        while (!bb_got_signal) {
                llist_t *item;
                unsigned i, j;
@@ -2031,8 +2181,8 @@ int ntpd_main(int argc UNUSED_PARAM, char **argv)
 
                i = 0;
 #if ENABLE_FEATURE_NTPD_SERVER
-               if (G.listen_fd != -1) {
-                       pfd[0].fd = G.listen_fd;
+               if (G_listen_fd != -1) {
+                       pfd[0].fd = G_listen_fd;
                        pfd[0].events = POLLIN;
                        i++;
                }
@@ -2077,16 +2227,33 @@ int ntpd_main(int argc UNUSED_PARAM, char **argv)
                timeout++; /* (nextaction - G.cur_time) rounds down, compensating */
 
                /* Here we may block */
-               VERB2 bb_error_msg("poll %us, sockets:%u, poll interval:%us", timeout, i, 1 << G.poll_exp);
+               VERB2 {
+                       if (i > (ENABLE_FEATURE_NTPD_SERVER && G_listen_fd != -1)) {
+                               /* We wait for at least one reply.
+                                * Poll for it, without wasting time for message.
+                                * Since replies often come under 1 second, this also
+                                * reduces clutter in logs.
+                                */
+                               nfds = poll(pfd, i, 1000);
+                               if (nfds != 0)
+                                       goto did_poll;
+                               if (--timeout <= 0)
+                                       goto did_poll;
+                       }
+                       bb_error_msg("poll:%us sockets:%u interval:%us", timeout, i, 1 << G.poll_exp);
+               }
                nfds = poll(pfd, i, timeout * 1000);
+ did_poll:
                gettime1900d(); /* sets G.cur_time */
                if (nfds <= 0) {
-                       if (G.script_name && G.cur_time - G.last_script_run > 11*60) {
+                       if (!bb_got_signal /* poll wasn't interrupted by a signal */
+                        && G.cur_time - G.last_script_run > 11*60
+                       ) {
                                /* Useful for updating battery-backed RTC and such */
                                run_script("periodic", G.last_update_offset);
                                gettime1900d(); /* sets G.cur_time */
                        }
-                       continue;
+                       goto check_unsync;
                }
 
                /* Process any received packets */
@@ -2117,8 +2284,24 @@ int ntpd_main(int argc UNUSED_PARAM, char **argv)
                                gettime1900d(); /* sets G.cur_time */
                        }
                }
+
+ check_unsync:
+               if (G.ntp_peers && G.stratum != MAXSTRAT) {
+                       for (item = G.ntp_peers; item != NULL; item = item->link) {
+                               peer_t *p = (peer_t *) item->data;
+                               if (p->reachable_bits)
+                                       goto have_reachable_peer;
+                       }
+                       /* No peer responded for last 8 packets, panic */
+                       G.polladj_count = 0;
+                       G.poll_exp = MINPOLL;
+                       G.stratum = MAXSTRAT;
+                       run_script("unsync", 0.0);
+ have_reachable_peer: ;
+               }
        } /* while (!bb_got_signal) */
 
+       remove_pidfile(CONFIG_PID_FILE_PATH "/ntpd.pid");
        kill_myself_with_sig(bb_got_signal);
 }
 
@@ -2249,14 +2432,13 @@ set_freq(double freq) /* frequency update */
                        if (pps_enable) {
                                if (!(pll_status & STA_PPSTIME))
                                        report_event(EVNT_KERN,
-                                           NULL, "PPS enabled");
+                                               NULL, "PPS enabled");
                                ntv.status |= STA_PPSTIME | STA_PPSFREQ;
                        } else {
                                if (pll_status & STA_PPSTIME)
                                        report_event(EVNT_KERN,
-                                           NULL, "PPS disabled");
-                               ntv.status &= ~(STA_PPSTIME |
-                                   STA_PPSFREQ);
+                                               NULL, "PPS disabled");
+                               ntv.status &= ~(STA_PPSTIME | STA_PPSFREQ);
                        }
                        if (sys_leap == LEAP_ADDSECOND)
                                ntv.status |= STA_INS;
@@ -2272,7 +2454,7 @@ set_freq(double freq) /* frequency update */
                if (ntp_adjtime(&ntv) == TIME_ERROR) {
                        if (!(ntv.status & STA_PPSSIGNAL))
                                report_event(EVNT_KERN, NULL,
-                                   "PPS no signal");
+                                               "PPS no signal");
                }
                pll_status = ntv.status;
 #ifdef STA_NANO