-
Notifications
You must be signed in to change notification settings - Fork 5
feat(kernel): added PTY subsystem, proc_set_handle API, and reusable line discipline #66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,228 @@ | ||
| #include "pty/pty.h" | ||
| #include "resource/resource.h" | ||
| #include "common/ring_buffer.h" | ||
| #include "fs/fstypes.h" | ||
| #include "mm/heap.h" | ||
| #include "dynpriv/dynpriv.h" | ||
|
|
||
| namespace pty { | ||
|
|
||
| __PRIVILEGED_BSS static uint32_t g_next_pty_id; | ||
|
|
||
| __PRIVILEGED_CODE void pty_channel::ref_destroy(pty_channel* self) { | ||
| if (!self) { | ||
| return; | ||
| } | ||
| ring_buffer_destroy(self->m_input_rb); | ||
| ring_buffer_destroy(self->m_output_rb); | ||
| heap::kfree_delete(self); | ||
| } | ||
|
|
||
| __PRIVILEGED_CODE static void pty_echo_fn(void* ctx, const uint8_t* buf, size_t len) { | ||
| auto* chan = static_cast<pty_channel*>(ctx); | ||
| (void)ring_buffer_write(chan->m_output_rb, buf, len, true); | ||
| } | ||
|
|
||
| // Master ops | ||
|
|
||
| static ssize_t pty_master_read( | ||
| resource::resource_object* obj, void* kdst, size_t count, uint32_t flags | ||
| ) { | ||
| if (!obj || !obj->impl || !kdst) { | ||
| return resource::ERR_INVAL; | ||
| } | ||
| auto* ep = static_cast<pty_endpoint*>(obj->impl); | ||
| bool nonblock = (flags & fs::O_NONBLOCK) != 0; | ||
| ssize_t result; | ||
| RUN_ELEVATED({ | ||
| result = ring_buffer_read(ep->channel->m_output_rb, | ||
| static_cast<uint8_t*>(kdst), count, nonblock); | ||
| }); | ||
| return result; | ||
| } | ||
|
|
||
| static ssize_t pty_master_write( | ||
| resource::resource_object* obj, const void* ksrc, size_t count, uint32_t flags | ||
| ) { | ||
| (void)flags; | ||
| if (!obj || !obj->impl || !ksrc) { | ||
| return resource::ERR_INVAL; | ||
| } | ||
| auto* ep = static_cast<pty_endpoint*>(obj->impl); | ||
| auto* chan = ep->channel.ptr(); | ||
|
|
||
| ssize_t result; | ||
| RUN_ELEVATED({ | ||
| if (chan->m_input_rb->reader_closed) { | ||
| result = resource::ERR_PIPE; | ||
| } else { | ||
| terminal::ld_input_buf(&chan->m_ld, chan->m_input_rb, &chan->m_echo, | ||
| static_cast<const char*>(ksrc), count); | ||
| result = static_cast<ssize_t>(count); | ||
| } | ||
| }); | ||
| return result; | ||
| } | ||
|
|
||
| static void pty_master_close(resource::resource_object* obj) { | ||
| if (!obj || !obj->impl) { | ||
| return; | ||
| } | ||
| auto* ep = static_cast<pty_endpoint*>(obj->impl); | ||
|
|
||
| RUN_ELEVATED({ | ||
| ring_buffer_close_write(ep->channel->m_input_rb); | ||
| ring_buffer_close_read(ep->channel->m_output_rb); | ||
| heap::kfree_delete(ep); | ||
| }); | ||
| obj->impl = nullptr; | ||
| } | ||
|
|
||
| // Slave ops | ||
|
|
||
| static ssize_t pty_slave_read( | ||
| resource::resource_object* obj, void* kdst, size_t count, uint32_t flags | ||
| ) { | ||
| if (!obj || !obj->impl || !kdst) { | ||
| return resource::ERR_INVAL; | ||
| } | ||
| auto* ep = static_cast<pty_endpoint*>(obj->impl); | ||
| bool nonblock = (flags & fs::O_NONBLOCK) != 0; | ||
| ssize_t result; | ||
| RUN_ELEVATED({ | ||
| result = ring_buffer_read(ep->channel->m_input_rb, | ||
| static_cast<uint8_t*>(kdst), count, nonblock); | ||
| }); | ||
| return result; | ||
| } | ||
|
|
||
| static ssize_t pty_slave_write( | ||
| resource::resource_object* obj, const void* ksrc, size_t count, uint32_t flags | ||
| ) { | ||
| if (!obj || !obj->impl || !ksrc) { | ||
| return resource::ERR_INVAL; | ||
| } | ||
| auto* ep = static_cast<pty_endpoint*>(obj->impl); | ||
| bool nonblock = (flags & fs::O_NONBLOCK) != 0; | ||
| ssize_t result; | ||
| RUN_ELEVATED({ | ||
| result = ring_buffer_write(ep->channel->m_output_rb, | ||
| static_cast<const uint8_t*>(ksrc), count, nonblock); | ||
| }); | ||
| return result; | ||
| } | ||
|
|
||
| static void pty_slave_close(resource::resource_object* obj) { | ||
| if (!obj || !obj->impl) { | ||
| return; | ||
| } | ||
| auto* ep = static_cast<pty_endpoint*>(obj->impl); | ||
|
|
||
| RUN_ELEVATED({ | ||
| ring_buffer_close_write(ep->channel->m_output_rb); | ||
| ring_buffer_close_read(ep->channel->m_input_rb); | ||
| heap::kfree_delete(ep); | ||
| }); | ||
| obj->impl = nullptr; | ||
| } | ||
|
|
||
| static int32_t pty_slave_ioctl( | ||
| resource::resource_object* obj, uint32_t cmd, uint64_t arg | ||
| ) { | ||
| (void)arg; | ||
| if (!obj || !obj->impl) { | ||
| return resource::ERR_INVAL; | ||
| } | ||
| auto* ep = static_cast<pty_endpoint*>(obj->impl); | ||
| return terminal::ld_set_mode(&ep->channel->m_ld, cmd); | ||
| } | ||
|
|
||
| // Ops tables | ||
|
|
||
| static const resource::resource_ops g_pty_master_ops = { | ||
| pty_master_read, | ||
| pty_master_write, | ||
| pty_master_close, | ||
| nullptr, | ||
| }; | ||
|
|
||
| static const resource::resource_ops g_pty_slave_ops = { | ||
| pty_slave_read, | ||
| pty_slave_write, | ||
| pty_slave_close, | ||
| pty_slave_ioctl, | ||
| }; | ||
|
|
||
| // Pair creation | ||
|
|
||
| __PRIVILEGED_CODE int32_t create_pair( | ||
| resource::resource_object** out_master, | ||
| resource::resource_object** out_slave | ||
| ) { | ||
| if (!out_master || !out_slave) { | ||
| return resource::ERR_INVAL; | ||
| } | ||
|
|
||
| auto chan = rc::make_kref<pty_channel>(); | ||
| if (!chan) { | ||
| return resource::ERR_NOMEM; | ||
| } | ||
|
|
||
| chan->m_input_rb = ring_buffer_create(PTY_RING_CAPACITY); | ||
| if (!chan->m_input_rb) { | ||
| return resource::ERR_NOMEM; | ||
| } | ||
|
|
||
| chan->m_output_rb = ring_buffer_create(PTY_RING_CAPACITY); | ||
| if (!chan->m_output_rb) { | ||
| ring_buffer_destroy(chan->m_input_rb); | ||
| chan->m_input_rb = nullptr; | ||
| return resource::ERR_NOMEM; | ||
| } | ||
|
|
||
| terminal::ld_init(&chan->m_ld); | ||
| chan->m_echo = { pty_echo_fn, chan.ptr() }; | ||
| chan->m_id = __atomic_fetch_add(&g_next_pty_id, 1, __ATOMIC_RELAXED); | ||
|
|
||
| auto* ep_master = heap::kalloc_new<pty_endpoint>(); | ||
| if (!ep_master) { | ||
| return resource::ERR_NOMEM; | ||
| } | ||
| ep_master->channel = chan; | ||
| ep_master->is_master = true; | ||
|
|
||
| auto* ep_slave = heap::kalloc_new<pty_endpoint>(); | ||
| if (!ep_slave) { | ||
| heap::kfree_delete(ep_master); | ||
| return resource::ERR_NOMEM; | ||
| } | ||
| ep_slave->channel = static_cast<rc::strong_ref<pty_channel>&&>(chan); | ||
| ep_slave->is_master = false; | ||
|
|
||
| auto* obj_master = heap::kalloc_new<resource::resource_object>(); | ||
| if (!obj_master) { | ||
| heap::kfree_delete(ep_slave); | ||
| heap::kfree_delete(ep_master); | ||
| return resource::ERR_NOMEM; | ||
| } | ||
| obj_master->type = resource::resource_type::PTY; | ||
| obj_master->ops = &g_pty_master_ops; | ||
| obj_master->impl = ep_master; | ||
|
|
||
| auto* obj_slave = heap::kalloc_new<resource::resource_object>(); | ||
| if (!obj_slave) { | ||
| heap::kfree_delete(obj_master); | ||
| heap::kfree_delete(ep_slave); | ||
| heap::kfree_delete(ep_master); | ||
| return resource::ERR_NOMEM; | ||
| } | ||
| obj_slave->type = resource::resource_type::PTY; | ||
| obj_slave->ops = &g_pty_slave_ops; | ||
| obj_slave->impl = ep_slave; | ||
|
|
||
| *out_master = obj_master; | ||
| *out_slave = obj_slave; | ||
| return resource::OK; | ||
| } | ||
|
|
||
| } // namespace pty | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| #ifndef STELLUX_PTY_PTY_H | ||
| #define STELLUX_PTY_PTY_H | ||
|
|
||
| #include "common/types.h" | ||
| #include "rc/ref_counted.h" | ||
| #include "rc/strong_ref.h" | ||
| #include "terminal/line_discipline.h" | ||
|
|
||
| struct ring_buffer; | ||
| namespace resource { struct resource_object; } | ||
|
|
||
| namespace pty { | ||
|
|
||
| constexpr int32_t OK = 0; | ||
| constexpr int32_t ERR = -1; | ||
| constexpr size_t PTY_RING_CAPACITY = 4096; | ||
|
|
||
| struct pty_channel : rc::ref_counted<pty_channel> { | ||
| ring_buffer* m_input_rb; // master write -> ld -> slave read | ||
| ring_buffer* m_output_rb; // slave write -> master read | ||
| terminal::line_discipline m_ld; | ||
| terminal::echo_target m_echo; | ||
| uint32_t m_id; | ||
|
|
||
| /** @note Privilege: **required** */ | ||
| __PRIVILEGED_CODE static void ref_destroy(pty_channel* self); | ||
| }; | ||
|
|
||
| struct pty_endpoint { | ||
| rc::strong_ref<pty_channel> channel; | ||
| bool is_master; | ||
| }; | ||
|
|
||
| /** | ||
| * @brief Create a connected PTY master/slave pair. | ||
| * Returns two resource_objects, each with refcount 1. | ||
| * @note Privilege: **required** | ||
| */ | ||
| __PRIVILEGED_CODE int32_t create_pair( | ||
| resource::resource_object** out_master, | ||
| resource::resource_object** out_slave | ||
| ); | ||
|
|
||
| } // namespace pty | ||
|
|
||
| #endif // STELLUX_PTY_PTY_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.