rc.common 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. procd_open_service "$(basename ${basescript:-$initscript})" "$initscript"
  84. "$@"
  85. procd_close_service
  86. }
  87. start() {
  88. rc_procd start_service "$@"
  89. if eval "type service_started" 2>/dev/null >/dev/null; then
  90. service_started
  91. fi
  92. }
  93. trace() {
  94. TRACE_SYSCALLS=1
  95. start "$@"
  96. }
  97. stop() {
  98. stop_service "$@"
  99. procd_kill "$(basename ${basescript:-$initscript})" "$1"
  100. }
  101. reload() {
  102. if eval "type reload_service" 2>/dev/null >/dev/null; then
  103. reload_service "$@"
  104. else
  105. start
  106. fi
  107. }
  108. running() {
  109. service_running "$@"
  110. }
  111. }
  112. ALL_COMMANDS="start stop reload restart boot shutdown enable disable enabled depends ${EXTRA_COMMANDS}"
  113. list_contains ALL_COMMANDS "$action" || action=help
  114. [ "$action" = "reload" ] && action='eval reload "$@" || restart "$@" && :'
  115. $action "$@"