Commit graph

335 commits

Author SHA1 Message Date
Alexandru Ardelean
27d712d394 ubusd_monitor: alloc & free the buffer outside of the loop
Should save a few cycles, since the data that's
being changed is only the seq number.
And the `ub` is always created as shared.

Signed-off-by: Alexandru Ardelean <ardeleanalex@gmail.com>
2017-11-13 09:57:55 +01:00
Alexandru Ardelean
be146ad2bc ubusd: rename goto label from error to out
Semantic has changed a bit.

Signed-off-by: Alexandru Ardelean <ardeleanalex@gmail.com>
2017-11-13 09:55:48 +01:00
Alexandru Ardelean
e02813b2cc ubusd: don't free messages in ubus_send_msg() anymore
This makes it clear that `ubus_msg_send()` is only
about sending and queue-ing messages, and has nothing
to do with free-ing.

It can be a bit misleading/confusing when trying to go
through the code and make assumptions about whether a
buffer is free'd in ubus_send_msg(), or is free'd outside.

In `ubusd_proto_receive_message()` the `ubus_msg_free()`
is now called before the `if (ret == -1)` check.
That way, all callbacks will have their messages free'd,
which is what's desired, but confusing, because:
* ubusd_handle_invoke() called ubus_msg_free() before returning -1
* ubusd_handle_notify() called ubus_msg_free() before returning -1
* ubusd_handle_response() called ubus_msg_send(,,free=true) before returning -1
* ubus_msg_send() would call ubus_msg_send(,,free=false)
* all other callback callers would `ubus_msg_send(,,free=true)`
  (free the buffers in ubus_msg_send() )

In all other places, where `ubus_msg_send(,,free=true)`
an explicit `ubus_msg_free()` was added.

Signed-off-by: Alexandru Ardelean <ardeleanalex@gmail.com>
2017-11-13 09:55:19 +01:00
Bob Ham
6d1ea6c33d libubus: Fix deletion from context's object AVL tree when removing object
Objects are stored in the ubus context in an AVL tree.  An AVL tree
node contains a pointer to a key value.  For the ubus context, this
points to the id member of the object structure.  In
ubus_remove_object_cb, the id member is set to zero and then after,
avl_delete is called and fails.  To fix this, we call avl_delete
before setting the object id to zero.

Signed-off-by: Bob Ham <bob.ham@tomltd.co.uk>
2017-11-13 09:51:40 +01:00
Rosen Penev
9c13096b16 ubus: Remove unnecessary memset calls.
Replace malloc+memset with calloc.

Signed-off-by: Rosen Penev <rosenp@gmail.com>
Signed-off-by: Felix Fietkau <nbd@nbd.name>
2017-11-13 09:46:12 +01:00
John Crispin
24ffe9b582 libubus-req: add data_cb callback handling for ubus notifications
Signed-off-by: John Crispin <john@phrozen.org>
2017-11-06 08:23:08 +01:00
Hans Dedecker
34c6e818e4 cli: fix listen_timeout compile issue
Variable listen_timeout is redefined as a function resulting into
a compile issue. Define the function as ubus_cli_listen_timeout
to fix the compile issue.

Signed-off-by: Hans Dedecker <dedeckeh@gmail.com>
2017-02-20 11:32:19 +01:00
Hans Dedecker
dfe338350e libubus: reset ctx->sock.error when doing ubus reconnect
When ubus connect fails ctx->sock.eof will be set but ctx->sock.error
can be set as well. Reset ctx->sock.error as well when doing ubus
reconnect

Signed-off-by: Hans Dedecker <dedeckeh@gmail.com>
2017-02-20 11:31:56 +01:00
Felix Fietkau
6eb3c9647e cli: do not use default timeout for listen
Default to listening for events forever unless -t is explicitly
specified on the command line

Signed-off-by: Felix Fietkau <nbd@nbd.name>
2017-02-15 11:35:38 +01:00
Felix Fietkau
453b87f631 cli: add support for subscribing to objects
Signed-off-by: Felix Fietkau <nbd@nbd.name>
2017-02-14 15:58:49 +01:00
Alexandru Ardelean
c09e4f06f0 ubusd: fix incomplete copy of shared buf during queue-ing
For a shared ubus_msg_buf, the ubus_msg_ref function will
create a copy for queue-ing.

