]> TLD Linux GIT Repositories - rc-scripts.git/blobdiff - lib/functions
- version 0.5.4
[rc-scripts.git] / lib / functions
index fa12b3c9e40291e8d325de03d259e64c6efae063..b4c4840aa6dce182ea5b226d74cb07eb01cdb94a 100644 (file)
@@ -39,7 +39,6 @@ BLUE=4
 MAGENTA=5
 CYAN=6
 WHITE=7
-NORMAL=15
 # Bold definition (second parameter to termput setaf)
 BOLD=1
 NOBOLD=0
@@ -53,59 +52,13 @@ CTLD="$GREEN"               # "TLD Linux" color
 CI="$RED"              # Capital I color (press I to enter interactive startup)
 CRESMAN="$GREEN"       # "Resource Manager" color
 CHARS=""               # Characters displayed on the beginning of show line
-CCHARS="$NORMAL"       # Color of these characters (look at /etc/sysconfig/init-colors.gentoo example)
+CCHARS="$WHITE"                # Color of these characters (look at /etc/sysconfig/init-colors.gentoo example)
 
 # Source configuration if available - may override default values
 [ -r /etc/sysconfig/init-colors ] && . /etc/sysconfig/init-colors
 [ -r /etc/sysconfig/system ] && . /etc/sysconfig/system
 [ -r /etc/sysconfig/bootsplash ] && . /etc/sysconfig/bootsplash
 
-if [ -z "$VSERVER" -o "$VSERVER" = "detect" ]; then
-       {
-               while read _f _ctx; do
-                       [ "$_f" = "VxID:" -o "$_f" = "s_context:" ] && break
-               done </proc/self/status
-       } 2>/dev/null
-       if [ -z "$_ctx" -o "$_ctx" = "0" ]; then
-               VSERVER=no
-       else
-               VSERVER=yes
-       fi
-       unset _f _ctx
-fi
-
-# VSERVER_ISOLATION_NET = isolation only inside of vserver guests
-if [ -z "$VSERVER_ISOLATION_NET" -o "$VSERVER_ISOLATION_NET" = "detect" ]; then
-       VSERVER_ISOLATION_NET=no
-       if [ "$VSERVER" = "yes" ]; then
-               if [ -f /proc/self/nsproxy ]; then
-                       # older kernels
-                       {
-                               while read _t _data; do
-                                       [ "$_t" = "net:" ] && break
-                               done < /proc/self/nsproxy
-                       } 2> /dev/null
-                       if [ "${_data##*\(}" = "I)" ]; then
-                               VSERVER_ISOLATION_NET=yes
-                       fi
-               elif [ -f /proc/self/ninfo ]; then
-                       # newer kernels
-                       {
-                               while read _t _data; do
-                                       [ "$_t" = "NCaps:" ] && break
-                               done < /proc/self/ninfo
-                       } 2> /dev/null
-                       if [ "${_t}" = "NCaps:" ]; then
-                               VSERVER_ISOLATION_NET=yes
-                       fi
-               else
-                       # assume (very?) old kernel mode
-                       VSERVER_ISOLATION_NET=yes
-               fi
-               unset _f _data
-       fi
-fi
-
 # we need to know in functions if we were called from a terminal
 if [ -z "$ISATTY" ]; then
        [ -t ] && ISATTY=yes || ISATTY=no
