1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
# Setup and shell utility functions for use in hotplug agents.
# vim: syntax=sh
#DEBUG=yes
#EVENTS_LOG='/dev/hotplug.log'
FIRMWARE_DIRS='/lib/firmware /usr/local/lib/firmware /usr/lib/hotplug/firmware'
EVENTS_DIR='/dev/.events'
MODPROBE='/sbin/modprobe -s -q'
PATH='/sbin:/bin:/usr/sbin:/usr/bin'
[ -e /etc/default/hotplug ] && . /etc/default/hotplug
if [ -x /usr/bin/logger ]; then
LOGGER=/usr/bin/logger
elif [ -x /bin/logger ]; then
LOGGER=/bin/logger
else
unset LOGGER
fi
# for diagnostics
if [ -t 1 -a -z "$LOGGER" ] || [ ! -e '/dev/log' ]; then
mesg() {
echo "$@" >&2
}
elif [ -t 1 ]; then
mesg() {
echo "$@"
$LOGGER -t "${0##*/}[$$]" "$@"
}
else
mesg() {
$LOGGER -t "${0##*/}[$$]" "$@"
}
fi
debug_mesg() {
[ -z "$DEBUG" -o "$DEBUG" = no ] && return 0
mesg "$@"
}
wait_for_file() {
local file=$1
local timeout=$2
[ "$timeout" ] || timeout=120
local count=$timeout
while [ $count != 0 ]; do
[ -e "$file" ] && return 0
sleep 1
count=$(($count - 1))
done
mesg "$file did not appear before the timeout!"
exit 1
}
# Read a single line from file $1 in the $DEVPATH directory.
# The function must not return an error even if the file does not exist.
sysread() {
local file="$1"
[ -e "/sys$DEVPATH/$file" ] || return 0
local value
read value < "/sys$DEVPATH/$file" || return 0
echo "$value"
}
sysreadlink() {
local file="$1"
[ -e "/sys$DEVPATH/$file" ] || return 0
readlink -f /sys$DEVPATH/$file 2> /dev/null || true
}
# returns true if a directory is writeable
writeable() {
if ln -s check $1/.is-writeable 2> /dev/null; then
rm -f $1/.is-writeable
return 0
else
return 1
fi
}
##############################################################################
lock_rules_file() {
[ -e /dev/.udev/ ] || return 0
RULES_LOCK="/dev/.udev/.lock-${RULES_FILE##*/}"
retry=30
while ! mkdir $RULES_LOCK 2> /dev/null; do
if [ $retry -eq 0 ]; then
echo "Cannot lock $RULES_FILE!" >&2
exit 2
fi
sleep 1
retry=$(($retry - 1))
done
}
unlock_rules_file() {
[ "$RULES_LOCK" ] || return 0
rmdir $RULES_LOCK || true
}
choose_rules_file() {
local tmp_rules_file="/dev/.udev/tmp-rules--${RULES_FILE##*/}"
[ -e "$RULES_FILE" -o -e "$tmp_rules_file" ] || PRINT_HEADER=1
if writeable ${RULES_FILE%/*}; then
RO_RULES_FILE='/dev/null'
else
RO_RULES_FILE=$RULES_FILE
RULES_FILE=$tmp_rules_file
fi
}
##############################################################################
raw_find_next_available() {
local links="$1"
local basename=${links%%[ 0-9]*}
local max=-1
for name in $links; do
local num=${name#$basename}
[ "$num" ] || num=0
[ $num -gt $max ] && max=$num
done
local max=$(($max + 1))
# "name0" actually is just "name"
[ $max -eq 0 ] && return
echo "$max"
}
find_all_rules() {
local key="$1"
local linkre="$2"
local match="$3"
local search='.*[[:space:],]'"$key"'"\('"$linkre"'\)"[[:space:]]*\(,.*\|\\\|\)$'
echo $(sed -n -e "${match}s/${search}/\1/p" $RO_RULES_FILE $RULES_FILE)
}
|