the delete handle should return an error if no service is named instead of killing...
[project/procd.git] / service.c
index 8dbb1e6..b5f4f3e 100644 (file)
--- a/service.c
+++ b/service.c
@@ -1,3 +1,17 @@
+/*
+ * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
+ * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1
+ * as published by the Free Software Foundation
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
 #include <libubox/avl-cmp.h>
 #include "procd.h"
 #include "service.h"
@@ -35,15 +49,15 @@ service_instance_update(struct vlist_tree *tree, struct vlist_node *node_new,
                in_n = container_of(node_new, struct service_instance, node);
 
        if (in_o && in_n) {
-               DPRINTF("Update instance %s::%s\n", in_o->srv->name, in_o->name);
+               DEBUG(1, "Update instance %s::%s\n", in_o->srv->name, in_o->name);
                instance_update(in_o, in_n);
                instance_free(in_n);
        } else if (in_o) {
-               DPRINTF("Free instance %s::%s\n", in_o->srv->name, in_o->name);
+               DEBUG(1, "Free instance %s::%s\n", in_o->srv->name, in_o->name);
                instance_stop(in_o, false);
                instance_free(in_o);
        } else if (in_n) {
-               DPRINTF("Create instance %s::%s\n", in_n->srv->name, in_n->name);
+               DEBUG(1, "Create instance %s::%s\n", in_n->srv->name, in_n->name);
                instance_start(in_n);
        }
 }
@@ -54,9 +68,7 @@ service_alloc(const char *name)
        struct service *s;
        char *new_name;
 
-       s = calloc(1, sizeof(*s) + strlen(name) + 1);
-
-       new_name = (char *) (s + 1);
+       s = calloc_a(sizeof(*s), &new_name, strlen(name) + 1);
        strcpy(new_name, name);
 
        vlist_init(&s->instances, avl_strcmp, service_instance_update);
@@ -71,6 +83,7 @@ enum {
        SERVICE_SET_NAME,
        SERVICE_SET_SCRIPT,
        SERVICE_SET_INSTANCES,
+       SERVICE_SET_TRIGGER,
        __SERVICE_SET_MAX
 };
 
@@ -78,15 +91,17 @@ static const struct blobmsg_policy service_set_attrs[__SERVICE_SET_MAX] = {
        [SERVICE_SET_NAME] = { "name", BLOBMSG_TYPE_STRING },
        [SERVICE_SET_SCRIPT] = { "script", BLOBMSG_TYPE_STRING },
        [SERVICE_SET_INSTANCES] = { "instances", BLOBMSG_TYPE_TABLE },
+       [SERVICE_SET_TRIGGER] = { "triggers", BLOBMSG_TYPE_ARRAY },
 };
 
-
 static int
 service_update(struct service *s, struct blob_attr *config, struct blob_attr **tb, bool add)
 {
        struct blob_attr *cur;
        int rem;
 
+       s->trigger = tb[SERVICE_SET_TRIGGER];
+
        if (tb[SERVICE_SET_INSTANCES]) {
                if (!add)
                        vlist_update(&s->instances);
@@ -129,6 +144,25 @@ static const struct blobmsg_policy service_del_attrs[__SERVICE_DEL_ATTR_MAX] = {
        [SERVICE_DEL_ATTR_INSTANCE] = { "instance", BLOBMSG_TYPE_STRING },
 };
 
+enum {
+       SERVICE_LIST_ATTR_VERBOSE,
+       __SERVICE_LIST_ATTR_MAX,
+};
+
+static const struct blobmsg_policy service_list_attrs[__SERVICE_LIST_ATTR_MAX] = {
+       [SERVICE_LIST_ATTR_VERBOSE] = { "verbose", BLOBMSG_TYPE_INT32 },
+};
+
+enum {
+       EVENT_TYPE,
+       EVENT_DATA,
+       __EVENT_MAX
+};
+
+static const struct blobmsg_policy event_policy[__EVENT_MAX] = {
+       [EVENT_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
+       [EVENT_DATA] = { .name = "data", .type = BLOBMSG_TYPE_TABLE },
+};
 
 static int
 service_handle_set(struct ubus_context *ctx, struct ubus_object *obj,
@@ -150,11 +184,11 @@ service_handle_set(struct ubus_context *ctx, struct ubus_object *obj,
 
        s = avl_find_element(&services, name, s, avl);
        if (s) {
-               DPRINTF("Update service %s\n", name);
+               DEBUG(1, "Update service %s\n", name);
                return service_update(s, msg, tb, add);
        }
 
-       DPRINTF("Create service %s\n", name);
+       DEBUG(1, "Create service %s\n", name);
        s = service_alloc(name);
        if (!s)
                return UBUS_STATUS_UNKNOWN_ERROR;
@@ -173,15 +207,20 @@ free:
 }
 
 static void
-service_dump(struct service *s)
+service_dump(struct service *s, int verbose)
 {
        struct service_instance *in;
        void *c, *i;
 
+       if (avl_is_empty(&s->instances.avl))
+               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);
+               instance_dump(&b, in, verbose);
        blobmsg_close_table(&b, i);
        blobmsg_close_table(&b, c);
 }
@@ -191,11 +230,18 @@ service_handle_list(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_LIST_ATTR_MAX];
        struct service *s;
+       int verbose = 0;
+
+       blobmsg_parse(service_list_attrs, __SERVICE_LIST_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
+
+       if (tb[SERVICE_LIST_ATTR_VERBOSE] && blobmsg_get_u32(tb[SERVICE_LIST_ATTR_VERBOSE]))
+               verbose = 1;
 
        blob_buf_init(&b, 0);
        avl_for_each_element(&services, s, avl)
-               service_dump(s);
+               service_dump(s, verbose);
 
        ubus_send_reply(ctx, req, b.head);
 
@@ -208,17 +254,14 @@ service_handle_delete(struct ubus_context *ctx, struct ubus_object *obj,
                    struct blob_attr *msg)
 {
        struct blob_attr *tb[__SERVICE_DEL_ATTR_MAX], *cur;
-       struct service *s, *tmp;
+       struct service *s;
        struct service_instance *in;
 
        blobmsg_parse(service_del_attrs, __SERVICE_DEL_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
 
        cur = tb[SERVICE_DEL_ATTR_NAME];
-       if (!cur) {
-               avl_for_each_element_safe(&services, s, avl, tmp)
-                       service_delete(s);
-               return 0;
-       }
+       if (!cur)
+               return UBUS_STATUS_NOT_FOUND;
 
        s = avl_find_element(&services, blobmsg_data(cur), s, avl);
        if (!s)
@@ -232,7 +275,7 @@ service_handle_delete(struct ubus_context *ctx, struct ubus_object *obj,
 
        in = vlist_find(&s->instances, blobmsg_data(cur), in, node);
        if (!in) {
-               fprintf(stderr, "instance %s not found\n", (char *) blobmsg_data(cur));
+               ERROR("instance %s not found\n", (char *) blobmsg_data(cur));
                return UBUS_STATUS_NOT_FOUND;
        }
 
@@ -267,6 +310,52 @@ service_handle_update(struct ubus_context *ctx, struct ubus_object *obj,
        return 0;
 }
 
+static int
+service_handle_event(struct ubus_context *ctx, struct ubus_object *obj,
+                       struct ubus_request_data *req, const char *method,
+                       struct blob_attr *msg)
+{
+       struct blob_attr *tb[__EVENT_MAX];
+
+       if (!msg)
+               return UBUS_STATUS_INVALID_ARGUMENT;
+
+       blobmsg_parse(event_policy, __EVENT_MAX, tb, blob_data(msg), blob_len(msg));
+       if (!tb[EVENT_TYPE] || !tb[EVENT_DATA])
+               return UBUS_STATUS_INVALID_ARGUMENT;
+
+       trigger_event(blobmsg_get_string(tb[EVENT_TYPE]), tb[EVENT_DATA]);
+
+       return 0;
+}
+
+enum {
+       TRIGGER_ATTR,
+       __TRIGGER_MAX
+};
+
+static const struct blobmsg_policy trigger_policy[__TRIGGER_MAX] = {
+       [TRIGGER_ATTR] = { .name = "triggers", .type = BLOBMSG_TYPE_ARRAY },
+};
+
+static int service_handle_trigger(struct ubus_context *ctx, struct ubus_object *obj,
+                       struct ubus_request_data *req, const char *method,
+                       struct blob_attr *msg)
+{
+       struct blob_attr *tb[__TRIGGER_MAX];
+
+       if (!msg)
+               return UBUS_STATUS_INVALID_ARGUMENT;
+
+       blobmsg_parse(trigger_policy, __TRIGGER_MAX, tb, blob_data(msg), blob_len(msg));
+       if (!tb[TRIGGER_ATTR])
+               return UBUS_STATUS_INVALID_ARGUMENT;
+
+       trigger_add(tb[TRIGGER_ATTR], NULL);
+
+       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),
@@ -274,6 +363,8 @@ static struct ubus_method main_object_methods[] = {
        UBUS_METHOD("delete", service_handle_delete, service_del_attrs),
        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("trigger", service_handle_trigger, trigger_policy),
 };
 
 static struct ubus_object_type main_object_type =
@@ -286,7 +377,7 @@ static struct ubus_object main_object = {
        .n_methods = ARRAY_SIZE(main_object_methods),
 };
 
-void procd_init_service(struct ubus_context *ctx)
+void ubus_init_service(struct ubus_context *ctx)
 {
        avl_init(&services, avl_strcmp, false, NULL);
        ubus_add_object(ctx, &main_object);