From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS3215 2.6.0.0/16 X-Spam-Status: No, score=-4.2 required=3.0 tests=AWL,BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,SPF_HELO_PASS,SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by dcvr.yhbt.net (Postfix) with ESMTPS id 963611F8C6 for ; Wed, 7 Jul 2021 10:23:06 +0000 (UTC) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 7824E389367A for ; Wed, 7 Jul 2021 10:23:05 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7824E389367A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1625653385; bh=YVkXW3pVZ51K3diuLC0LU85JsMTA+j/MBxE+TNq9OeE=; h=To:Subject:References:Date:In-Reply-To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=XNKzGgEhhjlEF91aYaQM1dQKVKCFb/CI00aVF5/2UvHsBZIdqL401UUpsxuGzeN0i tR5LM/xmr7NB0ADBOgRCro7b/RTifOGySzOfpfU9Rs8i1AncqlN0M4RP24S/P6TazC NF4nusV0Jix0nbfdYM62NuhIpSVQm0bHxVL9fjWA= 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 1377B385702F for ; Wed, 7 Jul 2021 10:22:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 1377B385702F Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-450-Ugpzx3yFO-iEHco6LRF89g-1; Wed, 07 Jul 2021 06:22:43 -0400 X-MC-Unique: Ugpzx3yFO-iEHco6LRF89g-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 857AA362FA; Wed, 7 Jul 2021 10:22:42 +0000 (UTC) Received: from oldenburg.str.redhat.com (ovpn-115-5.ams2.redhat.com [10.36.115.5]) by smtp.corp.redhat.com (Postfix) with ESMTPS id C45F05C1D5; Wed, 7 Jul 2021 10:22:41 +0000 (UTC) To: Adhemerval Zanella via Libc-alpha Subject: Re: [PATCH v7 2/4] linux: Add close_range References: <20210706145839.1658623-1-adhemerval.zanella@linaro.org> <20210706145839.1658623-3-adhemerval.zanella@linaro.org> Date: Wed, 07 Jul 2021 12:22:39 +0200 In-Reply-To: <20210706145839.1658623-3-adhemerval.zanella@linaro.org> (Adhemerval Zanella via Libc-alpha's message of "Tue, 6 Jul 2021 11:58:37 -0300") Message-ID: <87fswqa0eo.fsf@oldenburg.str.redhat.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Florian Weimer via Libc-alpha Reply-To: Florian Weimer Errors-To: libc-alpha-bounces+e=80x24.org@sourceware.org Sender: "Libc-alpha" * Adhemerval Zanella via Libc-alpha: > diff --git a/manual/llio.texi b/manual/llio.texi > index eafc27120d..ea6d34dd5a 100644 > --- a/manual/llio.texi > +++ b/manual/llio.texi > @@ -284,6 +284,55 @@ of trying to close its underlying file descriptor with @code{close}. > This flushes any buffered output and updates the stream object to > indicate that it is closed. > > +@deftypefun int close_range (unsigned int @var{lowfd}, unsigned int @var{maxfd}, int @var{flags}) > +@standards{Linux, unistd.h} > +@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{@acsfd{}}} > +@c This is a syscall for Linux v5.9. There is no fallback emulation for > +@c older kernels. > + > +The function @code{close_range} closes the file descriptor from @var{lowfd} > +to @var{maxfd} (inclusive). This function is similar to call @code{close} in > +specified file descriptor range depending on the @var{flags}. > + > +This is function is only supported on recent Linux versions and @theglibc{} > +does not provide any fallback (the application will need to handle possible > +@code{ENOSYS}). > + > +The @var{flags} add options on how the files are closes. Linux currently > +supports: > + > +@vtable @code > +@item CLOSE_RANGE_UNSHARE > +Unshare the file descriptor table before closing file descriptors. > + > +@item CLOSE_RANGE_CLOEXEC > +Set the @code{FD_CLOEXEC} bit instead of closing the file descriptor. > +@end vtable > + > +The normal return value from @code{close_range} is @math{0}; a value > +of @math{-1} is returned in case of failure. The following @code{errno} error > +conditions are defined for this function: > + > +@table @code > +@item EINVAL > +The @var{lowfd} value is larger than @var{maxfd} or an unsupported @var{flags} > +is used. > + > +@item ENOMEM > +Either there is not enough memory for the operation, or the process is > +out of address space. I think the address space limitation does not apply here, and this error can only happen for CLOSE_RANGE_UNSHARE. This is important information because plain close cannot fail, either. No possibility of failure is a requirement in many cases to use this interface. > +@item EMFILE > +The process has too many files open. > +The maximum number of file descriptors is controlled by the > +@code{RLIMIT_NOFILE} resource limit; @pxref{Limits on Resources}. Can EMFILE (or ENFILE) really happen even with CLOSE_RANGE_UNSHARE? I don't think so. > diff --git a/sysdeps/unix/sysv/linux/bits/unistd_ext.h b/sysdeps/unix/sysv/linux/bits/unistd_ext.h > index 2e529be577..bf313e8af8 100644 > --- a/sysdeps/unix/sysv/linux/bits/unistd_ext.h > +++ b/sysdeps/unix/sysv/linux/bits/unistd_ext.h > @@ -33,4 +33,26 @@ > not detached and has not been joined. */ > extern __pid_t gettid (void) __THROW; > > +#ifdef __has_include > +# if __has_include ("linux/close_range.h") > +# include "linux/close_range.h" > +# endif > #endif > +/* Unshare the file descriptor table before closing file descriptors. */ > +#ifndef CLOSE_RANGE_UNSHARE > +# define CLOSE_RANGE_UNSHARE (1U << 1) > +#endif > +/* Set the FD_CLOEXEC bit instead of closing the file descriptor. */ > +#ifndef CLOSE_RANGE_CLOEXEC > +# define CLOSE_RANGE_CLOEXEC (1U << 2) > +#endif > + > +/* Close all file descriptors in the range FD up to MAX_FD. The flag FLAGS > + are define by the CLOSE_RANGE prefix. This function behaves like close > + on the range, but in a fail-safe where it will either fail and not close > + any file descriptor or close all of them. Returns 0 on successor or -1 > + for failure (and sets errno accordingly). */ > +extern int close_range (unsigned int __fd, unsigned int __max_fd, > + int __flags) __THROW; The fail-safe aspect is not mentioned in the manual. It's confused because it's unclear what happens with gaps. Thanks, Florian