top: add keyboard commands to --help
[oweals/busybox.git] / networking / ntpd.c
index 6707e9bdb9faa0952507971630d20445613bc279..8fe529edbb878c6ab8a9a785654ec1a306faebb7 100644 (file)
@@ -49,7 +49,7 @@
 /* High-level description of the algorithm:
  *
  * We start running with very small poll_exp, BURSTPOLL,
- * in order to quickly accumulate INITIAL_SAMLPES datapoints
+ * in order to quickly accumulate INITIAL_SAMPLES datapoints
  * for each peer. Then, time is stepped if the offset is larger
  * than STEP_THRESHOLD, otherwise it isn't; anyway, we enlarge
  * poll_exp to MINPOLL and enter frequency measurement step:
@@ -77,7 +77,7 @@
 
 #define RETRY_INTERVAL  5       /* on error, retry in N secs */
 #define RESPONSE_INTERVAL 15    /* wait for reply up to N secs */
-#define INITIAL_SAMLPES 4       /* how many samples do we want for init */
+#define INITIAL_SAMPLES 4       /* how many samples do we want for init */
 
 /* Clock discipline parameters and constants */
 
@@ -89,7 +89,7 @@
 //UNUSED: #define PANIC_THRESHOLD 1000    /* panic threshold (sec) */
 
 #define FREQ_TOLERANCE  0.000015 /* frequency tolerance (15 PPM) */
-#define BURSTPOLL       0      /* initial poll */
+#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) */
 #define MAXPOLL         12      /* maximum poll interval (12: 1.1h, 17: 36.4h). std ntpd uses 17 */
@@ -865,7 +865,7 @@ fit(peer_t *p, double rd)
                VERB3 bb_error_msg("peer %s unfit for selection: unreachable", p->p_dotted);
                return 0;
        }
-#if 0  /* we filter out such packets earlier */
+#if 0 /* we filter out such packets earlier */
        if ((p->lastpkt_status & LI_ALARM) == LI_ALARM
         || p->lastpkt_stratum >= MAXSTRAT
        ) {
@@ -1765,6 +1765,10 @@ recv_and_process_client_pkt(void /*int fd*/)
        /* this time was obtained between poll() and recv() */
        msg.m_rectime = d_to_lfp(G.cur_time);
        msg.m_xmttime = d_to_lfp(gettime1900d()); /* this instant */
+       if (G.peer_cnt == 0) {
+               /* we have no peers: "stratum 1 server" mode. reftime = our own time */
+               G.reftime = G.cur_time;
+       }
        msg.m_reftime = d_to_lfp(G.reftime);
        msg.m_orgtime = query_xmttime;
        msg.m_rootdelay = d_to_sfp(G.rootdelay);
@@ -1902,8 +1906,13 @@ static NOINLINE void ntp_init(char **argv)
                bb_show_usage();
 //     if (opts & OPT_x) /* disable stepping, only slew is allowed */
 //             G.time_was_stepped = 1;
-       while (peers)
-               add_peers(llist_pop(&peers));
+       if (peers) {
+               while (peers)
+                       add_peers(llist_pop(&peers));
+       } else {
+               /* -l but no peers: "stratum 1 server" mode */
+               G.stratum = 1;
+       }
        if (!(opts & OPT_n)) {
                bb_daemonize_or_rexec(DAEMON_DEVNULL_STDIO, argv);
                logmode = LOGMODE_NONE;
@@ -1963,14 +1972,14 @@ int ntpd_main(int argc UNUSED_PARAM, char **argv)
        idx2peer = xzalloc(sizeof(idx2peer[0]) * cnt);
        pfd = xzalloc(sizeof(pfd[0]) * cnt);
 
-       /* Countdown: we never sync before we sent INITIAL_SAMLPES+1
+       /* Countdown: we never sync before we sent INITIAL_SAMPLES+1
         * packets to each peer.
         * NB: if some peer is not responding, we may end up sending
         * fewer packets to it and more to other peers.
-        * NB2: sync usually happens using INITIAL_SAMLPES packets,
+        * NB2: sync usually happens using INITIAL_SAMPLES packets,
         * since last reply does not come back instantaneously.
         */
-       cnt = G.peer_cnt * (INITIAL_SAMLPES + 1);
+       cnt = G.peer_cnt * (INITIAL_SAMPLES + 1);
 
        while (!bb_got_signal) {
                llist_t *item;
@@ -2101,7 +2110,7 @@ direct_freq(double fp_offset)
 }
 
 static void
-set_freq(double        freq) /* frequency update */
+set_freq(double freq) /* frequency update */
 {
        char tbuf[80];