Skip to content

Commit f7e4226

Browse files
committed
utils: Detect machine_id corruption and fill out a dummy value
Signed-off-by: Hiroshi Hatake <[email protected]>
1 parent d573777 commit f7e4226

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

src/flb_utils.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1479,6 +1479,7 @@ int flb_utils_get_machine_id(char **out_id, size_t *out_size)
14791479
char *id;
14801480
size_t bytes;
14811481
char *uuid;
1482+
int fallback = FLB_FALSE;
14821483

14831484
#ifdef __linux__
14841485
char *dbus_var = "/var/lib/dbus/machine-id";
@@ -1488,6 +1489,11 @@ int flb_utils_get_machine_id(char **out_id, size_t *out_size)
14881489
if (access(dbus_var, F_OK) == 0) { /* check if the file exists first */
14891490
ret = machine_id_read_and_sanitize(dbus_var, &id, &bytes);
14901491
if (ret == 0) {
1492+
if (bytes == 0) {
1493+
/* guid is somewhat corrupted */
1494+
fallback = FLB_TRUE;
1495+
goto fallback;
1496+
}
14911497
*out_id = id;
14921498
*out_size = bytes;
14931499
return 0;
@@ -1498,6 +1504,11 @@ int flb_utils_get_machine_id(char **out_id, size_t *out_size)
14981504
if (access(dbus_etc, F_OK) == 0) { /* check if the file exists first */
14991505
ret = machine_id_read_and_sanitize(dbus_etc, &id, &bytes);
15001506
if (ret == 0) {
1507+
if (bytes == 0) {
1508+
/* guid is somewhat corrupted */
1509+
fallback = FLB_TRUE;
1510+
goto fallback;
1511+
}
15011512
*out_id = id;
15021513
*out_size = bytes;
15031514
return 0;
@@ -1593,6 +1604,8 @@ int flb_utils_get_machine_id(char **out_id, size_t *out_size)
15931604
}
15941605
#endif
15951606

1607+
fallback:
1608+
15961609
flb_warn("falling back on random machine UUID");
15971610

15981611
/* generate a random uuid */
@@ -1605,6 +1618,9 @@ int flb_utils_get_machine_id(char **out_id, size_t *out_size)
16051618
if (ret == 0) {
16061619
*out_id = uuid;
16071620
*out_size = strlen(uuid);
1621+
if (fallback == FLB_TRUE) {
1622+
return 2;
1623+
}
16081624
return 0;
16091625
}
16101626

tests/internal/utils.c

Lines changed: 8 additions & 3 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,10 +626,15 @@ 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);
632+
if (ret == 2) {
633+
TEST_CHECK(size2 != size);
634+
}
635+
else {
636+
TEST_CHECK(size2 == size);
637+
}
633638

634639
for (idx = 0; idx < size; idx++) {
635640
if (!TEST_CHECK(id[idx] == id2[idx])) {

0 commit comments

Comments
 (0)