rule_generator.functions revision 6997e3b2dc0095985071e2f7496342a850cdb5ad
fb0951b02ebf51a93acf12721d8857d31ce57ba3Lennart Poettering# functions used by the udev rule generator
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering# Copyright (C) 2006 Marco d'Itri <md@Linux.IT>
3e2147858f21943d5f4a781c60f33ac22c6096edKay Sievers
3e2147858f21943d5f4a781c60f33ac22c6096edKay Sievers# This program is free software: you can redistribute it and/or modify
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering# it under the terms of the GNU General Public License as published by
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering# the Free Software Foundation, either version 2 of the License, or
5430f7f2bc7330f3088b894166bf3524a067e3d8Lennart Poettering# (at your option) any later version.
5430f7f2bc7330f3088b894166bf3524a067e3d8Lennart Poettering
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering# This program is distributed in the hope that it will be useful,
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering# but WITHOUT ANY WARRANTY; without even the implied warranty of
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering# GNU General Public License for more details.
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering
5430f7f2bc7330f3088b894166bf3524a067e3d8Lennart Poettering# You should have received a copy of the GNU General Public License
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering# along with this program. If not, see <http://www.gnu.org/licenses/>.
5430f7f2bc7330f3088b894166bf3524a067e3d8Lennart Poettering
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart PoetteringPATH='/usr/bin:/bin:/usr/sbin:/sbin'
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering
4e949c11a1df4547d5f102e4131e07b026369cd7Javier Jardón# Read a single line from file $1 in the $DEVPATH directory.
4e949c11a1df4547d5f102e4131e07b026369cd7Javier Jardón# The function must not return an error even if the file does not exist.
4e949c11a1df4547d5f102e4131e07b026369cd7Javier Jardónsysread() {
4f9bca639a806e235173ec08e01de8bdcec82f81Lennart Poettering local file="$1"
4e949c11a1df4547d5f102e4131e07b026369cd7Javier Jardón [ -e "/sys$DEVPATH/$file" ] || return 0
4e949c11a1df4547d5f102e4131e07b026369cd7Javier Jardón local value
4e949c11a1df4547d5f102e4131e07b026369cd7Javier Jardón read value < "/sys$DEVPATH/$file" || return 0
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering echo "$value"
78a825f216d39ee0295b00647b059d45467e1d02Kay Sievers}
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poetteringsysreadlink() {
a80db8bd5f7f15859e8891aab9fc3694ce4cd0bdJavier Jardón local file="$1"
a80db8bd5f7f15859e8891aab9fc3694ce4cd0bdJavier Jardón [ -e "/sys$DEVPATH/$file" ] || return 0
4db6d587c37c0357d20c79bf1a7c9afd4c7ced61Kay Sievers readlink -f /sys$DEVPATH/$file 2> /dev/null || true
907dd1953b7517534d646f5b2777780020c896e2Kay Sievers}
eb7bbee6cd182d5c4eb1e1180631c35158f59379Kay Sievers
6aad7f2cd75c88f990c7857bb173b3e8d0faee0fUmut Tezduyar Lindskog# Return true if a directory is writeable.
91ca5bf0b6f3b487a16cc262527c9de6744db624Martin Pittwriteable() {
3e2147858f21943d5f4a781c60f33ac22c6096edKay Sievers if ln -s test-link $1/.is-writeable 2> /dev/null; then
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering rm -f $1/.is-writeable
22be093ffb403a1c474037939ca9b88b1ee39f77Lennart Poettering return 0
3e2147858f21943d5f4a781c60f33ac22c6096edKay Sievers else
8666abb452db73d9a11ead61251eec42bc531cceKay Sievers return 1
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering fi
5e63ce78b5018ba612e794a610a6f13c5eefade7Cristian Rodríguez}
3d585edbb14b9705c80183aeb16dfd0a28df0ac9Lennart Poettering
5e63ce78b5018ba612e794a610a6f13c5eefade7Cristian Rodríguez# Create a lock file for the current rules file.
0fa2cac4f0cdefaf1addd7f1fe0fd8113db9360bKay Sieverslock_rules_file() {
0fa2cac4f0cdefaf1addd7f1fe0fd8113db9360bKay Sievers RUNDIR=$(udevadm info --run)
0fa2cac4f0cdefaf1addd7f1fe0fd8113db9360bKay Sievers [ -e "$RUNDIR" ] || return 0
4c6abc93c708762ae3f377eab8dbd357262cc432Koen Kooi
f15515b5e6a9ebe95c938cc670df6e576fcf9176Filipe Brandenburger RULES_LOCK="$RUNDIR/.lock-${RULES_FILE##*/}"
0fa2cac4f0cdefaf1addd7f1fe0fd8113db9360bKay Sievers
f15515b5e6a9ebe95c938cc670df6e576fcf9176Filipe Brandenburger retry=30
f15515b5e6a9ebe95c938cc670df6e576fcf9176Filipe Brandenburger while ! mkdir $RULES_LOCK 2> /dev/null; do
f15515b5e6a9ebe95c938cc670df6e576fcf9176Filipe Brandenburger if [ $retry -eq 0 ]; then
f15515b5e6a9ebe95c938cc670df6e576fcf9176Filipe Brandenburger echo "Cannot lock $RULES_FILE!" >&2
f15515b5e6a9ebe95c938cc670df6e576fcf9176Filipe Brandenburger exit 2
f15515b5e6a9ebe95c938cc670df6e576fcf9176Filipe Brandenburger fi
f15515b5e6a9ebe95c938cc670df6e576fcf9176Filipe Brandenburger sleep 1
f15515b5e6a9ebe95c938cc670df6e576fcf9176Filipe Brandenburger retry=$(($retry - 1))
f15515b5e6a9ebe95c938cc670df6e576fcf9176Filipe Brandenburger done
f15515b5e6a9ebe95c938cc670df6e576fcf9176Filipe Brandenburger}
2f96919bcdd0978164c801b21e053fb3b31e8bacFilipe Brandenburger
a59f16ce4a11c440cb2136ad3d5e3184714c545eRobert Schieleunlock_rules_file() {
2f96919bcdd0978164c801b21e053fb3b31e8bacFilipe Brandenburger [ "$RULES_LOCK" ] || return 0
1c7dde3e475978c569a982d65fd86d4b4e3caad8Bastien Nocera rmdir $RULES_LOCK || true
2f96919bcdd0978164c801b21e053fb3b31e8bacFilipe Brandenburger}
2f96919bcdd0978164c801b21e053fb3b31e8bacFilipe Brandenburger
2f96919bcdd0978164c801b21e053fb3b31e8bacFilipe Brandenburger# Choose the real rules file if it is writeable or a temporary file if not.
2f96919bcdd0978164c801b21e053fb3b31e8bacFilipe Brandenburger# Both files should be checked later when looking for existing rules.
2f96919bcdd0978164c801b21e053fb3b31e8bacFilipe Brandenburgerchoose_rules_file() {
2f96919bcdd0978164c801b21e053fb3b31e8bacFilipe Brandenburger RUNDIR=$(udevadm info --run)
2f96919bcdd0978164c801b21e053fb3b31e8bacFilipe Brandenburger local tmp_rules_file="$RUNDIR/tmp-rules--${RULES_FILE##*/}"
1c7dde3e475978c569a982d65fd86d4b4e3caad8Bastien Nocera [ -e "$RULES_FILE" -o -e "$tmp_rules_file" ] || PRINT_HEADER=1
1c7dde3e475978c569a982d65fd86d4b4e3caad8Bastien Nocera
1c7dde3e475978c569a982d65fd86d4b4e3caad8Bastien Nocera if writeable ${RULES_FILE%/*}; then
20f56fddcd58c84fa73597486e905c652667214fDidier Roche RO_RULES_FILE='/dev/null'
1c7dde3e475978c569a982d65fd86d4b4e3caad8Bastien Nocera else
e9da3678fcfc774b325dc1eaa054d0e00028a1fcLennart Poettering RO_RULES_FILE=$RULES_FILE
e9da3678fcfc774b325dc1eaa054d0e00028a1fcLennart Poettering RULES_FILE=$tmp_rules_file
e9da3678fcfc774b325dc1eaa054d0e00028a1fcLennart Poettering fi
3ce4fad8f548db9edb19869ea540e3192d2123f4Kay Sievers}
f975e971accc4d50c73ae53167db3df7a7099cf2Lennart Poettering
e9da3678fcfc774b325dc1eaa054d0e00028a1fcLennart Poettering# Return the name of the first free device.
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poetteringraw_find_next_available() {
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering local links="$1"
3e2147858f21943d5f4a781c60f33ac22c6096edKay Sievers
fe1fed02c7637a2c18cd575f78be7fda27972148Zbigniew Jędrzejewski-Szmek local basename=${links%%[ 0-9]*}
92ec4495f76a7a2a6c31f5bb2a5240f78dcfe1d2Javier Jardón local max=-1
2d0efdf1af5ff77441228854343c61d42a89840cSamuli Suominen for name in $links; do
2d0efdf1af5ff77441228854343c61d42a89840cSamuli Suominen local num=${name#$basename}
b51fc639f01ee6194af3e7e944a79accce474fe1Dave Reisner [ "$num" ] || num=0
dd5ae4c36c89da5dbe8d1628939b26c00db98753Przemyslaw Kedzierski [ $num -gt $max ] && max=$num
dd5ae4c36c89da5dbe8d1628939b26c00db98753Przemyslaw Kedzierski done
2d0efdf1af5ff77441228854343c61d42a89840cSamuli Suominen
96ede2601f27cd5fe52eed96b873bef55cd0ce23Lennart Poettering local max=$(($max + 1))
2d0efdf1af5ff77441228854343c61d42a89840cSamuli Suominen # "name0" actually is just "name"
edeb68c53f1cdc452016b4c8512586a70b1262e3Tom Gundersen [ $max -eq 0 ] && return
2d0efdf1af5ff77441228854343c61d42a89840cSamuli Suominen echo "$max"
7801356442578ff6e1c65844eb9e65c819af4660Zbigniew Jędrzejewski-Szmek}
66b0e0e0e3652227fe107ab9d09fa14fd4bc4dfaCristian Rodríguez
66b0e0e0e3652227fe107ab9d09fa14fd4bc4dfaCristian Rodríguez# Find all rules matching a key (with action) and a pattern.
f00929ad622c978f8ad83590a15a765b4beecac9Dimitri John Ledkovfind_all_rules() {
f00929ad622c978f8ad83590a15a765b4beecac9Dimitri John Ledkov local key="$1"
f00929ad622c978f8ad83590a15a765b4beecac9Dimitri John Ledkov local linkre="$2"
446883528524429283626208928b51f49f28f810Lennart Poettering local match="$3"
446883528524429283626208928b51f49f28f810Lennart Poettering
4acbce79798347cddf1e1d42e9be571e0a041873Zbigniew Jędrzejewski-Szmek local search='.*[[:space:],]'"$key"'"('"$linkre"')".*'
4acbce79798347cddf1e1d42e9be571e0a041873Zbigniew Jędrzejewski-Szmek echo $(sed -n -r -e 's/^#.*//' -e "${match}s/${search}/\1/p" \
80a5cbace45a6adbf2f9119edc5a4b10db493064Kay Sievers $RO_RULES_FILE \
b37250d661ed67d07c734630617d73e64f6d7e49Zbigniew Jędrzejewski-Szmek $([ -e $RULES_FILE ] && echo $RULES_FILE) \
b37250d661ed67d07c734630617d73e64f6d7e49Zbigniew Jędrzejewski-Szmek 2>/dev/null)
6581f00f7eabdaccf587a4b6af60ed4696dd2791Zbigniew Jędrzejewski-Szmek}
6581f00f7eabdaccf587a4b6af60ed4696dd2791Zbigniew Jędrzejewski-Szmek