| |
1 ## |
| |
2 ## @l_prefix@/etc/rc.func -- Run-Command Helper Functions |
| |
3 ## Copyright (c) 2000-2007 OpenPKG Foundation e.V. <http://openpkg.net/> |
| |
4 ## Copyright (c) 2000-2007 Ralf S. Engelschall <http://engelschall.com/> |
| |
5 ## |
| |
6 ## Permission to use, copy, modify, and distribute this software for |
| |
7 ## any purpose with or without fee is hereby granted, provided that |
| |
8 ## the above copyright notice and this permission notice appear in all |
| |
9 ## copies. |
| |
10 ## |
| |
11 ## THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED |
| |
12 ## WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| |
13 ## MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| |
14 ## IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR |
| |
15 ## CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| |
16 ## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| |
17 ## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
| |
18 ## USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| |
19 ## ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| |
20 ## OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| |
21 ## OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| |
22 ## SUCH DAMAGE. |
| |
23 ## |
| |
24 |
| |
25 ## |
| |
26 ## NOTICE: This script is a collection of reusable shell functions |
| |
27 ## running under GNU Bash during the execution of OpenPKG run-command |
| |
28 ## sections. |
| |
29 ## |
| |
30 |
| |
31 # |
| |
32 # rcMsg (display message) |
| |
33 # |
| |
34 # Usage: rcMsg [-e] [-w] <message> |
| |
35 # Example: rcMsg -e "invalid command line" |
| |
36 # Description: display a regular/warning/error message. |
| |
37 # |
| |
38 rcMsg () { |
| |
39 local prefix="rc:" |
| |
40 while [ $# -gt 0 ]; do |
| |
41 case $1 in |
| |
42 -e ) prefix="${prefix}ERROR:"; shift ;; |
| |
43 -w ) prefix="${prefix}WARNING:"; shift ;; |
| |
44 * ) break ;; |
| |
45 esac |
| |
46 done |
| |
47 echo "${prefix} $*" |
| |
48 return 0 |
| |
49 } |
| |
50 |
| |
51 # |
| |
52 # rcPath (manipulate colon-separated PATH-style variable) |
| |
53 # |
| |
54 # Usage: rcPath [-a] [-r] [-p] [-e] <var> <dir> [<dir> ...] |
| |
55 # Example: rcPath -a -e PATH /bin /sbin /usr/bin /usr/sbin /usr/ccs/bin |
| |
56 # Description: removes (-r) or adds (-a) by appending or prepending |
| |
57 # (-p) one or more directories <dir> (optionally have |
| |
58 # to be existing if -e is given) to a colon-separated |
| |
59 # PATH-style variable <var>. In case a directory already |
| |
60 # exists, it is first removed. |
| |
61 # |
| |
62 rcPath () { |
| |
63 local mode="" |
| |
64 local prepend=0 |
| |
65 local exists=0 |
| |
66 while [ $# -gt 0 ]; do |
| |
67 case $1 in |
| |
68 -a ) mode="add"; shift ;; |
| |
69 -r ) mode="remove"; shift ;; |
| |
70 -p ) prepend=1; shift ;; |
| |
71 -e ) exists=1; shift ;; |
| |
72 * ) break ;; |
| |
73 esac |
| |
74 done |
| |
75 local var="$1" |
| |
76 shift |
| |
77 if [ ".${mode}" = .add ]; then |
| |
78 local edit_del="" |
| |
79 local edit_add="" |
| |
80 local dir |
| |
81 for dir in "$@"; do |
| |
82 if [ ".${exists}" = .1 ] && [ ! -d "${dir}" ]; then |
| |
83 continue |
| |
84 fi |
| |
85 edit_del="${edit_del} -e 's;^${dir}\$;;' -e 's;^${dir}:;;'" |
| |
86 edit_del="${edit_del} -e 's;:${dir}:;:;' -e 's;:${dir}\$;;'" |
| |
87 if [ ".${prepend}" = .0 ]; then |
| |
88 edit_add="${edit_add} -e 's;\$;:${dir};'" |
| |
89 else |
| |
90 edit_add="-e 's;^;${dir}:;' ${edit_add}" |
| |
91 fi |
| |
92 done |
| |
93 if [ ".${edit_del}${edit_add}" != . ]; then |
| |
94 eval "${var}=\`echo \"\$${var}\" | sed ${edit_del} ${edit_add}\`" |
| |
95 fi |
| |
96 return 0 |
| |
97 elif [ ".${mode}" = .remove ]; then |
| |
98 local edit="" |
| |
99 local dir |
| |
100 for dir in "$@"; do |
| |
101 edit="${edit} -e 's;^${dir}\$;;' -e 's;^${dir}:;;'" |
| |
102 edit="${edit} -e 's;:${dir}:;:;' -e 's;:${dir}\$;;'" |
| |
103 done |
| |
104 eval "${var}=\`echo \"\$${var}\" | sed ${edit}\`" |
| |
105 return 0 |
| |
106 else |
| |
107 rcMsg -e "rcPath: neither add (-a) nor remove (-r) operation specified" |
| |
108 return 1 |
| |
109 fi |
| |
110 } |
| |
111 |
| |
112 # |
| |
113 # rcTmp (temporary file handling) |
| |
114 # |
| |
115 # Usage: rcTmp [-i] [-f [-n <name>]] [-k] |
| |
116 # Example: rcTmp -i; tmpfile=`rcTmp -f -n tmp`; ...; rcTmp -k |
| |
117 # Description: ??? |
| |
118 # |
| |
119 rcTmp () { |
| |
120 local mode="" |
| |
121 local name="" |
| |
122 while [ $# -gt 0 ]; do |
| |
123 case $1 in |
| |
124 -i ) mode="init"; shift ;; |
| |
125 -f ) mode="file"; shift ;; |
| |
126 -k ) mode="kill"; shift ;; |
| |
127 -n ) name="$2"; shift; shift ;; |
| |
128 * ) break ;; |
| |
129 esac |
| |
130 done |
| |
131 if [ ".${mode}" = .init ]; then |
| |
132 if [ ".${RC_TMPDIR}" = . ]; then |
| |
133 local i=0 |
| |
134 while [ ${i} -lt 10 ]; do |
| |
135 RC_TMPDIR="@l_prefix@/RPM/TMP/rc-`date '+%Y%m%d%H%M%S'`-$$" |
| |
136 (umask 022; mkdir ${RC_TMPDIR} >/dev/null 2>&1) && break |
| |
137 i=$((${i} + 1)) |
| |
138 sleep 1 |
| |
139 done |
| |
140 if [ ${i} -eq 10 ]; then |
| |
141 rcMsg -e "rcTmp: unable to establish secure temporary directory" 1>&2 |
| |
142 return 1 |
| |
143 fi |
| |
144 declare -r RC_TMPDIR |
| |
145 fi |
| |
146 return 0 |
| |
147 elif [ ".${mode}" = .file ]; then |
| |
148 echo "${RC_TMPDIR}/${name:-tmp}" |
| |
149 return 0 |
| |
150 elif [ ".${mode}" = .kill ]; then |
| |
151 if [ ".${RC_TMPDIR}" = . ]; then |
| |
152 rcMsg -e "rcTmp: no secure temporary directory known" |
| |
153 return 1 |
| |
154 else |
| |
155 rm -rf ${RC_TMPDIR} |
| |
156 return 0 |
| |
157 fi |
| |
158 else |
| |
159 rcMsg -e "rcTmp: neither init (-i), file (-f) nor kill (-k) operation specified" |
| |
160 return 1 |
| |
161 fi |
| |
162 } |
| |
163 |
| |
164 # |
| |
165 # rcService (check for service status enable/active/usable) |
| |
166 # |
| |
167 # Usage: rcService <pkg> <service> <val> |
| |
168 # Example: if rcService openssh enable yes; then ... |
| |
169 # Description: check <service> of package <pkg> against value <val>. |
| |
170 # <service> has to be one of "enable", "active" or "usable". |
| |
171 # <val> has to be either "no", "yes", or "unknown". |
| |
172 # |
| |
173 rcService () { |
| |
174 local pkg="`echo ${1} | sed -e 's;-;_;g'`" |
| |
175 local var="${pkg}_${2}" |
| |
176 local chk="${3}" |
| |
177 eval "local val=\$${var}" |
| |
178 if [ ".${val}" = . ]; then |
| |
179 eval `@l_prefix@/bin/openpkg rc 2>/dev/null --silent ${1} status || true` |
| |
180 eval "local val=\$${var}" |
| |
181 fi |
| |
182 if [ ".${val}" = ".${chk}" ]; then |
| |
183 return 0 |
| |
184 else |
| |
185 return 1 |
| |
186 fi |
| |
187 } |
| |
188 |
| |
189 # |
| |
190 # rcVarIsYes (check variable for positive value) |
| |
191 # |
| |
192 # Usage: rcVarIsYes <var> |
| |
193 # Example: if rcVarIsYes foo; then ... |
| |
194 # Description: check whether a variable <var> contains a positive |
| |
195 # value, i.e., the values "yes", "true", "on" or "1" in |
| |
196 # arbitrary lower or upper case. |
| |
197 # |
| |
198 rcVarIsYes () { |
| |
199 local var="${1}" |
| |
200 eval "local val=\"\$${var}\"" |
| |
201 case "${val}" in |
| |
202 [Yy][Ee][Ss] | [Tt][Rr][Uu][Ee] | [Oo][Nn] | 1 ) |
| |
203 return 0 |
| |
204 ;; |
| |
205 * ) |
| |
206 return 1 |
| |
207 ;; |
| |
208 esac |
| |
209 } |
| |
210 |