diff --git a/gdb/Makefile.in b/gdb/Makefile.in
index f9606b8fc7..d5f1450035 100644
--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -444,6 +444,7 @@ SUBDIR_UNITTESTS_SRCS = \
unittests/rsp-low-selftests.c \
unittests/scoped_fd-selftests.c \
unittests/scoped_mmap-selftests.c \
+ unittests/scoped_pipe-selftests.c \
unittests/scoped_restore-selftests.c \
unittests/string_view-selftests.c \
unittests/style-selftests.c \
diff --git a/gdb/unittests/scoped_pipe-selftests.c b/gdb/unittests/scoped_pipe-selftests.c
new file mode 100644
index 0000000000..dd634bec97
--- /dev/null
+++ b/gdb/unittests/scoped_pipe-selftests.c
@@ -0,0 +1,96 @@
+/* Self tests for scoped_pipe for GDB, the GNU debugger.
+
+ Copyright (C) 2020 Free Software Foundation, Inc.
+
+ This file is part of GDB.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see . */
+
+#include "defs.h"
+
+#include "gdbsupport/scoped_pipe.h"
+#include "config.h"
+#include "gdbsupport/selftest.h"
+
+namespace selftests {
+namespace scoped_pipe {
+
+/* Test that the pipe is correctly created. */
+
+static void
+test_create ()
+{
+ ::scoped_pipe spipe;
+
+ SELF_CHECK (spipe.get_read_end () > 0);
+ SELF_CHECK (spipe.get_write_end () > 0);
+}
+
+/* Test that we can write and read from the pipe. */
+
+static void
+test_transmission ()
+{
+ int foo = 123;
+ ::scoped_pipe spipe;
+
+ /* Write to the pipe. */
+ {
+ ssize_t writeret;
+
+ do
+ {
+ writeret = write (spipe.get_write_end (), &foo, sizeof (foo));
+ }
+ while (writeret < 0 && (errno == EAGAIN || errno == EINTR));
+
+ SELF_CHECK (writeret > 0);
+ }
+
+ /* Read from the pipe, and check if the value read is the same as
+ the one that was written. */
+ {
+ ssize_t readret;
+ int read_foo;
+
+ do
+ {
+ readret = read (spipe.get_read_end (), &read_foo, sizeof (read_foo));
+ }
+ while (readret < 0 && (errno == EAGAIN || errno == EINTR));
+
+ SELF_CHECK (readret > 0);
+
+ SELF_CHECK (read_foo == foo);
+ }
+}
+
+/* Run selftests. */
+static void
+run_tests ()
+{
+ test_create ();
+ test_transmission ();
+}
+
+} /* namespace scoped_pipe */
+} /* namespace selftests */
+
+void _initialize_scoped_pipe_selftests ();
+void
+_initialize_scoped_pipe_selftests ()
+{
+ selftests::register_test ("scoped_pipe",
+ selftests::scoped_pipe::run_tests);
+}
diff --git a/gdbsupport/scoped_pipe.h b/gdbsupport/scoped_pipe.h
new file mode 100644
index 0000000000..8f133b442f
--- /dev/null
+++ b/gdbsupport/scoped_pipe.h
@@ -0,0 +1,63 @@
+/* scoped_pipe, automatically close a pipe.
+
+ Copyright (C) 2020 Free Software Foundation, Inc.
+
+ This file is part of GDB.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see . */
+
+#ifndef COMMON_SCOPED_PIPE_H
+#define COMMON_SCOPED_PIPE_H
+
+#include
+#include "filestuff.h"
+
+/* A smart-pointer-like class to automatically close a pipe. */
+
+class scoped_pipe
+{
+public:
+ explicit scoped_pipe ()
+ {
+ if (gdb_pipe_cloexec (m_pipe) < 0)
+ error (_("gdb_pipe_cloexec: %s"), safe_strerror (errno));
+ }
+
+ ~scoped_pipe ()
+ {
+ if (m_pipe[0] >= 0)
+ close (m_pipe[0]);
+ if (m_pipe[1] >= 0)
+ close (m_pipe[1]);
+ }
+
+ DISABLE_COPY_AND_ASSIGN (scoped_pipe);
+
+ /* Get the read end of the pipe. */
+ int get_read_end () const noexcept
+ {
+ return m_pipe[0];
+ }
+
+ /* Get the write end of the pipe. */
+ int get_write_end () const noexcept
+ {
+ return m_pipe[1];
+ }
+
+private:
+ int m_pipe[2];
+};
+
+#endif /* ! COMMON_SCOPED_PIPE_H */