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

Contents of /trunk/sbin/functions.sh

Parent Directory Parent Directory | Revision Log Revision Log


Revision 345 - (show annotations) (download) (as text)
Sun Apr 6 16:28:27 2003 UTC (16 years ago) by azarah
File MIME type: text/x-sh
File size: 10575 byte(s)
various fixes; moved .c files to src

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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.20