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