allow instances to register ubus object that should be watched
authorJohn Crispin <blogic@openwrt.org>
Wed, 4 Jun 2014 11:46:28 +0000 (12:46 +0100)
committerJohn Crispin <blogic@openwrt.org>
Thu, 5 Jun 2014 10:45:15 +0000 (11:45 +0100)
Signed-off-by: John Crispin <blogic@openwrt.org>
CMakeLists.txt
procd.h
service/instance.c
service/trigger.c
service/watch.c [new file with mode: 0644]
ubus.c

index c50b05d559ba62cad897b210b7f474824122a188..ace2b51c7bbddcefb7c950bd365cc0a4dc34b2ae 100644 (file)
@@ -11,7 +11,7 @@ IF(APPLE)
 ENDIF()
 
 SET(SOURCES procd.c signal.c watchdog.c state.c        inittab.c rcS.c ubus.c system.c
-       service/service.c service/instance.c service/validate.c service/trigger.c
+       service/service.c service/instance.c service/validate.c service/trigger.c service/watch.c
        plug/coldplug.c plug/hotplug.c utils/utils.c)
 
 find_library(json NAMES json-c json)
@@ -42,6 +42,13 @@ INSTALL(TARGETS udevtrigger
 )
 
 
+ADD_EXECUTABLE(upgraded upgraded.c watchdog.c)
+TARGET_LINK_LIBRARIES(upgraded ubox)
+INSTALL(TARGETS upgraded
+       RUNTIME DESTINATION sbin
+)
+
+
 ADD_EXECUTABLE(askfirst utils/askfirst.c)
 INSTALL(TARGETS askfirst
        RUNTIME DESTINATION sbin
diff --git a/procd.h b/procd.h
index 62f45b8faf72aa505f2518d9891290ce77ed3e65..4ae0dc9a7e01507db603a6551032ee93e4323169 100644 (file)
--- a/procd.h
+++ b/procd.h
@@ -47,8 +47,12 @@ void procd_bcast_event(char *event, struct blob_attr *msg);
 
 struct trigger;
 void trigger_init(void);
-void trigger_event(char *type, struct blob_attr *data);
+void trigger_event(const char *type, struct blob_attr *data);
 void trigger_add(struct blob_attr *rule, void *id);
 void trigger_del(void *id);
 
+void watch_add(const char *_name, void *id);
+void watch_del(void *id);
+void watch_ubus(struct ubus_context *ctx);
+
 #endif
index 4a457593b22019144b585a96ee467699401de9e4..fa3594b710e0d453f8c6443f0e0557e060c5cf20 100644 (file)
@@ -38,6 +38,7 @@ enum {
        INSTANCE_ATTR_RESPAWN,
        INSTANCE_ATTR_NICE,
        INSTANCE_ATTR_LIMITS,
+       INSTANCE_ATTR_WATCH,
        __INSTANCE_ATTR_MAX
 };
 
@@ -51,6 +52,7 @@ static const struct blobmsg_policy instance_attr[__INSTANCE_ATTR_MAX] = {
        [INSTANCE_ATTR_RESPAWN] = { "respawn", BLOBMSG_TYPE_ARRAY },
        [INSTANCE_ATTR_NICE] = { "nice", BLOBMSG_TYPE_INT32 },
        [INSTANCE_ATTR_LIMITS] = { "limits", BLOBMSG_TYPE_TABLE },
+       [INSTANCE_ATTR_WATCH] = { "watch", BLOBMSG_TYPE_ARRAY },
 };
 
 struct instance_netdev {
@@ -414,6 +416,15 @@ instance_config_parse(struct service_instance *in)
                trigger_add(in->trigger, in);
        }
 
+       if (tb[INSTANCE_ATTR_WATCH]) {
+               blobmsg_for_each_attr(cur2, tb[INSTANCE_ATTR_WATCH], rem) {
+                       if (blobmsg_type(cur2) != BLOBMSG_TYPE_STRING)
+                               continue;
+                       DEBUG(3, "watch for %s\n", blobmsg_get_string(cur2));
+                       watch_add(blobmsg_get_string(cur2), in);
+               }
+       }
+
        if ((cur = tb[INSTANCE_ATTR_NICE])) {
                in->nice = (int8_t) blobmsg_get_u32(cur);
                if (in->nice < -20 || in->nice > 20)
@@ -494,6 +505,7 @@ instance_free(struct service_instance *in)
        uloop_process_delete(&in->proc);
        uloop_timeout_cancel(&in->timeout);
        trigger_del(in);
+       watch_del(in);
        free(in->trigger);
        instance_config_cleanup(in);
        free(in->config);
index 480367b05bc37c8ac7766db9fbe8829166b1f1cb..9fb77cd361ac6e0d75b8349ab702c7565eebe94c 100644 (file)
@@ -323,7 +323,7 @@ void trigger_init(void)
        q.max_running_tasks = 1;
 }
 
-void trigger_event(char *type, struct blob_attr *data)
+void trigger_event(const char *type, struct blob_attr *data)
 {
        struct trigger *t;
 
diff --git a/service/watch.c b/service/watch.c
new file mode 100644 (file)
index 0000000..1d7cf78
--- /dev/null
@@ -0,0 +1,124 @@
+/*
+ * 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 <stdlib.h>
+#include <unistd.h>
+
+#include <libubox/blobmsg_json.h>
+
+#include "../procd.h"
+
+struct watch_object {
+       struct list_head list;
+
+       void *id;
+       char *name;
+};
+
+struct watch_subscribe {
+       struct uloop_timeout t;
+       uint32_t id;
+};
+
+static struct ubus_event_handler watch_event;
+static struct ubus_subscriber watch_subscribe;
+static LIST_HEAD(watch_objects);
+
+static void watch_subscribe_cb(struct ubus_context *ctx, struct ubus_event_handler *ev,
+               const char *type, struct blob_attr *msg)
+{
+       static const struct blobmsg_policy policy = {
+               "path", BLOBMSG_TYPE_STRING
+       };
+       struct watch_object *o;
+       struct blob_attr *attr;
+       const char *path;
+
+       DEBUG(3, "ubus event %s\n", type);
+       if (strcmp(type, "ubus.object.add") != 0)
+               return;
+
+       blobmsg_parse(&policy, 1, &attr, blob_data(msg), blob_len(msg));
+       if (!attr)
+               return;
+
+       path = blobmsg_data(attr);
+       DEBUG(3, "ubus path %s\n", path);
+
+       list_for_each_entry(o, &watch_objects, list) {
+               unsigned int id;
+
+               if (strcmp(o->name, path))
+                       continue;
+               if (ubus_lookup_id(ctx, path, &id))
+                       continue;
+               if (!ubus_subscribe(ctx, &watch_subscribe, id))
+                       return;
+               ERROR("failed to suscribe %d\n", id);
+       }
+}
+
+void
+watch_add(const char *_name, void *id)
+{
+       int len = strlen(_name);
+       char *name;
+       struct watch_object *o = calloc_a(sizeof(*o), &name, len + 1);
+
+       o->name = name;
+       strcpy(name, _name);
+       o->id = id;
+       list_add(&o->list, &watch_objects);
+}
+
+void
+watch_del(void *id)
+{
+       struct watch_object *t, *n;
+
+       list_for_each_entry_safe(t, n, &watch_objects, list) {
+               if (t->id != id)
+                       continue;
+               list_del(&t->list);
+               free(t);
+       }
+}
+
+static int
+watch_notify_cb(struct ubus_context *ctx, struct ubus_object *obj,
+               struct ubus_request_data *req, const char *method,
+               struct blob_attr *msg)
+{
+       if (1 || debug >= 3) {
+               char *str;
+
+               str = blobmsg_format_json(msg, true);
+               LOG("Received ubus notify '%s': %s\n", method, str);
+               free(str);
+       }
+
+       trigger_event(method, msg);
+       return 0;
+}
+
+void
+watch_ubus(struct ubus_context *ctx)
+{
+       watch_event.cb = watch_subscribe_cb;
+       watch_subscribe.cb = watch_notify_cb;
+       if (ubus_register_event_handler(ctx, &watch_event, "ubus.object.add"))
+               ERROR("failed to add ubus event handler\n");
+       if (ubus_register_subscriber(ctx, &watch_subscribe))
+               ERROR("failed to register ubus subscriber\n");
+}
diff --git a/ubus.c b/ubus.c
index 6166254a9af8cba5a2438a907c0d776f6b5987fe..bb3908e826b3bf83873e56ef8b5e51426530d8e4 100644 (file)
--- a/ubus.c
+++ b/ubus.c
@@ -53,6 +53,7 @@ ubus_connect_cb(struct uloop_timeout *timeout)
        ctx->connection_lost = ubus_disconnect_cb;
        ubus_init_service(ctx);
        ubus_init_system(ctx);
+       watch_ubus(ctx);
 
        DEBUG(2, "Connected to ubus, id=%08x\n", ctx->local_id);
        ubus_add_uloop(ctx);