From patchwork Fri Oct 2 17:06:18 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 40651 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 84E51398E466; Fri, 2 Oct 2020 17:06:44 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 84E51398E466 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1601658404; bh=hcYfMn8gDBQBig5xNKCq/Vw8vFAmnPW3oVn3Vmb9JTQ=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=d2DaZY6YSyeTX5TivYT2zbbInSBiJcc0fAoL0LMhMTtP6M9hgwCJfihCqwcoS+Drv dEB+lUlYWCsWoIy4irot/JkD8gW8rY2zWhwMYswN+2KRv+WDkuehxZrG2dYvQQn+yB 7n5JW0UFdV+PAsi5su7LxLkvGU9O8UURxirBB5W4= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qk1-x744.google.com (mail-qk1-x744.google.com [IPv6:2607:f8b0:4864:20::744]) by sourceware.org (Postfix) with ESMTPS id ABE90398E460 for ; Fri, 2 Oct 2020 17:06:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org ABE90398E460 Received: by mail-qk1-x744.google.com with SMTP id w12so2089805qki.6 for ; Fri, 02 Oct 2020 10:06:41 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=hcYfMn8gDBQBig5xNKCq/Vw8vFAmnPW3oVn3Vmb9JTQ=; b=Tzzehn+ktcPLvKXTuQ59pS+9+LGYYhEG7mw9d3Ad4xFMrES0yAGgHMIN7aB5PRrGkx uDA6/apqCwz+WT0ti/Mr5pXb4eJbO3BeE6AWlKc2nWhYzHbE1FzTIu6yZSxa40bbUE6L 0YsNOZx1esjmIdUXrwV8pbUEdpBZ0GAxkXIUPgWDCAxICvhdZyRLi6fQ64WiEzZKFu6S Jb2KP8uJgPQBn3/vvE7d3aM4em2FHx8fSj4MNrXITgQjRr8nO/7kVA/gNEt5e+PQj9KE qgg8UvFojFQ+hVd1WjHgxs2BVywmP69Lh+fFbFbmQ/itpMNqDNqh6DXpMDjzZFmVBYeQ Xiqw== X-Gm-Message-State: AOAM5333iQ0N0HfmvxmHmImLbzP0FeE1thRYYtnW6tZgZoliN7VuPSCy Mapv9L4+bWKZ2XyTHivvS0C7SU0YQ9hAOg== X-Google-Smtp-Source: ABdhPJyuk6lbqG7DGui8252/QLCYHsrBLRbk4GemE0GeT2JLRodtykuf4M4hF7An8Ye+qmPxU52eXQ== X-Received: by 2002:a37:6393:: with SMTP id x141mr2995945qkb.238.1601658400974; Fri, 02 Oct 2020 10:06:40 -0700 (PDT) Received: from localhost.localdomain ([177.194.48.209]) by smtp.googlemail.com with ESMTPSA id d10sm1436891qkk.1.2020.10.02.10.06.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Oct 2020 10:06:40 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH v2 7/9] linux: Add __old_readdir64_unlocked Date: Fri, 2 Oct 2020 14:06:18 -0300 Message-Id: <20201002170620.1611673-8-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201002170620.1611673-1-adhemerval.zanella@linaro.org> References: <20201002170620.1611673-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-13.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, 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: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Netto Reply-To: Adhemerval Zanella Cc: James Clarke , John Paul Adrian Glaubitz Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" And use it __old_readdir64_r. Checked on i686-linux-gnu. --- sysdeps/unix/sysv/linux/olddirent.h | 2 + sysdeps/unix/sysv/linux/readdir64.c | 21 +++++-- sysdeps/unix/sysv/linux/readdir64_r.c | 79 ++++++--------------------- 3 files changed, 35 insertions(+), 67 deletions(-) diff --git a/sysdeps/unix/sysv/linux/olddirent.h b/sysdeps/unix/sysv/linux/olddirent.h index 2d2559c1e9..b118b0ef96 100644 --- a/sysdeps/unix/sysv/linux/olddirent.h +++ b/sysdeps/unix/sysv/linux/olddirent.h @@ -32,6 +32,8 @@ struct __old_dirent64 /* Now define the internal interfaces. */ extern struct __old_dirent64 *__old_readdir64 (DIR *__dirp); libc_hidden_proto (__old_readdir64); +extern struct __old_dirent64 *__old_readdir64_unlocked (DIR *__dirp) + attribute_hidden; extern int __old_readdir64_r (DIR *__dirp, struct __old_dirent64 *__entry, struct __old_dirent64 **__result); extern __ssize_t __old_getdents64 (int __fd, char *__buf, size_t __nbytes) diff --git a/sysdeps/unix/sysv/linux/readdir64.c b/sysdeps/unix/sysv/linux/readdir64.c index a774f7c6e3..ce50067637 100644 --- a/sysdeps/unix/sysv/linux/readdir64.c +++ b/sysdeps/unix/sysv/linux/readdir64.c @@ -116,15 +116,11 @@ versioned_symbol (libc, __readdir64, readdir64, GLIBC_2_2); attribute_compat_text_section struct __old_dirent64 * -__old_readdir64 (DIR *dirp) +__old_readdir64_unlocked (DIR *dirp) { struct __old_dirent64 *dp; int saved_errno = errno; -#if IS_IN (libc) - __libc_lock_lock (dirp->lock); -#endif - do { size_t reclen; @@ -168,6 +164,21 @@ __old_readdir64 (DIR *dirp) /* Skip deleted files. */ } while (dp->d_ino == 0); + return dp; +} + +attribute_compat_text_section +struct __old_dirent64 * +__old_readdir64 (DIR *dirp) +{ + struct __old_dirent64 *dp; + +#if IS_IN (libc) + __libc_lock_lock (dirp->lock); +#endif + + dp = __old_readdir64_unlocked (dirp); + #if IS_IN (libc) __libc_lock_unlock (dirp->lock); #endif diff --git a/sysdeps/unix/sysv/linux/readdir64_r.c b/sysdeps/unix/sysv/linux/readdir64_r.c index e5ef7be4c2..133c534a69 100644 --- a/sysdeps/unix/sysv/linux/readdir64_r.c +++ b/sysdeps/unix/sysv/linux/readdir64_r.c @@ -91,89 +91,44 @@ __old_readdir64_r (DIR *dirp, struct __old_dirent64 *entry, { struct __old_dirent64 *dp; size_t reclen; - const int saved_errno = errno; - int ret; __libc_lock_lock (dirp->lock); - do + while (1) { - if (dirp->offset >= dirp->size) - { - /* We've emptied out our buffer. Refill it. */ - - size_t maxread = dirp->allocation; - ssize_t bytes; - - maxread = dirp->allocation; - - bytes = __old_getdents64 (dirp->fd, dirp->data, maxread); - if (bytes <= 0) - { - /* On some systems getdents fails with ENOENT when the - open directory has been rmdir'd already. POSIX.1 - requires that we treat this condition like normal EOF. */ - if (bytes < 0 && errno == ENOENT) - { - bytes = 0; - __set_errno (saved_errno); - } - if (bytes < 0) - dirp->errcode = errno; - - dp = NULL; - break; - } - dirp->size = (size_t) bytes; - - /* Reset the offset into the buffer. */ - dirp->offset = 0; - } - - dp = (struct __old_dirent64 *) &dirp->data[dirp->offset]; + dp = __old_readdir64_unlocked (dirp); + if (dp == NULL) + break; reclen = dp->d_reclen; + if (reclen <= offsetof (struct __old_dirent64, d_name) + NAME_MAX + 1) + break; - dirp->offset += reclen; - - dirp->filepos = dp->d_off; - - if (reclen > offsetof (struct __old_dirent64, d_name) + NAME_MAX + 1) + /* The record is very long. It could still fit into the caller-supplied + buffer if we can skip padding at the end. */ + size_t namelen = _D_EXACT_NAMLEN (dp); + if (namelen <= NAME_MAX) { - /* The record is very long. It could still fit into the - caller-supplied buffer if we can skip padding at the - end. */ - size_t namelen = _D_EXACT_NAMLEN (dp); - if (namelen <= NAME_MAX) - reclen = offsetof (struct __old_dirent64, d_name) + namelen + 1; - else - { - /* The name is too long. Ignore this file. */ - dirp->errcode = ENAMETOOLONG; - dp->d_ino = 0; - continue; - } + reclen = offsetof (struct dirent64, d_name) + namelen + 1; + break; } - /* Skip deleted and ignored files. */ + /* The name is too long. Ignore this file. */ + dirp->errcode = ENAMETOOLONG; + dp->d_ino = 0; } - while (dp->d_ino == 0); if (dp != NULL) { *result = memcpy (entry, dp, reclen); entry->d_reclen = reclen; - ret = 0; } else - { - *result = NULL; - ret = dirp->errcode; - } + *result = NULL; __libc_lock_unlock (dirp->lock); - return ret; + return dp != NULL ? 0 : dirp->errcode; } compat_symbol (libc, __old_readdir64_r, readdir64_r, GLIBC_2_1);