Browse Source

Do a quick DNS probe to decide to stay in the forground and attempt

an (auto) settime or give up. 15s timeout is still in effect. ok florian@
OPENBSD_6_6
otto 5 years ago
parent
commit
6f5c796c99
4 changed files with 70 additions and 13 deletions
  1. +4
    -4
      src/usr.sbin/ntpd/client.c
  2. +15
    -5
      src/usr.sbin/ntpd/ntp.c
  3. +47
    -1
      src/usr.sbin/ntpd/ntp_dns.c
  4. +4
    -3
      src/usr.sbin/ntpd/ntpd.h

+ 4
- 4
src/usr.sbin/ntpd/client.c View File

@ -1,4 +1,4 @@
/* $OpenBSD: client.c,v 1.108 2019/06/16 07:36:25 otto Exp $ */
/* $OpenBSD: client.c,v 1.109 2019/06/20 07:28:18 otto Exp $ */
/* /*
* Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
@ -240,7 +240,7 @@ handle_auto(double offset)
if (offset < AUTO_THRESHOLD) { if (offset < AUTO_THRESHOLD) {
/* don't bother */ /* don't bother */
priv_settime(0);
priv_settime(0, "offset is negative or close enough");
return; return;
} }
/* collect some more */ /* collect some more */
@ -254,7 +254,7 @@ handle_auto(double offset)
offset = (v[AUTO_REPLIES / 2 - 1] + v[AUTO_REPLIES / 2]) / 2; offset = (v[AUTO_REPLIES / 2 - 1] + v[AUTO_REPLIES / 2]) / 2;
else else
offset = v[AUTO_REPLIES / 2]; offset = v[AUTO_REPLIES / 2];
priv_settime(offset);
priv_settime(offset, "");
} }
int int
@ -459,7 +459,7 @@ client_dispatch(struct ntp_peer *p, u_int8_t settime, u_int8_t automatic)
if (automatic) if (automatic)
handle_auto(p->reply[p->shift].offset); handle_auto(p->reply[p->shift].offset);
else else
priv_settime(p->reply[p->shift].offset);
priv_settime(p->reply[p->shift].offset, "");
} }
if (++p->shift >= OFFSET_ARRAY_SIZE) if (++p->shift >= OFFSET_ARRAY_SIZE)


+ 15
- 5
src/usr.sbin/ntpd/ntp.c View File

