D-Bus  1.8.16
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 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 
203 void
205 {
206  _dbus_assert (!pending->completed);
207 
208  pending->completed = TRUE;
209 
210  if (pending->function)
211  {
212  void *user_data;
213  user_data = dbus_pending_call_get_data (pending,
214  notify_user_data_slot);
215 
216  (* pending->function) (pending, user_data);
217  }
218 }
219 
227 void
229  DBusConnection *connection)
230 {
231  _dbus_assert (connection == pending->connection);
232 
233  if (pending->timeout_link)
234  {
236  pending->timeout_link);
237  pending->timeout_link = NULL;
238  }
239 }
240 
249 {
250  _dbus_assert (pending != NULL);
251 
252  return pending->timeout_added;
253 }
254 
255 
262 void
264  dbus_bool_t is_added)
265 {
266  _dbus_assert (pending != NULL);
267 
268  pending->timeout_added = is_added;
269 }
270 
271 
278 DBusTimeout *
280 {
281  _dbus_assert (pending != NULL);
282 
283  return pending->timeout;
284 }
285 
294 {
295  _dbus_assert (pending != NULL);
296 
297  return pending->reply_serial;
298 }
299 
306 void
308  dbus_uint32_t serial)
309 {
310  _dbus_assert (pending != NULL);
311  _dbus_assert (pending->reply_serial == 0);
312 
313  pending->reply_serial = serial;
314 }
315 
324 {
325  _dbus_assert (pending != NULL);
326 
327  CONNECTION_LOCK (pending->connection);
328  return pending->connection;
329 }
330 
339 {
340  _dbus_assert (pending != NULL);
341 
342  return pending->connection;
343 }
344 
355  DBusMessage *message,
356  dbus_uint32_t serial)
357 {
358  DBusList *reply_link;
359  DBusMessage *reply;
360 
362  "Did not receive a reply. Possible causes include: "
363  "the remote application did not send a reply, "
364  "the message bus security policy blocked the reply, "
365  "the reply timeout expired, or "
366  "the network connection was broken.");
367  if (reply == NULL)
368  return FALSE;
369 
370  reply_link = _dbus_list_alloc_link (reply);
371  if (reply_link == NULL)
372  {
373  /* it's OK to unref this, nothing that could have attached a callback
374  * has ever seen it */
375  dbus_message_unref (reply);
376  return FALSE;
377  }
378 
379  pending->timeout_link = reply_link;
380 
382 
383  return TRUE;
384 }
385 
395 {
396  dbus_int32_t old_refcount;
397 
398  old_refcount = _dbus_atomic_inc (&pending->refcount);
399  _dbus_pending_call_trace_ref (pending, old_refcount, old_refcount + 1,
400  "ref_unlocked");
401 
402  return pending;
403 }
404 
405 
406 static void
407 _dbus_pending_call_last_unref (DBusPendingCall *pending)
408 {
409  DBusConnection *connection;
410 
411  /* If we get here, we should be already detached
412  * from the connection, or never attached.
413  */
414  _dbus_assert (!pending->timeout_added);
415 
416  connection = pending->connection;
417 
418  /* this assumes we aren't holding connection lock... */
420 
421  if (pending->timeout != NULL)
422  _dbus_timeout_unref (pending->timeout);
423 
424  if (pending->timeout_link)
425  {
428  pending->timeout_link = NULL;
429  }
430 
431  if (pending->reply)
432  {
433  dbus_message_unref (pending->reply);
434  pending->reply = NULL;
435  }
436 
437  dbus_free (pending);
438 
439  dbus_pending_call_free_data_slot (&notify_user_data_slot);
440 
441  /* connection lock should not be held. */
442  /* Free the connection last to avoid a weird state while
443  * calling out to application code where the pending exists
444  * but not the connection.
445  */
446  dbus_connection_unref (connection);
447 }
448 
456 void
458 {
459  dbus_int32_t old_refcount;
460 
461  old_refcount = _dbus_atomic_dec (&pending->refcount);
462  _dbus_assert (old_refcount > 0);
463  _dbus_pending_call_trace_ref (pending, old_refcount,
464  old_refcount - 1, "unref_and_unlock");
465 
466  CONNECTION_UNLOCK (pending->connection);
467 
468  if (old_refcount == 1)
469  _dbus_pending_call_last_unref (pending);
470 }
471 
481 {
482  return pending->completed;
483 }
484 
485 static DBusDataSlotAllocator slot_allocator =
486  _DBUS_DATA_SLOT_ALLOCATOR_INIT (_DBUS_LOCK_NAME (pending_call_slots));
487 
503  dbus_int32_t slot,
504  void *data,
505  DBusFreeFunction free_data_func)
506 {
507  DBusFreeFunction old_free_func;
508  void *old_data;
509  dbus_bool_t retval;
510 
511  retval = _dbus_data_slot_list_set (&slot_allocator,
512  &pending->slot_list,
513  slot, data, free_data_func,
514  &old_free_func, &old_data);
515 
516  /* Drop locks to call out to app code */
517  CONNECTION_UNLOCK (pending->connection);
518 
519  if (retval)
520  {
521  if (old_free_func)
522  (* old_free_func) (old_data);
523  }
524 
525  CONNECTION_LOCK (pending->connection);
526 
527  return retval;
528 }
529 
578 {
579  dbus_int32_t old_refcount;
580 
581  _dbus_return_val_if_fail (pending != NULL, NULL);
582 
583  old_refcount = _dbus_atomic_inc (&pending->refcount);
584  _dbus_pending_call_trace_ref (pending, old_refcount, old_refcount + 1,
585  "ref");
586 
587  return pending;
588 }
589 
596 void
598 {
599  dbus_int32_t old_refcount;
600 
601  _dbus_return_if_fail (pending != NULL);
602 
603  old_refcount = _dbus_atomic_dec (&pending->refcount);
604  _dbus_pending_call_trace_ref (pending, old_refcount, old_refcount - 1,
605  "unref");
606 
607  if (old_refcount == 1)
608  _dbus_pending_call_last_unref(pending);
609 }
610 
624  void *user_data,
625  DBusFreeFunction free_user_data)
626 {
627  dbus_bool_t ret = FALSE;
628 
629  _dbus_return_val_if_fail (pending != NULL, FALSE);
630 
631  CONNECTION_LOCK (pending->connection);
632 
633  /* could invoke application code! */
634  if (!_dbus_pending_call_set_data_unlocked (pending, notify_user_data_slot,
635  user_data, free_user_data))
636  goto out;
637 
638  pending->function = function;
639  ret = TRUE;
640 
641 out:
642  CONNECTION_UNLOCK (pending->connection);
643 
644  return ret;
645 }
646 
662 void
664 {
665  _dbus_return_if_fail (pending != NULL);
666 
668  pending);
669 }
670 
680 {
681  dbus_bool_t completed;
682 
683  _dbus_return_val_if_fail (pending != NULL, FALSE);
684 
685  CONNECTION_LOCK (pending->connection);
686  completed = pending->completed;
687  CONNECTION_UNLOCK (pending->connection);
688 
689  return completed;
690 }
691 
703 {
704  DBusMessage *message;
705 
706  _dbus_return_val_if_fail (pending != NULL, NULL);
707  _dbus_return_val_if_fail (pending->completed, NULL);
708  _dbus_return_val_if_fail (pending->reply != NULL, NULL);
709 
710  CONNECTION_LOCK (pending->connection);
711 
712  message = pending->reply;
713  pending->reply = NULL;
714 
715  CONNECTION_UNLOCK (pending->connection);
716 
717  _dbus_message_trace_ref (message, -1, -1, "dbus_pending_call_steal_reply");
718  return message;
719 }
720 
736 void
738 {
739  _dbus_return_if_fail (pending != NULL);
740 
742 }
743 
760 {
761  _dbus_return_val_if_fail (slot_p != NULL, FALSE);
762 
763  return _dbus_data_slot_allocator_alloc (&slot_allocator,
764  slot_p);
765 }
766 
778 void
780 {
781  _dbus_return_if_fail (slot_p != NULL);
782  _dbus_return_if_fail (*slot_p >= 0);
783 
784  _dbus_data_slot_allocator_free (&slot_allocator, slot_p);
785 }
786 
802  dbus_int32_t slot,
803  void *data,
804  DBusFreeFunction free_data_func)
805 {
806  dbus_bool_t retval;
807 
808  _dbus_return_val_if_fail (pending != NULL, FALSE);
809  _dbus_return_val_if_fail (slot >= 0, FALSE);
810 
811 
812  CONNECTION_LOCK (pending->connection);
813  retval = _dbus_pending_call_set_data_unlocked (pending, slot, data, free_data_func);
814  CONNECTION_UNLOCK (pending->connection);
815  return retval;
816 }
817 
826 void*
828  dbus_int32_t slot)
829 {
830  void *res;
831 
832  _dbus_return_val_if_fail (pending != NULL, NULL);
833 
834  CONNECTION_LOCK (pending->connection);
835  res = _dbus_data_slot_list_get (&slot_allocator,
836  &pending->slot_list,
837  slot);
838  CONNECTION_UNLOCK (pending->connection);
839 
840  return res;
841 }
842 
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:227
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:701
void(* DBusPendingCallNotifyFunction)(DBusPendingCall *pending, void *user_data)
Called when a pending call now has a reply available.
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()
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'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
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'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 "1".
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't been timed out, add its timeout error reply to the connection'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
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 "0".
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
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'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:542
dbus_bool_t(* DBusTimeoutHandler)(void *data)
function to run when the timeout is handled
Definition: dbus-timeout.h:41
DBusMessage * reply
Reply (after we'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.