diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-11-09 08:36:07 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-11-25 11:21:20 +0000 |
commit | eae52fdaa9298e00f14b0b6256400d200db9c373 (patch) | |
tree | a3040a19bd024295ded05370853647bab9d7c225 /src/claim/claim_id.c | |
parent | Adding upstream version 1.47.5. (diff) | |
download | netdata-eae52fdaa9298e00f14b0b6256400d200db9c373.tar.xz netdata-eae52fdaa9298e00f14b0b6256400d200db9c373.zip |
Adding upstream version 2.0.3.upstream/2.0.3
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/claim/claim_id.c')
-rw-r--r-- | src/claim/claim_id.c | 123 |
1 files changed, 123 insertions, 0 deletions
diff --git a/src/claim/claim_id.c b/src/claim/claim_id.c new file mode 100644 index 000000000..dd79eb640 --- /dev/null +++ b/src/claim/claim_id.c @@ -0,0 +1,123 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +#include "claim_id.h" + +static struct { + SPINLOCK spinlock; + ND_UUID claim_uuid; + ND_UUID claim_uuid_saved; +} claim = { + .spinlock = NETDATA_SPINLOCK_INITIALIZER, +}; + +void claim_id_clear_previous_working(void) { + spinlock_lock(&claim.spinlock); + claim.claim_uuid_saved = UUID_ZERO; + spinlock_unlock(&claim.spinlock); +} + +void claim_id_set(ND_UUID new_claim_id) { + spinlock_lock(&claim.spinlock); + + if(!UUIDiszero(claim.claim_uuid)) { + if(aclk_online()) + claim.claim_uuid_saved = claim.claim_uuid; + claim.claim_uuid = UUID_ZERO; + } + + claim.claim_uuid = new_claim_id; + if(localhost) + localhost->aclk.claim_id_of_origin = claim.claim_uuid; + + spinlock_unlock(&claim.spinlock); +} + +// returns true when the supplied str is a valid UUID. +// giving NULL, an empty string, or "NULL" is valid. +bool claim_id_set_str(const char *claim_id_str) { + bool rc; + + ND_UUID uuid; + if(!claim_id_str || !*claim_id_str || strcmp(claim_id_str, "NULL") == 0) { + uuid = UUID_ZERO, + rc = true; + } + else + rc = uuid_parse(claim_id_str, uuid.uuid) == 0; + + claim_id_set(uuid); + + return rc; +} + +ND_UUID claim_id_get_uuid(void) { + static ND_UUID uuid; + spinlock_lock(&claim.spinlock); + uuid = claim.claim_uuid; + spinlock_unlock(&claim.spinlock); + return uuid; +} + +void claim_id_get_str(char str[UUID_STR_LEN]) { + ND_UUID uuid = claim_id_get_uuid(); + + if(UUIDiszero(uuid)) + memset(str, 0, UUID_STR_LEN); + else + uuid_unparse_lower(uuid.uuid, str); +} + +const char *claim_id_get_str_mallocz(void) { + char *str = mallocz(UUID_STR_LEN); + claim_id_get_str(str); + return str; +} + +CLAIM_ID claim_id_get(void) { + CLAIM_ID ret = { + .uuid = claim_id_get_uuid(), + }; + + if(claim_id_is_set(ret)) + uuid_unparse_lower(ret.uuid.uuid, ret.str); + else + ret.str[0] = '\0'; + + return ret; +} + +CLAIM_ID claim_id_get_last_working(void) { + CLAIM_ID ret = { 0 }; + + spinlock_lock(&claim.spinlock); + ret.uuid = claim.claim_uuid_saved; + spinlock_unlock(&claim.spinlock); + + if(claim_id_is_set(ret)) + uuid_unparse_lower(ret.uuid.uuid, ret.str); + else + ret.str[0] = '\0'; + + return ret; +} + +CLAIM_ID rrdhost_claim_id_get(RRDHOST *host) { + CLAIM_ID ret = { 0 }; + + if(host == localhost) { + ret.uuid = claim_id_get_uuid(); + if(UUIDiszero(ret.uuid)) + ret.uuid = host->aclk.claim_id_of_parent; + } + else { + if (!UUIDiszero(host->aclk.claim_id_of_origin)) + ret.uuid = host->aclk.claim_id_of_origin; + else + ret.uuid = host->aclk.claim_id_of_parent; + } + + if(claim_id_is_set(ret)) + uuid_unparse_lower(ret.uuid.uuid, ret.str); + + return ret; +} |