procd.sh 9.4 KB

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