aboutsummaryrefslogtreecommitdiffstats
path: root/common/sysutils.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-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*/