40-systemd 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # -*-Shell-script-*-
  2. # /lib/lsb/init-functions
  3. _use_systemctl=0
  4. if [ -d /run/systemd/system ]; then
  5. if [ -n "${__init_d_script_name:-}" ]; then # scripts run with new init-d-script
  6. executable="$__init_d_script_name"
  7. argument="$1"
  8. elif [ "${0##*/}" = "init-d-script" ] ||
  9. [ "${0##*/}" = "${1:-}" ]; then # scripts run with old init-d-script
  10. executable="$1"
  11. argument="$2"
  12. else # plain old scripts
  13. executable="$0"
  14. argument="${1:-}"
  15. fi
  16. prog=${executable##*/}
  17. service="${prog%.sh}.service"
  18. # Don't try to run masked services. systemctl <= 230 always succeeds here,
  19. # but later systemctls fail on nonexisting units; be compatible with both
  20. state=$(systemctl -p LoadState --value show $service 2>/dev/null) || state="not-found"
  21. [ "$state" = "masked" ] && exit 0
  22. # Redirect SysV init scripts when executed by the user
  23. if [ $PPID -ne 1 ] && [ -z "${SYSTEMCTL_SKIP_REDIRECT:-}" ]; then
  24. case $(readlink -f "$executable") in
  25. /etc/init.d/*)
  26. # If the state is not-found, this might be a newly installed SysV init
  27. # script where systemd-sysv-generator has not been run yet.
  28. [ "$state" != "not-found" ] || [ "$(id -u)" != 0 ] || systemctl --no-ask-password daemon-reload
  29. _use_systemctl=1
  30. # Some services can't reload through the .service file,
  31. # but can through the init script.
  32. if [ "$(systemctl -p CanReload --value show $service 2>/dev/null)" = "no" ] && [ "${argument:-}" = "reload" ]; then
  33. _use_systemctl=0
  34. fi
  35. ;;
  36. esac
  37. fi
  38. fi
  39. systemctl_redirect () {
  40. local s
  41. local rc
  42. local prog=${1##*/}
  43. local command=$2
  44. case "$command" in
  45. start)
  46. s="Starting $prog (via systemctl)"
  47. ;;
  48. stop)
  49. s="Stopping $prog (via systemctl)"
  50. ;;
  51. reload|force-reload)
  52. s="Reloading $prog configuration (via systemctl)"
  53. ;;
  54. try-restart)
  55. s="Restarting $prog if running (via systemctl)"
  56. ;;
  57. restart)
  58. s="Restarting $prog (via systemctl)"
  59. ;;
  60. esac
  61. service="${prog%.sh}.service"
  62. # avoid deadlocks during bootup and shutdown from units/hooks
  63. # which call "invoke-rc.d service reload" and similar, since
  64. # the synchronous wait plus systemd's normal behaviour of
  65. # transactionally processing all dependencies first easily
  66. # causes dependency loops
  67. if ! OUT=$(systemctl is-system-running 2>/dev/null) && [ "$OUT" != "degraded" ]; then
  68. sctl_args="--job-mode=ignore-dependencies"
  69. fi
  70. [ "$command" = status ] || log_daemon_msg "$s" "$service"
  71. /bin/systemctl --no-pager $sctl_args $command "$service"
  72. rc=$?
  73. [ "$command" = status ] || log_end_msg $rc
  74. return $rc
  75. }
  76. if [ "$_use_systemctl" = "1" ]; then
  77. # Some init scripts use "set -e" and "set -u", we don't want that
  78. # here
  79. set +e
  80. set +u
  81. case "$argument" in
  82. start|stop|restart|reload|force-reload|try-restart|status)
  83. systemctl_redirect $executable $argument
  84. exit $?
  85. ;;
  86. esac
  87. fi