#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
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;
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;
/* 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;
//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;
}
) {
//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
// "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 */
/* 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
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) {
/* 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);
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 */
*/
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;
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++;
}
/* 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
}
} /* while (!bb_got_signal) */
+ remove_pidfile(CONFIG_PID_FILE_PATH "/ntpd.pid");
kill_myself_with_sig(bb_got_signal);
}