| // Copyright 2020 The Fuchsia Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| #include <fuchsia/hardware/pty/llcpp/fidl.h> |
| #include <lib/fdio/directory.h> |
| #include <lib/fdio/fd.h> |
| #include <lib/fdio/io.h> |
| #include <lib/fdio/unsafe.h> |
| #include <lib/zx/channel.h> |
| #include <stdio.h> |
| #include <zircon/status.h> |
| #include <zircon/syscalls.h> |
| |
| #include <utility> |
| |
| #include "fuchsia-compat.h" |
| |
| namespace fpty = ::llcpp::fuchsia::hardware::pty; |
| |
| int fuchsia_open_pty_client(int fd, uint32_t id) { |
| zx::channel device_channel, client_channel; |
| zx_status_t status = zx::channel::create(0, &device_channel, &client_channel); |
| if (status != ZX_OK) { |
| fprintf(stderr, "Failed to open PTY client: %s\n", zx_status_get_string(status)); |
| return -1; |
| } |
| |
| fdio_t* io = fdio_unsafe_fd_to_io(fd); |
| if (io == NULL) { |
| fprintf(stderr, "Failed to open PTY client: couldn't create fdio\n"); |
| return -1; |
| } |
| |
| auto result = fpty::Device::Call::OpenClient(zx::unowned_channel(fdio_unsafe_borrow_channel(io)), |
| id, std::move(device_channel)); |
| |
| fdio_unsafe_release(io); |
| if (result.status() != ZX_OK) { |
| fprintf(stderr, "Failed to open PTY client: %s\n", zx_status_get_string(result.status())); |
| return -1; |
| } |
| if (result->s != ZX_OK) { |
| fprintf(stderr, "Failed to open PTY client: %s\n", zx_status_get_string(result->s)); |
| return -1; |
| } |
| |
| int client_fd; |
| status = fdio_fd_create(client_channel.release(), &client_fd); |
| if (status != ZX_OK) { |
| fprintf(stderr, "Failed to open PTY client: %s\n", zx_status_get_string(status)); |
| return -1; |
| } |
| return client_fd; |
| } |