3 # functions This file contains functions to be used by most or all
4 # shell scripts in the /etc/init.d directory.
9 # Make sure umask is sane
12 # Set up a default search path.
13 PATH="/sbin:/usr/sbin:/bin:/usr/bin"
16 if [ $PPID -ne 1 -a -z "$SYSTEMCTL_SKIP_REDIRECT" ] && \
17 ( /bin/mountpoint -q /cgroup/systemd || /bin/mountpoint -q /sys/fs/cgroup/systemd ) ; then
19 /etc/init.d/*|/etc/rc.d/init.d/*)
25 systemctl_redirect () {
32 s=$"Starting $prog (via systemctl): "
35 s=$"Stopping $prog (via systemctl): "
38 s=$"Reloading $prog configuration (via systemctl): "
40 restart|try-restart|condrestart)
41 s=$"Restarting $prog (via systemctl): "
45 action "$s" /bin/systemctl $command "$prog.service"
48 # Get a sane screen width
49 [ -z "${COLUMNS:-}" ] && COLUMNS=80
51 #if [ -z "${CONSOLETYPE:-}" ]; then
52 # if [ -r "/dev/stderr" ]; then
53 # CONSOLETYPE="$(/sbin/consoletype < /dev/stderr)"
55 # CONSOLETYPE="$(/sbin/consoletype)"
59 if [ -z "${NOLOCALE:-}" ] && [ -z "${LANGSH_SOURCED:-}" ] && [ -f /etc/sysconfig/i18n ] ; then
60 . /etc/profile.d/lang.sh 2>/dev/null
61 # avoid propagating LANGSH_SOURCED any further
65 # Read in our configuration
66 if [ -z "${BOOTUP:-}" ]; then
67 if [ -f /etc/sysconfig/init ]; then
70 # This all seem confusing? Look in /etc/sysconfig/init,
71 # or in /usr/doc/initscripts-*/sysconfig.txt
74 MOVE_TO_COL="echo -en \\033[${RES_COL}G"
75 SETCOLOR_SUCCESS="echo -en \\033[1;32m"
76 SETCOLOR_FAILURE="echo -en \\033[1;31m"
77 SETCOLOR_WARNING="echo -en \\033[1;33m"
78 SETCOLOR_NORMAL="echo -en \\033[0;39m"
81 if [ "$CONSOLETYPE" = "serial" ]; then
91 # Interpret escape sequences in an fstab entry
93 fstab-decode echo "$1"
96 # Check if any of $pid (could be plural) are running
101 [ -d "/proc/$i" ] && return 0
107 ls -bl "$@" 2>/dev/null| awk '{ print $NF }'
114 if strstr "$line" "$s"; then
122 # __umount_loop awk_program fstab_file first_msg retry_msg umount_args
123 # awk_program should process fstab_file and return a list of fstab-encoded
124 # paths; it doesn't have to handle comments in fstab_file.
129 remaining=$(LC_ALL=C awk "/^#/ {next} $1" "$2" | sort -r)
130 while [ -n "$remaining" -a "$retry" -gt 0 ]; do
131 if [ "$retry" -eq 3 ]; then
132 action "$3" fstab-decode umount $5 $remaining
134 action "$4" fstab-decode umount $5 $remaining
137 remaining=$(LC_ALL=C awk "/^#/ {next} $1" "$2" | sort -r)
138 while [ "$count" -gt 0 ]; do
139 [ -z "$remaining" ] && break
141 # jordan # usleep 500000
143 remaining=$(LC_ALL=C awk "/^#/ {next} $1" "$2" | sort -r)
145 [ -z "$remaining" ] && break
146 fstab-decode /sbin/fuser -k -m $sig $remaining >/dev/null
153 # Similar to __umount loop above, specialized for loopback devices
154 __umount_loopback_loop() {
155 local remaining devremaining sig=
158 remaining=$(awk '$1 ~ /^\/dev\/loop/ && $2 != "/" {print $2}' /proc/mounts)
159 devremaining=$(awk '$1 ~ /^\/dev\/loop/ && $2 != "/" {print $1}' /proc/mounts)
160 while [ -n "$remaining" -a "$retry" -gt 0 ]; do
161 if [ "$retry" -eq 3 ]; then
162 action $"Unmounting loopback filesystems: " \
163 fstab-decode umount $remaining
165 action $"Unmounting loopback filesystems (retry):" \
166 fstab-decode umount $remaining
168 for dev in $devremaining ; do
169 losetup $dev > /dev/null 2>&1 && \
170 action $"Detaching loopback device $dev: " \
173 remaining=$(awk '$1 ~ /^\/dev\/loop/ && $2 != "/" {print $2}' /proc/mounts)
174 devremaining=$(awk '$1 ~ /^\/dev\/loop/ && $2 != "/" {print $1}' /proc/mounts)
175 [ -z "$remaining" ] && break
176 fstab-decode /sbin/fuser -k -m $sig $remaining >/dev/null
183 # __proc_pids {program} [pidfile]
184 # Set $pid to pids from /var/run* for {program}. $pid should be declared
185 # local in the caller.
186 # Returns LSB exit code for the 'status' action.
189 local pid_file=${2:-/var/run/$base.pid}
192 if [ -f "$pid_file" ] ; then
195 [ ! -r "$pid_file" ] && return 4 # "user had insufficient privilege"
198 [ -z "$line" ] && break
200 [ -z "${p//[0-9]/}" ] && [ -d "/proc/$p" ] && pid="$pid $p"
204 if [ -n "$pid" ]; then
207 return 1 # "Program is dead and /var/run pid file exists"
209 return 3 # "Program is not running"
212 # Output PIDs of matching processes, found using pidof
214 pidof -c -o $$ -o $PPID -o %PPID -x "$1" || \
215 pidof -c -o $$ -o $PPID -o %PPID -x "${1##*/}"
216 # jordan # pidof -c -m -o $$ -o $PPID -o %PPID -x "$1" || \
217 # jordan # pidof -c -m -o $$ -o $PPID -o %PPID -x "${1##*/}"
221 # A function to start a program.
224 local gotbase= force= nicelevel corelimit
225 local pid base= user= nice= bg= pid_file=
228 while [ "$1" != "${1##[-+]}" ]; do
230 '') echo $"$0: Usage: daemon [+/-nicelevel] {program}"
255 pid_file=${1#--pidfile=}
266 *) echo $"$0: Usage: daemon [+/-nicelevel] {program}"
272 [ -z "$gotbase" ] && base=${1##*/}
274 # See if it's already running. Look *only* at the pid file.
275 __pids_var_run "$base" "$pid_file"
277 [ -n "$pid" -a -z "$force" ] && return
279 # make sure it doesn't core dump anywhere unless requested
280 corelimit="ulimit -S -c ${DAEMON_COREFILE_LIMIT:-0}"
282 # if they set NICELEVEL in /etc/sysconfig/foo, honor it
283 [ -n "${NICELEVEL:-}" ] && nice="nice -n $NICELEVEL"
285 # if they set CGROUP_DAEMON in /etc/sysconfig/foo, honor it
286 if [ -n "${CGROUP_DAEMON}" ]; then
287 if [ ! -x /bin/cgexec ]; then
288 echo -n "Cgroups not installed"; warning
291 cgroup="/bin/cgexec";
292 for i in $CGROUP_DAEMON; do
293 cgroup="$cgroup -g $i";
299 [ "${BOOTUP:-}" = "verbose" -a -z "${LSB:-}" ] && echo -n " $base"
302 if [ -z "$user" ]; then
303 $cgroup $nice /bin/bash -c "$corelimit >/dev/null 2>&1 ; $*"
305 $cgroup $nice runuser -s /bin/bash $user -c "$corelimit >/dev/null 2>&1 ; $*"
308 [ "$?" -eq 0 ] && success $"$base startup" || failure $"$base startup"
311 # A function to stop a program.
313 local RC killlevel= base pid pid_file= delay
317 if [ "$#" -eq 0 ]; then
318 echo $"Usage: killproc [-p pidfile] [ -d delay] {program} [-signal]"
321 if [ "$1" = "-p" ]; then
325 if [ "$1" = "-d" ]; then
331 # check for second arg to be kill level
332 [ -n "${2:-}" ] && killlevel=$2
338 __pids_var_run "$1" "$pid_file"
340 if [ -z "$pid" ]; then
341 if [ -z "$pid_file" ]; then
342 pid="$(__pids_pidof "$1")"
344 [ "$RC" = "4" ] && { failure $"$base shutdown" ; return $RC ;}
349 if [ -n "$pid" ] ; then
350 [ "$BOOTUP" = "verbose" -a -z "${LSB:-}" ] && echo -n "$base "
351 if [ -z "$killlevel" ] ; then
352 if checkpid $pid 2>&1; then
353 # TERM first, then KILL if not dead
354 kill -TERM $pid >/dev/null 2>&1
356 # jordan # usleep 100000
357 if checkpid $pid && sleep 1 &&
358 checkpid $pid && sleep $delay &&
360 kill -KILL $pid >/dev/null 2>&1
362 # jordan # usleep 100000
367 [ "$RC" -eq 0 ] && failure $"$base shutdown" || success $"$base shutdown"
369 # use specified level only
371 if checkpid $pid; then
372 kill $killlevel $pid >/dev/null 2>&1
374 [ "$RC" -eq 0 ] && success $"$base $killlevel" || failure $"$base $killlevel"
375 elif [ -n "${LSB:-}" ]; then
376 RC=7 # Program is not running
380 if [ -n "${LSB:-}" -a -n "$killlevel" ]; then
381 RC=7 # Program is not running
383 failure $"$base shutdown"
388 # Remove pid file if any.
389 if [ -z "$killlevel" ]; then
390 rm -f "${pid_file:-/var/run/$base.pid}"
395 # A function to find the pid of a program. Looks *only* at the pidfile
400 if [ "$#" = 0 ] ; then
401 echo $"Usage: pidfileofproc {program}"
406 [ -n "$pid" ] && echo $pid
410 # A function to find the pid of a program.
412 local RC pid pid_file=
415 if [ "$#" = 0 ]; then
416 echo $"Usage: pidofproc [-p pidfile] {program}"
419 if [ "$1" = "-p" ]; then
423 fail_code=3 # "Program is not running"
425 # First try "/var/run/*.pid" files
426 __pids_var_run "$1" "$pid_file"
428 if [ -n "$pid" ]; then
433 [ -n "$pid_file" ] && return $RC
434 __pids_pidof "$1" || return $RC
438 local base pid lock_file= pid_file=
441 if [ "$#" = 0 ] ; then
442 echo $"Usage: status [-p pidfile] {program}"
445 if [ "$1" = "-p" ]; then
449 if [ "$1" = "-l" ]; then
455 if [ "$_use_systemctl" = "1" ]; then
456 systemctl status ${0##*/}.service
461 __pids_var_run "$1" "$pid_file"
463 if [ -z "$pid_file" -a -z "$pid" ]; then
464 pid="$(__pids_pidof "$1")"
466 if [ -n "$pid" ]; then
467 echo $"${base} (pid $pid) is running..."
473 echo $"${base} (pid $pid) is running..."
477 echo $"${base} dead but pid file exists"
481 echo $"${base} status unknown due to insufficient privileges."
485 if [ -z "${lock_file}" ]; then
488 # See if /var/lock/subsys/${lock_file} exists
489 if [ -f /var/lock/subsys/${lock_file} ]; then
490 echo $"${base} dead but subsys locked"
493 echo $"${base} is stopped"
498 [ "$BOOTUP" = "color" ] && $MOVE_TO_COL
500 [ "$BOOTUP" = "color" ] && $SETCOLOR_SUCCESS
502 [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
509 [ "$BOOTUP" = "color" ] && $MOVE_TO_COL
511 [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
513 [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
520 [ "$BOOTUP" = "color" ] && $MOVE_TO_COL
522 [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
524 [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
531 [ "$BOOTUP" = "color" ] && $MOVE_TO_COL
533 [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
535 [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
541 # Inform the graphical boot of our current state
542 update_boot_stage() {
543 if [ -x /usr/bin/plymouth ]; then
544 /usr/bin/plymouth --update="$1"
549 # Log that something succeeded
551 [ "$BOOTUP" != "verbose" -a -z "${LSB:-}" ] && echo_success
555 # Log that something failed
558 [ "$BOOTUP" != "verbose" -a -z "${LSB:-}" ] && echo_failure
559 [ -x /usr/bin/plymouth ] && /usr/bin/plymouth --details
563 # Log that something passed, but may have had errors. Useful for fsck
566 [ "$BOOTUP" != "verbose" -a -z "${LSB:-}" ] && echo_passed
573 [ "$BOOTUP" != "verbose" -a -z "${LSB:-}" ] && echo_warning
577 # Run some action. Log its output.
584 "$@" && success $"$STRING" || failure $"$STRING"
590 # returns OK if $1 contains $2
592 [ "${1#*$2*}" = "$1" ] && return 1
596 # Confirm whether we really want to run this service
598 [ -x /usr/bin/plymouth ] && /usr/bin/plymouth --hide-splash
600 echo -n $"Start service $1 (Y)es/(N)o/(C)ontinue? [Y] "
602 if strstr $"yY" "$answer" || [ "$answer" = "" ] ; then
604 elif strstr $"cC" "$answer" ; then
605 rm -f /var/run/confirm
606 [ -x /usr/bin/plymouth ] && /usr/bin/plymouth --show-splash
608 elif strstr $"nN" "$answer" ; then
614 # resolve a device node to its major:minor numbers in decimal or hex
618 if [ "$1" = "hex" ]; then
621 ls -lH "$2" | awk '{ sub(/,/, "", $5); printf("'"$fmt"'", $5, $6); }'
625 # Check whether file $1 is a backup or rpm-generated file and should be ignored
628 *~ | *.bak | *.orig | *.rpmnew | *.rpmorig | *.rpmsave)
635 # Evaluate shvar-style booleans
638 [tT] | [yY] | [yY][eE][sS] | [tT][rR][uU][eE])
645 # Evaluate shvar-style booleans
648 [fF] | [nN] | [nN][oO] | [fF][aA][lL][sS][eE])
656 [ "$1" = "/dev/urandom" -o "$1" = "/dev/hw_random" \
657 -o "$1" = "/dev/random" ]
660 find_crypto_mount_point() {
661 local fs_spec fs_file fs_vfstype remaining_fields
663 while read fs_spec fs_file remaining_fields; do
664 if [ "$fs_spec" = "/dev/mapper/$1" ]; then
671 # Because of a chicken/egg problem, init_crypto must be run twice. /var may be
672 # encrypted but /var/lib/random-seed is needed to initialize swap.
674 local have_random dst src key opt mode owner params makeswap skip arg opt
675 local param value rc ret mke2fs mdir prompt mount_point
679 while read dst src key opt; do
680 [ -z "$dst" -o "${dst#\#}" != "$dst" ] && continue
681 [ -b "/dev/mapper/$dst" ] && continue;
682 if [ "$have_random" = 0 ] && key_is_random "$key"; then
685 if [ -n "$key" -a "x$key" != "xnone" ]; then
686 if test -e "$key" ; then
687 owner=$(ls -l $key | (read a b owner rest; echo $owner))
688 if ! key_is_random "$key"; then
689 mode=$(ls -l "$key" | cut -c 5-10)
690 if [ "$mode" != "------" ]; then
691 echo $"INSECURE MODE FOR $key"
694 if [ "$owner" != root ]; then
695 echo $"INSECURE OWNER FOR $key"
698 echo $"Key file for $dst not found, skipping"
709 # Parse the src field for UUID= and convert to real device names
710 if [ "${src%%=*}" == "UUID" ]; then
711 src=$(/sbin/blkid -t "$src" -l -o device)
712 elif [ "${src/^\/dev\/disk\/by-uuid\/}" != "$src" ]; then
713 src=$(__readlink $src)
715 # Is it a block device?
716 [ -b "$src" ] || continue
717 # Is it already a device mapper slave? (this is gross)
719 devesc=${devesc//\//!}
720 for d in /sys/block/dm-*/slaves ; do
721 [ -e $d/$devesc ] && continue 2
723 # Parse the options field, convert to cryptsetup parameters and
724 # contruct the command line
725 while [ -n "$opt" ]; do
730 value=${arg##$param=}
734 params="$params -c $value"
735 if [ -z "$value" ]; then
736 echo $"$dst: no value for cipher option, skipping"
741 params="$params -s $value"
742 if [ -z "$value" ]; then
743 echo $"$dst: no value for size option, skipping"
748 params="$params -h $value"
749 if [ -z "$value" ]; then
750 echo $"$dst: no value for hash option, skipping"
764 if [ "$skip" = "yes" ]; then
768 if [ -z "$makeswap" ] && cryptsetup isLuks "$src" 2>/dev/null ; then
769 if key_is_random "$key"; then
770 echo $"$dst: LUKS requires non-random key, skipping"
774 if [ -n "$params" ]; then
775 echo "$dst: options are invalid for LUKS partitions," \
778 if [ -n "$key" ]; then
779 /sbin/cryptsetup -d $key luksOpen "$src" "$dst" <&1 2>/dev/null && success || failure
782 mount_point="$(find_crypto_mount_point $dst)"
783 [ -n "$mount_point" ] || mount_point=${src##*/}
784 prompt=$(printf $"%s is password protected" "$mount_point")
785 plymouth ask-for-password --prompt "$prompt" --command="/sbin/cryptsetup luksOpen -T1 $src $dst" <&1
789 [ -z "$key" ] && plymouth --hide-splash
790 /sbin/cryptsetup $params ${key:+-d $key} create "$dst" "$src" <&1 2>/dev/null && success || failure
792 [ -z "$key" ] && plymouth --show-splash
794 if [ $rc -ne 0 ]; then
798 if [ -b "/dev/mapper/$dst" ]; then
799 if [ "$makeswap" = "yes" ]; then
800 mkswap "/dev/mapper/$dst" 2>/dev/null >/dev/null
802 if [ "$mke2fs" = "yes" ]; then
803 if mke2fs "/dev/mapper/$dst" 2>/dev/null >/dev/null \
804 && mdir=$(mktemp -d /tmp/mountXXXXXX); then
805 mount "/dev/mapper/$dst" "$mdir" && chmod 1777 "$mdir"
815 # A sed expression to filter out the files that is_ignored_file recognizes
816 __sed_discard_ignored_files='/\(~\|\.bak\|\.orig\|\.rpmnew\|\.rpmorig\|\.rpmsave\)$/d'
818 if [ "$_use_systemctl" = "1" ]; then
819 if [ "x$1" = xstart -o \
821 "x$1" = xrestart -o \
823 "x$1" = xtry-restart -o \
824 "x$1" = xforce-reload -o \
825 "x$1" = xcondrestart ] ; then
827 systemctl_redirect $0 $1