procd: update to latest git head
[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 #
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 _procd_open_trigger
72 service_triggers
73 _procd_close_trigger
74 _procd_ubus_call set
75 }
76
77 _procd_add_array_data() {
78 while [ -n "$1" ]; do
79 json_add_string "" "$1"
80 shift
81 done
82 }
83
84 _procd_add_array() {
85 json_add_array "$1"
86 shift
87 _procd_add_array_data "$@"
88 json_close_array
89 }
90
91 _procd_add_table_data() {
92 while [ -n "$1" ]; do
93 local var="${1%%=*}"
94 local val="${1#*=}"
95 [[ "$1" == "$val" ]] && val=
96 json_add_string "$var" "$val"
97 shift
98 done
99 }
100
101 _procd_add_table() {
102 json_add_object "$1"
103 shift
104 _procd_add_table_data "$@"
105 json_close_object
106 }
107
108 _procd_open_instance() {
109 local name="$1"; shift
110
111 _PROCD_INSTANCE_SEQ="$(($_PROCD_INSTANCE_SEQ + 1))"
112 name="${name:-instance$_PROCD_INSTANCE_SEQ}"
113 json_add_object "$name"
114 }
115
116 _procd_open_trigger() {
117 json_add_array "triggers"
118 }
119
120 _procd_set_param() {
121 local type="$1"; shift
122
123 case "$type" in
124 env|data)
125 _procd_add_table "$type" "$@"
126 ;;
127 command|netdev|file|respawn)
128 _procd_add_array "$type" "$@"
129 ;;
130 nice)
131 json_add_int "$type" "$1"
132 ;;
133 esac
134 }
135
136 _procd_add_config_trigger() {
137 json_add_array
138 _procd_add_array_data "config.change"
139
140 json_add_array
141 _procd_add_array_data "if"
142
143 json_add_array
144 _procd_add_array_data "eq" "package" "$1"
145 shift
146 json_close_array
147
148 json_add_array
149 _procd_add_array_data "run_script" "$@"
150 json_close_array
151
152 json_close_array
153
154 json_close_array
155 }
156
157 _procd_add_reload_trigger() {
158 local script=$(readlink "$initscript")
159 local name=$(basename ${script:-$initscript})
160
161 _procd_add_config_trigger $1 /etc/init.d/$name reload
162 }
163
164 _procd_add_reload_trigger() {
165 local script=$(readlink "$initscript")
166 local name=$(basename ${script:-$initscript})
167
168 _procd_add_config_trigger $1 /etc/init.d/$name reload
169 }
170
171 _procd_append_param() {
172 local type="$1"; shift
173
174 json_select "$type"
175 case "$type" in
176 env|data)
177 _procd_add_table_data "$@"
178 ;;
179 command|netdev|file|respawn)
180 _procd_add_array_data "$@"
181 ;;
182 esac
183 json_select ..
184 }
185
186 _procd_close_instance() {
187 json_close_object
188 }
189
190 _procd_close_trigger() {
191 json_close_array
192 }
193
194 _procd_add_instance() {
195 _procd_open_instance
196 _procd_set_command "$@"
197 _procd_close_instance
198 }
199
200 _procd_kill() {
201 local service="$1"
202 local instance="$2"
203
204 json_init
205 [ -n "$service" ] && json_add_string name "$service"
206 [ -n "$instance" ] && json_add_string instance "$instance"
207 _procd_ubus_call delete
208 }
209
210 _procd_wrapper \
211 procd_open_service \
212 procd_close_service \
213 procd_add_instance \
214 procd_add_config_trigger \
215 procd_add_reload_trigger \
216 procd_open_trigger \
217 procd_close_trigger \
218 procd_open_instance \
219 procd_close_instance \
220 procd_set_param \
221 procd_append_param \
222 procd_kill