rule_generator.functions 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # functions used by the udev rule generator
  2. # Copyright (C) 2006 Marco d'Itri <md@Linux.IT>
  3. # Updated for LFS by Bruce Dubbs <bdubbs@linuxfromscratch.org>
  4. # Hardcoded RUNDIR
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 2 of the License, or
  8. # (at your option) any later version.
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. PATH='/usr/bin:/bin:/usr/sbin:/sbin'
  16. # Read a single line from file $1 in the $DEVPATH directory.
  17. # The function must not return an error even if the file does not exist.
  18. sysread() {
  19. local file="$1"
  20. [ -e "/sys$DEVPATH/$file" ] || return 0
  21. local value
  22. read value < "/sys$DEVPATH/$file" || return 0
  23. echo "$value"
  24. }
  25. sysreadlink() {
  26. local file="$1"
  27. [ -e "/sys$DEVPATH/$file" ] || return 0
  28. readlink -f /sys$DEVPATH/$file 2> /dev/null || true
  29. }
  30. # Return true if a directory is writeable.
  31. writeable() {
  32. if ln -s test-link $1/.is-writeable 2> /dev/null; then
  33. rm -f $1/.is-writeable
  34. return 0
  35. else
  36. return 1
  37. fi
  38. }
  39. # Create a lock file for the current rules file.
  40. lock_rules_file() {
  41. RUNDIR=/run/udev
  42. [ -e "$RUNDIR" ] || return 0
  43. RULES_LOCK="$RUNDIR/.lock-${RULES_FILE##*/}"
  44. retry=30
  45. while ! mkdir $RULES_LOCK 2> /dev/null; do
  46. if [ $retry -eq 0 ]; then
  47. echo "Cannot lock $RULES_FILE!" >&2
  48. exit 2
  49. fi
  50. sleep 1
  51. retry=$(($retry - 1))
  52. done
  53. }
  54. unlock_rules_file() {
  55. [ "$RULES_LOCK" ] || return 0
  56. rmdir $RULES_LOCK || true
  57. }
  58. # Choose the real rules file if it is writeable or a temporary file if not.
  59. # Both files should be checked later when looking for existing rules.
  60. choose_rules_file() {
  61. RUNDIR=/run/udev
  62. local tmp_rules_file="$RUNDIR/tmp-rules--${RULES_FILE##*/}"
  63. [ -e "$RULES_FILE" -o -e "$tmp_rules_file" ] || PRINT_HEADER=1
  64. if writeable ${RULES_FILE%/*}; then
  65. RO_RULES_FILE='/dev/null'
  66. else
  67. RO_RULES_FILE=$RULES_FILE
  68. RULES_FILE=$tmp_rules_file
  69. fi
  70. }
  71. # Return the name of the first free device.
  72. raw_find_next_available() {
  73. local links="$1"
  74. local basename=${links%%[ 0-9]*}
  75. local max=-1
  76. for name in $links; do
  77. local num=${name#$basename}
  78. [ "$num" ] || num=0
  79. [ $num -gt $max ] && max=$num
  80. done
  81. local max=$(($max + 1))
  82. # "name0" actually is just "name"
  83. [ $max -eq 0 ] && return
  84. echo "$max"
  85. }
  86. # Find all rules matching a key (with action) and a pattern.
  87. find_all_rules() {
  88. local key="$1"
  89. local linkre="$2"
  90. local match="$3"
  91. local search='.*[[:space:],]'"$key"'"('"$linkre"')".*'
  92. echo $(sed -n -r -e 's/^#.*//' -e "${match}s/${search}/\1/p" \
  93. $RO_RULES_FILE \
  94. $([ -e $RULES_FILE ] && echo $RULES_FILE) \
  95. 2>/dev/null)
  96. }