DBusConnection
[D-Bus low-level public API]

Connection to another application. More...

Data Structures

struct  DBusObjectPathVTable
 Virtual table that must be implemented to handle a portion of the object path hierarchy. More...

Typedefs

typedef DBusPreallocatedSend DBusPreallocatedSend
 Opaque type representing preallocated resources so a message can be sent without further memory allocation.
typedef DBusConnection DBusConnection
 Opaque type representing a connection to a remote application and associated incoming/outgoing message queues.
typedef DBusObjectPathVTable DBusObjectPathVTable
 Set of functions that must be implemented to handle messages sent to a particular object path.
typedef dbus_bool_t(*) DBusAddWatchFunction (DBusWatch *watch, void *data)
 Called when libdbus needs a new watch to be monitored by the main loop.
typedef void(*) DBusWatchToggledFunction (DBusWatch *watch, void *data)
 Called when dbus_watch_get_enabled() may return a different value than it did before.
typedef void(*) DBusRemoveWatchFunction (DBusWatch *watch, void *data)
 Called when libdbus no longer needs a watch to be monitored by the main loop.
typedef dbus_bool_t(*) DBusAddTimeoutFunction (DBusTimeout *timeout, void *data)
 Called when libdbus needs a new timeout to be monitored by the main loop.
typedef void(*) DBusTimeoutToggledFunction (DBusTimeout *timeout, void *data)
 Called when dbus_timeout_get_enabled() may return a different value than it did before.
typedef void(*) DBusRemoveTimeoutFunction (DBusTimeout *timeout, void *data)
 Called when libdbus no longer needs a timeout to be monitored by the main loop.
typedef void(*) DBusDispatchStatusFunction (DBusConnection *connection, DBusDispatchStatus new_status, void *data)
 Called when the return value of dbus_connection_get_dispatch_status() may have changed.
typedef void(*) DBusWakeupMainFunction (void *data)
 Called when the main loop's thread should be notified that there's now work to do.
typedef dbus_bool_t(*) DBusAllowUnixUserFunction (DBusConnection *connection, unsigned long uid, void *data)
 Called during authentication on UNIX systems to check whether the given user ID is allowed to connect.
typedef void(*) DBusPendingCallNotifyFunction (DBusPendingCall *pending, void *user_data)
 Called when a pending call now has a reply available.
typedef DBusHandlerResult(*) DBusHandleMessageFunction (DBusConnection *connection, DBusMessage *message, void *user_data)
 Called when a message needs to be handled.
typedef void(*) DBusObjectPathUnregisterFunction (DBusConnection *connection, void *user_data)
 Called when a DBusObjectPathVTable is unregistered (or its connection is freed).
typedef DBusHandlerResult(*) DBusObjectPathMessageFunction (DBusConnection *connection, DBusMessage *message, void *user_data)
 Called when a message is sent to a registered object path.

Enumerations

enum  DBusWatchFlags { DBUS_WATCH_READABLE = 1 << 0, DBUS_WATCH_WRITABLE = 1 << 1, DBUS_WATCH_ERROR = 1 << 2, DBUS_WATCH_HANGUP = 1 << 3 }
 Indicates the status of a DBusWatch. More...
enum  DBusDispatchStatus { DBUS_DISPATCH_DATA_REMAINS, DBUS_DISPATCH_COMPLETE, DBUS_DISPATCH_NEED_MEMORY }
 Indicates the status of incoming data on a DBusConnection. More...

Functions

DBusConnectiondbus_connection_open (const char *address, DBusError *error)
 Gets a connection to a remote address.
DBusConnectiondbus_connection_open_private (const char *address, DBusError *error)
 Opens a new, dedicated connection to a remote address.
DBusConnectiondbus_connection_ref (DBusConnection *connection)
 Increments the reference count of a DBusConnection.
void dbus_connection_unref (DBusConnection *connection)
 Decrements the reference count of a DBusConnection, and finalizes it if the count reaches zero.
void dbus_connection_close (DBusConnection *connection)
 Closes a private connection, so no further data can be sent or received.
dbus_bool_t dbus_connection_get_is_connected (DBusConnection *connection)
 Gets whether the connection is currently open.
dbus_bool_t dbus_connection_get_is_authenticated (DBusConnection *connection)
 Gets whether the connection was authenticated.
void dbus_connection_set_exit_on_disconnect (DBusConnection *connection, dbus_bool_t exit_on_disconnect)
 Set whether _exit() should be called when the connection receives a disconnect signal.
DBusPreallocatedSenddbus_connection_preallocate_send (DBusConnection *connection)
 Preallocates resources needed to send a message, allowing the message to be sent without the possibility of memory allocation failure.
void dbus_connection_free_preallocated_send (DBusConnection *connection, DBusPreallocatedSend *preallocated)
 Frees preallocated message-sending resources from dbus_connection_preallocate_send().
void dbus_connection_send_preallocated (DBusConnection *connection, DBusPreallocatedSend *preallocated, DBusMessage *message, dbus_uint32_t *client_serial)
 Sends a message using preallocated resources.
dbus_bool_t dbus_connection_send (DBusConnection *connection, DBusMessage *message, dbus_uint32_t *serial)
 Adds a message to the outgoing message queue.
dbus_bool_t dbus_connection_send_with_reply (DBusConnection *connection, DBusMessage *message, DBusPendingCall **pending_return, int timeout_milliseconds)
 Queues a message to send, as with dbus_connection_send(), but also returns a DBusPendingCall used to receive a reply to the message.
DBusMessagedbus_connection_send_with_reply_and_block (DBusConnection *connection, DBusMessage *message, int timeout_milliseconds, DBusError *error)
 Sends a message and blocks a certain time period while waiting for a reply.
void dbus_connection_flush (DBusConnection *connection)
 Blocks until the outgoing message queue is empty.
dbus_bool_t dbus_connection_read_write_dispatch (DBusConnection *connection, int timeout_milliseconds)
 This function is intended for use with applications that don't want to write a main loop and deal with DBusWatch and DBusTimeout.
dbus_bool_t dbus_connection_read_write (DBusConnection *connection, int timeout_milliseconds)
 This function is intended for use with applications that don't want to write a main loop and deal with DBusWatch and DBusTimeout.
DBusMessagedbus_connection_borrow_message (DBusConnection *connection)
 Returns the first-received message from the incoming message queue, leaving it in the queue.
void dbus_connection_return_message (DBusConnection *connection, DBusMessage *message)
 Used to return a message after peeking at it using dbus_connection_borrow_message().
void dbus_connection_steal_borrowed_message (DBusConnection *connection, DBusMessage *message)
 Used to keep a message after peeking at it using dbus_connection_borrow_message().
DBusMessagedbus_connection_pop_message (DBusConnection *connection)
 Returns the first-received message from the incoming message queue, removing it from the queue.
DBusDispatchStatus dbus_connection_get_dispatch_status (DBusConnection *connection)
 Gets the current state of the incoming message queue.
DBusDispatchStatus dbus_connection_dispatch (DBusConnection *connection)
 Processes any incoming data.
dbus_bool_t dbus_connection_set_watch_functions (DBusConnection *connection, DBusAddWatchFunction add_function, DBusRemoveWatchFunction remove_function, DBusWatchToggledFunction toggled_function, void *data, DBusFreeFunction free_data_function)
 Sets the watch functions for the connection.
dbus_bool_t dbus_connection_set_timeout_functions (DBusConnection *connection, DBusAddTimeoutFunction add_function, DBusRemoveTimeoutFunction remove_function, DBusTimeoutToggledFunction toggled_function, void *data, DBusFreeFunction free_data_function)
 Sets the timeout functions for the connection.
void dbus_connection_set_wakeup_main_function (DBusConnection *connection, DBusWakeupMainFunction wakeup_main_function, void *data, DBusFreeFunction free_data_function)
 Sets the mainloop wakeup function for the connection.
void dbus_connection_set_dispatch_status_function (DBusConnection *connection, DBusDispatchStatusFunction function, void *data, DBusFreeFunction free_data_function)
 Set a function to be invoked when the dispatch status changes.
dbus_bool_t dbus_connection_get_unix_fd (DBusConnection *connection, int *fd)
 Get the UNIX file descriptor of the connection, if any.
dbus_bool_t dbus_connection_get_socket (DBusConnection *connection, int *fd)
 Gets the underlying Windows or UNIX socket file descriptor of the connection, if any.
