2020-07-30 21:38:15 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-07-30 21:38:15 +00:00
|
|
|
*/
|
|
|
|
|
2022-08-20 23:04:35 +00:00
|
|
|
#include <AK/RefPtr.h>
|
2021-01-25 15:07:10 +00:00
|
|
|
#include <Kernel/Debug.h>
|
2020-07-30 21:38:15 +00:00
|
|
|
#include <Kernel/FileSystem/Custody.h>
|
|
|
|
#include <Kernel/FileSystem/VirtualFileSystem.h>
|
2022-03-05 01:05:24 +00:00
|
|
|
#include <Kernel/KLexicalPath.h>
|
2022-02-07 11:57:57 +00:00
|
|
|
#include <Kernel/Net/LocalSocket.h>
|
2020-07-30 21:38:15 +00:00
|
|
|
#include <Kernel/Process.h>
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
ErrorOr<FlatPtr> Process::sys$open(Userspace<Syscall::SC_open_params const*> user_params)
|
2020-07-30 21:38:15 +00:00
|
|
|
{
|
2022-08-17 20:03:04 +00:00
|
|
|
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this);
|
2021-09-05 15:51:37 +00:00
|
|
|
auto params = TRY(copy_typed_from_user(user_params));
|
2020-07-30 21:38:15 +00:00
|
|
|
|
|
|
|
int dirfd = params.dirfd;
|
|
|
|
int options = params.options;
|
|
|
|
u16 mode = params.mode;
|
|
|
|
|
|
|
|
if (options & O_NOFOLLOW_NOERROR)
|
2021-03-01 12:49:16 +00:00
|
|
|
return EINVAL;
|
2020-07-30 21:38:15 +00:00
|
|
|
|
|
|
|
if (options & O_UNLINK_INTERNAL)
|
2021-03-01 12:49:16 +00:00
|
|
|
return EINVAL;
|
2020-07-30 21:38:15 +00:00
|
|
|
|
2022-03-05 01:05:24 +00:00
|
|
|
auto path = TRY(get_syscall_path_argument(params.path));
|
|
|
|
|
|
|
|
// Disable checking open pledges when building userspace with coverage
|
|
|
|
// so that all processes can write out coverage data even with pledges
|
|
|
|
bool skip_pledge_verification = false;
|
|
|
|
|
|
|
|
#ifdef SKIP_PATH_VALIDATION_FOR_COVERAGE_INSTRUMENTATION
|
|
|
|
if (KLexicalPath::basename(path->view()).ends_with(".profraw"sv))
|
|
|
|
skip_pledge_verification = true;
|
|
|
|
#endif
|
|
|
|
if (!skip_pledge_verification) {
|
|
|
|
if (options & O_WRONLY)
|
|
|
|
TRY(require_promise(Pledge::wpath));
|
|
|
|
else if (options & O_RDONLY)
|
|
|
|
TRY(require_promise(Pledge::rpath));
|
2020-07-30 21:38:15 +00:00
|
|
|
|
2022-03-05 01:05:24 +00:00
|
|
|
if (options & O_CREAT)
|
|
|
|
TRY(require_promise(Pledge::cpath));
|
|
|
|
}
|
2020-07-30 21:38:15 +00:00
|
|
|
|
|
|
|
// Ignore everything except permission bits.
|
2021-01-23 14:35:20 +00:00
|
|
|
mode &= 0777;
|
2020-07-30 21:38:15 +00:00
|
|
|
|
2021-09-05 14:19:36 +00:00
|
|
|
dbgln_if(IO_DEBUG, "sys$open(dirfd={}, path='{}', options={}, mode={})", dirfd, path->view(), options, mode);
|
2020-07-30 21:38:15 +00:00
|
|
|
|
2022-01-29 00:22:28 +00:00
|
|
|
auto fd_allocation = TRY(allocate_fd());
|
2022-08-20 23:04:35 +00:00
|
|
|
RefPtr<Custody> base;
|
2020-07-30 21:38:15 +00:00
|
|
|
if (dirfd == AT_FDCWD) {
|
|
|
|
base = current_directory();
|
|
|
|
} else {
|
2022-01-29 00:22:28 +00:00
|
|
|
auto base_description = TRY(open_file_description(dirfd));
|
2020-07-30 21:38:15 +00:00
|
|
|
if (!base_description->is_directory())
|
2021-03-01 12:49:16 +00:00
|
|
|
return ENOTDIR;
|
2020-07-30 21:38:15 +00:00
|
|
|
if (!base_description->custody())
|
2021-03-01 12:49:16 +00:00
|
|
|
return EINVAL;
|
2020-07-30 21:38:15 +00:00
|
|
|
base = base_description->custody();
|
|
|
|
}
|
|
|
|
|
2022-08-21 14:02:24 +00:00
|
|
|
auto description = TRY(VirtualFileSystem::the().open(credentials(), path->view(), options, mode & ~umask(), *base));
|
2020-07-30 21:38:15 +00:00
|
|
|
|
2022-02-07 11:57:57 +00:00
|
|
|
if (description->inode() && description->inode()->bound_socket())
|
2021-03-01 12:49:16 +00:00
|
|
|
return ENXIO;
|
2020-07-30 21:38:15 +00:00
|
|
|
|
2022-01-29 00:29:07 +00:00
|
|
|
return m_fds.with_exclusive([&](auto& fds) -> ErrorOr<FlatPtr> {
|
2022-01-29 00:22:28 +00:00
|
|
|
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 21:38:15 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 23:51:39 +00:00
|
|
|
ErrorOr<FlatPtr> Process::sys$close(int fd)
|
2020-07-30 21:38:15 +00:00
|
|
|
{
|
2022-08-17 20:03:04 +00:00
|
|
|
VERIFY_NO_PROCESS_BIG_LOCK(this);
|
2021-12-29 09:11:45 +00:00
|
|
|
TRY(require_promise(Pledge::stdio));
|
2022-01-29 00:22:28 +00:00
|
|
|
auto description = TRY(open_file_description(fd));
|
2021-08-14 13:15:11 +00:00
|
|
|
auto result = description->close();
|
2022-01-29 00:29:07 +00:00
|
|
|
m_fds.with_exclusive([fd](auto& fds) { fds[fd] = {}; });
|
2021-11-07 23:51:39 +00:00
|
|
|
if (result.is_error())
|
|
|
|
return result.release_error();
|
|
|
|
return 0;
|
2020-07-30 21:38:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|