D-Bus  1.6.12
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 
278  DBusAddWatchFunction add_function,
279  DBusRemoveWatchFunction remove_function,
280  DBusWatchToggledFunction toggled_function,
281  void *data,
282  DBusFreeFunction free_data_function)
283 {
284  /* Add watches with the new watch function, failing on OOM */
285  if (add_function != NULL)
286  {
287  DBusList *link;
288 
289  link = _dbus_list_get_first_link (&watch_list->watches);
290  while (link != NULL)
291  {
292  DBusList *next = _dbus_list_get_next_link (&watch_list->watches,
293  link);
294 
295 #ifdef DBUS_ENABLE_VERBOSE_MODE
296  {
297  const char *watch_type;
298  int flags;
299 
300  flags = dbus_watch_get_flags (link->data);
301  if ((flags & DBUS_WATCH_READABLE) &&
302  (flags & DBUS_WATCH_WRITABLE))
303  watch_type = "readwrite";
304  else if (flags & DBUS_WATCH_READABLE)
305  watch_type = "read";
306  else if (flags & DBUS_WATCH_WRITABLE)
307  watch_type = "write";
308  else
309  watch_type = "not read or write";
310 
311  _dbus_verbose ("Adding a %s watch on fd %d using newly-set add watch function\n",
312  watch_type,
313  dbus_watch_get_socket (link->data));
314  }
315 #endif /* DBUS_ENABLE_VERBOSE_MODE */
316 
317  if (!(* add_function) (link->data, data))
318  {
319  /* remove it all again and return FALSE */
320  DBusList *link2;
321 
322  link2 = _dbus_list_get_first_link (&watch_list->watches);
323  while (link2 != link)
324  {
325  DBusList *next = _dbus_list_get_next_link (&watch_list->watches,
326  link2);
327 
328  _dbus_verbose ("Removing watch on fd %d using newly-set remove function because initial add failed\n",
329  dbus_watch_get_socket (link2->data));
330 
331  (* remove_function) (link2->data, data);
332 
333  link2 = next;
334  }
335 
336  return FALSE;
337  }
338 
339  link = next;
340  }
341  }
342 
343  /* Remove all current watches from previous watch handlers */
344 
345  if (watch_list->remove_watch_function != NULL)
346  {
347  _dbus_verbose ("Removing all pre-existing watches\n");
348 
349  _dbus_list_foreach (&watch_list->watches,
351  watch_list->watch_data);
352  }
353 
354  if (watch_list->watch_free_data_function != NULL)
355  (* watch_list->watch_free_data_function) (watch_list->watch_data);
356 
357  watch_list->add_watch_function = add_function;
358  watch_list->remove_watch_function = remove_function;
359  watch_list->watch_toggled_function = toggled_function;
360  watch_list->watch_data = data;
361  watch_list->watch_free_data_function = free_data_function;
362 
363  return TRUE;
364 }
365 
376  DBusWatch *watch)
377 {
378  if (!_dbus_list_append (&watch_list->watches, watch))
379  return FALSE;
380 
381  _dbus_watch_ref (watch);
382 
383  if (watch_list->add_watch_function != NULL)
384  {
385  _dbus_verbose ("Adding watch on fd %d\n",
386  dbus_watch_get_socket (watch));
387 
388  if (!(* watch_list->add_watch_function) (watch,
389  watch_list->watch_data))
390  {
391  _dbus_list_remove_last (&watch_list->watches, watch);
392  _dbus_watch_unref (watch);
393  return FALSE;
394  }
395  }
396 
397  return TRUE;
398 }
399 
407 void
409  DBusWatch *watch)
410 {
411  if (!_dbus_list_remove (&watch_list->watches, watch))
412  _dbus_assert_not_reached ("Nonexistent watch was removed");
413 
414  if (watch_list->remove_watch_function != NULL)
415  {
416  _dbus_verbose ("Removing watch on fd %d\n",
417  dbus_watch_get_socket (watch));
418 
419  (* watch_list->remove_watch_function) (watch,
420  watch_list->watch_data);
421  }
422 
423  _dbus_watch_unref (watch);
424 }
425 
434 void
436  DBusWatch *watch,
437  dbus_bool_t enabled)
438 {
439  enabled = !!enabled;
440 
441  if (enabled == watch->enabled)
442  return;
443 
444  watch->enabled = enabled;
445 
446  if (watch_list->watch_toggled_function != NULL)
447  {
448  _dbus_verbose ("Toggling watch %p on fd %d to %d\n",
449  watch, dbus_watch_get_socket (watch), watch->enabled);
450 
451  (* watch_list->watch_toggled_function) (watch,
452  watch_list->watch_data);
453  }
454 }
455 
468 void
470  DBusWatchHandler handler,
471  void *data,
472  DBusFreeFunction free_data_function)
473 {
474  if (watch->free_handler_data_function)
475  (* watch->free_handler_data_function) (watch->handler_data);
476 
477  watch->handler = handler;
478  watch->handler_data = data;
479  watch->free_handler_data_function = free_data_function;
480 }
481 
513 int
515 {
516  _dbus_return_val_if_fail (watch != NULL, -1);
517 
518  return dbus_watch_get_unix_fd(watch);
519 }
520 
534 int
536 {
537  _dbus_return_val_if_fail (watch != NULL, -1);
538 
539  /* FIXME remove #ifdef and do this on a lower level
540  * (watch should have set_socket and set_unix_fd and track
541  * which it has, and the transport should provide the
542  * appropriate watch type)
543  */
544 #ifdef DBUS_UNIX
545  return watch->fd;
546 #else
547  return dbus_watch_get_socket( watch );
548 #endif
549 }
550 
563 int
565 {
566  _dbus_return_val_if_fail (watch != NULL, -1);
567 
568  return watch->fd;
569 }
570 
584 unsigned int
586 {
587  _dbus_return_val_if_fail (watch != NULL, 0);
588  _dbus_assert ((watch->flags & VALID_WATCH_FLAGS) == watch->flags);
589 
590  return watch->flags;
591 }
592 
600 void*
602 {
603  _dbus_return_val_if_fail (watch != NULL, NULL);
604 
605  return watch->data;
606 }
607 
619 void
621  void *data,
622  DBusFreeFunction free_data_function)
623 {
624  _dbus_return_if_fail (watch != NULL);
625 
626  _dbus_verbose ("Setting watch fd %d data to data = %p function = %p from data = %p function = %p\n",
627  dbus_watch_get_socket (watch),
628  data, free_data_function, watch->data, watch->free_data_function);
629 
630  if (watch->free_data_function != NULL)
631  (* watch->free_data_function) (watch->data);
632 
633  watch->data = data;
634  watch->free_data_function = free_data_function;
635 }
636 
646 {
647  _dbus_return_val_if_fail (watch != NULL, FALSE);
648 
649  return watch->enabled;
650 }
651 
652 
677  unsigned int flags)
678 {
679  _dbus_return_val_if_fail (watch != NULL, FALSE);
680 
681 #ifndef DBUS_DISABLE_CHECKS
682  if (watch->fd < 0 || watch->flags == 0)
683  {
684  _dbus_warn_check_failed ("Watch is invalid, it should have been removed\n");
685  return TRUE;
686  }
687 #endif
688 
689  _dbus_return_val_if_fail (watch->fd >= 0 /* fails if watch was removed */, TRUE);
690 
691  _dbus_watch_sanitize_condition (watch, &flags);
692 
693  if (flags == 0)
694  {
695  _dbus_verbose ("After sanitization, watch flags on fd %d were 0\n",
696  watch->fd);
697  return TRUE;
698  }
699  else
700  return (* watch->handler) (watch, flags,
701  watch->handler_data);
702 }
703 
704 
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(* DBusRemoveWatchFunction)(DBusWatch *watch, void *data)
Called when libdbus no longer needs a watch to be monitored by the main loop.
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
Definition: dbus-memory.c:700
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
DBUS_EXPORT dbus_bool_t dbus_watch_get_enabled(DBusWatch *watch)
Returns whether a watch is enabled or not.
Definition: dbus-watch.c:645
#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:375
void _dbus_warn_check_failed(const char *format,...)
Prints a &quot;critical&quot; warning to stderr when an assertion fails; differs from _dbus_warn primarily in t...
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&#39;s DBusWatchToggledFunction if appro...
Definition: dbus-watch.c:435
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:404
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:435
dbus_bool_t(* DBusAddWatchFunction)(DBusWatch *watch, void *data)
Called when libdbus needs a new watch to be monitored by the main loop.
#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:564
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:259
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:748
dbus_bool_t(* DBusWatchHandler)(DBusWatch *watch, unsigned int flags, void *data)
function to run when the watch is handled
Definition: dbus-watch.h:43
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:535
int refcount
Reference count.
Definition: dbus-watch.c:42
#define TRUE
Expands to &quot;1&quot;.
#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:514
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:620
DBusWatchList implementation details.
Definition: dbus-watch.c:214
unsigned int enabled
Whether it&#39;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:601
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:277
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:556
#define FALSE
Expands to &quot;0&quot;.
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:676
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
void(* DBusWatchToggledFunction)(DBusWatch *watch, void *data)
Called when dbus_watch_get_enabled() may return a different value than it did before.
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:585
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:469
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&#39;s DBusRemoveWatchFunction if appropriat...
Definition: dbus-watch.c:408
void _dbus_list_clear(DBusList **list)
Frees all links in the list and sets the list head to NULL.
Definition: dbus-list.c:531