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