Advertisement
Guest User

Untitled

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