From: Gary Benson <gbenson@redhat.com>
To: gdb-patches@sourceware.org
Subject: [PATCH 7/7] Implement vFile:setfs in gdbserver
Date: Thu, 16 Apr 2015 12:54:00 -0000 [thread overview]
Message-ID: <1429186791-6867-8-git-send-email-gbenson@redhat.com> (raw)
In-Reply-To: <1429186791-6867-1-git-send-email-gbenson@redhat.com>
gdb/gdbserver/ChangeLog:
* target.h (struct target_ops) <call_with_fs_of>: New field.
* linux-low.c (nat/linux-namespaces.h): New include.
(linux_low_call_with_fs_of): New function.
* linux-low.c (linux_target_ops): Initialize call_with_fs_of.
* hostio.c (hostio_fs_pid): New static variable.
(handle_setfs): New function.
(enter_filesystem): Likewise.
(open_closure): New structure.
(handle_open_1): New function.
(handle_open): Update to use enter_filesystem.
(unlink_closure): New structure.
(handle_unlink_1): New function.
(handle_unlink): Update to use enter_filesystem.
(readlink_closure): New structure.
(handle_readlink_1): New function.
(handle_readlink): Update to use enter_filesystem.
(handle_vFile): Handle vFile:setfs packets.
---
gdb/gdbserver/ChangeLog | 20 ++++++
gdb/gdbserver/hostio.c | 152 ++++++++++++++++++++++++++++++++++++++++----
gdb/gdbserver/linux-low.c | 10 +++
gdb/gdbserver/target.h | 6 ++
4 files changed, 174 insertions(+), 14 deletions(-)
diff --git a/gdb/gdbserver/hostio.c b/gdb/gdbserver/hostio.c
index b03b5ad..9a1e443 100644
--- a/gdb/gdbserver/hostio.c
+++ b/gdb/gdbserver/hostio.c
@@ -243,6 +243,51 @@ hostio_reply_with_data (char *own_buf, char *buffer, int len,
return input_index;
}
+/* Process ID of inferior whose filesystem hostio functions
+ that take FILENAME arguments will use. Zero means to use
+ our own filesystem. */
+
+static int hostio_fs_pid = 0;
+
+/* Handle a "vFile:setfs:" packet. */
+
+static void
+handle_setfs (char *own_buf)
+{
+ char *p;
+ int pid;
+
+ p = own_buf + strlen ("vFile:setfs:");
+
+ if (require_int (&p, &pid)
+ || pid < 0
+ || require_end (p))
+ {
+ hostio_packet_error (own_buf);
+ return;
+ }
+
+ hostio_fs_pid = pid;
+
+ hostio_reply (own_buf, 0);
+}
+
+/* Cause the filesystem to appear as it does to the process specified
+ by the most recent valid "vFile:setfs:" packet and call FUNC with
+ argument ARG, restoring the filesystem to its original state
+ afterwards. Return nonzero if FUNC was called, zero otherwise (and
+ set ERRNO). */
+
+static int
+enter_filesystem (void (*func) (void *), void *arg)
+{
+ if (hostio_fs_pid != 0 && the_target->call_with_fs_of != NULL)
+ return the_target->call_with_fs_of (hostio_fs_pid, func, arg);
+
+ func (arg);
+ return 1;
+}
+
static int
fileio_open_flags_to_host (int fileio_open_flags, int *open_flags_p)
{
@@ -275,23 +320,45 @@ fileio_open_flags_to_host (int fileio_open_flags, int *open_flags_p)
return 0;
}
+/* Arguments and return value of open(2). */
+
+struct open_closure
+{
+ char filename[HOSTIO_PATH_MAX];
+ int flags;
+ int mode;
+ int return_value;
+};
+
+/* Helper for handle_open. */
+
+static void
+handle_open_1 (void *arg)
+{
+ struct open_closure *clo = (struct open_closure *) arg;
+
+ clo->return_value = open (clo->filename, clo->flags, clo->mode);
+}
+
+/* Handle a "vFile:open:" packet. */
+
static void
handle_open (char *own_buf)
{
- char filename[HOSTIO_PATH_MAX];
char *p;
- int fileio_flags, mode, flags, fd;
+ int fileio_flags, fd;
struct fd_list *new_fd;
+ struct open_closure clo;
p = own_buf + strlen ("vFile:open:");
- if (require_filename (&p, filename)
+ if (require_filename (&p, clo.filename)
|| require_comma (&p)
|| require_int (&p, &fileio_flags)
|| require_comma (&p)
- || require_int (&p, &mode)
+ || require_int (&p, &clo.mode)
|| require_end (p)
- || fileio_open_flags_to_host (fileio_flags, &flags))
+ || fileio_open_flags_to_host (fileio_flags, &clo.flags))
{
hostio_packet_error (own_buf);
return;
@@ -299,7 +366,11 @@ handle_open (char *own_buf)
/* We do not need to convert MODE, since the fileio protocol
uses the standard values. */
- fd = open (filename, flags, mode);
+
+ if (enter_filesystem (handle_open_1, &clo))
+ fd = clo.return_value;
+ else
+ fd = -1;
if (fd == -1)
{
@@ -487,23 +558,46 @@ handle_close (char *own_buf)
hostio_reply (own_buf, ret);
}
+/* Arguments and return value of unlink(2). */
+
+struct unlink_closure
+{
+ char filename[HOSTIO_PATH_MAX];
+ int return_value;
+};
+
+/* Helper for handle_unlink. */
+
+static void
+handle_unlink_1 (void *arg)
+{
+ struct unlink_closure *clo = (struct unlink_closure *) arg;
+
+ clo->return_value = unlink (clo->filename);
+}
+
+/* Handle a "vFile:unlink:" packet. */
+
static void
handle_unlink (char *own_buf)
{
- char filename[HOSTIO_PATH_MAX];
+ struct unlink_closure clo;
char *p;
int ret;
p = own_buf + strlen ("vFile:unlink:");
- if (require_filename (&p, filename)
+ if (require_filename (&p, clo.filename)
|| require_end (p))
{
hostio_packet_error (own_buf);
return;
}
- ret = unlink (filename);
+ if (enter_filesystem (handle_unlink_1, &clo))
+ ret = clo.return_value;
+ else
+ ret = -1;
if (ret == -1)
{
@@ -514,30 +608,57 @@ handle_unlink (char *own_buf)
hostio_reply (own_buf, ret);
}
+/* Arguments and return value of readlink(2). */
+
+struct readlink_closure
+{
+ char filename[HOSTIO_PATH_MAX];
+ char linkname[HOSTIO_PATH_MAX];
+ int return_value;
+};
+
+/* Helper for handle_readlink. */
+
+static void
+handle_readlink_1 (void *arg)
+{
+ struct readlink_closure *clo = (struct readlink_closure *) arg;
+
+ clo->return_value = readlink (clo->filename, clo->linkname,
+ sizeof (clo->linkname) - 1);
+}
+
+/* Handle a "vFile:readlink:" packet. */
+
static void
handle_readlink (char *own_buf, int *new_packet_len)
{
- char filename[HOSTIO_PATH_MAX], linkname[HOSTIO_PATH_MAX];
+ struct readlink_closure clo;
char *p;
int ret, bytes_sent;
p = own_buf + strlen ("vFile:readlink:");
- if (require_filename (&p, filename)
+ if (require_filename (&p, clo.filename)
|| require_end (p))
{
hostio_packet_error (own_buf);
return;
}
- ret = readlink (filename, linkname, sizeof (linkname) - 1);
+ if (enter_filesystem (handle_readlink_1, &clo))
+ ret = clo.return_value;
+ else
+ ret = -1;
+
if (ret == -1)
{
hostio_error (own_buf);
return;
}
- bytes_sent = hostio_reply_with_data (own_buf, linkname, ret, new_packet_len);
+ bytes_sent = hostio_reply_with_data (own_buf, clo.linkname,
+ ret, new_packet_len);
/* If the response does not fit into a single packet, do not attempt
to return a partial response, but simply fail. */
@@ -550,7 +671,10 @@ handle_readlink (char *own_buf, int *new_packet_len)
int
handle_vFile (char *own_buf, int packet_len, int *new_packet_len)
{
- if (startswith (own_buf, "vFile:open:"))
+ if (the_target->call_with_fs_of != NULL
+ && startswith (own_buf, "vFile:setfs:"))
+ handle_setfs (own_buf);
+ else if (startswith (own_buf, "vFile:open:"))
handle_open (own_buf);
else if (startswith (own_buf, "vFile:pread:"))
handle_pread (own_buf, new_packet_len);
diff --git a/gdb/gdbserver/linux-low.c b/gdb/gdbserver/linux-low.c
index 74f754d..4f01dbd 100644
--- a/gdb/gdbserver/linux-low.c
+++ b/gdb/gdbserver/linux-low.c
@@ -51,6 +51,7 @@
definition of elf_fpregset_t. */
#include <elf.h>
#endif
+#include "nat/linux-namespaces.h"
#ifndef SPUFS_MAGIC
#define SPUFS_MAGIC 0x23c9b64e
@@ -6349,6 +6350,14 @@ current_lwp_ptid (void)
return ptid_of (current_thread);
}
+/* Implementation of target_ops->call_with_fs_of. */
+
+static int
+linux_low_call_with_fs_of (int pid, void (*func) (void *), void *arg)
+{
+ return linux_ns_enter (pid, LINUX_NS_MNT, func, arg);
+}
+
static struct target_ops linux_target_ops = {
linux_create_inferior,
linux_attach,
@@ -6434,6 +6443,7 @@ static struct target_ops linux_target_ops = {
#endif
linux_supports_range_stepping,
linux_proc_pid_to_exec_file,
+ linux_low_call_with_fs_of,
};
static void
diff --git a/gdb/gdbserver/target.h b/gdb/gdbserver/target.h
index 6280c26..c766a2c 100644
--- a/gdb/gdbserver/target.h
+++ b/gdb/gdbserver/target.h
@@ -402,6 +402,12 @@ struct target_ops
string should be copied into a buffer by the client if the string
will not be immediately used, or if it must persist. */
char *(*pid_to_exec_file) (int pid);
+
+ /* Cause the filesystem to appear as it does to process PID and
+ call FUNC with argument ARG, restoring the filesystem to its
+ original state afterwards. Return nonzero if FUNC was called,
+ zero otherwise (and set ERRNO). */
+ int (*call_with_fs_of) (int pid, void (*func) (void *), void *arg);
};
extern struct target_ops *the_target;
--
1.7.1
next prev parent reply other threads:[~2015-04-16 12:54 UTC|newest]
Thread overview: 84+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-04-16 12:19 [PATCH 0/7] GNU/Linux mount namespace support Gary Benson
2015-04-16 12:19 ` [PATCH 4/7] Remove linux_proc_pid_get_ns Gary Benson
2015-04-17 4:36 ` Doug Evans
2015-04-17 13:44 ` Gary Benson
2015-04-16 12:20 ` [PATCH 2/7] Introduce target_fileio_set_fs Gary Benson
2015-04-17 3:04 ` Doug Evans
2015-04-17 13:36 ` Gary Benson
2015-04-17 14:21 ` Pedro Alves
2015-04-17 17:28 ` Doug Evans
2015-04-17 17:46 ` Pedro Alves
2015-04-20 11:11 ` Gary Benson
2015-04-16 12:20 ` [PATCH 1/7] Move make_cleanup_close to common code Gary Benson
2015-04-17 2:47 ` Doug Evans
2015-04-16 12:27 ` [PATCH 6/7] Implement multiple-filesystem support for remote targets Gary Benson
2015-04-16 15:12 ` Eli Zaretskii
2015-04-17 15:06 ` Pedro Alves
2015-04-17 16:00 ` Gary Benson
2015-04-17 16:07 ` Pedro Alves
2015-04-17 16:20 ` Gary Benson
2015-04-17 15:31 ` Pedro Alves
2015-04-17 16:01 ` Gary Benson
2015-04-16 12:34 ` [PATCH 3/7] Introduce nat/linux-namespaces.[ch] Gary Benson
2015-04-17 4:26 ` Doug Evans
2015-04-17 13:41 ` Gary Benson
2015-04-17 14:52 ` Pedro Alves
2015-04-17 17:32 ` Doug Evans
2015-04-20 11:12 ` Gary Benson
2015-04-16 12:54 ` Gary Benson [this message]
2015-04-17 15:30 ` [PATCH 7/7] Implement vFile:setfs in gdbserver Pedro Alves
2015-04-17 16:47 ` Gary Benson
2015-04-17 16:29 ` Gary Benson
2015-04-17 17:09 ` Pedro Alves
2015-04-16 13:06 ` [PATCH 5/7] Implement multiple-filesystem support for Linux targets Gary Benson
2015-04-17 15:35 ` [PATCH 0/7] GNU/Linux mount namespace support Pedro Alves
2015-04-20 16:49 ` Iago López Galeiras
2015-04-21 7:56 ` Gary Benson
2015-04-30 12:06 ` [PATCH 6/9 v2] Implement mount namespace support for native Linux targets Gary Benson
2015-04-30 16:24 ` Eli Zaretskii
2015-04-30 18:05 ` Gary Benson
2015-05-21 14:59 ` Pedro Alves
2015-05-27 10:16 ` Gary Benson
2015-04-30 12:06 ` [PATCH 5/9 v2] Add "inferior" argument to some target_fileio functions Gary Benson
2015-05-21 14:57 ` Pedro Alves
2015-04-30 12:06 ` [PATCH 0/9 v2] GNU/Linux mount namespace support Gary Benson
2015-06-10 14:23 ` [pushed][PATCH " Gary Benson
2015-04-30 12:06 ` [PATCH 3/9 v2] Remove linux_proc_pid_get_ns Gary Benson
2015-05-21 14:56 ` Pedro Alves
2015-04-30 12:15 ` [PATCH 4/9 v2] Comment and whitespace changes Gary Benson
2015-05-21 14:57 ` Pedro Alves
2015-04-30 12:41 ` [PATCH 8/9 v2] Implement vFile:setfs in gdbserver Gary Benson
2015-05-21 15:00 ` Pedro Alves
2015-06-09 14:11 ` Gary Benson
2015-06-09 14:23 ` Pedro Alves
2015-06-10 9:01 ` Gary Benson
2015-06-10 9:41 ` Gary Benson
2015-06-10 14:53 ` Pedro Alves
2015-04-30 12:45 ` [PATCH 2/9 v2] Introduce nat/linux-namespaces.[ch] Gary Benson
[not found] ` <20150501000739.740.47967@domU-12-31-39-0A-A0-4F>
2015-05-01 9:28 ` Gary Benson
2015-05-01 13:18 ` Alban Crequy
2015-05-01 20:29 ` Gary Benson
2015-05-06 18:55 ` Alban Crequy
2015-05-07 8:42 ` Gary Benson
2015-05-07 10:39 ` Gary Benson
2015-05-21 14:56 ` Pedro Alves
2015-05-27 10:14 ` Gary Benson
2015-06-11 8:40 ` James Greenhalgh
2015-06-11 11:04 ` Pedro Alves
2015-06-11 12:42 ` [OB PATCH] Use pulongest for printing ssize_t Gary Benson
2015-06-15 15:02 ` [PATCH 2/9 v2] Introduce nat/linux-namespaces.[ch] Michael Eager
2015-06-15 22:12 ` Michael Eager
2015-06-16 8:40 ` Gary Benson
2015-06-16 14:19 ` Michael Eager
2015-06-17 9:51 ` Gary Benson
2016-01-08 10:49 ` Yao Qi
2016-01-11 16:40 ` Gary Benson
2016-01-18 11:44 ` [OB PATCH] Fix gdbserver build failure on targets without fork Gary Benson
2015-04-30 14:12 ` [PATCH 1/9 v2] Move make_cleanup_close to common code Gary Benson
2015-05-21 14:56 ` Pedro Alves
2015-05-27 9:52 ` Gary Benson
2015-04-30 14:12 ` [PATCH 7/9 v2] Implement multiple-filesystem support for remote targets Gary Benson
2015-04-30 17:10 ` Eli Zaretskii
2015-05-21 15:04 ` Pedro Alves
2015-04-30 14:14 ` [PATCH 9/9 v2] Announce new container-awareness features for GNU/Linux systems Gary Benson
2015-04-30 16:20 ` Eli Zaretskii
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1429186791-6867-8-git-send-email-gbenson@redhat.com \
--to=gbenson@redhat.com \
--cc=gdb-patches@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox