ip link: add VLAN support
[oweals/busybox.git] / networking / ntpd.c
index 72e9d0be2161646ba5d6bfd7ff7570b7b219b619..b04391eb5f04397756eb977466b31f7be860cb51 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
@@ -94,6 +95,7 @@
 #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 BAD_DELAY_GROWTH 4     /* drop packet if its delay grew by more than this */
 
 /* Clock discipline parameters and constants */
 
@@ -803,22 +805,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;
        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.
@@ -829,6 +843,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;
 
@@ -865,8 +881,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
@@ -886,11 +900,11 @@ 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: */
@@ -1615,6 +1629,7 @@ recv_and_process_peer_pkt(peer_t *p)
        ssize_t     size;
        msg_t       msg;
        double      T1, T2, T3, T4;
+       double      dv;
        unsigned    interval;
        datapoint_t *datapoint;
        peer_t      *q;
@@ -1664,9 +1679,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: peer is unsynced, next query in %us", p->p_dotted, interval);
-               goto set_next_and_ret;
+               bb_error_msg("reply from %s: peer is unsynced", p->p_dotted);
+               goto pick_normal_interval;
        }
 
 //     /* Verify valid root distance */
@@ -1699,21 +1713,31 @@ recv_and_process_peer_pkt(peer_t *p)
        T4 = G.cur_time;
 
        p->lastpkt_recv_time = T4;
-
        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    = ((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 */
@@ -1810,6 +1834,7 @@ recv_and_process_peer_pkt(peer_t *p)
        }
 
        /* Decide when to send new query for this peer */
+ pick_normal_interval:
        interval = poll_interval(0);
 
  set_next_and_ret:
@@ -1850,10 +1875,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;
@@ -2079,6 +2104,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;
@@ -2156,12 +2183,14 @@ int ntpd_main(int argc UNUSED_PARAM, char **argv)
  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 */
@@ -2192,8 +2221,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);
 }
 
@@ -2324,14 +2369,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;
@@ -2347,7 +2391,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