dbus_bool_t dbus_connection_get_unix_user (DBusConnection *connection, unsigned long *uid)
 Gets the UNIX user ID of the connection if known.
dbus_bool_t dbus_connection_get_unix_process_id (DBusConnection *connection, unsigned long *pid)
 Gets the process ID of the connection if any.
void dbus_connection_set_unix_user_function (DBusConnection *connection, DBusAllowUnixUserFunction function, void *data, DBusFreeFunction free_data_function)
 Sets a predicate function used to determine whether a given user ID is allowed to connect.
void dbus_connection_set_route_peer_messages (DBusConnection *connection, dbus_bool_t value)
 Normally DBusConnection automatically handles all messages to the org.freedesktop.DBus.Peer interface.
dbus_bool_t dbus_connection_add_filter (DBusConnection *connection, DBusHandleMessageFunction function, void *user_data, DBusFreeFunction free_data_function)
 Adds a message filter.
void dbus_connection_remove_filter (DBusConnection *connection, DBusHandleMessageFunction function, void *user_data)
 Removes a previously-added message filter.
dbus_bool_t dbus_connection_register_object_path (DBusConnection *connection, const char *path, const DBusObjectPathVTable *vtable, void *user_data)
 Registers a handler for a given path in the object hierarchy.
dbus_bool_t dbus_connection_register_fallback (DBusConnection *connection, const char *path, const DBusObjectPathVTable *vtable, void *user_data)
 Registers a fallback handler for a given subsection of the object hierarchy.
dbus_bool_t dbus_connection_unregister_object_path (DBusConnection *connection, const char *path)
 Unregisters the handler registered with exactly the given path.
dbus_bool_t dbus_connection_get_object_path_data (DBusConnection *connection, const char *path, void **data_p)
 Gets the user data passed to dbus_connection_register_object_path() or dbus_connection_register_fallback().
dbus_bool_t dbus_connection_list_registered (DBusConnection *connection, const char *parent_path, char ***child_entries)
 Lists the registered fallback handlers and object path handlers at the given parent_path.
dbus_bool_t dbus_connection_allocate_data_slot (dbus_int32_t *slot_p)
 Allocates an integer ID to be used for storing application-specific data on any DBusConnection.
void dbus_connection_free_data_slot (dbus_int32_t *slot_p)
 Deallocates a global ID for connection data slots.
dbus_bool_t dbus_connection_set_data (DBusConnection *connection, dbus_int32_t slot, void *data, DBusFreeFunction free_data_func)
 Stores a pointer on a DBusConnection, along with an optional function to be used for freeing the data when the data is set again, or when the connection is finalized.
void * dbus_connection_get_data (DBusConnection *connection, dbus_int32_t slot)
 Retrieves data previously set with dbus_connection_set_data().
void dbus_connection_set_change_sigpipe (dbus_bool_t will_modify_sigpipe)
 This function sets a global flag for whether dbus_connection_new() will set SIGPIPE behavior to SIG_IGN.
void dbus_connection_set_max_message_size (DBusConnection *connection, long size)
 Specifies the maximum size message this connection is allowed to receive.
long dbus_connection_get_max_message_size (DBusConnection *connection)
 Gets the value set by dbus_connection_set_max_message_size().
void dbus_connection_set_max_received_size (DBusConnection *connection, long size)
 Sets the maximum total number of bytes that can be used for all messages received on this connection.
long dbus_connection_get_max_received_size (DBusConnection *connection)
 Gets the value set by dbus_connection_set_max_received_size().
long dbus_connection_get_outgoing_size (DBusConnection *connection)
 Gets the approximate size in bytes of all messages in the outgoing message queue.
dbus_bool_t dbus_connection_has_messages_to_send (DBusConnection *connection)
 Checks whether there are messages in the outgoing message queue.

Detailed Description

Connection to another application.

A DBusConnection represents a connection to another application. Messages can be sent and received via this connection. The other application may be a message bus; for convenience, the function dbus_bus_get() is provided to automatically open a connection to the well-known message buses.

In brief a DBusConnection is a message queue associated with some message transport mechanism such as a socket. The connection maintains a queue of incoming messages and a queue of outgoing messages.

Several functions use the following terms:

The function dbus_connection_read_write_dispatch() for example does all three of these things, offering a simple alternative to a main loop.

In an application with a main loop, the read/write/dispatch operations are usually separate.

The connection provides DBusWatch and DBusTimeout objects to the main loop. These are used to know when reading, writing, or dispatching should be performed.

Incoming messages are processed by calling dbus_connection_dispatch(). dbus_connection_dispatch() runs any handlers registered for the topmost message in the message queue, then discards the message, then returns.

dbus_connection_get_dispatch_status() indicates whether messages are currently in the queue that need dispatching. dbus_connection_set_dispatch_status_function() allows you to set a function to be used to monitor the dispatch status.

If you're using GLib or Qt add-on libraries for D-Bus, there are special convenience APIs in those libraries that hide all the details of dispatch and watch/timeout monitoring. For example, dbus_connection_setup_with_g_main().

If you aren't using these add-on libraries, but want to process messages asynchronously, you must manually call dbus_connection_set_dispatch_status_function(), dbus_connection_set_watch_functions(), dbus_connection_set_timeout_functions() providing appropriate functions to integrate the connection with your application's main loop. This can be tricky to get right; main loops are not simple.

If you don't need to be asynchronous, you can ignore DBusWatch, DBusTimeout, and dbus_connection_dispatch(). Instead, dbus_connection_read_write_dispatch() can be used.

Or, in very simple applications, dbus_connection_pop_message() may be all you need, allowing you to avoid setting up any handler functions (see dbus_connection_add_filter(), dbus_connection_register_object_path() for more on handlers).

When you use dbus_connection_send() or one of its variants to send a message, the message is added to the outgoing queue. It's actually written to the network later; either in dbus_watch_handle() invoked by your main loop, or in dbus_connection_flush() which blocks until it can write out the entire outgoing queue. The GLib/Qt add-on libraries again handle the details here for you by setting up watch functions.

When a connection is disconnected, you are guaranteed to get a signal "Disconnected" from the interface DBUS_INTERFACE_LOCAL, path DBUS_PATH_LOCAL.

You may not drop the last reference to a DBusConnection until that connection has been disconnected.

You may dispatch the unprocessed incoming message queue even if the connection is disconnected. However, "Disconnected" will always be the last message in the queue (obviously no messages are received after disconnection).

After calling dbus_threads_init(), DBusConnection has thread locks and drops them when invoking user callbacks, so in general is transparently threadsafe. However, DBusMessage does NOT have thread locks; you must not send the same message to multiple DBusConnection if those connections will be used from different threads, for example.

Also, if you dispatch or pop messages from multiple threads, it may work in the sense that it won't crash, but it's tough to imagine sane results; it will be completely unpredictable which messages go to which threads.

It's recommended to dispatch from a single thread.

The most useful function to call from multiple threads at once is dbus_connection_send_with_reply_and_block(). That is, multiple threads can make method calls at the same time.

If you aren't using threads, you can use a main loop and dbus_pending_call_set_notify() to achieve a similar result.


Typedef Documentation

typedef dbus_bool_t(* ) DBusAddTimeoutFunction(DBusTimeout *timeout, void *data)

Called when libdbus needs a new timeout to be monitored by the main loop.

Returns FALSE if it lacks enough memory to add the watch. Set by dbus_connection_set_timeout_functions() or dbus_server_set_timeout_functions().

Definition at line 109 of file dbus-connection.h.

typedef dbus_bool_t(* ) DBusAddWatchFunction(DBusWatch *watch, void *data)

Called when libdbus needs a new watch to be monitored by the main loop.

Returns FALSE if it lacks enough memory to add the watch. Set by dbus_connection_set_watch_functions() or dbus_server_set_watch_functions().

Definition at line 90 of file dbus-connection.h.

typedef dbus_bool_t(* ) DBusAllowUnixUserFunction(DBusConnection *connection, unsigned long uid, void *data)

Called during authentication on UNIX systems to check whether the given user ID is allowed to connect.

Never called on Windows. Set with dbus_connection_set_unix_user_function().

Definition at line 140 of file dbus-connection.h.

typedef void(* ) DBusDispatchStatusFunction(DBusConnection *connection, DBusDispatchStatus new_status, void *data)

Called when the return value of dbus_connection_get_dispatch_status() may have changed.

