Refactored the descriptor table and added FD_CLOEXEC and FD_CLOFORK.

open(2) now also respects O_CLOEXEC and O_CLOFORK.
This commit is contained in:
Jonas 'Sortie' Termansen 2012-03-04 21:06:49 +01:00
parent 777fc04682
commit 75b14aa821
5 changed files with 87 additions and 36 deletions

View File

@ -1,6 +1,6 @@
/****************************************************************************** /*******************************************************************************
COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2011. COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2012.
This file is part of Sortix. This file is part of Sortix.
@ -14,18 +14,19 @@
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details. details.
You should have received a copy of the GNU General Public License along You should have received a copy of the GNU General Public License along with
with Sortix. If not, see <http://www.gnu.org/licenses/>. Sortix. If not, see <http://www.gnu.org/licenses/>.
descriptors.cpp descriptors.cpp
Handles file descriptors, socket descriptors, and whatnot for each process. Handles file descriptors, socket descriptors, and whatnot for each process.
******************************************************************************/ *******************************************************************************/
#include "platform.h" #include "platform.h"
#include <libmaxsi/memory.h> #include <libmaxsi/memory.h>
#include "descriptors.h" #include "descriptors.h"
#include "device.h" #include "device.h"
#include <sortix/fcntl.h>
using namespace Maxsi; using namespace Maxsi;
@ -33,7 +34,7 @@ namespace Sortix
{ {
// When in doubt use brute-force. This class could easily be optimized. // When in doubt use brute-force. This class could easily be optimized.
Device* const reserveddevideptr = (Device*) 0x1UL; Device* const RESERVED_DEVICE = (Device*) 0x1UL;
DescriptorTable::DescriptorTable() DescriptorTable::DescriptorTable()
{ {
@ -50,9 +51,10 @@ namespace Sortix
{ {
for ( int i = 0; i < numdevices; i++ ) for ( int i = 0; i < numdevices; i++ )
{ {
if ( devices[i] == NULL || devices[i] == reserveddevideptr ) { continue; } Device* dev = devices[i].dev;
if ( !dev || dev == RESERVED_DEVICE ) { continue; }
devices[i]->Unref(); dev->Unref();
} }
delete[] devices; delete[] devices;
@ -64,10 +66,11 @@ namespace Sortix
{ {
for ( int i = 0; i < numdevices; i++ ) for ( int i = 0; i < numdevices; i++ )
{ {
if ( devices[i] == NULL ) if ( !devices[i].dev )
{ {
object->Refer(); object->Refer();
devices[i] = object; devices[i].dev = object;
devices[i].flags = 0;
return i; return i;
} }
} }
@ -75,15 +78,16 @@ namespace Sortix
int newlistlength = numdevices*2; int newlistlength = numdevices*2;
if ( newlistlength == 0 ) { newlistlength = 8; } if ( newlistlength == 0 ) { newlistlength = 8; }
Device** newlist = new Device*[newlistlength]; DescriptorEntry* newlist = new DescriptorEntry[newlistlength];
if ( newlist == NULL ) { return -1; } if ( newlist == NULL ) { return -1; }
if ( devices != NULL ) if ( devices != NULL )
{ {
Memory::Copy(newlist, devices, sizeof(Device*) * numdevices); Memory::Copy(newlist, devices, sizeof(*devices) * numdevices);
} }
Memory::Set(newlist + numdevices, 0, sizeof(Device*) * (newlistlength-numdevices)); size_t numpadded = newlistlength-numdevices;
Memory::Set(newlist + numdevices, 0, sizeof(*devices) * numpadded);
delete[] devices; delete[] devices;
@ -96,50 +100,77 @@ namespace Sortix
void DescriptorTable::Free(int index) void DescriptorTable::Free(int index)
{ {
ASSERT(index < numdevices); ASSERT(index < numdevices);
ASSERT(devices[index] != NULL); ASSERT(devices[index].dev);
if ( devices[index] != reserveddevideptr ) if ( devices[index].dev != RESERVED_DEVICE )
{ {
devices[index]->Unref(); devices[index].dev->Unref();
} }
devices[index] = NULL; devices[index].dev = NULL;
devices[index].flags = 0;
} }
int DescriptorTable::Reserve() int DescriptorTable::Reserve()
{ {
return Allocate(reserveddevideptr); return Allocate(RESERVED_DEVICE);
} }
void DescriptorTable::UseReservation(int index, Device* object) void DescriptorTable::UseReservation(int index, Device* object)
{ {
ASSERT(index < index); ASSERT(index < index);
ASSERT(devices[index] != NULL); ASSERT(devices[index].dev != NULL);
ASSERT(devices[index] == reserveddevideptr); ASSERT(devices[index].dev == RESERVED_DEVICE);
object->Refer(); object->Refer();
devices[index] = object; devices[index].dev = object;
devices[index].flags = 0;
} }
bool DescriptorTable::Fork(DescriptorTable* forkinto) bool DescriptorTable::Fork(DescriptorTable* forkinto)
{ {
Device** newlist = new Device*[numdevices]; DescriptorEntry* newlist = new DescriptorEntry[numdevices];
if ( newlist == NULL ) { return false; } if ( !newlist ) { return false; }
for ( int i = 0; i < numdevices; i++ ) for ( int i = 0; i < numdevices; i++ )
{ {
// TODO: Possibly deal with a potential O_CLOFORK! Device* dev = devices[i].dev;
newlist[i] = devices[i]; int flags = devices[i].flags;
if ( !devices[i] ) { continue; } if ( flags & FD_CLOFORK ) { dev = NULL; flags = 0; }
if ( devices[i] == reserveddevideptr ) { continue; } newlist[i].dev = dev;
newlist[i]->Refer(); newlist[i].flags = flags;
if ( !dev || dev == RESERVED_DEVICE ) { continue; }
newlist[i].dev->Refer();
} }
ASSERT(forkinto->devices == NULL); ASSERT(!forkinto->devices);
forkinto->devices = newlist; forkinto->devices = newlist;
forkinto->numdevices = numdevices; forkinto->numdevices = numdevices;
return true; return true;
} }
void DescriptorTable::OnExecute()
{
for ( int i = 0; i < numdevices; i++ )
{
if ( devices[i].flags & FD_CLOEXEC ) { Free(i); }
}
}
void DescriptorTable::SetFlags(int index, int flags)
{
ASSERT(0 <= index && index < numdevices);
ASSERT(devices[index].dev);
devices[index].flags = flags;
}
int DescriptorTable::GetFlags(int index)
{
ASSERT(0 <= index && index < numdevices);
ASSERT(devices[index].dev);
return devices[index].flags;
}
} }

View File

@ -1,6 +1,6 @@
/****************************************************************************** /*******************************************************************************
COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2011. COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2012.
This file is part of Sortix. This file is part of Sortix.
@ -14,13 +14,13 @@
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details. details.
You should have received a copy of the GNU General Public License along You should have received a copy of the GNU General Public License along with
with Sortix. If not, see <http://www.gnu.org/licenses/>. Sortix. If not, see <http://www.gnu.org/licenses/>.
descriptors.h descriptors.h
Handles file descriptors, socket descriptors, and whatnot for each process. Handles file descriptors, socket descriptors, and whatnot for each process.
******************************************************************************/ *******************************************************************************/
#ifndef SORTIX_DESCRIPTORS_H #ifndef SORTIX_DESCRIPTORS_H
#define SORTIX_DESCRIPTORS_H #define SORTIX_DESCRIPTORS_H
@ -29,6 +29,12 @@ namespace Sortix
{ {
class Device; class Device;
struct DescriptorEntry
{
Device* dev;
int flags;
};
class DescriptorTable class DescriptorTable
{ {
public: public:
@ -37,7 +43,7 @@ namespace Sortix
private: private:
int numdevices; int numdevices;
Device** devices; DescriptorEntry* devices;
public: public:
int Allocate(Device* object); int Allocate(Device* object);
@ -45,14 +51,17 @@ namespace Sortix
void Free(int index); void Free(int index);
void UseReservation(int index, Device* object); void UseReservation(int index, Device* object);
bool Fork(DescriptorTable* forkinto); bool Fork(DescriptorTable* forkinto);
void OnExecute();
void Reset(); void Reset();
void SetFlags(int index, int flags);
int GetFlags(int index);
public: public:
inline Device* Get(int index) inline Device* Get(int index)
{ {
if ( !devices ) { return NULL; } if ( !devices ) { return NULL; }
if ( index < 0 || numdevices <= index ) { return NULL; } if ( index < 0 || numdevices <= index ) { return NULL; }
return devices[index]; return devices[index].dev;
} }
}; };

View File

@ -41,6 +41,10 @@ __BEGIN_DECLS
#define O_DIRECTORY (1<<6) #define O_DIRECTORY (1<<6)
#define O_EXCL (1<<7) #define O_EXCL (1<<7)
#define O_TRUNC (1<<8) #define O_TRUNC (1<<8)
#define O_CLOFORK (1<<9)
#define FD_CLOEXEC (1<<0)
#define FD_CLOFORK (1<<1)
__END_DECLS __END_DECLS

View File

@ -31,6 +31,7 @@
#include "filesystem.h" #include "filesystem.h"
#include "directory.h" #include "directory.h"
#include "mount.h" #include "mount.h"
#include <sortix/fcntl.h>
using namespace Maxsi; using namespace Maxsi;
@ -75,6 +76,10 @@ namespace Sortix
if ( !dev ) { return -1; /* TODO: errno */ } if ( !dev ) { return -1; /* TODO: errno */ }
int fd = process->descriptors.Allocate(dev); int fd = process->descriptors.Allocate(dev);
if ( fd < 0 ) { dev->Unref(); } if ( fd < 0 ) { dev->Unref(); }
int fdflags = 0;
if ( flags & O_CLOEXEC ) { fdflags |= FD_CLOEXEC; }
if ( flags & O_CLOFORK ) { fdflags |= FD_CLOFORK; }
process->descriptors.SetFlags(fd, fdflags);
return fd; return fd;
} }

View File

@ -288,6 +288,8 @@ namespace Sortix
ExecuteCPU(argc, stackargv, stackpos, entry, regs); ExecuteCPU(argc, stackargv, stackpos, entry, regs);
descriptors.OnExecute();
return 0; return 0;
} }