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
|
|
|
*/
|
|
|
|
|
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>
|
|
|
|
#include <Kernel/Process.h>
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
2021-06-28 18:59:35 +00:00
|
|
|
KResultOr<FlatPtr> Process::sys$open(Userspace<const Syscall::SC_open_params*> user_params)
|
2020-07-30 21:38:15 +00:00
|
|
|
{
|
2021-07-18 18:20:12 +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
|
|
|
|
|
|
|
if (options & O_WRONLY)
|
|
|
|
REQUIRE_PROMISE(wpath);
|
|
|
|
else if (options & O_RDONLY)
|
|
|
|
REQUIRE_PROMISE(rpath);
|
|
|
|
|
|
|
|
if (options & O_CREAT)
|
|
|
|
REQUIRE_PROMISE(cpath);
|
|
|
|
|
|
|
|
// 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
|
|
|
auto path = TRY(get_syscall_path_argument(params.path));
|
2021-01-14 23:18:55 +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
|
|
|
|
2021-09-05 14:19:36 +00:00
|
|
|
auto fd_allocation = TRY(m_fds.allocate());
|
2020-07-30 21:38:15 +00:00
|
|
|
RefPtr<Custody> base;
|
|
|
|
if (dirfd == AT_FDCWD) {
|
|
|
|
base = current_directory();
|
|
|
|
} else {
|
2021-09-05 16:34:28 +00:00
|
|
|
auto base_description = TRY(fds().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();
|
|
|
|
}
|
|
|
|
|
2021-09-05 14:19:36 +00:00
|
|
|
auto description = TRY(VirtualFileSystem::the().open(path->view(), options, mode & ~umask(), *base));
|
2020-07-30 21:38:15 +00:00
|
|
|
|
|
|
|
if (description->inode() && description->inode()->socket())
|
2021-03-01 12:49:16 +00:00
|
|
|
return ENXIO;
|
2020-07-30 21:38:15 +00:00
|
|
|
|
|
|
|
u32 fd_flags = (options & O_CLOEXEC) ? FD_CLOEXEC : 0;
|
2021-09-05 14:19:36 +00:00
|
|
|
m_fds[fd_allocation.fd].set(move(description), fd_flags);
|
|
|
|
return fd_allocation.fd;
|
2020-07-30 21:38:15 +00:00
|
|
|
}
|
|
|
|
|
2021-06-28 18:59:35 +00:00
|
|
|
KResultOr<FlatPtr> Process::sys$close(int fd)
|
2020-07-30 21:38:15 +00:00
|
|
|
{
|
2021-07-18 18:20:12 +00:00
|
|
|
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
|
2020-07-30 21:38:15 +00:00
|
|
|
REQUIRE_PROMISE(stdio);
|
2021-09-05 16:34:28 +00:00
|
|
|
auto description = TRY(fds().file_description(fd));
|
2021-08-14 13:15:11 +00:00
|
|
|
auto result = description->close();
|
2020-07-30 21:38:15 +00:00
|
|
|
m_fds[fd] = {};
|
2021-08-14 13:15:11 +00:00
|
|
|
return result;
|
2020-07-30 21:38:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|