Problem is, that during the dequeue (especially) in client_cb,
the header is 0-ed (because it's was a newly alloc-ed buffer).

And during ubus_msg_writev(), the header info will be ignored
by the client.

Signed-off-by: Alexandru Ardelean <ardeleanalex@gmail.com>
2017-02-07 10:45:14 +01:00
Felix Fietkau
91acde66b9 libubus: do not modify uloop_cancelled
uloop_cancelled was used for two purposes within ubus_complete_request:
- interrupting recursive requests on SIGINT/SIGTERM
- breaking out of the poll loop in a recursive request that completed

Saving/restorung uloop_cancelled was buggy, leading to SIGTERM not being
processed properly. Simplify the logic by using a separate field for
internal use

Signed-off-by: Felix Fietkau <nbd@nbd.name>
2017-02-03 18:39:15 +01:00
Felix Fietkau
763b9b2cf2 libubus: reset ctx->sock.eof to fix reconnect issues
Signed-off-by: Felix Fietkau <nbd@nbd.name>
2017-02-03 14:13:54 +01:00
Felix Fietkau
6f4e11e1db libubus: do not register/unregister with uloop during sync requests
This was leftover code from before this codepath was converted to
polling directly

Signed-off-by: Felix Fietkau <nbd@nbd.name>
2017-01-22 14:39:34 +01:00
Felix Fietkau
cd82f9758d libubus: move uloop_init() call to ubus_connect_ctx
uloop should not be used before it is initialized

Signed-off-by: Felix Fietkau <nbd@nbd.name>
2017-01-22 14:28:49 +01:00
Mihai Richard
97ac89f972 ubusd: fix issue caused by an implicit cast
An -1 returned by ubus_msg_writev() will be interpreted as
UINT_MAX during a check to see how much data had could be
written on the socket.

Because sizeof() will return size_t it will promote the
comparsion to unsigned

Signed-off-by: Mihai Richard <mihairichard@live.com>
2017-01-20 11:27:00 +01:00
Abhimanyu Vishwakarma
ad5333a73b ubus: ubus_free: clear pending timers before freeing context
If a synchronous operation is executed on a ubus context after
uloop_done() has been called, the context's pending_timer
may remain in uloop's list of timeouts.
This leads to undefined behaviour during next execution of uloop
code, as it may be referring to unavailable memory or memory
that has been allocated for different purposes.

Signed-off-by: Marcin Nowakowski <marcin.nowakowski@imgtec.com>
Signed-off-by: Abhimanyu Vishwakarma <Abhimanyu.Vishwakarma@imgtec.com>
2017-01-04 22:02:43 +01:00
Felix Fietkau
d5fabacba1 libubus: reduce code duplication, fix indentation
Signed-off-by: Felix Fietkau <nbd@nbd.name>
2016-12-24 14:35:58 +01:00
amine ahd
df088f03c0 Allow sending a fd along a ubus_request
Signed-off-by: amine.ahd <amine.ahd@gmail.com>
2016-12-24 14:31:04 +01:00
Felix Fietkau
5ca6bae3a2 ubusd: fix id lookup of objects with path and no methods
Signed-off-by: Felix Fietkau <nbd@nbd.name>
2016-12-24 14:30:13 +01:00
Vasily Goldobin
d009a08473 lua: add 'defer_request' and 'complete_deferred_request' functions
Add Lua bindings for 'ubus_defer_request' and 'ubus_complete_deferred_request' functions with sample code.

Signed-off-by: Vasily Goldobin <vasily@lynxtech.ru>
2016-11-21 11:27:05 +01:00
Felix Fietkau
fd61773031 ubusd_proto: fix crash when trying to subscribe to system objects
Signed-off-by: Felix Fietkau <nbd@nbd.name>
2016-11-20 16:40:06 +01:00
Felix Fietkau
04e21a65ef build: remove /opt/local prefix
Signed-off-by: Felix Fietkau <nbd@nbd.name>
2016-11-20 16:26:19 +01:00
Alexandru Ardelean
312448a5b1 cli: register event handler first, then do lookup
We seem to be getting timeout for ubus wait_for calls
every now and then.

And it's not reliably reproducible.
Looking at the code the only thing that would look like
a potetntial reason, is that between the ubus_lookup() and
ubus_register_event_handler() calls, there's a very narrow
window where the event would get sent out and we would not
get it, thus having to timeout.

It doesn't look like registering the event handler first
is a big problem for the whole wait_check_object() logic.

Signed-off-by: Alexandru Ardelean <ardeleanalex@gmail.com>
2016-10-12 20:00:35 +02:00
Denis Osvald
ba45b27f8c ubusd: fix inverted check in ubusd_reply_add
Signed-off-by: Denis Osvald <denis.osvald@sartura.hr>
2016-09-05 13:59:34 +02:00
Denis Osvald
d6bb4f1a1d ubusd: don't check ACL when object is NULL
If there are any ACLs present other than global wildcard "*", the AVL
tree comparator will compare ACL key to object name. However, object
name can be NULL in cases where ACL check is done on call to internal
ubus objects (e.g. ubus monitor).

With this change we skip checking ACLs on such NULL objects.

Signed-off-by: Denis Osvald <denis.osvald@sartura.hr>
2016-08-25 13:58:11 +02:00
Felix Fietkau
686c05d738 ubusd: fix sending remove-object notification
Patch by Delio Brignoli <brignoli.delio@gmail.com>

Both ubusd_free_object (eventually via ubusd_create_object_event_msg)
and ubus_proto_send_msg_from_blob() use the same message buffer.
So ubusd_handle_remove_object builds the payload which gets (indirectly)
overwritten by the call to ubusd_free_object and then sent again by
ubus_proto_send_msg_from_blob.

Signed-off-by: Felix Fietkau <nbd@nbd.name>
2016-08-23 12:55:09 +02:00
Felix Fietkau
b405050ceb ubusd: add missing NULL pointer checks for obj->type
Signed-off-by: Felix Fietkau <nbd@nbd.name>
2016-08-23 11:36:41 +02:00
Florian Fainelli
053be7df87 cmake: Fix find_library for ubusd and examples/server
Both ubusd and cli TARGET_LINK_LIBRARIES reference ${json} which is
obtained via find_library(), but since the find_library() is searched
after the TARGET_LINK_LIBRARIES for ubusd, ubusd always gets an empty
${json} variable.

examples/server also links against libjson-c, but we were not setting
TARGET_LINK_LIBRARIES accordingly, so do that too with ${json} appended.

This was causing linking errors for ubusd and then examples/server using
an external toolchain (stbgcc-4.8-1.x).

Fixes: 9f52d1769b ("cli: use the new json-c library name")
Signed-off-by: Florian Fainelli <f.fainelli@gmail.com>
2016-07-01 15:12:28 +02:00
Rujun Wang
2f09fd7181 Fix the length of msg buffer after realloc
Signed-off-by: Rujun Wang <chinawrj@gmail.com>
2016-06-26 13:05:06 +02:00
Iain Fraser
976719d89c Implemented publish/subscribe lua bindings to libubus-lua with example lua files. 2016-06-06 14:47:10 +02:00
John Crispin
96ab0b3032 ubusd: remove systemd socket activation support
Signed-off-by: John Crispin <john@phrozen.org>
2016-06-01 11:39:34 +02:00
Matthias Schiffer
964adfdd39 ubusd: fix systemd socket activation support
62cdfc3 added systemd units including a ubus.socket unit, but didn't
actually add socket activation support to ubusd. This would cause the first
connection that activated ubusd to hang (as ubusd ignored it), and stopping
ubusd would break it completely (as ubusd removed the socket file).

The ENABLE_SYSTEMD default is changed to OFF as the socket activation uses
libsystemd, so setting ENABLE_SYSTEMD to ON will now require libsystemd.

Signed-off-by: Matthias Schiffer <mschiffer@universe-factory.net>
2016-06-01 11:32:46 +02:00
Eyal Birger
3b8d4b5653 libubus: nullify stale msgbuf pointer in case of ubus_connect_ctx() failure
If the ubus_reconnect() call fails in ubus_connect_ctx(), the msgbuf.data
newly allocated buffer is freed, but its pointer in the ubus_context is not
removed.

This leads to a double free error if ubus_auto_shutdown() is called for cleanup
after ubus_auto_connect() failed to connect to ubusd.

Signed-off-by: Eyal Birger <eyal.birger@gmail.com>
2016-05-15 09:39:24 +02:00
Petr Štetiar
fcf5d8af65 cmake: Add ubox, blobmsg_json libraries and include dirs lookup
Otherwise cmake uses files from system which sometimes isn't wanted, ie.
for testing.

Signed-off-by: Petr Å tetiar <ynezz@true.cz>
2016-03-07 09:38:04 +01:00
Zhao, Gang
b958a9aa60 cli: add timeout support for listen command
Currently 'ubus listen' command does not respect the timeout parameter,
i.e., whether timeout parameter is provided, 'ubus listen' will always
run infinitely. This patch let 'ubus listen' command support the timeout
parameter. For example, 'ubus listen -t 60' will cause the command to
exit after 60 seconds.

Signed-off-by: Zhao, Gang <gang.zhao.42@gmail.com>
2016-02-29 21:06:42 +01:00
Zhao, Gang
6cd8912f10 cli: static keyword tweak
Function ubus_cli_listen() is called only once, and function uloop_run()
and uloop_done() which need struct ubus_event_handler listener are
called within this function, so it's not needed to mark struct
ubus_event_handler listener as static. The same as pointer struct
ubus_context *ctx in main function.

Global struct commands is only used in this file, so it should be marked
static.

Signed-off-by: Zhao, Gang <gang.zhao.42@gmail.com>
2016-02-29 21:06:28 +01:00
Eyal Birger
5dfd3c16fa ubus: use network order in ubus message header fields
Changing the ubus message header fields from 'host' order to 'network' order
allows passing ubus messages between hosts with different endianity.

Example use (creating a ubus proxy):

on host A (e.g. big endian router already running ubusd), run:
$ socat TCP-LISTEN:5699,fork UNIX:/var/run/ubus.sock &

On host B (e.g. little endian development PC) run:
$ socat UNIX-LISTEN:/var/run/ubus.sock,fork TCP:<host A IP>:5699 &

Now ubus applications can be run on host B and seamlessly interact with ubus
applications on host A.

Signed-off-by: Eyal Birger <eyal.birger@gmail.com>
2016-02-28 09:56:48 +01:00
Emanuel Taube
fda009d8ba libubus.h: marking unused variables
Inform the compiler that the variables are not gona be used to avoid
compiler warnings.

Signed-off-by: Emanuel Taube <emanuel.taube@gmail.com>
2016-02-22 11:41:03 +01:00
Ben Kelly
6a86e65f7d libubus.h: add ubus_auto_shutdown()
Add ubus_auto_shutdown function, which ensures any pending uloop_timer is cancelled before calling ubus_shutdown on the context.

This avoids a condition where ubus_shutdown() is called during ubus_auto_connect attempting a reconnection.

Signed-off-by: Ben Kelly <ben@benjii.net>
2016-02-07 14:24:28 +01:00
Felix Fietkau
619f3a160d ubusd: fix a memory leak on user/group client info
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
2016-01-26 10:10:39 +01:00
Felix Fietkau
8bb34756ce lua: fix stack leak in ubus method handling
Signed-off-by: Chen Bin <ewolfok@126.com>
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
2016-01-20 14:59:06 +01:00
Felix Fietkau
1d2b3bbdbe ubusd: add missing NULL pointer checks for ACL processing
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
2016-01-13 12:29:01 +01:00
Alexandru Ardelean
85e4de4193 libubus: add null for ubus_context object in ubus_shutdown()
At the moment, we do our own null checks before calling ubus_free().
The likely-hood of 'ctx' being null (in ubus_free()) is low,
but since free() handles null, might make sense for ubus_free() to do so as well.

Signed-off-by: Alexandru Ardelean <ardeleanalex@gmail.com>
2015-12-19 12:29:23 +01:00
Felix Fietkau
68da4ceb87 ubusd: allow all object access if uid=0 (ignore gid)
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
2015-12-09 20:16:36 +01:00
Felix Fietkau
8c9322c723 ubusd: fix the return code for acl check mismatch
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
2015-12-09 19:48:08 +01:00
Felix Fietkau
766d49876f ubusd: simplify/fix avl loop in ubusd_reply_add()
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
2015-12-09 19:40:20 +01:00
Felix Fietkau
0eff70a64c ubusd: simplify/fix avl loop in ubusd_acl_check()
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
2015-12-09 19:36:49 +01:00
Felix Fietkau
635ada4251 libubus: add socket EOF handling to ubus_complete_request()
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
2015-12-09 18:35:07 +01:00
Felix Fietkau
04e644bcce libubus: fix error handling during close after partially receiving a message
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
2015-12-09 18:26:43 +01:00