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

d017ad
From a3b3df8d0891ff83b61f62020652798d365299c0 Mon Sep 17 00:00:00 2001
d017ad
From: Michael Trapp <michael.trapp@sap.com>
d017ad
Date: Mon, 20 Jun 2022 17:10:36 +0200
d017ad
Subject: libuuid: Implement continuous clock handling for time based UUIDs
d017ad
d017ad
In a uuidd setup, the daemon is a singleton and can maintain it's own
d017ad
resources for time based UUID generation. This requires a dedicated
d017ad
'clock sequence range' but does not need any further lock/update of
d017ad
the LIBUUID_CLOCK_FILE from uuidd. The range of available clock values
d017ad
is extended by a continuous handling of the clock updates - instead of
d017ad
updating the value to the current timestamp, it is incremented by
d017ad
the number of requested UUIDs.
d017ad
d017ad
Upstream: http://github.com/util-linux/util-linux/commit/3cfba7d39b66eff4307218fefd8bb34bb1621f83
d017ad
Addresses: https://bugzilla.redhat.com/show_bug.cgi?id=2141970
d017ad
Signed-off-by: Karel Zak <kzak@redhat.com>
d017ad
---
d017ad
 libuuid/src/gen_uuid.c  | 91 ++++++++++++++++++++++++++++++++++++++---
d017ad
 libuuid/src/libuuid.sym |  1 +
d017ad
 libuuid/src/uuidd.h     |  1 +
d017ad
 misc-utils/uuidd.8.adoc |  3 ++
d017ad
 misc-utils/uuidd.c      | 54 +++++++++++++++++++++---
d017ad
 5 files changed, 140 insertions(+), 10 deletions(-)
d017ad
d017ad
diff --git a/libuuid/src/gen_uuid.c b/libuuid/src/gen_uuid.c
d017ad
index d353fa1a0..ac682a1d8 100644
d017ad
--- a/libuuid/src/gen_uuid.c
d017ad
+++ b/libuuid/src/gen_uuid.c
d017ad
@@ -209,6 +209,8 @@ static int get_node_id(unsigned char *node_id)
d017ad
 
d017ad
 /* Assume that the gettimeofday() has microsecond granularity */
d017ad
 #define MAX_ADJUSTMENT 10
d017ad
+/* Reserve a clock_seq value for the 'continuous clock' implementation */
d017ad
+#define CLOCK_SEQ_CONT 0
d017ad
 
