procd: update to latest git
[openwrt/svn-archive/archive.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 esac
147 }
148
149 _procd_add_interface_trigger() {
150 json_add_array
151 _procd_add_array_data "$1"
152 shift
153
154 json_add_array
155 _procd_add_array_data "if"
156
157 json_add_array
158 _procd_add_array_data "eq" "interface" "$1"
159 shift
160 json_close_array
161
162 json_add_array
163 _procd_add_array_data "run_script" "$@"
164 json_close_array
165
166 json_close_array
167
168 json_close_array
169 }
170
171 _procd_add_reload_interface_trigger() {
172 local script=$(readlink "$initscript")
173 local name=$(basename ${script:-$initscript})
174
175 _procd_open_trigger
176 _procd_add_interface_trigger "interface.*" $1 /etc/init.d/$name reload
177 _procd_close_trigger
178 }
179
180 _procd_add_config_trigger() {
181 json_add_array
182 _procd_add_array_data "$1"
183 shift
184
185 json_add_array
186 _procd_add_array_data "if"
187
188 json_add_array
189 _procd_add_array_data "eq" "package" "$1"
190 shift
191 json_close_array
192
193 json_add_array
194 _procd_add_array_data "run_script" "$@"
195 json_close_array
196
197 json_close_array
198
199 json_close_array
200 }
201
202 _procd_add_raw_trigger() {
203 json_add_array
204 _procd_add_array_data "$1"
205 shift
206 local timeout=$1
207 shift
208
209 json_add_array
210 json_add_array
211 _procd_add_array_data "run_script" "$@"
212 json_close_array
213 json_close_array
214
215 json_add_int "" "$timeout"
216
217 json_close_array
218 }
219
220 _procd_add_reload_trigger() {
221 local script=$(readlink "$initscript")
222 local name=$(basename ${script:-$initscript})
223 local file
224
225 _procd_open_trigger
226 for file in "$@"; do
227 _procd_add_config_trigger "config.change" "$file" /etc/init.d/$name reload
228 done
229 _procd_close_trigger
230 }
231
232 _procd_add_validation() {
233 _procd_open_validate
234 $@
235 _procd_close_validate
236 }
237
238 _procd_append_param() {
239 local type="$1"; shift
240 local _json_no_warning=1
241
242 json_select "$type"
243 [ $? = 0 ] || {
244 _procd_set_param "$type" "$@"
245 return
246 }
247 case "$type" in
248 env|data|limits)
249 _procd_add_table_data "$@"
250 ;;
251 command|netdev|file|respawn|watch)
252 _procd_add_array_data "$@"
253 ;;
254 error)
255 json_add_string "" "$@"
256 ;;
257 esac
258 json_select ..
259 }
260
261 _procd_close_instance() {
262 json_close_object
263 }
264
265 _procd_close_trigger() {
266 json_close_array
267 }
268
269 _procd_close_validate() {
270 json_close_array
271 }
272
273 _procd_add_instance() {
274 _procd_open_instance
275 _procd_set_param command "$@"
276 _procd_close_instance
277 }
278
279 _procd_kill() {
280 local service="$1"
281 local instance="$2"
282
283 json_init
284 [ -n "$service" ] && json_add_string name "$service"
285 [ -n "$instance" ] && json_add_string instance "$instance"
286 _procd_ubus_call delete
287 }
288
289 procd_open_data() {
290 local name="$1"
291 json_set_namespace procd __procd_old_cb
292 json_add_object data
293 }
294
295 procd_close_data() {
296 json_close_object
297 json_set_namespace $__procd_old_cb
298 }
299
300 _procd_set_config_changed() {
301 local package="$1"
302
303 json_init
304 json_add_string type config.change
305 json_add_object data
306 json_add_string package "$package"
307 json_close_object
308
309 ubus call service event "$(json_dump)"
310 }
311
312 procd_add_mdns_service() {
313 local service proto port
314 service=$1; shift
315 proto=$1; shift
316 port=$1; shift
317 json_add_object "${service}_$port"
318 json_add_string "service" "_$service._$proto.local"
319 json_add_int port "$port"
320 [ -n "$1" ] && {
321 json_add_array txt
322 for txt in $@; do json_add_string "" $txt; done
323 json_select ..
324 }
325 json_select ..
326 }
327
328 procd_add_mdns() {
329 procd_open_data
330 json_add_object "mdns"
331 procd_add_mdns_service $@
332 json_close_object
333 procd_close_data
334 }
335
336 uci_validate_section()
337 {
338 local _package="$1"
339 local _type="$2"
340 local _name="$3"
341 local _result
342 local _error
343 shift; shift; shift
344 _result=`/sbin/validate_data "$_package" "$_type" "$_name" "$@" 2> /dev/null`
345 _error=$?
346 eval "$_result"
347 [ "$_error" = "0" ] || `/sbin/validate_data "$_package" "$_type" "$_name" "$@" 1> /dev/null`
348 return $_error
349 }
350
351 _procd_wrapper \
352 procd_open_service \
353 procd_close_service \
354 procd_add_instance \
355 procd_add_raw_trigger \
356 procd_add_config_trigger \
357 procd_add_interface_trigger \
358 procd_add_reload_trigger \
359 procd_add_reload_interface_trigger \
360 procd_add_interface_reload \
361 procd_open_trigger \
362 procd_close_trigger \
363 procd_open_instance \
364 procd_close_instance \
365 procd_open_validate \
366 procd_close_validate \
367 procd_set_param \
368 procd_append_param \
369 procd_add_validation \
370 procd_set_config_changed \
371 procd_kill