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: AS22989 209.51.188.0/24 X-Spam-Status: No, score=-3.7 required=3.0 tests=AWL,BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dcvr.yhbt.net (Postfix) with ESMTPS id 76E691F55B for ; Mon, 11 May 2020 16:38:37 +0000 (UTC) Received: from localhost ([::1]:34620 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jYBRs-0001tP-9I for normalperson@yhbt.net; Mon, 11 May 2020 12:38:36 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:54838) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jYBQS-0007uI-W6 for bug-gnulib@gnu.org; Mon, 11 May 2020 12:37:09 -0400 Received: from mo6-p00-ob.smtp.rzone.de ([2a01:238:20a:202:5300::10]:12728) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jYBQR-0001D0-4s for bug-gnulib@gnu.org; Mon, 11 May 2020 12:37:08 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; t=1589215021; s=strato-dkim-0002; d=clisp.org; h=References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: X-RZG-CLASS-ID:X-RZG-AUTH:From:Subject:Sender; bh=X0jqBKUrtArAi/eAnRVVyFFe3+clgnRZ12GKzVjm+FY=; b=gPP7pr0Af5QwYCPZ3+QJpq1CDsOyZvKFJYSMVvLsB2fug1nM2EeV1SznWeOD76YrjN 04augnw0E7eh3SfPvTMYylyAyL8MWll6M6JZvgfPlfCfWlWD4lEwkk7xIsSLFeCnoOV/ 28p5rHfTCM5n9PvY3AdFx5JdwK6oz7B3tgU0oeIQy9Nx/HXRSvawuuyjNKODCrHS0D5Q g3U6qFklWbRiz9mDLccxO6hMCIsLc04tzTLCkMvIxOsQoOGvYuyVRNl9JDbOaqrOKRgt pEJSyZDRQDiTmLbFwD+733P/T9Yltf/y/e2x9VSGEFin9yG/T8LvvFbsd2z6F57UA/Bq Py8g== X-RZG-AUTH: ":Ln4Re0+Ic/6oZXR1YgKryK8brlshOcZlIWs+iCP5vnk6shH+AHjwLuWOH6fzxfs=" X-RZG-CLASS-ID: mo00 Received: from bruno.haible.de by smtp.strato.de (RZmta 46.6.2 DYNA|AUTH) with ESMTPSA id j093d3w4BGb1UJe (using TLSv1 with cipher ECDHE-RSA-AES256-SHA (curve X9_62_prime256v1 with 256 ECDH bits, eq. 3072 bits RSA)) (Client did not present a certificate); Mon, 11 May 2020 18:37:01 +0200 (CEST) From: Bruno Haible To: bug-gnulib@gnu.org Subject: Re: portability of fopen and 'e' (O_CLOEXEC) flag Date: Mon, 11 May 2020 18:37:00 +0200 Message-ID: <21479440.KlRyoMc3gv@omega> User-Agent: KMail/5.1.3 (Linux/4.4.0-177-generic; KDE/5.18.0; x86_64; ; ) In-Reply-To: <7d7cfa6e-af75-82d0-e076-bf411b87a3ab@gmx.de> References: <7d7cfa6e-af75-82d0-e076-bf411b87a3ab@gmx.de> MIME-Version: 1.0 Content-Transfer-Encoding: 7Bit Content-Type: text/plain; charset="us-ascii" Received-SPF: none client-ip=2a01:238:20a:202:5300::10; envelope-from=bruno@clisp.org; helo=mo6-p00-ob.smtp.rzone.de X-detected-operating-system: by eggs.gnu.org: No matching host in p0f cache. That's all we know. X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_PASS=-0.001, URIBL_BLOCKED=0.001 autolearn=_AUTOLEARN X-Spam_action: no action X-BeenThere: bug-gnulib@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Gnulib discussion list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Tim =?ISO-8859-1?Q?R=FChsen?= Errors-To: bug-gnulib-bounces+normalperson=yhbt.net@gnu.org Sender: "bug-gnulib" Hi Tim, > i would like to ask for your expert knowledge. > > How to prevent file descriptor leaks in a multi-threaded application > that fork+exec. Quick answer is surely "use O_CLOEXEC" to close those > file descriptors on exec. > > But how does this work with fopen in a portable way ? > GNU libc has the 'e' flag for exactly this. Yes [1]. > How about other non-GNU OSes / alternative C libraries ? POSIX [2][3], macOS [4], FreeBSD [5], Solaris [6] don't support this 'e' flag. How about using open() with O_CLOEXEC, and then fdopen()? Bruno [1] http://man7.org/linux/man-pages/man3/fopen.3.html [2] https://pubs.opengroup.org/onlinepubs/9699919799/functions/fopen.html [3] http://man7.org/linux/man-pages/man3/fopen.3p.html [4] https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/fopen.3.html [5] https://www.freebsd.org/cgi/man.cgi?query=fopen&sektion=3&apropos=0&manpath=freebsd [6] https://docs.oracle.com/cd/E36784_01/html/E36874/fopen-3c.html