d017ad
 /*
d017ad
  * Get clock from global sequence clock counter.
d017ad
@@ -275,8 +277,10 @@ static int get_clock(uint32_t *clock_high, uint32_t *clock_low,
d017ad
 	}
d017ad
 
d017ad
 	if ((last.tv_sec == 0) && (last.tv_usec == 0)) {
d017ad
-		ul_random_get_bytes(&clock_seq, sizeof(clock_seq));
d017ad
-		clock_seq &= 0x3FFF;
d017ad
+		do {
d017ad
+			ul_random_get_bytes(&clock_seq, sizeof(clock_seq));
d017ad
+			clock_seq &= 0x3FFF;
d017ad
+		} while (clock_seq == CLOCK_SEQ_CONT);
d017ad
 		gettimeofday(&last, NULL);
d017ad
 		last.tv_sec--;
d017ad
 	}
d017ad
@@ -286,7 +290,9 @@ try_again:
d017ad
 	if ((tv.tv_sec < last.tv_sec) ||
d017ad
 	    ((tv.tv_sec == last.tv_sec) &&
d017ad
 	     (tv.tv_usec < last.tv_usec))) {
d017ad
-		clock_seq = (clock_seq+1) & 0x3FFF;
d017ad
+		do {
d017ad
+			clock_seq = (clock_seq+1) & 0x3FFF;
d017ad
+		} while (clock_seq == CLOCK_SEQ_CONT);
d017ad
 		adjustment = 0;
d017ad
 		last = tv;
d017ad
 	} else if ((tv.tv_sec == last.tv_sec) &&
d017ad
@@ -331,6 +337,64 @@ try_again:
d017ad
 	return ret;
d017ad
 }
d017ad
 
d017ad
+/*
d017ad
+ * Get current time in 100ns ticks.
d017ad
+ */
d017ad
+static uint64_t get_clock_counter(void)
d017ad
+{
d017ad
+	struct timeval tv;
d017ad
+	uint64_t clock_reg;
d017ad
+
d017ad
+	gettimeofday(&tv, NULL);
d017ad
+	clock_reg = tv.tv_usec*10;
d017ad
+	clock_reg += ((uint64_t) tv.tv_sec) * 10000000ULL;
d017ad
+
d017ad
+	return clock_reg;
d017ad
+}
d017ad
+
d017ad
+/*
d017ad
+ * Get continuous clock value.
d017ad
+ *
d017ad
+ * Return -1 if there is no further clock counter available,
d017ad
+ * otherwise return 0.
d017ad
+ *
d017ad
+ * This implementation doesn't deliver clock counters based on
d017ad
+ * the current time because last_clock_reg is only incremented
d017ad
+ * by the number of requested UUIDs.
d017ad
+ * max_clock_offset is used to limit the offset of last_clock_reg.
d017ad
+ */
d017ad
+static int get_clock_cont(uint32_t *clock_high,
d017ad
+			  uint32_t *clock_low,
d017ad
+			  int num,
d017ad
+			  uint32_t max_clock_offset)
d017ad
+{
d017ad
+	/* 100ns based time offset according to RFC 4122. 4.1.4. */
d017ad
+	const uint64_t reg_offset = (((uint64_t) 0x01B21DD2) << 32) + 0x13814000;
d017ad
+	static uint64_t last_clock_reg = 0;
d017ad
+	uint64_t clock_reg;
d017ad
+
d017ad
+	if (last_clock_reg == 0)
d017ad
+		last_clock_reg = get_clock_counter();
d017ad
+
d017ad
+	clock_reg = get_clock_counter();
d017ad
+	if (max_clock_offset) {
d017ad
+		uint64_t clock_offset = max_clock_offset * 10000000ULL;
d017ad
+		if (last_clock_reg < (clock_reg - clock_offset))
d017ad
+			last_clock_reg = clock_reg - clock_offset;
d017ad
+	}
d017ad
+
d017ad
+	clock_reg += MAX_ADJUSTMENT;
d017ad
+
d017ad
+	if ((last_clock_reg + num) >= clock_reg)
d017ad
+		return -1;
d017ad
+
d017ad
+	*clock_high = (last_clock_reg + reg_offset) >> 32;
d017ad
+	*clock_low = last_clock_reg + reg_offset;
d017ad
+	last_clock_reg += num;
d017ad
+
d017ad
+	return 0;
d017ad
+}
d017ad
+
d017ad
 #if defined(HAVE_UUIDD) && defined(HAVE_SYS_UN_H)
d017ad
 
d017ad
 /*
d017ad
@@ -403,7 +467,7 @@ static int get_uuid_via_daemon(int op __attribute__((__unused__)),
d017ad
 }
d017ad
 #endif
d017ad
 
d017ad
-int __uuid_generate_time(uuid_t out, int *num)
d017ad
+static int __uuid_generate_time_internal(uuid_t out, int *num, uint32_t cont_offset)
d017ad
 {
d017ad
 	static unsigned char node_id[6];
d017ad
 	static int has_init = 0;
d017ad
@@ -423,7 +487,14 @@ int __uuid_generate_time(uuid_t out, int *num)
d017ad
 		}
d017ad
 		has_init = 1;
d017ad
 	}
d017ad
-	ret = get_clock(&clock_mid, &uu.time_low, &uu.clock_seq, num);
d017ad
+	if (cont_offset) {
d017ad
+		ret = get_clock_cont(&clock_mid, &uu.time_low, *num, cont_offset);
d017ad
+		uu.clock_seq = CLOCK_SEQ_CONT;
d017ad
+		if (ret != 0)	/* fallback to previous implpementation */
d017ad
+			ret = get_clock(&clock_mid, &uu.time_low, &uu.clock_seq, num);
d017ad
+	} else {
d017ad
+		ret = get_clock(&clock_mid, &uu.time_low, &uu.clock_seq, num);
d017ad
+	}
d017ad
 	uu.clock_seq |= 0x8000;
