From patchwork Sat May 2 22:51:51 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gabriel Corona X-Patchwork-Id: 6534 Received: (qmail 98216 invoked by alias); 2 May 2015 22:53:01 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 98153 invoked by uid 89); 2 May 2015 22:53:00 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.8 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_LOW, SPF_NEUTRAL autolearn=ham version=3.3.2 X-HELO: smtp1-g21.free.fr Received: from smtp1-g21.free.fr (HELO smtp1-g21.free.fr) (212.27.42.1) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-GCM-SHA384 encrypted) ESMTPS; Sat, 02 May 2015 22:52:59 +0000 Received: from localhost.localdomain (unknown [81.56.99.41]) by smtp1-g21.free.fr (Postfix) with ESMTP id 6DF5D94000A; Sun, 3 May 2015 00:50:13 +0200 (CEST) From: Gabriel Corona To: gdb-patches@sourceware.org Cc: Gabriel Corona Subject: [PATCH] Make gdbserver connect to SOCK_STREAM sockets Date: Sun, 3 May 2015 00:51:51 +0200 Message-Id: <1430607111-17193-1-git-send-email-gabriel.corona@enst-bretagne.fr> In GDB: (gdb) target remote |socat STDIO UNIX-LISTEN:foo.sock In gdbserver: $ gdbserver foo.sock ./foo Conflicts: gdb/gdbserver/remote-utils.c --- gdb/gdbserver/remote-utils.c | 86 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 84 insertions(+), 2 deletions(-) diff --git a/gdb/gdbserver/remote-utils.c b/gdb/gdbserver/remote-utils.c index 1de86be..b1b6c7f 100644 --- a/gdb/gdbserver/remote-utils.c +++ b/gdb/gdbserver/remote-utils.c @@ -36,6 +36,9 @@ #if HAVE_SYS_SOCKET_H #include #endif +#if HAVE_SYS_UN_H +#include +#endif #if HAVE_NETDB_H #include #endif @@ -280,6 +283,77 @@ remote_prepare (char *name) transport_is_reliable = 1; } +#ifndef USE_WIN32API +static int open_shell_command(char* command) +{ + int sockets[2]; + int res; + pid_t child, pid; + + res = socketpair(AF_LOCAL, SOCK_STREAM, 0, sockets); + if (res < 0) + error ("Could not get socketpair."); + child = fork(); + if (child < 0) + { + error ("Could not fork."); + } + else if (child == 0) + { + if (close (sockets[0]) < 0) + exit (1); + if (dup2 (sockets[1], 0) < 0 || dup2 (sockets[1], 1) < 0) + exit (1); + res = fork (); + if (res < 0) + exit (1); + if (res != 0) + exit (0); + execl ("/bin/sh", "sh", "-c", command, NULL); + exit (1); + } + else + { + signal (SIGPIPE, SIG_IGN); + while ((pid = waitpid (child, NULL, 0)) < 0 && errno == EINTR); + if (pid < 0) + error ("Could not wait for child."); + close (sockets[1]); + return sockets[0]; + } + return -1; +} +#endif + +#ifndef USE_WIN32API +int socket_open (char* name) +{ + int sock; + struct sockaddr_un addr; + + if (strlen (name) >= sizeof (addr.sun_path)) + { + return -1; + } + + sock = socket (AF_UNIX, SOCK_STREAM, 0); + if (sock < 0) + { + return -1; + } + + addr.sun_family = AF_UNIX; + strcpy (addr.sun_path, name); + if (connect (sock, (const struct sockaddr *) &addr, + sizeof (struct sockaddr_un)) < 0) + { + close (sock); + return -1; + } + return sock; +} +#endif + /* Open a connection to a remote debugger. NAME is the filename used for communication. */ @@ -311,10 +385,18 @@ remote_open (char *name) else if (port_str == NULL) { struct stat statbuf; + int res; - if (stat (name, &statbuf) == 0 + res = stat (name, &statbuf); + if (res == 0 && (S_ISCHR (statbuf.st_mode) || S_ISFIFO (statbuf.st_mode))) - remote_desc = open (name, O_RDWR); + { + remote_desc = open (name, O_RDWR); + } + else if (res == 0 && S_ISSOCK (statbuf.st_mode)) + { + remote_desc = socket_open (name); + } else { errno = EINVAL;