@ -1,4 +1,4 @@
/* $OpenBSD: ntp.c,v 1.155 2019/06/16 07:36:25 otto Exp $ */
/* $OpenBSD: ntp.c,v 1.156 2019/06/20 07:28:18 otto Exp $ */
/* /*
* Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
@ -302,7 +302,7 @@ ntp_main(struct ntpd_conf *nconf, struct passwd *pw, int argc, char **argv)
sensors_cnt = 0; sensors_cnt = 0;
TAILQ_FOREACH(s, &conf->ntp_sensors, entry) { TAILQ_FOREACH(s, &conf->ntp_sensors, entry) {
if (conf->settime && s->offsets[0].offset) if (conf->settime && s->offsets[0].offset)
priv_settime(s->offsets[0].offset);
priv_settime(s->offsets[0].offset, NULL);
sensors_cnt++; sensors_cnt++;
if (s->next > 0 && s->next < nextaction) if (s->next > 0 && s->next < nextaction)
nextaction = s->next; nextaction = s->next;
@ -312,7 +312,7 @@ ntp_main(struct ntpd_conf *nconf, struct passwd *pw, int argc, char **argv)
if (conf->settime && if (conf->settime &&
((trial_cnt > 0 && sent_cnt == 0) || ((trial_cnt > 0 && sent_cnt == 0) ||
(peer_cnt == 0 && sensors_cnt == 0))) (peer_cnt == 0 && sensors_cnt == 0)))
priv_settime(0); /* no good peers, don't wait */
priv_settime(0, "no valid peers configured");
TAILQ_FOREACH(cstr, &conf->constraints, entry) { TAILQ_FOREACH(cstr, &conf->constraints, entry) {
if (constraint_query(cstr) == -1) if (constraint_query(cstr) == -1)
@ -521,7 +521,7 @@ ntp_dispatch_imsg_dns(void)
log_warnx("DNS lookup tempfail"); log_warnx("DNS lookup tempfail");
peer->state = STATE_DNS_TEMPFAIL; peer->state = STATE_DNS_TEMPFAIL;
if (++conf->tmpfail > TRIES_AUTO_DNSFAIL) if (++conf->tmpfail > TRIES_AUTO_DNSFAIL)
priv_settime(0);
priv_settime(0, "of dns failures");
break; break;
} }
@ -569,6 +569,14 @@ ntp_dispatch_imsg_dns(void)
constraint_msg_dns(imsg.hdr.peerid, constraint_msg_dns(imsg.hdr.peerid,
imsg.data, imsg.hdr.len - IMSG_HEADER_SIZE); imsg.data, imsg.hdr.len - IMSG_HEADER_SIZE);
break; break;
case IMSG_PROBE_ROOT:
dlen = imsg.hdr.len - IMSG_HEADER_SIZE;
if (dlen != sizeof(int))
fatalx("IMSG_PROBE_ROOT");
memcpy(&n, imsg.data, sizeof(int));
if (n < 0)
priv_settime(0, "dns probe failed");
break;
default: default:
break; break;
} }
@ -754,8 +762,10 @@ offset_compare(const void *aa, const void *bb)
} }
void void
priv_settime(double offset)
priv_settime(double offset, char *msg)
{ {
if (offset == 0)
log_info("cancel settime because %s", msg);
imsg_compose(ibuf_main, IMSG_SETTIME, 0, 0, -1, imsg_compose(ibuf_main, IMSG_SETTIME, 0, 0, -1,
&offset, sizeof(offset)); &offset, sizeof(offset));
conf->settime = 0; conf->settime = 0;


+ 47
- 1
src/usr.sbin/ntpd/ntp_dns.c View File

@ -1,4 +1,4 @@
/* $OpenBSD: ntp_dns.c,v 1.22 2019/06/12 05:04:45 otto Exp $ */
/* $OpenBSD: ntp_dns.c,v 1.23 2019/06/20 07:28:18 otto Exp $ */
/* /*
* Copyright (c) 2003-2008 Henning Brauer <henning@openbsd.org> * Copyright (c) 2003-2008 Henning Brauer <henning@openbsd.org>
@ -19,6 +19,9 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/resource.h> #include <sys/resource.h>
#include <sys/time.h> #include <sys/time.h>
#include <netinet/in.h>
#include <arpa/nameser.h>
#include <resolv.h>
#include <netinet/in.h> #include <netinet/in.h>
@ -40,6 +43,8 @@ struct imsgbuf *ibuf_dns;
void sighdlr_dns(int); void sighdlr_dns(int);
int dns_dispatch_imsg(struct ntpd_conf *); int dns_dispatch_imsg(struct ntpd_conf *);
int probe_root_ns(void);
void probe_root(void);
void void
sighdlr_dns(int sig) sighdlr_dns(int sig)
@ -96,6 +101,8 @@ ntp_dns(struct ntpd_conf *nconf, struct passwd *pw)
if (pledge("stdio dns", NULL) == -1) if (pledge("stdio dns", NULL) == -1)
err(1, "pledge"); err(1, "pledge");
probe_root();
while (quit_dns == 0) { while (quit_dns == 0) {
pfd[0].fd = ibuf_dns->fd; pfd[0].fd = ibuf_dns->fd;
pfd[0].events = POLLIN; pfd[0].events = POLLIN;
@ -202,3 +209,42 @@ dns_dispatch_imsg(struct ntpd_conf *nconf)
} }
return (0); return (0);
} }
int
probe_root_ns(void)
{
int ret;
int old_retrans, old_retry, old_options;
unsigned char buf[4096];
old_retrans = _res.retrans;
old_retry = _res.retry;
old_options = _res.options;
_res.retrans = 1;
_res.retry = 1;
_res.options |= RES_USE_CD;
ret = res_query(".", C_IN, T_NS, buf, sizeof(buf));
_res.retrans = old_retrans;
_res.retry = old_retry;
_res.options = old_options;
return ret;
}
void
probe_root(void)
{
int n;
n = probe_root_ns();
if (n < 0) {
/* give programs like unwind a second chance */
sleep(1);
n = probe_root_ns();
}
if (imsg_compose(ibuf_dns, IMSG_PROBE_ROOT, 0, 0, -1, &n,
sizeof(int)) == -1)
fatalx("probe_root");
}

+ 4
- 3
src/usr.sbin/ntpd/ntpd.h View File

@ -1,4 +1,4 @@
/* $OpenBSD: ntpd.h,v 1.143 2019/06/16 07:36:25 otto Exp $ */
/* $OpenBSD: ntpd.h,v 1.144 2019/06/20 07:28:18 otto Exp $ */
/* /*
* Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
@ -311,7 +311,8 @@ enum imsg_type {
IMSG_CTL_SHOW_ALL, IMSG_CTL_SHOW_ALL,
IMSG_CTL_SHOW_ALL_END, IMSG_CTL_SHOW_ALL_END,
IMSG_SYNCED, IMSG_SYNCED,
IMSG_UNSYNCED
IMSG_UNSYNCED,
IMSG_PROBE_ROOT
}; };
enum ctl_actions { enum ctl_actions {
@ -327,7 +328,7 @@ enum ctl_actions {
void ntp_main(struct ntpd_conf *, struct passwd *, int, char **); void ntp_main(struct ntpd_conf *, struct passwd *, int, char **);
void peer_addr_head_clear(struct ntp_peer *); void peer_addr_head_clear(struct ntp_peer *);
int priv_adjtime(void); int priv_adjtime(void);
void priv_settime(double);
void priv_settime(double, char *);
void priv_dns(int, char *, u_int32_t); void priv_dns(int, char *, u_int32_t);
int offset_compare(const void *, const void *); int offset_compare(const void *, const void *);
void update_scale(double); void update_scale(double);


Loading…
Cancel
Save