d017ad
 	uu.time_mid = (uint16_t) clock_mid;
d017ad
 	uu.time_hi_and_version = ((clock_mid >> 16) & 0x0FFF) | 0x1000;
d017ad
@@ -432,6 +503,16 @@ int __uuid_generate_time(uuid_t out, int *num)
d017ad
 	return ret;
d017ad
 }
d017ad
 
d017ad
+int __uuid_generate_time(uuid_t out, int *num)
d017ad
+{
d017ad
+	return __uuid_generate_time_internal(out, num, 0);
d017ad
+}
d017ad
+
d017ad
+int __uuid_generate_time_cont(uuid_t out, int *num, uint32_t cont_offset)
d017ad
+{
d017ad
+	return __uuid_generate_time_internal(out, num, cont_offset);
d017ad
+}
d017ad
+
d017ad
 /*
d017ad
  * Generate time-based UUID and store it to @out
d017ad
  *
d017ad
diff --git a/libuuid/src/libuuid.sym b/libuuid/src/libuuid.sym
d017ad
index 342453368..96372a857 100644
d017ad
--- a/libuuid/src/libuuid.sym
d017ad
+++ b/libuuid/src/libuuid.sym
d017ad
@@ -60,6 +60,7 @@ global:
d017ad
 UUIDD_PRIVATE {
d017ad
 global:
d017ad
 	__uuid_generate_time;
d017ad
+	__uuid_generate_time_cont;
d017ad
 	__uuid_generate_random;
d017ad
 local:
d017ad
 	*;
d017ad
diff --git a/libuuid/src/uuidd.h b/libuuid/src/uuidd.h
d017ad
index fbe821ff3..f76acc8b2 100644
d017ad
--- a/libuuid/src/uuidd.h
d017ad
+++ b/libuuid/src/uuidd.h
d017ad
@@ -49,6 +49,7 @@
d017ad
 #define UUIDD_MAX_OP			UUIDD_OP_BULK_RANDOM_UUID
d017ad
 
d017ad
 extern int __uuid_generate_time(uuid_t out, int *num);
d017ad
+extern int __uuid_generate_time_cont(uuid_t out, int *num, uint32_t cont);
d017ad
 extern int __uuid_generate_random(uuid_t out, int *num);
d017ad
 
d017ad
 #endif /* _UUID_UUID_H */
d017ad
diff --git a/misc-utils/uuidd.8.adoc b/misc-utils/uuidd.8.adoc
d017ad
index 77ee2b3e6..c87125901 100644
d017ad
--- a/misc-utils/uuidd.8.adoc
d017ad
+++ b/misc-utils/uuidd.8.adoc
d017ad
@@ -24,6 +24,9 @@ The *uuidd* daemon is used by the UUID library to generate universally unique id
d017ad
 
d017ad
 == OPTIONS
d017ad
 
d017ad
+*-C*, *--cont-clock* _opt_arg_::
d017ad
+Activate continuous clock handling for time based UUIDs. *uuidd* could use all possible clock values, beginning with the daemon's start time. The optional argument can be used to set a value for the max_clock_offset. This gurantees, that a clock value of a UUID will always be within the range of the max_clock_offset. '-C' or '--cont-clock' enables the feature with a default max_clock_offset of 2 hours. '-C<NUM>[hd]' or '--cont-clock=<NUM>[hd]' enables the feature with a max_clock_offset of NUM seconds. In case of an appended h or d, the NUM value is read in hours or days. The minimum value is 60 seconds, the maximum value is 365 days.
d017ad
+
d017ad
 *-d*, *--debug*::
d017ad
 Run uuidd in debugging mode. This prevents uuidd from running as a daemon.
d017ad
 
