move instance code to a separate source file
[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, 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 instance_update(in_o, in_n);
40 instance_free(in_n);
41 } else if (in_o) {
42 instance_stop(in_o, false);
43 instance_free(in_o);
44 } else if (in_n) {
45 instance_start(in_n);
46 }
47 }
48
49 static struct service *
50 service_alloc(const char *name)
51 {
52 struct service *s;
53
54 s = calloc(1, sizeof(*s));
55 vlist_init(&s->instances, avl_strcmp, service_instance_update);
56 s->instances.keep_old = true;
57
58 return s;
59 }
60
61 enum {
62 SERVICE_ATTR_NAME,
63 SERVICE_ATTR_SCRIPT,
64 SERVICE_ATTR_INSTANCES,
65 __SERVICE_ATTR_MAX
66 };
67
68 static const struct blobmsg_policy service_attrs[__SERVICE_ATTR_MAX] = {
69 [SERVICE_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
70 [SERVICE_ATTR_SCRIPT] = { "script", BLOBMSG_TYPE_STRING },
71 [SERVICE_ATTR_INSTANCES] = { "instances", BLOBMSG_TYPE_TABLE },
72 };
73
74
75 static int
76 service_update(struct service *s, struct blob_attr *config, struct blob_attr **tb)
77 {
78 struct blob_attr *old_config = s->config;
79 struct blob_attr *cur;
80 int rem;
81
82 /* only the pointer changes, the content stays the same,
83 * no avl update necessary */
84 s->name = s->avl.key = blobmsg_data(tb[SERVICE_ATTR_NAME]);
85 s->config = config;
86
87 if (tb[SERVICE_ATTR_INSTANCES]) {
88 vlist_update(&s->instances);
89 blobmsg_for_each_attr(cur, tb[SERVICE_ATTR_INSTANCES], rem) {
90 service_instance_add(s, cur);
91 }
92 vlist_flush(&s->instances);
93 }
94
95 free(old_config);
96
97 return 0;
98 }
99
100 static void
101 service_delete(struct service *s)
102 {
103 vlist_flush_all(&s->instances);
104 avl_delete(&services, &s->avl);
105 free(s->config);
106 free(s);
107 }
108
109 static int
110 service_handle_set(struct ubus_context *ctx, struct ubus_object *obj,
111 struct ubus_request_data *req, const char *method,
112 struct blob_attr *msg)
113 {
114 struct blob_attr *tb[__SERVICE_ATTR_MAX], *cur;
115 struct service *s = NULL;
116 const char *name;
117 int ret = UBUS_STATUS_INVALID_ARGUMENT;
118
119 msg = blob_memdup(msg);
120 if (!msg)
121 return UBUS_STATUS_UNKNOWN_ERROR;
122
123 blobmsg_parse(service_attrs, __SERVICE_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
124 cur = tb[SERVICE_ATTR_NAME];
125 if (!cur)
126 goto free;
127
128 name = blobmsg_data(cur);
129
130 s = avl_find_element(&services, name, s, avl);
131 if (s)
132 return service_update(s, msg, tb);
133
134 s = service_alloc(name);
135 if (!s)
136 return UBUS_STATUS_UNKNOWN_ERROR;
137
138 ret = service_update(s, msg, tb);
139 if (ret)
140 goto free;
141
142 avl_insert(&services, &s->avl);
143
144 return 0;
145
146 free:
147 free(msg);
148 return ret;
149 }
150
151 static int
152 service_handle_list(struct ubus_context *ctx, struct ubus_object *obj,
153 struct ubus_request_data *req, const char *method,
154 struct blob_attr *msg)
155 {
156 struct service *s;
157
158 blob_buf_init(&b, 0);
159 avl_for_each_element(&services, s, avl) {
160 void *c;
161
162 c = blobmsg_open_table(&b, s->name);
163 blobmsg_close_table(&b, c);
164 }
165
166 ubus_send_reply(ctx, req, b.head);
167
168 return 0;
169 }
170
171 enum {
172 SERVICE_DEL_NAME,
173 __SERVICE_DEL_MAX,
174 };
175
176 static const struct blobmsg_policy service_del_attrs[__SERVICE_DEL_MAX] = {
177 [SERVICE_DEL_NAME] = { "name", BLOBMSG_TYPE_STRING },
178 };
179
180
181 static int
182 service_handle_delete(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 blob_attr *tb[__SERVICE_DEL_MAX], *cur;
187 struct service *s, *tmp;
188
189 blobmsg_parse(service_del_attrs, __SERVICE_DEL_MAX, tb, blob_data(msg), blob_len(msg));
190
191 cur = tb[SERVICE_ATTR_NAME];
192 if (!cur) {
193 avl_for_each_element_safe(&services, s, avl, tmp)
194 service_delete(s);
195 return 0;
196 }
197
198 s = avl_find_element(&services, blobmsg_data(cur), s, avl);
199 if (!s)
200 return UBUS_STATUS_NOT_FOUND;
201
202 service_delete(s);
203 return 0;
204 }
205
206 static struct ubus_method main_object_methods[] = {
207 { .name = "list", .handler = service_handle_list },
208 { .name = "set", .handler = service_handle_set },
209 { .name = "delete", .handler = service_handle_delete },
210 };
211
212 static struct ubus_object_type main_object_type =
213 UBUS_OBJECT_TYPE("service", main_object_methods);
214
215 static struct ubus_object main_object = {
216 .name = "service",
217 .type = &main_object_type,
218 .methods = main_object_methods,
219 .n_methods = ARRAY_SIZE(main_object_methods),
220 };
221
222 void procd_init_service(struct ubus_context *ctx)
223 {
224 avl_init(&services, avl_strcmp, false, NULL);
225 ubus_add_object(ctx, &main_object);
226 }