summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2022-09-09 13:04:01 +0200
committerJo-Philipp Wich <jo@mein.io>2022-09-09 13:04:01 +0200
commit8e240fa8e58e68f6ea084efbdd59051ad6ce031f (patch)
tree5afedeb19dc9cec3b3cb6e9b06e6a3f916c19a88
parent5cdddd32ef3df78be7cb187446235a04491036b5 (diff)
ubus: allow object method call handlers to return a numeric status code
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>
-rw-r--r--lib/ubus.c11
1 files changed, 10 insertions, 1 deletions
diff --git a/lib/ubus.c b/lib/ubus.c
index 4766119..e2491ec 100644
--- a/lib/ubus.c
+++ b/lib/ubus.c
@@ -1226,7 +1226,16 @@ uc_ubus_handle_reply_common(struct ubus_context *ctx,
* returned and if reqobj.reply() hasn't been called, immediately
* finish deferred request with UBUS_STATUS_NO_DATA. */
else if (!callctx->replied) {
- ubus_complete_deferred_request(ctx, &callctx->req, UBUS_STATUS_NO_DATA);
+ rv = UBUS_STATUS_NO_DATA;
+
+ if (ucv_type(res) == UC_INTEGER) {
+ rv = (int)ucv_int64_get(res);
+
+ if (rv < 0 || rv > __UBUS_STATUS_LAST)
+ rv = UBUS_STATUS_UNKNOWN_ERROR;
+ }
+
+ ubus_complete_deferred_request(ctx, &callctx->req, rv);
callctx->replied = true;
}