From 7fc719b7e7a260f0bdddfa52e1c66d837d458437 Mon Sep 17 00:00:00 2001
From: Ray Strode <rstrode@redhat.com>
Date: Wed, 7 May 2008 14:03:07 +0000
Subject: [PATCH 1/4] Consolidate ior and lock name getter logic to one place to facilitate

2008-05-07  Ray Strode  <rstrode@redhat.com>

	Consolidate ior and lock name getter logic to one
	place to facilitate changing that logic

	* bonobo-activation/bonobo-activation-private.h:
	* bonobo-activation/bonobo-activation-base-service.c:
	(get_lock_fname), (get_ior_fname),
	(_bonobo_activation_lock_fname_get),
	(_bonobo_activation_ior_fname_get): Rename and export
	get_lock_fname and get_ior_fname.
	(rloc_file_lock), (rloc_file_check), (rloc_file_register),
	(rloc_file_unregister): adapt for renamed functions
	* activation-server/activation-server-main.c
	(dump_ior): get ior name from libbonobo-activation instead
	of hardcoding it to "bonobo-activation-server-ior"

svn path=/trunk/; revision=3460
---
 ChangeLog                                          |   17 +++++++++++++++++
 activation-server/activation-server-main.c         |    4 +---
 bonobo-activation/bonobo-activation-base-service.c |   18 +++++++++---------
 bonobo-activation/bonobo-activation-private.h      |    3 +++
 4 files changed, 30 insertions(+), 12 deletions(-)

diff --git a/activation-server/activation-server-main.c b/activation-server/activation-server-main.c
index 6271916..8696d36 100644
--- a/activation-server/activation-server-main.c
+++ b/activation-server/activation-server-main.c
@@ -363,9 +363,7 @@ dump_ior (CORBA_ORB orb, int dev_null_fd, CORBA_Environment *ev)
 #endif
         if (server_reg) {
                 char *fname;
-                fname = g_build_filename (ORBit_get_safe_tmp (),
-                                          "bonobo-activation-server-ior",
-                                          NULL);
+                fname = _bonobo_activation_ior_fname_get ();
                 fh = g_fopen (fname, "w+");
 		fprintf (fh, "%s\n", ior);
 		fclose (fh);
diff --git a/bonobo-activation/bonobo-activation-base-service.c b/bonobo-activation/bonobo-activation-base-service.c
index a8f410f..813e11b 100644
--- a/bonobo-activation/bonobo-activation-base-service.c
+++ b/bonobo-activation/bonobo-activation-base-service.c
@@ -498,16 +498,16 @@ get_tmpdir (void)
         return tmpdir;
 }
 
-static char *
-get_lock_fname (void)
+char *
+_bonobo_activation_lock_fname_get (void)
 {
         return g_build_filename (get_tmpdir (),
                                  "bonobo-activation-register.lock",
                                  NULL);
 }
 
