gnome-session r4891 - in branches/dbus_based: . gnome-session



Author: mccann
Date: Wed Jul 30 03:52:48 2008
New Revision: 4891
URL: http://svn.gnome.org/viewvc/gnome-session?rev=4891&view=rev

Log:
2008-07-29  William Jon McCann  <jmccann redhat com>

	* gnome-session/gsm-client.c (gsm_client_get_unix_process_id):
	* gnome-session/gsm-client.h:
	* gnome-session/gsm-dbus-client.c (get_caller_info),
	(gsm_dbus_client_set_bus_name), (dbus_client_get_unix_process_id),
	(gsm_dbus_client_class_init):
	* gnome-session/gsm-xsmp-client.c (_parse_value_as_uint),
	(xsmp_get_unix_process_id), (gsm_xsmp_client_class_init):
	* gnome-session/org.gnome.SessionManager.Client.xml:
	Add GetUnixProcessId to Client interface.



Modified:
   branches/dbus_based/ChangeLog
   branches/dbus_based/gnome-session/gsm-client.c
   branches/dbus_based/gnome-session/gsm-client.h
   branches/dbus_based/gnome-session/gsm-dbus-client.c
   branches/dbus_based/gnome-session/gsm-xsmp-client.c
   branches/dbus_based/gnome-session/org.gnome.SessionManager.Client.xml

Modified: branches/dbus_based/gnome-session/gsm-client.c
==============================================================================
--- branches/dbus_based/gnome-session/gsm-client.c	(original)
+++ branches/dbus_based/gnome-session/gsm-client.c	Wed Jul 30 03:52:48 2008
@@ -400,6 +400,18 @@
         return TRUE;
 }
 
+gboolean
+gsm_client_get_unix_process_id (GsmClient  *client,
+                                guint      *pid,
+                                GError    **error)
+{
+        g_return_val_if_fail (GSM_IS_CLIENT (client), GSM_CLIENT_RESTART_NEVER);
+
+        *pid = GSM_CLIENT_GET_CLASS (client)->impl_get_unix_process_id (client);
+
+        return TRUE;
+}
+
 char *
 gsm_client_get_app_name (GsmClient *client)
 {

Modified: branches/dbus_based/gnome-session/gsm-client.h
==============================================================================
--- branches/dbus_based/gnome-session/gsm-client.h	(original)
+++ branches/dbus_based/gnome-session/gsm-client.h	Wed Jul 30 03:52:48 2008
@@ -76,6 +76,7 @@
         /* virtual methods */
         char *                (*impl_get_app_name)           (GsmClient *client);
         GsmClientRestartStyle (*impl_get_restart_style_hint) (GsmClient *client);
+        guint                 (*impl_get_unix_process_id)    (GsmClient *client);
         void                  (*impl_query_end_session)      (GsmClient *client,
                                                               guint      flags);
         void                  (*impl_end_session)            (GsmClient *client,
@@ -125,6 +126,9 @@
 gboolean              gsm_client_get_status                 (GsmClient  *client,
                                                              guint      *status,
                                                              GError    **error);
+gboolean              gsm_client_get_unix_process_id        (GsmClient  *client,
+                                                             guint      *pid,
+                                                             GError    **error);
 
 /* private */
 

Modified: branches/dbus_based/gnome-session/gsm-dbus-client.c
==============================================================================
--- branches/dbus_based/gnome-session/gsm-dbus-client.c	(original)
+++ branches/dbus_based/gnome-session/gsm-dbus-client.c	Wed Jul 30 03:52:48 2008
@@ -48,6 +48,7 @@
 struct GsmDBusClientPrivate
 {
         char                 *bus_name;
+        GPid                  caller_pid;
         GsmClientRestartStyle restart_style_hint;
         DBusConnection       *connection;
 };
@@ -252,16 +253,99 @@
         client->priv = GSM_DBUS_CLIENT_GET_PRIVATE (client);
 }
 
+/* adapted from PolicyKit */
+static gboolean
+get_caller_info (GsmDBusClient *client,
+                 const char    *sender,
+                 uid_t         *calling_uid,
+                 pid_t         *calling_pid)
+{
+        gboolean         res;
+        GError          *error;
+        DBusGConnection *connection;
+        DBusGProxy      *bus_proxy;
+
+        res = FALSE;
+        bus_proxy = NULL;
+
+        if (sender == NULL) {
+                goto out;
+        }
+
+        error = NULL;
+        connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
+        if (connection == NULL) {
+                if (error != NULL) {
+                        g_warning ("error getting session bus: %s", error->message);
+                        g_error_free (error);
+                }
+                goto out;
+        }
+
+        bus_proxy = dbus_g_proxy_new_for_name (connection,
+                                               DBUS_SERVICE_DBUS,
+                                               DBUS_PATH_DBUS,
+                                               DBUS_INTERFACE_DBUS);
+
+        error = NULL;
+        if (! dbus_g_proxy_call (bus_proxy, "GetConnectionUnixUser", &error,
+                                 G_TYPE_STRING, sender,
+                                 G_TYPE_INVALID,
+                                 G_TYPE_UINT, calling_uid,
+                                 G_TYPE_INVALID)) {
+                g_debug ("GetConnectionUnixUser() failed: %s", error->message);
+                g_error_free (error);
+                goto out;
+        }
+
+        error = NULL;
+        if (! dbus_g_proxy_call (bus_proxy, "GetConnectionUnixProcessID", &error,
+                                 G_TYPE_STRING, sender,
+                                 G_TYPE_INVALID,
+                                 G_TYPE_UINT, calling_pid,
+                                 G_TYPE_INVALID)) {
+                g_debug ("GetConnectionUnixProcessID() failed: %s", error->message);
+                g_error_free (error);
+                goto out;
+        }
+
+        res = TRUE;
+
+        g_debug ("uid = %d", *calling_uid);
+        g_debug ("pid = %d", *calling_pid);
+
+out:
+        if (bus_proxy != NULL) {
+                g_object_unref (bus_proxy);
+        }
+        return res;
+}
+
 static void
 gsm_dbus_client_set_bus_name (GsmDBusClient  *client,
                               const char     *bus_name)
 {
+        uid_t    uid;
+        pid_t    pid;
+
         g_return_if_fail (GSM_IS_DBUS_CLIENT (client));
 
         g_free (client->priv->bus_name);
 
         client->priv->bus_name = g_strdup (bus_name);
         g_object_notify (G_OBJECT (client), "bus-name");
+
+        if (client->priv->bus_name != NULL) {
+                gboolean res;
+
+                res = get_caller_info (client, bus_name, &uid, &pid);
+                if (! res) {
+                        pid = 0;
+                }
+        } else {
+                pid = 0;
+        }
+        client->priv->caller_pid = pid;
 }
 
 const char *
@@ -370,6 +454,12 @@
         return (GSM_DBUS_CLIENT (client)->priv->restart_style_hint);
 }
 
