4189e2d4b8e79fc3bce29bc1db5396a2065bf906
[project/procd.git] / service.c
1 /*
2 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
3 * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License version 2.1
7 * as published by the Free Software Foundation
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14
15 #include <libubox/blobmsg_json.h>
16 #include <libubox/avl-cmp.h>
17 #include "procd.h"
18 #include "service.h"
19 #include "instance.h"
20
21 struct avl_tree services;
22 static struct blob_buf b;
23
24 static void
25 service_instance_add(struct service *s, struct blob_attr *attr)
26 {
27 struct service_instance *in;
28
29 if (blobmsg_type(attr) != BLOBMSG_TYPE_TABLE)
30 return;
31
32 in = calloc(1, sizeof(*in));
33 if (!in)
34 return;
35
36 instance_init(in, s, attr);
37 vlist_add(&s->instances, &in->node, (void *) in->name);
38 }
39
40 static void
41 service_instance_update(struct vlist_tree *tree, struct vlist_node *node_new,
42 struct vlist_node *node_old)
43 {
44 struct service_instance *in_o = NULL, *in_n = NULL;
45
46 if (node_old)
47 in_o = container_of(node_old, struct service_instance, node);
48
49 if (node_new)
50 in_n = container_of(node_new, struct service_instance, node);
51
52 if (in_o && in_n) {
53 DEBUG(1, "Update instance %s::%s\n", in_o->srv->name, in_o->name);
54 instance_update(in_o, in_n);
55 instance_free(in_n);
56 } else if (in_o) {
57 DEBUG(1, "Free instance %s::%s\n", in_o->srv->name, in_o->name);
58 instance_stop(in_o, false);
59 instance_free(in_o);
60 } else if (in_n) {
61 DEBUG(1, "Create instance %s::%s\n", in_n->srv->name, in_n->name);
62 instance_start(in_n);
63 }
64 }
65
66 static struct service *
67 service_alloc(const char *name)
68 {
69 struct service *s;
70 char *new_name;
71
72 s = calloc_a(sizeof(*s), &new_name, strlen(name) + 1);
73 strcpy(new_name, name);
74
75 vlist_init(&s->instances, avl_strcmp, service_instance_update);
76 s->instances.keep_old = true;
77 s->name = new_name;
78 s->avl.key = s->name;
79
80 return s;
81 }
82
83 enum {
84 SERVICE_SET_NAME,
85 SERVICE_SET_SCRIPT,
86 SERVICE_SET_INSTANCES,
87 SERVICE_SET_TRIGGER,
88 __SERVICE_SET_MAX
89 };
90
91 static const struct blobmsg_policy service_set_attrs[__SERVICE_SET_MAX] = {
92 [SERVICE_SET_NAME] = { "name", BLOBMSG_TYPE_STRING },
93 [SERVICE_SET_SCRIPT] = { "script", BLOBMSG_TYPE_STRING },
94 [SERVICE_SET_INSTANCES] = { "instances", BLOBMSG_TYPE_TABLE },
95 [SERVICE_SET_TRIGGER] = { "triggers", BLOBMSG_TYPE_ARRAY },
96 };
97
98 static int
99 service_update(struct service *s, struct blob_attr *config, struct blob_attr **tb, bool add)
100 {
101 struct blob_attr *cur;
102 int rem;
103
104 if (s->trigger)
105 trigger_del(s);
106
107 if (tb[SERVICE_SET_TRIGGER] && blobmsg_data_len(tb[SERVICE_SET_TRIGGER])) {
108 s->trigger = tb[SERVICE_SET_TRIGGER];
109 trigger_add(s->trigger, s);
110 }
111
112 if (tb[SERVICE_SET_INSTANCES]) {
113 if (!add)
114 vlist_update(&s->instances);
115 blobmsg_for_each_attr(cur, tb[SERVICE_SET_INSTANCES], rem) {
116 service_instance_add(s, cur);
117 }
118 if (!add)
119 vlist_flush(&s->instances);
120 }
121
122 return 0;
123 }
124
125 static void
126 service_delete(struct service *s)
127 {
128 vlist_flush_all(&s->instances);
129 avl_delete(&services, &s->avl);
130 trigger_del(s);
131 free(s->config);
132 free(s);
133 }
134
135 enum {
136 SERVICE_ATTR_NAME,
137 __SERVICE_ATTR_MAX,
138 };
139
140 static const struct blobmsg_policy service_attrs[__SERVICE_ATTR_MAX] = {
141 [SERVICE_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
142 };
143
144 enum {
145 SERVICE_DEL_ATTR_NAME,
146 SERVICE_DEL_ATTR_INSTANCE,
147 __SERVICE_DEL_ATTR_MAX,
148 };
149
150 static const struct blobmsg_policy service_del_attrs[__SERVICE_DEL_ATTR_MAX] = {
151 [SERVICE_DEL_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
152 [SERVICE_DEL_ATTR_INSTANCE] = { "instance", BLOBMSG_TYPE_STRING },
153 };
154
155 enum {
156 SERVICE_LIST_ATTR_VERBOSE,
157 __SERVICE_LIST_ATTR_MAX,
158 };
159
160 static const struct blobmsg_policy service_list_attrs[__SERVICE_LIST_ATTR_MAX] = {
161 [SERVICE_LIST_ATTR_VERBOSE] = { "verbose", BLOBMSG_TYPE_BOOL },
162 };
163
164 enum {
165 EVENT_TYPE,
166 EVENT_DATA,
167 __EVENT_MAX
168 };
169
170 static const struct blobmsg_policy event_policy[__EVENT_MAX] = {
171 [EVENT_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
172 [EVENT_DATA] = { .name = "data", .type = BLOBMSG_TYPE_TABLE },
173 };
174
175 static int
176 service_handle_set(struct ubus_context *ctx, struct ubus_object *obj,
177 struct ubus_request_data *req, const char *method,
178 struct blob_attr *msg)
179 {
180 struct blob_attr *tb[__SERVICE_SET_MAX], *cur;
181 struct service *s = NULL;
182 const char *name;
183 int ret = UBUS_STATUS_INVALID_ARGUMENT;
184 bool add = !strcmp(method, "add");
185
186 blobmsg_parse(service_set_attrs, __SERVICE_SET_MAX, tb, blob_data(msg), blob_len(msg));
187 cur = tb[SERVICE_ATTR_NAME];
188 if (!cur)
189 goto free;
190
191 name = blobmsg_data(cur);
192
193 s = avl_find_element(&services, name, s, avl);
194 if (s) {
195 DEBUG(1, "Update service %s\n", name);
196 return service_update(s, msg, tb, add);
197 }
198
199 DEBUG(1, "Create service %s\n", name);
200 s = service_alloc(name);
201 if (!s)
202 return UBUS_STATUS_UNKNOWN_ERROR;
203
204 ret = service_update(s, msg, tb, add);
205 if (ret)
206 goto free;
207
208 avl_insert(&services, &s->avl);
209
210 return 0;
211
212 free:
213 free(msg);
214 return ret;
215 }
216
217 static void
218 service_dump(struct service *s, int verbose)
219 {
220 struct service_instance *in;
221 void *c, *i;
222
223 if (avl_is_empty(&s->instances.avl))
224 return;
225
226 c = blobmsg_open_table(&b, s->name);
227 if (verbose && s->trigger)
228 blobmsg_add_blob(&b, s->trigger);
229 i = blobmsg_open_table(&b, "instances");
230 vlist_for_each_element(&s->instances, in, node)
231 instance_dump(&b, in, verbose);
232 blobmsg_close_table(&b, i);
233 blobmsg_close_table(&b, c);
234 }
235
236 static int
237 service_handle_list(struct ubus_context *ctx, struct ubus_object *obj,
238 struct ubus_request_data *req, const char *method,
239 struct blob_attr *msg)
240 {
241 struct blob_attr *tb[__SERVICE_LIST_ATTR_MAX];
242 struct service *s;
243 int verbose = 0;
244
245 blobmsg_parse(service_list_attrs, __SERVICE_LIST_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
246
247 if (tb[SERVICE_LIST_ATTR_VERBOSE] && blobmsg_get_bool(tb[SERVICE_LIST_ATTR_VERBOSE]))
248 verbose = 1;
249
250 blob_buf_init(&b, 0);
251 avl_for_each_element(&services, s, avl)
252 service_dump(s, verbose);
253
254 ubus_send_reply(ctx, req, b.head);
255
256 return 0;
257 }
258
259 static int
260 service_handle_delete(struct ubus_context *ctx, struct ubus_object *obj,
261 struct ubus_request_data *req, const char *method,
262 struct blob_attr *msg)
263 {
264 struct blob_attr *tb[__SERVICE_DEL_ATTR_MAX], *cur;
265 struct service *s;
266 struct service_instance *in;
267
268 blobmsg_parse(service_del_attrs, __SERVICE_DEL_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
269
270 cur = tb[SERVICE_DEL_ATTR_NAME];
271 if (!cur)
272 return UBUS_STATUS_NOT_FOUND;
273
274 s = avl_find_element(&services, blobmsg_data(cur), s, avl);
275 if (!s)
276 return UBUS_STATUS_NOT_FOUND;
277
278 cur = tb[SERVICE_DEL_ATTR_INSTANCE];
279 if (!cur) {
280 service_delete(s);
281 return 0;
282 }
283
284 in = vlist_find(&s->instances, blobmsg_data(cur), in, node);
285 if (!in) {
286 ERROR("instance %s not found\n", (char *) blobmsg_data(cur));
287 return UBUS_STATUS_NOT_FOUND;
288 }
289
290 vlist_delete(&s->instances, &in->node);
291
292 return 0;
293 }
294
295 static int
296 service_handle_update(struct ubus_context *ctx, struct ubus_object *obj,
297 struct ubus_request_data *req, const char *method,
298 struct blob_attr *msg)
299 {
300 struct blob_attr *tb[__SERVICE_ATTR_MAX], *cur;
301 struct service *s;
302
303 blobmsg_parse(service_attrs, __SERVICE_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
304
305 cur = tb[SERVICE_ATTR_NAME];
306 if (!cur)
307 return UBUS_STATUS_INVALID_ARGUMENT;
308
309 s = avl_find_element(&services, blobmsg_data(cur), s, avl);
310 if (!s)
311 return UBUS_STATUS_NOT_FOUND;
312
313 if (!strcmp(method, "update_start"))
314 vlist_update(&s->instances);
315 else
316 vlist_flush(&s->instances);
317
318 return 0;
319 }
320
321 static int
322 service_handle_event(struct ubus_context *ctx, struct ubus_object *obj,
323 struct ubus_request_data *req, const char *method,
324 struct blob_attr *msg)
325 {
326 struct blob_attr *tb[__EVENT_MAX];
327
328 if (!msg)
329 return UBUS_STATUS_INVALID_ARGUMENT;
330
331 blobmsg_parse(event_policy, __EVENT_MAX, tb, blob_data(msg), blob_len(msg));
332 if (!tb[EVENT_TYPE] || !tb[EVENT_DATA])
333 return UBUS_STATUS_INVALID_ARGUMENT;
334
335 trigger_event(blobmsg_get_string(tb[EVENT_TYPE]), tb[EVENT_DATA]);
336
337 return 0;
338 }
339
340 static struct ubus_method main_object_methods[] = {
341 UBUS_METHOD("set", service_handle_set, service_set_attrs),
342 UBUS_METHOD("add", service_handle_set, service_set_attrs),
343 UBUS_METHOD("list", service_handle_list, service_attrs),
344 UBUS_METHOD("delete", service_handle_delete, service_del_attrs),
345 UBUS_METHOD("update_start", service_handle_update, service_attrs),
346 UBUS_METHOD("update_complete", service_handle_update, service_attrs),
347 UBUS_METHOD("event", service_handle_event, event_policy),
348 };
349
350 static struct ubus_object_type main_object_type =
351 UBUS_OBJECT_TYPE("service", main_object_methods);
352
353 static struct ubus_object main_object = {
354 .name = "service",
355 .type = &main_object_type,
356 .methods = main_object_methods,
357 .n_methods = ARRAY_SIZE(main_object_methods),
358 };
359
360 void ubus_init_service(struct ubus_context *ctx)
361 {
362 avl_init(&services, avl_strcmp, false, NULL);
363 ubus_add_object(ctx, &main_object);
364 }