procd.sh 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. # procd API:
  2. #
  3. # procd_open_service(name, [script]):
  4. # Initialize a new procd command message containing a service with one or more instances
  5. #
  6. # procd_close_service()
  7. # Send the command message for the service
  8. #
  9. # procd_open_instance([name]):
  10. # Add an instance to the service described by the previous procd_open_service call
  11. #
  12. # procd_set_param(type, [value...])
  13. # Available types:
  14. # command: command line (array).
  15. # env: environment variable (passed to the process)
  16. # data: arbitrary name/value pairs for detecting config changes (table)
  17. # file: configuration files (array)
  18. # netdev: bound network device (detects ifindex changes)
  19. #
  20. # No space separation is done for arrays/tables - use one function argument per command line argument
  21. #
  22. # procd_close_instance():
  23. # Complete the instance being prepared
  24. #
  25. # procd_kill(service, [instance]):
  26. # Kill a service instance (or all instances)
  27. #
  28. . $IPKG_INSTROOT/usr/share/libubox/jshn.sh
  29. _PROCD_SERVICE=
  30. _procd_call() {
  31. local old_cb
  32. json_set_namespace procd old_cb
  33. "$@"
  34. json_set_namespace $old_cb
  35. }
  36. _procd_wrapper() {
  37. while [ -n "$1" ]; do
  38. eval "$1() { _procd_call _$1 \"\$@\"; }"
  39. shift
  40. done
  41. }
  42. _procd_ubus_call() {
  43. local cmd="$1"
  44. ubus call service "$cmd" "$(json_dump)"
  45. json_cleanup
  46. }
  47. _procd_open_service() {
  48. local name="$1"
  49. local script="$2"
  50. _PROCD_SERVICE="$name"
  51. _PROCD_INSTANCE_SEQ=0
  52. json_init
  53. json_add_string name "$name"
  54. [ -n "$script" ] && json_add_string script "$script"
  55. json_add_object instances
  56. }
  57. _procd_close_service() {
  58. json_close_object
  59. _procd_open_trigger
  60. service_triggers
  61. _procd_close_trigger
  62. _procd_ubus_call set
  63. }
  64. _procd_add_array_data() {
  65. while [ -n "$1" ]; do
  66. json_add_string "" "$1"
  67. shift
  68. done
  69. }
  70. _procd_add_array() {
  71. json_add_array "$1"
  72. shift
  73. _procd_add_array_data "$@"
  74. json_close_array
  75. }
  76. _procd_add_table_data() {
  77. while [ -n "$1" ]; do
  78. local var="${1%%=*}"
  79. local val="${1#*=}"
  80. [[ "$1" == "$val" ]] && val=
  81. json_add_string "$var" "$val"
  82. shift
  83. done
  84. }
  85. _procd_add_table() {
  86. json_add_object "$1"
  87. shift
  88. _procd_add_table_data "$@"
  89. json_close_object
  90. }
  91. _procd_open_instance() {
  92. local name="$1"; shift
  93. _PROCD_INSTANCE_SEQ="$(($_PROCD_INSTANCE_SEQ + 1))"
  94. name="${name:-instance$_PROCD_INSTANCE_SEQ}"
  95. json_add_object "$name"
  96. }
  97. _procd_open_trigger() {
  98. json_add_array "triggers"
  99. }
  100. _procd_set_param() {
  101. local type="$1"; shift
  102. case "$type" in
  103. env|data)
  104. _procd_add_table "$type" "$@"
  105. ;;
  106. command|netdev|file)
  107. _procd_add_array "$type" "$@"
  108. ;;
  109. nice)
  110. json_add_int "$type" "$1"
  111. ;;
  112. esac
  113. }
  114. _procd_add_config_trigger() {
  115. json_add_array
  116. _procd_add_array_data "config.change"
  117. json_add_array
  118. _procd_add_array_data "if"
  119. json_add_array
  120. _procd_add_array_data "eq" "package" "$1"
  121. shift
  122. json_close_array
  123. json_add_array
  124. _procd_add_array_data "run_script" "$@"
  125. json_close_array
  126. json_close_array
  127. json_close_array
  128. }
  129. _procd_append_param() {
  130. local type="$1"; shift
  131. json_select "$type"
  132. case "$type" in
  133. env|data)
  134. _procd_add_table_data "$@"
  135. ;;
  136. command|netdev|file)
  137. _procd_add_array_data "$@"
  138. ;;
  139. esac
  140. json_select ..
  141. }
  142. _procd_close_instance() {
  143. json_close_object
  144. }
  145. _procd_close_trigger() {
  146. json_close_array
  147. }
  148. _procd_add_instance() {
  149. _procd_open_instance
  150. _procd_set_command "$@"
  151. _procd_close_instance
  152. }
  153. _procd_kill() {
  154. local service="$1"
  155. local instance="$2"
  156. json_init
  157. [ -n "$service" ] && json_add_string name "$service"
  158. [ -n "$instance" ] && json_add_string instance "$instance"
  159. _procd_ubus_call delete
  160. }
  161. _procd_wrapper \
  162. procd_open_service \
  163. procd_close_service \
  164. procd_add_instance \
  165. procd_add_config_trigger \
  166. procd_open_trigger \
  167. procd_close_trigger \
  168. procd_open_instance \
  169. procd_close_instance \
  170. procd_set_param \
  171. procd_append_param \
  172. procd_kill