mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Markus Wichmann <nullplan@gmx.net>
To: musl@lists.openwall.com
Subject: Re: readdir(3): behavior on descriptors with O_SEARCH
Date: Sun, 28 Aug 2016 09:10:52 +0200	[thread overview]
Message-ID: <20160828071052.GA28382@voyager> (raw)
In-Reply-To: <CAMqzjetdOGzD3dieMyV+xZ-J72m8oqnNgwjBa+_czFFkzh7e9w@mail.gmail.com>

On Sat, Aug 27, 2016 at 09:23:50PM +0300, Dmitry Selyutin wrote:
> int const flags = (O_DIRECTORY | O_SEARCH);
> int descriptor = open(path, flags);
> DIR *handle = fdopendir(descriptor);
> struct dirent *entry = readdir(handle);
> 
> To cut the long story short, any attempt to call readdir(3) on directory
> handle obtained via fdopendir(3) returns NULL and sets the errno variable
> to EBADF. This behavior arises only on descriptors opened with (O_DIRECTORY
> | O_SEARCH) flags enabled; it goes away if O_SEARCH flag is removed.
> 

Try strace(1). That should tell you what glibc and musl are doing
differently. For instace, whether glibc removes O_SEARCH from the fd.

musl defines O_SEARCH to be equal to O_PATH. The manpage says that
O_PATH means the file isn't opened for reading. I guess if you do that
then getdents(2) will fail, which is what musl uses to implement
readdir(3).

I tried to do the same trace in glibc 2.19 (which is what Debian stable
is using right now), but to no avail: O_SEARCH isn't even mentioned
anywhere in that code. But its implementation of fdopendir(3) rejects
fds open only for writing. The readdir(3) implementation is, of course,
overcomplicated, but also seems to just call getdents(2). And then it
tries to pack the kernel structures into its own structures, probably
for ABI reasons. And people wonder why I dislike dynamic linking...

> So it seems that O_SEARCH is the reason; I thought that this flag tells
> exactly "well, I'm going to use it for search only", which implies "well,
> I'm going to use only readdir(3) to get information about files inside". Is
> my interpretation correct?
> 

My manpage doesn't know O_SEARCH, but it knows O_PATH, and then you're
wrong. It means "I'll only use this fd in *at() and fchdir() and
similar; this fd isn't open for reading."

> I'm not really sure if it is a bug, since I suspect POSIX may allow open(3)
> with (O_DIRECTORY | O_SEARCH) flags to behave in an implementation-defined
> matter; it can be possible that file descriptors obtained via open(3) with
> O_DIRECTORY flag set are guaranteed to work only with fchdir(3) and *at(3)
> operations. However, if such behavior is intentional, it would be a good
> idea (in my opinion) make fdopendir(3) return NULL (though it won't match
> behavior e.g. for glibc).
> 

POSIX doesn't know O_SEARCH or O_PATH, and thus mandates nothing about
their meaning.

Ciao,
Markus


  reply	other threads:[~2016-08-28  7:10 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <CAMqzjetOkwQ7wi4p3MY_HT46v6pVRh_eHSi6SbwC96qoz+ivFg@mail.gmail.com>
2016-08-27 18:23 ` Dmitry Selyutin
2016-08-28  7:10   ` Markus Wichmann [this message]
2016-08-28  8:12     ` Dmitry Selyutin
2016-08-28  9:02       ` Dmitry Selyutin
2016-08-28 15:06         ` Rich Felker
2016-08-28 16:02           ` Dmitry Selyutin
2016-08-28 16:20             ` Rich Felker
2016-08-28 17:14               ` Dmitry Selyutin

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=20160828071052.GA28382@voyager \
    --to=nullplan@gmx.net \
    --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).