procd: update to latest git head
[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 #
21 #   No space separation is done for arrays/tables - use one function argument per command line argument
22 #
23 # procd_close_instance():
24 #   Complete the instance being prepared
25 #
26 # procd_kill(service, [instance]):
27 #   Kill a service instance (or all instances)
28 #
29
30 . $IPKG_INSTROOT/usr/share/libubox/jshn.sh
31
32 _PROCD_SERVICE=
33
34 _procd_call() {
35         local old_cb
36
37         json_set_namespace procd old_cb
38         "$@"
39         json_set_namespace $old_cb
40 }
41
42 _procd_wrapper() {
43         while [ -n "$1" ]; do
44                 eval "$1() { _procd_call _$1 \"\$@\"; }"
45                 shift
46         done
47 }
48
49 _procd_ubus_call() {
50         local cmd="$1"
51
52         ubus call service "$cmd" "$(json_dump)"
53         json_cleanup
54 }
55
56 _procd_open_service() {
57         local name="$1"
58         local script="$2"
59
60         _PROCD_SERVICE="$name"
61         _PROCD_INSTANCE_SEQ=0
62
63         json_init
64         json_add_string name "$name"
65         [ -n "$script" ] && json_add_string script "$script"
66         json_add_object instances
67 }
68
69 _procd_close_service() {
70         json_close_object
71         service_triggers
72         _procd_ubus_call set
73 }
74
75 _procd_add_array_data() {
76         while [ -n "$1" ]; do
77                 json_add_string "" "$1"
78                 shift
79         done
80 }
81
82 _procd_add_array() {
83         json_add_array "$1"
84         shift
85         _procd_add_array_data "$@"
86         json_close_array
87 }
88
89 _procd_add_table_data() {
90         while [ -n "$1" ]; do
91                 local var="${1%%=*}"
92                 local val="${1#*=}"
93                 [[ "$1" == "$val" ]] && val=
94                 json_add_string "$var" "$val"
95                 shift
96         done
97 }
98
99 _procd_add_table() {
100         json_add_object "$1"
101         shift
102         _procd_add_table_data "$@"
103         json_close_object
104 }
105
106 _procd_open_instance() {
107         local name="$1"; shift
108
109         _PROCD_INSTANCE_SEQ="$(($_PROCD_INSTANCE_SEQ + 1))"
110         name="${name:-instance$_PROCD_INSTANCE_SEQ}"
111         json_add_object "$name"
112 }
113
114 _procd_open_trigger() {
115         json_add_array "triggers"
116 }
117
118 _procd_open_validate() {
119         json_add_array "validate"
120 }
121
122 _procd_set_param() {
123         local type="$1"; shift
124
125         case "$type" in
126                 env|data)
127                         _procd_add_table "$type" "$@"
128                 ;;
129                 command|netdev|file|respawn)
130                         _procd_add_array "$type" "$@"
131                 ;;
132                 nice)
133                         json_add_int "$type" "$1"
134                 ;;
135         esac
136 }
137
138 _procd_add_config_trigger() {
139         json_add_array
140         _procd_add_array_data "$1"
141         shift
142
143         json_add_array
144         _procd_add_array_data "if"
145
146         json_add_array
147         _procd_add_array_data "eq" "package" "$1"
148         shift
149         json_close_array
150
151         json_add_array
152         _procd_add_array_data "run_script" "$@"
153         json_close_array
154
155         json_close_array
156
157         json_close_array
158 }
159
160 _procd_add_reload_trigger() {
161         local script=$(readlink "$initscript")
162         local name=$(basename ${script:-$initscript})
163
164         _procd_open_trigger
165         _procd_add_config_trigger "config.change" $1 /etc/init.d/$name reload
166         _procd_close_trigger
167 }
168
169 _procd_add_validation() {
170         _procd_open_validate
171         $@
172         _procd_close_validate
173 }
174
175 _procd_append_param() {
176         local type="$1"; shift
177
178         json_select "$type"
179         case "$type" in
180                 env|data)
181                         _procd_add_table_data "$@"
182                 ;;
183                 command|netdev|file|respawn)
184                         _procd_add_array_data "$@"
185                 ;;
186         esac
187         json_select ..
188 }
189
190 _procd_close_instance() {
191         json_close_object
192 }
193
194 _procd_close_trigger() {
195         json_close_array
196 }
197
198 _procd_close_validate() {
199         json_close_array
200 }
201
202 _procd_add_instance() {
203         _procd_open_instance
204         _procd_set_param command "$@"
205         _procd_close_instance
206 }
207
208 _procd_kill() {
209         local service="$1"
210         local instance="$2"
211
212         json_init
213         [ -n "$service" ] && json_add_string name "$service"
214         [ -n "$instance" ] && json_add_string instance "$instance"
215         _procd_ubus_call delete
216 }
217
218 uci_validate_section()
219 {
220         local error=0
221
222         [ "$4" = "" ] && return 1
223         [ "$3" = "" ] && {
224                 json_add_object
225                 json_add_string "package" "$1"
226                 json_add_string "type" "$2"
227                 json_add_object "data"
228
229                 shift; shift; shift
230
231                 while [ -n "$1" ]; do
232                         json_add_string "${1%:*}" "${1#*:}"
233                         shift
234                 done
235
236                 json_close_object
237                 json_close_object
238                 return 0
239         }
240
241         local section="${3}"
242         config_load "${1}"
243         shift; shift; shift
244
245         while [ -n "$1" ]; do
246                 local name=${1%%:*}
247                 local tmp=${1#*:}
248                 local type=${tmp%%:*}
249                 local default=""
250
251                 [ "$tmp" = "$type" ] || default=${tmp#*:}
252
253                 shift
254                 config_get "${name}" "${section}" "${name}"
255                 eval val=\$$name
256
257                 [ "$type" = "bool" ] && {
258                         case "$val" in
259                         1|on|true|enabled) val=1;;
260                         0|off|false|disabled) val=0;;
261                         *) val="";;
262                         esac
263                 }
264                 [ -z "$val" ] && val=${default}
265                 eval $name=\"$val\"
266                 [ -z "$val" ] || {
267                         /sbin/validate_data "${type}" "${val}"
268                         [ $? -eq 0 ] || error="$((error + 1))"
269                 }
270         done
271
272         return $error
273 }
274
275 _procd_wrapper \
276         procd_open_service \
277         procd_close_service \
278         procd_add_instance \
279         procd_add_config_trigger \
280         procd_add_reload_trigger \
281         procd_open_trigger \
282         procd_close_trigger \
283         procd_open_instance \
284         procd_close_instance \
285         procd_set_param \
286         procd_append_param \
287         procd_add_validation \
288         procd_kill