Skip to content

Commit aea5443

Browse files
committed
Introduce helper to detect kernel declarations.
This is to avoid playing roulette with KERNEL_VERSION vs RHEL versions.
1 parent 8a2966f commit aea5443

File tree

3 files changed

+70
-4
lines changed

3 files changed

+70
-4
lines changed

Makefile.in

+5-2
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,13 @@ ccflags-y = @KOPTS@
2020

2121
all: ipt_NETFLOW.ko libipt_NETFLOW.so libip6t_NETFLOW.so @SNMPTARGET@
2222

23-
ipt_NETFLOW.ko: version.h ipt_NETFLOW.c ipt_NETFLOW.h compat.h Makefile
23+
ipt_NETFLOW.ko: version.h ipt_NETFLOW.c ipt_NETFLOW.h compat_def.h compat.h Makefile
2424
@echo Compiling for kernel $(KVERSION)
25+
@test "$(TRAVIS)" && cat compat_def.h
2526
make -C $(KDIR) M=$(CURDIR) modules CONFIG_DEBUG_INFO=y
2627
@touch $@
28+
compat_def.h: gen_compat_def
29+
./gen_compat_def > $@
2730
sparse: | version.h ipt_NETFLOW.c ipt_NETFLOW.h compat.h Makefile
2831
@rm -f ipt_NETFLOW.ko ipt_NETFLOW.o
2932
@echo Compiling for kernel $(KVERSION)
@@ -41,7 +44,7 @@ mclean:
4144
lclean:
4245
-rm -f *.so *_sh.o
4346
clean: mclean lclean
44-
-rm -f *.so *.o modules.order version.h
47+
-rm -f *.so *.o modules.order version.h compat_def.h
4548

4649
snmp_NETFLOW.so: snmp_NETFLOW.c
4750
$(CC) -fPIC -shared -o $@ $< -lnetsnmp

compat.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
#ifndef COMPAT_NETFLOW_H
88
#define COMPAT_NETFLOW_H
9-
9+
#include "compat_def.h"
1010

1111
#ifndef NIPQUAD
1212
# define NIPQUAD(addr) \
@@ -612,7 +612,7 @@ int in6_pton(const char *src, int srclen,
612612
#endif
613613

614614
/* Offset changes made in 613dbd95723aee7abd16860745691b6c7bda20dc */
615-
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,28) && LINUX_VERSION_CODE < KERNEL_VERSION(4,10,0)
615+
#ifndef HAVE_XT_FAMILY
616616
# if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35)
617617
# define xt_action_param xt_target_param
618618
# endif

gen_compat_def

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#!/bin/bash -efu
2+
# SPDX-License-Identifier: GPL-2.0-only
3+
#
4+
# generate defines based on if kernel have
5+
# some symbols declared
6+
#
7+
# Copyright (C) 2019 <[email protected]>
8+
#
9+
10+
fatal() {
11+
echo "Error: $*" >&2
12+
exit 1
13+
}
14+
15+
eval $(grep ^KDIR Makefile | tr -d ' ')
16+
[ "$KDIR" ] || fatal "KDIR is not found"
17+
18+
WD=cc-test-build
19+
mkdir -p $WD
20+
cd $WD || fatal "cannot cd to $WD"
21+
22+
kbuild_test_compile() {
23+
local cmd
24+
25+
cat > test.c
26+
echo obj-m = test.o > Makefile
27+
cmd="make -s -C $KDIR M=$PWD modules"
28+
echo "$cmd" > log
29+
if $cmd >> log 2>&1; then
30+
[ "$2" ] && echo "// $2 is declared ${3:+with <$3>}"
31+
echo "#define HAVE_$1"
32+
echo
33+
else
34+
echo "// ${2:-symbol} is undeclared${3:+ with <$3>}. Compile:"
35+
sed "s/^/\/\/ /" test.c
36+
echo "// Output:"
37+
sed "s/^/\/\/ /" log
38+
echo
39+
if ! grep -q undeclared log; then
40+
echo "Error: unexpected error from compiler" >&2
41+
cat log >&2
42+
echo >&2
43+
exit 3
44+
fi
45+
fi
46+
}
47+
48+
kbuild_test_symbol() {
49+
kbuild_test_compile ${1^^} $1 ${2-} <<EOF
50+
#include <linux/module.h>
51+
${2:+#include <$2>}
52+
MODULE_LICENSE("GPL");
53+
void *test = $1;
54+
EOF
55+
}
56+
57+
echo "// Autogenerated for $KDIR"
58+
echo
59+
60+
# helpers introduced in 613dbd95723aee7abd16860745691b6c7bda20dc
61+
kbuild_test_symbol xt_family linux/netfilter_ipv4/ip_tables.h
62+
63+
echo "// End of compat_def.h"

0 commit comments

Comments
 (0)