nc: show help text on bad parameters
[oweals/busybox.git] / networking / ntpd.c
index 6d9183a4bfa04264d665d12f1ad79f10e16c8dd2..6707e9bdb9faa0952507971630d20445613bc279 100644 (file)
@@ -3,7 +3,7 @@
  *
  * Author: Adam Tkac <vonsch@gmail.com>
  *
- * Licensed under GPLv2, see file LICENSE in this tarball for details.
+ * Licensed under GPLv2, see file LICENSE in this source tree.
  *
  * Parts of OpenNTPD clock syncronization code is replaced by
  * code which is based on ntp-4.2.6, whuch carries the following
@@ -592,7 +592,6 @@ filter_datapoints(peer_t *p)
                        p->filter_offset, x,
                        p->filter_dispersion,
                        p->filter_jitter);
-
 }
 
 static void
@@ -603,9 +602,9 @@ reset_peer_stats(peer_t *p, double offset)
 
        for (i = 0; i < NUM_DATAPOINTS; i++) {
                if (small_ofs) {
-                       p->filter_datapoint[i].d_recv_time -= offset;
+                       p->filter_datapoint[i].d_recv_time += offset;
                        if (p->filter_datapoint[i].d_offset != 0) {
-                               p->filter_datapoint[i].d_offset -= offset;
+                               p->filter_datapoint[i].d_offset += offset;
                        }
                } else {
                        p->filter_datapoint[i].d_recv_time  = G.cur_time;
@@ -614,13 +613,12 @@ reset_peer_stats(peer_t *p, double offset)
                }
        }
        if (small_ofs) {
-               p->lastpkt_recv_time -= offset;
+               p->lastpkt_recv_time += offset;
        } else {
                p->reachable_bits = 0;
                p->lastpkt_recv_time = G.cur_time;
        }
        filter_datapoints(p); /* recalc p->filter_xxx */
-       p->next_action_time -= offset;
        VERB5 bb_error_msg("%s->lastpkt_recv_time=%f", p->p_dotted, p->lastpkt_recv_time);
 }
 
@@ -815,11 +813,14 @@ step_time(double offset)
        for (item = G.ntp_peers; item != NULL; item = item->link) {
                peer_t *pp = (peer_t *) item->data;
                reset_peer_stats(pp, 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;
        }
        /* Globals: */
-       G.cur_time -= offset;
-       G.last_update_recv_time -= offset;
-       G.last_script_run -= offset;
+       G.cur_time += offset;
+       G.last_update_recv_time += offset;
+       G.last_script_run += offset;
 }
 
 
@@ -1919,9 +1920,28 @@ static NOINLINE void ntp_init(char **argv)
        if (opts & OPT_N)
                setpriority(PRIO_PROCESS, 0, -15);
 
-       bb_signals((1 << SIGTERM) | (1 << SIGINT), record_signo);
-       /* Removed SIGHUP here: */
-       bb_signals((1 << SIGPIPE) | (1 << SIGCHLD), SIG_IGN);
+       /* If network is up, syncronization occurs in ~10 seconds.
+        * We give "ntpd -q" a full minute to finish, then we exit.
+        *
+        * I tested ntpd 4.2.6p1 and apparently it never exits
+        * (will try forever), but it does not feel right.
+        * The goal of -q is to act like ntpdate: set time
+        * after a reasonably small period of polling, or fail.
+        */
+       if (opts & OPT_q)
+               alarm(60);
+
+       bb_signals(0
+               | (1 << SIGTERM)
+               | (1 << SIGINT)
+               | (1 << SIGALRM)
+               , record_signo
+       );
+       bb_signals(0
+               | (1 << SIGPIPE)
+               | (1 << SIGCHLD)
+               , SIG_IGN
+       );
 }
 
 int ntpd_main(int argc UNUSED_PARAM, char **argv) MAIN_EXTERNALLY_VISIBLE;
@@ -2059,7 +2079,6 @@ int ntpd_main(int argc UNUSED_PARAM, char **argv)
 static double
 direct_freq(double fp_offset)
 {
-
 #ifdef KERNEL_PLL
        /*
         * If the kernel is enabled, we need the residual offset to