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/FIFO.h>
|
|
|
|
#include <Kernel/Process.h>
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
2021-06-28 18:59:35 +00:00
|
|
|
KResultOr<FlatPtr> Process::sys$pipe(int pipefd[2], int flags)
|
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(stdio);
|
2021-06-22 18:22:17 +00:00
|
|
|
if (fds().open_count() + 2 > fds().max_open())
|
2021-03-01 12:49:16 +00:00
|
|
|
return EMFILE;
|
2020-07-30 21:38:15 +00:00
|
|
|
// Reject flags other than O_CLOEXEC.
|
|
|
|
if ((flags & O_CLOEXEC) != flags)
|
2021-03-01 12:49:16 +00:00
|
|
|
return EINVAL;
|
2020-07-30 21:38:15 +00:00
|
|
|
|
|
|
|
u32 fd_flags = (flags & O_CLOEXEC) ? FD_CLOEXEC : 0;
|
2021-08-01 09:30:52 +00:00
|
|
|
auto fifo = FIFO::try_create(uid());
|
|
|
|
if (!fifo)
|
|
|
|
return ENOMEM;
|
2020-07-30 21:38:15 +00:00
|
|
|
|
2021-09-05 14:22:52 +00:00
|
|
|
auto reader_fd_allocation = TRY(m_fds.allocate());
|
|
|
|
auto writer_fd_allocation = TRY(m_fds.allocate());
|
|
|
|
|
|
|
|
auto reader_description = TRY(fifo->open_direction(FIFO::Direction::Reader));
|
|
|
|
auto writer_description = TRY(fifo->open_direction(FIFO::Direction::Writer));
|
2020-07-30 21:38:15 +00:00
|
|
|
|
2021-09-05 14:22:52 +00:00
|
|
|
reader_description->set_readable(true);
|
|
|
|
writer_description->set_writable(true);
|
2021-07-28 06:59:24 +00:00
|
|
|
|
2021-09-05 14:22:52 +00:00
|
|
|
m_fds[reader_fd_allocation.fd].set(move(reader_description), fd_flags);
|
|
|
|
m_fds[writer_fd_allocation.fd].set(move(writer_description), fd_flags);
|
|
|
|
|
2021-09-05 15:38:37 +00:00
|
|
|
TRY(copy_to_user(&pipefd[0], &reader_fd_allocation.fd));
|
|
|
|
TRY(copy_to_user(&pipefd[1], &writer_fd_allocation.fd));
|
|
|
|
return KSuccess;
|
2020-07-30 21:38:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|