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
|
|
|
*/
|
|
|
|
|
|
|
|
#include <Kernel/FileSystem/FileDescription.h>
|
|
|
|
#include <Kernel/Net/LocalSocket.h>
|
|
|
|
#include <Kernel/Process.h>
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
2021-06-28 20:59:35 +02:00
|
|
|
KResultOr<FlatPtr> Process::sys$sendfd(int sockfd, int fd)
|
2020-07-30 23:38:15 +02:00
|
|
|
{
|
2021-07-18 11:20:12 -07:00
|
|
|
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
|
2020-07-30 23:38:15 +02:00
|
|
|
REQUIRE_PROMISE(sendfd);
|
2021-06-22 21:22:17 +03:00
|
|
|
auto socket_description = fds().file_description(sockfd);
|
2020-07-30 23:38:15 +02:00
|
|
|
if (!socket_description)
|
2021-03-01 13:49:16 +01:00
|
|
|
return EBADF;
|
2020-07-30 23:38:15 +02:00
|
|
|
if (!socket_description->is_socket())
|
2021-03-01 13:49:16 +01:00
|
|
|
return ENOTSOCK;
|
2020-07-30 23:38:15 +02:00
|
|
|
auto& socket = *socket_description->socket();
|
|
|
|
if (!socket.is_local())
|
2021-03-01 13:49:16 +01:00
|
|
|
return EAFNOSUPPORT;
|
2020-07-30 23:38:15 +02:00
|
|
|
if (!socket.is_connected())
|
2021-03-01 13:49:16 +01:00
|
|
|
return ENOTCONN;
|
2020-07-30 23:38:15 +02:00
|
|
|
|
2021-06-22 21:22:17 +03:00
|
|
|
auto passing_descriptor = fds().file_description(fd);
|
2020-07-30 23:38:15 +02:00
|
|
|
if (!passing_descriptor)
|
2021-03-01 13:49:16 +01:00
|
|
|
return EBADF;
|
2020-07-30 23:38:15 +02:00
|
|
|
|
|
|
|
auto& local_socket = static_cast<LocalSocket&>(socket);
|
|
|
|
return local_socket.sendfd(*socket_description, *passing_descriptor);
|
|
|
|
}
|
|
|
|
|
2021-06-28 20:59:35 +02:00
|
|
|
KResultOr<FlatPtr> Process::sys$recvfd(int sockfd, int options)
|
2020-07-30 23:38:15 +02:00
|
|
|
{
|
2021-07-18 11:20:12 -07:00
|
|
|
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
|
2020-07-30 23:38:15 +02:00
|
|
|
REQUIRE_PROMISE(recvfd);
|
2021-06-22 21:22:17 +03:00
|
|
|
auto socket_description = fds().file_description(sockfd);
|
2020-07-30 23:38:15 +02:00
|
|
|
if (!socket_description)
|
2021-03-01 13:49:16 +01:00
|
|
|
return EBADF;
|
2020-07-30 23:38:15 +02:00
|
|
|
if (!socket_description->is_socket())
|
2021-03-01 13:49:16 +01:00
|
|
|
return ENOTSOCK;
|
2020-07-30 23:38:15 +02:00
|
|
|
auto& socket = *socket_description->socket();
|
|
|
|
if (!socket.is_local())
|
2021-03-01 13:49:16 +01:00
|
|
|
return EAFNOSUPPORT;
|
2020-07-30 23:38:15 +02:00
|
|
|
|
2021-07-27 02:12:51 -07:00
|
|
|
auto new_fd_or_error = m_fds.allocate();
|
|
|
|
if (new_fd_or_error.is_error())
|
|
|
|
return new_fd_or_error.error();
|
2021-07-27 23:59:24 -07:00
|
|
|
auto new_fd = new_fd_or_error.release_value();
|
2020-07-30 23:38:15 +02:00
|
|
|
|
|
|
|
auto& local_socket = static_cast<LocalSocket&>(socket);
|
|
|
|
auto received_descriptor_or_error = local_socket.recvfd(*socket_description);
|
|
|
|
|
|
|
|
if (received_descriptor_or_error.is_error())
|
|
|
|
return received_descriptor_or_error.error();
|
|
|
|
|
2021-02-14 10:38:22 +01:00
|
|
|
u32 fd_flags = 0;
|
|
|
|
if (options & O_CLOEXEC)
|
|
|
|
fd_flags |= FD_CLOEXEC;
|
|
|
|
|
2021-07-27 23:59:24 -07:00
|
|
|
m_fds[new_fd.fd].set(*received_descriptor_or_error.value(), fd_flags);
|
|
|
|
return new_fd.fd;
|
2020-07-30 23:38:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|