diff --git a/sortix/descriptors.cpp b/sortix/descriptors.cpp index 31e601fc..aa1f99f6 100644 --- a/sortix/descriptors.cpp +++ b/sortix/descriptors.cpp @@ -23,6 +23,7 @@ *******************************************************************************/ #include +#include #include #include #include "descriptors.h" @@ -54,6 +55,7 @@ namespace Sortix if ( !dev || dev == RESERVED_DEVICE ) { continue; } dev->Unref(); + delete[] devices[i].path; } delete[] devices; @@ -61,7 +63,7 @@ namespace Sortix numdevices = 0; } - int DescriptorTable::Allocate(Device* object) + int DescriptorTable::Allocate(Device* object, char* pathcopy) { for ( int i = 0; i < numdevices; i++ ) { @@ -70,6 +72,7 @@ namespace Sortix object->Refer(); devices[i].dev = object; devices[i].flags = 0; + devices[i].path = pathcopy; return i; } } @@ -86,14 +89,17 @@ namespace Sortix } size_t numpadded = newlistlength-numdevices; - memset(newlist + numdevices, 0, sizeof(*devices) * numpadded); + for ( size_t i = numdevices; i < newlistlength; i++ ) + newlist[i].dev = NULL, + newlist[i].path = NULL, + newlist[i].flags = 0; delete[] devices; devices = newlist; numdevices = newlistlength; - return Allocate(object); + return Allocate(object, pathcopy); } void DescriptorTable::Free(int index) @@ -104,26 +110,30 @@ namespace Sortix if ( devices[index].dev != RESERVED_DEVICE ) { devices[index].dev->Unref(); + delete[] devices[index].path; } devices[index].dev = NULL; + devices[index].path = NULL; devices[index].flags = 0; } int DescriptorTable::Reserve() { - return Allocate(RESERVED_DEVICE); + return Allocate(RESERVED_DEVICE, NULL); } - void DescriptorTable::UseReservation(int index, Device* object) + void DescriptorTable::UseReservation(int index, Device* object, char* pathcopy) { assert(index < index); assert(devices[index].dev != NULL); assert(devices[index].dev == RESERVED_DEVICE); + assert(devices[index].path == NULL); object->Refer(); devices[index].dev = object; devices[index].flags = 0; + devices[index].path = pathcopy; } bool DescriptorTable::Fork(DescriptorTable* forkinto) @@ -135,11 +145,16 @@ namespace Sortix { Device* dev = devices[i].dev; int flags = devices[i].flags; + char* path = devices[i].path; + if ( !dev || dev == RESERVED_DEVICE ) + path = NULL; + path = path ? String::Clone(path) : NULL; if ( flags & FD_CLOFORK ) { dev = NULL; flags = 0; } newlist[i].dev = dev; newlist[i].flags = flags; if ( !dev || dev == RESERVED_DEVICE ) { continue; } newlist[i].dev->Refer(); + newlist[i].path = path; } assert(!forkinto->devices); @@ -171,5 +186,12 @@ namespace Sortix assert(devices[index].dev); return devices[index].flags; } + + const char* DescriptorTable::GetPath(int index) + { + assert(0 <= index && index < numdevices); + assert(devices[index].dev); + return devices[index].path; + } } diff --git a/sortix/descriptors.h b/sortix/descriptors.h index 62389116..a4d1637d 100644 --- a/sortix/descriptors.h +++ b/sortix/descriptors.h @@ -33,6 +33,7 @@ namespace Sortix { Device* dev; int flags; + char* path; }; class DescriptorTable @@ -46,15 +47,16 @@ namespace Sortix DescriptorEntry* devices; public: - int Allocate(Device* object); + int Allocate(Device* object, char* pathcopy); int Reserve(); void Free(int index); - void UseReservation(int index, Device* object); + void UseReservation(int index, Device* object, char* pathcopy); bool Fork(DescriptorTable* forkinto); void OnExecute(); void Reset(); void SetFlags(int index, int flags); int GetFlags(int index); + const char* GetPath(int index); public: inline Device* Get(int index) diff --git a/sortix/filesystem.cpp b/sortix/filesystem.cpp index 0bc0de21..22242175 100644 --- a/sortix/filesystem.cpp +++ b/sortix/filesystem.cpp @@ -23,6 +23,7 @@ ******************************************************************************/ #include +#include #include #include #include "syscall.h" @@ -70,10 +71,13 @@ namespace Sortix int SysOpen(const char* path, int flags, mode_t mode) { + char* pathcopy = String::Clone(path); + if ( !pathcopy ) + return -1; Process* process = CurrentProcess(); Device* dev = Open(path, flags, mode); - if ( !dev ) { return -1; /* TODO: errno */ } - int fd = process->descriptors.Allocate(dev); + if ( !dev ) { delete[] pathcopy; return -1; } + int fd = process->descriptors.Allocate(dev, pathcopy); if ( fd < 0 ) { dev->Unref(); } int fdflags = 0; if ( flags & O_CLOEXEC ) { fdflags |= FD_CLOEXEC; } diff --git a/sortix/io.cpp b/sortix/io.cpp index d4b56373..326c9b1c 100644 --- a/sortix/io.cpp +++ b/sortix/io.cpp @@ -23,6 +23,7 @@ *******************************************************************************/ #include +#include #include #include #include @@ -179,7 +180,9 @@ namespace Sortix Process* process = CurrentProcess(); Device* dev = process->descriptors.Get(fd); if ( !dev ) { errno = EBADF; return -1; } - return process->descriptors.Allocate(dev); + const char* path = process->descriptors.GetPath(fd); + char* pathcopy = path ? String::Clone(path) : NULL; + return process->descriptors.Allocate(dev, pathcopy); } void Init() diff --git a/sortix/pipe.cpp b/sortix/pipe.cpp index 43d78d3c..35e7a050 100644 --- a/sortix/pipe.cpp +++ b/sortix/pipe.cpp @@ -338,8 +338,8 @@ namespace Sortix if ( !writing ) { delete reading; return -1; /* TODO: ENOMEM */ } Process* process = CurrentProcess(); - int readfd = process->descriptors.Allocate(reading); - int writefd = process->descriptors.Allocate(writing); + int readfd = process->descriptors.Allocate(reading, NULL); + int writefd = process->descriptors.Allocate(writing, NULL); if ( readfd < 0 || writefd < 0 ) { diff --git a/sortix/textterminal.cpp b/sortix/textterminal.cpp index 624fb0ce..23c2c98a 100644 --- a/sortix/textterminal.cpp +++ b/sortix/textterminal.cpp @@ -40,6 +40,10 @@ TextTerminal::TextTerminal(TextBufferHandle* textbufhandle) Reset(); } +TextTerminal::~TextTerminal() +{ +} + void TextTerminal::Reset() { vgacolor = DEFAULT_COLOR;