1 # functions used by the udev rule generator
3 # This program is free software; you can redistribute it and/or modify it
4 # under the terms of the GNU General Public License as published by the
5 # Free Software Foundation version 2 of the License.
9 # Read a single line from file $1 in the $DEVPATH directory.
10 # The function must not return an error even if the file does not exist.
13 [ -e "/sys$DEVPATH/$file" ] || return 0
15 read value < "/sys$DEVPATH/$file" || return 0
21 [ -e "/sys$DEVPATH/$file" ] || return 0
22 readlink -f /sys$DEVPATH/$file 2> /dev/null || true
25 # Return true if a directory is writeable.
27 if ln -s test-link $1/.is-writeable 2> /dev/null; then
28 rm -f $1/.is-writeable
35 # Create a lock file for the current rules file.
37 [ -e /dev/.udev/ ] || return 0
39 RULES_LOCK="/dev/.udev/.lock-${RULES_FILE##*/}"
42 while ! mkdir $RULES_LOCK 2> /dev/null; do
43 if [ $retry -eq 0 ]; then
44 echo "Cannot lock $RULES_FILE!" >&2
53 [ "$RULES_LOCK" ] || return 0
54 rmdir $RULES_LOCK || true
57 # Choose the real rules file if it is writeable or a temporary file if not.
58 # Both files should be checked later when looking for existing rules.
60 local tmp_rules_file="/dev/.udev/tmp-rules--${RULES_FILE##*/}"
61 [ -e "$RULES_FILE" -o -e "$tmp_rules_file" ] || PRINT_HEADER=1
63 if writeable ${RULES_FILE%/*}; then
64 RO_RULES_FILE='/dev/null'
66 RO_RULES_FILE=$RULES_FILE
67 RULES_FILE=$tmp_rules_file
71 # Return the name of the first free device.
72 raw_find_next_available() {
75 local basename=${links%%[ 0-9]*}
77 for name in $links; do
78 local num=${name#$basename}
80 [ $num -gt $max ] && max=$num
83 local max=$(($max + 1))
84 # "name0" actually is just "name"
85 [ $max -eq 0 ] && return
89 # Find all rules matching a key (with action) and a pattern.
95 local search='[[:space:],]'"$key"'"('"$linkre"')"[[:space:]]*(,.*|\\|)$'
97 echo $(sed -n -r -e 's/^#.*//' -e "${match}s/${search}/\1/p" \
99 $([ -e $RULES_FILE ] && echo $RULES_FILE) \