2
0

procd.sh 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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_retry=$(uci_get system.@service[0].respawn_retry)
  302. _procd_add_array_data 3600 5 ${respawn_retry:-5}
  303. fi
  304. json_select ..
  305. fi
  306. json_close_object
  307. }
  308. _procd_add_instance() {
  309. _procd_open_instance
  310. _procd_set_param command "$@"
  311. _procd_close_instance
  312. }
  313. _procd_kill() {
  314. local service="$1"
  315. local instance="$2"
  316. json_init
  317. [ -n "$service" ] && json_add_string name "$service"
  318. [ -n "$instance" ] && json_add_string instance "$instance"
  319. _procd_ubus_call delete
  320. }
  321. _procd_send_signal() {
  322. local service="$1"
  323. local instance="$2"
  324. local signal="$3"
  325. json_init
  326. json_add_string name "$service"
  327. [ -n "$instance" -a "$instance" != "*" ] && json_add_string instance "$instance"
  328. [ -n "$signal" ] && json_add_int signal "$signal"
  329. _procd_ubus_call signal
  330. }
  331. procd_open_data() {
  332. local name="$1"
  333. json_set_namespace procd __procd_old_cb
  334. json_add_object data
  335. }
  336. procd_close_data() {
  337. json_close_object
  338. json_set_namespace $__procd_old_cb
  339. }
  340. _procd_set_config_changed() {
  341. local package="$1"
  342. json_init
  343. json_add_string type config.change
  344. json_add_object data
  345. json_add_string package "$package"
  346. json_close_object
  347. ubus call service event "$(json_dump)"
  348. }
  349. procd_add_mdns_service() {
  350. local service proto port
  351. service=$1; shift
  352. proto=$1; shift
  353. port=$1; shift
  354. json_add_object "${service}_$port"
  355. json_add_string "service" "_$service._$proto.local"
  356. json_add_int port "$port"
  357. [ -n "$1" ] && {
  358. json_add_array txt
  359. for txt in $@; do json_add_string "" $txt; done
  360. json_select ..
  361. }
  362. json_select ..
  363. }
  364. procd_add_mdns() {
  365. procd_open_data
  366. json_add_object "mdns"
  367. procd_add_mdns_service $@
  368. json_close_object
  369. procd_close_data
  370. }
  371. uci_validate_section()
  372. {
  373. local _package="$1"
  374. local _type="$2"
  375. local _name="$3"
  376. local _result
  377. local _error
  378. shift; shift; shift
  379. _result=`/sbin/validate_data "$_package" "$_type" "$_name" "$@" 2> /dev/null`
  380. _error=$?
  381. eval "$_result"
  382. [ "$_error" = "0" ] || `/sbin/validate_data "$_package" "$_type" "$_name" "$@" 1> /dev/null`
  383. return $_error
  384. }
  385. _procd_wrapper \
  386. procd_open_service \
  387. procd_close_service \
  388. procd_add_instance \
  389. procd_add_raw_trigger \
  390. procd_add_config_trigger \
  391. procd_add_interface_trigger \
  392. procd_add_reload_trigger \
  393. procd_add_reload_interface_trigger \
  394. procd_open_trigger \
  395. procd_close_trigger \
  396. procd_open_instance \
  397. procd_close_instance \
  398. procd_open_validate \
  399. procd_close_validate \
  400. procd_add_jail \
  401. procd_add_jail_mount \
  402. procd_add_jail_mount_rw \
  403. procd_set_param \
  404. procd_append_param \
  405. procd_add_validation \
  406. procd_set_config_changed \
  407. procd_kill \
  408. procd_send_signal