]> git.pld-linux.org Git - projects/rc-scripts.git/blobdiff - service
Allow overriding of vlan base device name
[projects/rc-scripts.git] / service
diff --git a/service b/service
index 3a0f2486408503f100af0bdb802c08e9fc69ac6c..2d5a3e555e42674e6bb9401eb39f0ffbdb2365d0 100755 (executable)
--- a/service
+++ b/service
 PATH="/sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin"
 export PATH
 
-VERSION="$(basename $0) ver. 0.91"
+is_ignored_file() {
+       case "$1" in
+       skeleton | README | *.dpkg-dist | *.dpkg-old | rc | rcS | single | reboot | bootclean.sh)
+               return 0
+               ;;
+       *rpmorig | *rpmnew | *rpmsave | *~ | *.orig)
+               return 0
+               ;;
+       esac
+       return 1
+}
+
+# check if SERVICE is present in systemd and ACTION is valid systemctl command
+# returns false if systemd is disabled or not active
+is_systemd_service() {
+       local SERVICE=$1 ACTION=$2
+
+       [ "$USE_SYSTEMD" = "no" ] && return 1
+
+       # if we are called from systemd itself, because some .service specified
+       # invocation via /sbin/service. this avoids loops
+       # detect this via CMDLINE var, which has leaked from geninitrd
+       if [ -n "$CMDLINE" ]; then
+               echo >&2  "Warning: CMDLINE env set, likely you are defining .service to use /sbin/service, please use /etc/rc.d/init.d/<SERVICE> instead"
+               return 1
+       fi
+
+       case "$ACTION" in
+       # list obtained as: man systemctl | grep N.*A.*M.*E
+       start | \
+       stop | \
+       reload | \
+       restart | \
+       try-restart | \
+       reload-or-restart | \
+       reload-or-try-restart | \
+       isolate | \
+       kill | \
+       is-active | \
+       status | \
+       show | \
+       reset-failed | \
+       enable | \
+       disable | \
+       is-enabled | \
+       reenable | \
+       preset | \
+       mask | \
+       unmask | \
+       link | \
+       load | \
+       snapshot | \
+       delete | \
+       set-environment | \
+       unset-environment )
+               ;;
+       *)
+               #echo "Not valid systemd command"
+               return 1
+       esac
+
+       [ -x /bin/systemd_booted ] || return 1
+       /bin/systemd_booted || return 1
+
+       /bin/systemctl show "$SERVICE".service | grep -q LoadError= && return 1 || return 0
+}
+
+status_all() {
+       local SERVICE TYPE has_systemd has_upstart
+
+       if [ "$USE_SYSTEMD" != "no" ] && [ -x /bin/systemd_booted ] && /bin/systemd_booted; then
+               has_systemd=1
+       else
+               unset has_systemd
+       fi
+
+       if [ "$USE_UPSTART" != "no" ] && [ -x /sbin/initctl ]; then
+               has_upstart=1
+       else
+               unset has_upstart
+       fi
+
+       cd ${SERVICEDIR}
+       for SERVICE in *; do
+               case "${SERVICE}" in
+               functions | halt | killall | single| linuxconf| kudzu)
+                       ;;
+               *)
+               if ! is_ignored_file "${SERVICE}" \
+                               && [ -x "${SERVICEDIR}/${SERVICE}" ]; then
+                       if [ "$has_systemd" ] && [ -f /lib/systemd/system/${SERVICE}.service ]; then
+                               # D for SystemD
+                               TYPE='D'
+                       elif [ "$has_upstart" ] && [ -f /etc/init/${SERVICE}.conf ]; then
+                               # U for upstart
+                               TYPE='U'
+                       else
+                               # S for SysVinit
+                               TYPE='S'
+                       fi
+                       if ! grep -qs "\Wstatus)" "$SERVICE"; then
+                               printf " %s %-60s %s\n" "$TYPE:[?]" "$SERVICE:" "unknown"
+                               continue
+                       else
+                               out=$(env -i USE_UPSTART=$USE_UPSTART LANG="$LANG" PATH="$PATH" TERM="$TERM" "$SERVICEDIR/$SERVICE" status 2>&1)
+                               if [ "$?" = "0" -a -n "$out" ]; then
+                                       printf " %s %-60s %s\n" "$TYPE:[+]" "$SERVICE:" "running"
+                                       continue
+                               else
+                                       printf " %s %-60s %s %s\n" "$TYPE:[-]" "$SERVICE:" "NOT running"
+                                       continue
+                               fi
+                       fi
+               fi
+               ;;
+               esac
+       done
+}
+
+VERSION="$(basename $0) ver. 0.91-pld"
 USAGE="Usage: $(basename $0) < option > | --status-all | \
 [ service_name [ command | --full-restart ] ]"
 
 SERVICE=
