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
|
|
|
*/
|
|
|
|
|
2022-08-21 01:04:35 +02:00
|
|
|
#include <AK/RefPtr.h>
|
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>
|
2023-02-24 20:10:59 +02:00
|
|
|
#include <Kernel/Library/KLexicalPath.h>
|
2022-02-07 12:57:57 +01:00
|
|
|
#include <Kernel/Net/LocalSocket.h>
|
2023-02-24 19:45:37 +02:00
|
|
|
#include <Kernel/Tasks/Process.h>
|
2020-07-30 23:38:15 +02:00
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
ErrorOr<FlatPtr> Process::sys$open(Userspace<Syscall::SC_open_params const*> user_params)
|
2020-07-30 23:38:15 +02:00
|
|
|
{
|
2023-04-03 16:00:17 +02:00
|
|
|
VERIFY_NO_PROCESS_BIG_LOCK(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
|
|
|
|
2022-03-04 18:05:24 -07: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 23:38:15 +02:00
|
|
|
|
2022-03-04 18:05:24 -07:00
|
|
|
if (options & O_CREAT)
|
|
|
|
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
|
|
|
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());
|
2023-04-03 15:59:10 +02:00
|
|
|
auto base = TRY(custody_for_dirfd(dirfd));
|
2022-08-21 16:02:24 +02:00
|
|
|
auto description = TRY(VirtualFileSystem::the().open(credentials(), path->view(), options, mode & ~umask(), *base));
|
2020-07-30 23:38:15 +02:00
|
|
|
|
2022-02-07 12:57:57 +01:00
|
|
|
if (description->inode() && description->inode()->bound_socket())
|
2021-03-01 13:49:16 +01:00
|
|
|
return ENXIO;
|
2020-07-30 23:38:15 +02:00
|
|
|
|
2022-01-29 01:29:07 +01:00
|
|
|
return m_fds.with_exclusive([&](auto& fds) -> ErrorOr<FlatPtr> {
|
2022-01-29 01:22:28 +01: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 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
|
|
|
{
|
2022-08-17 21:03:04 +01:00
|
|
|
VERIFY_NO_PROCESS_BIG_LOCK(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:29:07 +01:00
|
|
|
m_fds.with_exclusive([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
|
|
|
}
|
|
|
|
|
|
|
|
}
|