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>
|
2020-07-30 23:38:15 +02:00
|
|
|
#include <Kernel/Process.h>
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
2021-11-08 00:51:39 +01:00
|
|
|
ErrorOr<FlatPtr> Process::sys$fcntl(int fd, int cmd, u32 arg)
|
2020-07-30 23:38:15 +02:00
|
|
|
{
|
2021-07-18 11:20:12 -07:00
|
|
|
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this);
|
2021-12-29 00:10:17 -08:00
|
|
|
require_promise(Pledge::stdio);
|
2021-02-07 15:33:24 +03:30
|
|
|
dbgln_if(IO_DEBUG, "sys$fcntl: fd={}, cmd={}, arg={}", fd, cmd, arg);
|
2021-09-07 13:41:27 +02:00
|
|
|
auto description = TRY(fds().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) {
|
|
|
|
case F_DUPFD: {
|
|
|
|
int arg_fd = (int)arg;
|
|
|
|
if (arg_fd < 0)
|
2021-03-01 13:49:16 +01:00
|
|
|
return EINVAL;
|
2021-09-05 16:14:34 +02:00
|
|
|
auto fd_allocation = TRY(m_fds.allocate(arg_fd));
|
|
|
|
m_fds[fd_allocation.fd].set(*description);
|
|
|
|
return fd_allocation.fd;
|
2020-07-30 23:38:15 +02:00
|
|
|
}
|
|
|
|
case F_GETFD:
|
2020-07-30 23:50:31 +02:00
|
|
|
return m_fds[fd].flags();
|
2020-07-30 23:38:15 +02:00
|
|
|
case F_SETFD:
|
2020-07-30 23:50:31 +02:00
|
|
|
m_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:
|
2021-11-08 00:51:39 +01:00
|
|
|
TRY(description->apply_flock(Process::current(), Userspace<const flock*>(arg)));
|
|
|
|
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
|
|
|
}
|