From patchwork Wed Oct 21 14:15:40 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: 40803 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 9389A3959E4D; Wed, 21 Oct 2020 14:16:03 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9389A3959E4D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1603289763; bh=OvaHi4zqVRdwY/J/FajvpE4pV+IZ4YZSnfjvw4ArdSQ=; 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=W2XjsfHSzH6keEvHBJb+LEY9sJm7Wok5nvIRW+6dqHCABeTK6HOScSYYpNoP/9Wc5 SHC7xfAbU8EB7aqq8Ue8ExD8mGdCYfMN/UTJg0JLKQMGs+KwDY5/EDnfn5aOR/BClS IuqoqePSyc3XzwVroXcrKaha5ZEXDD7SVLXbn6AM= 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 3A6FA3959E4B for ; Wed, 21 Oct 2020 14:16:01 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 3A6FA3959E4B Received: by mail-qk1-x744.google.com with SMTP id 140so2618684qko.2 for ; Wed, 21 Oct 2020 07:16:01 -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=OvaHi4zqVRdwY/J/FajvpE4pV+IZ4YZSnfjvw4ArdSQ=; b=V98XrfJsQuqKmFOs/GUmZlXpu02ZtdiyLyI4b8fTH1rgkO290Bhd4qbxdbqGnd0tom R/+89ysxZyRZ7I9CrhSGB9Omi+orW/UcMAegOMFTBQaENoZ+n2y+D4y86I3nWLvrjOIZ hZGRprhPcWka1lunQbEswecbELAvip0znTNLmglSL1obTjusvNIol8v9OkuvVoF3FFy5 6Zv0TfQCFqyF8vhZxPGLLmlLxT57x7T0YM5OclsP8StsR562d85WbNqc9VOeIAFY6LQA TrbQcLg7A0SLRrA7EvE9DyhXomsaZOw68nRKVNni5G2PnWHrz6OFQoUbwI1L5cONLcH/ VUOQ== X-Gm-Message-State: AOAM5311fErk1xtmfA3oLBNnErDivzXX5/f/FfxRA01ue20Idv54mBvX GhswBX0gxQwWfGCxCXIpPgOr5TtlZ2kMzw== X-Google-Smtp-Source: ABdhPJy143qNL3l8lM6BSlQXPUOWQESyKhF/grVSR4EQsymSB9Q+L+UZzrW+on2A1qdCjLCMmayqWg== X-Received: by 2002:a37:9f0a:: with SMTP id i10mr3220636qke.61.1603289760480; Wed, 21 Oct 2020 07:16:00 -0700 (PDT) Received: from localhost.localdomain ([177.194.48.209]) by smtp.googlemail.com with ESMTPSA id e17sm1187952qts.54.2020.10.21.07.15.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Oct 2020 07:16:00 -0700 (PDT) To: libc-alpha@sourceware.org, Florian Weimer Subject: [PATCH v3 5/7] linux: Add __old_readdir64_unlocked Date: Wed, 21 Oct 2020 11:15:40 -0300 Message-Id: <20201021141542.2003377-6-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201021141542.2003377-1-adhemerval.zanella@linaro.org> References: <20201021141542.2003377-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-14.1 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 ee41807b97..acd190733c 100644 --- a/sysdeps/unix/sysv/linux/readdir64.c +++ b/sysdeps/unix/sysv/linux/readdir64.c @@ -101,15 +101,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; @@ -153,6 +149,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);