2020-07-30 21:38:15 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-07-30 21:38:15 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <Kernel/FileSystem/FileDescription.h>
|
|
|
|
#include <Kernel/Net/LocalSocket.h>
|
|
|
|
#include <Kernel/Process.h>
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
2021-06-28 18:59:35 +00:00
|
|
|
KResultOr<FlatPtr> Process::sys$sendfd(int sockfd, int fd)
|
2020-07-30 21:38:15 +00:00
|
|
|
{
|
2021-07-18 18:20:12 +00:00
|
|
|
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
|
2020-07-30 21:38:15 +00:00
|
|
|
REQUIRE_PROMISE(sendfd);
|
2021-09-05 16:34:28 +00:00
|
|
|
auto socket_description = TRY(fds().file_description(sockfd));
|
2020-07-30 21:38:15 +00:00
|
|
|
if (!socket_description->is_socket())
|
2021-03-01 12:49:16 +00:00
|
|
|
return ENOTSOCK;
|
2020-07-30 21:38:15 +00:00
|
|
|
auto& socket = *socket_description->socket();
|
|
|
|
if (!socket.is_local())
|
2021-03-01 12:49:16 +00:00
|
|
|
return EAFNOSUPPORT;
|
2020-07-30 21:38:15 +00:00
|
|
|
if (!socket.is_connected())
|
2021-03-01 12:49:16 +00:00
|
|
|
return ENOTCONN;
|
2020-07-30 21:38:15 +00:00
|
|
|
|
2021-09-05 16:34:28 +00:00
|
|
|
auto passing_descriptor = TRY(fds().file_description(fd));
|
2020-07-30 21:38:15 +00:00
|
|
|
auto& local_socket = static_cast<LocalSocket&>(socket);
|
|
|
|
return local_socket.sendfd(*socket_description, *passing_descriptor);
|
|
|
|
}
|
|
|
|
|
2021-06-28 18:59:35 +00:00
|
|
|
KResultOr<FlatPtr> Process::sys$recvfd(int sockfd, int options)
|
2020-07-30 21:38:15 +00:00
|
|
|
{
|
2021-07-18 18:20:12 +00:00
|
|
|
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
|
2020-07-30 21:38:15 +00:00
|
|
|
REQUIRE_PROMISE(recvfd);
|
2021-09-05 16:34:28 +00:00
|
|
|
auto socket_description = TRY(fds().file_description(sockfd));
|
2020-07-30 21:38:15 +00:00
|
|
|
if (!socket_description->is_socket())
|
2021-03-01 12:49:16 +00:00
|
|
|
return ENOTSOCK;
|
2020-07-30 21:38:15 +00:00
|
|
|
auto& socket = *socket_description->socket();
|
|
|
|
if (!socket.is_local())
|
2021-03-01 12:49:16 +00:00
|
|
|
return EAFNOSUPPORT;
|
2020-07-30 21:38:15 +00:00
|
|
|
|
2021-09-05 16:08:47 +00:00
|
|
|
auto fd_allocation = TRY(m_fds.allocate());
|
2020-07-30 21:38:15 +00:00
|
|
|
|
|
|
|
auto& local_socket = static_cast<LocalSocket&>(socket);
|
2021-09-05 16:08:47 +00:00
|
|
|
auto received_description = TRY(local_socket.recvfd(*socket_description));
|
2020-07-30 21:38:15 +00:00
|
|
|
|
2021-02-14 09:38:22 +00:00
|
|
|
u32 fd_flags = 0;
|
|
|
|
if (options & O_CLOEXEC)
|
|
|
|
fd_flags |= FD_CLOEXEC;
|
|
|
|
|
2021-09-05 16:08:47 +00:00
|
|
|
m_fds[fd_allocation.fd].set(move(received_description), fd_flags);
|
|
|
|
return fd_allocation.fd;
|
2020-07-30 21:38:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|