Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

in_calyptia_fleet: fix memory leaks during hot-reload of current and new configurations. #8133

Merged
merged 5 commits into from
Nov 4, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 34 additions & 25 deletions plugins/in_calyptia_fleet/in_calyptia_fleet.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ struct flb_in_calyptia_fleet_config {
long config_timestamp;

flb_sds_t api_key;

flb_sds_t fleet_id;
/* flag used to mark fleet_id for release when found automatically. */
int fleet_id_found;

flb_sds_t fleet_name;
flb_sds_t machine_id;
flb_sds_t config_dir;
Expand Down Expand Up @@ -375,6 +379,7 @@ static void *do_reload(void *data)
reload->flb->config->enable_hot_reload = FLB_TRUE;
reload->flb->config->conf_path_file = reload->cfg_path;

flb_free(reload);
sleep(5);
#ifndef FLB_SYSTEM_WINDOWS
kill(getpid(), SIGHUP);
Expand All @@ -386,21 +391,14 @@ static void *do_reload(void *data)

static int test_config_is_valid(flb_sds_t cfgpath)
{
struct flb_config *config;
struct flb_cf *conf;
int ret = FLB_FALSE;


config = flb_config_init();

if (config == NULL) {
goto config_init_error;
}

conf = flb_cf_create();

if (conf == NULL) {
goto cf_create_error;
goto config_init_error;
}

conf = flb_cf_create_from_file(conf, cfgpath);
Expand All @@ -409,22 +407,10 @@ static int test_config_is_valid(flb_sds_t cfgpath)
goto cf_create_from_file_error;
}

if (flb_config_load_config_format(config, conf)) {
goto cf_load_config_format_error;
}

if (flb_reload_property_check_all(config)) {
goto cf_property_check_error;
}

ret = FLB_TRUE;

cf_property_check_error:
cf_load_config_format_error:
cf_create_from_file_error:
flb_cf_destroy(conf);
cf_create_error:
flb_config_exit(config);
config_init_error:
return ret;
}
Expand All @@ -447,6 +433,7 @@ static int execute_reload(struct flb_in_calyptia_fleet_config *ctx, flb_sds_t cf
flb_input_collector_resume(ctx->collect_fd, ctx->ins);
}

flb_sds_destroy(cfgpath);
return FLB_FALSE;
}

Expand All @@ -463,6 +450,7 @@ static int execute_reload(struct flb_in_calyptia_fleet_config *ctx, flb_sds_t cf
flb_input_collector_resume(ctx->collect_fd, ctx->ins);
}

flb_sds_destroy(cfgpath);
return FLB_FALSE;
}

Expand Down Expand Up @@ -609,6 +597,7 @@ static ssize_t parse_fleet_search_json(struct flb_in_calyptia_fleet_config *ctx,
return -1;
}

ctx->fleet_id_found = 1;
ctx->fleet_id = flb_sds_create_len(cur->val.via.str.ptr,
cur->val.via.str.size);
break;
Expand Down Expand Up @@ -697,27 +686,34 @@ static int get_calyptia_fleet_id_by_name(struct flb_in_calyptia_fleet_config *ct

if (ret != 0) {
flb_plg_error(ctx->ins, "http do error");
flb_http_client_destroy(client);
return -1;
}

if (client->resp.status != 200) {
flb_plg_error(ctx->ins, "search http status code error: %d", client->resp.status);
flb_http_client_destroy(client);
return -1;
}

if (client->resp.payload_size <= 0) {
flb_plg_error(ctx->ins, "empty response");
flb_http_client_destroy(client);
return -1;
}

if (parse_fleet_search_json(ctx, client->resp.payload, client->resp.payload_size) == -1) {
flb_plg_error(ctx->ins, "unable to find fleet: %s", ctx->fleet_name);
flb_http_client_destroy(client);
return -1;
}

if (ctx->fleet_id == NULL) {
flb_http_client_destroy(client);
return -1;
}

flb_http_client_destroy(client);
return 0;
}

Expand Down Expand Up @@ -855,6 +851,7 @@ static int in_calyptia_fleet_collect(struct flb_input_instance *ins,

if (cfgfp == NULL) {
flb_plg_error(ctx->ins, "unable to open configuration file: %s", cfgname);
flb_sds_destroy(cfgname);
goto payload_error;
}

Expand Down Expand Up @@ -904,12 +901,14 @@ static int in_calyptia_fleet_collect(struct flb_input_instance *ins,
}
if (hdr == NULL) {
fclose(cfgfp);
flb_sds_destroy(cfgname);
goto header_error;
}
if (ctx->machine_id) {
hdr = flb_sds_printf(&header, " machine_id %s\n", ctx->machine_id);
if (hdr == NULL) {
fclose(cfgfp);
flb_sds_destroy(cfgname);
goto header_error;
}
}
Expand Down Expand Up @@ -938,25 +937,26 @@ static int in_calyptia_fleet_collect(struct flb_input_instance *ins,
flb_errno();
#endif
}

flb_sds_destroy(cfgnewname);
}

if (ctx->config_timestamp < time_last_modified) {
flb_plg_debug(ctx->ins, "new configuration is newer than current: %ld < %ld",
ctx->config_timestamp, time_last_modified);
flb_sds_destroy(data);
data = NULL;

if (execute_reload(ctx, cfgname) == FLB_FALSE) {
cfgoldname = old_fleet_config_filename(ctx);
cfgcurname = cur_fleet_config_filename(ctx);
rename(cfgoldname, cfgcurname);
flb_sds_destroy(cfgcurname);
flb_sds_destroy(cfgoldname);
flb_sds_destroy(cfgname);
goto reload_error;
}
else {
FLB_INPUT_RETURN(0);
}
}
else {
flb_sds_destroy(cfgname);
}

ret = 0;
Expand All @@ -972,6 +972,7 @@ static int in_calyptia_fleet_collect(struct flb_input_instance *ins,
}

http_error:
flb_plg_debug(ctx->ins, "freeing http client in fleet collect");
flb_http_client_destroy(client);
client_error:
flb_upstream_conn_release(u_conn);
Expand Down Expand Up @@ -1242,6 +1243,14 @@ static int in_calyptia_fleet_exit(void *data, struct flb_config *config)
(void) *config;
struct flb_in_calyptia_fleet_config *ctx = (struct flb_in_calyptia_fleet_config *)data;

if (ctx->fleet_url) {
flb_sds_destroy(ctx->fleet_url);
}

if (ctx->fleet_id && ctx->fleet_id_found) {
flb_sds_destroy(ctx->fleet_id);
}

flb_input_collector_delete(ctx->collect_fd, ctx->ins);
flb_upstream_destroy(ctx->u);
flb_free(ctx);
Expand Down
Loading