You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

221 lines
9.1 KiB

  1. #!/bin/sh
  2. ###########################################################################
  3. # /usr/bin/service
  4. #
  5. # A convenient wrapper for the /etc/init.d init scripts.
  6. #
  7. # This script is a modified version of the /sbin/service utility found on
  8. # Red Hat/Fedora systems (licensed GPLv2+).
  9. #
  10. # Copyright (C) 2006 Red Hat, Inc. All rights reserved.
  11. # Copyright (C) 2008 Canonical Ltd.
  12. # * August 2008 - Dustin Kirkland <kirkland@canonical.com>
  13. # Copyright (C) 2013 Michael Stapelberg <stapelberg@debian.org>
  14. #
  15. # This program is free software; you can redistribute it and/or modify
  16. # it under the terms of the GNU General Public License as published by
  17. # the Free Software Foundation; either version 2 of the License, or
  18. # (at your option) any later version.
  19. #
  20. # This program is distributed in the hope that it will be useful,
  21. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. # GNU General Public License for more details.
  24. #
  25. # You should have received a copy of the GNU General Public License
  26. # along with this program; if not, write to the Free Software
  27. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  28. #
  29. # On Debian GNU/Linux systems, the complete text of the GNU General
  30. # Public License can be found in `/usr/share/common-licenses/GPL-2'.
  31. ###########################################################################
  32. is_ignored_file() {
  33. case "$1" in
  34. skeleton | README | *.dpkg-dist | *.dpkg-old | rc | rcS | single | reboot | bootclean.sh)
  35. return 0
  36. ;;
  37. esac
  38. return 1
  39. }
  40. VERSION="`basename $0` ver. __VERSION__"
  41. USAGE="Usage: `basename $0` < option > | --status-all | \
  42. [ service_name [ command | --full-restart ] ]"
  43. SERVICE=
  44. ACTION=
  45. SERVICEDIR="/etc/init.d"
  46. OPTIONS=
  47. is_systemd=
  48. if [ $# -eq 0 ]; then
  49. echo "${USAGE}" >&2
  50. exit 1
  51. fi
  52. if [ -d /run/systemd/system ]; then
  53. is_systemd=1
  54. fi
  55. cd /
  56. while [ $# -gt 0 ]; do
  57. case "${1}" in
  58. --help | -h | --h* )
  59. echo "${USAGE}" >&2
  60. exit 0
  61. ;;
  62. --version | -V )
  63. echo "${VERSION}" >&2
  64. exit 0
  65. ;;
  66. *)
  67. if [ -z "${SERVICE}" -a $# -eq 1 -a "${1}" = "--status-all" ]; then
  68. cd ${SERVICEDIR}
  69. for SERVICE in * ; do
  70. case "${SERVICE}" in
  71. functions | halt | killall | single| linuxconf| kudzu)
  72. ;;
  73. *)
  74. if ! is_ignored_file "${SERVICE}" \
  75. && [ -x "${SERVICEDIR}/${SERVICE}" ]; then
  76. out=$(env -i LANG="$LANG" LANGUAGE="$LANGUAGE" LC_CTYPE="$LC_CTYPE" LC_NUMERIC="$LC_NUMERIC" LC_TIME="$LC_TIME" LC_COLLATE="$LC_COLLATE" LC_MONETARY="$LC_MONETARY" LC_MESSAGES="$LC_MESSAGES" LC_PAPER="$LC_PAPER" LC_NAME="$LC_NAME" LC_ADDRESS="$LC_ADDRESS" LC_TELEPHONE="$LC_TELEPHONE" LC_MEASUREMENT="$LC_MEASUREMENT" LC_IDENTIFICATION="$LC_IDENTIFICATION" LC_ALL="$LC_ALL" PATH="$PATH" TERM="$TERM" "$SERVICEDIR/$SERVICE" status 2>&1)
  77. retval=$?
  78. if echo "$out" | egrep -iq "usage:"; then
  79. #printf " %s %-60s %s\n" "[?]" "$SERVICE:" "unknown" 1>&2
  80. echo " [ ? ] $SERVICE" 1>&2
  81. continue
  82. else
  83. if [ "$retval" = "0" -a -n "$out" ]; then
  84. #printf " %s %-60s %s\n" "[+]" "$SERVICE:" "running"
  85. echo " [ + ] $SERVICE"
  86. continue
  87. else
  88. #printf " %s %-60s %s\n" "[-]" "$SERVICE:" "NOT running"
  89. echo " [ - ] $SERVICE"
  90. continue
  91. fi
  92. fi
  93. #env -i LANG="$LANG" LANGUAGE="$LANGUAGE" LC_CTYPE="$LC_CTYPE" LC_NUMERIC="$LC_NUMERIC" LC_TIME="$LC_TIME" LC_COLLATE="$LC_COLLATE" LC_MONETARY="$LC_MONETARY" LC_MESSAGES="$LC_MESSAGES" LC_PAPER="$LC_PAPER" LC_NAME="$LC_NAME" LC_ADDRESS="$LC_ADDRESS" LC_TELEPHONE="$LC_TELEPHONE" LC_MEASUREMENT="$LC_MEASUREMENT" LC_IDENTIFICATION="$LC_IDENTIFICATION" LC_ALL="$LC_ALL" PATH="$PATH" TERM="$TERM" "$SERVICEDIR/$SERVICE" status
  94. fi
  95. ;;
  96. esac
  97. done
  98. exit 0
  99. elif [ $# -eq 2 -a "${2}" = "--full-restart" ]; then
  100. SERVICE="${1}"
  101. # On systems using systemd, we just perform a normal restart:
  102. # A restart with systemd is already a full restart.
  103. if [ -n "$is_systemd" ]; then
  104. ACTION="restart"
  105. else
  106. if [ -x "${SERVICEDIR}/${SERVICE}" ]; then
  107. env -i LANG="$LANG" LANGUAGE="$LANGUAGE" LC_CTYPE="$LC_CTYPE" LC_NUMERIC="$LC_NUMERIC" LC_TIME="$LC_TIME" LC_COLLATE="$LC_COLLATE" LC_MONETARY="$LC_MONETARY" LC_MESSAGES="$LC_MESSAGES" LC_PAPER="$LC_PAPER" LC_NAME="$LC_NAME" LC_ADDRESS="$LC_ADDRESS" LC_TELEPHONE="$LC_TELEPHONE" LC_MEASUREMENT="$LC_MEASUREMENT" LC_IDENTIFICATION="$LC_IDENTIFICATION" LC_ALL="$LC_ALL" PATH="$PATH" TERM="$TERM" "$SERVICEDIR/$SERVICE" stop
  108. env -i LANG="$LANG" LANGUAGE="$LANGUAGE" LC_CTYPE="$LC_CTYPE" LC_NUMERIC="$LC_NUMERIC" LC_TIME="$LC_TIME" LC_COLLATE="$LC_COLLATE" LC_MONETARY="$LC_MONETARY" LC_MESSAGES="$LC_MESSAGES" LC_PAPER="$LC_PAPER" LC_NAME="$LC_NAME" LC_ADDRESS="$LC_ADDRESS" LC_TELEPHONE="$LC_TELEPHONE" LC_MEASUREMENT="$LC_MEASUREMENT" LC_IDENTIFICATION="$LC_IDENTIFICATION" LC_ALL="$LC_ALL" PATH="$PATH" TERM="$TERM" "$SERVICEDIR/$SERVICE" start
  109. exit $?
  110. fi
  111. fi
  112. elif [ -z "${SERVICE}" ]; then
  113. SERVICE="${1}"
  114. elif [ -z "${ACTION}" ]; then
  115. ACTION="${1}"
  116. else
  117. OPTIONS="${OPTIONS} ${1}"
  118. fi
  119. shift
  120. ;;
  121. esac
  122. done
  123. run_via_sysvinit() {
  124. # Otherwise, use the traditional sysvinit
  125. if [ -x "${SERVICEDIR}/${SERVICE}" ]; then
  126. exec env -i LANG="$LANG" LANGUAGE="$LANGUAGE" LC_CTYPE="$LC_CTYPE" LC_NUMERIC="$LC_NUMERIC" LC_TIME="$LC_TIME" LC_COLLATE="$LC_COLLATE" LC_MONETARY="$LC_MONETARY" LC_MESSAGES="$LC_MESSAGES" LC_PAPER="$LC_PAPER" LC_NAME="$LC_NAME" LC_ADDRESS="$LC_ADDRESS" LC_TELEPHONE="$LC_TELEPHONE" LC_MEASUREMENT="$LC_MEASUREMENT" LC_IDENTIFICATION="$LC_IDENTIFICATION" LC_ALL="$LC_ALL" PATH="$PATH" TERM="$TERM" "$SERVICEDIR/$SERVICE" ${ACTION} ${OPTIONS}
  127. else
  128. echo "${SERVICE}: unrecognized service" >&2
  129. exit 1
  130. fi
  131. }
  132. update_openrc_started_symlinks() {
  133. # maintain the symlinks of /run/openrc/started so that
  134. # rc-status works with the service command as well
  135. if [ -d /run/openrc/started ] ; then
  136. case "${ACTION}" in
  137. start)
  138. if [ ! -h /run/openrc/started/$SERVICE ] ; then
  139. ln -s $SERVICEDIR/$SERVICE /run/openrc/started/$SERVICE || true
  140. fi
  141. ;;
  142. stop)
  143. rm /run/openrc/started/$SERVICE || true
  144. ;;
  145. esac
  146. fi
  147. }
  148. # When this machine is running systemd, standard service calls are turned into
  149. # systemctl calls.
  150. if [ -n "$is_systemd" ]
  151. then
  152. UNIT="${SERVICE%.sh}.service"
  153. # avoid deadlocks during bootup and shutdown from units/hooks
  154. # which call "invoke-rc.d service reload" and similar, since
  155. # the synchronous wait plus systemd's normal behaviour of
  156. # transactionally processing all dependencies first easily
  157. # causes dependency loops
  158. if ! systemctl --quiet is-active multi-user.target; then
  159. sctl_args="--job-mode=ignore-dependencies"
  160. fi
  161. case "${ACTION}" in
  162. restart|status|try-restart)
  163. exec systemctl $sctl_args ${ACTION} ${UNIT}
  164. ;;
  165. start|stop)
  166. # Follow the principle of least surprise for SysV people:
  167. # When running "service foo stop" and foo happens to be a service that
  168. # has one or more .socket files, we also stop the .socket units.
  169. # Users who need more control will use systemctl directly.
  170. for unit in $(systemctl list-unit-files --full --type=socket 2>/dev/null | sed -ne 's/\.socket\s*[a-z]*\s*$/.socket/p'); do
  171. if [ "$(systemctl -p Triggers show $unit)" = "Triggers=${UNIT}" ]; then
  172. systemctl $sctl_args ${ACTION} $unit
  173. fi
  174. done
  175. exec systemctl $sctl_args ${ACTION} ${UNIT}
  176. ;;
  177. reload)
  178. _canreload="$(systemctl -p CanReload show ${UNIT} 2>/dev/null)"
  179. if [ "$_canreload" = "CanReload=no" ]; then
  180. # The reload action falls back to the sysv init script just in case
  181. # the systemd service file does not (yet) support reload for a
  182. # specific service.
  183. run_via_sysvinit
  184. else
  185. exec systemctl $sctl_args reload "${UNIT}"
  186. fi
  187. ;;
  188. force-stop)
  189. exec systemctl --signal=KILL kill "${UNIT}"
  190. ;;
  191. force-reload)
  192. _canreload="$(systemctl -p CanReload show ${UNIT} 2>/dev/null)"
  193. if [ "$_canreload" = "CanReload=no" ]; then
  194. exec systemctl $sctl_args restart "${UNIT}"
  195. else
  196. exec systemctl $sctl_args reload "${UNIT}"
  197. fi
  198. ;;
  199. *)
  200. # We try to run non-standard actions by running
  201. # the init script directly.
  202. run_via_sysvinit
  203. ;;
  204. esac
  205. fi
  206. update_openrc_started_symlinks
  207. run_via_sysvinit