[gnome-keyring/dbus-api] Move lookup executable path from pid into common code.



commit 23b4a9b6c73af6471d56cc46515cf8d81035155f
Author: Stef Walter <stef memberwebs com>
Date:   Sun Jun 21 05:22:16 2009 +0000

    Move lookup executable path from pid into common code.
    
    Created egg_unix_credentials_executable() function with code
    from gkr_daemon_client_set_current() so that it can be reused
    from more locations.

 daemon/util/gkr-daemon-util.c |   31 ++++++-------------------------
 egg/egg-unix-credentials.c    |   25 +++++++++++++++++++++++++
 egg/egg-unix-credentials.h    |    2 ++
 3 files changed, 33 insertions(+), 25 deletions(-)
---
diff --git a/daemon/util/gkr-daemon-util.c b/daemon/util/gkr-daemon-util.c
index d328a0a..fc9c97e 100644
--- a/daemon/util/gkr-daemon-util.c
+++ b/daemon/util/gkr-daemon-util.c
@@ -27,12 +27,14 @@
 #include "gkr-daemon-util.h"
 
 #include "egg/egg-cleanup.h"
+#include "egg/egg-unix-credentials.h"
 
 #include <glib.h>
 
 #include <sys/stat.h>
 
 #include <errno.h>
+#include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
 
@@ -175,38 +177,17 @@ GkrDaemonClient*
 gkr_daemon_client_set_current (pid_t pid, const gchar *app_path, const gchar *app_display)
 {
 	GkrDaemonClient *client;
-	gchar *path = NULL;
+	char *path = NULL;
 	
 	/* Try and figure out the path from the pid */
-#if defined(__linux__) || defined(__FreeBSD__)
-	if (pid > 0 && !app_path) {
-		char *buffer;
-		int len;
-		char *path = NULL;
-			
-#if defined(__linux__)
-		path = g_strdup_printf ("/proc/%d/exe", (gint)pid);
-#elif defined(__FreeBSD__)
-		path = g_strdup_printf ("/proc/%d/file", (gint)pid);
-#endif
-		buffer = g_file_read_link (path, NULL);
-		g_free (path);
-
-		len = (buffer != NULL) ? strlen (buffer) : 0;
-		if (len > 0) {
-			path = g_strndup (buffer, len);
-			app_path = path;
-		}
-		
-		g_free (buffer);
-	}
-#endif
+	if (pid > 0 && !app_path)
+		app_path = path = egg_unix_credentials_executable (pid);
 	
 	client = g_object_new (GKR_TYPE_DAEMON_CLIENT, "pid", pid, "app-path", app_path, 
 	                       "app-display", app_display, NULL);
 	
 	register_client (client);
-	g_free (path);
+	free (path);
 	
 	return client;
 }
diff --git a/egg/egg-unix-credentials.c b/egg/egg-unix-credentials.c
index 4cccb2e..9de1c76 100644
--- a/egg/egg-unix-credentials.c
+++ b/egg/egg-unix-credentials.c
@@ -207,3 +207,28 @@ again:
 		
 	return 0;
 }
+
+char*
+egg_unix_credentials_executable (pid_t pid)
+{
+	char *result = NULL;
+
+	/* Try and figure out the path from the pid */
+#if defined(__linux__) || defined(__FreeBSD__)
+	char path[1024];
+	char buffer[64];
+			
+#if defined(__linux__)
+	snprintf (buffer, sizeof (buffer), "/proc/%d/exe", (int)pid);
+#elif defined(__FreeBSD__)
+	snprintf (buffer, sizeof (buffer), "/proc/%d/file", (int)pid);
+#endif
+	
+	if (readlink(buffer, path, sizeof (path)) < 0)
+		fprintf (stderr, "readlink failed for file: %s", buffer);
+	else
+		result = strdup (path);
+#endif
+
+	return result;
+}
diff --git a/egg/egg-unix-credentials.h b/egg/egg-unix-credentials.h
index b271369..a389c1e 100644
--- a/egg/egg-unix-credentials.h
+++ b/egg/egg-unix-credentials.h
@@ -29,4 +29,6 @@ int egg_unix_credentials_read (int sock, pid_t *pid, uid_t *uid);
 
 int egg_unix_credentials_write (int sock);
  
+char* egg_unix_credentials_executable (pid_t pid);
+
 #endif /*EGGUNIXCREDENTIALS_H_*/



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