@@ -166,25 +119,7 @@ strstr() {
 
 # Apply sysctl settings, including files in /etc/sysctl.d
 apply_sysctl() {
-       if [ -x /lib/systemd/systemd-sysctl ]; then
-               /lib/systemd/systemd-sysctl
-               return
-       fi
-
-       local file
-       for file in /usr/lib/sysctl.d/*.conf; do
-               [ -f /run/sysctl.d/${file##*/} ] && continue
-               [ -f /etc/sysctl.d/${file##*/} ] && continue
-               test -f "$file" && sysctl -q -e -p "$file"
-       done
-       for file in /run/sysctl.d/*.conf; do
-               [ -f /etc/sysctl.d/${file##*/} ] && continue
-               test -f "$file" && sysctl -q -e -p "$file"
-       done
-       for file in /etc/sysctl.d/*.conf; do
-               test -f "$file" && sysctl -q -e -p "$file"
-       done
-       sysctl -q -e -p /etc/sysctl.conf
+       sysctl -q -e --system
 }
 
 if is_yes "$FASTRC" || is_yes "$IN_SHUTDOWN"; then
@@ -297,7 +232,7 @@ termput() {
                        is_yes "$COLOR_INIT" && echo -ne "\033[${ISBOLD};3${2}m"
                        ;;
                op)
-                       termput setaf $NORMAL
+                       echo -ne "\033[0m"
                        ;;
                esac
        else
@@ -310,7 +245,7 @@ termput() {
                        is_yes "$COLOR_INIT" && tput setaf "$2"
                        ;;
                op)
-                       termput setaf $NORMAL
+                       tput sgr0
                        ;;
                esac
        fi
@@ -373,7 +308,7 @@ nls() {
 rc_splash() {
        local action="$1"
 
-       if ! is_no "$BOOT_SPLASH" && ! is_yes "$VSERVER"; then
+       if ! is_no "$BOOT_SPLASH"; then
                [ -x /bin/splash ] && /bin/splash "$action"
        fi
 
@@ -487,7 +422,6 @@ checkpid() {
 # - inside chroot get only those processes, which are inside chroot.
 # - don't filter out pids which do not have corresponding running processes (process died etc)
 # (note: some processes like named are chrooted but run outside chroot)
-# - do nothing inside vserver
 filter_chroot() {
        # no pids, exit early
        [ $# -eq 0 ] && return
@@ -499,11 +433,6 @@ filter_chroot() {
                pids=$(filter_ns "$@") && set -- "$pids"
        fi
 
-       if is_yes "$VSERVER"; then
-               echo $@
-               return
-       fi
-
        if [ $# -lt 1 -o ! -d /proc/1 ]; then
                echo $@
                return
@@ -619,16 +548,90 @@ _daemon_set_ulimits() {
        done
 }
 
+# inner function used by daemon().
+# do not call this directly, as it expects variables being inherited.
+# also it expects to be called from subshell as it exports env.
+# it expects options parsed by daemon() and command to be executed in "$@".
+_daemon_exec() {
+       local prog=""
+       umask ${SERVICE_UMASK:-$DEFAULT_SERVICE_UMASK};
+       export USER=root HOME=/tmp TMPDIR=/tmp
+
+       nice=${nice:-$DEFAULT_SERVICE_RUN_NICE_LEVEL}
+       nice=${nice:-0}
+
+       # make nice level absolute, not to be dependant of nice level of shell where service started
+       nice=$(($nice - $(nice)))
+
+       if [ "$closefds" = 1 ]; then
+               exec 1>&-
+               exec 2>&-
+               exec 0<&-
+       elif [ "$redirfds" = 1 ]; then
+               exec 1>/dev/null
+               exec 2>/dev/null
+               exec 0</dev/null
+       else
+               exec 2>&1
+               exec 0</dev/null
+       fi
+
+       if is_no "$RC_LOGGING"; then
+               prog=$1; shift
+               if [ ! -x $prog ]; then
+                       logger -t rc-scripts -p daemon.debug "daemon: Searching PATH for $prog, consider using full path in initscript"
+                       local a o=$IFS
+                       IFS=:
+                       for a in $PATH; do
+                               if [ -x $a/$prog ]; then
+                                       prog=$a/$prog
+                                       break
+                               fi
+                       done
+                       IFS=$o
+               fi
+               set -- "$prog" "$@"
+
+               # use setsid to detach from terminal,
+               # NOTE: setsid needs to be "outer" program
+               # otherwise start-stop-daemon would capture the setsid pid not the actual program
+
+               prog=$1; shift
+               /usr/bin/setsid \
+               /sbin/start-stop-daemon -q --start \
+                       --nicelevel $nice \
+                       ${pidfile:+--pidfile $pidfile} \
+                       ${makepid:+--make-pidfile} \
+                       ${user:+--chuid $user} \
+                       ${chdir:+--chdir "$chdir"} \
+                       ${fork:+--background} \
+                       ${SERVICE_DROPCAPS:+--dropcap $SERVICE_DROPCAPS} \
+                       --exec "$prog" \
+                       -- "$@"
+       else
+               if [ "$fork" = "1" ]; then
+                       export PIDFILE="/dev/null"
+                       if [ "$makepid" ] && [ "$pidfile" ]; then
+                               export PIDFILE="$pidfile"
+                       fi
+                       set -- /lib/rc-scripts/makepid "$@"
+                       set -- /usr/bin/setsid "$@"
+               fi
+               if [ -n "$user" -a "$user" != "root" ]; then
+                       set -- /bin/runuser -u "$user" -- "$@"
+               fi
+
+               nice -n $nice initlog -c "$*" 2>&1 </dev/null
+       fi
+}
+
 # A function to start a program (now it's useful on read-only filesystem too)
 daemon() {
-       local errors="" prog="" end="" waitname="" waittime=""
+       local errors="" waitname="" waittime=0
        local exit_code=0
        local nice=$SERVICE_RUN_NICE_LEVEL
        local fork user closefds redirfds pidfile makepid chdir=/
 
-       # NOTE: if you wonder how the shellish (by syntax) $prog works in ssd mode,
-       # then the answer is: it totally ignores $prog and uses "$@" itself.
-
        while [ $# -gt 0 ]; do
                case $1 in
                '')
@@ -646,7 +649,6 @@ daemon() {
                        ;;
                --fork)
                        fork=1
-                       end='&'
                        ;;
                --chdir)
                        shift
@@ -677,6 +679,8 @@ daemon() {
                        ;;
                --makepid)
                        makepid=1
+                       # makepid implies fork
+                       fork=1
                        ;;
                -*|+*)
                        nice=$1
@@ -689,19 +693,6 @@ daemon() {
                esac
                shift
        done
-       if [ -n "$user" -a "$user" != "root" ]; then
-               prog="/bin/su $user -s /bin/sh -c \""
-       fi
-       if [ "$fork" = "1" ]; then
-               prog="/usr/bin/setsid ${prog:-sh -c \"}"
-       fi
-       # If command to execute ends with quotation mark, add remaining
-       # arguments and close quotation.
-       if [ "$prog" != "${prog%\"}" ]; then
-               prog="$prog $*$end\""
-       else
-               prog="$prog $*$end"
-       fi
 
        _daemon_set_ulimits
 
@@ -712,67 +703,7 @@ daemon() {
        busy
        cd $chdir
        [ -n "$SERVICE_CPUSET" ] && is_yes "$CPUSETS" && echo $$ > "/dev/cpuset/${SERVICE_CPUSET}/tasks"
-       if errors=$(
-               umask ${SERVICE_UMASK:-$DEFAULT_SERVICE_UMASK};
-               export USER=root HOME=/tmp TMPDIR=/tmp
-
-               nice=${nice:-$DEFAULT_SERVICE_RUN_NICE_LEVEL}
-               nice=${nice:-0}
-
-               # make nice level absolute, not to be dependant of nice level of shell where service started
-               nice=$(($nice - $(nice)))
-
-               if [ "$closefds" = 1 ]; then
-                       exec 1>&-
-                       exec 2>&-
-                       exec 0<&-
-               elif [ "$redirfds" = 1 ]; then
-                       exec 1>/dev/null
-                       exec 2>/dev/null
-                       exec 0</dev/null
-               else
-                       exec 2>&1
-                       exec 0</dev/null
-               fi
-
-               if is_no "$RC_LOGGING"; then
-                       prog=$1; shift
-                       if [ ! -x $prog ]; then
-                               logger -t rc-scripts -p daemon.debug "daemon: Searching PATH for $prog, consider using full path in initscript"
-                               local a o=$IFS
-                               IFS=:
-                               for a in $PATH; do
-                                       if [ -x $a/$prog ]; then
-                                               prog=$a/$prog
-                                               break
-                                       fi
-                               done
-                               IFS=$o
-                       fi
-                       set -- "$prog" "$@"
-
-                       # use setsid to detach from terminal,
-                       # needs pidfile or ssd would check setsid program instead of real program
-                       if [ "$pidfile" ]; then
-                               set -- /usr/bin/setsid "$@"
-                       fi
-
-                       prog=$1; shift
-                       /sbin/start-stop-daemon -q --start \
-                               --nicelevel $nice \
-                               ${pidfile:+--pidfile $pidfile} \
-                               ${makepid:+--make-pidfile} \
-                               ${user:+--chuid $user} \
-                               ${chdir:+--chdir "$chdir"} \
-                               ${fork:+--background} \
-                               ${SERVICE_DROPCAPS:+--dropcap $SERVICE_DROPCAPS} \
-                               --exec "$prog" \
-                               -- "$@"
-               else
-                       nice -n $nice initlog -c "$prog" 2>&1 </dev/null
-               fi
-               ); then
-
+       if errors=$(_daemon_exec "$@"); then
                # wait for process (or pidfile) to be created
                if [ "$waittime" -gt 0 ]; then
                        # waitname can be empty, as if pidfile is in use, it is not relevant
@@ -965,7 +896,11 @@ killproc() {
 
        # Remove pid file if any.
        if [ "$notset" = "1" ]; then
-               rm -f /var/run/${base}.pid
+               if [ -f "${pidfile}" ] ; then
+                       rm -f "$pidfile"
+               else
+                       rm -f /var/run/${base}.pid
+               fi
        fi
 
        return $result
@@ -993,10 +928,12 @@ pidofproc() {
                for p in $(< "${pidfile}"); do
                        [ -z "$(echo "$p" | awk '{gsub(/[0-9]/,"");print;}')" ] && pid="$pid $p"
                done
+       else
+               unset pidfile
        fi
 
        # Next try "pidof" if pidfile is not specified
-       if [ -z "$pid" ] && [ -z "$pidfile" ]; then
+       if [ -z "$pid" ] && [ -z "$2" ]; then
                pid=$(pidof -o $$ -o $PPID -o %PPID -x "$1")
        fi
 
@@ -1091,6 +1028,9 @@ confirm() {
 
 # module is needed (ie. is requested, is available and isn't loaded already)
 is_module() {
+       # fail if /sbin/lsmod does not exists (ex. in containers)
+       [ -x /sbin/lsmod ] || return 1
+
        # module name without .o at end
        if ! lsmod | grep -q "$1"; then
                if ls -1R /lib/modules/$(uname -r)/ 2> /dev/null | grep -q "^${1}.\(\|k\)o\(\|.gz\)"; then
@@ -1323,35 +1263,6 @@ rc_gettext_init() {
        fi
 }
 
-use_upstart () {
-       return 1
-}
-emit () {
-       return 0
-}
-is_upstart_task() {
-       return 1
-}
-is_upstart_running() {
-       return 1
-}
-upstart_start() {
-       return 1
-}
-upstart_stop() {
-       return 1
-}
-upstart_reload() {
-       return 0
-}
-upstart_status() {
-       return 1
-}
-_upstart_controlled() {
-       return 0
-}
-alias upstart_controlled='_upstart_controlled $0 "$@"'
-
 rc_gettext_init
 rc_cache_init