From patchwork Wed Aug 5 07:14:41 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 1341121 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=libc-alpha-bounces@sourceware.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=sourceware.org Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=qZ57yUxQ; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4BM2v01t99z9sR4 for ; Wed, 5 Aug 2020 17:14:52 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 157CD3840C2A; Wed, 5 Aug 2020 07:14:49 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 157CD3840C2A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1596611689; bh=KGQZeJ9D5YTRcAWOh0uTWG+UYVsfDNmlXdhgr6UyCb8=; h=To:Subject:In-Reply-To:References:Date:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=qZ57yUxQfbvrMJJWSjWc20NpuNSYt/3QnlkvCTz2kUX5IfFlmD9FgI6+oqYZ9RA/k IJ4JWXhLNsIuaxDstiY80B4/cPhyrr5QotKQyQRO0diPNgThBlF4+2hlBOgOM2OeYH HOXpJiuCQCRgh/EsaJj3WaOa4kNFsq0/nCKFDHHg= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-1.mimecast.com (us-smtp-2.mimecast.com [205.139.110.61]) by sourceware.org (Postfix) with ESMTP id 5871C3840C2A for ; Wed, 5 Aug 2020 07:14:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 5871C3840C2A 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-470-eJgvSRHNNPqXBL1P-CITAA-1; Wed, 05 Aug 2020 03:14:44 -0400 X-MC-Unique: eJgvSRHNNPqXBL1P-CITAA-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 562BE100CC84 for ; Wed, 5 Aug 2020 07:14:43 +0000 (UTC) Received: from oldenburg2.str.redhat.com (ovpn-112-189.ams2.redhat.com [10.36.112.189]) by smtp.corp.redhat.com (Postfix) with ESMTPS id C893710013D7 for ; Wed, 5 Aug 2020 07:14:42 +0000 (UTC) To: libc-alpha@sourceware.org Subject: [PATCH 2/3] Linux: unlockpt needs to fail with EINVAL, not ENOTTY (bug 26053) In-Reply-To: <0c9120dce321f606d15090f8832039b122b056a0.1596611597.git.fweimer@redhat.com> References: <0c9120dce321f606d15090f8832039b122b056a0.1596611597.git.fweimer@redhat.com> Message-Id: Date: Wed, 05 Aug 2020 09:14:41 +0200 User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.3 (gnu/linux) 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 X-Spam-Status: No, score=-12.0 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_PASS, TXREP 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: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Florian Weimer via Libc-alpha From: Florian Weimer Reply-To: Florian Weimer Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" The EINVAL error code is mandated by POSIX and documented in the manual. Also clean up the unlockpt implementation a bit, assuming that TIOCSPTLCK is always defined. Enhance login/tst-grantpt to cover unlockpt corner cases. Reviewed-by: Adhemerval Zanella --- login/tst-grantpt.c | 20 ++++++++++++++++---- sysdeps/unix/sysv/linux/unlockpt.c | 21 +++++---------------- 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/login/tst-grantpt.c b/login/tst-grantpt.c index 1d7a220fcf..8ca901ef94 100644 --- a/login/tst-grantpt.c +++ b/login/tst-grantpt.c @@ -1,4 +1,4 @@ -/* Test for grantpt error corner cases. +/* Test for grantpt, unlockpt error corner cases. Copyright (C) 2001-2020 Free Software Foundation, Inc. This file is part of the GNU C Library. @@ -28,7 +28,7 @@ #include #include -/* Test grantpt with a closed descriptor. */ +/* Test grantpt, unlockpt with a closed descriptor. */ static void test_ebadf (void) { @@ -48,9 +48,12 @@ test_ebadf (void) printf ("grantpt(): expected: return = %d, errno = %d\n", -1, EBADF); printf (" got: return = %d, errno = %d\n", ret, err); } + + TEST_COMPARE (unlockpt (fd), -1); + TEST_COMPARE (errno, EBADF); } -/* Test grantpt on a regular file. */ +/* Test grantpt, unlockpt on a regular file. */ static void test_einval (void) { @@ -68,10 +71,13 @@ test_einval (void) printf (" got: return = %d, errno = %d\n", ret, err); } + TEST_COMPARE (unlockpt (fd), -1); + TEST_COMPARE (errno, EINVAL); + xclose (fd); } -/* Test grantpt on a non-ptmx pseudo-terminal. */ +/* Test grantpt, unlockpt on a non-ptmx pseudo-terminal. */ static void test_not_ptmx (void) { @@ -80,6 +86,9 @@ test_not_ptmx (void) TEST_COMPARE (grantpt (ptmx), 0); TEST_COMPARE (unlockpt (ptmx), 0); + /* A second unlock succeeds as well. */ + TEST_COMPARE (unlockpt (ptmx), 0); + const char *name = ptsname (ptmx); TEST_VERIFY_EXIT (name != NULL); int pts = open (name, O_RDWR | O_NOCTTY); @@ -88,6 +97,9 @@ test_not_ptmx (void) TEST_COMPARE (grantpt (pts), -1); TEST_COMPARE (errno, EINVAL); + TEST_COMPARE (unlockpt (pts), -1); + TEST_COMPARE (errno, EINVAL); + xclose (pts); xclose (ptmx); } diff --git a/sysdeps/unix/sysv/linux/unlockpt.c b/sysdeps/unix/sysv/linux/unlockpt.c index 3a0ac7a96c..4d98abece0 100644 --- a/sysdeps/unix/sysv/linux/unlockpt.c +++ b/sysdeps/unix/sysv/linux/unlockpt.c @@ -27,22 +27,11 @@ int unlockpt (int fd) { -#ifdef TIOCSPTLCK - int save_errno = errno; int unlock = 0; - if (__ioctl (fd, TIOCSPTLCK, &unlock)) - { - if (errno == EINVAL) - { - __set_errno (save_errno); - return 0; - } - else - return -1; - } -#endif - /* If we have no TIOCSPTLCK ioctl, all slave pseudo terminals are - unlocked by default. */ - return 0; + int ret = __ioctl (fd, TIOCSPTLCK, &unlock); + if (ret != 0 && errno == ENOTTY) + /* POSIX mandates EINVAL for non-ptmx descriptors. */ + __set_errno (EINVAL); + return ret; }