project/ubus.git
3 months agoCMakeLists.txt: bump minimum cmake version master
Felix Fietkau [Mon, 18 Dec 2023 12:30:59 +0000 (13:30 +0100)]
CMakeLists.txt: bump minimum cmake version

Removes warnings and fixes rpath issues on macOS

Signed-off-by: Felix Fietkau <nbd@nbd.name>
4 months agolibubus: fix initial subscribe with autosubscribe
Felix Fietkau [Tue, 28 Nov 2023 12:52:56 +0000 (13:52 +0100)]
libubus: fix initial subscribe with autosubscribe

Lookup + callbacks need to run after the subscriber object has been registered

Signed-off-by: Felix Fietkau <nbd@nbd.name>
4 months agolibubus: fix reconnect with auto subscribe
Felix Fietkau [Tue, 28 Nov 2023 12:14:45 +0000 (13:14 +0100)]
libubus: fix reconnect with auto subscribe

Look up all objects after reconnect and re-subscribe

Signed-off-by: Felix Fietkau <nbd@nbd.name>
5 months agoAdd auto subscribe support
John Crispin [Mon, 13 Nov 2023 13:53:19 +0000 (14:53 +0100)]
Add auto subscribe support

Add a new property to ubus_subscriber called new_obj_cb. It gets called
everytime a new object appears on the bus. If the callback returns true,
libubus will automatically subscribe to the notifications.

Signed-off-by: John Crispin <john@phrozen.org>
Signed-off-by: Felix Fietkau <nbd@nbd.name>
10 months agolibubus: add missing uloop_fd_delete call in ubus_shutdown
Felix Fietkau [Mon, 5 Jun 2023 20:05:55 +0000 (22:05 +0200)]
libubus: add missing uloop_fd_delete call in ubus_shutdown

Leaving the fd added could cause segfaults if ctx is freed

Signed-off-by: Felix Fietkau <nbd@nbd.name>
22 months agoubusd: add lookup command queuing support
Daniel Danzberger [Wed, 8 Jun 2022 11:12:29 +0000 (13:12 +0200)]
ubusd: add lookup command queuing support

Defers and continues a client's lookup command to avoid unnecessary
buffering under load.

Signed-off-by: Daniel Danzberger <daniel@dd-wrt.com>
Signed-off-by: Felix Fietkau <nbd@nbd.name>
22 months agoubusd: handle invoke on event object without data
Erik Karlsson [Fri, 2 Jul 2021 16:25:05 +0000 (18:25 +0200)]
ubusd: handle invoke on event object without data

When a built-in object is invoked with UBUS_ATTR_DATA absent, recv_msg
will be called with NULL as the msg argument and ubusd_forward_event
and ubusd_alloc_event_pattern need to handle this. Otherwise, a
truncated invoke of "send" or "register" on UBUS_SYSTEM_OBJECT_EVENT
that is missing UBUS_ATTR_DATA will cause ubusd to crash with SIGSEGV.

Signed-off-by: Erik Karlsson <erik.karlsson@genexis.eu>
22 months agolua: add optional path filter to objects() method
Etienne Champetier [Sun, 1 May 2022 21:58:09 +0000 (17:58 -0400)]
lua: add optional path filter to objects() method

'ubus list [<path>]' passes the path to ubusd,
this commit fix the lua bindings to do the same

Signed-off-by: Etienne Champetier <champetier.etienne@gmail.com>
2 years agocli: improve error logging for call command staging/stintel/dev
Stijn Tintel [Fri, 18 Feb 2022 11:18:24 +0000 (13:18 +0200)]
cli: improve error logging for call command

When the ubus "call" command fails, ubus prints "Command failed: ...".
This is fine when the call command is executed interactively by the
user. However, when the call command is executed non-interactively,
these log messages leave the user completely clueless:

  netifd: wan6 (10841): Command failed: Unknown error

  procd: /etc/rc.d/S80umdns: Failed to parse message data

These messages contain absolutely no info that explains where they come
from; it's not even clear they are coming from ubus. This makes tracking
down what's causing them virtually impossible.

Improve this situation by printing the full ubus call to stderr when
stderr is not a TTY.

Example of improved error message:
  netifd: wan (2836): Command failed: ubus call network.interface notify_proto { "action": 0, "link-up": false, "keep": false, "interface": "wan" } (Permission denied)

While one might argue not to include the JSON message in the log,
excluding it will still not help to reproduce the failed command.
As we only print the full command when stderr is not a TTY, seeing this
error means we're doing a bad ubus call somewhere, which is a bug and
should be fixed. Printing the full command makes more sense than
printing half the command and still requiring us to figure out the exact
command that failed.

Signed-off-by: Stijn Tintel <stijn@linux-ipv6.be>
Acked-by: Petr Štetiar <ynezz@true.cz>
Acked-by: Jo-Philipp Wich <jo@mein.io>
2 years agocli: use UBUS_STATUS_PARSE_ERROR
Stijn Tintel [Fri, 18 Feb 2022 10:51:03 +0000 (12:51 +0200)]
cli: use UBUS_STATUS_PARSE_ERROR

Use the newly introduced UBUS_STATUS_PARSE_ERROR status message in
ubus_cli_call() and ubus_cli_send().

Signed-off-by: Stijn Tintel <stijn@linux-ipv6.be>
Acked-by: Petr Štetiar <ynezz@true.cz>
Acked-by: Jo-Philipp Wich <jo@mein.io>
2 years agolibubus: introduce new status messages
Stijn Tintel [Fri, 18 Feb 2022 16:13:56 +0000 (18:13 +0200)]
libubus: introduce new status messages

Introduce the following new status messages in libubus:

UBUS_STATUS_NO_MEMORY
UBUS_STATUS_PARSE_ERROR
UBUS_STATUS_SYSTEM_ERROR

Signed-off-by: Stijn Tintel <stijn@linux-ipv6.be>
Acked-by: Petr Štetiar <ynezz@true.cz>
Acked-by: Jo-Philipp Wich <jo@mein.io>
2 years agolibubus: increase stack depth for processing obj msgs
Felix Fietkau [Mon, 9 Aug 2021 10:56:54 +0000 (12:56 +0200)]
libubus: increase stack depth for processing obj msgs

Avoid running into an issue where issuing a request from an obj message
can lead to recursive message processing and list corruption

