Changeset 3621 in MondoRescue for branches/3.3/mindi-busybox/networking/ntpd.c


Ignore:
Timestamp:
Dec 20, 2016, 4:07:32 PM (7 years ago)
Author:
Bruno Cornec
Message:

New 3?3 banch for incorporation of latest busybox 1.25. Changing minor version to handle potential incompatibilities.

Location:
branches/3.3
Files:
1 edited
1 copied

Legend:

Unmodified
Added
Removed
  • branches/3.3/mindi-busybox/networking/ntpd.c

    r3232 r3621  
    22 * NTP client/server, based on OpenNTPD 3.9p1
    33 *
    4  * Author: Adam Tkac <vonsch@gmail.com>
     4 * Busybox port author: Adam Tkac (C) 2009 <vonsch@gmail.com>
    55 *
    6  * Licensed under GPLv2, see file LICENSE in this source tree.
     6 * OpenNTPd 3.9p1 copyright holders:
     7 *   Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
     8 *   Copyright (c) 2004 Alexander Guy <alexander.guy@andern.org>
     9 *
     10 * OpenNTPd code is licensed under ISC-style licence:
     11 *
     12 * Permission to use, copy, modify, and distribute this software for any
     13 * purpose with or without fee is hereby granted, provided that the above
     14 * copyright notice and this permission notice appear in all copies.
     15 *
     16 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     17 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     18 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     19 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     20 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
     21 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
     22 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     23 ***********************************************************************
    724 *
    825 * Parts of OpenNTPD clock syncronization code is replaced by
    9  * code which is based on ntp-4.2.6, whuch carries the following
     26 * code which is based on ntp-4.2.6, which carries the following
    1027 * copyright notice:
    1128 *
    12  ***********************************************************************
    13  *                                                                     *
    14  * Copyright (c) University of Delaware 1992-2009                      *
    15  *                                                                     *
    16  * Permission to use, copy, modify, and distribute this software and   *
    17  * its documentation for any purpose with or without fee is hereby     *
    18  * granted, provided that the above copyright notice appears in all    *
    19  * copies and that both the copyright notice and this permission       *
    20  * notice appear in supporting documentation, and that the name        *
    21  * University of Delaware not be used in advertising or publicity      *
    22  * pertaining to distribution of the software without specific,        *
    23  * written prior permission. The University of Delaware makes no       *
    24  * representations about the suitability this software for any         *
    25  * purpose. It is provided "as is" without express or implied          *
    26  * warranty.                                                           *
    27  *                                                                     *
     29 * Copyright (c) University of Delaware 1992-2009
     30 *
     31 * Permission to use, copy, modify, and distribute this software and
     32 * its documentation for any purpose with or without fee is hereby
     33 * granted, provided that the above copyright notice appears in all
     34 * copies and that both the copyright notice and this permission
     35 * notice appear in supporting documentation, and that the name
     36 * University of Delaware not be used in advertising or publicity
     37 * pertaining to distribution of the software without specific,
     38 * written prior permission. The University of Delaware makes no
     39 * representations about the suitability this software for any
     40 * purpose. It is provided "as is" without express or implied warranty.
    2841 ***********************************************************************
    2942 */
    3043
    3144//usage:#define ntpd_trivial_usage
    32 //usage:    "[-dnqNw"IF_FEATURE_NTPD_SERVER("l")"] [-S PROG] [-p PEER]..."
     45//usage:    "[-dnqNw"IF_FEATURE_NTPD_SERVER("l -I IFACE")"] [-S PROG] [-p PEER]..."
    3346//usage:#define ntpd_full_usage "\n\n"
    3447//usage:       "NTP client/server\n"
     
    3851//usage:     "\n    -N  Run at high priority"
    3952//usage:     "\n    -w  Do not set time (only query peers), implies -n"
    40 //usage:    IF_FEATURE_NTPD_SERVER(
    41 //usage:     "\n    -l  Run as server on port 123"
    42 //usage:    )
    4353//usage:     "\n    -S PROG Run PROG after stepping time, stratum change, and every 11 mins"
    4454//usage:     "\n    -p PEER Obtain time from PEER (may be repeated)"
     55//usage:    IF_FEATURE_NTPD_CONF(
     56//usage:     "\n        If -p is not given, 'server HOST' lines"
     57//usage:     "\n        from /etc/ntp.conf are used"
     58//usage:    )
     59//usage:    IF_FEATURE_NTPD_SERVER(
     60//usage:     "\n    -l  Also run as server on port 123"
     61//usage:     "\n    -I IFACE Bind server to IFACE, implies -l"
     62//usage:    )
     63
     64// -l and -p options are not compatible with "standard" ntpd:
     65// it has them as "-l logfile" and "-p pidfile".
     66// -S and -w are not compat either, "standard" ntpd has no such opts.
    4567
    4668#include "libbb.h"
     
    5274# define IPTOS_LOWDELAY 0x10
    5375#endif
    54 #ifndef IP_PKTINFO
    55 # error "Sorry, your kernel has to support IP_PKTINFO"
    56 #endif
    5776
    5877
    5978/* Verbosity control (max level of -dddd options accepted).
    60  * max 5 is very talkative (and bloated). 2 is non-bloated,
     79 * max 6 is very talkative (and bloated). 3 is non-bloated,
    6180 * production level setting.
    6281 */
    63 #define MAX_VERBOSE     2
     82#define MAX_VERBOSE     3
    6483
    6584
     
    91110 * then the time is stepped, all datapoints are discarded,
    92111 * and we go back to steady state.
     112 *
     113 * Made some changes to speed up re-syncing after our clock goes bad
     114 * (tested with suspending my laptop):
     115 * - if largish offset (>= STEP_THRESHOLD == 1 sec) is seen
     116 *   from a peer, schedule next query for this peer soon
     117 *   without drastically lowering poll interval for everybody.
     118 *   This makes us collect enough data for step much faster:
     119 *   e.g. at poll = 10 (1024 secs), step was done within 5 minutes
     120 *   after first reply which indicated that our clock is 14 seconds off.
     121 * - on step, do not discard d_dispersion data of the existing datapoints,
     122 *   do not clear reachable_bits. This prevents discarding first ~8
     123 *   datapoints after the step.
    93124 */
    94125
    95 #define RETRY_INTERVAL  5       /* on error, retry in N secs */
    96 #define RESPONSE_INTERVAL 15    /* wait for reply up to N secs */
    97 #define INITIAL_SAMPLES 4       /* how many samples do we want for init */
    98 
    99 /* Clock discipline parameters and constants */
     126#define INITIAL_SAMPLES    4    /* how many samples do we want for init */
     127#define BAD_DELAY_GROWTH   4    /* drop packet if its delay grew by more than this */
     128
     129#define RETRY_INTERVAL    32    /* on send/recv error, retry in N secs (need to be power of 2) */
     130#define NOREPLY_INTERVAL 512    /* sent, but got no reply: cap next query by this many seconds */
     131#define RESPONSE_INTERVAL 16    /* wait for reply up to N secs */
    100132
    101133/* Step threshold (sec). std ntpd uses 0.128.
    102  * Using exact power of 2 (1/8) results in smaller code */
    103 #define STEP_THRESHOLD  0.125
    104 #define WATCH_THRESHOLD 128     /* stepout threshold (sec). std ntpd uses 900 (11 mins (!)) */
     134 */
     135#define STEP_THRESHOLD     1
     136/* Slew threshold (sec): adjtimex() won't accept offsets larger than this.
     137 * Using exact power of 2 (1/8) results in smaller code
     138 */
     139#define SLEW_THRESHOLD 0.125
     140/* Stepout threshold (sec). std ntpd uses 900 (11 mins (!)) */
     141#define WATCH_THRESHOLD  128
    105142/* NB: set WATCH_THRESHOLD to ~60 when debugging to save time) */
    106143//UNUSED: #define PANIC_THRESHOLD 1000    /* panic threshold (sec) */
     144
     145/*
     146 * If we got |offset| > BIGOFF from a peer, cap next query interval
     147 * for this peer by this many seconds:
     148 */
     149#define BIGOFF          STEP_THRESHOLD
     150#define BIGOFF_INTERVAL (1 << 7) /* 128 s */
    107151
    108152#define FREQ_TOLERANCE  0.000015 /* frequency tolerance (15 PPM) */
    109153#define BURSTPOLL       0       /* initial poll */
    110154#define MINPOLL         5       /* minimum poll interval. std ntpd uses 6 (6: 64 sec) */
    111 /* If offset > discipline_jitter * POLLADJ_GATE, and poll interval is >= 2^BIGPOLL,
    112  * then it is decreased _at once_. (If < 2^BIGPOLL, it will be decreased _eventually_).
     155/*
     156 * If offset > discipline_jitter * POLLADJ_GATE, and poll interval is > 2^BIGPOLL,
     157 * then it is decreased _at once_. (If <= 2^BIGPOLL, it will be decreased _eventually_).
    113158 */
    114 #define BIGPOLL         10      /* 2^10 sec ~= 17 min */
     159#define BIGPOLL         9       /* 2^9 sec ~= 8.5 min */
    115160#define MAXPOLL         12      /* maximum poll interval (12: 1.1h, 17: 36.4h). std ntpd uses 17 */
    116 /* Actively lower poll when we see such big offsets.
    117  * With STEP_THRESHOLD = 0.125, it means we try to sync more aggressively
    118  * if offset increases over ~0.04 sec */
    119 #define POLLDOWN_OFFSET (STEP_THRESHOLD / 3)
     161/*
     162 * Actively lower poll when we see such big offsets.
     163 * With SLEW_THRESHOLD = 0.125, it means we try to sync more aggressively
     164 * if offset increases over ~0.04 sec
     165 */
     166//#define POLLDOWN_OFFSET (SLEW_THRESHOLD / 3)
    120167#define MINDISP         0.01    /* minimum dispersion (sec) */
    121168#define MAXDISP         16      /* maximum dispersion (sec) */
     
    228275    uint8_t          lastpkt_stratum;
    229276    uint8_t          reachable_bits;
    230         /* when to send new query (if p_fd == -1)
    231         * or when receive times out (if p_fd >= 0): */
     277    /* when to send new query (if p_fd == -1)
     278    * or when receive times out (if p_fd >= 0): */
    232279    double           next_action_time;
    233280    double           p_xmttime;
     281    double           p_raw_delay;
     282    /* p_raw_delay is set even by "high delay" packets */
     283    /* lastpkt_delay isn't */
    234284    double           lastpkt_recv_time;
    235285    double           lastpkt_delay;
     
    243293    /* last sent packet: */
    244294    msg_t            p_xmt_msg;
     295    char             p_hostname[1];
    245296} peer_t;
    246297
     
    260311    OPT_S = (1 << 6),
    261312    OPT_l = (1 << 7) * ENABLE_FEATURE_NTPD_SERVER,
     313    OPT_I = (1 << 8) * ENABLE_FEATURE_NTPD_SERVER,
    262314    /* We hijack some bits for other purposes */
    263315    OPT_qq = (1 << 31),
     
    278330#if ENABLE_FEATURE_NTPD_SERVER
    279331    int      listen_fd;
     332    char     *if_name;
    280333# define G_listen_fd (G.listen_fd)
    281334#else
     
    329382#define G_precision_sec  0.002
    330383    uint8_t  stratum;
    331     /* Bool. After set to 1, never goes back to 0: */
    332     smallint initial_poll_complete;
    333384
    334385#define STATE_NSET      0       /* initial state, "nothing is set" */
    335386//#define STATE_FSET    1       /* frequency set from file */
    336 #define STATE_SPIK      2       /* spike detected */
     387//#define STATE_SPIK    2       /* spike detected */
    337388//#define STATE_FREQ    3       /* initial frequency */
    338389#define STATE_SYNC      4       /* clock synchronized (normal operation) */
     
    359410#define G (*ptr_to_globals)
    360411
    361 static const int const_IPTOS_LOWDELAY = IPTOS_LOWDELAY;
    362 
    363412
    364413#define VERB1 if (MAX_VERBOSE && G.verbose)
     
    367416#define VERB4 if (MAX_VERBOSE >= 4 && G.verbose >= 4)
    368417#define VERB5 if (MAX_VERBOSE >= 5 && G.verbose >= 5)
     418#define VERB6 if (MAX_VERBOSE >= 6 && G.verbose >= 6)
    369419
    370420
     
    568618    sum = 0;
    569619    for (i = 0; i < NUM_DATAPOINTS; i++) {
    570         VERB4 {
     620        VERB5 {
    571621            bb_error_msg("datapoint[%d]: off:%f disp:%f(%f) age:%f%s",
    572622                i,
     
    665715    p->filter_jitter = sum > G_precision_sec ? sum : G_precision_sec;
    666716
    667     VERB3 bb_error_msg("filter offset:%+f disp:%f jitter:%f",
     717    VERB4 bb_error_msg("filter offset:%+f disp:%f jitter:%f",
    668718            p->filter_offset,
    669719            p->filter_dispersion,
     
    675725{
    676726    int i;
    677     bool small_ofs = fabs(offset) < 16 * STEP_THRESHOLD;
     727    bool small_ofs = fabs(offset) < STEP_THRESHOLD;
     728
     729    /* Used to set p->filter_datapoint[i].d_dispersion = MAXDISP
     730     * and clear reachable bits, but this proved to be too agressive:
     731     * after step (tested with suspending laptop for ~30 secs),
     732     * this caused all previous data to be considered invalid,
     733     * making us needing to collect full ~8 datapoins per peer
     734     * after step in order to start trusting them.
     735     * In turn, this was making poll interval decrease even after
     736     * step was done. (Poll interval decreases already before step
     737     * in this scenario, because we see large offsets and end up with
     738     * no good peer to select).
     739     */
    678740
    679741    for (i = 0; i < NUM_DATAPOINTS; i++) {
     
    690752            p->filter_datapoint[i].d_recv_time  = G.cur_time;
    691753            p->filter_datapoint[i].d_offset     = 0;
    692             p->filter_datapoint[i].d_dispersion = MAXDISP;
     754            /*p->filter_datapoint[i].d_dispersion = MAXDISP;*/
    693755        }
    694756    }
     
    696758        p->lastpkt_recv_time += offset;
    697759    } else {
    698         p->reachable_bits = 0;
     760        /*p->reachable_bits = 0;*/
    699761        p->lastpkt_recv_time = G.cur_time;
    700762    }
    701763    filter_datapoints(p); /* recalc p->filter_xxx */
    702     VERB5 bb_error_msg("%s->lastpkt_recv_time=%f", p->p_dotted, p->lastpkt_recv_time);
     764    VERB6 bb_error_msg("%s->lastpkt_recv_time=%f", p->p_dotted, p->lastpkt_recv_time);
    703765}
    704766
    705767static void
    706 add_peers(char *s)
    707 {
     768resolve_peer_hostname(peer_t *p, int loop_on_fail)
     769{
     770    len_and_sockaddr *lsa;
     771
     772 again:
     773    lsa = host2sockaddr(p->p_hostname, 123);
     774    if (!lsa) {
     775        /* error message already emitted by host2sockaddr() */
     776        if (!loop_on_fail)
     777            return;
     778//FIXME: do this to avoid infinite looping on typo in a hostname?
     779//well... in which case, what is a good value for loop_on_fail?
     780        //if (--loop_on_fail == 0)
     781        //  xfunc_die();
     782        sleep(5);
     783        goto again;
     784    }
     785    free(p->p_lsa);
     786    free(p->p_dotted);
     787    p->p_lsa = lsa;
     788    p->p_dotted = xmalloc_sockaddr2dotted_noport(&lsa->u.sa);
     789}
     790
     791static void
     792add_peers(const char *s)
     793{
     794    llist_t *item;
    708795    peer_t *p;
    709796
    710     p = xzalloc(sizeof(*p));
    711     p->p_lsa = xhost2sockaddr(s, 123);
    712     p->p_dotted = xmalloc_sockaddr2dotted_noport(&p->p_lsa->u.sa);
     797    p = xzalloc(sizeof(*p) + strlen(s));
     798    strcpy(p->p_hostname, s);
     799    resolve_peer_hostname(p, /*loop_on_fail=*/ 1);
     800
     801    /* Names like N.<country2chars>.pool.ntp.org are randomly resolved
     802     * to a pool of machines. Sometimes different N's resolve to the same IP.
     803     * It is not useful to have two peers with same IP. We skip duplicates.
     804     */
     805    for (item = G.ntp_peers; item != NULL; item = item->link) {
     806        peer_t *pp = (peer_t *) item->data;
     807        if (strcmp(p->p_dotted, pp->p_dotted) == 0) {
     808            bb_error_msg("duplicate peer %s (%s)", s, p->p_dotted);
     809            free(p->p_lsa);
     810            free(p->p_dotted);
     811            free(p);
     812            return;
     813        }
     814    }
     815
    713816    p->p_fd = -1;
    714817    p->p_xmt_msg.m_status = MODE_CLIENT | (NTP_VERSION << 3);
    715818    p->next_action_time = G.cur_time; /* = set_next(p, 0); */
    716     reset_peer_stats(p, 16 * STEP_THRESHOLD);
     819    reset_peer_stats(p, STEP_THRESHOLD);
    717820
    718821    llist_add_to(&G.ntp_peers, p);
     
    778881        if (family == AF_INET)
    779882#endif
    780             setsockopt(fd, IPPROTO_IP, IP_TOS, &const_IPTOS_LOWDELAY, sizeof(const_IPTOS_LOWDELAY));
     883            setsockopt_int(fd, IPPROTO_IP, IP_TOS, IPTOS_LOWDELAY);
    781884        free(local_lsa);
    782885    }
     
    801904     * Save the real transmit timestamp locally.
    802905     */
    803     p->p_xmt_msg.m_xmttime.int_partl = random();
    804     p->p_xmt_msg.m_xmttime.fractionl = random();
     906    p->p_xmt_msg.m_xmttime.int_partl = rand();
     907    p->p_xmt_msg.m_xmttime.fractionl = rand();
    805908    p->p_xmttime = gettime1900d();
     909
     910    /* Were doing it only if sendto worked, but
     911     * loss of sync detection needs reachable_bits updated
     912     * even if sending fails *locally*:
     913     * "network is unreachable" because cable was pulled?
     914     * We still need to declare "unsync" if this condition persists.
     915     */
     916    p->reachable_bits <<= 1;
    806917
    807918    if (do_sendto(p->p_fd, /*from:*/ NULL, /*to:*/ &p->p_lsa->u.sa, /*addrlen:*/ p->p_lsa->len,
     
    810921        close(p->p_fd);
    811922        p->p_fd = -1;
     923        /*
     924         * We know that we sent nothing.
     925         * We can retry *soon* without fearing
     926         * that we are flooding the peer.
     927         */
    812928        set_next(p, RETRY_INTERVAL);
    813929        return;
    814930    }
    815931
    816     p->reachable_bits <<= 1;
    817932    set_next(p, RESPONSE_INTERVAL);
    818933}
     
    820935
    821936/* Note that there is no provision to prevent several run_scripts
    822  * to be done in quick succession. In fact, it happens rather often
     937 * to be started in quick succession. In fact, it happens rather often
    823938 * if initial syncronization results in a step.
    824939 * You will see "step" and then "stratum" script runs, sometimes
     
    830945    char *argv[3];
    831946    char *env1, *env2, *env3, *env4;
     947
     948    G.last_script_run = G.cur_time;
    832949
    833950    if (!G.script_name)
     
    867984    free(env3);
    868985    free(env4);
    869 
    870     G.last_script_run = G.cur_time;
    871986}
    872987
     
    8881003    VERB2 {
    8891004        tval = tvc.tv_sec;
    890         strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", localtime(&tval));
     1005        strftime_YYYYMMDDHHMMSS(buf, sizeof(buf), &tval);
    8911006        bb_error_msg("current time is %s.%06u", buf, (unsigned)tvc.tv_usec);
    8921007    }
    8931008    tval = tvn.tv_sec;
    894     strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", localtime(&tval));
     1009    strftime_YYYYMMDDHHMMSS(buf, sizeof(buf), &tval);
    8951010    bb_error_msg("setting time to %s.%06u (offset %+fs)", buf, (unsigned)tvn.tv_usec, offset);
    8961011
     
    9211036}
    9221037
     1038static void clamp_pollexp_and_set_MAXSTRAT(void)
     1039{
     1040    if (G.poll_exp < MINPOLL)
     1041        G.poll_exp = MINPOLL;
     1042    if (G.poll_exp > BIGPOLL)
     1043        G.poll_exp = BIGPOLL;
     1044    G.polladj_count = 0;
     1045    G.stratum = MAXSTRAT;
     1046}
     1047
    9231048
    9241049/*
     
    9601085    if ((p->reachable_bits & (p->reachable_bits-1)) == 0) {
    9611086        /* One or zero bits in reachable_bits */
    962         VERB3 bb_error_msg("peer %s unfit for selection: unreachable", p->p_dotted);
     1087        VERB4 bb_error_msg("peer %s unfit for selection: unreachable", p->p_dotted);
    9631088        return 0;
    9641089    }
     
    9671092     || p->lastpkt_stratum >= MAXSTRAT
    9681093    ) {
    969         VERB3 bb_error_msg("peer %s unfit for selection: bad status/stratum", p->p_dotted);
     1094        VERB4 bb_error_msg("peer %s unfit for selection: bad status/stratum", p->p_dotted);
    9701095        return 0;
    9711096    }
     
    9731098    /* rd is root_distance(p) */
    9741099    if (rd > MAXDIST + FREQ_TOLERANCE * (1 << G.poll_exp)) {
    975         VERB3 bb_error_msg("peer %s unfit for selection: root distance too high", p->p_dotted);
     1100        VERB4 bb_error_msg("peer %s unfit for selection: root distance too high", p->p_dotted);
    9761101        return 0;
    9771102    }
     
    10021127    num_points = 0;
    10031128    item = G.ntp_peers;
    1004     if (G.initial_poll_complete) while (item != NULL) {
     1129    while (item != NULL) {
    10051130        double rd, offset;
    10061131
     
    10131138        }
    10141139
    1015         VERB4 bb_error_msg("interval: [%f %f %f] %s",
     1140        VERB5 bb_error_msg("interval: [%f %f %f] %s",
    10161141                offset - rd,
    10171142                offset,
     
    10381163    num_candidates = num_points / 3;
    10391164    if (num_candidates == 0) {
    1040         VERB3 bb_error_msg("no valid datapoints, no peer selected");
     1165        VERB3 bb_error_msg("no valid datapoints%s", ", no peer selected");
    10411166        return NULL;
    10421167    }
     
    10961221        num_falsetickers++;
    10971222        if (num_falsetickers * 2 >= num_candidates) {
    1098             VERB3 bb_error_msg("too many falsetickers:%d (candidates:%d), no peer selected",
    1099                     num_falsetickers, num_candidates);
     1223            VERB3 bb_error_msg("falsetickers:%d, candidates:%d%s",
     1224                    num_falsetickers, num_candidates,
     1225                    ", no peer selected");
    11001226            return NULL;
    11011227        }
    11021228    }
    1103     VERB3 bb_error_msg("selected interval: [%f, %f]; candidates:%d falsetickers:%d",
     1229    VERB4 bb_error_msg("selected interval: [%f, %f]; candidates:%d falsetickers:%d",
    11041230            low, high, num_candidates, num_falsetickers);
    11051231
     
    11191245        /* x.opt_rd == root_distance(p); */
    11201246        survivor[num_survivors].metric = MAXDIST * p->lastpkt_stratum + point[i].opt_rd;
    1121         VERB4 bb_error_msg("survivor[%d] metric:%f peer:%s",
     1247        VERB5 bb_error_msg("survivor[%d] metric:%f peer:%s",
    11221248            num_survivors, survivor[num_survivors].metric, p->p_dotted);
    11231249        num_survivors++;
     
    11291255     */
    11301256    if (num_survivors < MIN_SELECTED) {
    1131         VERB3 bb_error_msg("num_survivors %d < %d, no peer selected",
    1132                 num_survivors, MIN_SELECTED);
     1257        VERB3 bb_error_msg("survivors:%d%s",
     1258                num_survivors,
     1259                ", no peer selected");
    11331260        return NULL;
    11341261    }
     
    11501277
    11511278        if (num_survivors <= MIN_CLUSTERED) {
    1152             VERB3 bb_error_msg("num_survivors %d <= %d, not discarding more",
     1279            VERB4 bb_error_msg("num_survivors %d <= %d, not discarding more",
    11531280                    num_survivors, MIN_CLUSTERED);
    11541281            break;
     
    11761303                max_idx = i;
    11771304            }
    1178             VERB5 bb_error_msg("survivor %d selection_jitter^2:%f",
     1305            VERB6 bb_error_msg("survivor %d selection_jitter^2:%f",
    11791306                    i, selection_jitter_sq);
    11801307        }
    11811308        max_selection_jitter = SQRT(max_selection_jitter / num_survivors);
    1182         VERB4 bb_error_msg("max_selection_jitter (at %d):%f min_jitter:%f",
     1309        VERB5 bb_error_msg("max_selection_jitter (at %d):%f min_jitter:%f",
    11831310                max_idx, max_selection_jitter, min_jitter);
    11841311
     
    11891316         */
    11901317        if (max_selection_jitter < min_jitter) {
    1191             VERB3 bb_error_msg("max_selection_jitter:%f < min_jitter:%f, num_survivors:%d, not discarding more",
     1318            VERB4 bb_error_msg("max_selection_jitter:%f < min_jitter:%f, num_survivors:%d, not discarding more",
    11921319                    max_selection_jitter, min_jitter, num_survivors);
    11931320            break;
     
    11971324         * and go around again.
    11981325         */
    1199         VERB5 bb_error_msg("dropping survivor %d", max_idx);
     1326        VERB6 bb_error_msg("dropping survivor %d", max_idx);
    12001327        num_survivors--;
    12011328        while (max_idx < num_survivors) {
     
    12391366        for (i = 1; i < num_survivors; i++) {
    12401367            if (G.last_update_peer == survivor[i].p) {
    1241                 VERB4 bb_error_msg("keeping old synced peer");
     1368                VERB5 bb_error_msg("keeping old synced peer");
    12421369                p = G.last_update_peer;
    12431370                goto keep_old;
     
    12471374    G.last_update_peer = p;
    12481375 keep_old:
    1249     VERB3 bb_error_msg("selected peer %s filter_offset:%+f age:%f",
     1376    VERB4 bb_error_msg("selected peer %s filter_offset:%+f age:%f",
    12501377            p->p_dotted,
    12511378            p->filter_offset,
     
    12661393     * the current time.
    12671394     */
    1268     VERB3 bb_error_msg("disc_state=%d last update offset=%f recv_time=%f",
     1395    VERB4 bb_error_msg("disc_state=%d last update offset=%f recv_time=%f",
    12691396            disc_state, offset, recv_time);
    12701397    G.discipline_state = disc_state;
     
    12851412    double freq_drift;
    12861413#endif
     1414#if !USING_KERNEL_PLL_LOOP || USING_INITIAL_FREQ_ESTIMATION
    12871415    double since_last_update;
     1416#endif
    12881417    double etemp, dtemp;
    12891418
     
    13041433     */
    13051434    if (recv_time <= G.last_update_recv_time) {
    1306         VERB3 bb_error_msg("same or older datapoint: %f >= %f, not using it",
    1307                 G.last_update_recv_time, recv_time);
     1435        VERB3 bb_error_msg("update from %s: same or older datapoint, not using it",
     1436            p->p_dotted);
    13081437        return 0; /* "leave poll interval as is" */
    13091438    }
     
    13131442     * and frequency errors.
    13141443     */
     1444#if !USING_KERNEL_PLL_LOOP || USING_INITIAL_FREQ_ESTIMATION
    13151445    since_last_update = recv_time - G.reftime;
     1446#endif
    13161447#if !USING_KERNEL_PLL_LOOP
    13171448    freq_drift = 0;
     
    13211452        /* Ignore updates until the stepout threshold */
    13221453        if (since_last_update < WATCH_THRESHOLD) {
    1323             VERB3 bb_error_msg("measuring drift, datapoint ignored, %f sec remains",
     1454            VERB4 bb_error_msg("measuring drift, datapoint ignored, %f sec remains",
    13241455                    WATCH_THRESHOLD - since_last_update);
    13251456            return 0; /* "leave poll interval as is" */
     
    13351466     */
    13361467    if (abs_offset > STEP_THRESHOLD) {
     1468#if 0
     1469        double remains;
     1470
     1471// This "spike state" seems to be useless, peer selection already drops
     1472// occassional "bad" datapoints. If we are here, there were _many_
     1473// large offsets. When a few first large offsets are seen,
     1474// we end up in "no valid datapoints, no peer selected" state.
     1475// Only when enough of them are seen (which means it's not a fluke),
     1476// we end up here. Looks like _our_ clock is off.
    13371477        switch (G.discipline_state) {
    13381478        case STATE_SYNC:
    13391479            /* The first outlyer: ignore it, switch to SPIK state */
    1340             VERB3 bb_error_msg("offset:%+f - spike detected", offset);
     1480            VERB3 bb_error_msg("update from %s: offset:%+f, spike%s",
     1481                p->p_dotted, offset,
     1482                "");
    13411483            G.discipline_state = STATE_SPIK;
    13421484            return -1; /* "decrease poll interval" */
     
    13461488             * is found or the stepout threshold is exceeded.
    13471489             */
    1348             if (since_last_update < WATCH_THRESHOLD) {
    1349                 VERB3 bb_error_msg("spike detected, datapoint ignored, %f sec remains",
    1350                         WATCH_THRESHOLD - since_last_update);
     1490            remains = WATCH_THRESHOLD - since_last_update;
     1491            if (remains > 0) {
     1492                VERB3 bb_error_msg("update from %s: offset:%+f, spike%s",
     1493                    p->p_dotted, offset,
     1494                    ", datapoint ignored");
    13511495                return -1; /* "decrease poll interval" */
    13521496            }
    13531497            /* fall through: we need to step */
    13541498        } /* switch */
     1499#endif
    13551500
    13561501        /* Step the time and clamp down the poll interval.
     
    13751520         * intervals.
    13761521         */
    1377         VERB3 bb_error_msg("stepping time by %+f; poll_exp=MINPOLL", offset);
     1522        VERB4 bb_error_msg("stepping time by %+f; poll_exp=MINPOLL", offset);
    13781523        step_time(offset);
    13791524        if (option_mask32 & OPT_q) {
     
    13821527        }
    13831528
    1384         G.polladj_count = 0;
    1385         G.poll_exp = MINPOLL;
    1386         G.stratum = MAXSTRAT;
     1529        clamp_pollexp_and_set_MAXSTRAT();
    13871530
    13881531        run_script("step", offset);
     1532
     1533        recv_time += offset;
    13891534
    13901535#if USING_INITIAL_FREQ_ESTIMATION
     
    13961541        abs_offset = offset = 0;
    13971542        set_new_values(STATE_SYNC, offset, recv_time);
    1398 
    13991543    } else { /* abs_offset <= STEP_THRESHOLD */
    14001544
    1401         if (G.poll_exp < MINPOLL && G.initial_poll_complete) {
    1402             VERB3 bb_error_msg("small offset:%+f, disabling burst mode", offset);
    1403             G.polladj_count = 0;
    1404             G.poll_exp = MINPOLL;
    1405         }
     1545        /* The ratio is calculated before jitter is updated to make
     1546         * poll adjust code more sensitive to large offsets.
     1547         */
     1548        G.offset_to_jitter_ratio = abs_offset / G.discipline_jitter;
    14061549
    14071550        /* Compute the clock jitter as the RMS of exponentially
     
    14111554        dtemp = SQUARE(offset - G.last_update_offset);
    14121555        G.discipline_jitter = SQRT(etemp + (dtemp - etemp) / AVG);
     1556        if (G.discipline_jitter < G_precision_sec)
     1557            G.discipline_jitter = G_precision_sec;
    14131558
    14141559        switch (G.discipline_state) {
     
    14291574            set_new_values(STATE_SYNC, offset, recv_time);
    14301575#endif
    1431             VERB3 bb_error_msg("transitioning to FREQ, datapoint ignored");
     1576            VERB4 bb_error_msg("transitioning to FREQ, datapoint ignored");
    14321577            return 0; /* "leave poll interval as is" */
    14331578
     
    14871632    }
    14881633
    1489     if (G.discipline_jitter < G_precision_sec)
    1490         G.discipline_jitter = G_precision_sec;
    1491     G.offset_to_jitter_ratio = abs_offset / G.discipline_jitter;
    1492 
    14931634    G.reftime = G.cur_time;
    14941635    G.ntp_status = p->lastpkt_status;
     
    14981639    dtemp += MAXD(p->filter_dispersion + FREQ_TOLERANCE * (G.cur_time - p->lastpkt_recv_time) + abs_offset, MINDISP);
    14991640    G.rootdisp = p->lastpkt_rootdisp + dtemp;
    1500     VERB3 bb_error_msg("updating leap/refid/reftime/rootdisp from peer %s", p->p_dotted);
     1641    VERB4 bb_error_msg("updating leap/refid/reftime/rootdisp from peer %s", p->p_dotted);
    15011642
    15021643    /* We are in STATE_SYNC now, but did not do adjtimex yet.
     
    15181659    G.discipline_wander = SQRT(etemp + (dtemp - etemp) / AVG);
    15191660
    1520     VERB3 bb_error_msg("discipline freq_drift=%.9f(int:%ld corr:%e) wander=%f",
     1661    VERB4 bb_error_msg("discipline freq_drift=%.9f(int:%ld corr:%e) wander=%f",
    15211662            G.discipline_freq_drift,
    15221663            (long)(G.discipline_freq_drift * 65536e6),
     
    15241665            G.discipline_wander);
    15251666#endif
    1526     VERB3 {
     1667    VERB4 {
    15271668        memset(&tmx, 0, sizeof(tmx));
    15281669        if (adjtimex(&tmx) < 0)
     
    15431684#endif
    15441685    tmx.modes = ADJ_OFFSET | ADJ_STATUS | ADJ_TIMECONST;// | ADJ_MAXERROR | ADJ_ESTERROR;
    1545     tmx.offset = (offset * 1000000); /* usec */
    1546     tmx.status = STA_PLL;
    1547     if (G.ntp_status & LI_PLUSSEC)
    1548         tmx.status |= STA_INS;
    1549     if (G.ntp_status & LI_MINUSSEC)
    1550         tmx.status |= STA_DEL;
    1551 
    1552     tmx.constant = G.poll_exp - 4;
     1686    tmx.constant = (int)G.poll_exp - 4;
    15531687    /* EXPERIMENTAL.
    15541688     * The below if statement should be unnecessary, but...
     
    15611695     * larger than jitter.
    15621696     */
    1563     if (tmx.constant > 0 && G.offset_to_jitter_ratio >= TIMECONST_HACK_GATE)
     1697    if (G.offset_to_jitter_ratio >= TIMECONST_HACK_GATE)
    15641698        tmx.constant--;
     1699    tmx.offset = (long)(offset * 1000000); /* usec */
     1700    if (SLEW_THRESHOLD < STEP_THRESHOLD) {
     1701        if (tmx.offset > (long)(SLEW_THRESHOLD * 1000000)) {
     1702            tmx.offset = (long)(SLEW_THRESHOLD * 1000000);
     1703            tmx.constant--;
     1704        }
     1705        if (tmx.offset < -(long)(SLEW_THRESHOLD * 1000000)) {
     1706            tmx.offset = -(long)(SLEW_THRESHOLD * 1000000);
     1707            tmx.constant--;
     1708        }
     1709    }
     1710    if (tmx.constant < 0)
     1711        tmx.constant = 0;
     1712
     1713    tmx.status = STA_PLL;
     1714    if (G.ntp_status & LI_PLUSSEC)
     1715        tmx.status |= STA_INS;
     1716    if (G.ntp_status & LI_MINUSSEC)
     1717        tmx.status |= STA_DEL;
    15651718
    15661719    //tmx.esterror = (uint32_t)(clock_jitter * 1e6);
     
    15721725     * Not sure why. Perhaps it is normal.
    15731726     */
    1574     VERB3 bb_error_msg("adjtimex:%d freq:%ld offset:%+ld status:0x%x",
     1727    VERB4 bb_error_msg("adjtimex:%d freq:%ld offset:%+ld status:0x%x",
    15751728                rc, tmx.freq, tmx.offset, tmx.status);
    15761729    G.kernel_freq_drift = tmx.freq / 65536;
    1577     VERB2 bb_error_msg("update from:%s offset:%+f jitter:%f clock drift:%+.3fppm tc:%d",
    1578             p->p_dotted, offset, G.discipline_jitter, (double)tmx.freq / 65536, (int)tmx.constant);
     1730    VERB2 bb_error_msg("update from:%s offset:%+f delay:%f jitter:%f clock drift:%+.3fppm tc:%d",
     1731            p->p_dotted,
     1732            offset,
     1733            p->lastpkt_delay,
     1734            G.discipline_jitter,
     1735            (double)tmx.freq / 65536,
     1736            (int)tmx.constant
     1737    );
    15791738
    15801739    return 1; /* "ok to increase poll interval" */
     
    15871746 */
    15881747static unsigned
    1589 retry_interval(void)
    1590 {
    1591     /* Local problem, want to retry soon */
    1592     unsigned interval, r;
    1593     interval = RETRY_INTERVAL;
    1594     r = random();
    1595     interval += r % (unsigned)(RETRY_INTERVAL / 4);
    1596     VERB3 bb_error_msg("chose retry interval:%u", interval);
     1748poll_interval(int upper_bound)
     1749{
     1750    unsigned interval, r, mask;
     1751    interval = 1 << G.poll_exp;
     1752    if (interval > upper_bound)
     1753        interval = upper_bound;
     1754    mask = ((interval-1) >> 4) | 1;
     1755    r = rand();
     1756    interval += r & mask; /* ~ random(0..1) * interval/16 */
     1757    VERB4 bb_error_msg("chose poll interval:%u (poll_exp:%d)", interval, G.poll_exp);
    15971758    return interval;
    15981759}
    1599 static unsigned
    1600 poll_interval(int exponent)
    1601 {
    1602     unsigned interval, r;
    1603     exponent = G.poll_exp + exponent;
    1604     if (exponent < 0)
    1605         exponent = 0;
    1606     interval = 1 << exponent;
    1607     r = random();
    1608     interval += ((r & (interval-1)) >> 4) + ((r >> 8) & 1); /* + 1/16 of interval, max */
    1609     VERB3 bb_error_msg("chose poll interval:%u (poll_exp:%d exp:%d)", interval, G.poll_exp, exponent);
    1610     return interval;
     1760static void
     1761adjust_poll(int count)
     1762{
     1763    G.polladj_count += count;
     1764    if (G.polladj_count > POLLADJ_LIMIT) {
     1765        G.polladj_count = 0;
     1766        if (G.poll_exp < MAXPOLL) {
     1767            G.poll_exp++;
     1768            VERB4 bb_error_msg("polladj: discipline_jitter:%f ++poll_exp=%d",
     1769                    G.discipline_jitter, G.poll_exp);
     1770        }
     1771    } else if (G.polladj_count < -POLLADJ_LIMIT || (count < 0 && G.poll_exp > BIGPOLL)) {
     1772        G.polladj_count = 0;
     1773        if (G.poll_exp > MINPOLL) {
     1774            llist_t *item;
     1775
     1776            G.poll_exp--;
     1777            /* Correct p->next_action_time in each peer
     1778             * which waits for sending, so that they send earlier.
     1779             * Old pp->next_action_time are on the order
     1780             * of t + (1 << old_poll_exp) + small_random,
     1781             * we simply need to subtract ~half of that.
     1782             */
     1783            for (item = G.ntp_peers; item != NULL; item = item->link) {
     1784                peer_t *pp = (peer_t *) item->data;
     1785                if (pp->p_fd < 0)
     1786                    pp->next_action_time -= (1 << G.poll_exp);
     1787            }
     1788            VERB4 bb_error_msg("polladj: discipline_jitter:%f --poll_exp=%d",
     1789                    G.discipline_jitter, G.poll_exp);
     1790        }
     1791    } else {
     1792        VERB4 bb_error_msg("polladj: count:%d", G.polladj_count);
     1793    }
    16111794}
    16121795static NOINLINE void
     
    16171800    msg_t       msg;
    16181801    double      T1, T2, T3, T4;
     1802    double      offset;
     1803    double      prev_delay, delay;
    16191804    unsigned    interval;
    16201805    datapoint_t *datapoint;
    16211806    peer_t      *q;
    16221807
     1808    offset = 0;
     1809
    16231810    /* We can recvfrom here and check from.IP, but some multihomed
    16241811     * ntp servers reply from their *other IP*.
    16251812     * TODO: maybe we should check at least what we can: from.port == 123?
    16261813     */
     1814 recv_again:
    16271815    size = recv(p->p_fd, &msg, sizeof(msg), MSG_DONTWAIT);
    1628     if (size == -1) {
    1629         bb_perror_msg("recv(%s) error", p->p_dotted);
    1630         if (errno == EHOSTUNREACH || errno == EHOSTDOWN
    1631          || errno == ENETUNREACH || errno == ENETDOWN
    1632          || errno == ECONNREFUSED || errno == EADDRNOTAVAIL
    1633          || errno == EAGAIN
    1634         ) {
    1635 //TODO: always do this?
    1636             interval = retry_interval();
    1637             goto set_next_and_ret;
    1638         }
    1639         xfunc_die();
     1816    if (size < 0) {
     1817        if (errno == EINTR)
     1818            /* Signal caught */
     1819            goto recv_again;
     1820        if (errno == EAGAIN)
     1821            /* There was no packet after all
     1822             * (poll() returning POLLIN for a fd
     1823             * is not a ironclad guarantee that data is there)
     1824             */
     1825            return;
     1826        /*
     1827         * If you need a different handling for a specific
     1828         * errno, always explain it in comment.
     1829         */
     1830        bb_perror_msg_and_die("recv(%s) error", p->p_dotted);
    16401831    }
    16411832
     
    16631854     || msg.m_stratum > NTP_MAXSTRATUM
    16641855    ) {
    1665 // TODO: stratum 0 responses may have commands in 32-bit m_refid field:
    1666 // "DENY", "RSTR" - peer does not like us at all
    1667 // "RATE" - peer is overloaded, reduce polling freq
    1668         interval = poll_interval(0);
    1669         bb_error_msg("reply from %s: peer is unsynced, next query in %us", p->p_dotted, interval);
    1670         goto set_next_and_ret;
     1856        bb_error_msg("reply from %s: peer is unsynced", p->p_dotted);
     1857        /*
     1858         * Stratum 0 responses may have commands in 32-bit m_refid field:
     1859         * "DENY", "RSTR" - peer does not like us at all,
     1860         * "RATE" - peer is overloaded, reduce polling freq.
     1861         * If poll interval is small, increase it.
     1862         */
     1863        if (G.poll_exp < BIGPOLL)
     1864            goto increase_interval;
     1865        goto pick_normal_interval;
    16711866    }
    16721867
     
    16741869//  if (msg.m_rootdelay / 2 + msg.m_rootdisp >= MAXDISP || p->lastpkt_reftime > msg.m_xmt)
    16751870//      return;                 /* invalid header values */
    1676 
    1677     p->lastpkt_status = msg.m_status;
    1678     p->lastpkt_stratum = msg.m_stratum;
    1679     p->lastpkt_rootdelay = sfp_to_d(msg.m_rootdelay);
    1680     p->lastpkt_rootdisp = sfp_to_d(msg.m_rootdisp);
    1681     p->lastpkt_refid = msg.m_refid;
    16821871
    16831872    /*
     
    17001889    T4 = G.cur_time;
    17011890
    1702     p->lastpkt_recv_time = T4;
    1703 
    1704     VERB5 bb_error_msg("%s->lastpkt_recv_time=%f", p->p_dotted, p->lastpkt_recv_time);
    1705     p->datapoint_idx = p->reachable_bits ? (p->datapoint_idx + 1) % NUM_DATAPOINTS : 0;
    1706     datapoint = &p->filter_datapoint[p->datapoint_idx];
    1707     datapoint->d_recv_time = T4;
    1708     datapoint->d_offset    = ((T2 - T1) + (T3 - T4)) / 2;
    17091891    /* The delay calculation is a special case. In cases where the
    17101892     * server and client clocks are running at different rates and
     
    17131895     * the delay is clamped not less than the system precision.
    17141896     */
    1715     p->lastpkt_delay = (T4 - T1) - (T3 - T2);
    1716     if (p->lastpkt_delay < G_precision_sec)
    1717         p->lastpkt_delay = G_precision_sec;
     1897    delay = (T4 - T1) - (T3 - T2);
     1898    if (delay < G_precision_sec)
     1899        delay = G_precision_sec;
     1900    /*
     1901     * If this packet's delay is much bigger than the last one,
     1902     * it's better to just ignore it than use its much less precise value.
     1903     */
     1904    prev_delay = p->p_raw_delay;
     1905    p->p_raw_delay = delay;
     1906    if (p->reachable_bits && delay > prev_delay * BAD_DELAY_GROWTH) {
     1907        bb_error_msg("reply from %s: delay %f is too high, ignoring", p->p_dotted, delay);
     1908        goto pick_normal_interval;
     1909    }
     1910
     1911    p->lastpkt_delay = delay;
     1912    p->lastpkt_recv_time = T4;
     1913    VERB6 bb_error_msg("%s->lastpkt_recv_time=%f", p->p_dotted, p->lastpkt_recv_time);
     1914    p->lastpkt_status = msg.m_status;
     1915    p->lastpkt_stratum = msg.m_stratum;
     1916    p->lastpkt_rootdelay = sfp_to_d(msg.m_rootdelay);
     1917    p->lastpkt_rootdisp = sfp_to_d(msg.m_rootdisp);
     1918    p->lastpkt_refid = msg.m_refid;
     1919
     1920    p->datapoint_idx = p->reachable_bits ? (p->datapoint_idx + 1) % NUM_DATAPOINTS : 0;
     1921    datapoint = &p->filter_datapoint[p->datapoint_idx];
     1922    datapoint->d_recv_time = T4;
     1923    datapoint->d_offset    = offset = ((T2 - T1) + (T3 - T4)) / 2;
    17181924    datapoint->d_dispersion = LOG2D(msg.m_precision_exp) + G_precision_sec;
    17191925    if (!p->reachable_bits) {
     
    17211927        int i;
    17221928        for (i = 0; i < NUM_DATAPOINTS; i++) {
    1723             p->filter_datapoint[i].d_offset = datapoint->d_offset;
     1929            p->filter_datapoint[i].d_offset = offset;
    17241930        }
    17251931    }
     
    17291935        bb_error_msg("reply from %s: offset:%+f delay:%f status:0x%02x strat:%d refid:0x%08x rootdelay:%f reach:0x%02x",
    17301936            p->p_dotted,
    1731             datapoint->d_offset,
     1937            offset,
    17321938            p->lastpkt_delay,
    17331939            p->lastpkt_status,
     
    17451951    filter_datapoints(p);
    17461952    q = select_and_cluster();
    1747     rc = -1;
     1953    rc = 0;
    17481954    if (q) {
    1749         rc = 0;
    17501955        if (!(option_mask32 & OPT_w)) {
    17511956            rc = update_local_clock(q);
     1957#if 0
     1958//Disabled this because there is a case where largish offsets
     1959//are unavoidable: if network round-trip delay is, say, ~0.6s,
     1960//error in offset estimation would be ~delay/2 ~= 0.3s.
     1961//Thus, offsets will be usually in -0.3...0.3s range.
     1962//In this case, this code would keep poll interval small,
     1963//but it won't be helping.
     1964//BIGOFF check below deals with a case of seeing multi-second offsets.
     1965
    17521966            /* If drift is dangerously large, immediately
    17531967             * drop poll interval one step down.
    17541968             */
    17551969            if (fabs(q->filter_offset) >= POLLDOWN_OFFSET) {
    1756                 VERB3 bb_error_msg("offset:%+f > POLLDOWN_OFFSET", q->filter_offset);
    1757                 goto poll_down;
     1970                VERB4 bb_error_msg("offset:%+f > POLLDOWN_OFFSET", q->filter_offset);
     1971                adjust_poll(-POLLADJ_LIMIT * 3);
     1972                rc = 0;
    17581973            }
    1759         }
    1760     }
    1761     /* else: no peer selected, rc = -1: we want to poll more often */
     1974#endif
     1975        }
     1976    } else {
     1977        /* No peer selected.
     1978         * If poll interval is small, increase it.
     1979         */
     1980        if (G.poll_exp < BIGPOLL)
     1981            goto increase_interval;
     1982    }
    17621983
    17631984    if (rc != 0) {
     
    17711992            /* was += G.poll_exp but it is a bit
    17721993             * too optimistic for my taste at high poll_exp's */
    1773             G.polladj_count += MINPOLL;
    1774             if (G.polladj_count > POLLADJ_LIMIT) {
    1775                 G.polladj_count = 0;
    1776                 if (G.poll_exp < MAXPOLL) {
    1777                     G.poll_exp++;
    1778                     VERB3 bb_error_msg("polladj: discipline_jitter:%f ++poll_exp=%d",
    1779                             G.discipline_jitter, G.poll_exp);
    1780                 }
    1781             } else {
    1782                 VERB3 bb_error_msg("polladj: incr:%d", G.polladj_count);
    1783             }
     1994 increase_interval:
     1995            adjust_poll(MINPOLL);
    17841996        } else {
    1785             G.polladj_count -= G.poll_exp * 2;
    1786             if (G.polladj_count < -POLLADJ_LIMIT || G.poll_exp >= BIGPOLL) {
    1787  poll_down:
    1788                 G.polladj_count = 0;
    1789                 if (G.poll_exp > MINPOLL) {
    1790                     llist_t *item;
    1791 
    1792                     G.poll_exp--;
    1793                     /* Correct p->next_action_time in each peer
    1794                      * which waits for sending, so that they send earlier.
    1795                      * Old pp->next_action_time are on the order
    1796                      * of t + (1 << old_poll_exp) + small_random,
    1797                      * we simply need to subtract ~half of that.
    1798                      */
    1799                     for (item = G.ntp_peers; item != NULL; item = item->link) {
    1800                         peer_t *pp = (peer_t *) item->data;
    1801                         if (pp->p_fd < 0)
    1802                             pp->next_action_time -= (1 << G.poll_exp);
    1803                     }
    1804                     VERB3 bb_error_msg("polladj: discipline_jitter:%f --poll_exp=%d",
    1805                             G.discipline_jitter, G.poll_exp);
    1806                 }
    1807             } else {
    1808                 VERB3 bb_error_msg("polladj: decr:%d", G.polladj_count);
    1809             }
     1997            VERB3 if (rc > 0)
     1998                bb_error_msg("want smaller interval: offset/jitter = %u",
     1999                    G.offset_to_jitter_ratio);
     2000            adjust_poll(-G.poll_exp * 2);
    18102001        }
    18112002    }
    18122003
    18132004    /* Decide when to send new query for this peer */
    1814     interval = poll_interval(0);
    1815 
    1816  set_next_and_ret:
     2005 pick_normal_interval:
     2006    interval = poll_interval(INT_MAX);
     2007    if (fabs(offset) >= BIGOFF && interval > BIGOFF_INTERVAL) {
     2008        /* If we are synced, offsets are less than SLEW_THRESHOLD,
     2009         * or at the very least not much larger than it.
     2010         * Now we see a largish one.
     2011         * Either this peer is feeling bad, or packet got corrupted,
     2012         * or _our_ clock is wrong now and _all_ peers will show similar
     2013         * largish offsets too.
     2014         * I observed this with laptop suspend stopping clock.
     2015         * In any case, it makes sense to make next request soonish:
     2016         * cases 1 and 2: get a better datapoint,
     2017         * case 3: allows to resync faster.
     2018         */
     2019        interval = BIGOFF_INTERVAL;
     2020    }
     2021
    18172022    set_next(p, interval);
    18182023}
     
    18442049        bb_error_msg("malformed packet received from %s: size %u", addr, (int)size);
    18452050        free(addr);
     2051        goto bail;
     2052    }
     2053
     2054    /* Respond only to client and symmetric active packets */
     2055    if ((msg.m_status & MODE_MASK) != MODE_CLIENT
     2056     && (msg.m_status & MODE_MASK) != MODE_SYM_ACT
     2057    ) {
    18462058        goto bail;
    18472059    }
     
    19792191    llist_t *peers;
    19802192
    1981     srandom(getpid());
     2193    srand(getpid());
    19822194
    19832195    if (getuid())
     
    19852197
    19862198    /* Set some globals */
     2199    G.discipline_jitter = G_precision_sec;
    19872200    G.stratum = MAXSTRAT;
    19882201    if (BURSTPOLL != 0)
     
    19922205    /* Parse options */
    19932206    peers = NULL;
    1994     opt_complementary = "dd:p::wn"; /* d: counter; p: list; -w implies -n */
     2207    opt_complementary = "dd:p::wn"         /* -d: counter; -p: list; -w implies -n */
     2208        IF_FEATURE_NTPD_SERVER(":Il"); /* -I implies -l */
    19952209    opts = getopt32(argv,
    19962210            "nqNx" /* compat */
    19972211            "wp:S:"IF_FEATURE_NTPD_SERVER("l") /* NOT compat */
     2212            IF_FEATURE_NTPD_SERVER("I:") /* compat */
    19982213            "d" /* compat */
    19992214            "46aAbgL", /* compat, ignored */
    2000             &peers, &G.script_name, &G.verbose);
    2001     if (!(opts & (OPT_p|OPT_l)))
    2002         bb_show_usage();
     2215            &peers,&G.script_name,
     2216#if ENABLE_FEATURE_NTPD_SERVER
     2217            &G.if_name,
     2218#endif
     2219            &G.verbose);
     2220
    20032221//  if (opts & OPT_x) /* disable stepping, only slew is allowed */
    20042222//      G.time_was_stepped = 1;
     
    20062224        while (peers)
    20072225            add_peers(llist_pop(&peers));
    2008     } else {
     2226    }
     2227#if ENABLE_FEATURE_NTPD_CONF
     2228    else {
     2229        parser_t *parser;
     2230        char *token[3];
     2231
     2232        parser = config_open("/etc/ntp.conf");
     2233        while (config_read(parser, token, 3, 1, "# \t", PARSE_NORMAL)) {
     2234            if (strcmp(token[0], "server") == 0 && token[1]) {
     2235                add_peers(token[1]);
     2236                continue;
     2237            }
     2238            bb_error_msg("skipping %s:%u: unimplemented command '%s'",
     2239                "/etc/ntp.conf", parser->lineno, token[0]
     2240            );
     2241        }
     2242        config_close(parser);
     2243    }
     2244#endif
     2245    if (G.peer_cnt == 0) {
     2246        if (!(opts & OPT_l))
     2247            bb_show_usage();
    20092248        /* -l but no peers: "stratum 1 server" mode */
    20102249        G.stratum = 1;
    2011     }
    2012     if (!(opts & OPT_n)) {
    2013         bb_daemonize_or_rexec(DAEMON_DEVNULL_STDIO, argv);
    2014         logmode = LOGMODE_NONE;
    20152250    }
    20162251#if ENABLE_FEATURE_NTPD_SERVER
     
    20182253    if (opts & OPT_l) {
    20192254        G_listen_fd = create_and_bind_dgram_or_die(NULL, 123);
     2255        if (opts & OPT_I) {
     2256            if (setsockopt_bindtodevice(G_listen_fd, G.if_name))
     2257                xfunc_die();
     2258        }
    20202259        socket_want_pktinfo(G_listen_fd);
    2021         setsockopt(G_listen_fd, IPPROTO_IP, IP_TOS, &const_IPTOS_LOWDELAY, sizeof(const_IPTOS_LOWDELAY));
    2022     }
    2023 #endif
     2260        setsockopt_int(G_listen_fd, IPPROTO_IP, IP_TOS, IPTOS_LOWDELAY);
     2261    }
     2262#endif
     2263    if (!(opts & OPT_n)) {
     2264        bb_daemonize_or_rexec(DAEMON_DEVNULL_STDIO, argv);
     2265        logmode = LOGMODE_NONE;
     2266    }
    20242267    /* I hesitate to set -20 prio. -15 should be high enough for timekeeping */
    20252268    if (opts & OPT_N)
     
    21092352                    /* Time to send new req */
    21102353                    if (--cnt == 0) {
    2111                         G.initial_poll_complete = 1;
     2354                        VERB4 bb_error_msg("disabling burst mode");
     2355                        G.polladj_count = 0;
     2356                        G.poll_exp = MINPOLL;
    21122357                    }
    21132358                    send_query_to_peer(p);
     
    21162361                    close(p->p_fd);
    21172362                    p->p_fd = -1;
    2118                     timeout = poll_interval(-2); /* -2: try a bit sooner */
     2363                    /* If poll interval is small, increase it */
     2364                    if (G.poll_exp < BIGPOLL)
     2365                        adjust_poll(MINPOLL);
     2366                    timeout = poll_interval(NOREPLY_INTERVAL);
    21192367                    bb_error_msg("timed out waiting for %s, reach 0x%02x, next query in %us",
    21202368                            p->p_dotted, p->reachable_bits, timeout);
     2369
     2370                    /* What if don't see it because it changed its IP? */
     2371                    if (p->reachable_bits == 0)
     2372                        resolve_peer_hostname(p, /*loop_on_fail=*/ 0);
     2373
    21212374                    set_next(p, timeout);
    21222375                }
     
    21602413        gettime1900d(); /* sets G.cur_time */
    21612414        if (nfds <= 0) {
    2162             if (G.script_name && G.cur_time - G.last_script_run > 11*60) {
     2415            if (!bb_got_signal /* poll wasn't interrupted by a signal */
     2416             && G.cur_time - G.last_script_run > 11*60
     2417            ) {
    21632418                /* Useful for updating battery-backed RTC and such */
    21642419                run_script("periodic", G.last_update_offset);
    21652420                gettime1900d(); /* sets G.cur_time */
    21662421            }
    2167             continue;
     2422            goto check_unsync;
    21682423        }
    21692424
     
    21952450                gettime1900d(); /* sets G.cur_time */
    21962451            }
     2452        }
     2453
     2454 check_unsync:
     2455        if (G.ntp_peers && G.stratum != MAXSTRAT) {
     2456            for (item = G.ntp_peers; item != NULL; item = item->link) {
     2457                peer_t *p = (peer_t *) item->data;
     2458                if (p->reachable_bits)
     2459                    goto have_reachable_peer;
     2460            }
     2461            /* No peer responded for last 8 packets, panic */
     2462            clamp_pollexp_and_set_MAXSTRAT();
     2463            run_script("unsync", 0.0);
     2464 have_reachable_peer: ;
    21972465        }
    21982466    } /* while (!bb_got_signal) */
Note: See TracChangeset for help on using the changeset viewer.