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