[daemon-fix] Fixed sending daemon match rules for kdbus broadcasts
[platform/upstream/dbus.git] / dbus / dbus-watch.c
index 55b182b..b82c57d 100644 (file)
@@ -1,9 +1,9 @@
-/* -*- mode: C; c-file-style: "gnu" -*- */
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
 /* dbus-watch.c DBusWatch implementation
  *
  * Copyright (C) 2002, 2003  Red Hat Inc.
  *
- * Licensed under the Academic Free License version 1.2
+ * 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
  * 
  * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ * 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"
@@ -33,6 +34,9 @@
  * @{
  */
 
+/**
+ * Implementation of DBusWatch
+ */
 struct DBusWatch
 {
   int refcount;                        /**< Reference count */
@@ -46,8 +50,28 @@ struct DBusWatch
   void *data;                          /**< Application data. */
   DBusFreeFunction free_data_function; /**< Free the application data. */
   unsigned int enabled : 1;            /**< Whether it's enabled. */
+  unsigned int oom_last_time : 1;      /**< Whether it was OOM last time. */
 };
 
+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;
+}
+
 /**
  * Creates a new DBusWatch. Used to add a file descriptor to be polled
  * by a main loop.
@@ -94,11 +118,14 @@ _dbus_watch_new (int               fd,
  * Increments the reference count of a DBusWatch object.
  *
  * @param watch the watch object.
+ * @returns the watch object.
  */
-void
+DBusWatch *
 _dbus_watch_ref (DBusWatch *watch)
 {
   watch->refcount += 1;
+
+  return watch;
 }
 
 /**
@@ -116,6 +143,9 @@ _dbus_watch_unref (DBusWatch *watch)
   watch->refcount -= 1;
   if (watch->refcount == 0)
     {
+      if (watch->fd != -1)
+        _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)
@@ -229,6 +259,25 @@ _dbus_watch_list_free (DBusWatchList *watch_list)
   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 */
+
 /**
  * Sets the watch functions. This function is the "backend"
  * for dbus_connection_set_watch_functions() and
@@ -262,8 +311,9 @@ _dbus_watch_list_set_functions (DBusWatchList           *watch_list,
           DBusList *next = _dbus_list_get_next_link (&watch_list->watches,
                                                      link);
 
-          _dbus_verbose ("Adding a watch on fd %d using newly-set add watch function\n",
-                         dbus_watch_get_fd (link->data));
+          _dbus_verbose ("Adding a %s watch on fd %d using newly-set add watch function\n",
+                         watch_flags_to_string (dbus_watch_get_flags (link->data)),
+                         dbus_watch_get_socket (link->data));
           
           if (!(* add_function) (link->data, data))
             {
@@ -277,7 +327,7 @@ _dbus_watch_list_set_functions (DBusWatchList           *watch_list,
                                                              link2);
                   
                   _dbus_verbose ("Removing watch on fd %d using newly-set remove function because initial add failed\n",
-                                 dbus_watch_get_fd (link2->data));
+                                 dbus_watch_get_socket (link2->data));
                   
                   (* remove_function) (link2->data, data);
                   
@@ -334,7 +384,7 @@ _dbus_watch_list_add_watch (DBusWatchList *watch_list,
   if (watch_list->add_watch_function != NULL)
     {
       _dbus_verbose ("Adding watch on fd %d\n",
-                     dbus_watch_get_fd (watch));
+                     dbus_watch_get_socket (watch));
       
       if (!(* watch_list->add_watch_function) (watch,
                                                watch_list->watch_data))
@@ -365,7 +415,7 @@ _dbus_watch_list_remove_watch  (DBusWatchList *watch_list,
   if (watch_list->remove_watch_function != NULL)
     {
       _dbus_verbose ("Removing watch on fd %d\n",
-                     dbus_watch_get_fd (watch));
+                     dbus_watch_get_socket (watch));
       
       (* watch_list->remove_watch_function) (watch,
                                              watch_list->watch_data);
@@ -396,8 +446,8 @@ _dbus_watch_list_toggle_watch (DBusWatchList           *watch_list,
   
   if (watch_list->watch_toggled_function != NULL)
     {
-      _dbus_verbose ("Toggling watch on fd %d to %d\n",
-                     dbus_watch_get_fd (watch), watch->enabled);
+      _dbus_verbose ("Toggling watch %p on fd %d to %d\n",
+                     watch, dbus_watch_get_socket (watch), watch->enabled);
       
       (* watch_list->watch_toggled_function) (watch,
                                               watch_list->watch_data);
@@ -440,6 +490,9 @@ _dbus_watch_set_handler (DBusWatch        *watch,
  * Types and functions related to DBusWatch. A watch represents
  * a file descriptor that the main loop needs to monitor,
  * as in Qt's QSocketNotifier or GLib's g_io_add_watch().
+ *
+ * Use dbus_connection_set_watch_functions() or dbus_server_set_watch_functions()
+ * to be notified when libdbus needs to add or remove watches.
  * 
  * @{
  */
