From 84282323d598f599e8e016467a118a7957f5ae1b Mon Sep 17 00:00:00 2001 From: Jakub Hrozek Date: Wed, 11 Nov 2015 13:40:16 +0100 Subject: [PATCH 2/2] DP: Reduce code duplication in Data Provider handlers Instead of setting the three same variables over again, add a structure be_sbus_reply_data with a default initializer BE_SBUS_REPLY_DATA_INIT. The handlers can then set the structure to BE_SBUS_REPLY_DATA_INIT on declaration or set a particular value with be_sbus_reply_data_set. The handler can also reply to the message (typically on failure state) with be_sbus_req_reply_data() --- src/providers/data_provider_be.c | 270 +++++++++++++++------------------------ 1 file changed, 102 insertions(+), 168 deletions(-) diff --git a/src/providers/data_provider_be.c b/src/providers/data_provider_be.c index 1027f039c6446e25ef57b7cde01dcdb54e6c8486..0580c799cd8ba4d6348612f47f51775a1f6bff5c 100644 --- a/src/providers/data_provider_be.c +++ b/src/providers/data_provider_be.c @@ -337,6 +337,40 @@ static errno_t be_offline_reply(struct sbus_request **sbus_req_ptr) return ret; } +struct be_sbus_reply_data { + dbus_uint16_t err_maj; + dbus_uint32_t err_min; + const char *err_msg; +}; + +#define BE_SBUS_REPLY_DATA_INIT { .err_maj = DP_ERR_FATAL, \ + .err_min = EFAULT, \ + .err_msg = "Fatal error" \ + }; + +static inline void be_sbus_reply_data_set(struct be_sbus_reply_data *rdata, + dbus_uint16_t err_maj, + dbus_uint32_t err_min, + const char *err_msg) +{ + if (rdata == NULL) { + DEBUG(SSSDBG_CRIT_FAILURE, + "Bug: Attempt to set NULL be_sbus_reply_data\n"); + return; + } + + rdata->err_maj = err_maj; + rdata->err_min = err_min; + rdata->err_msg = err_msg; +} + +static inline errno_t be_sbus_req_reply_data(struct sbus_request *sbus_req, + struct be_sbus_reply_data *data) +{ + return be_sbus_reply(sbus_req, data->err_maj, + data->err_min, data->err_msg); +} + void be_terminate_domain_requests(struct be_ctx *be_ctx, const char *domain) { @@ -757,9 +791,7 @@ static int be_get_subdomains(struct sbus_request *dbus_req, void *user_data) struct be_req *be_req = NULL; struct be_client *becli; char *domain_hint; - dbus_uint16_t err_maj; - dbus_uint32_t err_min; - const char *err_msg; + struct be_sbus_reply_data req_reply = BE_SBUS_REPLY_DATA_INIT; int ret; becli = talloc_get_type(user_data, struct be_client); @@ -772,10 +804,8 @@ static int be_get_subdomains(struct sbus_request *dbus_req, void *user_data) /* return an error if corresponding backend target is not configured */ if (becli->bectx->bet_info[BET_SUBDOMAINS].bet_ops == NULL) { - DEBUG(SSSDBG_TRACE_INTERNAL, "Undefined backend target.\n"); - err_maj = DP_ERR_FATAL; - err_min = ENODEV; - err_msg = "Subdomains back end target is not configured"; + be_sbus_reply_data_set(&req_reply, DP_ERR_FATAL, ENODEV, + "Subdomains back end target is not configured"); goto immediate; } @@ -786,9 +816,8 @@ static int be_get_subdomains(struct sbus_request *dbus_req, void *user_data) */ if (becli->bectx->offstat.offline) { DEBUG(SSSDBG_TRACE_FUNC, "Cannot proceed, provider is offline.\n"); - err_maj = DP_ERR_OFFLINE; - err_min = EAGAIN; - err_msg = "Provider is offline"; + be_sbus_reply_data_set(&req_reply, DP_ERR_OFFLINE, EAGAIN, + "Provider is offline"); goto immediate; } @@ -797,24 +826,21 @@ static int be_get_subdomains(struct sbus_request *dbus_req, void *user_data) be_req = be_req_create(becli, becli, becli->bectx, "get subdomains", get_subdomains_callback, dbus_req); if (!be_req) { - err_maj = DP_ERR_FATAL; - err_min = ENOMEM; - err_msg = "Out of memory"; + be_sbus_reply_data_set(&req_reply, DP_ERR_FATAL, ENOMEM, + "Out of memory"); goto immediate; } req = talloc(be_req, struct be_subdom_req); if (!req) { - err_maj = DP_ERR_FATAL; - err_min = ENOMEM; - err_msg = "Out of memory"; + be_sbus_reply_data_set(&req_reply, DP_ERR_FATAL, ENOMEM, + "Out of memory"); goto immediate; } req->domain_hint = talloc_strdup(req, domain_hint); if (!req->domain_hint) { - err_maj = DP_ERR_FATAL; - err_min = ENOMEM; - err_msg = "Out of memory"; + be_sbus_reply_data_set(&req_reply, DP_ERR_FATAL, ENOMEM, + "Out of memory"); goto immediate; } @@ -826,31 +852,16 @@ static int be_get_subdomains(struct sbus_request *dbus_req, void *user_data) be_req, becli->bectx->bet_info[BET_SUBDOMAINS].bet_ops->handler); if (ret != EOK) { - err_maj = DP_ERR_FATAL; - err_min = ret; - err_msg = "Cannot file back end request"; + be_sbus_reply_data_set(&req_reply, DP_ERR_FATAL, ret, + "Cannot file back end request"); goto immediate; } return EOK; immediate: - if (be_req) { - talloc_free(be_req); - } - - /* send reply back */ - 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 (!(err_maj == DP_ERR_FATAL && err_min == ENODEV)) { - DEBUG(SSSDBG_TRACE_LIBS, "Request processed. Returned %d,%d,%s\n", - err_maj, err_min, err_msg); - } - + talloc_free(be_req); + be_sbus_req_reply_data(dbus_req, &req_reply); return EOK; } @@ -1183,9 +1194,7 @@ static int be_get_account_info(struct sbus_request *dbus_req, void *user_data) char *domain; uint32_t attr_type; int ret; - dbus_uint16_t err_maj; - dbus_uint32_t err_min; - const char *err_msg; + struct be_sbus_reply_data req_reply = BE_SBUS_REPLY_DATA_INIT; be_req = NULL; @@ -1224,9 +1233,8 @@ static int be_get_account_info(struct sbus_request *dbus_req, void *user_data) be_req = be_req_create(becli, becli, becli->bectx, "get account info", be_req_default_callback, dbus_req); if (!be_req) { - err_maj = DP_ERR_FATAL; - err_min = ENOMEM; - err_msg = "Out of memory"; + be_sbus_reply_data_set(&req_reply, DP_ERR_FATAL, ENOMEM, + "Out of memory"); goto done; } @@ -1234,26 +1242,23 @@ static int be_get_account_info(struct sbus_request *dbus_req, void *user_data) if (ret != EOK) { DEBUG(SSSDBG_CRIT_FAILURE, "Unable to set request domain [%d]: %s\n", ret, sss_strerror(ret)); - err_maj = DP_ERR_FATAL; - err_min = ret; - err_msg = sss_strerror(ret); + be_sbus_reply_data_set(&req_reply, DP_ERR_FATAL, + ret, sss_strerror(ret)); goto done; } req = talloc_zero(be_req, struct be_acct_req); if (!req) { - err_maj = DP_ERR_FATAL; - err_min = ENOMEM; - err_msg = "Out of memory"; + be_sbus_reply_data_set(&req_reply, DP_ERR_FATAL, ENOMEM, + "Out of memory"); goto done; } req->entry_type = type; req->attr_type = (int)attr_type; req->domain = talloc_strdup(req, domain); if (!req->domain) { - err_maj = DP_ERR_FATAL; - err_min = ENOMEM; - err_msg = "Out of memory"; + be_sbus_reply_data_set(&req_reply, DP_ERR_FATAL, ENOMEM, + "Out of memory"); goto done; } @@ -1261,9 +1266,8 @@ static int be_get_account_info(struct sbus_request *dbus_req, void *user_data) (attr_type != BE_ATTR_MEM) && (attr_type != BE_ATTR_ALL)) { /* Unrecognized attr type */ - err_maj = DP_ERR_FATAL; - err_min = EINVAL; - err_msg = "Invalid Attrs Parameter"; + be_sbus_reply_data_set(&req_reply, DP_ERR_FATAL, EINVAL, + "Invalid Attrs Parameter"); goto done; } @@ -1299,55 +1303,35 @@ static int be_get_account_info(struct sbus_request *dbus_req, void *user_data) req->filter_value = NULL; req->extra_value = NULL; } else { - err_maj = DP_ERR_FATAL; - err_min = EINVAL; - err_msg = "Invalid Filter"; + be_sbus_reply_data_set(&req_reply, DP_ERR_FATAL, EINVAL, + "Invalid filter"); goto done; } if (ret != EOK) { - err_maj = DP_ERR_FATAL; - err_min = EINVAL; - err_msg = "Invalid Filter"; + be_sbus_reply_data_set(&req_reply, DP_ERR_FATAL, EINVAL, + "Invalid filter"); goto done; } } else { - err_maj = DP_ERR_FATAL; - err_min = EINVAL; - err_msg = "Missing Filter Parameter"; + be_sbus_reply_data_set(&req_reply, DP_ERR_FATAL, EINVAL, + "Missing filter parameter"); goto done; } ret = be_file_account_request(be_req, req); if (ret != EOK) { - err_maj = DP_ERR_FATAL; - err_min = ret; - err_msg = "Cannot file account request"; + be_sbus_reply_data_set(&req_reply, DP_ERR_FATAL, EINVAL, + "Cannot file account request"); goto done; } return EOK; done: - if (be_req) { - talloc_free(be_req); - } - - if (dbus_req) { - 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_CONF_SETTINGS, "Request processed. Returned %d,%d,%s\n", - err_maj, err_min, err_msg); - } - + talloc_free(be_req); + be_sbus_req_reply_data(dbus_req, &req_reply); return EOK; } @@ -1715,9 +1699,7 @@ static int be_autofs_handler(struct sbus_request *dbus_req, void *user_data) uint32_t type; char *filter; char *filter_val; - dbus_uint16_t err_maj; - dbus_uint32_t err_min; - const char *err_msg; + struct be_sbus_reply_data req_reply = BE_SBUS_REPLY_DATA_INIT; DEBUG(SSSDBG_TRACE_FUNC, "Entering be_autofs_handler()\n"); @@ -1750,15 +1732,13 @@ static int be_autofs_handler(struct sbus_request *dbus_req, void *user_data) if (strncmp(filter, "mapname=", 8) == 0) { filter_val = &filter[8]; } else { - err_maj = DP_ERR_FATAL; - err_min = EINVAL; - err_msg = "Invalid Filter"; + be_sbus_reply_data_set(&req_reply, DP_ERR_FATAL, EINVAL, + "Invalid filter"); goto done; } } else { - err_maj = DP_ERR_FATAL; - err_min = EINVAL; - err_msg = "Missing Filter Parameter"; + be_sbus_reply_data_set(&req_reply, DP_ERR_FATAL, EINVAL, + "Missing filter parameter"); goto done; } @@ -1767,9 +1747,8 @@ static int be_autofs_handler(struct sbus_request *dbus_req, void *user_data) be_req_default_callback, dbus_req); if (be_req == NULL) { DEBUG(SSSDBG_CRIT_FAILURE, "talloc_zero failed.\n"); - err_maj = DP_ERR_FATAL; - err_min = ENOMEM; - err_msg = "Out of memory"; + be_sbus_reply_data_set(&req_reply, DP_ERR_FATAL, ENOMEM, + "Out of memory"); goto done; } @@ -1777,18 +1756,16 @@ static int be_autofs_handler(struct sbus_request *dbus_req, void *user_data) be_autofs_req = talloc_zero(be_req, struct be_autofs_req); if (be_autofs_req == NULL) { DEBUG(SSSDBG_CRIT_FAILURE, "talloc_zero failed.\n"); - err_maj = DP_ERR_FATAL; - err_min = ENOMEM; - err_msg = "Out of memory"; + be_sbus_reply_data_set(&req_reply, DP_ERR_FATAL, ENOMEM, + "Out of memory"); goto done; } be_autofs_req->mapname = talloc_strdup(be_autofs_req, filter_val); if (be_autofs_req->mapname == NULL) { DEBUG(SSSDBG_CRIT_FAILURE, "talloc_strdup failed.\n"); - err_maj = DP_ERR_FATAL; - err_min = ENOMEM; - err_msg = "Out of memory"; + be_sbus_reply_data_set(&req_reply, DP_ERR_FATAL, ENOMEM, + "Out of memory"); goto done; } @@ -1796,9 +1773,8 @@ static int be_autofs_handler(struct sbus_request *dbus_req, void *user_data) if (!be_cli->bectx->bet_info[BET_AUTOFS].bet_ops) { DEBUG(SSSDBG_CRIT_FAILURE, "Undefined backend target.\n"); - err_maj = DP_ERR_FATAL; - err_min = ENODEV; - err_msg = "Autofs back end target is not configured"; + be_sbus_reply_data_set(&req_reply, DP_ERR_FATAL, ENODEV, + "Autofs back end target is not configured"); goto done; } @@ -1807,33 +1783,16 @@ static int be_autofs_handler(struct sbus_request *dbus_req, void *user_data) be_cli->bectx->bet_info[BET_AUTOFS].bet_ops->handler); if (ret != EOK) { DEBUG(SSSDBG_CRIT_FAILURE, "be_file_request failed.\n"); - err_maj = DP_ERR_FATAL; - err_min = ENODEV; - err_msg = "Cannot file back end request"; + be_sbus_reply_data_set(&req_reply, DP_ERR_FATAL, ret, + "Cannot file back end request"); goto done; } return EOK; done: - if (be_req) { - talloc_free(be_req); - } - - if (dbus_req) { - 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); - } - + talloc_free(be_req); + be_sbus_req_reply_data(dbus_req, &req_reply); return EOK; } @@ -1845,9 +1804,7 @@ static int be_host_handler(struct sbus_request *dbus_req, void *user_data) uint32_t flags; char *filter; int ret; - dbus_uint16_t err_maj; - dbus_uint32_t err_min; - const char *err_msg; + struct be_sbus_reply_data req_reply = BE_SBUS_REPLY_DATA_INIT; be_req = NULL; @@ -1881,17 +1838,15 @@ static int be_host_handler(struct sbus_request *dbus_req, void *user_data) be_req = be_req_create(becli, becli, becli->bectx, "hostinfo", be_req_default_callback, dbus_req); if (!be_req) { - err_maj = DP_ERR_FATAL; - err_min = ENOMEM; - err_msg = "Out of memory"; + be_sbus_reply_data_set(&req_reply, DP_ERR_FATAL, ENOMEM, + "Out of memory"); goto done; } req = talloc(be_req, struct be_host_req); if (!req) { - err_maj = DP_ERR_FATAL; - err_min = ENOMEM; - err_msg = "Out of memory"; + be_sbus_reply_data_set(&req_reply, DP_ERR_FATAL, ENOMEM, + "Out of memory"); goto done; } req->type = BE_REQ_HOST | (flags & BE_REQ_FAST); @@ -1908,15 +1863,13 @@ static int be_host_handler(struct sbus_request *dbus_req, void *user_data) } if (ret) { - err_maj = DP_ERR_FATAL; - err_min = EINVAL; - err_msg = "Invalid Filter"; + be_sbus_reply_data_set(&req_reply, DP_ERR_FATAL, EINVAL, + "Invalid filter"); goto done; } } else { - err_maj = DP_ERR_FATAL; - err_min = EINVAL; - err_msg = "Missing Filter Parameter"; + be_sbus_reply_data_set(&req_reply, DP_ERR_FATAL, EINVAL, + "Missing filter parameter"); goto done; } @@ -1924,9 +1877,8 @@ static int be_host_handler(struct sbus_request *dbus_req, void *user_data) if (!becli->bectx->bet_info[BET_HOSTID].bet_ops) { DEBUG(SSSDBG_CRIT_FAILURE, "Undefined backend target.\n"); - err_maj = DP_ERR_FATAL; - err_min = ENODEV; - err_msg = "HostID back end target is not configured"; + be_sbus_reply_data_set(&req_reply, DP_ERR_FATAL, ENODEV, + "HostID back end target is not configured"); goto done; } @@ -1934,34 +1886,16 @@ static int be_host_handler(struct sbus_request *dbus_req, void *user_data) be_req, becli->bectx->bet_info[BET_HOSTID].bet_ops->handler); if (ret != EOK) { - err_maj = DP_ERR_FATAL; - err_min = ret; - err_msg = "Failed to file request"; + be_sbus_reply_data_set(&req_reply, DP_ERR_FATAL, ret, + "Cannot file back end request"); goto done; } return EOK; done: - if (be_req) { - talloc_free(be_req); - } - - if (dbus_req) { - 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); - } - + talloc_free(be_req); + be_sbus_req_reply_data(dbus_req, &req_reply); return EOK; } -- 2.4.3