Don't call dbus_connection_dispatch recursively
[platform/upstream/at-spi2-core.git] / dbind / dbind.c
1 /*
2  * Copyright 2008-2011 Novell, Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #include <stdio.h>
21 #include <stdarg.h>
22 #include <sys/time.h>
23 #include <string.h>
24 #include <glib.h>
25
26 #include "config.h"
27 #include "dbind/dbind.h"
28 #include "atspi/atspi-gmain.h"
29
30 static int dbind_timeout = -1;
31
32 /*
33  * FIXME: compare types - to ensure they match &
34  *        do dynamic padding of structures etc.
35  */
36
37 /*---------------------------------------------------------------------------*/
38
39 typedef struct _SpiReentrantCallClosure 
40 {
41   DBusMessage *reply;
42 } SpiReentrantCallClosure;
43
44 static void
45 set_reply (DBusPendingCall * pending, void *user_data)
46 {
47   SpiReentrantCallClosure* closure = (SpiReentrantCallClosure *) user_data; 
48
49   closure->reply = dbus_pending_call_steal_reply (pending);
50   dbus_pending_call_unref (pending);
51 }
52
53 static gint
54 time_elapsed (struct timeval *origin)
55 {
56   struct timeval tv;
57
58   gettimeofday (&tv, NULL);
59   return (tv.tv_sec - origin->tv_sec) * 1000 + (tv.tv_usec - origin->tv_usec) / 1000;
60 }
61
62 DBusMessage *
63 dbind_send_and_allow_reentry (DBusConnection * bus, DBusMessage * message, DBusError *error)
64 {
65   DBusPendingCall *pending;
66   SpiReentrantCallClosure *closure;
67   const char *unique_name = dbus_bus_get_unique_name (bus);
68   const char *destination = dbus_message_get_destination (message);
69   struct timeval tv;
70   DBusMessage *ret;
71   static gboolean in_dispatch = FALSE;
72
73   if (unique_name && destination &&
74       strcmp (destination, unique_name) != 0)
75     {
76       ret = dbus_connection_send_with_reply_and_block (bus, message,
77                                                        dbind_timeout, error);
78       if (g_main_depth () == 0 && !in_dispatch)
79       {
80         in_dispatch = TRUE;
81         while (dbus_connection_dispatch (bus) == DBUS_DISPATCH_DATA_REMAINS);
82         in_dispatch = FALSE;
83       }
84       return ret;
85     }
86
87   closure = g_new0 (SpiReentrantCallClosure, 1);
88   closure->reply = NULL;
89   atspi_dbus_connection_setup_with_g_main(bus, NULL);
90   if (!dbus_connection_send_with_reply (bus, message, &pending, dbind_timeout)
91       || !pending)
92     {
93       g_free (closure);
94       return NULL;
95     }
96   dbus_pending_call_set_notify (pending, set_reply, (void *) closure, g_free);
97
98   closure->reply = NULL;
99   gettimeofday (&tv, NULL);
100   dbus_pending_call_ref (pending);
101   while (!closure->reply)
102     {
103       if (!dbus_connection_read_write_dispatch (bus, dbind_timeout))
104         {
105           //dbus_pending_call_set_notify (pending, NULL, NULL, NULL);
106           dbus_pending_call_cancel (pending);
107           dbus_pending_call_unref (pending);
108           return NULL;
109         }
110       if (time_elapsed (&tv) > dbind_timeout)
111         {
112           //dbus_pending_call_set_notify (pending, NULL, NULL, NULL);
113           dbus_pending_call_cancel (pending);
114           dbus_pending_call_unref (pending);
115           dbus_set_error_const (error, "org.freedesktop.DBus.Error.NoReply",
116                                 "timeout from dbind");
117           return NULL;
118         }
119     }
120   
121   ret = closure->reply;
122   dbus_pending_call_unref (pending);
123   return ret;
124 }
125
126 dbus_bool_t
127 dbind_method_call_reentrant_va (DBusConnection *cnx,
128                                 const char     *bus_name,
129                                 const char     *path,
130                                 const char     *interface,
131                                 const char     *method,
132                                 DBusError      *opt_error,
133                                 const char     *arg_types,
134                                 va_list         args)
135 {
136     dbus_bool_t success = FALSE;
137     DBusMessage *msg = NULL, *reply = NULL;
138     DBusMessageIter iter;
139     DBusError *err, real_err;
140     const char *p;
141   va_list args_demarshal;
142
143   dbus_error_init (&real_err);
144
145   va_copy (args_demarshal, args);
146     if (opt_error)
147         err = opt_error;
148     else {
149         err = &real_err;
150     }
151
152     msg = dbus_message_new_method_call (bus_name, path, interface, method);
153     if (!msg)
154         goto out;
155
156     p = arg_types;
157     dbus_message_iter_init_append (msg, &iter);
158     dbind_any_marshal_va (&iter, &p, args);
159
160     reply = dbind_send_and_allow_reentry (cnx, msg, err);
161     if (!reply)
162         goto out;
163
164     if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR)
165     {
166       const char *name = dbus_message_get_error_name (reply);
167       goto out;
168     }
169     /* demarshal */
170     if (p[0] == '=' && p[1] == '>')
171     {
172         DBusMessageIter iter;
173         dbus_message_iter_init (reply, &iter);
174         if (strcmp (p + 2, dbus_message_get_signature (reply)) != 0)
175         {
176             g_warning ("dbind: Call to \"%s\" returned signature %s; expected %s",
177                        method, dbus_message_get_signature (reply), p + 2);
178             if (opt_error)
179                 dbus_set_error (opt_error, DBUS_ERROR_INVALID_ARGS,
180                                 "Call to \"%s\" returned signature %s; expected %s",
181                                 method, dbus_message_get_signature (reply),
182                                 p + 2);
183             goto out;
184         }
185         p = arg_types;
186         dbind_any_demarshal_va (&iter, &p, args_demarshal);
187     }
188
189     success = TRUE;
190 out:
191     if (msg)
192         dbus_message_unref (msg);
193
194     if (reply)
195         dbus_message_unref (reply);
196
197     if (dbus_error_is_set (&real_err))
198         dbus_error_free (&real_err);
199
200     va_end (args_demarshal);
201     return success;
202 }
203
204 /**
205  * dbind_method_call_reentrant:
206  *
207  * @cnx:       A D-Bus Connection used to make the method call.
208  * @bus_name:  The D-Bus bus name of the program where the method call should
209  *             be made.
210  * @path:      The D-Bus object path that should handle the method.
211  * @interface: The D-Bus interface used to scope the method name.
212  * @method:    Method to be invoked.
213  * @opt_error: D-Bus error.
214  * @arg_types: Variable length arguments interleaving D-Bus argument types
215  *             and pointers to argument data.
216  *
217  * Makes a D-Bus method call using the supplied location data, method name and
218  * argument data.This function is re-entrant. It continuously reads from the D-Bus
219  * bus and dispatches messages until a reply has been recieved.
220  **/
221 dbus_bool_t
222 dbind_method_call_reentrant (DBusConnection *cnx,
223                              const char     *bus_name,
224                              const char     *path,
225                              const char     *interface,
226                              const char     *method,
227                              DBusError      *opt_error,
228                              const char     *arg_types,
229                              ...)
230 {
231     dbus_bool_t success = FALSE;
232     va_list args;
233
234     va_start (args, arg_types);
235     success = dbind_method_call_reentrant_va (cnx,
236                                               bus_name,
237                                               path,
238                                               interface,
239                                               method,
240                                               opt_error,
241                                               arg_types,
242                                               args);
243     va_end (args);
244
245     return success;
246 }
247
248 /*---------------------------------------------------------------------------*/
249
250 dbus_bool_t
251 dbind_emit_signal_va (DBusConnection *cnx,
252                       const char     *path,
253                       const char     *interface,
254                       const char     *signal,
255                       DBusError      *opt_error,
256                       const char     *arg_types,
257                       va_list         args)
258 {
259     dbus_bool_t success = FALSE;
260     DBusMessage *msg = NULL;
261     DBusMessageIter iter;
262     DBusError *err, real_err;
263     const char *p;
264
265     dbus_error_init (&real_err);
266
267     if (opt_error)
268         err = opt_error;
269     else {
270         err = &real_err;
271     }
272
273     msg = dbus_message_new_signal (path, interface, signal);
274     if (!msg)
275         goto out;
276
277     p = arg_types;
278     dbus_message_iter_init_append (msg, &iter);
279     dbind_any_marshal_va (&iter, &p, args);
280
281     if (!dbus_connection_send (cnx, msg, NULL))
282        goto out;
283
284     success = TRUE;
285 out:
286
287     if (msg)
288         dbus_message_unref (msg);
289
290     if (dbus_error_is_set (&real_err))
291         dbus_error_free (&real_err);
292
293     return success;
294 }
295
296 /**
297  * dbind_emit_signal:
298  *
299  * @cnx:       A D-Bus Connection used to make the method call.
300  * @path:      The D-Bus object path that this signal is emitted from.
301  * @interface: The D-Bus interface used to scope the method name.
302  * @signal:    Name of signal to emit.
303  * @opt_error: D-Bus error.
304  * @arg_types: Variable length arguments interleaving D-Bus argument types
305  *             and pointers to argument data.
306  *
307  * Emits a D-Bus signal  using the supplied signal name and argument data.
308  **/
309 dbus_bool_t
310 dbind_emit_signal (DBusConnection *cnx,
311                    const char     *path,
312                    const char     *interface,
313                    const char     *signal,
314                    DBusError      *opt_error,
315                    const char     *arg_types,
316                    ...)
317 {
318     dbus_bool_t success = FALSE;
319     va_list args;
320
321     va_start (args, arg_types);
322     success = dbind_emit_signal_va (cnx, path, interface, signal, opt_error, arg_types, args);
323     va_end (args);
324
325     return success;
326 }
327 void
328 dbind_set_timeout (int timeout)
329 {
330   dbind_timeout = timeout;
331 }
332
333
334 /*END------------------------------------------------------------------------*/