2003-10-12 Havoc Pennington <hp@pobox.com>
authorHavoc Pennington <hp@redhat.com>
Sun, 12 Oct 2003 05:59:39 +0000 (05:59 +0000)
committerHavoc Pennington <hp@redhat.com>
Sun, 12 Oct 2003 05:59:39 +0000 (05:59 +0000)
        Added test code that 1) starts an actual bus daemon and 2) uses
DBusGProxy; fixed bugs that were revealed by the test. Lots
more testing possible, but this is the basic framework.

* glib/dbus-gproxy.c (dbus_gproxy_manager_unregister): remove
empty proxy lists from the proxy list hash

* dbus/dbus-message.c (dbus_message_iter_get_args_valist): add a
couple of return_if_fail checks

* dbus/dbus-pending-call.c (_dbus_pending_call_new): use dbus_new0
to allocate, so everything is cleared to NULL as it should be.

* glib/dbus-gmain.c (dbus_connection_setup_with_g_main): pass
source as data to dbus_connection_set_timeout_functions() as the
timeout functions expected

* test/glib/run-test.sh: add a little script to start up a message
bus and run tests using it

* tools/dbus-launch.1: updates

* tools/dbus-launch.c (main): add --config-file option

* tools/dbus-launch.c (main): remove confusing else if (runprog)
that could never be reached.

* dbus/dbus-message.c (dbus_message_new_method_return)
(dbus_message_new_error, dbus_message_new_signal): set the
no-reply-expected flag on all these. Redundant, but may
as well be consistent.

12 files changed:
ChangeLog
bus/connection.c
dbus/dbus-marshal.c
dbus/dbus-message.c
dbus/dbus-pending-call.c
doc/TODO
glib/dbus-gmain.c
glib/dbus-gproxy.c
test/glib/Makefile.am
test/glib/test-dbus-glib.c
tools/dbus-launch.1
tools/dbus-launch.c

index 272f840..21e6f14 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,37 @@
+2003-10-12  Havoc Pennington  <hp@pobox.com>
+
+        Added test code that 1) starts an actual bus daemon and 2) uses
+       DBusGProxy; fixed bugs that were revealed by the test. Lots 
+       more testing possible, but this is the basic framework.
+       
+       * glib/dbus-gproxy.c (dbus_gproxy_manager_unregister): remove
+       empty proxy lists from the proxy list hash
+
+       * dbus/dbus-message.c (dbus_message_iter_get_args_valist): add a
+       couple of return_if_fail checks
+
+       * dbus/dbus-pending-call.c (_dbus_pending_call_new): use dbus_new0
+       to allocate, so everything is cleared to NULL as it should be.
+
+       * glib/dbus-gmain.c (dbus_connection_setup_with_g_main): pass
+       source as data to dbus_connection_set_timeout_functions() as the 
+       timeout functions expected
+
+       * test/glib/run-test.sh: add a little script to start up a message
+       bus and run tests using it
+
+       * tools/dbus-launch.1: updates
+
+       * tools/dbus-launch.c (main): add --config-file option
+
+       * tools/dbus-launch.c (main): remove confusing else if (runprog)
+       that could never be reached.
+
+       * dbus/dbus-message.c (dbus_message_new_method_return) 
+       (dbus_message_new_error, dbus_message_new_signal): set the
+       no-reply-expected flag on all these. Redundant, but may
+       as well be consistent.
+
 2003-10-11  Havoc Pennington  <hp@pobox.com>
 
        * test/decode-gcov.c (function_solve_graph): make broken block
index 68e4158..6a4b55d 100644 (file)
 
 static void bus_connection_remove_transactions (DBusConnection *connection);
 