@@ -453,14 +506,66 @@ _dbus_watch_set_handler (DBusWatch        *watch,
  */
 
 /**
- * Gets the file descriptor that should be watched.
- *
+ * Deprecated former name of dbus_watch_get_unix_fd().
+ * 
  * @param watch the DBusWatch object.
  * @returns the file descriptor to watch.
  */
 int
 dbus_watch_get_fd (DBusWatch *watch)
 {
+  _dbus_return_val_if_fail (watch != NULL, -1);
+
+  return dbus_watch_get_unix_fd(watch);
+}
+
+/**
+ * Returns a UNIX file descriptor to be watched,
+ * which may be a pipe, socket, or other type of
+ * descriptor. On UNIX this is preferred to
+ * dbus_watch_get_socket() since it works with
+ * more kinds of #DBusWatch.
+ *
+ * Always returns -1 on Windows. On Windows you use
+ * dbus_watch_get_socket() to get a Winsock socket to watch.
+ * 
+ * @param watch the DBusWatch object.
+ * @returns the file descriptor to 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
+}
+
+/**
+ * Returns a socket to be watched, on UNIX this will return -1 if our
+ * transport is not socket-based so dbus_watch_get_unix_fd() is
+ * preferred.
+ *
+ * On Windows, dbus_watch_get_unix_fd() returns -1 but this function
+ * returns a Winsock socket (assuming the transport is socket-based,
+ * as it always is for now).
+ * 
+ * @param watch the DBusWatch object.
+ * @returns the socket to watch.
+ */
+int
+dbus_watch_get_socket (DBusWatch *watch)
+{
+  _dbus_return_val_if_fail (watch != NULL, -1);
+
   return watch->fd;
 }
 
@@ -480,6 +585,7 @@ dbus_watch_get_fd (DBusWatch *watch)
 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;
@@ -495,6 +601,8 @@ dbus_watch_get_flags (DBusWatch *watch)
 void*
 dbus_watch_get_data (DBusWatch *watch)
 {
+  _dbus_return_val_if_fail (watch != NULL, NULL);
+
   return watch->data;
 }
 
@@ -514,8 +622,10 @@ dbus_watch_set_data (DBusWatch        *watch,
                      void             *data,
                      DBusFreeFunction  free_data_function)
 {
+  _dbus_return_if_fail (watch != NULL);
+
   _dbus_verbose ("Setting watch fd %d data to data = %p function = %p from data = %p function = %p\n",
-                 dbus_watch_get_fd (watch),
+                 dbus_watch_get_socket (watch),
                  data, free_data_function, watch->data, watch->free_data_function);
   
   if (watch->free_data_function != NULL)
@@ -535,12 +645,14 @@ dbus_watch_set_data (DBusWatch        *watch,
 dbus_bool_t
 dbus_watch_get_enabled (DBusWatch *watch)
 {
+  _dbus_return_val_if_fail (watch != NULL, FALSE);
+
   return watch->enabled;
 }
 
 
 /**
- * Called to notify the D-BUS library when a previously-added watch is
+ * Called to notify the D-Bus library when a previously-added watch is
  * ready for reading or writing, or has an exception such as a hangup.
  * 
  * If this function returns #FALSE, then the file descriptor may still
@@ -565,11 +677,12 @@ dbus_bool_t
 dbus_watch_handle (DBusWatch    *watch,
                    unsigned int  flags)
 {
+  _dbus_return_val_if_fail (watch != NULL, FALSE);
+
 #ifndef DBUS_DISABLE_CHECKS
   if (watch->fd < 0 || watch->flags == 0)
     {
-      _dbus_warn ("%s: Watch is invalid, it should have been removed\n",
-                  _DBUS_FUNCTION_NAME);
+      _dbus_warn_check_failed ("Watch is invalid, it should have been removed\n");
       return TRUE;
     }
 #endif