chiark / gitweb /
tabs are as useful as a hole in the head
[elogind.git] / src / extras / rule_generator / rule_generator.functions
1 # functions used by the udev rule generator
2
3 # Copyright (C) 2006 Marco d'Itri <md@Linux.IT>
4
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.
9
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.
14
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/>.
17
18 PATH='/usr/bin:/bin:/usr/sbin:/sbin'
19
20 # Read a single line from file $1 in the $DEVPATH directory.
21 # The function must not return an error even if the file does not exist.
22 sysread() {
23         local file="$1"
24         [ -e "/sys$DEVPATH/$file" ] || return 0
25         local value
26         read value < "/sys$DEVPATH/$file" || return 0
27         echo "$value"
28 }
29
30 sysreadlink() {
31         local file="$1"
32         [ -e "/sys$DEVPATH/$file" ] || return 0
33         readlink -f /sys$DEVPATH/$file 2> /dev/null || true
34 }
35
36 # Return true if a directory is writeable.
37 writeable() {
38         if ln -s test-link $1/.is-writeable 2> /dev/null; then
39                 rm -f $1/.is-writeable
40                 return 0
41         else
42                 return 1
43         fi
44 }
45
46 # Create a lock file for the current rules file.
47 lock_rules_file() {
48         RUNDIR=$(udevadm info --run)
49         [ -e "$RUNDIR" ] || return 0
50
51         RULES_LOCK="$RUNDIR/.lock-${RULES_FILE##*/}"
52
53         retry=30
54         while ! mkdir $RULES_LOCK 2> /dev/null; do
55                 if [ $retry -eq 0 ]; then
56                          echo "Cannot lock $RULES_FILE!" >&2
57                          exit 2
58                 fi
59                 sleep 1
60                 retry=$(($retry - 1))
61         done
62 }
63
64 unlock_rules_file() {
65         [ "$RULES_LOCK" ] || return 0
66         rmdir $RULES_LOCK || true
67 }
68
69 # Choose the real rules file if it is writeable or a temporary file if not.
70 # Both files should be checked later when looking for existing rules.
71 choose_rules_file() {
72         RUNDIR=$(udevadm info --run)
73         local tmp_rules_file="$RUNDIR/tmp-rules--${RULES_FILE##*/}"
74         [ -e "$RULES_FILE" -o -e "$tmp_rules_file" ] || PRINT_HEADER=1
75
76         if writeable ${RULES_FILE%/*}; then
77                 RO_RULES_FILE='/dev/null'
78         else
79                 RO_RULES_FILE=$RULES_FILE
80                 RULES_FILE=$tmp_rules_file
81         fi
82 }
83
84 # Return the name of the first free device.
85 raw_find_next_available() {
86         local links="$1"
87
88         local basename=${links%%[ 0-9]*}
89         local max=-1
90         for name in $links; do
91                 local num=${name#$basename}
92                 [ "$num" ] || num=0
93                 [ $num -gt $max ] && max=$num
94         done
95
96         local max=$(($max + 1))
97         # "name0" actually is just "name"
98         [ $max -eq 0 ] && return
99         echo "$max"
100 }
101
102 # Find all rules matching a key (with action) and a pattern.
103 find_all_rules() {
104         local key="$1"
105         local linkre="$2"
106         local match="$3"
107
108         local search='.*[[:space:],]'"$key"'"('"$linkre"')".*'
109         echo $(sed -n -r -e 's/^#.*//' -e "${match}s/${search}/\1/p" \
110                 $RO_RULES_FILE \
111                 $([ -e $RULES_FILE ] && echo $RULES_FILE) \
112                 2>/dev/null)
113 }