Signed-off-by: Felix Fietkau <nbd@nbd.name>
2 years agolibubus: process pending messages in data handler if stack depth is 0
Felix Fietkau [Thu, 12 Aug 2021 10:35:19 +0000 (12:35 +0200)]
libubus: process pending messages in data handler if stack depth is 0

Process pending messages before attempting to read new ones. After completing
the poll, process any remaining pending messages.

A previous message processing call which issued a request from within
its handler may have left behind more object messages to process.

Signed-off-by: Felix Fietkau <nbd@nbd.name>
2 years agolibubus: use list_empty/list_first_entry in ubus_process_pending_msg
Felix Fietkau [Mon, 9 Aug 2021 10:49:34 +0000 (12:49 +0200)]
libubus: use list_empty/list_first_entry in ubus_process_pending_msg

Simplifies checks and avoids potential list corruption on recursive calls

Signed-off-by: Felix Fietkau <nbd@nbd.name>
2 years agoubusd: log ACL init errors
Rafał Miłecki [Fri, 20 Aug 2021 16:39:46 +0000 (18:39 +0200)]
ubusd: log ACL init errors

This makes it easier to notice ubusd (and so often a system) failing to
start properly. Some users reported procd failing to initialize fully
without a clear error in case of faulty /etc/passwd.

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
Acked-by: John Crispin <john@phrozen.org>
2 years agoubusd: fix tx_queue linked list usage
Alexander Van Parys [Wed, 30 Jun 2021 18:12:46 +0000 (20:12 +0200)]
ubusd: fix tx_queue linked list usage

Use the correct parameters for list_add_tail when enqueueing a message
in the tx_queue. Previously, list_add_tail(list, entry) was used instead
of list_add_tail(entry, list). Due to this, the list would only contain
the latest entry, and previously inserted entries were left dangling.

Reset the tx_queue offset after a message from the tx_queue has been sent
completely.

Signed-off-by: Alexander Van Parys <alexander.vanparys_ext@softathome.com>
2 years agoubusd: protect against too-short messages
Julian Squires [Wed, 21 Apr 2021 13:36:06 +0000 (11:06 -0230)]
ubusd: protect against too-short messages

A bad client can send a message whose blob_attr len is less than 4,
and ubus_msg_new happily points ->data off the end of the allocated
buffer, leading to invalid reads, writes, and eventually a crash if
ubus monitor is running:

==17683== Invalid write of size 4
==17683==    at 0x10A915: client_cb (ubusd_main.c:143)
==17683==    by 0x48495E3: uloop_run_events (uloop.c:198)
==17683==    by 0x48495E3: uloop_run_timeout (uloop.c:555)
==17683==    by 0x10A503: uloop_run (uloop.h:111)
==17683==    by 0x10A503: main (ubusd_main.c:284)
==17683==  Address 0x4a63200 is 0 bytes after a block of size 32 alloc'd
==17683==    at 0x4837B65: calloc (vg_replace_malloc.c:752)
==17683==    by 0x10AA87: ubus_msg_new (ubusd.c:47)
==17683==    by 0x10A8CE: client_cb (ubusd_main.c:135)
==17683==    by 0x48495E3: uloop_run_events (uloop.c:198)
==17683==    by 0x48495E3: uloop_run_timeout (uloop.c:555)
==17683==    by 0x10A503: uloop_run (uloop.h:111)
==17683==    by 0x10A503: main (ubusd_main.c:284)
==17683==
==17683== Invalid read of size 4
==17683==    at 0x10A645: blob_len (blob.h:102)
==17683==    by 0x10A93D: blob_raw_len (blob.h:111)
==17683==    by 0x10A93D: client_cb (ubusd_main.c:149)
==17683==    by 0x48495E3: uloop_run_events (uloop.c:198)
==17683==    by 0x48495E3: uloop_run_timeout (uloop.c:555)
==17683==    by 0x10A503: uloop_run (uloop.h:111)
==17683==    by 0x10A503: main (ubusd_main.c:284)
==17683==  Address 0x4a63200 is 0 bytes after a block of size 32 alloc'd
==17683==    at 0x4837B65: calloc (vg_replace_malloc.c:752)
==17683==    by 0x10AA87: ubus_msg_new (ubusd.c:47)
==17683==    by 0x10A8CE: client_cb (ubusd_main.c:135)
==17683==    by 0x48495E3: uloop_run_events (uloop.c:198)
==17683==    by 0x48495E3: uloop_run_timeout (uloop.c:555)
==17683==    by 0x10A503: uloop_run (uloop.h:111)
==17683==    by 0x10A503: main (ubusd_main.c:284)
==17683==
==17683== Invalid read of size 4
==17683==    at 0x10ACE8: blob_len (blob.h:102)
==17683==    by 0x10B7E1: blob_raw_len (blob.h:111)
==17683==    by 0x10B7E1: ubusd_proto_receive_message (ubusd_proto.c:457)
==17683==    by 0x10A9A7: client_cb (ubusd_main.c:169)
==17683==    by 0x48495E3: uloop_run_events (uloop.c:198)
==17683==    by 0x48495E3: uloop_run_timeout (uloop.c:555)
==17683==    by 0x10A503: uloop_run (uloop.h:111)
==17683==    by 0x10A503: main (ubusd_main.c:284)
==17683==  Address 0x4a63200 is 0 bytes after a block of size 32 alloc'd
==17683==    at 0x4837B65: calloc (vg_replace_malloc.c:752)
==17683==    by 0x10AA87: ubus_msg_new (ubusd.c:47)
==17683==    by 0x10A8CE: client_cb (ubusd_main.c:135)
==17683==    by 0x48495E3: uloop_run_events (uloop.c:198)
==17683==    by 0x48495E3: uloop_run_timeout (uloop.c:555)
==17683==    by 0x10A503: uloop_run (uloop.h:111)
==17683==    by 0x10A503: main (ubusd_main.c:284)
==17683==
==17683== Invalid read of size 4
==17683==    at 0x10D39B: blob_len (blob.h:102)
==17683==    by 0x10D53E: ubusd_monitor_message (ubusd_monitor.c:91)
==17683==    by 0x10A99C: client_cb (ubusd_main.c:168)
==17683==    by 0x48495E3: uloop_run_events (uloop.c:198)
==17683==    by 0x48495E3: uloop_run_timeout (uloop.c:555)
==17683==    by 0x10A503: uloop_run (uloop.h:111)
==17683==    by 0x10A503: main (ubusd_main.c:284)
==17683==  Address 0x4a6b3e0 is 0 bytes after a block of size 32 alloc'd
==17683==    at 0x4837B65: calloc (vg_replace_malloc.c:752)
==17683==    by 0x10AA87: ubus_msg_new (ubusd.c:47)
==17683==    by 0x10A8CE: client_cb (ubusd_main.c:135)
==17683==    by 0x48495E3: uloop_run_events (uloop.c:198)
==17683==    by 0x48495E3: uloop_run_timeout (uloop.c:555)
==17683==    by 0x10A503: uloop_run (uloop.h:111)
==17683==    by 0x10A503: main (ubusd_main.c:284)
==17683==
==17683== Invalid read of size 1
==17683==    at 0x4848286: blob_put (blob.c:167)
==17683==    by 0x10D555: ubusd_monitor_message (ubusd_monitor.c:91)
==17683==    by 0x10A99C: client_cb (ubusd_main.c:168)
==17683==    by 0x48495E3: uloop_run_events (uloop.c:198)
==17683==    by 0x48495E3: uloop_run_timeout (uloop.c:555)
==17683==    by 0x10A503: uloop_run (uloop.h:111)
==17683==    by 0x10A503: main (ubusd_main.c:284)
==17683==  Address 0x4a6b3e4 is 4 bytes after a block of size 32 alloc'd
==17683==    at 0x4837B65: calloc (vg_replace_malloc.c:752)
==17683==    by 0x10AA87: ubus_msg_new (ubusd.c:47)
==17683==    by 0x10A8CE: client_cb (ubusd_main.c:135)
==17683==    by 0x48495E3: uloop_run_events (uloop.c:198)
==17683==    by 0x48495E3: uloop_run_timeout (uloop.c:555)
==17683==    by 0x10A503: uloop_run (uloop.h:111)
==17683==    by 0x10A503: main (ubusd_main.c:284)
==17683==
==17683==
==17683== Process terminating with default action of signal 11 (SIGSEGV)
==17683==  Bad permissions for mapped region at address 0x4E43000
==17683==    at 0x4848286: blob_put (blob.c:167)
==17683==    by 0x10D555: ubusd_monitor_message (ubusd_monitor.c:91)
==17683==    by 0x10A99C: client_cb (ubusd_main.c:168)
==17683==    by 0x48495E3: uloop_run_events (uloop.c:198)
==17683==    by 0x48495E3: uloop_run_timeout (uloop.c:555)
==17683==    by 0x10A503: uloop_run (uloop.h:111)
==17683==    by 0x10A503: main (ubusd_main.c:284)

