Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions src/libraries/Native/Unix/System.Native/pal_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,10 @@ int32_t SystemNative_ReadDirR(DIR* dir, uint8_t* buffer, int32_t bufferSize, Dir
return errno == 0 ? -1 : errno;
}
#else
int error = readdir_r(dir, entry, &result);
int error;

// EINTR isn't documented, happens in practice on macOS.
while ((error = readdir_r(dir, entry, &result)) && error == EINTR);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are you sure the EINTR is returned as the return value and not in errno?

Copy link
Contributor Author

@Therzok Therzok Feb 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have a reliable repro, but I can look in xnu source. I can adapt it so it checks errno.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like readdir_r sets the return value to whatever is in errno?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've read a few man pages of different OSes and there seems to be little consistency regarding this.
If using errno works on macOS then I'd prefer that since it's what we use everywhere else.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've changed it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If using errno works on macOS then I'd prefer that since it's what we use everywhere else.

Except this API explicitly says errors are returned rather than surfacing through errno. Is there an example man page that says readdir_r sets errno? What happens if a readdir_r implementation doesn't touch errno at all, errno retains an EINTR value from prior to the readdir_r call, and readdir_r hits another error... might we end up in an infinite loop?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can change the code back to checking the return code :D

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before you do, I'd like to hear from @akoeplinger :) I was actually asking questions rather than stating things that must happen ;-)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


// positive error number returned -> failure
if (error != 0)
Expand Down Expand Up @@ -473,12 +476,27 @@ int32_t SystemNative_ReadDirR(DIR* dir, uint8_t* buffer, int32_t bufferSize, Dir

DIR* SystemNative_OpenDir(const char* path)
{
return opendir(path);
DIR *result;

// EINTR isn't documented, happens in practice on macOS.
while ((result = opendir(path)) == NULL && errno == EINTR);

return result;
}

int32_t SystemNative_CloseDir(DIR* dir)
{
return closedir(dir);
int32_t result;

result = closedir(dir);

// EINTR isn't documented, happens in practice on macOS.
if (result < 0 && errno == EINTR)
{
result = 0;
}

return result;
}

int32_t SystemNative_Pipe(int32_t pipeFds[2], int32_t flags)
Expand Down