dcavalca / rpms / util-linux

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