The following Python program minimally reproduces the issue:

import socket

sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
sock.connect('/tmp/usock')
sock.recv(12)
sock.send(b'\x00\x04\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00')

Signed-off-by: Julian Squires <julian@cipht.net>
2 years agoubusd: add per-client tx queue limit
Arnout Vandecappelle (Essensium/Mind) [Thu, 25 Mar 2021 21:45:02 +0000 (22:45 +0100)]
ubusd: add per-client tx queue limit

No new message can be enqueued if this brings the total queue length of
that client over UBUS_CLIENT_MAX_TXQ_LEN.

Set UBUS_CLIENT_MAX_TXQ_LEN to UBUS_MAX_MSGLEN, i.e. 1MB. This limit
should be plenty for any practical use case.

Signed-off-by: Arnout Vandecappelle (Essensium/Mind) <arnout@mind.be>
2 years agoubusd: convert tx_queue to linked list
Arnout Vandecappelle (Essensium/Mind) [Thu, 25 Mar 2021 21:45:01 +0000 (22:45 +0100)]
ubusd: convert tx_queue to linked list

ubusd maintains a per-client tx_queue containing references to message
buffers that have not been sent yet (due to the socket blocking). This
is a fixed-size, 64-element queue.

When more than 64 elements are queued, subsequent elements are simply
dropped. Thus, a client that is waiting for those messages will block
indefinitely. In particular, this happens when more than +- 250 objects
are registered on the bus and either "ubus list" or "ubus wait_for" is
called. The responses to these requests consist of a message buffer per
object. Since in practice, ubusd will not yield between the sends of
these message buffers, the client has no time to process them and
eventually the output socket blocks. After 64 more objects, the rest is
dropped, including the final message that indicates termination. Thus,
the client waits indefinitely for the termination message.

To solve this, turn the tx_queue into a variable-sized linked list
instead of a fixed-size queue.

To maintain the linked list, an additional structure ubus_msg_buf_list
is created. It is not possible to add the linked list to ubus_msg_buf,
because that is shared between clients.

Note that this infinite tx_queue opens the door to a DoS attack. You can
open a client and a server connection, then send messages from the
client to the server without ever reading anything on the server side.
This will eventually lead to an out-of-memory. However, such a DoS
already existed anyway, it just requires opening multiple server
connections and filling up the fixed-size queue on each one. To protect
against such DoS attacks, we'd need to:
- keep a global maximum queue size that applies to all rx and tx queues
  together;
- stop reading from any connection when the maximum is reached;
- close any connection when it hasn't become writeable after some
  timeout.

Fixes: https://bugs.openwrt.org/index.php?do=details&task_id=1525
Signed-off-by: Arnout Vandecappelle (Essensium/Mind) <arnout@mind.be>
3 years agocmake: add a possibility to set library version
Felix Fietkau [Mon, 15 Feb 2021 18:11:58 +0000 (19:11 +0100)]
cmake: add a possibility to set library version

Add a new `ABIVERSION` define which allows to control the SOVERSION used
for the built shared library. This is needed for downstream packaging to
properly track breaking ABI changes when updating to newer versions of
the library.

Signed-off-by: Felix Fietkau <nbd@nbd.name>
3 years agoubusd: attempt to create socket folder
Daniel Golle [Fri, 4 Dec 2020 09:53:11 +0000 (09:53 +0000)]
ubusd: attempt to create socket folder

When ubus is running as root, /var/run/ubus most likely hasn't been
created as well (as that's the homedir of user ubus, and if a user
ubus was found, then ubus would run being that user).
Blindly attempt to create the directory (which won't do any harm if
it does exist and/or ubus is not running as root) to still be able to
start ubus in case of user ubus not existing.

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
3 years agoubusd_acl: add support for wildcard in methods
Ansuel Smith [Fri, 23 Oct 2020 01:01:55 +0000 (03:01 +0200)]
ubusd_acl: add support for wildcard in methods

