+/*
+ * 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>
[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;
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;
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;
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;
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;
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: