1#!/bin/sh 2# SPDX-License-Identifier: GPL-2.0 3 4if [ $# -ne 2 ] 5then 6 echo "Usage: headers_install.sh INFILE OUTFILE" 7 echo 8 echo "Prepares kernel header files for use by user space, by removing" 9 echo "all compiler.h definitions and #includes, removing any" 10 echo "#ifdef __KERNEL__ sections, and putting __underscores__ around" 11 echo "asm/inline/volatile keywords." 12 echo 13 echo "INFILE: header file to operate on" 14 echo "OUTFILE: output file which the processed header is written to" 15 16 exit 1 17fi 18 19# Grab arguments 20INFILE=$1 21OUTFILE=$2 22TMPFILE=$OUTFILE.tmp 23 24trap 'rm -f $OUTFILE $TMPFILE' EXIT 25 26# SPDX-License-Identifier with GPL variants must have "WITH Linux-syscall-note" 27if [ -n "$(sed -n -e "/SPDX-License-Identifier:.*GPL-/{/WITH Linux-syscall-note/!p}" $INFILE)" ]; then 28 echo "error: $INFILE: missing \"WITH Linux-syscall-note\" for SPDX-License-Identifier" >&2 29 exit 1 30fi 31 32sed -E -e ' 33 s/([[:space:](])(__user|__force|__iomem)[[:space:]]/\1/g 34 s/__attribute_const__([[:space:]]|$)/\1/g 35 s@^#include <linux/compiler.h>@@ 36 s/(^|[^a-zA-Z0-9])__packed([^a-zA-Z0-9_]|$)/\1__attribute__((packed))\2/g 37 s/(^|[[:space:](])(inline|asm|volatile)([[:space:](]|$)/\1__\2__\3/g 38 s@#(ifndef|define|endif[[:space:]]*/[*])[[:space:]]*_UAPI@#\1 @ 39 s/__ASSEMBLY__/__ASSEMBLER__/g 40' $INFILE > $TMPFILE || exit 1 41 42scripts/unifdef -U__KERNEL__ -D__EXPORTED_HEADERS__ $TMPFILE > $OUTFILE 43[ $? -gt 1 ] && exit 1 44 45# Remove /* ... */ style comments, and find CONFIG_ references in code 46configs=$(sed -e ' 47:comment 48 s:/\*[^*][^*]*:/*: 49 s:/\*\*\**\([^/]\):/*\1: 50 t comment 51 s:/\*\*/: : 52 t comment 53 /\/\*/! b check 54 N 55 b comment 56:print 57 P 58 D 59:check 60 s:^\(CONFIG_[[:alnum:]_]*\):\1\n: 61 t print 62 s:^[[:alnum:]_][[:alnum:]_]*:: 63 s:^[^[:alnum:]_][^[:alnum:]_]*:: 64 t check 65 d 66' $OUTFILE) 67 68for c in $configs 69do 70 echo "error: $INFILE: leak $c to user-space" >&2 71 exit 1 72done 73 74rm -f $TMPFILE 75trap - EXIT 76