add update_start/update_complete methods
[project/procd.git] / service.c
1 #include <libubox/avl-cmp.h>
2 #include "procd.h"
3 #include "service.h"
4 #include "instance.h"
5
6 struct avl_tree services;
7 static struct blob_buf b;
8
9 static void
10 service_instance_add(struct service *s, struct blob_attr *attr)
11 {
12         struct service_instance *in;
13         const char *name = blobmsg_name(attr);
14
15         if (blobmsg_type(attr) != BLOBMSG_TYPE_TABLE)
16                 return;
17
18         in = calloc(1, sizeof(*in));
19         if (!in)
20                 return;
21
22         instance_init(in, s, attr);
23         vlist_add(&s->instances, &in->node, (void *) name);
24 }
25
26 static void
27 service_instance_update(struct vlist_tree *tree, struct vlist_node *node_new,
28                         struct vlist_node *node_old)
29 {
30         struct service_instance *in_o = NULL, *in_n = NULL;
31
32         if (node_old)
33                 in_o = container_of(node_old, struct service_instance, node);
34
35         if (node_new)
36                 in_n = container_of(node_new, struct service_instance, node);
37
38         if (in_o && in_n) {
39                 DPRINTF("Update instance %s::%s\n", in_o->srv->name, in_o->name);
40                 instance_update(in_o, in_n);
41                 instance_free(in_n);
42         } else if (in_o) {
43                 DPRINTF("Free instance %s::%s\n", in_o->srv->name, in_o->name);
44                 instance_stop(in_o, false);
45                 instance_free(in_o);
46         } else if (in_n) {
47                 DPRINTF("Create instance %s::%s\n", in_n->srv->name, in_n->name);
48                 instance_start(in_n);
49         }
50 }
51
52 static struct service *
53 service_alloc(const char *name)
54 {
55         struct service *s;
56
57         s = calloc(1, sizeof(*s));
58         vlist_init(&s->instances, avl_strcmp, service_instance_update);
59         s->instances.keep_old = true;
60
61         return s;
62 }
63
64 enum {
65         SERVICE_SET_NAME,
66         SERVICE_SET_SCRIPT,
67         SERVICE_SET_INSTANCES,
68         __SERVICE_SET_MAX
69 };
70
71 static const struct blobmsg_policy service_set_attrs[__SERVICE_SET_MAX] = {
72         [SERVICE_SET_NAME] = { "name", BLOBMSG_TYPE_STRING },
73         [SERVICE_SET_SCRIPT] = { "script", BLOBMSG_TYPE_STRING },
74         [SERVICE_SET_INSTANCES] = { "instances", BLOBMSG_TYPE_TABLE },
75 };
76
77
78 static int
79 service_update(struct service *s, struct blob_attr *config, struct blob_attr **tb)
80 {
81         struct blob_attr *old_config = s->config;
82         struct blob_attr *cur;
83         int rem;
84
85         /* only the pointer changes, the content stays the same,
86          * no avl update necessary */
87         s->name = s->avl.key = blobmsg_data(tb[SERVICE_SET_NAME]);
88         s->config = config;
89
90         if (tb[SERVICE_SET_INSTANCES]) {
91                 vlist_update(&s->instances);
92                 blobmsg_for_each_attr(cur, tb[SERVICE_SET_INSTANCES], rem) {
93                         service_instance_add(s, cur);
94                 }
95                 vlist_flush(&s->instances);
96         }
97
98         free(old_config);
99
100         return 0;
101 }
102
103 static void
104 service_delete(struct service *s)
105 {
106         vlist_flush_all(&s->instances);
107         avl_delete(&services, &s->avl);
108         free(s->config);
109         free(s);
110 }
111
112 enum {
113         SERVICE_ATTR_NAME,
114         __SERVICE_ATTR_MAX,
115 };
116
117 static const struct blobmsg_policy service_attrs[__SERVICE_ATTR_MAX] = {
118         [SERVICE_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
119 };
120
121
122 static int
123 service_handle_set(struct ubus_context *ctx, struct ubus_object *obj,
124                    struct ubus_request_data *req, const char *method,
125                    struct blob_attr *msg)
126 {
127         struct blob_attr *tb[__SERVICE_SET_MAX], *cur;
128         struct service *s = NULL;
129         const char *name;
130         int ret = UBUS_STATUS_INVALID_ARGUMENT;
131
132         msg = blob_memdup(msg);
133         if (!msg)
134                 return UBUS_STATUS_UNKNOWN_ERROR;
135
136         blobmsg_parse(service_set_attrs, __SERVICE_SET_MAX, tb, blob_data(msg), blob_len(msg));
137         cur = tb[SERVICE_ATTR_NAME];
138         if (!cur)
139                 goto free;
140
141         name = blobmsg_data(cur);
142
143         s = avl_find_element(&services, name, s, avl);
144         if (s) {
145                 DPRINTF("Update service %s\n", name);
146                 return service_update(s, msg, tb);
147         }
148
149         DPRINTF("Create service %s\n", name);
150         s = service_alloc(name);
151         if (!s)
152                 return UBUS_STATUS_UNKNOWN_ERROR;
153
154         ret = service_update(s, msg, tb);
155         if (ret)
156                 goto free;
157
158         avl_insert(&services, &s->avl);
159
160         return 0;
161
162 free:
163         free(msg);
164         return ret;
165 }
166
167 static void
168 service_dump(struct service *s)
169 {
170         struct service_instance *in;
171         void *c, *i;
172
173         c = blobmsg_open_table(&b, s->name);
174         i = blobmsg_open_table(&b, "instances");
175         vlist_for_each_element(&s->instances, in, node)
176                 instance_dump(&b, in);
177         blobmsg_close_table(&b, i);
178         blobmsg_close_table(&b, c);
179 }
180
181 static int
182 service_handle_list(struct ubus_context *ctx, struct ubus_object *obj,
183                     struct ubus_request_data *req, const char *method,
184                     struct blob_attr *msg)
185 {
186         struct service *s;
187
188         blob_buf_init(&b, 0);
189         avl_for_each_element(&services, s, avl)
190                 service_dump(s);
191
192         ubus_send_reply(ctx, req, b.head);
193
194         return 0;
195 }
196
197 static int
198 service_handle_delete(struct ubus_context *ctx, struct ubus_object *obj,
199                     struct ubus_request_data *req, const char *method,
200                     struct blob_attr *msg)
201 {
202         struct blob_attr *tb[__SERVICE_ATTR_MAX], *cur;
203         struct service *s, *tmp;
204
205         blobmsg_parse(service_attrs, __SERVICE_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
206
207         cur = tb[SERVICE_ATTR_NAME];
208         if (!cur) {
209                 avl_for_each_element_safe(&services, s, avl, tmp)
210                         service_delete(s);
211                 return 0;
212         }
213
214         s = avl_find_element(&services, blobmsg_data(cur), s, avl);
215         if (!s)
216                 return UBUS_STATUS_NOT_FOUND;
217
218         service_delete(s);
219         return 0;
220 }
221
222 static int
223 service_handle_update(struct ubus_context *ctx, struct ubus_object *obj,
224                       struct ubus_request_data *req, const char *method,
225                       struct blob_attr *msg)
226 {
227         struct blob_attr *tb[__SERVICE_ATTR_MAX], *cur;
228         struct service *s;
229
230         blobmsg_parse(service_attrs, __SERVICE_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
231
232         cur = tb[SERVICE_ATTR_NAME];
233         if (!cur)
234                 return UBUS_STATUS_INVALID_ARGUMENT;
235
236         s = avl_find_element(&services, blobmsg_data(cur), s, avl);
237         if (!s)
238                 return UBUS_STATUS_NOT_FOUND;
239
240         if (!strcmp(method, "update_start"))
241                 vlist_update(&s->instances);
242         else
243                 vlist_flush(&s->instances);
244
245         return 0;
246 }
247
248 static struct ubus_method main_object_methods[] = {
249         UBUS_METHOD("set", service_handle_set, service_set_attrs),
250         UBUS_METHOD("list", service_handle_list, service_attrs),
251         UBUS_METHOD("delete", service_handle_delete, service_attrs),
252         UBUS_METHOD("update_start", service_handle_update, service_attrs),
253         UBUS_METHOD("update_complete", service_handle_update, service_attrs),
254 };
255
256 static struct ubus_object_type main_object_type =
257         UBUS_OBJECT_TYPE("service", main_object_methods);
258
259 static struct ubus_object main_object = {
260         .name = "service",
261         .type = &main_object_type,
262         .methods = main_object_methods,
263         .n_methods = ARRAY_SIZE(main_object_methods),
264 };
265
266 void procd_init_service(struct ubus_context *ctx)
267 {
268         avl_init(&services, avl_strcmp, false, NULL);
269         ubus_add_object(ctx, &main_object);
270 }