make the socket non-blocking, explicitly wait for data using poll()
[project/ubus.git] / libubus.c
index d84e970..af8cc46 100644 (file)
--- a/libubus.c
+++ b/libubus.c
@@ -1,8 +1,22 @@
+/*
+ * Copyright (C) 2011 Felix Fietkau <nbd@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 <sys/types.h>
 #include <sys/uio.h>
 #include <sys/socket.h>
 #include <fcntl.h>
 #include <unistd.h>
+#include <poll.h>
 
 #include <libubox/blob.h>
 #include <libubox/blobmsg.h>
@@ -22,6 +36,8 @@ const char *__ubus_strerror[__UBUS_STATUS_LAST] = {
        [UBUS_STATUS_NO_DATA] = "No response",
        [UBUS_STATUS_PERMISSION_DENIED] = "Permission denied",
        [UBUS_STATUS_TIMEOUT] = "Request timed out",
+       [UBUS_STATUS_NOT_SUPPORTED] = "Operation not supported",
+       [UBUS_STATUS_UNKNOWN_ERROR] = "Unknown error",
 };
 
 static struct blob_buf b;
@@ -73,6 +89,14 @@ out:
        return err;
 }
 
+static void wait_data(int fd, bool write)
+{
+       struct pollfd pfd = { .fd = fd };
+
+       pfd.events = write ? POLLOUT : POLLIN;
+       poll(&pfd, 1, 0);
+}
+
 static int writev_retry(int fd, struct iovec *iov, int iov_len)
 {
        int len = 0;
@@ -82,9 +106,7 @@ static int writev_retry(int fd, struct iovec *iov, int iov_len)
                if (cur_len < 0) {
                        switch(errno) {
                        case EAGAIN:
-                               /* turn off non-blocking mode */
-                               fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) &
-                                     ~O_NONBLOCK);
+                               wait_data(fd, true);
                                break;
                        case EINTR:
                                break;
@@ -150,6 +172,9 @@ static bool recv_retry(int fd, struct iovec *iov, bool wait)
        int bytes;
 
        while (iov->iov_len > 0) {
+               if (wait)
+                       wait_data(fd, false);
+
                bytes = read(fd, iov->iov_base, iov->iov_len);
                if (bytes < 0) {
                        bytes = 0;
@@ -615,59 +640,29 @@ static void ubus_add_object_cb(struct ubus_request *req, int type, struct blob_a
        avl_insert(&req->ctx->objects, &obj->avl);
 }
 
-static bool ubus_push_table_data(const struct ubus_signature **sig, int *rem, bool array)
+static void ubus_push_method_data(const struct ubus_method *m)
 {
-       const struct ubus_signature *cur;
-       bool nest_type;
-       void *nest;
+       void *mtbl;
+       int i;
 
-       while (rem) {
-               cur = (*sig)++;
-               (*rem)--;
-               switch(cur->type) {
-               case UBUS_SIGNATURE_END:
-                       return !array;
-               case BLOBMSG_TYPE_INT32:
-               case BLOBMSG_TYPE_STRING:
-                       blobmsg_add_u32(&b, cur->name, cur->type);
-                       break;
-               case BLOBMSG_TYPE_TABLE:
-               case BLOBMSG_TYPE_ARRAY:
-                       nest_type = cur->type == BLOBMSG_TYPE_ARRAY;
-                       nest = blobmsg_open_nested(&b, cur->name, nest_type);
-                       if (!ubus_push_table_data(sig, rem, nest_type))
-                               return false;
-                       blobmsg_close_table(&b, nest);
-                       break;
-               default:
-                       return false;
-               }
-               if (array)
-                       return true;
-       }
-       return false;
+       mtbl = blobmsg_open_table(&b, m->name);
+
+       for (i = 0; i < m->n_policy; i++)
+               blobmsg_add_u32(&b, m->policy[i].name, m->policy[i].type);
+
+       blobmsg_close_table(&b, mtbl);
 }
 
-static bool ubus_push_object_type(struct ubus_object_type *type)
+static bool ubus_push_object_type(const struct ubus_object_type *type)
 {
-       void *s, *m;
-       int rem = type->n_signature;
-       const struct ubus_signature *sig = type->signature;
+       void *s;
+       int i;
 
        s = blob_nest_start(&b, UBUS_ATTR_SIGNATURE);
-       while (rem) {
-               if (sig->type != UBUS_SIGNATURE_METHOD)
-                       return false;
-
-               m = blobmsg_open_table(&b, sig->name);
 
-               sig++;
-               rem--;
-               if (!ubus_push_table_data(&sig, &rem, false))
-                       return false;
+       for (i = 0; i < type->n_methods; i++)
+               ubus_push_method_data(&type->methods[i]);
 
-               blobmsg_close_table(&b, m);
-       }
        blob_nest_end(&b, s);
 
        return true;
@@ -875,6 +870,8 @@ struct ubus_context *ubus_connect(const char *path)
        if (!ctx->local_id)
                goto error_close;
 
+       fcntl(ctx->sock.fd, F_SETFL, fcntl(ctx->sock.fd, F_GETFL) | O_NONBLOCK);
+
        return ctx;
 
 error_free_buf: