Skip to content

Commit

Permalink
Add command line options (and function) to get the release
Browse files Browse the repository at this point in the history
Add the following function and command line options to get the current
release of bpfilter:
- bfcli --version
- bpfilter --version
- bf_version() in libbpfilter
  • Loading branch information
qdeslandes committed Feb 5, 2025
1 parent 6494bc9 commit 0c35c1c
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/bfcli/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
)
Expand Down
10 changes: 10 additions & 0 deletions src/bfcli/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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")) {
Expand Down
2 changes: 2 additions & 0 deletions src/bpfilter/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -47,6 +48,7 @@ add_executable(bpfilter
target_include_directories(bpfilter
PUBLIC
${CMAKE_SOURCE_DIR}/src
${CMAKE_BINARY_DIR}/include
)

target_link_libraries(bpfilter
Expand Down
8 changes: 4 additions & 4 deletions src/bpfilter/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -37,6 +38,7 @@ add_library(core
target_include_directories(core
PUBLIC
${CMAKE_SOURCE_DIR}/src
${CMAKE_BINARY_DIR}/include
)

target_link_libraries(core
Expand Down
6 changes: 6 additions & 0 deletions src/core/opts.c
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = {
Expand Down Expand Up @@ -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},
};

Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 2 additions & 0 deletions src/libbpfilter/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
Expand Down
7 changes: 7 additions & 0 deletions src/libbpfilter/bpfilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
11 changes: 11 additions & 0 deletions src/libbpfilter/version.c
Original file line number Diff line number Diff line change
@@ -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;
}
8 changes: 8 additions & 0 deletions src/version.h.in
Original file line number Diff line number Diff line change
@@ -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@"
1 change: 1 addition & 0 deletions tests/unit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0c35c1c

Please sign in to comment.