-
-. /etc/rc.d/init.d/functions
+USE_UPSTART=
+USE_SYSTEMD=
 
 if [ -d /etc/rc.d/init.d ]; then
        SERVICEDIR="/etc/rc.d/init.d"
@@ -23,121 +142,53 @@ if [ $# -eq 0 ]; then
        exit 1
 fi
 
-is_task() {
-       grep -q '^task' "/etc/init/$1.conf"
-}
-is_running() {
-       initctl status "$1" 2>/dev/null | grep -q running
-}
-upstart_start() {
-       local SERVICE=$1
-       is_running "${SERVICE}" && return 0
-       msg_starting "${SERVICE}"
-       if errors=$(/sbin/initctl start ${SERVICE} 2>&1) ; then
-               ok
-               return 0
-       else
-               fail
-               echo "$errors" >&2
-               return 1
-       fi
-}
-upstart_stop() {
-       local SERVICE=$1
-       if ! is_running "${SERVICE}" && ! is_task "${SERVICE}" ; then
-               return 0
-       fi
-       msg_stopping "${SERVICE}"
-       if errors=$(/sbin/initctl stop ${SERVICE}) ; then
-               ok
-               return 0
-       else
-               fail
-               echo "$errors" >&2
-               return 1
-       fi
-}
-upstart_status() {
-       # get service status
-       # should be compliant with
-        # http://refspecs.freestandards.org/LSB_3.1.1/LSB-Core-generic/LSB-Core-generic/iniscrptact.html
-       local SERVICE=$1
-       local status
-       if is_task "${SERVICE}" ; then
-               # we probably should have a way to handle task status
-               return 0
-       fi
-       if ! status=$(/sbin/initctl status "${SERVICE}") ; then
-               # program or service status is not known
-               return 4
-       fi
-       if strstr "$status" "running" ; then
-               # program is running or service is OK
-               return 0
-       else
-               # program is not running
-               return 3
-       fi
-       # TODO: other statuses
-}
-
 cd /
 while [ $# -gt 0 ]; do
        case "${1}" in
-         --help | -h | --h* )
+       --help | -h | --h* )
                echo "${USAGE}" >&2
                exit 0
                ;;
-         --version | -V )
+       --version | -V )
                echo "${VERSION}" >&2
                exit 0
                ;;
