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-09-07 13:39:11 +02:00
|
|
|
#include <Kernel/FileSystem/OpenFileDescription.h>
|
2020-07-30 23:38:15 +02:00
|
|
|
#include <Kernel/Net/LocalSocket.h>
|
|
|
|
|
#include <Kernel/Process.h>
|
|
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
|
2021-11-08 00:51:39 +01:00
|
|
|
ErrorOr<FlatPtr> Process::sys$sendfd(int sockfd, int fd)
|
2020-07-30 23:38:15 +02:00
|
|
|
{
|
2022-08-17 21:03:04 +01:00
|
|
|
VERIFY_NO_PROCESS_BIG_LOCK(this);
|
2021-12-29 01:11:45 -08:00
|
|
|
TRY(require_promise(Pledge::sendfd));
|
2022-01-29 01:22:28 +01:00
|
|
|
auto socket_description = TRY(open_file_description(sockfd));
|
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
|
|
|
|
2022-01-29 01:22:28 +01:00
|
|
|
auto passing_description = TRY(open_file_description(fd));
|
2020-07-30 23:38:15 +02:00
|
|
|
auto& local_socket = static_cast<LocalSocket&>(socket);
|
2021-11-08 00:51:39 +01:00
|
|
|
TRY(local_socket.sendfd(*socket_description, move(passing_description)));
|
|
|
|
|
return 0;
|
2020-07-30 23:38:15 +02:00
|
|
|
}
|
|
|
|
|
|
2021-11-08 00:51:39 +01:00
|
|
|
ErrorOr<FlatPtr> Process::sys$recvfd(int sockfd, int options)
|
2020-07-30 23:38:15 +02:00
|
|
|
{
|
2022-08-17 21:03:04 +01:00
|
|
|
VERIFY_NO_PROCESS_BIG_LOCK(this);
|
2021-12-29 01:11:45 -08:00
|
|
|
TRY(require_promise(Pledge::recvfd));
|
2022-01-29 01:22:28 +01:00
|
|
|
auto socket_description = TRY(open_file_description(sockfd));
|
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
|
|
|
|
2022-01-29 01:29:07 +01:00
|
|
|
auto fd_allocation = TRY(m_fds.with_exclusive([](auto& fds) { return fds.allocate(); }));
|
2020-07-30 23:38:15 +02:00
|
|
|
|
|
|
|
|
auto& local_socket = static_cast<LocalSocket&>(socket);
|
2021-09-05 18:08:47 +02:00
|
|
|
auto received_description = TRY(local_socket.recvfd(*socket_description));
|
2020-07-30 23:38:15 +02:00
|
|
|
|
2021-02-14 10:38:22 +01:00
|
|
|
u32 fd_flags = 0;
|
|
|
|
|
if (options & O_CLOEXEC)
|
|
|
|
|
fd_flags |= FD_CLOEXEC;
|
|
|
|
|
|
2022-01-29 01:29:07 +01:00
|
|
|
m_fds.with_exclusive([&](auto& fds) { fds[fd_allocation.fd].set(move(received_description), fd_flags); });
|
2021-09-05 18:08:47 +02:00
|
|
|
return fd_allocation.fd;
|
2020-07-30 23:38:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|