+typedef struct
+{
+  BusExpireItem expire_item;
+
+  DBusConnection *will_get_reply;
+  DBusConnection *will_send_reply;
+
+  dbus_uint32_t reply_serial;
+  
+} BusPendingReply;
+
 struct BusConnections
 {
   int refcount;
index 1e4992a..ecc1e1a 100644 (file)
@@ -1400,7 +1400,7 @@ _dbus_demarshal_double_array (const DBusString  *str,
  * @param pos the position in the string
  * @param new_pos the new position of the string
  * @param array the array
- * @param array_len length of the demarshaled data
+ * @param array_len location for length of the demarshaled data or NULL
  * @returns #TRUE on success
  */
 dbus_bool_t
index e23e56e..4530976 100644 (file)
@@ -1224,6 +1224,8 @@ dbus_message_new_method_return (DBusMessage *method_call)
       return NULL;
     }
 
+  dbus_message_set_no_reply (message, TRUE);
+
   if (!dbus_message_set_reply_serial (message,
                                       dbus_message_get_serial (method_call)))
     {
@@ -1268,6 +1270,8 @@ dbus_message_new_signal (const char *path,
       dbus_message_unref (message);
       return NULL;
     }
+
+  dbus_message_set_no_reply (message, TRUE);
   
   return message;
 }
@@ -1313,6 +1317,8 @@ dbus_message_new_error (DBusMessage *reply_to,
       return NULL;
     }
 
+  dbus_message_set_no_reply (message, TRUE);
+  
   if (!dbus_message_set_reply_serial (message,
                                       dbus_message_get_serial (reply_to)))
     {
@@ -2207,6 +2213,9 @@ dbus_message_iter_get_args_valist (DBusMessageIter *iter,
            data = va_arg (var_args, void *);
            len = va_arg (var_args, int *);
 
+            _dbus_return_val_if_fail (data != NULL, FALSE);
+            _dbus_return_val_if_fail (len != NULL, FALSE);
+            
            if (dbus_message_iter_get_array_type (iter) != type)
              {
                dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
index dad444e..e0d8e3e 100644 (file)
@@ -64,7 +64,7 @@ _dbus_pending_call_new (DBusConnection    *connection,
   if (!dbus_pending_call_allocate_data_slot (&notify_user_data_slot))
     return NULL;
   
-  pending = dbus_new (DBusPendingCall, 1);
+  pending = dbus_new0 (DBusPendingCall, 1);
   
   if (pending == NULL)
     {
index 8c57888..ca35957 100644 (file)
--- a/doc/TODO
+++ b/doc/TODO
 
  - dbus_message_iter_init_array_iterator has "iter" and "iterator" 
    in the same function name
+
+ - the GLib bindings varargs take DBUS_TYPE_WHATEVER and 
+   return stuff allocated with dbus_malloc(); should this 
+   be made more "G" at some expense in code duplication?
index 7c845d5..6f0737b 100644 (file)
@@ -492,7 +492,7 @@ dbus_connection_setup_with_g_main (DBusConnection *connection,
                                               add_timeout,
                                               remove_timeout,
                                               timeout_toggled,
-                                              NULL, NULL))
+                                              source, NULL))
     goto nomem;
     
   dbus_connection_set_wakeup_main_function (connection,
index 13b4248..f5e5918 100644 (file)
@@ -460,7 +460,7 @@ dbus_gproxy_manager_unregister (DBusGProxyManager *manager,
 #ifndef G_DISABLE_CHECKS
   if (manager->proxy_lists == NULL)
     {
-      g_warning ("Trying to disconnect a signal on a proxy but none are connected\n");
+      g_warning ("Trying to unregister a proxy but there aren't any registered");
       return;
     }
 #endif
@@ -468,13 +468,11 @@ dbus_gproxy_manager_unregister (DBusGProxyManager *manager,
   tri = tristring_from_proxy (proxy);
   
   list = g_hash_table_lookup (manager->proxy_lists, tri);
-  
-  g_free (tri);
 
 #ifndef G_DISABLE_CHECKS
   if (list == NULL)
     {
-      g_warning ("Trying to disconnect a signal on a proxy but none are connected\n");
+      g_warning ("Trying to unregister a proxy but it isn't registered");
       return;
     }
 #endif
@@ -485,12 +483,21 @@ dbus_gproxy_manager_unregister (DBusGProxyManager *manager,
 
   g_assert (g_slist_find (list->proxies, proxy) == NULL);
 
+  if (list->proxies == NULL)
+    {
+      g_hash_table_remove (manager->proxy_lists,
+                           tri);
+      list = NULL;
+    }
+  
   if (g_hash_table_size (manager->proxy_lists) == 0)
     {
       g_hash_table_destroy (manager->proxy_lists);
       manager->proxy_lists = NULL;
     }
-  
+
+  g_free (tri);
+      
   UNLOCK_MANAGER (manager);
 }
 
index 9f900b1..608e7d0 100644 (file)
@@ -1,5 +1,17 @@
 INCLUDES=-I$(top_srcdir) $(DBUS_CLIENT_CFLAGS) $(DBUS_GLIB_CFLAGS) -I$(top_srcdir)/glib
 
+## note that TESTS has special meaning (stuff to use in make check)
+## so if adding tests not to be run in make check, don't add them to 
+## TESTS
+if DBUS_BUILD_TESTS
+TESTS_ENVIRONMENT=DBUS_TOP_BUILDDIR=$(top_builddir) DBUS_TEST_HOMEDIR=$(top_builddir)/dbus
+TESTS=run-test.sh
+else
+TESTS=
+endif
+
+EXTRA_DIST=run-test.sh
+
 if DBUS_BUILD_TESTS
 
 if HAVE_GLIB_THREADS
index beda0a7..c302b23 100644 (file)
@@ -1,52 +1,74 @@
 /* -*- mode: C; c-file-style: "gnu" -*- */
 #include "dbus-glib.h"
 #include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
 
 int
 main (int argc, char **argv)
 {
   DBusConnection *connection;
-  DBusMessage *message, *reply;  
   GMainLoop *loop;
-  DBusError error;
-  
-  if (argc < 2)
-    {
-      g_printerr ("Give the server address as an argument\n");
-      return 1;
-    }
+  GError *error;
+  DBusGProxy *driver;
+  DBusPendingCall *call;
+  char **service_list;
+  int service_list_len;
+  int i;
 
+  g_type_init ();
+  
   loop = g_main_loop_new (NULL, FALSE);
 
-  dbus_error_init (&error);
-  connection = dbus_connection_open (argv[1], &error);
+  error = NULL;
+  connection = dbus_bus_get_with_g_main (DBUS_BUS_SESSION,
+                                         &error);
   if (connection == NULL)
     {
-      g_printerr ("Failed to open connection to %s: %s\n", argv[1],
-                  error.message);
-      dbus_error_free (&error);
-      return 1;
+      g_printerr ("Failed to open connection to bus: %s\n",
+                  error->message);
+      g_error_free (error);
+      exit (1);
     }
 
-  dbus_connection_setup_with_g_main (connection, NULL);
+  /* Create a proxy object for the "bus driver" */
+  
+  driver = dbus_gproxy_new_for_service (connection,
+                                        DBUS_SERVICE_ORG_FREEDESKTOP_DBUS,
+                                        DBUS_PATH_ORG_FREEDESKTOP_DBUS,
+                                        DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS);
+
+  /* Call ListServices method */
+  
+  call = dbus_gproxy_begin_call (driver, "ListServices", DBUS_TYPE_INVALID);
 
-  message = dbus_message_new_method_call (DBUS_SERVICE_ORG_FREEDESKTOP_DBUS,
-                                          DBUS_PATH_ORG_FREEDESKTOP_DBUS,
-                                          DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
-                                          "Hello");
+  error = NULL;
+  if (!dbus_gproxy_end_call (driver, call, &error,
+                             DBUS_TYPE_ARRAY, DBUS_TYPE_STRING,
+                             &service_list, &service_list_len,
+                             DBUS_TYPE_INVALID))
+    {
+      g_printerr ("Failed to complete ListServices call: %s\n",
+                  error->message);
+      g_error_free (error);
+      exit (1);
+    }
 
-  dbus_error_init (&error);
-  reply = dbus_connection_send_with_reply_and_block (connection, message, -1, &error);
-  if (reply == NULL)
+  g_print ("Services on the message bus:\n");
+  i = 0;
+  while (i < service_list_len)
     {
-      g_printerr ("Error on hello message: %s\n", error.message);
-      dbus_error_free (&error);
-      return 1;
+      g_assert (service_list[i] != NULL);
+      g_print ("  %s\n", service_list[i]);
+      ++i;
     }
+  g_assert (service_list[i] == NULL);
   
-  g_print ("reply received\n");
+  dbus_free_string_array (service_list);
+
+  g_object_unref (G_OBJECT (driver));
   
-  g_main_loop_run (loop);
+  g_print ("Successfully completed %s\n", argv[0]);
   
   return 0;
 }
index 95708c7..771dcb2 100644 (file)
@@ -7,7 +7,7 @@
 dbus-launch \- Utility to start a message bus from a shell script
 .SH SYNOPSIS
 .PP
-.B dbus-launch [\-\-version] [\-\-sh-syntax] [\-\-csh-syntax] [\-\-auto-syntax] [\-\-exit-with-session] [PROGRAM] [ARGS...]
+.B dbus-launch [\-\-version] [\-\-sh-syntax] [\-\-csh-syntax] [\-\-auto-syntax] [\-\-exit-with-session] [\-\-config-file=FILENAME] [PROGRAM] [ARGS...]
 
 .SH DESCRIPTION
 
@@ -25,15 +25,18 @@ will then set the appropriate environment variables and execute the
 specified program, with the specified arguments.  See below for
 examples.
 
-Finally, you may use the \-\-auto-syntax command to cause
-\fIdbus-launch\fP to emit shell code to set up the environment.  This
-is useful in shell scripts.  With this option, \fIdbus-launch\fP looks
-at the value of the SHELL environment variable to determine which
-shell syntax should be used.  If SHELL ends in "csh", then
-csh-compatible code is emitted; otherwise Bourne shell code is
-emitted.  Instead of passing \-\-auto-syntax, you may explicity
-specify a particular one by using \-\-sh-syntax for Bourne syntax, or
-\-\-csh-syntax for csh syntax.
+Finally, you may use the \-\-sh-syntax, \-\-csh-syntax, or
+\-\-auto-syntax commands to cause \fIdbus-launch\fP to emit shell code
+to set up the environment.  This is useful in shell scripts.
+
+With the \-\-auto-syntax option, \fIdbus-launch\fP looks at the value
+of the SHELL environment variable to determine which shell syntax
+should be used.  If SHELL ends in "csh", then csh-compatible code is
+emitted; otherwise Bourne shell code is emitted.  Instead of passing
+\-\-auto-syntax, you may explicity specify a particular one by using
+\-\-sh-syntax for Bourne syntax, or \-\-csh-syntax for csh syntax.
+In scripts, it's more robust to avoid \-\-auto-syntax and you hopefully
+know which shell your script is written in.
 
 .PP
 See http://www.freedesktop.org/software/dbus/ for more information
@@ -47,7 +50,7 @@ sh-compatible shell to start the per-session bus daemon:
   ## test for an existing bus daemon, just to be safe
   if test -z "$DBUS_SESSION_BUS_ADDRESS" ; then
       ## if not found, launch a new one
-      eval `dbus-launch --auto-syntax --exit-with-session`
+      eval `dbus-launch --sh-syntax --exit-with-session`
       echo "D-BUS per-session daemon address is: $DBUS_SESSION_BUS_ADDRESS"
   fi
 
@@ -62,17 +65,22 @@ program, like so:
 dbus-launch gnome-session
 
 .fi
-The above would likely be appropriate for ~/.xsession.
+The above would likely be appropriate for ~/.xsession or ~/.Xclients.
 
 .SH OPTIONS
 The following options are supported:
 .TP
 .I "--auto-syntax"
-Attempt to detect the shell in use, and emit compatible code.
+Choose \-\-csh-syntax or \-\-sh-syntax based on the SHELL environment variable.
+
+.TP
+.I "--config-file=FILENAME"
+Pass \-\-config-file=FILENAME to the bus daemon, instead of passing it 
+the \-\-session argument. See the man page for dbus-daemon-1
 
 .TP
 .I "--csh-syntax"
-Emit csh compatible code.
+Emit csh compatible code to set up environment variables.
 
 .TP
 .I "--exit-with-session"
@@ -83,7 +91,7 @@ it kills the message bus daemon.
 
 .TP
 .I "--sh-syntax"
-Emit Bourne-shell compatible code.
+Emit Bourne-shell compatible code to set up environment variables.
 
 .TP
 .I "--version"
index 3b42901..dc9dad4 100644 (file)
@@ -593,8 +593,10 @@ main (int argc, char **argv)
   int bus_pid_to_launcher_pipe[2];
   int bus_pid_to_babysitter_pipe[2];
   int bus_address_to_launcher_pipe[2];
+  char *config_file;
   
   exit_with_session = FALSE;
+  config_file = NULL;
   
   prev_arg = NULL;
   i = 1;
@@ -618,8 +620,34 @@ main (int argc, char **argv)
         version ();
       else if (strcmp (arg, "--exit-with-session") == 0)
         exit_with_session = TRUE;
-      else if (runprog)
-       usage (1);
+      else if (strstr (arg, "--config-file=") == arg)
+        {
+          const char *file;
+
+          if (config_file != NULL)
+            {
+              fprintf (stderr, "--config-file given twice\n");
+              exit (1);
+            }
+          
+          file = strchr (arg, '=');
+          ++file;
+
+          config_file = xstrdup (file);
+        }
+      else if (prev_arg &&
+               strcmp (prev_arg, "--config-file") == 0)
+        {
+          if (config_file != NULL)
+            {
+              fprintf (stderr, "--config-file given twice\n");
+              exit (1);
+            }
+
+          config_file = xstrdup (arg);
+        }
+      else if (strcmp (arg, "--config-file") == 0)
+        ; /* wait for next arg */
       else
        {
          runprog = arg;
@@ -736,9 +764,10 @@ main (int argc, char **argv)
       execlp ("dbus-daemon-1",
               "dbus-daemon-1",
               "--fork",
-              "--session",
               "--print-pid", write_pid_fd_as_string,
               "--print-address", write_address_fd_as_string,
+              config_file ? "--config-file" : "--session",
+              config_file, /* has to be last in this varargs list */
               NULL);
 
       fprintf (stderr,