Blame SOURCES/0085-libuuid-Implement-continuous-clock-handling-for-time.patch

257b8c
From ce49a152fd8a830b1c9c0a8b745ea71feff71697 Mon Sep 17 00:00:00 2001
257b8c
From: Michael Trapp <michael.trapp@sap.com>
257b8c
Date: Mon, 20 Jun 2022 17:10:36 +0200
257b8c
Subject: libuuid: Implement continuous clock handling for time based UUIDs
257b8c
257b8c
In a uuidd setup, the daemon is a singleton and can maintain it's own
257b8c
resources for time based UUID generation. This requires a dedicated
257b8c
'clock sequence range' but does not need any further lock/update of
257b8c
the LIBUUID_CLOCK_FILE from uuidd. The range of available clock values
257b8c
is extended by a continuous handling of the clock updates - instead of
257b8c
updating the value to the current timestamp, it is incremented by
257b8c
the number of requested UUIDs.
257b8c
257b8c
[kzak@redhat.com: - backport from upstream v2.39 to to RHEL-8]
257b8c
257b8c
Upstream: http://github.com/util-linux/util-linux/commit/3cfba7d39b66eff4307218fefd8bb34bb1621f83
257b8c
Addresses: https://bugzilla.redhat.com/show_bug.cgi?id=2141969
257b8c
Signed-off-by: Karel Zak <kzak@redhat.com>
257b8c
---
257b8c
 libuuid/src/gen_uuid.c  | 91 ++++++++++++++++++++++++++++++++++++++---
257b8c
 libuuid/src/libuuid.sym |  1 +
257b8c
 libuuid/src/uuidd.h     |  1 +
257b8c
 misc-utils/uuidd.8.in   | 12 ++++++
257b8c
 misc-utils/uuidd.c      | 86 ++++++++++++++++++++++++++++++++++++--
257b8c
 5 files changed, 182 insertions(+), 9 deletions(-)
257b8c
257b8c
diff --git a/libuuid/src/gen_uuid.c b/libuuid/src/gen_uuid.c
257b8c
index 27c135db5..f557053f7 100644
257b8c
--- a/libuuid/src/gen_uuid.c
257b8c
+++ b/libuuid/src/gen_uuid.c
257b8c
@@ -209,6 +209,8 @@ static int get_node_id(unsigned char *node_id)
257b8c
 
257b8c
 /* Assume that the gettimeofday() has microsecond granularity */
257b8c
 #define MAX_ADJUSTMENT 10
257b8c
+/* Reserve a clock_seq value for the 'continuous clock' implementation */
257b8c
+#define CLOCK_SEQ_CONT 0
257b8c
 
