procd.sh 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. # respawn info: array with 3 values $fail_threshold $restart_timeout $max_fail
  16. # env: environment variable (passed to the process)
  17. # data: arbitrary name/value pairs for detecting config changes (table)
  18. # file: configuration files (array)
  19. # netdev: bound network device (detects ifindex changes)
  20. # limits: resource limits (passed to the process)
  21. # user info: array with 1 values $username
  22. # pidfile: file name to write pid into
  23. #
  24. # No space separation is done for arrays/tables - use one function argument per command line argument
  25. #
  26. # procd_close_instance():
  27. # Complete the instance being prepared
  28. #
  29. # procd_kill(service, [instance]):
  30. # Kill a service instance (or all instances)
  31. #
  32. . $IPKG_INSTROOT/usr/share/libubox/jshn.sh
  33. _PROCD_SERVICE=
  34. _procd_call() {
  35. local old_cb
  36. json_set_namespace procd old_cb
  37. "$@"
  38. json_set_namespace $old_cb
  39. }
  40. _procd_wrapper() {
  41. while [ -n "$1" ]; do
  42. eval "$1() { _procd_call _$1 \"\$@\"; }"
  43. shift
  44. done
  45. }
  46. _procd_ubus_call() {
  47. local cmd="$1"
  48. [ -n "$PROCD_DEBUG" ] && json_dump >&2
  49. ubus call service "$cmd" "$(json_dump)"
  50. json_cleanup
  51. }
  52. _procd_open_service() {
  53. local name="$1"
  54. local script="$2"
  55. _PROCD_SERVICE="$name"
  56. _PROCD_INSTANCE_SEQ=0
  57. json_init
  58. json_add_string name "$name"
  59. [ -n "$script" ] && json_add_string script "$script"
  60. json_add_object instances
  61. }
  62. _procd_close_service() {
  63. json_close_object
  64. service_triggers
  65. _procd_ubus_call set
  66. }
  67. _procd_add_array_data() {
  68. while [ "$#" -gt 0 ]; do
  69. json_add_string "" "$1"
  70. shift
  71. done
  72. }
  73. _procd_add_array() {
  74. json_add_array "$1"
  75. shift
  76. _procd_add_array_data "$@"
  77. json_close_array
  78. }
  79. _procd_add_table_data() {
  80. while [ -n "$1" ]; do
  81. local var="${1%%=*}"
  82. local val="${1#*=}"
  83. [ "$1" = "$val" ] && val=
  84. json_add_string "$var" "$val"
  85. shift
  86. done
  87. }
  88. _procd_add_table() {
  89. json_add_object "$1"
  90. shift
  91. _procd_add_table_data "$@"
  92. json_close_object
  93. }
  94. _procd_open_instance() {
  95. local name="$1"; shift
  96. _PROCD_INSTANCE_SEQ="$(($_PROCD_INSTANCE_SEQ + 1))"
  97. name="${name:-instance$_PROCD_INSTANCE_SEQ}"
  98. json_add_object "$name"
  99. [ -n "$TRACE_SYSCALLS" ] && json_add_boolean trace "1"
  100. }
  101. _procd_open_trigger() {
  102. json_add_array "triggers"
  103. }
  104. _procd_open_validate() {
  105. json_add_array "validate"
  106. }
  107. _procd_add_jail() {
  108. json_add_object "jail"
  109. json_add_string name "$1"
  110. shift
  111. for a in $@; do
  112. case $a in
  113. log) json_add_boolean "log" "1";;
  114. ubus) json_add_boolean "ubus" "1";;
  115. procfs) json_add_boolean "procfs" "1";;
  116. sysfs) json_add_boolean "sysfs" "1";;
  117. ronly) json_add_boolean "ronly" "1";;
  118. esac
  119. done
  120. json_add_object "mount"
  121. json_close_object
  122. json_close_object
  123. }
  124. _procd_add_jail_mount() {
  125. local _json_no_warning=1
  126. json_select "jail"
  127. [ $? = 0 ] || return
  128. json_select "mount"
  129. [ $? = 0 ] || {
  130. json_select ..
  131. return
  132. }
  133. for a in $@; do
  134. json_add_string "$a" "0"
  135. done
  136. json_select ..
  137. json_select ..
  138. }
  139. _procd_add_jail_mount_rw() {
  140. local _json_no_warning=1
  141. json_select "jail"
  142. [ $? = 0 ] || return
  143. json_select "mount"
  144. [ $? = 0 ] || {
  145. json_select ..
  146. return
  147. }
  148. for a in $@; do
  149. json_add_string "$a" "1"
  150. done
  151. json_select ..
  152. json_select ..
  153. }
  154. _procd_set_param() {
  155. local type="$1"; shift
  156. case "$type" in
  157. env|data|limits)
  158. _procd_add_table "$type" "$@"
  159. ;;
  160. command|netdev|file|respawn|watch)
  161. _procd_add_array "$type" "$@"
  162. ;;
  163. error)
  164. json_add_array "$type"
  165. json_add_string "" "$@"
  166. json_close_array
  167. ;;
  168. nice)
  169. json_add_int "$type" "$1"
  170. ;;
  171. pidfile|user|seccomp|capabilities)
  172. json_add_string "$type" "$1"
  173. ;;
  174. stdout|stderr|no_new_privs)
  175. json_add_boolean "$type" "$1"
  176. ;;
  177. esac
  178. }
  179. _procd_add_interface_trigger() {
  180. json_add_array
  181. _procd_add_array_data "$1"
  182. shift
  183. json_add_array
  184. _procd_add_array_data "if"
  185. json_add_array
  186. _procd_add_array_data "eq" "interface" "$1"
  187. shift
  188. json_close_array
  189. json_add_array
  190. _procd_add_array_data "run_script" "$@"
  191. json_close_array
  192. json_close_array
  193. json_close_array
  194. }
  195. _procd_add_reload_interface_trigger() {
  196. local script=$(readlink "$initscript")
  197. local name=$(basename ${script:-$initscript})
  198. _procd_open_trigger
  199. _procd_add_interface_trigger "interface.*" $1 /etc/init.d/$name reload
  200. _procd_close_trigger
  201. }
  202. _procd_add_config_trigger() {
  203. json_add_array
  204. _procd_add_array_data "$1"
  205. shift
  206. json_add_array
  207. _procd_add_array_data "if"
  208. json_add_array
  209. _procd_add_array_data "eq" "package" "$1"
  210. shift
  211. json_close_array
  212. json_add_array
  213. _procd_add_array_data "run_script" "$@"
  214. json_close_array
  215. json_close_array
  216. json_close_array
  217. }
  218. _procd_add_raw_trigger() {
  219. json_add_array
  220. _procd_add_array_data "$1"
  221. shift
  222. local timeout=$1
  223. shift
  224. json_add_array
  225. json_add_array
  226. _procd_add_array_data "run_script" "$@"
  227. json_close_array
  228. json_close_array
  229. json_add_int "" "$timeout"
  230. json_close_array
  231. }
  232. _procd_add_reload_trigger() {
  233. local script=$(readlink "$initscript")
  234. local name=$(basename ${script:-$initscript})
  235. local file
  236. _procd_open_trigger
  237. for file in "$@"; do
  238. _procd_add_config_trigger "config.change" "$file" /etc/init.d/$name reload
  239. done
  240. _procd_close_trigger
  241. }
  242. _procd_add_validation() {
  243. _procd_open_validate
  244. $@
  245. _procd_close_validate
  246. }
  247. _procd_append_param() {
  248. local type="$1"; shift
  249. local _json_no_warning=1
  250. json_select "$type"
  251. [ $? = 0 ] || {
  252. _procd_set_param "$type" "$@"
  253. return
  254. }
  255. case "$type" in
  256. env|data|limits)
  257. _procd_add_table_data "$@"
  258. ;;
  259. command|netdev|file|respawn|watch)
  260. _procd_add_array_data "$@"
  261. ;;
  262. error)
  263. json_add_string "" "$@"
  264. ;;
  265. esac
  266. json_select ..
  267. }
  268. _procd_close_instance() {
  269. local respawn_vals
  270. _json_no_warning=1
  271. if json_select respawn ; then
  272. json_get_values respawn_vals
  273. if [ -z "$respawn_vals" ]; then
  274. local respawn_retry=$(uci_get system.@service[0].respawn_retry)
  275. _procd_add_array_data 3600 5 ${respawn_retry:-5}
  276. fi
  277. json_select ..
  278. fi
  279. json_close_object
  280. }
  281. _procd_close_trigger() {
  282. json_close_array
  283. }
  284. _procd_close_validate() {
  285. json_close_array
  286. }
  287. _procd_add_instance() {
  288. _procd_open_instance
  289. _procd_set_param command "$@"
  290. _procd_close_instance
  291. }
  292. _procd_kill() {
  293. local service="$1"
  294. local instance="$2"
  295. json_init
  296. [ -n "$service" ] && json_add_string name "$service"
  297. [ -n "$instance" ] && json_add_string instance "$instance"
  298. _procd_ubus_call delete
  299. }
  300. procd_open_data() {
  301. local name="$1"
  302. json_set_namespace procd __procd_old_cb
  303. json_add_object data
  304. }
  305. procd_close_data() {
  306. json_close_object
  307. json_set_namespace $__procd_old_cb
  308. }
  309. _procd_set_config_changed() {
  310. local package="$1"
  311. json_init
  312. json_add_string type config.change
  313. json_add_object data
  314. json_add_string package "$package"
  315. json_close_object
  316. ubus call service event "$(json_dump)"
  317. }
  318. procd_add_mdns_service() {
  319. local service proto port
  320. service=$1; shift
  321. proto=$1; shift
  322. port=$1; shift
  323. json_add_object "${service}_$port"
  324. json_add_string "service" "_$service._$proto.local"
  325. json_add_int port "$port"
  326. [ -n "$1" ] && {
  327. json_add_array txt
  328. for txt in $@; do json_add_string "" $txt; done
  329. json_select ..
  330. }
  331. json_select ..
  332. }
  333. procd_add_mdns() {
  334. procd_open_data
  335. json_add_object "mdns"
  336. procd_add_mdns_service $@
  337. json_close_object
  338. procd_close_data
  339. }
  340. uci_validate_section()
  341. {
  342. local _package="$1"
  343. local _type="$2"
  344. local _name="$3"
  345. local _result
  346. local _error
  347. shift; shift; shift
  348. _result=`/sbin/validate_data "$_package" "$_type" "$_name" "$@" 2> /dev/null`
  349. _error=$?
  350. eval "$_result"
  351. [ "$_error" = "0" ] || `/sbin/validate_data "$_package" "$_type" "$_name" "$@" 1> /dev/null`
  352. return $_error
  353. }
  354. _procd_wrapper \
  355. procd_open_service \
  356. procd_close_service \
  357. procd_add_instance \
  358. procd_add_raw_trigger \
  359. procd_add_config_trigger \
  360. procd_add_interface_trigger \
  361. procd_add_reload_trigger \
  362. procd_add_reload_interface_trigger \
  363. procd_open_trigger \
  364. procd_close_trigger \
  365. procd_open_instance \
  366. procd_close_instance \
  367. procd_open_validate \
  368. procd_close_validate \
  369. procd_add_jail \
  370. procd_add_jail_mount \
  371. procd_add_jail_mount_rw \
  372. procd_set_param \
  373. procd_append_param \
  374. procd_add_validation \
  375. procd_set_config_changed \
  376. procd_kill