summaryrefslogtreecommitdiffhomepage
path: root/lib/ubus.c
AgeCommit message (Collapse)Author
2024-05-02ubus: fix uc_ubus_have_uloop for eloop+uloop combinationFelix Fietkau
When uloop has been integrated with eloop (in hostapd/wpa_supplicant), uloop_cancelling will return false, since uloop_run is not being called. This leads to ubus.defer() calling uloop_run in a context where it can prevent the other event loop from running. Fix this by detecting event loop integration via uloop_fd_set_cb being set Signed-off-by: Felix Fietkau <nbd@nbd.name>
2024-04-07ubus: add explicit support for deferring incoming requestsFelix Fietkau
This is needed when asynchronously processing requests via uloop, e.g. using uclient. Example script: let libubus = require("ubus"); let uloop = require("uloop"); uloop.init(); let ubus = libubus.connect(); ubus.publish("test", { test: { call: function(req) { req.defer(); uloop.timer(1000, () => { req.reply({ msg: "Hello, world!" }, 0); }); }, args: {} }, }); uloop.run(); uloop.done(); Signed-off-by: Felix Fietkau <nbd@nbd.name>
2024-02-21ubus: automatically clear error informationJo-Philipp Wich
Make all functions clear the last error information on success in order to ensure that `error()` never reports stale information. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2023-11-30ubus: make ubus_context first in uc_ubus_connection_tFelix Fietkau
Allows other C code to gain access to the ubus context without having to know the layout of the other fields. Also reduce the amount of unnecessary pointer indirection Signed-off-by: Felix Fietkau <nbd@nbd.name>
2023-05-27ubus: add support for strings containing null bytesFelix Fietkau
When converting ucode strings to blobmsg, use blobmsg_add_field in order to explicltly pass the length of the data. In the other direction, use ucv_string_new_length based on the attribute length. Signed-off-by: Felix Fietkau <nbd@nbd.name>
2023-03-17ubus: support multiple call return valuesJo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2022-09-21ubus: hold reference to underlying connection until deferred is concludedJo-Philipp Wich
Prevent GC'ing (and thus tearing down) an active ubus connection as long as there's still unfinished deferred request contexts alive. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2022-09-09ubus: support obtaining numeric error codeJo-Philipp Wich
Some ubus users require access to the original ubus error status returned by various operations for fine grained error handling. Extend the error() function with an optional boolean argument which causes the function to return the numeric error code instead of a preformatted message when invoked. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2022-09-09ubus: add toplevel constants for ubus status codesJo-Philipp Wich
Add constants for ubus status codes to the toplevel module scope in order to avoid the need for magic values in the code. Suggested-by: Felix Fietkau <nbd@nbd.name> Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2022-09-09ubus: allow object method call handlers to return a numeric status codeJo-Philipp Wich
The implicit return style for sending ubus method replies currently always emits an UBUS_STATUS_NO_DATA code in case neither req.reply() was called, nor a deferred or object were returned by the handler function. This slightly complicates the implementation of handlers that do not wish to send reply data but simply acknowledge the request with an UBUS_STATUS_OK code. In order to simplify this use case, allow handlers to override the default status by treating integer return values as ubus error codes. After this change, the following handler: function (request) { /* do some work */ request.reply(null, 0); } ... can be rewritten as: function (request) { /* do some work */ return 0; } Suggested-by: Felix Fietkau <nbd@nbd.name> Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2022-08-24ubus: fix GCC strncpy() truncation warningJo-Philipp Wich
When building with gcc-10 and -O2, the following warning in ubus.c is triggered during the compilation: In function ‘uc_ubus_object_register’, inlined from ‘uc_ubus_publish’ at .../ubus.c:1521:10: .../ubus.c:1464:14: error: ‘strncpy’ output truncated before terminating nul copying as many bytes from a string as its length [-Werror=stringop-truncation] 1464 | obj->name = strncpy(onptr, ubus_object_name, namelen); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .../ubus.c: In function ‘uc_ubus_publish’: .../ubus.c:1447:12: note: length computed here 1447 | namelen = strlen(ubus_object_name); | ^~~~~~~~~~~~~~~~~~~~~~~~ cc1: all warnings being treated as errors Solve this issue by using memcpy() instead. We already take care of allocating a zeroed, strlen() + 1 sized destination buffer so loosing the `\0` byte of the source string is perfectly fine. Fixes: #100 Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2022-06-29ubus: end uloop on exceptions in managed codeJo-Philipp Wich
Instead of silently continuing, end the uloop when encountering exceptions in ucode callbacks to let those exceptions propagate to the host program. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2022-03-07ubus: add event supportJo-Philipp Wich
Extend the ubus binding to cover ubus event handling APIs. Instantiating ubus event listener: listener = conn.listener( "event.type.*", function (type, data) { ...event callback... } ); listener.remove(); Broadcasting events: conn.event("event.type.foo", { ...event data... }); Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2022-03-07ubus: refactor error and argument handlingJo-Philipp Wich
- Add more detailled error messages - Introduce helpers for fetching and validating function call arguments - Get rid of some uneeded blob->jso->ucv conversions Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2022-03-07ubus: add object publishing, notify and subscribe supportJo-Philipp Wich
Extend the ubus binding to cover ubus object publishing, notifications on objects, as well as subscriber APIs. Instantiating ubus objects: obj = conn.publish("objname", { methodname: { args: { ...argspec... }, call: function(request) { ...method handler... } }, ... }, function() { ...subscription status change handler... }); obj.notify(...); obj.remove(); Emitting notifications: obj.notify("notificationtype", { ...notification data... }, function(type, data) { ...data callback... }, function(idx, ret) { ...status callback... }, function() { ...completion callback... }, 100 /* timeout */ ); Instantiating subscribers: sub = conn.subscriber( function(notify) { ...notification handler... }, function(id) { ...object gone handler... } ); sub.subscribe("objname"); sub.unsubscribe("objname"); sub.remove(); Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2021-12-08ubus: add support for async requestsJo-Philipp Wich
Introduce a new ubus.defer() method which initiates asynchroneous requests and invokes the completion callback passed as 4th argument once the reply is received. This allows multiplexing mutliple ubus requests with the same ucode VM context / the same uloop event loop. In case the ucode context is not running under an active uloop, the ubus module will spawn uloop itself once the first asynchroneous request is launched and terminate the loop once all pending requests finished. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2021-11-01treewide: fix typo in exported function names and typesJo-Philipp Wich
Fix instances of misspelled "resource". This commit breaks the exported libucode ABI. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2021-09-24ubus: properly handle signed 64bit values tooJo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2021-09-23ubus: fix handling signed 16bit and 32bit integersJo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2021-07-11treewide: harmonize function namingJo-Philipp Wich
- Ensure that most functions follow the subject_verb naming schema - Move type related function from value.c to types.c - Rename value.c to vallist.c Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2021-07-11treewide: move header files into dedicated directoryJo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2021-07-11treewide: consolidate typedef namingJo-Philipp Wich
Ensure that all custom typedef and vector declaration type names end with a "_t" suffix. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2021-07-11lib: rename uc_add_proto_functions() to uc_add_functions()Jo-Philipp Wich
The naming is an artifact from before the introduction of the new type system. In the current code, there is nothing special about prototypes, they're simple object values. Also introduce a new singular uc_add_function() convenience macro while we're at it. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2021-07-11treewide: move ressource type registry into vm instanceJo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2021-04-25treewide: rework internal data type systemJo-Philipp Wich
Instead of relying on json_object values internally, use custom types to represent the different ucode value types which brings a number of advantages compared to the previous approach: - Due to the use of tagged pointers, small integer, string and bool values can be stored directly in the pointer addresses, vastly reducing required heap memory - Ability to create circular data structures such as `let o; o = { test: o };` - Ability to register custom `tostring()` function through prototypes - Initial mark/sweep GC implementation to tear down circular object graphs on VM deinit The change also paves the way for possible future extensions such as constant variables and meta methods for custom ressource types. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2021-03-08ubus: make error function available on the connection instanceJo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2021-02-17treewide: rewrite ucode interpreterJo-Philipp Wich
Replace the former AST walking interpreter implementation with a single pass bytecode compiler and a corresponding virtual machine. The rewrite lays the groundwork for a couple of improvements with will be subsequently implemented: - Ability to precompile ucode sources into binary byte code - Strippable debug information - Reduced runtime memory usage Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-12-07ubus: fix uninitialized pointer return in uc_ubus_call() and uc_ubus_list()Jo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-11-19treewide: rebrand to ucodeJo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-10-19eval: record correct source contexts in call stackJo-Philipp Wich
Also handle calls to C functions. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-09-20treewide: rework extended type handlingJo-Philipp Wich
Register prototype object directly together with the type instead of setting it manually whenever an extended type value is instantiated. This also allows freeing the various prototype objects in dlopened modules. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-09-10treewide: eliminate unused function argumentsJo-Philipp Wich
Also introduce convenience macro for registering function arrays in modules. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2020-09-10lib: add ubus moduleJo-Philipp Wich
Also move shared library output files to the lib/ directory Signed-off-by: Jo-Philipp Wich <jo@mein.io>