Skip to content

Commit

Permalink
utils: Detect machine_id corruption and fill out a dummy value
Browse files Browse the repository at this point in the history
Signed-off-by: Hiroshi Hatake <[email protected]>
  • Loading branch information
cosmo0920 committed Dec 4, 2024
1 parent d573777 commit f7e4226
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
16 changes: 16 additions & 0 deletions src/flb_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -1479,6 +1479,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";
Expand All @@ -1488,6 +1489,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;
Expand All @@ -1498,6 +1504,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;
Expand Down Expand Up @@ -1593,6 +1604,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 */
Expand All @@ -1605,6 +1618,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;
}

Expand Down
11 changes: 8 additions & 3 deletions tests/internal/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -626,10 +626,15 @@ 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);
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])) {
Expand Down

0 comments on commit f7e4226

Please sign in to comment.