D-Bus  1.8.16
dbus-watch.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-watch.c DBusWatch implementation
3  *
4  * Copyright (C) 2002, 2003 Red Hat Inc.
5  *
6  * Licensed under the Academic Free License version 2.1
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  *
22  */
23 
24 #include <config.h>
25 #include "dbus-internals.h"
26 #include "dbus-watch.h"
27 #include "dbus-list.h"
28 
40 struct DBusWatch
41 {
42  int refcount;
43  int fd;
44  unsigned int flags;
47  void *handler_data;
50  void *data;
52  unsigned int enabled : 1;
53  unsigned int oom_last_time : 1;
54 };
55 
57 _dbus_watch_get_enabled (DBusWatch *watch)
58 {
59  return watch->enabled;
60 }
61 
63 _dbus_watch_get_oom_last_time (DBusWatch *watch)
64 {
65  return watch->oom_last_time;
66 }
67 
68 void
69 _dbus_watch_set_oom_last_time (DBusWatch *watch,
70  dbus_bool_t oom)
71 {
72  watch->oom_last_time = oom;
73 }
74 
87 DBusWatch*
89  unsigned int flags,
90  dbus_bool_t enabled,
91  DBusWatchHandler handler,
92  void *data,
93  DBusFreeFunction free_data_function)
94 {
95  DBusWatch *watch;
96 
97 #define VALID_WATCH_FLAGS (DBUS_WATCH_WRITABLE | DBUS_WATCH_READABLE)
98 
99  _dbus_assert ((flags & VALID_WATCH_FLAGS) == flags);
100 
101  watch = dbus_new0 (DBusWatch, 1);
102  if (watch == NULL)
103  return NULL;
104 
105  watch->refcount = 1;
106  watch->fd = fd;
107  watch->flags = flags;
108  watch->enabled = enabled;
109 
110  watch->handler = handler;
111  watch->handler_data = data;
112  watch->free_handler_data_function = free_data_function;
113 
114  return watch;
115 }
116 
123 DBusWatch *
125 {
126  watch->refcount += 1;
127 
128  return watch;
129 }
130 
137 void
139 {
140  _dbus_assert (watch != NULL);
141  _dbus_assert (watch->refcount > 0);
142 
143  watch->refcount -= 1;
144  if (watch->refcount == 0)
145  {
146  if (watch->fd != -1)
147  _dbus_warn ("this watch should have been invalidated");
148 
149  dbus_watch_set_data (watch, NULL, NULL); /* call free_data_function */
150 
151  if (watch->free_handler_data_function)
152  (* watch->free_handler_data_function) (watch->handler_data);
153 
154  dbus_free (watch);
155  }
156 }
157 
168 void
170 {
171  watch->fd = -1;
172  watch->flags = 0;
173 }
174 
184 void
186  unsigned int *condition)
187 {
188  if (!(watch->flags & DBUS_WATCH_READABLE))
189  *condition &= ~DBUS_WATCH_READABLE;
190  if (!(watch->flags & DBUS_WATCH_WRITABLE))
191  *condition &= ~DBUS_WATCH_WRITABLE;
192 }
193 
194 
215 {
221  void *watch_data;
223 };
224 
233 {
234  DBusWatchList *watch_list;
235 
236  watch_list = dbus_new0 (DBusWatchList, 1);
237  if (watch_list == NULL)
238  return NULL;
239 
240  return watch_list;
241 }
242 
248 void
250 {
251  /* free watch_data and removes watches as a side effect */
252  _dbus_watch_list_set_functions (watch_list,
253  NULL, NULL, NULL, NULL, NULL);
254  _dbus_list_foreach (&watch_list->watches,
256  NULL);
257  _dbus_list_clear (&watch_list->watches);
258 
259  dbus_free (watch_list);
260 }
261 
262 #ifdef DBUS_ENABLE_VERBOSE_MODE
263 static const char*
264 watch_flags_to_string (int flags)
265 {
266  const char *watch_type;
267 
268  if ((flags & DBUS_WATCH_READABLE) &&
269  (flags & DBUS_WATCH_WRITABLE))
270  watch_type = "readwrite";
271  else if (flags & DBUS_WATCH_READABLE)
272  watch_type = "read";
273  else if (flags & DBUS_WATCH_WRITABLE)
274  watch_type = "write";
275  else
276  watch_type = "not read or write";
277  return watch_type;
278 }
279 #endif /* DBUS_ENABLE_VERBOSE_MODE */
280 
297  DBusAddWatchFunction add_function,
298  DBusRemoveWatchFunction remove_function,
299  DBusWatchToggledFunction toggled_function,
300  void *data,
301  DBusFreeFunction free_data_function)
302 {
303  /* Add watches with the new watch function, failing on OOM */
304  if (add_function != NULL)
305  {
306  DBusList *link;
307 
308  link = _dbus_list_get_first_link (&watch_list->watches);
309  while (link != NULL)
310  {
311  DBusList *next = _dbus_list_get_next_link (&watch_list->watches,
312  link);
313 
314  _dbus_verbose ("Adding a %s watch on fd %d using newly-set add watch function\n",
315  watch_flags_to_string (dbus_watch_get_flags (link->data)),
316  dbus_watch_get_socket (link->data));
317 
318  if (!(* add_function) (link->data, data))
319  {
320  /* remove it all again and return FALSE */
321  DBusList *link2;
322 
323  link2 = _dbus_list_get_first_link (&watch_list->watches);
324  while (link2 != link)
325  {
326  DBusList *next = _dbus_list_get_next_link (&watch_list->watches,
327  link2);
328 
329  _dbus_verbose ("Removing watch on fd %d using newly-set remove function because initial add failed\n",
330  dbus_watch_get_socket (link2->data));
331 
332  (* remove_function) (link2->data, data);
333 
334  link2 = next;
335  }
336 
337  return FALSE;
338  }
339 
340  link = next;
341  }
342  }
343 
344  /* Remove all current watches from previous watch handlers */
345 
346  if (watch_list->remove_watch_function != NULL)
347  {
348  _dbus_verbose ("Removing all pre-existing watches\n");
349 
350  _dbus_list_foreach (&watch_list->watches,
352  watch_list->watch_data);
353  }
354 
355  if (watch_list->watch_free_data_function != NULL)
356  (* watch_list->watch_free_data_function) (watch_list->watch_data);
357 
358  watch_list->add_watch_function = add_function;
359  watch_list->remove_watch_function = remove_function;
360  watch_list->watch_toggled_function = toggled_function;
361  watch_list->watch_data = data;
362  watch_list->watch_free_data_function = free_data_function;
363 
364  return TRUE;
365 }
366 
377  DBusWatch *watch)
378 {
379  if (!_dbus_list_append (&watch_list->watches, watch))
380  return FALSE;
381 
382  _dbus_watch_ref (watch);
383 
384  if (watch_list->add_watch_function != NULL)
385  {
386  _dbus_verbose ("Adding watch on fd %d\n",
387  dbus_watch_get_socket (watch));
388 
389  if (!(* watch_list->add_watch_function) (watch,
390  watch_list->watch_data))
391  {
392  _dbus_list_remove_last (&watch_list->watches, watch);
393  _dbus_watch_unref (watch);
394  return FALSE;
395  }
396  }
397 
398  return TRUE;
399 }
400 
408 void
410  DBusWatch *watch)
411 {
412  if (!_dbus_list_remove (&watch_list->watches, watch))
413  _dbus_assert_not_reached ("Nonexistent watch was removed");
414 
415  if (watch_list->remove_watch_function != NULL)
416  {
417  _dbus_verbose ("Removing watch on fd %d\n",
418  dbus_watch_get_socket (watch));
419 
420  (* watch_list->remove_watch_function) (watch,
421  watch_list->watch_data);
422  }
423 
424  _dbus_watch_unref (watch);
425 }
426 
435 void
437  DBusWatch *watch,
438  dbus_bool_t enabled)
439 {
440  enabled = !!enabled;
441 
442  if (enabled == watch->enabled)
443  return;
444 
445  watch->enabled = enabled;
446 
447  if (watch_list->watch_toggled_function != NULL)
448  {
449  _dbus_verbose ("Toggling watch %p on fd %d to %d\n",
450  watch, dbus_watch_get_socket (watch), watch->enabled);
451 
452  (* watch_list->watch_toggled_function) (watch,
453  watch_list->watch_data);
454  }
455 }
456 
464 void
466  dbus_bool_t enabled)
467 {
468  DBusList *link;
469 
470  for (link = _dbus_list_get_first_link (&watch_list->watches);
471  link != NULL;
472  link = _dbus_list_get_next_link (&watch_list->watches, link))
473  {
474  _dbus_watch_list_toggle_watch (watch_list, link->data, enabled);
475  }
476 }
477 
490 void
492  DBusWatchHandler handler,
493  void *data,
494  DBusFreeFunction free_data_function)
495 {
496  if (watch->free_handler_data_function)
497  (* watch->free_handler_data_function) (watch->handler_data);
498 
499  watch->handler = handler;
500  watch->handler_data = data;
501  watch->free_handler_data_function = free_data_function;
502 }
503 
535 int
537 {
538  _dbus_return_val_if_fail (watch != NULL, -1);
539 
540  return dbus_watch_get_unix_fd(watch);
541 }
542 
556 int
558 {
559  _dbus_return_val_if_fail (watch != NULL, -1);
560 
561  /* FIXME remove #ifdef and do this on a lower level
562  * (watch should have set_socket and set_unix_fd and track
563  * which it has, and the transport should provide the
564  * appropriate watch type)
565  */
566 #ifdef DBUS_UNIX
567  return watch->fd;
568 #else
569  return dbus_watch_get_socket( watch );
570 #endif
571 }
572 
585 int
587 {
588  _dbus_return_val_if_fail (watch != NULL, -1);
589 
590  return watch->fd;
591 }
592 
606 unsigned int
608 {
609  _dbus_return_val_if_fail (watch != NULL, 0);
610  _dbus_assert ((watch->flags & VALID_WATCH_FLAGS) == watch->flags);
611 
612  return watch->flags;
613 }
614 
622 void*
624 {
625  _dbus_return_val_if_fail (watch != NULL, NULL);
626 
627  return watch->data;
628 }
629 
641 void
643  void *data,
644  DBusFreeFunction free_data_function)
645 {
646  _dbus_return_if_fail (watch != NULL);
647 
648  _dbus_verbose ("Setting watch fd %d data to data = %p function = %p from data = %p function = %p\n",
649  dbus_watch_get_socket (watch),
650  data, free_data_function, watch->data, watch->free_data_function);
651 
652  if (watch->free_data_function != NULL)
653  (* watch->free_data_function) (watch->data);
654 
655  watch->data = data;
656  watch->free_data_function = free_data_function;
657 }
658 
668 {
669  _dbus_return_val_if_fail (watch != NULL, FALSE);
670 
671  return watch->enabled;
672 }
673 
674 
699  unsigned int flags)
700 {
701  _dbus_return_val_if_fail (watch != NULL, FALSE);
702 
703 #ifndef DBUS_DISABLE_CHECKS
704  if (watch->fd < 0 || watch->flags == 0)
705  {
706  _dbus_warn_check_failed ("Watch is invalid, it should have been removed\n");
707  return TRUE;
708  }
709 #endif
710 
711  _dbus_return_val_if_fail (watch->fd >= 0 /* fails if watch was removed */, TRUE);
712 
713  _dbus_watch_sanitize_condition (watch, &flags);
714 
715  if (flags == 0)
716  {
717  _dbus_verbose ("After sanitization, watch flags on fd %d were 0\n",
718  watch->fd);
719  return TRUE;
720  }
721  else
722  return (* watch->handler) (watch, flags,
723  watch->handler_data);
724 }
725 
726 
Implementation of DBusWatch.
Definition: dbus-watch.c:40
#define NULL
A null pointer, defined appropriately for C or C++.
void(* DBusFreeFunction)(void *memory)
The type of a function which frees a block of memory.
Definition: dbus-memory.h:64
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
Definition: dbus-memory.c:701
DBusWatchList * _dbus_watch_list_new(void)
Creates a new watch list.
Definition: dbus-watch.c:232
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:169
DBusFreeFunction watch_free_data_function
Free function for watch callback data.
Definition: dbus-watch.c:222
void * handler_data
Watch handler data.
Definition: dbus-watch.c:47
void(* DBusWatchToggledFunction)(DBusWatch *watch, void *data)
Called when dbus_watch_get_enabled() may return a different value than it did before.
DBUS_EXPORT dbus_bool_t dbus_watch_get_enabled(DBusWatch *watch)
Returns whether a watch is enabled or not.
Definition: dbus-watch.c:667
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
void * data
Data stored at this element.
Definition: dbus-list.h:38
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:376
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...
dbus_bool_t(* DBusWatchHandler)(DBusWatch *watch, unsigned int flags, void *data)
function to run when the watch is handled
Definition: dbus-watch.h:43
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:436
DBusAddWatchFunction add_watch_function
Callback for adding a watch.
Definition: dbus-watch.c:218
DBusFreeFunction free_data_function
Free the application data.
Definition: dbus-watch.c:51
DBusWatchToggledFunction watch_toggled_function
Callback on toggling enablement.
Definition: dbus-watch.c:220
void * watch_data
Data for watch callbacks.
Definition: dbus-watch.c:221
dbus_bool_t _dbus_list_remove(DBusList **list, void *data)
Removes a value from the list.
Definition: dbus-list.c:415
unsigned int flags
Conditions to watch.
Definition: dbus-watch.c:44
#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:90
int fd
File descriptor.
Definition: dbus-watch.c:43
dbus_bool_t _dbus_list_remove_last(DBusList **list, void *data)
Removes a value from the list.
Definition: dbus-list.c:446
#define dbus_new0(type, count)
Safe macro for using dbus_malloc0().
Definition: dbus-memory.h:59
As in POLLOUT.
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition: dbus-types.h:35
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:586
void _dbus_warn(const char *format,...)
Prints a warning message to stderr.
dbus_bool_t _dbus_list_append(DBusList **list, void *data)
Appends a value to the list.
Definition: dbus-list.c:270
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:185
void _dbus_list_foreach(DBusList **list, DBusForeachFunction function, void *data)
Calls the given function for each element in the list.
Definition: dbus-list.c:759
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:465
unsigned int oom_last_time
Whether it was OOM last time.
Definition: dbus-watch.c:53
void * data
Application data.
Definition: dbus-watch.c:50
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:557
int refcount
Reference count.
Definition: dbus-watch.c:42
#define TRUE
Expands to "1".
#define _dbus_assert_not_reached(explanation)
Aborts with an error message if called.
DBUS_EXPORT DBUS_DEPRECATED int dbus_watch_get_fd(DBusWatch *watch)
Deprecated former name of dbus_watch_get_unix_fd().
Definition: dbus-watch.c:536
dbus_bool_t(* DBusAddWatchFunction)(DBusWatch *watch, void *data)
Called when libdbus needs a new watch to be monitored by the main loop.
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:642
void(* DBusRemoveWatchFunction)(DBusWatch *watch, void *data)
Called when libdbus no longer needs a watch to be monitored by the main loop.
DBusWatchList implementation details.
Definition: dbus-watch.c:214
unsigned int enabled
Whether it's enabled.
Definition: dbus-watch.c:52
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:623
DBusWatch * _dbus_watch_ref(DBusWatch *watch)
Increments the reference count of a DBusWatch object.
Definition: dbus-watch.c:124
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:296
A node in a linked list.
Definition: dbus-list.h:34
DBusWatch * _dbus_watch_new(int fd, unsigned int flags, dbus_bool_t enabled, DBusWatchHandler handler, void *data, DBusFreeFunction free_data_function)
Creates a new DBusWatch.
Definition: dbus-watch.c:88
void(* DBusForeachFunction)(void *element, void *data)
Used to iterate over each item in a collection, such as a DBusList.
DBusList * _dbus_list_get_first_link(DBusList **list)
Gets the first link in the list.
Definition: dbus-list.c:567
#define FALSE
Expands to "0".
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:698
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:138
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:607
As in POLLIN.
DBusFreeFunction free_handler_data_function
Free the watch handler data.
Definition: dbus-watch.c:48
void _dbus_watch_list_free(DBusWatchList *watch_list)
Frees a DBusWatchList.
Definition: dbus-watch.c:249
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:491
DBusRemoveWatchFunction remove_watch_function
Callback for removing a watch.
Definition: dbus-watch.c:219
DBusWatchHandler handler
Watch handler.
Definition: dbus-watch.c:46
DBusList * watches
Watch objects.
Definition: dbus-watch.c:216
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:409
void _dbus_list_clear(DBusList **list)
Frees all links in the list and sets the list head to NULL.
Definition: dbus-list.c:542