257b8c
 /*
257b8c
  * Get clock from global sequence clock counter.
257b8c
@@ -275,8 +277,10 @@ static int get_clock(uint32_t *clock_high, uint32_t *clock_low,
257b8c
 	}
257b8c
 
257b8c
 	if ((last.tv_sec == 0) && (last.tv_usec == 0)) {
257b8c
-		random_get_bytes(&clock_seq, sizeof(clock_seq));
257b8c
-		clock_seq &= 0x3FFF;
257b8c
+		do {
257b8c
+			random_get_bytes(&clock_seq, sizeof(clock_seq));
257b8c
+			clock_seq &= 0x3FFF;
257b8c
+		} while (clock_seq == CLOCK_SEQ_CONT);
257b8c
 		gettimeofday(&last, NULL);
257b8c
 		last.tv_sec--;
257b8c
 	}
257b8c
@@ -286,7 +290,9 @@ try_again:
257b8c
 	if ((tv.tv_sec < last.tv_sec) ||
257b8c
 	    ((tv.tv_sec == last.tv_sec) &&
257b8c
 	     (tv.tv_usec < last.tv_usec))) {
257b8c
-		clock_seq = (clock_seq+1) & 0x3FFF;
257b8c
+		do {
257b8c
+			clock_seq = (clock_seq+1) & 0x3FFF;
257b8c
+		} while (clock_seq == CLOCK_SEQ_CONT);
257b8c
 		adjustment = 0;
257b8c
 		last = tv;
257b8c
 	} else if ((tv.tv_sec == last.tv_sec) &&
257b8c
@@ -331,6 +337,64 @@ try_again:
257b8c
 	return ret;
257b8c
 }
257b8c
 
257b8c
+/*
257b8c
+ * Get current time in 100ns ticks.
257b8c
+ */
257b8c
+static uint64_t get_clock_counter(void)
257b8c
+{
257b8c
+	struct timeval tv;
257b8c
+	uint64_t clock_reg;
257b8c
+
257b8c
+	gettimeofday(&tv, NULL);
257b8c
+	clock_reg = tv.tv_usec*10;
257b8c
+	clock_reg += ((uint64_t) tv.tv_sec) * 10000000ULL;
257b8c
+
257b8c
+	return clock_reg;
257b8c
+}
257b8c
+
257b8c
+/*
257b8c
+ * Get continuous clock value.
257b8c
+ *
257b8c
+ * Return -1 if there is no further clock counter available,
257b8c
+ * otherwise return 0.
257b8c
+ *
257b8c
+ * This implementation doesn't deliver clock counters based on
257b8c
+ * the current time because last_clock_reg is only incremented
257b8c
+ * by the number of requested UUIDs.
257b8c
+ * max_clock_offset is used to limit the offset of last_clock_reg.
257b8c
+ */
257b8c
+static int get_clock_cont(uint32_t *clock_high,
257b8c
+			  uint32_t *clock_low,
257b8c
+			  int num,
257b8c
+			  uint32_t max_clock_offset)
257b8c
+{
257b8c
+	/* 100ns based time offset according to RFC 4122. 4.1.4. */
257b8c
+	const uint64_t reg_offset = (((uint64_t) 0x01B21DD2) << 32) + 0x13814000;
257b8c
+	static uint64_t last_clock_reg = 0;
257b8c
+	uint64_t clock_reg;
257b8c
+
257b8c
+	if (last_clock_reg == 0)
257b8c
+		last_clock_reg = get_clock_counter();
257b8c
+
257b8c
+	clock_reg = get_clock_counter();
257b8c
+	if (max_clock_offset) {
257b8c
+		uint64_t clock_offset = max_clock_offset * 10000000ULL;
257b8c
+		if (last_clock_reg < (clock_reg - clock_offset))
257b8c
+			last_clock_reg = clock_reg - clock_offset;
257b8c
+	}
257b8c
+
257b8c
+	clock_reg += MAX_ADJUSTMENT;
257b8c
+
257b8c
+	if ((last_clock_reg + num) >= clock_reg)
257b8c
+		return -1;
257b8c
+
257b8c
+	*clock_high = (last_clock_reg + reg_offset) >> 32;
257b8c
+	*clock_low = last_clock_reg + reg_offset;
257b8c
+	last_clock_reg += num;
257b8c
+
257b8c
+	return 0;
257b8c
+}
257b8c
+
257b8c
 #if defined(HAVE_UUIDD) && defined(HAVE_SYS_UN_H)
257b8c
 
257b8c
 /*
257b8c
@@ -403,7 +467,7 @@ static int get_uuid_via_daemon(int op __attribute__((__unused__)),
257b8c
 }
257b8c
 #endif
257b8c
 
257b8c
-int __uuid_generate_time(uuid_t out, int *num)
257b8c
+static int __uuid_generate_time_internal(uuid_t out, int *num, uint32_t cont_offset)
257b8c
 {
257b8c
 	static unsigned char node_id[6];
257b8c
 	static int has_init = 0;
257b8c
@@ -423,7 +487,14 @@ int __uuid_generate_time(uuid_t out, int *num)
257b8c
 		}
257b8c
 		has_init = 1;
257b8c
 	}
257b8c
-	ret = get_clock(&clock_mid, &uu.time_low, &uu.clock_seq, num);
257b8c
+	if (cont_offset) {
257b8c
+		ret = get_clock_cont(&clock_mid, &uu.time_low, *num, cont_offset);
257b8c
+		uu.clock_seq = CLOCK_SEQ_CONT;
257b8c
+		if (ret != 0)	/* fallback to previous implpementation */
257b8c
+			ret = get_clock(&clock_mid, &uu.time_low, &uu.clock_seq, num);
257b8c
+	} else {
257b8c
+		ret = get_clock(&clock_mid, &uu.time_low, &uu.clock_seq, num);
257b8c
+	}
257b8c
 	uu.clock_seq |= 0x8000;
