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