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