]> code.delx.au - pulseaudio/blob - src/daemon/server-lookup.c
dbus-protocol: Connection handling for local connections.
[pulseaudio] / src / daemon / server-lookup.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2009 Tanu Kaskinen
5
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2.1 of the License,
9 or (at your option) any later version.
10
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <dbus/dbus.h>
27
28 #include <pulse/client-conf.h>
29 #include <pulse/xmalloc.h>
30
31 #include <pulsecore/core.h>
32 #include <pulsecore/core-util.h>
33 #include <pulsecore/dbus-common.h>
34 #include <pulsecore/dbus-shared.h>
35 #include <pulsecore/macro.h>
36
37 #include "server-lookup.h"
38
39 struct pa_dbusobj_server_lookup {
40 pa_core *core;
41 pa_dbus_connection *conn;
42 pa_bool_t path_registered;
43 };
44
45 static const char introspection[] =
46 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE
47 "<node>"
48 " <!-- If you are looking for documentation make sure to check out\n"
49 " http://pulseaudio.org/wiki/DBusInterface -->\n"
50 " <interface name=\"org.pulseaudio.ServerLookup\">"
51 " <method name=\"GetDBusAddress\">"
52 " <arg name=\"result\" type=\"s\" direction=\"out\"/>"
53 " </method>"
54 " </interface>"
55 " <interface name=\"org.freedesktop.DBus.Introspectable\">"
56 " <method name=\"Introspect\">"
57 " <arg name=\"data\" type=\"s\" direction=\"out\"/>"
58 " </method>"
59 " </interface>"
60 "</node>";
61
62 static void unregister_cb(DBusConnection *conn, void *user_data) {
63 pa_dbusobj_server_lookup *sl = user_data;
64
65 pa_assert(sl);
66 pa_assert(sl->path_registered);
67
68 sl->path_registered = FALSE;
69 }
70
71 static DBusHandlerResult handle_introspect(DBusConnection *conn, DBusMessage *msg, pa_dbusobj_server_lookup *sl) {
72 const char *i = introspection;
73 DBusMessage *reply = NULL;
74
75 pa_assert(conn);
76 pa_assert(msg);
77
78 if (!(reply = dbus_message_new_method_return(msg)))
79 goto fail;
80
81 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &i, DBUS_TYPE_INVALID))
82 goto fail;
83
84 if (!dbus_connection_send(conn, reply, NULL))
85 goto oom;
86
87 dbus_message_unref(reply);
88
89 return DBUS_HANDLER_RESULT_HANDLED;
90
91 fail:
92 if (reply)
93 dbus_message_unref(reply);
94
95 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
96
97 oom:
98 if (reply)
99 dbus_message_unref(reply);
100
101 return DBUS_HANDLER_RESULT_NEED_MEMORY;
102 }
103
104 static DBusHandlerResult handle_get_dbus_address(DBusConnection *conn, DBusMessage *msg, pa_dbusobj_server_lookup *sl) {
105 DBusMessage *reply = NULL;
106 pa_client_conf *conf = NULL;
107 char *address = NULL;
108
109 pa_assert(conn);
110 pa_assert(msg);
111 pa_assert(sl);
112
113 conf = pa_client_conf_new();
114
115 if (pa_client_conf_load(conf, NULL) < 0) {
116 if (!(reply = dbus_message_new_error(msg, "org.pulseaudio.ClientConfLoadError", "Failed to load client.conf.")))
117 goto fail;
118 if (!dbus_connection_send(conn, reply, NULL))
119 goto oom;
120 return DBUS_HANDLER_RESULT_HANDLED;
121 }
122
123 pa_client_conf_free(conf);
124
125 if (conf->default_dbus_server) {
126 if (!(address = dbus_address_escape_value(conf->default_dbus_server)))
127 goto oom;
128 } else {
129 if (!(address = pa_get_dbus_address_from_server_type(sl->core->server_type))) {
130 if (!(reply = dbus_message_new_error(msg, DBUS_ERROR_FAILED, "get_dbus_server_from_type() failed.")))
131 goto fail;
132 if (!dbus_connection_send(conn, reply, NULL))
133 goto oom;
134 return DBUS_HANDLER_RESULT_HANDLED;
135 }
136 }
137
138 if (!(reply = dbus_message_new_method_return(msg)))
139 goto oom;
140
141 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &address, DBUS_TYPE_INVALID))
142 goto fail;
143
144 if (!dbus_connection_send(conn, reply, NULL))
145 goto oom;
146
147 pa_log_debug("handle_get_dbus_address(): Sent reply with address '%s'.", address);
148
149 pa_xfree(address);
150
151 dbus_message_unref(reply);
152
153 return DBUS_HANDLER_RESULT_HANDLED;
154
155 fail:
156 if (conf)
157 pa_client_conf_free(conf);
158
159 pa_xfree(address);
160
161 if (reply)
162 dbus_message_unref(reply);
163
164 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
165
166 oom:
167 if (conf)
168 pa_client_conf_free(conf);
169
170 pa_xfree(address);
171
172 if (reply)
173 dbus_message_unref(reply);
174
175 return DBUS_HANDLER_RESULT_NEED_MEMORY;
176 }
177
178 static DBusHandlerResult message_cb(DBusConnection *conn, DBusMessage *msg, void *user_data) {
179 pa_dbusobj_server_lookup *sl = user_data;
180
181 pa_assert(conn);
182 pa_assert(msg);
183 pa_assert(sl);
184
185 /* pa_log("Got message! type = %s path = %s iface = %s member = %s dest = %s", dbus_message_type_to_string(dbus_message_get_type(msg)), dbus_message_get_path(msg), dbus_message_get_interface(msg), dbus_message_get_member(msg), dbus_message_get_destination(msg)); */
186
187 if (dbus_message_is_method_call(msg, "org.freedesktop.DBus.Introspectable", "Introspect"))
188 return handle_introspect(conn, msg, sl);
189
190 if (dbus_message_is_method_call(msg, "org.pulseaudio.ServerLookup", "GetDBusAddress"))
191 return handle_get_dbus_address(conn, msg, sl);
192
193 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
194 }
195
196 static DBusObjectPathVTable vtable = {
197 .unregister_function = unregister_cb,
198 .message_function = message_cb,
199 .dbus_internal_pad1 = NULL,
200 .dbus_internal_pad2 = NULL,
201 .dbus_internal_pad3 = NULL,
202 .dbus_internal_pad4 = NULL
203 };
204
205 pa_dbusobj_server_lookup *pa_dbusobj_server_lookup_new(pa_core *c) {
206 pa_dbusobj_server_lookup *sl;
207 DBusError error;
208
209 dbus_error_init(&error);
210
211 sl = pa_xnew(pa_dbusobj_server_lookup, 1);
212 sl->core = c;
213 sl->path_registered = FALSE;
214
215 if (!(sl->conn = pa_dbus_bus_get(c, DBUS_BUS_SESSION, &error)) || dbus_error_is_set(&error)) {
216 pa_log("Unable to contact D-Bus: %s: %s", error.name, error.message);
217 goto fail;
218 }
219
220 if (!dbus_connection_register_object_path(pa_dbus_connection_get(sl->conn), "/org/pulseaudio/server_lookup", &vtable, sl)) {
221 pa_log("dbus_connection_register_object_path() failed for /org/pulseaudio/server_lookup.");
222 goto fail;
223 }
224
225 sl->path_registered = TRUE;
226
227 return sl;
228
229 fail:
230 dbus_error_free(&error);
231
232 pa_dbusobj_server_lookup_free(sl);
233
234 return NULL;
235 }
236
237 void pa_dbusobj_server_lookup_free(pa_dbusobj_server_lookup *sl) {
238 pa_assert(sl);
239
240 if (sl->path_registered) {
241 pa_assert(sl->conn);
242 if (!dbus_connection_unregister_object_path(pa_dbus_connection_get(sl->conn), "/org/pulseaudio/server_lookup"))
243 pa_log_debug("dbus_connection_unregister_object_path() failed for /org/pulseaudio/server_lookup.");
244 }
245
246 if (sl->conn)
247 pa_dbus_connection_unref(sl->conn);
248
249 pa_xfree(sl);
250 }