All of lore.kernel.org
 help / color / mirror / Atom feed
From: Elladan <elladan@eskimo.com>
To: Andreas Schwab <schwab@suse.de>
Cc: Elladan <elladan@eskimo.com>, Stevie O <stevie@qrpff.net>,
	Alan Cox <alan@lxorguk.ukuu.org.uk>,
	Zack Weinberg <zack@codesourcery.com>,
	linux-kernel@vger.kernel.org
Subject: Re: close return value (was Re: [ANNOUNCE] Ext3 vs Reiserfs benchmarks)
Date: Wed, 17 Jul 2002 09:49:33 -0700	[thread overview]
Message-ID: <20020717164933.GA2136@eskimo.com> (raw)
In-Reply-To: <je65zel8pr.fsf@sykes.suse.de>

On Wed, Jul 17, 2002 at 04:39:28PM +0200, Andreas Schwab wrote:
> Elladan <elladan@eskimo.com> writes:
> 
> |> On Wed, Jul 17, 2002 at 12:17:40AM -0400, Stevie O wrote:
> |> > At 07:22 PM 7/16/2002 -0700, Elladan wrote:
> |> > >  1. Thread 1 performs close() on a file descriptor.  close fails.
> |> > >  2. Thread 2 performs open().
> |> > >* 3. Thread 1 performs close() again, just to make sure.
> |> > >
> |> > >
> |> > >open() may return any file descriptor not currently in use.
> |> > 
> |> > I'm confused here... the only way close() can fail is if the file
> |> > descriptor is invalid (EBADF); wouldn't it be rather stupid to close()
> |> > a known-to-be-bad descriptor?
> |> 
> |> Well, obviously, if that's the case.  However, the man page for close(2)
> |> doesn't agree (see below).  close() is allowed to return EBADF, EINTR,
> |> or EIO.
> |> 
> |> The question is, does the OS standard guarantee that the fd is closed,
> |> even if close() returns EINTR or EIO?  Just going by the normal usage of
> |> EINTR, one might think otherwise.  It doesn't appear to be documented
> |> one way or another.
> 
> POSIX says the state of the file descriptor when close fails (with errno
> != EBADF) is unspecified, which means:
> 
>     The value or behavior may vary among implementations that conform to
>     IEEE Std 1003.1-2001. An application should not rely on the existence
>     or validity of the value or behavior. An application that relies on
>     any particular value or behavior cannot be assured to be portable
>     across conforming implementations.

This doesn't mean an OS shouldn't specify the behavior.  Just because
the cross-platform standard leaves it unspecified doesn't mean the OS
should.

Consider what this says, if a particular OS doesn't pick a standard
which the application can port to.  It means that the *only way* to
correctly close a file descriptor is like this:

int ret;
do {
	ret = close(fd);
} while(ret == -1 && errno != EBADF);

That means, if we get an error, we have to loop until the kernel throws
a BADF error!  We can't detect that the file is closed from any other
error value, because only BADF has a defined behavior.

This would sort of work, though of course be hideous, for a single
threaded app.  Now consider a multithreaded app.  To correctly implement
this we have to lock around all calls to close and
open/socket/dup/pipe/creat/etc...

This is clearly ridiculous, and not at all as intended.  Either standard
will work for an OS (though guaranteeing close the first time is much
simpler all around), but it needs to be specified and stuck to, or you
get horrible things like this to work around a bad spec:


void lock_syscalls();
void unlock_syscalls();

int threadsafe_open(const char *file, int flags, mode_t mode)
{
	int fd;
	lock_syscalls();
	fd = open(file, flags, mode);
	unlock_syscalls();
	return fd;
}

int threadsafe_close(int fd)
{
	int ret;
	lock_syscalls();
	do {
		ret = close(fd);
	} while(ret == -1 && errno != EBADF);
	unlock_syscalls();
	return ret;
}

int threadsafe_socket() ...
int threadsafe_pipe() ...
int threadsafe_dup() ...
int threadsafe_creat() ...
int threadsafe_socketpair() ...
int threadsafe_accept() ...

-J


  reply	other threads:[~2002-07-17 16:47 UTC|newest]

Thread overview: 83+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20020712162306$aa7d@traf.lcs.mit.edu>
     [not found] ` <mit.lcs.mail.linux-kernel/20020712162306$aa7d@traf.lcs.mit.edu>
2002-07-15 15:22   ` [ANNOUNCE] Ext3 vs Reiserfs benchmarks Patrick J. LoPresti
2002-07-15 17:31     ` Chris Mason
2002-07-15 18:33     ` Matthias Andree
     [not found]     ` <20020715173337$acad@traf.lcs.mit.edu>
     [not found]       ` <mit.lcs.mail.linux-kernel/20020715173337$acad@traf.lcs.mit.edu>
