[PATCH 2/3] agent,common: move get_socket_name() into common.

Daniel Kahn Gillmor dkg at fifthhorseman.net
Wed Oct 26 22:37:07 CEST 2016


* agent/gpg-agent.c (get_socket_name): move to ...
* common/sysutils.c (gnupg_get_socket_name): ... here.

This allows us to use the same functionality in dirmngr as well.

Signed-off-by: Daniel Kahn Gillmor <dkg at fifthhorseman.net>
---
 agent/gpg-agent.c | 50 ++------------------------------------------------
 common/sysutils.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++
 common/sysutils.h |  1 +
 3 files changed, 53 insertions(+), 48 deletions(-)

diff --git a/agent/gpg-agent.c b/agent/gpg-agent.c
index 6f73fc7..7294c69 100644
--- a/agent/gpg-agent.c
+++ b/agent/gpg-agent.c
@@ -573,52 +573,6 @@ remove_socket (char *name, char *redir_name)
 }
 
 
-/* Return a malloc'ed string that is the path to the passed
- * unix-domain socket (or return NULL if this is not a valid
- * unix-domain socket).  We use a plain int here because it is only
- * used on Linux.
- *
- * FIXME: This function needs to be moved to libassuan.  */
-#ifndef HAVE_W32_SYSTEM
-static char *
-get_socket_name (int fd)
-{
-  struct sockaddr_un un;
-  socklen_t len = sizeof(un);
-  char *name = NULL;
-
-  if (getsockname (fd, (struct sockaddr*)&un, &len) != 0)
-    log_error ("could not getsockname(%d): %s\n", fd,
-               gpg_strerror (gpg_error_from_syserror ()));
-  else if (un.sun_family != AF_UNIX)
-    log_error ("file descriptor %d is not a unix-domain socket\n", fd);
-  else if (len <= offsetof (struct sockaddr_un, sun_path))
-    log_error ("socket name not present for file descriptor %d\n", fd);
-  else if (len > sizeof(un))
-    log_error ("socket name for file descriptor %d was truncated "
-               "(passed %zu bytes, wanted %u)\n", fd, sizeof(un), len);
-  else
-    {
-      size_t namelen = len - offsetof (struct sockaddr_un, sun_path);
-
-      log_debug ("file descriptor %d has path %s (%zu octets)\n", fd,
-                 un.sun_path, namelen);
-      name = xtrymalloc (namelen + 1);
-      if (!name)
-        log_error ("failed to allocate memory for name of fd %d: %s\n",
-                   fd, gpg_strerror (gpg_error_from_syserror ()));
-      else
-        {
-          memcpy (name, un.sun_path, namelen);
-          name[namelen] = 0;
-        }
-    }
-
-  return name;
-}
-#endif /*!HAVE_W32_SYSTEM*/
-
-
 /* Discover which inherited file descriptors correspond to which
  * services/sockets offered by gpg-agent, using the LISTEN_FDS and
  * LISTEN_FDNAMES convention.  The understood labels are "ssh",
@@ -727,7 +681,7 @@ map_supervised_sockets (gnupg_fd_t *r_fd,
         log_fatal ("file descriptor 3 must be valid in --supervised mode"
                    " if LISTEN_FDNAMES is not set\n");
       *r_fd = 3;
-      socket_name = get_socket_name (3);
+      socket_name = gnupg_get_socket_name (3);
     }
   else if (fd_count != nfdnames)
     {
@@ -749,7 +703,7 @@ map_supervised_sockets (gnupg_fd_t *r_fd,
                   fd = 3 + i;
                   if (**tbl[j].fdaddr == -1)
                     {
-                      name = get_socket_name (fd);
+                      name = gnupg_get_socket_name (fd);
                       if (name)
                         {
                           **tbl[j].fdaddr = fd;
diff --git a/common/sysutils.c b/common/sysutils.c
index 3a08df7..e04e10b 100644
--- a/common/sysutils.c
+++ b/common/sysutils.c
@@ -62,6 +62,9 @@
 #  include <winsock2.h>
 # endif
 # include <windows.h>
+#else /*!HAVE_W32_SYSTEM*/
+# include <sys/socket.h>
+# include <sys/un.h>
 #endif
 #ifdef HAVE_INOTIFY_INIT
 # include <sys/inotify.h>
@@ -1082,3 +1085,50 @@ gnupg_inotify_has_name (int fd, const char *name)
 
   return 0; /* Not found.  */
 }
+
+
+/* Return a malloc'ed string that is the path to the passed
+ * unix-domain socket (or return NULL if this is not a valid
+ * unix-domain socket).  We use a plain int here because it is only
+ * used on Linux.
+ *
+ * FIXME: This function needs to be moved to libassuan.  */
+#ifndef HAVE_W32_SYSTEM
+char *
+gnupg_get_socket_name (int fd)
+{
+  struct sockaddr_un un;
+  socklen_t len = sizeof(un);
+  char *name = NULL;
+
+  if (getsockname (fd, (struct sockaddr*)&un, &len) != 0)
+    log_error ("could not getsockname(%d): %s\n", fd,
+               gpg_strerror (gpg_error_from_syserror ()));
+  else if (un.sun_family != AF_UNIX)
+    log_error ("file descriptor %d is not a unix-domain socket\n", fd);
+  else if (len <= offsetof (struct sockaddr_un, sun_path))
+    log_error ("socket name not present for file descriptor %d\n", fd);
+  else if (len > sizeof(un))
+    log_error ("socket name for file descriptor %d was truncated "
+               "(passed %zu bytes, wanted %u)\n", fd, sizeof(un), len);
+  else
+    {
+      size_t namelen = len - offsetof (struct sockaddr_un, sun_path);
+
+      log_debug ("file descriptor %d has path %s (%zu octets)\n", fd,
+                 un.sun_path, namelen);
+      name = xtrymalloc (namelen + 1);
+      if (!name)
+        log_error ("failed to allocate memory for name of fd %d: %s\n",
+                   fd, gpg_strerror (gpg_error_from_syserror ()));
+      else
+        {
+          memcpy (name, un.sun_path, namelen);
+          name[namelen] = 0;
+        }
+    }
+
+  return name;
+}
+#endif /*!HAVE_W32_SYSTEM*/
+
diff --git a/common/sysutils.h b/common/sysutils.h
index ea92e4c..7105107 100644
--- a/common/sysutils.h
+++ b/common/sysutils.h
@@ -66,6 +66,7 @@ char *gnupg_mkdtemp (char *template);
 int  gnupg_setenv (const char *name, const char *value, int overwrite);
 int  gnupg_unsetenv (const char *name);
 char *gnupg_getcwd (void);
+char *gnupg_get_socket_name (int fd);
 
 gpg_error_t gnupg_inotify_watch_socket (int *r_fd, const char *socket_name);
 int gnupg_inotify_has_name (int fd, const char *name);
-- 
2.9.3




More information about the Gnupg-devel mailing list