+static guint
+dbus_client_get_unix_process_id (GsmClient *client)
+{
+        return (GSM_DBUS_CLIENT (client)->priv->caller_pid);
+}
+
 static void
 dbus_client_query_end_session (GsmClient *client,
                                guint      flags)
@@ -495,6 +585,7 @@
         client_class->impl_cancel_end_session     = dbus_client_cancel_end_session;
         client_class->impl_get_app_name           = dbus_client_get_app_name;
         client_class->impl_get_restart_style_hint = dbus_client_get_restart_style_hint;
+        client_class->impl_get_unix_process_id    = dbus_client_get_unix_process_id;
 
         g_object_class_install_property (object_class,
                                          PROP_BUS_NAME,

Modified: branches/dbus_based/gnome-session/gsm-xsmp-client.c
==============================================================================
--- branches/dbus_based/gnome-session/gsm-xsmp-client.c	(original)
+++ branches/dbus_based/gnome-session/gsm-xsmp-client.c	Wed Jul 30 03:52:48 2008
@@ -19,15 +19,14 @@
  * 02111-1307, USA.
  */
 
-#ifdef HAVE_CONFIG_H
 #include "config.h"
-#endif
 
 #include <fcntl.h>
 #include <stdlib.h>
 #include <string.h>
 #include <time.h>
 #include <unistd.h>
+#include <errno.h>
 
 #include <glib/gi18n.h>
 
@@ -649,6 +648,55 @@
         return hint;
 }
 
+static gboolean
+_parse_value_as_uint (const char *value,
+                      guint      *uintval)
+{
+        char  *end_of_valid_uint;
+        gulong ulong_value;
+        guint  uint_value;
+
+        errno = 0;
+        ulong_value = strtoul (value, &end_of_valid_uint, 10);
+
+        if (*value == '\0' || *end_of_valid_uint != '\0') {
+                return FALSE;
+        }
+
+        uint_value = ulong_value;
+        if (uint_value != ulong_value || errno == ERANGE) {
+                return FALSE;
+        }
+
+        *uintval = uint_value;
+
+        return TRUE;
+}
+
+static guint
+xsmp_get_unix_process_id (GsmClient *client)
+{
+        SmProp  *prop;
+        guint    pid;
+        gboolean res;
+
+        g_debug ("GsmXSMPClient: getting pid");
+
+        prop = find_property (GSM_XSMP_CLIENT (client), SmProcessID, NULL);
+
+        if (!prop || strcmp (prop->type, SmARRAY8) != 0) {
+                return 0;
+        }
+
+        pid = 0;
+        res = _parse_value_as_uint ((char *)prop->vals[0].value, &pid);
+        if (! res) {
+                pid = 0;
+        }
+
+        return pid;
+}
+
 static void
 gsm_xsmp_client_class_init (GsmXSMPClientClass *klass)
 {
@@ -666,6 +714,7 @@
         client_class->impl_cancel_end_session     = xsmp_cancel_end_session;
         client_class->impl_get_app_name           = xsmp_get_app_name;
         client_class->impl_get_restart_style_hint = xsmp_get_restart_style_hint;
+        client_class->impl_get_unix_process_id    = xsmp_get_unix_process_id;
 
         signals[REGISTER_REQUEST] =
                 g_signal_new ("register-request",

Modified: branches/dbus_based/gnome-session/org.gnome.SessionManager.Client.xml
==============================================================================
--- branches/dbus_based/gnome-session/org.gnome.SessionManager.Client.xml	(original)
+++ branches/dbus_based/gnome-session/org.gnome.SessionManager.Client.xml	Wed Jul 30 03:52:48 2008
@@ -38,6 +38,18 @@
         </doc:description>
       </doc:doc>
     </method>
+    <method name="GetUnixProcessId">
+      <arg type="u" name="pid" direction="out">
+        <doc:doc>
+          <doc:summary>The Unix process identifier</doc:summary>
+        </doc:doc>
+      </arg>
+      <doc:doc>
+        <doc:description>
+          <doc:para>Return the Unix process identifier for this client.</doc:para>
+        </doc:description>
+      </doc:doc>
+    </method>
     <method name="GetStatus">
       <arg type="u" name="status" direction="out">
         <doc:doc>



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]