From 52daf64bf533bc3fd4c6b81c371f82360fcdcbab Mon Sep 17 00:00:00 2001 From: henning <> Date: Sun, 28 May 2006 20:39:16 +0000 Subject: [PATCH] allow for weight to be added to sensors or servers, so that one can weight timedelta sensors higher than ntp peers, for example ok deraadt mbalmer --- src/usr.sbin/ntpd/ntp.c | 14 +++++++------ src/usr.sbin/ntpd/ntpd.h | 5 ++++- src/usr.sbin/ntpd/parse.y | 42 +++++++++++++++++++++++++++++++------ src/usr.sbin/ntpd/sensors.c | 3 ++- 4 files changed, 50 insertions(+), 14 deletions(-) diff --git a/src/usr.sbin/ntpd/ntp.c b/src/usr.sbin/ntpd/ntp.c index 3d1c0c6a..0bf8bc27 100644 --- a/src/usr.sbin/ntpd/ntp.c +++ b/src/usr.sbin/ntpd/ntp.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ntp.c,v 1.76 2006/05/28 18:47:25 henning Exp $ */ +/* $OpenBSD: ntp.c,v 1.77 2006/05/28 20:39:16 henning Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -422,7 +422,7 @@ priv_adjtime(void) { struct ntp_peer *p; struct ntp_sensor *s; - int offset_cnt = 0, i = 0; + int offset_cnt = 0, i = 0, j; struct ntp_offset **offsets; double offset_median; @@ -431,13 +431,13 @@ priv_adjtime(void) continue; if (!p->update.good) return; - offset_cnt++; + offset_cnt += p->weight; } TAILQ_FOREACH(s, &conf->ntp_sensors, entry) { if (!s->update.good) continue; - offset_cnt++; + offset_cnt += p->weight; } if ((offsets = calloc(offset_cnt, sizeof(struct ntp_offset *))) == NULL) @@ -446,13 +446,15 @@ priv_adjtime(void) TAILQ_FOREACH(p, &conf->ntp_peers, entry) { if (p->trustlevel < TRUSTLEVEL_BADPEER) continue; - offsets[i++] = &p->update; + for (j = 0; j < p->weight; j++) + offsets[i++] = &p->update; } TAILQ_FOREACH(s, &conf->ntp_sensors, entry) { if (!s->update.good) continue; - offsets[i++] = &s->update; + for (j = 0; j < s->weight; j++) + offsets[i++] = &s->update; } qsort(offsets, offset_cnt, sizeof(struct ntp_offset *), offset_compare); diff --git a/src/usr.sbin/ntpd/ntpd.h b/src/usr.sbin/ntpd/ntpd.h index 3e534119..788b3935 100644 --- a/src/usr.sbin/ntpd/ntpd.h +++ b/src/usr.sbin/ntpd/ntpd.h @@ -1,4 +1,4 @@ -/* $OpenBSD: ntpd.h,v 1.68 2006/05/28 18:47:25 henning Exp $ */ +/* $OpenBSD: ntpd.h,v 1.69 2006/05/28 20:39:16 henning Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -124,6 +124,7 @@ struct ntp_peer { u_int32_t id; u_int8_t shift; u_int8_t trustlevel; + u_int8_t weight; int lasterror; }; @@ -133,11 +134,13 @@ struct ntp_sensor { time_t next; char *device; int sensorid; + u_int8_t weight; }; struct ntp_conf_sensor { TAILQ_ENTRY(ntp_conf_sensor) entry; char *device; + u_int8_t weight; }; struct ntpd_conf { diff --git a/src/usr.sbin/ntpd/parse.y b/src/usr.sbin/ntpd/parse.y index ea515c56..8405f861 100644 --- a/src/usr.sbin/ntpd/parse.y +++ b/src/usr.sbin/ntpd/parse.y @@ -1,4 +1,4 @@ -/* $OpenBSD: parse.y,v 1.27 2006/05/27 17:01:07 henning Exp $ */ +/* $OpenBSD: parse.y,v 1.28 2006/05/28 20:39:16 henning Exp $ */ /* * Copyright (c) 2002, 2003, 2004 Henning Brauer @@ -62,10 +62,11 @@ typedef struct { %} %token LISTEN ON -%token SERVER SERVERS SENSOR +%token SERVER SERVERS SENSOR WEIGHT %token ERROR %token STRING %type address +%type number weight %% grammar : /* empty */ @@ -106,7 +107,7 @@ conf_main : LISTEN ON address { free($3->name); free($3); } - | SERVERS address { + | SERVERS address weight { struct ntp_peer *p; struct ntp_addr *h, *next; @@ -128,6 +129,7 @@ conf_main : LISTEN ON address { next = NULL; p = new_peer(); + p->weight = $3; p->addr = h; p->addr_head.a = h; p->addr_head.pool = 1; @@ -144,7 +146,7 @@ conf_main : LISTEN ON address { free($2->name); free($2); } - | SERVER address { + | SERVER address weight { struct ntp_peer *p; struct ntp_addr *h, *next; @@ -165,6 +167,7 @@ conf_main : LISTEN ON address { p->addr = h; } + p->weight = $3; p->addr_head.a = p->addr; p->addr_head.pool = 0; p->addr_head.name = strdup($2->name); @@ -176,10 +179,11 @@ conf_main : LISTEN ON address { free($2->name); free($2); } - | SENSOR STRING { + | SENSOR STRING weight { struct ntp_conf_sensor *s; s = new_sensor($2); + s->weight = $3; free($2); TAILQ_INSERT_TAIL(&conf->ntp_conf_sensors, s, entry); } @@ -200,6 +204,31 @@ address : STRING { } ; +number : STRING { + u_long ulval; + const char *errstr; + + ulval = strtonum($1, 0, ULONG_MAX, &errstr); + if (errstr) { + yyerror("\"%s\" invalid: %s", $1, errstr); + free($1); + YYERROR; + } else + $$ = ulval; + free($1); + } + ; + +weight : /* empty */ { $$ = 1; } + | WEIGHT number { + if ($2 < 1 || $2 > 10) { + yyerror("weight must be between 1 and 10"); + YYERROR; + } + $$ = $2; + } + ; + %% struct keywords { @@ -238,7 +267,8 @@ lookup(char *s) { "on", ON}, { "sensor", SENSOR}, { "server", SERVER}, - { "servers", SERVERS} + { "servers", SERVERS}, + { "weight", WEIGHT} }; const struct keywords *p; diff --git a/src/usr.sbin/ntpd/sensors.c b/src/usr.sbin/ntpd/sensors.c index 47c4b815..cd4f1897 100644 --- a/src/usr.sbin/ntpd/sensors.c +++ b/src/usr.sbin/ntpd/sensors.c @@ -1,4 +1,4 @@ -/* $OpenBSD: sensors.c,v 1.14 2006/05/28 19:04:37 henning Exp $ */ +/* $OpenBSD: sensors.c,v 1.15 2006/05/28 20:39:16 henning Exp $ */ /* * Copyright (c) 2006 Henning Brauer @@ -101,6 +101,7 @@ sensor_add(struct sensor *sensor) fatal("sensor_add calloc"); s->next = time(NULL); + s->weight = cs->weight; if ((s->device = strdup(sensor->device)) == NULL) fatal("sensor_add strdup"); s->sensorid = sensor->num;