[telepathy-mission-control/telepathy-mission-control-5.2] mc-wait-for-name: wait for a finite time (currently 5 minutes), and define exit status better

Simon McVittie simon.mcvittie at collabora.co.uk
Mon Sep 28 06:14:07 PDT 2009


---
 configure.ac               |    2 +-
 util/mc-wait-for-name.1.in |   18 ++++++++-----
 util/wait-for-name.c       |   56 ++++++++++++++++++++++++++++++++++++++++----
 3 files changed, 63 insertions(+), 13 deletions(-)

diff --git a/configure.ac b/configure.ac
index caefac9..c48c2f4 100644
--- a/configure.ac
+++ b/configure.ac
@@ -18,7 +18,7 @@ AC_PROG_LIBTOOL
 AM_PROG_MKDIR_P
 
 AC_HEADER_STDC
-AC_CHECK_HEADERS([sys/stat.h sys/types.h])
+AC_CHECK_HEADERS([sys/stat.h sys/types.h sysexits.h])
 AC_CHECK_FUNCS([umask])
 
 case "$PACKAGE_VERSION" in
diff --git a/util/mc-wait-for-name.1.in b/util/mc-wait-for-name.1.in
index c49f3b8..e4ce204 100644
--- a/util/mc-wait-for-name.1.in
+++ b/util/mc-wait-for-name.1.in
@@ -21,14 +21,18 @@ will be provided automatically (after a while) by the desktop session.
 .SH EXIT STATUS
 .TP
 0
-The bus name eventually appeared
+The bus name eventually appeared.
 .TP
-Non-zero
-mc-wait-for-name was unable to connect to the session bus, or some other error
-occurred
+64 (EX_USAGE)
+Invocation error (too many or too few arguments, or the bus name given was
+not a syntactically valid well-known bus name).
+.TP
+69 (EX_UNAVAILABLE)
+mc-wait-for-name was unable to connect to the session bus.
+.TP
+75 (EX_TEMPFAIL)
+The name did not appear within a reasonable time.
 .SH OPTIONS
 There are no additional command-line options.
 .SH BUGS
-If the requested well-known bus name never appears, then
-.B mc-wait-for-name
-will only exit when the session bus terminates.
+The "reasonable time" to wait is currently hard-coded.
diff --git a/util/wait-for-name.c b/util/wait-for-name.c
index 9756ca3..af8bcdb 100644
--- a/util/wait-for-name.c
+++ b/util/wait-for-name.c
@@ -30,14 +30,40 @@
  *
  */
 
+#ifdef HAVE_SYSEXITS_H
+# include <sysexits.h>
+#else
+# define EX_USAGE 64
+# define EX_UNAVAILABLE 69
+# define EX_SOFTWARE 70
+# define EX_TEMPFAIL 75
+#endif
+
 #include <glib.h>
+
 #include <telepathy-glib/dbus.h>
 
+static int exit_status = EX_SOFTWARE;
+static guint timeout_id = 0;
+static guint idle_id = 0;
+
+static gboolean
+quit_because_found (gpointer data)
+{
+  idle_id = 0;
+  g_main_loop_quit (data);
+  g_main_loop_unref (data);
+  exit_status = 0;
+  return FALSE;
+}
+
 static gboolean
-quit (gpointer data)
+quit_because_timeout (gpointer data)
 {
+  timeout_id = 0;
   g_main_loop_quit (data);
   g_main_loop_unref (data);
+  exit_status = EX_TEMPFAIL;
   return FALSE;
 }
 
@@ -54,10 +80,14 @@ noc_cb (TpDBusDaemon *bus_daemon,
   else
     {
       g_debug ("%s now owned by %s", name, new_owner);
-      g_idle_add (quit, g_main_loop_ref (data));
+
+      if (idle_id == 0)
+        idle_id = g_idle_add (quit_because_found, g_main_loop_ref (data));
     }
 }
 
+#define WFN_TIMEOUT (5 * 60) /* 5 minutes */
+
 int
 main (int argc,
       char **argv)
@@ -73,7 +103,7 @@ main (int argc,
         NULL))
     {
       g_message ("Usage: mc-wait-for-name com.example.SomeBusName");
-      return 2;
+      return EX_USAGE;
     }
 
   g_type_init ();
@@ -83,16 +113,32 @@ main (int argc,
     {
       g_message ("%s", error->message);
       g_error_free (error);
-      return 1;
+      return EX_UNAVAILABLE;
     }
 
   loop = g_main_loop_new (NULL, FALSE);
   tp_dbus_daemon_watch_name_owner (bus_daemon, argv[1],
       noc_cb, g_main_loop_ref (loop), (GDestroyNotify) g_main_loop_unref);
+
+  g_timeout_add_seconds (WFN_TIMEOUT, quit_because_timeout,
+      g_main_loop_ref (loop));
+
   g_main_loop_run (loop);
 
+  if (timeout_id != 0)
+    {
+      g_source_remove (timeout_id);
+      g_main_loop_unref (loop);
+    }
+
+  if (idle_id != 0)
+    {
+      g_source_remove (idle_id);
+      g_main_loop_unref (loop);
+    }
+
   g_main_loop_unref (loop);
   g_object_unref (bus_daemon);
 
-  return 0;
+  return exit_status;
 }
-- 
1.5.6.5




More information about the telepathy-commits mailing list