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>
|
2020-07-31 00:38:15 +03:00
|
|
|
#include <Kernel/FileSystem/FileDescription.h>
|
|
|
|
#include <Kernel/Process.h>
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
2021-06-28 21:59:35 +03:00
|
|
|
KResultOr<FlatPtr> Process::sys$fcntl(int fd, int cmd, u32 arg)
|
2020-07-31 00:38:15 +03:00
|
|
|
{
|
|
|
|
REQUIRE_PROMISE(stdio);
|
2021-02-07 15:03:24 +03:00
|
|
|
dbgln_if(IO_DEBUG, "sys$fcntl: fd={}, cmd={}, arg={}", fd, cmd, arg);
|
2020-07-31 00:38:15 +03:00
|
|
|
auto description = file_description(fd);
|
|
|
|
if (!description)
|
2021-03-01 15:49:16 +03:00
|
|
|
return EBADF;
|
2020-07-31 00:38:15 +03:00
|
|
|
// NOTE: The FD flags are not shared between FileDescription objects.
|
|
|
|
// 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;
|
2020-07-31 00:38:15 +03:00
|
|
|
int new_fd = alloc_fd(arg_fd);
|
|
|
|
if (new_fd < 0)
|
|
|
|
return new_fd;
|
|
|
|
m_fds[new_fd].set(*description);
|
|
|
|
return new_fd;
|
|
|
|
}
|
|
|
|
case F_GETFD:
|
2020-07-31 00:50:31 +03:00
|
|
|
return m_fds[fd].flags();
|
2020-07-31 00:38:15 +03:00
|
|
|
case F_SETFD:
|
2020-07-31 00:50:31 +03:00
|
|
|
m_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();
|
|
|
|
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
|
|
|
}
|