Hello,
I got tired of hearing numbers thrown around without basis in a 4-5 day old 
thread
when the subject was probably trivial, so I wrote a parser for ntp.conf
this morning.

This version falls back to /etc/ntp.conf when -p is not specified; if -p
is specified, ntp.conf is ignored. Only lines starting with "server" are
used.

Here (Debian Squeeze) the patch is +10 bytes without FEATURE_NTPD_CONF,
+250 bytes with it on. I could get rid of that +10 bytes easily; it's
just because I changed how it detects whether to error out.

HTH,
Isaac Dunham
>From 3e0a8c764d58d1637fdcbaf24b6b4f77b2216402 Mon Sep 17 00:00:00 2001
From: Isaac Dunham <[email protected]>
Date: Sat, 22 Mar 2014 11:44:30 -0700
Subject: [PATCH] ntpd: parse /etc/ntp.conf

This is a rudimentary parser for /etc/ntp.conf; it only looks for
lines like this:
server some.pool.ntp.org
server 0.0.0.0
All options are ignored.
+10 bytes when disabled, though I could make it +0.

Enabled, it's +250 bytes with gcc 4.4 and glibc 2.11.3:

function                                             old     new   delta
ntp_init                                             406     523    +117
add_peers                                              -     107    +107
.rodata                                           141382  141402     +20
packed_usage                                       29386   29392      +6
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 3/0 up/down: 250/0)             Total: 250 bytes
   text	   data	    bss	    dec	    hex	filename
 759413	   2059	   9020	 770492	  bc1bc	busybox_old
 759657	   2059	   9020	 770736	  bc2b0	busybox_unstripped
---
 networking/Config.src |    8 ++++++++
 networking/ntpd.c     |   29 +++++++++++++++++++++++++++--
 2 files changed, 35 insertions(+), 2 deletions(-)

diff --git a/networking/Config.src b/networking/Config.src
index ca0ddcd..fbad7ec 100644
--- a/networking/Config.src
+++ b/networking/Config.src
@@ -664,6 +664,14 @@ config FEATURE_NTPD_SERVER
 	  Make ntpd usable as a NTP server. If you disable this option
 	  ntpd will be usable only as a NTP client.
 
+config FEATURE_NTPD_CONF
+	bool "Make ntpd understand /etc/ntp.conf"
+	default y
+	depends on NTPD
+	help
+	  Make ntpd look in /etc/ntp.conf for peers. Only "server address"
+	  is supported.
+
 config PSCAN
 	bool "pscan"
 	default y
diff --git a/networking/ntpd.c b/networking/ntpd.c
index 44592ce..bbecdd3 100644
--- a/networking/ntpd.c
+++ b/networking/ntpd.c
@@ -42,6 +42,9 @@
 //usage:	)
 //usage:     "\n	-S PROG	Run PROG after stepping time, stratum change, and every 11 mins"
 //usage:     "\n	-p PEER	Obtain time from PEER (may be repeated)"
+//usage:	IF_FEATURE_NTPD_CONF(
+//usage:     "\nIf -p is not specified, look in /etc/ntp.conf"
+//usage:	)
 
 #include "libbb.h"
 #include <math.h>
@@ -2087,17 +2090,39 @@ static NOINLINE void ntp_init(char **argv)
 			"d" /* compat */
 			"46aAbgL", /* compat, ignored */
 			&peers, &G.script_name, &G.verbose);
-	if (!(opts & (OPT_p|OPT_l)))
-		bb_show_usage();
+
 //	if (opts & OPT_x) /* disable stepping, only slew is allowed */
 //		G.time_was_stepped = 1;
 	if (peers) {
 		while (peers)
 			add_peers(llist_pop(&peers));
 	} else {
+#if ENABLE_FEATURE_NTPD_CONF
+		size_t bsiz = 0, i=0;
+		char *buf = 0;
+		FILE *stream = fopen("/etc/ntp.conf","r");
+
+		while (stream && !errno) {
+			getline(&buf, &bsiz, stream);
+			if (!strncmp(buf, "server", 6)) {
+				char *cbuf = buf+6;
+				while (cbuf[0]=='\t' || cbuf[0]==' ')
+					cbuf++;
+				while (cbuf[i] > 35) i++;
+				cbuf[i] = 0;
+				add_peers(xstrdup(cbuf));
+			}
+		}
+		if (buf) free(buf);
+		if (stream) fclose(stream);
+		errno = 0;
+	}
+	if ((opts & OPT_l) && !G.peer_cnt) {
+#endif
 		/* -l but no peers: "stratum 1 server" mode */
 		G.stratum = 1;
 	}
+	if (!(opts & OPT_l) && !G.peer_cnt) bb_show_usage();
 	if (!(opts & OPT_n)) {
 		bb_daemonize_or_rexec(DAEMON_DEVNULL_STDIO, argv);
 		logmode = LOGMODE_NONE;
-- 
1.7.10.4

_______________________________________________
busybox mailing list
[email protected]
http://lists.busybox.net/mailman/listinfo/busybox

Reply via email to