D-Bus  1.6.12
dbus-pending-call.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-pending-call.c Object representing a call in progress.
3  *
4  * Copyright (C) 2002, 2003 Red Hat Inc.
5  *
6  * Licensed under the Academic Free License version 2.1
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  *
22  */
23 
24 #include <config.h>
25 #include "dbus-internals.h"
26 #include "dbus-connection-internal.h"
27 #include "dbus-message-internal.h"
28 #include "dbus-pending-call-internal.h"
29 #include "dbus-pending-call.h"
30 #include "dbus-list.h"
31 #include "dbus-threads.h"
32 #include "dbus-test.h"
33 
53 #define CONNECTION_LOCK(connection) _dbus_connection_lock(connection)
54 
57 #define CONNECTION_UNLOCK(connection) _dbus_connection_unlock(connection)
58 
63 {
78  unsigned int completed : 1;
79  unsigned int timeout_added : 1;
80 };
81 
82 #ifdef DBUS_ENABLE_VERBOSE_MODE
83 static void
84 _dbus_pending_call_trace_ref (DBusPendingCall *pending_call,
85  int old_refcount,
86  int new_refcount,
87  const char *why)
88 {
89  static int enabled = -1;
90 
91  _dbus_trace_ref ("DBusPendingCall", pending_call, old_refcount,
92  new_refcount, why, "DBUS_PENDING_CALL_TRACE", &enabled);
93 }
94 #else
95 #define _dbus_pending_call_trace_ref(p, o, n, w) \
96  do \
97  {\
98  (void) (o); \
99  (void) (n); \
100  } while (0)
101 #endif
102 
103 static dbus_int32_t notify_user_data_slot = -1;
104 
117  int timeout_milliseconds,
118  DBusTimeoutHandler timeout_handler)
119 {
120  DBusPendingCall *pending;
121  DBusTimeout *timeout;
122 
123  _dbus_assert (timeout_milliseconds >= 0 || timeout_milliseconds == -1);
124 
125  if (timeout_milliseconds == -1)
126  timeout_milliseconds = _DBUS_DEFAULT_TIMEOUT_VALUE;
127 
128  if (!dbus_pending_call_allocate_data_slot (&notify_user_data_slot))
129  return NULL;
130 
131  pending = dbus_new0 (DBusPendingCall, 1);
132 
133  if (pending == NULL)
134  {
135  dbus_pending_call_free_data_slot (&notify_user_data_slot);
136  return NULL;
137  }
138 
139  if (timeout_milliseconds != DBUS_TIMEOUT_INFINITE)
140  {
141  timeout = _dbus_timeout_new (timeout_milliseconds,
142  timeout_handler,
143  pending, NULL);
144 
145  if (timeout == NULL)
146  {
147  dbus_pending_call_free_data_slot (&notify_user_data_slot);
148  dbus_free (pending);
149  return NULL;
150  }
151 
152  pending->timeout = timeout;
153  }
154  else
155  {
156  pending->timeout = NULL;
157  }
158 
159  _dbus_atomic_inc (&pending->refcount);
160  pending->connection = connection;
162 
164 
165  _dbus_pending_call_trace_ref (pending, 0, 1, "new_unlocked");
166 
167  return pending;
168 }
169 
178 void
180  DBusMessage *message)
181 {
182  if (message == NULL)
183  {
184  message = pending->timeout_link->data;
185  _dbus_list_clear (&pending->timeout_link);
186  }
187  else
188  dbus_message_ref (message);
189 
190  _dbus_verbose (" handing message %p (%s) to pending call serial %u\n",
191  message,
193  "method return" :
195  "error" : "other type",
196  pending->reply_serial);
197 
198  _dbus_assert (pending->reply == NULL);
200  pending->reply = message;
201 }
202 
210 void
212 {
213  _dbus_assert (!pending->completed);
214 
215  pending->completed = TRUE;
216 
217  if (pending->function)
218  {
219  void *user_data;
220  user_data = dbus_pending_call_get_data (pending,
221  notify_user_data_slot);
222 
223  (* pending->function) (pending, user_data);
224  }
225 }
226 
234 void
236  DBusConnection *connection)
237 {
238  _dbus_assert (connection == pending->connection);
239 
240  if (pending->timeout_link)
241  {
243  pending->timeout_link);
244  pending->timeout_link = NULL;
245  }
246 }
247 
256 {
257  _dbus_assert (pending != NULL);
258 
259  return pending->timeout_added;
260 }
261 
262 
269 void
271  dbus_bool_t is_added)
272 {
273  _dbus_assert (pending != NULL);
274 
275  pending->timeout_added = is_added;
276 }
277 
278 
285 DBusTimeout *
287 {
288  _dbus_assert (pending != NULL);
289 
290  return pending->timeout;
291 }
292 
301 {
302  _dbus_assert (pending != NULL);
303 
304  return pending->reply_serial;
305 }
306 
313 void
315  dbus_uint32_t serial)
316 {
317  _dbus_assert (pending != NULL);
318  _dbus_assert (pending->reply_serial == 0);
319 
320  pending->reply_serial = serial;
321 }
322 
331 {
332  _dbus_assert (pending != NULL);
333 
334  CONNECTION_LOCK (pending->connection);
335  return pending->connection;
336 }
337 
346 {
347  _dbus_assert (pending != NULL);
348 
349  return pending->connection;
350 }
351 
362  DBusMessage *message,
363  dbus_uint32_t serial)
364 {
365  DBusList *reply_link;
366  DBusMessage *reply;
367 
369  "Did not receive a reply. Possible causes include: "
370  "the remote application did not send a reply, "
371  "the message bus security policy blocked the reply, "
372  "the reply timeout expired, or "
373  "the network connection was broken.");
374  if (reply == NULL)
375  return FALSE;
376 
377  reply_link = _dbus_list_alloc_link (reply);
378  if (reply_link == NULL)
379  {
380  /* it's OK to unref this, nothing that could have attached a callback
381  * has ever seen it */
382  dbus_message_unref (reply);
383  return FALSE;
384  }
385 
386  pending->timeout_link = reply_link;
387 
389 
390  return TRUE;
391 }
392 
402 {
403  dbus_int32_t old_refcount;
404 
405  old_refcount = _dbus_atomic_inc (&pending->refcount);
406  _dbus_pending_call_trace_ref (pending, old_refcount, old_refcount + 1,
407  "ref_unlocked");
408 
409  return pending;
410 }
411 
412 
413 static void
414 _dbus_pending_call_last_unref (DBusPendingCall *pending)
415 {
416  DBusConnection *connection;
417 
418  /* If we get here, we should be already detached
419  * from the connection, or never attached.
420  */
421  _dbus_assert (!pending->timeout_added);
422 
423  connection = pending->connection;
424 
425  /* this assumes we aren't holding connection lock... */
427 
428  if (pending->timeout != NULL)
429  _dbus_timeout_unref (pending->timeout);
430 
431  if (pending->timeout_link)
432  {
435  pending->timeout_link = NULL;
436  }
437 
438  if (pending->reply)
439  {
440  dbus_message_unref (pending->reply);
441  pending->reply = NULL;
442  }
443 
444  dbus_free (pending);
445 
446  dbus_pending_call_free_data_slot (&notify_user_data_slot);
447 
448  /* connection lock should not be held. */
449  /* Free the connection last to avoid a weird state while
450  * calling out to application code where the pending exists
451  * but not the connection.
452  */
453  dbus_connection_unref (connection);
454 }
455 
463 void
465 {
466  dbus_int32_t old_refcount;
467 
468  old_refcount = _dbus_atomic_dec (&pending->refcount);
469  _dbus_assert (old_refcount > 0);
470  _dbus_pending_call_trace_ref (pending, old_refcount,
471  old_refcount - 1, "unref_and_unlock");
472 
473  CONNECTION_UNLOCK (pending->connection);
474 
475  if (old_refcount == 1)
476  _dbus_pending_call_last_unref (pending);
477 }
478 
488 {
489  return pending->completed;
490 }
491 
492 static DBusDataSlotAllocator slot_allocator;
493 _DBUS_DEFINE_GLOBAL_LOCK (pending_call_slots);
494 
510  dbus_int32_t slot,
511  void *data,
512  DBusFreeFunction free_data_func)
513 {
514  DBusFreeFunction old_free_func;
515  void *old_data;
516  dbus_bool_t retval;
517 
518  retval = _dbus_data_slot_list_set (&slot_allocator,
519  &pending->slot_list,
520  slot, data, free_data_func,
521  &old_free_func, &old_data);
522 
523  /* Drop locks to call out to app code */
524  CONNECTION_UNLOCK (pending->connection);
525 
526  if (retval)
527  {
528  if (old_free_func)
529  (* old_free_func) (old_data);
530  }
531 
532  CONNECTION_LOCK (pending->connection);
533 
534  return retval;
535 }
536 
585 {
586  dbus_int32_t old_refcount;
587 
588  _dbus_return_val_if_fail (pending != NULL, NULL);
589 
590  old_refcount = _dbus_atomic_inc (&pending->refcount);
591  _dbus_pending_call_trace_ref (pending, old_refcount, old_refcount + 1,
592  "ref");
593 
594  return pending;
595 }
596 
603 void
605 {
606  dbus_int32_t old_refcount;
607 
608  _dbus_return_if_fail (pending != NULL);
609 
610  old_refcount = _dbus_atomic_dec (&pending->refcount);
611  _dbus_pending_call_trace_ref (pending, old_refcount, old_refcount - 1,
612  "unref");
613 
614  if (old_refcount == 1)
615  _dbus_pending_call_last_unref(pending);
616 }
617 
631  void *user_data,
632  DBusFreeFunction free_user_data)
633 {
634  dbus_bool_t ret = FALSE;
635 
636  _dbus_return_val_if_fail (pending != NULL, FALSE);
637 
638  CONNECTION_LOCK (pending->connection);
639 
640  /* could invoke application code! */
641  if (!_dbus_pending_call_set_data_unlocked (pending, notify_user_data_slot,
642  user_data, free_user_data))
643  goto out;
644 
645  pending->function = function;
646  ret = TRUE;
647 
648 out:
649  CONNECTION_UNLOCK (pending->connection);
650 
651  return ret;
652 }
653 
669 void
671 {
672  _dbus_return_if_fail (pending != NULL);
673 
675  pending);
676 }
677 
687 {
688  dbus_bool_t completed;
689 
690  _dbus_return_val_if_fail (pending != NULL, FALSE);
691 
692  CONNECTION_LOCK (pending->connection);
693  completed = pending->completed;
694  CONNECTION_UNLOCK (pending->connection);
695 
696  return completed;
697 }
698 
710 {
711  DBusMessage *message;
712 
713  _dbus_return_val_if_fail (pending != NULL, NULL);
714  _dbus_return_val_if_fail (pending->completed, NULL);
715  _dbus_return_val_if_fail (pending->reply != NULL, NULL);
716 
717  CONNECTION_LOCK (pending->connection);
718 
719  message = pending->reply;
720  pending->reply = NULL;
721 
722  CONNECTION_UNLOCK (pending->connection);
723 
724  _dbus_message_trace_ref (message, -1, -1, "dbus_pending_call_steal_reply");
725  return message;
726 }
727 
743 void
745 {
746  _dbus_return_if_fail (pending != NULL);
747 
749 }
750 
767 {
768  _dbus_return_val_if_fail (slot_p != NULL, FALSE);
769 
770  return _dbus_data_slot_allocator_alloc (&slot_allocator,
771  &_DBUS_LOCK_NAME (pending_call_slots),
772  slot_p);
773 }
774 
786 void
788 {
789  _dbus_return_if_fail (slot_p != NULL);
790  _dbus_return_if_fail (*slot_p >= 0);
791 
792  _dbus_data_slot_allocator_free (&slot_allocator, slot_p);
793 }
794 
810  dbus_int32_t slot,
811  void *data,
812  DBusFreeFunction free_data_func)
813 {
814  dbus_bool_t retval;
815 
816  _dbus_return_val_if_fail (pending != NULL, FALSE);
817  _dbus_return_val_if_fail (slot >= 0, FALSE);
818 
819 
820  CONNECTION_LOCK (pending->connection);
821  retval = _dbus_pending_call_set_data_unlocked (pending, slot, data, free_data_func);
822  CONNECTION_UNLOCK (pending->connection);
823  return retval;
824 }
825 
834 void*
836  dbus_int32_t slot)
837 {
838  void *res;
839 
840  _dbus_return_val_if_fail (pending != NULL, NULL);
841 
842  CONNECTION_LOCK (pending->connection);
843  res = _dbus_data_slot_list_get (&slot_allocator,
844  &pending->slot_list,
845  slot);
846  CONNECTION_UNLOCK (pending->connection);
847 
848  return res;
849 }
850 
unsigned int dbus_uint32_t
A 32-bit unsigned integer on all platforms.
An atomic integer safe to increment or decrement from multiple threads.
Definition: dbus-sysdeps.h:229
void _dbus_pending_call_complete(DBusPendingCall *pending)
Calls notifier function for the pending call and sets the call to completed.
DBusMessage * dbus_message_ref(DBusMessage *message)
Increments the reference count of a DBusMessage.
Internals of DBusTimeout.
Definition: dbus-timeout.c:40
DBusTimeout * _dbus_timeout_new(int interval, DBusTimeoutHandler handler, void *data, DBusFreeFunction free_data_function)
Creates a new DBusTimeout, enabled by default.
Definition: dbus-timeout.c:63
#define NULL
A null pointer, defined appropriately for C or C++.
void(* DBusFreeFunction)(void *memory)
The type of a function which frees a block of memory.
Definition: dbus-memory.h:64
void _dbus_connection_queue_synthesized_message_link(DBusConnection *connection, DBusList *link)
Adds a link + message to the incoming message queue.
DBusConnection * _dbus_pending_call_get_connection_unlocked(DBusPendingCall *pending)
Gets the connection associated with this pending call.
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
Definition: dbus-memory.c:700
DBusMessage * dbus_pending_call_steal_reply(DBusPendingCall *pending)
Gets the reply, or returns NULL if none has been received yet.
dbus_bool_t _dbus_pending_call_set_data_unlocked(DBusPendingCall *pending, dbus_int32_t slot, void *data, DBusFreeFunction free_data_func)
Stores a pointer on a DBusPendingCall, along with an optional function to be used for freeing the dat...
DBusDataSlotList slot_list
Data stored by allocated integer ID.
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
void * data
Data stored at this element.
Definition: dbus-list.h:38
Implementation details of DBusPendingCall - all fields are private.
Implementation details of DBusConnection.
#define DBUS_MESSAGE_TYPE_ERROR
Message type of an error reply message, see dbus_message_get_type()
DBusList * _dbus_list_alloc_link(void *data)
Allocates a linked list node.
Definition: dbus-list.c:231
#define DBUS_MESSAGE_TYPE_METHOD_RETURN
Message type of a method return message, see dbus_message_get_type()
dbus_bool_t _dbus_data_slot_allocator_alloc(DBusDataSlotAllocator *allocator, DBusRMutex **mutex_loc, dbus_int32_t *slot_id_p)
Allocates an integer ID to be used for storing data in a DBusDataSlotList.
Definition: dbus-dataslot.c:69
void _dbus_timeout_unref(DBusTimeout *timeout)
Decrements the reference count of a DBusTimeout object and finalizes the object if the count reaches ...
Definition: dbus-timeout.c:107
dbus_bool_t dbus_pending_call_set_notify(DBusPendingCall *pending, DBusPendingCallNotifyFunction function, void *user_data, DBusFreeFunction free_user_data)
Sets a notification function to be called when the reply is received or the pending call times out...
dbus_bool_t dbus_pending_call_set_data(DBusPendingCall *pending, dbus_int32_t slot, void *data, DBusFreeFunction free_data_func)
Stores a pointer on a DBusPendingCall, along with an optional function to be used for freeing the dat...
dbus_bool_t _dbus_pending_call_get_completed_unlocked(DBusPendingCall *pending)
Checks whether the pending call has received a reply yet, or not.
void _dbus_connection_block_pending_call(DBusPendingCall *pending)
Blocks until a pending call times out or gets a reply.
void * dbus_pending_call_get_data(DBusPendingCall *pending, dbus_int32_t slot)
Retrieves data previously set with dbus_pending_call_set_data().
dbus_int32_t _dbus_atomic_dec(DBusAtomic *atomic)
Atomically decrement an integer.
void dbus_pending_call_free_data_slot(dbus_int32_t *slot_p)
Deallocates a global ID for DBusPendingCall data slots.
Internals of DBusMessage.
unsigned int timeout_added
Have added the timeout.
DBusConnection * connection
Connections we&#39;re associated with.
dbus_bool_t _dbus_pending_call_set_timeout_error_unlocked(DBusPendingCall *pending, DBusMessage *message, dbus_uint32_t serial)
Sets the reply message associated with the pending call to a timeout error.
dbus_bool_t dbus_pending_call_get_completed(DBusPendingCall *pending)
Checks whether the pending call has received a reply yet, or not.
#define dbus_new0(type, count)
Safe macro for using dbus_malloc0().
Definition: dbus-memory.h:59
DBusList * timeout_link
Preallocated timeout response.
dbus_bool_t _dbus_pending_call_is_timeout_added_unlocked(DBusPendingCall *pending)
Checks to see if a timeout has been added.
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition: dbus-types.h:35
void(* DBusPendingCallNotifyFunction)(DBusPendingCall *pending, void *user_data)
Called when a pending call now has a reply available.
DBusTimeout * timeout
Timeout.
void dbus_pending_call_cancel(DBusPendingCall *pending)
Cancels the pending call, such that any reply or error received will just be ignored.
dbus_bool_t _dbus_data_slot_list_set(DBusDataSlotAllocator *allocator, DBusDataSlotList *list, int slot, void *data, DBusFreeFunction free_data_func, DBusFreeFunction *old_free_func, void **old_data)
Stores a pointer in the data slot list, along with an optional function to be used for freeing the da...
#define DBUS_ERROR_NO_REPLY
No reply to a message expecting one, usually means a timeout occurred.
dbus_bool_t dbus_pending_call_allocate_data_slot(dbus_int32_t *slot_p)
Allocates an integer ID to be used for storing application-specific data on any DBusPendingCall.
DBusPendingCallNotifyFunction function
Notifier when reply arrives.
dbus_uint32_t dbus_message_get_reply_serial(DBusMessage *message)
Returns the serial that the message is a reply to or 0 if none.
dbus_int32_t _dbus_atomic_inc(DBusAtomic *atomic)
Atomically increments an integer.
DBusPendingCall * dbus_pending_call_ref(DBusPendingCall *pending)
Increments the reference count on a pending call.
dbus_uint32_t _dbus_pending_call_get_reply_serial_unlocked(DBusPendingCall *pending)
Gets the reply&#39;s serial number.
void * _dbus_data_slot_list_get(DBusDataSlotAllocator *allocator, DBusDataSlotList *list, int slot)
Retrieves data previously set with _dbus_data_slot_list_set_data().
#define CONNECTION_UNLOCK(connection)
shorter and more visible way to write _dbus_connection_unlock()
#define TRUE
Expands to &quot;1&quot;.
void _dbus_data_slot_list_init(DBusDataSlotList *list)
Initializes a slot list.
Data structure that stores the actual user data set at a given slot.
Definition: dbus-dataslot.h:67
void _dbus_list_free_link(DBusList *link)
Frees a linked list node allocated with _dbus_list_alloc_link.
Definition: dbus-list.c:243
void dbus_pending_call_unref(DBusPendingCall *pending)
Decrements the reference count on a pending call, freeing it if the count reaches 0...
#define _DBUS_DEFINE_GLOBAL_LOCK(name)
Defines a global lock variable with the given name.
An allocator that tracks a set of slot IDs.
Definition: dbus-dataslot.h:55
#define DBUS_TIMEOUT_INFINITE
An integer constant representing an infinite timeout.
void _dbus_pending_call_queue_timeout_error_unlocked(DBusPendingCall *pending, DBusConnection *connection)
If the pending call hasn&#39;t been timed out, add its timeout error reply to the connection&#39;s incoming m...
void dbus_pending_call_block(DBusPendingCall *pending)
Block until the pending call is completed.
DBusPendingCall * _dbus_pending_call_new_unlocked(DBusConnection *connection, int timeout_milliseconds, DBusTimeoutHandler timeout_handler)
Creates a new pending reply object.
void _dbus_pending_call_set_reply_unlocked(DBusPendingCall *pending, DBusMessage *message)
Sets the reply of a pending call with the given message, or if the message is NULL, by timing out the pending call.
A node in a linked list.
Definition: dbus-list.h:34
dbus_bool_t(* DBusTimeoutHandler)(void *data)
function to run when the timeout is handled
Definition: dbus-timeout.h:41
unsigned int completed
TRUE if completed.
void _dbus_connection_remove_pending_call(DBusConnection *connection, DBusPendingCall *pending)
Removes a pending call from the connection, such that the pending reply will be ignored.
dbus_uint32_t reply_serial
Expected serial of reply.
#define FALSE
Expands to &quot;0&quot;.
int dbus_message_get_type(DBusMessage *message)
Gets the type of a message.
#define _DBUS_LOCK_NAME(name)
Expands to name of a global lock variable.
void dbus_connection_unref(DBusConnection *connection)
Decrements the reference count of a DBusConnection, and finalizes it if the count reaches zero...
DBusPendingCall * _dbus_pending_call_ref_unlocked(DBusPendingCall *pending)
Increments the reference count on a pending call, while the lock on its connection is already held...
DBusConnection * _dbus_connection_ref_unlocked(DBusConnection *connection)
Increments the reference count of a DBusConnection.
int dbus_int32_t
A 32-bit signed integer on all platforms.
#define CONNECTION_LOCK(connection)
Internals of DBusPendingCall.
DBusTimeout * _dbus_pending_call_get_timeout_unlocked(DBusPendingCall *pending)
Retrives the timeout.
void _dbus_data_slot_allocator_free(DBusDataSlotAllocator *allocator, dbus_int32_t *slot_id_p)
Deallocates an ID previously allocated with _dbus_data_slot_allocator_alloc().
DBusAtomic refcount
reference count
void dbus_message_unref(DBusMessage *message)
Decrements the reference count of a DBusMessage, freeing the message if the count reaches 0...
void _dbus_pending_call_set_reply_serial_unlocked(DBusPendingCall *pending, dbus_uint32_t serial)
Sets the reply&#39;s serial number.
void _dbus_pending_call_unref_and_unlock(DBusPendingCall *pending)
Decrements the reference count on a pending call, freeing it if the count reaches 0...
void _dbus_pending_call_set_timeout_added_unlocked(DBusPendingCall *pending, dbus_bool_t is_added)
Sets wether the timeout has been added.
DBusConnection * _dbus_pending_call_get_connection_and_lock(DBusPendingCall *pending)
Gets the connection associated with this pending call.
void _dbus_data_slot_list_free(DBusDataSlotList *list)
Frees the data slot list and all data slots contained in it, calling application-provided free functi...
void _dbus_list_clear(DBusList **list)
Frees all links in the list and sets the list head to NULL.
Definition: dbus-list.c:531
DBusMessage * reply
Reply (after we&#39;ve received it)
DBusMessage * dbus_message_new_error(DBusMessage *reply_to, const char *error_name, const char *error_message)
Creates a new message that is an error reply to another message.