Skip to content

Commit bfa594c

Browse files
cosmo0920edsiper
authored andcommitted
utils: Detect machine_id corruption and fill out a dummy value
Signed-off-by: Hiroshi Hatake <[email protected]>
1 parent 412d3ea commit bfa594c

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

src/flb_utils.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1481,6 +1481,7 @@ int flb_utils_get_machine_id(char **out_id, size_t *out_size)
14811481
char *id;
14821482
size_t bytes;
14831483
char *uuid;
1484+
int fallback = FLB_FALSE;
14841485

14851486
#ifdef __linux__
14861487
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)
14901491
if (access(dbus_var, F_OK) == 0) { /* check if the file exists first */
14911492
ret = machine_id_read_and_sanitize(dbus_var, &id, &bytes);
14921493
if (ret == 0) {
1494+
if (bytes == 0) {
1495+
/* guid is somewhat corrupted */
1496+
fallback = FLB_TRUE;
1497+
goto fallback;
1498+
}
14931499
*out_id = id;
14941500
*out_size = bytes;
14951501
return 0;
@@ -1500,6 +1506,11 @@ int flb_utils_get_machine_id(char **out_id, size_t *out_size)
15001506
if (access(dbus_etc, F_OK) == 0) { /* check if the file exists first */
15011507
ret = machine_id_read_and_sanitize(dbus_etc, &id, &bytes);
15021508
if (ret == 0) {
1509+
if (bytes == 0) {
1510+
/* guid is somewhat corrupted */
1511+
fallback = FLB_TRUE;
1512+
goto fallback;
1513+
}
15031514
*out_id = id;
15041515
*out_size = bytes;
15051516
return 0;
@@ -1595,6 +1606,8 @@ int flb_utils_get_machine_id(char **out_id, size_t *out_size)
15951606
}
15961607
#endif
15971608

1609+
fallback:
1610+
15981611
flb_warn("falling back on random machine UUID");
15991612

16001613
/* generate a random uuid */
@@ -1607,6 +1620,9 @@ int flb_utils_get_machine_id(char **out_id, size_t *out_size)
16071620
if (ret == 0) {
16081621
*out_id = uuid;
16091622
*out_size = strlen(uuid);
1623+
if (fallback == FLB_TRUE) {
1624+
return 2;
1625+
}
16101626
return 0;
16111627
}
16121628

tests/internal/utils.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ void test_flb_utils_get_machine_id()
615615
size_t size2;
616616

617617
ret = flb_utils_get_machine_id(&id, &size);
618-
TEST_CHECK(ret == 0);
618+
TEST_CHECK(ret == 0 || ret == 2);
619619
TEST_CHECK(size != 0);
620620
TEST_CHECK(id != NULL);
621621

@@ -626,15 +626,19 @@ void test_flb_utils_get_machine_id()
626626
}
627627

628628
ret = flb_utils_get_machine_id(&id2, &size2);
629-
TEST_CHECK(ret == 0);
629+
TEST_CHECK(ret == 0 || ret == 2);
630630
TEST_CHECK(size2 != 0);
631631
TEST_CHECK(id2 != NULL);
632-
TEST_CHECK(size2 == size);
633-
634-
for (idx = 0; idx < size; idx++) {
635-
if (!TEST_CHECK(id[idx] == id2[idx])) {
636-
fprintf(stderr, "bad byte in id v2 id2: id[%d] = 0x%02x, id2[%d] = 0x%02x\n",
637-
idx, id[idx], idx, id2[idx]);
632+
if (ret == 2) {
633+
TEST_CHECK(size2 == size);
634+
}
635+
else {
636+
TEST_CHECK(size2 == size);
637+
for (idx = 0; idx < size; idx++) {
638+
if (!TEST_CHECK(id[idx] == id2[idx])) {
639+
fprintf(stderr, "bad byte in id v2 id2: id[%d] = 0x%02x, id2[%d] = 0x%02x\n",
640+
idx, id[idx], idx, id2[idx]);
641+
}
638642
}
639643
}
640644

0 commit comments

Comments
 (0)