D-Bus 1.14.10
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
41{
43 DBusPollable fd;
44 unsigned int flags;
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
68void
69_dbus_watch_set_oom_last_time (DBusWatch *watch,
70 dbus_bool_t oom)
71{
72 watch->oom_last_time = oom;
73}
74
88_dbus_watch_new (DBusPollable fd,
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
123DBusWatch *
125{
126 watch->refcount += 1;
127
128 return watch;
129}
130
137void
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 (_dbus_pollable_is_valid (watch->fd))
147 _dbus_warn ("this watch should have been invalidated");
148
149 dbus_watch_set_data (watch, NULL, NULL); /* call free_data_function */
150
152 (* watch->free_handler_data_function) (watch->handler_data);
153
154 dbus_free (watch);
155 }
156}
157
168void
170{
171 _dbus_pollable_invalidate (&watch->fd);
172 watch->flags = 0;
173}
174
184void
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{
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
248void
250{
251 /* free watch_data and removes watches as a side effect */
253 NULL, NULL, NULL, NULL, NULL);
254
255 _dbus_list_clear_full (&watch_list->watches,
257
258 dbus_free (watch_list);
259}
260
261#ifdef DBUS_ENABLE_VERBOSE_MODE
262static const char*
263watch_flags_to_string (int flags)
264{
265 const char *watch_type;
266
267 if ((flags & DBUS_WATCH_READABLE) &&
268 (flags & DBUS_WATCH_WRITABLE))
269 watch_type = "readwrite";
270 else if (flags & DBUS_WATCH_READABLE)
271 watch_type = "read";
272 else if (flags & DBUS_WATCH_WRITABLE)
273 watch_type = "write";
274 else
275 watch_type = "not read or write";
276 return watch_type;
277}
278#endif /* DBUS_ENABLE_VERBOSE_MODE */
279
296 DBusAddWatchFunction add_function,
297 DBusRemoveWatchFunction remove_function,
298 DBusWatchToggledFunction toggled_function,
299 void *data,
300 DBusFreeFunction free_data_function)
301{
302 /* Add watches with the new watch function, failing on OOM */
303 if (add_function != NULL)
304 {
305 DBusList *link;
306
307 link = _dbus_list_get_first_link (&watch_list->watches);
308 while (link != NULL)
309 {
310 DBusList *next = _dbus_list_get_next_link (&watch_list->watches,
311 link);
312#ifdef DBUS_ENABLE_VERBOSE_MODE
313 DBusWatch *watch = link->data;
314
315 _dbus_verbose ("Adding a %s watch on fd %" DBUS_POLLABLE_FORMAT " using newly-set add watch function\n",
316 watch_flags_to_string (dbus_watch_get_flags (link->data)),
317 _dbus_pollable_printable (watch->fd));
318#endif
319
320 if (!(* add_function) (link->data, data))
321 {
322 /* remove it all again and return FALSE */
323 DBusList *link2;
324
325 link2 = _dbus_list_get_first_link (&watch_list->watches);
326 while (link2 != link)
327 {
328 DBusList *next2 = _dbus_list_get_next_link (&watch_list->watches,
329 link2);
330#ifdef DBUS_ENABLE_VERBOSE_MODE
331 DBusWatch *watch2 = link2->data;
332
333 _dbus_verbose ("Removing watch on fd %" DBUS_POLLABLE_FORMAT " using newly-set remove function because initial add failed\n",
334 _dbus_pollable_printable (watch2->fd));
335#endif
336
337 (* remove_function) (link2->data, data);
338
339 link2 = next2;
340 }
341
342 return FALSE;
343 }
344
345 link = next;
346 }
347 }
348
349 /* Remove all current watches from previous watch handlers */
350
351 if (watch_list->remove_watch_function != NULL)
352 {
353 _dbus_verbose ("Removing all pre-existing watches\n");
354
355 _dbus_list_foreach (&watch_list->watches,
357 watch_list->watch_data);
358 }
359
360 if (watch_list->watch_free_data_function != NULL)
361 (* watch_list->watch_free_data_function) (watch_list->watch_data);
362
363 watch_list->add_watch_function = add_function;
364 watch_list->remove_watch_function = remove_function;
365 watch_list->watch_toggled_function = toggled_function;
366 watch_list->watch_data = data;
367 watch_list->watch_free_data_function = free_data_function;
368
369 return TRUE;
370}
371
382 DBusWatch *watch)
383{
384 if (!_dbus_list_append (&watch_list->watches, watch))
385 return FALSE;
386
387 _dbus_watch_ref (watch);
388
389 if (watch_list->add_watch_function != NULL)
390 {
391 _dbus_verbose ("Adding watch on fd %" DBUS_POLLABLE_FORMAT "\n",
392 _dbus_pollable_printable (watch->fd));
393
394 if (!(* watch_list->add_watch_function) (watch,
395 watch_list->watch_data))
396 {
397 _dbus_list_remove_last (&watch_list->watches, watch);
398 _dbus_watch_unref (watch);
399 return FALSE;
400 }
401 }
402
403 return TRUE;
404}
405
413void
415 DBusWatch *watch)
416{
417 if (!_dbus_list_remove (&watch_list->watches, watch))
418 _dbus_assert_not_reached ("Nonexistent watch was removed");
419
420 if (watch_list->remove_watch_function != NULL)
421 {
422 _dbus_verbose ("Removing watch on fd %" DBUS_POLLABLE_FORMAT "\n",
423 _dbus_pollable_printable (watch->fd));
424
425 (* watch_list->remove_watch_function) (watch,
426 watch_list->watch_data);
427 }
428
429 _dbus_watch_unref (watch);
430}
431
440void
442 DBusWatch *watch,
443 dbus_bool_t enabled)
444{
445 enabled = !!enabled;
446
447 if (enabled == watch->enabled)
448 return;
449
450 watch->enabled = enabled;
451
452 if (watch_list->watch_toggled_function != NULL)
453 {
454 _dbus_verbose ("Toggling watch %p on fd %" DBUS_POLLABLE_FORMAT " to %d\n",
455 watch,
456 _dbus_pollable_printable (watch->fd),
457 watch->enabled);
458
459 (* watch_list->watch_toggled_function) (watch,
460 watch_list->watch_data);
461 }
462}
463
471void
473 dbus_bool_t enabled)
474{
475 DBusList *link;
476
477 for (link = _dbus_list_get_first_link (&watch_list->watches);
478 link != NULL;
479 link = _dbus_list_get_next_link (&watch_list->watches, link))
480 {
481 _dbus_watch_list_toggle_watch (watch_list, link->data, enabled);
482 }
483}
484
497void
499 DBusWatchHandler handler,
500 void *data,
501 DBusFreeFunction free_data_function)
502{
504 (* watch->free_handler_data_function) (watch->handler_data);
505
506 watch->handler = handler;
507 watch->handler_data = data;
508 watch->free_handler_data_function = free_data_function;
509}
510
542int
544{
545 _dbus_return_val_if_fail (watch != NULL, -1);
546
547 return dbus_watch_get_unix_fd(watch);
548}
549
563int
565{
566 _dbus_return_val_if_fail (watch != NULL, -1);
567
568 /* FIXME remove #ifdef and do this on a lower level
569 * (watch should have set_socket and set_unix_fd and track
570 * which it has, and the transport should provide the
571 * appropriate watch type)
572 */
573#ifdef DBUS_UNIX
574 return watch->fd;
575#else
576 return dbus_watch_get_socket( watch );
577#endif
578}
579
592int
594{
595 _dbus_return_val_if_fail (watch != NULL, -1);
596
597#ifdef DBUS_UNIX
598 return watch->fd;
599#else
600 return _dbus_socket_get_int (watch->fd);
601#endif
602}
603
605_dbus_watch_get_socket (DBusWatch *watch)
606{
607 DBusSocket s;
608
609 _dbus_assert (watch != NULL);
610
611#ifdef DBUS_UNIX
612 s.fd = watch->fd;
613#else
614 s = watch->fd;
615#endif
616
617 return s;
618}
619
620DBusPollable
621_dbus_watch_get_pollable (DBusWatch *watch)
622{
623 _dbus_assert (watch != NULL);
624
625 return watch->fd;
626}
627
641unsigned int
643{
644 _dbus_return_val_if_fail (watch != NULL, 0);
645 _dbus_assert ((watch->flags & VALID_WATCH_FLAGS) == watch->flags);
646
647 return watch->flags;
648}
649
657void*
659{
660 _dbus_return_val_if_fail (watch != NULL, NULL);
661
662 return watch->data;
663}
664
676void
678 void *data,
679 DBusFreeFunction free_data_function)
680{
681 _dbus_return_if_fail (watch != NULL);
682
683 _dbus_verbose ("Setting watch fd %" DBUS_POLLABLE_FORMAT " data to data = %p function = %p from data = %p function = %p\n",
684 _dbus_pollable_printable (watch->fd),
685 data, free_data_function, watch->data, watch->free_data_function);
686
687 if (watch->free_data_function != NULL)
688 (* watch->free_data_function) (watch->data);
689
690 watch->data = data;
691 watch->free_data_function = free_data_function;
692}
693
703{
704 _dbus_return_val_if_fail (watch != NULL, FALSE);
705
706 return watch->enabled;
707}
708
709
734 unsigned int flags)
735{
736 _dbus_return_val_if_fail (watch != NULL, FALSE);
737
738#ifndef DBUS_DISABLE_CHECKS
739 if (!_dbus_pollable_is_valid (watch->fd) || watch->flags == 0)
740 {
741 _dbus_warn_check_failed ("Watch is invalid, it should have been removed");
742 return TRUE;
743 }
744#endif
745
746 _dbus_return_val_if_fail (_dbus_pollable_is_valid (watch->fd) /* fails if watch was removed */, TRUE);
747
748 _dbus_watch_sanitize_condition (watch, &flags);
749
750 if (flags == 0)
751 {
752 _dbus_verbose ("After sanitization, watch flags on fd %" DBUS_POLLABLE_FORMAT " were 0\n",
753 _dbus_pollable_printable (watch->fd));
754 return TRUE;
755 }
756 else
757 return (* watch->handler) (watch, flags,
758 watch->handler_data);
759}
760
761
void(* DBusWatchToggledFunction)(DBusWatch *watch, void *data)
Called when dbus_watch_get_enabled() may return a different value than it did before.
dbus_bool_t(* DBusAddWatchFunction)(DBusWatch *watch, void *data)
Called when libdbus needs a new watch to be monitored by the main loop.
void(* DBusRemoveWatchFunction)(DBusWatch *watch, void *data)
Called when libdbus no longer needs a watch to be monitored by the main loop.
@ DBUS_WATCH_READABLE
As in POLLIN.
@ DBUS_WATCH_WRITABLE
As in POLLOUT.
#define _dbus_assert_not_reached(explanation)
Aborts with an error message if called.
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
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...
void(* DBusForeachFunction)(void *element, void *data)
Used to iterate over each item in a collection, such as a DBusList.
void _dbus_warn(const char *format,...)
Prints a warning message to stderr.
DBusList * _dbus_list_get_first_link(DBusList **list)
Gets the first link in the list.
Definition: dbus-list.c:595
dbus_bool_t _dbus_list_remove(DBusList **list, void *data)
Removes a value from the list.
Definition: dbus-list.c:416
void _dbus_list_clear_full(DBusList **list, DBusFreeFunction function)
Free every link and every element in the list.
Definition: dbus-list.c:568
void _dbus_list_foreach(DBusList **list, DBusForeachFunction function, void *data)
Calls the given function for each element in the list.
Definition: dbus-list.c:787
dbus_bool_t _dbus_list_remove_last(DBusList **list, void *data)
Removes a value from the list.
Definition: dbus-list.c:447
dbus_bool_t _dbus_list_append(DBusList **list, void *data)
Appends a value to the list.
Definition: dbus-list.c:271
#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:119
#define NULL
A null pointer, defined appropriately for C or C++.
#define TRUE
Expands to "1".
#define FALSE
Expands to "0".
void(* DBusFreeFunction)(void *memory)
The type of a function which frees a block of memory.
Definition: dbus-memory.h:63
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
Definition: dbus-memory.c:692
#define dbus_new0(type, count)
Safe macro for using dbus_malloc0().
Definition: dbus-memory.h:58
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition: dbus-types.h:35
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:381
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:441
DBusWatchList * _dbus_watch_list_new(void)
Creates a new watch list.
Definition: dbus-watch.c:232
void _dbus_watch_list_free(DBusWatchList *watch_list)
Frees a DBusWatchList.
Definition: dbus-watch.c:249
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:295
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:498
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:472
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
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:88
dbus_bool_t(* DBusWatchHandler)(DBusWatch *watch, unsigned int flags, void *data)
function to run when the watch is handled
Definition: dbus-watch.h:43
DBusWatch * _dbus_watch_ref(DBusWatch *watch)
Increments the reference count of a DBusWatch object.
Definition: dbus-watch.c:124
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 _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:414
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
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:564
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:677
DBUS_EXPORT DBUS_DEPRECATED int dbus_watch_get_fd(DBusWatch *watch)
Deprecated former name of dbus_watch_get_unix_fd().
Definition: dbus-watch.c:543
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:658
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:593
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:733
DBUS_EXPORT dbus_bool_t dbus_watch_get_enabled(DBusWatch *watch)
Returns whether a watch is enabled or not.
Definition: dbus-watch.c:702
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:642
A node in a linked list.
Definition: dbus-list.h:35
void * data
Data stored at this element.
Definition: dbus-list.h:38
Socket interface.
Definition: dbus-sysdeps.h:181
DBusWatchList implementation details.
Definition: dbus-watch.c:215
DBusFreeFunction watch_free_data_function
Free function for watch callback data.
Definition: dbus-watch.c:222
DBusRemoveWatchFunction remove_watch_function
Callback for removing a watch.
Definition: dbus-watch.c:219
void * watch_data
Data for watch callbacks.
Definition: dbus-watch.c:221
DBusList * watches
Watch objects.
Definition: dbus-watch.c:216
DBusAddWatchFunction add_watch_function
Callback for adding a watch.
Definition: dbus-watch.c:218
DBusWatchToggledFunction watch_toggled_function
Callback on toggling enablement.
Definition: dbus-watch.c:220
Implementation of DBusWatch.
Definition: dbus-watch.c:41
unsigned int enabled
Whether it's enabled.
Definition: dbus-watch.c:52
unsigned int oom_last_time
Whether it was OOM last time.
Definition: dbus-watch.c:53
int refcount
Reference count.
Definition: dbus-watch.c:42
DBusFreeFunction free_handler_data_function
Free the watch handler data.
Definition: dbus-watch.c:48
void * data
Application data.
Definition: dbus-watch.c:50
DBusPollable fd
File descriptor.
Definition: dbus-watch.c:43
unsigned int flags
Conditions to watch.
Definition: dbus-watch.c:44
DBusWatchHandler handler
Watch handler.
Definition: dbus-watch.c:46
DBusFreeFunction free_data_function
Free the application data.
Definition: dbus-watch.c:51
void * handler_data
Watch handler data.
Definition: dbus-watch.c:47