@cryptotaxi247 / netdata-1 / commits / 203f6a44c

better daemon errors about files; (#4342)

* better daemon errors about files; fixes #4341 * allow max depth 10 while reading dirs; fixes #4312 * exclude . and .. even if they are not reported as directories; #4312 * lower recursive depth to 3 * strict control on directory recursion

Costa Tsaousis committed Oct 1, 2018 at 23:22 UTC 203f6a44cc01d7d315bfe04052aa05f37fdb2eb0
8 files changed +101 -65
src/appconfig.c
+1 -1
@@ -447,7 +447,7 @@ int appconfig_load(struct config *root, char *filename, int overwrite_used)
447
448 FILE *fp = fopen(filename, "r");
449 if(!fp) {
450 - error("CONFIG: cannot open file '%s'", filename);
450 + // info("CONFIG: cannot open file '%s'. Using internal defaults.", filename);
451 return 0;
452 }
453
src/apps_plugin.c
+1 -1
@@ -3571,7 +3571,7 @@ static void parse_args(int argc, char **argv)
3571 if(freq > 0) update_every = freq;
3572
3573 if(read_apps_groups_conf(user_config_dir, "groups")) {
3574 - error("Cannot read process groups configuration file '%s/apps_groups.conf'. Will try '%s/apps_groups.conf'", user_config_dir, stock_config_dir);
3574 + info("Cannot read process groups configuration file '%s/apps_groups.conf'. Will try '%s/apps_groups.conf'", user_config_dir, stock_config_dir);
3575
3576 if(read_apps_groups_conf(stock_config_dir, "groups")) {
3577 error("Cannot read process groups '%s/apps_groups.conf'. There are no internal defaults. Failing.", stock_config_dir);
src/common.c
+61 -50
@@ -1534,84 +1534,95 @@ int path_is_file(const char *path, const char *subpath) {
1534 return is_file;
1535 }
1536
1537 -void recursive_config_double_dir_load(const char *user_path, const char *stock_path, const char *subpath, int (*callback)(const char *filename, void *data), void *data) {
1537 +void recursive_config_double_dir_load(const char *user_path, const char *stock_path, const char *subpath, int (*callback)(const char *filename, void *data), void *data, size_t depth) {
1538 + if(depth > 3) {
1539 + error("CONFIG: Max directory depth reached while reading user path '%s', stock path '%s', subpath '%s'", user_path, stock_path, subpath);
1540 + return;
1541 + }
1542 +
1543 char *udir = strdupz_path_subpath(user_path, subpath);
1544 char *sdir = strdupz_path_subpath(stock_path, subpath);
1545
1541 - debug(D_HEALTH, "Configuration traversing user-config directory '%s', stock config directory '%s'", udir, sdir);
1546 + debug(D_HEALTH, "CONFIG traversing user-config directory '%s', stock config directory '%s'", udir, sdir);
1547
1548 DIR *dir = opendir(udir);
1549 if (!dir) {
1545 - error("Configuration cannot open user-config directory '%s'.", udir);
1550 + error("CONFIG cannot open user-config directory '%s'.", udir);
1551 }
1552 else {
1553 struct dirent *de = NULL;
1554 while((de = readdir(dir))) {
1550 - if(de->d_type == DT_DIR
1551 - && ( (de->d_name[0] == '.' && de->d_name[1] == '\0')
1552 - || (de->d_name[0] == '.' && de->d_name[1] == '.' && de->d_name[2] == '\0')
1553 - )) {
1554 - debug(D_HEALTH, "Configuration ignoring user-config directory '%s/%s'", udir, de->d_name);
1555 - continue;
1555 + if(de->d_type == DT_DIR || de->d_type == DT_LNK) {
1556 + if( !de->d_name[0] ||
1557 + (de->d_name[0] == '.' && de->d_name[1] == '\0') ||
1558 + (de->d_name[0] == '.' && de->d_name[1] == '.' && de->d_name[2] == '\0')
1559 + ) {
1560 + debug(D_HEALTH, "CONFIG ignoring user-config directory '%s/%s'", udir, de->d_name);
1561 + continue;
1562 + }
1563 +
1564 + if(path_is_dir(udir, de->d_name)) {
1565 + recursive_config_double_dir_load(udir, sdir, de->d_name, callback, data, depth + 1);
1566 + continue;
1567 + }
1568 }
1569
1558 - if(path_is_dir(udir, de->d_name)) {
1559 - recursive_config_double_dir_load(udir, sdir, de->d_name, callback, data);
1560 - continue;
1570 + if(de->d_type == DT_REG || de->d_type == DT_LNK) {
1571 + size_t len = strlen(de->d_name);
1572 + if(path_is_file(udir, de->d_name) &&
1573 + len > 5 && !strcmp(&de->d_name[len - 5], ".conf")) {
1574 + char *filename = strdupz_path_subpath(udir, de->d_name);
1575 + callback(filename, data);
1576 + freez(filename);
1577 + }
1578 + else
1579 + debug(D_HEALTH, "CONFIG ignoring user-config file '%s/%s'", udir, de->d_name);
1580 }
1562 -
1563 - size_t len = strlen(de->d_name);
1564 - if(path_is_file(udir, de->d_name) &&
1565 - len > 5 && !strcmp(&de->d_name[len - 5], ".conf")) {
1566 - char *filename = strdupz_path_subpath(udir, de->d_name);
1567 - callback(filename, data);
1568 - freez(filename);
1569 - }
1570 -
1571 - else
1572 - debug(D_HEALTH, "Health ignoring user config file '%s/%s'", udir, de->d_name);
1581 }
1582
1583 closedir(dir);
1584 }
1585
1578 - debug(D_HEALTH, "Health configuration traversing stock config directory '%s', user config directory '%s'", sdir, udir);
1586 + debug(D_HEALTH, "CONFIG traversing stock config directory '%s', user config directory '%s'", sdir, udir);
1587
1588 dir = opendir(sdir);
1589 if (!dir) {
1582 - error("Health configuration cannot open stock config directory '%s'.", sdir);
1590 + error("CONFIG cannot open stock config directory '%s'.", sdir);
1591 }
1592 else {
1593 struct dirent *de = NULL;
1594 while((de = readdir(dir))) {
1587 - if(de->d_type == DT_DIR
1588 - && ( (de->d_name[0] == '.' && de->d_name[1] == '\0')
1589 - || (de->d_name[0] == '.' && de->d_name[1] == '.' && de->d_name[2] == '\0')
1590 - )) {
1591 - debug(D_HEALTH, "Health ignoring stock config directory '%s/%s'", sdir, de->d_name);
1592 - continue;
1595 + if(de->d_type == DT_DIR || de->d_type == DT_LNK) {
1596 + if( !de->d_name[0] ||
1597 + (de->d_name[0] == '.' && de->d_name[1] == '\0') ||
1598 + (de->d_name[0] == '.' && de->d_name[1] == '.' && de->d_name[2] == '\0')
1599 + ) {
1600 + debug(D_HEALTH, "CONFIG ignoring stock config directory '%s/%s'", sdir, de->d_name);
1601 + continue;
1602 + }
1603 +
1604 + if(path_is_dir(sdir, de->d_name)) {
1605 + // we recurse in stock subdirectory, only when there is no corresponding
1606 + // user subdirectory - to avoid reading the files twice
1607 +
1608 + if(!path_is_dir(udir, de->d_name))
1609 + recursive_config_double_dir_load(udir, sdir, de->d_name, callback, data, depth + 1);
1610 +
1611 + continue;
1612 + }
1613 }
1614
1595 - if(path_is_dir(sdir, de->d_name)) {
1596 - // we recurse in stock subdirectory, only when there is no corresponding
1597 - // user subdirectory - to avoid reading the files twice
1598 -
1599 - if(!path_is_dir(udir, de->d_name))
1600 - recursive_config_double_dir_load(udir, sdir, de->d_name, callback, data);
1601 -
1602 - continue;
1615 + if(de->d_type == DT_REG || de->d_type == DT_LNK) {
1616 + size_t len = strlen(de->d_name);
1617 + if(path_is_file(sdir, de->d_name) && !path_is_file(udir, de->d_name) &&
1618 + len > 5 && !strcmp(&de->d_name[len - 5], ".conf")) {
1619 + char *filename = strdupz_path_subpath(sdir, de->d_name);
1620 + callback(filename, data);
1621 + freez(filename);
1622 + }
1623 + else
1624 + debug(D_HEALTH, "CONFIG ignoring stock config file '%s/%s'", sdir, de->d_name);
1625 }
1604 -
1605 - size_t len = strlen(de->d_name);
1606 - if(path_is_file(sdir, de->d_name) && !path_is_file(udir, de->d_name) &&
1607 - len > 5 && !strcmp(&de->d_name[len - 5], ".conf")) {
1608 - char *filename = strdupz_path_subpath(sdir, de->d_name);
1609 - callback(filename, data);
1610 - freez(filename);
1611 - }
1612 -
1613 - else
1614 - debug(D_HEALTH, "Health ignoring stock config file '%s/%s'", sdir, de->d_name);
1626 }
1627
1628 closedir(dir);
src/common.h
+8 -2
@@ -431,8 +431,14 @@ extern const char *program_version;
431 extern char *strdupz_path_subpath(const char *path, const char *subpath);
432 extern int path_is_dir(const char *path, const char *subpath);
433 extern int path_is_file(const char *path, const char *subpath);
434 -extern void recursive_config_double_dir_load(const char *user_path, const char *stock_path, const char *subpath
435 - , int (*callback)(const char *filename, void *data), void *data);
434 +extern void recursive_config_double_dir_load(
435 + const char *user_path
436 + , const char *stock_path
437 + , const char *subpath
438 + , int (*callback)(const char *filename, void *data)
439 + , void *data
440 + , size_t depth
441 + );
442
443 /* fix for alpine linux */
444 #ifndef RUSAGE_THREAD
src/health_config.c
+1 -1
@@ -855,5 +855,5 @@ static int health_readfile(const char *filename, void *data) {
855
856 void health_readdir(RRDHOST *host, const char *user_path, const char *stock_path, const char *subpath) {
857 if(unlikely(!host->health_enabled)) return;
858 - recursive_config_double_dir_load(user_path, stock_path, subpath, health_readfile, (void *) host);
858 + recursive_config_double_dir_load(user_path, stock_path, subpath, health_readfile, (void *) host, 0);
859 }
src/log.h
+2
@@ -1,5 +1,7 @@
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 +#include "common.h"
4 +
5 #ifndef NETDATA_LOG_H
6 #define NETDATA_LOG_H 1
7
src/main.c
+26 -9
@@ -630,28 +630,45 @@ void set_global_environment() {
630 }
631
632 static int load_netdata_conf(char *filename, char overwrite_used) {
633 - if(filename)
634 - return config_load(filename, overwrite_used);
633 + errno = 0;
634
636 - filename = strdupz_path_subpath(netdata_configured_user_config_dir, "netdata.conf");
635 + int ret = 0;
636
638 - int ret = config_load(filename, overwrite_used);
639 - if(!ret) {
640 - freez(filename);
641 - filename = strdupz_path_subpath(netdata_configured_stock_config_dir, "netdata.conf");
637 + if(filename && *filename) {
638 ret = config_load(filename, overwrite_used);
639 + if(!ret)
640 + error("CONFIG: cannot load config file '%s'.", filename);
641 + }
642 + else {
643 + filename = strdupz_path_subpath(netdata_configured_user_config_dir, "netdata.conf");
644 +
645 + ret = config_load(filename, overwrite_used);
646 + if(!ret) {
647 + info("CONFIG: cannot load user config '%s'. Will try the stock version.", filename);
648 + freez(filename);
649 +
650 + filename = strdupz_path_subpath(netdata_configured_stock_config_dir, "netdata.conf");
651 + ret = config_load(filename, overwrite_used);
652 + if(!ret)
653 + info("CONFIG: cannot load stock config '%s'. Running with internal defaults.", filename);
654 + }
655 +
656 + freez(filename);
657 }
644 - freez(filename);
658
659 return ret;
660 }
661
662 static void load_stream_conf() {
663 + errno = 0;
664 char *filename = strdupz_path_subpath(netdata_configured_user_config_dir, "stream.conf");
665 if(!appconfig_load(&stream_config, filename, 0)) {
666 + info("CONFIG: cannot load user config '%s'. Will try stock config.", filename);
667 freez(filename);
668 +
669 filename = strdupz_path_subpath(netdata_configured_stock_config_dir, "stream.conf");
654 - appconfig_load(&stream_config, filename, 0);
670 + if(!appconfig_load(&stream_config, filename, 0))
671 + info("CONFIG: cannot load stock config '%s'. Running with internal defaults.", filename);
672 }
673 freez(filename);
674 }
src/statsd.c
+1 -1
@@ -1392,7 +1392,7 @@ static int statsd_file_callback(const char *filename, void *data) {
1392 }
1393
1394 static inline void statsd_readdir(const char *user_path, const char *stock_path, const char *subpath) {
1395 - recursive_config_double_dir_load(user_path, stock_path, subpath, statsd_file_callback, NULL);
1395 + recursive_config_double_dir_load(user_path, stock_path, subpath, statsd_file_callback, NULL, 0);
1396 }
1397
1398 // --------------------------------------------------------------------------------------------------------------------