2012-08-07 18:19:44 -04:00
|
|
|
/*******************************************************************************
|
|
|
|
|
2016-01-18 08:36:25 -05:00
|
|
|
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2013, 2014, 2015, 2016.
|
2012-08-07 18:19:44 -04:00
|
|
|
|
|
|
|
This file is part of Sortix.
|
|
|
|
|
|
|
|
Sortix is free software: you can redistribute it and/or modify it under the
|
|
|
|
terms of the GNU General Public License as published by the Free Software
|
|
|
|
Foundation, either version 3 of the License, or (at your option) any later
|
|
|
|
version.
|
|
|
|
|
|
|
|
Sortix is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along with
|
|
|
|
Sortix. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
dtable.cpp
|
|
|
|
Table of file descriptors.
|
|
|
|
|
|
|
|
*******************************************************************************/
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
2013-01-15 19:45:51 -05:00
|
|
|
#include <limits.h>
|
2012-08-07 18:19:44 -04:00
|
|
|
#include <string.h>
|
|
|
|
|
2013-10-26 20:42:10 -04:00
|
|
|
#include <sortix/fcntl.h>
|
|
|
|
|
|
|
|
#include <sortix/kernel/descriptor.h>
|
|
|
|
#include <sortix/kernel/dtable.h>
|
|
|
|
#include <sortix/kernel/kernel.h>
|
|
|
|
#include <sortix/kernel/kthread.h>
|
|
|
|
#include <sortix/kernel/process.h>
|
|
|
|
#include <sortix/kernel/refcount.h>
|
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
namespace Sortix {
|
|
|
|
|
|
|
|
DescriptorTable::DescriptorTable()
|
|
|
|
{
|
|
|
|
dtablelock = KTHREAD_MUTEX_INITIALIZER;
|
|
|
|
entries = NULL;
|
|
|
|
numentries = 0;
|
2014-08-20 18:02:37 -04:00
|
|
|
first_not_taken = 0;
|
2012-08-07 18:19:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
DescriptorTable::~DescriptorTable()
|
|
|
|
{
|
|
|
|
Reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DescriptorTable::IsGoodEntry(int i)
|
|
|
|
{
|
2015-09-19 13:37:31 -04:00
|
|
|
return 0 <= i && i < numentries && entries[i].desc;
|
2012-08-07 18:19:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Ref<DescriptorTable> DescriptorTable::Fork()
|
|
|
|
{
|
|
|
|
ScopedLock lock(&dtablelock);
|
|
|
|
Ref<DescriptorTable> ret(new DescriptorTable);
|
2015-09-19 13:37:31 -04:00
|
|
|
if ( !ret )
|
|
|
|
return Ref<DescriptorTable>(NULL);
|
2012-08-07 18:19:44 -04:00
|
|
|
ret->entries = new dtableent_t[numentries];
|
2015-09-19 13:37:31 -04:00
|
|
|
if ( !ret->entries )
|
|
|
|
return Ref<DescriptorTable>(NULL);
|
2014-08-20 18:02:37 -04:00
|
|
|
ret->first_not_taken = 0;
|
2015-09-19 13:37:31 -04:00
|
|
|
ret->numentries = numentries;
|
|
|
|
for ( int i = 0; i < numentries; i++ )
|
2012-08-07 18:19:44 -04:00
|
|
|
{
|
2015-09-19 13:37:31 -04:00
|
|
|
if ( !entries[i].desc || entries[i].flags & FD_CLOFORK )
|
2014-08-20 18:02:37 -04:00
|
|
|
{
|
2015-09-19 13:37:31 -04:00
|
|
|
//ret->entries[i].desc = NULL; // Constructor did this already.
|
|
|
|
ret->entries[i].flags = 0;
|
|
|
|
continue;
|
2014-08-20 18:02:37 -04:00
|
|
|
}
|
2015-09-19 13:37:31 -04:00
|
|
|
ret->entries[i] = entries[i];
|
|
|
|
if ( ret->first_not_taken == i )
|
|
|
|
ret->first_not_taken = i + 1;
|
2012-08-07 18:19:44 -04:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<Descriptor> DescriptorTable::Get(int index)
|
|
|
|
{
|
|
|
|
ScopedLock lock(&dtablelock);
|
2015-09-19 13:37:31 -04:00
|
|
|
if ( !IsGoodEntry(index) )
|
|
|
|
return errno = EBADF, Ref<Descriptor>(NULL);
|
2012-08-07 18:19:44 -04:00
|
|
|
return entries[index].desc;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DescriptorTable::Enlargen(int atleast)
|
|
|
|
{
|
2015-09-19 13:37:31 -04:00
|
|
|
if ( numentries == INT_MAX )
|
|
|
|
return errno = EMFILE, false; // Cannot enlargen any more.
|
|
|
|
// TODO: Modern overflow checks.
|
2012-08-07 18:19:44 -04:00
|
|
|
int newnumentries = INT_MAX - numentries < numentries ?
|
|
|
|
INT_MAX :
|
|
|
|
numentries ? 2 * numentries : 8;
|
|
|
|
if ( newnumentries < atleast )
|
|
|
|
newnumentries = atleast;
|
|
|
|
dtableent_t* newentries = new dtableent_t[newnumentries];
|
|
|
|
if ( !newentries )
|
|
|
|
return false;
|
|
|
|
for ( int i = 0; i < numentries; i++ )
|
2015-09-19 13:37:31 -04:00
|
|
|
{
|
|
|
|
newentries[i] = entries[i];
|
|
|
|
entries[i].desc.Reset();
|
|
|
|
}
|
2012-08-07 18:19:44 -04:00
|
|
|
for ( int i = numentries; i < newnumentries; i++ )
|
2015-09-19 13:37:31 -04:00
|
|
|
{
|
|
|
|
//newentries[i].desc = NULL; // Constructor did this already.
|
2012-08-07 18:19:44 -04:00
|
|
|
newentries[i].flags = 0;
|
2015-09-19 13:37:31 -04:00
|
|
|
}
|
|
|
|
delete[] entries;
|
|
|
|
entries = newentries;
|
2012-08-07 18:19:44 -04:00
|
|
|
numentries = newnumentries;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-09-19 13:37:31 -04:00
|
|
|
int DescriptorTable::AllocateInternal(Ref<Descriptor> desc,
|
|
|
|
int flags,
|
2013-09-24 07:14:18 -04:00
|
|
|
int min_index)
|
2012-08-07 18:19:44 -04:00
|
|
|
{
|
2013-09-24 07:14:18 -04:00
|
|
|
// dtablelock is held.
|
2012-08-07 18:19:44 -04:00
|
|
|
if ( flags & ~__FD_ALLOWED_FLAGS )
|
|
|
|
return errno = EINVAL, -1;
|
2013-09-24 07:14:18 -04:00
|
|
|
if ( min_index < 0 )
|
|
|
|
return errno = EINVAL, -1;
|
2014-08-20 18:02:37 -04:00
|
|
|
if ( min_index < first_not_taken )
|
|
|
|
min_index = first_not_taken;
|
2013-09-24 07:14:18 -04:00
|
|
|
for ( int i = min_index; i < numentries; i++ )
|
2014-08-20 18:02:37 -04:00
|
|
|
{
|
2015-09-19 13:37:31 -04:00
|
|
|
if ( entries[i].desc )
|
|
|
|
continue;
|
|
|
|
entries[i].desc = desc;
|
|
|
|
entries[i].flags = flags;
|
|
|
|
if ( first_not_taken == i )
|
|
|
|
first_not_taken = i + 1;
|
|
|
|
return i;
|
2014-08-20 18:02:37 -04:00
|
|
|
}
|
2015-09-19 13:37:31 -04:00
|
|
|
first_not_taken = numentries;
|
2012-08-07 18:19:44 -04:00
|
|
|
int oldnumentries = numentries;
|
2013-09-24 07:14:18 -04:00
|
|
|
if ( !Enlargen(min_index) )
|
2012-08-07 18:19:44 -04:00
|
|
|
return -1;
|
2015-09-19 13:37:31 -04:00
|
|
|
int i = oldnumentries;
|
2012-08-07 18:19:44 -04:00
|
|
|
entries[i].desc = desc;
|
|
|
|
entries[i].flags = flags;
|
2015-09-19 13:37:31 -04:00
|
|
|
if ( first_not_taken == i )
|
|
|
|
first_not_taken = i + 1;
|
2012-08-07 18:19:44 -04:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2013-09-24 07:14:18 -04:00
|
|
|
int DescriptorTable::Allocate(Ref<Descriptor> desc, int flags, int min_index)
|
|
|
|
{
|
|
|
|
ScopedLock lock(&dtablelock);
|
|
|
|
return AllocateInternal(desc, flags, min_index);
|
|
|
|
}
|
|
|
|
|
|
|
|
int DescriptorTable::Allocate(int src_index, int flags, int min_index)
|
|
|
|
{
|
|
|
|
ScopedLock lock(&dtablelock);
|
|
|
|
if ( !IsGoodEntry(src_index) )
|
|
|
|
return errno = EBADF, -1;
|
|
|
|
return AllocateInternal(entries[src_index].desc, flags, min_index);
|
|
|
|
}
|
|
|
|
|
2013-09-23 14:41:31 -04:00
|
|
|
int DescriptorTable::Copy(int from, int to, int flags)
|
2012-08-07 18:19:44 -04:00
|
|
|
{
|
2013-09-23 14:41:31 -04:00
|
|
|
if ( flags & ~__FD_ALLOWED_FLAGS )
|
|
|
|
return errno = EINVAL, -1;
|
2012-08-07 18:19:44 -04:00
|
|
|
ScopedLock lock(&dtablelock);
|
|
|
|
if ( from < 0 || to < 0 )
|
|
|
|
return errno = EINVAL, -1;
|
|
|
|
if ( !(from < numentries) )
|
|
|
|
return errno = EBADF, -1;
|
|
|
|
if ( !entries[from].desc )
|
|
|
|
return errno = EBADF, -1;
|
2013-09-23 14:41:31 -04:00
|
|
|
if ( from == to )
|
|
|
|
return errno = EINVAL, -1;
|
2012-08-07 18:19:44 -04:00
|
|
|
while ( !(to < numentries) )
|
2015-09-19 13:37:31 -04:00
|
|
|
{
|
|
|
|
if ( to == INT_MAX )
|
|
|
|
return errno = EBADF, -1;
|
|
|
|
if ( !Enlargen(to + 1) )
|
2012-08-07 18:19:44 -04:00
|
|
|
return -1;
|
2015-09-19 13:37:31 -04:00
|
|
|
}
|
2012-08-07 18:19:44 -04:00
|
|
|
if ( entries[to].desc != entries[from].desc )
|
|
|
|
{
|
|
|
|
if ( entries[to].desc )
|
2015-09-19 13:37:31 -04:00
|
|
|
{
|
|
|
|
// TODO: Should this be synced or otherwise properly closed?
|
|
|
|
}
|
2012-08-07 18:19:44 -04:00
|
|
|
entries[to].desc = entries[from].desc;
|
|
|
|
}
|
2013-09-23 14:41:31 -04:00
|
|
|
entries[to].flags = flags;
|
2015-09-19 13:37:31 -04:00
|
|
|
if ( first_not_taken == to )
|
|
|
|
first_not_taken = to + 1;
|
2012-08-07 18:19:44 -04:00
|
|
|
return to;
|
|
|
|
}
|
|
|
|
|
2015-05-13 11:48:53 -04:00
|
|
|
Ref<Descriptor> DescriptorTable::FreeKeepInternal(int index)
|
2012-08-07 18:19:44 -04:00
|
|
|
{
|
2015-09-19 13:37:31 -04:00
|
|
|
if ( !IsGoodEntry(index) )
|
|
|
|
return errno = EBADF, Ref<Descriptor>(NULL);
|
2012-08-07 18:19:44 -04:00
|
|
|
Ref<Descriptor> ret = entries[index].desc;
|
|
|
|
entries[index].desc.Reset();
|
2015-09-19 13:37:31 -04:00
|
|
|
entries[index].flags = 0;
|
2014-08-20 18:02:37 -04:00
|
|
|
if ( index < first_not_taken )
|
|
|
|
first_not_taken = index;
|
2012-08-07 18:19:44 -04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-05-13 11:48:53 -04:00
|
|
|
Ref<Descriptor> DescriptorTable::FreeKeep(int index)
|
|
|
|
{
|
|
|
|
ScopedLock lock(&dtablelock);
|
|
|
|
return FreeKeepInternal(index);
|
|
|
|
}
|
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
void DescriptorTable::Free(int index)
|
|
|
|
{
|
|
|
|
FreeKeep(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DescriptorTable::OnExecute()
|
|
|
|
{
|
|
|
|
ScopedLock lock(&dtablelock);
|
|
|
|
for ( int i = 0; i < numentries; i++ )
|
|
|
|
{
|
|
|
|
if ( !entries[i].desc )
|
|
|
|
continue;
|
|
|
|
if ( !(entries[i].flags & FD_CLOEXEC) )
|
|
|
|
continue;
|
|
|
|
entries[i].desc.Reset();
|
2014-08-20 18:02:37 -04:00
|
|
|
if ( i < first_not_taken )
|
|
|
|
first_not_taken = i;
|
2012-08-07 18:19:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DescriptorTable::Reset()
|
|
|
|
{
|
|
|
|
ScopedLock lock(&dtablelock);
|
2015-09-19 13:37:31 -04:00
|
|
|
for ( int i = 0; i < numentries; i++ )
|
|
|
|
if ( entries[i].desc )
|
|
|
|
entries[i].desc.Reset();
|
2012-08-07 18:19:44 -04:00
|
|
|
numentries = 0;
|
|
|
|
delete[] entries;
|
|
|
|
entries = NULL;
|
2014-08-20 18:02:37 -04:00
|
|
|
first_not_taken = 0;
|
2012-08-07 18:19:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DescriptorTable::SetFlags(int index, int flags)
|
|
|
|
{
|
|
|
|
if ( flags & ~__FD_ALLOWED_FLAGS )
|
2015-09-19 13:37:31 -04:00
|
|
|
return errno = EINVAL, false;
|
2012-08-07 18:19:44 -04:00
|
|
|
ScopedLock lock(&dtablelock);
|
2015-09-19 13:37:31 -04:00
|
|
|
if ( !IsGoodEntry(index) )
|
|
|
|
return errno = EBADF, false;
|
2012-08-07 18:19:44 -04:00
|
|
|
entries[index].flags = flags;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int DescriptorTable::GetFlags(int index)
|
|
|
|
{
|
|
|
|
ScopedLock lock(&dtablelock);
|
2015-09-19 13:37:31 -04:00
|
|
|
if ( !IsGoodEntry(index) )
|
|
|
|
return errno = EBADF, -1;
|
2012-08-07 18:19:44 -04:00
|
|
|
return entries[index].flags;
|
|
|
|
}
|
|
|
|
|
2014-08-09 06:29:35 -04:00
|
|
|
int DescriptorTable::Previous(int index)
|
|
|
|
{
|
|
|
|
ScopedLock lock(&dtablelock);
|
|
|
|
if ( index < 0 )
|
|
|
|
index = numentries;
|
|
|
|
do index--;
|
|
|
|
while ( 0 <= index && !IsGoodEntry(index) );
|
|
|
|
if ( index < 0 )
|
|
|
|
return errno = EBADF, -1;
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
|
|
|
int DescriptorTable::Next(int index)
|
|
|
|
{
|
|
|
|
ScopedLock lock(&dtablelock);
|
|
|
|
if ( index < 0 )
|
|
|
|
index = -1;
|
|
|
|
if ( numentries <= index )
|
|
|
|
return errno = EBADF, -1;
|
|
|
|
do index++;
|
|
|
|
while ( index < numentries && !IsGoodEntry(index) );
|
|
|
|
if ( numentries <= index )
|
|
|
|
return errno = EBADF, -1;
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
2015-05-13 11:48:53 -04:00
|
|
|
int DescriptorTable::CloseFrom(int index)
|
|
|
|
{
|
|
|
|
if ( index < 0 )
|
|
|
|
return errno = EBADF, -1;
|
|
|
|
ScopedLock lock(&dtablelock);
|
|
|
|
bool any = false;
|
2016-01-18 08:36:25 -05:00
|
|
|
for ( ; index < numentries; index++ )
|
2015-05-13 11:48:53 -04:00
|
|
|
{
|
|
|
|
if ( !IsGoodEntry(index) )
|
|
|
|
continue;
|
|
|
|
FreeKeepInternal(index);
|
|
|
|
any = true;
|
|
|
|
}
|
|
|
|
return any ? 0 : (errno = EBADF, -1);
|
|
|
|
}
|
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
} // namespace Sortix
|