rc.common 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #!/bin/sh
  2. # Copyright (C) 2006-2012 OpenWrt.org
  3. . $IPKG_INSTROOT/lib/functions.sh
  4. . $IPKG_INSTROOT/lib/functions/service.sh
  5. initscript=$1
  6. action=${2:-help}
  7. shift 2
  8. start() {
  9. return 0
  10. }
  11. stop() {
  12. return 0
  13. }
  14. reload() {
  15. return 1
  16. }
  17. restart() {
  18. trap '' TERM
  19. stop "$@"
  20. start "$@"
  21. }
  22. boot() {
  23. start "$@"
  24. }
  25. shutdown() {
  26. stop
  27. }
  28. disable() {
  29. name="$(basename "${initscript}")"
  30. rm -f "$IPKG_INSTROOT"/etc/rc.d/S??$name
  31. rm -f "$IPKG_INSTROOT"/etc/rc.d/K??$name
  32. }
  33. enable() {
  34. name="$(basename "${initscript}")"
  35. disable
  36. [ -n "$START" -o -n "$STOP" ] || {
  37. echo "/etc/init.d/$name does not have a START or STOP value"
  38. return 1
  39. }
  40. [ "$START" ] && ln -s "../init.d/$name" "$IPKG_INSTROOT/etc/rc.d/S${START}${name##S[0-9][0-9]}"
  41. [ "$STOP" ] && ln -s "../init.d/$name" "$IPKG_INSTROOT/etc/rc.d/K${STOP}${name##K[0-9][0-9]}"
  42. }
  43. enabled() {
  44. name="$(basename "${initscript}")"
  45. [ -x "$IPKG_INSTROOT/etc/rc.d/S${START}${name##S[0-9][0-9]}" ]
  46. }
  47. depends() {
  48. return 0
  49. }
  50. help() {
  51. cat <<EOF
  52. Syntax: $initscript [command]
  53. Available commands:
  54. start Start the service
  55. stop Stop the service
  56. restart Restart the service
  57. reload Reload configuration files (or restart if that fails)
  58. enable Enable service autostart
  59. disable Disable service autostart
  60. $EXTRA_HELP
  61. EOF
  62. }
  63. # for procd
  64. start_service() {
  65. return 0
  66. }
  67. stop_service() {
  68. return 0
  69. }
  70. service_triggers() {
  71. return 0
  72. }
  73. service_running() {
  74. return 0
  75. }
  76. ${INIT_TRACE:+set -x}
  77. . "$initscript"
  78. [ -n "$USE_PROCD" ] && {
  79. EXTRA_COMMANDS="${EXTRA_COMMANDS} running trace"
  80. . $IPKG_INSTROOT/lib/functions/procd.sh
  81. basescript=$(readlink "$initscript")
  82. rc_procd() {
  83. local method="set"
  84. [ -n "$2" ] && method="add"
  85. procd_open_service "$(basename ${basescript:-$initscript})" "$initscript"
  86. "$@"
  87. procd_close_service "$method"
  88. }
  89. start() {
  90. rc_procd start_service "$@"
  91. if eval "type service_started" 2>/dev/null >/dev/null; then
  92. service_started
  93. fi
  94. }
  95. trace() {
  96. TRACE_SYSCALLS=1
  97. start "$@"
  98. }
  99. stop() {
  100. stop_service "$@"
  101. procd_kill "$(basename ${basescript:-$initscript})" "$1"
  102. }
  103. reload() {
  104. if eval "type reload_service" 2>/dev/null >/dev/null; then
  105. reload_service "$@"
  106. else
  107. start
  108. fi
  109. }
  110. running() {
  111. service_running "$@"
  112. }
  113. }
  114. ALL_COMMANDS="start stop reload restart boot shutdown enable disable enabled depends ${EXTRA_COMMANDS}"
  115. list_contains ALL_COMMANDS "$action" || action=help
  116. [ "$action" = "reload" ] && action='eval reload "$@" || restart "$@" && :'
  117. $action "$@"