1
0
Fork 0
mirror of https://gitlab.com/sortix/sortix.git synced 2023-02-13 20:55:38 -05:00

Add closefrom(2).

This commit is contained in:
Jonas 'Sortie' Termansen 2015-05-13 17:48:53 +02:00
parent aa09f8cecc
commit 5915e2cd14
9 changed files with 83 additions and 8 deletions

View file

@ -1,6 +1,6 @@
/******************************************************************************* /*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2013, 2014. Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2013, 2014, 2015.
This file is part of Sortix. This file is part of Sortix.
@ -181,9 +181,8 @@ int DescriptorTable::Copy(int from, int to, int flags)
return to; return to;
} }
Ref<Descriptor> DescriptorTable::FreeKeep(int index) Ref<Descriptor> DescriptorTable::FreeKeepInternal(int index)
{ {
ScopedLock lock(&dtablelock);
if ( !IsGoodEntry(index) ) { errno = EBADF; return Ref<Descriptor>(NULL); } if ( !IsGoodEntry(index) ) { errno = EBADF; return Ref<Descriptor>(NULL); }
Ref<Descriptor> ret = entries[index].desc; Ref<Descriptor> ret = entries[index].desc;
entries[index].desc.Reset(); entries[index].desc.Reset();
@ -192,6 +191,12 @@ Ref<Descriptor> DescriptorTable::FreeKeep(int index)
return ret; return ret;
} }
Ref<Descriptor> DescriptorTable::FreeKeep(int index)
{
ScopedLock lock(&dtablelock);
return FreeKeepInternal(index);
}
void DescriptorTable::Free(int index) void DescriptorTable::Free(int index)
{ {
FreeKeep(index); FreeKeep(index);
@ -273,4 +278,24 @@ int DescriptorTable::Next(int index)
return index; return index;
} }
int DescriptorTable::CloseFrom(int index)
{
if ( index < 0 )
return errno = EBADF, -1;
ScopedLock lock(&dtablelock);
bool any = false;
while ( index < numentries )
{
if ( !IsGoodEntry(index) )
continue;
FreeKeepInternal(index);
any = true;
}
return any ? 0 : (errno = EBADF, -1);
}
} // namespace Sortix } // namespace Sortix

View file

@ -1,6 +1,6 @@
/******************************************************************************* /*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2013, 2014. Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2013, 2014, 2015.
This file is part of Sortix. This file is part of Sortix.
@ -54,12 +54,14 @@ public:
int GetFlags(int index); int GetFlags(int index);
int Previous(int index); int Previous(int index);
int Next(int index); int Next(int index);
int CloseFrom(int index);
private: private:
void Reset(); // Hey, reference counted. Don't call this. void Reset(); // Hey, reference counted. Don't call this.
bool IsGoodEntry(int i); bool IsGoodEntry(int i);
bool Enlargen(int atleast); bool Enlargen(int atleast);
int AllocateInternal(Ref<Descriptor> desc, int flags, int min_index); int AllocateInternal(Ref<Descriptor> desc, int flags, int min_index);
Ref<Descriptor> FreeKeepInternal(int index);
private: private:
kthread_mutex_t dtablelock; kthread_mutex_t dtablelock;

View file

@ -60,6 +60,7 @@ int sys_clock_gettimeres(clockid_t, struct timespec*, struct timespec*);
int sys_clock_nanosleep(clockid_t, int, const struct timespec*, struct timespec*); int sys_clock_nanosleep(clockid_t, int, const struct timespec*, struct timespec*);
int sys_clock_settimeres(clockid_t, const struct timespec*, const struct timespec*); int sys_clock_settimeres(clockid_t, const struct timespec*, const struct timespec*);
int sys_close(int); int sys_close(int);
int sys_closefrom(int);
int sys_connect(int, const void*, size_t); int sys_connect(int, const void*, size_t);
int sys_dispmsg_issue(void*, size_t); int sys_dispmsg_issue(void*, size_t);
int sys_dup(int); int sys_dup(int);

View file

@ -1,6 +1,6 @@
/******************************************************************************* /*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2013, 2014. Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2013, 2014, 2015.
This file is part of Sortix. This file is part of Sortix.
@ -177,6 +177,7 @@
#define SYSCALL_SETHOSTNAME 149 #define SYSCALL_SETHOSTNAME 149
#define SYSCALL_UNMOUNTAT 150 #define SYSCALL_UNMOUNTAT 150
#define SYSCALL_FSM_MOUNTAT 151 #define SYSCALL_FSM_MOUNTAT 151
#define SYSCALL_MAX_NUM 152 /* index of highest constant + 1 */ #define SYSCALL_CLOSEFROM 152
#define SYSCALL_MAX_NUM 153 /* index of highest constant + 1 */
#endif #endif

