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>
|
2023-02-24 17:45:37 +00:00
|
|
|
#include <Kernel/Tasks/Process.h>
|
2020-07-30 21:38:15 +00:00
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
2022-08-16 18:35:32 +00:00
|
|
|
ErrorOr<FlatPtr> Process::sys$pipe(Userspace<int*> pipefd, int flags)
|
2020-07-30 21:38:15 +00:00
|
|
|
{
|
2022-08-17 20:03:04 +00:00
|
|
|
VERIFY_NO_PROCESS_BIG_LOCK(this);
|
2021-12-29 09:11:45 +00:00
|
|
|
TRY(require_promise(Pledge::stdio));
|
2022-08-16 18:16:17 +00:00
|
|
|
|
2021-09-19 02:39:00 +00:00
|
|
|
// Reject flags other than O_CLOEXEC, O_NONBLOCK
|
|
|
|
if ((flags & (O_CLOEXEC | O_NONBLOCK)) != 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;
|
2022-08-20 22:21:01 +00:00
|
|
|
auto credentials = this->credentials();
|
|
|
|
auto fifo = TRY(FIFO::try_create(credentials->uid()));
|
2020-07-30 21:38:15 +00:00
|
|
|
|
2021-09-05 14:22:52 +00:00
|
|
|
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-09-19 02:39:00 +00:00
|
|
|
if (flags & O_NONBLOCK) {
|
|
|
|
reader_description->set_blocking(false);
|
|
|
|
writer_description->set_blocking(false);
|
|
|
|
}
|
2021-07-28 06:59:24 +00:00
|
|
|
|
2022-01-29 00:29:07 +00:00
|
|
|
TRY(m_fds.with_exclusive([&](auto& fds) -> ErrorOr<void> {
|
2022-08-16 18:39:45 +00:00
|
|
|
auto reader_fd_allocation = TRY(fds.allocate());
|
|
|
|
auto writer_fd_allocation = TRY(fds.allocate());
|
|
|
|
|
2022-01-29 00:22:28 +00:00
|
|
|
fds[reader_fd_allocation.fd].set(move(reader_description), fd_flags);
|
|
|
|
fds[writer_fd_allocation.fd].set(move(writer_description), fd_flags);
|
2022-08-16 18:35:32 +00:00
|
|
|
|
|
|
|
int fds_for_userspace[2] = {
|
|
|
|
reader_fd_allocation.fd,
|
|
|
|
writer_fd_allocation.fd,
|
|
|
|
};
|
2023-03-24 08:59:28 +00:00
|
|
|
if (copy_n_to_user(pipefd, fds_for_userspace, 2).is_error()) {
|
|
|
|
// Avoid leaking both file descriptors on error.
|
2022-08-16 18:35:32 +00:00
|
|
|
fds[reader_fd_allocation.fd] = {};
|
|
|
|
fds[writer_fd_allocation.fd] = {};
|
|
|
|
return EFAULT;
|
|
|
|
}
|
2022-01-29 00:22:28 +00:00
|
|
|
return {};
|
|
|
|
}));
|
2021-11-07 23:51:39 +00:00
|
|
|
return 0;
|
2020-07-30 21:38:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|