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