Set with dbus_connection_set_dispatch_status_function().

Definition at line 127 of file dbus-connection.h.

typedef DBusHandlerResult(* ) DBusHandleMessageFunction(DBusConnection *connection, DBusMessage *message, void *user_data)

Called when a message needs to be handled.

The result indicates whether or not more handlers should be run. Set with dbus_connection_add_filter().

Definition at line 154 of file dbus-connection.h.

typedef DBusHandlerResult(* ) DBusObjectPathMessageFunction(DBusConnection *connection, DBusMessage *message, void *user_data)

Called when a message is sent to a registered object path.

Found in DBusObjectPathVTable which is registered with dbus_connection_register_object_path() or dbus_connection_register_fallback().

Definition at line 279 of file dbus-connection.h.

typedef void(* ) DBusObjectPathUnregisterFunction(DBusConnection *connection, void *user_data)

Called when a DBusObjectPathVTable is unregistered (or its connection is freed).

Found in DBusObjectPathVTable.

Definition at line 272 of file dbus-connection.h.

typedef void(* ) DBusPendingCallNotifyFunction(DBusPendingCall *pending, void *user_data)

Called when a pending call now has a reply available.

Set with dbus_pending_call_set_notify().

Definition at line 147 of file dbus-connection.h.

typedef void(* ) DBusRemoveTimeoutFunction(DBusTimeout *timeout, void *data)

Called when libdbus no longer needs a timeout to be monitored by the main loop.

Set by dbus_connection_set_timeout_functions() or dbus_server_set_timeout_functions().

Definition at line 122 of file dbus-connection.h.

typedef void(* ) DBusRemoveWatchFunction(DBusWatch *watch, void *data)

Called when libdbus no longer needs a watch to be monitored by the main loop.

Set by dbus_connection_set_watch_functions() or dbus_server_set_watch_functions().

Definition at line 102 of file dbus-connection.h.

typedef void(* ) DBusTimeoutToggledFunction(DBusTimeout *timeout, void *data)

Called when dbus_timeout_get_enabled() may return a different value than it did before.

Set by dbus_connection_set_timeout_functions() or dbus_server_set_timeout_functions().

Definition at line 116 of file dbus-connection.h.

typedef void(* ) DBusWakeupMainFunction(void *data)

Called when the main loop's thread should be notified that there's now work to do.

Set with dbus_connection_set_wakeup_main_function().

Definition at line 134 of file dbus-connection.h.

typedef void(* ) DBusWatchToggledFunction(DBusWatch *watch, void *data)

Called when dbus_watch_get_enabled() may return a different value than it did before.

Set by dbus_connection_set_watch_functions() or dbus_server_set_watch_functions().

Definition at line 96 of file dbus-connection.h.


Enumeration Type Documentation

enum DBusDispatchStatus

Indicates the status of incoming data on a DBusConnection.

This determines whether dbus_connection_dispatch() needs to be called.

Enumerator:
DBUS_DISPATCH_DATA_REMAINS  There is more data to potentially convert to messages.
DBUS_DISPATCH_COMPLETE  All currently available data has been processed.
DBUS_DISPATCH_NEED_MEMORY  More memory is needed to continue.

Definition at line 78 of file dbus-connection.h.

enum DBusWatchFlags

Indicates the status of a DBusWatch.

