From patchwork Fri Aug 30 19:51:46 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 1979200 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=K24eyzgT; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.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 ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WwTML08Jsz1yfn for ; Sat, 31 Aug 2024 05:52:30 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id BC9A43864825 for ; Fri, 30 Aug 2024 19:52:27 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTP id 8F7893864815 for ; Fri, 30 Aug 2024 19:51:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8F7893864815 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 8F7893864815 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725047520; cv=none; b=KSuKh8nj3dbtTgD3bHYrDWEIVq0hlw8uiC8cwvVKoALYXVSsKwSGBtoOXDc1Jc6YfLm6B+cRKuJkQh1RWKuOvegu1mjmvOBDIuSB8cv6CnZqvuWrycJAGnPsMVoYouT+1Ia8DndH/i7Uz7T2NTSSwOLLGJ2LNd75ARcXSZT9Aag= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725047520; c=relaxed/simple; bh=aQD8i902b/1sh3+AiAa7YJwspnWLuqHtsBxKcvpC+To=; h=DKIM-Signature:From:To:Subject:Message-ID:Date:MIME-Version; b=TJOEZesh43eeQkUzTXlB8PsQ7UuBQtu4y+IjNFT4YtDHqW4a4ggwqesvyAbs6LSRup8qlAxH6ccGUThAyI7VFQm4rwOHkZzM0mH0/SYigjh0Oc7npz/yG2tqIM0z/3DOE7VPg2VycKw3leTLxCMqwoBnoHQQwdtGvFuMLDd/8bw= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1725047514; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=SLocgDOggViG4CZozolmleVjC0QR3NtVt3KGgSOIFkY=; b=K24eyzgTZ5UFN70PVghHEeUHd9JB0jOf6qKnIby8Aa+2hhqZf++OEX7ls6iLBgfnKkamWJ sOINm0LiEdAYQYAyn0kkxI372Q+RM2JJ/TBTJBakGsWyms7X5r57TwU5VO9CLoHf2f7PA5 zdonOD0bHiqUHmL9BYzMXmmr/PF/1hQ= Received: from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-264-Gw-nYr8DMJC6e7Gij1qTiw-1; Fri, 30 Aug 2024 15:51:51 -0400 X-MC-Unique: Gw-nYr8DMJC6e7Gij1qTiw-1 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id E6FC918DB7EE for ; Fri, 30 Aug 2024 19:51:50 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.2.16.7]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 1F41C3003B63 for ; Fri, 30 Aug 2024 19:51:48 +0000 (UTC) From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH v4 01/13] Bundle userspace header from Linux 6.10 In-Reply-To: Message-ID: <58daa5582fae28f238bbb1f1cecadc3ff8ded1a7.1725047142.git.fweimer@redhat.com> References: X-From-Line: 58daa5582fae28f238bbb1f1cecadc3ff8ded1a7 Mon Sep 17 00:00:00 2001 Date: Fri, 30 Aug 2024 21:51:46 +0200 User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-22.9 required=5.0 tests=BAYES_00, DKIM_INVALID, DKIM_SIGNED, GIT_PATCH_0, KAM_DMARC_NONE, KAM_DMARC_STATUS, KAM_NUMSUBJECT, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org And include the required licensing information. The only change is a removed trailing empty line in LICENSES/exceptions/Linux-syscall-note. Bundling is the recommended way to deal with the evolution of the FUSE userspace interface because structs change sizes over time. The kernel maintains compatibility, but source-level compatibility on recompilation may require additional code that is aware of older struct sizes. Signed-off-by: Florian Weimer --- support/bundled/README | 5 + support/bundled/linux/COPYING | 20 + .../LICENSES/exceptions/Linux-syscall-note | 24 + .../bundled/linux/LICENSES/preferred/GPL-2.0 | 359 +++++ .../bundled/linux/include/uapi/linux/fuse.h | 1189 +++++++++++++++++ 5 files changed, 1597 insertions(+) create mode 100644 support/bundled/README create mode 100644 support/bundled/linux/COPYING create mode 100644 support/bundled/linux/LICENSES/exceptions/Linux-syscall-note create mode 100644 support/bundled/linux/LICENSES/preferred/GPL-2.0 create mode 100644 support/bundled/linux/include/uapi/linux/fuse.h diff --git a/support/bundled/README b/support/bundled/README new file mode 100644 index 0000000000..e861b3d40a --- /dev/null +++ b/support/bundled/README @@ -0,0 +1,5 @@ +This subtree contains bundled files included verbatim from other +sources. They are used for building the support/ infrastructure. + +linux/ + Select files from the Linux 6.10 source tree. diff --git a/support/bundled/linux/COPYING b/support/bundled/linux/COPYING new file mode 100644 index 0000000000..a635a38ef9 --- /dev/null +++ b/support/bundled/linux/COPYING @@ -0,0 +1,20 @@ +The Linux Kernel is provided under: + + SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note + +Being under the terms of the GNU General Public License version 2 only, +according with: + + LICENSES/preferred/GPL-2.0 + +With an explicit syscall exception, as stated at: + + LICENSES/exceptions/Linux-syscall-note + +In addition, other licenses may also apply. Please see: + + Documentation/process/license-rules.rst + +for more details. + +All contributions to the Linux Kernel are subject to this COPYING file. diff --git a/support/bundled/linux/LICENSES/exceptions/Linux-syscall-note b/support/bundled/linux/LICENSES/exceptions/Linux-syscall-note new file mode 100644 index 0000000000..adbe756a05 --- /dev/null +++ b/support/bundled/linux/LICENSES/exceptions/Linux-syscall-note @@ -0,0 +1,24 @@ +SPDX-Exception-Identifier: Linux-syscall-note +SPDX-URL: https://spdx.org/licenses/Linux-syscall-note.html +SPDX-Licenses: GPL-2.0, GPL-2.0+, GPL-1.0+, LGPL-2.0, LGPL-2.0+, LGPL-2.1, LGPL-2.1+, GPL-2.0-only, GPL-2.0-or-later +Usage-Guide: + This exception is used together with one of the above SPDX-Licenses + to mark user space API (uapi) header files so they can be included + into non GPL compliant user space application code. + To use this exception add it with the keyword WITH to one of the + identifiers in the SPDX-Licenses tag: + SPDX-License-Identifier: WITH Linux-syscall-note +License-Text: + + NOTE! This copyright does *not* cover user programs that use kernel + services by normal system calls - this is merely considered normal use + of the kernel, and does *not* fall under the heading of "derived work". + Also note that the GPL below is copyrighted by the Free Software + Foundation, but the instance of code that it refers to (the Linux + kernel) is copyrighted by me and others who actually wrote it. + + Also note that the only valid version of the GPL as far as the kernel + is concerned is _this_ particular version of the license (ie v2, not + v2.2 or v3.x or whatever), unless explicitly otherwise stated. + + Linus Torvalds diff --git a/support/bundled/linux/LICENSES/preferred/GPL-2.0 b/support/bundled/linux/LICENSES/preferred/GPL-2.0 new file mode 100644 index 0000000000..ff0812fd89 --- /dev/null +++ b/support/bundled/linux/LICENSES/preferred/GPL-2.0 @@ -0,0 +1,359 @@ +Valid-License-Identifier: GPL-2.0 +Valid-License-Identifier: GPL-2.0-only +Valid-License-Identifier: GPL-2.0+ +Valid-License-Identifier: GPL-2.0-or-later +SPDX-URL: https://spdx.org/licenses/GPL-2.0.html +Usage-Guide: + To use this license in source code, put one of the following SPDX + tag/value pairs into a comment according to the placement + guidelines in the licensing rules documentation. + For 'GNU General Public License (GPL) version 2 only' use: + SPDX-License-Identifier: GPL-2.0 + or + SPDX-License-Identifier: GPL-2.0-only + For 'GNU General Public License (GPL) version 2 or any later version' use: + SPDX-License-Identifier: GPL-2.0+ + or + SPDX-License-Identifier: GPL-2.0-or-later +License-Text: + + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc. + 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Library General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + 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 2 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, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) year name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, the commands you use may +be called something other than `show w' and `show c'; they could even be +mouse-clicks or menu items--whatever suits your program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the program + `Gnomovision' (which makes passes at compilers) written by James Hacker. + + , 1 April 1989 + Ty Coon, President of Vice + +This General Public License does not permit incorporating your program into +proprietary programs. If your program is a subroutine library, you may +consider it more useful to permit linking proprietary applications with the +library. If this is what you want to do, use the GNU Library General +Public License instead of this License. diff --git a/support/bundled/linux/include/uapi/linux/fuse.h b/support/bundled/linux/include/uapi/linux/fuse.h new file mode 100644 index 0000000000..d08b99d60f --- /dev/null +++ b/support/bundled/linux/include/uapi/linux/fuse.h @@ -0,0 +1,1189 @@ +/* SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-2-Clause) */ +/* + This file defines the kernel interface of FUSE + Copyright (C) 2001-2008 Miklos Szeredi + + This program can be distributed under the terms of the GNU GPL. + See the file COPYING. + + This -- and only this -- header file may also be distributed under + the terms of the BSD Licence as follows: + + Copyright (C) 2001-2007 Miklos Szeredi. All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND + ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + SUCH DAMAGE. +*/ + +/* + * This file defines the kernel interface of FUSE + * + * Protocol changelog: + * + * 7.1: + * - add the following messages: + * FUSE_SETATTR, FUSE_SYMLINK, FUSE_MKNOD, FUSE_MKDIR, FUSE_UNLINK, + * FUSE_RMDIR, FUSE_RENAME, FUSE_LINK, FUSE_OPEN, FUSE_READ, FUSE_WRITE, + * FUSE_RELEASE, FUSE_FSYNC, FUSE_FLUSH, FUSE_SETXATTR, FUSE_GETXATTR, + * FUSE_LISTXATTR, FUSE_REMOVEXATTR, FUSE_OPENDIR, FUSE_READDIR, + * FUSE_RELEASEDIR + * - add padding to messages to accommodate 32-bit servers on 64-bit kernels + * + * 7.2: + * - add FOPEN_DIRECT_IO and FOPEN_KEEP_CACHE flags + * - add FUSE_FSYNCDIR message + * + * 7.3: + * - add FUSE_ACCESS message + * - add FUSE_CREATE message + * - add filehandle to fuse_setattr_in + * + * 7.4: + * - add frsize to fuse_kstatfs + * - clean up request size limit checking + * + * 7.5: + * - add flags and max_write to fuse_init_out + * + * 7.6: + * - add max_readahead to fuse_init_in and fuse_init_out + * + * 7.7: + * - add FUSE_INTERRUPT message + * - add POSIX file lock support + * + * 7.8: + * - add lock_owner and flags fields to fuse_release_in + * - add FUSE_BMAP message + * - add FUSE_DESTROY message + * + * 7.9: + * - new fuse_getattr_in input argument of GETATTR + * - add lk_flags in fuse_lk_in + * - add lock_owner field to fuse_setattr_in, fuse_read_in and fuse_write_in + * - add blksize field to fuse_attr + * - add file flags field to fuse_read_in and fuse_write_in + * - Add ATIME_NOW and MTIME_NOW flags to fuse_setattr_in + * + * 7.10 + * - add nonseekable open flag + * + * 7.11 + * - add IOCTL message + * - add unsolicited notification support + * - add POLL message and NOTIFY_POLL notification + * + * 7.12 + * - add umask flag to input argument of create, mknod and mkdir + * - add notification messages for invalidation of inodes and + * directory entries + * + * 7.13 + * - make max number of background requests and congestion threshold + * tunables + * + * 7.14 + * - add splice support to fuse device + * + * 7.15 + * - add store notify + * - add retrieve notify + * + * 7.16 + * - add BATCH_FORGET request + * - FUSE_IOCTL_UNRESTRICTED shall now return with array of 'struct + * fuse_ioctl_iovec' instead of ambiguous 'struct iovec' + * - add FUSE_IOCTL_32BIT flag + * + * 7.17 + * - add FUSE_FLOCK_LOCKS and FUSE_RELEASE_FLOCK_UNLOCK + * + * 7.18 + * - add FUSE_IOCTL_DIR flag + * - add FUSE_NOTIFY_DELETE + * + * 7.19 + * - add FUSE_FALLOCATE + * + * 7.20 + * - add FUSE_AUTO_INVAL_DATA + * + * 7.21 + * - add FUSE_READDIRPLUS + * - send the requested events in POLL request + * + * 7.22 + * - add FUSE_ASYNC_DIO + * + * 7.23 + * - add FUSE_WRITEBACK_CACHE + * - add time_gran to fuse_init_out + * - add reserved space to fuse_init_out + * - add FATTR_CTIME + * - add ctime and ctimensec to fuse_setattr_in + * - add FUSE_RENAME2 request + * - add FUSE_NO_OPEN_SUPPORT flag + * + * 7.24 + * - add FUSE_LSEEK for SEEK_HOLE and SEEK_DATA support + * + * 7.25 + * - add FUSE_PARALLEL_DIROPS + * + * 7.26 + * - add FUSE_HANDLE_KILLPRIV + * - add FUSE_POSIX_ACL + * + * 7.27 + * - add FUSE_ABORT_ERROR + * + * 7.28 + * - add FUSE_COPY_FILE_RANGE + * - add FOPEN_CACHE_DIR + * - add FUSE_MAX_PAGES, add max_pages to init_out + * - add FUSE_CACHE_SYMLINKS + * + * 7.29 + * - add FUSE_NO_OPENDIR_SUPPORT flag + * + * 7.30 + * - add FUSE_EXPLICIT_INVAL_DATA + * - add FUSE_IOCTL_COMPAT_X32 + * + * 7.31 + * - add FUSE_WRITE_KILL_PRIV flag + * - add FUSE_SETUPMAPPING and FUSE_REMOVEMAPPING + * - add map_alignment to fuse_init_out, add FUSE_MAP_ALIGNMENT flag + * + * 7.32 + * - add flags to fuse_attr, add FUSE_ATTR_SUBMOUNT, add FUSE_SUBMOUNTS + * + * 7.33 + * - add FUSE_HANDLE_KILLPRIV_V2, FUSE_WRITE_KILL_SUIDGID, FATTR_KILL_SUIDGID + * - add FUSE_OPEN_KILL_SUIDGID + * - extend fuse_setxattr_in, add FUSE_SETXATTR_EXT + * - add FUSE_SETXATTR_ACL_KILL_SGID + * + * 7.34 + * - add FUSE_SYNCFS + * + * 7.35 + * - add FOPEN_NOFLUSH + * + * 7.36 + * - extend fuse_init_in with reserved fields, add FUSE_INIT_EXT init flag + * - add flags2 to fuse_init_in and fuse_init_out + * - add FUSE_SECURITY_CTX init flag + * - add security context to create, mkdir, symlink, and mknod requests + * - add FUSE_HAS_INODE_DAX, FUSE_ATTR_DAX + * + * 7.37 + * - add FUSE_TMPFILE + * + * 7.38 + * - add FUSE_EXPIRE_ONLY flag to fuse_notify_inval_entry + * - add FOPEN_PARALLEL_DIRECT_WRITES + * - add total_extlen to fuse_in_header + * - add FUSE_MAX_NR_SECCTX + * - add extension header + * - add FUSE_EXT_GROUPS + * - add FUSE_CREATE_SUPP_GROUP + * - add FUSE_HAS_EXPIRE_ONLY + * + * 7.39 + * - add FUSE_DIRECT_IO_ALLOW_MMAP + * - add FUSE_STATX and related structures + * + * 7.40 + * - add max_stack_depth to fuse_init_out, add FUSE_PASSTHROUGH init flag + * - add backing_id to fuse_open_out, add FOPEN_PASSTHROUGH open flag + * - add FUSE_NO_EXPORT_SUPPORT init flag + * - add FUSE_NOTIFY_RESEND, add FUSE_HAS_RESEND init flag + */ + +#ifndef _LINUX_FUSE_H +#define _LINUX_FUSE_H + +#ifdef __KERNEL__ +#include +#else +#include +#endif + +/* + * Version negotiation: + * + * Both the kernel and userspace send the version they support in the + * INIT request and reply respectively. + * + * If the major versions match then both shall use the smallest + * of the two minor versions for communication. + * + * If the kernel supports a larger major version, then userspace shall + * reply with the major version it supports, ignore the rest of the + * INIT message and expect a new INIT message from the kernel with a + * matching major version. + * + * If the library supports a larger major version, then it shall fall + * back to the major protocol version sent by the kernel for + * communication and reply with that major version (and an arbitrary + * supported minor version). + */ + +/** Version number of this interface */ +#define FUSE_KERNEL_VERSION 7 + +/** Minor version number of this interface */ +#define FUSE_KERNEL_MINOR_VERSION 40 + +/** The node ID of the root inode */ +#define FUSE_ROOT_ID 1 + +/* Make sure all structures are padded to 64bit boundary, so 32bit + userspace works under 64bit kernels */ + +struct fuse_attr { + uint64_t ino; + uint64_t size; + uint64_t blocks; + uint64_t atime; + uint64_t mtime; + uint64_t ctime; + uint32_t atimensec; + uint32_t mtimensec; + uint32_t ctimensec; + uint32_t mode; + uint32_t nlink; + uint32_t uid; + uint32_t gid; + uint32_t rdev; + uint32_t blksize; + uint32_t flags; +}; + +/* + * The following structures are bit-for-bit compatible with the statx(2) ABI in + * Linux. + */ +struct fuse_sx_time { + int64_t tv_sec; + uint32_t tv_nsec; + int32_t __reserved; +}; + +struct fuse_statx { + uint32_t mask; + uint32_t blksize; + uint64_t attributes; + uint32_t nlink; + uint32_t uid; + uint32_t gid; + uint16_t mode; + uint16_t __spare0[1]; + uint64_t ino; + uint64_t size; + uint64_t blocks; + uint64_t attributes_mask; + struct fuse_sx_time atime; + struct fuse_sx_time btime; + struct fuse_sx_time ctime; + struct fuse_sx_time mtime; + uint32_t rdev_major; + uint32_t rdev_minor; + uint32_t dev_major; + uint32_t dev_minor; + uint64_t __spare2[14]; +}; + +struct fuse_kstatfs { + uint64_t blocks; + uint64_t bfree; + uint64_t bavail; + uint64_t files; + uint64_t ffree; + uint32_t bsize; + uint32_t namelen; + uint32_t frsize; + uint32_t padding; + uint32_t spare[6]; +}; + +struct fuse_file_lock { + uint64_t start; + uint64_t end; + uint32_t type; + uint32_t pid; /* tgid */ +}; + +/** + * Bitmasks for fuse_setattr_in.valid + */ +#define FATTR_MODE (1 << 0) +#define FATTR_UID (1 << 1) +#define FATTR_GID (1 << 2) +#define FATTR_SIZE (1 << 3) +#define FATTR_ATIME (1 << 4) +#define FATTR_MTIME (1 << 5) +#define FATTR_FH (1 << 6) +#define FATTR_ATIME_NOW (1 << 7) +#define FATTR_MTIME_NOW (1 << 8) +#define FATTR_LOCKOWNER (1 << 9) +#define FATTR_CTIME (1 << 10) +#define FATTR_KILL_SUIDGID (1 << 11) + +/** + * Flags returned by the OPEN request + * + * FOPEN_DIRECT_IO: bypass page cache for this open file + * FOPEN_KEEP_CACHE: don't invalidate the data cache on open + * FOPEN_NONSEEKABLE: the file is not seekable + * FOPEN_CACHE_DIR: allow caching this directory + * FOPEN_STREAM: the file is stream-like (no file position at all) + * FOPEN_NOFLUSH: don't flush data cache on close (unless FUSE_WRITEBACK_CACHE) + * FOPEN_PARALLEL_DIRECT_WRITES: Allow concurrent direct writes on the same inode + * FOPEN_PASSTHROUGH: passthrough read/write io for this open file + */ +#define FOPEN_DIRECT_IO (1 << 0) +#define FOPEN_KEEP_CACHE (1 << 1) +#define FOPEN_NONSEEKABLE (1 << 2) +#define FOPEN_CACHE_DIR (1 << 3) +#define FOPEN_STREAM (1 << 4) +#define FOPEN_NOFLUSH (1 << 5) +#define FOPEN_PARALLEL_DIRECT_WRITES (1 << 6) +#define FOPEN_PASSTHROUGH (1 << 7) + +/** + * INIT request/reply flags + * + * FUSE_ASYNC_READ: asynchronous read requests + * FUSE_POSIX_LOCKS: remote locking for POSIX file locks + * FUSE_FILE_OPS: kernel sends file handle for fstat, etc... (not yet supported) + * FUSE_ATOMIC_O_TRUNC: handles the O_TRUNC open flag in the filesystem + * FUSE_EXPORT_SUPPORT: filesystem handles lookups of "." and ".." + * FUSE_BIG_WRITES: filesystem can handle write size larger than 4kB + * FUSE_DONT_MASK: don't apply umask to file mode on create operations + * FUSE_SPLICE_WRITE: kernel supports splice write on the device + * FUSE_SPLICE_MOVE: kernel supports splice move on the device + * FUSE_SPLICE_READ: kernel supports splice read on the device + * FUSE_FLOCK_LOCKS: remote locking for BSD style file locks + * FUSE_HAS_IOCTL_DIR: kernel supports ioctl on directories + * FUSE_AUTO_INVAL_DATA: automatically invalidate cached pages + * FUSE_DO_READDIRPLUS: do READDIRPLUS (READDIR+LOOKUP in one) + * FUSE_READDIRPLUS_AUTO: adaptive readdirplus + * FUSE_ASYNC_DIO: asynchronous direct I/O submission + * FUSE_WRITEBACK_CACHE: use writeback cache for buffered writes + * FUSE_NO_OPEN_SUPPORT: kernel supports zero-message opens + * FUSE_PARALLEL_DIROPS: allow parallel lookups and readdir + * FUSE_HANDLE_KILLPRIV: fs handles killing suid/sgid/cap on write/chown/trunc + * FUSE_POSIX_ACL: filesystem supports posix acls + * FUSE_ABORT_ERROR: reading the device after abort returns ECONNABORTED + * FUSE_MAX_PAGES: init_out.max_pages contains the max number of req pages + * FUSE_CACHE_SYMLINKS: cache READLINK responses + * FUSE_NO_OPENDIR_SUPPORT: kernel supports zero-message opendir + * FUSE_EXPLICIT_INVAL_DATA: only invalidate cached pages on explicit request + * FUSE_MAP_ALIGNMENT: init_out.map_alignment contains log2(byte alignment) for + * foffset and moffset fields in struct + * fuse_setupmapping_out and fuse_removemapping_one. + * FUSE_SUBMOUNTS: kernel supports auto-mounting directory submounts + * FUSE_HANDLE_KILLPRIV_V2: fs kills suid/sgid/cap on write/chown/trunc. + * Upon write/truncate suid/sgid is only killed if caller + * does not have CAP_FSETID. Additionally upon + * write/truncate sgid is killed only if file has group + * execute permission. (Same as Linux VFS behavior). + * FUSE_SETXATTR_EXT: Server supports extended struct fuse_setxattr_in + * FUSE_INIT_EXT: extended fuse_init_in request + * FUSE_INIT_RESERVED: reserved, do not use + * FUSE_SECURITY_CTX: add security context to create, mkdir, symlink, and + * mknod + * FUSE_HAS_INODE_DAX: use per inode DAX + * FUSE_CREATE_SUPP_GROUP: add supplementary group info to create, mkdir, + * symlink and mknod (single group that matches parent) + * FUSE_HAS_EXPIRE_ONLY: kernel supports expiry-only entry invalidation + * FUSE_DIRECT_IO_ALLOW_MMAP: allow shared mmap in FOPEN_DIRECT_IO mode. + * FUSE_NO_EXPORT_SUPPORT: explicitly disable export support + * FUSE_HAS_RESEND: kernel supports resending pending requests, and the high bit + * of the request ID indicates resend requests + */ +#define FUSE_ASYNC_READ (1 << 0) +#define FUSE_POSIX_LOCKS (1 << 1) +#define FUSE_FILE_OPS (1 << 2) +#define FUSE_ATOMIC_O_TRUNC (1 << 3) +#define FUSE_EXPORT_SUPPORT (1 << 4) +#define FUSE_BIG_WRITES (1 << 5) +#define FUSE_DONT_MASK (1 << 6) +#define FUSE_SPLICE_WRITE (1 << 7) +#define FUSE_SPLICE_MOVE (1 << 8) +#define FUSE_SPLICE_READ (1 << 9) +#define FUSE_FLOCK_LOCKS (1 << 10) +#define FUSE_HAS_IOCTL_DIR (1 << 11) +#define FUSE_AUTO_INVAL_DATA (1 << 12) +#define FUSE_DO_READDIRPLUS (1 << 13) +#define FUSE_READDIRPLUS_AUTO (1 << 14) +#define FUSE_ASYNC_DIO (1 << 15) +#define FUSE_WRITEBACK_CACHE (1 << 16) +#define FUSE_NO_OPEN_SUPPORT (1 << 17) +#define FUSE_PARALLEL_DIROPS (1 << 18) +#define FUSE_HANDLE_KILLPRIV (1 << 19) +#define FUSE_POSIX_ACL (1 << 20) +#define FUSE_ABORT_ERROR (1 << 21) +#define FUSE_MAX_PAGES (1 << 22) +#define FUSE_CACHE_SYMLINKS (1 << 23) +#define FUSE_NO_OPENDIR_SUPPORT (1 << 24) +#define FUSE_EXPLICIT_INVAL_DATA (1 << 25) +#define FUSE_MAP_ALIGNMENT (1 << 26) +#define FUSE_SUBMOUNTS (1 << 27) +#define FUSE_HANDLE_KILLPRIV_V2 (1 << 28) +#define FUSE_SETXATTR_EXT (1 << 29) +#define FUSE_INIT_EXT (1 << 30) +#define FUSE_INIT_RESERVED (1 << 31) +/* bits 32..63 get shifted down 32 bits into the flags2 field */ +#define FUSE_SECURITY_CTX (1ULL << 32) +#define FUSE_HAS_INODE_DAX (1ULL << 33) +#define FUSE_CREATE_SUPP_GROUP (1ULL << 34) +#define FUSE_HAS_EXPIRE_ONLY (1ULL << 35) +#define FUSE_DIRECT_IO_ALLOW_MMAP (1ULL << 36) +#define FUSE_PASSTHROUGH (1ULL << 37) +#define FUSE_NO_EXPORT_SUPPORT (1ULL << 38) +#define FUSE_HAS_RESEND (1ULL << 39) + +/* Obsolete alias for FUSE_DIRECT_IO_ALLOW_MMAP */ +#define FUSE_DIRECT_IO_RELAX FUSE_DIRECT_IO_ALLOW_MMAP + +/** + * CUSE INIT request/reply flags + * + * CUSE_UNRESTRICTED_IOCTL: use unrestricted ioctl + */ +#define CUSE_UNRESTRICTED_IOCTL (1 << 0) + +/** + * Release flags + */ +#define FUSE_RELEASE_FLUSH (1 << 0) +#define FUSE_RELEASE_FLOCK_UNLOCK (1 << 1) + +/** + * Getattr flags + */ +#define FUSE_GETATTR_FH (1 << 0) + +/** + * Lock flags + */ +#define FUSE_LK_FLOCK (1 << 0) + +/** + * WRITE flags + * + * FUSE_WRITE_CACHE: delayed write from page cache, file handle is guessed + * FUSE_WRITE_LOCKOWNER: lock_owner field is valid + * FUSE_WRITE_KILL_SUIDGID: kill suid and sgid bits + */ +#define FUSE_WRITE_CACHE (1 << 0) +#define FUSE_WRITE_LOCKOWNER (1 << 1) +#define FUSE_WRITE_KILL_SUIDGID (1 << 2) + +/* Obsolete alias; this flag implies killing suid/sgid only. */ +#define FUSE_WRITE_KILL_PRIV FUSE_WRITE_KILL_SUIDGID + +/** + * Read flags + */ +#define FUSE_READ_LOCKOWNER (1 << 1) + +/** + * Ioctl flags + * + * FUSE_IOCTL_COMPAT: 32bit compat ioctl on 64bit machine + * FUSE_IOCTL_UNRESTRICTED: not restricted to well-formed ioctls, retry allowed + * FUSE_IOCTL_RETRY: retry with new iovecs + * FUSE_IOCTL_32BIT: 32bit ioctl + * FUSE_IOCTL_DIR: is a directory + * FUSE_IOCTL_COMPAT_X32: x32 compat ioctl on 64bit machine (64bit time_t) + * + * FUSE_IOCTL_MAX_IOV: maximum of in_iovecs + out_iovecs + */ +#define FUSE_IOCTL_COMPAT (1 << 0) +#define FUSE_IOCTL_UNRESTRICTED (1 << 1) +#define FUSE_IOCTL_RETRY (1 << 2) +#define FUSE_IOCTL_32BIT (1 << 3) +#define FUSE_IOCTL_DIR (1 << 4) +#define FUSE_IOCTL_COMPAT_X32 (1 << 5) + +#define FUSE_IOCTL_MAX_IOV 256 + +/** + * Poll flags + * + * FUSE_POLL_SCHEDULE_NOTIFY: request poll notify + */ +#define FUSE_POLL_SCHEDULE_NOTIFY (1 << 0) + +/** + * Fsync flags + * + * FUSE_FSYNC_FDATASYNC: Sync data only, not metadata + */ +#define FUSE_FSYNC_FDATASYNC (1 << 0) + +/** + * fuse_attr flags + * + * FUSE_ATTR_SUBMOUNT: Object is a submount root + * FUSE_ATTR_DAX: Enable DAX for this file in per inode DAX mode + */ +#define FUSE_ATTR_SUBMOUNT (1 << 0) +#define FUSE_ATTR_DAX (1 << 1) + +/** + * Open flags + * FUSE_OPEN_KILL_SUIDGID: Kill suid and sgid if executable + */ +#define FUSE_OPEN_KILL_SUIDGID (1 << 0) + +/** + * setxattr flags + * FUSE_SETXATTR_ACL_KILL_SGID: Clear SGID when system.posix_acl_access is set + */ +#define FUSE_SETXATTR_ACL_KILL_SGID (1 << 0) + +/** + * notify_inval_entry flags + * FUSE_EXPIRE_ONLY + */ +#define FUSE_EXPIRE_ONLY (1 << 0) + +/** + * extension type + * FUSE_MAX_NR_SECCTX: maximum value of &fuse_secctx_header.nr_secctx + * FUSE_EXT_GROUPS: &fuse_supp_groups extension + */ +enum fuse_ext_type { + /* Types 0..31 are reserved for fuse_secctx_header */ + FUSE_MAX_NR_SECCTX = 31, + FUSE_EXT_GROUPS = 32, +}; + +enum fuse_opcode { + FUSE_LOOKUP = 1, + FUSE_FORGET = 2, /* no reply */ + FUSE_GETATTR = 3, + FUSE_SETATTR = 4, + FUSE_READLINK = 5, + FUSE_SYMLINK = 6, + FUSE_MKNOD = 8, + FUSE_MKDIR = 9, + FUSE_UNLINK = 10, + FUSE_RMDIR = 11, + FUSE_RENAME = 12, + FUSE_LINK = 13, + FUSE_OPEN = 14, + FUSE_READ = 15, + FUSE_WRITE = 16, + FUSE_STATFS = 17, + FUSE_RELEASE = 18, + FUSE_FSYNC = 20, + FUSE_SETXATTR = 21, + FUSE_GETXATTR = 22, + FUSE_LISTXATTR = 23, + FUSE_REMOVEXATTR = 24, + FUSE_FLUSH = 25, + FUSE_INIT = 26, + FUSE_OPENDIR = 27, + FUSE_READDIR = 28, + FUSE_RELEASEDIR = 29, + FUSE_FSYNCDIR = 30, + FUSE_GETLK = 31, + FUSE_SETLK = 32, + FUSE_SETLKW = 33, + FUSE_ACCESS = 34, + FUSE_CREATE = 35, + FUSE_INTERRUPT = 36, + FUSE_BMAP = 37, + FUSE_DESTROY = 38, + FUSE_IOCTL = 39, + FUSE_POLL = 40, + FUSE_NOTIFY_REPLY = 41, + FUSE_BATCH_FORGET = 42, + FUSE_FALLOCATE = 43, + FUSE_READDIRPLUS = 44, + FUSE_RENAME2 = 45, + FUSE_LSEEK = 46, + FUSE_COPY_FILE_RANGE = 47, + FUSE_SETUPMAPPING = 48, + FUSE_REMOVEMAPPING = 49, + FUSE_SYNCFS = 50, + FUSE_TMPFILE = 51, + FUSE_STATX = 52, + + /* CUSE specific operations */ + CUSE_INIT = 4096, + + /* Reserved opcodes: helpful to detect structure endian-ness */ + CUSE_INIT_BSWAP_RESERVED = 1048576, /* CUSE_INIT << 8 */ + FUSE_INIT_BSWAP_RESERVED = 436207616, /* FUSE_INIT << 24 */ +}; + +enum fuse_notify_code { + FUSE_NOTIFY_POLL = 1, + FUSE_NOTIFY_INVAL_INODE = 2, + FUSE_NOTIFY_INVAL_ENTRY = 3, + FUSE_NOTIFY_STORE = 4, + FUSE_NOTIFY_RETRIEVE = 5, + FUSE_NOTIFY_DELETE = 6, + FUSE_NOTIFY_RESEND = 7, + FUSE_NOTIFY_CODE_MAX, +}; + +/* The read buffer is required to be at least 8k, but may be much larger */ +#define FUSE_MIN_READ_BUFFER 8192 + +#define FUSE_COMPAT_ENTRY_OUT_SIZE 120 + +struct fuse_entry_out { + uint64_t nodeid; /* Inode ID */ + uint64_t generation; /* Inode generation: nodeid:gen must + be unique for the fs's lifetime */ + uint64_t entry_valid; /* Cache timeout for the name */ + uint64_t attr_valid; /* Cache timeout for the attributes */ + uint32_t entry_valid_nsec; + uint32_t attr_valid_nsec; + struct fuse_attr attr; +}; + +struct fuse_forget_in { + uint64_t nlookup; +}; + +struct fuse_forget_one { + uint64_t nodeid; + uint64_t nlookup; +}; + +struct fuse_batch_forget_in { + uint32_t count; + uint32_t dummy; +}; + +struct fuse_getattr_in { + uint32_t getattr_flags; + uint32_t dummy; + uint64_t fh; +}; + +#define FUSE_COMPAT_ATTR_OUT_SIZE 96 + +struct fuse_attr_out { + uint64_t attr_valid; /* Cache timeout for the attributes */ + uint32_t attr_valid_nsec; + uint32_t dummy; + struct fuse_attr attr; +}; + +struct fuse_statx_in { + uint32_t getattr_flags; + uint32_t reserved; + uint64_t fh; + uint32_t sx_flags; + uint32_t sx_mask; +}; + +struct fuse_statx_out { + uint64_t attr_valid; /* Cache timeout for the attributes */ + uint32_t attr_valid_nsec; + uint32_t flags; + uint64_t spare[2]; + struct fuse_statx stat; +}; + +#define FUSE_COMPAT_MKNOD_IN_SIZE 8 + +struct fuse_mknod_in { + uint32_t mode; + uint32_t rdev; + uint32_t umask; + uint32_t padding; +}; + +struct fuse_mkdir_in { + uint32_t mode; + uint32_t umask; +}; + +struct fuse_rename_in { + uint64_t newdir; +}; + +struct fuse_rename2_in { + uint64_t newdir; + uint32_t flags; + uint32_t padding; +}; + +struct fuse_link_in { + uint64_t oldnodeid; +}; + +struct fuse_setattr_in { + uint32_t valid; + uint32_t padding; + uint64_t fh; + uint64_t size; + uint64_t lock_owner; + uint64_t atime; + uint64_t mtime; + uint64_t ctime; + uint32_t atimensec; + uint32_t mtimensec; + uint32_t ctimensec; + uint32_t mode; + uint32_t unused4; + uint32_t uid; + uint32_t gid; + uint32_t unused5; +}; + +struct fuse_open_in { + uint32_t flags; + uint32_t open_flags; /* FUSE_OPEN_... */ +}; + +struct fuse_create_in { + uint32_t flags; + uint32_t mode; + uint32_t umask; + uint32_t open_flags; /* FUSE_OPEN_... */ +}; + +struct fuse_open_out { + uint64_t fh; + uint32_t open_flags; + int32_t backing_id; +}; + +struct fuse_release_in { + uint64_t fh; + uint32_t flags; + uint32_t release_flags; + uint64_t lock_owner; +}; + +struct fuse_flush_in { + uint64_t fh; + uint32_t unused; + uint32_t padding; + uint64_t lock_owner; +}; + +struct fuse_read_in { + uint64_t fh; + uint64_t offset; + uint32_t size; + uint32_t read_flags; + uint64_t lock_owner; + uint32_t flags; + uint32_t padding; +}; + +#define FUSE_COMPAT_WRITE_IN_SIZE 24 + +struct fuse_write_in { + uint64_t fh; + uint64_t offset; + uint32_t size; + uint32_t write_flags; + uint64_t lock_owner; + uint32_t flags; + uint32_t padding; +}; + +struct fuse_write_out { + uint32_t size; + uint32_t padding; +}; + +#define FUSE_COMPAT_STATFS_SIZE 48 + +struct fuse_statfs_out { + struct fuse_kstatfs st; +}; + +struct fuse_fsync_in { + uint64_t fh; + uint32_t fsync_flags; + uint32_t padding; +}; + +#define FUSE_COMPAT_SETXATTR_IN_SIZE 8 + +struct fuse_setxattr_in { + uint32_t size; + uint32_t flags; + uint32_t setxattr_flags; + uint32_t padding; +}; + +struct fuse_getxattr_in { + uint32_t size; + uint32_t padding; +}; + +struct fuse_getxattr_out { + uint32_t size; + uint32_t padding; +}; + +struct fuse_lk_in { + uint64_t fh; + uint64_t owner; + struct fuse_file_lock lk; + uint32_t lk_flags; + uint32_t padding; +}; + +struct fuse_lk_out { + struct fuse_file_lock lk; +}; + +struct fuse_access_in { + uint32_t mask; + uint32_t padding; +}; + +struct fuse_init_in { + uint32_t major; + uint32_t minor; + uint32_t max_readahead; + uint32_t flags; + uint32_t flags2; + uint32_t unused[11]; +}; + +#define FUSE_COMPAT_INIT_OUT_SIZE 8 +#define FUSE_COMPAT_22_INIT_OUT_SIZE 24 + +struct fuse_init_out { + uint32_t major; + uint32_t minor; + uint32_t max_readahead; + uint32_t flags; + uint16_t max_background; + uint16_t congestion_threshold; + uint32_t max_write; + uint32_t time_gran; + uint16_t max_pages; + uint16_t map_alignment; + uint32_t flags2; + uint32_t max_stack_depth; + uint32_t unused[6]; +}; + +#define CUSE_INIT_INFO_MAX 4096 + +struct cuse_init_in { + uint32_t major; + uint32_t minor; + uint32_t unused; + uint32_t flags; +}; + +struct cuse_init_out { + uint32_t major; + uint32_t minor; + uint32_t unused; + uint32_t flags; + uint32_t max_read; + uint32_t max_write; + uint32_t dev_major; /* chardev major */ + uint32_t dev_minor; /* chardev minor */ + uint32_t spare[10]; +}; + +struct fuse_interrupt_in { + uint64_t unique; +}; + +struct fuse_bmap_in { + uint64_t block; + uint32_t blocksize; + uint32_t padding; +}; + +struct fuse_bmap_out { + uint64_t block; +}; + +struct fuse_ioctl_in { + uint64_t fh; + uint32_t flags; + uint32_t cmd; + uint64_t arg; + uint32_t in_size; + uint32_t out_size; +}; + +struct fuse_ioctl_iovec { + uint64_t base; + uint64_t len; +}; + +struct fuse_ioctl_out { + int32_t result; + uint32_t flags; + uint32_t in_iovs; + uint32_t out_iovs; +}; + +struct fuse_poll_in { + uint64_t fh; + uint64_t kh; + uint32_t flags; + uint32_t events; +}; + +struct fuse_poll_out { + uint32_t revents; + uint32_t padding; +}; + +struct fuse_notify_poll_wakeup_out { + uint64_t kh; +}; + +struct fuse_fallocate_in { + uint64_t fh; + uint64_t offset; + uint64_t length; + uint32_t mode; + uint32_t padding; +}; + +/** + * FUSE request unique ID flag + * + * Indicates whether this is a resend request. The receiver should handle this + * request accordingly. + */ +#define FUSE_UNIQUE_RESEND (1ULL << 63) + +struct fuse_in_header { + uint32_t len; + uint32_t opcode; + uint64_t unique; + uint64_t nodeid; + uint32_t uid; + uint32_t gid; + uint32_t pid; + uint16_t total_extlen; /* length of extensions in 8byte units */ + uint16_t padding; +}; + +struct fuse_out_header { + uint32_t len; + int32_t error; + uint64_t unique; +}; + +struct fuse_dirent { + uint64_t ino; + uint64_t off; + uint32_t namelen; + uint32_t type; + char name[]; +}; + +/* Align variable length records to 64bit boundary */ +#define FUSE_REC_ALIGN(x) \ + (((x) + sizeof(uint64_t) - 1) & ~(sizeof(uint64_t) - 1)) + +#define FUSE_NAME_OFFSET offsetof(struct fuse_dirent, name) +#define FUSE_DIRENT_ALIGN(x) FUSE_REC_ALIGN(x) +#define FUSE_DIRENT_SIZE(d) \ + FUSE_DIRENT_ALIGN(FUSE_NAME_OFFSET + (d)->namelen) + +struct fuse_direntplus { + struct fuse_entry_out entry_out; + struct fuse_dirent dirent; +}; + +#define FUSE_NAME_OFFSET_DIRENTPLUS \ + offsetof(struct fuse_direntplus, dirent.name) +#define FUSE_DIRENTPLUS_SIZE(d) \ + FUSE_DIRENT_ALIGN(FUSE_NAME_OFFSET_DIRENTPLUS + (d)->dirent.namelen) + +struct fuse_notify_inval_inode_out { + uint64_t ino; + int64_t off; + int64_t len; +}; + +struct fuse_notify_inval_entry_out { + uint64_t parent; + uint32_t namelen; + uint32_t flags; +}; + +struct fuse_notify_delete_out { + uint64_t parent; + uint64_t child; + uint32_t namelen; + uint32_t padding; +}; + +struct fuse_notify_store_out { + uint64_t nodeid; + uint64_t offset; + uint32_t size; + uint32_t padding; +}; + +struct fuse_notify_retrieve_out { + uint64_t notify_unique; + uint64_t nodeid; + uint64_t offset; + uint32_t size; + uint32_t padding; +}; + +/* Matches the size of fuse_write_in */ +struct fuse_notify_retrieve_in { + uint64_t dummy1; + uint64_t offset; + uint32_t size; + uint32_t dummy2; + uint64_t dummy3; + uint64_t dummy4; +}; + +struct fuse_backing_map { + int32_t fd; + uint32_t flags; + uint64_t padding; +}; + +/* Device ioctls: */ +#define FUSE_DEV_IOC_MAGIC 229 +#define FUSE_DEV_IOC_CLONE _IOR(FUSE_DEV_IOC_MAGIC, 0, uint32_t) +#define FUSE_DEV_IOC_BACKING_OPEN _IOW(FUSE_DEV_IOC_MAGIC, 1, \ + struct fuse_backing_map) +#define FUSE_DEV_IOC_BACKING_CLOSE _IOW(FUSE_DEV_IOC_MAGIC, 2, uint32_t) + +struct fuse_lseek_in { + uint64_t fh; + uint64_t offset; + uint32_t whence; + uint32_t padding; +}; + +struct fuse_lseek_out { + uint64_t offset; +}; + +struct fuse_copy_file_range_in { + uint64_t fh_in; + uint64_t off_in; + uint64_t nodeid_out; + uint64_t fh_out; + uint64_t off_out; + uint64_t len; + uint64_t flags; +}; + +#define FUSE_SETUPMAPPING_FLAG_WRITE (1ull << 0) +#define FUSE_SETUPMAPPING_FLAG_READ (1ull << 1) +struct fuse_setupmapping_in { + /* An already open handle */ + uint64_t fh; + /* Offset into the file to start the mapping */ + uint64_t foffset; + /* Length of mapping required */ + uint64_t len; + /* Flags, FUSE_SETUPMAPPING_FLAG_* */ + uint64_t flags; + /* Offset in Memory Window */ + uint64_t moffset; +}; + +struct fuse_removemapping_in { + /* number of fuse_removemapping_one follows */ + uint32_t count; +}; + +struct fuse_removemapping_one { + /* Offset into the dax window start the unmapping */ + uint64_t moffset; + /* Length of mapping required */ + uint64_t len; +}; + +#define FUSE_REMOVEMAPPING_MAX_ENTRY \ + (PAGE_SIZE / sizeof(struct fuse_removemapping_one)) + +struct fuse_syncfs_in { + uint64_t padding; +}; + +/* + * For each security context, send fuse_secctx with size of security context + * fuse_secctx will be followed by security context name and this in turn + * will be followed by actual context label. + * fuse_secctx, name, context + */ +struct fuse_secctx { + uint32_t size; + uint32_t padding; +}; + +/* + * Contains the information about how many fuse_secctx structures are being + * sent and what's the total size of all security contexts (including + * size of fuse_secctx_header). + * + */ +struct fuse_secctx_header { + uint32_t size; + uint32_t nr_secctx; +}; + +/** + * struct fuse_ext_header - extension header + * @size: total size of this extension including this header + * @type: type of extension + * + * This is made compatible with fuse_secctx_header by using type values > + * FUSE_MAX_NR_SECCTX + */ +struct fuse_ext_header { + uint32_t size; + uint32_t type; +}; + +/** + * struct fuse_supp_groups - Supplementary group extension + * @nr_groups: number of supplementary groups + * @groups: flexible array of group IDs + */ +struct fuse_supp_groups { + uint32_t nr_groups; + uint32_t groups[]; +}; + +#endif /* _LINUX_FUSE_H */ From patchwork Fri Aug 30 19:52:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 1979203 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=QlZxOPaV; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.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 ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WwTNk36XYz1yfn for ; Sat, 31 Aug 2024 05:53:42 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4867D3864837 for ; Fri, 30 Aug 2024 19:53:40 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTP id 1B81D386486F for ; Fri, 30 Aug 2024 19:52:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1B81D386486F Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 1B81D386486F Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725047540; cv=none; b=Rri4DppBsQ/AnzRcQkmd7igifWiwloahvMxanBnbkn178XeXFNakSgcRMwyEQwF8JRq38alZpNqVKfe86uj1awLtax0n2IXUZiHX2Ht/wu0njekX6RD9ANNjU9dhHkIHG/ysxGLkGYZtM5/B34JhgmqHxz7oMyvA0AM1MmrfX8s= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725047540; c=relaxed/simple; bh=qHtYvaLFNa5slA4FljEd6ahtFoTG5uD/zTbE+5CNIwM=; h=DKIM-Signature:From:To:Subject:Message-ID:Date:MIME-Version; b=LG6Zwt6Gc4E684H6gV+rMeHsmGI0oAj66FXxkc7g3a7axcDffkcAfzc0vdJktnDGwwbsV4xCdEi79ZGAam8fNxZVFOzr5Il2HfsOudjTQIOT+32vQDaFeHBiU1RzX95HjVc6R7QXEeedWYoZLj+UpPDFM68PtfKRJXdRsAQnBxQ= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1725047536; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=1/ZTmb5XMjOSQEhHYf5zKfq1DCB2Oi6sG1Ja4u+tTQI=; b=QlZxOPaVNAbhdnGkyJ86ZmMvk3XYOXdPQ73JAJnFyOPNG6o2z3YYdhgFakFFSJUrZchb7H DkQCLANyzuPD6wHffRcmR8Nc3rleT4Dt28UFQMHNoa3IOsAILU6vXSv01QJCtH/N10ucqr qNScUPUGpWcDE/sqttqEiirWeAAfiZM= Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-611-IirAydaaOlGCpur3_4QbFA-1; Fri, 30 Aug 2024 15:52:15 -0400 X-MC-Unique: IirAydaaOlGCpur3_4QbFA-1 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 749641955BF2 for ; Fri, 30 Aug 2024 19:52:14 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.2.16.7]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 46D5E1955F44 for ; Fri, 30 Aug 2024 19:52:13 +0000 (UTC) From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH v4 02/13] support: Add In-Reply-To: Message-ID: <873f5e4f2eecf46cda17c9b2d01a19cd5eec7386.1725047142.git.fweimer@redhat.com> References: X-From-Line: 873f5e4f2eecf46cda17c9b2d01a19cd5eec7386 Mon Sep 17 00:00:00 2001 Date: Fri, 30 Aug 2024 21:52:10 +0200 User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-10.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org Use static functions for readdir/readdir_r, so that -D_FILE_OFFSET_BITS=64 does not improperly redirect calls to the wrong implementation. --- support/Makefile | 6 +++ support/support_readdir_check.c | 29 +++++++++++++ support/support_readdir_r_check.c | 29 +++++++++++++ support/tst-xdirent.c | 70 +++++++++++++++++++++++++++++++ support/xclosedir.c | 28 +++++++++++++ support/xdirent.h | 53 +++++++++++++++++++++++ support/xfdopendir.c | 30 +++++++++++++ support/xopendir.c | 30 +++++++++++++ 8 files changed, 275 insertions(+) create mode 100644 support/support_readdir_check.c create mode 100644 support/support_readdir_r_check.c create mode 100644 support/tst-xdirent.c create mode 100644 support/xclosedir.c create mode 100644 support/xdirent.h create mode 100644 support/xfdopendir.c create mode 100644 support/xopendir.c diff --git a/support/Makefile b/support/Makefile index 6e3c55394f..ce194bfdd4 100644 --- a/support/Makefile +++ b/support/Makefile @@ -73,6 +73,8 @@ libsupport-routines = \ support_quote_blob \ support_quote_blob_wide \ support_quote_string \ + support_readdir_check \ + support_readdir_r_check \ support_record_failure \ support_run_diff \ support_select_modifies_timeout \ @@ -115,6 +117,7 @@ libsupport-routines = \ xclock_settime_time64 \ xclone \ xclose \ + xclosedir \ xconnect \ xcopy_file_range \ xdlfcn \ @@ -122,6 +125,7 @@ libsupport-routines = \ xdup2 \ xfchmod \ xfclose \ + xfdopendir \ xfgets \ xfopen \ xfork \ @@ -143,6 +147,7 @@ libsupport-routines = \ xmunmap \ xnewlocale \ xopen \ + xopendir \ xpipe \ xpoll \ xposix_memalign \ @@ -327,6 +332,7 @@ tests = \ tst-test_compare_string \ tst-test_compare_string_wide \ tst-timespec \ + tst-xdirent \ tst-xreadlink \ tst-xsigstack \ # tests diff --git a/support/support_readdir_check.c b/support/support_readdir_check.c new file mode 100644 index 0000000000..8d48a4c5a4 --- /dev/null +++ b/support/support_readdir_check.c @@ -0,0 +1,29 @@ +/* Error-checking helper for xreaddir, xreaddir64. + Copyright (C) 2024 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include + +#include + +void * +support_readdir_check (const char *name, void *result) +{ + if (result == NULL && errno != 0) + FAIL_EXIT1 ("%s: %m", name); + return result; +} diff --git a/support/support_readdir_r_check.c b/support/support_readdir_r_check.c new file mode 100644 index 0000000000..8675b08ff3 --- /dev/null +++ b/support/support_readdir_r_check.c @@ -0,0 +1,29 @@ +/* Error-checking helper for xreaddir_r, xreaddir64_r. + Copyright (C) 2024 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include + +#include + +int +support_readdir_r_check (const char *name, int result) +{ + if (result != 0) + FAIL_EXIT1 ("%s: %m", name); + return result; +} diff --git a/support/tst-xdirent.c b/support/tst-xdirent.c new file mode 100644 index 0000000000..7012b3185b --- /dev/null +++ b/support/tst-xdirent.c @@ -0,0 +1,70 @@ +/* Compile test for error-checking wrappers for + Copyright (C) 2024 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include + +#include +#include +#include + +static int +do_test (void) +{ + { + DIR *d = xopendir ("."); + struct dirent *e = xreaddir (d); + /* Assume that the "." special entry always comes first. */ + TEST_COMPARE_STRING (e->d_name, "."); + xclosedir (d); + } + + { + DIR *d = xopendir ("."); + struct dirent64 *e = xreaddir64 (d); + TEST_COMPARE_STRING (e->d_name, "."); + xclosedir (d); + } + + /* The functions readdir_r, readdir64_r were deprecated in glibc 2.24. */ + DIAG_PUSH_NEEDS_COMMENT; + DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Wdeprecated-declarations"); + + { + DIR *d = xopendir ("."); + struct dirent buf; + struct dirent *e; + TEST_COMPARE (xreaddir_r (d, &buf, &e), 0); + TEST_COMPARE_STRING (e->d_name, "."); + xclosedir (d); + } + + { + DIR *d = xopendir ("."); + struct dirent64 buf; + struct dirent64 *e; + TEST_COMPARE (xreaddir64_r (d, &buf, &e), 0); + TEST_COMPARE_STRING (e->d_name, "."); + xclosedir (d); + } + + DIAG_POP_NEEDS_COMMENT; + + return 0; +} + +#include diff --git a/support/xclosedir.c b/support/xclosedir.c new file mode 100644 index 0000000000..b490df5598 --- /dev/null +++ b/support/xclosedir.c @@ -0,0 +1,28 @@ +/* Error-checking wrapper for closedir. + Copyright (C) 2024 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include + +#include + +void +xclosedir (DIR *dir) +{ + if (closedir (dir) != 0) + FAIL_EXIT1 ("closedir: %m"); +} diff --git a/support/xdirent.h b/support/xdirent.h new file mode 100644 index 0000000000..83f41dba6b --- /dev/null +++ b/support/xdirent.h @@ -0,0 +1,53 @@ +/* Error-checking wrappers for + Copyright (C) 2024 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#ifndef SUPPORT_XDIRENT_H +#define SUPPORT_XDIRENT_H + +#include +#include +#include + +__BEGIN_DECLS + +DIR *xopendir (const char *path); +DIR *xfdopendir (int fd); +void xclosedir (DIR *); + +void *support_readdir_check (const char *, void *); +#define xreaddir(d) \ + ((struct dirent *) support_readdir_check ("readdir", \ + (errno = 0, readdir ((d))))) +#define xreaddir64(d) \ + ((struct dirent64 *) support_readdir_check ("readdir64", \ + (errno = 0, readdir64 ((d))))) + +int support_readdir_r_check (const char *, int); + +/* The functions readdir_r, readdir64_r were deprecated in glibc 2.24. */ +DIAG_PUSH_NEEDS_COMMENT; +DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Wdeprecated-declarations"); +#define xreaddir_r(d, e, r) \ + (support_readdir_r_check ("readdir_r", readdir_r ((d), (e), (r)))) +#define xreaddir64_r(d, e, r) \ + (support_readdir_r_check ("readdir64_r", readdir64_r ((d), (e), (r)))) +DIAG_POP_NEEDS_COMMENT; + +__END_DECLS + +#endif /* SUPPORT_XDIRENT_H */ diff --git a/support/xfdopendir.c b/support/xfdopendir.c new file mode 100644 index 0000000000..d881d28c73 --- /dev/null +++ b/support/xfdopendir.c @@ -0,0 +1,30 @@ +/* Error-checking wrapper for fdopendir. + Copyright (C) 2024 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include + +#include + +DIR * +xfdopendir (int fd) +{ + DIR *result = fdopendir (fd); + if (result == NULL) + FAIL_EXIT1 ("fdopendir (%d): %m", fd); + return result; +} diff --git a/support/xopendir.c b/support/xopendir.c new file mode 100644 index 0000000000..e4aee07fee --- /dev/null +++ b/support/xopendir.c @@ -0,0 +1,30 @@ +/* Error-checking wrapper for opendir. + Copyright (C) 2024 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include + +#include + +DIR * +xopendir (const char *path) +{ + DIR *result = opendir (path); + if (result == NULL) + FAIL_EXIT1 ("opendir (\"%s\"): %m", path); + return result; +} From patchwork Fri Aug 30 19:52:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 1979201 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=cNtT8ywC; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.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 ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WwTMw0rGVz1yfn for ; Sat, 31 Aug 2024 05:53:00 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E083A386480F for ; Fri, 30 Aug 2024 19:52:57 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTP id 062E4384CBA1 for ; Fri, 30 Aug 2024 19:52:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 062E4384CBA1 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 062E4384CBA1 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725047556; cv=none; b=f57abfzyckzKbwyzlKpnbEsfvv9maOXqHPN64IOYXw30lH3+NmWKNDD3zk4I+oQ8uRNfh/V4gzWXJ4bf4CaOim2wwCiOzQwKVZ1g8PXkwh9fcOHh7OZYQXc7Vtf6UGpe4YQpwhL7cVr+KYlejfj3jHgc5QXvMrTcdNqtX1DeVZo= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725047556; c=relaxed/simple; bh=OZcPbatKf0idjPtejiXGExoFwLhqaFaPx0CWzjYXplE=; h=DKIM-Signature:From:To:Subject:Message-ID:Date:MIME-Version; b=H0dMwcmNDJhwlJUI5Jkk73PNldInVYJwSVuugxO7jpxNd5xgTotrDAdAvKdXsQdCF8440J+L09ynbwY5KTxsAv3Mpb3vTwKh6ZAh7GfPHUXCkCPlNlPEaubXrwgErNjxSjqAhrPgDS5Jj4xA/b9y0j/n8e+3KzHwrholahLI2hk= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1725047551; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=vthv+HCL4bD7TGT1sY9l/ikNlU7TSxDKugSOAtv9pAM=; b=cNtT8ywCj0oeq6rsCy71wiNGt8vd1jUAjYHDinYW/9F1ehrKQ6TazOLMjeVTR5Sx7laTda fxx1j/g+efumlQqjjroC7RvkmibX/jmkAoch+NqiA8ffhcdM66zcd6WdcXy73m1muLzPo5 qiZSKf6J+JrPjpa3XbCs/mLRQhYWdD4= Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-193-8kOWRfOYPBOHdY7IMS8vNA-1; Fri, 30 Aug 2024 15:52:29 -0400 X-MC-Unique: 8kOWRfOYPBOHdY7IMS8vNA-1 Received: from mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.40]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 8A3AD1954B04 for ; Fri, 30 Aug 2024 19:52:27 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.2.16.7]) by mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 591E819560A3 for ; Fri, 30 Aug 2024 19:52:26 +0000 (UTC) From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH v4 03/13] Linux: readdir_r needs to report getdents failures (bug 32124) In-Reply-To: Message-ID: <69eb384e8ded80045d1a559186b8c0be1e354b0a.1725047142.git.fweimer@redhat.com> References: X-From-Line: 69eb384e8ded80045d1a559186b8c0be1e354b0a Mon Sep 17 00:00:00 2001 Date: Fri, 30 Aug 2024 21:52:23 +0200 User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.40 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-10.8 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_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org Upon error, return the errno value set by the __getdents call in __readdir_unlocked. Previously, kernel-reported errors were ignored. --- support/support_readdir_check.c | 3 +- support/support_readdir_r_check.c | 10 ++++- support/tst-xdirent.c | 22 +++++++---- support/xdirent.h | 57 +++++++++++++++++++++++------ sysdeps/unix/sysv/linux/readdir_r.c | 11 +++++- 5 files changed, 79 insertions(+), 24 deletions(-) diff --git a/support/support_readdir_check.c b/support/support_readdir_check.c index 8d48a4c5a4..5687004276 100644 --- a/support/support_readdir_check.c +++ b/support/support_readdir_check.c @@ -21,9 +21,10 @@ #include void * -support_readdir_check (const char *name, void *result) +support_readdir_check (const char *name, void *result, int saved_errno) { if (result == NULL && errno != 0) FAIL_EXIT1 ("%s: %m", name); + errno = saved_errno; return result; } diff --git a/support/support_readdir_r_check.c b/support/support_readdir_r_check.c index 8675b08ff3..6bbb0d0b32 100644 --- a/support/support_readdir_r_check.c +++ b/support/support_readdir_r_check.c @@ -21,9 +21,15 @@ #include int -support_readdir_r_check (const char *name, int result) +support_readdir_r_check (const char *name, int result, void *buf, void *ptr) { if (result != 0) - FAIL_EXIT1 ("%s: %m", name); + { + errno = result; + FAIL_EXIT1 ("%s: %m", name); + } + if (buf != ptr) + FAIL_EXIT1 ("%s: buffer pointer and returned pointer differ: %p != %p", + name, buf, ptr); return result; } diff --git a/support/tst-xdirent.c b/support/tst-xdirent.c index 7012b3185b..642483165a 100644 --- a/support/tst-xdirent.c +++ b/support/tst-xdirent.c @@ -30,6 +30,8 @@ do_test (void) struct dirent *e = xreaddir (d); /* Assume that the "." special entry always comes first. */ TEST_COMPARE_STRING (e->d_name, "."); + while (xreaddir (d) != NULL) + ; xclosedir (d); } @@ -37,6 +39,8 @@ do_test (void) DIR *d = xopendir ("."); struct dirent64 *e = xreaddir64 (d); TEST_COMPARE_STRING (e->d_name, "."); + while (xreaddir64 (d) != NULL) + ; xclosedir (d); } @@ -46,19 +50,21 @@ do_test (void) { DIR *d = xopendir ("."); - struct dirent buf; - struct dirent *e; - TEST_COMPARE (xreaddir_r (d, &buf, &e), 0); - TEST_COMPARE_STRING (e->d_name, "."); + struct dirent buf = { 0, }; + TEST_VERIFY (xreaddir_r (d, &buf)); + TEST_COMPARE_STRING (buf.d_name, "."); + while (xreaddir_r (d, &buf)) + ; xclosedir (d); } { DIR *d = xopendir ("."); - struct dirent64 buf; - struct dirent64 *e; - TEST_COMPARE (xreaddir64_r (d, &buf, &e), 0); - TEST_COMPARE_STRING (e->d_name, "."); + struct dirent64 buf = { 0, }; + TEST_VERIFY (xreaddir64_r (d, &buf)); + TEST_COMPARE_STRING (buf.d_name, "."); + while (xreaddir64_r (d, &buf)) + ; xclosedir (d); } diff --git a/support/xdirent.h b/support/xdirent.h index 83f41dba6b..8465d70ec1 100644 --- a/support/xdirent.h +++ b/support/xdirent.h @@ -22,6 +22,8 @@ #include #include #include +#include +#include __BEGIN_DECLS @@ -29,23 +31,54 @@ DIR *xopendir (const char *path); DIR *xfdopendir (int fd); void xclosedir (DIR *); -void *support_readdir_check (const char *, void *); -#define xreaddir(d) \ - ((struct dirent *) support_readdir_check ("readdir", \ - (errno = 0, readdir ((d))))) -#define xreaddir64(d) \ - ((struct dirent64 *) support_readdir_check ("readdir64", \ - (errno = 0, readdir64 ((d))))) +void *support_readdir_check (const char *, void *, int); -int support_readdir_r_check (const char *, int); +static __attribute__ ((unused)) struct dirent * +xreaddir (DIR *stream) +{ + int saved_errno = errno; + errno = 0; + struct dirent *result = readdir (stream); + return support_readdir_check ("readdir", result, saved_errno); +} + +static __attribute__ ((unused)) struct dirent64 * +xreaddir64 (DIR *stream) +{ + int saved_errno = errno; + errno = 0; + struct dirent64 *result = readdir64 (stream); + return support_readdir_check ("readdir64", result, saved_errno); +} /* The functions readdir_r, readdir64_r were deprecated in glibc 2.24. */ DIAG_PUSH_NEEDS_COMMENT; DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Wdeprecated-declarations"); -#define xreaddir_r(d, e, r) \ - (support_readdir_r_check ("readdir_r", readdir_r ((d), (e), (r)))) -#define xreaddir64_r(d, e, r) \ - (support_readdir_r_check ("readdir64_r", readdir64_r ((d), (e), (r)))) + +int support_readdir_r_check (const char *, int, void *, void *); + +static __attribute__ ((unused)) bool +xreaddir_r (DIR *stream, struct dirent *buf) +{ + struct dirent *ptr; + int ret = readdir_r (stream, buf, &ptr); + if (ret == 0 && ptr == NULL) + return false; + support_readdir_r_check ("readdir_r", ret, buf, ptr); + return true; +} + +static __attribute__ ((unused)) bool +xreaddir64_r (DIR *stream, struct dirent64 *buf) +{ + struct dirent64 *ptr; + int ret = readdir64_r (stream, buf, &ptr); + if (ret == 0 && ptr == NULL) + return false; + support_readdir_r_check ("readdir64_r", ret, buf, ptr); + return true; +} + DIAG_POP_NEEDS_COMMENT; __END_DECLS diff --git a/sysdeps/unix/sysv/linux/readdir_r.c b/sysdeps/unix/sysv/linux/readdir_r.c index ffd5262cf5..1d595688f7 100644 --- a/sysdeps/unix/sysv/linux/readdir_r.c +++ b/sysdeps/unix/sysv/linux/readdir_r.c @@ -25,14 +25,22 @@ __readdir_r (DIR *dirp, struct dirent *entry, struct dirent **result) { struct dirent *dp; size_t reclen; + int saved_errno = errno; __libc_lock_lock (dirp->lock); while (1) { + /* If errno is changed from 0, the NULL return value indicates + an actual error. It overrides a pending ENAMETOOLONG error. */ + __set_errno (0); dp = __readdir_unlocked (dirp); if (dp == NULL) - break; + { + if (errno != 0) + dirp->errcode = errno; + break; + } reclen = dp->d_reclen; if (reclen <= offsetof (struct dirent, d_name) + NAME_MAX + 1) @@ -61,6 +69,7 @@ __readdir_r (DIR *dirp, struct dirent *entry, struct dirent **result) __libc_lock_unlock (dirp->lock); + __set_errno (saved_errno); return dp != NULL ? 0 : dirp->errcode; } From patchwork Fri Aug 30 19:52:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 1979202 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=SLppZNsW; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WwTNH2lzWz1yfn for ; Sat, 31 Aug 2024 05:53:19 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8D6FC38650D0 for ; Fri, 30 Aug 2024 19:53:17 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTP id D1D81385020E for ; Fri, 30 Aug 2024 19:52:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D1D81385020E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org D1D81385020E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725047573; cv=none; b=jyiEwImttrb8kLUgdqLoEIENUQFVPeZWLEyeuZUsTToF8Au0SBHhCY5SNdDlIq1bMoTkqEplkzEMwK92nQ62ZLa9FkrBzx6vL1aVCy0os/ZFaE5YLPGb/Lup//CK63ux4z043akZSD7g1PNgkbicGEtDIqW/JR1fW3ZYYLwCOeM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725047573; c=relaxed/simple; bh=L3U6e+MvPppbGeTmFLoWS6JJWFQIpmSUgrX0e0Ykg+8=; h=DKIM-Signature:From:To:Subject:Message-ID:Date:MIME-Version; b=qv6tnXPVVPtIBOpZKG2nj0nsMZIF5uoTzTuVn+od5k4IeVFZ48su7m/US2rtzLj1dMFosBiocrkWW0rwri7gTe5O/EmpuGdohZr2zssH4JcOZR6iY5JBi1yAYWBxvoafim7+OQtZlyFKMgjIFiwIS7NDpQ9k+n4ILBsD7g4rNjg= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1725047570; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=M3v9vDqzoV8appCEo0RdgUIqn7I2GecXagNcV7JQ3a8=; b=SLppZNsWrVO6ooc5iEPylwg78rlMfTYlJEZjYIb6v9P04YjPu/GrRRgXUrv4+aScroQty0 dEDUggn++PgvczaiCI6Z0z3pItlrGZCJI+jsSjKE7BhC0KDjAXBbbVKIX+TIBfmJPHtbH/ CeMsbzpCB18v8S/hHsgH+mJ6umLbLmw= Received: from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-61-i_aQkWxyOAC-VijNJRrYUg-1; Fri, 30 Aug 2024 15:52:48 -0400 X-MC-Unique: i_aQkWxyOAC-VijNJRrYUg-1 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id C8C341955F03 for ; Fri, 30 Aug 2024 19:52:46 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.2.16.7]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 4C78919560AE for ; Fri, 30 Aug 2024 19:52:45 +0000 (UTC) From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH v4 04/13] support: Add In-Reply-To: Message-ID: References: X-From-Line: a773d6cc6ba0a6ff2495d25e6814564eaca1fa9c Mon Sep 17 00:00:00 2001 Date: Fri, 30 Aug 2024 21:52:42 +0200 User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-10.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org It allows to read directories using the six readdir variants without writing type-specific code or using skeleton files that are compiled four times. The readdir_r subtest for support_readdir_expect_error revealed bug 32124. --- support/Makefile | 2 + support/readdir.h | 79 ++++++++++ support/support_readdir.c | 287 ++++++++++++++++++++++++++++++++++ support/tst-support_readdir.c | 71 +++++++++ 4 files changed, 439 insertions(+) create mode 100644 support/readdir.h create mode 100644 support/support_readdir.c create mode 100644 support/tst-support_readdir.c diff --git a/support/Makefile b/support/Makefile index ce194bfdd4..ec9793ab1e 100644 --- a/support/Makefile +++ b/support/Makefile @@ -73,6 +73,7 @@ libsupport-routines = \ support_quote_blob \ support_quote_blob_wide \ support_quote_string \ + support_readdir \ support_readdir_check \ support_readdir_r_check \ support_record_failure \ @@ -326,6 +327,7 @@ tests = \ tst-support_quote_blob \ tst-support_quote_blob_wide \ tst-support_quote_string \ + tst-support_readdir \ tst-support_record_failure \ tst-test_compare \ tst-test_compare_blob \ diff --git a/support/readdir.h b/support/readdir.h new file mode 100644 index 0000000000..7561f5da02 --- /dev/null +++ b/support/readdir.h @@ -0,0 +1,79 @@ +/* Type-generic wrapper for readdir functions. + Copyright (C) 2024 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#ifndef SUPPORT_READDIR_H +#define SUPPORT_READDIR_H + +#include +#include +#include + +__BEGIN_DECLS + +/* Definition independent of _FILE_OFFSET_BITS. */ +struct support_dirent +{ + uint64_t d_ino; + uint64_t d_off; /* 0 if d_off is not supported. */ + uint32_t d_type; + char *d_name; +}; + +/* Operation to be performed by support_readdir below. */ +enum support_readdir_op + { + SUPPORT_READDIR, + SUPPORT_READDIR64, + SUPPORT_READDIR_R, + SUPPORT_READDIR64_R, + SUPPORT_READDIR64_COMPAT, + SUPPORT_READDIR64_R_COMPAT, + }; + +/* Returns the last supported function. May exclude + SUPPORT_READDIR64_R_COMPAT if not implemented. */ +enum support_readdir_op support_readdir_op_last (void); + +/* Returns the name of the function that corresponds to the OP constant. */ +const char *support_readdir_function (enum support_readdir_op op); + +/* Returns the inode field width for OP. */ +unsigned int support_readdir_inode_width (enum support_readdir_op op); + +/* Returns true if OP is an _r variant with name length restrictions. */ +bool support_readdir_r_variant (enum support_readdir_op op); + +/* First, free E->d_name and set the field to NULL. Then call the + readdir variant as specified by OP. If successfully, copy fields + to E, make a copy of the entry name using strdup, and write its + addres sto E->d_name. + + Return true if an entry was read, or false if the end of the + directory stream was reached. Terminates the process upon error. + The caller is expected to free E->d_name if the function is not + called again for this E. */ +bool support_readdir (DIR *stream, enum support_readdir_op op, + struct support_dirent *e); + +/* Checks t hat the readdir operation OP fails with errno value EXPECTED. */ +void support_readdir_expect_error (DIR *stream, enum support_readdir_op op, + int expected); + +__END_DECLS + +#endif /* SUPPORT_READDIR_H */ diff --git a/support/support_readdir.c b/support/support_readdir.c new file mode 100644 index 0000000000..78ccf4a48d --- /dev/null +++ b/support/support_readdir.c @@ -0,0 +1,287 @@ +/* Type-generic wrapper for readdir functions. + Copyright (C) 2024 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include + +#include +#include +#include +#include +#include +#include + +/* Copied from . */ +struct __old_dirent64 + { + __ino_t d_ino; + __off64_t d_off; + unsigned short int d_reclen; + unsigned char d_type; + char d_name[256]; + }; + +static struct __old_dirent64 *(*readdir64_compat) (DIR *); +static int (*readdir64_r_compat) (DIR *, struct __old_dirent64 *, + struct __old_dirent64 **); + +static void __attribute__ ((constructor)) +init (void) +{ + /* These compat symbols exists on alpha, i386, m67k , powerpc, s390, + sparc. at the same GLIBC_2.1 version. */ + readdir64_compat = dlvsym (RTLD_DEFAULT, "readdir64", "GLIBC_2.1"); + readdir64_r_compat = dlvsym (RTLD_DEFAULT, "readdir64_r", "GLIBC_2.1"); +} + +enum support_readdir_op +support_readdir_op_last (void) +{ + if (readdir64_r_compat != NULL) + { + TEST_VERIFY (readdir64_compat != NULL); + return SUPPORT_READDIR64_R_COMPAT; + } + else + return SUPPORT_READDIR64_R; +} + +const char * +support_readdir_function (enum support_readdir_op op) +{ + switch (op) + { + case SUPPORT_READDIR: + return "readdir"; + case SUPPORT_READDIR64: + return "readdir64"; + case SUPPORT_READDIR_R: + return "readdir_r"; + case SUPPORT_READDIR64_R: + return "readdir64_r"; + case SUPPORT_READDIR64_COMPAT: + return "readdir64@GBLIC_2.1"; + case SUPPORT_READDIR64_R_COMPAT: + return "readdir64_r@GBLIC_2.1"; + } + FAIL_EXIT1 ("invalid support_readdir_op constant: %d", op); +} + +unsigned int +support_readdir_inode_width (enum support_readdir_op op) +{ + switch (op) + { + case SUPPORT_READDIR: + case SUPPORT_READDIR_R: + return sizeof ((struct dirent) { 0, }.d_ino) * 8; + case SUPPORT_READDIR64: + case SUPPORT_READDIR64_R: + return sizeof ((struct dirent64) { 0, }.d_ino) * 8; + case SUPPORT_READDIR64_COMPAT: + case SUPPORT_READDIR64_R_COMPAT: + return sizeof ((struct __old_dirent64) { 0, }.d_ino) * 8; + } + FAIL_EXIT1 ("invalid support_readdir_op constant: %d", op); +} + +bool +support_readdir_r_variant (enum support_readdir_op op) +{ + switch (op) + { + case SUPPORT_READDIR: + case SUPPORT_READDIR64: + case SUPPORT_READDIR64_COMPAT: + return false; + case SUPPORT_READDIR_R: + case SUPPORT_READDIR64_R: + case SUPPORT_READDIR64_R_COMPAT: + return true; + } + FAIL_EXIT1 ("invalid support_readdir_op constant: %d", op); +} + +static bool +copy_dirent (struct support_dirent *dst, struct dirent *src) +{ + if (src == NULL) + return false; + dst->d_ino = src->d_ino; +#ifdef _DIRENT_HAVE_D_OFF + dst->d_off = src->d_off; +#else + dst->d_off = 0; +#endif + dst->d_type = src->d_type; + dst->d_name = xstrdup (src->d_name); + return true; +} + +static bool +copy_dirent64 (struct support_dirent *dst, struct dirent64 *src) +{ + if (src == NULL) + return false; + dst->d_ino = src->d_ino; +#ifdef _DIRENT_HAVE_D_OFF + dst->d_off = src->d_off; +#else + dst->d_off = 0; +#endif + dst->d_type = src->d_type; + dst->d_name = xstrdup (src->d_name); + return true; +} + +static bool +copy_old_dirent64 (struct support_dirent *dst, struct __old_dirent64 *src) +{ + if (src == NULL) + return false; + dst->d_ino = src->d_ino; +#ifdef _DIRENT_HAVE_D_OFF + dst->d_off = src->d_off; +#else + dst->d_off = 0; +#endif + dst->d_type = src->d_type; + dst->d_name = xstrdup (src->d_name); + return true; +} + +bool +support_readdir (DIR *stream, enum support_readdir_op op, + struct support_dirent *e) +{ + free (e->d_name); + e->d_name = NULL; + switch (op) + { + case SUPPORT_READDIR: + return copy_dirent (e, xreaddir (stream)); + case SUPPORT_READDIR64: + return copy_dirent64 (e, xreaddir64 (stream)); + + /* The functions readdir_r, readdir64_r were deprecated in glibc 2.24. */ + DIAG_PUSH_NEEDS_COMMENT; + DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Wdeprecated-declarations"); + + case SUPPORT_READDIR_R: + { + struct dirent buf; + if (!xreaddir_r (stream, &buf)) + return false; + return copy_dirent (e, &buf); + } + case SUPPORT_READDIR64_R: + { + struct dirent64 buf; + if (!xreaddir64_r (stream, &buf)) + return false; + return copy_dirent64 (e, &buf); + } + + DIAG_POP_NEEDS_COMMENT; + + case SUPPORT_READDIR64_COMPAT: + if (readdir64_compat == NULL) + FAIL_EXIT1 ("readdir64 compat function not implemented"); + return copy_old_dirent64 (e, readdir64_compat (stream)); + + case SUPPORT_READDIR64_R_COMPAT: + { + if (readdir64_r_compat == NULL) + FAIL_EXIT1 ("readdir64_r compat function not implemented"); + struct __old_dirent64 buf; + struct __old_dirent64 *e1; + int ret = readdir64_r_compat (stream, &buf, &e1); + if (ret != 0) + { + errno = ret; + FAIL ("readdir64_r@GLIBC_2.1: %m"); + return false; + } + if (e1 == NULL) + return false; + return copy_old_dirent64 (e, e1); + } + } + FAIL_EXIT1 ("support_readdir: invalid op argument %d", (int) op); +} + +void +support_readdir_expect_error (DIR *stream, enum support_readdir_op op, + int expected) +{ + switch (op) + { + case SUPPORT_READDIR: + errno = 0; + TEST_VERIFY (readdir (stream) == NULL); + TEST_COMPARE (errno, expected); + return; + case SUPPORT_READDIR64: + errno = 0; + TEST_VERIFY (readdir64 (stream) == NULL); + TEST_COMPARE (errno, expected); + return; + + /* The functions readdir_r, readdir64_r were deprecated in glibc 2.24. */ + DIAG_PUSH_NEEDS_COMMENT; + DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Wdeprecated-declarations"); + + case SUPPORT_READDIR_R: + { + struct dirent buf; + struct dirent *e; + errno = readdir_r (stream, &buf, &e); + TEST_COMPARE (errno, expected);; + } + return; + case SUPPORT_READDIR64_R: + { + struct dirent64 buf; + struct dirent64 *e; + errno = readdir64_r (stream, &buf, &e); + TEST_COMPARE (errno, expected);; + } + return; + + DIAG_POP_NEEDS_COMMENT; + + case SUPPORT_READDIR64_COMPAT: + if (readdir64_compat == NULL) + FAIL_EXIT1 ("readdir64_r compat function not implemented"); + errno = 0; + TEST_VERIFY (readdir64_compat (stream) == NULL); + TEST_COMPARE (errno, expected); + return; + case SUPPORT_READDIR64_R_COMPAT: + { + if (readdir64_r_compat == NULL) + FAIL_EXIT1 ("readdir64_r compat function not implemented"); + struct __old_dirent64 buf; + struct __old_dirent64 *e; + errno = readdir64_r_compat (stream, &buf, &e); + TEST_COMPARE (errno, expected); + } + return; + } + FAIL_EXIT1 ("support_readdir_expect_error: invalid op argument %d", + (int) op); +} diff --git a/support/tst-support_readdir.c b/support/tst-support_readdir.c new file mode 100644 index 0000000000..c84d523b83 --- /dev/null +++ b/support/tst-support_readdir.c @@ -0,0 +1,71 @@ +/* Test the support_readdir function. + Copyright (C) 2024 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include + +#include +#include +#include +#include +#include +#include + +static int +do_test (void) +{ + DIR *reference_stream = xopendir ("."); + struct dirent64 *reference = xreaddir64 (reference_stream); + + for (enum support_readdir_op op = 0; op <= support_readdir_op_last (); ++op) + { + DIR *stream = xopendir ("."); + struct support_dirent e; + memset (&e, 0xcc, sizeof (e)); + e.d_name = NULL; + TEST_VERIFY (support_readdir (stream, op, &e)); + TEST_COMPARE (e.d_ino, reference->d_ino); +#ifdef _DIRENT_HAVE_D_OFF + TEST_COMPARE (e.d_off, reference->d_off); +#else + TEST_COMPARE (e.d_off, 0); +#endif + TEST_COMPARE (e.d_type, reference->d_type); + TEST_COMPARE_STRING (e.d_name, reference->d_name); + free (e.d_name); + xclosedir (stream); + } + + xclosedir (reference_stream); + + /* Error injection test. */ + int devnull = xopen ("/dev/null", O_RDONLY, 0); + for (enum support_readdir_op op = 0; op <= support_readdir_op_last (); ++op) + { + DIR *stream = xopendir ("."); + /* A descriptor incompatible with readdir. */ + xdup2 (devnull, dirfd (stream)); + errno = -1; + support_readdir_expect_error (stream, op, ENOTDIR); + xclosedir (stream); + } + xclose (devnull); + + return 0; +} + +#include From patchwork Fri Aug 30 19:52:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 1979208 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=Jlf1my5z; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WwTQC6mXxz1yfn for ; Sat, 31 Aug 2024 05:54:59 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 2BB23384CBA1 for ; Fri, 30 Aug 2024 19:54:58 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTP id 43CAA385E011 for ; Fri, 30 Aug 2024 19:53:06 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 43CAA385E011 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 43CAA385E011 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725047591; cv=none; b=ZbAN0TLatviQMqUp3pYmo4QGoBvaaQn6eVLLdMkGkcLhu3Cw/Pqxla72h3Pi4JI3+AnWiV3invughxU/6xxtEv5/49CMJzVL9CkZqe3yy5mpSUFqsl5rl0jRmfbhEpKdMzqeZsdGvxt1o/jcTOlflugXbAWZAv/C2FPajJHdj7o= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725047591; c=relaxed/simple; bh=Pw8/m9BomgskMQBGIpTCI0NLbH+v+tFGQVxJ9FHPmQM=; h=DKIM-Signature:From:To:Subject:Message-ID:Date:MIME-Version; b=pNQtgiUBcdt4Vh17GRpx1SC01sadSg0QKsOaQbBCklTD+fy4Tpwz9KllfrPZOveJpKf8N6euHqm0nnDt7NPNxd8Fz+X9DdmAq2B2TE+EK3Y7Ky06O5Zg9+J8rU6ryU7wCNg66jwHHLFCSLt4/6hsP9j2YQcQQVwSCz6PwmYsCkM= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1725047586; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=snA0d8z0w8GGQXZOfwyIMrvsR91m1VJXif9yqnKxrUg=; b=Jlf1my5zAb1lOEcRI5E34xMH+b4AiL87tB46DAYpfitZjb4piNi8ofL8kS8zr3uMezBeE2 6xAf1YEnU5sTJ9b47muOXrBpu6rLt41Jzv3+9M9RT+zfvicY9TCOQDo+EOd6pTehjk19Hw 5wdAEYLQN6mLKaBiJpneznfHTRXvvjE= Received: from mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-152-i9LuguitOR2nhpI4v2RNYg-1; Fri, 30 Aug 2024 15:52:59 -0400 X-MC-Unique: i9LuguitOR2nhpI4v2RNYg-1 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 27DB71955D48 for ; Fri, 30 Aug 2024 19:52:58 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.2.16.7]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 8E32419560AA for ; Fri, 30 Aug 2024 19:52:56 +0000 (UTC) From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH v4 05/13] support: Add FUSE-based file system test framework to support/ In-Reply-To: Message-ID: <666dad4ffb0d685e41e2342491e6a5dd1858f60b.1725047142.git.fweimer@redhat.com> References: X-From-Line: 666dad4ffb0d685e41e2342491e6a5dd1858f60b Mon Sep 17 00:00:00 2001 Date: Fri, 30 Aug 2024 21:52:53 +0200 User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-10.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org This allows to monitor the exact file system operations performed by glibc and inject errors. Hurd does not have . To get the sources to compile at least, the same approach as in support/test-container.c is used. --- support/Makefile | 2 + support/fuse.h | 215 +++++++++++ support/support_fuse.c | 705 +++++++++++++++++++++++++++++++++++++ support/tst-support_fuse.c | 348 ++++++++++++++++++ 4 files changed, 1270 insertions(+) create mode 100644 support/fuse.h create mode 100644 support/support_fuse.c create mode 100644 support/tst-support_fuse.c diff --git a/support/Makefile b/support/Makefile index ec9793ab1e..fe9a099bed 100644 --- a/support/Makefile +++ b/support/Makefile @@ -62,6 +62,7 @@ libsupport-routines = \ support_format_herrno \ support_format_hostent \ support_format_netent \ + support_fuse \ support_isolate_in_subprocess \ support_mutex_pi_monotonic \ support_need_proc \ @@ -324,6 +325,7 @@ tests = \ tst-support_capture_subprocess \ tst-support_descriptors \ tst-support_format_dns_packet \ + tst-support_fuse \ tst-support_quote_blob \ tst-support_quote_blob_wide \ tst-support_quote_string \ diff --git a/support/fuse.h b/support/fuse.h new file mode 100644 index 0000000000..4c365fbc0c --- /dev/null +++ b/support/fuse.h @@ -0,0 +1,215 @@ +/* Facilities for FUSE-backed file system tests. + Copyright (C) 2024 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +/* Before using this functionality, use support_enter_mount_namespace + to ensure that mounts do not impact the overall system. */ + +#ifndef SUPPORT_FUSE_H +#define SUPPORT_FUSE_H + +#include +#include +#include +#include + +#include + +/* This function must be called furst, before support_fuse_mount, to + prepare unprivileged mounting. */ +void support_fuse_init (void); + +/* This function can be called instead of support_fuse_init. It does + not use mount and user namespaces, so it requires root privileges, + and cleanup after testing may be incomplete. This is intended only + for test development. */ +void support_fuse_init_no_namespace (void); + +/* Opaque type for tracking FUSE mount state. */ +struct support_fuse; + +/* This function disables a mount point created using + support_fuse_mount. */ +void support_fuse_unmount (struct support_fuse *) __nonnull ((1)); + +/* This function is called on a separate thread after calling + support_fuse_mount. F is the mount state, and CLOSURE the argument + that was passed to support_fuse_mount. The callback function is + expected to call support_fuse_next to read packets from the kernel + and handle them according to the test's need. */ +typedef void (*support_fuse_callback) (struct support_fuse *f, void *closure); + +/* This function creates a new mount point, implemented by CALLBACK. + CLOSURE is passed to CALLBACK as the second argument. */ +struct support_fuse *support_fuse_mount (support_fuse_callback callback, + void *closure) + __nonnull ((1)) __attr_dealloc (support_fuse_unmount, 1); + +/* This function returns the path to the mount point for F. The + returned string is valid until support_fuse_unmount (F) is called. */ +const char * support_fuse_mountpoint (struct support_fuse *f) __nonnull ((1)); + + +/* Renders the OPCODE as a string (FUSE_* constant. The caller must + free the returned string. */ +char * support_fuse_opcode (uint32_t opcode) __attr_dealloc_free; + +/* Use to provide a checked cast facility. Use the + support_fuse_in_cast macro below. */ +void *support_fuse_cast_internal (struct fuse_in_header *, uint32_t) + __nonnull ((1)); +void *support_fuse_cast_name_internal (struct fuse_in_header *, uint32_t, + size_t skip, char **name) + __nonnull ((1)); + +/* The macro expansion support_fuse_in_cast (P, TYPE) casts the + pointer INH to the appropriate type corresponding to the FUSE_TYPE + opcode. It fails (terminates the process) if INH->opcode does not + match FUSE_TYPE. The type of the returned pointer matches that of + the FUSE_* constant. + + Maintenance note: Adding support for additional struct fuse_*_in + types is generally easy, except when there is trailing data after + the struct (see below for support_fuse_cast_name, for example), and + the kernel has changed struct sizes over time. This has happened + recently with struct fuse_setxattr_in, and would require special + handling if implemented. */ +#define support_fuse_payload_type_INIT struct fuse_init_in +#define support_fuse_payload_type_LOOKUP char +#define support_fuse_payload_type_OPEN struct fuse_open_in +#define support_fuse_payload_type_READ struct fuse_read_in +#define support_fuse_payload_type_SETATTR struct fuse_setattr_in +#define support_fuse_payload_type_WRITE struct fuse_write_in +#define support_fuse_cast(typ, inh) \ + ((support_fuse_payload_type_##typ *) \ + support_fuse_cast_internal ((inh), FUSE_##typ)) + +/* Same as support_fuse_cast, but also writes the passed name to *NAMEP. */ +#define support_fuse_payload_name_type_CREATE struct fuse_create_in +#define support_fuse_payload_name_type_MKDIR struct fuse_mkdir_in +#define support_fuse_cast_name(typ, inh, namep) \ + ((support_fuse_payload_name_type_##typ *) \ + support_fuse_cast_name_internal \ + ((inh), FUSE_##typ, sizeof (support_fuse_payload_name_type_##typ), \ + (namep))) + +/* This function should be called from the callback function. It + returns NULL if the mount point has been unmounted. The result can + be cast using support_fuse_in_cast. The pointer is invalidated + with the next call to support_fuse_next. + + Typical use involves handling some basics using the + support_fuse_handle_* building blocks, following by a switch + statement on the result member of the returned struct, to implement + what a particular test needs. Casts to payload data should be made + using support_fuse_in_cast. + + By default, FUSE_FORGET responses are filtered. See + support_fuse_filter_forget for turning that off. */ +struct fuse_in_header *support_fuse_next (struct support_fuse *f) + __nonnull ((1)); + +/* This function can be called from a callback function to handle + basic aspects of directories (OPENDIR, GETATTR, RELEASEDIR). + inh->nodeid is used as the inode number for the directory. This + function must be called after support_fuse_next. */ +bool support_fuse_handle_directory (struct support_fuse *f) __nonnull ((1)); + +/* This function can be called from a callback function to handle + access to the mount point itself, after call support_fuse_next. */ +bool support_fuse_handle_mountpoint (struct support_fuse *f) __nonnull ((1)); + +/* If FILTER_ENABLED, future support_fuse_next calls will not return + FUSE_FORGET events (and simply discared them, as they require no + reply). If !FILTER_ENABLED, the callback needs to handle + FUSE_FORGET events and call support_fuse_no_reply. */ +void support_fuse_filter_forget (struct support_fuse *f, bool filter_enabled) + __nonnull ((1)); + +/* This function should be called from the callback function after + support_fuse_next returned a non-null pointer. It sends out a + response packet on the FUSE device with the supplied payload data. */ +void support_fuse_reply (struct support_fuse *f, + const void *payload, size_t payload_size) + __nonnull ((1)) __attr_access ((__read_only__, 2, 3)); + +/* This function should be called from the callback function. It + replies to a request with an error indicator. ERROR must be positive. */ +void support_fuse_reply_error (struct support_fuse *f, uint32_t error) + __nonnull ((1)); + +/* This function should be called from the callback function. It + sends out an empty (but success-indicating) reply packet. */ +void support_fuse_reply_empty (struct support_fuse *f) __nonnull ((1)); + +/* Do not send a reply. Only to be used after a support_fuse_next + call that returned a FUSE_FORGET event. */ +void support_fuse_no_reply (struct support_fuse *f) __nonnull ((1)); + +/* Specific reponse preparation functions. The returned object can be + updated as needed. If a NODEID argument is present, it will be + used to set the inode and FUSE nodeid fields. Without such an + argument, it is initialized from the current request (if the reply + requires this field). This function must be called after + support_fuse_next. The actual response must be sent using + support_fuse_reply_prepared (or a support_fuse_reply_error call can + be used to cancel the response). */ +struct fuse_entry_out *support_fuse_prepare_entry (struct support_fuse *f, + uint64_t nodeid) + __nonnull ((1)); +struct fuse_attr_out *support_fuse_prepare_attr (struct support_fuse *f) + __nonnull ((1)); + +/* Similar to the other support_fuse_prepare_* functions, but it + prepares for two response packets. They can be updated through the + pointers written to *OUT_ENTRY and *OUT_OPEN prior to calling + support_fuse_reply_prepared. */ +void support_fuse_prepare_create (struct support_fuse *f, + uint64_t nodeid, + struct fuse_entry_out **out_entry, + struct fuse_open_out **out_open) + __nonnull ((1, 3, 4)); + + +/* Prepare sending a directory stream. Must be called after + support_fuse_next and before support_fuse_dirstream_add. */ +struct support_fuse_dirstream; +struct support_fuse_dirstream *support_fuse_prepare_readdir (struct + support_fuse *f); + +/* Adds directory using D_INO, D_OFF, D_TYPE, D_NAME to the directory + stream D. Must be called after support_fuse_prepare_readdir. + + D_OFF is the offset of the next directory entry, not the current + one. The first entry has offset zero. The first requested offset + can be obtained from the READ payload (struct fuse_read_in) prior + to calling this function. + + Returns true if the entry could be added to the buffer, or false if + there was insufficient room. Sending the buffer is delayed until + support_fuse_reply_prepared is called. */ +bool support_fuse_dirstream_add (struct support_fuse_dirstream *d, + uint64_t d_ino, uint64_t d_off, + uint32_t d_type, + const char *d_name); + +/* Send a prepared response. Must be called after one of the + support_fuse_prepare_* functions and before the next + support_fuse_next call. */ +void support_fuse_reply_prepared (struct support_fuse *f) __nonnull ((1)); + +#endif /* SUPPORT_FUSE_H */ diff --git a/support/support_fuse.c b/support/support_fuse.c new file mode 100644 index 0000000000..135dbf1198 --- /dev/null +++ b/support/support_fuse.c @@ -0,0 +1,705 @@ +/* Facilities for FUSE-backed file system tests. + Copyright (C) 2024 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef __linux__ +# include +#else +/* Fallback definitions that mark the test as unsupported. */ +# define mount(...) ({ FAIL_UNSUPPORTED ("mount"); -1; }) +# define umount(...) ({ FAIL_UNSUPPORTED ("mount"); -1; }) +#endif + +struct support_fuse +{ + char *mountpoint; + void *buffer_start; /* Begin of allocation. */ + void *buffer_next; /* Next read position. */ + void *buffer_limit; /* End of buffered data. */ + void *buffer_end; /* End of allocation. */ + struct fuse_in_header *inh; /* Most recent request (support_fuse_next). */ + union /* Space for prepared responses. */ + { + struct fuse_attr_out attr; + struct fuse_entry_out entry; + struct + { + struct fuse_entry_out entry; + struct fuse_open_out open; + } create; + } prepared; + void *prepared_pointer; /* NULL if inactive. */ + size_t prepared_size; /* 0 if inactive. */ + + /* Used for preparing readdir responses. Already used-up area for + the current request is counted by prepared_size. */ + void *readdir_buffer; + size_t readdir_buffer_size; + + pthread_t handler; /* Thread handling requests. */ + uid_t uid; /* Cached value for the current process. */ + uid_t gid; /* Cached value for the current process. */ + int fd; /* FUSE file descriptor. */ + int connection; /* Entry under /sys/fs/fuse/connections. */ + bool filter_forget; /* Controls FUSE_FORGET event dropping. */ + _Atomic bool disconnected; +}; + +struct fuse_thread_wrapper_args +{ + struct support_fuse *f; + support_fuse_callback callback; + void *closure; +}; + +/* Set by support_fuse_init to indicate that support_fuse_mount may be + called. */ +static bool support_fuse_init_called; + +/* Allocate the read buffer in F with SIZE bytes capacity. Does not + free the previously allocated buffer. */ +static void support_fuse_allocate (struct support_fuse *f, size_t size) + __nonnull ((1)); + +/* Internal mkdtemp replacement */ +static char * support_fuse_mkdir (const char *prefix) __nonnull ((1)); + +/* Low-level allocation function for support_fuse_mount. Does not + perform the mount. */ +static struct support_fuse *support_fuse_open (void); + +/* Thread wrapper function for use with pthread_create. Uses struct + fuse_thread_wrapper_args. */ +static void *support_fuse_thread_wrapper (void *closure) __nonnull ((1)); + +/* Initial step before preparing a reply. SIZE must be the size of + the F->prepared member that is going to be used. */ +static void support_fuse_prepare_1 (struct support_fuse *f, size_t size); + +/* Similar to support_fuse_reply_error, but not check that ERROR is + not zero. */ +static void support_fuse_reply_error_1 (struct support_fuse *f, + uint32_t error) __nonnull ((1)); + +/* Path to the directory containing mount points. Initialized by an + ELF constructor. All mountpoints are collected there so that the + test wrapper can clean them up without keeping track of them + individually. */ +static char *support_fuse_mountpoints; + +/* PID of the process that should clean up the mount points in the ELF + destructor. */ +static pid_t support_fuse_cleanup_pid; + +static void +support_fuse_allocate (struct support_fuse *f, size_t size) +{ + f->buffer_start = xmalloc (size); + f->buffer_end = f->buffer_start + size; + f->buffer_limit = f->buffer_start; + f->buffer_next = f->buffer_limit; +} + +void +support_fuse_filter_forget (struct support_fuse *f, bool filter) +{ + f->filter_forget = filter; +} + +void * +support_fuse_cast_internal (struct fuse_in_header *p, uint32_t expected) +{ + if (expected != p->opcode + && !(expected == FUSE_READ && p->opcode == FUSE_READDIR)) + { + char *expected1 = support_fuse_opcode (expected); + char *actual = support_fuse_opcode (p->opcode); + FAIL_EXIT1 ("attempt to cast %s to %s", actual, expected1); + } + return p + 1; +} + +void * +support_fuse_cast_name_internal (struct fuse_in_header *p, uint32_t expected, + size_t skip, char **name) +{ + char *result = support_fuse_cast_internal (p, expected); + *name = result + skip; + return result; +} + +bool +support_fuse_dirstream_add (struct support_fuse_dirstream *d, + uint64_t d_ino, uint64_t d_off, + uint32_t d_type, const char *d_name) +{ + struct support_fuse *f = (struct support_fuse *) d; + size_t structlen = offsetof (struct fuse_dirent, name); + size_t namelen = strlen (d_name); /* No null termination. */ + size_t required_size = FUSE_DIRENT_ALIGN (structlen + namelen); + if (f->readdir_buffer_size - f->prepared_size < required_size) + return false; + struct fuse_dirent entry = + { + .ino = d_ino, + .off = d_off, + .type = d_type, + .namelen = namelen, + }; + memcpy (f->readdir_buffer + f->prepared_size, &entry, structlen); + /* Use strncpy to write padding and avoid passing uninitialized + bytes to the read system call. */ + strncpy (f->readdir_buffer + f->prepared_size + structlen, d_name, + required_size - structlen); + f->prepared_size += required_size; + return true; +} + +bool +support_fuse_handle_directory (struct support_fuse *f) +{ + TEST_VERIFY (f->inh != NULL); + switch (f->inh->opcode) + { + case FUSE_OPENDIR: + { + struct fuse_open_out out = + { + }; + support_fuse_reply (f, &out, sizeof (out)); + } + return true; + case FUSE_RELEASEDIR: + support_fuse_reply_empty (f); + return true; + case FUSE_GETATTR: + { + struct fuse_attr_out *out = support_fuse_prepare_attr (f); + out->attr.mode = S_IFDIR | 0700; + support_fuse_reply_prepared (f); + } + return true; + default: + return false; + } +} + +bool +support_fuse_handle_mountpoint (struct support_fuse *f) +{ + TEST_VERIFY (f->inh != NULL); + /* 1 is the root node. */ + if (f->inh->opcode == FUSE_GETATTR && f->inh->nodeid == 1) + return support_fuse_handle_directory (f); + return false; +} + +void +support_fuse_init (void) +{ + support_fuse_init_called = true; + + support_become_root (); + if (!support_enter_mount_namespace ()) + FAIL_UNSUPPORTED ("mount namespaces not supported"); +} + +void +support_fuse_init_no_namespace (void) +{ + support_fuse_init_called = true; +} + +static char * +support_fuse_mkdir (const char *prefix) +{ + /* Do not use mkdtemp to avoid interfering with its tests. */ + unsigned int counter = 1; + unsigned int pid = getpid (); + while (true) + { + char *path = xasprintf ("%s%u.%u/", prefix, pid, counter); + if (mkdir (path, 0700) == 0) + return path; + if (errno != EEXIST) + FAIL_EXIT1 ("mkdir (\"%s\"): %m", path); + free (path); + ++counter; + } +} + +struct support_fuse * +support_fuse_mount (support_fuse_callback callback, void *closure) +{ + TEST_VERIFY_EXIT (support_fuse_init_called); + + /* Request at least minor version 12 because it changed struct sizes. */ + enum { min_version = 12 }; + + struct support_fuse *f = support_fuse_open (); + char *mount_options + = xasprintf ("fd=%d,rootmode=040700,user_id=%u,group_id=%u", + f->fd, f->uid, f->gid); + if (mount ("fuse", f->mountpoint, "fuse.glibc", + MS_NOSUID|MS_NODEV, mount_options) + != 0) + FAIL_EXIT1 ("FUSE mount on %s: %m", f->mountpoint); + free (mount_options); + + /* Retry with an older FUSE version. */ + while (true) + { + struct fuse_in_header *inh = support_fuse_next (f); + struct fuse_init_in *init_in = support_fuse_cast (INIT, inh); + if (init_in->major < 7 + || (init_in->major == 7 && init_in->minor < min_version)) + FAIL_UNSUPPORTED ("kernel FUSE version is %u.%u, too old", + init_in->major, init_in->minor); + if (init_in->major > 7) + { + uint32_t major = 7; + support_fuse_reply (f, &major, sizeof (major)); + continue; + } + TEST_VERIFY (init_in->flags & FUSE_DONT_MASK); + struct fuse_init_out out = + { + .major = 7, + .minor = min_version, + /* Request that the kernel does not apply umask. */ + .flags = FUSE_DONT_MASK, + }; + support_fuse_reply (f, &out, sizeof (out)); + + { + struct fuse_thread_wrapper_args args = + { + .f = f, + .callback = callback, + .closure = closure, + }; + f->handler = xpthread_create (NULL, + support_fuse_thread_wrapper, &args); + struct stat64 st; + xstat64 (f->mountpoint, &st); + f->connection = minor (st.st_dev); + /* Got a reply from the thread, safe to deallocate args. */ + } + + return f; + } +} + +const char * +support_fuse_mountpoint (struct support_fuse *f) +{ + return f->mountpoint; +} + +void +support_fuse_no_reply (struct support_fuse *f) +{ + TEST_VERIFY (f->inh != NULL); + TEST_COMPARE (f->inh->opcode, FUSE_FORGET); + f->inh = NULL; +} + +char * +support_fuse_opcode (uint32_t op) +{ + const char *result; + switch (op) + { +#define X(n) case n: result = #n; break + X(FUSE_LOOKUP); + X(FUSE_FORGET); + X(FUSE_GETATTR); + X(FUSE_SETATTR); + X(FUSE_READLINK); + X(FUSE_SYMLINK); + X(FUSE_MKNOD); + X(FUSE_MKDIR); + X(FUSE_UNLINK); + X(FUSE_RMDIR); + X(FUSE_RENAME); + X(FUSE_LINK); + X(FUSE_OPEN); + X(FUSE_READ); + X(FUSE_WRITE); + X(FUSE_STATFS); + X(FUSE_RELEASE); + X(FUSE_FSYNC); + X(FUSE_SETXATTR); + X(FUSE_GETXATTR); + X(FUSE_LISTXATTR); + X(FUSE_REMOVEXATTR); + X(FUSE_FLUSH); + X(FUSE_INIT); + X(FUSE_OPENDIR); + X(FUSE_READDIR); + X(FUSE_RELEASEDIR); + X(FUSE_FSYNCDIR); + X(FUSE_GETLK); + X(FUSE_SETLK); + X(FUSE_SETLKW); + X(FUSE_ACCESS); + X(FUSE_CREATE); + X(FUSE_INTERRUPT); + X(FUSE_BMAP); + X(FUSE_DESTROY); + X(FUSE_IOCTL); + X(FUSE_POLL); + X(FUSE_NOTIFY_REPLY); + X(FUSE_BATCH_FORGET); + X(FUSE_FALLOCATE); + X(FUSE_READDIRPLUS); + X(FUSE_RENAME2); + X(FUSE_LSEEK); + X(FUSE_COPY_FILE_RANGE); + X(FUSE_SETUPMAPPING); + X(FUSE_REMOVEMAPPING); + X(FUSE_SYNCFS); + X(FUSE_TMPFILE); + X(FUSE_STATX); +#undef X + default: + return xasprintf ("FUSE_unknown_%u", op); + } + return xstrdup (result); +} + +static struct support_fuse * +support_fuse_open (void) +{ + struct support_fuse *result = xmalloc (sizeof (*result)); + result->mountpoint = support_fuse_mkdir (support_fuse_mountpoints); + result->inh = NULL; + result->prepared_pointer = NULL; + result->prepared_size = 0; + result->readdir_buffer = NULL; + result->readdir_buffer_size = 0; + result->uid = getuid (); + result->gid = getgid (); + result->fd = open ("/dev/fuse", O_RDWR, 0); + if (result->fd < 0) + { + if (errno == ENOENT || errno == ENODEV || errno == EPERM + || errno == EACCES) + FAIL_UNSUPPORTED ("cannot open /dev/fuse: %m"); + else + FAIL_EXIT1 ("cannot open /dev/fuse: %m"); + } + result->connection = -1; + result->filter_forget = true; + result->disconnected = false; + support_fuse_allocate (result, FUSE_MIN_READ_BUFFER); + return result; +} + +static void +support_fuse_prepare_1 (struct support_fuse *f, size_t size) +{ + TEST_VERIFY (f->prepared_pointer == NULL); + f->prepared_size = size; + memset (&f->prepared, 0, size); + f->prepared_pointer = &f->prepared; +} + +struct fuse_attr_out * +support_fuse_prepare_attr (struct support_fuse *f) +{ + support_fuse_prepare_1 (f, sizeof (f->prepared.attr)); + f->prepared.attr.attr.uid = f->uid; + f->prepared.attr.attr.gid = f->gid; + f->prepared.attr.attr.ino = f->inh->nodeid; + return &f->prepared.attr; +} + +void +support_fuse_prepare_create (struct support_fuse *f, + uint64_t nodeid, + struct fuse_entry_out **out_entry, + struct fuse_open_out **out_open) +{ + support_fuse_prepare_1 (f, sizeof (f->prepared.create)); + f->prepared.create.entry.nodeid = nodeid; + f->prepared.create.entry.attr.uid = f->uid; + f->prepared.create.entry.attr.gid = f->gid; + f->prepared.create.entry.attr.ino = nodeid; + *out_entry = &f->prepared.create.entry; + *out_open = &f->prepared.create.open; +} + +struct fuse_entry_out * +support_fuse_prepare_entry (struct support_fuse *f, uint64_t nodeid) +{ + support_fuse_prepare_1 (f, sizeof (f->prepared.entry)); + f->prepared.entry.nodeid = nodeid; + f->prepared.entry.attr.uid = f->uid; + f->prepared.entry.attr.gid = f->gid; + f->prepared.entry.attr.ino = nodeid; + return &f->prepared.entry; +} + +struct support_fuse_dirstream * +support_fuse_prepare_readdir (struct support_fuse *f) +{ + support_fuse_prepare_1 (f, 0); + struct fuse_read_in *p = support_fuse_cast (READ, f->inh); + if (p->size > f->readdir_buffer_size) + { + free (f->readdir_buffer); + f->readdir_buffer = xmalloc (p->size); + f->readdir_buffer_size = p->size; + } + f->prepared_pointer = f->readdir_buffer; + return (struct support_fuse_dirstream *) f; +} + +struct fuse_in_header * +support_fuse_next (struct support_fuse *f) +{ + TEST_VERIFY (f->inh == NULL); + while (true) + { + if (f->buffer_next < f->buffer_limit) + { + f->inh = f->buffer_next; + f->buffer_next = (void *) f->buffer_next + f->inh->len; + /* Suppress FUSE_FORGET responses if requested. */ + if (f->filter_forget && f->inh->opcode == FUSE_FORGET) + { + f->inh = NULL; + continue; + } + return f->inh; + } + ssize_t ret = read (f->fd, f->buffer_start, + f->buffer_end - f->buffer_start); + if (ret == 0) + FAIL_EXIT (1, "unexpected EOF on FUSE device"); + if (ret < 0 && errno == EINVAL) + { + /* Increase buffer size. */ + size_t new_size = 2 * (size_t) (f->buffer_end - f->buffer_start); + free (f->buffer_start); + support_fuse_allocate (f, new_size); + continue; + } + if (ret < 0) + { + if (f->disconnected) + /* Unmount detected. */ + return NULL; + FAIL_EXIT1 ("read error on FUSE device: %m"); + } + /* Read was successful, make [next, limit) the active buffer area. */ + f->buffer_next = f->buffer_start; + f->buffer_limit = (void *) f->buffer_start + ret; + } +} + +void +support_fuse_reply (struct support_fuse *f, + const void *payload, size_t payload_size) +{ + TEST_VERIFY_EXIT (f->inh != NULL); + TEST_VERIFY (f->prepared_pointer == NULL); + struct fuse_out_header outh = + { + .len = sizeof (outh) + payload_size, + .unique = f->inh->unique, + }; + struct iovec iov[] = + { + { &outh, sizeof (outh) }, + { (void *) payload, payload_size }, + }; + ssize_t ret = writev (f->fd, iov, array_length (iov)); + if (ret < 0) + { + if (!f->disconnected) + /* Some kernels produce write errors upon disconnect. */ + FAIL_EXIT1 ("FUSE write failed for %s response" + " (%zu bytes payload): %m", + support_fuse_opcode (f->inh->opcode), payload_size); + } + else if (ret != sizeof (outh) + payload_size) + FAIL_EXIT1 ("FUSE write short for %s response (%zu bytes payload):" + " %zd bytes", + support_fuse_opcode (f->inh->opcode), payload_size, ret); + f->inh = NULL; +} + +void +support_fuse_reply_empty (struct support_fuse *f) +{ + support_fuse_reply_error_1 (f, 0); +} + +static void +support_fuse_reply_error_1 (struct support_fuse *f, uint32_t error) +{ + TEST_VERIFY_EXIT (f->inh != NULL); + struct fuse_out_header outh = + { + .len = sizeof (outh), + .error = -error, + .unique = f->inh->unique, + }; + ssize_t ret = write (f->fd, &outh, sizeof (outh)); + if (ret < 0) + { + /* Some kernels produce write errors upon disconnect. */ + if (!f->disconnected) + FAIL_EXIT1 ("FUSE write failed for %s error response: %m", + support_fuse_opcode (f->inh->opcode)); + } + else if (ret != sizeof (outh)) + FAIL_EXIT1 ("FUSE write short for %s error response: %zd bytes", + support_fuse_opcode (f->inh->opcode), ret); + f->inh = NULL; + f->prepared_pointer = NULL; + f->prepared_size = 0; +} + +void +support_fuse_reply_error (struct support_fuse *f, uint32_t error) +{ + TEST_VERIFY (error > 0); + support_fuse_reply_error_1 (f, error); +} + +void +support_fuse_reply_prepared (struct support_fuse *f) +{ + TEST_VERIFY_EXIT (f->prepared_pointer != NULL); + /* Re-use the non-prepared reply function. It requires + f->prepared_* to be non-null, so reset the fields before the call. */ + void *prepared_pointer = f->prepared_pointer; + size_t prepared_size = f->prepared_size; + f->prepared_pointer = NULL; + f->prepared_size = 0; + support_fuse_reply (f, prepared_pointer, prepared_size); +} + +static void * +support_fuse_thread_wrapper (void *closure) +{ + struct fuse_thread_wrapper_args args + = *(struct fuse_thread_wrapper_args *) closure; + + /* Handle the initial stat call. */ + struct fuse_in_header *inh = support_fuse_next (args.f); + if (inh == NULL || !support_fuse_handle_mountpoint (args.f)) + { + support_fuse_reply_error (args.f, EIO); + return NULL; + } + + args.callback (args.f, args.closure); + return NULL; +} + +void +support_fuse_unmount (struct support_fuse *f) +{ + /* Signal the unmount to the handler thread. Some kernels report + not just ENODEV errors on read. */ + f->disconnected = true; + + { + char *path = xasprintf ("/sys/fs/fuse/connections/%d/abort", + f->connection); + /* Some kernels do not support these files under /sys. */ + int fd = open (path, O_RDWR | O_TRUNC); + if (fd >= 0) + { + TEST_COMPARE (write (fd, "1", 1), 1); + xclose (fd); + } + free (path); + } + if (umount (f->mountpoint) != 0) + FAIL ("FUSE: umount (\"%s\"): %m", f->mountpoint); + xpthread_join (f->handler); + if (rmdir (f->mountpoint) != 0) + FAIL ("FUSE: rmdir (\"%s\"): %m", f->mountpoint); + xclose (f->fd); + free (f->mountpoint); + free (f->readdir_buffer); + free (f); +} + +static void __attribute__ ((constructor)) +init (void) +{ + /* The test_dir test driver variable is not yet set at this point. */ + const char *tmpdir = getenv ("TMPDIR"); + if (tmpdir == NULL || tmpdir[0] == '\0') + tmpdir = "/tmp"; + + char *prefix = xasprintf ("%s/glibc-tst-fuse.", tmpdir); + support_fuse_mountpoints = support_fuse_mkdir (prefix); + free (prefix); + support_fuse_cleanup_pid = getpid (); +} + +static void __attribute__ ((destructor)) +fini (void) +{ + if (support_fuse_cleanup_pid != getpid () + || support_fuse_mountpoints == NULL) + return; + DIR *dir = xopendir (support_fuse_mountpoints); + while (true) + { + struct dirent64 *e = readdir64 (dir); + if (e == NULL) + /* Ignore errors. */ + break; + if (*e->d_name == '.') + /* Skip "." and "..". No hidden files expected. */ + continue; + if (unlinkat (dirfd (dir), e->d_name, AT_REMOVEDIR) != 0) + break; + rewinddir (dir); + } + xclosedir (dir); + rmdir (support_fuse_mountpoints); + free (support_fuse_mountpoints); + support_fuse_mountpoints = NULL; +} diff --git a/support/tst-support_fuse.c b/support/tst-support_fuse.c new file mode 100644 index 0000000000..c4075a6608 --- /dev/null +++ b/support/tst-support_fuse.c @@ -0,0 +1,348 @@ +/* Facilities for FUSE-backed file system tests. + Copyright (C) 2024 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static void +fuse_thread (struct support_fuse *f, void *closure) +{ + /* Turn on returning FUSE_FORGET responses. */ + support_fuse_filter_forget (f, false); + + /* Inode and nodeid for "file" and "new". */ + enum { NODE_FILE = 2, NODE_NEW, NODE_SUBDIR, NODE_SYMLINK }; + struct fuse_in_header *inh; + while ((inh = support_fuse_next (f)) != NULL) + { + { + char *opcode = support_fuse_opcode (inh->opcode); + printf ("info: (T) event %s(%llu) len=%u nodeid=%llu\n", + opcode, (unsigned long long int) inh->unique, inh->len, + (unsigned long long int) inh->nodeid); + free (opcode); + } + + /* Handle mountpoint and basic directory operation for the root (1). */ + if (support_fuse_handle_mountpoint (f) + || (inh->nodeid == 1 && support_fuse_handle_directory (f))) + continue; + + switch (inh->opcode) + { + case FUSE_READDIR: + /* Implementation of getdents64. */ + if (inh->nodeid == 1) + { + struct support_fuse_dirstream *d + = support_fuse_prepare_readdir (f); + TEST_COMPARE (support_fuse_cast (READ, inh)->offset, 0); + TEST_VERIFY (support_fuse_dirstream_add (d, 1, 1, DT_DIR, ".")); + TEST_VERIFY (support_fuse_dirstream_add (d, 1, 2, DT_DIR, "..")); + TEST_VERIFY (support_fuse_dirstream_add (d, NODE_FILE, 3, DT_REG, + "file")); + support_fuse_reply_prepared (f); + } + else + support_fuse_reply_error (f, EIO); + break; + case FUSE_LOOKUP: + /* Part of the implementation of open. */ + { + char *name = support_fuse_cast (LOOKUP, inh); + printf (" name: %s\n", name); + if (inh->nodeid == 1 && strcmp (name, "file") == 0) + { + struct fuse_entry_out *out + = support_fuse_prepare_entry (f, NODE_FILE); + out->attr.mode = S_IFREG | 0600; + support_fuse_reply_prepared (f); + } + else if (inh->nodeid == 1 && strcmp (name, "symlink") == 0) + { + struct fuse_entry_out *out + = support_fuse_prepare_entry (f, NODE_SYMLINK); + out->attr.mode = S_IFLNK | 0777; + support_fuse_reply_prepared (f); + } + else + support_fuse_reply_error (f, ENOENT); + } + break; + case FUSE_OPEN: + /* Implementation of open. */ + { + struct fuse_open_in *p = support_fuse_cast (OPEN, inh); + if (inh->nodeid == NODE_FILE) + { + TEST_VERIFY (!(p->flags & O_EXCL)); + struct fuse_open_out out = { 0, }; + support_fuse_reply (f, &out, sizeof (out)); + } + else + support_fuse_reply_error (f, ENOENT); + } + break; + case FUSE_GETATTR: + /* Happens after open. */ + if (inh->nodeid == NODE_FILE) + { + struct fuse_attr_out *out = support_fuse_prepare_attr (f); + out->attr.mode = S_IFREG | 0600; + out->attr.size = strlen ("Hello, world!"); + support_fuse_reply_prepared (f); + } + else + support_fuse_reply_error (f, ENOENT); + break; + case FUSE_READ: + /* Implementation of read. */ + if (inh->nodeid == NODE_FILE) + { + struct fuse_read_in *p = support_fuse_cast (READ, inh); + TEST_COMPARE (p->offset, 0); + TEST_VERIFY (p->size >= strlen ("Hello, world!")); + support_fuse_reply (f, + "Hello, world!", strlen ("Hello, world!")); + } + else + support_fuse_reply_error (f, EIO); + break; + case FUSE_FLUSH: + /* Sent in response to close. */ + support_fuse_reply_empty (f); + break; + case FUSE_GETXATTR: + /* This happens as part of a open-for-write operation. + Signal no support for extended attributes. */ + support_fuse_reply_error (f, ENOSYS); + break; + case FUSE_SETATTR: + /* This happens as part of a open-for-write operation to + implement O_TRUNC. */ + if (inh->nodeid == NODE_FILE) + { + struct fuse_setattr_in *p = support_fuse_cast (SETATTR, inh); + /* FATTR_LOCKOWNER may also be set. */ + TEST_COMPARE ((p->valid) & ~ FATTR_LOCKOWNER, FATTR_SIZE); + TEST_COMPARE (p->size, 0); + struct fuse_attr_out *out = support_fuse_prepare_attr (f); + out->attr.mode = S_IFREG | 0600; + support_fuse_reply_prepared (f); + } + else + support_fuse_reply_error (f, EIO); + break; + case FUSE_WRITE: + /* Implementation of write. */ + if (inh->nodeid == NODE_FILE) + { + struct fuse_write_in *p = support_fuse_cast (WRITE, inh); + TEST_COMPARE (p->offset, 0); + /* Write payload follows after struct fuse_write_in. */ + TEST_COMPARE_BLOB (p + 1, p->size, + "Good day to you too.", + strlen ("Good day to you too.")); + struct fuse_write_out out = + { + .size = p->size, + }; + support_fuse_reply (f, &out, sizeof (out)); + } + else + support_fuse_reply_error (f, EIO); + break; + case FUSE_CREATE: + /* Implementation of O_CREAT. */ + if (inh->nodeid == 1) + { + char *name; + struct fuse_create_in *p + = support_fuse_cast_name (CREATE, inh, &name); + TEST_VERIFY (S_ISREG (p->mode)); + TEST_COMPARE (p->mode & 07777, 0600); + TEST_COMPARE_STRING (name, "new"); + struct fuse_entry_out *out_entry; + struct fuse_open_out *out_open; + support_fuse_prepare_create (f, NODE_NEW, &out_entry, &out_open); + out_entry->attr.mode = S_IFREG | 0600; + support_fuse_reply_prepared (f); + } + else + support_fuse_reply_error (f, EIO); + break; + case FUSE_MKDIR: + /* Implementation of mkdir. */ + { + if (inh->nodeid == 1) + { + char *name; + struct fuse_mkdir_in *p + = support_fuse_cast_name (MKDIR, inh, &name); + TEST_COMPARE (p->mode, 01234); + TEST_COMPARE_STRING (name, "subdir"); + struct fuse_entry_out *out + = support_fuse_prepare_entry (f, NODE_SUBDIR); + out->attr.mode = S_IFDIR | p->mode; + support_fuse_reply_prepared (f); + } + else + support_fuse_reply_error (f, EIO); + } + break; + case FUSE_READLINK: + /* Implementation of readlink. */ + TEST_COMPARE (inh->nodeid, NODE_SYMLINK); + if (inh->nodeid == NODE_SYMLINK) + support_fuse_reply (f, "target-of-symbolic-link", + strlen ("target-of-symbolic-link")); + else + support_fuse_reply_error (f, EINVAL); + break; + case FUSE_FORGET: + support_fuse_no_reply (f); + break; + default: + support_fuse_reply_error (f, EIO); + } + } +} + +static int +do_test (void) +{ + support_fuse_init (); + + struct support_fuse *f = support_fuse_mount (fuse_thread, NULL); + + printf ("info: Attributes of mountpoint/root directory %s\n", + support_fuse_mountpoint (f)); + { + struct statx st; + xstatx (AT_FDCWD, support_fuse_mountpoint (f), 0, STATX_BASIC_STATS, &st); + TEST_COMPARE (st.stx_uid, getuid ()); + TEST_COMPARE (st.stx_gid, getgid ()); + TEST_VERIFY (S_ISDIR (st.stx_mode)); + TEST_COMPARE (st.stx_mode & 07777, 0700); + } + + printf ("info: List directory %s\n", support_fuse_mountpoint (f)); + { + DIR *dir = xopendir (support_fuse_mountpoint (f)); + + struct dirent *e = xreaddir (dir); + TEST_COMPARE (e->d_ino, 1); +#ifdef _DIRENT_HAVE_D_OFF + TEST_COMPARE (e->d_off, 1); +#endif + TEST_COMPARE (e->d_type, DT_DIR); + TEST_COMPARE_STRING (e->d_name, "."); + + e = xreaddir (dir); + TEST_COMPARE (e->d_ino, 1); +#ifdef _DIRENT_HAVE_D_OFF + TEST_COMPARE (e->d_off, 2); +#endif + TEST_COMPARE (e->d_type, DT_DIR); + TEST_COMPARE_STRING (e->d_name, ".."); + + e = xreaddir (dir); + TEST_COMPARE (e->d_ino, 2); +#ifdef _DIRENT_HAVE_D_OFF + TEST_COMPARE (e->d_off, 3); +#endif + TEST_COMPARE (e->d_type, DT_REG); + TEST_COMPARE_STRING (e->d_name, "file"); + + TEST_COMPARE (closedir (dir), 0); + } + + char *file_path = xasprintf ("%s/file", support_fuse_mountpoint (f)); + + printf ("info: Attributes of file %s\n", file_path); + { + struct statx st; + xstatx (AT_FDCWD, file_path, 0, STATX_BASIC_STATS, &st); + TEST_COMPARE (st.stx_uid, getuid ()); + TEST_COMPARE (st.stx_gid, getgid ()); + TEST_VERIFY (S_ISREG (st.stx_mode)); + TEST_COMPARE (st.stx_mode & 07777, 0600); + TEST_COMPARE (st.stx_size, strlen ("Hello, world!")); + } + + printf ("info: Read from %s\n", file_path); + { + int fd = xopen (file_path, O_RDONLY, 0); + char buf[64]; + ssize_t len = read (fd, buf, sizeof (buf)); + if (len < 0) + FAIL_EXIT1 ("read: %m"); + TEST_COMPARE_BLOB (buf, len, "Hello, world!", strlen ("Hello, world!")); + xclose (fd); + } + + printf ("info: Write to %s\n", file_path); + { + int fd = xopen (file_path, O_WRONLY | O_TRUNC, 0); + xwrite (fd, "Good day to you too.", strlen ("Good day to you too.")); + xclose (fd); + } + + printf ("info: Attempt O_EXCL creation of existing %s\n", file_path); + /* O_EXCL creation shall fail. */ + errno = 0; + TEST_COMPARE (open64 (file_path, O_RDWR | O_EXCL | O_CREAT, 0600), -1); + TEST_COMPARE (errno, EEXIST); + + free (file_path); + + { + char *new_path = xasprintf ("%s/new", support_fuse_mountpoint (f)); + printf ("info: Test successful O_EXCL creation at %s\n", new_path); + int fd = xopen (new_path, O_RDWR | O_EXCL | O_CREAT, 0600); + xclose (fd); + free (new_path); + } + + { + char *subdir_path = xasprintf ("%s/subdir", support_fuse_mountpoint (f)); + xmkdir (subdir_path, 01234); + } + + { + char *symlink_path = xasprintf ("%s/symlink", support_fuse_mountpoint (f)); + char *target = xreadlink (symlink_path); + TEST_COMPARE_STRING (target, "target-of-symbolic-link"); + free (target); + free (symlink_path); + } + + support_fuse_unmount (f); + return 0; +} + +#include From patchwork Fri Aug 30 19:53:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 1979204 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=UMAEAVQM; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WwTNl4tpCz1yfn for ; Sat, 31 Aug 2024 05:53:43 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id CE7A5386482E for ; Fri, 30 Aug 2024 19:53:41 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTP id 24C59384CB97 for ; Fri, 30 Aug 2024 19:53:13 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 24C59384CB97 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 24C59384CB97 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725047597; cv=none; b=o8G3Jqhc43Vd6c4OAFiWXtV2ZZilCFHT/rQ9Zad1rOQ0EydmYgmmkpxJUCcK4a9+G0KGbpMWol+vre6imr64hoOcxVVlEvTiK3kxdHhbD1H3lA43hIIbF0Fta9CBQ54b1bMffPy5APXXfu6kWVCNynmNEkQQ9uNdZyHlG8OjSMw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725047597; c=relaxed/simple; bh=4ebf3bBNrA3AKdUY24usMShoEfTMuwSI1v2Z6emQtTE=; h=DKIM-Signature:From:To:Subject:Message-ID:Date:MIME-Version; b=CG3AfOjFLOmhHGBKXfSSxPbmR9wmjaipRDxBUgBGd7Ti4MGKVpF+CiwBKulsmx2inOqJSAm/Lluw8Mkm6kH5qis3k8X9Z54nQYo/yQylFvutRdU5Kuj3GUG9CPwfmm1xGjxCvuvOTXIXhzEQGEyNkkqIpMbvZW806hKIXcKmxL0= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1725047592; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=fyOYqdCGQtTmH8woqR1f0tZ+JC+JdaLRmsh/dmy1MQg=; b=UMAEAVQMTHTEy8Hy5ImJM34k7JSpb0rWixhItUM6pa03xoX9cT86Z6S9cOJ13PNC3JmTMg PUxoAZpdWbdhkQNm8JhwrHTwbT3upo00EL8E8GYmxoXR8tVScg0EvATeRwi2yhLXk2ag5f TaiT8XX9RAArPPPS/z/1ErDo46pEFbw= Received: from mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-595-Mn8R1FC3Nia_W4_Chpfc0g-1; Fri, 30 Aug 2024 15:53:11 -0400 X-MC-Unique: Mn8R1FC3Nia_W4_Chpfc0g-1 Received: from mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.15]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id B62471955D4B for ; Fri, 30 Aug 2024 19:53:10 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.2.16.7]) by mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id C06B61955F1B for ; Fri, 30 Aug 2024 19:53:09 +0000 (UTC) From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH v4 06/13] misc: FUSE-based tests for mkstemp In-Reply-To: Message-ID: <125261f96ed1a0ebb7e7dab134c6b17bf8999020.1725047142.git.fweimer@redhat.com> References: X-From-Line: 125261f96ed1a0ebb7e7dab134c6b17bf8999020 Mon Sep 17 00:00:00 2001 Date: Fri, 30 Aug 2024 21:53:06 +0200 User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.15 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-10.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org The tests check that O_EXCL is used properly, that 0600 is used as the mode, that the characters used are as expected, and that the distribution of names generated is reasonably random. The tests run very slowly on some kernel versions, so make them xtests. --- misc/Makefile | 6 + misc/tst-mkstemp-fuse-parallel.c | 208 +++++++++++++++++++++++++++++++ misc/tst-mkstemp-fuse.c | 195 +++++++++++++++++++++++++++++ 3 files changed, 409 insertions(+) create mode 100644 misc/tst-mkstemp-fuse-parallel.c create mode 100644 misc/tst-mkstemp-fuse.c diff --git a/misc/Makefile b/misc/Makefile index 7b7f8351bf..1422c95317 100644 --- a/misc/Makefile +++ b/misc/Makefile @@ -292,6 +292,12 @@ tests-static := tst-empty tests-internal += tst-fd_to_filename tests-static += tst-fd_to_filename +# Tests with long run times. +xtests += \ + tst-mkstemp-fuse \ + tst-mkstemp-fuse-parallel \ + # xtests + ifeq ($(run-built-tests),yes) ifeq (yes,$(build-shared)) ifneq ($(PERL),no) diff --git a/misc/tst-mkstemp-fuse-parallel.c b/misc/tst-mkstemp-fuse-parallel.c new file mode 100644 index 0000000000..71b71f1a42 --- /dev/null +++ b/misc/tst-mkstemp-fuse-parallel.c @@ -0,0 +1,208 @@ +/* FUSE-based test for mkstemp. Parallel collision statistics. + Copyright (C) 2024 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* Enough space to record 4 times the expected number of replies (62**3). + See the processes array in do_test. */ +enum { results_allocated = 4 * 62 * 62 * 62 + 1000 }; + +/* The thread will store the results there. */ +static uint64_t *results; + +/* Currently used part of the results array. */ +static uint64_t results_used; + +/* Fail with EEXIST, but record observed names. */ +static void +fuse_thread (struct support_fuse *f, void *closure) +{ + struct fuse_in_header *inh; + while ((inh = support_fuse_next (f)) != NULL) + { + if (support_fuse_handle_mountpoint (f) + || (inh->nodeid == 1 && support_fuse_handle_directory (f))) + continue; + if (inh->opcode != FUSE_LOOKUP || results_used >= results_allocated) + { + support_fuse_reply_error (f, EIO); + continue; + } + + char *name = support_fuse_cast (LOOKUP, inh); + TEST_COMPARE_BLOB (name, 3, "new", 3); + TEST_COMPARE (strlen (name), 9); + /* Extract 8 bytes of the name: 'w', the X replacements, and the + null terminator. */ + memcpy (&results[results_used], name + 2, 8); + ++results_used; + struct fuse_entry_out *out = support_fuse_prepare_entry (f, 2); + out->attr.mode = S_IFREG | 0600; + support_fuse_reply_prepared (f); + } +} + +/* Used to sort the results array, to find duplicates. */ +static int +results_sort (const void *a1, const void *b1) +{ + const uint64_t *a = a1; + const uint64_t *b = b1; + if (*a < *b) + return -1; + if (*a == *b) + return 0; + return 1; +} + +/* Number of occurrences of certain streak lengths. */ +static size_t streak_lengths[6]; + +/* Called for every encountered streak. */ +static inline void +report_streak (uint64_t current, size_t length) +{ + if (length > 1) + { + printf ("info: name \"ne%.8s\" repeats: %zu\n", + (char *) ¤t, length); + TEST_VERIFY_EXIT (length < array_length (streak_lengths)); + } + ++streak_lengths[length]; +} + +static int +do_test (void) +{ + support_fuse_init (); + + results = xmalloc (results_allocated * sizeof (*results)); + + pid_t processes[4]; + + struct shared + { + /* Used to synchronize the start of all subprocesses, to make it + more likely to expose concurrency-related bugs. */ + pthread_barrier_t barrier1; + pthread_barrier_t barrier2; + + /* Filled in after fork. */ + char mountpoint[PATH_MAX]; + }; + + /* Used to synchronize the start of all subprocesses, to make it + more likely to expose concurrency-related bugs. */ + struct shared *pshared = support_shared_allocate (sizeof (*pshared)); + { + pthread_barrierattr_t attr; + xpthread_barrierattr_init (&attr); + xpthread_barrierattr_setpshared (&attr, PTHREAD_PROCESS_SHARED); + xpthread_barrierattr_destroy (&attr); + xpthread_barrier_init (&pshared->barrier1, &attr, + array_length (processes) + 1); + xpthread_barrier_init (&pshared->barrier2, &attr, + array_length (processes) + 1); + xpthread_barrierattr_destroy (&attr); + } + + for (int i = 0; i < array_length (processes); ++i) + { + processes[i] = xfork (); + if (processes[i] == 0) + { + /* Wait for mountpoint initialization. */ + xpthread_barrier_wait (&pshared->barrier1); + char *path = xasprintf ("%s/newXXXXXX", pshared->mountpoint); + + /* Park this process until all processes have started. */ + xpthread_barrier_wait (&pshared->barrier2); + errno = 0; + TEST_COMPARE (mkstemp (path), -1); + TEST_COMPARE (errno, EEXIST); + free (path); + _exit (0); + } + } + + /* Do this after the forking, to minimize initialization inteference. */ + struct support_fuse *f = support_fuse_mount (fuse_thread, NULL); + TEST_VERIFY (strlcpy (pshared->mountpoint, support_fuse_mountpoint (f), + sizeof (pshared->mountpoint)) + < sizeof (pshared->mountpoint)); + xpthread_barrier_wait (&pshared->barrier1); + + puts ("info: performing mkstemp calls"); + xpthread_barrier_wait (&pshared->barrier2); + + for (int i = 0; i < array_length (processes); ++i) + { + int status; + xwaitpid (processes[i], &status, 0); + TEST_COMPARE (status, 0); + } + + support_fuse_unmount (f); + + puts ("info: checking results"); + qsort (results, results_used, sizeof (*results), results_sort); + + size_t current = -1; + size_t streak = 0; + for (size_t i = 0; i < results_used; ++i) + if (results[i] == current) + ++streak; + else + { + report_streak (current, streak); + current = results[i]; + streak = 1; + } + report_streak (current, streak); + + puts ("info: repetition count distribution:"); + for (int i = 1; i < array_length (streak_lengths); ++i) + printf (" length %d: %zu\n", i, streak_lengths[i]); + /* Some arbitrary threshold, hopefully unlikely enough. Note that + these thresholds are considerably lower than what uniformly + distributed file names, chosen independently, would produce. */ + if (streak_lengths[2] > 20) + FAIL ("unexpectedly common repetition count 2: %zu", streak_lengths[2]); + if (streak_lengths[3] > 1) + FAIL ("unexpectedly common repetition count 3: %zu", streak_lengths[3]); + for (int i = 4; i < array_length (streak_lengths); ++i) + if (streak_lengths[i] > 0) + FAIL ("too many repeats of count %d: %zu", i, streak_lengths[i]); + + return 0; +} + +#include diff --git a/misc/tst-mkstemp-fuse.c b/misc/tst-mkstemp-fuse.c new file mode 100644 index 0000000000..5c4f791072 --- /dev/null +++ b/misc/tst-mkstemp-fuse.c @@ -0,0 +1,195 @@ +/* FUSE-based test for mkstemp. + Copyright (C) 2024 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* Set to true in do_test to cause the first FUSE_CREATE attempt to fail. */ +static _Atomic bool simulate_creat_race; + +/* Basic tests with eventually successful creation. */ +static void +fuse_thread_basic (struct support_fuse *f, void *closure) +{ + char *previous_name = NULL; + int state = 0; + struct fuse_in_header *inh; + while ((inh = support_fuse_next (f)) != NULL) + { + if (support_fuse_handle_mountpoint (f) + || (inh->nodeid == 1 && support_fuse_handle_directory (f))) + continue; + + switch (inh->opcode) + { + case FUSE_LOOKUP: + /* File does not exist initially. */ + TEST_COMPARE (inh->nodeid, 1); + if (simulate_creat_race) + { + if (state < 3) + ++state; + else + FAIL ("invalid state: %d", state); + } + else + { + TEST_COMPARE (state, 0); + state = 3; + } + support_fuse_reply_error (f, ENOENT); + break; + case FUSE_CREATE: + { + TEST_COMPARE (inh->nodeid, 1); + char *name; + struct fuse_create_in *p + = support_fuse_cast_name (CREATE, inh, &name); + /* Name follows after struct fuse_create_in. */ + TEST_COMPARE (p->flags & O_ACCMODE, O_RDWR); + TEST_VERIFY (p->flags & O_EXCL); + TEST_VERIFY (p->flags & O_CREAT); + TEST_COMPARE (p->mode & 07777, 0600); + TEST_VERIFY (S_ISREG (p->mode)); + TEST_COMPARE_BLOB (name, 3, "new", 3); + + if (state != 3 && simulate_creat_race) + { + ++state; + support_fuse_reply_error (f, EEXIST); + } + else + { + if (previous_name != NULL) + /* This test has a very small probability of failure + due to a harmless collision (one in 62**6 tests). */ + TEST_VERIFY (strcmp (name, previous_name) != 0); + TEST_COMPARE (state, 3); + ++state; + struct fuse_entry_out *entry; + struct fuse_open_out *open; + support_fuse_prepare_create (f, 2, &entry, &open); + entry->attr.mode = S_IFREG | 0600; + support_fuse_reply_prepared (f); + } + free (previous_name); + previous_name = xstrdup (name); + } + break; + case FUSE_FLUSH: + case FUSE_RELEASE: + TEST_COMPARE (state, 4); + TEST_COMPARE (inh->nodeid, 2); + support_fuse_reply_empty (f); + break; + default: + support_fuse_reply_error (f, EIO); + } + } + free (previous_name); +} + +/* Reply that all files exist. */ +static void +fuse_thread_eexist (struct support_fuse *f, void *closure) +{ + uint64_t counter = 0; + struct fuse_in_header *inh; + while ((inh = support_fuse_next (f)) != NULL) + { + if (support_fuse_handle_mountpoint (f) + || (inh->nodeid == 1 && support_fuse_handle_directory (f))) + continue; + + switch (inh->opcode) + { + case FUSE_LOOKUP: + ++counter; + TEST_COMPARE (inh->nodeid, 1); + char *name = support_fuse_cast (LOOKUP, inh); + TEST_COMPARE_BLOB (name, 3, "new", 3); + TEST_COMPARE (strlen (name), 9); + for (int i = 3; i <= 8; ++i) + { + /* The glibc implementation uses letters and digits only. */ + char ch = name[i]; + TEST_VERIFY (('0' <= ch && ch <= '9') + || ('a' <= ch && ch <= 'z') + || ('A' <= ch && ch <= 'Z')); + } + struct fuse_entry_out out = + { + .nodeid = 2, + .attr = { + .mode = S_IFREG | 0600, + .ino = 2, + }, + }; + support_fuse_reply (f, &out, sizeof (out)); + break; + default: + support_fuse_reply_error (f, EIO); + } + } + TEST_VERIFY (counter >= 200000); +} + +static int +do_test (void) +{ + support_fuse_init (); + + for (int do_simulate_creat_race = 0; do_simulate_creat_race < 2; + ++do_simulate_creat_race) + { + simulate_creat_race = do_simulate_creat_race; + printf ("info: testing with simulate_creat_race == %d\n", + (int) simulate_creat_race); + struct support_fuse *f = support_fuse_mount (fuse_thread_basic, NULL); + char *path = xasprintf ("%s/newXXXXXX", support_fuse_mountpoint (f)); + int fd = mkstemp (path); + TEST_VERIFY (fd > 2); + xclose (fd); + free (path); + support_fuse_unmount (f); + } + + puts ("info: testing EEXIST failure case for mkstemp"); + { + struct support_fuse *f = support_fuse_mount (fuse_thread_eexist, NULL); + char *path = xasprintf ("%s/newXXXXXX", support_fuse_mountpoint (f)); + errno = 0; + TEST_COMPARE (mkstemp (path), -1); + TEST_COMPARE (errno, EEXIST); + free (path); + support_fuse_unmount (f); + } + + return 0; +} + +#include From patchwork Fri Aug 30 19:53:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 1979211 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=gHTKIAOu; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.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 ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WwTR94rKlz1yfX for ; Sat, 31 Aug 2024 05:55:49 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 82E44386481E for ; Fri, 30 Aug 2024 19:55:47 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTP id 5C289385020E for ; Fri, 30 Aug 2024 19:53:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5C289385020E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 5C289385020E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725047605; cv=none; b=ZZfAd9f0IPOob1TBUcOIulZlkYQmXwgebYirczr14FpAXoK/S7taRhACrjTXNJYrGio+WvLZ2JtSYbt/pKhUlsGJIdGSGf1+1381Kt9NBPq5BQvbjOIu65zEZ7fNOv7E9K78ZqkwERZqJNWYrc+Cw57ySPlRgsV2VdNHWwHvXiQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725047605; c=relaxed/simple; bh=MHVXFC8KQxYbdtKwQ+zRZtEthSXEMCX25vnjoCGEWvg=; h=DKIM-Signature:From:To:Subject:Message-ID:Date:MIME-Version; b=ZRov5GOFTWJshvtIXorPdM7EN+QzW2P5DiYeIzDGHsi2OveMDddmloJItjp6xlhXFA73/mr+Hxw8CRhooH210RvkFRGSQg0OSE6BxlkMuidXMLF/OJfBuOrRkamD6qLHL2u1zWOs9c77L/Xz+0eQ8wbx8Kkpee9GzW+I1YMk2R4= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1725047603; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=BXco6yDFslTQsINyQiDcPoQrgCFt21WBZuFhNwAgpDM=; b=gHTKIAOu5HUjND7QM/6d6I0hF8yGcjUYMPB51qwwEOfS/adQcUPrr7mimBNuAphwUXQtU0 rnAgEU4Fv8RYIHpXjeHsUgUAEd61lc4f2NiXuIQ2+VxIphNJKQ1h1Pxozk/IPdBN9NzY/Y 7DaRJqh9OyTPm6aBk+rbOfFTJivE2xg= Received: from mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-452-Fnzwh1ttN2eJTD1WFDMs9g-1; Fri, 30 Aug 2024 15:53:21 -0400 X-MC-Unique: Fnzwh1ttN2eJTD1WFDMs9g-1 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id D94D81955D50 for ; Fri, 30 Aug 2024 19:53:20 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.2.16.7]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id E17201955F44 for ; Fri, 30 Aug 2024 19:53:19 +0000 (UTC) From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH v4 07/13] io: Add FUSE-based test for fchmod In-Reply-To: Message-ID: References: X-From-Line: c01366b23264a54ac07a1c2cefffb1bc59ee925e Mon Sep 17 00:00:00 2001 Date: Fri, 30 Aug 2024 21:53:16 +0200 User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-10.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org Test all mode arguments, and that extra bits are ignored as required by POSIX. --- io/Makefile | 1 + io/tst-fchmod-fuse.c | 114 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 io/tst-fchmod-fuse.c diff --git a/io/Makefile b/io/Makefile index 47666a1deb..8987dc1375 100644 --- a/io/Makefile +++ b/io/Makefile @@ -189,6 +189,7 @@ tests := \ tst-copy_file_range \ tst-faccessat \ tst-fchmod-errors \ + tst-fchmod-fuse \ tst-fchmodat \ tst-fchownat \ tst-fcntl \ diff --git a/io/tst-fchmod-fuse.c b/io/tst-fchmod-fuse.c new file mode 100644 index 0000000000..fbd3309963 --- /dev/null +++ b/io/tst-fchmod-fuse.c @@ -0,0 +1,114 @@ +/* FUSE-based test for fchmod. + Copyright (C) 2024 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include + +#include +#include +#include +#include +#include +#include + +/* Set from do_test to indicate the expected incoming mode change request. */ +static _Atomic int expected_mode; + +static void +fuse_thread (struct support_fuse *f, void *closure) +{ + struct fuse_in_header *inh; + while ((inh = support_fuse_next (f)) != NULL) + { + if (support_fuse_handle_mountpoint (f) + || (inh->nodeid == 1 && support_fuse_handle_directory (f))) + continue; + switch (inh->opcode) + { + case FUSE_LOOKUP: + { + char *name = support_fuse_cast (LOOKUP, inh); + TEST_COMPARE_STRING (name, "file"); + struct fuse_entry_out *out + = support_fuse_prepare_entry (f, 2); + out->attr.mode = S_IFREG | 0600; + support_fuse_reply_prepared (f); + } + break; + case FUSE_OPEN: + { + TEST_COMPARE (inh->nodeid, 2); + struct fuse_open_in *p = support_fuse_cast (OPEN, inh); + TEST_COMPARE (p->flags & O_ACCMODE, O_RDWR); + struct fuse_open_out out = { 0, }; + support_fuse_reply (f, &out, sizeof (out)); + } + break; + case FUSE_SETATTR: + { + TEST_COMPARE (inh->nodeid, 2); + struct fuse_setattr_in *p = support_fuse_cast (SETATTR, inh); + TEST_COMPARE (p->valid , FATTR_MODE); + TEST_COMPARE (p->mode, S_IFREG | expected_mode); + struct fuse_attr_out *out = support_fuse_prepare_attr (f); + out->attr.mode = S_IFREG | p->mode; + support_fuse_reply_prepared (f); + } + break; + case FUSE_FLUSH: + support_fuse_reply_empty (f); + break; + default: + support_fuse_reply_error (f, EIO); + } + } +} + +/* Test all mode values with the specified extra bits. */ +static void +test_with_bits (int fd, unsigned int extra_bits) +{ + for (int do_mode = 0; do_mode <= 07777; ++do_mode) + { + expected_mode = do_mode; + TEST_COMPARE (fchmod (fd, extra_bits | do_mode), 0); + } +} + +static int +do_test (void) +{ + support_fuse_init (); + + struct support_fuse *f = support_fuse_mount (fuse_thread, NULL); + char *path = xasprintf ("%s/file", support_fuse_mountpoint (f)); + int fd = xopen (path, O_RDWR, 0600); + free (path); + + test_with_bits (fd, 0); + /* POSIX requires that the extra bits are ignored. */ + test_with_bits (fd, S_IFREG); + test_with_bits (fd, S_IFDIR); + test_with_bits (fd, ~07777); + + xclose (fd); + support_fuse_unmount (f); + + return 0; +} + +#include From patchwork Fri Aug 30 19:53:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 1979206 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=CXcoDFzw; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.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 ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WwTPl2yVwz1yfn for ; Sat, 31 Aug 2024 05:54:35 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 415E53865487 for ; Fri, 30 Aug 2024 19:54:33 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTP id 9EC073864C6A for ; Fri, 30 Aug 2024 19:53:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9EC073864C6A Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 9EC073864C6A Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725047611; cv=none; b=q9pGMDPn9wGR3LEGKUv8jN+j43jPfqFVEGTwazXqveWoQ1oaNjyqEGee6N4DMN4GG4h7moCFqBL9TFGS9iWon91V8Q+va7o5/MXyAsPnrJkSVRDn5rRsJ9WCsAE0/FmVEYTegiM9G34vwBW8791tR3GDnFNywwZgq1vm1z4iiCU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725047611; c=relaxed/simple; bh=mDMFGWAwF8oIBwrSZASQd3DeeV6zMGpY90s9ZQRxKJo=; h=DKIM-Signature:From:To:Subject:Message-ID:Date:MIME-Version; b=FZJxi8C8FsUUkybDdDmdiTtl9ySTwCn0G924/vnsA1VCToy4MtCuOBD29Y+RrP6RlgWes8P1aJX0SE3++J8GG71JfOD/VxK82F5KG8KKdb2A3erlsT3WW9fJJ3T0K2l8G1W5vtpK8DPaEmYgDut17nb73RqkgPhuEkuYI6rZhQI= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1725047609; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=CFG4GJsQmk3P+eU/MYxOjBZKtTK0/BuJlD2Wh/7sldA=; b=CXcoDFzwCeH7DLbxr76ndorZsIpVyRiQMgpOIvIMvM6SnLJUiZvlP6CmYBFBXVL7hZBFwl 7qfJUbAaU0wRutmTFZGn+JcEtmHz9BB4vkGrjgSw62ph+t2zmlz1pBVDrXsm+LZcRyiIds rYY2AOVD8eXc4eUBfWAVxB8T+rzyjGo= Received: from mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-352-IIPAN5LdMLar778eq5O7rQ-1; Fri, 30 Aug 2024 15:53:28 -0400 X-MC-Unique: IIPAN5LdMLar778eq5O7rQ-1 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 4A1C01954B02 for ; Fri, 30 Aug 2024 19:53:27 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.2.16.7]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 11C9D1955F44 for ; Fri, 30 Aug 2024 19:53:25 +0000 (UTC) From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH v4 08/13] io: Add tst-lstat-nofollow, tst-lstat-nofollow-time64 In-Reply-To: Message-ID: References: X-From-Line: d26b1cb570caf483d80b8a3e8cc4e7d8df5b1e46 Mon Sep 17 00:00:00 2001 Date: Fri, 30 Aug 2024 21:53:22 +0200 User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-10.5 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_NUMSUBJECT, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org They verify that lstat, lstat64 do not follow symbolic links. --- io/Makefile | 2 + io/tst-lstat-nofollow-time64.c | 1 + io/tst-lstat-nofollow.c | 98 ++++++++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+) create mode 100644 io/tst-lstat-nofollow-time64.c create mode 100644 io/tst-lstat-nofollow.c diff --git a/io/Makefile b/io/Makefile index 8987dc1375..a401ec414e 100644 --- a/io/Makefile +++ b/io/Makefile @@ -209,6 +209,7 @@ tests := \ tst-lchmod \ tst-linkat \ tst-lockf \ + tst-lstat-nofollow \ tst-lutimes \ tst-mkdirat \ tst-mkfifoat \ @@ -238,6 +239,7 @@ tests-time64 := \ tst-futimes-time64\ tst-futimesat-time64 \ tst-lchmod-time64 \ + tst-lstat-nofollow-time64 \ tst-lutimes-time64 \ tst-stat-time64 \ tst-utime-time64 \ diff --git a/io/tst-lstat-nofollow-time64.c b/io/tst-lstat-nofollow-time64.c new file mode 100644 index 0000000000..45feb3f130 --- /dev/null +++ b/io/tst-lstat-nofollow-time64.c @@ -0,0 +1 @@ +#include "tst-lstat-nofollow.c" diff --git a/io/tst-lstat-nofollow.c b/io/tst-lstat-nofollow.c new file mode 100644 index 0000000000..5bbb557c72 --- /dev/null +++ b/io/tst-lstat-nofollow.c @@ -0,0 +1,98 @@ +/* Test that lstat does not follow symbolic links. + Copyright (C) 2024 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include +#include +#include +#include + +static void +fuse_thread (struct support_fuse *f, void *closure) +{ + struct fuse_in_header *inh; + while ((inh = support_fuse_next (f)) != NULL) + { + if (support_fuse_handle_mountpoint (f) + || (inh->nodeid == 1 && support_fuse_handle_directory (f))) + continue; + switch (inh->opcode) + { + case FUSE_LOOKUP: + { + TEST_COMPARE (inh->nodeid, 1); + TEST_COMPARE_STRING (support_fuse_cast (LOOKUP, inh), "symlink"); + struct fuse_entry_out *out = support_fuse_prepare_entry (f, 2); + out->attr.mode = S_IFLNK | 0777; + out->attr.size = strlen ("target"); + support_fuse_reply_prepared (f); + } + break; + case FUSE_GETATTR: + { + TEST_COMPARE (inh->nodeid, 2); + struct fuse_attr_out *out = support_fuse_prepare_attr (f); + out->attr.mode = S_IFLNK | 0777; + out->attr.size = strlen ("target"); + support_fuse_reply_prepared (f); + } + break; + case FUSE_READLINK: + /* The lstat operation must not attempt to look at the + symbolic link target. */ + FAIL ("attempt to obtain target of symblic link for node %llu", + (unsigned long long int) inh->nodeid); + break; + default: + FAIL ("unexpected event %s", support_fuse_opcode (inh->opcode)); + } + } +} + +static int +do_test (void) +{ + support_fuse_init (); + struct support_fuse *f = support_fuse_mount (fuse_thread, NULL); + char *symlink_path = xasprintf ("%s/symlink", support_fuse_mountpoint (f)); + + { + struct stat st = { 0, }; + TEST_COMPARE (lstat (symlink_path, &st), 0); + TEST_COMPARE (st.st_uid, getuid ()); + TEST_COMPARE (st.st_gid, getgid ()); + TEST_COMPARE (st.st_size, 6); + TEST_COMPARE (st.st_mode, S_IFLNK | 0777); + } + + { + struct stat64 st = { 0, }; + TEST_COMPARE (lstat64 (symlink_path, &st), 0); + TEST_COMPARE (st.st_uid, getuid ()); + TEST_COMPARE (st.st_gid, getgid ()); + TEST_COMPARE (st.st_size, 6); + TEST_COMPARE (st.st_mode, S_IFLNK | 0777); + } + + free (symlink_path); + support_fuse_unmount (f); + return 0; +} + +#include From patchwork Fri Aug 30 19:53:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 1979212 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=hw7MrPAB; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.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 ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WwTRw6BSFz1yfX for ; Sat, 31 Aug 2024 05:56:28 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B1C02386482F for ; Fri, 30 Aug 2024 19:56:26 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTP id B60823864C56 for ; Fri, 30 Aug 2024 19:53:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B60823864C56 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org B60823864C56 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725047620; cv=none; b=PNz72+EoxKWh3HEMJ5WKMeLBlPVZ72V6Xj7EUjOn+vXDfWAcPtbD9VMiPS5xhQ4IkBEpdks7XBh2b+enllUm5FdiqG/AmCf9ii5vrpKuqgIrtjeEfDwJwKCMUZoZdDC+iRa2Lz/e38qJ7nJfqhs5J7ztR2bN3Y1ZETpMrq4NGjc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725047620; c=relaxed/simple; bh=B+CF4Skh3/h2FBJSANTlD0rpPWDKt0IYWn1+VWtC0m0=; h=DKIM-Signature:From:To:Subject:Message-ID:Date:MIME-Version; b=J2XZnf4O5/hqARpEoVWDLQbTfYjoo8tGS/Out0F60+dTfMQKm4SsOe9nuQ5GSLvIC1nLZdYjD6mWlsY0yPrYoCXEI8GjoPbUtnsO9+JAvIu8HUK3RRR+QDV/r2iXHn9U8PqOVUolF4sq6lNaN67IzpJl+dUnCTQA3PNyS2Ymrs8= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1725047618; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=OnbKf1XXDRJngSV1GqPbWSoB+spGt4nDFVWmQemtILk=; b=hw7MrPABZHFzPDNsft3bVZcf7V6AJbe4pHAUhPqDEZx7he8BRpXEjUqidQNGraIBG69qYw gyhd7uXF0RiDUQJ9+kSlCr0nhycclttqvGPw5jUf3HSQa81XgHOelu3MwLTXKlWPwD1tK9 QUxfY+vsRCVCNoYxbVXFr6YKDyZQcJs= Received: from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-231-mcc_3jotOZC4QY5fzXfCug-1; Fri, 30 Aug 2024 15:53:36 -0400 X-MC-Unique: mcc_3jotOZC4QY5fzXfCug-1 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 2C6D11955F03 for ; Fri, 30 Aug 2024 19:53:36 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.2.16.7]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 13D883001FEF for ; Fri, 30 Aug 2024 19:53:34 +0000 (UTC) From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH v4 09/13] dirent: Add tst-closedir-leaks In-Reply-To: Message-ID: References: X-From-Line: c507e98237dc4cec6da0536eed3ac8f13697468f Mon Sep 17 00:00:00 2001 Date: Fri, 30 Aug 2024 21:53:31 +0200 User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-10.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org It verfies that closedir deallocates memory and closes file descriptors. --- dirent/Makefile | 20 ++++++++++ dirent/tst-closedir-leaks.c | 76 +++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 dirent/tst-closedir-leaks.c diff --git a/dirent/Makefile b/dirent/Makefile index 556f759f65..f9056724f0 100644 --- a/dirent/Makefile +++ b/dirent/Makefile @@ -58,6 +58,7 @@ tests := \ bug-readdir1 \ list \ opendir-tst1 \ + tst-closedir-leaks \ tst-fdopendir \ tst-fdopendir2 \ tst-scandir \ @@ -65,6 +66,18 @@ tests := \ tst-seekdir \ # tests +ifeq ($(run-built-tests),yes) +ifneq ($(PERL),no) +generated += \ + $(objpfx)tst-closedir-leaks-mem.out \ + # generated + +tests-special += \ + $(objpfx)tst-closedir-leaks-mem.out \ + # tests-special +endif # $(PERL) ! no +endif # $(run-built-tests) == yes + CFLAGS-scandir.c += $(uses-callbacks) CFLAGS-scandir64.c += $(uses-callbacks) CFLAGS-scandir-tail.c += $(uses-callbacks) @@ -74,3 +87,10 @@ CFLAGS-dirfd.c += $(config-cflags-wno-ignored-attributes) include ../Rules opendir-tst1-ARGS = --test-dir=${common-objpfx}dirent + +tst-closedir-leaks-ENV += MALLOC_TRACE=$(objpfx)tst-closedir-leaks.mtrace \ + LD_PRELOAD=$(common-objpfx)/malloc/libc_malloc_debug.so + +$(objpfx)tst-closedir-leaks-mem.out: $(objpfx)tst-closedir-leaks.out + $(common-objpfx)malloc/mtrace $(objpfx)tst-closedir-leaks.mtrace > $@; \ + $(evaluate-test) diff --git a/dirent/tst-closedir-leaks.c b/dirent/tst-closedir-leaks.c new file mode 100644 index 0000000000..7a26ddaa81 --- /dev/null +++ b/dirent/tst-closedir-leaks.c @@ -0,0 +1,76 @@ +/* Test for resource leaks in closedir. + Copyright (C) 2024 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static void +one_test (enum support_readdir_op op, unsigned int read_limit, + bool use_fdopendir) +{ + struct support_descriptors *fds = support_descriptors_list (); + struct support_dirent e = { 0, }; + + DIR *stream; + if (use_fdopendir) + { + int fd = xopen (".", O_RDONLY | O_DIRECTORY, 0); + stream = xfdopendir (fd); + /* The descriptor fd will be closed by closedir below. */ + } + else + stream = xopendir ("."); + for (unsigned int i = 0; i < read_limit; ++i) + if (!support_readdir (stream, op, &e)) + break; + TEST_COMPARE (closedir (stream), 0); + + free (e.d_name); + support_descriptors_check (fds); + support_descriptors_free (fds); +} + +static int +do_test (void) +{ + mtrace (); + + for (int use_fdopendir = 0; use_fdopendir < 2; ++use_fdopendir) + { + /* No reads, operation does not matter. */ + one_test (SUPPORT_READDIR, 0, use_fdopendir); + + for (enum support_readdir_op op = 0; op <= support_readdir_op_last(); + ++op) + { + one_test (op, 1, use_fdopendir); + one_test (op, -1, use_fdopendir); /* Unlimited reads. */ + } + } + + return 0; +} + +#include From patchwork Fri Aug 30 19:53:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 1979205 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=fEV0BFo7; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WwTPV0JJ0z1yfn for ; Sat, 31 Aug 2024 05:54:22 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4401C386484A for ; Fri, 30 Aug 2024 19:54:20 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTP id 1CDFD385E011 for ; Fri, 30 Aug 2024 19:53:49 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1CDFD385E011 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 1CDFD385E011 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725047635; cv=none; b=NnyBVCJcDo2hY5dBTtSXhrVL8S7OiqmxRcm7eqg4N6GLm6CoztoaA9cpq3xbNzhUiHFRgmtUck8tBIIgLN/TATEe8dPFt9fShPtp0kDl0CTjIDO1XeRKl2wCVq6EX90teYZVo2w//Mp/4WS9GQZM6dOAbQSdKeLY/sER8QS+HRU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725047635; c=relaxed/simple; bh=1irihp2opem/UKLe12Qaao0QPwH78iag2XhMfaUuTks=; h=DKIM-Signature:From:To:Subject:Message-ID:Date:MIME-Version; b=Dle76xtSVg8WDzNEqL7nN9H5/M0RpP8IKewLXwTknaEu/7KSnVyVcXuaDAhtgYCghLo3mUyo7n6oJsplqiQDC2vfdiFm/BFgsZFGttgSP/ilb7JZGQ9ruIn0zKaLesxQwlWjIL++hbMCo8yqT0vVv5w1UeTOIQdcmCjOJVO/+zo= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1725047628; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=A3Xs12h0eCIpV2TBdrixH1RJ0h8Qb41SKWOmO92HVYc=; b=fEV0BFo7Ks4gzDgsHpRkFWauMTKafSx8PRW/yFkQw9D8UwuJCk8+U53BUJi86WOeMOxFKa ub89IqebfTzaAFFjnN7XJ2/F5Z260whsP5qEHG76ViUpcTF68bYC24WOTX6Roi9jHK6sL2 igruCXCnrfBxUwVXSx85IDFiOA/olxg= Received: from mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-180-BswrGmcgNxWpAPNmd-O3Lg-1; Fri, 30 Aug 2024 15:53:47 -0400 X-MC-Unique: BswrGmcgNxWpAPNmd-O3Lg-1 Received: from mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.40]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id A57371955BFA for ; Fri, 30 Aug 2024 19:53:46 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.2.16.7]) by mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 8741B19560A3 for ; Fri, 30 Aug 2024 19:53:45 +0000 (UTC) From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH v4 10/13] Linux: Use readdir64_r for compat __old_readdir64_r (bug 32128) In-Reply-To: Message-ID: <0327ddcf78bd8bf81676b8da050ccc05aa4839de.1725047142.git.fweimer@redhat.com> References: X-From-Line: 0327ddcf78bd8bf81676b8da050ccc05aa4839de Mon Sep 17 00:00:00 2001 Date: Fri, 30 Aug 2024 21:53:42 +0200 User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.40 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-10.8 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_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org It is not necessary to do the conversion at the getdents64 layer for readdir64_r. Doing int piecewise for readdir64 is slightly simpler and allows deleting __old_getdents64. This fixes bug 32128 because readdir64_r handles the length check correctly. --- sysdeps/unix/sysv/linux/getdents64.c | 97 -------------------------- sysdeps/unix/sysv/linux/olddirent.h | 2 - sysdeps/unix/sysv/linux/readdir64.c | 90 +++++++++++++----------- sysdeps/unix/sysv/linux/readdir64_r.c | 98 ++++++--------------------- 4 files changed, 71 insertions(+), 216 deletions(-) diff --git a/sysdeps/unix/sysv/linux/getdents64.c b/sysdeps/unix/sysv/linux/getdents64.c index 227fbf21ae..795bd935f0 100644 --- a/sysdeps/unix/sysv/linux/getdents64.c +++ b/sysdeps/unix/sysv/linux/getdents64.c @@ -33,100 +33,3 @@ __getdents64 (int fd, void *buf, size_t nbytes) } libc_hidden_def (__getdents64) weak_alias (__getdents64, getdents64) - -#if _DIRENT_MATCHES_DIRENT64 -strong_alias (__getdents64, __getdents) -#else -# include - -# if SHLIB_COMPAT(libc, GLIBC_2_1, GLIBC_2_2) -# include -# include - -static ssize_t -handle_overflow (int fd, __off64_t offset, ssize_t count) -{ - /* If this is the first entry in the buffer, we can report the - error. */ - if (offset == 0) - { - __set_errno (EOVERFLOW); - return -1; - } - - /* Otherwise, seek to the overflowing entry, so that the next call - will report the error, and return the data read so far. */ - if (__lseek64 (fd, offset, SEEK_SET) != 0) - return -1; - return count; -} - -ssize_t -__old_getdents64 (int fd, char *buf, size_t nbytes) -{ - /* We do not move the individual directory entries. This is only - possible if the target type (struct __old_dirent64) is smaller - than the source type. */ - _Static_assert (offsetof (struct __old_dirent64, d_name) - <= offsetof (struct dirent64, d_name), - "__old_dirent64 is larger than dirent64"); - _Static_assert (__alignof__ (struct __old_dirent64) - <= __alignof__ (struct dirent64), - "alignment of __old_dirent64 is larger than dirent64"); - - ssize_t retval = INLINE_SYSCALL_CALL (getdents64, fd, buf, nbytes); - if (retval > 0) - { - /* This is the marker for the first entry. Offset 0 is reserved - for the first entry (see rewinddir). Here, we use it as a - marker for the first entry in the buffer. We never actually - seek to offset 0 because handle_overflow reports the error - directly, so it does not matter that the offset is incorrect - if entries have been read from the descriptor before (so that - the descriptor is not actually at offset 0). */ - __off64_t previous_offset = 0; - - char *p = buf; - char *end = buf + retval; - while (p < end) - { - struct dirent64 *source = (struct dirent64 *) p; - - /* Copy out the fixed-size data. */ - __ino_t ino = source->d_ino; - __off64_t offset = source->d_off; - unsigned int reclen = source->d_reclen; - unsigned char type = source->d_type; - - /* Check for ino_t overflow. */ - if (__glibc_unlikely (ino != source->d_ino)) - return handle_overflow (fd, previous_offset, p - buf); - - /* Convert to the target layout. Use a separate struct and - memcpy to side-step aliasing issues. */ - struct __old_dirent64 result; - result.d_ino = ino; - result.d_off = offset; - result.d_reclen = reclen; - result.d_type = type; - - /* Write the fixed-sized part of the result to the - buffer. */ - size_t result_name_offset = offsetof (struct __old_dirent64, d_name); - memcpy (p, &result, result_name_offset); - - /* Adjust the position of the name if necessary. Copy - everything until the end of the record, including the - terminating NUL byte. */ - if (result_name_offset != offsetof (struct dirent64, d_name)) - memmove (p + result_name_offset, source->d_name, - reclen - offsetof (struct dirent64, d_name)); - - p += reclen; - previous_offset = offset; - } - } - return retval; -} -# endif /* SHLIB_COMPAT(libc, GLIBC_2_1, GLIBC_2_2) */ -#endif /* _DIRENT_MATCHES_DIRENT64 */ diff --git a/sysdeps/unix/sysv/linux/olddirent.h b/sysdeps/unix/sysv/linux/olddirent.h index 239f790648..065ca41a6e 100644 --- a/sysdeps/unix/sysv/linux/olddirent.h +++ b/sysdeps/unix/sysv/linux/olddirent.h @@ -34,8 +34,6 @@ extern struct __old_dirent64 *__old_readdir64 (DIR *__dirp); libc_hidden_proto (__old_readdir64); 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) - attribute_hidden; int __old_scandir64 (const char * __dir, struct __old_dirent64 *** __namelist, int (*__selector) (const struct __old_dirent64 *), diff --git a/sysdeps/unix/sysv/linux/readdir64.c b/sysdeps/unix/sysv/linux/readdir64.c index e6f5108c0a..e6b8867b7a 100644 --- a/sysdeps/unix/sysv/linux/readdir64.c +++ b/sysdeps/unix/sysv/linux/readdir64.c @@ -26,17 +26,13 @@ #undef __readdir #undef readdir -/* Read a directory entry from DIRP. */ -struct dirent64 * -__readdir64 (DIR *dirp) +/* Read a directory entry from DIRP. No locking. */ +static struct dirent64 * +__readdir64_unlocked (DIR *dirp) { struct dirent64 *dp; int saved_errno = errno; -#if IS_IN (libc) - __libc_lock_lock (dirp->lock); -#endif - if (dirp->offset >= dirp->size) { /* We've emptied out our buffer. Refill it. */ @@ -53,9 +49,6 @@ __readdir64 (DIR *dirp) do not set errno in that case, to indicate success. */ if (bytes == 0 || errno == ENOENT) __set_errno (saved_errno); -#if IS_IN (libc) - __libc_lock_unlock (dirp->lock); -#endif return NULL; } dirp->size = (size_t) bytes; @@ -68,10 +61,16 @@ __readdir64 (DIR *dirp) dirp->offset += dp->d_reclen; dirp->filepos = dp->d_off; -#if IS_IN (libc) - __libc_lock_unlock (dirp->lock); -#endif + return dp; +} +/* Read a directory entry from DIRP. */ +struct dirent64 * +__readdir64 (DIR *dirp) +{ + __libc_lock_lock (dirp->lock); + struct dirent64 *dp = __readdir64_unlocked (dirp); + __libc_lock_unlock (dirp->lock); return dp; } libc_hidden_def (__readdir64) @@ -99,45 +98,54 @@ __old_readdir64 (DIR *dirp) struct __old_dirent64 *dp; int saved_errno = errno; -#if IS_IN (libc) __libc_lock_lock (dirp->lock); -#endif - if (dirp->offset >= dirp->size) + while (1) { - /* We've emptied out our buffer. Refill it. */ + errno = 0; + struct dirent64 *newdp = __readdir64_unlocked (dirp); + if (newdp == NULL) + { + if (errno == 0 && dirp->errcode != 0) + __set_errno (dirp->errcode); + else if (errno == 0) + __set_errno (saved_errno); + dp = NULL; + break; + } - size_t maxread = dirp->allocation; - ssize_t bytes; + /* Convert to the target layout. Use a separate struct and + memcpy to side-step aliasing issues. */ + struct __old_dirent64 result; + result.d_ino = newdp->d_ino; + result.d_off = newdp->d_off; + result.d_reclen = newdp->d_reclen; + result.d_type = newdp->d_type; - bytes = __old_getdents64 (dirp->fd, dirp->data, maxread); - if (bytes <= 0) + /* Check for ino_t overflow. */ + if (__glibc_unlikely (result.d_ino != newdp->d_ino)) { - /* Linux may fail with ENOENT on some file systems if the - directory inode is marked as dead (deleted). POSIX - treats this as a regular end-of-directory condition, so - do not set errno in that case, to indicate success. */ - if (bytes == 0 || errno == ENOENT) - __set_errno (saved_errno); -#if IS_IN (libc) - __libc_lock_unlock (dirp->lock); -#endif - return NULL; + dirp->errcode = ENAMETOOLONG; + continue; } - dirp->size = (size_t) bytes; - /* Reset the offset into the buffer. */ - dirp->offset = 0; - } + /* Overwrite the fixed-sized part. */ + dp = (struct __old_dirent64 *) newdp; + memcpy (dp, &result, offsetof (struct __old_dirent64, d_name)); - dp = (struct __old_dirent64 *) &dirp->data[dirp->offset]; - dirp->offset += dp->d_reclen; - dirp->filepos = dp->d_off; + /* Move the name. */ + _Static_assert (offsetof (struct __old_dirent64, d_name) + <= offsetof (struct dirent64, d_name), + "old struct must be smaller"); + if (offsetof (struct __old_dirent64, d_name) + != offsetof (struct dirent64, d_name)) + memmove (dp->d_name, newdp->d_name, strlen (newdp->d_name) + 1); -#if IS_IN (libc) - __libc_lock_unlock (dirp->lock); -#endif + __set_errno (saved_errno); + break; + } + __libc_lock_unlock (dirp->lock); return dp; } libc_hidden_def (__old_readdir64) diff --git a/sysdeps/unix/sysv/linux/readdir64_r.c b/sysdeps/unix/sysv/linux/readdir64_r.c index e87882ee06..7ad7e5945b 100644 --- a/sysdeps/unix/sysv/linux/readdir64_r.c +++ b/sysdeps/unix/sysv/linux/readdir64_r.c @@ -135,91 +135,37 @@ attribute_compat_text_section __old_readdir64_r (DIR *dirp, struct __old_dirent64 *entry, struct __old_dirent64 **result) { - 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; + struct dirent64 new_entry; + struct dirent64 *newp; + int ret = __readdir64_r (dirp, &new_entry, &newp); - dp = NULL; - break; - } - dirp->size = (size_t) bytes; - - /* Reset the offset into the buffer. */ - dirp->offset = 0; + if (ret != 0) + return ret; + else if (newp == NULL) + { + *result = NULL; + return 0; } - - dp = (struct __old_dirent64 *) &dirp->data[dirp->offset]; - - reclen = dp->d_reclen; - - dirp->offset += reclen; - - dirp->filepos = dp->d_off; - - if (reclen > offsetof (struct __old_dirent64, d_name) + NAME_MAX + 1) + else { - /* 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 + entry->d_ino = newp->d_ino; + if (entry->d_ino != newp->d_ino) { - /* The name is too long. Ignore this file. */ - dirp->errcode = ENAMETOOLONG; - dp->d_ino = 0; + dirp->errcode = EOVERFLOW; continue; } + size_t namelen = strlen (newp->d_name); + entry->d_off = newp->d_off; + entry->d_reclen = (offsetof (struct __old_dirent64, d_name) + + namelen + 1); + entry->d_type = newp->d_type; + memcpy (entry->d_name, newp->d_name, namelen + 1); + *result = entry; + return 0; } - - /* Skip deleted and ignored files. */ - } - 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; - } - - __libc_lock_unlock (dirp->lock); - - return ret; } compat_symbol (libc, __old_readdir64_r, readdir64_r, GLIBC_2_1); From patchwork Fri Aug 30 19:53:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 1979209 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=EreBPbxV; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.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 ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WwTQF1LFrz1ygJ for ; Sat, 31 Aug 2024 05:55:01 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 098E13864836 for ; Fri, 30 Aug 2024 19:54:59 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTP id 3330F385020E for ; Fri, 30 Aug 2024 19:53:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3330F385020E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 3330F385020E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725047641; cv=none; b=uQmO5GSXiHM2qNRosPG1j3tNRUMlHhQ571Vc8PyQkBSSEgxDl3xXXu4yL5/Rzemd+N+11roqckh+6BvuZ2O09E+4DZZDFggj0Wn9sbo8E/TdRgRVZF67g26Jis6tRkw0bvhRfmxZgIjgyOgjofZQsIoRdad74zFmzrbNwDFt1ls= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725047641; c=relaxed/simple; bh=Kabzf115xjrDGj9V3dl4i3TvQPcKB+kVIGzeIQwQ79c=; h=DKIM-Signature:From:To:Subject:Message-ID:Date:MIME-Version; b=SlKs4T7DgMs37g/qFApWYY8ZikQTgau8H1dtX/FdOZfReSik2FZ+ElbXH32+AeNwnyHqSLImQGyVF1o0y6Rr7V8JHStyOQAVRPsDUjLnSgoccVv4TJ/aRRo1ppxnFhOS3U1TvgGKpz6xRWxy45sovudD1dKunDhjpQfddyAiYI8= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1725047637; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=QOa9Wn6CMTbI80+htIqJSBS8Miv/9p+xavHdkFRKNg8=; b=EreBPbxV5X48GEi3MlKPpHU1xZ0/rnFuaSiSqfE43d3PszZadGhRTtL0Y7SsSv3l8pdNyJ jabTm8KnWop7pHzLbuMB43gPvYCOF7VIjcaAR6kjzeBVX0A1wXj0gtFsh29bKy0V4RLz6f s2G3eCaEwNZn8mjwWtor+3dhJZpijuI= Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-447--pnhUjilONC9TWzr5wh73g-1; Fri, 30 Aug 2024 15:53:56 -0400 X-MC-Unique: -pnhUjilONC9TWzr5wh73g-1 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id D18181955D4C for ; Fri, 30 Aug 2024 19:53:55 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.2.16.7]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id CE91F19560AA for ; Fri, 30 Aug 2024 19:53:54 +0000 (UTC) From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH v4 11/13] dirent: Add tst-readdir-long In-Reply-To: Message-ID: <4405f6fbaec624b13324d695907995ebb2bdf127.1725047142.git.fweimer@redhat.com> References: X-From-Line: 4405f6fbaec624b13324d695907995ebb2bdf127 Mon Sep 17 00:00:00 2001 Date: Fri, 30 Aug 2024 21:53:51 +0200 User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-10.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org It tests long names and ENAMETOOLONG handling, specifically for readdir_r. This is a regression test for bug 14699, bug 32124, and bug 32128. --- dirent/Makefile | 1 + dirent/tst-readdir-long.c | 231 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 232 insertions(+) create mode 100644 dirent/tst-readdir-long.c diff --git a/dirent/Makefile b/dirent/Makefile index f9056724f0..91edcf9e70 100644 --- a/dirent/Makefile +++ b/dirent/Makefile @@ -61,6 +61,7 @@ tests := \ tst-closedir-leaks \ tst-fdopendir \ tst-fdopendir2 \ + tst-readdir-long \ tst-scandir \ tst-scandir64 \ tst-seekdir \ diff --git a/dirent/tst-readdir-long.c b/dirent/tst-readdir-long.c new file mode 100644 index 0000000000..ec48e12300 --- /dev/null +++ b/dirent/tst-readdir-long.c @@ -0,0 +1,231 @@ +/* Test readdir (+variants) behavior with file names of varying length. + Copyright (C) 2024 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* If positive, at this length an EMSGSIZE error is injected. */ +static int inject_error_at_length; + +/* Return a file name, LENGTH bytes long. */ +static char * +name_of_length (size_t length) +{ + char *result = xmalloc (length + 1); + unsigned int prefix = snprintf (result, length + 1, "%zu-", length); + for (size_t i = prefix; i < length; ++i) + result[i] = 'A' + ((length + i) % 26); + result[length] = '\0'; + return result; +} + +/* Add the directory entry at OFFSET to the stream D. */ +static uint64_t +add_directory_entry (struct support_fuse_dirstream *d, uint64_t offset) +{ + unsigned int length = offset + 1; + if (length > 1000) + /* Longer than what is possible to produce with 256 + UTF-8-encoded Unicode code points. */ + return 0; + + char *to_free = NULL; + const char *name; + uint64_t ino = 1000 + length; /* Arbitrary value, distinct from 1. */ + uint32_t type = DT_REG; + if (offset <= 1) + { + type = DT_DIR; + name = ".." + !offset; /* "." or "..". */ + ino = 1; + } + else if (length == 1000) + name = "short"; + else + { + to_free = name_of_length (length); + name = to_free; + } + + ++offset; + bool added = support_fuse_dirstream_add (d, ino, offset, type, name); + free (to_free); + if (added) + return offset; + else + return 0; +} + +/* Set to true if getdents64 should produce only one entry. */ +static bool one_entry_per_getdents64; + +static void +fuse_thread (struct support_fuse *f, void *closure) +{ + struct fuse_in_header *inh; + while ((inh = support_fuse_next (f)) != NULL) + { + if (support_fuse_handle_mountpoint (f) + || (inh->nodeid == 1 && support_fuse_handle_directory (f))) + continue; + switch (inh->opcode) + { + case FUSE_READDIR: + if (inh->nodeid == 1) + { + uint64_t offset = support_fuse_cast (READ, inh)->offset; + if (inject_error_at_length == offset + 1) + support_fuse_reply_error (f, EMSGSIZE); + else + { + struct support_fuse_dirstream *d + = support_fuse_prepare_readdir (f); + while (true) + { + offset = add_directory_entry (d, offset); + if (offset == 0 || one_entry_per_getdents64 + /* Error will be reported at next READDIR. */ + || offset + 1 == inject_error_at_length) + break; + } + support_fuse_reply_prepared (f); + } + } + else + support_fuse_reply_error (f, EIO); + break; + default: + FAIL ("unexpected event %s", support_fuse_opcode (inh->opcode)); + support_fuse_reply_error (f, EIO); + } + } +} + +/* Run the tests for the specified readdir variant OP. */ +static void +run_readdir_tests (struct support_fuse *f, enum support_readdir_op op) +{ + printf ("info: testing %s (inject_error=%d unbuffered=%d)\n", + support_readdir_function (op), inject_error_at_length, + (int) one_entry_per_getdents64); + + bool testing_r = support_readdir_r_variant (op); + + DIR *dir = xopendir (support_fuse_mountpoint (f)); + struct support_dirent e = { 0, }; + TEST_VERIFY (support_readdir (dir, op, &e)); + TEST_COMPARE (e.d_ino, 1); + TEST_COMPARE_STRING (e.d_name, "."); + + TEST_VERIFY (support_readdir (dir, op, &e)); + TEST_COMPARE (e.d_ino, 1); + TEST_COMPARE_STRING (e.d_name, ".."); + + for (unsigned int i = 3; i < 1000; ++i) + { + if (i == inject_error_at_length) + /* Error expected below. */ + break; + + if (i >= sizeof ((struct dirent) { 0, }.d_name) && testing_r) + /* This is a readir_r test. The longer names are not + available because they do not fit into struct dirent. */ + break; + + char *expected_name = name_of_length (i); + TEST_COMPARE (strlen (expected_name), i); + TEST_VERIFY (support_readdir (dir, op, &e)); + TEST_COMPARE (e.d_ino, 1000 + i); + TEST_COMPARE_STRING (e.d_name, expected_name); + free (expected_name); + } + + if (inject_error_at_length == 0) + { + /* Check that the ENAMETOOLONG error does not prevent reading a + later short name. */ + TEST_VERIFY (support_readdir (dir, op, &e)); + TEST_COMPARE (e.d_ino, 2000); + TEST_COMPARE_STRING (e.d_name, "short"); + + if (testing_r) + /* An earlier name was too long. */ + support_readdir_expect_error (dir, op, ENAMETOOLONG); + else + /* Entire directory read without error. */ + TEST_VERIFY (!support_readdir (dir, op, &e)); + } + else + support_readdir_expect_error (dir, op, EMSGSIZE); + + free (e.d_name); + xclosedir (dir); +} + +/* Run all readdir variants for both fully-buffered an unbuffered + (one-at-a-time) directory streams. */ +static void +run_fully_buffered_and_singleton_buffers (struct support_fuse *f) +{ + for (int do_one_entry = 0; do_one_entry < 2; ++do_one_entry) + { + one_entry_per_getdents64 = do_one_entry; + for (enum support_readdir_op op = 0; op <= support_readdir_op_last(); + ++op) + run_readdir_tests (f, op); + } +} + +static int +do_test (void) +{ + /* Smoke test for name_of_length. */ + { + char *name = name_of_length (5); + TEST_COMPARE_STRING (name, "5-HIJ"); + free (name); + + name = name_of_length (6); + TEST_COMPARE_STRING (name, "6-IJKL"); + free (name); + } + + support_fuse_init (); + struct support_fuse *f = support_fuse_mount (fuse_thread, NULL); + + run_fully_buffered_and_singleton_buffers (f); + + inject_error_at_length = 100; + run_fully_buffered_and_singleton_buffers (f); + + inject_error_at_length = 300; + run_fully_buffered_and_singleton_buffers (f); + + support_fuse_unmount (f); + return 0; +} + +#include From patchwork Fri Aug 30 19:53:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 1979207 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=AjFJOA6/; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WwTQ24Sgpz1yfn for ; Sat, 31 Aug 2024 05:54:50 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D0DDF3864849 for ; Fri, 30 Aug 2024 19:54:48 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTP id 439B83864802 for ; Fri, 30 Aug 2024 19:54:06 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 439B83864802 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 439B83864802 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725047649; cv=none; b=XR1NeODpGxuAP+No39Pv5hBr9LJ88IjipbXWBoMLplAaOOvEN5TI+Mn8GVRtsCnhpfLmLk92ntGJLxWBeXpu9qc0HjoB2wA7B+mFQ9a8YNhgkJwjJXXez+wSH7yFDuB66/3kOjDpqjFpeAwVECHhNPpH5ogWl5twdNYLB01zr60= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725047649; c=relaxed/simple; bh=ncI6J16USlt4b3U/TYj4as0UYxebCuASFgLZ7Muwli8=; h=DKIM-Signature:From:To:Subject:Message-ID:Date:MIME-Version; b=cH7j/fbDqbw0oO/1yuBIdykDWVM2U3E8wlZWChpqU4SgjR0UcDhxzDV0asCE0/1DDOqzvRZ/X8frlOffzp4k4PSK7ZCK+iQyX7cRGPhflgTdHLElFKavXSS5ure3CH1pqvkMtCLiJlH/RmTxiHnp6D+4tZcA1GQUJdAB1uEHrl0= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1725047646; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=m1kdaoEnYjLyJpAN2h7tD416zBsTgGwVjNbm8XjkniA=; b=AjFJOA6/5ftoHMp6sTsFSD6yBJNbM2miczyeiA4Essj6tqM6WJZGPxpTl6dAD+fpcvcuhn pSkZbC/JU7RSiKlXSHzDqmwL3Er6JT/viL0eugoVp1GkhRqYCig3blRfYKK5ZJu9DsTux2 fvUCXmmyK9ISzYnNITbEEKbmaWza25M= Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-262-qRMEdsLHNZ-8VQj7lPRrsQ-1; Fri, 30 Aug 2024 15:54:04 -0400 X-MC-Unique: qRMEdsLHNZ-8VQj7lPRrsQ-1 Received: from mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.40]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id DC3031955D5A for ; Fri, 30 Aug 2024 19:54:03 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.2.16.7]) by mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id E3BA519560A3 for ; Fri, 30 Aug 2024 19:54:02 +0000 (UTC) From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH v4 12/13] dirent: Add tst-rewinddir In-Reply-To: Message-ID: <846df93101d4187b432d2fe25f4c1377a3b032d8.1725047142.git.fweimer@redhat.com> References: X-From-Line: 846df93101d4187b432d2fe25f4c1377a3b032d8 Mon Sep 17 00:00:00 2001 Date: Fri, 30 Aug 2024 21:53:59 +0200 User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.40 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-10.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org It verifies that rewinddir allows restarting the directory iteration. --- dirent/Makefile | 1 + dirent/tst-rewinddir.c | 207 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 208 insertions(+) create mode 100644 dirent/tst-rewinddir.c diff --git a/dirent/Makefile b/dirent/Makefile index 91edcf9e70..045c786575 100644 --- a/dirent/Makefile +++ b/dirent/Makefile @@ -62,6 +62,7 @@ tests := \ tst-fdopendir \ tst-fdopendir2 \ tst-readdir-long \ + tst-rewinddir \ tst-scandir \ tst-scandir64 \ tst-seekdir \ diff --git a/dirent/tst-rewinddir.c b/dirent/tst-rewinddir.c new file mode 100644 index 0000000000..1479766ebe --- /dev/null +++ b/dirent/tst-rewinddir.c @@ -0,0 +1,207 @@ +/* Test for rewinddir, using FUSE. + Copyright (C) 2024 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include +#include +#include +#include +#include +#include + +/* Return the file name at the indicated directory offset. */ +static char * +name_at_offset (unsigned int offset) +{ + if (offset <= 1) + return xstrdup (".." + !offset); /* "." or "..". */ + else + /* Pad the name with a lot of zeros, so that the dirent buffer gets + filled more quickly. */ + return xasprintf ("file%0240u", offset); +} + +/* This many directory entries, including "." and "..". */ +enum { directory_entries = 200 }; + +/* Add the directory entry at OFFSET to the stream D. */ +static uint64_t +add_directory_entry (struct support_fuse_dirstream *d, uint64_t offset) +{ + if (offset >= directory_entries) + return 0; + + char *name = name_at_offset (offset); + uint64_t ino = 1000 + offset; /* Arbitrary value, distinct from 1. */ + uint32_t type = DT_REG; + if (offset <= 1) + { + type = DT_DIR; + ino = 1; + } + + ++offset; + bool added = support_fuse_dirstream_add (d, ino, offset, type, name); + free (name); + if (added) + return offset; + else + return 0; +} + +/* Set to true if getdents64 should produce only one entry. */ +static bool one_entry_per_getdents64; + +static void +fuse_thread (struct support_fuse *f, void *closure) +{ + struct fuse_in_header *inh; + while ((inh = support_fuse_next (f)) != NULL) + { + if (support_fuse_handle_mountpoint (f) + || (inh->nodeid == 1 && support_fuse_handle_directory (f))) + continue; + switch (inh->opcode) + { + case FUSE_READDIR: + if (inh->nodeid == 1) + { + uint64_t offset = support_fuse_cast (READ, inh)->offset; + struct support_fuse_dirstream *d + = support_fuse_prepare_readdir (f); + while (true) + { + offset = add_directory_entry (d, offset); + if (offset == 0 || one_entry_per_getdents64) + break; + } + support_fuse_reply_prepared (f); + } + else + support_fuse_reply_error (f, EIO); + break; + default: + FAIL ("unexpected event %s", support_fuse_opcode (inh->opcode)); + support_fuse_reply_error (f, EIO); + } + } +} + +/* Lists the entire directory from start to end. */ +static void +verify_directory (DIR *dir, enum support_readdir_op op) +{ + struct support_dirent e = { 0, }; + + TEST_VERIFY (support_readdir (dir, op, &e)); + TEST_COMPARE_STRING (e.d_name, "."); + TEST_VERIFY (support_readdir (dir, op, &e)); + TEST_COMPARE_STRING (e.d_name, ".."); + for (int i = 2; i < directory_entries; ++i) + { + char *expected = name_at_offset (i); + TEST_VERIFY (support_readdir (dir, op, &e)); + TEST_COMPARE_STRING (e.d_name, expected); + free (expected); + } + TEST_VERIFY (!support_readdir (dir, op, &e)); + free (e.d_name); +} + +/* Run tests with rewinding after ENTRIES readdir calls. */ +static void +rewind_after (unsigned int rewind_at) +{ + for (enum support_readdir_op op = 0; op <= support_readdir_op_last (); ++op) + { + printf ("info: testing %s (rewind_at=%u)\n", + support_readdir_function (op), rewind_at); + + struct support_fuse *f = support_fuse_mount (fuse_thread, NULL); + DIR *dir = xopendir (support_fuse_mountpoint (f)); + struct support_dirent e = { 0, }; + + switch (rewind_at) + { + case 0: + break; + case 1: + TEST_VERIFY (support_readdir (dir, op, &e)); + TEST_COMPARE_STRING (e.d_name, "."); + break; + default: + TEST_VERIFY (support_readdir (dir, op, &e)); + TEST_COMPARE_STRING (e.d_name, "."); + TEST_VERIFY (support_readdir (dir, op, &e)); + TEST_COMPARE_STRING (e.d_name, ".."); + for (int i = 2; i < directory_entries; ++i) + { + if (i == rewind_at) + break; + char *expected = name_at_offset (i); + TEST_VERIFY (support_readdir (dir, op, &e)); + TEST_COMPARE_STRING (e.d_name, expected); + free (expected); + } + break; + } + + errno = 0; + rewinddir (dir); + TEST_COMPARE (errno, 0); + verify_directory (dir, op); + + free (e.d_name); + xclosedir (dir); + support_fuse_unmount (f); + } +} + +static int +do_test (void) +{ + support_fuse_init (); + + /* One pass without rewinding to verify that the generated directory + content matches expectations. */ + { + struct support_fuse *f = support_fuse_mount (fuse_thread, NULL); + DIR *dir = xopendir (support_fuse_mountpoint (f)); + verify_directory (dir, SUPPORT_READDIR64); + xclosedir (dir); + support_fuse_unmount (f); + } + + for (int do_unbuffered = 0; do_unbuffered < 2; ++do_unbuffered) + { + one_entry_per_getdents64 = do_unbuffered; + + for (int i = 0; i < 20; ++i) + rewind_after (i); + rewind_after (50); + rewind_after (100); + rewind_after (150); + rewind_after (180); + rewind_after (199); + } + + return 0; +} + +#include From patchwork Fri Aug 30 19:54:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 1979210 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=cl18ozuG; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.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 ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WwTQs1S0sz1yfX for ; Sat, 31 Aug 2024 05:55:33 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id F0F2E384CB97 for ; Fri, 30 Aug 2024 19:55:30 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTP id 254383864C65 for ; Fri, 30 Aug 2024 19:54:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 254383864C65 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 254383864C65 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725047660; cv=none; b=i8/HggZ+FIOcJB6HrVlUZ2J6o0gEITdDI0kid136expRD24onVHkGUHDacjNT7TsS3GBgdz394T+X2nsRtl8aoVb8CUzDFCCJcCUB3srPVs+oxZ0aVsAqvkONB8NthgYnwdZDo8x2Iit8qRNUa1yW+QrZbECVHJ7qk7JHp/C/Ug= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725047660; c=relaxed/simple; bh=I2UpkLyDBXVg/2owbfovqlOOPx3S/eTluHjleeOsCIY=; h=DKIM-Signature:From:To:Subject:Message-ID:Date:MIME-Version; b=Uv/eTNEHE+l0ft7FXWTOvuSjNgfo1MRSiqpXGOUyYe+GFFctAAAGqtR3mbRRlFwSU/eVR43NLc151I+Rd2RrZNFA6h3m+mmLGZ56DcS28NH5UJ27oPS/Mcptong6cNP+9/SixFWKw5pxX5NIOR5KPifU8WXh+9aJ++E0WL6ouPw= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1725047654; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=WkY5qh2VSSde1qnM3XMF2jh0Hm80GjuOubvs+/3DojY=; b=cl18ozuGVP0sEL/v9vmVbHfGSEpO2Rdb/pIA8GNIhg3sWcKQuI97+9I8wa16P85SsSGW3G 5q21VhNdkRj4rlvCsPbkwDD1p0QX5x3B8w5756gAop9H3SnWm61pqvRBTMHcZnQ3H4hvRt qjMsESKLBcbX2lpsdJCZaZXqXw3CFqQ= Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-529-WNUoHJgaOx6vSrAwMKJtwA-1; Fri, 30 Aug 2024 15:54:13 -0400 X-MC-Unique: WNUoHJgaOx6vSrAwMKJtwA-1 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 93E0E1955D53 for ; Fri, 30 Aug 2024 19:54:12 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.2.16.7]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 5225819560AA for ; Fri, 30 Aug 2024 19:54:11 +0000 (UTC) From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH v4 13/13] Linux: readdir64_r should not skip d_ino == 0 entries (bug 32126) In-Reply-To: Message-ID: <45939a6bcc66e746a672a92a72e12e46b772490b.1725047142.git.fweimer@redhat.com> References: X-From-Line: 45939a6bcc66e746a672a92a72e12e46b772490b Mon Sep 17 00:00:00 2001 Date: Fri, 30 Aug 2024 21:54:08 +0200 User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-10.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org This is the same bug as bug 12165, but for readdir_r. The regression test covers both bug 12165 and bug 32126. --- dirent/Makefile | 1 + dirent/tst-readdir-zero-inode.c | 134 ++++++++++++++++++++++++++ sysdeps/unix/sysv/linux/readdir64_r.c | 29 +++--- 3 files changed, 147 insertions(+), 17 deletions(-) create mode 100644 dirent/tst-readdir-zero-inode.c diff --git a/dirent/Makefile b/dirent/Makefile index 045c786575..11b772e3ab 100644 --- a/dirent/Makefile +++ b/dirent/Makefile @@ -62,6 +62,7 @@ tests := \ tst-fdopendir \ tst-fdopendir2 \ tst-readdir-long \ + tst-readdir-zero-inode \ tst-rewinddir \ tst-scandir \ tst-scandir64 \ diff --git a/dirent/tst-readdir-zero-inode.c b/dirent/tst-readdir-zero-inode.c new file mode 100644 index 0000000000..af9fb946ab --- /dev/null +++ b/dirent/tst-readdir-zero-inode.c @@ -0,0 +1,134 @@ +/* Test that readdir does not skip entries with d_ino == 0 (bug 12165). + Copyright (C) 2024 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include +#include +#include + +/* Add the directory entry at OFFSET to the stream D. */ +static uint64_t +add_directory_entry (struct support_fuse_dirstream *d, uint64_t offset) +{ + bool added = false; + ++offset; + switch (offset - 1) + { + case 0: + added = support_fuse_dirstream_add (d, 1, offset, DT_DIR, "."); + break; + case 1: + added = support_fuse_dirstream_add (d, 1, offset, DT_DIR, ".."); + break; + case 2: + added = support_fuse_dirstream_add (d, 2, offset, DT_REG, "before"); + break; + case 3: + added = support_fuse_dirstream_add (d, 0, offset, DT_REG, "zero"); + break; + case 4: + added = support_fuse_dirstream_add (d, 3, offset, DT_REG, "after"); + break; + } + if (added) + return offset; + else + return 0; +} + +/* Set to true if getdents64 should produce only one entry. */ +static bool one_entry_per_getdents64; + +static void +fuse_thread (struct support_fuse *f, void *closure) +{ + struct fuse_in_header *inh; + while ((inh = support_fuse_next (f)) != NULL) + { + if (support_fuse_handle_mountpoint (f) + || (inh->nodeid == 1 && support_fuse_handle_directory (f))) + continue; + switch (inh->opcode) + { + case FUSE_READDIR: + if (inh->nodeid == 1) + { + uint64_t offset = support_fuse_cast (READ, inh)->offset; + struct support_fuse_dirstream *d + = support_fuse_prepare_readdir (f); + while (true) + { + offset = add_directory_entry (d, offset); + if (offset == 0 || one_entry_per_getdents64) + break; + } + support_fuse_reply_prepared (f); + } + else + support_fuse_reply_error (f, EIO); + break; + default: + FAIL ("unexpected event %s", support_fuse_opcode (inh->opcode)); + support_fuse_reply_error (f, EIO); + } + } +} + +static int +do_test (void) +{ + support_fuse_init (); + + for (enum support_readdir_op op = 0; op <= support_readdir_op_last (); ++op) + { + struct support_fuse *f = support_fuse_mount (fuse_thread, NULL); + DIR *dir = xopendir (support_fuse_mountpoint (f)); + struct support_dirent e = { 0, }; + + TEST_VERIFY (support_readdir (dir, op, &e)); + TEST_COMPARE_STRING (e.d_name, "."); + TEST_COMPARE (e.d_ino, 1); + + TEST_VERIFY (support_readdir (dir, op, &e)); + TEST_COMPARE_STRING (e.d_name, ".."); + TEST_COMPARE (e.d_ino, 1); + + TEST_VERIFY (support_readdir (dir, op, &e)); + TEST_COMPARE_STRING (e.d_name, "before"); + TEST_COMPARE (e.d_ino, 2); + + TEST_VERIFY (support_readdir (dir, op, &e)); + TEST_COMPARE_STRING (e.d_name, "zero"); + TEST_COMPARE (e.d_ino, 0); + + TEST_VERIFY (support_readdir (dir, op, &e)); + TEST_COMPARE_STRING (e.d_name, "after"); + TEST_COMPARE (e.d_ino, 3); + + TEST_VERIFY (!support_readdir (dir, op, &e)); + + free (e.d_name); + xclosedir (dir); + support_fuse_unmount (f); + } + + return 0; +} + +#include diff --git a/sysdeps/unix/sysv/linux/readdir64_r.c b/sysdeps/unix/sysv/linux/readdir64_r.c index 7ad7e5945b..c42a161ffc 100644 --- a/sysdeps/unix/sysv/linux/readdir64_r.c +++ b/sysdeps/unix/sysv/linux/readdir64_r.c @@ -37,7 +37,7 @@ __readdir64_r (DIR *dirp, struct dirent64 *entry, struct dirent64 **result) __libc_lock_lock (dirp->lock); - do + while (1) { if (dirp->offset >= dirp->size) { @@ -79,26 +79,21 @@ __readdir64_r (DIR *dirp, struct dirent64 *entry, struct dirent64 **result) dirp->filepos = dp->d_off; - if (reclen > offsetof (struct dirent64, d_name) + NAME_MAX + 1) + if (reclen <= offsetof (struct dirent64, d_name) + NAME_MAX + 1) + break; + + /* 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 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; } - while (dp->d_ino == 0); if (dp != NULL) {