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/FileDescription.h>
|
|
|
|
#include <Kernel/FileSystem/VirtualFileSystem.h>
|
|
|
|
#include <Kernel/Process.h>
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
2021-03-01 12:49:16 +00:00
|
|
|
KResultOr<int> Process::sys$open(Userspace<const Syscall::SC_open_params*> user_params)
|
2020-07-30 21:38:15 +00:00
|
|
|
{
|
|
|
|
Syscall::SC_open_params params;
|
2020-09-12 03:11:07 +00:00
|
|
|
if (!copy_from_user(¶ms, user_params))
|
2021-03-01 12:49:16 +00:00
|
|
|
return EFAULT;
|
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
|
|
|
|
|
|
|
auto path = get_syscall_path_argument(params.path);
|
|
|
|
if (path.is_error())
|
|
|
|
return path.error();
|
2021-01-14 23:18:55 +00:00
|
|
|
|
2021-05-29 14:59:40 +00:00
|
|
|
dbgln_if(IO_DEBUG, "sys$open(dirfd={}, path='{}', options={}, mode={})", dirfd, path.value()->view(), options, mode);
|
2020-07-30 21:38:15 +00:00
|
|
|
int fd = alloc_fd();
|
|
|
|
if (fd < 0)
|
|
|
|
return fd;
|
|
|
|
|
|
|
|
RefPtr<Custody> base;
|
|
|
|
if (dirfd == AT_FDCWD) {
|
|
|
|
base = current_directory();
|
|
|
|
} else {
|
|
|
|
auto base_description = file_description(dirfd);
|
|
|
|
if (!base_description)
|
2021-03-01 12:49:16 +00:00
|
|
|
return EBADF;
|
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-05-29 14:59:40 +00:00
|
|
|
auto result = VFS::the().open(path.value()->view(), options, mode & ~umask(), *base);
|
2020-07-30 21:38:15 +00:00
|
|
|
if (result.is_error())
|
|
|
|
return result.error();
|
|
|
|
auto description = result.value();
|
|
|
|
|
|
|
|
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;
|
|
|
|
m_fds[fd].set(move(description), fd_flags);
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2021-03-01 12:49:16 +00:00
|
|
|
KResultOr<int> Process::sys$close(int fd)
|
2020-07-30 21:38:15 +00:00
|
|
|
{
|
|
|
|
REQUIRE_PROMISE(stdio);
|
|
|
|
auto description = file_description(fd);
|
2021-02-07 12:03:24 +00:00
|
|
|
dbgln_if(IO_DEBUG, "sys$close({}) {}", fd, description.ptr());
|
2020-07-30 21:38:15 +00:00
|
|
|
if (!description)
|
2021-03-01 12:49:16 +00:00
|
|
|
return EBADF;
|
2020-07-30 21:38:15 +00:00
|
|
|
int rc = description->close();
|
|
|
|
m_fds[fd] = {};
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|