Code cleanup

This commit is contained in:
Pierre Pronchery 2022-05-22 04:31:15 +02:00
parent 7d3623fab3
commit 9d30692a08
3 changed files with 17 additions and 18 deletions

View File

@ -55,6 +55,7 @@ struct _DaMon
}; };
/* functions */
/* damon_refresh */ /* damon_refresh */
static AppClient * _refresh_connect(Host * host, Event * event); static AppClient * _refresh_connect(Host * host, Event * event);
static int _refresh_uptime(AppClient * ac, Host * host, char * rrd); static int _refresh_uptime(AppClient * ac, Host * host, char * rrd);

View File

@ -55,7 +55,7 @@ typedef struct _Host
struct _DaMon struct _DaMon
{ {
char * prefix; char * prefix;
char * rrdcached; String * rrdcached;
unsigned int refresh; unsigned int refresh;
Host * hosts; Host * hosts;
unsigned int hosts_cnt; unsigned int hosts_cnt;
@ -202,7 +202,7 @@ static int _damon_init(DaMon * damon, char const * config, Event * event)
static int _init_config(DaMon * damon, char const * filename) static int _init_config(DaMon * damon, char const * filename)
{ {
Config * config; Config * config;
char const * p; String const * p;
char * q; char * q;
int tmp; int tmp;
@ -221,21 +221,21 @@ static int _init_config(DaMon * damon, char const * filename)
config_delete(config); config_delete(config);
return -1; return -1;
} }
if((p = config_get(config, "", "prefix")) == NULL) if((p = config_get(config, NULL, "prefix")) == NULL)
p = "."; p = ".";
if((damon->prefix = string_new(p)) == NULL) if((damon->prefix = string_new(p)) == NULL)
{ {
config_delete(config); config_delete(config);
return -1; return -1;
} }
if((p = config_get(config, "", "rrdcached")) != NULL if((p = config_get(config, NULL, "rrdcached")) != NULL
&& (damon->rrdcached = strdup(p)) == NULL) && (damon->rrdcached = string_new(p)) == NULL)
{ {
string_delete(damon->prefix); string_delete(damon->prefix);
config_delete(config); config_delete(config);
return -1; return -1;
} }
if((p = config_get(config, "", "refresh")) != NULL) if((p = config_get(config, NULL, "refresh")) != NULL)
{ {
tmp = strtol(p, &q, 10); tmp = strtol(p, &q, 10);
damon->refresh = (*p == '\0' || *q != '\0' || tmp <= 0) damon->refresh = (*p == '\0' || *q != '\0' || tmp <= 0)
@ -245,16 +245,16 @@ static int _init_config(DaMon * damon, char const * filename)
damon->refresh); damon->refresh);
#endif #endif
} }
if((p = config_get(config, "", "hosts")) != NULL) if((p = config_get(config, NULL, "hosts")) != NULL)
_init_config_hosts(damon, config, p); _init_config_hosts(damon, config, p);
config_delete(config); config_delete(config);
return 0; return 0;
} }
static int _init_config_hosts(DaMon * damon, Config * config, static int _init_config_hosts(DaMon * damon, Config * config,
char const * hosts) String const * hosts)
{ {
char const * h = hosts; String const * h = hosts;
unsigned int pos = 0; unsigned int pos = 0;
Host * p; Host * p;
@ -287,16 +287,14 @@ static int _init_config_hosts(DaMon * damon, Config * config,
static int _init_config_hosts_host(DaMon * damon, Config * config, Host * host, static int _init_config_hosts_host(DaMon * damon, Config * config, Host * host,
char const * h, unsigned int pos) char const * h, unsigned int pos)
{ {
char const * p; String const * p;
host->damon = damon; host->damon = damon;
host->appclient = NULL; host->appclient = NULL;
host->ifaces = NULL; host->ifaces = NULL;
host->vols = NULL; host->vols = NULL;
if((host->hostname = malloc(pos + 1)) == NULL) if((host->hostname = string_new_length(h, pos)) == NULL)
return damon_perror(NULL, -errno); return damon_perror(NULL, -errno);
strncpy(host->hostname, h, pos);
host->hostname[pos] = '\0';
#ifdef DEBUG #ifdef DEBUG
fprintf(stderr, "config: Host %s\n", host->hostname); fprintf(stderr, "config: Host %s\n", host->hostname);
#endif #endif
@ -327,7 +325,7 @@ static char ** _init_config_hosts_host_comma(char const * line)
l++; l++;
continue; continue;
} }
if((p = realloc(values, sizeof(char*) * (cnt + 2))) == NULL) if((p = realloc(values, sizeof(char *) * (cnt + 2))) == NULL)
break; break;
values = p; values = p;
if((values[cnt] = malloc(pos + 1)) != NULL) if((values[cnt] = malloc(pos + 1)) != NULL)
@ -368,6 +366,6 @@ static void _damon_destroy(DaMon * damon)
} }
event_delete(damon->event); event_delete(damon->event);
free(damon->hosts); free(damon->hosts);
free(damon->rrdcached); string_delete(damon->rrdcached);
free(damon->prefix); free(damon->prefix);
} }

View File

@ -269,10 +269,10 @@ static int _sysinfo_procs_generic(struct sysinfo * info)
return _probe_perror("/proc", 0); return _probe_perror("/proc", 0);
while((de = readdir(dir)) != NULL) while((de = readdir(dir)) != NULL)
{ {
#ifdef DT_DIR # ifdef DT_DIR
if(de->d_type != DT_DIR) if(de->d_type != DT_DIR)
continue; continue;
#endif # endif
if((l = strtol(de->d_name, NULL, 10)) <= 0) if((l = strtol(de->d_name, NULL, 10)) <= 0)
continue; continue;
info->procs++; info->procs++;
@ -675,7 +675,7 @@ static int _probe(AppServerOptions options)
Event * event; Event * event;
struct timeval tv; struct timeval tv;
memset(&probe, 0, sizeof(Probe)); memset(&probe, 0, sizeof(probe));
if(_probe_timeout(&probe) != 0) if(_probe_timeout(&probe) != 0)
{ {
free(probe.ifinfo); free(probe.ifinfo);