Skip to content

Commit 0c35c1c

Browse files
committed
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
1 parent 6494bc9 commit 0c35c1c

File tree

11 files changed

+55
-4
lines changed

11 files changed

+55
-4
lines changed

Diff for: src/bfcli/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ add_custom_target(bfcli_lexer
5050

5151
add_executable(bfcli
5252
${CMAKE_CURRENT_SOURCE_DIR}/main.c
53+
${CMAKE_BINARY_DIR}/include/version.h
5354
${CMAKE_CURRENT_BINARY_DIR}/generated/include/bfcli/parser.h
5455
${CMAKE_CURRENT_BINARY_DIR}/generated/bfcli/parser.c
5556
${CMAKE_CURRENT_BINARY_DIR}/generated/include/bfcli/lexer.h
@@ -59,6 +60,7 @@ add_executable(bfcli
5960
target_include_directories(bfcli
6061
PRIVATE
6162
${CMAKE_SOURCE_DIR}/src
63+
${CMAKE_BINARY_DIR}/include
6264
${CMAKE_CURRENT_BINARY_DIR}/generated/include
6365
${CMAKE_CURRENT_BINARY_DIR}/generated/include/bfcli
6466
)

Diff for: src/bfcli/main.c

+10
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "core/response.h"
2323
#include "core/set.h"
2424
#include "libbpfilter/bpfilter.h"
25+
#include "version.h"
2526

2627
int bf_send(const struct bf_request *request, struct bf_response **response);
2728

@@ -185,6 +186,15 @@ int main(int argc, char *argv[])
185186

186187
bf_logger_setup();
187188

189+
// If any of the arguments is --version, print the version and return.
190+
for (int i = 0; i < argc; ++i) {
191+
if (bf_streq("--version", argv[i])) {
192+
bf_info("bfcli version %s, libbpfilter version %s", BF_VERSION,
193+
bf_version());
194+
exit(0);
195+
}
196+
}
197+
188198
if (streq(obj_str, "ruleset") && streq(action_str, "set")) {
189199
r = _bf_do_ruleset_set(argc, argv);
190200
} else if (streq(obj_str, "ruleset") && streq(action_str, "flush")) {

Diff for: src/bpfilter/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ configure_file(
1212

1313
add_executable(bpfilter
1414
${CMAKE_CURRENT_SOURCE_DIR}/main.c
15+
${CMAKE_BINARY_DIR}/include/version.h
1516
${CMAKE_CURRENT_SOURCE_DIR}/cgen/cgen.h ${CMAKE_CURRENT_SOURCE_DIR}/cgen/cgen.c
1617
${CMAKE_CURRENT_SOURCE_DIR}/cgen/cgroup.h ${CMAKE_CURRENT_SOURCE_DIR}/cgen/cgroup.c
1718
${CMAKE_CURRENT_SOURCE_DIR}/cgen/dump.h ${CMAKE_CURRENT_SOURCE_DIR}/cgen/dump.c
@@ -47,6 +48,7 @@ add_executable(bpfilter
4748
target_include_directories(bpfilter
4849
PUBLIC
4950
${CMAKE_SOURCE_DIR}/src
51+
${CMAKE_BINARY_DIR}/include
5052
)
5153

5254
target_link_libraries(bpfilter

Diff for: src/bpfilter/main.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -273,14 +273,14 @@ static int _bf_init(int argc, char *argv[])
273273
if (sigaction(SIGTERM, &sighandler, NULL) < 0)
274274
return bf_err_r(errno, "can't override handler for SIGTERM");
275275

276-
r = _bf_ensure_runtime_dir();
277-
if (r < 0)
278-
return bf_err_r(r, "failed to ensure runtime directory exists");
279-
280276
r = bf_opts_init(argc, argv);
281277
if (r < 0)
282278
return bf_err_r(r, "failed to parse command line arguments");
283279

280+
r = _bf_ensure_runtime_dir();
281+
if (r < 0)
282+
return bf_err_r(r, "failed to ensure runtime directory exists");
283+
284284
// Either load context, or initialize it from scratch.
285285
if (!bf_opts_transient()) {
286286
r = _bf_load(ctx_path);

Diff for: src/core/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ find_package(PkgConfig REQUIRED)
55
pkg_check_modules(bpf REQUIRED IMPORTED_TARGET libbpf)
66

77
set(core_srcs
8+
${CMAKE_BINARY_DIR}/include/version.h
89
${CMAKE_CURRENT_SOURCE_DIR}/bpf.h ${CMAKE_CURRENT_SOURCE_DIR}/bpf.c
910
${CMAKE_CURRENT_SOURCE_DIR}/btf.h ${CMAKE_CURRENT_SOURCE_DIR}/btf.c
1011
${CMAKE_CURRENT_SOURCE_DIR}/chain.h ${CMAKE_CURRENT_SOURCE_DIR}/chain.c
@@ -37,6 +38,7 @@ add_library(core
3738
target_include_directories(core
3839
PUBLIC
3940
${CMAKE_SOURCE_DIR}/src
41+
${CMAKE_BINARY_DIR}/include
4042
)
4143

4244
target_link_libraries(core

Diff for: src/core/opts.c

+6
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@
1616
#include "core/front.h"
1717
#include "core/helper.h"
1818
#include "core/logger.h"
19+
#include "version.h"
1920

2021
enum
2122
{
2223
BF_OPT_NO_IPTABLES_KEY,
2324
BF_OPT_NO_NFTABLES_KEY,
2425
BF_OPT_NO_CLI_KEY,
26+
BF_OPT_VERSION,
2527
};
2628

2729
static const char *_bf_verbose_strs[] = {
@@ -89,6 +91,7 @@ static struct argp_option options[] = {
8991
{"no-cli", BF_OPT_NO_CLI_KEY, 0, 0, "Disable CLI support", 0},
9092
{"verbose", 'v', "VERBOSE_FLAG", 0,
9193
"Verbose flags to enable. Can be used more than once.", 0},
94+
{"version", BF_OPT_VERSION, 0, 0, "Print the version and return.", 0},
9295
{0},
9396
};
9497

@@ -148,6 +151,9 @@ static error_t _bf_opts_parser(int key, char *arg, struct argp_state *state)
148151
bf_info("enabling verbose for '%s'", arg);
149152
args->verbose |= (1 << opt);
150153
break;
154+
case BF_OPT_VERSION:
155+
bf_info("bpfilter version %s", BF_VERSION);
156+
exit(0);
151157
default:
152158
return ARGP_ERR_UNKNOWN;
153159
}

Diff for: src/libbpfilter/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ set(libbpfilter_srcs
77
${CMAKE_CURRENT_SOURCE_DIR}/generic.h ${CMAKE_CURRENT_SOURCE_DIR}/generic.c
88
${CMAKE_CURRENT_SOURCE_DIR}/ipt.c
99
${CMAKE_CURRENT_SOURCE_DIR}/nft.c
10+
${CMAKE_BINARY_DIR}/include/version.h ${CMAKE_CURRENT_SOURCE_DIR}/version.c
1011
)
1112

1213
configure_file(
@@ -29,6 +30,7 @@ set_target_properties(libbpfilter PROPERTIES OUTPUT_NAME bpfilter)
2930
target_include_directories(libbpfilter
3031
PRIVATE
3132
${CMAKE_SOURCE_DIR}/src
33+
${CMAKE_BINARY_DIR}/include
3234
)
3335

3436
target_link_libraries(libbpfilter

Diff for: src/libbpfilter/bpfilter.h

+7
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ struct ipt_replace;
1414
struct xt_counters_info;
1515
struct nlmsghdr;
1616

17+
/**
18+
* Return the version of the library.
19+
*
20+
* @return Version of the library, as a string.
21+
*/
22+
const char *bf_version(void);
23+
1724
/**
1825
* Request the daemon to remove all the chains and rules.
1926
*

Diff for: src/libbpfilter/version.c

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/* SPDX-License-Identifier: GPL-2.0-only */
2+
/*
3+
* Copyright (c) 2023 Meta Platforms, Inc. and affiliates.
4+
*/
5+
6+
#include "version.h"
7+
8+
const char *bf_version(void)
9+
{
10+
return BF_VERSION;
11+
}

Diff for: src/version.h.in

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/* SPDX-License-Identifier: GPL-2.0-only */
2+
/*
3+
* Copyright (c) 2023 Meta Platforms, Inc. and affiliates.
4+
*/
5+
6+
#pragma once
7+
8+
#define BF_VERSION "@PROJECT_VERSION@@PROJECT_VERSION_TWEAK@"

Diff for: tests/unit/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ target_include_directories(unit_bin
123123
PRIVATE
124124
${CMAKE_SOURCE_DIR}/src # First look for headers in src/
125125
${CMAKE_CURRENT_SOURCE_DIR} # Then use overrides in tests/units
126+
${CMAKE_BINARY_DIR}/include
126127
)
127128

128129
target_link_libraries(unit_bin

0 commit comments

Comments
 (0)