4 # this file was put together by Jordan to provide the same interface as
5 # /etc/init.d/functions on fedora systems
6 # (probably is extracted from one of the fedora releases as is, not sure about that)
8 # we unconditionnally ship this as /etc/init.d/functions.sfa,
9 # and then our own initscript (init.d/sfa) does source that
10 # conditionnally, i.e. when run on debian systems
13 # functions This file contains functions to be used by most or all
14 # shell scripts in the /etc/init.d directory.
17 TEXTDOMAIN=initscripts
19 # Make sure umask is sane
22 # Set up a default search path.
23 PATH="/sbin:/usr/sbin:/bin:/usr/bin"
26 if [ $PPID -ne 1 -a -z "$SYSTEMCTL_SKIP_REDIRECT" ] && \
27 ( /bin/mountpoint -q /cgroup/systemd || /bin/mountpoint -q /sys/fs/cgroup/systemd ) ; then
29 /etc/init.d/*|/etc/rc.d/init.d/*)
35 systemctl_redirect () {
42 s=$"Starting $prog (via systemctl): "
45 s=$"Stopping $prog (via systemctl): "
48 s=$"Reloading $prog configuration (via systemctl): "
50 restart|try-restart|condrestart)
51 s=$"Restarting $prog (via systemctl): "
55 action "$s" /bin/systemctl $command "$prog.service"
58 # Get a sane screen width
59 [ -z "${COLUMNS:-}" ] && COLUMNS=80
61 #if [ -z "${CONSOLETYPE:-}" ]; then
62 # if [ -r "/dev/stderr" ]; then
63 # CONSOLETYPE="$(/sbin/consoletype < /dev/stderr)"
65 # CONSOLETYPE="$(/sbin/consoletype)"
69 if [ -z "${NOLOCALE:-}" ] && [ -z "${LANGSH_SOURCED:-}" ] && [ -f /etc/sysconfig/i18n ] ; then
70 . /etc/profile.d/lang.sh 2>/dev/null
71 # avoid propagating LANGSH_SOURCED any further
75 # Read in our configuration
76 if [ -z "${BOOTUP:-}" ]; then
77 if [ -f /etc/sysconfig/init ]; then
80 # This all seem confusing? Look in /etc/sysconfig/init,
81 # or in /usr/doc/initscripts-*/sysconfig.txt
84 MOVE_TO_COL="echo -en \\033[${RES_COL}G"
85 SETCOLOR_SUCCESS="echo -en \\033[1;32m"
86 SETCOLOR_FAILURE="echo -en \\033[1;31m"
87 SETCOLOR_WARNING="echo -en \\033[1;33m"
88 SETCOLOR_NORMAL="echo -en \\033[0;39m"
91 if [ "$CONSOLETYPE" = "serial" ]; then
101 # Interpret escape sequences in an fstab entry
103 fstab-decode echo "$1"
106 # Check if any of $pid (could be plural) are running
111 [ -d "/proc/$i" ] && return 0
117 ls -bl "$@" 2>/dev/null| awk '{ print $NF }'
124 if strstr "$line" "$s"; then
132 # __umount_loop awk_program fstab_file first_msg retry_msg umount_args
133 # awk_program should process fstab_file and return a list of fstab-encoded
134 # paths; it doesn't have to handle comments in fstab_file.
139 remaining=$(LC_ALL=C awk "/^#/ {next} $1" "$2" | sort -r)
140 while [ -n "$remaining" -a "$retry" -gt 0 ]; do
141 if [ "$retry" -eq 3 ]; then
142 action "$3" fstab-decode umount $5 $remaining
144 action "$4" fstab-decode umount $5 $remaining
147 remaining=$(LC_ALL=C awk "/^#/ {next} $1" "$2" | sort -r)
148 while [ "$count" -gt 0 ]; do
149 [ -z "$remaining" ] && break
151 # jordan # usleep 500000
153 remaining=$(LC_ALL=C awk "/^#/ {next} $1" "$2" | sort -r)
155 [ -z "$remaining" ] && break
156 fstab-decode /sbin/fuser -k -m $sig $remaining >/dev/null
163 # Similar to __umount loop above, specialized for loopback devices
164 __umount_loopback_loop() {
165 local remaining devremaining sig=
168 remaining=$(awk '$1 ~ /^\/dev\/loop/ && $2 != "/" {print $2}' /proc/mounts)
169 devremaining=$(awk '$1 ~ /^\/dev\/loop/ && $2 != "/" {print $1}' /proc/mounts)
170 while [ -n "$remaining" -a "$retry" -gt 0 ]; do
171 if [ "$retry" -eq 3 ]; then
172 action $"Unmounting loopback filesystems: " \
173 fstab-decode umount $remaining
175 action $"Unmounting loopback filesystems (retry):" \
176 fstab-decode umount $remaining
178 for dev in $devremaining ; do
179 losetup $dev > /dev/null 2>&1 && \
180 action $"Detaching loopback device $dev: " \
183 remaining=$(awk '$1 ~ /^\/dev\/loop/ && $2 != "/" {print $2}' /proc/mounts)
184 devremaining=$(awk '$1 ~ /^\/dev\/loop/ && $2 != "/" {print $1}' /proc/mounts)
185 [ -z "$remaining" ] && break
186 fstab-decode /sbin/fuser -k -m $sig $remaining >/dev/null
193 # __proc_pids {program} [pidfile]
194 # Set $pid to pids from /var/run* for {program}. $pid should be declared
195 # local in the caller.
196 # Returns LSB exit code for the 'status' action.
199 local pid_file=${2:-/var/run/$base.pid}
202 if [ -f "$pid_file" ] ; then
205 [ ! -r "$pid_file" ] && return 4 # "user had insufficient privilege"
208 [ -z "$line" ] && break
210 [ -z "${p//[0-9]/}" ] && [ -d "/proc/$p" ] && pid="$pid $p"
214 if [ -n "$pid" ]; then
217 return 1 # "Program is dead and /var/run pid file exists"
219 return 3 # "Program is not running"
222 # Output PIDs of matching processes, found using pidof
224 pidof -c -o $$ -o $PPID -o %PPID -x "$1" || \
225 pidof -c -o $$ -o $PPID -o %PPID -x "${1##*/}"
226 # jordan # pidof -c -m -o $$ -o $PPID -o %PPID -x "$1" || \
227 # jordan # pidof -c -m -o $$ -o $PPID -o %PPID -x "${1##*/}"
231 # A function to start a program.
234 local gotbase= force= nicelevel corelimit
235 local pid base= user= nice= bg= pid_file=
238 while [ "$1" != "${1##[-+]}" ]; do
240 '') echo $"$0: Usage: daemon [+/-nicelevel] {program}"
265 pid_file=${1#--pidfile=}
276 *) echo $"$0: Usage: daemon [+/-nicelevel] {program}"
282 [ -z "$gotbase" ] && base=${1##*/}
284 # See if it's already running. Look *only* at the pid file.
285 __pids_var_run "$base" "$pid_file"
287 [ -n "$pid" -a -z "$force" ] && return
289 # make sure it doesn't core dump anywhere unless requested
290 corelimit="ulimit -S -c ${DAEMON_COREFILE_LIMIT:-0}"
292 # if they set NICELEVEL in /etc/sysconfig/foo, honor it
293 [ -n "${NICELEVEL:-}" ] && nice="nice -n $NICELEVEL"
295 # if they set CGROUP_DAEMON in /etc/sysconfig/foo, honor it
296 if [ -n "${CGROUP_DAEMON}" ]; then
297 if [ ! -x /bin/cgexec ]; then
298 echo -n "Cgroups not installed"; warning
301 cgroup="/bin/cgexec";
302 for i in $CGROUP_DAEMON; do
303 cgroup="$cgroup -g $i";
309 [ "${BOOTUP:-}" = "verbose" -a -z "${LSB:-}" ] && echo -n " $base"
312 if [ -z "$user" ]; then
313 $cgroup $nice /bin/bash -c "$corelimit >/dev/null 2>&1 ; $*"
315 $cgroup $nice runuser -s /bin/bash $user -c "$corelimit >/dev/null 2>&1 ; $*"
318 [ "$?" -eq 0 ] && success $"$base startup" || failure $"$base startup"
321 # A function to stop a program.
323 local RC killlevel= base pid pid_file= delay
327 if [ "$#" -eq 0 ]; then
328 echo $"Usage: killproc [-p pidfile] [ -d delay] {program} [-signal]"
331 if [ "$1" = "-p" ]; then
335 if [ "$1" = "-d" ]; then
341 # check for second arg to be kill level
342 [ -n "${2:-}" ] && killlevel=$2
348 __pids_var_run "$1" "$pid_file"
350 if [ -z "$pid" ]; then
351 if [ -z "$pid_file" ]; then
352 pid="$(__pids_pidof "$1")"
354 [ "$RC" = "4" ] && { failure $"$base shutdown" ; return $RC ;}
359 if [ -n "$pid" ] ; then
360 [ "$BOOTUP" = "verbose" -a -z "${LSB:-}" ] && echo -n "$base "
361 if [ -z "$killlevel" ] ; then
362 if checkpid $pid 2>&1; then
363 # TERM first, then KILL if not dead
364 kill -TERM $pid >/dev/null 2>&1
366 # jordan # usleep 100000
367 if checkpid $pid && sleep 1 &&
368 checkpid $pid && sleep $delay &&
370 kill -KILL $pid >/dev/null 2>&1
372 # jordan # usleep 100000
377 [ "$RC" -eq 0 ] && failure $"$base shutdown" || success $"$base shutdown"
379 # use specified level only
381 if checkpid $pid; then
382 kill $killlevel $pid >/dev/null 2>&1
384 [ "$RC" -eq 0 ] && success $"$base $killlevel" || failure $"$base $killlevel"
385 elif [ -n "${LSB:-}" ]; then
386 RC=7 # Program is not running
390 if [ -n "${LSB:-}" -a -n "$killlevel" ]; then
391 RC=7 # Program is not running
393 failure $"$base shutdown"
398 # Remove pid file if any.
399 if [ -z "$killlevel" ]; then
400 rm -f "${pid_file:-/var/run/$base.pid}"
405 # A function to find the pid of a program. Looks *only* at the pidfile
410 if [ "$#" = 0 ] ; then
411 echo $"Usage: pidfileofproc {program}"
416 [ -n "$pid" ] && echo $pid
420 # A function to find the pid of a program.
422 local RC pid pid_file=
425 if [ "$#" = 0 ]; then
426 echo $"Usage: pidofproc [-p pidfile] {program}"
429 if [ "$1" = "-p" ]; then
433 fail_code=3 # "Program is not running"
435 # First try "/var/run/*.pid" files
436 __pids_var_run "$1" "$pid_file"
438 if [ -n "$pid" ]; then
443 [ -n "$pid_file" ] && return $RC
444 __pids_pidof "$1" || return $RC
448 local base pid lock_file= pid_file=
451 if [ "$#" = 0 ] ; then
452 echo $"Usage: status [-p pidfile] {program}"
455 if [ "$1" = "-p" ]; then
459 if [ "$1" = "-l" ]; then
465 if [ "$_use_systemctl" = "1" ]; then
466 systemctl status ${0##*/}.service
471 __pids_var_run "$1" "$pid_file"
473 if [ -z "$pid_file" -a -z "$pid" ]; then
474 pid="$(__pids_pidof "$1")"
476 if [ -n "$pid" ]; then
477 echo $"${base} (pid $pid) is running..."
483 echo $"${base} (pid $pid) is running..."
487 echo $"${base} dead but pid file exists"
491 echo $"${base} status unknown due to insufficient privileges."
495 if [ -z "${lock_file}" ]; then
498 # See if /var/lock/subsys/${lock_file} exists
499 if [ -f /var/lock/subsys/${lock_file} ]; then
500 echo $"${base} dead but subsys locked"
503 echo $"${base} is stopped"
508 [ "$BOOTUP" = "color" ] && $MOVE_TO_COL
510 [ "$BOOTUP" = "color" ] && $SETCOLOR_SUCCESS
512 [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
519 [ "$BOOTUP" = "color" ] && $MOVE_TO_COL
521 [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
523 [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
530 [ "$BOOTUP" = "color" ] && $MOVE_TO_COL
532 [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
534 [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
541 [ "$BOOTUP" = "color" ] && $MOVE_TO_COL
543 [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
545 [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
551 # Inform the graphical boot of our current state
552 update_boot_stage() {
553 if [ -x /usr/bin/plymouth ]; then
554 /usr/bin/plymouth --update="$1"
559 # Log that something succeeded
561 [ "$BOOTUP" != "verbose" -a -z "${LSB:-}" ] && echo_success
565 # Log that something failed
568 [ "$BOOTUP" != "verbose" -a -z "${LSB:-}" ] && echo_failure
569 [ -x /usr/bin/plymouth ] && /usr/bin/plymouth --details
573 # Log that something passed, but may have had errors. Useful for fsck
576 [ "$BOOTUP" != "verbose" -a -z "${LSB:-}" ] && echo_passed
583 [ "$BOOTUP" != "verbose" -a -z "${LSB:-}" ] && echo_warning
587 # Run some action. Log its output.
594 "$@" && success $"$STRING" || failure $"$STRING"
600 # returns OK if $1 contains $2
602 [ "${1#*$2*}" = "$1" ] && return 1
606 # Confirm whether we really want to run this service
608 [ -x /usr/bin/plymouth ] && /usr/bin/plymouth --hide-splash
610 echo -n $"Start service $1 (Y)es/(N)o/(C)ontinue? [Y] "
612 if strstr $"yY" "$answer" || [ "$answer" = "" ] ; then
614 elif strstr $"cC" "$answer" ; then
615 rm -f /var/run/confirm
616 [ -x /usr/bin/plymouth ] && /usr/bin/plymouth --show-splash
618 elif strstr $"nN" "$answer" ; then
624 # resolve a device node to its major:minor numbers in decimal or hex
628 if [ "$1" = "hex" ]; then
631 ls -lH "$2" | awk '{ sub(/,/, "", $5); printf("'"$fmt"'", $5, $6); }'
635 # Check whether file $1 is a backup or rpm-generated file and should be ignored
638 *~ | *.bak | *.orig | *.rpmnew | *.rpmorig | *.rpmsave)
645 # Evaluate shvar-style booleans
648 [tT] | [yY] | [yY][eE][sS] | [tT][rR][uU][eE])
655 # Evaluate shvar-style booleans
658 [fF] | [nN] | [nN][oO] | [fF][aA][lL][sS][eE])
666 [ "$1" = "/dev/urandom" -o "$1" = "/dev/hw_random" \
667 -o "$1" = "/dev/random" ]
670 find_crypto_mount_point() {
671 local fs_spec fs_file fs_vfstype remaining_fields
673 while read fs_spec fs_file remaining_fields; do
674 if [ "$fs_spec" = "/dev/mapper/$1" ]; then
681 # Because of a chicken/egg problem, init_crypto must be run twice. /var may be
682 # encrypted but /var/lib/random-seed is needed to initialize swap.
684 local have_random dst src key opt mode owner params makeswap skip arg opt
685 local param value rc ret mke2fs mdir prompt mount_point
689 while read dst src key opt; do
690 [ -z "$dst" -o "${dst#\#}" != "$dst" ] && continue
691 [ -b "/dev/mapper/$dst" ] && continue;
692 if [ "$have_random" = 0 ] && key_is_random "$key"; then
695 if [ -n "$key" -a "x$key" != "xnone" ]; then
696 if test -e "$key" ; then
697 owner=$(ls -l $key | (read a b owner rest; echo $owner))
698 if ! key_is_random "$key"; then
699 mode=$(ls -l "$key" | cut -c 5-10)
700 if [ "$mode" != "------" ]; then
701 echo $"INSECURE MODE FOR $key"
704 if [ "$owner" != root ]; then
705 echo $"INSECURE OWNER FOR $key"
708 echo $"Key file for $dst not found, skipping"
719 # Parse the src field for UUID= and convert to real device names
720 if [ "${src%%=*}" == "UUID" ]; then
721 src=$(/sbin/blkid -t "$src" -l -o device)
722 elif [ "${src/^\/dev\/disk\/by-uuid\/}" != "$src" ]; then
723 src=$(__readlink $src)
725 # Is it a block device?
726 [ -b "$src" ] || continue
727 # Is it already a device mapper slave? (this is gross)
729 devesc=${devesc//\//!}
730 for d in /sys/block/dm-*/slaves ; do
731 [ -e $d/$devesc ] && continue 2
733 # Parse the options field, convert to cryptsetup parameters and
734 # contruct the command line
735 while [ -n "$opt" ]; do
740 value=${arg##$param=}
744 params="$params -c $value"
745 if [ -z "$value" ]; then
746 echo $"$dst: no value for cipher option, skipping"
751 params="$params -s $value"
752 if [ -z "$value" ]; then
753 echo $"$dst: no value for size option, skipping"
758 params="$params -h $value"
759 if [ -z "$value" ]; then
760 echo $"$dst: no value for hash option, skipping"
774 if [ "$skip" = "yes" ]; then
778 if [ -z "$makeswap" ] && cryptsetup isLuks "$src" 2>/dev/null ; then
779 if key_is_random "$key"; then
780 echo $"$dst: LUKS requires non-random key, skipping"
784 if [ -n "$params" ]; then
785 echo "$dst: options are invalid for LUKS partitions," \
788 if [ -n "$key" ]; then
789 /sbin/cryptsetup -d $key luksOpen "$src" "$dst" <&1 2>/dev/null && success || failure
792 mount_point="$(find_crypto_mount_point $dst)"
793 [ -n "$mount_point" ] || mount_point=${src##*/}
794 prompt=$(printf $"%s is password protected" "$mount_point")
795 plymouth ask-for-password --prompt "$prompt" --command="/sbin/cryptsetup luksOpen -T1 $src $dst" <&1
799 [ -z "$key" ] && plymouth --hide-splash
800 /sbin/cryptsetup $params ${key:+-d $key} create "$dst" "$src" <&1 2>/dev/null && success || failure
802 [ -z "$key" ] && plymouth --show-splash
804 if [ $rc -ne 0 ]; then
808 if [ -b "/dev/mapper/$dst" ]; then
809 if [ "$makeswap" = "yes" ]; then
810 mkswap "/dev/mapper/$dst" 2>/dev/null >/dev/null
812 if [ "$mke2fs" = "yes" ]; then
813 if mke2fs "/dev/mapper/$dst" 2>/dev/null >/dev/null \
814 && mdir=$(mktemp -d /tmp/mountXXXXXX); then
815 mount "/dev/mapper/$dst" "$mdir" && chmod 1777 "$mdir"
825 # A sed expression to filter out the files that is_ignored_file recognizes
826 __sed_discard_ignored_files='/\(~\|\.bak\|\.orig\|\.rpmnew\|\.rpmorig\|\.rpmsave\)$/d'
828 if [ "$_use_systemctl" = "1" ]; then
829 if [ "x$1" = xstart -o \
831 "x$1" = xrestart -o \
833 "x$1" = xtry-restart -o \
834 "x$1" = xforce-reload -o \
835 "x$1" = xcondrestart ] ; then
837 systemctl_redirect $0 $1