Skip to content

Commit ed13084

Browse files
authored
utils: check existence of machine-id files before reading them. (#7806)
Signed-off-by: Phillip Whelan <[email protected]>
1 parent 7c46a40 commit ed13084

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

src/flb_utils.c

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1356,19 +1356,23 @@ int flb_utils_get_machine_id(char **out_id, size_t *out_size)
13561356
char *dbus_etc = "/etc/machine-id";
13571357

13581358
/* dbus */
1359-
ret = machine_id_read_and_sanitize(dbus_var, &id, &bytes);
1360-
if (ret == 0) {
1361-
*out_id = id;
1362-
*out_size = bytes;
1363-
return 0;
1359+
if (access(dbus_var, F_OK) == 0) { /* check if the file exists first */
1360+
ret = machine_id_read_and_sanitize(dbus_var, &id, &bytes);
1361+
if (ret == 0) {
1362+
*out_id = id;
1363+
*out_size = bytes;
1364+
return 0;
1365+
}
13641366
}
13651367

13661368
/* etc */
1367-
ret = machine_id_read_and_sanitize(dbus_etc, &id, &bytes);
1368-
if (ret == 0) {
1369-
*out_id = id;
1370-
*out_size = bytes;
1371-
return 0;
1369+
if (access(dbus_etc, F_OK) == 0) { /* check if the file exists first */
1370+
ret = machine_id_read_and_sanitize(dbus_etc, &id, &bytes);
1371+
if (ret == 0) {
1372+
*out_id = id;
1373+
*out_size = bytes;
1374+
return 0;
1375+
}
13721376
}
13731377
#elif defined(__FreeBSD__) || defined(__NetBSD__) || \
13741378
defined(__OpenBSD__) || defined(__DragonFly__)

0 commit comments

Comments
 (0)