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/FIFO.h>
|
|
|
|
#include <Kernel/Process.h>
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
2021-11-08 00:51:39 +01:00
|
|
|
ErrorOr<FlatPtr> Process::sys$pipe(int pipefd[2], int flags)
|
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 01:11:45 -08:00
|
|
|
TRY(require_promise(Pledge::stdio));
|
2022-01-29 01:29:07 +01:00
|
|
|
auto open_count = fds().with_shared([](auto& fds) { return fds.open_count(); });
|
2022-01-29 01:22:28 +01:00
|
|
|
if (open_count + 2 > OpenFileDescriptions::max_open())
|
2021-03-01 13:49:16 +01:00
|
|
|
return EMFILE;
|
2021-09-18 19:39:00 -07:00
|
|
|
// Reject flags other than O_CLOEXEC, O_NONBLOCK
|
|
|
|
if ((flags & (O_CLOEXEC | O_NONBLOCK)) != flags)
|
2021-03-01 13:49:16 +01:00
|
|
|
return EINVAL;
|
2020-07-30 23:38:15 +02:00
|
|
|
|
|
|
|
u32 fd_flags = (flags & O_CLOEXEC) ? FD_CLOEXEC : 0;
|
2021-09-07 13:56:10 +02:00
|
|
|
auto fifo = TRY(FIFO::try_create(uid()));
|
2020-07-30 23:38:15 +02:00
|
|
|
|
2022-01-29 01:22:28 +01:00
|
|
|
ScopedDescriptionAllocation reader_fd_allocation;
|
|
|
|
ScopedDescriptionAllocation writer_fd_allocation;
|
|
|
|
|
2022-01-29 01:29:07 +01:00
|
|
|
TRY(m_fds.with_exclusive([&](auto& fds) -> ErrorOr<void> {
|
2022-01-29 01:22:28 +01:00
|
|
|
reader_fd_allocation = TRY(fds.allocate());
|
|
|
|
writer_fd_allocation = TRY(fds.allocate());
|
|
|
|
return {};
|
|
|
|
}));
|
2021-09-05 16:22:52 +02:00
|
|
|
|
|
|
|
auto reader_description = TRY(fifo->open_direction(FIFO::Direction::Reader));
|
|
|
|
auto writer_description = TRY(fifo->open_direction(FIFO::Direction::Writer));
|
2020-07-30 23:38:15 +02:00
|
|
|
|
2021-09-05 16:22:52 +02:00
|
|
|
reader_description->set_readable(true);
|
|
|
|
writer_description->set_writable(true);
|
2021-09-18 19:39:00 -07:00
|
|
|
if (flags & O_NONBLOCK) {
|
|
|
|
reader_description->set_blocking(false);
|
|
|
|
writer_description->set_blocking(false);
|
|
|
|
}
|
2021-07-27 23:59:24 -07:00
|
|
|
|
2022-01-29 01:29:07 +01:00
|
|
|
TRY(m_fds.with_exclusive([&](auto& fds) -> ErrorOr<void> {
|
2022-01-29 01:22:28 +01:00
|
|
|
fds[reader_fd_allocation.fd].set(move(reader_description), fd_flags);
|
|
|
|
fds[writer_fd_allocation.fd].set(move(writer_description), fd_flags);
|
|
|
|
return {};
|
|
|
|
}));
|
2021-09-05 16:22:52 +02:00
|
|
|
|
2021-09-05 17:38:37 +02:00
|
|
|
TRY(copy_to_user(&pipefd[0], &reader_fd_allocation.fd));
|
|
|
|
TRY(copy_to_user(&pipefd[1], &writer_fd_allocation.fd));
|
2021-11-08 00:51:39 +01:00
|
|
|
return 0;
|
2020-07-30 23:38:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|