Add support for wildcard in methods to permit access to all methods
defined by the object. This can be usefull for process that run as
non-root user and needs to access ubus method.

Signed-off-by: Ansuel Smith <ansuelsmth@gmail.com>
Signed-off-by: Daniel Golle <daniel@makrotopia.org>
3 years agoubus: move /var/run/ubus.sock to /var/run/ubus/ubus.sock
Daniel Golle [Mon, 19 Oct 2020 14:14:47 +0000 (15:14 +0100)]
ubus: move /var/run/ubus.sock to /var/run/ubus/ubus.sock

This makes it possible to run ubus as non-root.

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
3 years agotests: cram: fix usage test
Petr Štetiar [Wed, 5 Aug 2020 08:27:24 +0000 (10:27 +0200)]
tests: cram: fix usage test

Add new subscribe command help.

Fixes: 59835ae530f5 ("cli: document usage of "subscribe" command")
Signed-off-by: Petr Štetiar <ynezz@true.cz>
3 years agocli: document usage of "subscribe" command
Rafał Miłecki [Wed, 29 Jul 2020 08:17:18 +0000 (10:17 +0200)]
cli: document usage of "subscribe" command

Support for "subscribe" command was added years ago in the commit
453b87f6315f ("cli: add support for subscribing to objects"). Document
its usage.

Cc: Felix Fietkau <nbd@nbd.name>
Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
4 years agolua: avoid truncation of large numeric values
Alin Nastac [Mon, 3 Feb 2020 12:58:04 +0000 (13:58 +0100)]
lua: avoid truncation of large numeric values

If the Lua number exceeds the maximum value representable by an
unsigned 32bit integer, store it in an unsigned 64bit integer
field instead.

Signed-off-by: Alin Nastac <alin.nastac@gmail.com>
[align code style, reword commit message]
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
4 years agoubus: make libubus ready for linking into C++
Peter Stadler [Sat, 7 Dec 2019 11:45:47 +0000 (12:45 +0100)]
ubus: make libubus ready for linking into C++

Use extern "C" { ... } if using libubus.h in a C++ project.

Signed-off-by: Peter Stadler <peter.stadler@student.uibk.ac.at>
4 years agoubusd/libubus-io: fix socket descriptor passing
Petr Štetiar [Fri, 27 Dec 2019 13:48:32 +0000 (14:48 +0100)]
ubusd/libubus-io: fix socket descriptor passing

