From patchwork Wed Feb 26 20:05:37 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergio Durigan Junior X-Patchwork-Id: 38330 Received: (qmail 101126 invoked by alias); 26 Feb 2020 20:06:28 -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 101047 invoked by uid 89); 26 Feb 2020 20:06:28 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-15.6 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_SHORT, RCVD_IN_DNSWL_NONE, UNSUBSCRIBE_BODY autolearn=ham version=3.3.1 spammy=sk:_initia X-HELO: us-smtp-1.mimecast.com Received: from us-smtp-delivery-1.mimecast.com (HELO us-smtp-1.mimecast.com) (205.139.110.120) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 26 Feb 2020 20:06:26 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1582747585; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=g9yPWWLRJL+5AABq8d2XVnDu7qjs0stLBGruVXO3tzg=; b=PzzbAwFvwYSAusFdlSJbqovb0WYFQ7I9aljap5h4okkcqAv/LF3rN+3Wt3k7DGPiOedy5r KqGsgm/xtha61WsQwhVkANtaFYI4WeNx/i1xBWJIk+zjxfH6QzBNXubTYCJg/KP4c/EnwV tT52xwSwvVzv+VHpGGu6+rzyqCwQdl8= 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-380-t9LXAvFGOpG4bZbRH_oMfw-1; Wed, 26 Feb 2020 15:06:19 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 8CE3DDB6A; Wed, 26 Feb 2020 20:06:18 +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 E43D35DA2C; Wed, 26 Feb 2020 20:06:17 +0000 (UTC) From: Sergio Durigan Junior To: GDB Patches Cc: Pedro Alves , Tom Tromey , Eli Zaretskii , Ruslan Kabatsayev , Sergio Durigan Junior Subject: [PATCH 1/6] Introduce scoped_pipe.h Date: Wed, 26 Feb 2020 15:05:37 -0500 Message-Id: <20200226200542.746617-2-sergiodj@redhat.com> In-Reply-To: <20200226200542.746617-1-sergiodj@redhat.com> References: <20190926042155.31481-1-sergiodj@redhat.com> <20200226200542.746617-1-sergiodj@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-IsSubscribed: yes 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 * unittests/scoped_pipe-selftests.c: New file. gdbsupport/ChangeLog: yyyy-mm-dd Sergio Durigan Junior * scoped_pipe.h: New file. --- gdb/Makefile.in | 1 + gdb/unittests/scoped_pipe-selftests.c | 96 +++++++++++++++++++++++++++ gdbsupport/scoped_pipe.h | 63 ++++++++++++++++++ 3 files changed, 160 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 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 */