mirror of
https://github.com/Mathewnd/Astral.git
synced 2026-11-02 07:28:21 +00:00
fd: change allocation to add a lowest fd option
This commit is contained in:
+14
-10
@@ -4,8 +4,6 @@
|
|||||||
#include <kernel/alloc.h>
|
#include <kernel/alloc.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
// XXX limit the max fd number
|
|
||||||
|
|
||||||
int fd_release(fd_t* fd){
|
int fd_release(fd_t* fd){
|
||||||
spinlock_release(&fd->lock);
|
spinlock_release(&fd->lock);
|
||||||
return 0;
|
return 0;
|
||||||
@@ -28,16 +26,19 @@ int fd_access(fdtable_t* fdtable, fd_t** fd, int ifd){
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int fd_alloc(fdtable_t* fdtable, fd_t** fd, int* ifd){
|
int fd_alloc(fdtable_t* fdtable, fd_t** fd, int* ifd, int lowest){
|
||||||
|
if(lowest >= MAX_FD){
|
||||||
|
return EBADF;
|
||||||
|
}
|
||||||
|
|
||||||
spinlock_acquire(&fdtable->lock);
|
spinlock_acquire(&fdtable->lock);
|
||||||
|
|
||||||
// find fd to use
|
// find fd to use
|
||||||
|
|
||||||
bool ok = false;
|
bool ok = false;
|
||||||
|
|
||||||
for(uintmax_t i = fdtable->firstfreefd; i < fdtable->fdcount; ++i){
|
for(uintmax_t i = lowest; i < fdtable->fdcount; ++i){
|
||||||
if(!fdtable->fd[i]){
|
if(!fdtable->fd[i]){
|
||||||
fdtable->firstfreefd = i;
|
|
||||||
*ifd = i;
|
*ifd = i;
|
||||||
ok = true;
|
ok = true;
|
||||||
break;
|
break;
|
||||||
@@ -46,12 +47,16 @@ int fd_alloc(fdtable_t* fdtable, fd_t** fd, int* ifd){
|
|||||||
|
|
||||||
// resize table
|
// resize table
|
||||||
|
|
||||||
|
int targetfd = fdtable->fdcount > lowest ? fdtable->fdcount : lowest;
|
||||||
|
|
||||||
if(!ok){
|
if(!ok){
|
||||||
|
|
||||||
if(fdtable->fdcount == MAX_FD){
|
if(fdtable->fdcount == MAX_FD){
|
||||||
spinlock_release(&fdtable->lock);
|
spinlock_release(&fdtable->lock);
|
||||||
return EMFILE;
|
return EMFILE;
|
||||||
}
|
}
|
||||||
fd_t** tmp = realloc(fdtable->fd, sizeof(fd_t*)*(fdtable->fdcount + 1));
|
|
||||||
|
fd_t** tmp = realloc(fdtable->fd, sizeof(fd_t*)*(targetfd + 1));
|
||||||
|
|
||||||
if(!tmp){
|
if(!tmp){
|
||||||
spinlock_release(&fdtable->lock);
|
spinlock_release(&fdtable->lock);
|
||||||
@@ -59,8 +64,8 @@ int fd_alloc(fdtable_t* fdtable, fd_t** fd, int* ifd){
|
|||||||
}
|
}
|
||||||
|
|
||||||
fdtable->fd = tmp;
|
fdtable->fd = tmp;
|
||||||
*ifd = fdtable->fdcount;
|
*ifd = targetfd;
|
||||||
++fdtable->fdcount;
|
fdtable->fdcount = targetfd + 1;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -197,7 +202,7 @@ int fd_duplicate(fdtable_t* table, int src, int dest, int type, int* ret){
|
|||||||
}
|
}
|
||||||
|
|
||||||
if(type == 1){ // allocate for dup
|
if(type == 1){ // allocate for dup
|
||||||
err = fd_alloc(table, &destfd, &dest);
|
err = fd_alloc(table, &destfd, &dest, dest); // for fcntl F_DUPFD, dest is the lowest fd
|
||||||
if(err){
|
if(err){
|
||||||
fd_release(srcfd);
|
fd_release(srcfd);
|
||||||
return err;
|
return err;
|
||||||
@@ -226,7 +231,6 @@ int fd_duplicate(fdtable_t* table, int src, int dest, int type, int* ret){
|
|||||||
if(destfd){
|
if(destfd){
|
||||||
spinlock_acquire(&destfd->lock);
|
spinlock_acquire(&destfd->lock);
|
||||||
if(--destfd->refcount == 0){
|
if(--destfd->refcount == 0){
|
||||||
printf("killing\n");
|
|
||||||
if(destfd->node) vfs_close(destfd->node);
|
if(destfd->node) vfs_close(destfd->node);
|
||||||
free(destfd);
|
free(destfd);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -46,11 +46,10 @@ typedef struct {
|
|||||||
typedef struct {
|
typedef struct {
|
||||||
int lock;
|
int lock;
|
||||||
size_t fdcount;
|
size_t fdcount;
|
||||||
uintmax_t firstfreefd;
|
|
||||||
fd_t** fd;
|
fd_t** fd;
|
||||||
} fdtable_t;
|
} fdtable_t;
|
||||||
|
|
||||||
int fd_alloc(fdtable_t* fdtable, fd_t** fd, int* ifd);
|
int fd_alloc(fdtable_t* fdtable, fd_t** fd, int* ifd, int lowest);
|
||||||
int fd_free(fdtable_t* fdtable, int ifd);
|
int fd_free(fdtable_t* fdtable, int ifd);
|
||||||
int fd_access(fdtable_t* fdtable, fd_t** fd, int ifd);
|
int fd_access(fdtable_t* fdtable, fd_t** fd, int ifd);
|
||||||
int fd_release(fd_t* fd);
|
int fd_release(fd_t* fd);
|
||||||
|
|||||||
@@ -417,9 +417,9 @@ void sched_runinit(){
|
|||||||
|
|
||||||
fd_t *stdinfd, *stdoutfd, *stderrfd;
|
fd_t *stdinfd, *stdoutfd, *stderrfd;
|
||||||
|
|
||||||
fd_alloc(&proc->fdtable, &stdinfd, &tmp);
|
fd_alloc(&proc->fdtable, &stdinfd, &tmp, 0);
|
||||||
fd_alloc(&proc->fdtable, &stdoutfd, &tmp);
|
fd_alloc(&proc->fdtable, &stdoutfd, &tmp, 0);
|
||||||
fd_alloc(&proc->fdtable, &stderrfd, &tmp);
|
fd_alloc(&proc->fdtable, &stderrfd, &tmp, 0);
|
||||||
|
|
||||||
#define CONSOLE_PATH "dev/console"
|
#define CONSOLE_PATH "dev/console"
|
||||||
|
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ syscallret syscall_open(const char* pathname, int flags, mode_t mode){
|
|||||||
int ifd;
|
int ifd;
|
||||||
fd_t* fd;
|
fd_t* fd;
|
||||||
|
|
||||||
int err = fd_alloc(&proc->fdtable, &fd, &ifd);
|
int err = fd_alloc(&proc->fdtable, &fd, &ifd, 0);
|
||||||
|
|
||||||
if(err){
|
if(err){
|
||||||
retv.errno = err;
|
retv.errno = err;
|
||||||
|
|||||||
Reference in New Issue
Block a user