add service_validator support
[project/procd.git] / service.c
index 8aae975..c5f5bf3 100644 (file)
--- a/service.c
+++ b/service.c
  * GNU General Public License for more details.
  */
 
+#include <libubox/blobmsg_json.h>
 #include <libubox/avl-cmp.h>
 #include "procd.h"
 #include "service.h"
 #include "instance.h"
+#include "rcS.h"
 
 struct avl_tree services;
 static struct blob_buf b;
@@ -54,7 +56,7 @@ service_instance_update(struct vlist_tree *tree, struct vlist_node *node_new,
                instance_free(in_n);
        } else if (in_o) {
                DEBUG(1, "Free instance %s::%s\n", in_o->srv->name, in_o->name);
-               instance_stop(in_o, false);
+               instance_stop(in_o);
                instance_free(in_o);
        } else if (in_n) {
                DEBUG(1, "Create instance %s::%s\n", in_n->srv->name, in_n->name);
@@ -75,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;
 }
@@ -84,6 +87,7 @@ enum {
        SERVICE_SET_SCRIPT,
        SERVICE_SET_INSTANCES,
        SERVICE_SET_TRIGGER,
+       SERVICE_SET_VALIDATE,
        __SERVICE_SET_MAX
 };
 
@@ -92,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
@@ -100,7 +105,26 @@ service_update(struct service *s, struct blob_attr *config, struct blob_attr **t
        struct blob_attr *cur;
        int rem;
 
-       s->trigger = tb[SERVICE_SET_TRIGGER];
+       if (s->trigger) {
+               trigger_del(s);
+               free(s->trigger);
+               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)
+                       return -1;
+               memcpy(s->trigger, tb[SERVICE_SET_TRIGGER], blob_pad_len(tb[SERVICE_SET_TRIGGER]));
+               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)
@@ -112,6 +136,8 @@ service_update(struct service *s, struct blob_attr *config, struct blob_attr **t
                        vlist_flush(&s->instances);
        }
 
+       rc(s->name, "running");
+
        return 0;
 }
 
@@ -120,8 +146,11 @@ service_delete(struct service *s)
 {
        vlist_flush_all(&s->instances);
        avl_delete(&services, &s->avl);
-       free(s->config);
+       trigger_del(s);
+       s->trigger = NULL;
+       free(s->trigger);
        free(s);
+       service_validate_del(s);
 }
 
 enum {
@@ -164,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,
@@ -212,16 +254,21 @@ service_dump(struct service *s, int verbose)
        struct service_instance *in;
        void *c, *i;
 
-       if (avl_is_empty(&s->instances.avl))
+       c = blobmsg_open_table(&b, s->name);
+
+       if (avl_is_empty(&s->instances.avl)) {
+               blobmsg_close_table(&b, c);
                return;
+       }
 
-       c = blobmsg_open_table(&b, s->name);
        i = blobmsg_open_table(&b, "instances");
-       if (verbose && s->trigger)
-               blobmsg_add_blob(&b, s->trigger);
        vlist_for_each_element(&s->instances, in, node)
                instance_dump(&b, in, 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);
 }
 
@@ -329,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),
@@ -337,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 =
@@ -351,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();
+}
+