summaryrefslogtreecommitdiffstats
path: root/common/sysutils.c
diff options
context:
space:
mode:
authorDaniel Kahn Gillmor <dkg@fifthhorseman.net>2016-10-26 22:37:07 +0200
committerNIIBE Yutaka <gniibe@fsij.org>2016-10-27 03:37:17 +0200
commit6316b28e896957adb76a61a41d2e1c2a08d9f716 (patch)
tree399e2bb037feb8c21f75763e0abf6c0f510011ae /common/sysutils.c
parentdirmngr: report actual socket name. (diff)
downloadgnupg2-6316b28e896957adb76a61a41d2e1c2a08d9f716.tar.xz
gnupg2-6316b28e896957adb76a61a41d2e1c2a08d9f716.zip
agent,common: move get_socket_name() into common.
* 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@fifthhorseman.net>
Diffstat (limited to 'common/sysutils.c')
-rw-r--r--common/sysutils.c49
1 files changed, 49 insertions, 0 deletions
diff --git a/common/sysutils.c b/common/sysutils.c
index 60483ac36..71200a604 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>
@@ -1090,3 +1093,49 @@ 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*/