D-Bus  1.10.24
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 {
76  dbus_uint32_t reply_serial;
78  unsigned int completed : 1;
79  unsigned int timeout_added : 1;
80 };
81 
82 static void
83 _dbus_pending_call_trace_ref (DBusPendingCall *pending_call,
84  int old_refcount,
85  int new_refcount,
86  const char *why)
87 {
88 #ifdef DBUS_ENABLE_VERBOSE_MODE
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 #endif
94 }
95 
96 static dbus_int32_t notify_user_data_slot = -1;
97 
110  int timeout_milliseconds,
111  DBusTimeoutHandler timeout_handler)
112 {
113  DBusPendingCall *pending;
114  DBusTimeout *timeout;
115 
116  _dbus_assert (timeout_milliseconds >= 0 || timeout_milliseconds == -1);
117 
118  if (timeout_milliseconds == -1)
119  timeout_milliseconds = _DBUS_DEFAULT_TIMEOUT_VALUE;
120 
121  if (!dbus_pending_call_allocate_data_slot (&notify_user_data_slot))
122  return NULL;
123 
124  pending = dbus_new0 (DBusPendingCall, 1);
125 
126  if (pending == NULL)
127  {
128  dbus_pending_call_free_data_slot (&notify_user_data_slot);
129  return NULL;
130  }
131 
132  if (timeout_milliseconds != DBUS_TIMEOUT_INFINITE)
133  {
134  timeout = _dbus_timeout_new (timeout_milliseconds,
135  timeout_handler,
136  pending, NULL);
137 
138  if (timeout == NULL)
139  {
140  dbus_pending_call_free_data_slot (&notify_user_data_slot);
141  dbus_free (pending);
142  return NULL;
143  }
144 
145  pending->timeout = timeout;
146  }
147  else
148  {
149  pending->timeout = NULL;
150  }
151 
152  _dbus_atomic_inc (&pending->refcount);
153  pending->connection = connection;
155 
157 
158  _dbus_pending_call_trace_ref (pending, 0, 1, "new_unlocked");
159 
160  return pending;
161 }
162 
171 void
173  DBusMessage *message)
174 {
175  if (message == NULL)
176  {
177  message = pending->timeout_link->data;
178  _dbus_list_clear (&pending->timeout_link);
179  }
180  else
181  dbus_message_ref (message);
182 
183  _dbus_verbose (" handing message %p (%s) to pending call serial %u\n",
184  message,
186  "method return" :
188  "error" : "other type",
189  pending->reply_serial);
190 
191  _dbus_assert (pending->reply == NULL);
193  pending->reply = message;
194 }
195 
201 void
203 {
204  _dbus_assert (!pending->completed);
205 
206  pending->completed = TRUE;
207 }
208 
214 void
216 {
217  _dbus_assert (pending->completed);
218 
219  if (pending->function)
220  {
221  void *user_data;
222  user_data = dbus_pending_call_get_data (pending,
223  notify_user_data_slot);
224 
225  (* pending->function) (pending, user_data);
226  }
227 }
228 
236 void
238  DBusConnection *connection)
239 {
240  _dbus_assert (connection == pending->connection);
241 
242  if (pending->timeout_link)
243  {
245  pending->timeout_link);
246  pending->timeout_link = NULL;
247  }
248 }
249 
258 {
259  _dbus_assert (pending != NULL);
260 
261  return pending->timeout_added;
262 }
263 
264 
271 void
273  dbus_bool_t is_added)
274 {
275  _dbus_assert (pending != NULL);
276 
277  pending->timeout_added = is_added;
278 }
279 
280 
287 DBusTimeout *
289 {
290  _dbus_assert (pending != NULL);
291 
292  return pending->timeout;
293 }
294 
301 dbus_uint32_t
303 {
304  _dbus_assert (pending != NULL);
305 
306  return pending->reply_serial;
307 }
308 
315 void
317  dbus_uint32_t serial)
318 {
319  _dbus_assert (pending != NULL);
320  _dbus_assert (pending->reply_serial == 0);
321 
322  pending->reply_serial = serial;
323 }
324 
333 {
334  _dbus_assert (pending != NULL);
335 
336  CONNECTION_LOCK (pending->connection);
337  return pending->connection;
338 }
339 
348 {
349  _dbus_assert (pending != NULL);
350 
351  return pending->connection;
352 }
353 
364  DBusMessage *message,
365  dbus_uint32_t serial)
366 {
367  DBusList *reply_link;
368  DBusMessage *reply;
369 
371  "Did not receive a reply. Possible causes include: "
372  "the remote application did not send a reply, "
373  "the message bus security policy blocked the reply, "
374  "the reply timeout expired, or "
375  "the network connection was broken.");
376  if (reply == NULL)
377  return FALSE;
378 
379  reply_link = _dbus_list_alloc_link (reply);
380  if (reply_link == NULL)
381  {
382  /* it's OK to unref this, nothing that could have attached a callback
383  * has ever seen it */
384  dbus_message_unref (reply);
385  return FALSE;
386  }
387 
388  pending->timeout_link = reply_link;
389 
391 
392  return TRUE;
393 }
394 
404 {
405  dbus_int32_t old_refcount;
406 
407  old_refcount = _dbus_atomic_inc (&pending->refcount);
408  _dbus_pending_call_trace_ref (pending, old_refcount, old_refcount + 1,
409  "ref_unlocked");
410 
411  return pending;
412 }
413 
414 
415 static void
416 _dbus_pending_call_last_unref (DBusPendingCall *pending)
417 {
418  DBusConnection *connection;
419 
420  /* If we get here, we should be already detached
421  * from the connection, or never attached.
422  */
423  _dbus_assert (!pending->timeout_added);
424 
425  connection = pending->connection;
426 
427  /* this assumes we aren't holding connection lock... */
429 
430  if (pending->timeout != NULL)
431  _dbus_timeout_unref (pending->timeout);
432 
433  if (pending->timeout_link)
434  {
437  pending->timeout_link = NULL;
438  }
439 
440  if (pending->reply)
441  {
442  dbus_message_unref (pending->reply);
443  pending->reply = NULL;
444  }
445 
446  dbus_free (pending);
447 
448  dbus_pending_call_free_data_slot (&notify_user_data_slot);
449 
450  /* connection lock should not be held. */
451  /* Free the connection last to avoid a weird state while
452  * calling out to application code where the pending exists
453  * but not the connection.
454  */
455  dbus_connection_unref (connection);
456 }
457 
465 void
467 {
468  dbus_int32_t old_refcount;
469 
470  old_refcount = _dbus_atomic_dec (&pending->refcount);
471  _dbus_assert (old_refcount > 0);
472  _dbus_pending_call_trace_ref (pending, old_refcount,
473  old_refcount - 1, "unref_and_unlock");
474 
475  CONNECTION_UNLOCK (pending->connection);
476 
477  if (old_refcount == 1)
478  _dbus_pending_call_last_unref (pending);
479 }
480 
490 {
491  return pending->completed;
492 }
493 
494 static DBusDataSlotAllocator slot_allocator =
495  _DBUS_DATA_SLOT_ALLOCATOR_INIT (_DBUS_LOCK_NAME (pending_call_slots));
496 
512  dbus_int32_t slot,
513  void *data,
514  DBusFreeFunction free_data_func)
515 {
516  DBusFreeFunction old_free_func;
517  void *old_data;
518  dbus_bool_t retval;
519 
520  retval = _dbus_data_slot_list_set (&slot_allocator,
521  &pending->slot_list,
522  slot, data, free_data_func,
523  &old_free_func, &old_data);
524 
525  /* Drop locks to call out to app code */
526  CONNECTION_UNLOCK (pending->connection);
527 
528  if (retval)
529  {
530  if (old_free_func)
531  (* old_free_func) (old_data);
532  }
533 
534  CONNECTION_LOCK (pending->connection);
535 
536  return retval;
537 }
538 
587 {
588  dbus_int32_t old_refcount;
589 
590  _dbus_return_val_if_fail (pending != NULL, NULL);
591 
592  old_refcount = _dbus_atomic_inc (&pending->refcount);
593  _dbus_pending_call_trace_ref (pending, old_refcount, old_refcount + 1,
594  "ref");
595 
596  return pending;
597 }
598 
605 void
607 {
608  dbus_int32_t old_refcount;
609 
610  _dbus_return_if_fail (pending != NULL);
611 
612  old_refcount = _dbus_atomic_dec (&pending->refcount);
613  _dbus_pending_call_trace_ref (pending, old_refcount, old_refcount - 1,
614  "unref");
615 
616  if (old_refcount == 1)
617  _dbus_pending_call_last_unref(pending);
618 }
619 
633  void *user_data,
634  DBusFreeFunction free_user_data)
635 {
636  dbus_bool_t ret = FALSE;
637 
638  _dbus_return_val_if_fail (pending != NULL, FALSE);
639 
640  CONNECTION_LOCK (pending->connection);
641 
642  /* could invoke application code! */
643  if (!_dbus_pending_call_set_data_unlocked (pending, notify_user_data_slot,
644  user_data, free_user_data))
645  goto out;
646 
647  pending->function = function;
648  ret = TRUE;
649 
650 out:
651  CONNECTION_UNLOCK (pending->connection);
652 
653  return ret;
654 }
655 
671 void
673 {
674  _dbus_return_if_fail (pending != NULL);
675 
677  pending);
678 }
679 
689 {
690  dbus_bool_t completed;
691 
692  _dbus_return_val_if_fail (pending != NULL, FALSE);
693 
694  CONNECTION_LOCK (pending->connection);
695  completed = pending->completed;
696  CONNECTION_UNLOCK (pending->connection);
697 
698  return completed;
699 }
700 
712 {
713  DBusMessage *message;
714 
715  _dbus_return_val_if_fail (pending != NULL, NULL);
716  _dbus_return_val_if_fail (pending->completed, NULL);
717  _dbus_return_val_if_fail (pending->reply != NULL, NULL);
718 
719  CONNECTION_LOCK (pending->connection);
720 
721  message = pending->reply;
722  pending->reply = NULL;
723 
724  CONNECTION_UNLOCK (pending->connection);
725 
726  _dbus_message_trace_ref (message, -1, -1, "dbus_pending_call_steal_reply");
727  return message;
728 }
729 
745 void
747 {
748  _dbus_return_if_fail (pending != NULL);
749 
751 }
752 
769 {
770  _dbus_return_val_if_fail (slot_p != NULL, FALSE);
771 
772  return _dbus_data_slot_allocator_alloc (&slot_allocator,
773  slot_p);
774 }
775 
787 void
788 dbus_pending_call_free_data_slot (dbus_int32_t *slot_p)
789 {
790  _dbus_return_if_fail (slot_p != NULL);
791  _dbus_return_if_fail (*slot_p >= 0);
792 
793  _dbus_data_slot_allocator_free (&slot_allocator, slot_p);
794 }
795 
811  dbus_int32_t slot,
812  void *data,
813  DBusFreeFunction free_data_func)
814 {
815  dbus_bool_t retval;
816 
817  _dbus_return_val_if_fail (pending != NULL, FALSE);
818  _dbus_return_val_if_fail (slot >= 0, FALSE);
819 
820 
821  CONNECTION_LOCK (pending->connection);
822  retval = _dbus_pending_call_set_data_unlocked (pending, slot, data, free_data_func);
823  CONNECTION_UNLOCK (pending->connection);
824  return retval;
825 }
826 
835 void*
837  dbus_int32_t slot)
838 {
839  void *res;
840 
841  _dbus_return_val_if_fail (pending != NULL, NULL);
842 
843  CONNECTION_LOCK (pending->connection);
844  res = _dbus_data_slot_list_get (&slot_allocator,
845  &pending->slot_list,
846  slot);
847  CONNECTION_UNLOCK (pending->connection);
848 
849  return res;
850 }
851 
An atomic integer safe to increment or decrement from multiple threads.
Definition: dbus-sysdeps.h:279
DBusMessage * dbus_message_ref(DBusMessage *message)
Increments the reference count of a DBusMessage.
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
Internals of DBusTimeout.
Definition: dbus-timeout.c:40
#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:701
void _dbus_pending_call_finish_completion(DBusPendingCall *pending)
Call the notifier function for the pending call.
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:242
#define DBUS_MESSAGE_TYPE_METHOD_RETURN
Message type of a method return message, see dbus_message_get_type()
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().
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_int32_t _dbus_atomic_inc(DBusAtomic *atomic)
Atomically increments an integer.
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...
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
#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.
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:69
void _dbus_list_free_link(DBusList *link)
Frees a linked list node allocated with _dbus_list_alloc_link.
Definition: dbus-list.c:254
void dbus_pending_call_unref(DBusPendingCall *pending)
Decrements the reference count on a pending call, freeing it if the count reaches 0...
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.
DBUS_PRIVATE_EXPORT DBusPendingCall * _dbus_pending_call_new_unlocked(DBusConnection *connection, int timeout_milliseconds, DBusTimeoutHandler timeout_handler)
Creates a new pending reply object.
dbus_int32_t _dbus_atomic_dec(DBusAtomic *atomic)
Atomically decrement an integer.
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...
dbus_bool_t _dbus_data_slot_allocator_alloc(DBusDataSlotAllocator *allocator, dbus_int32_t *slot_id_p)
Allocates an integer ID to be used for storing data in a DBusDataSlotList.
Definition: dbus-dataslot.c:69
DBUS_PRIVATE_EXPORT 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...
DBUS_PRIVATE_EXPORT DBusConnection * _dbus_connection_ref_unlocked(DBusConnection *connection)
Increments the reference count of a DBusConnection.
#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.
DBUS_PRIVATE_EXPORT 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_start_completion_unlocked(DBusPendingCall *pending)
Sets the pending call to completed.
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:542
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.