From e5d27a8415c07af73dacdf1d312f31bc0fa1179d Mon Sep 17 00:00:00 2001 From: Phillip Whelan Date: Thu, 18 Jan 2024 11:52:55 -0600 Subject: [PATCH 1/3] in_calyptia_fleet: remove double free of cfl_array_destroy() Signed-off-by: Eduardo Silva --- plugins/in_calyptia_fleet/in_calyptia_fleet.c | 1 - 1 file changed, 1 deletion(-) diff --git a/plugins/in_calyptia_fleet/in_calyptia_fleet.c b/plugins/in_calyptia_fleet/in_calyptia_fleet.c index f56f4d6b15c..0f729641f3c 100644 --- a/plugins/in_calyptia_fleet/in_calyptia_fleet.c +++ b/plugins/in_calyptia_fleet/in_calyptia_fleet.c @@ -1353,7 +1353,6 @@ static int calyptia_config_delete_old_dir(const char *cfgpath) for (idx = 0; idx < ((ssize_t)files->entry_count); idx++) { unlink(files->entries[idx]->data.as_string); } - cfl_array_destroy(files); } /* attempt to delete the main directory */ From 4a90177332d41b42c6078650f50c085bd59d2c2e Mon Sep 17 00:00:00 2001 From: Phillip Whelan Date: Thu, 18 Jan 2024 12:11:47 -0600 Subject: [PATCH 2/3] in_calyptia_fleet: improve connection context handling Signed-off-by: Eduardo Silva --- plugins/in_calyptia_fleet/in_calyptia_fleet.c | 72 ++++++++----------- 1 file changed, 29 insertions(+), 43 deletions(-) diff --git a/plugins/in_calyptia_fleet/in_calyptia_fleet.c b/plugins/in_calyptia_fleet/in_calyptia_fleet.c index 0f729641f3c..afc3914abdb 100644 --- a/plugins/in_calyptia_fleet/in_calyptia_fleet.c +++ b/plugins/in_calyptia_fleet/in_calyptia_fleet.c @@ -103,7 +103,6 @@ struct flb_in_calyptia_fleet_config { }; static int get_calyptia_files(struct flb_in_calyptia_fleet_config *ctx, - struct flb_connection *u_conn, const char *url, time_t timestamp); @@ -878,14 +877,20 @@ static flb_sds_t get_project_id_from_api_key(struct flb_in_calyptia_fleet_config } static struct flb_http_client *fleet_http_do(struct flb_in_calyptia_fleet_config *ctx, - struct flb_connection *u_conn, flb_sds_t url) { - struct flb_http_client *client; - size_t b_sent; int ret = -1; + size_t b_sent; + struct flb_connection *u_conn; + struct flb_http_client *client; + + if (ctx == NULL || url == NULL) { + return NULL; + } - if (ctx == NULL || u_conn == NULL || url == NULL) { + u_conn = flb_upstream_conn_get(ctx->u); + if (u_conn == NULL) { + flb_plg_error(ctx->ins, "unable to get upstream connection"); return NULL; } @@ -904,7 +909,6 @@ static struct flb_http_client *fleet_http_do(struct flb_in_calyptia_fleet_config ctx->api_key, flb_sds_len(ctx->api_key)); ret = flb_http_do(client, &b_sent); - if (ret != 0) { flb_plg_error(ctx->ins, "http do error"); goto http_do_error; @@ -920,28 +924,28 @@ static struct flb_http_client *fleet_http_do(struct flb_in_calyptia_fleet_config goto http_do_error; } + flb_upstream_conn_release(u_conn); return client; http_do_error: flb_http_client_destroy(client); http_client_error: + flb_upstream_conn_release(u_conn); return NULL; } static int get_calyptia_fleet_id_by_name(struct flb_in_calyptia_fleet_config *ctx, - struct flb_connection *u_conn, struct flb_config *config) { struct flb_http_client *client; flb_sds_t url; flb_sds_t project_id; - if (ctx == NULL || u_conn == NULL || config == NULL) { + if (ctx == NULL || config == NULL) { return -1; } project_id = get_project_id_from_api_key(ctx); - if (project_id == NULL) { return -1; } @@ -955,7 +959,7 @@ static int get_calyptia_fleet_id_by_name(struct flb_in_calyptia_fleet_config *ct flb_sds_printf(&url, "/v1/search?project_id=%s&resource=fleet&term=%s", project_id, ctx->fleet_name); - client = fleet_http_do(ctx, u_conn, url); + client = fleet_http_do(ctx, url); flb_sds_destroy(url); if (!client) { @@ -981,7 +985,6 @@ static int get_calyptia_fleet_id_by_name(struct flb_in_calyptia_fleet_config *ct } static int get_calyptia_file(struct flb_in_calyptia_fleet_config *ctx, - struct flb_connection *u_conn, flb_sds_t url, const char *hdr, const char *dst, @@ -997,18 +1000,17 @@ static int get_calyptia_file(struct flb_in_calyptia_fleet_config *ctx, time_t last_modified; flb_sds_t fname; - if (ctx == NULL || u_conn == NULL || url == NULL) { + if (ctx == NULL || url == NULL) { return -1; } - client = fleet_http_do(ctx, u_conn, url); - + client = fleet_http_do(ctx, url); if (client == NULL) { return -1; } ret = case_header_lookup(client, "Last-modified", strlen("Last-modified"), - &fbit_last_modified, &fbit_last_modified_len); + &fbit_last_modified, &fbit_last_modified_len); if (ret < 0) { goto client_error; @@ -1019,7 +1021,8 @@ static int get_calyptia_file(struct flb_in_calyptia_fleet_config *ctx, last_modified = mktime(&tm_last_modified.tm); fname = time_fleet_config_filename(ctx, last_modified); - } else { + } + else { fname = flb_sds_create_len(dst, strlen(dst)); } @@ -1739,8 +1742,7 @@ static int create_fleet_header(struct flb_in_calyptia_fleet_config *ctx) return rc; } -static int get_calyptia_fleet_config(struct flb_in_calyptia_fleet_config *ctx, - struct flb_connection *u_conn) +static int get_calyptia_fleet_config(struct flb_in_calyptia_fleet_config *ctx) { flb_sds_t cfgname; flb_sds_t cfgnewname; @@ -1777,14 +1779,12 @@ static int get_calyptia_fleet_config(struct flb_in_calyptia_fleet_config *ctx, flb_sds_destroy(hdrname); /* create the base file. */ - ret = get_calyptia_file(ctx, u_conn, ctx->fleet_url, header, - NULL, &time_last_modified); - + ret = get_calyptia_file(ctx, ctx->fleet_url, header, NULL, &time_last_modified); flb_sds_destroy(header); /* new file created! */ if (ret == 1) { - get_calyptia_files(ctx, u_conn, ctx->fleet_files_url, time_last_modified); + get_calyptia_files(ctx, ctx->fleet_files_url, time_last_modified); cfgname = time_fleet_config_filename(ctx, time_last_modified); @@ -1818,31 +1818,19 @@ static int in_calyptia_fleet_collect(struct flb_input_instance *ins, struct flb_config *config, void *in_context) { - struct flb_in_calyptia_fleet_config *ctx = in_context; - struct flb_connection *u_conn; int ret = -1; - - u_conn = flb_upstream_conn_get(ctx->u); - - if (!u_conn) { - flb_plg_error(ctx->ins, "could not get an upstream connection to %s:%u", - ctx->ins->host.name, ctx->ins->host.port); - goto conn_error; - } + struct flb_in_calyptia_fleet_config *ctx = in_context; if (ctx->fleet_id == NULL) { - - if (get_calyptia_fleet_id_by_name(ctx, u_conn, config) == -1) { + if (get_calyptia_fleet_id_by_name(ctx, config) == -1) { flb_plg_error(ctx->ins, "unable to find fleet: %s", ctx->fleet_name); - goto calyptia_error; + goto fleet_id_error; } } - ret = get_calyptia_fleet_config(ctx, u_conn); + ret = get_calyptia_fleet_config(ctx); -calyptia_error: - flb_upstream_conn_release(u_conn); -conn_error: +fleet_id_error: FLB_INPUT_RETURN(ret); } @@ -2103,19 +2091,17 @@ static int create_fleet_files(struct flb_in_calyptia_fleet_config *ctx, } static int get_calyptia_files(struct flb_in_calyptia_fleet_config *ctx, - struct flb_connection *u_conn, const char *url, time_t timestamp) { struct flb_http_client *client; int ret = -1; - if (ctx == NULL || u_conn == NULL || url == NULL) { + if (ctx == NULL || url == NULL) { return -1; } - client = fleet_http_do(ctx, u_conn, ctx->fleet_files_url); - + client = fleet_http_do(ctx, ctx->fleet_files_url); if (client == NULL) { return -1; } From d405a68b3278860fa3e2a1c394e1b0ca56cf5fdb Mon Sep 17 00:00:00 2001 From: Eduardo Silva Date: Thu, 18 Jan 2024 12:25:21 -0600 Subject: [PATCH 3/3] in_calyptia_fleet: code style cleanup Signed-off-by: Eduardo Silva --- plugins/in_calyptia_fleet/in_calyptia_fleet.c | 67 +++++++------------ 1 file changed, 24 insertions(+), 43 deletions(-) diff --git a/plugins/in_calyptia_fleet/in_calyptia_fleet.c b/plugins/in_calyptia_fleet/in_calyptia_fleet.c index afc3914abdb..89d396721f7 100644 --- a/plugins/in_calyptia_fleet/in_calyptia_fleet.c +++ b/plugins/in_calyptia_fleet/in_calyptia_fleet.c @@ -39,7 +39,7 @@ #include #include -// Glob support +/* Glob support */ #ifndef _MSC_VER #include #endif @@ -102,6 +102,17 @@ struct flb_in_calyptia_fleet_config { int collect_fd; }; +struct reload_ctx { + flb_ctx_t *flb; + flb_sds_t cfg_path; +}; + +static flb_sds_t fleet_config_filename(struct flb_in_calyptia_fleet_config *ctx, char *fname); + +#define new_fleet_config_filename(a) fleet_config_filename((a), "new") +#define cur_fleet_config_filename(a) fleet_config_filename((a), "cur") +#define old_fleet_config_filename(a) fleet_config_filename((a), "old") + static int get_calyptia_files(struct flb_in_calyptia_fleet_config *ctx, const char *url, time_t timestamp); @@ -138,18 +149,15 @@ static char *find_case_header(struct flb_http_client *cli, const char *header) headstart = strstr(cli->resp.data, "\r\n"); - if (headstart == NULL) { return NULL; } /* Lookup the beginning of the header */ for (ptr = headstart; ptr != NULL && ptr+2 < cli->resp.payload; ptr = strstr(ptr, "\r\n")) { - if (ptr + 4 < cli->resp.payload && strcmp(ptr, "\r\n\r\n") == 0) { return NULL; } - ptr+=2; /* no space left for header */ @@ -159,7 +167,6 @@ static char *find_case_header(struct flb_http_client *cli, const char *header) /* matched header and the delimiter */ if (strncasecmp(ptr, header, strlen(header)) == 0) { - if (ptr[strlen(header)] == ':' && ptr[strlen(header)+1] == ' ') { return ptr; } @@ -184,9 +191,7 @@ static int case_header_lookup(struct flb_http_client *cli, ptr = find_case_header(cli, header); end = strstr(cli->resp.data, "\r\n\r\n"); - if (!ptr) { - if (end) { /* The headers are complete but the header is not there */ return -1; @@ -203,7 +208,6 @@ static int case_header_lookup(struct flb_http_client *cli, /* Lookup CRLF (end of line \r\n) */ crlf = strstr(ptr, "\r\n"); - if (!crlf) { return -1; } @@ -221,10 +225,6 @@ static int case_header_lookup(struct flb_http_client *cli, return 0; } -struct reload_ctx { - flb_ctx_t *flb; - flb_sds_t cfg_path; -}; static flb_sds_t generate_base_fleet_directory(struct flb_in_calyptia_fleet_config *ctx, flb_sds_t *fleet_dir) { @@ -268,11 +268,6 @@ static flb_sds_t fleet_config_filename(struct flb_in_calyptia_fleet_config *ctx, return cfgname; } - -#define new_fleet_config_filename(a) fleet_config_filename((a), "new") -#define cur_fleet_config_filename(a) fleet_config_filename((a), "cur") -#define old_fleet_config_filename(a) fleet_config_filename((a), "old") - static flb_sds_t time_fleet_config_filename(struct flb_in_calyptia_fleet_config *ctx, time_t t) { char s_last_modified[32]; @@ -387,9 +382,7 @@ static int is_timestamped_fleet_config_path(struct flb_in_calyptia_fleet_config errno = 0; val = strtol(fname, &end, 10); - - if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN)) || - (errno != 0 && val == 0)) { + if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN)) || (errno != 0 && val == 0)) { flb_errno(); return FLB_FALSE; } @@ -432,9 +425,8 @@ static int is_fleet_config(struct flb_in_calyptia_fleet_config *ctx, struct flb_ static int exists_new_fleet_config(struct flb_in_calyptia_fleet_config *ctx) { - flb_sds_t cfgnewname; int ret = FLB_FALSE; - + flb_sds_t cfgnewname; cfgnewname = new_fleet_config_filename(ctx); if (cfgnewname == NULL) { @@ -443,8 +435,8 @@ static int exists_new_fleet_config(struct flb_in_calyptia_fleet_config *ctx) } ret = access(cfgnewname, F_OK) == 0 ? FLB_TRUE : FLB_FALSE; - flb_sds_destroy(cfgnewname); + return ret; } @@ -468,9 +460,8 @@ static int exists_cur_fleet_config(struct flb_in_calyptia_fleet_config *ctx) static int exists_old_fleet_config(struct flb_in_calyptia_fleet_config *ctx) { - flb_sds_t cfgoldname; int ret = FLB_FALSE; - + flb_sds_t cfgoldname; cfgoldname = old_fleet_config_filename(ctx); if (cfgoldname == NULL) { @@ -479,8 +470,8 @@ static int exists_old_fleet_config(struct flb_in_calyptia_fleet_config *ctx) } ret = access(cfgoldname, F_OK) == 0 ? FLB_TRUE : FLB_FALSE; - flb_sds_destroy(cfgoldname); + return ret; } @@ -518,7 +509,6 @@ static int test_config_is_valid(struct flb_in_calyptia_fleet_config *ctx, } conf = flb_cf_create(); - if (conf == NULL) { flb_plg_debug(ctx->ins, "unable to create conf during validation test: %s", cfgpath); @@ -526,7 +516,6 @@ static int test_config_is_valid(struct flb_in_calyptia_fleet_config *ctx, } conf = flb_cf_create_from_file(conf, cfgpath); - if (conf == NULL) { flb_plg_debug(ctx->ins, "unable to create conf from file during validation test: %s", @@ -573,7 +562,6 @@ static int parse_config_name_timestamp(struct flb_in_calyptia_fleet_config *ctx, } fname = basename(realname); - flb_plg_debug(ctx->ins, "parsing configuration timestamp from path: %s", fname); errno = 0; @@ -725,7 +713,7 @@ static flb_sds_t parse_api_key_json(struct flb_in_calyptia_fleet_config *ctx, struct flb_pack_state pack_state; size_t off = 0; msgpack_unpacked result; - msgpack_object *projectID; + msgpack_object *tmp; flb_sds_t project_id = NULL; if (ctx == NULL || payload == NULL) { @@ -748,22 +736,20 @@ static flb_sds_t parse_api_key_json(struct flb_in_calyptia_fleet_config *ctx, msgpack_unpacked_init(&result); while (msgpack_unpack_next(&result, pack, out_size, &off) == MSGPACK_UNPACK_SUCCESS) { - projectID = msgpack_lookup_map_key(&result.data, "ProjectID"); - - if (projectID == NULL) { + tmp = msgpack_lookup_map_key(&result.data, "ProjectID"); + if (tmp == NULL) { flb_plg_error(ctx->ins, "unable to find fleet by name"); msgpack_unpacked_destroy(&result); return NULL; } - if (projectID->type != MSGPACK_OBJECT_STR) { - flb_plg_error(ctx->ins, "invalid fleet ID"); + if (tmp->type != MSGPACK_OBJECT_STR) { + flb_plg_error(ctx->ins, "invalid fleet ID data type"); msgpack_unpacked_destroy(&result); return NULL; } - project_id = flb_sds_create_len(projectID->via.str.ptr, - projectID->via.str.size); + project_id = flb_sds_create_len(tmp->via.str.ptr, tmp->via.str.size); break; } @@ -850,7 +836,6 @@ static flb_sds_t get_project_id_from_api_key(struct flb_in_calyptia_fleet_config } api_token_sep = strchr(ctx->api_key, '.'); - if (api_token_sep == NULL) { return NULL; } @@ -1168,7 +1153,6 @@ static char *dirname(char *path) { char *ptr; - ptr = strrchr(path, '\\'); if (ptr == NULL) { @@ -1194,7 +1178,6 @@ static struct cfl_array *read_glob_win(const char *path, struct cfl_array *list) } star = strchr(path, '*'); - if (star == NULL) { flb_error("path has no wild card: %s", path); return NULL; @@ -2140,7 +2123,6 @@ static int in_calyptia_fleet_init(struct flb_input_instance *in, /* Allocate space for the configuration */ ctx = flb_calloc(1, sizeof(struct flb_in_calyptia_fleet_config)); - if (!ctx) { flb_errno(); return -1; @@ -2151,8 +2133,7 @@ static int in_calyptia_fleet_init(struct flb_input_instance *in, /* Load the config map */ - ret = flb_input_config_map_set(in, (void *)ctx); - + ret = flb_input_config_map_set(in, (void *) ctx); if (ret == -1) { flb_free(ctx); flb_plg_error(in, "unable to load configuration");