View file

@ -126,6 +126,15 @@ int sys_close(int fd)
return desc->sync(&ctx); return desc->sync(&ctx);
} }
int sys_closefrom(int fd)
{
ioctx_t ctx; SetupUserIOCtx(&ctx);
Ref<DescriptorTable> dtable = CurrentProcess()->GetDTable();
int ret = dtable->CloseFrom(fd);
dtable.Reset();
return ret;
}
int sys_dup(int fd) int sys_dup(int fd)
{ {
Ref<DescriptorTable> dtable = CurrentProcess()->GetDTable(); Ref<DescriptorTable> dtable = CurrentProcess()->GetDTable();

View file

@ -1,6 +1,6 @@
/******************************************************************************* /*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2013, 2014. Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2013, 2014, 2015.
This file is part of Sortix. This file is part of Sortix.
@ -187,6 +187,7 @@ void* syscall_list[SYSCALL_MAX_NUM + 1] =
[SYSCALL_SETHOSTNAME] = (void*) sys_sethostname, [SYSCALL_SETHOSTNAME] = (void*) sys_sethostname,
[SYSCALL_UNMOUNTAT] = (void*) sys_unmountat, [SYSCALL_UNMOUNTAT] = (void*) sys_unmountat,
[SYSCALL_FSM_MOUNTAT] = (void*) sys_fsm_mountat, [SYSCALL_FSM_MOUNTAT] = (void*) sys_fsm_mountat,
[SYSCALL_CLOSEFROM] = (void*) sys_closefrom,
[SYSCALL_MAX_NUM] = (void*) sys_bad_syscall, [SYSCALL_MAX_NUM] = (void*) sys_bad_syscall,
}; };
} // extern "C" } // extern "C"

View file

@ -553,6 +553,7 @@ unistd/alarm.o \
unistd/chdir.o \ unistd/chdir.o \
unistd/chown.o \ unistd/chown.o \
unistd/chroot.o \ unistd/chroot.o \
unistd/closefrom.o \
unistd/close.o \ unistd/close.o \
unistd/confstr.o \ unistd/confstr.o \
unistd/dup2.o \ unistd/dup2.o \

View file

@ -1,6 +1,6 @@
/******************************************************************************* /*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2013, 2014. Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2013, 2014, 2015.
This file is part of the Sortix C Library. This file is part of the Sortix C Library.
@ -545,6 +545,7 @@ size_t getpagesize(void);
/* Functions copied from elsewhere. */ /* Functions copied from elsewhere. */
#if __USE_SORTIX #if __USE_SORTIX
int chroot(const char*); int chroot(const char*);
int closefrom(int);
int dup3(int, int, int); int dup3(int, int, int);
int execvpe(const char*, char* const [], char* const []); int execvpe(const char*, char* const [], char* const []);
char* get_current_dir_name(void); char* get_current_dir_name(void);

34
libc/unistd/closefrom.cpp Normal file
View file

@ -0,0 +1,34 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2015.
This file is part of the Sortix C Library.
The Sortix C Library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
The Sortix C Library 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 Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
unistd/closefrom.cpp
Closes all file descriptors from a value and upwards.
*******************************************************************************/
#include <sys/syscall.h>
#include <unistd.h>
DEFN_SYSCALL1(int, sys_closefrom, SYSCALL_CLOSEFROM, int);
extern "C" int closefrom(int fd)
{
return sys_closefrom(fd);
}