d017ad
diff --git a/misc-utils/uuidd.c b/misc-utils/uuidd.c
d017ad
index 78a37d2e8..b859ccb8c 100644
d017ad
--- a/misc-utils/uuidd.c
d017ad
+++ b/misc-utils/uuidd.c
d017ad
@@ -72,6 +72,8 @@ struct uuidd_cxt_t {
d017ad
 	const char	*cleanup_pidfile;
d017ad
 	const char	*cleanup_socket;
d017ad
 	uint32_t	timeout;
d017ad
+	uint32_t	cont_clock_offset;
d017ad
+
d017ad
 	unsigned int	debug: 1,
d017ad
 			quiet: 1,
d017ad
 			no_fork: 1,
d017ad
@@ -106,6 +108,8 @@ static void __attribute__((__noreturn__)) usage(void)
d017ad
 	fputs(_(" -P, --no-pid            do not create pid file\n"), out);
d017ad
 	fputs(_(" -F, --no-fork           do not daemonize using double-fork\n"), out);
d017ad
 	fputs(_(" -S, --socket-activation do not create listening socket\n"), out);
d017ad
+	fputs(_(" -C, --cont-clock[=<NUM>[hd]]\n"), out);
d017ad
+	fputs(_("                         activate continuous clock handling\n"), out);
d017ad
 	fputs(_(" -d, --debug             run in debugging mode\n"), out);
d017ad
 	fputs(_(" -q, --quiet             turn on quiet mode\n"), out);
d017ad
 	fputs(USAGE_SEPARATOR, out);
d017ad
@@ -438,6 +442,15 @@ static void server_loop(const char *socket_path, const char *pidfile_path,
d017ad
 	pfd[POLLFD_SOCKET].fd = s;
d017ad
 	pfd[POLLFD_SIGNAL].events = pfd[POLLFD_SOCKET].events = POLLIN | POLLERR | POLLHUP;
d017ad
 
d017ad
+	num = 1;
d017ad
+	if (uuidd_cxt->cont_clock_offset) {
d017ad
+		/* trigger initialization */
d017ad
+		(void) __uuid_generate_time_cont(uu, &num, uuidd_cxt->cont_clock_offset);
d017ad
+		if (uuidd_cxt->debug)
d017ad
+			fprintf(stderr, _("max_clock_offset = %u sec\n"),
d017ad
+				uuidd_cxt->cont_clock_offset);
d017ad
+	}
d017ad
+
d017ad
 	while (1) {
d017ad
 		ret = poll(pfd, ARRAY_SIZE(pfd),
d017ad
 				uuidd_cxt->timeout ?
d017ad
@@ -494,7 +507,8 @@ static void server_loop(const char *socket_path, const char *pidfile_path,
d017ad
 			break;
d017ad
 		case UUIDD_OP_TIME_UUID:
d017ad
 			num = 1;
d017ad
-			if (__uuid_generate_time(uu, &num) < 0 && !uuidd_cxt->quiet)
d017ad
+			ret = __uuid_generate_time_cont(uu, &num, uuidd_cxt->cont_clock_offset);
d017ad
+			if (ret < 0 && !uuidd_cxt->quiet)
d017ad
 				warnx(_("failed to open/lock clock counter"));
d017ad
 			if (uuidd_cxt->debug) {
d017ad
 				uuid_unparse(uu, str);
d017ad
@@ -505,7 +519,8 @@ static void server_loop(const char *socket_path, const char *pidfile_path,
d017ad
 			break;
d017ad
 		case UUIDD_OP_RANDOM_UUID:
d017ad
 			num = 1;
d017ad
-			if (__uuid_generate_time(uu, &num) < 0 && !uuidd_cxt->quiet)
d017ad
+			ret = __uuid_generate_time_cont(uu, &num, uuidd_cxt->cont_clock_offset);
d017ad
+			if (ret < 0 && !uuidd_cxt->quiet)
d017ad
 				warnx(_("failed to open/lock clock counter"));
d017ad
 			if (uuidd_cxt->debug) {
d017ad
 				uuid_unparse(uu, str);
d017ad
@@ -515,7 +530,8 @@ static void server_loop(const char *socket_path, const char *pidfile_path,
d017ad
 			reply_len = sizeof(uu);
d017ad
 			break;
d017ad
 		case UUIDD_OP_BULK_TIME_UUID:
d017ad
-			if (__uuid_generate_time(uu, &num) < 0 && !uuidd_cxt->quiet)
d017ad
+			ret = __uuid_generate_time_cont(uu, &num, uuidd_cxt->cont_clock_offset);
d017ad
+			if (ret < 0 && !uuidd_cxt->quiet)
d017ad
 				warnx(_("failed to open/lock clock counter"));
d017ad
 			if (uuidd_cxt->debug) {
d017ad
 				uuid_unparse(uu, str);
d017ad
@@ -567,6 +583,27 @@ static void __attribute__ ((__noreturn__)) unexpected_size(int size)
d017ad
 	errx(EXIT_FAILURE, _("Unexpected reply length from server %d"), size);
d017ad
 }
d017ad
 
d017ad
+static uint32_t parse_cont_clock(char *arg)
d017ad
+{
d017ad
+	uint32_t min_val = 60,
d017ad
+		 max_val = (3600 * 24 * 365),
d017ad
+		 factor = 1;
d017ad
+	char *p = &arg[strlen(arg)-1];
d017ad
+
d017ad
+	if ('h' == *p) {
d017ad
+		*p = '\0';
d017ad
+		factor = 3600;
d017ad
+		min_val = 1;
d017ad
+	}
d017ad
+	if ('d' == *p) {
d017ad
+		*p = '\0';
d017ad
+		factor = 24 * 3600;
d017ad
+		min_val = 1;
d017ad
+	}
d017ad
+	return factor * str2num_or_err(optarg, 10, _("failed to parse --cont-clock/-C"),
d017ad
+				       min_val, max_val / factor);
d017ad
+}
d017ad
+
d017ad
 static void parse_options(int argc, char **argv, struct uuidd_cxt_t *uuidd_cxt,
d017ad
 			  struct uuidd_options_t *uuidd_opts)
d017ad
 {
d017ad
@@ -581,6 +618,7 @@ static void parse_options(int argc, char **argv, struct uuidd_cxt_t *uuidd_cxt,
d017ad
 		{"no-pid", no_argument, NULL, 'P'},
d017ad
 		{"no-fork", no_argument, NULL, 'F'},
d017ad
 		{"socket-activation", no_argument, NULL, 'S'},
d017ad
+		{"cont-clock", optional_argument, NULL, 'C'},
d017ad
 		{"debug", no_argument, NULL, 'd'},
d017ad
 		{"quiet", no_argument, NULL, 'q'},
d017ad
 		{"version", no_argument, NULL, 'V'},
d017ad
@@ -596,9 +634,15 @@ static void parse_options(int argc, char **argv, struct uuidd_cxt_t *uuidd_cxt,
d017ad
 	int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
d017ad
 	int c;
d017ad
 
d017ad
-	while ((c = getopt_long(argc, argv, "p:s:T:krtn:PFSdqVh", longopts, NULL)) != -1) {
d017ad
+	while ((c = getopt_long(argc, argv, "p:s:T:krtn:PFSC::dqVh", longopts, NULL)) != -1) {
d017ad
 		err_exclusive_options(c, longopts, excl, excl_st);
d017ad
 		switch (c) {
d017ad
+		case 'C':
d017ad
+			if (optarg != NULL)
d017ad
+				uuidd_cxt->cont_clock_offset = parse_cont_clock(optarg);
d017ad
+			else
d017ad
+				uuidd_cxt->cont_clock_offset = 7200; /* default 2h */
d017ad
+			break;
d017ad
 		case 'd':
d017ad
 			uuidd_cxt->debug = 1;
d017ad
 			break;
d017ad
@@ -673,7 +717,7 @@ int main(int argc, char **argv)
d017ad
 	char		*cp;
d017ad
 	int		ret;
d017ad
 
d017ad
-	struct uuidd_cxt_t uuidd_cxt = { .timeout = 0 };
d017ad
+	struct uuidd_cxt_t uuidd_cxt = { .timeout = 0, .cont_clock_offset = 0 };
d017ad
 	struct uuidd_options_t uuidd_opts = { .socket_path = UUIDD_SOCKET_PATH };
d017ad
 
d017ad
 	setlocale(LC_ALL, "");
d017ad
-- 
d017ad
2.39.1
d017ad