257b8c
 	uu.time_mid = (uint16_t) clock_mid;
257b8c
 	uu.time_hi_and_version = ((clock_mid >> 16) & 0x0FFF) | 0x1000;
257b8c
@@ -432,6 +503,16 @@ int __uuid_generate_time(uuid_t out, int *num)
257b8c
 	return ret;
257b8c
 }
257b8c
 
257b8c
+int __uuid_generate_time(uuid_t out, int *num)
257b8c
+{
257b8c
+	return __uuid_generate_time_internal(out, num, 0);
257b8c
+}
257b8c
+
257b8c
+int __uuid_generate_time_cont(uuid_t out, int *num, uint32_t cont_offset)
257b8c
+{
257b8c
+	return __uuid_generate_time_internal(out, num, cont_offset);
257b8c
+}
257b8c
+
257b8c
 /*
257b8c
  * Generate time-based UUID and store it to @out
257b8c
  *
257b8c
diff --git a/libuuid/src/libuuid.sym b/libuuid/src/libuuid.sym
257b8c
index 9e3e80035..0a072b703 100644
257b8c
--- a/libuuid/src/libuuid.sym
257b8c
+++ b/libuuid/src/libuuid.sym
257b8c
@@ -51,6 +51,7 @@ global:
257b8c
 UUIDD_PRIVATE {
257b8c
 global:
257b8c
 	__uuid_generate_time;
257b8c
+	__uuid_generate_time_cont;
257b8c
 	__uuid_generate_random;
257b8c
 local:
257b8c
 	*;
257b8c
diff --git a/libuuid/src/uuidd.h b/libuuid/src/uuidd.h
257b8c
index e55c86f2f..14a01ade2 100644
257b8c
--- a/libuuid/src/uuidd.h
257b8c
+++ b/libuuid/src/uuidd.h
257b8c
@@ -50,5 +50,6 @@
257b8c
 
257b8c
 extern int __uuid_generate_time(uuid_t out, int *num);
257b8c
 extern void __uuid_generate_random(uuid_t out, int *num);
257b8c
+extern int __uuid_generate_time_cont(uuid_t out, int *num, uint32_t cont);
257b8c
 
257b8c
 #endif /* _UUID_UUID_H */
257b8c
diff --git a/misc-utils/uuidd.8.in b/misc-utils/uuidd.8.in
257b8c
index 0a5cf471b..28bcb48b5 100644
257b8c
--- a/misc-utils/uuidd.8.in
257b8c
+++ b/misc-utils/uuidd.8.in
257b8c
@@ -16,6 +16,18 @@ universally unique identifiers (UUIDs), especially time-based UUIDs,
257b8c
 in a secure and guaranteed-unique fashion, even in the face of large
257b8c
 numbers of threads running on different CPUs trying to grab UUIDs.
257b8c
 .SH OPTIONS
257b8c
+
257b8c
+.TP
257b8c
+.BR \-C , " \-\-cont\-clock " [\fInumber\fR]
257b8c
+Activate continuous clock handling for time based UUIDs. uuidd could use all
257b8c
+possible clock values, beginning with the daemon's start time. The optional
257b8c
+argument can be used to set a value for the max_clock_offset. This gurantees,
257b8c
+that a clock value of a UUID will always be within the range of the
257b8c
+max_clock_offset. '-C' or '--cont-clock' enables the feature with a default
257b8c
+max_clock_offset of 2 hours. '-C<NUM>[hd]' or '--cont-clock=<NUM>[hd]' enables
257b8c
+the feature with a max_clock_offset of NUM seconds. In case of an appended h or
257b8c
+d, the NUM value is read in hours or days. The minimum value is 60 seconds, the
257b8c
+maximum value is 365 days.
257b8c
 .TP
257b8c
 .BR \-d , " \-\-debug "
257b8c
 Run uuidd in debugging mode.  This prevents uuidd from running as a daemon.
