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

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