From cb4569c6157489111a23b3555aeb7dae64d4a77b Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Wed, 24 Oct 2012 19:43:11 +0200 Subject: [PATCH] Add fchownat(2). --- libc/Makefile | 1 + libc/fchownat.cpp | 36 ++++++++++++++++++++++++++++++ libc/include/unistd.h | 2 +- sortix/include/sortix/syscallnum.h | 3 ++- sortix/io.cpp | 15 +++++++++++-- 5 files changed, 53 insertions(+), 4 deletions(-) create mode 100644 libc/fchownat.cpp diff --git a/libc/Makefile b/libc/Makefile index ff9e2702..57399489 100644 --- a/libc/Makefile +++ b/libc/Makefile @@ -141,6 +141,7 @@ exit.o \ faccessat.o \ fchdir.o \ fchmod.o \ +fchownat.o \ fcloseall.o \ fcntl.o \ fddir-sortix.o \ diff --git a/libc/fchownat.cpp b/libc/fchownat.cpp new file mode 100644 index 00000000..3608e0a6 --- /dev/null +++ b/libc/fchownat.cpp @@ -0,0 +1,36 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2012. + + 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 . + + fchownat.cpp + Changes the owner and group of a file. + +*******************************************************************************/ + +#include +#include + +#include +#include + +DEFN_SYSCALL5(int, sys_fchownat, SYSCALL_FCHOWNAT, int, const char*, uid_t, gid_t, int); + +extern "C" int fchownat(int dirfd, const char* path, uid_t owner, gid_t group, int flags) +{ + return sys_fchownat(dirfd, path, owner, group, flags); +} diff --git a/libc/include/unistd.h b/libc/include/unistd.h index 916a263d..866399c4 100644 --- a/libc/include/unistd.h +++ b/libc/include/unistd.h @@ -93,7 +93,6 @@ char* crypt(const char*, const char*); char* ctermid(char*); void encrypt(char [64], int); int fchown(int, uid_t, gid_t); -int fchownat(int, const char*, uid_t, gid_t, int); int fdatasync(int); int fexecve(int, char* const [], char* const []); long fpathconf(int, int); @@ -161,6 +160,7 @@ int execvp(const char*, char* const []); pid_t fork(void); int faccessat(int, const char*, int, int); int fchdir(int); +int fchownat(int, const char*, uid_t, gid_t, int); int ftruncate(int, off_t); char* getcwd(char*, size_t); char* get_current_dir_name(void); diff --git a/sortix/include/sortix/syscallnum.h b/sortix/include/sortix/syscallnum.h index 7707658a..26c07234 100644 --- a/sortix/include/sortix/syscallnum.h +++ b/sortix/include/sortix/syscallnum.h @@ -87,6 +87,7 @@ #define SYSCALL_MKDIRAT 63 #define SYSCALL_FCHDIR 64 #define SYSCALL_TRUNCATEAT 65 -#define SYSCALL_MAX_NUM 66 /* index of highest constant + 1 */ +#define SYSCALL_FCHOWNAT 66 +#define SYSCALL_MAX_NUM 67 /* index of highest constant + 1 */ #endif diff --git a/sortix/io.cpp b/sortix/io.cpp index d4034e3e..0f1ed73d 100644 --- a/sortix/io.cpp +++ b/sortix/io.cpp @@ -351,14 +351,19 @@ static int sys_chdir(const char* path) return 0; } -static int sys_chown(const char* path, uid_t owner, gid_t group) +// TODO: fchown(2) + +static int sys_fchownat(int dirfd, const char* path, uid_t owner, gid_t group, int flags) { + if ( flags ) + return errno = ENOTSUP, -1; char* pathcopy = GetStringFromUser(path); if ( !pathcopy ) return -1; ioctx_t ctx; SetupUserIOCtx(&ctx); const char* relpath = pathcopy; - Ref from = PrepareLookup(&relpath); + Ref from = PrepareLookup(&relpath, dirfd); + if ( !from ) { delete[] pathcopy; return -1; } Ref desc = from->open(&ctx, relpath, O_WRONLY); from.Reset(); delete[] pathcopy; @@ -367,6 +372,11 @@ static int sys_chown(const char* path, uid_t owner, gid_t group) return desc->chown(&ctx, owner, group); } +static int sys_chown(const char* path, uid_t owner, gid_t group) +{ + return sys_fchownat(AT_FDCWD, path, owner, group, 0); +} + static int sys_chmod(const char* path, mode_t mode) { char* pathcopy = GetStringFromUser(path); @@ -461,6 +471,7 @@ void Init() Syscall::Register(SYSCALL_DUP2, (void*) sys_dup2); Syscall::Register(SYSCALL_FACCESSAT, (void*) sys_faccessat); Syscall::Register(SYSCALL_FCHDIR, (void*) sys_fchdir); + Syscall::Register(SYSCALL_FCHOWNAT, (void*) sys_fchownat); Syscall::Register(SYSCALL_FCNTL, (void*) sys_fcntl); Syscall::Register(SYSCALL_FSTATAT, (void*) sys_fstatat); Syscall::Register(SYSCALL_FSTAT, (void*) sys_fstat);