2020-07-31 00:38:15 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-07-31 00:38:15 +03:00
|
|
|
*/
|
|
|
|
|
2021-01-25 18:07:10 +03:00
|
|
|
#include <Kernel/Debug.h>
|
2021-09-07 14:39:11 +03:00
|
|
|
#include <Kernel/FileSystem/OpenFileDescription.h>
|
2020-07-31 00:38:15 +03:00
|
|
|
#include <Kernel/Process.h>
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<FlatPtr> Process::sys$fcntl(int fd, int cmd, u32 arg)
|
2020-07-31 00:38:15 +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::stdio));
|
2021-02-07 15:03:24 +03:00
|
|
|
dbgln_if(IO_DEBUG, "sys$fcntl: fd={}, cmd={}, arg={}", fd, cmd, arg);
|
2022-01-29 03:22:28 +03:00
|
|
|
auto description = TRY(open_file_description(fd));
|
2021-09-07 14:39:11 +03:00
|
|
|
// NOTE: The FD flags are not shared between OpenFileDescription objects.
|
2020-07-31 00:38:15 +03:00
|
|
|
// This means that dup() doesn't copy the FD_CLOEXEC flag!
|
|
|
|
switch (cmd) {
|
|
|
|
case F_DUPFD: {
|
|
|
|
int arg_fd = (int)arg;
|
|
|
|
if (arg_fd < 0)
|
2021-03-01 15:49:16 +03:00
|
|
|
return EINVAL;
|
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
|
|
|
auto fd_allocation = TRY(fds.allocate(arg_fd));
|
|
|
|
fds[fd_allocation.fd].set(*description);
|
|
|
|
return fd_allocation.fd;
|
|
|
|
});
|
2020-07-31 00:38:15 +03:00
|
|
|
}
|
|
|
|
case F_GETFD:
|
2022-01-29 03:29:07 +03:00
|
|
|
return m_fds.with_exclusive([fd](auto& fds) { return fds[fd].flags(); });
|
2020-07-31 00:38:15 +03:00
|
|
|
case F_SETFD:
|
2022-01-29 03:29:07 +03:00
|
|
|
m_fds.with_exclusive([fd, arg](auto& fds) { fds[fd].set_flags(arg); });
|
2020-07-31 00:38:15 +03: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 08:29:56 +03:00
|
|
|
case F_GETLK:
|
2021-11-08 02:51:39 +03:00
|
|
|
TRY(description->get_flock(Userspace<flock*>(arg)));
|
|
|
|
return 0;
|
2021-07-19 08:29:56 +03:00
|
|
|
case F_SETLK:
|
2021-11-08 02:51:39 +03:00
|
|
|
TRY(description->apply_flock(Process::current(), Userspace<const flock*>(arg)));
|
|
|
|
return 0;
|
2020-07-31 00:38:15 +03:00
|
|
|
default:
|
2021-03-01 15:49:16 +03:00
|
|
|
return EINVAL;
|
2020-07-31 00:38:15 +03:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2021-01-15 00:44:54 +03:00
|
|
|
|
2020-07-31 00:38:15 +03:00
|
|
|
}
|