-static char *
-get_ior_fname (void)
+char *
+_bonobo_activation_ior_fname_get (void)
 {
         return g_build_filename (get_tmpdir (),
                                  "bonobo-activation-server-ior",
@@ -543,7 +543,7 @@ rloc_file_lock (const BonoboActivationBaseServiceRegistry *registry,
         int retval;
         char *err;
 
-        fn = get_lock_fname ();
+        fn = _bonobo_activation_lock_fname_get ();
 
 	while ((lock_fd = open (fn, O_CREAT | O_RDWR, 0700)) < 0) {
 
@@ -577,7 +577,7 @@ rloc_file_lock (const BonoboActivationBaseServiceRegistry *registry,
 
         g_free (fn);
 #elif defined (G_OS_WIN32)
-	char *fn = get_lock_fname ();
+	char *fn = _bonobo_activation_lock_fname_get ();
         wchar_t *wfn = g_utf8_to_utf16 (fn, -1, NULL, NULL, NULL);
         
 	while ((lock_fd = _wsopen (wfn, O_CREAT|O_RDWR|_O_SHORT_LIVED|_O_NOINHERIT, _SH_DENYRW, 0700)) < 0) {
@@ -630,7 +630,7 @@ rloc_file_check (const BonoboActivationBaseServiceRegistry *registry,
 	FILE *fh;
 	char *fn;
 
-        fn = get_ior_fname ();
+        fn = _bonobo_activation_ior_fname_get ();
 	fh = g_fopen (fn, "r");
         g_free (fn);
 
@@ -662,7 +662,7 @@ rloc_file_register (const BonoboActivationBaseServiceRegistry *registry, const c
 	char *fn;
 	FILE *fh;
 
-        fn = get_ior_fname ();
+        fn = _bonobo_activation_ior_fname_get ();
 	fh = g_fopen (fn, "w");
 
         if (fh != NULL) {
@@ -681,7 +681,7 @@ rloc_file_unregister (const BonoboActivationBaseServiceRegistry *registry,
 {
 	char *fn;
 
-	g_unlink ((fn = get_ior_fname ()));
+	g_unlink ((fn = _bonobo_activation_ior_fname_get ()));
         g_free (fn);
 }
 
diff --git a/bonobo-activation/bonobo-activation-private.h b/bonobo-activation/bonobo-activation-private.h
index d8f6744..82590f3 100644
--- a/bonobo-activation/bonobo-activation-private.h
+++ b/bonobo-activation/bonobo-activation-private.h
@@ -85,6 +85,9 @@ Bonobo_ActivationClient bonobo_activation_client_get (void);
     /* used only for unit testing */
 CORBA_char * _bonobo_activation_get_activation_env_value (const char *name);
 
+char * _bonobo_activation_ior_fname_get (void);
+char * _bonobo_activation_lock_fname_get (void);
+
 #ifdef G_OS_WIN32
 
 char *_bonobo_activation_win32_replace_prefix (const char *runtime_prefix,
-- 
1.7.3


From 1470fbb20ae1670c0b776db9d4d448b201c10fea Mon Sep 17 00:00:00 2001
From: Ray Strode <rstrode@redhat.com>
Date: Wed, 7 May 2008 14:08:20 +0000
Subject: [PATCH 2/4] Key ior and lock filenames of dbus session guid

2008-05-07  Ray Strode  <rstrode@redhat.com>

	Key ior and lock filenames of dbus session guid

	* bonobo-activation/bonobo-activation-base-service.c
	(get_session_guid): New function to extract guid from
	$DBUS_SESSION_BUS_ADDRESS
	(_bonobo_activation_lock_fname_get),
	(_bonobo_activation_ior_fname_get): encode result of
	of get_session_guid () into returned filenames

diff --git a/bonobo-activation/bonobo-activation-base-service.c b/bonobo-activation/bonobo-activation-base-service.c
index 813e11b..0ddeb44 100644
--- a/bonobo-activation/bonobo-activation-base-service.c
+++ b/bonobo-activation/bonobo-activation-base-service.c
@@ -498,20 +498,60 @@ get_tmpdir (void)
         return tmpdir;
 }
 
+static const char *
+get_session_guid (void)
+{
+        const char *session_bus_address;
+        const char *guid;
+
+        /* FIXME: we may want to use dbus-address.h functions here
+         */
+        session_bus_address = g_getenv ("DBUS_SESSION_BUS_ADDRESS");
+
+        if (session_bus_address == NULL)
+                return NULL;
+
+        guid = g_strrstr (session_bus_address, "guid=");
+
+        if (guid == NULL)
+                return NULL;
+
+        if (guid[0] == '\0')
+                return NULL;
+
+        return guid + strlen ("guid=");
+}
+
 char *
 _bonobo_activation_lock_fname_get (void)
 {
-        return g_build_filename (get_tmpdir (),
-                                 "bonobo-activation-register.lock",
-                                 NULL);
+       const char *session_guid;
+
+       session_guid = get_session_guid ();
+
+       if (session_guid == NULL)
+               return g_build_filename (get_tmpdir (),
+                                        "bonobo-activation-register.lock",
+                                        NULL);
+
+       return g_strdup_printf ("%s" G_DIR_SEPARATOR_S "bonobo-activation-register-%s.lock",
+                               get_tmpdir (), session_guid);
 }
 
 char *
 _bonobo_activation_ior_fname_get (void)
 {
-        return g_build_filename (get_tmpdir (),
-                                 "bonobo-activation-server-ior",
-                                 NULL);
+       const char *session_guid;
+
+       session_guid = get_session_guid ();
+
+       if (session_guid == NULL)
+               return g_build_filename (get_tmpdir (),
+                                        "bonobo-activation-server-ior",
+                                        NULL);
+
+       return g_strdup_printf ("%s" G_DIR_SEPARATOR_S "bonobo-activation-server-%s-ior",
+                               get_tmpdir (), session_guid);
 }
 
 static void
-- 
1.7.3


From 65c3ce0b8c73eb7c2cfeea840b8064e7f2a08fef Mon Sep 17 00:00:00 2001
From: Ray Strode <rstrode@redhat.com>
Date: Wed, 7 May 2008 14:14:30 +0000
Subject: [PATCH 3/4] Make bonobo-activation-server exit when dbus session exits
diff --git a/activation-server/activation-server-main.c b/activation-server/activation-server-main.c
index 8696d36..23da08b 100644
--- a/activation-server/activation-server-main.c
+++ b/activation-server/activation-server-main.c
@@ -54,6 +54,8 @@
 
 #include <glib/gstdio.h>
 
+#include <dbus/dbus-glib-lowlevel.h>
+
 static gboolean        server_threaded = FALSE;
 static glong           server_guard_depth = 0;
 static GStaticRecMutex server_guard = G_STATIC_REC_MUTEX_INIT;
@@ -373,6 +375,21 @@ dump_ior (CORBA_ORB orb, int dev_null_fd, CORBA_Environment *ev)
 	CORBA_free (ior);
 }
 
+static DBusHandlerResult
+bus_message_handler (DBusConnection *connection,
+                     DBusMessage    *message,
+                     GMainLoop      *loop)
+{
+        if (dbus_message_is_signal (message,
+                                    DBUS_INTERFACE_LOCAL,
+                                    "Disconnected")) {
+                g_main_loop_quit (loop);
+                return DBUS_HANDLER_RESULT_HANDLED;
+        }
+
+        return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+}
+
 int
 main (int argc, char *argv[])
 {
@@ -396,6 +413,8 @@ main (int argc, char *argv[])
 	const gchar                  *debug_output_env;
 #endif
 	GError                       *error = NULL;
+        DBusConnection               *connection;
+        DBusError                     bus_error;
 
 #ifdef HAVE_SETSID
         /*
@@ -533,6 +552,27 @@ main (int argc, char *argv[])
 
 	od_finished_internal_registration (); 
 
+        dbus_error_init (&bus_error);
+        connection = dbus_bus_get (DBUS_BUS_SESSION, &bus_error);
+
+        if (dbus_error_is_set (&bus_error)) {
+                g_warning ("could not associate with desktop session: %s",
+                           bus_error.message);
+                connection = NULL;
+        } else {
+                GMainContext *main_context;
+
+                main_context = g_main_loop_get_context (main_loop);
+                dbus_connection_setup_with_g_main (connection, main_context);
+
+                if (dbus_connection_add_filter (connection,
+                                                (DBusHandleMessageFunction)
+                                                bus_message_handler, main_loop,
+                                                NULL)) {
+                        dbus_connection_set_exit_on_disconnect (connection, FALSE);
+                }
+        }
+
         if (getenv ("BONOBO_ACTIVATION_DEBUG") == NULL)
                 chdir ("/");
 
diff --git a/configure.in b/configure.in
index dd33403..32815c3 100644
--- a/configure.in
+++ b/configure.in
@@ -121,6 +121,8 @@ PKG_CHECK_MODULES(SERVER, \
 	gmodule-2.0 >= 2.0.1 \
 	ORBit-2.0 >= 2.4.0 \
 	ORBit-CosNaming-2.0 >= 2.4.0 \
+	dbus-1 >= 1.0.0 \
+	dbus-glib-1 >= 0.73 \
 	libxml-2.0 >= 2.4.20)
 AC_SUBST(SERVER_LIBS)
 AC_SUBST(SERVER_CFLAGS)
-- 
1.7.3


From b754d892ae9ec84effb73b408c5f181ed2887ae1 Mon Sep 17 00:00:00 2001
From: Ray Strode <rstrode@redhat.com>
Date: Wed, 7 May 2008 14:16:03 +0000
Subject: [PATCH 4/4] remove ior and lock files on exit path
diff --git a/activation-server/activation-server-main.c b/activation-server/activation-server-main.c
index 23da08b..f055afb 100644
--- a/activation-server/activation-server-main.c
+++ b/activation-server/activation-server-main.c
@@ -375,6 +375,20 @@ dump_ior (CORBA_ORB orb, int dev_null_fd, CORBA_Environment *ev)
 	CORBA_free (ior);
 }
 
+static void
+cleanup_ior_and_lock_files (void)
+{
+    char *fname;
+
+    fname = _bonobo_activation_ior_fname_get ();
+    g_unlink (fname);
+    g_free (fname);
+
+    fname = _bonobo_activation_lock_fname_get ();
+    g_unlink (fname);
+    g_free (fname);
+}
+
 static DBusHandlerResult
 bus_message_handler (DBusConnection *connection,
                      DBusMessage    *message,
@@ -588,6 +602,8 @@ main (int argc, char *argv[])
         bonobo_object_directory_shutdown (threaded_poa, ev);
         activation_context_shutdown ();
 
+        cleanup_ior_and_lock_files ();
+
 #ifdef HAVE_SYSLOG_H
 	closelog ();
 	g_free (syslog_ident);
-- 
1.7.3

