gdbserver: support Unix domain sockets

Message ID CAMsPy2shyuZECJ7Msx6ADk=SoyJHa6T5Ea0V2iKc7LJB9ntNVQ@mail.gmail.com
State New, archived
Headers

Commit Message

Yunlian Jiang March 10, 2016, 12:04 a.m. UTC
  This patch enables gdbserver to use Unix domain sockets if
the TCP sockets is not allowed to use. Wit this patch, user can
use "gdbserver +debug-socket --attach 123" to use Unix domain
sockets.
  

Comments

Pedro Alves March 10, 2016, 5:35 p.m. UTC | #1
On 03/10/2016 12:04 AM, Yunlian Jiang wrote:
> This patch enables gdbserver to use Unix domain sockets if
> the TCP sockets is not allowed to use. Wit this patch, user can
> use "gdbserver +debug-socket --attach 123" to use Unix domain
> sockets.

Thanks.

This is larger than what we can accept without a copyright
assignment with the FSF -- I looked at the records and
couldn't find one.  Let me know if you'd like to get that started.

We'll also need, at least:

- A gdb manual update.
- A gdbserver --help update
- NEWS entry
- ChangeLog entry

See [1] for more details.

A couple offhand questions:

- How do you plan on using this from the gdb side?

- Is there a reason for picking "+" over some other way / syntax?

[1] http://sourceware.org/gdb/wiki/ContributionChecklist

Thanks,
Pedro Alves
  
Pedro Alves March 14, 2016, 3:02 p.m. UTC | #2
On 03/10/2016 05:35 PM, Pedro Alves wrote:

> This is larger than what we can accept without a copyright
> assignment with the FSF -- I looked at the records and
> couldn't find one.  Let me know if you'd like to get that started.

Sorry, I notice now that I got confused here -- you're already covered
by Google's blanket copyright assignment.

Thanks,
Pedro Alves
  

Patch

diff --git a/gdb/gdbserver/remote-utils.c b/gdb/gdbserver/remote-utils.c
index e751473..b00960d 100644
--- a/gdb/gdbserver/remote-utils.c
+++ b/gdb/gdbserver/remote-utils.c
@@ -62,6 +62,10 @@ 
 #include <winsock2.h>
 #endif

+#ifndef USE_WIN32API
+#include <sys/un.h>
+#endif
+
 #if __QNX__
 #include <sys/iomgr.h>
 #endif /* __QNX__ */
@@ -288,6 +292,49 @@  remote_open (char *name)
 {
   char *port_str;

+  /* Enable gdbserver to use Unix domain sockets if applications aren't
+     allowed to bind to localhost TCP sockets.
+     Typical usage is "gdbserver +debug-socket --attach 123" */
+  if (name[0] == '+')
+    {
+#ifdef USE_WIN32API
+      error ("Only <host>:<port> is supported on this platform.");
+#else
+      struct sockaddr_un sockaddr;
+      socklen_t sockaddrlen;
+
+      listen_desc = socket (AF_UNIX, SOCK_STREAM, 0);
+      if (listen_desc == -1)
+        perror_with_name ("Can't create Unix domain socket");
+
+      /* Skip the initial '+'. */
+      name++;
+
+      memset (&sockaddr, 0, sizeof sockaddr);
+      sockaddr.sun_family = AF_UNIX;
+      snprintf(sockaddr.sun_path, sizeof (sockaddr.sun_path), "%s", name);
+      sockaddrlen = sizeof (sockaddr.sun_family) +
+          strlen (sockaddr.sun_path) + 1;
+
+      unlink (sockaddr.sun_path);
+
+      if (bind (listen_desc, (struct sockaddr *) &sockaddr, sockaddrlen)
+          || listen (listen_desc, 1))
+        perror_with_name ("Can't bind Unix domain socket");
+
+      fprintf (stderr, "Listening on Unix domain socket '%s'\n",
+               sockaddr.sun_path);
+      fflush (stderr);
+
+      /* Register the event loop handler.  */
+      add_file_handler (listen_desc, handle_accept_event, NULL);
+
+      transport_is_reliable = 1;
+#endif
+
+      return;
+    }
+
   port_str = strchr (name, ':');
 #ifdef USE_WIN32API
   if (port_str == NULL)