[PATCH] ntpd: Implement the "-q" option.

Adam Tkac vonsch at gmail.com
Mon Nov 23 14:43:55 UTC 2009


On Mon, Nov 23, 2009 at 02:07:41PM +0100, walter harms wrote:
> 
> 
> Adam Tkac schrieb:
> > On Sat, Nov 21, 2009 at 07:19:27PM +0100, walter harms wrote:
> >>
> >> Adam Tkac schrieb:
> >>> Hi all,
> >>>
> >>> I just finished the NTP client/server applet. Would it be possible to
> >>> merge it to the main repository, please?
> >>>
> >>> Comments are welcomed.
> >>>
> >>> Regards, Adam
> >>
> >> Hi i took a quick look at your code just a minor note,
> >> your ntpd should support -g  and -q
> > 
> > Main reason why I used different options than reference NTP software
> > (www.ntp.org) is that busybox applet is based on OpenNTP
> > (www.openntpd.org). Denys already renamed all options as in reference
> > NTP.
> > 
> > The "-g" parameter is already supported and I don't think we need to
> > implement "-q". You can simply wait till clock is set and then
> > terminate applet via SIG{INT,TERM}, for example.
> > 
> 
> but this is bad for scripting. NTL using common options improves accepting
> and script suddenly are portable.

Implementation of the "-q" option to ntpd. Size +50 B. Please apply it
to the main source.

Note this patch also fixes a bug in the client_dispatch() function.
There was reversed if() condition.

Regards, Adam
-------------- next part --------------
>From 92af97f4915ac911f256ded5f43170095d5b3aa0 Mon Sep 17 00:00:00 2001
From: Adam Tkac <vonsch at gmail.com>
Date: Mon, 23 Nov 2009 15:37:45 +0100
Subject: [PATCH] ntpd: Implement the "-q" option - exit after the first clock set. Size +50 B.

---
 include/usage.h   |    3 ++-
 networking/ntpd.c |   31 ++++++++++++++++---------------
 2 files changed, 18 insertions(+), 16 deletions(-)

diff --git a/include/usage.h b/include/usage.h
index 48cb8df..9acb2a8 100644
--- a/include/usage.h
+++ b/include/usage.h
@@ -3211,7 +3211,7 @@
        "Address:    127.0.0.1\n"
 
 #define ntpd_trivial_usage \
-	"[-dngl] [-p PEER]..."
+	"[-dnglq] [-p PEER]..."
 #define ntpd_full_usage "\n\n" \
        "NTP client/server\n" \
      "\nOptions:" \
@@ -3220,6 +3220,7 @@
      "\n	-g	Set system time even if offset is > 1000 sec" \
      "\n	-l	Run as server on port 123" \
      "\n	-p PEER	Obtain time from PEER (may be repeated)" \
+     "\n	-q	Quit after the first clock set"
 
 #define od_trivial_usage \
        "[-aBbcDdeFfHhIiLlOovXx] " IF_DESKTOP("[-t TYPE] ") "[FILE]"
diff --git a/networking/ntpd.c b/networking/ntpd.c
index e66b3b4..cc4b358 100644
--- a/networking/ntpd.c
+++ b/networking/ntpd.c
@@ -164,7 +164,7 @@ struct globals {
 	uint8_t		settime;
 	uint8_t		firstadj;
 	smallint	peer_cnt;
-
+	smallint	exit_after_settime;
 };
 #define G (*ptr_to_globals)
 
@@ -507,14 +507,14 @@ settime(double offset)
 	char		buf[80];
 	time_t		tval;
 
-#if 0
 	if (!G.settime)
-		return;
-#endif
+		goto bail;
+
+	G.settime = 0;
 
 	/* if the offset is small, don't call settimeofday */
 	if (offset < SETTIME_MIN_OFFSET && offset > -SETTIME_MIN_OFFSET)
-		return;
+		goto bail;
 
 	gettimeofday(&curtime, NULL); /* never fails */
 
@@ -525,11 +525,9 @@ settime(double offset)
 
 	if (settimeofday(&curtime, NULL) == -1) {
 		bb_error_msg("settimeofday");
-		return;
+		goto bail;
 	}
 
-	G.settime = 0;
-
 	tval = curtime.tv_sec;
 	strftime(buf, sizeof(buf), "%a %b %e %H:%M:%S %Z %Y", localtime(&tval));
 
@@ -543,6 +541,10 @@ settime(double offset)
 		if (p->deadline)
 			p->deadline -= offset;
 	}
+
+bail:
+	if (G.exit_after_settime)
+		exit(0);
 }
 
 static void
@@ -708,8 +710,7 @@ client_dispatch(ntp_peer_t *p)
 			offset->offset, offset->delay, (int) interval);
 
 	client_update(p);
-	if (!G.settime)
-		settime(offset->offset);
+	settime(offset->offset);
 
 	if (++p->shift >= OFFSET_ARRAY_SIZE)
 		p->shift = 0;
@@ -861,6 +862,7 @@ enum {
 	OPT_g = (1 << 1),
 	OPT_p = (1 << 2),
 	OPT_l = (1 << 3),
+	OPT_q = (1 << 4)
 };
 
 /* By doing init in a separate function we decrease stack usage
@@ -881,7 +883,7 @@ static NOINLINE void ntp_init(char **argv)
 	opts = getopt32(argv,
 			"ng" /* compat */
 			"p:"IF_FEATURE_NTPD_SERVER("l") /* NOT compat */
-			"d" /* compat */
+			"qd" /* compat */
 			"46aAbLNx", /* compat, ignored */
 			&peers, &G.verbose);
 #if ENABLE_FEATURE_NTPD_SERVER
@@ -900,6 +902,8 @@ static NOINLINE void ntp_init(char **argv)
 		logmode = LOGMODE_NONE;
 		bb_daemonize(DAEMON_DEVNULL_STDIO);
 	}
+	if (opts & OPT_q)
+		G.exit_after_settime = 1;
 
 	/* Set some globals */
 	{
@@ -1003,11 +1007,8 @@ int ntpd_main(int argc UNUSED_PARAM, char **argv)
 			}
 		}
 
-		if (g.settime
-		 && ((trial_cnt > 0 && sent_cnt == 0) || g.peer_cnt == 0)
-		) {
+		if ((trial_cnt > 0 && sent_cnt == 0) || g.peer_cnt == 0)
 			settime(0);	/* no good peers, don't wait */
-		}
 
 		timeout = nextaction - time(NULL);
 		if (timeout < 0)
-- 
1.6.5.2



More information about the busybox mailing list