chiark / gitweb /
man: add static device nodes and udevd debug options
[elogind.git] / extras / rule_generator / rule_generator.functions
1 # functions used by the udev rule generator
2 #
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.
6
7 PATH='/sbin:/bin'
8
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.
11 sysread() {
12         local file="$1"
13         [ -e "/sys$DEVPATH/$file" ] || return 0
14         local value
15         read value < "/sys$DEVPATH/$file" || return 0
16         echo "$value"
17 }
18
19 sysreadlink() {
20         local file="$1"
21         [ -e "/sys$DEVPATH/$file" ] || return 0
22         readlink -f /sys$DEVPATH/$file 2> /dev/null || true
23 }
24
25 # Return true if a directory is writeable.
26 writeable() {
27         if ln -s test-link $1/.is-writeable 2> /dev/null; then
28                 rm -f $1/.is-writeable
29                 return 0
30         else
31                 return 1
32         fi
33 }
34
35 # Create a lock file for the current rules file.
36 lock_rules_file() {
37         [ -e /dev/.udev/ ] || return 0
38
39         RULES_LOCK="/dev/.udev/.lock-${RULES_FILE##*/}"
40
41         retry=30
42         while ! mkdir $RULES_LOCK 2> /dev/null; do
43                 if [ $retry -eq 0 ]; then
44                          echo "Cannot lock $RULES_FILE!" >&2
45                          exit 2
46                 fi
47                 sleep 1
48                 retry=$(($retry - 1))
49         done
50 }
51
52 unlock_rules_file() {
53         [ "$RULES_LOCK" ] || return 0
54         rmdir $RULES_LOCK || true
55 }
56
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.
59 choose_rules_file() {
60         local tmp_rules_file="/dev/.udev/tmp-rules--${RULES_FILE##*/}"
61         [ -e "$RULES_FILE" -o -e "$tmp_rules_file" ] || PRINT_HEADER=1
62
63         if writeable ${RULES_FILE%/*}; then
64                 RO_RULES_FILE='/dev/null'
65         else
66                 RO_RULES_FILE=$RULES_FILE
67                 RULES_FILE=$tmp_rules_file
68         fi
69 }
70
71 # Return the name of the first free device.
72 raw_find_next_available() {
73         local links="$1"
74
75         local basename=${links%%[ 0-9]*}
76         local max=-1
77         for name in $links; do
78                 local num=${name#$basename}
79                 [ "$num" ] || num=0
80                 [ $num -gt $max ] && max=$num
81         done
82
83         local max=$(($max + 1))
84         # "name0" actually is just "name"
85         [ $max -eq 0 ] && return
86         echo "$max"
87 }
88
89 # Find all rules matching a key (with action) and a pattern.
90 find_all_rules() {
91         local key="$1"
92         local linkre="$2"
93         local match="$3"
94
95         local search='.*[[:space:],]'"$key"'"('"$linkre"')".*'
96         echo $(sed -n -r -e 's/^#.*//' -e "${match}s/${search}/\1/p" \
97                 $RO_RULES_FILE \
98                 $([ -e $RULES_FILE ] && echo $RULES_FILE) \
99                 2>/dev/null)
100 }