fd: bugfixes

- add an extra check
- properly set the fd entry in the table to 0 in a refcount > 0 free
- only close the fd node if there's an open node
This commit is contained in:
Matheus
2022-09-20 17:12:54 -03:00
parent 4581b460ff
commit 5e2059c284
+6 -4
View File
@@ -13,7 +13,7 @@ int fd_release(fd_t* fd){
int fd_access(fdtable_t* fdtable, fd_t** fd, int ifd){
spinlock_acquire(&fdtable->lock);
if(ifd >= fdtable->fdcount || fdtable->fd[ifd] == NULL){
if(ifd >= fdtable->fdcount || fdtable->fd[ifd] == NULL || fdtable->fd[ifd]->node == NULL){
spinlock_release(&fdtable->lock);
return EBADF;
}
@@ -91,8 +91,9 @@ int fd_free(fdtable_t* fdtable, int ifd){
fd_t* fd = fdtable->fd[ifd];
spinlock_acquire(&fd->lock);
if(--fd->refcount > 0){
fdtable->fd[ifd] = NULL;
spinlock_release(&fd->lock);
spinlock_release(&fdtable->lock);
return 0;
@@ -101,9 +102,10 @@ int fd_free(fdtable_t* fdtable, int ifd){
fdtable->fd[ifd] = NULL;
spinlock_release(&fdtable->lock);
int err = 0;
if(fd->node)
err = vfs_close(fd->node);
int err = vfs_close(fd->node);
free(fd);
return err;