a820a8a4191defbef75ae63c2085540f8f5dfcae
2 * Copyright (C) 2018 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 * Copyright (C) 2019 Michael Jeanson <mjeanson@efficios.com>
5 * SPDX-License-Identifier: LGPL-2.1-only
9 #include <common/compat/string.hpp>
20 const lttng_uuid nil_uuid
= {};
21 bool lttng_uuid_is_init
;
24 void lttng_uuid_to_str(const lttng_uuid
& uuid
, char *uuid_str
)
26 sprintf(uuid_str
, LTTNG_UUID_FMT
, LTTNG_UUID_FMT_VALUES(uuid
));
29 int lttng_uuid_from_str(const char *str_in
, lttng_uuid
& uuid_out
)
34 if (str_in
== nullptr) {
39 if (lttng_strnlen(str_in
, LTTNG_UUID_STR_LEN
) != LTTNG_UUID_STR_LEN
- 1) {
44 /* Scan to a temporary location in case of a partial match. */
45 if (sscanf(str_in
, LTTNG_UUID_FMT
, LTTNG_UUID_SCAN_VALUES(uuid_scan
)) !=
55 bool lttng_uuid_is_nil(const lttng_uuid
& uuid
)
57 return uuid
== nil_uuid
;
61 * Generate a random UUID according to RFC4122, section 4.4.
63 int lttng_uuid_generate(lttng_uuid
& uuid_out
)
67 if (!lttng_uuid_is_init
) {
69 * We don't need cryptographic quality randomness to
70 * generate UUIDs, seed rand with the epoch.
72 const time_t epoch
= time(NULL
);
74 if (epoch
== (time_t) -1) {
80 lttng_uuid_is_init
= true;
84 * Generate 16 bytes of random bits.
86 for (i
= 0; i
< LTTNG_UUID_LEN
; i
++) {
87 uuid_out
[i
] = (uint8_t) rand();
91 * Set the two most significant bits (bits 6 and 7) of the
92 * clock_seq_hi_and_reserved to zero and one, respectively.
94 uuid_out
[8] &= ~(1 << 6);
95 uuid_out
[8] |= (1 << 7);
98 * Set the four most significant bits (bits 12 through 15) of the
99 * time_hi_and_version field to the 4-bit version number from
103 uuid_out
[6] |= (LTTNG_UUID_VER
<< 4);
This page took 0.032501 seconds and 4 git commands to generate.