tools/testing/selftests/net/netfilter/nft_audit.sh

Source file repositories/reference/linux-study-clean/tools/testing/selftests/net/netfilter/nft_audit.sh

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/net/netfilter/nft_audit.sh
Extension
.sh
Size
6784 bytes
Lines
270
Domain
Support Tooling And Documentation
Bucket
tools
Inferred role
Support Tooling And Documentation: tools
Status
atlas-only

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

#!/bin/bash
# SPDX-License-Identifier: GPL-2.0
#
# Check that audit logs generated for nft commands are as expected.

SKIP_RC=4
RC=0

if [ -r /var/run/auditd.pid ];then
	read pid < /var/run/auditd.pid
	p=$(pgrep ^auditd$)

	if [ "$pid" -eq "$p" ]; then
		echo "SKIP: auditd is running"
		exit $SKIP_RC
	fi
fi

nft --version >/dev/null 2>&1 || {
	echo "SKIP: missing nft tool"
	exit $SKIP_RC
}

# nft must be recent enough to support "reset" keyword.
nft --check -f /dev/stdin >/dev/null 2>&1 <<EOF
add table t
add chain t c
reset rules t c
EOF

if [ "$?" -ne 0 ];then
	echo -n "SKIP: nft reset feature test failed: "
	nft --version
	exit $SKIP_RC
fi

# Run everything in a separate network namespace
[ "${1}" != "run" ] && { unshare -n "${0}" run; exit $?; }

# give other scripts a chance to finish - audit_logread sees all activity
sleep 1

logfile=$(mktemp)
rulefile=$(mktemp)
echo "logging into $logfile"
./audit_logread >"$logfile" &
logread_pid=$!
trap 'kill $logread_pid; rm -f $logfile $rulefile' EXIT
exec 3<"$logfile"

lsplit='s/^\(.*\) entries=\([^ ]*\) \(.*\)$/pfx="\1"\nval="\2"\nsfx="\3"/'
summarize_logs() {
	sum=0
	while read line; do
		eval $(sed "$lsplit" <<< "$line")
		[[ $sum -gt 0 ]] && {
			[[ "$pfx $sfx" == "$tpfx $tsfx" ]] && {
				let "sum += val"
				continue
			}
			echo "$tpfx entries=$sum $tsfx"
		}
		tpfx="$pfx"
		tsfx="$sfx"
		sum=$val
	done
	echo "$tpfx entries=$sum $tsfx"
}

do_test() { # (cmd, log)

Annotation

Implementation Notes