mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Olaf Titz <olaf@bigred.inka.de>
To: linux-kernel@vger.kernel.org
Subject: Re: light weight user level semaphores
Date: Sat, 21 Apr 2001 12:13:06 +0200	[thread overview]
Message-ID: <E14quO3-0004RM-00@g212.hadiko.de> (raw)
In-Reply-To: <E14qHRp-0007Yc-00@the-village.bc.nu> <Pine.LNX.4.31.0104190944090.4074-100000@penguin.transmeta.com> <E14qXEU-0005xo-00@g212.hadiko.de> <9bqgvi$63q$1@penguin.transmeta.com>

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 2617 bytes --]

> This is guaranteed behaviour of UNIX. You get file handles in order, or
> you don't get them at all.

You get the _next free_ file handle in order. What if your program
assumes they are all contiguous, and it is called by some other
program which forgot about FD_CLOEXEC and has some higher fds still
open? (xdm did this for ten years with its listening socket, just to
name a well-known example. So every program which asssumes contiguous
fd allocations would fail if started from an xdm session.)

If your program makes assumptions on its environment which are not
guaranteed it's broken.

What _is_ guaranteed is that after consecutive allocations of fds like
  for (i=0; i<n; ++i)
    fd[i]=open(...);
the following property holds:
    fd[i] > fd[j] if (i > j and fd[i]!=-1 and fd[j]!=-1).
What is absolutely nowhere guaranteed is that
    fd[i+1] = fd[i]+1.
It is not possible to guarantee this since any fd may be already open
before main() starts.

Of course you can guarantee that the fds are available like this:
  for (i=getdtablesize(); i>=0; --i)
    close(i);
and not calling library functions which may open fds.

> 	pid = fork();
> 	if (!pid) {
> 		close(0);
> 		close(1);
> 		dup(pipe[0]);	/* input pipe */
> 		dup(pipe[1]);	/* output pipe */
> 		execve("child");
> 		exit(1);
> 	}
>
> The above is absolutely _standard_ behaviour. It's required to work.

The reason why it works is that (a) the target fds are 0 and 1, and
(b) you close them explicitly. For less trivial uses, there is always
dup2().

> And btw, it's _still_ required to work even if there happens to be a
> "malloc()" in between the close() and the dup() calls.

I wouldn't count on that. It's clearly not required to work if there's
a getpwnam() in between. (I already had my share of problems with
syslog() in exactly this situation.)

Do we need a list of library functions which may open fds, like the
infamous "list of functions which may move or purge memory" on the Mac
(which grew longer with every OS release and Inside Mac supplement
issue)? Do we need to know for each library routine how it is
implemented?

> Trust me. You're arguing for clearly broken behaviour. malloc() and
> friends MUST NOT open file descriptors. It _will_ break programs that
> rely on traditional and documented features.

Traditional and documented is, in my view, the description as of the
open(2) man page:

       When the
       call is successful, the file descriptor returned  will  be
       the lowest file descriptor not currently open for the pro­
       cess.

which of course is exactly how it is implemented in the kernel.

Olaf

  parent reply	other threads:[~2001-04-21 10:33 UTC|newest]

Thread overview: 62+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20010417114433.D1108@w-mikek2.sequent.com>
2001-04-17 19:48 ` Linus Torvalds
2001-04-18 18:13   ` Bernd Eckenfels
2001-04-18 19:35   ` Ulrich Drepper
2001-04-19  8:20     ` Alon Ziv
2001-04-19  8:52       ` Abramo Bagnara
2001-04-19  9:08         ` Alexander Viro
2001-04-19 10:44           ` Abramo Bagnara
2001-04-19 16:11         ` Linus Torvalds
2001-04-19 16:33           ` Alexander Viro
2001-04-19 16:43             ` Linus Torvalds
2001-04-19 17:33               ` Alexander Viro
2001-04-19 17:38                 ` Linus Torvalds
2001-04-19 18:24                   ` Alexander Viro
2001-04-19 19:26                   ` Ulrich Drepper
2001-04-19 19:35                     ` Alan Cox
2001-04-19 20:06                       ` Ulrich Drepper
2001-04-19 20:11                         ` Alan Cox
2001-04-19 20:26                           ` Ulrich Drepper
2001-04-19 20:22                     ` Ingo Oeser
2001-04-19 20:40                       ` Ulrich Drepper
2001-04-19 20:51                       ` Linus Torvalds
2001-04-19 21:38                       ` Alan Cox
2001-04-19 20:49                     ` Linus Torvalds
2001-04-19 21:18                       ` Ulrich Drepper
2001-04-19 21:41                         ` Linus Torvalds
2001-04-19 22:46                           ` Ulrich Drepper
2001-04-20  1:35                             ` Alexander Viro
2001-04-20  2:45                               ` Ulrich Drepper
2001-04-19 16:43           ` Abramo Bagnara
2001-04-19 20:47           ` Ingo Oeser
2001-04-19 20:54             ` Linus Torvalds
2001-04-19  9:08       ` Ingo Oeser
2001-04-19 11:51       ` Alan Cox
2001-04-19 16:03       ` Linus Torvalds
2001-04-19 16:38         ` Alan Cox
2001-04-19 16:46           ` Linus Torvalds
2001-04-19 17:12             ` Alan Cox
2001-04-19 22:35               ` Rogier Wolff
2001-04-20  9:29             ` Olaf Titz
2001-04-20 14:19               ` Jesse Pollard
2001-04-20 18:36                 ` Olaf Titz
2001-04-20 23:33               ` Linus Torvalds
2001-04-21  4:06                 ` fd allocation [was: light weight user level semaphores] Edgar Toernig
2001-04-22  9:48                   ` Olaf Titz
2001-04-22 11:41                     ` light weight user level semaphores Alon Ziv
2001-04-22 12:44                       ` Alan Cox
2001-04-22 15:19                         ` Alon Ziv
2001-04-22 14:31                           ` Alexander Viro
2001-04-22 16:08                             ` Alon Ziv
2001-04-22 11:41                     ` Alon Ziv
2001-04-22 14:18                     ` David Woodhouse
2001-04-23 13:19                       ` David Howells
2001-04-23 14:48                         ` Alon Ziv
2001-04-23 15:40                           ` David Howells
2001-04-21 10:13                 ` Olaf Titz [this message]
2001-04-23 15:34                 ` Jeff Garzik
2001-04-23 19:18             ` Ingo Oeser
2001-04-24  0:19             ` David Wagner
2001-04-24  0:41               ` Alexander Viro
2001-04-19 19:47           ` Ulrich Drepper
2001-04-19 18:48         ` Olaf Titz
2001-04-19 13:59 George Talbot

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=E14quO3-0004RM-00@g212.hadiko.de \
    --to=olaf@bigred.inka.de \
    --cc=linux-kernel@vger.kernel.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®