From 5fbda04def6274650f4872666dc9c5ffc603b035 Mon Sep 17 00:00:00 2001 From: Phillip Whelan Date: Wed, 13 Sep 2023 07:43:14 -0700 Subject: [PATCH 1/7] fluent-bit: use console ctrl handlers in win32 to simulate SIGHUP. Signed-off-by: Phillip Whelan --- src/fluent-bit.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/src/fluent-bit.c b/src/fluent-bit.c index 774001f985a..d74f4e37e74 100644 --- a/src/fluent-bit.c +++ b/src/fluent-bit.c @@ -616,6 +616,40 @@ static void flb_signal_handler(int signal) } } +#ifdef FLB_SYSTEM_WINDOWS +#include + +static flb_ctx_t *handler_ctx = NULL; +static struct flb_cf *handler_opts = NULL; +static int handler_signal = 0; + +void flb_console_handler_set_ctx(flb_ctx_t *ctx, struct flb_cf *cf_opts) +{ + handler_ctx = ctx; + handler_opts = cf_opts; +} + +static bool flb_console_handler(DWORD evType) +{ + switch(evType) { + case 1 /* CTRL_BREAK_EVENT_1 */: + if (flb_bin_restarting == FLB_RELOAD_IDLE) { + flb_bin_restarting = FLB_RELOAD_IN_PROGRESS; + /* signal the main loop to execute reload. this is necessary since + * all signal handlers in win32 are executed on their own thread. + */ + handler_signal = 1; + flb_bin_restarting = FLB_RELOAD_IDLE; + } + else { + flb_utils_error(FLB_ERR_RELOADING_IN_PROGRESS); + } + break; + } + return 1; +} +#endif + static void flb_signal_init() { signal(SIGINT, &flb_signal_handler_break_loop); @@ -623,6 +657,9 @@ static void flb_signal_init() signal(SIGQUIT, &flb_signal_handler_break_loop); signal(SIGHUP, &flb_signal_handler); signal(SIGCONT, &flb_signal_handler); +#else + /* Use SetConsoleCtrlHandler on windows to simulate SIGHUP */ + SetConsoleCtrlHandler(flb_console_handler, 1); #endif signal(SIGTERM, &flb_signal_handler_break_loop); signal(SIGSEGV, &flb_signal_handler); @@ -995,6 +1032,10 @@ int flb_main(int argc, char **argv) cf = config->cf_main; service = cf_opts->service; +#ifdef FLB_SYSTEM_WINDOWS + flb_console_handler_set_ctx(ctx, cf_opts); +#endif + /* Add reference for cf_opts */ config->cf_opts = cf_opts; @@ -1315,8 +1356,19 @@ int flb_main(int argc, char **argv) while (ctx->status == FLB_LIB_OK && exit_signal == 0) { sleep(1); +#ifdef FLB_SYSTEM_WINDOWS + if (handler_signal == 1) { + handler_signal = 0; + flb_reload(ctx, cf_opts); + } +#endif + /* set the context again before checking the status again */ ctx = flb_context_get(); + +#ifdef FLB_SYSTEM_WINDOWS + flb_console_handler_set_ctx(ctx, cf_opts); +#endif } if (exit_signal) { From b65f87c74775e9358b8b65f836d6106bf8f84dc9 Mon Sep 17 00:00:00 2001 From: Phillip Whelan Date: Wed, 13 Sep 2023 07:43:48 -0700 Subject: [PATCH 2/7] in_calyptia_fleet: reenable fleet on windows. Signed-off-by: Phillip Whelan --- cmake/windows-setup.cmake | 2 -- 1 file changed, 2 deletions(-) diff --git a/cmake/windows-setup.cmake b/cmake/windows-setup.cmake index dbd7f4a4a2d..fa67cb0d872 100644 --- a/cmake/windows-setup.cmake +++ b/cmake/windows-setup.cmake @@ -60,8 +60,6 @@ if(FLB_WINDOWS_DEFAULTS) set(FLB_IN_EMITTER Yes) set(FLB_IN_PODMAN_METRICS No) set(FLB_IN_ELASTICSEARCH Yes) - # disable calyptia fleet management for now - set(FLB_IN_CALYPTIA_FLEET No) set(FLB_IN_SPLUNK Yes) # OUTPUT plugins From 2e83883d48cb7411acb1074ea268048e47a97430 Mon Sep 17 00:00:00 2001 From: Phillip Whelan Date: Wed, 13 Sep 2023 07:44:45 -0700 Subject: [PATCH 3/7] in_calyptia_fleet: use GenerateConsoleCtrlEvent on win32 to execute reload. Signed-off-by: Phillip Whelan --- plugins/in_calyptia_fleet/in_calyptia_fleet.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/plugins/in_calyptia_fleet/in_calyptia_fleet.c b/plugins/in_calyptia_fleet/in_calyptia_fleet.c index bb8dfb99992..7e0a81e4c03 100644 --- a/plugins/in_calyptia_fleet/in_calyptia_fleet.c +++ b/plugins/in_calyptia_fleet/in_calyptia_fleet.c @@ -348,8 +348,11 @@ static void *do_reload(void *data) reload->flb->config->conf_path_file = reload->cfg_path; sleep(5); +#ifndef FLB_SYSTEM_WINDOWS kill(getpid(), SIGHUP); - +#else + GenerateConsoleCtrlEvent(1 /* CTRL_BREAK_EVENT_1 */, 0); +#endif return NULL; } From 1fccdfa747d276f4c2819f631fefb956638a238a Mon Sep 17 00:00:00 2001 From: Phillip Whelan Date: Wed, 13 Sep 2023 07:47:34 -0700 Subject: [PATCH 4/7] in_calyptia_fleet: add support for creating configuration files for win32. Signed-off-by: Phillip Whelan --- plugins/in_calyptia_fleet/in_calyptia_fleet.c | 35 +++++++++++++++---- 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/plugins/in_calyptia_fleet/in_calyptia_fleet.c b/plugins/in_calyptia_fleet/in_calyptia_fleet.c index 7e0a81e4c03..3e2df36fff0 100644 --- a/plugins/in_calyptia_fleet/in_calyptia_fleet.c +++ b/plugins/in_calyptia_fleet/in_calyptia_fleet.c @@ -693,6 +693,10 @@ static int get_calyptia_fleet_id_by_name(struct flb_in_calyptia_fleet_config *ct return 0; } +#ifdef FLB_SYSTEM_WINDOWS +#define link(a, b) CreateHardLinkA(b, a, 0) +#endif + /* cb_collect callback */ static int in_calyptia_fleet_collect(struct flb_input_instance *ins, struct flb_config *config, @@ -715,6 +719,10 @@ static int in_calyptia_fleet_collect(struct flb_input_instance *ins, char *data; size_t b_sent; int ret = -1; +#ifdef FLB_SYSTEM_WINDOWS + DWORD err; + LPSTR lpMsg; +#endif u_conn = flb_upstream_conn_get(ctx->u); @@ -868,7 +876,16 @@ static int in_calyptia_fleet_collect(struct flb_input_instance *ins, flb_sds_destroy(cfgoldname); } - symlink(cfgname, cfgnewname); + if (!link(cfgname, cfgnewname)) { +#ifdef FLB_SYSTEM_WINDOWS + err = GetLastError(); + FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER, + NULL, err, 0, &lpMsg, 0, NULL); + flb_plg_error(ctx->ins, "unable to create hard link: %s", lpMsg); +#else + flb_errno(); +#endif + } } if (ctx->config_timestamp < time_last_modified) { @@ -901,12 +918,18 @@ static int in_calyptia_fleet_collect(struct flb_input_instance *ins, FLB_INPUT_RETURN(ret); } +#ifdef FLB_SYSTEM_WINDOWS +#define _mkdir(a, b) mkdir(a) +#else +#define _mkdir(a, b) mkdir(a, b) +#endif + /* recursively create directories, based on: * https://stackoverflow.com/a/2336245 * who found it at: * http://nion.modprobe.de/blog/archives/357-Recursive-directory-creation.html */ -static int _mkdir(const char *dir, int perms) { +static int __mkdir(const char *dir, int perms) { char tmp[255]; char *ptr = NULL; size_t len; @@ -926,7 +949,7 @@ static int _mkdir(const char *dir, int perms) { if (*ptr == '/') { *ptr = 0; if (access(tmp, F_OK) != 0) { - ret = mkdir(tmp, perms); + ret = _mkdir(tmp, perms); if (ret != 0) { return ret; } @@ -934,7 +957,7 @@ static int _mkdir(const char *dir, int perms) { *ptr = '/'; } } - return mkdir(tmp, perms); + return _mkdir(tmp, perms); } static int create_fleet_directory(struct flb_in_calyptia_fleet_config *ctx) @@ -942,7 +965,7 @@ static int create_fleet_directory(struct flb_in_calyptia_fleet_config *ctx) flb_sds_t myfleetdir; if (access(ctx->config_dir, F_OK) != 0) { - if (_mkdir(ctx->config_dir, 0700) != 0) { + if (__mkdir(ctx->config_dir, 0700) != 0) { return -1; } } @@ -959,7 +982,7 @@ static int create_fleet_directory(struct flb_in_calyptia_fleet_config *ctx) } if (access(myfleetdir, F_OK) != 0) { - if (_mkdir(myfleetdir, 0700) !=0) { + if (__mkdir(myfleetdir, 0700) !=0) { return -1; } } From 47156976f4996f86723cd0b10ef0f321ad4e22ec Mon Sep 17 00:00:00 2001 From: Phillip Whelan Date: Fri, 15 Sep 2023 12:50:02 -0300 Subject: [PATCH 5/7] fluent-bit: fix indentation. Signed-off-by: Phillip Whelan --- src/fluent-bit.c | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/src/fluent-bit.c b/src/fluent-bit.c index d74f4e37e74..c2d0a2a560d 100644 --- a/src/fluent-bit.c +++ b/src/fluent-bit.c @@ -632,9 +632,9 @@ void flb_console_handler_set_ctx(flb_ctx_t *ctx, struct flb_cf *cf_opts) static bool flb_console_handler(DWORD evType) { switch(evType) { - case 1 /* CTRL_BREAK_EVENT_1 */: - if (flb_bin_restarting == FLB_RELOAD_IDLE) { - flb_bin_restarting = FLB_RELOAD_IN_PROGRESS; + case 1 /* CTRL_BREAK_EVENT_1 */: + if (flb_bin_restarting == FLB_RELOAD_IDLE) { + flb_bin_restarting = FLB_RELOAD_IN_PROGRESS; /* signal the main loop to execute reload. this is necessary since * all signal handlers in win32 are executed on their own thread. */ @@ -645,7 +645,7 @@ static bool flb_console_handler(DWORD evType) flb_utils_error(FLB_ERR_RELOADING_IN_PROGRESS); } break; - } + } return 1; } #endif @@ -658,8 +658,8 @@ static void flb_signal_init() signal(SIGHUP, &flb_signal_handler); signal(SIGCONT, &flb_signal_handler); #else - /* Use SetConsoleCtrlHandler on windows to simulate SIGHUP */ - SetConsoleCtrlHandler(flb_console_handler, 1); + /* Use SetConsoleCtrlHandler on windows to simulate SIGHUP */ + SetConsoleCtrlHandler(flb_console_handler, 1); #endif signal(SIGTERM, &flb_signal_handler_break_loop); signal(SIGSEGV, &flb_signal_handler); @@ -873,12 +873,15 @@ static int parse_trace_pipeline(flb_ctx_t *ctx, const char *pipeline, char **tra } mk_list_foreach(cur, parts) { - key = NULL; - value = NULL; + key = NULL; + value = NULL; + part = mk_list_entry(cur, struct flb_split_entry, _head); + if (parse_trace_pipeline_prop(ctx, part->value, &key, &value) == FLB_ERROR) { return FLB_ERROR; } + if (strcmp(key, "input") == 0) { if (*trace_input != NULL) { flb_free(*trace_input); @@ -911,12 +914,14 @@ static int parse_trace_pipeline(flb_ctx_t *ctx, const char *pipeline, char **tra (char *)propname, strlen(propname), (char *)propval, strlen(propval)); } - if (key != NULL) { - mk_mem_free(key); - } - if (value != NULL) { - flb_free(value); - } + + if (key != NULL) { + mk_mem_free(key); + } + + if (value != NULL) { + flb_free(value); + } } flb_utils_split_free(parts); From 51f6b9cc17e6a704e29bd030e866cdca4c9ebc79 Mon Sep 17 00:00:00 2001 From: Phillip Whelan Date: Fri, 15 Sep 2023 14:06:14 -0300 Subject: [PATCH 6/7] fluent-bit: follow PHANDLER_ROUTINE definition more closely. Signed-off-by: Phillip Whelan --- src/fluent-bit.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/fluent-bit.c b/src/fluent-bit.c index c2d0a2a560d..51b814cfd02 100644 --- a/src/fluent-bit.c +++ b/src/fluent-bit.c @@ -629,7 +629,7 @@ void flb_console_handler_set_ctx(flb_ctx_t *ctx, struct flb_cf *cf_opts) handler_opts = cf_opts; } -static bool flb_console_handler(DWORD evType) +static BOOL WINAPI flb_console_handler(DWORD evType) { switch(evType) { case 1 /* CTRL_BREAK_EVENT_1 */: From 141c3c2e5c0e1f08d1168c8a17eee549a8645a6b Mon Sep 17 00:00:00 2001 From: Phillip Whelan Date: Wed, 20 Sep 2023 07:48:13 -0700 Subject: [PATCH 7/7] in_calyptia_fleet: add readlink equivalent for windows. Signed-off-by: Phillip Whelan --- plugins/in_calyptia_fleet/in_calyptia_fleet.c | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/plugins/in_calyptia_fleet/in_calyptia_fleet.c b/plugins/in_calyptia_fleet/in_calyptia_fleet.c index 3e2df36fff0..3175b5645c1 100644 --- a/plugins/in_calyptia_fleet/in_calyptia_fleet.c +++ b/plugins/in_calyptia_fleet/in_calyptia_fleet.c @@ -695,6 +695,29 @@ static int get_calyptia_fleet_id_by_name(struct flb_in_calyptia_fleet_config *ct #ifdef FLB_SYSTEM_WINDOWS #define link(a, b) CreateHardLinkA(b, a, 0) + +ssize_t readlink(const char *path, char *realpath, size_t srealpath) { + HANDLE hFile; + DWORD ret; + + hFile = CreateFile(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, + FILE_ATTRIBUTE_NORMAL, NULL); + + if (hFile == INVALID_HANDLE_VALUE) { + return -1; + } + + ret = GetFinalPathNameByHandleA(hFile, realpath, srealpath, VOLUME_NAME_NT); + + if (ret < srealpath) { + CloseHandle(hFile); + return -1; + } + + CloseHandle(hFile); + return ret; +} + #endif /* cb_collect callback */