From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-74.mimecast.com (us-smtp-delivery-74.mimecast.com [216.205.24.74]) by sourceware.org (Postfix) with ESMTP id 15747393741B for ; Tue, 17 Mar 2020 15:47:28 +0000 (GMT) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-107-ld8bP5PPOfinorwxAErkKw-1; Tue, 17 Mar 2020 11:47:26 -0400 X-MC-Unique: ld8bP5PPOfinorwxAErkKw-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 24B0518C8C01; Tue, 17 Mar 2020 15:47:25 +0000 (UTC) Received: from psique.yyz.redhat.com (unused-10-15-17-54.yyz.redhat.com [10.15.17.54]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7D95E10027A7; Tue, 17 Mar 2020 15:47:24 +0000 (UTC) From: Sergio Durigan Junior To: GDB Patches Cc: Pedro Alves , Tom Tromey , Kevin Buettner , Sergio Durigan Junior Subject: [PATCH v2 1/5] Introduce scoped_pipe.h Date: Tue, 17 Mar 2020 11:47:15 -0400 Message-Id: <20200317154719.2078283-2-sergiodj@redhat.com> In-Reply-To: <20200317154719.2078283-1-sergiodj@redhat.com> References: <20200226200542.746617-1-sergiodj@redhat.com> <20200317154719.2078283-1-sergiodj@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-23.2 required=5.0 tests=DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, UNSUBSCRIBE_BODY autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 17 Mar 2020 15:47:29 -0000 This simple patch introduces gdbsupport/scoped_pipe.h, which is based on gdbsupport/scoped_fd.h. When the object is instantiated, a pipe is created using 'gdb_pipe_cloexec'. There are two methods (get_read_end and get_write_end) that allow the user to obtain the read/write ends of the pipe (no more messing with [0] and [1]), and when the object is destroyed, the pipe is closed (both ends). gdb/ChangeLog: yyyy-mm-dd Sergio Durigan Junior =09* unittests/scoped_pipe-selftests.c: New file. gdbsupport/ChangeLog: yyyy-mm-dd Sergio Durigan Junior =09* scoped_pipe.h: New file. --- gdb/Makefile.in | 1 + gdb/unittests/scoped_pipe-selftests.c | 95 +++++++++++++++++++++++++++ gdbsupport/scoped_pipe.h | 63 ++++++++++++++++++ 3 files changed, 159 insertions(+) create mode 100644 gdb/unittests/scoped_pipe-selftests.c create mode 100644 gdbsupport/scoped_pipe.h diff --git a/gdb/Makefile.in b/gdb/Makefile.in index 0c331af4bf..458863b01c 100644 --- a/gdb/Makefile.in +++ b/gdb/Makefile.in @@ -447,6 +447,7 @@ SELFTESTS_SRCS =3D \ =09unittests/rsp-low-selftests.c \ =09unittests/scoped_fd-selftests.c \ =09unittests/scoped_mmap-selftests.c \ +=09unittests/scoped_pipe-selftests.c \ =09unittests/scoped_restore-selftests.c \ =09unittests/string_view-selftests.c \ =09unittests/style-selftests.c \ diff --git a/gdb/unittests/scoped_pipe-selftests.c b/gdb/unittests/scoped_p= ipe-selftests.c new file mode 100644 index 0000000000..14abbb1b7b --- /dev/null +++ b/gdb/unittests/scoped_pipe-selftests.c @@ -0,0 +1,95 @@ +/* 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 "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 =3D 123; + ::scoped_pipe spipe; + + /* Write to the pipe. */ + { + ssize_t writeret; + + do + { +=09writeret =3D write (spipe.get_write_end (), &foo, sizeof (foo)); + } + while (writeret < 0 && (errno =3D=3D EAGAIN || errno =3D=3D 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 + { +=09readret =3D read (spipe.get_read_end (), &read_foo, sizeof (read_foo)); + } + while (readret < 0 && (errno =3D=3D EAGAIN || errno =3D=3D EINTR)); + + SELF_CHECK (readret > 0); + + SELF_CHECK (read_foo =3D=3D 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", +=09=09=09 selftests::scoped_pipe::run_tests); +} diff --git a/gdbsupport/scoped_pipe.h b/gdbsupport/scoped_pipe.h new file mode 100644 index 0000000000..5180df80ae --- /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 GDBSUPPORT_SCOPED_PIPE_H +#define GDBSUPPORT_SCOPED_PIPE_H + +#include +#include "filestuff.h" + +/* A smart-pointer-like class to automatically close a pipe. */ + +class scoped_pipe +{ +public: + scoped_pipe () + { + if (gdb_pipe_cloexec (m_pipe) < 0) + error (_("gdb_pipe_cloexec: %s"), safe_strerror (errno)); + } + + ~scoped_pipe () + { + if (m_pipe[0] >=3D 0) + close (m_pipe[0]); + if (m_pipe[1] >=3D 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 /* ! GDBSUPPORT_SCOPED_PIPE_H */ --=20 2.24.1