2020-07-30 18:38:15 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-07-30 18:38:15 -03:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <Kernel/FileSystem/FIFO.h>
|
|
|
|
|
#include <Kernel/Process.h>
|
|
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
|
2021-06-28 15:59:35 -03:00
|
|
|
KResultOr<FlatPtr> Process::sys$pipe(int pipefd[2], int flags)
|
2020-07-30 18:38:15 -03:00
|
|
|
{
|
2021-07-18 15:20:12 -03:00
|
|
|
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
|
2020-07-30 18:38:15 -03:00
|
|
|
REQUIRE_PROMISE(stdio);
|
2021-06-22 15:22:17 -03:00
|
|
|
if (fds().open_count() + 2 > fds().max_open())
|
2021-03-01 09:49:16 -03:00
|
|
|
return EMFILE;
|
2021-09-18 23:39:00 -03:00
|
|
|
// Reject flags other than O_CLOEXEC, O_NONBLOCK
|
|
|
|
|
if ((flags & (O_CLOEXEC | O_NONBLOCK)) != flags)
|
2021-03-01 09:49:16 -03:00
|
|
|
return EINVAL;
|
2020-07-30 18:38:15 -03:00
|
|
|
|
|
|
|
|
u32 fd_flags = (flags & O_CLOEXEC) ? FD_CLOEXEC : 0;
|
2021-09-07 08:56:10 -03:00
|
|
|
auto fifo = TRY(FIFO::try_create(uid()));
|
2020-07-30 18:38:15 -03:00
|
|
|
|
2021-09-05 11:22:52 -03: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 18:38:15 -03:00
|
|
|
|
2021-09-05 11:22:52 -03:00
|
|
|
reader_description->set_readable(true);
|
|
|
|
|
writer_description->set_writable(true);
|
2021-09-18 23:39:00 -03:00
|
|
|
if (flags & O_NONBLOCK) {
|
|
|
|
|
reader_description->set_blocking(false);
|
|
|
|
|
writer_description->set_blocking(false);
|
|
|
|
|
}
|
2021-07-28 03:59:24 -03:00
|
|
|
|
2021-09-05 11:22:52 -03: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 12:38:37 -03: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 18:38:15 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|