Skip to contentSkip to conversation

gdbstub: re-do unlinking of unix socket before bind

PATCH In review Michael Tokarev 3 participants 7 messages Dec 25, 2025 – Jan 28 Arm/Amlogic Meson SoC support

lore
Share & tools
lore
#0 lore raw

cover gdbstub: re-do unlinking of unix socket before bind

Prior discussion is at
https://lore.kernel.org/qemu-devel/[email protected]/

In short: when adding unlink() before bind() for unix socket in gdbstub/user.c, a previous patch introduced dependency of linux-user binaries on qemu-sockets.c, which is more problematic for usually-static binaries. The monitor_get_fd stub which were also needed is another example why this might not be the right approach.

This patchset reverts the previous commit (with fixes for current code), and adds just a single unlink() to the original code which handled unix sockets directly.

Michael Tokarev (2):
Revert "gdbstub: Try unlinking the unix socket before binding"
gdbstub: unlink the unix socket before bind()

 gdbstub/user.c    | 30 +++++++++++++++++++++++++++---
 stubs/meson.build |  2 --
 util/meson.build  |  2 --
 3 files changed, 27 insertions(+), 7 deletions(-)
Signature · 1 line
2.47.3
#1 lore raw

PATCH 1/2 Revert "gdbstub: Try unlinking the unix socket before binding"

This reverts commit fccb744f41c69fec6fd92225fe907c6e69de5d44.

This commit introduced dependency of linux-user on qemu-sockets.c. The latter includes handling of various socket types, while gdbstub only needs unix sockets. Including different kinds of sockets makes it more problematic to build linux-user statically.

The original issue - the need to unlink unix socket before binding - will be addressed in the next change.

---
 gdbstub/user.c    | 29 ++++++++++++++++++++++++++---
 stubs/meson.build |  2 --
 util/meson.build  |  2 --
 3 files changed, 26 insertions(+), 7 deletions(-)
gdbstub/user.c +26 -3
index e233c59816..5e920a9b51 100644
@@ -314,10 +314,12 @@ static bool gdb_accept_socket(int gdb_fd)
     return true;
 }
 
-static int gdbserver_open_socket(const char *path, Error **errp)
+static int gdbserver_open_socket(const char *path)
 {
     g_autoptr(GString) buf = g_string_new("");
+    struct sockaddr_un sockaddr = {};
     const char *pid_placeholder;
+    int fd, ret;
 
     pid_placeholder = strstr(path, "%d");
     if (pid_placeholder != NULL) {
@@ -327,7 +329,28 @@ static int gdbserver_open_socket(const char *path, Error **errp)
         path = buf->str;
     }
 
-    return unix_listen(path, errp);
+    fd = socket(AF_UNIX, SOCK_STREAM, 0);
+    if (fd < 0) {
+        perror("create socket");
+        return -1;
+    }
+
+    sockaddr.sun_family = AF_UNIX;
+    pstrcpy(sockaddr.sun_path, sizeof(sockaddr.sun_path) - 1, path);
+    ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
+    if (ret < 0) {
+        perror("bind socket");
+        close(fd);
+        return -1;
+    }
+    ret = listen(fd, 1);
+    if (ret < 0) {
+        perror("listen socket");
+        close(fd);
+        return -1;
+    }
+
+    return fd;
 }
 
 static bool gdb_accept_tcp(int gdb_fd)
