summaryrefslogtreecommitdiffstats
path: root/service.c
diff options
context:
space:
mode:
authorJohn Crispin <blogic@openwrt.org>2013-11-11 19:28:13 +0100
committerJohn Crispin <blogic@openwrt.org>2013-11-12 10:32:32 +0100
commitf9d31edb8938341b9217ee4c14eb58111414eb97 (patch)
treeeeb48ec34c2f8a30df50de201b5f103e991466f0 /service.c
parentcd4af8eb78c30718f999c487c93e257bbd969cb1 (diff)
downloadunitd-f9d31edb8938341b9217ee4c14eb58111414eb97.tar
unitd-f9d31edb8938341b9217ee4c14eb58111414eb97.zip
add service_validator support
Signed-off-by: John Crispin <blogic@openwrt.org>
Diffstat (limited to 'service.c')
-rw-r--r--service.c64
1 files changed, 63 insertions, 1 deletions
diff --git a/service.c b/service.c
index 29acc57..c5f5bf3 100644
--- a/service.c
+++ b/service.c
@@ -77,6 +77,7 @@ service_alloc(const char *name)
s->instances.keep_old = true;
s->name = new_name;
s->avl.key = s->name;
+ INIT_LIST_HEAD(&s->validators);
return s;
}
@@ -86,6 +87,7 @@ enum {
SERVICE_SET_SCRIPT,
SERVICE_SET_INSTANCES,
SERVICE_SET_TRIGGER,
+ SERVICE_SET_VALIDATE,
__SERVICE_SET_MAX
};
@@ -94,6 +96,7 @@ static const struct blobmsg_policy service_set_attrs[__SERVICE_SET_MAX] = {
[SERVICE_SET_SCRIPT] = { "script", BLOBMSG_TYPE_STRING },
[SERVICE_SET_INSTANCES] = { "instances", BLOBMSG_TYPE_TABLE },
[SERVICE_SET_TRIGGER] = { "triggers", BLOBMSG_TYPE_ARRAY },
+ [SERVICE_SET_VALIDATE] = { "validate", BLOBMSG_TYPE_ARRAY },
};
static int
@@ -108,6 +111,8 @@ service_update(struct service *s, struct blob_attr *config, struct blob_attr **t
s->trigger = NULL;
}
+ service_validate_del(s);
+
if (tb[SERVICE_SET_TRIGGER] && blobmsg_data_len(tb[SERVICE_SET_TRIGGER])) {
s->trigger = malloc(blob_pad_len(tb[SERVICE_SET_TRIGGER]));
if (!s->trigger)
@@ -116,6 +121,11 @@ service_update(struct service *s, struct blob_attr *config, struct blob_attr **t
trigger_add(s->trigger, s);
}
+ if (tb[SERVICE_SET_VALIDATE] && blobmsg_data_len(tb[SERVICE_SET_VALIDATE])) {
+ blobmsg_for_each_attr(cur, tb[SERVICE_SET_VALIDATE], rem)
+ service_validate_add(s, cur);
+ }
+
if (tb[SERVICE_SET_INSTANCES]) {
if (!add)
vlist_update(&s->instances);
@@ -140,6 +150,7 @@ service_delete(struct service *s)
s->trigger = NULL;
free(s->trigger);
free(s);
+ service_validate_del(s);
}
enum {
@@ -182,6 +193,19 @@ static const struct blobmsg_policy event_policy[__EVENT_MAX] = {
[EVENT_DATA] = { .name = "data", .type = BLOBMSG_TYPE_TABLE },
};
+enum {
+ VALIDATE_PACKAGE,
+ VALIDATE_TYPE,
+ VALIDATE_SERVICE,
+ __VALIDATE_MAX
+};
+
+static const struct blobmsg_policy validate_policy[__VALIDATE_MAX] = {
+ [VALIDATE_PACKAGE] = { .name = "package", .type = BLOBMSG_TYPE_STRING },
+ [VALIDATE_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
+ [VALIDATE_SERVICE] = { .name = "service", .type = BLOBMSG_TYPE_STRING },
+};
+
static int
service_handle_set(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
@@ -243,6 +267,8 @@ service_dump(struct service *s, int verbose)
blobmsg_close_table(&b, i);
if (verbose && s->trigger)
blobmsg_add_blob(&b, s->trigger);
+ if (verbose && !list_empty(&s->validators))
+ service_validate_dump(&b, s);
blobmsg_close_table(&b, c);
}
@@ -350,6 +376,34 @@ service_handle_event(struct ubus_context *ctx, struct ubus_object *obj,
return 0;
}
+static int
+service_handle_validate(struct ubus_context *ctx, struct ubus_object *obj,
+ struct ubus_request_data *req, const char *method,
+ struct blob_attr *msg)
+{
+ struct blob_attr *tb[__VALIDATE_MAX];
+ char *p = NULL, *t = NULL;
+
+ if (!msg)
+ return UBUS_STATUS_INVALID_ARGUMENT;
+
+ blobmsg_parse(validate_policy, __VALIDATE_MAX, tb, blob_data(msg), blob_len(msg));
+ if (tb[VALIDATE_SERVICE]) {
+ return 0;
+ }
+ if (tb[VALIDATE_PACKAGE])
+ p = blobmsg_get_string(tb[VALIDATE_PACKAGE]);
+
+ if (tb[VALIDATE_TYPE])
+ t = blobmsg_get_string(tb[VALIDATE_TYPE]);
+
+ blob_buf_init(&b, 0);
+ service_validate_dump_all(&b, p, t);
+ ubus_send_reply(ctx, req, b.head);
+
+ return 0;
+}
+
static struct ubus_method main_object_methods[] = {
UBUS_METHOD("set", service_handle_set, service_set_attrs),
UBUS_METHOD("add", service_handle_set, service_set_attrs),
@@ -358,6 +412,7 @@ static struct ubus_method main_object_methods[] = {
UBUS_METHOD("update_start", service_handle_update, service_attrs),
UBUS_METHOD("update_complete", service_handle_update, service_attrs),
UBUS_METHOD("event", service_handle_event, event_policy),
+ UBUS_METHOD("validate", service_handle_validate, validate_policy),
};
static struct ubus_object_type main_object_type =
@@ -372,6 +427,13 @@ static struct ubus_object main_object = {
void ubus_init_service(struct ubus_context *ctx)
{
- avl_init(&services, avl_strcmp, false, NULL);
ubus_add_object(ctx, &main_object);
}
+
+void
+service_init(void)
+{
+ avl_init(&services, avl_strcmp, false, NULL);
+ service_validate_init();
+}
+