summaryrefslogtreecommitdiffstats
path: root/service.c
blob: 46f846dd40d47b383135b387498cb4d2bf868c61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include <libubox/avl-cmp.h>
#include "procd.h"
#include "service.h"
#include "instance.h"

struct avl_tree services;
static struct blob_buf b;

static void
service_instance_add(struct service *s, struct blob_attr *attr)
{
	struct service_instance *in;
	const char *name = blobmsg_name(attr);

	if (blobmsg_type(attr) != BLOBMSG_TYPE_TABLE)
		return;

	in = calloc(1, sizeof(*in));
	if (!in)
		return;

	instance_init(in, attr);
	vlist_add(&s->instances, &in->node, (void *) name);
}

static void
service_instance_update(struct vlist_tree *tree, struct vlist_node *node_new,
			struct vlist_node *node_old)
{
	struct service_instance *in_o = NULL, *in_n = NULL;

	if (node_old)
		in_o = container_of(node_old, struct service_instance, node);

	if (node_new)
		in_n = container_of(node_new, struct service_instance, node);

	if (in_o && in_n) {
		instance_update(in_o, in_n);
		instance_free(in_n);
	} else if (in_o) {
		instance_stop(in_o, false);
		instance_free(in_o);
	} else if (in_n) {
		instance_start(in_n);
	}
}

static struct service *
service_alloc(const char *name)
{
	struct service *s;

	s = calloc(1, sizeof(*s));
	vlist_init(&s->instances, avl_strcmp, service_instance_update);
	s->instances.keep_old = true;

	return s;
}

enum {
	SERVICE_ATTR_NAME,
	SERVICE_ATTR_SCRIPT,
	SERVICE_ATTR_INSTANCES,
	__SERVICE_ATTR_MAX
};

static const struct blobmsg_policy service_attrs[__SERVICE_ATTR_MAX] = {
	[SERVICE_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
	[SERVICE_ATTR_SCRIPT] = { "script", BLOBMSG_TYPE_STRING },
	[SERVICE_ATTR_INSTANCES] = { "instances", BLOBMSG_TYPE_TABLE },
};


static int
service_update(struct service *s, struct blob_attr *config, struct blob_attr **tb)
{
	struct blob_attr *old_config = s->config;
	struct blob_attr *cur;
	int rem;

	/* only the pointer changes, the content stays the same,
	 * no avl update necessary */
	s->name = s->avl.key = blobmsg_data(tb[SERVICE_ATTR_NAME]);
	s->config = config;

	if (tb[SERVICE_ATTR_INSTANCES]) {
		vlist_update(&s->instances);
		blobmsg_for_each_attr(cur, tb[SERVICE_ATTR_INSTANCES], rem) {
			service_instance_add(s, cur);
		}
		vlist_flush(&s->instances);
	}

	free(old_config);

	return 0;
}

static void
service_delete(struct service *s)
{
	vlist_flush_all(&s->instances);
	avl_delete(&services, &s->avl);
	free(s->config);
	free(s);
}

static int
service_handle_set(struct ubus_context *ctx, struct ubus_object *obj,
		   struct ubus_request_data *req, const char *method,
		   struct blob_attr *msg)
{
	struct blob_attr *tb[__SERVICE_ATTR_MAX], *cur;
	struct service *s = NULL;
	const char *name;
	int ret = UBUS_STATUS_INVALID_ARGUMENT;

	msg = blob_memdup(msg);
	if (!msg)
		return UBUS_STATUS_UNKNOWN_ERROR;

	blobmsg_parse(service_attrs, __SERVICE_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
	cur = tb[SERVICE_ATTR_NAME];
	if (!cur)
		goto free;

	name = blobmsg_data(cur);

	s = avl_find_element(&services, name, s, avl);
	if (s)
		return service_update(s, msg, tb);

	s = service_alloc(name);
	if (!s)
		return UBUS_STATUS_UNKNOWN_ERROR;

	ret = service_update(s, msg, tb);
	if (ret)
		goto free;

	avl_insert(&services, &s->avl);

	return 0;

free:
	free(msg);
	return ret;
}

static int
service_handle_list(struct ubus_context *ctx, struct ubus_object *obj,
		    struct ubus_request_data *req, const char *method,
		    struct blob_attr *msg)
{
	struct service *s;

	blob_buf_init(&b, 0);
	avl_for_each_element(&services, s, avl) {
		void *c;

		c = blobmsg_open_table(&b, s->name);
		blobmsg_close_table(&b, c);
	}

	ubus_send_reply(ctx, req, b.head);

	return 0;
}

enum {
	SERVICE_DEL_NAME,
	__SERVICE_DEL_MAX,
};

static const struct blobmsg_policy service_del_attrs[__SERVICE_DEL_MAX] = {
	[SERVICE_DEL_NAME] = { "name", BLOBMSG_TYPE_STRING },
};


static int
service_handle_delete(struct ubus_context *ctx, struct ubus_object *obj,
		    struct ubus_request_data *req, const char *method,
		    struct blob_attr *msg)
{
	struct blob_attr *tb[__SERVICE_DEL_MAX], *cur;
	struct service *s, *tmp;

	blobmsg_parse(service_del_attrs, __SERVICE_DEL_MAX, tb, blob_data(msg), blob_len(msg));

	cur = tb[SERVICE_ATTR_NAME];
	if (!cur) {
		avl_for_each_element_safe(&services, s, avl, tmp)
			service_delete(s);
		return 0;
	}

	s = avl_find_element(&services, blobmsg_data(cur), s, avl);
	if (!s)
		return UBUS_STATUS_NOT_FOUND;

	service_delete(s);
	return 0;
}

static struct ubus_method main_object_methods[] = {
	{ .name = "list", .handler = service_handle_list },
	{ .name = "set", .handler = service_handle_set },
	{ .name = "delete", .handler = service_handle_delete },
};

static struct ubus_object_type main_object_type =
	UBUS_OBJECT_TYPE("service", main_object_methods);

static struct ubus_object main_object = {
	.name = "service",
	.type = &main_object_type,
	.methods = main_object_methods,
	.n_methods = ARRAY_SIZE(main_object_methods),
};

void procd_init_service(struct ubus_context *ctx)
{
	avl_init(&services, avl_strcmp, false, NULL);
	ubus_add_object(ctx, &main_object);
}