f6c5e97216c13803d00e2cabfc38c6a563e41214
[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         [ -n "$TRACE_SYSCALLS" ] && json_add_boolean trace "1"
116 }
117
118 _procd_open_trigger() {
119         json_add_array "triggers"
120 }
121
122 _procd_open_validate() {
123         json_add_array "validate"
124 }
125
126 _procd_add_jail() {
127         json_add_object "jail"
128         json_add_string name "$1"
129         json_add_string root "/tmp/.jail/$1"
130
131         shift
132         
133         for a in $@; do
134                 case $a in
135                 log)    json_add_boolean "log" "1";;
136                 ubus)   json_add_boolean "ubus" "1";;
137                 procfs) json_add_boolean "procfs" "1";;
138                 sysfs)  json_add_boolean "sysfs" "1";;
139                 esac
140         done
141         json_add_object "mount"
142         json_close_object
143         json_close_object
144 }
145
146 _procd_add_jail_mount() {
147         local _json_no_warning=1
148
149         json_select "jail"
150         [ $? = 0 ] || return
151         json_select "mount"
152         [ $? = 0 ] || {
153                 json_select ..
154                 return
155         }
156         for a in $@; do
157                 json_add_string "$a" "0"
158         done
159         json_select ..
160         json_select ..
161 }
162
163 _procd_add_jail_mount_rw() {
164         local _json_no_warning=1
165
166         json_select "jail"
167         [ $? = 0 ] || return
168         json_select "mount"
169         [ $? = 0 ] || {
170                 json_select ..
171                 return
172         }
173         for a in $@; do
174                 json_add_string "$a" "1"
175         done
176         json_select ..
177         json_select ..
178 }
179
180 _procd_set_param() {
181         local type="$1"; shift
182
183         case "$type" in
184                 env|data|limits)
185                         _procd_add_table "$type" "$@"
186                 ;;
187                 command|netdev|file|respawn|watch)
188                         _procd_add_array "$type" "$@"
189                 ;;
190                 error)
191                         json_add_array "$type"
192                         json_add_string "" "$@"
193                         json_close_array
194                 ;;
195                 nice)
196                         json_add_int "$type" "$1"
197                 ;;
198                 user|seccomp)
199                         json_add_string "$type" "$1"
200                 ;;
201                 stdout|stderr)
202                         json_add_boolean "$type" "$1"
203                 ;;
204         esac
205 }
206
207 _procd_add_interface_trigger() {
208         json_add_array
209         _procd_add_array_data "$1"
210         shift
211
212         json_add_array
213         _procd_add_array_data "if"
214
215         json_add_array
216         _procd_add_array_data "eq" "interface" "$1"
217         shift
218         json_close_array
219
220         json_add_array
221         _procd_add_array_data "run_script" "$@"
222         json_close_array
223
224         json_close_array
225
226         json_close_array
227 }
228
229 _procd_add_reload_interface_trigger() {
230         local script=$(readlink "$initscript")
231         local name=$(basename ${script:-$initscript})
232
233         _procd_open_trigger
234         _procd_add_interface_trigger "interface.*" $1 /etc/init.d/$name reload
235         _procd_close_trigger
236 }
237
238 _procd_add_config_trigger() {
239         json_add_array
240         _procd_add_array_data "$1"
241         shift
242
243         json_add_array
244         _procd_add_array_data "if"
245
246         json_add_array
247         _procd_add_array_data "eq" "package" "$1"
248         shift
249         json_close_array
250
251         json_add_array
252         _procd_add_array_data "run_script" "$@"
253         json_close_array
254
255         json_close_array
256
257         json_close_array
258 }
259
260 _procd_add_raw_trigger() {
261         json_add_array
262         _procd_add_array_data "$1"
263         shift
264         local timeout=$1
265         shift
266
267         json_add_array
268         json_add_array
269         _procd_add_array_data "run_script" "$@"
270         json_close_array
271         json_close_array
272
273         json_add_int "" "$timeout"
274
275         json_close_array
276 }
277
278 _procd_add_reload_trigger() {
279         local script=$(readlink "$initscript")
280         local name=$(basename ${script:-$initscript})
281         local file
282
283         _procd_open_trigger
284         for file in "$@"; do
285                 _procd_add_config_trigger "config.change" "$file" /etc/init.d/$name reload
286         done
287         _procd_close_trigger
288 }
289
290 _procd_add_validation() {
291         _procd_open_validate
292         $@
293         _procd_close_validate
294 }
295
296 _procd_append_param() {
297         local type="$1"; shift
298         local _json_no_warning=1
299
300         json_select "$type"
301         [ $? = 0 ] || {
302                 _procd_set_param "$type" "$@"
303                 return
304         }
305         case "$type" in
306                 env|data|limits)
307                         _procd_add_table_data "$@"
308                 ;;
309                 command|netdev|file|respawn|watch)
310                         _procd_add_array_data "$@"
311                 ;;
312                 error)
313                         json_add_string "" "$@"
314                 ;;
315         esac
316         json_select ..
317 }
318
319 _procd_close_instance() {
320         json_close_object
321 }
322
323 _procd_close_trigger() {
324         json_close_array
325 }
326
327 _procd_close_validate() {
328         json_close_array
329 }
330
331 _procd_add_instance() {
332         _procd_open_instance
333         _procd_set_param command "$@"
334         _procd_close_instance
335 }
336
337 _procd_kill() {
338         local service="$1"
339         local instance="$2"
340
341         json_init
342         [ -n "$service" ] && json_add_string name "$service"
343         [ -n "$instance" ] && json_add_string instance "$instance"
344         _procd_ubus_call delete
345 }
346
347 procd_open_data() {
348         local name="$1"
349         json_set_namespace procd __procd_old_cb
350         json_add_object data
351 }
352
353 procd_close_data() {
354         json_close_object
355         json_set_namespace $__procd_old_cb
356 }
357
358 _procd_set_config_changed() {
359         local package="$1"
360
361         json_init
362         json_add_string type config.change
363         json_add_object data
364         json_add_string package "$package"
365         json_close_object
366
367         ubus call service event "$(json_dump)"
368 }
369
370 procd_add_mdns_service() {
371         local service proto port
372         service=$1; shift
373         proto=$1; shift
374         port=$1; shift
375         json_add_object "${service}_$port"
376         json_add_string "service" "_$service._$proto.local"
377         json_add_int port "$port"
378         [ -n "$1" ] && {
379                 json_add_array txt
380                 for txt in $@; do json_add_string "" $txt; done
381                 json_select ..
382         }
383         json_select ..
384 }
385
386 procd_add_mdns() {
387         procd_open_data
388         json_add_object "mdns"
389         procd_add_mdns_service $@
390         json_close_object
391         procd_close_data
392 }
393
394 uci_validate_section()
395 {
396         local _package="$1"
397         local _type="$2"
398         local _name="$3"
399         local _result
400         local _error
401         shift; shift; shift
402         _result=`/sbin/validate_data "$_package" "$_type" "$_name" "$@" 2> /dev/null`
403         _error=$?
404         eval "$_result"
405         [ "$_error" = "0" ] || `/sbin/validate_data "$_package" "$_type" "$_name" "$@" 1> /dev/null`
406         return $_error
407 }
408
409 _procd_wrapper \
410         procd_open_service \
411         procd_close_service \
412         procd_add_instance \
413         procd_add_raw_trigger \
414         procd_add_config_trigger \
415         procd_add_interface_trigger \
416         procd_add_reload_trigger \
417         procd_add_reload_interface_trigger \
418         procd_add_interface_reload \
419         procd_open_trigger \
420         procd_close_trigger \
421         procd_open_instance \
422         procd_close_instance \
423         procd_open_validate \
424         procd_close_validate \
425         procd_add_jail \
426         procd_add_jail_mount \
427         procd_add_jail_mount_rw \
428         procd_set_param \
429         procd_append_param \
430         procd_add_validation \
431         procd_set_config_changed \
432         procd_kill