dbus-watch.c
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
/* dbus-watch.c DBusWatch implementation
*
* Copyright (C) 2002, 2003 Red Hat Inc.
*
* SPDX-License-Identifier: AFL-2.1 OR GPL-2.0-or-later
*
* Licensed under the Academic Free License version 2.1
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <config.h>
#include "dbus-internals.h"
#include "dbus-watch.h"
#include "dbus-list.h"
struct DBusWatch
{
int refcount;
DBusPollable fd;
unsigned int flags;
DBusWatchHandler handler;
void *handler_data;
DBusFreeFunction free_handler_data_function;
void *data;
DBusFreeFunction free_data_function;
unsigned int enabled : 1;
unsigned int oom_last_time : 1;
};
dbus_bool_t
_dbus_watch_get_enabled (DBusWatch *watch)
{
return watch->enabled;
}
dbus_bool_t
_dbus_watch_get_oom_last_time (DBusWatch *watch)
{
return watch->oom_last_time;
}
void
_dbus_watch_set_oom_last_time (DBusWatch *watch,
dbus_bool_t oom)
{
watch->oom_last_time = oom;
}
DBusWatch*
_dbus_watch_new (DBusPollable fd,
unsigned int flags,
dbus_bool_t enabled,
DBusWatchHandler handler,
void *data,
DBusFreeFunction free_data_function)
{
DBusWatch *watch;
#define VALID_WATCH_FLAGS (DBUS_WATCH_WRITABLE | DBUS_WATCH_READABLE)
_dbus_assert ((flags & VALID_WATCH_FLAGS) == flags);
watch = dbus_new0 (DBusWatch, 1);
if (watch == NULL)
return NULL;
watch->refcount = 1;
watch->fd = fd;
watch->flags = flags;
watch->enabled = enabled;
watch->handler = handler;
watch->handler_data = data;
watch->free_handler_data_function = free_data_function;
return watch;
}
DBusWatch *
_dbus_watch_ref (DBusWatch *watch)
{
watch->refcount += 1;
return watch;
}
void
_dbus_watch_unref (DBusWatch *watch)
{
_dbus_assert (watch != NULL);
_dbus_assert (watch->refcount > 0);
watch->refcount -= 1;
if (watch->refcount == 0)
{
if (_dbus_pollable_is_valid (watch->fd))
_dbus_warn ("this watch should have been invalidated");
dbus_watch_set_data (watch, NULL, NULL); /* call free_data_function */
if (watch->free_handler_data_function)
(* watch->free_handler_data_function) (watch->handler_data);
dbus_free (watch);
}
}
void
_dbus_watch_invalidate (DBusWatch *watch)
{
_dbus_pollable_invalidate (&watch->fd);
watch->flags = 0;
}
void
_dbus_watch_sanitize_condition (DBusWatch *watch,
unsigned int *condition)
{
if (!(watch->flags & DBUS_WATCH_READABLE))
*condition &= ~DBUS_WATCH_READABLE;
if (!(watch->flags & DBUS_WATCH_WRITABLE))
*condition &= ~DBUS_WATCH_WRITABLE;
}
struct DBusWatchList
{
DBusList *watches;
DBusAddWatchFunction add_watch_function;
DBusRemoveWatchFunction remove_watch_function;
DBusWatchToggledFunction watch_toggled_function;
void *watch_data;
DBusFreeFunction watch_free_data_function;
};
DBusWatchList*
_dbus_watch_list_new (void)
{
DBusWatchList *watch_list;
watch_list = dbus_new0 (DBusWatchList, 1);
if (watch_list == NULL)
return NULL;
return watch_list;
}
void
_dbus_watch_list_free (DBusWatchList *watch_list)
{
/* free watch_data and removes watches as a side effect */
_dbus_watch_list_set_functions (watch_list,
NULL, NULL, NULL, NULL, NULL);
_dbus_list_clear_full (&watch_list->watches,
(DBusFreeFunction) _dbus_watch_unref);
dbus_free (watch_list);
}
#ifdef DBUS_ENABLE_VERBOSE_MODE
static const char*
watch_flags_to_string (int flags)
{
const char *watch_type;
if ((flags & DBUS_WATCH_READABLE) &&
(flags & DBUS_WATCH_WRITABLE))
watch_type = "readwrite";
else if (flags & DBUS_WATCH_READABLE)
watch_type = "read";
else if (flags & DBUS_WATCH_WRITABLE)
watch_type = "write";
else
watch_type = "not read or write";
return watch_type;
}
#endif /* DBUS_ENABLE_VERBOSE_MODE */
dbus_bool_t
_dbus_watch_list_set_functions (DBusWatchList *watch_list,
DBusAddWatchFunction add_function,
DBusRemoveWatchFunction remove_function,
DBusWatchToggledFunction toggled_function,
void *data,
DBusFreeFunction free_data_function)
{
/* Add watches with the new watch function, failing on OOM */
if (add_function != NULL)
{
DBusList *link;
link = _dbus_list_get_first_link (&watch_list->watches);
while (link != NULL)
{
DBusList *next = _dbus_list_get_next_link (&watch_list->watches,
link);
#ifdef DBUS_ENABLE_VERBOSE_MODE
DBusWatch *watch = link->data;
_dbus_verbose ("Adding a %s watch on fd %" DBUS_POLLABLE_FORMAT " using newly-set add watch function\n",
watch_flags_to_string (dbus_watch_get_flags (link->data)),
_dbus_pollable_printable (watch->fd));
#endif
if (!(* add_function) (link->data, data))
{
/* remove it all again and return FALSE */
DBusList *link2;
link2 = _dbus_list_get_first_link (&watch_list->watches);
while (link2 != link)
{
DBusList *next2 = _dbus_list_get_next_link (&watch_list->watches,
link2);
#ifdef DBUS_ENABLE_VERBOSE_MODE
DBusWatch *watch2 = link2->data;
_dbus_verbose ("Removing watch on fd %" DBUS_POLLABLE_FORMAT " using newly-set remove function because initial add failed\n",
_dbus_pollable_printable (watch2->fd));
#endif
(* remove_function) (link2->data, data);
link2 = next2;
}
return FALSE;
}
link = next;
}
}
/* Remove all current watches from previous watch handlers */
if (watch_list->remove_watch_function != NULL)
{
_dbus_verbose ("Removing all pre-existing watches\n");
_dbus_list_foreach (&watch_list->watches,
(DBusForeachFunction) watch_list->remove_watch_function,
watch_list->watch_data);
}
if (watch_list->watch_free_data_function != NULL)
(* watch_list->watch_free_data_function) (watch_list->watch_data);
watch_list->add_watch_function = add_function;
watch_list->remove_watch_function = remove_function;
watch_list->watch_toggled_function = toggled_function;
watch_list->watch_data = data;
watch_list->watch_free_data_function = free_data_function;
return TRUE;
}
dbus_bool_t
_dbus_watch_list_add_watch (DBusWatchList *watch_list,
DBusWatch *watch)
{
if (!_dbus_list_append (&watch_list->watches, watch))
return FALSE;
_dbus_watch_ref (watch);
if (watch_list->add_watch_function != NULL)
{
_dbus_verbose ("Adding watch on fd %" DBUS_POLLABLE_FORMAT "\n",
_dbus_pollable_printable (watch->fd));
if (!(* watch_list->add_watch_function) (watch,
watch_list->watch_data))
{
_dbus_list_remove_last (&watch_list->watches, watch);
_dbus_watch_unref (watch);
return FALSE;
}
}
return TRUE;
}
void
_dbus_watch_list_remove_watch (DBusWatchList *watch_list,
DBusWatch *watch)
{
if (!_dbus_list_remove (&watch_list->watches, watch))
_dbus_assert_not_reached ("Nonexistent watch was removed");
if (watch_list->remove_watch_function != NULL)
{
_dbus_verbose ("Removing watch on fd %" DBUS_POLLABLE_FORMAT "\n",
_dbus_pollable_printable (watch->fd));
(* watch_list->remove_watch_function) (watch,
watch_list->watch_data);
}
_dbus_watch_unref (watch);
}
void
_dbus_watch_list_toggle_watch (DBusWatchList *watch_list,
DBusWatch *watch,
dbus_bool_t enabled)
{
enabled = !!enabled;
if (enabled == watch->enabled)
return;
watch->enabled = enabled;
if (watch_list->watch_toggled_function != NULL)
{
_dbus_verbose ("Toggling watch %p on fd %" DBUS_POLLABLE_FORMAT " to %d\n",
watch,
_dbus_pollable_printable (watch->fd),
watch->enabled);
(* watch_list->watch_toggled_function) (watch,
watch_list->watch_data);
}
}
void
_dbus_watch_list_toggle_all_watches (DBusWatchList *watch_list,
dbus_bool_t enabled)
{
DBusList *link;
for (link = _dbus_list_get_first_link (&watch_list->watches);
link != NULL;
link = _dbus_list_get_next_link (&watch_list->watches, link))
{
_dbus_watch_list_toggle_watch (watch_list, link->data, enabled);
}
}
void
_dbus_watch_set_handler (DBusWatch *watch,
DBusWatchHandler handler,
void *data,
DBusFreeFunction free_data_function)
{
if (watch->free_handler_data_function)
(* watch->free_handler_data_function) (watch->handler_data);
watch->handler = handler;
watch->handler_data = data;
watch->free_handler_data_function = free_data_function;
}
int
dbus_watch_get_fd (DBusWatch *watch)
{
_dbus_return_val_if_fail (watch != NULL, -1);
return dbus_watch_get_unix_fd(watch);
}
int
dbus_watch_get_unix_fd (DBusWatch *watch)
{
_dbus_return_val_if_fail (watch != NULL, -1);
/* FIXME remove #ifdef and do this on a lower level
* (watch should have set_socket and set_unix_fd and track
* which it has, and the transport should provide the
* appropriate watch type)
*/
#ifdef DBUS_UNIX
return watch->fd;
#else
return dbus_watch_get_socket( watch );
#endif
}
int
dbus_watch_get_socket (DBusWatch *watch)
{
_dbus_return_val_if_fail (watch != NULL, -1);
#ifdef DBUS_UNIX
return watch->fd;
#else
return _dbus_socket_get_int (watch->fd);
#endif
}
DBusSocket
_dbus_watch_get_socket (DBusWatch *watch)
{
DBusSocket s;
_dbus_assert (watch != NULL);
#ifdef DBUS_UNIX
s.fd = watch->fd;
#else
s = watch->fd;
#endif
return s;
}
DBusPollable
_dbus_watch_get_pollable (DBusWatch *watch)
{
_dbus_assert (watch != NULL);
return watch->fd;
}
unsigned int
dbus_watch_get_flags (DBusWatch *watch)
{
_dbus_return_val_if_fail (watch != NULL, 0);
_dbus_assert ((watch->flags & VALID_WATCH_FLAGS) == watch->flags);
return watch->flags;
}
void*
dbus_watch_get_data (DBusWatch *watch)
{
_dbus_return_val_if_fail (watch != NULL, NULL);
return watch->data;
}
void
dbus_watch_set_data (DBusWatch *watch,
void *data,
DBusFreeFunction free_data_function)
{
_dbus_return_if_fail (watch != NULL);
_dbus_verbose ("Setting watch fd %" DBUS_POLLABLE_FORMAT " data to data = %p function = %p from data = %p function = %p\n",
_dbus_pollable_printable (watch->fd),
data, free_data_function, watch->data, watch->free_data_function);
if (watch->free_data_function != NULL)
(* watch->free_data_function) (watch->data);
watch->data = data;
watch->free_data_function = free_data_function;
}
dbus_bool_t
dbus_watch_get_enabled (DBusWatch *watch)
{
_dbus_return_val_if_fail (watch != NULL, FALSE);
return watch->enabled;
}
dbus_bool_t
dbus_watch_handle (DBusWatch *watch,
unsigned int flags)
{
_dbus_return_val_if_fail (watch != NULL, FALSE);
#ifndef DBUS_DISABLE_CHECKS
if (!_dbus_pollable_is_valid (watch->fd) || watch->flags == 0)
{
_dbus_warn_check_failed ("Watch is invalid, it should have been removed");
return TRUE;
}
#endif
_dbus_return_val_if_fail (_dbus_pollable_is_valid (watch->fd) /* fails if watch was removed */, TRUE);
_dbus_watch_sanitize_condition (watch, &flags);
if (flags == 0)
{
_dbus_verbose ("After sanitization, watch flags on fd %" DBUS_POLLABLE_FORMAT " were 0\n",
_dbus_pollable_printable (watch->fd));
return TRUE;
}
else
return (* watch->handler) (watch, flags,
watch->handler_data);
}
DBusWatchToggledFunction
void(* DBusWatchToggledFunction)(DBusWatch *watch, void *data)
Called when dbus_watch_get_enabled() may return a different value than it did before.
Definition dbus-connection.h:100
DBusAddWatchFunction
dbus_bool_t(* DBusAddWatchFunction)(DBusWatch *watch, void *data)
Called when libdbus needs a new watch to be monitored by the main loop.
Definition dbus-connection.h:94
DBusRemoveWatchFunction
void(* DBusRemoveWatchFunction)(DBusWatch *watch, void *data)
Called when libdbus no longer needs a watch to be monitored by the main loop.
Definition dbus-connection.h:106
DBUS_WATCH_READABLE
@ DBUS_WATCH_READABLE
As in POLLIN.
Definition dbus-connection.h:63
DBUS_WATCH_WRITABLE
@ DBUS_WATCH_WRITABLE
As in POLLOUT.
Definition dbus-connection.h:64
_dbus_assert_not_reached
#define _dbus_assert_not_reached(explanation)
Aborts with an error message if called.
Definition dbus-internals.h:164
_dbus_assert
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
Definition dbus-internals.h:153
DBusForeachFunction
void(* DBusForeachFunction)(void *element, void *data)
Used to iterate over each item in a collection, such as a DBusList.
Definition dbus-internals.h:347
_dbus_warn_check_failed
void _dbus_warn_check_failed(const char *format,...)
Prints a "critical" warning to stderr when an assertion fails; differs from _dbus_warn primarily in t...
Definition dbus-internals.c:310
_dbus_warn
void _dbus_warn(const char *format,...)
Prints a warning message to stderr.
Definition dbus-internals.c:278
_dbus_list_get_first_link
DBusList * _dbus_list_get_first_link(DBusList **list)
Gets the first link in the list.
Definition dbus-list.c:597
_dbus_list_remove
dbus_bool_t _dbus_list_remove(DBusList **list, void *data)
Removes a value from the list.
Definition dbus-list.c:418
_dbus_list_clear_full
void _dbus_list_clear_full(DBusList **list, DBusFreeFunction function)
Free every link and every element in the list.
Definition dbus-list.c:570
_dbus_list_foreach
void _dbus_list_foreach(DBusList **list, DBusForeachFunction function, void *data)
Calls the given function for each element in the list.
Definition dbus-list.c:789
_dbus_list_remove_last
dbus_bool_t _dbus_list_remove_last(DBusList **list, void *data)
Removes a value from the list.
Definition dbus-list.c:449
_dbus_list_append
dbus_bool_t _dbus_list_append(DBusList **list, void *data)
Appends a value to the list.
Definition dbus-list.c:273
_dbus_list_get_next_link
#define _dbus_list_get_next_link(list, link)
Gets the next link in the list, or NULL if there are no more links.
Definition dbus-list.h:121
NULL
#define NULL
A null pointer, defined appropriately for C or C++.
Definition dbus-macros.h:51
TRUE
#define TRUE
Expands to "1".
Definition dbus-macros.h:41
FALSE
#define FALSE
Expands to "0".
Definition dbus-macros.h:44
dbus_free
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
Definition dbus-memory.c:710
DBusFreeFunction
void(* DBusFreeFunction)(void *memory)
The type of a function which frees a block of memory.
Definition dbus-memory.h:65
dbus_new0
#define dbus_new0(type, count)
Safe macro for using dbus_malloc0().
Definition dbus-memory.h:60
dbus_bool_t
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition dbus-types.h:37
_dbus_watch_list_add_watch
dbus_bool_t _dbus_watch_list_add_watch(DBusWatchList *watch_list, DBusWatch *watch)
Adds a new watch to the watch list, invoking the application DBusAddWatchFunction if appropriate.
Definition dbus-watch.c:383
_dbus_watch_list_toggle_watch
void _dbus_watch_list_toggle_watch(DBusWatchList *watch_list, DBusWatch *watch, dbus_bool_t enabled)
Sets a watch to the given enabled state, invoking the application's DBusWatchToggledFunction if appro...
Definition dbus-watch.c:443
_dbus_watch_list_new
DBusWatchList * _dbus_watch_list_new(void)
Creates a new watch list.
Definition dbus-watch.c:234
_dbus_watch_list_free
void _dbus_watch_list_free(DBusWatchList *watch_list)
Frees a DBusWatchList.
Definition dbus-watch.c:251
_dbus_watch_list_set_functions
dbus_bool_t _dbus_watch_list_set_functions(DBusWatchList *watch_list, DBusAddWatchFunction add_function, DBusRemoveWatchFunction remove_function, DBusWatchToggledFunction toggled_function, void *data, DBusFreeFunction free_data_function)
Sets the watch functions.
Definition dbus-watch.c:297
_dbus_watch_set_handler
void _dbus_watch_set_handler(DBusWatch *watch, DBusWatchHandler handler, void *data, DBusFreeFunction free_data_function)
Sets the handler for the watch.
Definition dbus-watch.c:500
_dbus_watch_list_toggle_all_watches
void _dbus_watch_list_toggle_all_watches(DBusWatchList *watch_list, dbus_bool_t enabled)
Sets all watches to the given enabled state, invoking the application's DBusWatchToggledFunction if a...
Definition dbus-watch.c:474
_dbus_watch_sanitize_condition
void _dbus_watch_sanitize_condition(DBusWatch *watch, unsigned int *condition)
Sanitizes the given condition so that it only contains flags that the DBusWatch requested.
Definition dbus-watch.c:187
_dbus_watch_new
DBusWatch * _dbus_watch_new(DBusPollable fd, unsigned int flags, dbus_bool_t enabled, DBusWatchHandler handler, void *data, DBusFreeFunction free_data_function)
Creates a new DBusWatch.
Definition dbus-watch.c:90
DBusWatchHandler
dbus_bool_t(* DBusWatchHandler)(DBusWatch *watch, unsigned int flags, void *data)
function to run when the watch is handled
Definition dbus-watch.h:45
_dbus_watch_ref
DBusWatch * _dbus_watch_ref(DBusWatch *watch)
Increments the reference count of a DBusWatch object.
Definition dbus-watch.c:126
_dbus_watch_unref
void _dbus_watch_unref(DBusWatch *watch)
Decrements the reference count of a DBusWatch object and finalizes the object if the count reaches ze...
Definition dbus-watch.c:140
_dbus_watch_list_remove_watch
void _dbus_watch_list_remove_watch(DBusWatchList *watch_list, DBusWatch *watch)
Removes a watch from the watch list, invoking the application's DBusRemoveWatchFunction if appropriat...
Definition dbus-watch.c:416
_dbus_watch_invalidate
void _dbus_watch_invalidate(DBusWatch *watch)
Clears the file descriptor from a now-invalid watch object so that no one tries to use it.
Definition dbus-watch.c:171
dbus_watch_get_unix_fd
DBUS_EXPORT int dbus_watch_get_unix_fd(DBusWatch *watch)
Returns a UNIX file descriptor to be watched, which may be a pipe, socket, or other type of descripto...
Definition dbus-watch.c:566
dbus_watch_set_data
DBUS_EXPORT void dbus_watch_set_data(DBusWatch *watch, void *data, DBusFreeFunction free_data_function)
Sets data which can be retrieved with dbus_watch_get_data().
Definition dbus-watch.c:679
dbus_watch_get_fd
DBUS_EXPORT DBUS_DEPRECATED int dbus_watch_get_fd(DBusWatch *watch)
Deprecated former name of dbus_watch_get_unix_fd().
Definition dbus-watch.c:545
dbus_watch_get_data
DBUS_EXPORT void * dbus_watch_get_data(DBusWatch *watch)
Gets data previously set with dbus_watch_set_data() or NULL if none.
Definition dbus-watch.c:660
dbus_watch_get_socket
DBUS_EXPORT int dbus_watch_get_socket(DBusWatch *watch)
Returns a socket to be watched, on UNIX this will return -1 if our transport is not socket-based so d...
Definition dbus-watch.c:595
dbus_watch_handle
DBUS_EXPORT dbus_bool_t dbus_watch_handle(DBusWatch *watch, unsigned int flags)
Called to notify the D-Bus library when a previously-added watch is ready for reading or writing,...
Definition dbus-watch.c:735
dbus_watch_get_enabled
DBUS_EXPORT dbus_bool_t dbus_watch_get_enabled(DBusWatch *watch)
Returns whether a watch is enabled or not.
Definition dbus-watch.c:704
dbus_watch_get_flags
DBUS_EXPORT unsigned int dbus_watch_get_flags(DBusWatch *watch)
Gets flags from DBusWatchFlags indicating what conditions should be monitored on the file descriptor.
Definition dbus-watch.c:644
DBusList
A node in a linked list.
Definition dbus-list.h:37
DBusList::data
void * data
Data stored at this element.
Definition dbus-list.h:40
DBusSocket
Socket interface.
Definition dbus-sysdeps.h:185
DBusWatchList
DBusWatchList implementation details.
Definition dbus-watch.c:217
DBusWatchList::watch_free_data_function
DBusFreeFunction watch_free_data_function
Free function for watch callback data.
Definition dbus-watch.c:224
DBusWatchList::remove_watch_function
DBusRemoveWatchFunction remove_watch_function
Callback for removing a watch.
Definition dbus-watch.c:221
DBusWatchList::watch_data
void * watch_data
Data for watch callbacks.
Definition dbus-watch.c:223
DBusWatchList::watches
DBusList * watches
Watch objects.
Definition dbus-watch.c:218
DBusWatchList::add_watch_function
DBusAddWatchFunction add_watch_function
Callback for adding a watch.
Definition dbus-watch.c:220
DBusWatchList::watch_toggled_function
DBusWatchToggledFunction watch_toggled_function
Callback on toggling enablement.
Definition dbus-watch.c:222
DBusWatch
Implementation of DBusWatch.
Definition dbus-watch.c:43
DBusWatch::enabled
unsigned int enabled
Whether it's enabled.
Definition dbus-watch.c:54
DBusWatch::oom_last_time
unsigned int oom_last_time
Whether it was OOM last time.
Definition dbus-watch.c:55
DBusWatch::refcount
int refcount
Reference count.
Definition dbus-watch.c:44
DBusWatch::free_handler_data_function
DBusFreeFunction free_handler_data_function
Free the watch handler data.
Definition dbus-watch.c:50
DBusWatch::data
void * data
Application data.
Definition dbus-watch.c:52
DBusWatch::fd
DBusPollable fd
File descriptor.
Definition dbus-watch.c:45
DBusWatch::flags
unsigned int flags
Conditions to watch.
Definition dbus-watch.c:46
DBusWatch::handler
DBusWatchHandler handler
Watch handler.
Definition dbus-watch.c:48
DBusWatch::free_data_function
DBusFreeFunction free_data_function
Free the application data.
Definition dbus-watch.c:53
DBusWatch::handler_data
void * handler_data
Watch handler data.
Definition dbus-watch.c:49