/[baselayout]/trunk/sbin/functions.sh
Gentoo

Contents of /trunk/sbin/functions.sh

Parent Directory Parent Directory | Revision Log Revision Log


Revision 344 - (show annotations) (download) (as text)
Sun Mar 30 10:12:56 2003 UTC (16 years, 1 month ago) by azarah
File MIME type: text/x-sh
File size: 9998 byte(s)
serial console fix

1 # Copyright 1999-2003 Gentoo Technologies, Inc.
2 # Distributed under the terms of the GNU General Public License v2
3 # $Header$
4
5 umask 022
6
7 if [ -z "${EBUILD}" ]
8 then
9 # Setup a basic $PATH. Just add system default to existing.
10 # This should solve both /sbin and /usr/sbin not present when
11 # doing 'su -c foo', or for something like: PATH= rcscript start
12 PATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/sbin:${PATH}"
13 fi
14
15 # daemontools dir
16 SVCDIR="/var/lib/supervise"
17
18 # Check /etc/conf.d/rc for a description of these ...
19 svcdir="/var/lib/init.d"
20 svcmount="no"
21 svcfstype="tmpfs"
22 svcsize=1024
23 shmdir="/mnt/.shm"
24
25 # Different types of dependencies
26 deptypes="need use"
27 # Different types of order deps
28 ordtypes="before after"
29
30 #
31 # Internal variables
32 #
33
34 # Dont output to stdout?
35 QUIET_STDOUT="no"
36
37 #
38 # Default values for rc system
39 #
40 RC_NET_STRICT_CHECKING="no"
41
42 # Override defaults with user settings ...
43 [ -f /etc/conf.d/rc ] && source /etc/conf.d/rc
44
45
46 getcols() {
47 echo "$2"
48 }
49
50 if [ -n "${EBUILD}" ] && [ "${*/depend}" != "$*" ]
51 then
52 COLS="48 80"
53 else
54 COLS="`stty size 2> /dev/null`"
55 fi
56 if [ "${COLS}" = "0 0" ]
57 then
58 # Fix for serial tty (bug #11557)
59 COLS="24 80"
60 stty cols 80 &>/dev/null
61 stty rows 24 &>/dev/null
62 fi
63
64 COLS="`getcols ${COLS}`"
65 COLS=$((${COLS} -7))
66 ENDCOL=$'\e[A\e['${COLS}'G'
67 # Now, ${ENDCOL} will move us to the end of the column;
68 # irregardless of character width
69
70 # Now setup colors for easy reading
71 if [ -n "${EBUILD}" ] && [ "${*/depend}" = "$*" ]
72 then
73 NOCOLOR="`python -c 'import portage; print portage.settings["NOCOLOR"]' 2> /dev/null`"
74 fi
75 if [ -n "${EBUILD}" ] && [ "${*/depend}" = "$*" ] && [ "${NOCOLOR}" = "true" ]
76 then
77 GOOD=""
78 WARN=""
79 BAD=""
80 NORMAL=""
81
82 HILITE=""
83 BRACKET=""
84 else
85 GOOD=$'\e[32;01m'
86 WARN=$'\e[33;01m'
87 BAD=$'\e[31;01m'
88 NORMAL=$'\e[0m'
89
90 HILITE=$'\e[36;01m'
91 BRACKET=$'\e[34;01m'
92 fi
93
94 # void esyslog(char* priority, char* tag, char* message)
95 #
96 # use the system logger to log a message
97 #
98 esyslog() {
99 if [ -x /usr/bin/logger ]
100 then
101 pri="$1"
102 tag="$2"
103 shift 2
104 [ -z "$*" ] && return 0
105 /usr/bin/logger -p "${pri}" -t "${tag}" -- "$*"
106 fi
107 }
108
109 # void einfo(char* message)
110 #
111 # show an informative message (with a newline)
112 #
113 einfo() {
114 if [ "${QUIET_STDOUT}" = "yes" ]
115 then
116 return
117 else
118 echo -e " ${GOOD}*${NORMAL} ${*}"
119 fi
120 }
121
122 # void einfon(char* message)
123 #
124 # show an informative message (without a newline)
125 #
126 einfon() {
127 if [ "${QUIET_STDOUT}" = "yes" ]
128 then
129 return
130 else
131 echo -ne " ${GOOD}*${NORMAL} ${*}"
132 fi
133 }
134
135 # void ewarn(char* message)
136 #
137 # show a warning message + log it
138 #
139 ewarn() {
140 if [ "${QUIET_STDOUT}" = "yes" ]
141 then
142 echo " ${*}"
143 else
144 echo -e " ${WARN}*${NORMAL} ${*}"
145 fi
146
147 # Log warnings to system log
148 esyslog "daemon.warning" "rc-scripts" "${*}"
149 }
150
151 # void eerror(char* message)
152 #
153 # show an error message + log it
154 #
155 eerror() {
156 if [ "${QUIET_STDOUT}" = "yes" ]
157 then
158 echo " ${*}" >/dev/stderr
159 else
160 echo -e " ${BAD}*${NORMAL} ${*}"
161 fi
162
163 # Log errors to system log
164 esyslog "daemon.err" "rc-scripts" "${*}"
165 }
166
167 # void ebegin(char* message)
168 #
169 # show a message indicating the start of a process
170 #
171 ebegin() {
172 if [ "${QUIET_STDOUT}" = "yes" ]
173 then
174 return
175 else
176 echo -e " ${GOOD}*${NORMAL} ${*}..."
177 fi
178 }
179
180 # void eend(int error, char* errstr)
181 #
182 # indicate the completion of process
183 # if error, show errstr via eerror
184 #
185 eend() {
186 if [ "$#" -eq 0 ] || ([ -n "$1" ] && [ "$1" -eq 0 ])
187 then
188 if [ "${QUIET_STDOUT}" != "yes" ]
189 then
190 echo -e "${ENDCOL} ${BRACKET}[ ${GOOD}ok${BRACKET} ]${NORMAL}"
191 fi
192 else
193 local returnme="$1"
194 if [ "$#" -ge 2 ]
195 then
196 shift
197 eerror "${*}"
198 fi
199 if [ "${QUIET_STDOUT}" != "yes" ]
200 then
201 echo -e "${ENDCOL} ${BRACKET}[ ${BAD}!!${BRACKET} ]${NORMAL}"
202 # extra spacing makes it easier to read
203 echo
204 fi
205 return ${returnme}
206 fi
207 }
208
209 # void ewend(int error, char *warnstr)
210 #
211 # indicate the completion of process
212 # if error, show warnstr via ewarn
213 #
214 ewend() {
215 if [ "$#" -eq 0 ] || ([ -n "$1" ] && [ "$1" -eq 0 ])
216 then
217 if [ "${QUIET_STDOUT}" != "yes" ]
218 then
219 echo -e "${ENDCOL} ${BRACKET}[ ${GOOD}ok${BRACKET} ]${NORMAL}"
220 fi
221 else
222 local returnme="$1"
223 if [ "$#" -ge 2 ]
224 then
225 shift
226 ewarn "${*}"
227 fi
228 if [ "${QUIET_STDOUT}" != "yes" ]
229 then
230 echo -e "${ENDCOL} ${BRACKET}[ ${WARN}!!${BRACKET} ]${NORMAL}"
231 # extra spacing makes it easier to read
232 echo
233 fi
234 return "${returnme}"
235 fi
236 }
237
238 # bool wrap_rcscript(full_path_and_name_of_rc-script)
239 #
240 # check to see if a given rc-script has syntax errors
241 # zero == no errors
242 # nonzero == errors
243 #
244 wrap_rcscript() {
245 local retval=1
246
247 ( echo "function test_script() {" ; cat "$1"; echo "}" ) > "${svcdir}/foo.sh"
248
249 if source "${svcdir}/foo.sh" &> /dev/null
250 then
251 test_script &> /dev/null
252 retval=0
253 fi
254 rm -f "${svcdir}/foo.sh"
255 return "${retval}"
256 }
257
258 # int checkserver(void)
259 #
260 # Return 0 (no error) if this script is executed
261 # onto the server, one otherwise.
262 # See the boot section of /sbin/rc for more details.
263 #
264 checkserver() {
265 # Only do check if 'gentoo=adelie' is given as kernel param
266 if get_bootparam "adelie"
267 then
268 [ "`cat ${svcdir}/hostname`" = "(none)" ] || return 1
269 fi
270
271 return 0
272 }
273
274 # void init_node(void)
275 #
276 # Initialize an Adelie node.
277 #
278 init_node() {
279 ebegin "Importing local userspace on node"
280
281 try mount -t tmpfs none "${shmdir}"
282
283 for DIR in /etc /var /root
284 do
285
286 if grep -q -v "^${DIR}[[:space:]]" /etc/exports
287 then
288 mount -o nolock -n server:"${DIR}" "${DIR}"
289 fi
290
291 if [ -e "/etc/conf.d/exclude/${DIR}" ]
292 then
293 find "${DIR}/" -type d | grep -v -f "/etc/conf.d/exclude/${DIR}" \
294 > "${shmdir}/${DIR}.lst"
295 else
296 find "${DIR}/" -type d > "${shmdir}/${DIR}.lst"
297 fi
298
299 for SUBDIR in `cat ${shmdir}/${DIR}.lst`
300 do
301 mkdir -p "${shmdir}/${SUBDIR}"
302 chmod --reference="${SUBDIR}" "${shmdir}/${SUBDIR}"
303 cp -dp "${SUBDIR}"/* "${shmdir}/${SUBDIR}" &> /dev/null
304 done
305
306 if [ -e "/etc/conf.d/exclude/${DIR}" ]
307 then
308 for EMPTYDIR in `cat "/etc/conf.d/exclude/${DIR}"`
309 do
310 mkdir -p "${shmdir}/${EMPTYDIR}"
311 chmod --reference="${SUBDIR}" "${shmdir}/${SUBDIR}"
312 done
313 fi
314
315 umount -n "${DIR}" > /dev/null
316 mount -n -o bind "${shmdir}/${DIR}" "${DIR}"
317 done
318
319 mkdir -p "${shmdir}/tmp"
320 chmod 0777 "${shmdir}/tmp"
321 mount -n -o bind "${shmdir}/tmp" /tmp
322
323 cat /proc/mounts > /etc/mtab
324
325 cp -f /etc/inittab.node /etc/inittab
326 [ -e /etc/fstab.node ] && cp -f /etc/fstab.node /etc/fstab
327 killall -1 init
328
329 eend 0
330 }
331
332 # char *KV_major(string)
333 #
334 # Return the Major version part of given kernel version.
335 #
336 KV_major() {
337 [ -z "$1" ] && return 1
338
339 local KV="`echo $1 | \
340 awk '{ tmp = $0; gsub(/^[0-9\.]*/, "", tmp); sub(tmp, ""); print }'`"
341 echo "${KV}" | awk -- 'BEGIN { FS = "." } { print $1 }'
342 }
343
344 # char *KV_minor(string)
345 #
346 # Return the Minor version part of given kernel version.
347 #
348 KV_minor() {
349 [ -z "$1" ] && return 1
350
351 local KV="`echo $1 | \
352 awk '{ tmp = $0; gsub(/^[0-9\.]*/, "", tmp); sub(tmp, ""); print }'`"
353 echo "${KV}" | awk -- 'BEGIN { FS = "." } { print $2 }'
354 }
355
356 # char *KV_micro(string)
357 #
358 # Return the Micro version part of given kernel version.
359 #
360 KV_micro() {
361 [ -z "$1" ] && return 1
362
363 local KV="`echo $1 | \
364 awk '{ tmp = $0; gsub(/^[0-9\.]*/, "", tmp); sub(tmp, ""); print }'`"
365 echo "${KV}" | awk -- 'BEGIN { FS = "." } { print $3 }'
366 }
367
368 # int KV_to_int(string)
369 #
370 # Convert a string type kernel version (2.4.0) to an int (132096)
371 # for easy compairing or versions ...
372 #
373 KV_to_int() {
374 [ -z "$1" ] && return 1
375
376 local KV_MAJOR="`KV_major "$1"`"
377 local KV_MINOR="`KV_minor "$1"`"
378 local KV_MICRO="`KV_micro "$1"`"
379 local KV_int="$((KV_MAJOR * 65536 + KV_MINOR * 256 + KV_MICRO))"
380
381 # We make version 2.2.0 the minimum version we will handle as
382 # a sanity check ... if its less, we fail ...
383 if [ "${KV_int}" -ge 131584 ]
384 then
385 echo "${KV_int}"
386
387 return 0
388 else
389 return 1
390 fi
391 }
392
393 # int get_KV()
394 #
395 # return the kernel version (major, minor and micro concated) as an integer
396 #
397 get_KV() {
398 local KV="`uname -r 2> /dev/null`"
399
400 echo "`KV_to_int ${KV}`"
401
402 return $?
403 }
404
405 # bool get_bootparam(param)
406 #
407 # return 0 if gentoo=param was passed to the kernel
408 #
409 # EXAMPLE: if get_bootparam "nodevfs" ; then ....
410 #
411 get_bootparam() {
412 local copt=""
413 local parms=""
414 local retval=1
415
416 for copt in `cat /proc/cmdline`
417 do
418 if [ "${copt%=*}" = "gentoo" ]
419 then
420 params="`gawk -v PARAMS="${copt##*=}" '
421 BEGIN {
422 split(PARAMS, nodes, ",")
423 for (x in nodes)
424 print nodes[x]
425 }'`"
426
427 # Parse gentoo option
428 for x in ${params}
429 do
430 if [ "${x}" = "$1" ]
431 then
432 echo YES
433 retval=0
434 fi
435 done
436 fi
437 done
438 return ${retval}
439 }
440
441 # Safer way to list the contents of a directory,
442 # as it do not have the "empty dir bug".
443 #
444 # char *dolisting(param)
445 #
446 # print a list of the directory contents
447 #
448 # NOTE: quote the params if they contain globs.
449 # also, error checking is not that extensive ...
450 #
451 dolisting() {
452 local x=""
453 local y=""
454 local tmpstr=""
455 local mylist=""
456 local mypath="${*}"
457
458 if [ "${mypath%/\*}" != "${mypath}" ]
459 then
460 mypath="${mypath%/\*}"
461 fi
462 for x in ${mypath}
463 do
464 if [ ! -e ${x} ]
465 then
466 continue
467 fi
468 if [ ! -d ${x} ] && ( [ -L ${x} -o -f ${x} ] )
469 then
470 mylist="${mylist} `ls ${x} 2> /dev/null`"
471 else
472 if [ "${x%/}" != "${x}" ]
473 then
474 x="${x%/}"
475 fi
476 cd ${x}
477 tmpstr="`ls`"
478 for y in ${tmpstr}
479 do
480 mylist="${mylist} ${x}/${y}"
481 done
482 fi
483 done
484 echo "${mylist}"
485 }
486
487 # void save_options(char *option, char *optstring)
488 #
489 # save the settings ("optstring") for "option"
490 #
491 save_options() {
492 local myopts="$1"
493 shift
494 if [ ! -d ${svcdir}/options/${myservice} ]
495 then
496 install -d -m0755 ${svcdir}/options/${myservice}
497 fi
498 echo "$*" > ${svcdir}/options/${myservice}/${myopts}
499 }
500
501 # char *get_options(char *option)
502 #
503 # get the "optstring" for "option" that was saved
504 # by calling the save_options function
505 #
506 get_options() {
507 if [ -f ${svcdir}/options/${myservice}/$1 ]
508 then
509 cat ${svcdir}/options/${myservice}/$1
510 fi
511 }
512
513
514 # vim:ts=4

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.20