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