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 # ubuntu does not have /bin/systemctl
36 [ -f /bin/systemctl ] || _use_systemctl=0
39 systemctl_redirect () {
46 s=$"Starting $prog (via systemctl): "
49 s=$"Stopping $prog (via systemctl): "
52 s=$"Reloading $prog configuration (via systemctl): "
54 restart|try-restart|condrestart)
55 s=$"Restarting $prog (via systemctl): "
59 action "$s" /bin/systemctl $command "$prog.service"
62 # Get a sane screen width
63 [ -z "${COLUMNS:-}" ] && COLUMNS=80
65 #if [ -z "${CONSOLETYPE:-}" ]; then
66 # if [ -r "/dev/stderr" ]; then
67 # CONSOLETYPE="$(/sbin/consoletype < /dev/stderr)"
69 # CONSOLETYPE="$(/sbin/consoletype)"
73 if [ -z "${NOLOCALE:-}" ] && [ -z "${LANGSH_SOURCED:-}" ] && [ -f /etc/sysconfig/i18n ] ; then
74 . /etc/profile.d/lang.sh 2>/dev/null
75 # avoid propagating LANGSH_SOURCED any further
79 # Read in our configuration
80 if [ -z "${BOOTUP:-}" ]; then
81 if [ -f /etc/sysconfig/init ]; then
84 # This all seem confusing? Look in /etc/sysconfig/init,
85 # or in /usr/doc/initscripts-*/sysconfig.txt
88 MOVE_TO_COL="echo -en \\033[${RES_COL}G"
89 SETCOLOR_SUCCESS="echo -en \\033[1;32m"
90 SETCOLOR_FAILURE="echo -en \\033[1;31m"
91 SETCOLOR_WARNING="echo -en \\033[1;33m"
92 SETCOLOR_NORMAL="echo -en \\033[0;39m"
95 if [ "$CONSOLETYPE" = "serial" ]; then
105 # Interpret escape sequences in an fstab entry
107 fstab-decode echo "$1"
110 # Check if any of $pid (could be plural) are running
115 [ -d "/proc/$i" ] && return 0
121 ls -bl "$@" 2>/dev/null| awk '{ print $NF }'
128 if strstr "$line" "$s"; then
136 # __umount_loop awk_program fstab_file first_msg retry_msg umount_args
137 # awk_program should process fstab_file and return a list of fstab-encoded
138 # paths; it doesn't have to handle comments in fstab_file.
143 remaining=$(LC_ALL=C awk "/^#/ {next} $1" "$2" | sort -r)
144 while [ -n "$remaining" -a "$retry" -gt 0 ]; do
145 if [ "$retry" -eq 3 ]; then
146 action "$3" fstab-decode umount $5 $remaining
148 action "$4" fstab-decode umount $5 $remaining
151 remaining=$(LC_ALL=C awk "/^#/ {next} $1" "$2" | sort -r)
152 while [ "$count" -gt 0 ]; do
153 [ -z "$remaining" ] && break
155 # jordan # usleep 500000
157 remaining=$(LC_ALL=C awk "/^#/ {next} $1" "$2" | sort -r)
159 [ -z "$remaining" ] && break
160 fstab-decode /sbin/fuser -k -m $sig $remaining >/dev/null
167 # Similar to __umount loop above, specialized for loopback devices
168 __umount_loopback_loop() {
169 local remaining devremaining sig=
172 remaining=$(awk '$1 ~ /^\/dev\/loop/ && $2 != "/" {print $2}' /proc/mounts)
173 devremaining=$(awk '$1 ~ /^\/dev\/loop/ && $2 != "/" {print $1}' /proc/mounts)
174 while [ -n "$remaining" -a "$retry" -gt 0 ]; do
175 if [ "$retry" -eq 3 ]; then
176 action $"Unmounting loopback filesystems: " \
177 fstab-decode umount $remaining
179 action $"Unmounting loopback filesystems (retry):" \
180 fstab-decode umount $remaining
182 for dev in $devremaining ; do
183 losetup $dev > /dev/null 2>&1 && \
184 action $"Detaching loopback device $dev: " \
187 remaining=$(awk '$1 ~ /^\/dev\/loop/ && $2 != "/" {print $2}' /proc/mounts)
188 devremaining=$(awk '$1 ~ /^\/dev\/loop/ && $2 != "/" {print $1}' /proc/mounts)
189 [ -z "$remaining" ] && break
190 fstab-decode /sbin/fuser -k -m $sig $remaining >/dev/null
197 # __proc_pids {program} [pidfile]
198 # Set $pid to pids from /var/run* for {program}. $pid should be declared
199 # local in the caller.
200 # Returns LSB exit code for the 'status' action.
203 local pid_file=${2:-/var/run/$base.pid}
206 if [ -f "$pid_file" ] ; then
209 [ ! -r "$pid_file" ] && return 4 # "user had insufficient privilege"
212 [ -z "$line" ] && break
214 [ -z "${p//[0-9]/}" ] && [ -d "/proc/$p" ] && pid="$pid $p"
218 if [ -n "$pid" ]; then
221 return 1 # "Program is dead and /var/run pid file exists"
223 return 3 # "Program is not running"
226 # Output PIDs of matching processes, found using pidof
228 pidof -c -o $$ -o $PPID -o %PPID -x "$1" || \
229 pidof -c -o $$ -o $PPID -o %PPID -x "${1##*/}"
230 # jordan # pidof -c -m -o $$ -o $PPID -o %PPID -x "$1" || \
231 # jordan # pidof -c -m -o $$ -o $PPID -o %PPID -x "${1##*/}"
235 # A function to start a program.
238 local gotbase= force= nicelevel corelimit
239 local pid base= user= nice= bg= pid_file=
242 while [ "$1" != "${1##[-+]}" ]; do
244 '') echo $"$0: Usage: daemon [+/-nicelevel] {program}"
269 pid_file=${1#--pidfile=}
280 *) echo $"$0: Usage: daemon [+/-nicelevel] {program}"
286 [ -z "$gotbase" ] && base=${1##*/}
288 # See if it's already running. Look *only* at the pid file.
289 __pids_var_run "$base" "$pid_file"
291 [ -n "$pid" -a -z "$force" ] && return
293 # make sure it doesn't core dump anywhere unless requested
294 corelimit="ulimit -S -c ${DAEMON_COREFILE_LIMIT:-0}"
296 # if they set NICELEVEL in /etc/sysconfig/foo, honor it
297 [ -n "${NICELEVEL:-}" ] && nice="nice -n $NICELEVEL"
299 # if they set CGROUP_DAEMON in /etc/sysconfig/foo, honor it
300 if [ -n "${CGROUP_DAEMON}" ]; then
301 if [ ! -x /bin/cgexec ]; then
302 echo -n "Cgroups not installed"; warning
305 cgroup="/bin/cgexec";
306 for i in $CGROUP_DAEMON; do
307 cgroup="$cgroup -g $i";
313 [ "${BOOTUP:-}" = "verbose" -a -z "${LSB:-}" ] && echo -n " $base"
316 if [ -z "$user" ]; then
317 $cgroup $nice /bin/bash -c "$corelimit >/dev/null 2>&1 ; $*"
319 $cgroup $nice runuser -s /bin/bash $user -c "$corelimit >/dev/null 2>&1 ; $*"
322 [ "$?" -eq 0 ] && success $"$base startup" || failure $"$base startup"
325 # A function to stop a program.
327 local RC killlevel= base pid pid_file= delay
331 if [ "$#" -eq 0 ]; then
332 echo $"Usage: killproc [-p pidfile] [ -d delay] {program} [-signal]"
335 if [ "$1" = "-p" ]; then
339 if [ "$1" = "-d" ]; then
345 # check for second arg to be kill level
346 [ -n "${2:-}" ] && killlevel=$2
352 __pids_var_run "$1" "$pid_file"
354 if [ -z "$pid" ]; then
355 if [ -z "$pid_file" ]; then
356 pid="$(__pids_pidof "$1")"
358 [ "$RC" = "4" ] && { failure $"$base shutdown" ; return $RC ;}
363 if [ -n "$pid" ] ; then
364 [ "$BOOTUP" = "verbose" -a -z "${LSB:-}" ] && echo -n "$base "
365 if [ -z "$killlevel" ] ; then
366 if checkpid $pid 2>&1; then
367 # TERM first, then KILL if not dead
368 kill -TERM $pid >/dev/null 2>&1
370 # jordan # usleep 100000
371 if checkpid $pid && sleep 1 &&
372 checkpid $pid && sleep $delay &&
374 kill -KILL $pid >/dev/null 2>&1
376 # jordan # usleep 100000
381 [ "$RC" -eq 0 ] && failure $"$base shutdown" || success $"$base shutdown"
383 # use specified level only
385 if checkpid $pid; then
386 kill $killlevel $pid >/dev/null 2>&1
388 [ "$RC" -eq 0 ] && success $"$base $killlevel" || failure $"$base $killlevel"
389 elif [ -n "${LSB:-}" ]; then
390 RC=7 # Program is not running
394 if [ -n "${LSB:-}" -a -n "$killlevel" ]; then
395 RC=7 # Program is not running
397 failure $"$base shutdown"
402 # Remove pid file if any.
403 if [ -z "$killlevel" ]; then
404 rm -f "${pid_file:-/var/run/$base.pid}"
409 # A function to find the pid of a program. Looks *only* at the pidfile
414 if [ "$#" = 0 ] ; then
415 echo $"Usage: pidfileofproc {program}"
420 [ -n "$pid" ] && echo $pid
424 # A function to find the pid of a program.
426 local RC pid pid_file=
429 if [ "$#" = 0 ]; then
430 echo $"Usage: pidofproc [-p pidfile] {program}"
433 if [ "$1" = "-p" ]; then
437 fail_code=3 # "Program is not running"
439 # First try "/var/run/*.pid" files
440 __pids_var_run "$1" "$pid_file"
442 if [ -n "$pid" ]; then
447 [ -n "$pid_file" ] && return $RC
448 __pids_pidof "$1" || return $RC
452 local base pid lock_file= pid_file=
455 if [ "$#" = 0 ] ; then
456 echo $"Usage: status [-p pidfile] {program}"
459 if [ "$1" = "-p" ]; then
463 if [ "$1" = "-l" ]; then
469 if [ "$_use_systemctl" = "1" ]; then
470 systemctl status ${0##*/}.service
475 __pids_var_run "$1" "$pid_file"
477 if [ -z "$pid_file" -a -z "$pid" ]; then
478 pid="$(__pids_pidof "$1")"
480 if [ -n "$pid" ]; then
481 echo $"${base} (pid $pid) is running..."
487 echo $"${base} (pid $pid) is running..."
491 echo $"${base} dead but pid file exists"
495 echo $"${base} status unknown due to insufficient privileges."
499 if [ -z "${lock_file}" ]; then
502 # See if /var/lock/subsys/${lock_file} exists
503 if [ -f /var/lock/subsys/${lock_file} ]; then
504 echo $"${base} dead but subsys locked"
507 echo $"${base} is stopped"
512 [ "$BOOTUP" = "color" ] && $MOVE_TO_COL
514 [ "$BOOTUP" = "color" ] && $SETCOLOR_SUCCESS
516 [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
523 [ "$BOOTUP" = "color" ] && $MOVE_TO_COL
525 [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
527 [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
534 [ "$BOOTUP" = "color" ] && $MOVE_TO_COL
536 [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
538 [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
545 [ "$BOOTUP" = "color" ] && $MOVE_TO_COL
547 [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
549 [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
555 # Inform the graphical boot of our current state
556 update_boot_stage() {
557 if [ -x /usr/bin/plymouth ]; then
558 /usr/bin/plymouth --update="$1"
563 # Log that something succeeded
565 [ "$BOOTUP" != "verbose" -a -z "${LSB:-}" ] && echo_success
569 # Log that something failed
572 [ "$BOOTUP" != "verbose" -a -z "${LSB:-}" ] && echo_failure
573 [ -x /usr/bin/plymouth ] && /usr/bin/plymouth --details
577 # Log that something passed, but may have had errors. Useful for fsck
580 [ "$BOOTUP" != "verbose" -a -z "${LSB:-}" ] && echo_passed
587 [ "$BOOTUP" != "verbose" -a -z "${LSB:-}" ] && echo_warning
591 # Run some action. Log its output.
598 "$@" && success $"$STRING" || failure $"$STRING"
604 # returns OK if $1 contains $2
606 [ "${1#*$2*}" = "$1" ] && return 1
610 # Confirm whether we really want to run this service
612 [ -x /usr/bin/plymouth ] && /usr/bin/plymouth --hide-splash
614 echo -n $"Start service $1 (Y)es/(N)o/(C)ontinue? [Y] "
616 if strstr $"yY" "$answer" || [ "$answer" = "" ] ; then
618 elif strstr $"cC" "$answer" ; then
619 rm -f /var/run/confirm
620 [ -x /usr/bin/plymouth ] && /usr/bin/plymouth --show-splash
622 elif strstr $"nN" "$answer" ; then
628 # resolve a device node to its major:minor numbers in decimal or hex
632 if [ "$1" = "hex" ]; then
635 ls -lH "$2" | awk '{ sub(/,/, "", $5); printf("'"$fmt"'", $5, $6); }'
639 # Check whether file $1 is a backup or rpm-generated file and should be ignored
642 *~ | *.bak | *.orig | *.rpmnew | *.rpmorig | *.rpmsave)
649 # Evaluate shvar-style booleans
652 [tT] | [yY] | [yY][eE][sS] | [tT][rR][uU][eE])
659 # Evaluate shvar-style booleans
662 [fF] | [nN] | [nN][oO] | [fF][aA][lL][sS][eE])
670 [ "$1" = "/dev/urandom" -o "$1" = "/dev/hw_random" \
671 -o "$1" = "/dev/random" ]
674 find_crypto_mount_point() {
675 local fs_spec fs_file fs_vfstype remaining_fields
677 while read fs_spec fs_file remaining_fields; do
678 if [ "$fs_spec" = "/dev/mapper/$1" ]; then
685 # Because of a chicken/egg problem, init_crypto must be run twice. /var may be
686 # encrypted but /var/lib/random-seed is needed to initialize swap.
688 local have_random dst src key opt mode owner params makeswap skip arg opt
689 local param value rc ret mke2fs mdir prompt mount_point
693 while read dst src key opt; do
694 [ -z "$dst" -o "${dst#\#}" != "$dst" ] && continue
695 [ -b "/dev/mapper/$dst" ] && continue;
696 if [ "$have_random" = 0 ] && key_is_random "$key"; then
699 if [ -n "$key" -a "x$key" != "xnone" ]; then
700 if test -e "$key" ; then
701 owner=$(ls -l $key | (read a b owner rest; echo $owner))
702 if ! key_is_random "$key"; then
703 mode=$(ls -l "$key" | cut -c 5-10)
704 if [ "$mode" != "------" ]; then
705 echo $"INSECURE MODE FOR $key"
708 if [ "$owner" != root ]; then
709 echo $"INSECURE OWNER FOR $key"
712 echo $"Key file for $dst not found, skipping"
723 # Parse the src field for UUID= and convert to real device names
724 if [ "${src%%=*}" == "UUID" ]; then
725 src=$(/sbin/blkid -t "$src" -l -o device)
726 elif [ "${src/^\/dev\/disk\/by-uuid\/}" != "$src" ]; then
727 src=$(__readlink $src)
729 # Is it a block device?
730 [ -b "$src" ] || continue
731 # Is it already a device mapper slave? (this is gross)
733 devesc=${devesc//\//!}
734 for d in /sys/block/dm-*/slaves ; do
735 [ -e $d/$devesc ] && continue 2
737 # Parse the options field, convert to cryptsetup parameters and
738 # contruct the command line
739 while [ -n "$opt" ]; do
744 value=${arg##$param=}
748 params="$params -c $value"
749 if [ -z "$value" ]; then
750 echo $"$dst: no value for cipher option, skipping"
755 params="$params -s $value"
756 if [ -z "$value" ]; then
757 echo $"$dst: no value for size option, skipping"
762 params="$params -h $value"
763 if [ -z "$value" ]; then
764 echo $"$dst: no value for hash option, skipping"
778 if [ "$skip" = "yes" ]; then
782 if [ -z "$makeswap" ] && cryptsetup isLuks "$src" 2>/dev/null ; then
783 if key_is_random "$key"; then
784 echo $"$dst: LUKS requires non-random key, skipping"
788 if [ -n "$params" ]; then
789 echo "$dst: options are invalid for LUKS partitions," \
792 if [ -n "$key" ]; then
793 /sbin/cryptsetup -d $key luksOpen "$src" "$dst" <&1 2>/dev/null && success || failure
796 mount_point="$(find_crypto_mount_point $dst)"
797 [ -n "$mount_point" ] || mount_point=${src##*/}
798 prompt=$(printf $"%s is password protected" "$mount_point")
799 plymouth ask-for-password --prompt "$prompt" --command="/sbin/cryptsetup luksOpen -T1 $src $dst" <&1
803 [ -z "$key" ] && plymouth --hide-splash
804 /sbin/cryptsetup $params ${key:+-d $key} create "$dst" "$src" <&1 2>/dev/null && success || failure
806 [ -z "$key" ] && plymouth --show-splash
808 if [ $rc -ne 0 ]; then
812 if [ -b "/dev/mapper/$dst" ]; then
813 if [ "$makeswap" = "yes" ]; then
814 mkswap "/dev/mapper/$dst" 2>/dev/null >/dev/null
816 if [ "$mke2fs" = "yes" ]; then
817 if mke2fs "/dev/mapper/$dst" 2>/dev/null >/dev/null \
818 && mdir=$(mktemp -d /tmp/mountXXXXXX); then
819 mount "/dev/mapper/$dst" "$mdir" && chmod 1777 "$mdir"
829 # A sed expression to filter out the files that is_ignored_file recognizes
830 __sed_discard_ignored_files='/\(~\|\.bak\|\.orig\|\.rpmnew\|\.rpmorig\|\.rpmsave\)$/d'
832 if [ "$_use_systemctl" = "1" ]; then
833 if [ "x$1" = xstart -o \
835 "x$1" = xrestart -o \
837 "x$1" = xtry-restart -o \
838 "x$1" = xforce-reload -o \
839 "x$1" = xcondrestart ] ; then
841 systemctl_redirect $0 $1