headers_install.sh 1.8 KB

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