@@ -483,7 +506,7 @@ bool gdbserver_start(const char *args, Error **errp)
     if (port > 0) {
         gdb_fd = gdbserver_open_port(port, errp);
     } else {
-        gdb_fd = gdbserver_open_socket(port_or_path, errp);
+        gdb_fd = gdbserver_open_socket(port_or_path);
     }
     if (gdb_fd < 0) {
         return false;
stubs/meson.build +0 -2
index 0b2778c568..b6b1a3844e 100644
@@ -59,8 +59,6 @@ if have_user
   if not have_system
     stub_ss.add(files('qdev.c'))
   endif
-
-  stub_ss.add(files('monitor-internal.c'))
 endif
 
 if have_system
util/meson.build +0 -2
index 35029380a3..a925c09169 100644
@@ -87,8 +87,6 @@ if have_block or have_ga
   util_ss.add(files('qemu-coroutine.c', 'qemu-coroutine-lock.c', 'qemu-coroutine-io.c'))
   util_ss.add(files(f'coroutine-@[email protected]'))
   util_ss.add(files('thread-pool.c', 'qemu-timer.c'))
-endif
-if have_block or have_ga or have_user
   util_ss.add(files('qemu-sockets.c'))
 endif
 if have_block
Signature · 1 line
2.47.3
#2 lore raw

PATCH 2/2 gdbstub: unlink the unix socket before bind()

In reply to Michael Tokarev

This is another variant of doing what v9.2.0-1561-gfccb744f41c6 "gdbstub: Try unlinking the unix socket before binding" did, but that commit introduced dependency on qemu-sockets.c which is more problematic for statically-linked qemu-user binaries.

---
 gdbstub/user.c | 1 +
 1 file changed, 1 insertion(+)
gdbstub/user.c +1 -0
index 5e920a9b51..a2327c6135 100644
@@ -337,6 +337,7 @@ static int gdbserver_open_socket(const char *path)
 
     sockaddr.sun_family = AF_UNIX;
     pstrcpy(sockaddr.sun_path, sizeof(sockaddr.sun_path) - 1, path);
+    unlink(sockaddr.sun_path);
     ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
     if (ret < 0) {
         perror("bind socket");
Signature · 1 line
2.47.3
Reviewed-by on the v1 series
#3 lore raw
In reply to Michael Tokarev
Quoted 14 lines · Michael Tokarev

Prior discussion is at
https://lore.kernel.org/qemu-devel/[email protected]/

In short: when adding unlink() before bind() for unix socket in gdbstub/user.c, a previous patch introduced dependency of linux-user binaries on qemu-sockets.c, which is more problematic for usually-static binaries. The monitor_get_fd stub which were also needed is another example why this might not be the right approach.

This patchset reverts the previous commit (with fixes for current code), and adds just a single unlink() to the original code which handled unix sockets directly.

Quoted 8 lines

Michael Tokarev (2):
Revert "gdbstub: Try unlinking the unix socket before binding"
gdbstub: unlink the unix socket before bind()

  gdbstub/user.c    | 30 +++++++++++++++++++++++++++---
  stubs/meson.build |  2 --
  util/meson.build  |  2 --
  3 files changed, 27 insertions(+), 7 deletions(-)

Thanks!

#4 lore raw
In reply to Michael Tokarev
Quoted 10 lines · Michael Tokarev

Prior discussion is at
https://lore.kernel.org/qemu-devel/[email protected]/

In short: when adding unlink() before bind() for unix socket in gdbstub/user.c, a previous patch introduced dependency of linux-user binaries on qemu-sockets.c, which is more problematic for usually-static binaries. The monitor_get_fd stub which were also needed is another example why this might not be the right approach.

Quoted 3 lines

This patchset reverts the previous commit (with fixes for current code), and adds just a single unlink() to the original code which handled unix sockets directly.

This isn't applying cleanly for me. Could you re-base please?

Quoted 9 lines

Michael Tokarev (2):
Revert "gdbstub: Try unlinking the unix socket before binding"
gdbstub: unlink the unix socket before bind()

 gdbstub/user.c    | 30 +++++++++++++++++++++++++++---
 stubs/meson.build |  2 --
 util/meson.build  |  2 --
 3 files changed, 27 insertions(+), 7 deletions(-)
Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
#5 lore raw
Quoted 12 lines · Alex Bennée
Quoted 10 lines · Michael Tokarev

Prior discussion is at
https://lore.kernel.org/qemu-devel/[email protected]/

In short: when adding unlink() before bind() for unix socket in gdbstub/user.c, a previous patch introduced dependency of linux-user binaries on qemu-sockets.c, which is more problematic for usually-static binaries. The monitor_get_fd stub which were also needed is another example why this might not be the right approach.

Quoted 5 lines
Quoted 3 lines

This patchset reverts the previous commit (with fixes for current code), and adds just a single unlink() to the original code which handled unix sockets directly.

This isn't applying cleanly for me. Could you re-base please?

It isn't applying cleanly because this patchset has been applied to the master branch already -- a2b429b114e1812e0b3f222818a2755b6d87fb8a and e916f8b3b68bd9da45245927e2a428c3a0eaea54 .

HTH,

/mjt

#6 lore raw
Quoted 13 lines · Michael Tokarev
Quoted 12 lines · Alex Bennée
Quoted 10 lines · Michael Tokarev

Prior discussion is at
https://lore.kernel.org/qemu-devel/[email protected]/

In short: when adding unlink() before bind() for unix socket in gdbstub/user.c, a previous patch introduced dependency of linux-user binaries on qemu-sockets.c, which is more problematic for usually-static binaries. The monitor_get_fd stub which were also needed is another example why this might not be the right approach.

Quoted 8 lines
Quoted 4 lines
Quoted 3 lines

This patchset reverts the previous commit (with fixes for current code), and adds just a single unlink() to the original code which handled unix sockets directly.

This isn't applying cleanly for me. Could you re-base please?

It isn't applying cleanly because this patchset has been applied to the master branch already -- a2b429b114e1812e0b3f222818a2755b6d87fb8a and e916f8b3b68bd9da45245927e2a428c3a0eaea54 .

Ahh - I was confused as I had it on my list to get to. Sorry for the noise and thanks.

Quoted 4 lines

HTH,

/mjt

Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro