summaryrefslogtreecommitdiff
path: root/server/sbus
diff options
context:
space:
mode:
authorSimo Sorce <ssorce@redhat.com>2009-08-05 14:11:12 -0400
committerStephen Gallagher <sgallagh@redhat.com>2009-08-10 09:42:20 -0400
commit11c621b5ee1a0cdc27610f8b172017764acc285e (patch)
tree181c9079440367711c66d7281fc0aecb458fee77 /server/sbus
parentf1e4471551aa74015579bff0b64735cc9b085b74 (diff)
downloadsssd-11c621b5ee1a0cdc27610f8b172017764acc285e.tar.gz
sssd-11c621b5ee1a0cdc27610f8b172017764acc285e.tar.bz2
sssd-11c621b5ee1a0cdc27610f8b172017764acc285e.zip
Simplify interfaces initialization
Make as much as possible static, and remove use of talloc_reference and allocation/deallocation of memory when not necessary. Fix also responder use of rctx->conn, was mistakenly used for both monitor and dp connections.
Diffstat (limited to 'server/sbus')
-rw-r--r--server/sbus/sbus_client.c9
-rw-r--r--server/sbus/sbus_client.h2
-rw-r--r--server/sbus/sssd_dbus.h28
-rw-r--r--server/sbus/sssd_dbus_connection.c137
-rw-r--r--server/sbus/sssd_dbus_private.h6
-rw-r--r--server/sbus/sssd_dbus_server.c11
6 files changed, 93 insertions, 100 deletions
diff --git a/server/sbus/sbus_client.c b/server/sbus/sbus_client.c
index 82021d6e..df5c0712 100644
--- a/server/sbus/sbus_client.c
+++ b/server/sbus/sbus_client.c
@@ -26,7 +26,7 @@
int sbus_client_init(TALLOC_CTX *mem_ctx,
struct tevent_context *ev,
const char *server_address,
- struct sbus_method_ctx *sm_ctx,
+ struct sbus_interface *intf,
struct sbus_connection **_conn,
sbus_conn_destructor_fn destructor,
void *conn_pvt_data)
@@ -39,12 +39,7 @@ int sbus_client_init(TALLOC_CTX *mem_ctx,
return EINVAL;
}
- ret = sbus_new_connection(mem_ctx, ev, server_address, &conn);
- if (ret != EOK) {
- goto fail;
- }
-
- ret = sbus_conn_add_method_ctx(conn, sm_ctx);
+ ret = sbus_new_connection(mem_ctx, ev, server_address, intf, &conn);
if (ret != EOK) {
goto fail;
}
diff --git a/server/sbus/sbus_client.h b/server/sbus/sbus_client.h
index bce8f821..742f8a10 100644
--- a/server/sbus/sbus_client.h
+++ b/server/sbus/sbus_client.h
@@ -28,7 +28,7 @@
int sbus_client_init(TALLOC_CTX *mem_ctx,
struct tevent_context *ev,
const char *server_address,
- struct sbus_method_ctx *sm_ctx,
+ struct sbus_interface *intf,
struct sbus_connection **_conn,
sbus_conn_destructor_fn destructor,
void *conn_pvt_data);
diff --git a/server/sbus/sssd_dbus.h b/server/sbus/sssd_dbus.h
index 6487ced6..ac02c444 100644
--- a/server/sbus/sssd_dbus.h
+++ b/server/sbus/sssd_dbus.h
@@ -59,15 +59,17 @@ enum {
#define DBUS_INTROSPECT_INTERFACE "org.freedesktop.DBus.Introspectable"
#define DBUS_INTROSPECT_METHOD "Introspect"
+#define SBUS_DEFAULT_VTABLE { NULL, sbus_message_handler, NULL, NULL, NULL, NULL }
+
struct sbus_method {
const char *method;
sbus_msg_handler_fn fn;
};
-struct sbus_method_ctx {
- char *interface;
- char *path;
- DBusObjectPathMessageFunction message_handler;
+struct sbus_interface {
+ const char *interface;
+ const char *path;
+ DBusObjectPathVTable vtable;
struct sbus_method *methods;
sbus_msg_handler_fn introspect_fn;
};
@@ -76,7 +78,7 @@ struct sbus_method_ctx {
int sbus_new_server(TALLOC_CTX *mem_ctx,
struct tevent_context *ev,
const char *address,
- struct sbus_method_ctx *ctx,
+ struct sbus_interface *intf,
struct sbus_connection **server,
sbus_server_conn_init_fn init_fn, void *init_pvt_data);
@@ -92,6 +94,7 @@ int sbus_new_server(TALLOC_CTX *mem_ctx,
int sbus_new_connection(TALLOC_CTX *ctx,
struct tevent_context *ev,
const char *address,
+ struct sbus_interface *intf,
struct sbus_connection **conn);
/* sbus_add_connection
@@ -106,11 +109,12 @@ int sbus_new_connection(TALLOC_CTX *ctx,
* connections, including those retrieved from
* dbus_bus_get
*/
-int sbus_add_connection(TALLOC_CTX *ctx,
- struct tevent_context *ev,
- DBusConnection *dbus_conn,
- struct sbus_connection **conn,
- int connection_type);
+int sbus_init_connection(TALLOC_CTX *ctx,
+ struct tevent_context *ev,
+ DBusConnection *dbus_conn,
+ struct sbus_interface *intf,
+ int connection_type,
+ struct sbus_connection **_conn);
void sbus_conn_set_destructor(struct sbus_connection *conn,
sbus_conn_destructor_fn destructor);
@@ -121,8 +125,8 @@ DBusConnection *sbus_get_connection(struct sbus_connection *conn);
void sbus_disconnect(struct sbus_connection *conn);
void sbus_conn_set_private_data(struct sbus_connection *conn, void *pvt_data);
void *sbus_conn_get_private_data(struct sbus_connection *conn);
-int sbus_conn_add_method_ctx(struct sbus_connection *conn,
- struct sbus_method_ctx *method_ctx);
+int sbus_conn_add_interface(struct sbus_connection *conn,
+ struct sbus_interface *intf);
bool sbus_conn_disconnecting(struct sbus_connection *conn);
/* max_retries < 0: retry forever
diff --git a/server/sbus/sssd_dbus_connection.c b/server/sbus/sssd_dbus_connection.c
index f2435bfb..ab414da0 100644
--- a/server/sbus/sssd_dbus_connection.c
+++ b/server/sbus/sssd_dbus_connection.c
@@ -7,14 +7,14 @@
/* Types */
struct dbus_ctx_list;
-struct method_holder {
- struct method_holder *prev, *next;
+struct sbus_interface_p {
+ struct sbus_interface_p *prev, *next;
struct sbus_connection *conn;
- struct sbus_method_ctx *method_ctx;
- DBusObjectPathVTable *dbus_vtable;
+ struct sbus_interface *intf;
};
-static bool path_in_method_list(struct method_holder *list, const char *path);
+static bool path_in_interface_list(struct sbus_interface_p *list,
+ const char *path);
static void sbus_unreg_object_paths(struct sbus_connection *conn);
static int sbus_auto_reconnect(struct sbus_connection *conn);
@@ -56,8 +56,8 @@ static void sbus_dispatch(struct tevent_context *ev,
DEBUG(0, ("Cannot start auto-reconnection.\n"));
conn->reconnect_callback(conn,
- SBUS_RECONNECT_ERROR,
- conn->reconnect_pvt);
+ SBUS_RECONNECT_ERROR,
+ conn->reconnect_pvt);
return;
}
@@ -129,11 +129,12 @@ static int sbus_conn_set_fns(struct sbus_connection *conn);
* Set up a D-BUS connection to use the libevents mainloop
* for handling file descriptor and timed events
*/
-int sbus_add_connection(TALLOC_CTX *ctx,
- struct tevent_context *ev,
- DBusConnection *dbus_conn,
- struct sbus_connection **_conn,
- int connection_type)
+int sbus_init_connection(TALLOC_CTX *ctx,
+ struct tevent_context *ev,
+ DBusConnection *dbus_conn,
+ struct sbus_interface *intf,
+ int connection_type,
+ struct sbus_connection **_conn)
{
struct sbus_connection *conn;
int ret;
@@ -149,10 +150,16 @@ int sbus_add_connection(TALLOC_CTX *ctx,
ret = sbus_conn_set_fns(conn);
if (ret != EOK) {
talloc_free(conn);
+ return ret;
}
- *_conn = conn;
+ ret = sbus_conn_add_interface(conn, intf);
+ if (ret != EOK) {
+ talloc_free(conn);
+ return ret;
+ }
+ *_conn = conn;
return ret;
}
@@ -208,9 +215,10 @@ static int sbus_conn_set_fns(struct sbus_connection *conn)
}
int sbus_new_connection(TALLOC_CTX *ctx, struct tevent_context *ev,
- const char *address,
- struct sbus_connection **conn)
+ const char *address, struct sbus_interface *intf,
+ struct sbus_connection **_conn)
{
+ struct sbus_connection *conn;
DBusConnection *dbus_conn;
DBusError dbus_error;
int ret;
@@ -226,17 +234,18 @@ int sbus_new_connection(TALLOC_CTX *ctx, struct tevent_context *ev,
return EIO;
}
- ret = sbus_add_connection(ctx, ev, dbus_conn,
- conn, SBUS_CONN_TYPE_SHARED);
+ ret = sbus_init_connection(ctx, ev, dbus_conn, intf,
+ SBUS_CONN_TYPE_SHARED, &conn);
if (ret != EOK) {
/* FIXME: release resources */
}
/* Store the address for later reconnection */
- (*conn)->address = talloc_strdup((*conn), address);
+ conn->address = talloc_strdup(conn, address);
- dbus_connection_set_exit_on_disconnect((*conn)->dbus.conn, FALSE);
+ dbus_connection_set_exit_on_disconnect(conn->dbus.conn, FALSE);
+ *_conn = conn;
return ret;
}
@@ -364,7 +373,7 @@ DBusHandlerResult sbus_message_handler(DBusConnection *dbus_conn,
DBusMessage *message,
void *user_data)
{
- struct method_holder *ctx;
+ struct sbus_interface_p *intf_p;
const char *method;
const char *path;
const char *msg_interface;
@@ -375,7 +384,7 @@ DBusHandlerResult sbus_message_handler(DBusConnection *dbus_conn,
if (!user_data) {
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
- ctx = talloc_get_type(user_data, struct method_holder);
+ intf_p = talloc_get_type(user_data, struct sbus_interface_p);
method = dbus_message_get_member(message);
DEBUG(9, ("Received SBUS method [%s]\n", method));
@@ -386,18 +395,18 @@ DBusHandlerResult sbus_message_handler(DBusConnection *dbus_conn,
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
/* Validate the D-BUS path */
- if (strcmp(path, ctx->method_ctx->path) != 0)
+ if (strcmp(path, intf_p->intf->path) != 0)
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
/* Validate the method interface */
- if (strcmp(msg_interface, ctx->method_ctx->interface) == 0) {
+ if (strcmp(msg_interface, intf_p->intf->interface) == 0) {
found = 0;
- for (i = 0; ctx->method_ctx->methods[i].method != NULL; i++) {
- if (strcmp(method, ctx->method_ctx->methods[i].method) == 0) {
+ for (i = 0; intf_p->intf->methods[i].method != NULL; i++) {
+ if (strcmp(method, intf_p->intf->methods[i].method) == 0) {
found = 1;
- ret = ctx->method_ctx->methods[i].fn(message, ctx->conn);
+ ret = intf_p->intf->methods[i].fn(message, intf_p->conn);
if (ret != EOK) {
- return sbus_reply_internal_error(message, ctx->conn);
+ return sbus_reply_internal_error(message, intf_p->conn);
}
break;
}
@@ -407,7 +416,7 @@ DBusHandlerResult sbus_message_handler(DBusConnection *dbus_conn,
/* Reply DBUS_ERROR_UNKNOWN_METHOD */
DEBUG(1, ("No matching method found for %s.\n", method));
reply = dbus_message_new_error(message, DBUS_ERROR_UNKNOWN_METHOD, NULL);
- sbus_conn_send_reply(ctx->conn, reply);
+ sbus_conn_send_reply(intf_p->conn, reply);
dbus_message_unref(reply);
}
}
@@ -418,13 +427,13 @@ DBusHandlerResult sbus_message_handler(DBusConnection *dbus_conn,
if (strcmp(msg_interface, DBUS_INTROSPECT_INTERFACE) == 0 &&
strcmp(method, DBUS_INTROSPECT_METHOD) == 0)
{
- if (ctx->method_ctx->introspect_fn) {
+ if (intf_p->intf->introspect_fn) {
/* If we have been asked for introspection data and we have
* an introspection function registered, user that.
*/
- ret = ctx->method_ctx->introspect_fn(message, ctx->conn);
+ ret = intf_p->intf->introspect_fn(message, intf_p->conn);
if (ret != EOK) {
- return sbus_reply_internal_error(message, ctx->conn);
+ return sbus_reply_internal_error(message, intf_p->conn);
}
}
}
@@ -438,49 +447,35 @@ DBusHandlerResult sbus_message_handler(DBusConnection *dbus_conn,
/* Adds a new D-BUS path message handler to the connection
* Note: this must be a unique path.
*/
-int sbus_conn_add_method_ctx(struct sbus_connection *conn,
- struct sbus_method_ctx *method_ctx)
+int sbus_conn_add_interface(struct sbus_connection *conn,
+ struct sbus_interface *intf)
{
- struct method_holder *mh;
+ struct sbus_interface_p *intf_p;
dbus_bool_t dbret;
const char *path;
- if (!conn || !method_ctx || !method_ctx->message_handler) {
+ if (!conn || !intf || !intf->vtable.message_function) {
return EINVAL;
}
- path = method_ctx->path;
+ path = intf->path;
- if (path_in_method_list(conn->method_list, path)) {
+ if (path_in_interface_list(conn->intf_list, path)) {
DEBUG(0, ("Cannot add method context with identical path.\n"));
return EINVAL;
}
- mh = talloc_zero(conn, struct method_holder);
- if (!mh) {
- return ENOMEM;
- }
- mh->conn = conn;
-
- /* Set up the vtable for the object path */
- mh->dbus_vtable = talloc_zero(conn, DBusObjectPathVTable);
- if (!mh->dbus_vtable) {
- talloc_free(mh);
- return ENOMEM;
- }
- mh->dbus_vtable->message_function = method_ctx->message_handler;
-
- mh->method_ctx = talloc_reference(mh, method_ctx);
- if (!mh->method_ctx) {
- /* talloc_reference only fails on insufficient memory */
- talloc_free(mh);
+ intf_p = talloc_zero(conn, struct sbus_interface_p);
+ if (!intf_p) {
return ENOMEM;
}
+ intf_p->conn = conn;
+ intf_p->intf = intf;
- DLIST_ADD(conn->method_list, mh);
+ DLIST_ADD(conn->intf_list, intf_p);
dbret = dbus_connection_register_object_path(conn->dbus.conn,
- path, mh->dbus_vtable, mh);
+ path, &intf->vtable, intf_p);
if (!dbret) {
DEBUG(0, ("Could not register object path to the connection.\n"));
return ENOMEM;
@@ -489,9 +484,10 @@ int sbus_conn_add_method_ctx(struct sbus_connection *conn,
return EOK;
}
-static bool path_in_method_list(struct method_holder *list, const char *path)
+static bool path_in_interface_list(struct sbus_interface_p *list,
+ const char *path)
{
- struct method_holder *iter;
+ struct sbus_interface_p *iter;
if (!list || !path) {
return false;
@@ -499,7 +495,7 @@ static bool path_in_method_list(struct method_holder *list, const char *path)
iter = list;
while (iter != NULL) {
- if (strcmp(iter->method_ctx->path, path) == 0) {
+ if (strcmp(iter->intf->path, path) == 0) {
return true;
}
iter = iter->next;
@@ -510,16 +506,12 @@ static bool path_in_method_list(struct method_holder *list, const char *path)
static void sbus_unreg_object_paths(struct sbus_connection *conn)
{
- struct method_holder *iter = conn->method_list;
- struct method_holder *purge;
+ struct sbus_interface_p *iter = conn->intf_list;
while (iter != NULL) {
dbus_connection_unregister_object_path(conn->dbus.conn,
- iter->method_ctx->path);
- DLIST_REMOVE(conn->method_list, iter);
- purge = iter;
+ iter->intf->path);
iter = iter->next;
- talloc_free(purge);
}
}
@@ -538,7 +530,7 @@ static void sbus_reconnect(struct tevent_context *ev,
struct timeval tv, void *data)
{
struct sbus_connection *conn;
- struct method_holder *mh;
+ struct sbus_interface_p *iter;
DBusError dbus_error;
dbus_bool_t dbret;
int ret;
@@ -559,17 +551,18 @@ static void sbus_reconnect(struct tevent_context *ev,
}
/* Re-register object paths */
- mh = conn->method_list;
- while (mh) {
+ iter = conn->intf_list;
+ while (iter) {
dbret = dbus_connection_register_object_path(conn->dbus.conn,
- mh->method_ctx->path,
- mh->dbus_vtable, mh);
+ iter->intf->path,
+ &iter->intf->vtable,
+ iter);
if (!dbret) {
DEBUG(0, ("Could not register object path.\n"));
dbus_connection_unref(conn->dbus.conn);
goto failed;
}
- mh = mh->next;
+ iter = iter->next;
}
/* Reset retries to 0 to resume dispatch processing */
diff --git a/server/sbus/sssd_dbus_private.h b/server/sbus/sssd_dbus_private.h
index 01eaf00d..79cae230 100644
--- a/server/sbus/sssd_dbus_private.h
+++ b/server/sbus/sssd_dbus_private.h
@@ -10,6 +10,8 @@ enum dbus_conn_type {
SBUS_CONNECTION
};
+struct sbus_interface_p;
+
struct sbus_connection {
struct tevent_context *ev;
@@ -24,7 +26,7 @@ struct sbus_connection {
void *pvt_data; /* Private data for this connection */
/* dbus tables and handlers */
- struct method_holder *method_list;
+ struct sbus_interface_p *intf_list;
/* reconnect settings */
int retries;
@@ -34,7 +36,7 @@ struct sbus_connection {
void *reconnect_pvt;
/* server related stuff */
- struct sbus_method_ctx *server_method;
+ struct sbus_interface *server_intf;
sbus_server_conn_init_fn srv_init_fn;
void *srv_init_data;
};
diff --git a/server/sbus/sssd_dbus_server.c b/server/sbus/sssd_dbus_server.c
index 8a123d6d..ef2de106 100644
--- a/server/sbus/sssd_dbus_server.c
+++ b/server/sbus/sssd_dbus_server.c
@@ -49,8 +49,9 @@ static void sbus_server_init_new_connection(DBusServer *dbus_server,
}
DEBUG(5,("Adding connection %lX.\n", conn));
- ret = sbus_add_connection(server, server->ev, dbus_conn,
- &conn, SBUS_CONN_TYPE_PRIVATE);
+ ret = sbus_init_connection(server, server->ev,
+ dbus_conn, server->server_intf,
+ SBUS_CONN_TYPE_PRIVATE, &conn);
if (ret != 0) {
dbus_connection_close(dbus_conn);
DEBUG(5,("Closing connection (failed setup)"));
@@ -61,8 +62,6 @@ static void sbus_server_init_new_connection(DBusServer *dbus_server,
DEBUG(5,("Got a connection\n"));
- sbus_conn_add_method_ctx(conn, server->server_method);
-
/*
* Initialize connection-specific features
* This may set a more detailed destructor, but
@@ -87,7 +86,7 @@ static void sbus_server_init_new_connection(DBusServer *dbus_server,
int sbus_new_server(TALLOC_CTX *mem_ctx,
struct tevent_context *ev,
const char *address,
- struct sbus_method_ctx *method,
+ struct sbus_interface *intf,
struct sbus_connection **_server,
sbus_server_conn_init_fn init_fn, void *init_pvt_data)
{
@@ -121,7 +120,7 @@ int sbus_new_server(TALLOC_CTX *mem_ctx,
server->ev = ev;
server->type = SBUS_SERVER;
server->dbus.server = dbus_server;
- server->server_method = method;
+ server->server_intf = intf;
server->srv_init_fn = init_fn;
server->srv_init_data = init_pvt_data;