2020-07-30 17:38:15 -04:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 04:24:48 -04:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-07-30 17:38:15 -04:00
|
|
|
*/
|
|
|
|
|
2021-09-07 07:39:11 -04:00
|
|
|
#include <Kernel/FileSystem/OpenFileDescription.h>
|
2020-07-30 17:38:15 -04:00
|
|
|
#include <Kernel/Net/LocalSocket.h>
|
2023-02-24 12:45:37 -05:00
|
|
|
#include <Kernel/Tasks/Process.h>
|
2020-07-30 17:38:15 -04:00
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
2021-11-07 18:51:39 -05:00
|
|
|
ErrorOr<FlatPtr> Process::sys$sendfd(int sockfd, int fd)
|
2020-07-30 17:38:15 -04:00
|
|
|
{
|
2022-08-17 16:03:04 -04:00
|
|
|
VERIFY_NO_PROCESS_BIG_LOCK(this);
|
2021-12-29 04:11:45 -05:00
|
|
|
TRY(require_promise(Pledge::sendfd));
|
2022-01-28 19:22:28 -05:00
|
|
|
auto socket_description = TRY(open_file_description(sockfd));
|
2020-07-30 17:38:15 -04:00
|
|
|
if (!socket_description->is_socket())
|
2021-03-01 07:49:16 -05:00
|
|
|
return ENOTSOCK;
|
2020-07-30 17:38:15 -04:00
|
|
|
auto& socket = *socket_description->socket();
|
|
|
|
if (!socket.is_local())
|
2021-03-01 07:49:16 -05:00
|
|
|
return EAFNOSUPPORT;
|
2020-07-30 17:38:15 -04:00
|
|
|
if (!socket.is_connected())
|
2021-03-01 07:49:16 -05:00
|
|
|
return ENOTCONN;
|
2020-07-30 17:38:15 -04:00
|
|
|
|
2022-01-28 19:22:28 -05:00
|
|
|
auto passing_description = TRY(open_file_description(fd));
|
2020-07-30 17:38:15 -04:00
|
|
|
auto& local_socket = static_cast<LocalSocket&>(socket);
|
2021-11-07 18:51:39 -05:00
|
|
|
TRY(local_socket.sendfd(*socket_description, move(passing_description)));
|
|
|
|
return 0;
|
2020-07-30 17:38:15 -04:00
|
|
|
}
|
|
|
|
|
2021-11-07 18:51:39 -05:00
|
|
|
ErrorOr<FlatPtr> Process::sys$recvfd(int sockfd, int options)
|
2020-07-30 17:38:15 -04:00
|
|
|
{
|
2022-08-17 16:03:04 -04:00
|
|
|
VERIFY_NO_PROCESS_BIG_LOCK(this);
|
2021-12-29 04:11:45 -05:00
|
|
|
TRY(require_promise(Pledge::recvfd));
|
2022-01-28 19:22:28 -05:00
|
|
|
auto socket_description = TRY(open_file_description(sockfd));
|
2020-07-30 17:38:15 -04:00
|
|
|
if (!socket_description->is_socket())
|
2021-03-01 07:49:16 -05:00
|
|
|
return ENOTSOCK;
|
2020-07-30 17:38:15 -04:00
|
|
|
auto& socket = *socket_description->socket();
|
|
|
|
if (!socket.is_local())
|
2021-03-01 07:49:16 -05:00
|
|
|
return EAFNOSUPPORT;
|
2020-07-30 17:38:15 -04:00
|
|
|
|
2022-01-28 19:29:07 -05:00
|
|
|
auto fd_allocation = TRY(m_fds.with_exclusive([](auto& fds) { return fds.allocate(); }));
|
2020-07-30 17:38:15 -04:00
|
|
|
|
|
|
|
auto& local_socket = static_cast<LocalSocket&>(socket);
|
2021-09-05 12:08:47 -04:00
|
|
|
auto received_description = TRY(local_socket.recvfd(*socket_description));
|
2020-07-30 17:38:15 -04:00
|
|
|
|
2021-02-14 04:38:22 -05:00
|
|
|
u32 fd_flags = 0;
|
|
|
|
if (options & O_CLOEXEC)
|
|
|
|
fd_flags |= FD_CLOEXEC;
|
|
|
|
|
2022-01-28 19:29:07 -05:00
|
|
|
m_fds.with_exclusive([&](auto& fds) { fds[fd_allocation.fd].set(move(received_description), fd_flags); });
|
2021-09-05 12:08:47 -04:00
|
|
|
return fd_allocation.fd;
|
2020-07-30 17:38:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|