From 989f5401f3d18a3c1b33f315bb3c1096855894ce Mon Sep 17 00:00:00 2001 From: Francis Deslauriers Date: Thu, 9 Dec 2021 11:44:07 -0500 Subject: [PATCH] Import uuid.h from lttng-tools Signed-off-by: Francis Deslauriers Change-Id: Idbbb9be64fb30efd1fe9393e15d1c4973bcae33d --- include/lttng/uuid.h | 38 ++++++++++++++++++++++++++++++++++++++ src/Kbuild | 5 +++-- src/lttng-uuid.c | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 include/lttng/uuid.h create mode 100644 src/lttng-uuid.c diff --git a/include/lttng/uuid.h b/include/lttng/uuid.h new file mode 100644 index 00000000..76e3ce84 --- /dev/null +++ b/include/lttng/uuid.h @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2011 Mathieu Desnoyers + * + * SPDX-License-Identifier: MIT + * + */ + +#ifndef LTTNG_UUID_H +#define LTTNG_UUID_H + +/* + * Includes final \0. + */ +#define LTTNG_UUID_STR_LEN 37 +#define LTTNG_UUID_LEN 16 +#define LTTNG_UUID_VER 4 + +#define LTTNG_UUID_FMT \ + "%02hhx%02hhx%02hhx%02hhx-%02hhx" \ + "%02hhx-%02hhx%02hhx-%02hhx%02hhx" \ + "-%02hhx%02hhx%02hhx%02hhx%02hhx" \ + "%02hhx" + +#define LTTNG_UUID_FMT_VALUES(uuid) \ + (uuid)[0], (uuid)[1], (uuid)[2], (uuid)[3], (uuid)[4], (uuid)[5], \ + (uuid)[6], (uuid)[7], (uuid)[8], (uuid)[9], (uuid)[10], (uuid)[11], \ + (uuid)[12], (uuid)[13], (uuid)[14], (uuid)[15] + +#define LTTNG_UUID_SCAN_VALUES(uuid) \ + &(uuid)[0], &(uuid)[1], &(uuid)[2], &(uuid)[3], &(uuid)[4], &(uuid)[5], \ + &(uuid)[6], &(uuid)[7], &(uuid)[8], &(uuid)[9], &(uuid)[10], &(uuid)[11], \ + &(uuid)[12], &(uuid)[13], &(uuid)[14], &(uuid)[15] + +typedef unsigned char lttng_uuid[LTTNG_UUID_LEN]; + +int lttng_uuid_from_str(const char *str_in, lttng_uuid uuid_out); + +#endif /* LTTNG_UUID_H */ diff --git a/src/Kbuild b/src/Kbuild index 4aba1cd7..a4f6dddd 100644 --- a/src/Kbuild +++ b/src/Kbuild @@ -71,7 +71,7 @@ lttng-tracer-objs := lib/msgpack/msgpack.o \ lttng-context-need-reschedule.o \ lttng-calibrate.o \ lttng-context-hostname.o \ - lttng-context-callstack.o \ + lttng-context-callstack.o \ probes/lttng.o \ lttng-tracker-id.o \ lttng-bytecode.o lttng-bytecode-interpreter.o \ @@ -79,7 +79,8 @@ lttng-tracer-objs := lib/msgpack/msgpack.o \ lttng-bytecode-validator.o \ probes/lttng-probe-user.o \ lttng-tp-mempool.o \ - lttng-event-notifier-notification.o + lttng-event-notifier-notification.o \ + lttng-uuid.o lttng-wrapper-objs := wrapper/page_alloc.o \ wrapper/random.o \ diff --git a/src/lttng-uuid.c b/src/lttng-uuid.c new file mode 100644 index 00000000..151b2fea --- /dev/null +++ b/src/lttng-uuid.c @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2018 Jérémie Galarneau + * Copyright (C) 2019 Michael Jeanson + * + * SPDX-License-Identifier: LGPL-2.1-only + * + */ + +#include +#include + +int lttng_uuid_from_str(const char *str_in, lttng_uuid uuid_out) +{ + int ret = 0; + lttng_uuid uuid_scan; + + if ((str_in == NULL) || (uuid_out == NULL)) { + ret = -1; + goto end; + } + + if (strnlen(str_in, LTTNG_UUID_STR_LEN) != LTTNG_UUID_STR_LEN - 1) { + ret = -1; + goto end; + } + + /* Scan to a temporary location in case of a partial match. */ + if (sscanf(str_in, LTTNG_UUID_FMT, LTTNG_UUID_SCAN_VALUES(uuid_scan)) != + LTTNG_UUID_LEN) { + ret = -1; + } + + memcpy(uuid_out, uuid_scan, LTTNG_UUID_LEN); +end: + return ret; +} -- 2.34.1