mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Vasiliy Kulikov <segoon@openwall.com>
To: musl@lists.openwall.com
Subject: close(2) failure cases (was: some fixes to musl)
Date: Sun, 24 Jul 2011 13:19:18 +0400	[thread overview]
Message-ID: <20110724091918.GA6076@albatros> (raw)
In-Reply-To: <20110721182101.GB132@brightrain.aerifal.cx>

Rich,

On Thu, Jul 21, 2011 at 14:21 -0400, Rich Felker wrote:
> > I wonder what is the Right Way to handle close() failures in generic
> > case.
> 
> I think 99% of them are complete non-issues. The whole idea that close
> can fail (for reasons other than EBADF) is a misdesign, but it can
> generally only happen for odd devices or NFS (which is broken in many
> more fundamental ways anyway). It certainly can't fail for terminal
> devices, pipes, sockets, directories, etc. which are the only places
> libc needs it.

I've looked at some Linux code related to close().  It may fail iff
file_ops->flush() is not NULL, and close() fails iff ->flush() fails.

So, as the following files don't implement ->flush, they cannot fail on
close():

* tty
* pipe
* sockets (all families)
* some classic devices: full, null, zero, mem, kmem

I cannot say something for sure for directories.  However, NFS'
implementation doesn't implement it => close() cannot fail.  (The
comment in the code says all directories ops are synchronous.)

close() may fail on NFS regural files.  As there is API to work with
/etc/* files, and / can be NFS mount point, it's good to check for
close() (and all other syscalls) failures on such files (whether write()
is actually succeeded).

Even if close() fails, the fd is freed.  So fd leakage is impossible.


As musl is Linux specific libc, you're free to rely on Linux specific
restrictions/features.  However, I don't know whether close()
implementation is guaranteed to be errorless for these types of files.
If no, it may be changed in the future by introducing some new feature
(and it will not break POSIX as POSIX explicitly defines close()
failure possibility).  It's very unlikely though, as breaking tons of
programs relying on implicit Linux behaviour is not encouraged by Linus.


Thanks,

-- 
Vasiliy


  parent reply	other threads:[~2011-07-24  9:19 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-07-21 17:02 some fixes to musl Vasiliy Kulikov
2011-07-21 18:21 ` Rich Felker
2011-07-21 19:00   ` Solar Designer
2011-07-22  8:19     ` Vasiliy Kulikov
2011-07-22 13:30       ` Rich Felker
2011-07-21 19:17   ` Vasiliy Kulikov
2011-07-22  2:08     ` Rich Felker
2011-07-24  9:39       ` Vasiliy Kulikov
2011-07-24 12:56         ` Rich Felker
2011-07-24 18:38           ` Vasiliy Kulikov
2011-07-24  9:19   ` Vasiliy Kulikov [this message]
2011-07-24 12:24     ` close(2) failure cases (was: some fixes to musl) Rich Felker
2011-07-24 17:49       ` Vasiliy Kulikov
2011-07-24 22:29         ` Rich Felker
2011-07-25 17:36           ` Vasiliy Kulikov
2011-07-22  1:57 ` some fixes to musl Rich Felker
2011-07-22  4:30   ` Rich Felker
2011-07-22  8:26     ` Vasiliy Kulikov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20110724091918.GA6076@albatros \
    --to=segoon@openwall.com \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).