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