use fs type to veryfiy procfs/sysfs (#16813)
Ilya Mashchenko committed
Jan 20, 2024 at 15:52 UTC
d7cebfab2b52dacb9aa5a81d5a86584d6f1f40d2
1 file changed
+36
-10
libnetdata/libnetdata.c
+36
-10
@@ -1303,27 +1303,53 @@ int snprintfz(char *dst, size_t n, const char *fmt, ...) {
1303
return ret;
1304
}
1305
1306
-static int is_virtual_filesystem(const char *path, char **reason) {
1306
+static int is_procfs(const char *path, char **reason) {
1307
+#if defined(__APPLE__) || defined(__FreeBSD__)
1308
+ (void)path;
1309
+ (void)reason;
1310
+#else
1311
+ struct statfs stat;
1312
1313
+ if (statfs(path, &stat) == -1) {
1314
+ if (reason)
1315
+ *reason = "failed to statfs()";
1316
+ return -1;
1317
+ }
1318
+
1319
+#if defined PROC_SUPER_MAGIC
1320
+ if (stat.f_type != PROC_SUPER_MAGIC) {
1321
+ if (reason)
1322
+ *reason = "type is not procfs";
1323
+ return -1;
1324
+ }
1325
+#endif
1326
+
1327
+#endif
1328
+
1329
+ return 0;
1330
+}
1331
+
1332
+static int is_sysfs(const char *path, char **reason) {
1333
#if defined(__APPLE__) || defined(__FreeBSD__)
1334
(void)path;
1335
(void)reason;
1336
#else
1337
struct statfs stat;
1313
- // stat.f_fsid.__val[0] is a file system id
1314
- // stat.f_fsid.__val[1] is the inode
1315
- // so their combination uniquely identifies the file/dir
1338
1339
if (statfs(path, &stat) == -1) {
1318
- if(reason) *reason = "failed to statfs()";
1340
+ if (reason)
1341
+ *reason = "failed to statfs()";
1342
return -1;
1343
}
1344
1322
- if(stat.f_fsid.__val[0] != 0 || stat.f_fsid.__val[1] != 0) {
1323
- errno = EINVAL;
1324
- if(reason) *reason = "is not a virtual file system";
1345
+#if defined SYSFS_MAGIC
1346
+ if (stat.f_type != SYSFS_MAGIC) {
1347
+ if (reason)
1348
+ *reason = "type is not sysfs";
1349
return -1;
1350
}
1351
+#endif
1352
+
1353
#endif
1354
1355
return 0;
@@ -1355,11 +1381,11 @@ int verify_netdata_host_prefix(bool log_msg) {
1381
1382
path = buffer;
1383
snprintfz(path, FILENAME_MAX, "%s/proc", netdata_configured_host_prefix);
1358
- if(is_virtual_filesystem(path, &reason) == -1)
1384
+ if(is_procfs(path, &reason) == -1)
1385
goto failed;
1386
1387
snprintfz(path, FILENAME_MAX, "%s/sys", netdata_configured_host_prefix);
1362
- if(is_virtual_filesystem(path, &reason) == -1)
1388
+ if(is_sysfs(path, &reason) == -1)
1389
goto failed;
1390
1391
if (netdata_configured_host_prefix && *netdata_configured_host_prefix) {