From 0c35c1c1b9a02ed10d76a831ff8e3ee4d56fa233 Mon Sep 17 00:00:00 2001 From: Quentin Deslandes Date: Wed, 5 Feb 2025 15:22:26 +0100 Subject: [PATCH] Add command line options (and function) to get the release Add the following function and command line options to get the current release of bpfilter: - bfcli --version - bpfilter --version - bf_version() in libbpfilter --- src/bfcli/CMakeLists.txt | 2 ++ src/bfcli/main.c | 10 ++++++++++ src/bpfilter/CMakeLists.txt | 2 ++ src/bpfilter/main.c | 8 ++++---- src/core/CMakeLists.txt | 2 ++ src/core/opts.c | 6 ++++++ src/libbpfilter/CMakeLists.txt | 2 ++ src/libbpfilter/bpfilter.h | 7 +++++++ src/libbpfilter/version.c | 11 +++++++++++ src/version.h.in | 8 ++++++++ tests/unit/CMakeLists.txt | 1 + 11 files changed, 55 insertions(+), 4 deletions(-) create mode 100644 src/libbpfilter/version.c create mode 100644 src/version.h.in diff --git a/src/bfcli/CMakeLists.txt b/src/bfcli/CMakeLists.txt index fc75626f..3281935a 100644 --- a/src/bfcli/CMakeLists.txt +++ b/src/bfcli/CMakeLists.txt @@ -50,6 +50,7 @@ add_custom_target(bfcli_lexer add_executable(bfcli ${CMAKE_CURRENT_SOURCE_DIR}/main.c + ${CMAKE_BINARY_DIR}/include/version.h ${CMAKE_CURRENT_BINARY_DIR}/generated/include/bfcli/parser.h ${CMAKE_CURRENT_BINARY_DIR}/generated/bfcli/parser.c ${CMAKE_CURRENT_BINARY_DIR}/generated/include/bfcli/lexer.h @@ -59,6 +60,7 @@ add_executable(bfcli target_include_directories(bfcli PRIVATE ${CMAKE_SOURCE_DIR}/src + ${CMAKE_BINARY_DIR}/include ${CMAKE_CURRENT_BINARY_DIR}/generated/include ${CMAKE_CURRENT_BINARY_DIR}/generated/include/bfcli ) diff --git a/src/bfcli/main.c b/src/bfcli/main.c index 8eab3f3c..6e04e628 100644 --- a/src/bfcli/main.c +++ b/src/bfcli/main.c @@ -22,6 +22,7 @@ #include "core/response.h" #include "core/set.h" #include "libbpfilter/bpfilter.h" +#include "version.h" int bf_send(const struct bf_request *request, struct bf_response **response); @@ -185,6 +186,15 @@ int main(int argc, char *argv[]) bf_logger_setup(); + // If any of the arguments is --version, print the version and return. + for (int i = 0; i < argc; ++i) { + if (bf_streq("--version", argv[i])) { + bf_info("bfcli version %s, libbpfilter version %s", BF_VERSION, + bf_version()); + exit(0); + } + } + if (streq(obj_str, "ruleset") && streq(action_str, "set")) { r = _bf_do_ruleset_set(argc, argv); } else if (streq(obj_str, "ruleset") && streq(action_str, "flush")) { diff --git a/src/bpfilter/CMakeLists.txt b/src/bpfilter/CMakeLists.txt index 1e242afc..95a0e9c7 100644 --- a/src/bpfilter/CMakeLists.txt +++ b/src/bpfilter/CMakeLists.txt @@ -12,6 +12,7 @@ configure_file( add_executable(bpfilter ${CMAKE_CURRENT_SOURCE_DIR}/main.c + ${CMAKE_BINARY_DIR}/include/version.h ${CMAKE_CURRENT_SOURCE_DIR}/cgen/cgen.h ${CMAKE_CURRENT_SOURCE_DIR}/cgen/cgen.c ${CMAKE_CURRENT_SOURCE_DIR}/cgen/cgroup.h ${CMAKE_CURRENT_SOURCE_DIR}/cgen/cgroup.c ${CMAKE_CURRENT_SOURCE_DIR}/cgen/dump.h ${CMAKE_CURRENT_SOURCE_DIR}/cgen/dump.c @@ -47,6 +48,7 @@ add_executable(bpfilter target_include_directories(bpfilter PUBLIC ${CMAKE_SOURCE_DIR}/src + ${CMAKE_BINARY_DIR}/include ) target_link_libraries(bpfilter diff --git a/src/bpfilter/main.c b/src/bpfilter/main.c index 8d2c13e0..880ca78b 100644 --- a/src/bpfilter/main.c +++ b/src/bpfilter/main.c @@ -273,14 +273,14 @@ static int _bf_init(int argc, char *argv[]) if (sigaction(SIGTERM, &sighandler, NULL) < 0) return bf_err_r(errno, "can't override handler for SIGTERM"); - r = _bf_ensure_runtime_dir(); - if (r < 0) - return bf_err_r(r, "failed to ensure runtime directory exists"); - r = bf_opts_init(argc, argv); if (r < 0) return bf_err_r(r, "failed to parse command line arguments"); + r = _bf_ensure_runtime_dir(); + if (r < 0) + return bf_err_r(r, "failed to ensure runtime directory exists"); + // Either load context, or initialize it from scratch. if (!bf_opts_transient()) { r = _bf_load(ctx_path); diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 5879a338..77d2013e 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -5,6 +5,7 @@ find_package(PkgConfig REQUIRED) pkg_check_modules(bpf REQUIRED IMPORTED_TARGET libbpf) set(core_srcs + ${CMAKE_BINARY_DIR}/include/version.h ${CMAKE_CURRENT_SOURCE_DIR}/bpf.h ${CMAKE_CURRENT_SOURCE_DIR}/bpf.c ${CMAKE_CURRENT_SOURCE_DIR}/btf.h ${CMAKE_CURRENT_SOURCE_DIR}/btf.c ${CMAKE_CURRENT_SOURCE_DIR}/chain.h ${CMAKE_CURRENT_SOURCE_DIR}/chain.c @@ -37,6 +38,7 @@ add_library(core target_include_directories(core PUBLIC ${CMAKE_SOURCE_DIR}/src + ${CMAKE_BINARY_DIR}/include ) target_link_libraries(core diff --git a/src/core/opts.c b/src/core/opts.c index 0bb617b8..078d6913 100644 --- a/src/core/opts.c +++ b/src/core/opts.c @@ -16,12 +16,14 @@ #include "core/front.h" #include "core/helper.h" #include "core/logger.h" +#include "version.h" enum { BF_OPT_NO_IPTABLES_KEY, BF_OPT_NO_NFTABLES_KEY, BF_OPT_NO_CLI_KEY, + BF_OPT_VERSION, }; static const char *_bf_verbose_strs[] = { @@ -89,6 +91,7 @@ static struct argp_option options[] = { {"no-cli", BF_OPT_NO_CLI_KEY, 0, 0, "Disable CLI support", 0}, {"verbose", 'v', "VERBOSE_FLAG", 0, "Verbose flags to enable. Can be used more than once.", 0}, + {"version", BF_OPT_VERSION, 0, 0, "Print the version and return.", 0}, {0}, }; @@ -148,6 +151,9 @@ static error_t _bf_opts_parser(int key, char *arg, struct argp_state *state) bf_info("enabling verbose for '%s'", arg); args->verbose |= (1 << opt); break; + case BF_OPT_VERSION: + bf_info("bpfilter version %s", BF_VERSION); + exit(0); default: return ARGP_ERR_UNKNOWN; } diff --git a/src/libbpfilter/CMakeLists.txt b/src/libbpfilter/CMakeLists.txt index 29eeb72c..66b7d991 100644 --- a/src/libbpfilter/CMakeLists.txt +++ b/src/libbpfilter/CMakeLists.txt @@ -7,6 +7,7 @@ set(libbpfilter_srcs ${CMAKE_CURRENT_SOURCE_DIR}/generic.h ${CMAKE_CURRENT_SOURCE_DIR}/generic.c ${CMAKE_CURRENT_SOURCE_DIR}/ipt.c ${CMAKE_CURRENT_SOURCE_DIR}/nft.c + ${CMAKE_BINARY_DIR}/include/version.h ${CMAKE_CURRENT_SOURCE_DIR}/version.c ) configure_file( @@ -29,6 +30,7 @@ set_target_properties(libbpfilter PROPERTIES OUTPUT_NAME bpfilter) target_include_directories(libbpfilter PRIVATE ${CMAKE_SOURCE_DIR}/src + ${CMAKE_BINARY_DIR}/include ) target_link_libraries(libbpfilter diff --git a/src/libbpfilter/bpfilter.h b/src/libbpfilter/bpfilter.h index 59357570..acf6a412 100644 --- a/src/libbpfilter/bpfilter.h +++ b/src/libbpfilter/bpfilter.h @@ -14,6 +14,13 @@ struct ipt_replace; struct xt_counters_info; struct nlmsghdr; +/** + * Return the version of the library. + * + * @return Version of the library, as a string. + */ +const char *bf_version(void); + /** * Request the daemon to remove all the chains and rules. * diff --git a/src/libbpfilter/version.c b/src/libbpfilter/version.c new file mode 100644 index 00000000..3160a41f --- /dev/null +++ b/src/libbpfilter/version.c @@ -0,0 +1,11 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Copyright (c) 2023 Meta Platforms, Inc. and affiliates. + */ + +#include "version.h" + +const char *bf_version(void) +{ + return BF_VERSION; +} diff --git a/src/version.h.in b/src/version.h.in new file mode 100644 index 00000000..97cd5f68 --- /dev/null +++ b/src/version.h.in @@ -0,0 +1,8 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Copyright (c) 2023 Meta Platforms, Inc. and affiliates. + */ + +#pragma once + +#define BF_VERSION "@PROJECT_VERSION@@PROJECT_VERSION_TWEAK@" diff --git a/tests/unit/CMakeLists.txt b/tests/unit/CMakeLists.txt index 30942e5c..71919094 100644 --- a/tests/unit/CMakeLists.txt +++ b/tests/unit/CMakeLists.txt @@ -123,6 +123,7 @@ target_include_directories(unit_bin PRIVATE ${CMAKE_SOURCE_DIR}/src # First look for headers in src/ ${CMAKE_CURRENT_SOURCE_DIR} # Then use overrides in tests/units + ${CMAKE_BINARY_DIR}/include ) target_link_libraries(unit_bin