From 1011c09d5e59ff1957a0c7ce5ed3b6e5366aa453 Mon Sep 17 00:00:00 2001 From: Hiroshi Hatake Date: Wed, 4 Dec 2024 17:32:42 +0900 Subject: [PATCH] utils: Detect machine_id corruption and fill out a dummy value Signed-off-by: Hiroshi Hatake --- src/flb_utils.c | 16 ++++++++++++++++ tests/internal/utils.c | 20 ++++++++++++-------- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/src/flb_utils.c b/src/flb_utils.c index 3aa4412086b..33fc32c2a35 100644 --- a/src/flb_utils.c +++ b/src/flb_utils.c @@ -1481,6 +1481,7 @@ int flb_utils_get_machine_id(char **out_id, size_t *out_size) char *id; size_t bytes; char *uuid; + int fallback = FLB_FALSE; #ifdef __linux__ char *dbus_var = "/var/lib/dbus/machine-id"; @@ -1490,6 +1491,11 @@ int flb_utils_get_machine_id(char **out_id, size_t *out_size) if (access(dbus_var, F_OK) == 0) { /* check if the file exists first */ ret = machine_id_read_and_sanitize(dbus_var, &id, &bytes); if (ret == 0) { + if (bytes == 0) { + /* guid is somewhat corrupted */ + fallback = FLB_TRUE; + goto fallback; + } *out_id = id; *out_size = bytes; return 0; @@ -1500,6 +1506,11 @@ int flb_utils_get_machine_id(char **out_id, size_t *out_size) if (access(dbus_etc, F_OK) == 0) { /* check if the file exists first */ ret = machine_id_read_and_sanitize(dbus_etc, &id, &bytes); if (ret == 0) { + if (bytes == 0) { + /* guid is somewhat corrupted */ + fallback = FLB_TRUE; + goto fallback; + } *out_id = id; *out_size = bytes; return 0; @@ -1595,6 +1606,8 @@ int flb_utils_get_machine_id(char **out_id, size_t *out_size) } #endif +fallback: + flb_warn("falling back on random machine UUID"); /* generate a random uuid */ @@ -1607,6 +1620,9 @@ int flb_utils_get_machine_id(char **out_id, size_t *out_size) if (ret == 0) { *out_id = uuid; *out_size = strlen(uuid); + if (fallback == FLB_TRUE) { + return 2; + } return 0; } diff --git a/tests/internal/utils.c b/tests/internal/utils.c index f55a3672c37..68fdc6900d3 100644 --- a/tests/internal/utils.c +++ b/tests/internal/utils.c @@ -615,7 +615,7 @@ void test_flb_utils_get_machine_id() size_t size2; ret = flb_utils_get_machine_id(&id, &size); - TEST_CHECK(ret == 0); + TEST_CHECK(ret == 0 || ret == 2); TEST_CHECK(size != 0); TEST_CHECK(id != NULL); @@ -626,15 +626,19 @@ void test_flb_utils_get_machine_id() } ret = flb_utils_get_machine_id(&id2, &size2); - TEST_CHECK(ret == 0); + TEST_CHECK(ret == 0 || ret == 2); TEST_CHECK(size2 != 0); TEST_CHECK(id2 != NULL); - TEST_CHECK(size2 == size); - - for (idx = 0; idx < size; idx++) { - if (!TEST_CHECK(id[idx] == id2[idx])) { - fprintf(stderr, "bad byte in id v2 id2: id[%d] = 0x%02x, id2[%d] = 0x%02x\n", - idx, id[idx], idx, id2[idx]); + if (ret == 2) { + TEST_CHECK(size2 == size); + } + else { + TEST_CHECK(size2 == size); + for (idx = 0; idx < size; idx++) { + if (!TEST_CHECK(id[idx] == id2[idx])) { + fprintf(stderr, "bad byte in id v2 id2: id[%d] = 0x%02x, id2[%d] = 0x%02x\n", + idx, id[idx], idx, id2[idx]); + } } }