From e8cd27c353dd0b97f3e65216052580f5be892ad0 Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Thu, 17 Nov 2011 10:22:43 +0100 Subject: [PATCH] Added close(2) and fixed bugs in pipe(2) and others. --- libmaxsi/c/hsrc/unistd.h | 2 +- libmaxsi/io.cpp | 5 +++++ sortix/descriptors.cpp | 15 +++++++++++---- sortix/io.cpp | 10 ++++++++++ sortix/pipe.cpp | 4 ++-- sortix/syscallnum.h | 3 ++- 6 files changed, 31 insertions(+), 8 deletions(-) diff --git a/libmaxsi/c/hsrc/unistd.h b/libmaxsi/c/hsrc/unistd.h index f150ce2b..3a1a7eac 100644 --- a/libmaxsi/c/hsrc/unistd.h +++ b/libmaxsi/c/hsrc/unistd.h @@ -80,7 +80,6 @@ int access(const char*, int); unsigned alarm(unsigned); int chdir(const char*); int chown(const char*, uid_t, gid_t); -int close(int); size_t confstr(int, char*, size_t); char* crypt(const char*, const char*); char* ctermid(char*); @@ -159,6 +158,7 @@ extern char* optarg; extern int opterr, optind, optopt; #endif +int close(int); void _exit(int); pid_t fork(void); pid_t getpid(void); diff --git a/libmaxsi/io.cpp b/libmaxsi/io.cpp index 6217652c..dfde5839 100644 --- a/libmaxsi/io.cpp +++ b/libmaxsi/io.cpp @@ -33,6 +33,7 @@ namespace Maxsi DEFN_SYSCALL3(ssize_t, SysRead, 18, int, void*, size_t); DEFN_SYSCALL3(ssize_t, SysWrite, 19, int, const void*, size_t); DEFN_SYSCALL1(int, SysPipe, 20, int*); + DEFN_SYSCALL1(int, SysClose, 21, int); size_t Print(const char* Message) { @@ -78,6 +79,10 @@ namespace Maxsi return SysPipe(pipefd); } + extern "C" int close(int fd) + { + return SysClose(fd); + } #endif } diff --git a/sortix/descriptors.cpp b/sortix/descriptors.cpp index 45cad569..fea77ded 100644 --- a/sortix/descriptors.cpp +++ b/sortix/descriptors.cpp @@ -59,6 +59,7 @@ namespace Sortix { if ( devices[i] == NULL ) { + object->Refer(); devices[i] = object; return i; } @@ -87,7 +88,7 @@ namespace Sortix void DescriptorTable::Free(int index) { - ASSERT(index < index); + ASSERT(index < numdevices); ASSERT(devices[index] != NULL); if ( devices[index] != reserveddevideptr ) @@ -109,6 +110,7 @@ namespace Sortix ASSERT(devices[index] != NULL); ASSERT(devices[index] == reserveddevideptr); + object->Refer(); devices[index] = object; } @@ -117,9 +119,14 @@ namespace Sortix Device** newlist = new Device*[numdevices]; if ( newlist == NULL ) { return false; } - Memory::Copy(newlist, devices, sizeof(Device*) * numdevices); - - // TODO: Possibly deal with a potential O_CLOFORK! + for ( int i = 0; i < numdevices; i++ ) + { + // TODO: Possibly deal with a potential O_CLOFORK! + newlist[i] = devices[i]; + if ( !devices[i] ) { continue; } + if ( devices[i] == reserveddevideptr ) { continue; } + newlist[i]->Refer(); + } ASSERT(forkinto->devices == NULL); diff --git a/sortix/io.cpp b/sortix/io.cpp index 75bba0f4..b7f362d3 100644 --- a/sortix/io.cpp +++ b/sortix/io.cpp @@ -117,10 +117,20 @@ namespace Sortix return 0; } + int SysClose(int fd) + { + Process* process = CurrentProcess(); + Device* dev = process->descriptors.Get(fd); + if ( !dev ) { return -1; /* TODO: EBADF */ } + process->descriptors.Free(fd); + return 0; + } + void Init() { Syscall::Register(SYSCALL_WRITE, (void*) SysWrite); Syscall::Register(SYSCALL_READ, (void*) SysRead); + Syscall::Register(SYSCALL_CLOSE, (void*) SysClose); } } } diff --git a/sortix/pipe.cpp b/sortix/pipe.cpp index ac0dd49e..4f05ed57 100644 --- a/sortix/pipe.cpp +++ b/sortix/pipe.cpp @@ -71,8 +71,8 @@ namespace Sortix DevPipeStorage::~DevPipeStorage() { - ASSERT(!readwaiting); - ASSERT(!writewaiting); + if ( readwaiting ) { Syscall::ScheduleResumption(readwaiting); } + if ( writewaiting ) { Syscall::ScheduleResumption(writewaiting); } delete[] buffer; } diff --git a/sortix/syscallnum.h b/sortix/syscallnum.h index c420ea7f..f79ba6d0 100644 --- a/sortix/syscallnum.h +++ b/sortix/syscallnum.h @@ -46,7 +46,8 @@ #define SYSCALL_READ 18 #define SYSCALL_WRITE 19 #define SYSCALL_PIPE 20 -#define SYSCALL_MAX_NUM 21 /* index of highest constant + 1 */ +#define SYSCALL_CLOSE 21 +#define SYSCALL_MAX_NUM 22 /* index of highest constant + 1 */ #endif