From daeca114eef3086377b682b36fdc0eeb74cccad8 Mon Sep 17 00:00:00 2001 From: ABC Date: Sat, 28 Sep 2019 09:48:17 +0300 Subject: [PATCH] Introduce helper to detect kernel declarations. This is to avoid playing roulette with mainline kernel versions vs stable versions vs RHEL versions. --- Makefile.in | 6 +++-- compat.h | 4 +-- gen_compat_def | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 4 deletions(-) create mode 100755 gen_compat_def diff --git a/Makefile.in b/Makefile.in index 2c4d0a1..9aaca52 100644 --- a/Makefile.in +++ b/Makefile.in @@ -20,10 +20,12 @@ ccflags-y = @KOPTS@ all: ipt_NETFLOW.ko libipt_NETFLOW.so libip6t_NETFLOW.so @SNMPTARGET@ -ipt_NETFLOW.ko: version.h ipt_NETFLOW.c ipt_NETFLOW.h compat.h Makefile +ipt_NETFLOW.ko: version.h ipt_NETFLOW.c ipt_NETFLOW.h compat_def.h compat.h Makefile @echo Compiling for kernel $(KVERSION) make -C $(KDIR) M=$(CURDIR) modules CONFIG_DEBUG_INFO=y @touch $@ +compat_def.h: gen_compat_def + ./gen_compat_def > $@ sparse: | version.h ipt_NETFLOW.c ipt_NETFLOW.h compat.h Makefile @rm -f ipt_NETFLOW.ko ipt_NETFLOW.o @echo Compiling for kernel $(KVERSION) @@ -41,7 +43,7 @@ mclean: lclean: -rm -f *.so *_sh.o clean: mclean lclean - -rm -f *.so *.o modules.order version.h + -rm -f *.so *.o modules.order version.h compat_def.h snmp_NETFLOW.so: snmp_NETFLOW.c $(CC) -fPIC -shared -o $@ $< -lnetsnmp diff --git a/compat.h b/compat.h index 4996736..0f9896b 100644 --- a/compat.h +++ b/compat.h @@ -6,7 +6,7 @@ #ifndef COMPAT_NETFLOW_H #define COMPAT_NETFLOW_H - +#include "compat_def.h" #ifndef NIPQUAD # define NIPQUAD(addr) \ @@ -612,7 +612,7 @@ int in6_pton(const char *src, int srclen, #endif /* Offset changes made in 613dbd95723aee7abd16860745691b6c7bda20dc */ -#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,28) && LINUX_VERSION_CODE < KERNEL_VERSION(4,10,0) +#ifndef HAVE_XT_FAMILY # if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35) # define xt_action_param xt_target_param # endif diff --git a/gen_compat_def b/gen_compat_def new file mode 100755 index 0000000..bbc3976 --- /dev/null +++ b/gen_compat_def @@ -0,0 +1,71 @@ +#!/bin/bash -efu +# SPDX-License-Identifier: GPL-2.0-only +# +# Generate defines based on kernel having +# some symbols declared +# +# Copyright (C) 2019 +# + +fatal() { + echo "Error: $*" >&2 + exit 1 +} + +eval $(grep ^KDIR Makefile | tr -d ' ') +[ "$KDIR" ] || fatal "KDIR is not found" + +WD=cc-test-build +mkdir -p $WD +cd $WD || fatal "cannot cd to $WD" + +kbuild_test_compile() { + local cmd + + cat > test.c + echo obj-m = test.o > Makefile + cmd="make -s -C $KDIR M=$PWD modules" + echo "$cmd" > log + if $cmd >> log 2>&1; then + [ "$2" ] && echo "// $2 is declared ${3:+with <$3>}" + echo "#define HAVE_$1" + echo + else + echo "// ${2:-symbol} is undeclared${3:+ with <$3>}. Compile:" + sed "s/^/\/\/ /" test.c + echo "// Output:" + sed "s/^/\/\/ /" log + echo + if ! grep -q undeclared log; then + echo "Error: unexpected error from compiler" >&2 + cat log >&2 + echo >&2 + exit 3 + fi + fi +} + +kbuild_test_symbol() { +kbuild_test_compile ${1^^} $1 ${2-} < +${2:+#include <$2>} +MODULE_LICENSE("GPL"); +void *test = $1; +EOF +} + +echo "// Autogenerated for $KDIR" +echo + +# helpers introduced in 613dbd95723aee7abd16860745691b6c7bda20dc +kbuild_test_symbol xt_family linux/netfilter_ipv4/ip_tables.h + +echo "// End of compat_def.h" + +cd $OLDPWD +rm -rf $WD + +# debug output for Travis +if [ -z "${PWD/*travis*}" ]; then + cat compat_def.h >&2 +fi