add a debug message for showing starting of instances
[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 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 void
152 service_dump(struct service *s)
153 {
154 struct service_instance *in;
155 void *c, *i;
156
157 c = blobmsg_open_table(&b, s->name);
158 i = blobmsg_open_table(&b, "instances");
159 vlist_for_each_element(&s->instances, in, node)
160 instance_dump(&b, in);
161 blobmsg_close_table(&b, i);
162 blobmsg_close_table(&b, c);
163 }
164
165 static int
166 service_handle_list(struct ubus_context *ctx, struct ubus_object *obj,
167 struct ubus_request_data *req, const char *method,
168 struct blob_attr *msg)
169 {
170 struct service *s;
171
172 blob_buf_init(&b, 0);
173 avl_for_each_element(&services, s, avl)
174 service_dump(s);
175
176 ubus_send_reply(ctx, req, b.head);
177
178 return 0;
179 }
180
181 enum {
182 SERVICE_DEL_NAME,
183 __SERVICE_DEL_MAX,
184 };
185
186 static const struct blobmsg_policy service_del_attrs[__SERVICE_DEL_MAX] = {
187 [SERVICE_DEL_NAME] = { "name", BLOBMSG_TYPE_STRING },
188 };
189
190
191 static int
192 service_handle_delete(struct ubus_context *ctx, struct ubus_object *obj,
193 struct ubus_request_data *req, const char *method,
194 struct blob_attr *msg)
195 {
196 struct blob_attr *tb[__SERVICE_DEL_MAX], *cur;
197 struct service *s, *tmp;
198
199 blobmsg_parse(service_del_attrs, __SERVICE_DEL_MAX, tb, blob_data(msg), blob_len(msg));
200
201 cur = tb[SERVICE_ATTR_NAME];
202 if (!cur) {
203 avl_for_each_element_safe(&services, s, avl, tmp)
204 service_delete(s);
205 return 0;
206 }
207
208 s = avl_find_element(&services, blobmsg_data(cur), s, avl);
209 if (!s)
210 return UBUS_STATUS_NOT_FOUND;
211
212 service_delete(s);
213 return 0;
214 }
215
216 static struct ubus_method main_object_methods[] = {
217 { .name = "list", .handler = service_handle_list },
218 { .name = "set", .handler = service_handle_set },
219 { .name = "delete", .handler = service_handle_delete },
220 };
221
222 static struct ubus_object_type main_object_type =
223 UBUS_OBJECT_TYPE("service", main_object_methods);
224
225 static struct ubus_object main_object = {
226 .name = "service",
227 .type = &main_object_type,
228 .methods = main_object_methods,
229 .n_methods = ARRAY_SIZE(main_object_methods),
230 };
231
232 void procd_init_service(struct ubus_context *ctx)
233 {
234 avl_init(&services, avl_strcmp, false, NULL);
235 ubus_add_object(ctx, &main_object);
236 }