2021-05-12 22:17:51 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* Copyright (c) 2021, sin-ack <sin-ack@protonmail.com>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <Kernel/API/InodeWatcherFlags.h>
|
|
|
|
#include <Kernel/FileSystem/Custody.h>
|
|
|
|
#include <Kernel/FileSystem/InodeWatcher.h>
|
2021-09-07 14:39:11 +03:00
|
|
|
#include <Kernel/FileSystem/OpenFileDescription.h>
|
2021-05-12 22:17:51 +03:00
|
|
|
#include <Kernel/Process.h>
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<FlatPtr> Process::sys$create_inode_watcher(u32 flags)
|
2021-05-12 22:17:51 +03:00
|
|
|
{
|
2021-07-18 21:20:12 +03:00
|
|
|
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
|
2021-12-29 12:11:45 +03:00
|
|
|
TRY(require_promise(Pledge::rpath));
|
2021-05-12 22:17:51 +03:00
|
|
|
|
2022-01-29 03:22:28 +03:00
|
|
|
auto fd_allocation = TRY(allocate_fd());
|
2021-09-05 19:41:01 +03:00
|
|
|
auto watcher = TRY(InodeWatcher::try_create());
|
2021-09-07 14:39:11 +03:00
|
|
|
auto description = TRY(OpenFileDescription::try_create(move(watcher)));
|
2021-05-12 22:17:51 +03:00
|
|
|
|
2021-09-05 19:41:01 +03:00
|
|
|
description->set_readable(true);
|
|
|
|
if (flags & static_cast<unsigned>(InodeWatcherFlags::Nonblock))
|
|
|
|
description->set_blocking(false);
|
2021-05-12 22:17:51 +03:00
|
|
|
|
2022-01-29 03:29:07 +03:00
|
|
|
return m_fds.with_exclusive([&](auto& fds) -> ErrorOr<FlatPtr> {
|
2022-01-29 03:22:28 +03:00
|
|
|
fds[fd_allocation.fd].set(move(description));
|
2021-05-12 22:17:51 +03:00
|
|
|
|
2022-01-29 03:22:28 +03:00
|
|
|
if (flags & static_cast<unsigned>(InodeWatcherFlags::CloseOnExec))
|
|
|
|
fds[fd_allocation.fd].set_flags(fds[fd_allocation.fd].flags() | FD_CLOEXEC);
|
2021-05-12 22:17:51 +03:00
|
|
|
|
2022-01-29 03:22:28 +03:00
|
|
|
return fd_allocation.fd;
|
|
|
|
});
|
2021-05-12 22:17:51 +03:00
|
|
|
}
|
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<FlatPtr> Process::sys$inode_watcher_add_watch(Userspace<const Syscall::SC_inode_watcher_add_watch_params*> user_params)
|
2021-05-12 22:17:51 +03:00
|
|
|
{
|
2021-07-18 21:20:12 +03:00
|
|
|
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
|
2021-12-29 12:11:45 +03:00
|
|
|
TRY(require_promise(Pledge::rpath));
|
2021-09-05 18:51:37 +03:00
|
|
|
auto params = TRY(copy_typed_from_user(user_params));
|
2021-05-12 22:17:51 +03:00
|
|
|
|
2022-01-29 03:22:28 +03:00
|
|
|
auto description = TRY(open_file_description(params.fd));
|
2021-05-12 22:17:51 +03:00
|
|
|
if (!description->is_inode_watcher())
|
|
|
|
return EBADF;
|
2021-12-18 20:37:21 +03:00
|
|
|
auto* inode_watcher = description->inode_watcher();
|
2021-09-05 19:41:01 +03:00
|
|
|
auto path = TRY(get_syscall_path_argument(params.user_path));
|
|
|
|
auto custody = TRY(VirtualFileSystem::the().resolve_path(path->view(), current_directory()));
|
2021-05-12 22:17:51 +03:00
|
|
|
if (!custody->inode().fs().supports_watchers())
|
|
|
|
return ENOTSUP;
|
|
|
|
|
2021-09-05 19:41:01 +03:00
|
|
|
return TRY(inode_watcher->register_inode(custody->inode(), params.event_mask));
|
2021-05-12 22:17:51 +03:00
|
|
|
}
|
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<FlatPtr> Process::sys$inode_watcher_remove_watch(int fd, int wd)
|
2021-05-12 22:17:51 +03:00
|
|
|
{
|
2021-07-18 21:20:12 +03:00
|
|
|
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
|
2022-01-29 03:22:28 +03:00
|
|
|
auto description = TRY(open_file_description(fd));
|
2021-05-12 22:17:51 +03:00
|
|
|
if (!description->is_inode_watcher())
|
|
|
|
return EBADF;
|
2021-11-08 02:51:39 +03:00
|
|
|
TRY(description->inode_watcher()->unregister_by_wd(wd));
|
|
|
|
return 0;
|
2021-05-12 22:17:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|