+       --ignore-dependencies)
+               export SYSTEMCTL_IGNORE_DEPENDENCIES=1
+               shift
+               ;;
+       --skip-redirect)
+               export SYSTEMCTL_SKIP_REDIRECT=1
+               shift
+               ;;
+       --upstart)
+               USE_UPSTART=yes
+               shift
+               ;;
+       --no-upstart)
+               USE_UPSTART=no
+               shift
+               ;;
+       --no-systemd)
+               USE_SYSTEMD=no
+               shift
+               ;;
          *)
                if [ -z "${SERVICE}" -a $# -eq 1 -a "${1}" = "--status-all" ]; then
-                       cd ${SERVICEDIR}
-                       for SERVICE in * ; do
-                               if use_upstart && [ -f "/etc/init/${SERVICE}.conf" ] ; then
-                                       continue
-                               fi
-                               case "${SERVICE}" in
-                                 functions | halt | killall | single| linuxconf| kudzu | \
-                                 *rpmorig | *rpmnew | *rpmsave | *~ | *.orig)
-                                       ;;
-                                 *)
-                                       if [ -x "${SERVICEDIR}/${SERVICE}" ]; then
-                                               env -i LANG=$LANG PATH=$PATH TERM=$TERM "${SERVICEDIR}/${SERVICE}" status
-                                       fi
-                                       ;;
-                               esac
-                       done
-                       if [ -d /etc/init ] && use_upstart ; then
-                               cd /etc/init
-                               for f in *.conf ; do
-                                       SERVICE="${f%.conf}"
-                                       case "${SERVICE}" in
-                                         control-alt-delete | rc | rcS-sulogin | rcS | start-ttys | tty)
-                                               ;;
-                                         *)
-                                               /sbin/initctl status "${SERVICE}"
-                                               ;;
-                                       esac
-                               done
-                       fi
+                       status_all
                        exit 0
                elif [ $# -eq 2 -a "${2}" = "--full-restart" ]; then
                        SERVICE="${1}"
-                       if [ -f "/etc/init/${SERVICE}.conf" ] && use_upstart ; then
-                               upstart_stop
-                               upstart_start
-                       elif [ -x "${SERVICEDIR}/${SERVICE}" ]; then
-                               env -i LANG=$LANG PATH=$PATH TERM=$TERM "${SERVICEDIR}/${SERVICE}" stop
-                               env -i LANG=$LANG PATH=$PATH TERM=$TERM "${SERVICEDIR}/${SERVICE}" start
+                       if [ -x "${SERVICEDIR}/${SERVICE}" ]; then
+                               env -i USE_UPSTART=$USE_UPSTART LANG=$LANG PATH=$PATH TERM=$TERM "${SERVICEDIR}/${SERVICE}" stop
+                               env -i USE_UPSTART=$USE_UPSTART LANG=$LANG PATH=$PATH TERM=$TERM "${SERVICEDIR}/${SERVICE}" start
                                exit $?
                        fi
                elif [ -z "${SERVICE}" ]; then
                        SERVICE="${1}"
+               elif [ -z "${ACTION}" ]; then
+                       ACTION="${1}"
                else
-                       COMMAND="${1}"
                        OPTIONS="${OPTIONS} ${1}"
                fi
                shift
@@ -145,59 +196,12 @@ while [ $# -gt 0 ]; do
        esac
 done
 
-if [ -f "/etc/init/${SERVICE}.conf" ] && use_upstart ; then
-       case $COMMAND in
-               start)
-                       upstart_start "${SERVICE}"
-                       exit $?
-                       ;;
-               stop)
-                       upstart_stop "${SERVICE}"
-                       exit $?
-                       ;;
-               status)
-                       upstart_status "${SERVICE}"
-                       exit $?
-                       ;;
-               force-reload)
-                       if ! grep -Eq '#\s*pld-flags:.*no-sighup-reload' "/etc/init/${SERVICE}.conf" ; then
-                               upstart_reload "${SERVICE}"
-                               exit $?
-                       else
-                               upstart_stop "${SERVICE}"
-                               upstart_start "${SERVICE}"
-                               exit $?
-                       fi
-                       ;;
-               reload)
-                       if ! grep -Eq '#\s*pld-flags:.*no-sighup-reload' "/etc/init/${SERVICE}.conf" ; then
-                               upstart_reload "${SERVICE}"
-                               exit $?
-                       fi
-                       if [ ! -x "${SERVICEDIR}/${SERVICE}" ]; then
-                               msg_usage "$0 {start|stop|restart|status|force-reload}"
-                               exit 3
-                       fi
-                       # if not handled here, pass it the rc.d/init.d script
-                       ;;
-               *)
-                       if [ ! -x "${SERVICEDIR}/${SERVICE}" ]; then
-                               commands="start|stop|restart|status|force-reload"
-                               if ! grep -Eq '#\s*pld-flags:.*no-sighup-reload' "/etc/init/${SERVICE}.conf" ; then
-                                       commands="$commands|reload"
-                               fi
-                               msg_usage "$0 {$commands}"
-                               exit 3
-                       fi
-                       # pass the rest to the rc.d/init.d script
-                       ;;
-       esac
-fi
-
-if [ -x "${SERVICEDIR}/${SERVICE}" ]; then
-       env -i LANG=$LANG PATH=$PATH TERM=$TERM "${SERVICEDIR}/${SERVICE}" ${OPTIONS}
+if is_systemd_service "${SERVICE}" "${ACTION}"; then
+       echo >&2 "Redirecting to /bin/systemctl --output=cat ${ACTION} ${SERVICE}.service ${OPTIONS}"
+       exec /bin/systemctl --output=cat ${ACTION} ${SERVICE}.service ${OPTIONS}
+elif [ -x "${SERVICEDIR}/${SERVICE}" ]; then
+       exec env -i USE_UPSTART=$USE_UPSTART LANG=$LANG PATH=$PATH TERM=$TERM "${SERVICEDIR}/${SERVICE}" ${ACTION} ${OPTIONS}
 else
        echo "${SERVICE}: unrecognized service" >&2
        exit 1
 fi
-
This page took 0.051366 seconds and 4 git commands to generate.