1 # functions used by the udev rule generator
3 # Copyright (C) 2006 Marco d'Itri <md@Linux.IT>
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.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
23 # Read a single line from file $1 in the $DEVPATH directory.
24 # The function must not return an error even if the file does not exist.
27 [ -e "/sys$DEVPATH/$file" ] || return 0
29 read value < "/sys$DEVPATH/$file" || return 0
35 [ -e "/sys$DEVPATH/$file" ] || return 0
36 readlink -f /sys$DEVPATH/$file 2> /dev/null || true
39 # Return true if a directory is writeable.
41 if ln -s test-link $1/.is-writeable 2> /dev/null; then
42 rm -f $1/.is-writeable
49 # Create a lock file for the current rules file.
51 RUNDIR=$(udevadm info --run)
52 [ -e "$RUNDIR" ] || return 0
54 RULES_LOCK="$RUNDIR/.lock-${RULES_FILE##*/}"
57 while ! mkdir $RULES_LOCK 2> /dev/null; do
58 if [ $retry -eq 0 ]; then
59 echo "Cannot lock $RULES_FILE!" >&2
68 [ "$RULES_LOCK" ] || return 0
69 rmdir $RULES_LOCK || true
72 # Choose the real rules file if it is writeable or a temporary file if not.
73 # Both files should be checked later when looking for existing rules.
75 RUNDIR=$(udevadm info --run)
76 local tmp_rules_file="$RUNDIR/tmp-rules--${RULES_FILE##*/}"
77 [ -e "$RULES_FILE" -o -e "$tmp_rules_file" ] || PRINT_HEADER=1
79 if writeable ${RULES_FILE%/*}; then
80 RO_RULES_FILE='/dev/null'
82 RO_RULES_FILE=$RULES_FILE
83 RULES_FILE=$tmp_rules_file
87 # Return the name of the first free device.
88 raw_find_next_available() {
91 local basename=${links%%[ 0-9]*}
93 for name in $links; do
94 local num=${name#$basename}
96 [ $num -gt $max ] && max=$num
99 local max=$(($max + 1))
100 # "name0" actually is just "name"
101 [ $max -eq 0 ] && return
105 # Find all rules matching a key (with action) and a pattern.
111 local search='.*[[:space:],]'"$key"'"('"$linkre"')".*'
112 echo $(sed -n -r -e 's/^#.*//' -e "${match}s/${search}/\1/p" \
114 $([ -e $RULES_FILE ] && echo $RULES_FILE) \