From 8e240fa8e58e68f6ea084efbdd59051ad6ce031f Mon Sep 17 00:00:00 2001 From: Jo-Philipp Wich Date: Fri, 9 Sep 2022 13:04:01 +0200 Subject: 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 Signed-off-by: Jo-Philipp Wich --- lib/ubus.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) 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; } -- cgit v1.2.3