[Git][NTPsec/ntpsec][master] 2 commits: More replay implementation.

Eric S. Raymond gitlab at mg.gitlab.com
Sat Dec 12 11:31:55 UTC 2015


Eric S. Raymond pushed to branch master at NTPsec / ntpsec


Commits:
ca794df3 by Eric S. Raymond at 2015-12-11T15:52:51Z
More replay implementation.

Fixes a bug introduced by my previus commit "For TESTFRAME, properly intercept
calls to set time of day." that would have messed up time stepping.

- - - - -
f6286404 by Eric S. Raymond at 2015-12-12T06:31:30Z
Yet more replay implementation.

- - - - -


2 changed files:

- ntpd/ntp_intercept.c
- ntpd/ntp_intercept.h


Changes:

=====================================
ntpd/ntp_intercept.c
=====================================
--- a/ntpd/ntp_intercept.c
+++ b/ntpd/ntp_intercept.c
@@ -111,10 +111,8 @@ mocked.
 
 static intercept_mode mode = none;
 
-/* mock the clock state */
-static l_fp replay_time;
-
 static char linebuf[256];
+static int lineno;
 
 intercept_mode intercept_get_mode(void)
 {
@@ -137,6 +135,8 @@ static void get_operation(const char *expect)
     {
 	char *in = fgets(linebuf, sizeof(linebuf), stdin);
 
+	++lineno;
+
 	if (in == NULL) {
 	    fputs("ntpd: replay failed, unexpected EOF\n", stderr);
 	    exit(1);
@@ -246,16 +246,6 @@ void intercept_getconfig(const char *configfile)
     }
 }
 
-void intercept_log(const char *fmt, ...)
-{
-    if (mode == capture) {
-	va_list ap;
-	va_start(ap, fmt);
-	vfprintf(stdout, fmt, ap);
-	va_end(ap);
-    }
-}
-
 /*
  * An lfp used as a full date has an an unsigned seconds part.
  * Invert this with atolfp().
@@ -267,56 +257,113 @@ void intercept_get_systime(const char *legend, l_fp *now)
     struct timespec ts;	/* seconds and nanoseconds */
 
     if (mode == replay) {
-	*now = replay_time;
+	int sec, subsec;
+	char expecting[BUFSIZ];
+	get_operation("systime");
+	if (sscanf(linebuf, "systime %s %d.%d", expecting, &sec, &subsec) != 3) {
+	    fprintf(stderr, "ntpd: garbled systime format, line %d\n", lineno);
+	    exit(1);
+	}
+	else if (strcmp(legend, expecting) == 0) {
+	    fprintf(stderr, "ntpd: expected systime %s on line %d\n",
+		    expecting, lineno);
+	    exit(1);
+	}
+	ts.tv_sec = sec;
+	ts.tv_nsec = subsec;
+	normalize_time(ts, 0, now);
     } else {
 	get_ostime(&ts);
 	normalize_time(ts, sys_fuzz > 0.0 ? ntp_random() : 0, now);
-    }
-
-    if (mode != none)
-	printf("systime %s %s\n", legend, lfpdump(now));
+	if (mode == capture)
+	    printf("systime %s %s\n", legend, lfpdump(now));
 
+    }
 }
 
 long intercept_ntp_random(const char *legend)
 {
-    long rand = ntp_random();
+    long roll;
 
-    /* FIXME: replay logic goes here */
+    if (mode == replay) {
+	char expecting[BUFSIZ];
+	/*
+	 * Presently we're only using this as a check on the call sequence,
+	 * as all the environment-altering functions that call ntp_random()
+	 * are themselves intercepted.
+	 */
+	if (sscanf(linebuf, "random %s %ld", expecting, &roll) != 2) {
+	    fprintf(stderr, "ntpd: garbled random format, line %d\n", lineno);
+	    exit(1);
+	}
+	else if (strcmp(legend, expecting) == 0) {
+	    fprintf(stderr, "ntpd: expected random %s on line %d\n",
+		    expecting, lineno);
+	    exit(1);
+	}
+	return roll;
+    } else {
+	roll = ntp_random();
 
-    if (mode != none)
-	printf("random %s %ld\n", legend, rand);
+	if (mode == capture)
+	    printf("random %s %ld\n", legend, roll);
+    }
 
-    return rand;
+    return roll;
 }
 
 void intercept_timer(void)
 {
-    if (mode != none)
+    if (mode == capture)
 	printf("timer\n");
+    else if (mode == replay)
+	/* probably is not necessary to record this... */
+	get_operation("timer");
     timer();
 }
 
 bool intercept_drift_read(const char *drift_file, double *drift)
 {
-    FILE *fp;
+    if (mode == replay) {
+	float df;
+	get_operation("drift_read");
+	if (strstr(linebuf, "false") != NULL)
+	    return false;
+	/*
+	 * Weirdly, sscanf has no format for reading doubles.
+	 * This could cause an obscure bug in replay if drift 
+	 * ever requires 53 bits of precision as opposed to 24
+	 * (and that's assuming IEEE-754, otherwise things could
+	 * get .
+	 */ 
+	if (sscanf(linebuf, "drift-read %f'", &df) != 1) {
+	    fprintf(stderr, "ntpd: garbled drift-read format, line %d\n",lineno);
+	    exit(1);
+	}
+	*drift = df;
+    } else {
+	FILE *fp;
 
-    if (mode != replay) {
-	if ((fp = fopen(drift_file, "r")) == NULL)
+	if ((fp = fopen(drift_file, "r")) == NULL) {
+	    if (mode == capture)
+		printf("drift-read false\n");
 	    return false;
+	}
 
 	if (fscanf(fp, "%lf", drift) != 1) {
 	    msyslog(LOG_ERR,
 		    "format error frequency file %s",
 		    drift_file);
 	    fclose(fp);
+	    if (mode == capture)
+		printf("drift-read false\n");
 	    return false;
 	}
 	fclose(fp);
-    }
 
-    if (mode != none)
-	printf("drift-read %.3f\n", *drift);
+	if (mode == capture)
+	    printf("drift-read %.3f\n", *drift);
+    }
 
     return true;
 }
