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>
|
2021-09-07 11:39:11 +00:00
|
|
|
#include <Kernel/FileSystem/OpenFileDescription.h>
|
2023-02-24 17:45:37 +00:00
|
|
|
#include <Kernel/Tasks/Process.h>
|
2020-07-30 21:38:15 +00:00
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
2022-07-02 23:02:45 +00:00
|
|
|
ErrorOr<FlatPtr> Process::sys$fcntl(int fd, int cmd, uintptr_t arg)
|
2020-07-30 21:38:15 +00:00
|
|
|
{
|
2023-04-03 11:28:50 +00:00
|
|
|
VERIFY_NO_PROCESS_BIG_LOCK(this);
|
2021-12-29 09:11:45 +00:00
|
|
|
TRY(require_promise(Pledge::stdio));
|
2021-02-07 12:03:24 +00:00
|
|
|
dbgln_if(IO_DEBUG, "sys$fcntl: fd={}, cmd={}, arg={}", fd, cmd, arg);
|
2022-01-29 00:22:28 +00:00
|
|
|
auto description = TRY(open_file_description(fd));
|
2021-09-07 11:39:11 +00:00
|
|
|
// NOTE: The FD flags are not shared between OpenFileDescription objects.
|
2020-07-30 21:38:15 +00:00
|
|
|
// This means that dup() doesn't copy the FD_CLOEXEC flag!
|
|
|
|
switch (cmd) {
|
2023-02-11 21:17:41 +00:00
|
|
|
case F_DUPFD_CLOEXEC:
|
2020-07-30 21:38:15 +00:00
|
|
|
case F_DUPFD: {
|
|
|
|
int arg_fd = (int)arg;
|
|
|
|
if (arg_fd < 0)
|
2021-03-01 12:49:16 +00:00
|
|
|
return EINVAL;
|
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
|
|
|
auto fd_allocation = TRY(fds.allocate(arg_fd));
|
2023-02-11 21:17:41 +00:00
|
|
|
fds[fd_allocation.fd].set(*description, (cmd == F_DUPFD_CLOEXEC) ? FD_CLOEXEC : 0);
|
2022-01-29 00:22:28 +00:00
|
|
|
return fd_allocation.fd;
|
|
|
|
});
|
2020-07-30 21:38:15 +00:00
|
|
|
}
|
|
|
|
case F_GETFD:
|
2022-01-29 00:29:07 +00:00
|
|
|
return m_fds.with_exclusive([fd](auto& fds) { return fds[fd].flags(); });
|
2020-07-30 21:38:15 +00:00
|
|
|
case F_SETFD:
|
2022-01-29 00:29:07 +00:00
|
|
|
m_fds.with_exclusive([fd, arg](auto& fds) { fds[fd].set_flags(arg); });
|
2020-07-30 21:38:15 +00:00
|
|
|
break;
|
|
|
|
case F_GETFL:
|
|
|
|
return description->file_flags();
|
|
|
|
case F_SETFL:
|
|
|
|
description->set_file_flags(arg);
|
|
|
|
break;
|
|
|
|
case F_ISTTY:
|
|
|
|
return description->is_tty();
|
2021-07-19 05:29:56 +00:00
|
|
|
case F_GETLK:
|
2021-11-07 23:51:39 +00:00
|
|
|
TRY(description->get_flock(Userspace<flock*>(arg)));
|
|
|
|
return 0;
|
2021-07-19 05:29:56 +00:00
|
|
|
case F_SETLK:
|
2022-07-13 23:17:01 +00:00
|
|
|
TRY(description->apply_flock(Process::current(), Userspace<flock const*>(arg), ShouldBlock::No));
|
|
|
|
return 0;
|
|
|
|
case F_SETLKW:
|
|
|
|
TRY(description->apply_flock(Process::current(), Userspace<flock const*>(arg), ShouldBlock::Yes));
|
2021-11-07 23:51:39 +00:00
|
|
|
return 0;
|
2020-07-30 21:38:15 +00:00
|
|
|
default:
|
2021-03-01 12:49:16 +00:00
|
|
|
return EINVAL;
|
2020-07-30 21:38:15 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2021-01-14 21:44:54 +00:00
|
|
|
|
2020-07-30 21:38:15 +00:00
|
|
|
}
|