Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds an intermediate stage in closing an fd. #441

Merged
merged 2 commits into from
Oct 8, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions src/freertos_drivers/common/Device.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,11 @@ int Device::close(struct _reent *reent, int fd)
// stdin, stdout, and stderr never get closed
return 0;
}
files[fd].inshdn = true;
Copy link
Owner

Choose a reason for hiding this comment

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

Question: What if f->dev->close(f) returns an error? Should we reset the inshdn back to false? Otherwise, I don't think we can ever free the fd because we cannot make another close() call later.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good catch, fixed.

int result = f->dev->close(f);
if (result < 0)
{
files[fd].inshdn = false;
errno = -result;
return -1;
}
Expand Down
1 change: 1 addition & 0 deletions src/freertos_drivers/common/Devtab.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ struct File
off_t offset; /**< current offset within file */
int flags; /**< open flags */
uint8_t inuse : 1; /**< true if this is an open fd. */
uint8_t inshdn : 1; /**< true if this fd is in shutdown. */
uint8_t device : 1; /**< true if this is a device, false if file system */
uint8_t dir : 1; /**< true if this is a directory, else false */
uint8_t dirty : 1; /**< true if this file is dirty and needs flush */
Expand Down
3 changes: 2 additions & 1 deletion src/freertos_drivers/common/Fileio.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ int FileIO::fd_alloc(void)
if (files[i].inuse == false)
{
files[i].inuse = true;
files[i].inshdn = false;
files[i].device = true;
files[i].dir = false;
files[i].dirty = false;
Expand Down Expand Up @@ -85,7 +86,7 @@ File* FileIO::file_lookup(int fd)
errno = EBADF;
return nullptr;
}
if (files[fd].inuse == 0)
if (files[fd].inuse == 0 || files[fd].inshdn == 1)
{
errno = EBADF;
return nullptr;
Expand Down