add options to override the ubus socket name for the cli and the example program
[project/ubus.git] / libubus.c
index 12bdb37..8749d7c 100644 (file)
--- a/libubus.c
+++ b/libubus.c
@@ -27,6 +27,7 @@ const char *__ubus_strerror[__UBUS_STATUS_LAST] = {
        [UBUS_STATUS_METHOD_NOT_FOUND] = "Method not found",
        [UBUS_STATUS_NOT_FOUND] = "Not found",
        [UBUS_STATUS_NO_DATA] = "No response",
+       [UBUS_STATUS_PERMISSION_DENIED] = "Permission denied",
 };
 
 static struct blob_buf b;
@@ -123,6 +124,8 @@ static bool recv_retry(int fd, struct iovec *iov, bool wait)
                bytes = read(fd, iov->iov_base, iov->iov_len);
                if (bytes < 0) {
                        bytes = 0;
+                       if (uloop_cancelled)
+                               return false;
                        if (errno == EINTR)
                                continue;
 
@@ -306,7 +309,8 @@ static void ubus_process_invoke(struct ubus_context *ctx, struct ubus_msghdr *hd
        }
 
        for (method = 0; method < obj->n_methods; method++)
-               if (!strcmp(obj->methods[method].name,
+               if (!obj->methods[method].name ||
+                   !strcmp(obj->methods[method].name,
                            blob_data(attrbuf[UBUS_ATTR_METHOD])))
                        goto found;
 
@@ -605,21 +609,21 @@ static bool ubus_push_object_type(struct ubus_object_type *type)
        return true;
 }
 
-int ubus_publish(struct ubus_context *ctx, struct ubus_object *obj)
+static int __ubus_publish(struct ubus_context *ctx, struct ubus_object *obj)
 {
        struct ubus_request req;
        int ret;
 
-       if (obj->id || !obj->name || !obj->type)
-               return UBUS_STATUS_INVALID_ARGUMENT;
-
        blob_buf_init(&b, 0);
-       blob_put_string(&b, UBUS_ATTR_OBJPATH, obj->name);
 
-       if (obj->type->id)
-               blob_put_int32(&b, UBUS_ATTR_OBJTYPE, obj->type->id);
-       else if (!ubus_push_object_type(obj->type))
-               return UBUS_STATUS_INVALID_ARGUMENT;
+       if (obj->name && obj->type) {
+               blob_put_string(&b, UBUS_ATTR_OBJPATH, obj->name);
+
+               if (obj->type->id)
+                       blob_put_int32(&b, UBUS_ATTR_OBJTYPE, obj->type->id);
+               else if (!ubus_push_object_type(obj->type))
+                       return UBUS_STATUS_INVALID_ARGUMENT;
+       }
 
        ubus_start_request(ctx, &req, b.head, UBUS_MSG_PUBLISH, 0);
        req.raw_data_cb = ubus_publish_cb;
@@ -634,6 +638,64 @@ int ubus_publish(struct ubus_context *ctx, struct ubus_object *obj)
        return 0;
 }
 
+int ubus_publish(struct ubus_context *ctx, struct ubus_object *obj)
+{
+       if (!obj->name || !obj->type)
+               return UBUS_STATUS_INVALID_ARGUMENT;
+
+       return __ubus_publish(ctx, obj);
+}
+
+static int ubus_event_cb(struct ubus_context *ctx, struct ubus_object *obj,
+                        struct ubus_request_data *req,
+                        const char *method, struct blob_attr *msg)
+{
+       struct ubus_event_handler *ev;
+
+       ev = container_of(obj, struct ubus_event_handler, obj);
+       ev->cb(ctx, ev, method, msg);
+       return 0;
+}
+
+static const struct ubus_method event_method = {
+       .name = NULL,
+       .handler = ubus_event_cb,
+};
+
+int ubus_register_event_handler(struct ubus_context *ctx,
+                               struct ubus_event_handler *ev,
+                               const char *pattern)
+{
+       struct ubus_object *obj = &ev->obj;
+       struct blob_buf b2;
+       int ret;
+
+       if (!obj->id) {
+               obj->methods = &event_method;
+               obj->n_methods = 1;
+
+               if (!!obj->name ^ !!obj->type)
+                       return UBUS_STATUS_INVALID_ARGUMENT;
+
+               ret = __ubus_publish(ctx, obj);
+               if (ret)
+                       return ret;
+       }
+
+       /* use a second buffer, ubus_invoke() overwrites the primary one */
+       memset(&b2, 0, sizeof(b2));
+       blob_buf_init(&b2, 0);
+       blobmsg_add_u32(&b2, "object", obj->id);
+       if (pattern)
+               blobmsg_add_string(&b2, "pattern", pattern);
+
+       ret = ubus_invoke(ctx, UBUS_SYSTEM_OBJECT_EVENT, "register", b2.head,
+                         NULL, NULL);
+
+       return 0;
+}
+
+
 void ubus_default_connection_lost(struct ubus_context *ctx)
 {
        if (ctx->sock.registered)