2020-07-30 18:38:15 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-07-30 18:38:15 -03:00
|
|
|
*/
|
|
|
|
|
|
2021-01-25 12:07:10 -03:00
|
|
|
#include <Kernel/Debug.h>
|
2021-09-07 08:39:11 -03:00
|
|
|
#include <Kernel/FileSystem/OpenFileDescription.h>
|
2020-07-30 18:38:15 -03:00
|
|
|
#include <Kernel/Process.h>
|
|
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
|
2021-06-28 15:59:35 -03:00
|
|
|
KResultOr<FlatPtr> Process::sys$fcntl(int fd, int cmd, u32 arg)
|
2020-07-30 18:38:15 -03:00
|
|
|
{
|
2021-07-18 15:20:12 -03:00
|
|
|
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this);
|
2020-07-30 18:38:15 -03:00
|
|
|
REQUIRE_PROMISE(stdio);
|
2021-02-07 09:03:24 -03:00
|
|
|
dbgln_if(IO_DEBUG, "sys$fcntl: fd={}, cmd={}, arg={}", fd, cmd, arg);
|
2021-09-07 08:41:27 -03:00
|
|
|
auto description = TRY(fds().open_file_description(fd));
|
2021-09-07 08:39:11 -03:00
|
|
|
// NOTE: The FD flags are not shared between OpenFileDescription objects.
|
2020-07-30 18: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 09:49:16 -03:00
|
|
|
return EINVAL;
|
2021-09-05 11:14:34 -03:00
|
|
|
auto fd_allocation = TRY(m_fds.allocate(arg_fd));
|
|
|
|
|
m_fds[fd_allocation.fd].set(*description);
|
|
|
|
|
return fd_allocation.fd;
|
2020-07-30 18:38:15 -03:00
|
|
|
}
|
|
|
|
|
case F_GETFD:
|
2020-07-30 18:50:31 -03:00
|
|
|
return m_fds[fd].flags();
|
2020-07-30 18:38:15 -03:00
|
|
|
case F_SETFD:
|
2020-07-30 18:50:31 -03:00
|
|
|
m_fds[fd].set_flags(arg);
|
2020-07-30 18: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 02:29:56 -03:00
|
|
|
case F_GETLK:
|
|
|
|
|
return description->get_flock(Userspace<flock*>(arg));
|
|
|
|
|
case F_SETLK:
|
2021-08-19 16:45:07 -03:00
|
|
|
return description->apply_flock(Process::current(), Userspace<const flock*>(arg));
|
2020-07-30 18:38:15 -03:00
|
|
|
default:
|
2021-03-01 09:49:16 -03:00
|
|
|
return EINVAL;
|
2020-07-30 18:38:15 -03:00
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2021-01-14 18:44:54 -03:00
|
|
|
|
2020-07-30 18:38:15 -03:00
|
|
|
}
|