mirror of
https://github.com/Mathewnd/Astral.git
synced 2026-10-29 05:38: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 <string.h>
|
||||
|
||||
// XXX limit the max fd number
|
||||
|
||||
int fd_release(fd_t* fd){
|
||||
spinlock_release(&fd->lock);
|
||||
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);
|
||||
|
||||
// find fd to use
|
||||
|
||||
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]){
|
||||
fdtable->firstfreefd = i;
|
||||
*ifd = i;
|
||||
ok = true;
|
||||
break;
|
||||
@@ -46,12 +47,16 @@ int fd_alloc(fdtable_t* fdtable, fd_t** fd, int* ifd){
|
||||
|
||||
// resize table
|
||||
|
||||
int targetfd = fdtable->fdcount > lowest ? fdtable->fdcount : lowest;
|
||||
|
||||
if(!ok){
|
||||
|
||||
if(fdtable->fdcount == MAX_FD){
|
||||
spinlock_release(&fdtable->lock);
|
||||
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){
|
||||
spinlock_release(&fdtable->lock);
|
||||
@@ -59,8 +64,8 @@ int fd_alloc(fdtable_t* fdtable, fd_t** fd, int* ifd){
|
||||
}
|
||||
|
||||
fdtable->fd = tmp;
|
||||
*ifd = fdtable->fdcount;
|
||||
++fdtable->fdcount;
|
||||
*ifd = targetfd;
|
||||
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
|
||||
err = fd_alloc(table, &destfd, &dest);
|
||||
err = fd_alloc(table, &destfd, &dest, dest); // for fcntl F_DUPFD, dest is the lowest fd
|
||||
if(err){
|
||||
fd_release(srcfd);
|
||||
return err;
|
||||
@@ -226,7 +231,6 @@ int fd_duplicate(fdtable_t* table, int src, int dest, int type, int* ret){
|
||||
if(destfd){
|
||||
spinlock_acquire(&destfd->lock);
|
||||
if(--destfd->refcount == 0){
|
||||
printf("killing\n");
|
||||
if(destfd->node) vfs_close(destfd->node);
|
||||
free(destfd);
|
||||
}
|
||||
|
||||
@@ -46,11 +46,10 @@ typedef struct {
|
||||
typedef struct {
|
||||
int lock;
|
||||
size_t fdcount;
|
||||
uintmax_t firstfreefd;
|
||||
fd_t** fd;
|
||||
} 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_access(fdtable_t* fdtable, fd_t** fd, int ifd);
|
||||
int fd_release(fd_t* fd);
|
||||
|
||||
@@ -417,9 +417,9 @@ void sched_runinit(){
|
||||
|
||||
fd_t *stdinfd, *stdoutfd, *stderrfd;
|
||||
|
||||
fd_alloc(&proc->fdtable, &stdinfd, &tmp);
|
||||
fd_alloc(&proc->fdtable, &stdoutfd, &tmp);
|
||||
fd_alloc(&proc->fdtable, &stderrfd, &tmp);
|
||||
fd_alloc(&proc->fdtable, &stdinfd, &tmp, 0);
|
||||
fd_alloc(&proc->fdtable, &stdoutfd, &tmp, 0);
|
||||
fd_alloc(&proc->fdtable, &stderrfd, &tmp, 0);
|
||||
|
||||
#define CONSOLE_PATH "dev/console"
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@ syscallret syscall_open(const char* pathname, int flags, mode_t mode){
|
||||
int ifd;
|
||||
fd_t* fd;
|
||||
|
||||
int err = fd_alloc(&proc->fdtable, &fd, &ifd);
|
||||
int err = fd_alloc(&proc->fdtable, &fd, &ifd, 0);
|
||||
|
||||
if(err){
|
||||
retv.errno = err;
|
||||
|
||||
Reference in New Issue
Block a user