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