blob: f29145c167f1c56d6087e72cce77d197318a15eb [file] [log] [blame]
// Copyright 2019 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 <lib/fdio/fdio.h>
#include <fbl/auto_lock.h>
#include "fdio_unistd.h"
#include "internal.h"
__EXPORT
zx_status_t fdio_fd_create(zx_handle_t handle, int* fd_out) {
zx::status io = fdio::create(zx::handle(handle));
if (io.is_error()) {
return io.status_value();
}
std::optional fd = bind_to_fd(io.value());
if (fd.has_value()) {
*fd_out = fd.value();
return ZX_OK;
}
return ZX_ERR_BAD_STATE;
}
__EXPORT
zx_status_t fdio_cwd_clone(zx_handle_t* out_handle) {
fdio_ptr cwd = []() {
fbl::AutoLock lock(&fdio_lock);
return fdio_cwd_handle.get();
}();
return cwd->clone(out_handle);
}
__EXPORT
zx_status_t fdio_fd_clone(int fd, zx_handle_t* out_handle) {
fdio_ptr io = fd_to_io(fd);
if (io == nullptr) {
return ZX_ERR_INVALID_ARGS;
}
// TODO(fxbug.dev/30920): implement/honor close-on-exec flag
return io->clone(out_handle);
}
__EXPORT
zx_status_t fdio_fd_transfer(int fd, zx_handle_t* out_handle) {
fdio_ptr io = unbind_from_fd(fd);
if (io == nullptr) {
return ZX_ERR_INVALID_ARGS;
}
std::optional ptr = GetLastReference(std::move(io));
if (ptr.has_value()) {
return ptr.value().unwrap(out_handle);
}
return ZX_ERR_UNAVAILABLE;
}