Skip to content
Open
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions include/fluent-bit/flb_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ int flb_utils_proxy_url_split(const char *in_url, char **out_protocol,
char **out_host, char **out_port);
int flb_utils_read_file(char *path, char **out_buf, size_t *out_size);
int flb_utils_read_file_offset(char *path, off_t offset_start, off_t offset_end, char **out_buf, size_t *out_size);
const char *flb_utils_resolve_config_path(const char *path, const char *conf_path,
char *buf, size_t size);
char *flb_utils_get_os_name();
int flb_utils_uuid_v4_gen(char *buf);
int flb_utils_get_machine_id(char **out_id, size_t *out_size);
Expand Down
21 changes: 6 additions & 15 deletions plugins/filter_lua/lua_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ struct lua_filter *lua_config_create(struct flb_filter_instance *ins,
const char *script = NULL;
const char *tmp = NULL;
(void) config;
struct stat st;
struct lua_filter *lf;
struct mk_list *split = NULL;
struct mk_list *head = NULL;
Expand Down Expand Up @@ -81,20 +80,12 @@ struct lua_filter *lua_config_create(struct flb_filter_instance *ins,
return NULL;
}

/* Compose path */
ret = stat(script, &st);
if (ret == -1 && errno == ENOENT) {
if (script[0] == '/') {
flb_plg_error(lf->ins, "cannot access script '%s'", script);
flb_free(lf);
return NULL;
}

if (config->conf_path) {
snprintf(buf, sizeof(buf) - 1, "%s%s",
config->conf_path, script);
script = buf;
}
script = flb_utils_resolve_config_path(script, config->conf_path,
buf, sizeof(buf));
if (!script) {
flb_plg_error(lf->ins, "script path is too long");
flb_free(lf);
return NULL;
}

/* Validate script path */
Expand Down
15 changes: 13 additions & 2 deletions plugins/filter_wasm/filter_wasm.c
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,8 @@ static int cb_wasm_pre_run(struct flb_filter_instance *f_ins,
{
struct flb_filter_wasm *ctx = NULL;
int ret = -1;
char path[PATH_MAX + 1];
const char *resolved;

/* Allocate space for the configuration */
ctx = flb_calloc(1, sizeof(struct flb_filter_wasm));
Expand All @@ -332,7 +334,9 @@ static int cb_wasm_pre_run(struct flb_filter_instance *f_ins,
}

/* Check accessibility for the wasm path */
ret = access(ctx->wasm_path, R_OK);
resolved = flb_utils_resolve_config_path(ctx->wasm_path, config->conf_path,
path, sizeof(path));
ret = resolved ? access(resolved, R_OK) : -1;
if (ret != 0) {
flb_plg_error(f_ins, "cannot access wasm program at %s", ctx->wasm_path);
goto pre_run_error;
Expand All @@ -356,6 +360,8 @@ static int cb_wasm_init(struct flb_filter_instance *f_ins,
struct flb_wasm_config *wasm_conf = NULL;
int ret = -1;
const char *tmp;
const char *resolved;
char path[PATH_MAX + 1];
int event_format;

/* Allocate space for the configuration */
Expand Down Expand Up @@ -407,7 +413,12 @@ static int cb_wasm_init(struct flb_filter_instance *f_ins,
flb_filter_set_context(f_ins, ctx);

/* Instantiate the WASM module once and store it in the context */
ctx->wasm = flb_wasm_instantiate(config, ctx->wasm_path,
resolved = flb_utils_resolve_config_path(ctx->wasm_path, config->conf_path,
path, sizeof(path));
if (!resolved) {
goto init_error;
}
ctx->wasm = flb_wasm_instantiate(config, resolved,
ctx->accessible_dir_list,
ctx->wasm_conf);

Expand Down
1 change: 1 addition & 0 deletions plugins/processor_fts/fts.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ struct fts_value {
struct fts_runtime {
struct flb_luajit *vm;
char *script;
char *resolved_script;
char *code;
char *call;
char *error_mode;
Expand Down
21 changes: 20 additions & 1 deletion plugins/processor_fts/fts_runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/

#include <fluent-bit/flb_info.h>
#include <fluent-bit/flb_utils.h>
#ifdef FLB_HAVE_LUAJIT
#include <luajit.h>
#endif
Expand Down Expand Up @@ -165,6 +166,8 @@ void fts_runtime_destroy(struct fts_runtime *ctx)
flb_free(ctx->undo);
ctx->undo = NULL;
ctx->undo_size = 0;
flb_free(ctx->resolved_script);
ctx->resolved_script = NULL;

if (ctx->vm) {
flb_luajit_destroy(ctx->vm);
Expand Down Expand Up @@ -311,7 +314,7 @@ static int initialize_policy(lua_State *l)
ret = flb_luajit_load_buffer(ctx->vm, ctx->code, strlen(ctx->code), "fts_policy");
}
else {
ret = flb_luajit_load_script(ctx->vm, ctx->script);
ret = flb_luajit_load_script(ctx->vm, ctx->resolved_script ? ctx->resolved_script : ctx->script);
}

if (ret != 0) {
Expand Down Expand Up @@ -344,6 +347,22 @@ int fts_runtime_initialize(struct fts_runtime *ctx, struct flb_config *config)
{
lua_State *l;
int ret;
char path[PATH_MAX + 1];
const char *resolved;

if (ctx->script) {
resolved = flb_utils_resolve_config_path(ctx->script, config->conf_path,
path, sizeof(path));
if (!resolved) {
return FLB_PROCESSOR_FAILURE;
}
if (resolved != ctx->script) {
ctx->resolved_script = flb_strdup(resolved);
if (!ctx->resolved_script) {
return FLB_PROCESSOR_FAILURE;
}
}
}

ctx->vm = flb_luajit_create(config);
if (!ctx->vm) {
Expand Down
27 changes: 11 additions & 16 deletions src/config_format/flb_cf_fluentbit.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <fluent-bit/flb_mem.h>
#include <fluent-bit/flb_kv.h>
#include <fluent-bit/flb_compat.h>
#include <fluent-bit/flb_utils.h>

#include <monkey/mk_core.h>

Expand Down Expand Up @@ -438,27 +439,21 @@ static int read_config(struct flb_cf *cf, struct local_ctx *ctx,
return -1;
}

/* Check if the path exists (relative cases for included files) */
#ifndef FLB_HAVE_STATIC_CONF
if (ctx->level >= 0) {
ret = stat(cfg_file, &st);
if (ret == -1 && errno == ENOENT) {
/* Try to resolve the real path (if exists) */
if (cfg_file[0] == '/') {
return -1;
}

if (ctx->root_path) {
snprintf(tmp, PATH_MAX, "%s/%s", ctx->root_path, cfg_file);
cfg_file = tmp;
}
/* stat again */
ret = stat(cfg_file, &st);
if (ret < 0) {
flb_errno();
/* Keep the main file as supplied; resolve only included files. */
if (ctx->level > 0) {
cfg_file = (char *) flb_utils_resolve_config_path(cfg_file, ctx->root_path,
tmp, sizeof(tmp));
if (!cfg_file) {
return -1;
}
}
ret = stat(cfg_file, &st);
if (ret < 0) {
flb_errno();
return -1;
}
#ifndef _WIN32
/* check if readed file */
for (i=0; i<*ino_num; i++) {
Expand Down
25 changes: 5 additions & 20 deletions src/flb_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -710,27 +710,12 @@ int flb_lib_config_file(struct flb_lib_ctx *ctx, const char *path)
char *cfg = NULL;
char *end;
char *real_path;
struct stat st;

/* Check if file exists and resolve path */
ret = stat(path, &st);
if (ret == -1 && errno == ENOENT) {
/* Try to resolve the real path (if exists) */
if (path[0] == '/') {
fprintf(stderr, "Error: configuration file not found: %s\n", path);
return -1;
}

if (ctx->config->conf_path) {
snprintf(tmp, PATH_MAX, "%s%s", ctx->config->conf_path, path);
cfg = tmp;
}
else {
cfg = (char *) path;
}
}
else {
cfg = (char *) path;
cfg = (char *) flb_utils_resolve_config_path(path, ctx->config->conf_path,
tmp, sizeof(tmp));
if (!cfg) {
fprintf(stderr, "Error: configuration file path is too long: %s\n", path);
return -1;
}

if (access(cfg, R_OK) != 0) {
Expand Down
25 changes: 11 additions & 14 deletions src/flb_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -1689,25 +1689,22 @@ int flb_parser_load_multiline_parser_definitions(const char *cfg, struct flb_cf
int flb_parser_conf_file_stat(const char *file, struct flb_config *config)
{
int ret;
char tmp[PATH_MAX + 1];
const char *cfg;
struct stat st;

ret = stat(file, &st);
if (ret == -1 && errno == ENOENT) {
/* Try to resolve the real path (if exists) */
if (file[0] == '/') {
flb_utils_error(FLB_ERR_CFG_PARSER_FILE);
return -1;
}

if (config->conf_path) {
/* Handle as special case here. */
return -2;
}
cfg = flb_utils_resolve_config_path(file, config->conf_path, tmp, sizeof(tmp));
if (!cfg) {
return -1;
}

ret = stat(cfg, &st);
if (ret == -1) {
flb_utils_error(FLB_ERR_CFG_PARSER_FILE);
return -1;
}

return 0;
return cfg == file ? 0 : -2;
}

/* Load parsers from a configuration file */
Expand All @@ -1724,7 +1721,7 @@ int flb_parser_conf_file(const char *file, struct flb_config *config)
return -1;
}
else if (ret == -2) {
snprintf(tmp, PATH_MAX, "%s%s", config->conf_path, file);
snprintf(tmp, sizeof(tmp), "%s%s", config->conf_path, file);
cfg = tmp;
}
else {
Expand Down
21 changes: 5 additions & 16 deletions src/flb_plugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -388,27 +388,16 @@ int flb_plugin_load_config_file(const char *file, struct flb_config *config)
char *cfg = NULL;
struct mk_list *head;
struct cfl_list *head_e;
struct stat st;
struct flb_cf *cf;
struct flb_cf_section *section;
struct cfl_kvpair *entry;

#ifndef FLB_HAVE_STATIC_CONF
ret = stat(file, &st);
if (ret == -1 && errno == ENOENT) {
/* Try to resolve the real path (if exists) */
if (file[0] == '/') {
flb_utils_error(FLB_ERR_CFG_PLUGIN_FILE);
return -1;
}

if (config->conf_path) {
snprintf(tmp, PATH_MAX, "%s%s", config->conf_path, file);
cfg = tmp;
}
}
else {
cfg = (char *) file;
cfg = (char *) flb_utils_resolve_config_path(file, config->conf_path,
tmp, sizeof(tmp));
if (!cfg) {
flb_utils_error(FLB_ERR_CFG_PLUGIN_FILE);
return -1;
}

flb_debug("[plugin] opening configuration file %s", cfg);
Expand Down
18 changes: 3 additions & 15 deletions src/flb_upstream_ha.c
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,6 @@ struct flb_upstream_ha *flb_upstream_ha_from_file(const char *file,
const char *cfg = NULL;
char *tmp;
char path[PATH_MAX + 1];
struct stat st;
struct mk_list *head;
struct mk_list *g_head;
struct flb_upstream_ha *ups;
Expand All @@ -372,20 +371,9 @@ struct flb_upstream_ha *flb_upstream_ha_from_file(const char *file,
struct flb_cf_section *node_section;

#ifndef FLB_HAVE_STATIC_CONF
ret = stat(file, &st);
if (ret == -1 && errno == ENOENT) {
/* Try to resolve the real path (if exists) */
if (file[0] == '/') {
return NULL;
}

if (config->conf_path) {
snprintf(path, PATH_MAX, "%s%s", config->conf_path, file);
cfg = path;
}
}
else {
cfg = file;
cfg = flb_utils_resolve_config_path(file, config->conf_path, path, sizeof(path));
if (!cfg) {
return NULL;
}
flb_debug("[upstream_ha] opening file %s", cfg);
cf = flb_cf_create_from_file(NULL, (char *) cfg);
Expand Down
32 changes: 32 additions & 0 deletions src/flb_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <msgpack.h>

#include <monkey/mk_core.h>
Expand Down Expand Up @@ -66,6 +67,37 @@ extern struct flb_aws_error_reporter *error_reporter;
*/
#define FLB_UTILS_FRAGMENT_PRIVATE_BLOCK_DESCRIPTOR 0xE0

/* Prefer a file beside the main configuration over a same-named file in CWD. */
const char *flb_utils_resolve_config_path(const char *path, const char *conf_path,
char *buf, size_t size)
Comment thread
cosmo0920 marked this conversation as resolved.
{
int ret;
size_t len;
struct stat st;

if (!path || !conf_path || path[0] == '/') {
return path;
}
#ifdef _WIN32
if (path[0] == '\\' || (path[0] && path[1] == ':')) {
return path;
}
#endif

len = strlen(conf_path);
ret = snprintf(buf, size, "%s%s%s", conf_path,
len > 0 && conf_path[len - 1] == '/' ? "" : "/", path);
if (ret < 0 || (size_t) ret >= size) {
return NULL;
}

if (stat(buf, &st) == 0 || errno != ENOENT) {
return buf;
}

return path;
}

void flb_utils_error(int err)
{
char *msg = NULL;
Expand Down
Loading
Loading