| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | #include "claim_id.h" |
| 4 | |
| 5 | static struct { |
| 6 | SPINLOCK spinlock; |
| 7 | ND_UUID claim_uuid; |
| 8 | ND_UUID claim_uuid_saved; |
| 9 | } claim = { |
| 10 | .spinlock = SPINLOCK_INITIALIZER, |
| 11 | }; |
| 12 | |
| 13 | void claim_id_clear_previous_working(void) { |
| 14 | spinlock_lock(&claim.spinlock); |
| 15 | claim.claim_uuid_saved = UUID_ZERO; |
| 16 | spinlock_unlock(&claim.spinlock); |
| 17 | } |
| 18 | |
| 19 | void claim_id_set(ND_UUID new_claim_id) { |
| 20 | spinlock_lock(&claim.spinlock); |
| 21 | |
| 22 | if(!UUIDiszero(claim.claim_uuid)) { |
| 23 | if(aclk_online()) |
| 24 | claim.claim_uuid_saved = claim.claim_uuid; |
| 25 | claim.claim_uuid = UUID_ZERO; |
| 26 | } |
| 27 | |
| 28 | claim.claim_uuid = new_claim_id; |
| 29 | if(localhost) |
| 30 | localhost->aclk.claim_id_of_origin = claim.claim_uuid; |
| 31 | |
| 32 | spinlock_unlock(&claim.spinlock); |
| 33 | } |
| 34 | |
| 35 | // returns true when the supplied str is a valid UUID. |
| 36 | // giving NULL, an empty string, or "NULL" is valid. |
| 37 | bool claim_id_set_str(const char *claim_id_str) { |
| 38 | bool rc; |
| 39 | |
| 40 | ND_UUID uuid; |
| 41 | if(!claim_id_str || !*claim_id_str || strcmp(claim_id_str, "NULL") == 0) { |
| 42 | uuid = UUID_ZERO, |
| 43 | rc = true; |
| 44 | } |
| 45 | else |
| 46 | rc = uuid_parse(claim_id_str, uuid.uuid) == 0; |
| 47 | |
| 48 | claim_id_set(uuid); |
| 49 | |
| 50 | return rc; |
| 51 | } |
| 52 | |
| 53 | ND_UUID claim_id_get_uuid(void) { |
| 54 | ND_UUID uuid; |
| 55 | spinlock_lock(&claim.spinlock); |
| 56 | uuid = claim.claim_uuid; |
| 57 | spinlock_unlock(&claim.spinlock); |
| 58 | return uuid; |
| 59 | } |
| 60 | |
| 61 | void claim_id_get_str(char str[UUID_STR_LEN]) { |
| 62 | ND_UUID uuid = claim_id_get_uuid(); |
| 63 | |
| 64 | if(UUIDiszero(uuid)) |
| 65 | memset(str, 0, UUID_STR_LEN); |
| 66 | else |
| 67 | uuid_unparse_lower(uuid.uuid, str); |
| 68 | } |
| 69 | |
| 70 | const char *claim_id_get_str_mallocz(void) { |
| 71 | char *str = mallocz(UUID_STR_LEN); |
| 72 | claim_id_get_str(str); |
| 73 | return str; |
| 74 | } |
| 75 | |
| 76 | CLAIM_ID claim_id_get(void) { |
| 77 | CLAIM_ID ret = { |
| 78 | .uuid = claim_id_get_uuid(), |
| 79 | }; |
| 80 | |
| 81 | if(claim_id_is_set(ret)) |
| 82 | uuid_unparse_lower(ret.uuid.uuid, ret.str); |
| 83 | else |
| 84 | ret.str[0] = '\0'; |
| 85 | |
| 86 | return ret; |
| 87 | } |
| 88 | |
| 89 | CLAIM_ID claim_id_get_last_working(void) { |
| 90 | CLAIM_ID ret = { 0 }; |
| 91 | |
| 92 | spinlock_lock(&claim.spinlock); |
| 93 | ret.uuid = claim.claim_uuid_saved; |
| 94 | spinlock_unlock(&claim.spinlock); |
| 95 | |
| 96 | if(claim_id_is_set(ret)) |
| 97 | uuid_unparse_lower(ret.uuid.uuid, ret.str); |
| 98 | else |
| 99 | ret.str[0] = '\0'; |
| 100 | |
| 101 | return ret; |
| 102 | } |
| 103 | |
| 104 | CLAIM_ID rrdhost_claim_id_get(RRDHOST *host) { |
| 105 | CLAIM_ID ret = { 0 }; |
| 106 | |
| 107 | if(host == localhost) { |
| 108 | ret.uuid = claim_id_get_uuid(); |
| 109 | if(UUIDiszero(ret.uuid) || (!aclk_online() && !UUIDiszero(host->aclk.claim_id_of_parent))) |
| 110 | ret.uuid = host->aclk.claim_id_of_parent; |
| 111 | } |
| 112 | else { |
| 113 | if (!UUIDiszero(host->aclk.claim_id_of_origin)) |
| 114 | ret.uuid = host->aclk.claim_id_of_origin; |
| 115 | else |
| 116 | ret.uuid = host->aclk.claim_id_of_parent; |
| 117 | } |
| 118 | |
| 119 | if(claim_id_is_set(ret)) |
| 120 | uuid_unparse_lower(ret.uuid.uuid, ret.str); |
| 121 | |
| 122 | return ret; |
| 123 | } |