From a686c429363a923bc65bc4e201d849cf0ec70dde Mon Sep 17 00:00:00 2001 From: Craig Ringer Date: Thu, 13 Apr 2023 16:24:06 +1200 Subject: [PATCH 1/2] plugins: exec: exit fluent-bit after oneshot Exit fluent-bit when the child process of the 'exec' plugin exits in one-shot mode if the new Exit_After_Oneshot plugin option is true. While exiting after the oneshot command is the logical behaviour, doing so by default would change existing behaviour that may affect existing uses, so it's not made the default. Optionally propagate the child process exit code to the exit code of fluent-bit itself if the new plugin option Propagate_Oneshot_Exit_Code is True. This allows fluent-bit to be invoked as a wrapper for a child command. Signed-off-by: Craig Ringer --- plugins/in_exec/in_exec.c | 98 ++++++++++++++++++++++++++++++++++++++- plugins/in_exec/in_exec.h | 2 + 2 files changed, 99 insertions(+), 1 deletion(-) diff --git a/plugins/in_exec/in_exec.c b/plugins/in_exec/in_exec.c index 875b6139037..81b5e5b21cb 100644 --- a/plugins/in_exec/in_exec.c +++ b/plugins/in_exec/in_exec.c @@ -29,6 +29,7 @@ #include #include #include +#include #include "in_exec.h" @@ -37,6 +38,8 @@ static int in_exec_collect(struct flb_input_instance *ins, struct flb_config *config, void *in_context) { int ret = -1; + int cmdret; + int flb_exit_code; uint64_t val; size_t str_len = 0; FILE *cmdp = NULL; @@ -165,7 +168,72 @@ static int in_exec_collect(struct flb_input_instance *ins, collect_end: if(cmdp != NULL){ - pclose(cmdp); + /* + * If we're propagating the child exit code to the fluent-bit exit code + * in one-shot mode, popen() will have invoked our child command via + * its own shell, so unless the shell itself exited on a signal the + * translation is already done for us. + * For references on exit code handling in wrappers see + * https://www.gnu.org/software/bash/manual/html_node/Exit-Status.html + * and + * https://skarnet.org/software/execline/exitcodes.html + * + */ + cmdret = pclose(cmdp); + if (cmdret == -1) { + flb_errno(); + flb_plg_debug(ctx->ins, + "unexpected error while waiting for exit of command %s ", + ctx->cmd); + /* + * The exit code of the shell run by popen() could not be + * determined; exit with 128, which is not a code that could be + * returned through a shell by a real child command. + */ + flb_exit_code = 128; + } else if (WIFEXITED(cmdret)) { + flb_plg_debug(ctx->ins, "command %s exited with code %d", ctx->cmd, WEXITSTATUS(cmdret)); + /* + * Propagate shell exit code, which may encode a normal or signal + * exit for the real child process, directly to the caller. This + * could be greater than 127 if the shell encoded a signal exit + * status from the child process into its own return code. + */ + flb_exit_code = WEXITSTATUS(cmdret); + } else if (WIFSIGNALED(cmdret)) { + flb_plg_debug(ctx->ins, "command %s exited with signal %d", ctx->cmd, WTERMSIG(cmdret)); + /* + * Follow the shell convention of returning 128+signo for signal + * exits. The consumer of fluent-bit's exit code will be unable to + * differentiate between the shell exiting on a signal and the + * process called by the shell exiting on a signal. + */ + flb_exit_code = 128 + WTERMSIG(cmdret); + } else { + flb_plg_debug(ctx->ins, "command %s exited with unknown status", + ctx->cmd); + flb_exit_code = 128; + } + + /* + * In one-shot mode, exit fluent-bit once the child process terminates. + */ + if (ctx->exit_after_oneshot == FLB_TRUE) { + /* + * propagate the child process exit code as the fluent-bit exit + * code so fluent-bit with the exec plugin can be used as a + * command wrapper. + */ + if (ctx->propagate_exit_code == FLB_TRUE) { + config->exit_status_code = flb_exit_code; + } + flb_plg_debug(ctx->ins, + "one-shot command exited, terminating fluent-bit"); + flb_engine_exit(config); + } else { + flb_plg_debug(ctx->ins, + "one-shot command exited but exit_after_oneshot not set"); + } } return ret; @@ -212,6 +280,23 @@ static int in_exec_config_read(struct flb_exec *ctx, ctx->interval_nsec = atoi(DEFAULT_INTERVAL_NSEC); } + /* + * propagate_exit_code is not being forced to imply exit_after_oneshot in + * case somebody in future wishes to make the exec plugin exit on nonzero + * exit codes for normal repeating commands. + */ + if (ctx->propagate_exit_code && !ctx->exit_after_oneshot) { + flb_plg_error(in, + "propagate_exit_code=True option makes no sense without " + "exit_after_oneshot=True"); + return -1; + } + + if (ctx->exit_after_oneshot && !ctx->oneshot) { + flb_plg_debug(in, "exit_after_oneshot implies oneshot mode, enabling"); + ctx->oneshot = FLB_TRUE; + } + if (ctx->oneshot) { ctx->interval_sec = -1; ctx->interval_nsec = -1; @@ -378,6 +463,17 @@ static struct flb_config_map config_map[] = { 0, FLB_TRUE, offsetof(struct flb_exec, oneshot), "execute the command only once" }, + { + FLB_CONFIG_MAP_BOOL, "exit_after_oneshot", "false", + 0, FLB_TRUE, offsetof(struct flb_exec, exit_after_oneshot), + "exit fluent-bit after the command terminates in one-shot mode" + }, + { + FLB_CONFIG_MAP_BOOL, "propagate_exit_code", "false", + 0, FLB_TRUE, offsetof(struct flb_exec, propagate_exit_code), + "propagate oneshot exit command fluent-bit exit code using " + "shell exit code translation conventions" + }, /* EOF */ {0} }; diff --git a/plugins/in_exec/in_exec.h b/plugins/in_exec/in_exec.h index 64430b89be7..efde8c8d9ce 100644 --- a/plugins/in_exec/in_exec.h +++ b/plugins/in_exec/in_exec.h @@ -45,6 +45,8 @@ struct flb_exec { int interval_sec; int interval_nsec; struct flb_log_event_encoder log_encoder; + int exit_after_oneshot; + int propagate_exit_code; }; #endif /* FLB_IN_EXEC_H */ From b4cdb8918a1a075011a035f775d65c24207917e2 Mon Sep 17 00:00:00 2001 From: Craig Ringer Date: Mon, 22 May 2023 14:05:37 +1200 Subject: [PATCH 2/2] plugins: exec: Windows compatibility for exit code checks Add a compatibility shim to handle win32's lack of POSIX sys/wait.h macros like WIFEXITED, so fluent-bit's in_exec plugin can use them on platforms where they're supported, but fall back to a simple exit code extraction on windows. Signed-off-by: Craig Ringer --- CMakeLists.txt | 1 + cmake/platform_feature_checks.cmake | 8 +++ plugins/in_exec/in_exec.c | 23 ++++--- plugins/in_exec/in_exec_win32_compat.h | 94 ++++++++++++++++++++++++++ 4 files changed, 115 insertions(+), 11 deletions(-) create mode 100644 cmake/platform_feature_checks.cmake create mode 100644 plugins/in_exec/in_exec_win32_compat.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 9af783c793e..10d988c808a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -89,6 +89,7 @@ include(ExternalProject) include(cmake/FindJournald.cmake) include(cmake/FindMonkey.cmake) include(cmake/macros.cmake) +include(cmake/platform_feature_checks.cmake) set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake/sanitizers-cmake/cmake" ${CMAKE_MODULE_PATH}) find_package(Sanitizers) diff --git a/cmake/platform_feature_checks.cmake b/cmake/platform_feature_checks.cmake new file mode 100644 index 00000000000..c209631cc40 --- /dev/null +++ b/cmake/platform_feature_checks.cmake @@ -0,0 +1,8 @@ +# Feature tests for various platform and compiler capabilities, +# system headers, etc. + +include(CheckIncludeFile) +CHECK_INCLUDE_FILE("sys/wait.h" FLB_HAVE_SYS_WAIT_H) +if (FLB_HAVE_SYS_WAIT_H) + FLB_DEFINITION(FLB_HAVE_SYS_WAIT_H) +endif() diff --git a/plugins/in_exec/in_exec.c b/plugins/in_exec/in_exec.c index 81b5e5b21cb..b5d66acdc78 100644 --- a/plugins/in_exec/in_exec.c +++ b/plugins/in_exec/in_exec.c @@ -29,7 +29,7 @@ #include #include #include -#include +#include "in_exec_win32_compat.h" #include "in_exec.h" @@ -59,7 +59,7 @@ static int in_exec_collect(struct flb_input_instance *ins, } } - cmdp = popen(ctx->cmd, "r"); + cmdp = flb_popen(ctx->cmd, "r"); if (cmdp == NULL) { flb_plg_debug(ctx->ins, "command %s failed", ctx->cmd); goto collect_end; @@ -177,9 +177,8 @@ static int in_exec_collect(struct flb_input_instance *ins, * https://www.gnu.org/software/bash/manual/html_node/Exit-Status.html * and * https://skarnet.org/software/execline/exitcodes.html - * */ - cmdret = pclose(cmdp); + cmdret = flb_pclose(cmdp); if (cmdret == -1) { flb_errno(); flb_plg_debug(ctx->ins, @@ -191,24 +190,26 @@ static int in_exec_collect(struct flb_input_instance *ins, * returned through a shell by a real child command. */ flb_exit_code = 128; - } else if (WIFEXITED(cmdret)) { - flb_plg_debug(ctx->ins, "command %s exited with code %d", ctx->cmd, WEXITSTATUS(cmdret)); + } else if (FLB_WIFEXITED(cmdret)) { + flb_plg_debug(ctx->ins, "command %s exited with code %d", + ctx->cmd, FLB_WEXITSTATUS(cmdret)); /* * Propagate shell exit code, which may encode a normal or signal * exit for the real child process, directly to the caller. This * could be greater than 127 if the shell encoded a signal exit * status from the child process into its own return code. */ - flb_exit_code = WEXITSTATUS(cmdret); - } else if (WIFSIGNALED(cmdret)) { - flb_plg_debug(ctx->ins, "command %s exited with signal %d", ctx->cmd, WTERMSIG(cmdret)); + flb_exit_code = FLB_WEXITSTATUS(cmdret); + } else if (FLB_WIFSIGNALED(cmdret)) { + flb_plg_debug(ctx->ins, "command %s exited with signal %d", + ctx->cmd, FLB_WTERMSIG(cmdret)); /* * Follow the shell convention of returning 128+signo for signal * exits. The consumer of fluent-bit's exit code will be unable to * differentiate between the shell exiting on a signal and the * process called by the shell exiting on a signal. */ - flb_exit_code = 128 + WTERMSIG(cmdret); + flb_exit_code = 128 + FLB_WTERMSIG(cmdret); } else { flb_plg_debug(ctx->ins, "command %s exited with unknown status", ctx->cmd); @@ -227,7 +228,7 @@ static int in_exec_collect(struct flb_input_instance *ins, if (ctx->propagate_exit_code == FLB_TRUE) { config->exit_status_code = flb_exit_code; } - flb_plg_debug(ctx->ins, + flb_plg_info(ctx->ins, "one-shot command exited, terminating fluent-bit"); flb_engine_exit(config); } else { diff --git a/plugins/in_exec/in_exec_win32_compat.h b/plugins/in_exec/in_exec_win32_compat.h new file mode 100644 index 00000000000..9f0dfe695cd --- /dev/null +++ b/plugins/in_exec/in_exec_win32_compat.h @@ -0,0 +1,94 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ + +/* Fluent Bit + * ========== + * Copyright (C) 2015-2022 The Fluent Bit Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef FLB_IN_EXEC_WIN32_COMPAT_H +#define FLB_IN_EXEC_WIN32_COMPAT_H + +#include +#include + +/* + * Work around lack of sys/wait.h and POSIX exit status macros from waitpid() + * in win32's _popen() and _pclose() implementation, since fluent-bit uses + * these in the in_exec plugin. + * + * On POSIX-like OSes this'll just use the standard macros with a name alias. + * + * On windows, where the concept of a signal exit does not exist, it defines + * dummy macros to indicate that the process exited normally and extract the + * exit code. + * + * These macros are for use with flb_pclose() only. Do not use them with + * other APIs that may differ in return value semantics. + */ +#ifdef FLB_HAVE_SYS_WAIT_H +#include +#define FLB_WIFEXITED(status) WIFEXITED((status)) +#define FLB_WEXITSTATUS(status) WEXITSTATUS((status)) +#define FLB_WIFSIGNALED(status) WIFSIGNALED((status)) +#define FLB_WTERMSIG(status) WTERMSIG((status)) +#else +#define FLB_WIFEXITED(status) (1) +#define FLB_WEXITSTATUS(status) ((status) & 0x00ff) +#define FLB_WIFSIGNALED(status) (0) +#define FLB_WTERMSIG(status) (-1) +#endif + +/* + * Because Windows has to do everything differently, call _popen() and + * _pclose() instead of the POSIX popen() and pclose() functions. + * + * flb_pclose() has different return value semantics on Windows vs non-windows + * targets because it propagates the pclose() or _pclose() return value + * directly. You MUST use the FLB_WIFEXITED(), FLB_WEXITSTATUS(), + * FLB_WIFSIGNALED() and FLB_WTERMSIG() macros to consume the return value, + * rather than the underlying POSIX macros or manual bit-shifts. + */ +#if !defined(FLB_SYSTEM_WINDOWS) +static inline FILE* flb_popen(const char *command, const char *type) { + return popen(command, type); +} +static inline int flb_pclose(FILE *stream) { + return pclose(stream); +} +#define FLB_PCLOSE pclose +#else +static inline FILE* flb_popen(const char *command, const char *type) { + return _popen(command, type); +} +/* + * flb_pclose() has the same return value on Windows as win32 _pclose(), rather + * than posix pclose(). The process exit code is not bit-shifted to the high + * byte. + * + * The MSVC docs for _pclose() at + * https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/pclose?view=msvc-170 + * are misleading; they say that "The format of the return value is the same as + * for _cwait, except the low-order and high-order bytes are swapped." But + * _cwait isn't documented as having any meaningful return on success, the + * process exit code is meant to be in its "termstat" out parameter per + * https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/cwait?view=msvc-170 + * The return code of _pclose() actually appears to be the process exit code + * without the bit-shift that waitpid() applies. + */ +static inline int flb_pclose(FILE *stream) { + return _pclose(stream); +} +#endif + +#endif /* FLB_IN_EXEC_WIN32_COMPAT_H */