Enumerator:
DBUS_WATCH_READABLE  As in POLLIN.
DBUS_WATCH_WRITABLE  As in POLLOUT.
DBUS_WATCH_ERROR  As in POLLERR (can't watch for this, but can be present in current state passed to dbus_watch_handle()).
DBUS_WATCH_HANGUP  As in POLLHUP (can't watch for it, but can be present in current state passed to dbus_watch_handle()).

Definition at line 58 of file dbus-connection.h.


Function Documentation

dbus_bool_t dbus_connection_add_filter ( DBusConnection connection,
DBusHandleMessageFunction  function,
void *  user_data,
DBusFreeFunction  free_data_function 
)

Adds a message filter.

Filters are handlers that are run on all incoming messages, prior to the objects registered with dbus_connection_register_object_path(). Filters are run in the order that they were added. The same handler can be added as a filter more than once, in which case it will be run more than once. Filters added during a filter callback won't be run on the message being processed.

Todo:
we don't run filters on messages while blocking without entering the main loop, since filters are run as part of dbus_connection_dispatch(). This is probably a feature, as filters could create arbitrary reentrancy. But kind of sucks if you're trying to filter METHOD_RETURN for some reason.
Parameters:
connection the connection
function function to handle messages
user_data user data to pass to the function
free_data_function function to use for freeing user data
Returns:
TRUE on success, FALSE if not enough memory.

Definition at line 4951 of file dbus-connection.c.

References _dbus_list_append(), CONNECTION_LOCK, CONNECTION_UNLOCK, dbus_new0, FALSE, filter_list, DBusMessageFilter::free_user_data_function, DBusMessageFilter::function, NULL, DBusMessageFilter::refcount, TRUE, DBusMessageFilter::user_data, and DBusAtomic::value.

dbus_bool_t dbus_connection_allocate_data_slot ( dbus_int32_t slot_p  ) 

Allocates an integer ID to be used for storing application-specific data on any DBusConnection.

The allocated ID may then be used with dbus_connection_set_data() and dbus_connection_get_data(). The passed-in slot must be initialized to -1, and is filled in with the slot ID. If the passed-in slot is not -1, it's assumed to be already allocated, and its refcount is incremented.

The allocated slot is global, i.e. all DBusConnection objects will have a slot with the given integer ID reserved.

Parameters:
slot_p address of a global variable storing the slot
Returns:
FALSE on failure (no memory)

Definition at line 5260 of file dbus-connection.c.

References _dbus_data_slot_allocator_alloc(), and _DBUS_LOCK_NAME.

DBusMessage * dbus_connection_borrow_message ( DBusConnection connection  ) 

Returns the first-received message from the incoming message queue, leaving it in the queue.

If the queue is empty, returns NULL.

The caller does not own a reference to the returned message, and must either return it using dbus_connection_return_message() or keep it after calling dbus_connection_steal_borrowed_message(). No one can get at the message while its borrowed, so return it as quickly as possible and don't keep a reference to it after returning it. If you need to keep the message, make a copy of it.

dbus_connection_dispatch() will block if called while a borrowed message is outstanding; only one piece of code can be playing with the incoming queue at a time. This function will block if called during a dbus_connection_dispatch().

Parameters:
connection the connection.
Returns:
next message in the incoming queue.

Definition at line 3454 of file dbus-connection.c.

References _dbus_assert, _dbus_list_get_first(), CONNECTION_LOCK, CONNECTION_UNLOCK, dbus_connection_get_dispatch_status(), DBUS_DISPATCH_DATA_REMAINS, incoming_messages, message_borrowed, and NULL.

void dbus_connection_close ( DBusConnection connection  ) 

Closes a private connection, so no further data can be sent or received.

This disconnects the transport (such as a socket) underlying the connection.

Attempts to send messages after closing a connection are safe, but will result in error replies generated locally in libdbus.

This function does not affect the connection's reference count. It's safe to close a connection more than once; all calls after the first do nothing. It's impossible to "reopen" a connection, a new connection must be created. This function may result in a call to the DBusDispatchStatusFunction set with dbus_connection_set_dispatch_status_function(), as the disconnect message it generates needs to be dispatched.

If a connection is dropped by the remote application, it will close itself.

You must close a connection prior to releasing the last reference to the connection. If you dbus_connection_unref() for the last time without closing the connection, the results are undefined; it is a bug in your program and libdbus will try to print a warning.

You may not close a shared connection. Connections created with dbus_connection_open() or dbus_bus_get() are shared. These connections are owned by libdbus, and applications should only unref them, never close them. Applications can know it is safe to unref these connections because libdbus will be holding a reference as long as the connection is open. Thus, either the connection is closed and it is OK to drop the last reference, or the connection is open and the app knows it does not have the last reference.

Connections created with dbus_connection_open_private() or dbus_bus_get_private() are not kept track of or referenced by libdbus. The creator of these connections is responsible for calling dbus_connection_close() prior to releasing the last reference, if the connection is not already disconnected.

Parameters:
connection the private (unshared) connection to close

Definition at line 2741 of file dbus-connection.c.

References _dbus_current_generation, _dbus_warn_check_failed(), CONNECTION_LOCK, CONNECTION_UNLOCK, generation, NULL, and shareable.

DBusDispatchStatus dbus_connection_dispatch ( DBusConnection connection  ) 

Processes any incoming data.

If there's incoming raw data that has not yet been parsed, it is parsed, which may or may not result in adding messages to the incoming queue.

The incoming data buffer is filled when the connection reads from its underlying transport (such as a socket). Reading usually happens in dbus_watch_handle() or dbus_connection_read_write().

If there are complete messages in the incoming queue, dbus_connection_dispatch() removes one message from the queue and processes it. Processing has three steps.

First, any method replies are passed to DBusPendingCall or dbus_connection_send_with_reply_and_block() in order to complete the pending method call.

Second, any filters registered with dbus_connection_add_filter() are run. If any filter returns DBUS_HANDLER_RESULT_HANDLED then processing stops after that filter.

Third, if the message is a method call it is forwarded to any registered object path handlers added with dbus_connection_register_object_path() or dbus_connection_register_fallback().

A single call to dbus_connection_dispatch() will process at most one message; it will not clear the entire message queue.

Be careful about calling dbus_connection_dispatch() from inside a message handler, i.e. calling dbus_connection_dispatch() recursively. If threads have been initialized with a recursive mutex function, then this will not deadlock; however, it can certainly confuse your application.

Todo:
some FIXME in here about handling DBUS_HANDLER_RESULT_NEED_MEMORY
Parameters:
connection the connection
Returns:
dispatch status, see dbus_connection_get_dispatch_status()

Definition at line 4141 of file dbus-connection.c.

References _dbus_connection_ref_unlocked(), _dbus_hash_table_lookup_int(), _dbus_list_clear(), _dbus_list_copy(), _dbus_list_foreach(), _dbus_list_free_link(), _dbus_list_get_first_link(), _dbus_list_get_next_link, _dbus_object_tree_dispatch_and_unlock(), _dbus_string_append_printf(), _dbus_string_free(), _dbus_string_init(), CONNECTION_LOCK, CONNECTION_UNLOCK, DBusList::data, dbus_connection_unref(), DBUS_DISPATCH_COMPLETE, DBUS_DISPATCH_DATA_REMAINS, DBUS_DISPATCH_NEED_MEMORY, DBUS_ERROR_UNKNOWN_METHOD, DBUS_HANDLER_RESULT_HANDLED, DBUS_HANDLER_RESULT_NEED_MEMORY, DBUS_HANDLER_RESULT_NOT_YET_HANDLED, dbus_message_get_interface(), dbus_message_get_member(), dbus_message_get_reply_serial(), dbus_message_get_signature(), dbus_message_get_type(), dbus_message_new_error(), DBUS_MESSAGE_TYPE_METHOD_CALL, dbus_message_unref(), dbus_pending_call_unref(), filter_list, DBusMessageFilter::function, NULL, objects, pending_replies, and DBusMessageFilter::user_data.

void dbus_connection_flush ( DBusConnection connection  ) 

Blocks until the outgoing message queue is empty.

Parameters:
connection the connection.

Definition at line 3255 of file dbus-connection.c.

References CONNECTION_LOCK, and NULL.

void dbus_connection_free_data_slot ( dbus_int32_t slot_p  ) 

Deallocates a global ID for connection data slots.

dbus_connection_get_data() and dbus_connection_set_data() may no longer be used with this slot. Existing data stored on existing DBusConnection objects will be freed when the connection is finalized, but may not be retrieved (and may only be replaced if someone else reallocates the slot). When the refcount on the passed-in slot reaches 0, it is set to -1.

Parameters:
slot_p address storing the slot to deallocate

Definition at line 5279 of file dbus-connection.c.

References _dbus_data_slot_allocator_free().

void dbus_connection_free_preallocated_send ( DBusConnection connection,
DBusPreallocatedSend preallocated 
)

Frees preallocated message-sending resources from dbus_connection_preallocate_send().

Should only be called if the preallocated resources are not used to send a message.

Parameters:
connection the connection
preallocated the resources

Definition at line 2877 of file dbus-connection.c.

References _dbus_counter_unref(), _dbus_list_free_link(), DBusPreallocatedSend::connection, DBusPreallocatedSend::counter_link, DBusList::data, dbus_free(), NULL, and DBusPreallocatedSend::queue_link.

void * dbus_connection_get_data ( DBusConnection connection,
dbus_int32_t  slot 
)

Retrieves data previously set with dbus_connection_set_data().

The slot must still be allocated (must not have been freed).

Parameters:
connection the connection
slot the slot to get data from
Returns:
the data, or NULL if not found

Definition at line 5340 of file dbus-connection.c.

References _dbus_data_slot_list_get(), CONNECTION_LOCK, CONNECTION_UNLOCK, NULL, and slot_list.

DBusDispatchStatus dbus_connection_get_dispatch_status ( DBusConnection connection  ) 

Gets the current state of the incoming message queue.

DBUS_DISPATCH_DATA_REMAINS indicates that the message queue may contain messages. DBUS_DISPATCH_COMPLETE indicates that the incoming queue is empty. DBUS_DISPATCH_NEED_MEMORY indicates that there could be data, but we can't know for sure without more memory.

To process the incoming message queue, use dbus_connection_dispatch() or (in rare cases) dbus_connection_pop_message().

Note, DBUS_DISPATCH_DATA_REMAINS really means that either we have messages in the queue, or we have raw bytes buffered up that need to be parsed. When these bytes are parsed, they may not add up to an entire message. Thus, it's possible to see a status of DBUS_DISPATCH_DATA_REMAINS but not have a message yet.

In particular this happens on initial connection, because all sorts of authentication protocol stuff has to be parsed before the first message arrives.

Parameters:
connection the connection.
Returns:
current dispatch status

Definition at line 3969 of file dbus-connection.c.

References CONNECTION_LOCK, CONNECTION_UNLOCK, DBUS_DISPATCH_COMPLETE, and NULL.

Referenced by dbus_connection_borrow_message(), and dbus_connection_pop_message().

dbus_bool_t dbus_connection_get_is_authenticated ( DBusConnection connection  ) 

Gets whether the connection was authenticated.

(Note that if the connection was authenticated then disconnected, this function still returns TRUE)

Parameters:
connection the connection
Returns:
TRUE if the connection was ever authenticated

Definition at line 2804 of file dbus-connection.c.

References _dbus_transport_get_is_authenticated(), CONNECTION_LOCK, CONNECTION_UNLOCK, FALSE, NULL, and transport.

dbus_bool_t dbus_connection_get_is_connected ( DBusConnection connection  ) 

Gets whether the connection is currently open.

A connection may become disconnected when the remote application closes its end, or exits; a connection may also be disconnected with dbus_connection_close().

There are not separate states for "closed" and "disconnected," the two terms are synonymous. This function should really be called get_is_open() but for historical reasons is not.

Parameters:
connection the connection.
Returns:
TRUE if the connection is still alive.

Definition at line 2782 of file dbus-connection.c.

References CONNECTION_LOCK, CONNECTION_UNLOCK, FALSE, and NULL.

long dbus_connection_get_max_message_size ( DBusConnection connection  ) 

Gets the value set by dbus_connection_set_max_message_size().

Parameters:
connection the connection
Returns:
the max size of a single message

Definition at line 5397 of file dbus-connection.c.

References _dbus_transport_get_max_message_size(), CONNECTION_LOCK, CONNECTION_UNLOCK, NULL, and transport.

long dbus_connection_get_max_received_size ( DBusConnection connection  ) 

Gets the value set by dbus_connection_set_max_received_size().

Parameters:
connection the connection
Returns:
the max size of all live messages

Definition at line 5453 of file dbus-connection.c.

References _dbus_transport_get_max_received_size(), CONNECTION_LOCK, CONNECTION_UNLOCK, NULL, and transport.

dbus_bool_t dbus_connection_get_object_path_data ( DBusConnection connection,
const char *  path,
void **  data_p 
)

Gets the user data passed to dbus_connection_register_object_path() or dbus_connection_register_fallback().

If nothing was registered at this path, the data is filled in with NULL.

Parameters:
connection the connection
path the path you registered with
data_p location to store the user data, or NULL
Returns:
FALSE if not enough memory

Definition at line 5181 of file dbus-connection.c.

References _dbus_decompose_path(), _dbus_object_tree_get_user_data_unlocked(), CONNECTION_LOCK, CONNECTION_UNLOCK, dbus_free_string_array(), FALSE, NULL, objects, and TRUE.

long dbus_connection_get_outgoing_size ( DBusConnection connection  ) 

Gets the approximate size in bytes of all messages in the outgoing message queue.

The size is approximate in that you shouldn't use it to decide how many bytes to read off the network or anything of that nature, as optimizations may choose to tell small white lies to avoid performance overhead.

Parameters:
connection the connection
Returns:
the number of bytes that have been queued up but not sent

Definition at line 5476 of file dbus-connection.c.

References _dbus_counter_get_value(), CONNECTION_LOCK, CONNECTION_UNLOCK, NULL, and outgoing_counter.

dbus_bool_t dbus_connection_get_socket ( DBusConnection connection,
int *  fd 
)

Gets the underlying Windows or UNIX socket file descriptor of the connection, if any.

DO NOT read or write to the file descriptor, or try to select() on it; use DBusWatch for main loop integration. Not all connections will have a socket. So for adding descriptors to the main loop, use dbus_watch_get_fd() and so forth.

If the connection is not socket-based, this function will return FALSE, even if the connection does have a file descriptor of some kind. i.e. this function always returns specifically a socket file descriptor.

Parameters:
connection the connection
fd return location for the file descriptor.
Returns:
TRUE if fd is successfully obtained.

Definition at line 4749 of file dbus-connection.c.

References _dbus_transport_get_socket_fd(), CONNECTION_LOCK, CONNECTION_UNLOCK, FALSE, NULL, and transport.

Referenced by dbus_connection_get_unix_fd().

dbus_bool_t dbus_connection_get_unix_fd ( DBusConnection connection,
int *  fd 
)

Get the UNIX file descriptor of the connection, if any.

This can be used for SELinux access control checks with getpeercon() for example. DO NOT read or write to the file descriptor, or try to select() on it; use DBusWatch for main loop integration. Not all connections will have a file descriptor. So for adding descriptors to the main loop, use dbus_watch_get_fd() and so forth.

If the connection is socket-based, you can also use dbus_connection_get_socket(), which will work on Windows too. This function always fails on Windows.

Right now the returned descriptor is always a socket, but that is not guaranteed.

Parameters:
connection the connection
fd return location for the file descriptor.
Returns:
TRUE if fd is successfully obtained.

Definition at line 4719 of file dbus-connection.c.

References dbus_connection_get_socket(), FALSE, NULL, and transport.

dbus_bool_t dbus_connection_get_unix_process_id ( DBusConnection connection,
unsigned long *  pid 
)

Gets the process ID of the connection if any.

Returns TRUE if the uid is filled in. Always returns FALSE prior to authenticating the connection.

Parameters:
connection the connection
pid return location for the process ID
Returns:
TRUE if uid is filled in with a valid process ID

Definition at line 4828 of file dbus-connection.c.

References _dbus_transport_get_is_authenticated(), _dbus_transport_get_unix_process_id(), CONNECTION_LOCK, CONNECTION_UNLOCK, FALSE, NULL, and transport.

dbus_bool_t dbus_connection_get_unix_user ( DBusConnection connection,
unsigned long *  uid 
)

Gets the UNIX user ID of the connection if known.

Returns TRUE if the uid is filled in. Always returns FALSE on non-UNIX platforms. Always returns FALSE prior to authenticating the connection.

The UID is only read by servers from clients; clients can't usually get the UID of servers, because servers do not authenticate to clients. The returned UID is the UID the connection authenticated as.

The message bus is a server and the apps connecting to the bus are clients.

You can ask the bus to tell you the UID of another connection though if you like; this is done with dbus_bus_get_unix_user().

Parameters:
connection the connection
uid return location for the user ID
Returns:
TRUE if uid is filled in with a valid user ID

Definition at line 4789 of file dbus-connection.c.

References _dbus_transport_get_is_authenticated(), _dbus_transport_get_unix_user(), CONNECTION_LOCK, CONNECTION_UNLOCK, FALSE, NULL, and transport.

dbus_bool_t dbus_connection_has_messages_to_send ( DBusConnection connection  ) 

Checks whether there are messages in the outgoing message queue.

Use dbus_connection_flush() to block until all outgoing messages have been written to the underlying transport (such as a socket).

Parameters:
connection the connection.
Returns:
TRUE if the outgoing queue is non-empty.

Definition at line 548 of file dbus-connection.c.

References _dbus_connection_has_messages_to_send_unlocked(), CONNECTION_LOCK, CONNECTION_UNLOCK, FALSE, and NULL.

dbus_bool_t dbus_connection_list_registered ( DBusConnection connection,
const char *  parent_path,
char ***  child_entries 
)

Lists the registered fallback handlers and object path handlers at the given parent_path.

The returned array should be freed with dbus_free_string_array().

Parameters:
connection the connection
parent_path the path to list the child handlers of
child_entries returns NULL-terminated array of children
Returns:
FALSE if no memory to allocate the child entries

Definition at line 5218 of file dbus-connection.c.

References _dbus_decompose_path(), _dbus_object_tree_list_registered_and_unlock(), CONNECTION_LOCK, dbus_free_string_array(), FALSE, NULL, and objects.

DBusConnection * dbus_connection_open ( const char *  address,
DBusError error 
)

Gets a connection to a remote address.

If a connection to the given address already exists, returns the existing connection with its reference count incremented. Otherwise, returns a new connection and saves the new connection for possible re-use if a future call to dbus_connection_open() asks to connect to the same server.

Use dbus_connection_open_private() to get a dedicated connection not shared with other callers of dbus_connection_open().

If the open fails, the function returns NULL, and provides a reason for the failure in the error parameter. Pass NULL for the error parameter if you aren't interested in the reason for failure.

Because this connection is shared, no user of the connection may call dbus_connection_close(). However, when you are done with the connection you should call dbus_connection_unref().

Note:
Prefer dbus_connection_open() to dbus_connection_open_private() unless you have good reason; connections are expensive enough that it's wasteful to create lots of connections to the same server.
Parameters:
address the address.
error address where an error can be returned.
Returns:
new connection, or NULL on failure.

Definition at line 2399 of file dbus-connection.c.

References NULL, and TRUE.

DBusConnection * dbus_connection_open_private ( const char *  address,
DBusError error 
)

Opens a new, dedicated connection to a remote address.

Unlike dbus_connection_open(), always creates a new connection. This connection will not be saved or recycled by libdbus.

If the open fails, the function returns NULL, and provides a reason for the failure in the error parameter. Pass NULL for the error parameter if you aren't interested in the reason for failure.

When you are done with this connection, you must dbus_connection_close() to disconnect it, and dbus_connection_unref() to free the connection object.

(The dbus_connection_close() can be skipped if the connection is already known to be disconnected, for example if you are inside a handler for the Disconnected signal.)

Note:
Prefer dbus_connection_open() to dbus_connection_open_private() unless you have good reason; connections are expensive enough that it's wasteful to create lots of connections to the same server.
Parameters:
address the address.
error address where an error can be returned.
Returns:
new connection, or NULL on failure.

Definition at line 2442 of file dbus-connection.c.

References FALSE, and NULL.

DBusMessage * dbus_connection_pop_message ( DBusConnection connection  ) 

Returns the first-received message from the incoming message queue, removing it from the queue.

The caller owns a reference to the returned message. If the queue is empty, returns NULL.

This function bypasses any message handlers that are registered, and so using it is usually wrong. Instead, let the main loop invoke dbus_connection_dispatch(). Popping messages manually is only useful in very simple programs that don't share a DBusConnection with any libraries or other modules.

There is a lock that covers all ways of accessing the incoming message queue, so dbus_connection_dispatch(), dbus_connection_pop_message(), dbus_connection_borrow_message(), etc. will all block while one of the others in the group is running.

Parameters:
connection the connection.
Returns:
next message in the incoming queue.

Definition at line 3681 of file dbus-connection.c.

References CONNECTION_LOCK, dbus_connection_get_dispatch_status(), DBUS_DISPATCH_DATA_REMAINS, and NULL.

DBusPreallocatedSend * dbus_connection_preallocate_send ( DBusConnection connection  ) 

Preallocates resources needed to send a message, allowing the message to be sent without the possibility of memory allocation failure.

Allows apps to create a future guarantee that they can send a message regardless of memory shortages.

Parameters:
connection the connection we're preallocating for.
Returns:
the preallocated resources, or NULL

Definition at line 2851 of file dbus-connection.c.

References CONNECTION_LOCK, CONNECTION_UNLOCK, and NULL.

dbus_bool_t dbus_connection_read_write ( DBusConnection connection,
int  timeout_milliseconds 
)

This function is intended for use with applications that don't want to write a main loop and deal with DBusWatch and DBusTimeout.

See also dbus_connection_read_write_dispatch().

As long as the connection is open, this function will block until it can read or write, then read or write, then return TRUE.

If the connection is closed, the function returns FALSE.

The return value indicates whether reading or writing is still possible, i.e. whether the connection is connected.

Note that even after disconnection, messages may remain in the incoming queue that need to be processed. dbus_connection_read_write_dispatch() dispatches incoming messages for you; with dbus_connection_read_write() you have to arrange to drain the incoming queue yourself.

Parameters:
connection the connection
timeout_milliseconds max time to block or -1 for infinite
Returns:
TRUE if still connected

Definition at line 3405 of file dbus-connection.c.

References FALSE, and NULL.

dbus_bool_t dbus_connection_read_write_dispatch ( DBusConnection connection,
int  timeout_milliseconds 
)

This function is intended for use with applications that don't want to write a main loop and deal with DBusWatch and DBusTimeout.

An example usage would be:

   while (dbus_connection_read_write_dispatch (connection, -1))
     ; // empty loop body

In this usage you would normally have set up a filter function to look at each message as it is dispatched. The loop terminates when the last message from the connection (the disconnected signal) is processed.

If there are messages to dispatch, this function will dbus_connection_dispatch() once, and return. If there are no messages to dispatch, this function will block until it can read or write, then read or write, then return.

The way to think of this function is that it either makes some sort of progress, or it blocks. Note that, while it is blocked on I/O, it cannot be interrupted (even by other threads), which makes this function unsuitable for applications that do more than just react to received messages.

The return value indicates whether the disconnect message has been processed, NOT whether the connection is connected. This is important because even after disconnecting, you want to process any messages you received prior to the disconnect.

Parameters:
connection the connection
timeout_milliseconds max time to block or -1 for infinite
Returns:
TRUE if the disconnect message has not been processed

Definition at line 3373 of file dbus-connection.c.

References FALSE, NULL, and TRUE.

DBusConnection * dbus_connection_ref ( DBusConnection connection  ) 

Increments the reference count of a DBusConnection.

Parameters:
connection the connection.
Returns:
the connection.

Definition at line 2464 of file dbus-connection.c.

References _dbus_assert, _dbus_atomic_inc(), _dbus_current_generation, CONNECTION_LOCK, CONNECTION_UNLOCK, generation, NULL, refcount, and DBusAtomic::value.

dbus_bool_t dbus_connection_register_fallback ( DBusConnection connection,
const char *  path,
const DBusObjectPathVTable vtable,
void *  user_data 
)

Registers a fallback handler for a given subsection of the object hierarchy.

The given vtable handles messages at or below the given path. You can use this to establish a default message handling policy for a whole "subdirectory."

Parameters:
connection the connection
path a '/' delimited string of path elements
vtable the virtual table
user_data data to pass to functions in the vtable
Returns:
FALSE if not enough memory

Definition at line 5109 of file dbus-connection.c.

References _dbus_decompose_path(), _dbus_object_tree_register(), CONNECTION_LOCK, CONNECTION_UNLOCK, dbus_free_string_array(), FALSE, NULL, objects, and TRUE.

dbus_bool_t dbus_connection_register_object_path ( DBusConnection connection,
const char *  path,
const DBusObjectPathVTable vtable,
void *  user_data 
)

Registers a handler for a given path in the object hierarchy.

The given vtable handles messages sent to exactly the given path.

Parameters:
connection the connection
path a '/' delimited string of path elements
vtable the virtual table
user_data data to pass to functions in the vtable
Returns:
FALSE if not enough memory

Definition at line 5066 of file dbus-connection.c.

References _dbus_decompose_path(), _dbus_object_tree_register(), CONNECTION_LOCK, CONNECTION_UNLOCK, dbus_free_string_array(), FALSE, NULL, and objects.

void dbus_connection_remove_filter ( DBusConnection connection,
DBusHandleMessageFunction  function,
void *  user_data 
)

Removes a previously-added message filter.

It is a programming error to call this function for a handler that has not been added as a filter. If the given handler was added more than once, only one instance of it will be removed (the most recently-added instance).

Parameters:
connection the connection
function the handler to remove
user_data user data for the handler to remove

Definition at line 5003 of file dbus-connection.c.

References _dbus_list_get_last_link(), _dbus_list_get_prev_link, _dbus_list_remove_link(), _dbus_warn(), CONNECTION_LOCK, CONNECTION_UNLOCK, DBusList::data, filter_list, DBusMessageFilter::free_user_data_function, DBusMessageFilter::function, NULL, and DBusMessageFilter::user_data.

void dbus_connection_return_message ( DBusConnection connection,
DBusMessage message 
)

Used to return a message after peeking at it using dbus_connection_borrow_message().

Only called if message from dbus_connection_borrow_message() was non-NULL.

Parameters:
connection the connection
message the message from dbus_connection_borrow_message()

Definition at line 3503 of file dbus-connection.c.

References _dbus_assert, CONNECTION_LOCK, dispatch_acquired, message_borrowed, and NULL.

dbus_bool_t dbus_connection_send ( DBusConnection connection,
DBusMessage message,
dbus_uint32_t serial 
)

Adds a message to the outgoing message queue.

Does not block to write the message to the network; that happens asynchronously. To force the message to be written, call dbus_connection_flush(). Because this only queues the message, the only reason it can fail is lack of memory. Even if the connection is disconnected, no error will be returned.

If the function fails due to lack of memory, it returns FALSE. The function will never fail for other reasons; even if the connection is disconnected, you can queue an outgoing message, though obviously it won't be sent.

The message serial is used by the remote application to send a reply; see dbus_message_get_serial() or the D-Bus specification.

Parameters:
connection the connection.
message the message to write.
serial return location for message serial, or NULL if you don't care
Returns:
TRUE on success.

Definition at line 2967 of file dbus-connection.c.

References _dbus_connection_send_and_unlock(), CONNECTION_LOCK, FALSE, and NULL.

void dbus_connection_send_preallocated ( DBusConnection connection,
DBusPreallocatedSend preallocated,
DBusMessage message,
dbus_uint32_t client_serial 
)

Sends a message using preallocated resources.

This function cannot fail. It works identically to dbus_connection_send() in other respects. Preallocated resources comes from dbus_connection_preallocate_send(). This function "consumes" the preallocated resources, they need not be freed separately.

Parameters:
connection the connection
preallocated the preallocated resources
message the message to send
client_serial return location for client serial assigned to the message

Definition at line 2903 of file dbus-connection.c.

References DBusPreallocatedSend::connection, CONNECTION_LOCK, dbus_message_get_interface(), dbus_message_get_member(), dbus_message_get_type(), DBUS_MESSAGE_TYPE_METHOD_CALL, DBUS_MESSAGE_TYPE_SIGNAL, and NULL.

dbus_bool_t dbus_connection_send_with_reply ( DBusConnection connection,
DBusMessage message,
DBusPendingCall **  pending_return,
int  timeout_milliseconds 
)

Queues a message to send, as with dbus_connection_send(), but also returns a DBusPendingCall used to receive a reply to the message.

If no reply is received in the given timeout_milliseconds, this function expires the pending reply and generates a synthetic error reply (generated in-process, not by the remote application) indicating that a timeout occurred.

A DBusPendingCall will see a reply message before any filters or registered object path handlers. See dbus_connection_dispatch() for details on when handlers are run.

A DBusPendingCall will always see exactly one reply message, unless it's cancelled with dbus_pending_call_cancel().

If NULL is passed for the pending_return, the DBusPendingCall will still be generated internally, and used to track the message reply timeout. This means a timeout error will occur if no reply arrives, unlike with dbus_connection_send().

If -1 is passed for the timeout, a sane default timeout is used. -1 is typically the best value for the timeout for this reason, unless you want a very short or very long timeout. There is no way to avoid a timeout entirely, other than passing INT_MAX for the timeout to mean "very long timeout." libdbus clamps an INT_MAX timeout down to a few hours timeout though.

Warning:
if the connection is disconnected, the DBusPendingCall will be set to NULL, so be careful with this.
Parameters:
connection the connection
message the message to send
pending_return return location for a DBusPendingCall object, or NULL if connection is disconnected
timeout_milliseconds timeout in milliseconds or -1 for default
Returns:
FALSE if no memory, TRUE otherwise.

Definition at line 3043 of file dbus-connection.c.

References _dbus_message_set_serial(), _dbus_pending_call_new_unlocked(), _dbus_pending_call_set_timeout_error_unlocked(), CONNECTION_LOCK, CONNECTION_UNLOCK, dbus_message_get_serial(), dbus_pending_call_unref(), FALSE, NULL, and TRUE.

Referenced by dbus_connection_send_with_reply_and_block().

DBusMessage * dbus_connection_send_with_reply_and_block ( DBusConnection connection,
DBusMessage message,
int  timeout_milliseconds,
DBusError error 
)

Sends a message and blocks a certain time period while waiting for a reply.

This function does not reenter the main loop, i.e. messages other than the reply are queued up but not processed. This function is used to invoke method calls on a remote object.

If a normal reply is received, it is returned, and removed from the incoming message queue. If it is not received, NULL is returned and the error is set to DBUS_ERROR_NO_REPLY. If an error reply is received, it is converted to a DBusError and returned as an error, then the reply message is deleted and NULL is returned. If something else goes wrong, result is set to whatever is appropriate, such as DBUS_ERROR_NO_MEMORY or DBUS_ERROR_DISCONNECTED.

Warning:
While this function blocks the calling thread will not be processing the incoming message queue. This means you can end up deadlocked if the application you're talking to needs you to reply to a method. To solve this, either avoid the situation, block in a separate thread from the main connection-dispatching thread, or use dbus_pending_call_set_notify() to avoid blocking.
Parameters:
connection the connection
message the message to send
timeout_milliseconds timeout in milliseconds or -1 for default
error return location for error message
Returns:
the message that is the reply or NULL with an error code if the function fails.

Definition at line 3164 of file dbus-connection.c.

References _dbus_assert, dbus_connection_send_with_reply(), DBUS_ERROR_DISCONNECTED, dbus_message_unref(), dbus_pending_call_block(), dbus_pending_call_steal_reply(), dbus_pending_call_unref(), dbus_set_error(), dbus_set_error_from_message(), and NULL.

Referenced by dbus_bus_get_unix_user(), dbus_bus_name_has_owner(), dbus_bus_register(), dbus_bus_release_name(), dbus_bus_request_name(), and dbus_bus_start_service_by_name().

void dbus_connection_set_change_sigpipe ( dbus_bool_t  will_modify_sigpipe  ) 

This function sets a global flag for whether dbus_connection_new() will set SIGPIPE behavior to SIG_IGN.

Parameters:
will_modify_sigpipe TRUE to allow sigpipe to be set to SIG_IGN

Definition at line 5365 of file dbus-connection.c.

References FALSE.

dbus_bool_t dbus_connection_set_data ( DBusConnection connection,
dbus_int32_t  slot,
void *  data,
DBusFreeFunction  free_data_func 
)

Stores a pointer on a DBusConnection, along with an optional function to be used for freeing the data when the data is set again, or when the connection is finalized.

The slot number must have been allocated with dbus_connection_allocate_data_slot().

Parameters:
connection the connection
slot the slot number
data the data to store
free_data_func finalizer function for the data
Returns:
TRUE if there was enough memory to store the data

Definition at line 5300 of file dbus-connection.c.

References _dbus_data_slot_list_set(), CONNECTION_LOCK, CONNECTION_UNLOCK, FALSE, NULL, and slot_list.

void dbus_connection_set_dispatch_status_function ( DBusConnection connection,
DBusDispatchStatusFunction  function,
void *  data,
DBusFreeFunction  free_data_function 
)

Set a function to be invoked when the dispatch status changes.

If the dispatch status is DBUS_DISPATCH_DATA_REMAINS, then dbus_connection_dispatch() needs to be called to process incoming messages. However, dbus_connection_dispatch() MUST NOT BE CALLED from inside the DBusDispatchStatusFunction. Indeed, almost any reentrancy in this function is a bad idea. Instead, the DBusDispatchStatusFunction should simply save an indication that messages should be dispatched later, when the main loop is re-entered.

If you don't set a dispatch status function, you have to be sure to dispatch on every iteration of your main loop, especially if dbus_watch_handle() or dbus_timeout_handle() were called.

Parameters:
connection the connection
function function to call on dispatch status changes
data data for function
free_data_function free the function data

Definition at line 4674 of file dbus-connection.c.

References CONNECTION_LOCK, CONNECTION_UNLOCK, dispatch_status_data, dispatch_status_function, free_dispatch_status_data, and NULL.

void dbus_connection_set_exit_on_disconnect ( DBusConnection connection,
dbus_bool_t  exit_on_disconnect 
)

Set whether _exit() should be called when the connection receives a disconnect signal.

The call to _exit() comes after any handlers for the disconnect signal run; handlers can cancel the exit by calling this function.

By default, exit_on_disconnect is FALSE; but for message bus connections returned from dbus_bus_get() it will be toggled on by default.

Parameters:
connection the connection
exit_on_disconnect TRUE if _exit() should be called after a disconnect signal

Definition at line 2831 of file dbus-connection.c.

References CONNECTION_LOCK, CONNECTION_UNLOCK, exit_on_disconnect, FALSE, and NULL.

void dbus_connection_set_max_message_size ( DBusConnection connection,
long  size 
)

Specifies the maximum size message this connection is allowed to receive.

Larger messages will result in disconnecting the connection.

Parameters:
connection a DBusConnection
size maximum message size the connection can receive, in bytes

Definition at line 5379 of file dbus-connection.c.

References _dbus_transport_set_max_message_size(), CONNECTION_LOCK, CONNECTION_UNLOCK, NULL, and transport.

void dbus_connection_set_max_received_size ( DBusConnection connection,
long  size 
)

Sets the maximum total number of bytes that can be used for all messages received on this connection.

Messages count toward the maximum until they are finalized. When the maximum is reached, the connection will not read more data until some messages are finalized.

The semantics of the maximum are: if outstanding messages are already above the maximum, additional messages will not be read. The semantics are not: if the next message would cause us to exceed the maximum, we don't read it. The reason is that we don't know the size of a message until after we read it.

Thus, the max live messages size can actually be exceeded by up to the maximum size of a single message.

Also, if we read say 1024 bytes off the wire in a single read(), and that contains a half-dozen small messages, we may exceed the size max by that amount. But this should be inconsequential.

This does imply that we can't call read() with a buffer larger than we're willing to exceed this limit by.

Parameters:
connection the connection
size the maximum size in bytes of all outstanding messages

Definition at line 5435 of file dbus-connection.c.

References _dbus_transport_set_max_received_size(), CONNECTION_LOCK, CONNECTION_UNLOCK, NULL, and transport.

void dbus_connection_set_route_peer_messages ( DBusConnection connection,
dbus_bool_t  value 
)

Normally DBusConnection automatically handles all messages to the org.freedesktop.DBus.Peer interface.

However, the message bus wants to be able to route methods on that interface through the bus and to other applications. If routing peer messages is enabled, then messages with the org.freedesktop.DBus.Peer interface that also have a bus destination name set will not be automatically handled by the DBusConnection and instead will be dispatched normally to the application.

If a normal application sets this flag, it can break things badly. So don't set this unless you are the message bus.

Parameters:
connection the connection
value TRUE to pass through org.freedesktop.DBus.Peer messages with a bus name set

Definition at line 4919 of file dbus-connection.c.

References CONNECTION_LOCK, CONNECTION_UNLOCK, NULL, route_peer_messages, and TRUE.

dbus_bool_t dbus_connection_set_timeout_functions ( DBusConnection connection,
DBusAddTimeoutFunction  add_function,
DBusRemoveTimeoutFunction  remove_function,
DBusTimeoutToggledFunction  toggled_function,
void *  data,
DBusFreeFunction  free_data_function 
)

Sets the timeout functions for the connection.

These functions are responsible for making the application's main loop aware of timeouts. When using Qt, typically the DBusAddTimeoutFunction would create a QTimer. When using GLib, the DBusAddTimeoutFunction would call g_timeout_add.

The DBusTimeoutToggledFunction notifies the application that the timeout has been enabled or disabled. Call dbus_timeout_get_enabled() to check this. A disabled timeout should have no effect, and enabled timeout should be added to the main loop. This feature is used instead of simply adding/removing the timeout because enabling/disabling can be done without memory allocation. With Qt, QTimer::start() and QTimer::stop() can be used to enable and disable. The toggled function may be NULL if a main loop re-queries dbus_timeout_get_enabled() every time anyway. Whenever a timeout is toggled, its interval may change.

The DBusTimeout can be queried for the timer interval using dbus_timeout_get_interval(). dbus_timeout_handle() should be called repeatedly, each time the interval elapses, starting after it has elapsed once. The timeout stops firing when it is removed with the given remove_function. The timer interval may change whenever the timeout is added, removed, or toggled.

Parameters:
connection the connection.
add_function function to add a timeout.
remove_function function to remove a timeout.
toggled_function function to notify of enable/disable
data data to pass to add_function and remove_function.
free_data_function function to be called to free the data.
Returns:
FALSE on failure (no memory)

Definition at line 4569 of file dbus-connection.c.

References _dbus_connection_ref_unlocked(), _dbus_timeout_list_set_functions(), _dbus_warn_check_failed(), CONNECTION_LOCK, CONNECTION_UNLOCK, dbus_connection_unref(), FALSE, NULL, and timeouts.

void dbus_connection_set_unix_user_function ( DBusConnection connection,
DBusAllowUnixUserFunction  function,
void *  data,
DBusFreeFunction  free_data_function 
)

Sets a predicate function used to determine whether a given user ID is allowed to connect.

When an incoming connection has authenticated with a particular user ID, this function is called; if it returns TRUE, the connection is allowed to proceed, otherwise the connection is disconnected.

If the function is set to NULL (as it is by default), then only the same UID as the server process will be allowed to connect.

On Windows, the function will be set and its free_data_function will be invoked when the connection is freed or a new function is set. However, the function will never be called, because there are no UNIX user ids to pass to it.

Todo:
add a Windows API analogous to dbus_connection_set_unix_user_function()
Parameters:
connection the connection
function the predicate
data data to pass to the predicate
free_data_function function to free the data

Definition at line 4880 of file dbus-connection.c.

References _dbus_transport_set_unix_user_function(), CONNECTION_LOCK, CONNECTION_UNLOCK, NULL, and transport.

void dbus_connection_set_wakeup_main_function ( DBusConnection connection,
DBusWakeupMainFunction  wakeup_main_function,
void *  data,
DBusFreeFunction  free_data_function 
)

Sets the mainloop wakeup function for the connection.

This function is responsible for waking up the main loop (if its sleeping in another thread) when some some change has happened to the connection that the mainloop needs to reconsider (e.g. a message has been queued for writing). When using Qt, this typically results in a call to QEventLoop::wakeUp(). When using GLib, it would call g_main_context_wakeup().

Parameters:
connection the connection.
wakeup_main_function function to wake up the mainloop
data data to pass wakeup_main_function
free_data_function function to be called to free the data.

Definition at line 4628 of file dbus-connection.c.

References CONNECTION_LOCK, CONNECTION_UNLOCK, free_wakeup_main_data, NULL, wakeup_main_data, and wakeup_main_function.

dbus_bool_t dbus_connection_set_watch_functions ( DBusConnection connection,
DBusAddWatchFunction  add_function,
DBusRemoveWatchFunction  remove_function,
DBusWatchToggledFunction  toggled_function,
void *  data,
DBusFreeFunction  free_data_function 
)

Sets the watch functions for the connection.

These functions are responsible for making the application's main loop aware of file descriptors that need to be monitored for events, using select() or poll(). When using Qt, typically the DBusAddWatchFunction would create a QSocketNotifier. When using GLib, the DBusAddWatchFunction could call g_io_add_watch(), or could be used as part of a more elaborate GSource. Note that when a watch is added, it may not be enabled.

The DBusWatchToggledFunction notifies the application that the watch has been enabled or disabled. Call dbus_watch_get_enabled() to check this. A disabled watch should have no effect, and enabled watch should be added to the main loop. This feature is used instead of simply adding/removing the watch because enabling/disabling can be done without memory allocation. The toggled function may be NULL if a main loop re-queries dbus_watch_get_enabled() every time anyway.

The DBusWatch can be queried for the file descriptor to watch using dbus_watch_get_fd(), and for the events to watch for using dbus_watch_get_flags(). The flags returned by dbus_watch_get_flags() will only contain DBUS_WATCH_READABLE and DBUS_WATCH_WRITABLE, never DBUS_WATCH_HANGUP or DBUS_WATCH_ERROR; all watches implicitly include a watch for hangups, errors, and other exceptional conditions.

Once a file descriptor becomes readable or writable, or an exception occurs, dbus_watch_handle() should be called to notify the connection of the file descriptor's condition.

dbus_watch_handle() cannot be called during the DBusAddWatchFunction, as the connection will not be ready to handle that watch yet.

It is not allowed to reference a DBusWatch after it has been passed to remove_function.

If FALSE is returned due to lack of memory, the failure may be due to a FALSE return from the new add_function. If so, the add_function may have been called successfully one or more times, but the remove_function will also have been called to remove any successful adds. i.e. if FALSE is returned the net result should be that dbus_connection_set_watch_functions() has no effect, but the add_function and remove_function may have been called.

Todo:
We need to drop the lock when we call the add/remove/toggled functions which can be a side effect of setting the watch functions.
Parameters:
connection the connection.
add_function function to begin monitoring a new descriptor.
remove_function function to stop monitoring a descriptor.
toggled_function function to notify of enable/disable
data data to pass to add_function and remove_function.
free_data_function function to be called to free the data.
Returns:
FALSE on failure (no memory)

Definition at line 4487 of file dbus-connection.c.

References _dbus_connection_ref_unlocked(), _dbus_warn_check_failed(), _dbus_watch_list_set_functions(), CONNECTION_LOCK, CONNECTION_UNLOCK, dbus_connection_unref(), FALSE, NULL, and watches.

void dbus_connection_steal_borrowed_message ( DBusConnection connection,
DBusMessage message 
)

Used to keep a message after peeking at it using dbus_connection_borrow_message().

Before using this function, see the caveats/warnings in the documentation for dbus_connection_pop_message().

Parameters:
connection the connection
message the message from dbus_connection_borrow_message()

Definition at line 3535 of file dbus-connection.c.

References _dbus_assert, _dbus_list_pop_first(), CONNECTION_LOCK, dispatch_acquired, incoming_messages, message_borrowed, n_incoming, and NULL.

void dbus_connection_unref ( DBusConnection connection  ) 

Decrements the reference count of a DBusConnection, and finalizes it if the count reaches zero.

Note: it is a bug to drop the last reference to a connection that is still connected.

For shared connections, libdbus will own a reference as long as the connection is connected, so you can know that either you don't have the last reference, or it's OK to drop the last reference. Most connections are shared. dbus_connection_open() and dbus_bus_get() return shared connections.

For private connections, the creator of the connection must arrange for dbus_connection_close() to be called prior to dropping the last reference. Private connections come from dbus_connection_open_private() or dbus_bus_get_private().

Parameters:
connection the connection.

Definition at line 2610 of file dbus-connection.c.

References _dbus_assert, _dbus_atomic_dec(), _dbus_current_generation, _dbus_transport_get_is_connected(), _dbus_warn_check_failed(), CONNECTION_LOCK, CONNECTION_UNLOCK, generation, NULL, refcount, shareable, transport, and DBusAtomic::value.

Referenced by _dbus_object_tree_unregister_and_unlock(), dbus_connection_dispatch(), dbus_connection_set_timeout_functions(), and dbus_connection_set_watch_functions().

dbus_bool_t dbus_connection_unregister_object_path ( DBusConnection connection,
const char *  path 
)

Unregisters the handler registered with exactly the given path.

It's a bug to call this function for a path that isn't registered. Can unregister both fallback paths and object paths.

Parameters:
connection the connection
path a '/' delimited string of path elements
Returns:
FALSE if not enough memory

Definition at line 5149 of file dbus-connection.c.

References _dbus_decompose_path(), _dbus_object_tree_unregister_and_unlock(), CONNECTION_LOCK, dbus_free_string_array(), FALSE, NULL, objects, and TRUE.


Generated on Fri Sep 21 18:12:14 2007 for D-Bus by  doxygen 1.5.1