fixup service_dump logic
[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);
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 c = blobmsg_open_table(&b, s->name);
224
225 if (avl_is_empty(&s->instances.avl)) {
226 blobmsg_close_table(&b, c);
227 return;
228 }
229
230 i = blobmsg_open_table(&b, "instances");
231 vlist_for_each_element(&s->instances, in, node)
232 instance_dump(&b, in, verbose);
233 blobmsg_close_table(&b, i);
234 if (verbose && s->trigger)
235 blobmsg_add_blob(&b, s->trigger);
236 blobmsg_close_table(&b, c);
237 }
238
239 static int
240 service_handle_list(struct ubus_context *ctx, struct ubus_object *obj,
241 struct ubus_request_data *req, const char *method,
242 struct blob_attr *msg)
243 {
244 struct blob_attr *tb[__SERVICE_LIST_ATTR_MAX];
245 struct service *s;
246 int verbose = 0;
247
248 blobmsg_parse(service_list_attrs, __SERVICE_LIST_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
249
250 if (tb[SERVICE_LIST_ATTR_VERBOSE] && blobmsg_get_bool(tb[SERVICE_LIST_ATTR_VERBOSE]))
251 verbose = 1;
252
253 blob_buf_init(&b, 0);
254 avl_for_each_element(&services, s, avl)
255 service_dump(s, verbose);
256
257 ubus_send_reply(ctx, req, b.head);
258
259 return 0;
260 }
261
262 static int
263 service_handle_delete(struct ubus_context *ctx, struct ubus_object *obj,
264 struct ubus_request_data *req, const char *method,
265 struct blob_attr *msg)
266 {
267 struct blob_attr *tb[__SERVICE_DEL_ATTR_MAX], *cur;
268 struct service *s;
269 struct service_instance *in;
270
271 blobmsg_parse(service_del_attrs, __SERVICE_DEL_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
272
273 cur = tb[SERVICE_DEL_ATTR_NAME];
274 if (!cur)
275 return UBUS_STATUS_NOT_FOUND;
276
277 s = avl_find_element(&services, blobmsg_data(cur), s, avl);
278 if (!s)
279 return UBUS_STATUS_NOT_FOUND;
280
281 cur = tb[SERVICE_DEL_ATTR_INSTANCE];
282 if (!cur) {
283 service_delete(s);
284 return 0;
285 }
286
287 in = vlist_find(&s->instances, blobmsg_data(cur), in, node);
288 if (!in) {
289 ERROR("instance %s not found\n", (char *) blobmsg_data(cur));
290 return UBUS_STATUS_NOT_FOUND;
291 }
292
293 vlist_delete(&s->instances, &in->node);
294
295 return 0;
296 }
297
298 static int
299 service_handle_update(struct ubus_context *ctx, struct ubus_object *obj,
300 struct ubus_request_data *req, const char *method,
301 struct blob_attr *msg)
302 {
303 struct blob_attr *tb[__SERVICE_ATTR_MAX], *cur;
304 struct service *s;
305
306 blobmsg_parse(service_attrs, __SERVICE_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
307
308 cur = tb[SERVICE_ATTR_NAME];
309 if (!cur)
310 return UBUS_STATUS_INVALID_ARGUMENT;
311
312 s = avl_find_element(&services, blobmsg_data(cur), s, avl);
313 if (!s)
314 return UBUS_STATUS_NOT_FOUND;
315
316 if (!strcmp(method, "update_start"))
317 vlist_update(&s->instances);
318 else
319 vlist_flush(&s->instances);
320
321 return 0;
322 }
323
324 static int
325 service_handle_event(struct ubus_context *ctx, struct ubus_object *obj,
326 struct ubus_request_data *req, const char *method,
327 struct blob_attr *msg)
328 {
329 struct blob_attr *tb[__EVENT_MAX];
330
331 if (!msg)
332 return UBUS_STATUS_INVALID_ARGUMENT;
333
334 blobmsg_parse(event_policy, __EVENT_MAX, tb, blob_data(msg), blob_len(msg));
335 if (!tb[EVENT_TYPE] || !tb[EVENT_DATA])
336 return UBUS_STATUS_INVALID_ARGUMENT;
337
338 trigger_event(blobmsg_get_string(tb[EVENT_TYPE]), tb[EVENT_DATA]);
339
340 return 0;
341 }
342
343 static struct ubus_method main_object_methods[] = {
344 UBUS_METHOD("set", service_handle_set, service_set_attrs),
345 UBUS_METHOD("add", service_handle_set, service_set_attrs),
346 UBUS_METHOD("list", service_handle_list, service_attrs),
347 UBUS_METHOD("delete", service_handle_delete, service_del_attrs),
348 UBUS_METHOD("update_start", service_handle_update, service_attrs),
349 UBUS_METHOD("update_complete", service_handle_update, service_attrs),
350 UBUS_METHOD("event", service_handle_event, event_policy),
351 };
352
353 static struct ubus_object_type main_object_type =
354 UBUS_OBJECT_TYPE("service", main_object_methods);
355
356 static struct ubus_object main_object = {
357 .name = "service",
358 .type = &main_object_type,
359 .methods = main_object_methods,
360 .n_methods = ARRAY_SIZE(main_object_methods),
361 };
362
363 void ubus_init_service(struct ubus_context *ctx)
364 {
365 avl_init(&services, avl_strcmp, false, NULL);
366 ubus_add_object(ctx, &main_object);
367 }