CONFIG_PID_FILE_PATH: new configuration option for pidfile paths
[oweals/busybox.git] / networking / ntpd.c
index 4d939458c78dca391cff370e394c3edcf5cc710d..7facf94840bd9403ec62a1a945855f6584ecefef 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
@@ -220,14 +221,14 @@ typedef struct {
 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;
@@ -276,6 +277,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;
@@ -892,6 +896,11 @@ step_time(double 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;
@@ -899,11 +908,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;
 }
 
 
@@ -1620,22 +1634,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
@@ -1644,8 +1666,8 @@ recv_and_process_peer_pkt(peer_t *p)
 // "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, next query in %us", p->p_dotted, interval);
+               goto set_next_and_ret;
        }
 
 //     /* Verify valid root distance */
@@ -1791,16 +1813,8 @@ recv_and_process_peer_pkt(peer_t *p)
        /* Decide when to send new query for this peer */
        interval = poll_interval(0);
 
- 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
@@ -1815,10 +1829,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) {
@@ -1861,7 +1875,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);
 
@@ -2000,11 +2014,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 */
@@ -2066,6 +2080,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;
@@ -2078,8 +2094,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++;
                }
@@ -2125,7 +2141,7 @@ int ntpd_main(int argc UNUSED_PARAM, char **argv)
 
                /* Here we may block */
                VERB2 {
-                       if (i > (ENABLE_FEATURE_NTPD_SERVER && G.listen_fd != -1)) {
+                       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
@@ -2181,6 +2197,7 @@ int ntpd_main(int argc UNUSED_PARAM, char **argv)
                }
        } /* while (!bb_got_signal) */
 
+       remove_pidfile(CONFIG_PID_FILE_PATH "/ntpd.pid");
        kill_myself_with_sig(bb_got_signal);
 }