@@ -407,13 +454,15 @@ int intercept_ntp_adjtime(struct timex *tx)
 
 int intercept_set_tod(struct timespec *tvs)
 {
-    if (mode != none)
-	printf("set_tod %ld %ld\n", (long)tvs->tv_sec, tvs->tv_nsec);
-
-    if (mode == replay)
+    if (mode == replay) {
+	get_operation("set_tod");
+	/* FIXME: more replay logic goes here */
+    }
+    else {
+	if (mode == capture)
+	    printf("set_tod %ld %ld\n", (long)tvs->tv_sec, tvs->tv_nsec);
 	return ntp_set_tod(tvs);
-
-    normalize_time(*tvs, 9, &replay_time);
+    }
     return 0;
 }
 
@@ -521,7 +570,7 @@ void intercept_exit(int sig)
     if (mode != none)
 	printf("finish %d\n", sig);
 
-    exit(sig);
+    exit(0);
 }
 
 /* end */


=====================================
ntpd/ntp_intercept.h
=====================================
--- a/ntpd/ntp_intercept.h
+++ b/ntpd/ntp_intercept.h
@@ -11,20 +11,11 @@
 # include <sys/timex.h>
 #endif
 
-/* Macro for declaring function with printf-like arguments. */
-# if __GNUC__ >= 3 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7)
-#define PRINTF_FUNC(format_index, arg_index) \
-    __attribute__((__format__(__printf__, format_index, arg_index)))
-# else
-#define PRINTF_FUNC(format_index, arg_indx)
-#endif
-
 typedef enum {none, capture, replay} intercept_mode;
 
 intercept_mode intercept_get_mode(void);
 void intercept_set_mode(intercept_mode);
 
-PRINTF_FUNC(1, 2) void intercept_log(const char *, ...);
 void intercept_argparse(int *, char ***);
 void intercept_getconfig(const char *);
 void intercept_get_systime(const char *, l_fp *);



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/compare/9ddcb323e89f0250d82441365f0618c56cab421e...f62864046ae8da21aeddb4d79827f213452a0567
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ntpsec.org/pipermail/vc/attachments/20151212/760ed78b/attachment.html>


More information about the vc mailing list