Add fchdirat(2).

This commit is contained in:
Jonas 'Sortie' Termansen 2013-05-29 14:03:53 +02:00
parent 3093623f18
commit ddf3dd329f
6 changed files with 50 additions and 8 deletions

View File

@ -198,6 +198,7 @@ _exit.o \
_Exit.o \
exit.o \
faccessat.o \
fchdirat.o \
fchdir.o \
fchmodat.o \
fchmod.o \

View File

@ -1,6 +1,6 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012.
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2013
This file is part of the Sortix C Library.
@ -22,12 +22,10 @@
*******************************************************************************/
#include <sys/syscall.h>
#include <fcntl.h>
#include <unistd.h>
DEFN_SYSCALL1(int, SysChDir, SYSCALL_CHDIR, const char*);
extern "C" int chdir(const char* path)
{
return SysChDir(path);
return fchdirat(AT_FDCWD, path);
}

34
libc/fchdirat.cpp Normal file
View File

@ -0,0 +1,34 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2013
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/>.
fchdirat.cpp
Changes the current working directory.
*******************************************************************************/
#include <sys/syscall.h>
#include <unistd.h>
DEFN_SYSCALL2(int, sys_fchdirat, SYSCALL_FCHDIRAT, int, const char*);
extern "C" int fchdirat(int dirfd, const char* path)
{
return sys_fchdirat(dirfd, path);
}

View File

@ -316,6 +316,7 @@ int execvp(const char*, char* const []);
pid_t fork(void);
int faccessat(int, const char*, int, int);
int fchdir(int);
int fchdirat(int, const char*);
int fchown(int, uid_t, gid_t);
int fchownat(int, const char*, uid_t, gid_t, int);
int fsync(int);

View File

@ -129,6 +129,7 @@
#define SYSCALL_CLOCK_NANOSLEEP 105
#define SYSCALL_TIMENS 106
#define SYSCALL_UMASK 107
#define SYSCALL_MAX_NUM 108 /* index of highest constant + 1 */
#define SYSCALL_FCHDIRAT 108
#define SYSCALL_MAX_NUM 109 /* index of highest constant + 1 */
#endif

View File

@ -367,14 +367,14 @@ static int sys_fchdir(int fd)
return 0;
}
static int sys_chdir(const char* path)
static int sys_fchdirat(int dirfd, const char* path)
{
char* pathcopy = GetStringFromUser(path);
if ( !pathcopy )
return -1;
ioctx_t ctx; SetupUserIOCtx(&ctx);
const char* relpath = pathcopy;
Ref<Descriptor> from = PrepareLookup(&relpath);
Ref<Descriptor> from = PrepareLookup(&relpath, dirfd);
Ref<Descriptor> desc = from->open(&ctx, relpath, O_READ | O_DIRECTORY);
from.Reset();
delete[] pathcopy;
@ -384,6 +384,12 @@ static int sys_chdir(const char* path)
return 0;
}
// TODO: This system call is replaced by sys_fchownat, will be removed soon.
static int sys_chdir(const char* path)
{
return sys_fchdirat(AT_FDCWD, path);
}
static int sys_fchown(int fd, uid_t owner, gid_t group)
{
Ref<Descriptor> desc = CurrentProcess()->GetDescriptor(fd);
@ -864,6 +870,7 @@ void Init()
Syscall::Register(SYSCALL_DUP2, (void*) sys_dup2);
Syscall::Register(SYSCALL_DUP, (void*) sys_dup);
Syscall::Register(SYSCALL_FACCESSAT, (void*) sys_faccessat);
Syscall::Register(SYSCALL_FCHDIRAT, (void*) sys_fchdirat);
Syscall::Register(SYSCALL_FCHDIR, (void*) sys_fchdir);
Syscall::Register(SYSCALL_FCHMODAT, (void*) sys_fchmodat);
Syscall::Register(SYSCALL_FCHMOD, (void*) sys_fchmod);