summaryrefslogtreecommitdiffstats
path: root/src/providers
diff options
context:
space:
mode:
authorJakub Hrozek <jhrozek@redhat.com>2015-11-11 13:40:16 +0100
committerLukas Slebodnik <lslebodn@redhat.com>2015-12-10 12:28:49 +0100
commit0741237b3f9209af43d956216b3c2f09b90c4ebc (patch)
treee16ce071bfb89bfb7034db8ac39f4aa000f12ed3 /src/providers
parent4afc1f2b6ca066d30d2be5ccda9fa760b5a6016e (diff)
downloadsssd-0741237b3f9209af43d956216b3c2f09b90c4ebc.tar.gz
sssd-0741237b3f9209af43d956216b3c2f09b90c4ebc.tar.xz
sssd-0741237b3f9209af43d956216b3c2f09b90c4ebc.zip
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() Reviewed-by: Pavel Březina <pbrezina@redhat.com>
Diffstat (limited to 'src/providers')
-rw-r--r--src/providers/data_provider_be.c269
1 files changed, 102 insertions, 167 deletions
diff --git a/src/providers/data_provider_be.c b/src/providers/data_provider_be.c
index f7f096b73..8e2baf27d 100644
--- a/src/providers/data_provider_be.c
+++ b/src/providers/data_provider_be.c
@@ -341,6 +341,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 = ERR_INTERNAL, \
+ .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)
{
@@ -761,9 +795,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);
@@ -777,9 +809,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;
}
@@ -790,9 +821,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;
}
@@ -801,24 +831,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;
}
@@ -830,31 +857,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;
}
@@ -1187,9 +1199,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;
@@ -1228,9 +1238,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;
}
@@ -1238,26 +1247,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;
}
@@ -1265,9 +1271,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;
}
@@ -1303,55 +1308,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;
}
@@ -1719,9 +1704,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");
@@ -1754,15 +1737,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;
}
@@ -1771,9 +1752,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;
}
@@ -1781,18 +1761,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;
}
@@ -1800,9 +1778,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;
}
@@ -1811,33 +1788,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;
}
@@ -1849,9 +1809,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;
@@ -1885,17 +1843,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);
@@ -1912,15 +1868,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;
}
@@ -1928,9 +1882,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;
}
@@ -1938,34 +1891,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;
}