rc.common 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. err=1
  36. name="$(basename "${initscript}")"
  37. [ "$START" ] && \
  38. ln -sf "../init.d/$name" "$IPKG_INSTROOT/etc/rc.d/S${START}${name##S[0-9][0-9]}" && \
  39. err=0
  40. [ "$STOP" ] && \
  41. ln -sf "../init.d/$name" "$IPKG_INSTROOT/etc/rc.d/K${STOP}${name##K[0-9][0-9]}" && \
  42. err=0
  43. return $err
  44. }
  45. enabled() {
  46. name="$(basename "${initscript}")"
  47. name="${name##[SK][0-9][0-9]}"
  48. {
  49. [ -z "${START:-}" ] || [ -L "$IPKG_INSTROOT/etc/rc.d/S${START}$name" ]
  50. } && {
  51. [ -z "${STOP:-}" ] || [ -L "$IPKG_INSTROOT/etc/rc.d/K${STOP}$name" ]
  52. }
  53. }
  54. depends() {
  55. return 0
  56. }
  57. ALL_HELP=""
  58. ALL_COMMANDS="boot shutdown depends"
  59. extra_command() {
  60. local cmd="$1"
  61. local help="$2"
  62. local extra="$(printf "%-16s%s" "${cmd}" "${help}")"
  63. ALL_HELP="${ALL_HELP}\t${extra}\n"
  64. ALL_COMMANDS="${ALL_COMMANDS} ${cmd}"
  65. }
  66. help() {
  67. cat <<EOF
  68. Syntax: $initscript [command]
  69. Available commands:
  70. EOF
  71. echo -e "$ALL_HELP"
  72. }
  73. # for procd
  74. start_service() {
  75. return 0
  76. }
  77. stop_service() {
  78. return 0
  79. }
  80. service_triggers() {
  81. return 0
  82. }
  83. service_data() {
  84. return 0
  85. }
  86. service_running() {
  87. local instance="${1:-*}"
  88. procd_running "$(basename $initscript)" "$instance"
  89. }
  90. ${INIT_TRACE:+set -x}
  91. extra_command "start" "Start the service"
  92. extra_command "stop" "Stop the service"
  93. extra_command "restart" "Restart the service"
  94. extra_command "reload" "Reload configuration files (or restart if service does not implement reload)"
  95. extra_command "enable" "Enable service autostart"
  96. extra_command "disable" "Disable service autostart"
  97. extra_command "enabled" "Check if service is started on boot"
  98. . "$initscript"
  99. [ -n "$USE_PROCD" ] && {
  100. extra_command "running" "Check if service is running"
  101. extra_command "status" "Service status"
  102. extra_command "trace" "Start with syscall trace"
  103. extra_command "info" "Dump procd service info"
  104. . $IPKG_INSTROOT/lib/functions/procd.sh
  105. basescript=$(readlink "$initscript")
  106. rc_procd() {
  107. local method="set"
  108. [ -n "$2" ] && method="add"
  109. procd_open_service "$(basename ${basescript:-$initscript})" "$initscript"
  110. "$@"
  111. procd_close_service "$method"
  112. }
  113. start() {
  114. rc_procd start_service "$@"
  115. if eval "type service_started" 2>/dev/null >/dev/null; then
  116. service_started
  117. fi
  118. }
  119. trace() {
  120. TRACE_SYSCALLS=1
  121. start "$@"
  122. }
  123. info() {
  124. json_init
  125. json_add_string name "$(basename ${basescript:-$initscript})"
  126. json_add_boolean verbose "1"
  127. _procd_ubus_call list
  128. }
  129. stop() {
  130. procd_lock
  131. stop_service "$@"
  132. procd_kill "$(basename ${basescript:-$initscript})" "$1"
  133. if eval "type service_stopped" 2>/dev/null >/dev/null; then
  134. service_stopped
  135. fi
  136. }
  137. reload() {
  138. if eval "type reload_service" 2>/dev/null >/dev/null; then
  139. procd_lock
  140. reload_service "$@"
  141. else
  142. start
  143. fi
  144. }
  145. running() {
  146. service_running "$@"
  147. }
  148. status() {
  149. if eval "type status_service" 2>/dev/null >/dev/null; then
  150. status_service "$@"
  151. else
  152. _procd_status "$(basename ${basescript:-$initscript})" "$1"
  153. fi
  154. }
  155. }
  156. ALL_COMMANDS="${ALL_COMMANDS} ${EXTRA_COMMANDS}"
  157. ALL_HELP="${ALL_HELP}${EXTRA_HELP}"
  158. list_contains ALL_COMMANDS "$action" || action=help
  159. $action "$@"