mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 20:07:35 +00:00
Kernel: Use custody_for_dirfd() in more syscalls
This simplifies a lot of syscalls, some of which were doing very unnecessarily verbose things instead of calling this.
This commit is contained in:
parent
f0c9c5e076
commit
97ac4601f5
6 changed files with 7 additions and 87 deletions
|
@ -17,17 +17,7 @@ ErrorOr<FlatPtr> Process::sys$chmod(Userspace<Syscall::SC_chmod_params const*> u
|
||||||
TRY(require_promise(Pledge::fattr));
|
TRY(require_promise(Pledge::fattr));
|
||||||
auto params = TRY(copy_typed_from_user(user_params));
|
auto params = TRY(copy_typed_from_user(user_params));
|
||||||
auto path = TRY(get_syscall_path_argument(params.path));
|
auto path = TRY(get_syscall_path_argument(params.path));
|
||||||
|
auto base = TRY(custody_for_dirfd(params.dirfd));
|
||||||
RefPtr<Custody> base;
|
|
||||||
if (params.dirfd == AT_FDCWD) {
|
|
||||||
base = current_directory();
|
|
||||||
} else {
|
|
||||||
auto base_description = TRY(open_file_description(params.dirfd));
|
|
||||||
if (!base_description->custody())
|
|
||||||
return EINVAL;
|
|
||||||
base = base_description->custody();
|
|
||||||
}
|
|
||||||
|
|
||||||
TRY(VirtualFileSystem::the().chmod(credentials(), path->view(), params.mode, *base, params.follow_symlinks ? 0 : O_NOFOLLOW_NOERROR));
|
TRY(VirtualFileSystem::the().chmod(credentials(), path->view(), params.mode, *base, params.follow_symlinks ? 0 : O_NOFOLLOW_NOERROR));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,17 +27,7 @@ ErrorOr<FlatPtr> Process::sys$chown(Userspace<Syscall::SC_chown_params const*> u
|
||||||
TRY(require_promise(Pledge::chown));
|
TRY(require_promise(Pledge::chown));
|
||||||
auto params = TRY(copy_typed_from_user(user_params));
|
auto params = TRY(copy_typed_from_user(user_params));
|
||||||
auto path = TRY(get_syscall_path_argument(params.path));
|
auto path = TRY(get_syscall_path_argument(params.path));
|
||||||
|
auto base = TRY(custody_for_dirfd(params.dirfd));
|
||||||
RefPtr<Custody> base;
|
|
||||||
if (params.dirfd == AT_FDCWD) {
|
|
||||||
base = current_directory();
|
|
||||||
} else {
|
|
||||||
auto base_description = TRY(open_file_description(params.dirfd));
|
|
||||||
if (!base_description->custody())
|
|
||||||
return EINVAL;
|
|
||||||
base = base_description->custody();
|
|
||||||
}
|
|
||||||
|
|
||||||
TRY(VirtualFileSystem::the().chown(credentials(), path->view(), params.uid, params.gid, *base, params.follow_symlinks ? 0 : O_NOFOLLOW_NOERROR));
|
TRY(VirtualFileSystem::the().chown(credentials(), path->view(), params.uid, params.gid, *base, params.follow_symlinks ? 0 : O_NOFOLLOW_NOERROR));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -55,18 +55,7 @@ ErrorOr<FlatPtr> Process::sys$open(Userspace<Syscall::SC_open_params const*> use
|
||||||
dbgln_if(IO_DEBUG, "sys$open(dirfd={}, path='{}', options={}, mode={})", dirfd, path->view(), options, mode);
|
dbgln_if(IO_DEBUG, "sys$open(dirfd={}, path='{}', options={}, mode={})", dirfd, path->view(), options, mode);
|
||||||
|
|
||||||
auto fd_allocation = TRY(allocate_fd());
|
auto fd_allocation = TRY(allocate_fd());
|
||||||
RefPtr<Custody> base;
|
auto base = TRY(custody_for_dirfd(dirfd));
|
||||||
if (dirfd == AT_FDCWD) {
|
|
||||||
base = current_directory();
|
|
||||||
} else {
|
|
||||||
auto base_description = TRY(open_file_description(dirfd));
|
|
||||||
if (!base_description->is_directory())
|
|
||||||
return ENOTDIR;
|
|
||||||
if (!base_description->custody())
|
|
||||||
return EINVAL;
|
|
||||||
base = base_description->custody();
|
|
||||||
}
|
|
||||||
|
|
||||||
auto description = TRY(VirtualFileSystem::the().open(credentials(), path->view(), options, mode & ~umask(), *base));
|
auto description = TRY(VirtualFileSystem::the().open(credentials(), path->view(), options, mode & ~umask(), *base));
|
||||||
|
|
||||||
if (description->inode() && description->inode()->bound_socket())
|
if (description->inode() && description->inode()->bound_socket())
|
||||||
|
|
|
@ -28,18 +28,7 @@ ErrorOr<FlatPtr> Process::sys$stat(Userspace<Syscall::SC_stat_params const*> use
|
||||||
auto params = TRY(copy_typed_from_user(user_params));
|
auto params = TRY(copy_typed_from_user(user_params));
|
||||||
|
|
||||||
auto path = TRY(get_syscall_path_argument(params.path));
|
auto path = TRY(get_syscall_path_argument(params.path));
|
||||||
|
auto base = TRY(custody_for_dirfd(params.dirfd));
|
||||||
RefPtr<Custody> base;
|
|
||||||
if (params.dirfd == AT_FDCWD) {
|
|
||||||
base = current_directory();
|
|
||||||
} else {
|
|
||||||
auto base_description = TRY(open_file_description(params.dirfd));
|
|
||||||
if (!base_description->is_directory())
|
|
||||||
return ENOTDIR;
|
|
||||||
if (!base_description->custody())
|
|
||||||
return EINVAL;
|
|
||||||
base = base_description->custody();
|
|
||||||
}
|
|
||||||
auto metadata = TRY(VirtualFileSystem::the().lookup_metadata(credentials(), path->view(), *base, params.follow_symlinks ? 0 : O_NOFOLLOW_NOERROR));
|
auto metadata = TRY(VirtualFileSystem::the().lookup_metadata(credentials(), path->view(), *base, params.follow_symlinks ? 0 : O_NOFOLLOW_NOERROR));
|
||||||
auto statbuf = TRY(metadata.stat());
|
auto statbuf = TRY(metadata.stat());
|
||||||
TRY(copy_to_user(params.statbuf, &statbuf));
|
TRY(copy_to_user(params.statbuf, &statbuf));
|
||||||
|
|
|
@ -19,15 +19,7 @@ ErrorOr<FlatPtr> Process::sys$unlink(int dirfd, Userspace<char const*> user_path
|
||||||
if (flags & ~AT_REMOVEDIR)
|
if (flags & ~AT_REMOVEDIR)
|
||||||
return Error::from_errno(EINVAL);
|
return Error::from_errno(EINVAL);
|
||||||
|
|
||||||
RefPtr<Custody> base;
|
auto base = TRY(custody_for_dirfd(dirfd));
|
||||||
if (dirfd == AT_FDCWD) {
|
|
||||||
base = current_directory();
|
|
||||||
} else {
|
|
||||||
auto base_description = TRY(open_file_description(dirfd));
|
|
||||||
if (!base_description->custody())
|
|
||||||
return Error::from_errno(EINVAL);
|
|
||||||
base = base_description->custody();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (flags & AT_REMOVEDIR)
|
if (flags & AT_REMOVEDIR)
|
||||||
TRY(VirtualFileSystem::the().rmdir(credentials(), path->view(), *base));
|
TRY(VirtualFileSystem::the().rmdir(credentials(), path->view(), *base));
|
||||||
|
|
|
@ -19,8 +19,6 @@ ErrorOr<FlatPtr> Process::sys$utimensat(Userspace<Syscall::SC_utimensat_params c
|
||||||
|
|
||||||
auto params = TRY(copy_typed_from_user(user_params));
|
auto params = TRY(copy_typed_from_user(user_params));
|
||||||
auto now = kgettimeofday().to_timespec();
|
auto now = kgettimeofday().to_timespec();
|
||||||
|
|
||||||
int dirfd = params.dirfd;
|
|
||||||
int follow_symlink = params.flag & AT_SYMLINK_NOFOLLOW ? O_NOFOLLOW_NOERROR : 0;
|
int follow_symlink = params.flag & AT_SYMLINK_NOFOLLOW ? O_NOFOLLOW_NOERROR : 0;
|
||||||
|
|
||||||
timespec times[2];
|
timespec times[2];
|
||||||
|
@ -37,36 +35,8 @@ ErrorOr<FlatPtr> Process::sys$utimensat(Userspace<Syscall::SC_utimensat_params c
|
||||||
times[1] = now;
|
times[1] = now;
|
||||||
}
|
}
|
||||||
|
|
||||||
OwnPtr<KString> path;
|
auto path = TRY(get_syscall_path_argument(params.path));
|
||||||
RefPtr<OpenFileDescription> description;
|
auto base = TRY(custody_for_dirfd(params.dirfd));
|
||||||
RefPtr<Custody> base;
|
|
||||||
|
|
||||||
auto path_or_error = get_syscall_path_argument(params.path);
|
|
||||||
if (path_or_error.is_error()) {
|
|
||||||
// If the path is empty ("") but not a nullptr, attempt to get a path
|
|
||||||
// from the file descriptor. This allows futimens() to be implemented
|
|
||||||
// in terms of utimensat().
|
|
||||||
if (params.path.characters && params.path.length == 0) {
|
|
||||||
description = TRY(open_file_description(dirfd));
|
|
||||||
path = TRY(description->original_absolute_path());
|
|
||||||
base = description->custody();
|
|
||||||
} else {
|
|
||||||
return path_or_error.release_error();
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
path = path_or_error.release_value();
|
|
||||||
if (dirfd == AT_FDCWD) {
|
|
||||||
base = current_directory();
|
|
||||||
} else {
|
|
||||||
description = TRY(open_file_description(dirfd));
|
|
||||||
if (!KLexicalPath::is_absolute(path->view()) && !description->is_directory())
|
|
||||||
return ENOTDIR;
|
|
||||||
if (!description->custody())
|
|
||||||
return EINVAL;
|
|
||||||
base = description->custody();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
auto& atime = times[0];
|
auto& atime = times[0];
|
||||||
auto& mtime = times[1];
|
auto& mtime = times[1];
|
||||||
TRY(VirtualFileSystem::the().utimensat(credentials(), path->view(), *base, atime, mtime, follow_symlink));
|
TRY(VirtualFileSystem::the().utimensat(credentials(), path->view(), *base, atime, mtime, follow_symlink));
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue