From owner-freebsd-hackers@freebsd.org Fri Dec 29 18:48:47 2017 Return-Path: Delivered-To: freebsd-hackers@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id E051DEAA906 for ; Fri, 29 Dec 2017 18:48:47 +0000 (UTC) (envelope-from asomers@gmail.com) Received: from mail-lf0-x22a.google.com (mail-lf0-x22a.google.com [IPv6:2a00:1450:4010:c07::22a]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client CN "smtp.gmail.com", Issuer "Google Internet Authority G2" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id EF69C7674A; Fri, 29 Dec 2017 18:48:46 +0000 (UTC) (envelope-from asomers@gmail.com) Received: by mail-lf0-x22a.google.com with SMTP id y78so43214534lfd.1; Fri, 29 Dec 2017 10:48:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:cc; bh=M74N0wv+ZzLe3afnIIV0Gmayy2eRaEaVovDSRG8OE2w=; b=OmG4gelruXdEQMUSYAqGnaZJBxo3+EfJlW80cx3Y6k3rkk+WzfCKUTE9kKcvvKUAuu ZoxOXSNmDKU2Vsi5W0iGFpBJOWrUNofHtq31yQ9ykOdJugcZB7UzXSfXSfsipI4BeN3X /lhIuLZT9z0l/wZ05iJgG92UktMwOkzLri4AYC7DexdzC4RaIt1ZaC3VkrcuUCZ09kor tHtN2sPRbGC2xbRohYuNADp71A0ZQ2z+EoZpb45YQANts2bPB0O4XEVscYRfQshXKujc KsNeMF5zS1SkUYqnQbeqT6R+0Yf5FeM3/TVP0EObKhEDx6GJ/ObiWR1sq6sABEl3nfiM eIyA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:sender:in-reply-to:references:from :date:message-id:subject:to:cc; bh=M74N0wv+ZzLe3afnIIV0Gmayy2eRaEaVovDSRG8OE2w=; b=I5CAgnnPWr/Or7NdIjVDjG1/kfQgwM24JxaiORtkR6+I8XOEae4w6L0PXkJKpxOOC2 4DDepML62lPtV0oUvj6AtmpwvwGY7DxM/jNvUYtBzTcxHsCYfpe0CsUKwGrCkmQExDdI P6LllBQuk9cndW+O3eVtA/GcdLT5qAT7UKL7lNaU4/X2JKBsNgxV87ZOOS1QMVOM5Ww+ OZQNh3vweDeBcOrBZWPYtve7KHEd/qdxYbl35TlaLHkLLyaE6DI6qISd2hQhdVycVb99 qaZPFWOfq3vvq6w3F7bIEfOguQgY5BxkX/oyqQMMu2bEQ8BaO/wjxaCfarkgaFXzsBdj lOhQ== X-Gm-Message-State: AKGB3mJtMCmNVrXlIdi2yImxfMqT4l6tgHjrBs3vqx+nPPfD5/c571oe 7IVOCVn9nlCJ/nWulRAfiJR/CYNXbciqG/d8Ji0= X-Google-Smtp-Source: ACJfBouV7W1sqXKeKegJOK1t7/1LQBZmWf2EY8YZ+ygj1OKvBwR8jvmjgjYgtpVif9IluSWmaSy9YZy2vWa6sSlAXwU= X-Received: by 10.46.27.24 with SMTP id b24mr14282188ljb.54.1514573324184; Fri, 29 Dec 2017 10:48:44 -0800 (PST) MIME-Version: 1.0 Sender: asomers@gmail.com Received: by 10.179.76.19 with HTTP; Fri, 29 Dec 2017 10:48:43 -0800 (PST) In-Reply-To: <1514572041.12000.7.camel@freebsd.org> References: <24acbd94-c52f-e71a-8a96-d608a10963c6@rawbw.com> <1514572041.12000.7.camel@freebsd.org> From: Alan Somers Date: Fri, 29 Dec 2017 11:48:43 -0700 X-Google-Sender-Auth: 3zExsrbv-trUWA07JcYYBim4jO0 Message-ID: Subject: Re: Is it considered to be ok to not check the return code of close(2) in base? To: Ian Lepore Cc: Yuri , Freebsd hackers list Content-Type: text/plain; charset="UTF-8" X-Content-Filtered-By: Mailman/MimeDel 2.1.25 X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.25 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 29 Dec 2017 18:48:48 -0000 On Fri, Dec 29, 2017 at 11:27 AM, Ian Lepore wrote: > On Fri, 2017-12-29 at 10:19 -0800, Yuri wrote: > > Some base utilities sometimes close files that they open for their > > purposes without checking the error code of close(2). > > > > Is this considered to be ok, because it's just a close call and we > > are > > done with that file descriptor, or is it considered to be more > > appropriate to check close's error code? > > > > Maybe there is some policy that covers this? > > > > IMO, every system call's return value should be checked, just in > > case. > > > > > > Yuri > > > > There's really no point in checking on a close from a file opened only > for reading. You can argue it should be checked on a file open for > writing, but often isn't because you're then confronted with the > question "what should/can I do if there is an error?" If you report > the error and exit, then what about other files that were open at the > time? They're going to be closed by the kernel as part of process > cleanup, with no error checking or reporting. > > Also, with the async nature of filesystems, IO errors can still happen > after the close, unless fsync() was used. So if you're going to miss > most of the errors because of that, why bother to check at all? > > -- Ian > I would argue the opposite. There are very few reasons why close(s) would ever fail, and the most likely is EBADF. EBADF indicates a programming bug, like a double close or use of an uninitialized variable. Those could easily turn into worse bugs in the future. So I think the best course of action is to check the return code, assert() on EBADF, and ignore, or possibly log, other errors. -Alan