fix a memleak
[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         char *new_name;
57
58         s = calloc(1, sizeof(*s) + strlen(name) + 1);
59
60         new_name = (char *) s + 1;
61         strcpy(new_name, name);
62
63         vlist_init(&s->instances, avl_strcmp, service_instance_update);
64         s->instances.keep_old = true;
65         s->name = new_name;
66         s->avl.key = s->name;
67
68         return s;
69 }
70
71 enum {
72         SERVICE_SET_NAME,
73         SERVICE_SET_SCRIPT,
74         SERVICE_SET_INSTANCES,
75         __SERVICE_SET_MAX
76 };
77
78 static const struct blobmsg_policy service_set_attrs[__SERVICE_SET_MAX] = {
79         [SERVICE_SET_NAME] = { "name", BLOBMSG_TYPE_STRING },
80         [SERVICE_SET_SCRIPT] = { "script", BLOBMSG_TYPE_STRING },
81         [SERVICE_SET_INSTANCES] = { "instances", BLOBMSG_TYPE_TABLE },
82 };
83
84
85 static int
86 service_update(struct service *s, struct blob_attr *config, struct blob_attr **tb)
87 {
88         struct blob_attr *cur;
89         int rem;
90
91         if (tb[SERVICE_SET_INSTANCES]) {
92                 vlist_update(&s->instances);
93                 blobmsg_for_each_attr(cur, tb[SERVICE_SET_INSTANCES], rem) {
94                         service_instance_add(s, cur);
95                 }
96                 vlist_flush(&s->instances);
97         }
98
99         return 0;
100 }
101
102 static void
103 service_delete(struct service *s)
104 {
105         vlist_flush_all(&s->instances);
106         avl_delete(&services, &s->avl);
107         free(s->config);
108         free(s);
109 }
110
111 enum {
112         SERVICE_ATTR_NAME,
113         __SERVICE_ATTR_MAX,
114 };
115
116 static const struct blobmsg_policy service_attrs[__SERVICE_ATTR_MAX] = {
117         [SERVICE_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
118 };
119
120
121 static int
122 service_handle_set(struct ubus_context *ctx, struct ubus_object *obj,
123                    struct ubus_request_data *req, const char *method,
124                    struct blob_attr *msg)
125 {
126         struct blob_attr *tb[__SERVICE_SET_MAX], *cur;
127         struct service *s = NULL;
128         const char *name;
129         int ret = UBUS_STATUS_INVALID_ARGUMENT;
130
131         blobmsg_parse(service_set_attrs, __SERVICE_SET_MAX, tb, blob_data(msg), blob_len(msg));
132         cur = tb[SERVICE_ATTR_NAME];
133         if (!cur)
134                 goto free;
135
136         name = blobmsg_data(cur);
137
138         s = avl_find_element(&services, name, s, avl);
139         if (s) {
140                 DPRINTF("Update service %s\n", name);
141                 return service_update(s, msg, tb);
142         }
143
144         DPRINTF("Create service %s\n", name);
145         s = service_alloc(name);
146         if (!s)
147                 return UBUS_STATUS_UNKNOWN_ERROR;
148
149         ret = service_update(s, msg, tb);
150         if (ret)
151                 goto free;
152
153         avl_insert(&services, &s->avl);
154
155         return 0;
156
157 free:
158         free(msg);
159         return ret;
160 }
161
162 static void
163 service_dump(struct service *s)
164 {
165         struct service_instance *in;
166         void *c, *i;
167
168         c = blobmsg_open_table(&b, s->name);
169         i = blobmsg_open_table(&b, "instances");
170         vlist_for_each_element(&s->instances, in, node)
171                 instance_dump(&b, in);
172         blobmsg_close_table(&b, i);
173         blobmsg_close_table(&b, c);
174 }
175
176 static int
177 service_handle_list(struct ubus_context *ctx, struct ubus_object *obj,
178                     struct ubus_request_data *req, const char *method,
179                     struct blob_attr *msg)
180 {
181         struct service *s;
182
183         blob_buf_init(&b, 0);
184         avl_for_each_element(&services, s, avl)
185                 service_dump(s);
186
187         ubus_send_reply(ctx, req, b.head);
188
189         return 0;
190 }
191
192 static int
193 service_handle_delete(struct ubus_context *ctx, struct ubus_object *obj,
194                     struct ubus_request_data *req, const char *method,
195                     struct blob_attr *msg)
196 {
197         struct blob_attr *tb[__SERVICE_ATTR_MAX], *cur;
198         struct service *s, *tmp;
199
200         blobmsg_parse(service_attrs, __SERVICE_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
201
202         cur = tb[SERVICE_ATTR_NAME];
203         if (!cur) {
204                 avl_for_each_element_safe(&services, s, avl, tmp)
205                         service_delete(s);
206                 return 0;
207         }
208
209         s = avl_find_element(&services, blobmsg_data(cur), s, avl);
210         if (!s)
211                 return UBUS_STATUS_NOT_FOUND;
212
213         service_delete(s);
214         return 0;
215 }
216
217 static int
218 service_handle_update(struct ubus_context *ctx, struct ubus_object *obj,
219                       struct ubus_request_data *req, const char *method,
220                       struct blob_attr *msg)
221 {
222         struct blob_attr *tb[__SERVICE_ATTR_MAX], *cur;
223         struct service *s;
224
225         blobmsg_parse(service_attrs, __SERVICE_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
226
227         cur = tb[SERVICE_ATTR_NAME];
228         if (!cur)
229                 return UBUS_STATUS_INVALID_ARGUMENT;
230
231         s = avl_find_element(&services, blobmsg_data(cur), s, avl);
232         if (!s)
233                 return UBUS_STATUS_NOT_FOUND;
234
235         if (!strcmp(method, "update_start"))
236                 vlist_update(&s->instances);
237         else
238                 vlist_flush(&s->instances);
239
240         return 0;
241 }
242
243 static struct ubus_method main_object_methods[] = {
244         UBUS_METHOD("set", service_handle_set, service_set_attrs),
245         UBUS_METHOD("list", service_handle_list, service_attrs),
246         UBUS_METHOD("delete", service_handle_delete, service_attrs),
247         UBUS_METHOD("update_start", service_handle_update, service_attrs),
248         UBUS_METHOD("update_complete", service_handle_update, service_attrs),
249 };
250
251 static struct ubus_object_type main_object_type =
252         UBUS_OBJECT_TYPE("service", main_object_methods);
253
254 static struct ubus_object main_object = {
255         .name = "service",
256         .type = &main_object_type,
257         .methods = main_object_methods,
258         .n_methods = ARRAY_SIZE(main_object_methods),
259 };
260
261 void procd_init_service(struct ubus_context *ctx)
262 {
263         avl_init(&services, avl_strcmp, false, NULL);
264         ubus_add_object(ctx, &main_object);
265 }