Skip to content

Commit

Permalink
Introduce helper to detect kernel declarations.
Browse files Browse the repository at this point in the history
This is to avoid playing roulette with mainline kernel versions vs
stable versions vs RHEL versions.
  • Loading branch information
aabc committed Sep 28, 2019
1 parent 8a2966f commit daeca11
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 4 deletions.
6 changes: 4 additions & 2 deletions Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#ifndef COMPAT_NETFLOW_H
#define COMPAT_NETFLOW_H

#include "compat_def.h"

#ifndef NIPQUAD
# define NIPQUAD(addr) \
Expand Down Expand Up @@ -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
Expand Down
71 changes: 71 additions & 0 deletions gen_compat_def
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
#

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-} <<EOF
#include <linux/module.h>
${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

0 comments on commit daeca11

Please sign in to comment.