From 1a837a04fd1bfb1fbf99c101d6637c6e89d00690 Mon Sep 17 00:00:00 2001 From: Jakub Hrozek Date: Wed, 11 Nov 2015 13:39:43 +0100 Subject: [PATCH 1/2] DP: Reduce code duplication in the callback handlers Instead of calling sbus_request_return_and_finish() directly with the same checks copied over, add a be_sbus_reply() helper instead. --- src/providers/data_provider_be.c | 268 ++++++++++++++------------------------- 1 file changed, 96 insertions(+), 172 deletions(-) diff --git a/src/providers/data_provider_be.c b/src/providers/data_provider_be.c index a9b33b5c6a047f21ebc779fd6a73a109ceadec9e..5ba04b6f1ef03dea041230e04009efaf8558746a 100644 --- a/src/providers/data_provider_be.c +++ b/src/providers/data_provider_be.c @@ -241,6 +241,86 @@ void be_req_terminate(struct be_req *be_req, be_req->fn(be_req, dp_err_type, errnum, errstr); } + +static errno_t be_sbus_reply(struct sbus_request *sbus_req, + dbus_uint16_t err_maj, + dbus_uint32_t err_min, + const char *err_msg) +{ + errno_t ret; + const char *safe_err_msg; + + /* Only return a reply if one was requested + * There may not be one if this request began + * while we were offline + */ + if (sbus_req == NULL) { + return EOK; + } + + safe_err_msg = safe_be_req_err_msg(err_msg, err_maj); + + if (err_maj == DP_ERR_FATAL && err_min == ENODEV) { + DEBUG(SSSDBG_TRACE_LIBS, "Handler not configured\n"); + } else { + DEBUG(SSSDBG_TRACE_LIBS, + "Request processed. Returned %d,%d,%s\n", + err_maj, err_min, err_msg); + } + + ret = sbus_request_return_and_finish(sbus_req, + DBUS_TYPE_UINT16, &err_maj, + DBUS_TYPE_UINT32, &err_min, + DBUS_TYPE_STRING, &safe_err_msg, + DBUS_TYPE_INVALID); + if (ret != EOK) { + DEBUG(SSSDBG_CRIT_FAILURE, + "sbus_request_return_and_finish failed: [%d]: %s\n", + ret, sss_strerror(ret)); + } + + return ret; +} + +static errno_t be_sbus_req_reply(struct sbus_request *sbus_req, + const char *req_name, + int dp_err_type, + int errnum, + const char *errstr) +{ + const char *req_msg; + dbus_uint16_t err_maj; + dbus_uint32_t err_min; + + err_maj = dp_err_type; + err_min = errnum; + + req_msg = req_name ? req_name : "sbus"; + DEBUG(SSSDBG_TRACE_FUNC, "Replying to %s request\n", req_msg); + return be_sbus_reply(sbus_req, err_maj, err_min, errstr); +} + +/* Send back an immediate reply and set the sbus_request to NULL + * so that we are sure the request is not reused in the future + */ +static errno_t be_offline_reply(struct sbus_request **sbus_req_ptr, + const char *req_name) +{ + struct sbus_request *dbus_req; + errno_t ret; + + if (sbus_req_ptr == NULL) { + return EINVAL; + } + dbus_req = *sbus_req_ptr; + + ret = be_sbus_req_reply(dbus_req, req_name, + DP_ERR_OFFLINE, EAGAIN, + "Fast reply - offline"); + *sbus_req_ptr = NULL; + return ret; +} + void be_terminate_domain_requests(struct be_ctx *be_ctx, const char *domain) { @@ -438,9 +518,6 @@ static void be_queue_next_request(struct be_req *be_req, enum bet_type type) struct bet_queue_item **req_queue; struct sbus_request *dbus_req; int ret; - uint16_t err_maj; - uint32_t err_min; - const char *err_msg = "Cannot file back end request"; struct be_req *next_be_req = NULL; req_queue = &be_req->becli->bectx->bet_info[type].req_queue; @@ -480,21 +557,9 @@ static void be_queue_next_request(struct be_req *be_req, enum bet_type type) dbus_req = (struct sbus_request *) next_be_req->pvt; - if (dbus_req) { - /* Return a reply if one was requested - * There may not be one if this request began - * while we were offline - */ - err_maj = DP_ERR_FATAL; - err_min = ret; - - sbus_request_return_and_finish(dbus_req, - DBUS_TYPE_UINT16, &err_maj, - DBUS_TYPE_UINT32, &err_min, - DBUS_TYPE_STRING, &err_msg, - DBUS_TYPE_INVALID); - } - + be_sbus_req_reply(dbus_req, "be_queue_next_request", + DP_ERR_FATAL, ret, + "Cannot file back end request"); talloc_free(next_be_req); } @@ -662,34 +727,12 @@ static void get_subdomains_callback(struct be_req *req, const char *errstr) { struct sbus_request *dbus_req; - dbus_uint16_t err_maj = 0; - dbus_uint32_t err_min = 0; - const char *err_msg = NULL; - - DEBUG(SSSDBG_TRACE_FUNC, "Backend returned: (%d, %d, %s) [%s]\n", - dp_err_type, errnum, errstr?errstr:"", - dp_err_to_string(dp_err_type)); be_queue_next_request(req, BET_SUBDOMAINS); - dbus_req = (struct sbus_request *)req->pvt; - - if (dbus_req) { - /* Return a reply if one was requested - * There may not be one if this request began - * while we were offline - */ - err_maj = dp_err_type; - err_min = errnum; - err_msg = safe_be_req_err_msg(errstr, dp_err_type); - - sbus_request_return_and_finish(dbus_req, - DBUS_TYPE_UINT16, &err_maj, - DBUS_TYPE_UINT32, &err_min, - DBUS_TYPE_STRING, &err_msg, - DBUS_TYPE_INVALID); - } + dbus_req = (struct sbus_request *) req->pvt; + be_sbus_req_reply(dbus_req, "subdomains", dp_err_type, errnum, errstr); talloc_free(req); } @@ -802,33 +845,10 @@ static void acctinfo_callback(struct be_req *req, const char *errstr) { struct sbus_request *dbus_req; - dbus_uint16_t err_maj = 0; - dbus_uint32_t err_min = 0; - const char *err_msg = NULL; - dbus_req = (struct sbus_request *)req->pvt; + dbus_req = (struct sbus_request *) req->pvt; - if (dbus_req) { - /* Return a reply if one was requested - * There may not be one if this request began - * while we were offline - */ - - err_maj = dp_err_type; - err_min = errnum; - err_msg = safe_be_req_err_msg(errstr, dp_err_type); - - sbus_request_return_and_finish(dbus_req, - DBUS_TYPE_UINT16, &err_maj, - DBUS_TYPE_UINT32, &err_min, - DBUS_TYPE_STRING, &err_msg, - DBUS_TYPE_INVALID); - - DEBUG(SSSDBG_CONF_SETTINGS, "Request processed. Returned %d,%d,%s\n", - err_maj, err_min, err_msg); - } - - /* finally free the request */ + be_sbus_req_reply(dbus_req, "account info", dp_err_type, errnum, errstr); talloc_free(req); } @@ -1186,24 +1206,11 @@ static int be_get_account_info(struct sbus_request *dbus_req, void *user_data) * return offline immediately */ if ((type & BE_REQ_FAST) && becli->bectx->offstat.offline) { - /* Send back an immediate reply */ - err_maj = DP_ERR_OFFLINE; - err_min = EAGAIN; - err_msg = "Fast reply - offline"; - - ret = sbus_request_return_and_finish(dbus_req, - DBUS_TYPE_UINT16, &err_maj, - DBUS_TYPE_UINT32, &err_min, - DBUS_TYPE_STRING, &err_msg, - DBUS_TYPE_INVALID); + ret = be_offline_reply(&dbus_req, "account"); if (ret != EOK) { return ret; } - DEBUG(SSSDBG_CONF_SETTINGS, "Request processed. Returned %d,%d,%s\n", - err_maj, err_min, err_msg); - - dbus_req = NULL; /* This reply will be queued and sent * when we reenter the mainloop. * @@ -1535,41 +1542,16 @@ done: return EOK; } -static void be_sudo_handler_reply(struct sbus_request *dbus_req, - dbus_uint16_t dp_err, - dbus_uint32_t dp_ret, - const char *errstr) -{ - const char *err_msg = NULL; - - if (dbus_req == NULL) { - return; - } - - err_msg = errstr ? errstr : "No errmsg set\n"; - sbus_request_return_and_finish(dbus_req, - DBUS_TYPE_UINT16, &dp_err, - DBUS_TYPE_UINT32, &dp_ret, - DBUS_TYPE_STRING, &err_msg, - DBUS_TYPE_INVALID); - - DEBUG(SSSDBG_FUNC_DATA, "SUDO Backend returned: (%d, %d, %s)\n", - dp_err, dp_ret, errstr ? errstr : ""); -} - static void be_sudo_handler_callback(struct be_req *req, int dp_err, int dp_ret, const char *errstr) { - const char *err_msg = NULL; struct sbus_request *dbus_req; - dbus_req = (struct sbus_request *)(req->pvt); - - err_msg = safe_be_req_err_msg(errstr, dp_err); - be_sudo_handler_reply(dbus_req, dp_err, dp_ret, err_msg); + dbus_req = (struct sbus_request *) req->pvt; + be_sbus_req_reply(dbus_req, "sudo", dp_err, dp_ret, errstr); talloc_free(req); } @@ -1620,9 +1602,8 @@ static int be_sudo_handler(struct sbus_request *dbus_req, void *user_data) * return offline immediately */ if ((type & BE_REQ_FAST) && be_cli->bectx->offstat.offline) { - be_sudo_handler_reply(dbus_req, DP_ERR_OFFLINE, EAGAIN, - "Fast reply - offline"); - be_req->pvt = dbus_req = NULL; + be_offline_reply(&dbus_req, "sudo"); + be_req->pvt = NULL; /* This reply will be queued and sent * when we reenter the mainloop. * @@ -1772,24 +1753,7 @@ static int be_autofs_handler(struct sbus_request *dbus_req, void *user_data) * return offline immediately */ if ((type & BE_REQ_FAST) && be_cli->bectx->offstat.offline) { - /* Send back an immediate reply */ - err_maj = DP_ERR_OFFLINE; - err_min = EAGAIN; - err_msg = "Fast reply - offline"; - - ret = sbus_request_return_and_finish(dbus_req, - DBUS_TYPE_UINT16, &err_maj, - DBUS_TYPE_UINT32, &err_min, - DBUS_TYPE_STRING, &err_msg, - DBUS_TYPE_INVALID); - if (ret != EOK) { - return ret; - } - - DEBUG(SSSDBG_TRACE_LIBS, "Request processed. Returned %d,%d,%s\n", - err_maj, err_min, err_msg); - - dbus_req = NULL; + be_offline_reply(&dbus_req, "autofs"); /* This reply will be queued and sent * when we reenter the mainloop. * @@ -1895,34 +1859,10 @@ static void be_autofs_handler_callback(struct be_req *req, const char *errstr) { struct sbus_request *dbus_req; - dbus_uint16_t err_maj = 0; - dbus_uint32_t err_min = 0; - const char *err_msg = NULL; - dbus_req = (struct sbus_request *)req->pvt; + dbus_req = (struct sbus_request *) req->pvt; - if (dbus_req) { - /* Return a reply if one was requested - * There may not be one if this request began - * while we were offline - */ - - err_maj = dp_err_type; - err_min = errnum; - err_msg = safe_be_req_err_msg(errstr, dp_err_type); - - sbus_request_return_and_finish(dbus_req, - DBUS_TYPE_UINT16, &err_maj, - DBUS_TYPE_UINT32, &err_min, - DBUS_TYPE_STRING, &err_msg, - DBUS_TYPE_INVALID); - - DEBUG(SSSDBG_TRACE_LIBS, - "Request processed. Returned %d,%d,%s\n", - err_maj, err_min, err_msg); - } - - /* finally free the request */ + be_sbus_req_reply(dbus_req, "autofs", dp_err_type, errnum, errstr); talloc_free(req); } @@ -1957,24 +1897,8 @@ static int be_host_handler(struct sbus_request *dbus_req, void *user_data) */ if ((flags & BE_REQ_FAST) && becli->bectx->offstat.offline) { /* Send back an immediate reply */ - err_maj = DP_ERR_OFFLINE; - err_min = EAGAIN; - err_msg = "Fast reply - offline"; + be_offline_reply(&dbus_req, "host"); - ret = sbus_request_return_and_finish(dbus_req, - DBUS_TYPE_UINT16, &err_maj, - DBUS_TYPE_UINT32, &err_min, - DBUS_TYPE_STRING, &err_msg, - DBUS_TYPE_INVALID); - if (ret != EOK) { - return ret; - } - - DEBUG(SSSDBG_TRACE_LIBS, - "Request processed. Returned %d,%d,%s\n", - err_maj, err_min, err_msg); - - dbus_req = NULL; /* This reply will be queued and sent * when we reenter the mainloop. * -- 2.4.3