257b8c
diff --git a/misc-utils/uuidd.c b/misc-utils/uuidd.c
257b8c
index 8b83d91c0..e3c0abad7 100644
257b8c
--- a/misc-utils/uuidd.c
257b8c
+++ b/misc-utils/uuidd.c
257b8c
@@ -49,6 +49,8 @@ struct uuidd_cxt_t {
257b8c
 	const char	*cleanup_pidfile;
257b8c
 	const char	*cleanup_socket;
257b8c
 	uint32_t	timeout;
257b8c
+	uint32_t	cont_clock_offset;
257b8c
+
257b8c
 	unsigned int	debug: 1,
257b8c
 			quiet: 1,
257b8c
 			no_fork: 1,
257b8c
@@ -73,6 +75,8 @@ static void __attribute__((__noreturn__)) usage(void)
257b8c
 	fputs(_(" -P, --no-pid            do not create pid file\n"), out);
257b8c
 	fputs(_(" -F, --no-fork           do not daemonize using double-fork\n"), out);
257b8c
 	fputs(_(" -S, --socket-activation do not create listening socket\n"), out);
257b8c
+	fputs(_(" -C, --cont-clock[=<NUM>[hd]]\n"), out);
257b8c
+	fputs(_("                         activate continuous clock handling\n"), out);
257b8c
 	fputs(_(" -d, --debug             run in debugging mode\n"), out);
257b8c
 	fputs(_(" -q, --quiet             turn on quiet mode\n"), out);
257b8c
 	fputs(USAGE_SEPARATOR, out);
257b8c
@@ -401,6 +405,15 @@ static void server_loop(const char *socket_path, const char *pidfile_path,
257b8c
 	pfd[POLLFD_SOCKET].fd = s;
257b8c
 	pfd[POLLFD_SIGNAL].events = pfd[POLLFD_SOCKET].events = POLLIN | POLLERR | POLLHUP;
257b8c
 
257b8c
+	num = 1;
257b8c
+	if (uuidd_cxt->cont_clock_offset) {
257b8c
+		/* trigger initialization */
257b8c
+		(void) __uuid_generate_time_cont(uu, &num, uuidd_cxt->cont_clock_offset);
257b8c
+		if (uuidd_cxt->debug)
257b8c
+			fprintf(stderr, _("max_clock_offset = %u sec\n"),
257b8c
+				uuidd_cxt->cont_clock_offset);
257b8c
+	}
257b8c
+
257b8c
 	while (1) {
257b8c
 		ret = poll(pfd, ARRAY_SIZE(pfd),
257b8c
 				uuidd_cxt->timeout ?
257b8c
@@ -458,7 +471,7 @@ static void server_loop(const char *socket_path, const char *pidfile_path,
257b8c
 			break;
257b8c
 		case UUIDD_OP_TIME_UUID:
257b8c
 			num = 1;
257b8c
-			__uuid_generate_time(uu, &num);
257b8c
+			__uuid_generate_time_cont(uu, &num, uuidd_cxt->cont_clock_offset);
257b8c
 			if (uuidd_cxt->debug) {
257b8c
 				uuid_unparse(uu, str);
257b8c
 				fprintf(stderr, _("Generated time UUID: %s\n"), str);
257b8c
@@ -477,7 +490,7 @@ static void server_loop(const char *socket_path, const char *pidfile_path,
257b8c
 			reply_len = sizeof(uu);
257b8c
 			break;
257b8c
 		case UUIDD_OP_BULK_TIME_UUID:
257b8c
-			__uuid_generate_time(uu, &num);
257b8c
+			 __uuid_generate_time_cont(uu, &num, uuidd_cxt->cont_clock_offset);
257b8c
 			if (uuidd_cxt->debug) {
257b8c
 				uuid_unparse(uu, str);
257b8c
 				fprintf(stderr, P_("Generated time UUID %s "
257b8c
@@ -530,6 +543,64 @@ static void __attribute__ ((__noreturn__)) unexpected_size(int size)
257b8c
 	errx(EXIT_FAILURE, _("Unexpected reply length from server %d"), size);
257b8c
 }
257b8c
 
257b8c
+/* Backport from v2.39 lib/strutils.c */
257b8c
+static int ul_strtos64(const char *str, int64_t *num, int base)
257b8c
+{
257b8c
+	char *end = NULL;
257b8c
+
257b8c
+	if (str == NULL || *str == '\0')
257b8c
+		return -(errno = EINVAL);
257b8c
+
257b8c
+	errno = 0;
257b8c
+	*num = (int64_t) strtoimax(str, &end, base);
257b8c
+
257b8c
+	if (errno != 0)
257b8c
+		return -errno;
257b8c
+	if (str == end || (end && *end))
257b8c
+		return -(errno = EINVAL);
257b8c
+	return 0;
257b8c
+}
257b8c
+
257b8c
+/* Backport from v2.39 lib/strutils.c */
257b8c
+static int64_t str2num_or_err(const char *str, int base, const char *errmesg,
257b8c
+			     int64_t low, int64_t up)
257b8c
+{
257b8c
+	int64_t num = 0;
257b8c
+	int rc;
257b8c
+
257b8c
+	rc = ul_strtos64(str, &num, base);
257b8c
+	if (rc == 0 && ((low && num < low) || (up && num > up)))
257b8c
+		rc = -(errno = ERANGE);
257b8c
+
257b8c
+	if (rc) {
257b8c
+		if (errno == ERANGE)
257b8c
+			err(EXIT_FAILURE, "%s: '%s'", errmesg, str);
257b8c
+		errx(EXIT_FAILURE, "%s: '%s'", errmesg, str);
257b8c
+	}
257b8c
+	return num;
257b8c
+}
257b8c
+
257b8c
+static uint32_t parse_cont_clock(char *arg)
257b8c
+{
257b8c
+	uint32_t min_val = 60,
257b8c
+		 max_val = (3600 * 24 * 365),
257b8c
+		 factor = 1;
257b8c
+	char *p = &arg[strlen(arg)-1];
257b8c
+
257b8c
+	if ('h' == *p) {
257b8c
+		*p = '\0';
257b8c
+		factor = 3600;
257b8c
+		min_val = 1;
257b8c
+	}
257b8c
+	if ('d' == *p) {
257b8c
+		*p = '\0';
257b8c
+		factor = 24 * 3600;
257b8c
+		min_val = 1;
257b8c
+	}
257b8c
+	return factor * str2num_or_err(optarg, 10, _("failed to parse --cont-clock/-C"),
257b8c
+				       min_val, max_val / factor);
257b8c
+}
257b8c
+
257b8c
 int main(int argc, char **argv)
257b8c
 {
257b8c
 	const char	*socket_path = UUIDD_SOCKET_PATH;
257b8c
@@ -543,7 +614,7 @@ int main(int argc, char **argv)
257b8c
 	int		no_pid = 0;
257b8c
 	int		s_flag = 0;
257b8c
 
257b8c
-	struct uuidd_cxt_t uuidd_cxt = { .timeout = 0 };
257b8c
+	struct uuidd_cxt_t uuidd_cxt = { .timeout = 0, .cont_clock_offset = 0 };
257b8c
 
257b8c
 	static const struct option longopts[] = {
257b8c
 		{"pid", required_argument, NULL, 'p'},
257b8c
@@ -556,6 +627,7 @@ int main(int argc, char **argv)
257b8c
 		{"no-pid", no_argument, NULL, 'P'},
257b8c
 		{"no-fork", no_argument, NULL, 'F'},
257b8c
 		{"socket-activation", no_argument, NULL, 'S'},
257b8c
+		{"cont-clock", optional_argument, NULL, 'C'},
257b8c
 		{"debug", no_argument, NULL, 'd'},
257b8c
 		{"quiet", no_argument, NULL, 'q'},
257b8c
 		{"version", no_argument, NULL, 'V'},
257b8c
@@ -576,10 +648,16 @@ int main(int argc, char **argv)
257b8c
 	atexit(close_stdout);
257b8c
 
257b8c
 	while ((c =
257b8c
-		getopt_long(argc, argv, "p:s:T:krtn:PFSdqVh", longopts,
257b8c
+		getopt_long(argc, argv, "p:s:T:krtn:PFSC::dqVh", longopts,
257b8c
 			    NULL)) != -1) {
257b8c
 		err_exclusive_options(c, longopts, excl, excl_st);
257b8c
 		switch (c) {
257b8c
+		case 'C':
257b8c
+			if (optarg != NULL)
257b8c
+				uuidd_cxt.cont_clock_offset = parse_cont_clock(optarg);
257b8c
+			else
257b8c
+				uuidd_cxt.cont_clock_offset = 7200; /* default 2h */
257b8c
+			break;
257b8c
 		case 'd':
257b8c
 			uuidd_cxt.debug = 1;
257b8c
 			break;
257b8c
-- 
257b8c
2.38.1
257b8c