procd: support pidfile writing.
[openwrt/staging/jow.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 $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
33 . $IPKG_INSTROOT/usr/share/libubox/jshn.sh
34
35 _PROCD_SERVICE=
36
37 _procd_call() {
38 local old_cb
39
40 json_set_namespace procd old_cb
41 "$@"
42 json_set_namespace $old_cb
43 }
44
45 _procd_wrapper() {
46 while [ -n "$1" ]; do
47 eval "$1() { _procd_call _$1 \"\$@\"; }"
48 shift
49 done
50 }
51
52 _procd_ubus_call() {
53 local cmd="$1"
54
55 [ -n "$PROCD_DEBUG" ] && json_dump >&2
56 ubus call service "$cmd" "$(json_dump)"
57 json_cleanup
58 }
59
60 _procd_open_service() {
61 local name="$1"
62 local script="$2"
63
64 _PROCD_SERVICE="$name"
65 _PROCD_INSTANCE_SEQ=0
66
67 json_init
68 json_add_string name "$name"
69 [ -n "$script" ] && json_add_string script "$script"
70 json_add_object instances
71 }
72
73 _procd_close_service() {
74 json_close_object
75 service_triggers
76 _procd_ubus_call set
77 }
78
79 _procd_add_array_data() {
80 while [ "$#" -gt 0 ]; do
81 json_add_string "" "$1"
82 shift
83 done
84 }
85
86 _procd_add_array() {
87 json_add_array "$1"
88 shift
89 _procd_add_array_data "$@"
90 json_close_array
91 }
92
93 _procd_add_table_data() {
94 while [ -n "$1" ]; do
95 local var="${1%%=*}"
96 local val="${1#*=}"
97 [ "$1" = "$val" ] && val=
98 json_add_string "$var" "$val"
99 shift
100 done
101 }
102
103 _procd_add_table() {
104 json_add_object "$1"
105 shift
106 _procd_add_table_data "$@"
107 json_close_object
108 }
109
110 _procd_open_instance() {
111 local name="$1"; shift
112
113 _PROCD_INSTANCE_SEQ="$(($_PROCD_INSTANCE_SEQ + 1))"
114 name="${name:-instance$_PROCD_INSTANCE_SEQ}"
115 json_add_object "$name"
116 [ -n "$TRACE_SYSCALLS" ] && json_add_boolean trace "1"
117 }
118
119 _procd_open_trigger() {
120 json_add_array "triggers"
121 }
122
123 _procd_open_validate() {
124 json_add_array "validate"
125 }
126
127 _procd_add_jail() {
128 json_add_object "jail"
129 json_add_string name "$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 ronly) json_add_boolean "ronly" "1";;
140 esac
141 done
142 json_add_object "mount"
143 json_close_object
144 json_close_object
145 }
146
147 _procd_add_jail_mount() {
148 local _json_no_warning=1
149
150 json_select "jail"
151 [ $? = 0 ] || return
152 json_select "mount"
153 [ $? = 0 ] || {
154 json_select ..
155 return
156 }
157 for a in $@; do
158 json_add_string "$a" "0"
159 done
160 json_select ..
161 json_select ..
162 }
163
164 _procd_add_jail_mount_rw() {
165 local _json_no_warning=1
166
167 json_select "jail"
168 [ $? = 0 ] || return
169 json_select "mount"
170 [ $? = 0 ] || {
171 json_select ..
172 return
173 }
174 for a in $@; do
175 json_add_string "$a" "1"
176 done
177 json_select ..
178 json_select ..
179 }
180
181 _procd_set_param() {
182 local type="$1"; shift
183
184 case "$type" in
185 env|data|limits)
186 _procd_add_table "$type" "$@"
187 ;;
188 command|netdev|file|respawn|watch)
189 _procd_add_array "$type" "$@"
190 ;;
191 error)
192 json_add_array "$type"
193 json_add_string "" "$@"
194 json_close_array
195 ;;
196 nice)
197 json_add_int "$type" "$1"
198 ;;
199 pidfile|user|seccomp|capabilities)
200 json_add_string "$type" "$1"
201 ;;
202 stdout|stderr|no_new_privs)
203 json_add_boolean "$type" "$1"
204 ;;
205 esac
206 }
207
208 _procd_add_interface_trigger() {
209 json_add_array
210 _procd_add_array_data "$1"
211 shift
212
213 json_add_array
214 _procd_add_array_data "if"
215
216 json_add_array
217 _procd_add_array_data "eq" "interface" "$1"
218 shift
219 json_close_array
220
221 json_add_array
222 _procd_add_array_data "run_script" "$@"
223 json_close_array
224
225 json_close_array
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 local respawn_vals
321 if json_select respawn ; then
322 json_get_values respawn_vals
323 if [ -z "$respawn_vals" ]; then
324 local respawn_retry=$(uci_get system.@service[0].respawn_retry)
325 _procd_add_array_data 3600 5 ${respawn_retry:-5}
326 fi
327 json_select ..
328 fi
329
330 json_close_object
331 }
332
333 _procd_close_trigger() {
334 json_close_array
335 }
336
337 _procd_close_validate() {
338 json_close_array
339 }
340
341 _procd_add_instance() {
342 _procd_open_instance
343 _procd_set_param command "$@"
344 _procd_close_instance
345 }
346
347 _procd_kill() {
348 local service="$1"
349 local instance="$2"
350
351 json_init
352 [ -n "$service" ] && json_add_string name "$service"
353 [ -n "$instance" ] && json_add_string instance "$instance"
354 _procd_ubus_call delete
355 }
356
357 procd_open_data() {
358 local name="$1"
359 json_set_namespace procd __procd_old_cb
360 json_add_object data
361 }
362
363 procd_close_data() {
364 json_close_object
365 json_set_namespace $__procd_old_cb
366 }
367
368 _procd_set_config_changed() {
369 local package="$1"
370
371 json_init
372 json_add_string type config.change
373 json_add_object data
374 json_add_string package "$package"
375 json_close_object
376
377 ubus call service event "$(json_dump)"
378 }
379
380 procd_add_mdns_service() {
381 local service proto port
382 service=$1; shift
383 proto=$1; shift
384 port=$1; shift
385 json_add_object "${service}_$port"
386 json_add_string "service" "_$service._$proto.local"
387 json_add_int port "$port"
388 [ -n "$1" ] && {
389 json_add_array txt
390 for txt in $@; do json_add_string "" $txt; done
391 json_select ..
392 }
393 json_select ..
394 }
395
396 procd_add_mdns() {
397 procd_open_data
398 json_add_object "mdns"
399 procd_add_mdns_service $@
400 json_close_object
401 procd_close_data
402 }
403
404 uci_validate_section()
405 {
406 local _package="$1"
407 local _type="$2"
408 local _name="$3"
409 local _result
410 local _error
411 shift; shift; shift
412 _result=`/sbin/validate_data "$_package" "$_type" "$_name" "$@" 2> /dev/null`
413 _error=$?
414 eval "$_result"
415 [ "$_error" = "0" ] || `/sbin/validate_data "$_package" "$_type" "$_name" "$@" 1> /dev/null`
416 return $_error
417 }
418
419 _procd_wrapper \
420 procd_open_service \
421 procd_close_service \
422 procd_add_instance \
423 procd_add_raw_trigger \
424 procd_add_config_trigger \
425 procd_add_interface_trigger \
426 procd_add_reload_trigger \
427 procd_add_reload_interface_trigger \
428 procd_open_trigger \
429 procd_close_trigger \
430 procd_open_instance \
431 procd_close_instance \
432 procd_open_validate \
433 procd_close_validate \
434 procd_add_jail \
435 procd_add_jail_mount \
436 procd_add_jail_mount_rw \
437 procd_set_param \
438 procd_append_param \
439 procd_add_validation \
440 procd_set_config_changed \
441 procd_kill