Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Performance of openat() and fstatat()

302 views
Skip to first unread message

Nordlöw

unread,
Nov 30, 2009, 5:59:29 AM11/30/09
to
Is there a performance aspect on using openat() and fstatat() over open
() and fstat() or is it just for convenience?

Thanks,
Nordlöw

Jens Thoms Toerring

unread,
Nov 30, 2009, 8:43:13 AM11/30/09
to
Nordlöw <per.n...@gmail.com> wrote:
> Is there a performance aspect on using openat() and fstatat() over open
> () and fstat() or is it just for convenience?

Going by what's stated in the new POSIX standard the purpose of
these new functions is neither performance nor convenience but
the avoidance of race conditions. If you use fstat() on a file
in a different directory than the current working directory and
then open() it (relying on the results of fstat()), another pro-
cess could have changed in between where the path leads to, so
instead of opening the file you called fstat() on you might be
tricked into opening a completely different one. I.e. let's say
you call fstat() on

/foo/bar/a.txt

and now another process gets in between and does

mv /foo/bar /foo/baz
mkdir /foo/bar
cp something_bad.txt /foo/bar/a.txt

then you would not open the file you had tested with fstat()
but the file just created by the other process. And that (at
least as far as I understand it at the momente) is what can
be avoided by using these new functions since you don't spe-
cify the path to the file by name but with respect to the file
descriptor for the directory (which isn't changed on renaming).

Regards, Jens
--
\ Jens Thoms Toerring ___ j...@toerring.de
\__________________________ http://toerring.de

David Schwartz

unread,
Nov 30, 2009, 9:07:59 AM11/30/09
to
On Nov 30, 5:43 am, j...@toerring.de (Jens Thoms Toerring) wrote:

> Going by what's stated in the new POSIX standard the purpose of
> these new functions is neither performance nor convenience but
> the avoidance of race conditions. If you use fstat() on a file
> in a different directory than the current working directory and
> then open() it (relying on the results of fstat()), another pro-
> cess could have changed in between where the path leads to, so
> instead of opening the file you called fstat() on you might be
> tricked into opening a completely different one. I.e. let's say

[snip]

To put it more simply, functions like 'fstatat', 'openat', and the
ability to open a directory and get a file descriptor for it provide a
way to have a sequence of operations that all definitely refer to the
same directory.

They also provide a way for a process to have more than one equivalent
of a current working directory and they provide a way to pass a
working directory to a function. This can be especially useful in
multi-threaded processes.

DS

Nordlöw

unread,
Nov 30, 2009, 11:00:17 AM11/30/09
to

Superb answers!

I am grateful!

/Nordlöw

0 new messages