In commit 5d7ca8309d0a ("ubusd/libubus-io: fix variable sized struct
position warning") the position of cmsghdr struct has been changed in
order to fix clang-9 compiler warning, but it has introduced regression
in at least `logread` which hanged indefinitely.

So this patch reworks the socket descriptor passing in a way recommended
in the `cmsg(3)` manual page.

Ref: http://lists.infradead.org/pipermail/openwrt-devel/2019-December/020840.html
Fixes: 5d7ca8309d0a ("ubusd/libubus-io: fix variable sized struct position warning")
Reported-by: Hannu Nyman <hannu.nyman@welho.com>
Signed-off-by: Petr Štetiar <ynezz@true.cz>
4 years agoci: enable unit testing
Petr Štetiar [Thu, 26 Dec 2019 08:46:05 +0000 (09:46 +0100)]
ci: enable unit testing

In commit 08f17c87a000 ("add fuzzer and cram based unit tests") some
fuzz/unit tests were added so enable them on CI as well.

Signed-off-by: Petr Štetiar <ynezz@true.cz>
4 years agofix blob parsing vulnerability by using blob_parse_untrusted
Petr Štetiar [Thu, 19 Dec 2019 10:25:56 +0000 (11:25 +0100)]
fix blob parsing vulnerability by using blob_parse_untrusted

blob_parse expects blobs from trusted inputs, but it can be supplied
with possibly malicious blobs from untrusted inputs as well, which might
lead to undefined behaviour and/or crash of ubus daemon. In order to
prevent such conditions, switch to blob_parse_untrusted which should
hopefully handle such untrusted inputs appropriately.

Signed-off-by: Petr Štetiar <ynezz@true.cz>
4 years agoubus_monitor: workaround possibly false positive uses of memory after it is freed
Petr Štetiar [Thu, 19 Dec 2019 09:48:57 +0000 (10:48 +0100)]
ubus_monitor: workaround possibly false positive uses of memory after it is freed

scan-build from clang-9 has reported following:

 libubox/list.h:141:2: warning: Use of memory after it is freed
        _list_add(_new, head, head->next);

Signed-off-by: Petr Štetiar <ynezz@true.cz>
4 years agoubusd_monitor: fix possible null pointer dereference
Petr Štetiar [Thu, 19 Dec 2019 10:10:46 +0000 (11:10 +0100)]
ubusd_monitor: fix possible null pointer dereference

This dereference could possibly happen if the calloc call fails as the
return value is unchecked. While at it refactor the code little bit to
make it easier to follow, use safe list iterator and provide return
value for ubusd_monitor_connect.

Signed-off-by: Petr Štetiar <ynezz@true.cz>
4 years agoubus_common: remove duplicate ARRAY_SIZE and add missing include
Petr Štetiar [Thu, 19 Dec 2019 09:42:35 +0000 (10:42 +0100)]
ubus_common: remove duplicate ARRAY_SIZE and add missing include

ARRAY_SIZE is available from libubox/utils.h.

Signed-off-by: Petr Štetiar <ynezz@true.cz>
4 years agoworkaround possibly false positive uses of memory after it is freed
Petr Štetiar [Mon, 16 Dec 2019 22:41:31 +0000 (23:41 +0100)]
workaround possibly false positive uses of memory after it is freed

scan-build from clang-9 has reported following:

 libubox/list.h:83:22: warning: Use of memory after it is freed
        entry->next->prev = entry->prev;
                            ^~~~~~~~~~~

 ubusd_event.c:42:3: warning: Use of memory after it is freed
                ubusd_delete_event_source(ev);
                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Which might be a false positives, but in order to make the code pass the
static analyzer checks, rewrite the while loops on lists with the safe
list iterator.

Signed-off-by: Petr Štetiar <ynezz@true.cz>
4 years agolua: ubus_lua_do_subscribe: fix copy&paste error
Petr Štetiar [Mon, 16 Dec 2019 22:02:02 +0000 (23:02 +0100)]
lua: ubus_lua_do_subscribe: fix copy&paste error

Fix copy&paste error in Lua ubus subscriber removal callback.

Addresses-Coverity-ID: 1412311 ("Copy-paste error")
Signed-off-by: Petr Štetiar <ynezz@true.cz>
4 years agolua: workaround false positive dereference of null pointer
Petr Štetiar [Mon, 16 Dec 2019 20:52:55 +0000 (21:52 +0100)]
lua: workaround false positive dereference of null pointer

scan-build from clang-9 has reported following:

 ubus.c:837:16: warning: Access to field 'rnotify' results in a dereference of a null pointer (loaded from variable 'sub')
                 sub->rnotify = luaL_ref(L, -2);

Which is false positive as the lua_error() does a long jump and
therefore never returns and this long jump probably confuses the static
analyzer. So this patch workarounds this false positive by helping
static analyzer by using common Lua idiom which is to return
lua_error()'s return value.

Ref: https://www.lua.org/manual/5.1/manual.html#lua_error
Addresses-Coverity-ID: 1412355 ("Dereference after null check")
Signed-off-by: Petr Štetiar <ynezz@true.cz>
4 years agoadd fuzzer and cram based unit tests
Petr Štetiar [Thu, 12 Dec 2019 22:24:15 +0000 (23:24 +0100)]
add fuzzer and cram based unit tests

For improved QA etc.

Signed-off-by: Petr Štetiar <ynezz@true.cz>
4 years agorefactor ubusd.c into reusable ubusd_library
Petr Štetiar [Thu, 12 Dec 2019 09:05:48 +0000 (10:05 +0100)]
refactor ubusd.c into reusable ubusd_library

In order to allow reusability in unit testing & fuzzing.

Signed-off-by: Petr Štetiar <ynezz@true.cz>
4 years agoexamples: remove dead increments
Petr Štetiar [Wed, 11 Dec 2019 12:31:59 +0000 (13:31 +0100)]
examples: remove dead increments

Fixes following error reported by clang-9 analyzer:

 examples/server.c:244:2: warning: Value stored to 'argc' is never read
        argc -= optind;
        ^       ~~~~~~

 examples/server.c:245:2: warning: Value stored to 'argv' is never read
        argv += optind;
        ^       ~~~~~~

Signed-off-by: Petr Štetiar <ynezz@true.cz>
4 years agoadd initial GitLab CI support
Petr Štetiar [Wed, 11 Dec 2019 11:59:40 +0000 (12:59 +0100)]
add initial GitLab CI support

Uses currently proof-of-concept openwrt-ci[1] in order to:

 * improve the quality of the codebase in various areas
 * decrease code review time and help merging contributions faster
 * get automagic feedback loop on various platforms and tools
   - out of tree build with OpenWrt SDK on following targets:
     * ath79-generic
     * imx6-generic
     * malta-be
     * mvebu-cortexa53
   - out of tree native build on x86/64 with GCC (versions 7, 8, 9) and Clang 10
   - out of tree native x86/64 static code analysis with cppcheck and
     scan-build from Clang 9

1. https://gitlab.com/ynezz/openwrt-ci/

Signed-off-by: Petr Štetiar <ynezz@true.cz>
4 years agolibubus: fix incompatible pointer types assigment
Petr Štetiar [Wed, 11 Dec 2019 10:15:02 +0000 (11:15 +0100)]
libubus: fix incompatible pointer types assigment

Fixes following error reported by clang-9 analyzer:

 libubus.c:286:19: error: incompatible pointer types assigning to 'struct blob_attr *' from 'char *' [-Werror,-Wincompatible-pointer-types]
         ctx->msgbuf.data = (char *) calloc(UBUS_MSG_CHUNK_SIZE, sizeof(char));

Result of 'calloc' is converted to a pointer of type 'struct blob_attr',
which is incompatible with sizeof operand type 'char'.

Signed-off-by: Petr Štetiar <ynezz@true.cz>
4 years agoiron out all extra compiler warnings
Petr Štetiar [Wed, 11 Dec 2019 09:36:36 +0000 (10:36 +0100)]
iron out all extra compiler warnings

clang-9 on x86/64 has reported following warnings/errors:

 libubus-acl.c:123:2: error: comparison of integers of different signs: 'size_t' (aka 'unsigned long') and 'int' [-Werror,-Wsign-compare]
 libubus-io.c:108:18: error: comparison of integers of different signs: 'int' and 'size_t' (aka 'unsigned long') [-Werror,-Wsign-compare]
 libubus-io.c:395:56: error: comparison of integers of different signs: 'ssize_t' (aka 'long') and 'size_t' (aka 'unsigned long') [-Werror,-Wsign-compare]
 libubus-req.c:441:4: error: comparison of integers of different signs: 'size_t' (aka 'unsigned long') and 'int' [-Werror,-Wsign-compare]
 ubusd_acl.c:119:18: error: comparison of integers of different signs: 'int' and 'unsigned long' [-Werror,-Wsign-compare]
 ubusd_acl.c:152:5: error: comparison of integers of different signs: 'size_t' (aka 'unsigned long') and 'int' [-Werror,-Wsign-compare]
 ubusd_acl.c:348:3: error: comparison of integers of different signs: 'size_t' (aka 'unsigned long') and 'int' [-Werror,-Wsign-compare]
 ubusd_acl.c:352:3: error: comparison of integers of different signs: 'size_t' (aka 'unsigned long') and 'int' [-Werror,-Wsign-compare]
 ubusd_acl.c:357:3: error: comparison of integers of different signs: 'size_t' (aka 'unsigned long') and 'int' [-Werror,-Wsign-compare]
 ubusd_acl.c:362:3: error: comparison of integers of different signs: 'size_t' (aka 'unsigned long') and 'int' [-Werror,-Wsign-compare]
 ubusd_acl.c:367:3: error: comparison of integers of different signs: 'size_t' (aka 'unsigned long') and 'int' [-Werror,-Wsign-compare]
 ubusd_acl.c:447:16: error: comparison of integers of different signs: 'int' and '__size_t' (aka 'unsigned long') [-Werror,-Wsign-compare]
 ubusd_acl.c:502:18: error: comparison of integers of different signs: 'int' and 'unsigned long' [-Werror,-Wsign-compare]
 ubusd.c:123:13: error: comparison of integers of different signs: 'int' and 'unsigned long' [-Werror,-Wsign-compare]
 ubusd.c:170:15: error: comparison of integers of different signs: 'int' and 'unsigned long' [-Werror,-Wsign-compare]
 ubusd.c:262:43: error: comparison of integers of different signs: 'int' and 'unsigned long' [-Werror,-Wsign-compare]
 ubusd.c:287:30: error: comparison of integers of different signs: 'int' and 'unsigned long' [-Werror,-Wsign-compare]
 ubusd_event.c:170:18: error: comparison of integers of different signs: 'int' and 'unsigned long' [-Werror,-Wsign-compare]
 ubusd_obj.c:71:2: error: comparison of integers of different signs: 'size_t' (aka 'unsigned long') and 'int' [-Werror,-Wsign-compare]

Signed-off-by: Petr Štetiar <ynezz@true.cz>
4 years agoubusd/libubus-io: fix variable sized struct position warning
Petr Štetiar [Wed, 11 Dec 2019 09:07:08 +0000 (10:07 +0100)]
ubusd/libubus-io: fix variable sized struct position warning

Fixes following clang-9 compiler warnings:

 ubusd.c:99:18: error: field 'h' with variable sized type 'struct cmsghdr' not at the end of a struct or class is a GNU extension [-Werror,-Wgnu-variable-sized-type-not-at-end]
                struct cmsghdr h;
                               ^

Signed-off-by: Petr Štetiar <ynezz@true.cz>
4 years agoubusd: fix comparison of integers of different signs
Petr Štetiar [Wed, 11 Dec 2019 09:04:36 +0000 (10:04 +0100)]
ubusd: fix comparison of integers of different signs

Fixes following clang-9 compiler warning:

 ubusd.c:36:19: error: comparison of integers of different signs: 'uint32_t' (aka 'unsigned int') and 'int' [-Werror,-Wsign-compare]
         if (ub->refcount == ~0) {
             ~~~~~~~~~~~~ ^  ~~

Signed-off-by: Petr Štetiar <ynezz@true.cz>
4 years agocmake: enable extra compiler checks
Petr Štetiar [Wed, 11 Dec 2019 08:59:02 +0000 (09:59 +0100)]
cmake: enable extra compiler checks

Let's enforce additional automatic checks enforced by the compiler in
order to catch possible errors during compilation.

Signed-off-by: Petr Štetiar <ynezz@true.cz>
4 years agoubus: Support static builds
Alban Bedel [Fri, 14 Jun 2019 14:51:56 +0000 (16:51 +0200)]
ubus: Support static builds

Extend the cmake config to build and install static libraries
when -DBUILD_STATIC=ON is used.

Signed-off-by: Alban Bedel <albeu@free.fr>
4 years agoubusd: retry sending messages on EINTR
Felix Fietkau [Tue, 16 Apr 2019 09:13:44 +0000 (11:13 +0200)]
ubusd: retry sending messages on EINTR

Avoids unnecessary delays and/or blocking on messages

Signed-off-by: Felix Fietkau <nbd@nbd.name>
5 years agolibubus: attempt to receive data before calling poll
Felix Fietkau [Tue, 16 Apr 2019 09:00:49 +0000 (11:00 +0200)]
libubus: attempt to receive data before calling poll

Data is often already present here

Signed-off-by: Felix Fietkau <nbd@nbd.name>
5 years agolibubus: do not abort recv_retry before completing a message
Felix Fietkau [Tue, 16 Apr 2019 08:34:45 +0000 (10:34 +0200)]
libubus: do not abort recv_retry before completing a message

If the header is read but not the remainder of the message, the stream
will be out of sync and parsing of future messages won't work

Signed-off-by: Felix Fietkau <nbd@nbd.name>
5 years agoubusd_acl: event send access list support
Hans Dedecker [Wed, 3 Oct 2018 13:36:18 +0000 (15:36 +0200)]
ubusd_acl: event send access list support

Adds event send access list support in ubus via the "send" keyword

Example of a json file:
{
"user": "superuser",
"send": [ "wireless.*" ],
}

Signed-off-by: Koen Dergent <koen.cj.dergent@gmail.com>
Signed-off-by: Hans Dedecker <dedeckeh@gmail.com>
5 years agoubusd_acl: event listen access list support
Hans Dedecker [Wed, 3 Oct 2018 13:36:17 +0000 (15:36 +0200)]
ubusd_acl: event listen access list support

Adds event listen access list support in ubus via the "listen" keyword

Example of a json file:

{
    "user": "superuser",
    "listen": [ "network.*" ],
}

Signed-off-by: Koen Dergent <koen.cj.dergent@gmail.com>
Signed-off-by: Hans Dedecker <dedeckeh@gmail.com>
5 years agoubusd_acl: rework wildcard support
Hans Dedecker [Wed, 3 Oct 2018 13:36:16 +0000 (15:36 +0200)]
ubusd_acl: rework wildcard support

Wildcard access list support was failing in case multiple wildcards
entries were defined and/or when a specific access list string
overlapped a wildcard entry.
Root cause of the problem was the way how wildcard entries were sorted
in the avl tree by the compare function ubusd_acl_match_path resulting
into a non acces list match for a given object path.

The avl_tree sorting has been changed to make use of avl_strcmp; as such
there's no distinction anymore between non-wildcard and wildcard entries
in the avl_tree compare function as the boolean partial marks an access
list entry as a wildcard entry.

When trying to find an access list match for an object path the access list
tree is iterated as long as the number of characters between the access list
string and object path is monotonically increasing.

Signed-off-by: Hans Dedecker <dedeckeh@gmail.com>
5 years agoubusd_event: move strmatch_len to ubus_common.h
Hans Dedecker [Wed, 3 Oct 2018 13:36:15 +0000 (15:36 +0200)]
ubusd_event: move strmatch_len to ubus_common.h

Rename strmatch_len into ubus_strmatch_len and move it to ubus_common.h

Signed-off-by: Hans Dedecker <dedeckeh@gmail.com>
5 years agoubus/lua: add support for BLOBMSG_TYPE_DOUBLE
Marcus Comstedt [Tue, 21 Aug 2018 10:43:06 +0000 (12:43 +0200)]
ubus/lua: add support for BLOBMSG_TYPE_DOUBLE

Signed-off-by: Marcus Comstedt <marcus@mc.pp.se>
5 years agolibubus: pass an empty UBUS_ATTR_DATA block if msg is NULL on invoke
Felix Fietkau [Thu, 26 Jul 2018 13:17:36 +0000 (15:17 +0200)]
libubus: pass an empty UBUS_ATTR_DATA block if msg is NULL on invoke

Signed-off-by: Felix Fietkau <nbd@nbd.name>
5 years agolibubus: check for non-NULL data before running callbacks
Felix Fietkau [Wed, 25 Jul 2018 08:37:30 +0000 (10:37 +0200)]
libubus: check for non-NULL data before running callbacks

UBUS_MSG_INVOKE and UBUS_MSG_DATA can be sent without UBUS_ATTR_DATA
present. Most ubus users assume that the msg argument passed can never
be NULL, so this change prevents a crash

Signed-off-by: Felix Fietkau <nbd@nbd.name>
6 years agoubus/lua: pass notification name to callback
Dirk Feytons [Tue, 16 Jan 2018 17:13:39 +0000 (18:13 +0100)]
ubus/lua: pass notification name to callback

The callback function registered to be invoked when subscribing to a
notification was only passed the notification data (if any) but not the name
of the notification.

This name is now passed as second argument to remain backwards compatible.
The example subscriber.lua has also be updated.

Signed-off-by: Dirk Feytons <dirk.feytons@gmail.com>
6 years agovalgrind complained about these
John Crispin [Wed, 3 Jan 2018 10:56:48 +0000 (11:56 +0100)]
valgrind complained about these

==18834== Warning: invalid file descriptor -1 in syscall close()
==18834==    at 0x5326D20: __close_nocancel (syscall-template.S:84)
==18834==    by 0x5046DC7: ubus_process_obj_msg (libubus-obj.c:143)
==18834==    by 0x5045E98: ubus_process_msg (libubus.c:106)
==18834==    by 0x50468D0: ubus_handle_data (libubus-io.c:314)
==18834==    by 0x4E3D125: uloop_run_events (uloop.c:198)
==18834==    by 0x4E3D125: uloop_run_timeout (uloop.c:555)
==18834==    by 0x109BEF: uloop_run (uloop.h:111)
==18834==    by 0x109BEF: main (main.c:25)

Signed-off-by: John Crispin <john@phrozen.org>
6 years agofix invalid close() call
John Crispin [Wed, 3 Jan 2018 10:52:56 +0000 (11:52 +0100)]
fix invalid close() call

valgrind complained about this one

==18632== Warning: invalid file descriptor -1 in syscall close()
==18632==    at 0x5326D20: __close_nocancel (syscall-template.S:84)
==18632==    by 0x5046C02: ubus_process_invoke (libubus-obj.c:98)
==18632==    by 0x5046DC3: ubus_process_obj_msg (libubus-obj.c:142)
==18632==    by 0x5045E98: ubus_process_msg (libubus.c:106)
==18632==    by 0x50468D0: ubus_handle_data (libubus-io.c:314)
==18632==    by 0x4E3D125: uloop_run_events (uloop.c:198)
==18632==    by 0x4E3D125: uloop_run_timeout (uloop.c:555)
==18632==    by 0x109BEF: uloop_run (uloop.h:111)
==18632==    by 0x109BEF: main (main.c:25)

Signed-off-by: John Crispin <john@phrozen.org>
6 years agoubusd: move global retmsg per client
Alexandru Ardelean [Wed, 7 Jun 2017 11:09:31 +0000 (14:09 +0300)]
ubusd: move global retmsg per client

Even with the tx_queue-ing issue resolved, what
seems to happen afterwards, is that all the messages
seems to get through, but the client still loops
in the `ubus_complete_request()` waiting for
`req->status_msg` or for a timeout.

Though, the timeout does not seem to happen, because
the data is processed in `ubus_poll_data()`, with
a infinite poll() timeout (ubus_complete_request() is
called with timeout 0).

It's likely that either the `seq` or `peer` sent from
ubusd are wrong, and the client cannot get the correct
ubus request in `ubus_process_req_msg()`.
I haven't digged too deep into this ; setting the
`retmsg` object on the client struct seems to have
resolved any hanging with the `ubus list` command.

Signed-off-by: Alexandru Ardelean <ardeleanalex@gmail.com>
Signed-off-by: Felix Fietkau <nbd@nbd.name> [fix placement of retmsg in cl]
6 years agoubusd_monitor: alloc & free the buffer outside of the loop
Alexandru Ardelean [Wed, 5 Jul 2017 13:21:47 +0000 (16:21 +0300)]
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>
6 years agoubusd: rename goto label from `error` to `out`
Alexandru Ardelean [Wed, 5 Jul 2017 13:21:22 +0000 (16:21 +0300)]
ubusd: rename goto label from `error` to `out`

Semantic has changed a bit.

Signed-off-by: Alexandru Ardelean <ardeleanalex@gmail.com>
6 years agoubusd: don't free messages in ubus_send_msg() anymore
Alexandru Ardelean [Wed, 5 Jul 2017 13:20:51 +0000 (16:20 +0300)]
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>
6 years agolibubus: Fix deletion from context's object AVL tree when removing object
Bob Ham [Thu, 9 Nov 2017 15:53:24 +0000 (15:53 +0000)]
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>
6 years agoubus: Remove unnecessary memset calls.
Rosen Penev [Tue, 7 Nov 2017 20:34:20 +0000 (12:34 -0800)]
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>
6 years agolibubus-req: add data_cb callback handling for ubus notifications
John Crispin [Wed, 1 Nov 2017 21:14:42 +0000 (22:14 +0100)]
libubus-req: add data_cb callback handling for ubus notifications

Signed-off-by: John Crispin <john@phrozen.org>
7 years agocli: fix listen_timeout compile issue
Hans Dedecker [Sat, 18 Feb 2017 12:41:53 +0000 (13:41 +0100)]
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>
7 years agolibubus: reset ctx->sock.error when doing ubus reconnect
Hans Dedecker [Sat, 18 Feb 2017 12:41:52 +0000 (13:41 +0100)]
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>
7 years agocli: do not use default timeout for listen
Felix Fietkau [Wed, 15 Feb 2017 10:35:35 +0000 (11:35 +0100)]
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>
7 years agocli: add support for subscribing to objects
Felix Fietkau [Tue, 14 Feb 2017 14:58:49 +0000 (15:58 +0100)]
cli: add support for subscribing to objects

Signed-off-by: Felix Fietkau <nbd@nbd.name>
7 years agoubusd: fix incomplete copy of shared buf during queue-ing
Alexandru Ardelean [Thu, 2 Feb 2017 15:59:49 +0000 (17:59 +0200)]
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>
7 years agolibubus: do not modify uloop_cancelled
Felix Fietkau [Fri, 3 Feb 2017 17:39:11 +0000 (18:39 +0100)]
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>
7 years agolibubus: reset ctx->sock.eof to fix reconnect issues
Felix Fietkau [Fri, 3 Feb 2017 13:13:48 +0000 (14:13 +0100)]
libubus: reset ctx->sock.eof to fix reconnect issues

Signed-off-by: Felix Fietkau <nbd@nbd.name>
7 years agolibubus: do not register/unregister with uloop during sync requests
Felix Fietkau [Sun, 22 Jan 2017 13:33:37 +0000 (14:33 +0100)]
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>
7 years agolibubus: move uloop_init() call to ubus_connect_ctx
Felix Fietkau [Sun, 22 Jan 2017 13:28:47 +0000 (14:28 +0100)]
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>
7 years agoubusd: fix issue caused by an implicit cast
Mihai Richard [Thu, 19 Jan 2017 12:51:04 +0000 (12:51 +0000)]
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>
7 years agoubus: ubus_free: clear pending timers before freeing context
Abhimanyu Vishwakarma [Tue, 3 Jan 2017 06:07:05 +0000 (11:37 +0530)]
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>
7 years agolibubus: reduce code duplication, fix indentation
Felix Fietkau [Sat, 24 Dec 2016 13:35:58 +0000 (14:35 +0100)]
libubus: reduce code duplication, fix indentation

Signed-off-by: Felix Fietkau <nbd@nbd.name>
7 years agoAllow sending a fd along a ubus_request
amine ahd [Mon, 19 Dec 2016 09:30:36 +0000 (10:30 +0100)]
Allow sending a fd along a ubus_request

Signed-off-by: amine.ahd <amine.ahd@gmail.com>
7 years agoubusd: fix id lookup of objects with path and no methods
Felix Fietkau [Sun, 20 Nov 2016 16:05:00 +0000 (17:05 +0100)]
ubusd: fix id lookup of objects with path and no methods

Signed-off-by: Felix Fietkau <nbd@nbd.name>
7 years agolua: add 'defer_request' and 'complete_deferred_request' functions
Vasily Goldobin [Wed, 16 Nov 2016 15:26:13 +0000 (20:26 +0500)]
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>
7 years agoubusd_proto: fix crash when trying to subscribe to system objects
Felix Fietkau [Sun, 20 Nov 2016 15:40:06 +0000 (16:40 +0100)]
ubusd_proto: fix crash when trying to subscribe to system objects

Signed-off-by: Felix Fietkau <nbd@nbd.name>
7 years agobuild: remove /opt/local prefix
Felix Fietkau [Sun, 20 Nov 2016 15:26:19 +0000 (16:26 +0100)]
build: remove /opt/local prefix

Signed-off-by: Felix Fietkau <nbd@nbd.name>
7 years agocli: register event handler first, then do lookup
Alexandru Ardelean [Thu, 5 May 2016 11:03:30 +0000 (14:03 +0300)]
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>
7 years agoubusd: fix inverted check in ubusd_reply_add
Denis Osvald [Thu, 25 Aug 2016 11:54:54 +0000 (13:54 +0200)]
ubusd: fix inverted check in ubusd_reply_add

Signed-off-by: Denis Osvald <denis.osvald@sartura.hr>
7 years agoubusd: don't check ACL when object is NULL
Denis Osvald [Thu, 25 Aug 2016 11:54:52 +0000 (13:54 +0200)]
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>
7 years agoubusd: fix sending remove-object notification
Felix Fietkau [Tue, 23 Aug 2016 10:55:08 +0000 (12:55 +0200)]
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>
7 years agoubusd: add missing NULL pointer checks for obj->type
Felix Fietkau [Tue, 23 Aug 2016 09:36:41 +0000 (11:36 +0200)]
ubusd: add missing NULL pointer checks for obj->type

Signed-off-by: Felix Fietkau <nbd@nbd.name>
7 years agocmake: Fix find_library for ubusd and examples/server
Florian Fainelli [Fri, 1 Jul 2016 21:57:44 +0000 (14:57 -0700)]
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: 9f52d1769b762 ("cli: use the new json-c library name")
Signed-off-by: Florian Fainelli <f.fainelli@gmail.com>
7 years agoFix the length of msg buffer after realloc
Rujun Wang [Thu, 23 Jun 2016 03:06:06 +0000 (11:06 +0800)]
Fix the length of msg buffer after realloc

Signed-off-by: Rujun Wang <chinawrj@gmail.com>
7 years agoImplemented publish/subscribe lua bindings to libubus-lua with example lua files.
Iain Fraser [Thu, 19 May 2016 13:40:42 +0000 (14:40 +0100)]
Implemented publish/subscribe lua bindings to libubus-lua with example lua files.

7 years agoubusd: remove systemd socket activation support
John Crispin [Wed, 1 Jun 2016 09:37:30 +0000 (11:37 +0200)]
ubusd: remove systemd socket activation support

Signed-off-by: John Crispin <john@phrozen.org>
7 years agoubusd: fix systemd socket activation support
Matthias Schiffer [Tue, 24 May 2016 00:01:48 +0000 (02:01 +0200)]
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>
7 years agolibubus: nullify stale msgbuf pointer in case of ubus_connect_ctx() failure
Eyal Birger [Sun, 15 May 2016 05:13:27 +0000 (08:13 +0300)]
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>
8 years agocmake: Add ubox, blobmsg_json libraries and include dirs lookup
Petr Štetiar [Sat, 5 Mar 2016 13:59:33 +0000 (14:59 +0100)]
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>
8 years agocli: add timeout support for listen command
Zhao, Gang [Fri, 26 Feb 2016 09:18:39 +0000 (17:18 +0800)]
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>
8 years agocli: static keyword tweak
Zhao, Gang [Fri, 26 Feb 2016 09:18:38 +0000 (17:18 +0800)]
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>
8 years agoubus: use network order in ubus message header fields
Eyal Birger [Mon, 15 Feb 2016 04:09:38 +0000 (06:09 +0200)]
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>
8 years agolibubus.h: marking unused variables
Emanuel Taube [Fri, 12 Feb 2016 12:28:12 +0000 (13:28 +0100)]
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>
8 years agolibubus.h: add ubus_auto_shutdown()
Ben Kelly [Tue, 2 Feb 2016 13:02:59 +0000 (15:02 +0200)]
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>