Advertisement
Guest User

/etc/rc.d/init.d/functions

a guest
Jan 10th, 2012
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.15 KB | None | 0 0
  1. # -*-Shell-script-*-
  2. #
  3. # functions This file contains functions to be used by most or all
  4. # shell scripts in the /etc/init.d directory.
  5. #
  6.  
  7. TEXTDOMAIN=initscripts
  8.  
  9. # Make sure umask is sane
  10. umask 022
  11.  
  12. # Set up a default search path.
  13. PATH="/sbin:/usr/sbin:/bin:/usr/bin"
  14. export PATH
  15.  
  16. # Get a sane screen width
  17. [ -z "${COLUMNS:-}" ] && COLUMNS=80
  18.  
  19. [ -z "${CONSOLETYPE:-}" ] && CONSOLETYPE="`/sbin/consoletype`"
  20.  
  21. if [ -f /etc/sysconfig/i18n -a -z "${NOLOCALE:-}" ] ; then
  22. . /etc/profile.d/lang.sh
  23. fi
  24.  
  25. # Read in our configuration
  26. if [ -z "${BOOTUP:-}" ]; then
  27. if [ -f /etc/sysconfig/init ]; then
  28. . /etc/sysconfig/init
  29. else
  30. # This all seem confusing? Look in /etc/sysconfig/init,
  31. # or in /usr/doc/initscripts-*/sysconfig.txt
  32. BOOTUP=color
  33. RES_COL=60
  34. MOVE_TO_COL="echo -en \\033[${RES_COL}G"
  35. SETCOLOR_SUCCESS="echo -en \\033[1;32m"
  36. SETCOLOR_FAILURE="echo -en \\033[1;31m"
  37. SETCOLOR_WARNING="echo -en \\033[1;33m"
  38. SETCOLOR_NORMAL="echo -en \\033[0;39m"
  39. LOGLEVEL=1
  40. fi
  41. if [ "$CONSOLETYPE" = "serial" ]; then
  42. BOOTUP=serial
  43. MOVE_TO_COL=
  44. SETCOLOR_SUCCESS=
  45. SETCOLOR_FAILURE=
  46. SETCOLOR_WARNING=
  47. SETCOLOR_NORMAL=
  48. fi
  49. fi
  50.  
  51. if [ "${BOOTUP:-}" != "verbose" ]; then
  52. INITLOG_ARGS="-q"
  53. else
  54. INITLOG_ARGS=
  55. fi
  56.  
  57. # Interpret escape sequences in an fstab entry
  58. fstab_decode_str() {
  59. fstab-decode echo "$1"
  60. }
  61.  
  62. # Check if $pid (could be plural) are running
  63. checkpid() {
  64. local i
  65.  
  66. for i in $* ; do
  67. [ -d "/proc/$i" ] && return 0
  68. done
  69. return 1
  70. }
  71.  
  72. # __umount_loop awk_program fstab_file first_msg retry_msg umount_args
  73. # awk_program should process fstab_file and return a list of fstab-encoded
  74. # paths; it doesn't have to handle comments in fstab_file.
  75. __umount_loop() {
  76. local remaining sig=
  77. local retry=3
  78.  
  79. remaining=$(LC_ALL=C awk "/^#/ {next} $1" "$2" | sort -r)
  80. while [ -n "$remaining" -a "$retry" -gt 0 ]; do
  81. if [ "$retry" -eq 3 ]; then
  82. action "$3" fstab-decode umount $5 $remaining
  83. else
  84. action "$4" fstab-decode umount $5 $remaining
  85. fi
  86. sleep 2
  87. remaining=$(LC_ALL=C awk "/^#/ {next} $1" "$2" | sort -r)
  88. [ -z "$remaining" ] && break
  89. fstab-decode /sbin/fuser -k -m $sig $remaining >/dev/null
  90. sleep 5
  91. retry=$(($retry -1))
  92. sig=-9
  93. done
  94. }
  95.  
  96. # Similar to __umount loop above, specialized for loopback devices
  97. __umount_loopback_loop() {
  98. local remaining devremaining sig=
  99. local retry=3
  100.  
  101. remaining=$(awk '$1 ~ /^\/dev\/loop/ && $2 != "/" {print $2}' /proc/mounts)
  102. devremaining=$(awk '$1 ~ /^\/dev\/loop/ && $2 != "/" {print $1}' /proc/mounts)
  103. while [ -n "$remaining" -a "$retry" -gt 0 ]; do
  104. if [ "$retry" -eq 3 ]; then
  105. action $"Unmounting loopback filesystems: " \
  106. fstab-decode umount $remaining
  107. else
  108. action $"Unmounting loopback filesystems (retry):" \
  109. fstab-decode umount $remaining
  110. fi
  111. for dev in $devremaining ; do
  112. losetup $dev > /dev/null 2>&1 && \
  113. action $"Detaching loopback device $dev: " \
  114. losetup -d $dev
  115. done
  116. remaining=$(awk '$1 ~ /^\/dev\/loop/ && $2 != "/" {print $2}' /proc/mounts)
  117. devremaining=$(awk '$1 ~ /^\/dev\/loop/ && $2 != "/" {print $1}' /proc/mounts)
  118. [ -z "$remaining" ] && break
  119. fstab-decode /sbin/fuser -k -m $sig $remaining >/dev/null
  120. sleep 5
  121. retry=$(($retry -1))
  122. sig=-9
  123. done
  124. }
  125.  
  126. # __proc_pids {program} [pidfile]
  127. # Set $pid to pids from /var/run* for {program}. $pid should be declared
  128. # local in the caller.
  129. # Returns LSB exit code for the 'status' action.
  130. __pids_var_run() {
  131. local base=${1##*/}
  132. local pid_file=${2:-/var/run/$base.pid}
  133.  
  134. pid=
  135. if [ -f "$pid_file" ] ; then
  136. local line p
  137. read line < "$pid_file"
  138. for p in $line ; do
  139. [ -z "${p//[0-9]/}" -a -d "/proc/$p" ] && pid="$pid $p"
  140. done
  141. if [ -n "$pid" ]; then
  142. return 0
  143. fi
  144. return 1 # "Program is dead and /var/run pid file exists"
  145. fi
  146. return 3 # "Program is not running"
  147. }
  148.  
  149. # Output PIDs of matching processes, found using pidof
  150. __pids_pidof() {
  151. pidof -c -o $$ -o $PPID -o %PPID -x "$1" || \
  152. pidof -c -o $$ -o $PPID -o %PPID -x "${1##*/}"
  153. }
  154.  
  155.  
  156. # A function to start a program.
  157. daemon() {
  158. # Test syntax.
  159. local gotbase= force= nicelevel corelimit
  160. local pid base= user= nice= bg= pid_file=
  161. nicelevel=0
  162. while [ "$1" != "${1##[-+]}" ]; do
  163. case $1 in
  164. '') echo $"$0: Usage: daemon [+/-nicelevel] {program}"
  165. return 1;;
  166. --check)
  167. base=$2
  168. gotbase="yes"
  169. shift 2
  170. ;;
  171. --check=?*)
  172. base=${1#--check=}
  173. gotbase="yes"
  174. shift
  175. ;;
  176. --user)
  177. user=$2
  178. shift 2
  179. ;;
  180. --user=?*)
  181. user=${1#--user=}
  182. shift
  183. ;;
  184. --pidfile)
  185. pid_file=$2
  186. shift 2
  187. ;;
  188. --pidfile=?*)
  189. pid_file=${1#--pidfile=}
  190. shift
  191. ;;
  192. --force)
  193. force="force"
  194. shift
  195. ;;
  196. [-+][0-9]*)
  197. nice="nice -n $1"
  198. shift
  199. ;;
  200. *) echo $"$0: Usage: daemon [+/-nicelevel] {program}"
  201. return 1;;
  202. esac
  203. done
  204.  
  205. # Save basename.
  206. [ -z "$gotbase" ] && base=${1##*/}
  207.  
  208. # See if it's already running. Look *only* at the pid file.
  209. __pids_var_run "$base" "$pid_file"
  210.  
  211. [ -n "$pid" -a -z "$force" ] && return
  212.  
  213. # make sure it doesn't core dump anywhere unless requested
  214. corelimit="ulimit -S -c ${DAEMON_COREFILE_LIMIT:-0}"
  215.  
  216. # if they set NICELEVEL in /etc/sysconfig/foo, honor it
  217. [ -n "${NICELEVEL:-}" ] && nice="nice -n $NICELEVEL"
  218.  
  219. # Echo daemon
  220. [ "${BOOTUP:-}" = "verbose" -a -z "${LSB:-}" ] && echo -n " $base"
  221.  
  222. # And start it up.
  223. if [ -z "$user" ]; then
  224. $nice /bin/bash -c "$corelimit >/dev/null 2>&1 ; $*"
  225. else
  226. $nice runuser -s /bin/bash - $user -c "$corelimit >/dev/null 2>&1 ; $*"
  227. fi
  228. [ "$?" -eq 0 ] && success $"$base startup" || failure $"$base startup"
  229. }
  230.  
  231. # A function to stop a program.
  232. killproc() {
  233. local RC killlevel= base pid pid_file= delay
  234.  
  235. RC=0; delay=3
  236. # Test syntax.
  237. if [ "$#" -eq 0 ]; then
  238. echo $"Usage: killproc [-p pidfile] [ -d delay] {program} [-signal]"
  239. return 1
  240. fi
  241. if [ "$1" = "-p" ]; then
  242. pid_file=$2
  243. shift 2
  244. fi
  245. if [ "$1" = "-d" ]; then
  246. delay=$2
  247. shift 2
  248. fi
  249.  
  250.  
  251. # check for second arg to be kill level
  252. [ -n "${2:-}" ] && killlevel=$2
  253.  
  254. # Save basename.
  255. base=${1##*/}
  256.  
  257. # Find pid.
  258. __pids_var_run "$1" "$pid_file"
  259. if [ -z "$pid_file" -a -z "$pid" ]; then
  260. pid="$(__pids_pidof "$1")"
  261. fi
  262.  
  263. # Kill it.
  264. if [ -n "$pid" ] ; then
  265. [ "$BOOTUP" = "verbose" -a -z "${LSB:-}" ] && echo -n "$base "
  266. if [ -z "$killlevel" ] ; then
  267. if checkpid $pid 2>&1; then
  268. # TERM first, then KILL if not dead
  269. kill -TERM $pid >/dev/null 2>&1
  270. usleep 100000
  271. if checkpid $pid && sleep 1 &&
  272. checkpid $pid && sleep $delay &&
  273. checkpid $pid ; then
  274. kill -KILL $pid >/dev/null 2>&1
  275. usleep 100000
  276. fi
  277. fi
  278. checkpid $pid
  279. RC=$?
  280. [ "$RC" -eq 0 ] && failure $"$base shutdown" || success $"$base shutdown"
  281. RC=$((! $RC))
  282. # use specified level only
  283. else
  284. if checkpid $pid; then
  285. kill $killlevel $pid >/dev/null 2>&1
  286. RC=$?
  287. [ "$RC" -eq 0 ] && success $"$base $killlevel" || failure $"$base $killlevel"
  288. elif [ -n "${LSB:-}" ]; then
  289. RC=7 # Program is not running
  290. fi
  291. fi
  292. else
  293. if [ -n "${LSB:-}" -a -n "$killlevel" ]; then
  294. RC=7 # Program is not running
  295. else
  296. failure $"$base shutdown"
  297. RC=0
  298. fi
  299. fi
  300.  
  301. # Remove pid file if any.
  302. if [ -z "$killlevel" ]; then
  303. rm -f "${pid_file:-/var/run/$base.pid}"
  304. fi
  305. return $RC
  306. }
  307.  
  308. # A function to find the pid of a program. Looks *only* at the pidfile
  309. pidfileofproc() {
  310. local pid
  311.  
  312. # Test syntax.
  313. if [ "$#" = 0 ] ; then
  314. echo $"Usage: pidfileofproc {program}"
  315. return 1
  316. fi
  317.  
  318. __pids_var_run "$1"
  319. [ -n "$pid" ] && echo $pid
  320. return 0
  321. }
  322.  
  323. # A function to find the pid of a program.
  324. pidofproc() {
  325. local RC pid pid_file=
  326.  
  327. # Test syntax.
  328. if [ "$#" = 0 ]; then
  329. echo $"Usage: pidofproc [-p pidfile] {program}"
  330. return 1
  331. fi
  332. if [ "$1" = "-p" ]; then
  333. pid_file=$2
  334. shift 2
  335. fi
  336. fail_code=3 # "Program is not running"
  337.  
  338. # First try "/var/run/*.pid" files
  339. __pids_var_run "$1" "$pid_file"
  340. RC=$?
  341. if [ -n "$pid" ]; then
  342. echo $pid
  343. return 0
  344. fi
  345.  
  346. [ -n "$pid_file" ] && return $RC
  347. __pids_pidof "$1" || return $RC
  348. }
  349.  
  350. status() {
  351. local base pid pid_file=
  352.  
  353. # Test syntax.
  354. if [ "$#" = 0 ] ; then
  355. echo $"Usage: status [-p pidfile] {program}"
  356. return 1
  357. fi
  358. if [ "$1" = "-p" ]; then
  359. pid_file=$2
  360. shift 2
  361. fi
  362. base=${1##*/}
  363.  
  364. # First try "pidof"
  365. pid="$(__pids_pidof "$1")"
  366. if [ -n "$pid" ]; then
  367. echo $"${base} (pid $pid) is running..."
  368. return 0
  369. fi
  370.  
  371. # Next try "/var/run/*.pid" files
  372. __pids_var_run "$1" "$pid_file"
  373. case "$?" in
  374. 0)
  375. echo $"${base} (pid $pid) is running..."
  376. return 0
  377. ;;
  378. 1)
  379. echo $"${base} dead but pid file exists"
  380. return 1
  381. ;;
  382. esac
  383. # See if /var/lock/subsys/${base} exists
  384. if [ -f /var/lock/subsys/${base} ]; then
  385. echo $"${base} dead but subsys locked"
  386. return 2
  387. fi
  388. echo $"${base} is stopped"
  389. return 3
  390. }
  391.  
  392. echo_success() {
  393. [ "$BOOTUP" = "color" ] && $MOVE_TO_COL
  394. echo -n "["
  395. [ "$BOOTUP" = "color" ] && $SETCOLOR_SUCCESS
  396. echo -n $" OK "
  397. [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  398. echo -n "]"
  399. echo -ne "\r"
  400. return 0
  401. }
  402.  
  403. echo_failure() {
  404. [ "$BOOTUP" = "color" ] && $MOVE_TO_COL
  405. echo -n "["
  406. [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
  407. echo -n $"FAILED"
  408. [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  409. echo -n "]"
  410. echo -ne "\r"
  411. return 1
  412. }
  413.  
  414. echo_passed() {
  415. [ "$BOOTUP" = "color" ] && $MOVE_TO_COL
  416. echo -n "["
  417. [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
  418. echo -n $"PASSED"
  419. [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  420. echo -n "]"
  421. echo -ne "\r"
  422. return 1
  423. }
  424.  
  425. echo_warning() {
  426. [ "$BOOTUP" = "color" ] && $MOVE_TO_COL
  427. echo -n "["
  428. [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
  429. echo -n $"WARNING"
  430. [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  431. echo -n "]"
  432. echo -ne "\r"
  433. return 1
  434. }
  435.  
  436. # Inform the graphical boot of our current state
  437. update_boot_stage() {
  438. if [ "$GRAPHICAL" = "yes" -a -x /usr/bin/rhgb-client ]; then
  439. /usr/bin/rhgb-client --update="$1"
  440. fi
  441. return 0
  442. }
  443.  
  444. # Log that something succeeded
  445. success() {
  446. #if [ -z "${IN_INITLOG:-}" ]; then
  447. # initlog $INITLOG_ARGS -n $0 -s "$1" -e 1
  448. #fi
  449. [ "$BOOTUP" != "verbose" -a -z "${LSB:-}" ] && echo_success
  450. return 0
  451. }
  452.  
  453. # Log that something failed
  454. failure() {
  455. local rc=$?
  456. #if [ -z "${IN_INITLOG:-}" ]; then
  457. # initlog $INITLOG_ARGS -n $0 -s "$1" -e 2
  458. #fi
  459. [ "$BOOTUP" != "verbose" -a -z "${LSB:-}" ] && echo_failure
  460. [ -x /usr/bin/rhgb-client ] && /usr/bin/rhgb-client --details=yes
  461. return $rc
  462. }
  463.  
  464. # Log that something passed, but may have had errors. Useful for fsck
  465. passed() {
  466. local rc=$?
  467. #if [ -z "${IN_INITLOG:-}" ]; then
  468. # initlog $INITLOG_ARGS -n $0 -s "$1" -e 1
  469. #fi
  470. [ "$BOOTUP" != "verbose" -a -z "${LSB:-}" ] && echo_passed
  471. return $rc
  472. }
  473.  
  474. # Log a warning
  475. warning() {
  476. local rc=$?
  477. #if [ -z "${IN_INITLOG:-}" ]; then
  478. # initlog $INITLOG_ARGS -n $0 -s "$1" -e 1
  479. #fi
  480. [ "$BOOTUP" != "verbose" -a -z "${LSB:-}" ] && echo_warning
  481. return $rc
  482. }
  483.  
  484. # Run some action. Log its output.
  485. action() {
  486. local STRING rc
  487.  
  488. STRING=$1
  489. echo -n "$STRING "
  490. if [ "${RHGB_STARTED:-}" != "" -a -w /etc/rhgb/temp/rhgb-console ]; then
  491. echo -n "$STRING " > /etc/rhgb/temp/rhgb-console
  492. fi
  493. shift
  494. "$@" && success $"$STRING" || failure $"$STRING"
  495. rc=$?
  496. echo
  497. if [ "${RHGB_STARTED:-}" != "" -a -w /etc/rhgb/temp/rhgb-console ]; then
  498. if [ "$rc" = "0" ]; then
  499. echo_success > /etc/rhgb/temp/rhgb-console
  500. else
  501. echo_failure > /etc/rhgb/temp/rhgb-console
  502. [ -x /usr/bin/rhgb-client ] && /usr/bin/rhgb-client --details=yes
  503. fi
  504. echo > /etc/rhgb/temp/rhgb-console
  505. fi
  506. return $rc
  507. }
  508.  
  509. # returns OK if $1 contains $2
  510. strstr() {
  511. [ "${1#*$2*}" = "$1" ] && return 1
  512. return 0
  513. }
  514.  
  515. # Confirm whether we really want to run this service
  516. confirm() {
  517. [ -x /usr/bin/rhgb-client ] && /usr/bin/rhgb-client --details=yes
  518. while : ; do
  519. echo -n $"Start service $1 (Y)es/(N)o/(C)ontinue? [Y] "
  520. read answer
  521. if strstr $"yY" "$answer" || [ "$answer" = "" ] ; then
  522. return 0
  523. elif strstr $"cC" "$answer" ; then
  524. rm -f /var/run/confirm
  525. [ -x /usr/bin/rhgb-client ] && /usr/bin/rhgb-client --details=no
  526. return 2
  527. elif strstr $"nN" "$answer" ; then
  528. return 1
  529. fi
  530. done
  531. }
  532.  
  533. # resolve a device node to its major:minor numbers in decimal or hex
  534. get_numeric_dev() {
  535. (
  536. fmt="%d:%d"
  537. if [ "$1" == "hex" ]; then
  538. fmt="%x:%x"
  539. fi
  540. ls -lH "$2" | awk '{ sub(/,/, "", $5); printf("'"$fmt"'", $5, $6); }'
  541. ) 2>/dev/null
  542. }
  543.  
  544. # find the working name for a running dm device with the same table as one
  545. # that dmraid would create
  546. resolve_dm_name() {
  547. (
  548. name="$1"
  549.  
  550. line=$(/sbin/dmraid -ay -t --ignorelocking | \
  551. egrep -iv "no block devices found|No RAID disks" | \
  552. awk -F ':' "{ if (\$1 ~ /^$name$/) { print \$2; }}")
  553. for x in $line ; do
  554. if [[ "$x" =~ "^/dev/" ]] ; then
  555. majmin=$(get_numeric_dev dec $x)
  556. line=$(echo "$line" | sed -e "s,$x\( \|$\),$majmin\1,g")
  557. fi
  558. done
  559. line=$(echo "$line" | sed -e 's/^[ \t]*//' -e 's/[ \t]*$//' \
  560. -e 's/ core [12] [[:digit:]]\+ / core [12] [[:digit:]]\\+ /')
  561. /sbin/dmsetup table | \
  562. sed -n -e "s/.*\(no block devices found\|No devices found\).*//" \
  563. -e "s/\(^[^:]\+\): $line\( \+$\|$\)/\1/p"
  564. ) 2>/dev/null
  565. }
  566.  
  567. # Check whether file $1 is a backup or rpm-generated file and should be ignored
  568. is_ignored_file() {
  569. case "$1" in
  570. *~ | *.bak | *.orig | *.rpmnew | *.rpmorig | *.rpmsave)
  571. return 0
  572. ;;
  573. esac
  574. return 1
  575. }
  576. # A sed expression to filter out the files that is_ignored_file recognizes
  577. __sed_discard_ignored_files='/\(~\|\.bak\|\.orig\|\.rpmnew\|\.rpmorig\|\.rpmsave\)$/d'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement