1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-watch.c DBusWatch implementation
4 * Copyright (C) 2002, 2003 Red Hat Inc.
6 * Licensed under the Academic Free License version 2.1
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.
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.
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
25 #include "dbus-internals.h"
26 #include "dbus-watch.h"
27 #include "dbus-list.h"
30 * @defgroup DBusWatchInternals DBusWatch implementation details
31 * @ingroup DBusInternals
32 * @brief implementation details for DBusWatch
38 * Implementation of DBusWatch
42 int refcount; /**< Reference count */
43 DBusPollable fd; /**< File descriptor. */
44 unsigned int flags; /**< Conditions to watch. */
46 DBusWatchHandler handler; /**< Watch handler. */
47 void *handler_data; /**< Watch handler data. */
48 DBusFreeFunction free_handler_data_function; /**< Free the watch handler data. */
50 void *data; /**< Application data. */
51 DBusFreeFunction free_data_function; /**< Free the application data. */
52 unsigned int enabled : 1; /**< Whether it's enabled. */
53 unsigned int oom_last_time : 1; /**< Whether it was OOM last time. */
57 _dbus_watch_get_enabled (DBusWatch *watch)
59 return watch->enabled;
63 _dbus_watch_get_oom_last_time (DBusWatch *watch)
65 return watch->oom_last_time;
69 _dbus_watch_set_oom_last_time (DBusWatch *watch,
72 watch->oom_last_time = oom;
76 * Creates a new DBusWatch. Used to add a file descriptor to be polled
79 * @param fd the file descriptor to be watched.
80 * @param flags the conditions to watch for on the descriptor.
81 * @param enabled the initial enabled state
82 * @param handler the handler function
83 * @param data data for handler function
84 * @param free_data_function function to free the data
85 * @returns the new DBusWatch object.
88 _dbus_watch_new (DBusPollable fd,
91 DBusWatchHandler handler,
93 DBusFreeFunction free_data_function)
97 #define VALID_WATCH_FLAGS (DBUS_WATCH_WRITABLE | DBUS_WATCH_READABLE)
99 _dbus_assert ((flags & VALID_WATCH_FLAGS) == flags);
101 watch = dbus_new0 (DBusWatch, 1);
107 watch->flags = flags;
108 watch->enabled = enabled;
110 watch->handler = handler;
111 watch->handler_data = data;
112 watch->free_handler_data_function = free_data_function;
118 * Increments the reference count of a DBusWatch object.
120 * @param watch the watch object.
121 * @returns the watch object.
124 _dbus_watch_ref (DBusWatch *watch)
126 watch->refcount += 1;
132 * Decrements the reference count of a DBusWatch object
133 * and finalizes the object if the count reaches zero.
135 * @param watch the watch object.
138 _dbus_watch_unref (DBusWatch *watch)
140 _dbus_assert (watch != NULL);
141 _dbus_assert (watch->refcount > 0);
143 watch->refcount -= 1;
144 if (watch->refcount == 0)
146 if (_dbus_pollable_is_valid (watch->fd))
147 _dbus_warn ("this watch should have been invalidated");
149 dbus_watch_set_data (watch, NULL, NULL); /* call free_data_function */
151 if (watch->free_handler_data_function)
152 (* watch->free_handler_data_function) (watch->handler_data);
159 * Clears the file descriptor from a now-invalid watch object so that
160 * no one tries to use it. This is because a watch may stay alive due
161 * to reference counts after the file descriptor is closed.
162 * Invalidation makes it easier to catch bugs. It also
163 * keeps people from doing dorky things like assuming file descriptors
164 * are unique (never recycled).
166 * @param watch the watch object.
169 _dbus_watch_invalidate (DBusWatch *watch)
171 _dbus_pollable_invalidate (&watch->fd);
176 * Sanitizes the given condition so that it only contains
177 * flags that the DBusWatch requested. e.g. if the
178 * watch is a DBUS_WATCH_READABLE watch then
179 * DBUS_WATCH_WRITABLE will be stripped from the condition.
181 * @param watch the watch object.
182 * @param condition address of the condition to sanitize.
185 _dbus_watch_sanitize_condition (DBusWatch *watch,
186 unsigned int *condition)
188 if (!(watch->flags & DBUS_WATCH_READABLE))
189 *condition &= ~DBUS_WATCH_READABLE;
190 if (!(watch->flags & DBUS_WATCH_WRITABLE))
191 *condition &= ~DBUS_WATCH_WRITABLE;
196 * @typedef DBusWatchList
198 * Opaque data type representing a list of watches
199 * and a set of DBusAddWatchFunction/DBusRemoveWatchFunction.
200 * Automatically handles removing/re-adding watches
201 * when the DBusAddWatchFunction is updated or changed.
202 * Holds a reference count to each watch.
204 * Used in the implementation of both DBusServer and
210 * DBusWatchList implementation details. All fields
216 DBusList *watches; /**< Watch objects. */
218 DBusAddWatchFunction add_watch_function; /**< Callback for adding a watch. */
219 DBusRemoveWatchFunction remove_watch_function; /**< Callback for removing a watch. */
220 DBusWatchToggledFunction watch_toggled_function; /**< Callback on toggling enablement */
221 void *watch_data; /**< Data for watch callbacks */
222 DBusFreeFunction watch_free_data_function; /**< Free function for watch callback data */
226 * Creates a new watch list. Returns #NULL if insufficient
229 * @returns the new watch list, or #NULL on failure.
232 _dbus_watch_list_new (void)
234 DBusWatchList *watch_list;
236 watch_list = dbus_new0 (DBusWatchList, 1);
237 if (watch_list == NULL)
244 * Frees a DBusWatchList.
246 * @param watch_list the watch list.
249 _dbus_watch_list_free (DBusWatchList *watch_list)
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,
255 (DBusForeachFunction) _dbus_watch_unref,
257 _dbus_list_clear (&watch_list->watches);
259 dbus_free (watch_list);
262 #ifdef DBUS_ENABLE_VERBOSE_MODE
264 watch_flags_to_string (int flags)
266 const char *watch_type;
268 if ((flags & DBUS_WATCH_READABLE) &&
269 (flags & DBUS_WATCH_WRITABLE))
270 watch_type = "readwrite";
271 else if (flags & DBUS_WATCH_READABLE)
273 else if (flags & DBUS_WATCH_WRITABLE)
274 watch_type = "write";
276 watch_type = "not read or write";
279 #endif /* DBUS_ENABLE_VERBOSE_MODE */
282 * Sets the watch functions. This function is the "backend"
283 * for dbus_connection_set_watch_functions() and
284 * dbus_server_set_watch_functions().
286 * @param watch_list the watch list.
287 * @param add_function the add watch function.
288 * @param remove_function the remove watch function.
289 * @param toggled_function function on toggling enabled flag, or #NULL
290 * @param data the data for those functions.
291 * @param free_data_function the function to free the data.
292 * @returns #FALSE if not enough memory
296 _dbus_watch_list_set_functions (DBusWatchList *watch_list,
297 DBusAddWatchFunction add_function,
298 DBusRemoveWatchFunction remove_function,
299 DBusWatchToggledFunction toggled_function,
301 DBusFreeFunction free_data_function)
303 /* Add watches with the new watch function, failing on OOM */
304 if (add_function != NULL)
308 link = _dbus_list_get_first_link (&watch_list->watches);
311 DBusList *next = _dbus_list_get_next_link (&watch_list->watches,
313 #ifdef DBUS_ENABLE_VERBOSE_MODE
314 DBusWatch *watch = link->data;
316 _dbus_verbose ("Adding a %s watch on fd %" DBUS_POLLABLE_FORMAT " using newly-set add watch function\n",
317 watch_flags_to_string (dbus_watch_get_flags (link->data)),
318 _dbus_pollable_printable (watch->fd));
321 if (!(* add_function) (link->data, data))
323 /* remove it all again and return FALSE */
326 link2 = _dbus_list_get_first_link (&watch_list->watches);
327 while (link2 != link)
329 DBusList *next2 = _dbus_list_get_next_link (&watch_list->watches,
331 #ifdef DBUS_ENABLE_VERBOSE_MODE
332 DBusWatch *watch2 = link2->data;
334 _dbus_verbose ("Removing watch on fd %" DBUS_POLLABLE_FORMAT " using newly-set remove function because initial add failed\n",
335 _dbus_pollable_printable (watch2->fd));
338 (* remove_function) (link2->data, data);
350 /* Remove all current watches from previous watch handlers */
352 if (watch_list->remove_watch_function != NULL)
354 _dbus_verbose ("Removing all pre-existing watches\n");
356 _dbus_list_foreach (&watch_list->watches,
357 (DBusForeachFunction) watch_list->remove_watch_function,
358 watch_list->watch_data);
361 if (watch_list->watch_free_data_function != NULL)
362 (* watch_list->watch_free_data_function) (watch_list->watch_data);
364 watch_list->add_watch_function = add_function;
365 watch_list->remove_watch_function = remove_function;
366 watch_list->watch_toggled_function = toggled_function;
367 watch_list->watch_data = data;
368 watch_list->watch_free_data_function = free_data_function;
374 * Adds a new watch to the watch list, invoking the
375 * application DBusAddWatchFunction if appropriate.
377 * @param watch_list the watch list.
378 * @param watch the watch to add.
379 * @returns #TRUE on success, #FALSE if no memory.
382 _dbus_watch_list_add_watch (DBusWatchList *watch_list,
385 if (!_dbus_list_append (&watch_list->watches, watch))
388 _dbus_watch_ref (watch);
390 if (watch_list->add_watch_function != NULL)
392 _dbus_verbose ("Adding watch on fd %" DBUS_POLLABLE_FORMAT "\n",
393 _dbus_pollable_printable (watch->fd));
395 if (!(* watch_list->add_watch_function) (watch,
396 watch_list->watch_data))
398 _dbus_list_remove_last (&watch_list->watches, watch);
399 _dbus_watch_unref (watch);
408 * Removes a watch from the watch list, invoking the
409 * application's DBusRemoveWatchFunction if appropriate.
411 * @param watch_list the watch list.
412 * @param watch the watch to remove.
415 _dbus_watch_list_remove_watch (DBusWatchList *watch_list,
418 if (!_dbus_list_remove (&watch_list->watches, watch))
419 _dbus_assert_not_reached ("Nonexistent watch was removed");
421 if (watch_list->remove_watch_function != NULL)
423 _dbus_verbose ("Removing watch on fd %" DBUS_POLLABLE_FORMAT "\n",
424 _dbus_pollable_printable (watch->fd));
426 (* watch_list->remove_watch_function) (watch,
427 watch_list->watch_data);
430 _dbus_watch_unref (watch);
434 * Sets a watch to the given enabled state, invoking the
435 * application's DBusWatchToggledFunction if appropriate.
437 * @param watch_list the watch list.
438 * @param watch the watch to toggle.
439 * @param enabled #TRUE to enable
442 _dbus_watch_list_toggle_watch (DBusWatchList *watch_list,
448 if (enabled == watch->enabled)
451 watch->enabled = enabled;
453 if (watch_list->watch_toggled_function != NULL)
455 _dbus_verbose ("Toggling watch %p on fd %" DBUS_POLLABLE_FORMAT " to %d\n",
457 _dbus_pollable_printable (watch->fd),
460 (* watch_list->watch_toggled_function) (watch,
461 watch_list->watch_data);
466 * Sets all watches to the given enabled state, invoking the
467 * application's DBusWatchToggledFunction if appropriate.
469 * @param watch_list the watch list.
470 * @param enabled #TRUE to enable
473 _dbus_watch_list_toggle_all_watches (DBusWatchList *watch_list,
478 for (link = _dbus_list_get_first_link (&watch_list->watches);
480 link = _dbus_list_get_next_link (&watch_list->watches, link))
482 _dbus_watch_list_toggle_watch (watch_list, link->data, enabled);
487 * Sets the handler for the watch.
489 * @todo this function only exists because of the weird
490 * way connection watches are done, see the note
491 * in docs for _dbus_connection_handle_watch().
493 * @param watch the watch
494 * @param handler the new handler
495 * @param data the data
496 * @param free_data_function free data with this
499 _dbus_watch_set_handler (DBusWatch *watch,
500 DBusWatchHandler handler,
502 DBusFreeFunction free_data_function)
504 if (watch->free_handler_data_function)
505 (* watch->free_handler_data_function) (watch->handler_data);
507 watch->handler = handler;
508 watch->handler_data = data;
509 watch->free_handler_data_function = free_data_function;
515 * @defgroup DBusWatch DBusWatch
517 * @brief Object representing a file descriptor to be watched.
519 * Types and functions related to DBusWatch. A watch represents
520 * a file descriptor that the main loop needs to monitor,
521 * as in Qt's QSocketNotifier or GLib's g_io_add_watch().
523 * Use dbus_connection_set_watch_functions() or dbus_server_set_watch_functions()
524 * to be notified when libdbus needs to add or remove watches.
532 * Opaque object representing a file descriptor
533 * to be watched for changes in readability,
534 * writability, or hangup.
538 * Deprecated former name of dbus_watch_get_unix_fd().
540 * @param watch the DBusWatch object.
541 * @returns the file descriptor to watch.
544 dbus_watch_get_fd (DBusWatch *watch)
546 _dbus_return_val_if_fail (watch != NULL, -1);
548 return dbus_watch_get_unix_fd(watch);
552 * Returns a UNIX file descriptor to be watched,
553 * which may be a pipe, socket, or other type of
554 * descriptor. On UNIX this is preferred to
555 * dbus_watch_get_socket() since it works with
556 * more kinds of #DBusWatch.
558 * Always returns -1 on Windows. On Windows you use
559 * dbus_watch_get_socket() to get a Winsock socket to watch.
561 * @param watch the DBusWatch object.
562 * @returns the file descriptor to watch.
565 dbus_watch_get_unix_fd (DBusWatch *watch)
567 _dbus_return_val_if_fail (watch != NULL, -1);
569 /* FIXME remove #ifdef and do this on a lower level
570 * (watch should have set_socket and set_unix_fd and track
571 * which it has, and the transport should provide the
572 * appropriate watch type)
577 return dbus_watch_get_socket( watch );
582 * Returns a socket to be watched, on UNIX this will return -1 if our
583 * transport is not socket-based so dbus_watch_get_unix_fd() is
586 * On Windows, dbus_watch_get_unix_fd() returns -1 but this function
587 * returns a Winsock socket (assuming the transport is socket-based,
588 * as it always is for now).
590 * @param watch the DBusWatch object.
591 * @returns the socket to watch.
594 dbus_watch_get_socket (DBusWatch *watch)
596 _dbus_return_val_if_fail (watch != NULL, -1);
601 return _dbus_socket_get_int (watch->fd);
606 _dbus_watch_get_socket (DBusWatch *watch)
610 _dbus_assert (watch != NULL);
622 _dbus_watch_get_pollable (DBusWatch *watch)
624 _dbus_assert (watch != NULL);
630 * Gets flags from DBusWatchFlags indicating
631 * what conditions should be monitored on the
634 * The flags returned will only contain DBUS_WATCH_READABLE
635 * and DBUS_WATCH_WRITABLE, never DBUS_WATCH_HANGUP or
636 * DBUS_WATCH_ERROR; all watches implicitly include a watch
637 * for hangups, errors, and other exceptional conditions.
639 * @param watch the DBusWatch object.
640 * @returns the conditions to watch.
643 dbus_watch_get_flags (DBusWatch *watch)
645 _dbus_return_val_if_fail (watch != NULL, 0);
646 _dbus_assert ((watch->flags & VALID_WATCH_FLAGS) == watch->flags);
652 * Gets data previously set with dbus_watch_set_data()
655 * @param watch the DBusWatch object.
656 * @returns previously-set data.
659 dbus_watch_get_data (DBusWatch *watch)
661 _dbus_return_val_if_fail (watch != NULL, NULL);
667 * Sets data which can be retrieved with dbus_watch_get_data().
668 * Intended for use by the DBusAddWatchFunction and
669 * DBusRemoveWatchFunction to store their own data. For example with
670 * Qt you might store the QSocketNotifier for this watch and with GLib
671 * you might store a GSource.
673 * @param watch the DBusWatch object.
674 * @param data the data.
675 * @param free_data_function function to be called to free the data.
678 dbus_watch_set_data (DBusWatch *watch,
680 DBusFreeFunction free_data_function)
682 _dbus_return_if_fail (watch != NULL);
684 _dbus_verbose ("Setting watch fd %" DBUS_POLLABLE_FORMAT " data to data = %p function = %p from data = %p function = %p\n",
685 _dbus_pollable_printable (watch->fd),
686 data, free_data_function, watch->data, watch->free_data_function);
688 if (watch->free_data_function != NULL)
689 (* watch->free_data_function) (watch->data);
692 watch->free_data_function = free_data_function;
696 * Returns whether a watch is enabled or not. If not
697 * enabled, it should not be polled by the main loop.
699 * @param watch the DBusWatch object
700 * @returns #TRUE if the watch is enabled
703 dbus_watch_get_enabled (DBusWatch *watch)
705 _dbus_return_val_if_fail (watch != NULL, FALSE);
707 return watch->enabled;
712 * Called to notify the D-Bus library when a previously-added watch is
713 * ready for reading or writing, or has an exception such as a hangup.
715 * If this function returns #FALSE, then the file descriptor may still
716 * be ready for reading or writing, but more memory is needed in order
717 * to do the reading or writing. If you ignore the #FALSE return, your
718 * application may spin in a busy loop on the file descriptor until
719 * memory becomes available, but nothing more catastrophic should
722 * dbus_watch_handle() cannot be called during the
723 * DBusAddWatchFunction, as the connection will not be ready to handle
726 * It is not allowed to reference a DBusWatch after it has been passed
727 * to remove_function.
729 * @param watch the DBusWatch object.
730 * @param flags the poll condition using #DBusWatchFlags values
731 * @returns #FALSE if there wasn't enough memory
734 dbus_watch_handle (DBusWatch *watch,
737 _dbus_return_val_if_fail (watch != NULL, FALSE);
739 #ifndef DBUS_DISABLE_CHECKS
740 if (!_dbus_pollable_is_valid (watch->fd) || watch->flags == 0)
742 _dbus_warn_check_failed ("Watch is invalid, it should have been removed");
747 _dbus_return_val_if_fail (_dbus_pollable_is_valid (watch->fd) /* fails if watch was removed */, TRUE);
749 _dbus_watch_sanitize_condition (watch, &flags);
753 _dbus_verbose ("After sanitization, watch flags on fd %" DBUS_POLLABLE_FORMAT " were 0\n",
754 _dbus_pollable_printable (watch->fd));
758 return (* watch->handler) (watch, flags,
759 watch->handler_data);