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