2002-07-15 19:13         ` Patrick J. LoPresti
2002-07-15 20:55           ` Matthias Andree
2002-07-15 21:23             ` Patrick J. LoPresti
2002-07-15 21:38               ` Thunder from the hill
2002-07-16 12:31                 ` Matthias Andree
2002-07-16 15:53                   ` Thunder from the hill
2002-07-16 19:26                     ` Matthias Andree
2002-07-16 19:38                       ` Thunder from the hill
2002-07-16 23:22                         ` close return value (was Re: [ANNOUNCE] Ext3 vs Reiserfs benchmarks) Zack Weinberg
2002-07-17  1:03                           ` Alan Cox
2002-07-16 23:52                             ` close return value David S. Miller
2002-07-17  1:35                               ` Alan Cox
2002-07-17  0:20                                 ` David S. Miller
2002-07-17  1:05                                   ` Linus Torvalds
2002-07-17  1:05                                     ` David S. Miller
2002-07-17  1:23                                       ` Linus Torvalds
2002-07-17 11:51                                         ` Matthias Andree
2002-07-17 17:23                                           ` Andries Brouwer
2002-07-20  8:00                                         ` Florian Weimer
2002-07-20 16:45                                           ` Linus Torvalds
2002-07-26  0:06                                             ` EFAULT vs. SIGSEGV [was Re: close return value] Pavel Machek
2002-07-26 14:01                                               ` (no subject) Alexis Deruelle
     [not found]                                   ` <mailman.1026868201.10433.linux-kernel2news@redhat.com>
2002-07-18  0:01                                     ` close return value Pete Zaitcev
2002-07-18  0:10                                       ` Thunder from the hill
     [not found]                                       ` <mit.lcs.mail.linux-kernel/200207180001.g6I015f02681@devserv.devel.redhat.com>
2002-07-18 14:42                                         ` Patrick J. LoPresti
2002-07-18 15:13                                           ` Richard B. Johnson
2002-07-18 15:32                                             ` Sandy Harris
2002-07-18 23:47                                           ` Albert D. Cahalan
2002-07-19 16:12                                             ` Patrick J. LoPresti
2002-07-19 16:24                                               ` Joseph Malicki
2002-07-19 18:48                                                 ` Patrick J. LoPresti
2002-07-19 19:25                                                   ` Lars Marowsky-Bree
2002-07-19 19:30                                                     ` Arnaldo Carvalho de Melo
2002-07-19 19:45                                                       ` Joseph Malicki
2002-07-19 19:55                                                         ` Arnaldo Carvalho de Melo
2002-07-20 18:25                                                         ` Bernd Eckenfels
2002-07-20 23:06                                                         ` Sandy Harris
2002-07-20 14:42                                                 ` Andries Brouwer
2002-07-18 20:09                                       ` Hildo.Biersma
2002-07-18 23:55                                         ` Pete Zaitcev
2002-07-19 11:31                                           ` Hildo.Biersma
2002-07-19 16:16                                             ` Pete Zaitcev
2002-07-23 22:19                                           ` Bill Davidsen
2002-07-17  0:10                             ` close return value (was Re: [ANNOUNCE] Ext3 vs Reiserfs benchmarks) Zack Weinberg
2002-07-17  1:45                               ` Alan Cox
2002-07-17 18:24                                 ` Zack Weinberg
2002-07-22 16:42                                 ` Rogier Wolff
2002-07-17  8:00                               ` Lars Marowsky-Bree
2002-07-17 15:49                                 ` Thunder from the hill
2002-07-17  2:22                             ` Elladan
2002-07-17  2:54                               ` Thunder from the hill
2002-07-17  3:00                                 ` Elladan
2002-07-17  3:10                                   ` Thunder from the hill
2002-07-17  3:31                                     ` Elladan
2002-07-17  4:17                               ` Stevie O
2002-07-17  4:38                                 ` Elladan
2002-07-17 14:39                                   ` Andreas Schwab
2002-07-17 16:49                                     ` Elladan [this message]
2002-07-17 17:43                                       ` Linus Torvalds
2002-07-17 22:07                                         ` Elladan
2002-07-18  9:48                                         ` Ketil Froyn
2002-07-17 17:17                                   ` Andries Brouwer
2002-07-17 17:51                                     ` Richard Gooch
2002-07-17  7:34                               ` close return value (was Re: [ANNOUNCE] Ext3 vs Reiserfs benchmarks Kai Henningsen
2002-07-15 21:59               ` Ketil Froyn
2002-07-15 23:08                 ` Matti Aarnio
2002-07-16 12:33                   ` Matthias Andree
2002-07-15 22:55             ` Alan Cox
2002-07-15 21:58               ` Matthias Andree
2002-07-15 21:14           ` Chris Mason
2002-07-15 21:31             ` Patrick J. LoPresti
2002-07-15 22:12               ` Richard A Nelson
2002-07-16  1:02               ` Lawrence Greenfield
     [not found]                 ` <mit.lcs.mail.linux-kernel/200207160102.g6G12BiH022986@lin2.andrew.cmu.edu>
2002-07-16  1:43                   ` Patrick J. LoPresti
2002-07-16  1:56                     ` Thunder from the hill
2002-07-16 12:47                     ` Matthias Andree
2002-07-16 21:09                     ` James Antill
2002-07-16 12:35             ` Matthias Andree
2002-07-16  7:07     ` Dax Kelson
     [not found] <fa.lfdnrtv.5h8i1j@ifi.uio.no>
     [not found] ` <fa.i1e82rv.1digoa4@ifi.uio.no>
2002-07-17  3:46   ` close return value (was Re: [ANNOUNCE] Ext3 vs Reiserfs benchmarks) Russ Allbery

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=20020717164933.GA2136@eskimo.com \
    --to=elladan@eskimo.com \
    --cc=alan@lxorguk.ukuu.org.uk \
    --cc=linux-kernel@vger.kernel.org \
    --cc=schwab@suse.de \
    --cc=stevie@qrpff.net \
    --cc=zack@codesourcery.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.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.