2020-07-30 23:38:15 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-07-30 23:38:15 +02:00
|
|
|
*/
|
|
|
|
|
2021-01-25 16:07:10 +01:00
|
|
|
#include <Kernel/Debug.h>
|
2020-07-30 23:38:15 +02:00
|
|
|
#include <Kernel/FileSystem/Custody.h>
|
|
|
|
#include <Kernel/FileSystem/VirtualFileSystem.h>
|
|
|
|
#include <Kernel/Process.h>
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
2021-11-08 00:51:39 +01:00
|
|
|
ErrorOr<FlatPtr> Process::sys$open(Userspace<const Syscall::SC_open_params*> user_params)
|
2020-07-30 23:38:15 +02:00
|
|
|
{
|
2021-07-18 11:20:12 -07:00
|
|
|
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
|
2021-09-05 17:51:37 +02:00
|
|
|
auto params = TRY(copy_typed_from_user(user_params));
|
2020-07-30 23:38:15 +02:00
|
|
|
|
|
|
|
int dirfd = params.dirfd;
|
|
|
|
int options = params.options;
|
|
|
|
u16 mode = params.mode;
|
|
|
|
|
|
|
|
if (options & O_NOFOLLOW_NOERROR)
|
2021-03-01 13:49:16 +01:00
|
|
|
return EINVAL;
|
2020-07-30 23:38:15 +02:00
|
|
|
|
|
|
|
if (options & O_UNLINK_INTERNAL)
|
2021-03-01 13:49:16 +01:00
|
|
|
return EINVAL;
|
2020-07-30 23:38:15 +02:00
|
|
|
|
|
|
|
if (options & O_WRONLY)
|
2021-12-29 01:11:45 -08:00
|
|
|
TRY(require_promise(Pledge::wpath));
|
2020-07-30 23:38:15 +02:00
|
|
|
else if (options & O_RDONLY)
|
2021-12-29 01:11:45 -08:00
|
|
|
TRY(require_promise(Pledge::rpath));
|
2020-07-30 23:38:15 +02:00
|
|
|
|
|
|
|
if (options & O_CREAT)
|
2021-12-29 01:11:45 -08:00
|
|
|
TRY(require_promise(Pledge::cpath));
|
2020-07-30 23:38:15 +02:00
|
|
|
|
|
|
|
// Ignore everything except permission bits.
|
2021-01-23 15:35:20 +01:00
|
|
|
mode &= 0777;
|
2020-07-30 23:38:15 +02:00
|
|
|
|
2021-09-05 16:19:36 +02:00
|
|
|
auto path = TRY(get_syscall_path_argument(params.path));
|
2021-01-15 00:18:55 +01:00
|
|
|
|
2021-09-05 16:19:36 +02:00
|
|
|
dbgln_if(IO_DEBUG, "sys$open(dirfd={}, path='{}', options={}, mode={})", dirfd, path->view(), options, mode);
|
2020-07-30 23:38:15 +02:00
|
|
|
|
2022-01-29 01:22:28 +01:00
|
|
|
auto fd_allocation = TRY(allocate_fd());
|
2020-07-30 23:38:15 +02:00
|
|
|
RefPtr<Custody> base;
|
|
|
|
if (dirfd == AT_FDCWD) {
|
|
|
|
base = current_directory();
|
|
|
|
} else {
|
2022-01-29 01:22:28 +01:00
|
|
|
auto base_description = TRY(open_file_description(dirfd));
|
2020-07-30 23:38:15 +02:00
|
|
|
if (!base_description->is_directory())
|
2021-03-01 13:49:16 +01:00
|
|
|
return ENOTDIR;
|
2020-07-30 23:38:15 +02:00
|
|
|
if (!base_description->custody())
|
2021-03-01 13:49:16 +01:00
|
|
|
return EINVAL;
|
2020-07-30 23:38:15 +02:00
|
|
|
base = base_description->custody();
|
|
|
|
}
|
|
|
|
|
2021-09-05 16:19:36 +02:00
|
|
|
auto description = TRY(VirtualFileSystem::the().open(path->view(), options, mode & ~umask(), *base));
|
2020-07-30 23:38:15 +02:00
|
|
|
|
|
|
|
if (description->inode() && description->inode()->socket())
|
2021-03-01 13:49:16 +01:00
|
|
|
return ENXIO;
|
2020-07-30 23:38:15 +02:00
|
|
|
|
2022-01-29 01:22:28 +01:00
|
|
|
return m_fds.with([&](auto& fds) -> ErrorOr<FlatPtr> {
|
|
|
|
u32 fd_flags = (options & O_CLOEXEC) ? FD_CLOEXEC : 0;
|
|
|
|
fds[fd_allocation.fd].set(move(description), fd_flags);
|
|
|
|
return fd_allocation.fd;
|
|
|
|
});
|
2020-07-30 23:38:15 +02:00
|
|
|
}
|
|
|
|
|
2021-11-08 00:51:39 +01:00
|
|
|
ErrorOr<FlatPtr> Process::sys$close(int fd)
|
2020-07-30 23:38:15 +02:00
|
|
|
{
|
2021-07-18 11:20:12 -07:00
|
|
|
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
|
2021-12-29 01:11:45 -08:00
|
|
|
TRY(require_promise(Pledge::stdio));
|
2022-01-29 01:22:28 +01:00
|
|
|
auto description = TRY(open_file_description(fd));
|
2021-08-14 15:15:11 +02:00
|
|
|
auto result = description->close();
|
2022-01-29 01:22:28 +01:00
|
|
|
m_fds.with([fd](auto& fds) { fds[fd] = {}; });
|
2021-11-08 00:51:39 +01:00
|
|
|
if (result.is_error())
|
|
|
|
return result.release_error();
|
|
|
|
return 0;
|
2020-07-30 23:38:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|