From patchwork Mon Jul 29 06:18:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 94681 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A2C0F3858403 for ; Mon, 29 Jul 2024 06:19:21 +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 8D28B3858C60 for ; Mon, 29 Jul 2024 06:18:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8D28B3858C60 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 8D28B3858C60 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=1722233934; cv=none; b=R5Kt3W4FAiSuINMkInjakKhjwMcSFALHudtjFtEskbwYLIthsS89EM4pU0whdB/7pEgik++e549EKLpYEUxtkou97EUrc3a6CKlaIPyEDqnzTZPbJWrp8fKFPW6grLqHrS15xVjhU3lUiMbP+lVAHZGXh1sMyYCcMYJMy++Cd9w= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1722233934; c=relaxed/simple; bh=usPY9YxEij08MPSJ5vgKTYT+EoIYMxW4mUeX2bovxKk=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=UzmMc0+jHMyou4Ju7O25/H6mUkPjimpXGILIWYBgkhiRqAk34K3PiswuwOSA0iiOiAsSVAXVgHzh1UDVXP7JZR3OxACXwiLdxBIVZduHH8DFi4GuGeNK1uNWEk3xUSvXi9a3pzsAWihJvjlEUp7yXZiZUhtO5nIREol0uwGrhs0= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1722233922; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type; bh=/hblqCnSsTBAUnlgiDtcBjz8VB0JW5+5utTiihg5pKM=; b=XSryvQhiPHHR0OxWzOxzq6oBcQ70dNpczvh8GjwFjSPgZcqP2J7hh7rh/3oPI1AtTp1Z2W WPRKmNpI0x3VqBQJuldt29JUEgvzxUwv9GQf36wt8sP44b9GnJBn685nDn2S9tXnmCL/Lh SFPZq+GVrCdgGrRor4o93J8R6w/o6cI= 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-501-WJCIFEVmMP6F_CBjJ9StmA-1; Mon, 29 Jul 2024 02:18:40 -0400 X-MC-Unique: WJCIFEVmMP6F_CBjJ9StmA-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 BE3CB1955D44 for ; Mon, 29 Jul 2024 06:18:39 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.45.224.31]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id BBEB41955F40 for ; Mon, 29 Jul 2024 06:18:38 +0000 (UTC) From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH v5] manual: Add Descriptor-Relative Access section Date: Mon, 29 Jul 2024 08:18:35 +0200 Message-ID: <87plqwsouc.fsf@oldenburg.str.redhat.com> 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.9 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 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~patchwork=sourceware.org@sourceware.org From: Florian Weimer And document the functions openat, openat64, fstatat, fstatat64. (The safety assessment for fstatat was already obsolete because current glibc assumes kernel support for the underlying system call.) --- manual/filesys.texi | 189 ++++++++++++++++++++++++++++++++++++++++++++++++++-- manual/llio.texi | 28 ++++++++ manual/startup.texi | 8 ++- 3 files changed, 218 insertions(+), 7 deletions(-) base-commit: 32328a5a1461ff88c0b1e04954e9c68b3fa7f56d diff --git a/manual/filesys.texi b/manual/filesys.texi index 47d929744e..8419cb31f5 100644 --- a/manual/filesys.texi +++ b/manual/filesys.texi @@ -15,6 +15,7 @@ access permissions and modification times. @menu * Working Directory:: This is used to resolve relative file names. +* Descriptor-Relative Access:: Ways to control file name lookup. * Accessing Directories:: Finding out what files a directory contains. * Working with Directory Trees:: Apply actions to all files or a selectable @@ -89,7 +90,7 @@ zero, then the buffer is that large; otherwise, the buffer is as large as necessary to hold the result. The return value is @var{buffer} on success and a null pointer on failure. -The following @code{errno} error conditions are defined for this function: +The following @code{errno} error conditions are defined: @table @code @item EINVAL @@ -206,6 +207,151 @@ An I/O error occurred. @end table @end deftypefun +@node Descriptor-Relative Access +@section Descriptor-Relative Access +@cindex file name resolution based on descriptors +@cindex descriptor-based file name resolution +@cindex @code{@dots{}at} functions + +Many functions that accept file names have @code{@dots{}at} variants +which accept a file descriptor and a file name argument instead of just +a file name argument. For example, @code{fstatat} is the +descriptor-based variant of the @code{fstat} function. Most such +functions also accept an additional flags argument which changes the +behavior of the file name lookup based on the passed @code{AT_@dots{}} +flags. + +There are several reasons to use descriptor-relative access: + +@itemize @bullet +@item +The working directory is a process-wide resource, so individual threads +cannot change it without affecting other threads in the process. +Explicitly specifying the directory against which relative paths are +resolved can be a thread-safe alternative to changing the working +directory. + +@item +If a progrem wishes to access a directory tree which is being modified +concurrently, perhaps even by a different user on the system, the +program must avoid looking up file names with multiple components, in +order to detect symbolic links, using the @code{O_NOFOLLOW} flag +(@pxref{Open-time Flags}) or the @code{AT_SYMLINK_FOLLOW} flag +(described below). Without directory-relative access, it is necessary +to use the @code{fchdir} function to change the working directory +(@pxref{Working Directory}), which is not thread-safe. + +@item +Listing directory contents using the @code{readdir} or @code{readdir64} +functions (@pxref{Reading/Closing Directory}) does not provide full file +name paths. Using @code{@dots{}at} functions, it is possible to use the +file names directly, without having to construct such full paths. + +@item +Additional flags available with some of the @code{@dots{}at} functions +provide access to functionality which is not available otherwise. +@end itemize + +The file descriptor used by these @code{@dots{}at} functions has the +following uses: + +@itemize @bullet +@item +It can be a file descriptor referring to a directory. Such a descriptor +can be created explicitly using the @code{open} function and the +@code{O_RDONLY} file access mode, with or without the @code{O_DIRECTORY} +flag. @xref{Opening and Closing Files}. Or it can be created +implicitly by @code{opendir} and retrieved using the @code{dirfd} +function. @xref{Opening a Directory}. + +If a directory descriptor is used with one of the @code{@dots{}at} +functions, a relative file name argument is resolved relative to +directory referred to by the file descriptor, just as if that directory +were the current working directory. Absolute file name arguments +(starting with @samp{/}) are resolved against the file system root, and +the descriptor argument is effectively ignored. + +This means that file name lookup is not constrained to the directory of +the descriptor. For example, it is possible to access a file +@file{example} in the descriptor's parent directory using a file name +argument @code{"../example"}, or in the root directory using +@code{"/example"}. + +If the file descriptor refers to a directory, the empty string @code{""} +is not a valid file name argument. It is possible to use @code{"."} to +refer to the directory itself. Also see @code{AT_EMPTY_PATH} below. + +@item +@vindex @code{AT_FDCWD} +The special value @code{AT_FDCWD}. This means that the current working +directory is used for the lookup if the file name is a relative. For +@code{@dots{}at} functions with an @code{AT_@dots{}} flags argument, +this provides a shortcut to use those flags with regular (not +descriptor-based) file name lookups. + +If @code{AT_FDCWD} is used, the empty string @code{""} is not a valid +file name argument. + +@item +An arbitrary file descriptor, along with an empty string @code{""} as +the file name argument, and the @code{AT_EMPTY_PATH} flag. In this +case, the operation uses the file descriptor directly, without further +file name resolution. On Linux, this allows operations on descriptors +opened with the @code{O_PATH} flag. For regular descriptors (opened +without @code{O_PATH}), the same functionality is also available through +the plain descriptor-based functions (for example, @code{fstat} instead +of @code{fstatat}). + +This is a GNU extension. +@end itemize + +@cindex file name resolution flags +@cindex @code{AT_*} file name resolution flags +The flags argument in @code{@dots{}at} functions can be a combination of +the following flags, defined in @file{fcntl.h}. Not all such functions +support all flags, and some (such as @code{openat}) do not accept a +flags argument at all. + +In the flag descriptions below, the @dfn{effective final path component} +refers to the final component (basename) of the full path constructed +from the descriptor and file name arguments, using file name lookup, as +described above. + +@vtable @code +@item AT_EMPTY_PATH +This flag is used with an empty file name @code{""} and a descriptor +which does not necessarily refer to a directory. It is most useful with +@code{O_PATH} descriptors, as described above. This flag is a GNU +extension. + +@item AT_NO_AUTOMOUNT +If the effective final path component refers to a potential file system +mount point controlled by an auto-mounting service, the operation does +not trigger auto-mounting and refers to the unmounted mount point +instead. @xref{Mount-Unmount-Remount}. If a file system has already +been mounted at the effective final path component, the operation +applies to the file or directory in the mounted file system, not the +underlying file system that was mounted over. This flag is a GNU +extension. + +@item AT_SYMLINK_FOLLOW +If the effective final path component is a symbolic link, the +operation follows the symbolic link and operates on its target. (For +most functions, this is the default behavior.) + +@item AT_SYMLINK_NOFOLLOW +If the effective final path component is a symbolic link, the +operation operates on the symbolic link, without following it. The +difference in behavior enabled by this flag is similar to the difference +between the @code{lstat} and @code{stat} functions, or the behavior +activated by the @code{O_NOFOLLOW} argument to the @code{open} function. +Even with the @code{AT_SYMLINK_NOFOLLOW} flag present, symbolic links in +a non-final component of the file name are still followed. +@end vtable + +There is no relationship between these flags and the type argument to +the @code{getauxval} function (with @code{AT_@dots{}} constants defined +in @file{elf.h}). @xref{Auxiliary Vector}. @node Accessing Directories @section Accessing Directories @@ -1250,10 +1396,11 @@ A hardware error occurred while trying to read or write the to filesystem. The @code{linkat} function is analogous to the @code{link} function, except that it identifies its source and target using a combination of a -file descriptor (referring to a directory) and a pathname. If a -pathnames is not absolute, it is resolved relative to the corresponding -file descriptor. The special file descriptor @code{AT_FDCWD} denotes -the current directory. +file descriptor (referring to a directory) and a file name. +@xref{Descriptor-Relative Access}. For @code{linkat}, if a file name is +not absolute, it is resolved relative to the corresponding file +descriptor. As usual, the special value @code{AT_FDCWD} denotes the +current directory. The @var{flags} argument is a combination of the following flags: @@ -2095,6 +2242,38 @@ replaces the interface for small files on 32-bit machines. @c available. @c @safety{@mtsafe{}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}} + +@deftypefun int fstatat (int @var{filedes}, const char *@var{filename}, struct stat *@var{buf}, int @var{flags}) +@standards{POSIX.1, sys/stat.h} +@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} +This function is a descriptor-relative version of the @code{fstat} +function above. @xref{Descriptor-Relative Access}. The @var{flags} +argument can contain a combination of the flags @code{AT_EMPTY_PATH}, +@code{AT_NO_AUTOMOUNT}, @code{AT_SYMLINK_NOFOLLOW}. + +Compared to @code{fstat}, the following additional error conditions can +occur: + +@table @code +@item EBADF +The @var{filedes} argument is not a valid file descriptor. + +@item EINVAL +The @var{flags} argument is not valid for this function. + +@item ENOTDIR +The descriptor @var{filedes} is not associated with a directory, and +@var{filename} is a relative file name. +@end table +@end deftypefun + +@deftypefun int fstatat64 (int @var{filedes}, const char *@var{filename}, struct stat64 *@var{buf}, int @var{flags}) +@standards{GNU, sys/stat.h} +@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} +This function is the large-file variant of @code{fstatat}, similar to +how @code{fstat64} is the variant of @code{fstat}. +@end deftypefun + @deftypefun int lstat (const char *@var{filename}, struct stat *@var{buf}) @standards{BSD, sys/stat.h} @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} diff --git a/manual/llio.texi b/manual/llio.texi index 6f0a48609b..f1962a050a 100644 --- a/manual/llio.texi +++ b/manual/llio.texi @@ -181,6 +181,34 @@ new, extended API using 64 bit file sizes and offsets transparently replaces the old API. @end deftypefun +@deftypefun int openat (int @var{filedes}, const char *@var{filename}, int @var{flags}[, mode_t @var{mode}]) +@standards{POSIX.1, fcntl.h} +@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{@acsfd{}}} +This function is the descriptor-relative variant of the @code{open} +function. @xref{Descriptor-Relative Access}. + +Note that the @var{flags} argument of @code{openat} does not accept +@code{AT_@dots{}} flags, only the flags described for the @code{open} +function above. + +The @code{openat} function can fail for additional reasons: + +@table @code +@item EBADF +The @var{filedes} argument is not a valid file descriptor. + +@item ENOTDIR +The descriptor @var{filedes} is not associated with a directory, and +@var{filename} is a relative file name. +@end table +@end deftypefun + +@deftypefun int openat64 (int @var{filedes}, const char *@var{filename}, int @var{flags}[, mode_t @var{mode}]) +@standards{GNU, fcntl.h} +The large-file variant of the @code{openat}, similar to how +@code{open64} is the large-file variant of @code{open}. +@end deftypefun + @deftypefn {Obsolete function} int creat (const char *@var{filename}, mode_t @var{mode}) @standards{POSIX.1, fcntl.h} @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{@acsfd{}}} diff --git a/manual/startup.texi b/manual/startup.texi index 747beed4d9..ab53604078 100644 --- a/manual/startup.texi +++ b/manual/startup.texi @@ -664,8 +664,12 @@ basis there may be information that is not available any other way. This function is used to inquire about the entries in the auxiliary vector. The @var{type} argument should be one of the @samp{AT_} symbols defined in @file{elf.h}. If a matching entry is found, the value is -returned; if the entry is not found, zero is returned and @code{errno} is -set to @code{ENOENT}. +returned; if the entry is not found, zero is returned and @code{errno} +is set to @code{ENOENT}. + +There is no relationship between the @samp{AT_} contants defined in +@file{elf.h} and the file name lookup flags in @file{fcntl.h}. +@xref{Descriptor-Relative Access}. @end deftypefun For some platforms, the key @code{AT_HWCAP} is the easiest way to inquire