diff options
-rw-r--r-- | collectors/proc.plugin/proc_mdstat.c | 2 | ||||
-rw-r--r-- | collectors/proc.plugin/sys_class_power_supply.c | 98 | ||||
-rw-r--r-- | health/health.c | 38 | ||||
-rw-r--r-- | libnetdata/popen/popen.c | 10 | ||||
-rw-r--r-- | libnetdata/url/url.c | 17 |
5 files changed, 79 insertions, 86 deletions
diff --git a/collectors/proc.plugin/proc_mdstat.c b/collectors/proc.plugin/proc_mdstat.c index 5c29d31c7b..abfd2ff122 100644 --- a/collectors/proc.plugin/proc_mdstat.c +++ b/collectors/proc.plugin/proc_mdstat.c @@ -197,7 +197,7 @@ int do_proc_mdstat(int update_every, usec_t dt) { }
s++;
}
- if(unlikely(str_total[0] == '\0' || str_inuse[0] == '\0')) {
+ if(unlikely(str_total[0] == '\0' || !str_inuse || str_inuse[0] == '\0')) {
error("Cannot read /proc/mdstat raid health status. Unexpected format.");
continue;
}
diff --git a/collectors/proc.plugin/sys_class_power_supply.c b/collectors/proc.plugin/sys_class_power_supply.c index 5e37ad5aba..c558a384da 100644 --- a/collectors/proc.plugin/sys_class_power_supply.c +++ b/collectors/proc.plugin/sys_class_power_supply.c @@ -245,67 +245,75 @@ int do_sys_class_power_supply(int update_every, usec_t dt) { if(unlikely(ps->capacity->fd == -1)) { error("Cannot open file '%s'", ps->capacity->filename); power_supply_free(ps); + ps = NULL; } } - ssize_t r = read(ps->capacity->fd, buffer, 30); - if(unlikely(r < 1)) { - error("Cannot read file '%s'", ps->capacity->filename); - power_supply_free(ps); - } - else { - buffer[r] = '\0'; - ps->capacity->value = str2ull(buffer); - } + if (ps) + { + ssize_t r = read(ps->capacity->fd, buffer, 30); + if(unlikely(r < 1)) { + error("Cannot read file '%s'", ps->capacity->filename); + power_supply_free(ps); + ps = NULL; + } + else { + buffer[r] = '\0'; + ps->capacity->value = str2ull(buffer); - if(unlikely(!keep_fds_open)) { - close(ps->capacity->fd); - ps->capacity->fd = -1; - } - else if(unlikely(lseek(ps->capacity->fd, 0, SEEK_SET) == -1)) { - error("Cannot seek in file '%s'", ps->capacity->filename); - close(ps->capacity->fd); - ps->capacity->fd = -1; + if(unlikely(!keep_fds_open)) { + close(ps->capacity->fd); + ps->capacity->fd = -1; + } + else if(unlikely(lseek(ps->capacity->fd, 0, SEEK_SET) == -1)) { + error("Cannot seek in file '%s'", ps->capacity->filename); + close(ps->capacity->fd); + ps->capacity->fd = -1; + } + } } } // read property files int read_error = 0; struct ps_property *pr; - for(pr = ps->property_root; pr && !read_error; pr = pr->next) { - struct ps_property_dim *pd; - for(pd = pr->property_dim_root; pd; pd = pd->next) { - if(likely(!pd->always_zero)) { - char buffer[30 + 1]; - - if(unlikely(pd->fd == -1)) { - pd->fd = open(pd->filename, O_RDONLY, 0666); + if (ps) + { + for(pr = ps->property_root; pr && !read_error; pr = pr->next) { + struct ps_property_dim *pd; + for(pd = pr->property_dim_root; pd; pd = pd->next) { + if(likely(!pd->always_zero)) { + char buffer[30 + 1]; + if(unlikely(pd->fd == -1)) { - error("Cannot open file '%s'", pd->filename); + pd->fd = open(pd->filename, O_RDONLY, 0666); + if(unlikely(pd->fd == -1)) { + error("Cannot open file '%s'", pd->filename); + read_error = 1; + power_supply_free(ps); + break; + } + } + + ssize_t r = read(pd->fd, buffer, 30); + if(unlikely(r < 1)) { + error("Cannot read file '%s'", pd->filename); read_error = 1; power_supply_free(ps); break; } - } - - ssize_t r = read(pd->fd, buffer, 30); - if(unlikely(r < 1)) { - error("Cannot read file '%s'", pd->filename); - read_error = 1; - power_supply_free(ps); - break; - } - buffer[r] = '\0'; - pd->value = str2ull(buffer); + buffer[r] = '\0'; + pd->value = str2ull(buffer); - if(unlikely(!keep_fds_open)) { - close(pd->fd); - pd->fd = -1; - } - else if(unlikely(lseek(pd->fd, 0, SEEK_SET) == -1)) { - error("Cannot seek in file '%s'", pd->filename); - close(pd->fd); - pd->fd = -1; + if(unlikely(!keep_fds_open)) { + close(pd->fd); + pd->fd = -1; + } + else if(unlikely(lseek(pd->fd, 0, SEEK_SET) == -1)) { + error("Cannot seek in file '%s'", pd->filename); + close(pd->fd); + pd->fd = -1; + } } } } diff --git a/health/health.c b/health/health.c index 1460b5ba48..ed43b146c2 100644 --- a/health/health.c +++ b/health/health.c @@ -45,32 +45,30 @@ inline char *health_stock_config_dir(void) { * Function used to initialize the silencer structure. */ void health_silencers_init(void) { - struct stat statbuf; - if (!stat(silencers_filename,&statbuf)) { - off_t length = statbuf.st_size; + FILE *fd = fopen(silencers_filename, "r"); + if (fd) { + fseek(fd, 0 , SEEK_END); + off_t length = (off_t) ftell(fd); + fseek(fd, 0 , SEEK_SET); + if (length && length < HEALTH_SILENCERS_MAX_FILE_LEN) { - FILE *fd = fopen(silencers_filename, "r"); - if (fd) { - char *str = mallocz((length+1)* sizeof(char)); - if(str) { - size_t copied; - copied = fread(str, sizeof(char), length, fd); - if (copied == (length* sizeof(char))) { - str[length] = 0x00; - json_parse(str, NULL, health_silencers_json_read_callback); - info("Parsed health silencers file %s", silencers_filename); - } else { - error("Cannot read the data from health silencers file %s", silencers_filename); - } - freez(str); + char *str = mallocz((length+1)* sizeof(char)); + if(str) { + size_t copied; + copied = fread(str, sizeof(char), length, fd); + if (copied == (length* sizeof(char))) { + str[length] = 0x00; + json_parse(str, NULL, health_silencers_json_read_callback); + info("Parsed health silencers file %s", silencers_filename); + } else { + error("Cannot read the data from health silencers file %s", silencers_filename); } - fclose(fd); - } else { - error("Cannot open the file %s",silencers_filename); + freez(str); } } else { error("Health silencers file %s has the size %ld that is out of range[ 1 , %d ]. Aborting read.", silencers_filename, length, HEALTH_SILENCERS_MAX_FILE_LEN); } + fclose(fd); } else { error("Cannot open the file %s",silencers_filename); } diff --git a/libnetdata/popen/popen.c b/libnetdata/popen/popen.c index 177aebfc04..906b105357 100644 --- a/libnetdata/popen/popen.c +++ b/libnetdata/popen/popen.c @@ -68,7 +68,7 @@ static inline FILE *custom_popene(const char *command, volatile pid_t *pidptr, c int i; for(i = (int) (sysconf(_SC_OPEN_MAX) - 1); i >= 0; i--) if(i != STDIN_FILENO && i != STDERR_FILENO) - fcntl(i, F_SETFD, FD_CLOEXEC); + (void)fcntl(i, F_SETFD, FD_CLOEXEC); if (!posix_spawn_file_actions_init(&fa)) { // move the pipe to stdout in the child @@ -97,7 +97,7 @@ static inline FILE *custom_popene(const char *command, volatile pid_t *pidptr, c debug(D_CHILDS, "Spawned command: '%s' on pid %d from parent pid %d.", command, pid, getpid()); } else { error("Failed to spawn command: '%s' from parent pid %d.", command, getpid()); - close(pipefd[PIPE_READ]); + fclose(fp); fp = NULL; } close(pipefd[PIPE_WRITE]); @@ -116,7 +116,11 @@ error_after_posix_spawn_file_actions_init: if (posix_spawn_file_actions_destroy(&fa)) error("posix_spawn_file_actions_destroy"); error_after_pipe: - close(pipefd[PIPE_READ]); + if (fp) + fclose(fp); + else + close(pipefd[PIPE_READ]); + close(pipefd[PIPE_WRITE]); return NULL; } diff --git a/libnetdata/url/url.c b/libnetdata/url/url.c index 7df9faaf02..d1d5081397 100644 --- a/libnetdata/url/url.c +++ b/libnetdata/url/url.c @@ -44,23 +44,6 @@ char *url_encode(char *str) { } /** - * URL Decode - * - * Returns a url-decoded version of str - * IMPORTANT: be sure to free() the returned string after use - * - * @param str the string that will be decode - * - * @return a pointer for the url decoded. - */ -char *url_decode(char *str) { - size_t size = strlen(str) + 1; - - char *buf = mallocz(size); - return url_decode_r(buf, str, size); -} - -/** * Percentage escape decode * * Decode %XX character or return 0 if cannot |