blob: b4bf43f29f9ed270f01587b80469f57668f90a25 [file] [edit]
/*
* Copyright 2026, The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef LIBWRAPFD_WRAPFD_H
#define LIBWRAPFD_WRAPFD_H
#include <stdint.h>
#include <sys/cdefs.h>
__BEGIN_DECLS
struct WrapfdDriver;
/**
* wrapfd_driver_new() - Allocates a new WrapfdDriver instance.
*
* Returns: An opaque pointer to the WrapfdDriver instance or NULL on failure.
*/
struct WrapfdDriver* wrapfd_driver_new();
/**
* wrapfd_driver_destroy() - Deallocates a WrapfdDriver instance.
*
* This does not destroy the wrapfds that has been created with
* wrapfd_driver_wrap().
*
* @driver: An opaque pointer to the WrapfdDriver instance.
*/
void wrapfd_driver_destroy(struct WrapfdDriver* driver);
/**
* wrapfd_driver_wrap() - Uses a WrapfdDriver instance to wrap an fd.
* @driver: The opaque pointer to the WrapfdDriver instance.
* @fd: The fd to wrap.
* @prot: A bitmask of permissions to apply (e.g. PROT_READ | PROT_WRITE).
*
* The fd that is returned wraps @fd, and can be used with the wrapfd functions
* to control how the underlying file can be mapped. The caller is responsible
* for managing @fd after this call returns.
*
* Returns:
* An fd on success.
* -EINVAL if the driver pointer is NULL.
* -EINVAL if the fd to wrap is not a valid or supported fd.
* -EINVAL if the prot value is not PROT_READ, PROT_WRITE, a combination of the two, or PROT_NONE.
* -EBADF if the driver pointer was not obtained via a call to wrapfd_driver_new().
* -ENOMEM if the kernel could not allocate memory to satisfy the request.
* -EMFILE if the process reaches the limit of total number of open files.
*/
int wrapfd_driver_wrap(const struct WrapfdDriver* driver, int fd, int prot);
enum WrapfdState : uint32_t {
WRAPFD_STATE_EMPTY = 0,
WRAPFD_STATE_RDONLY = 1,
WRAPFD_STATE_RDWR = 2,
};
/**
* wrapfd_get_state() - Query the state of a wrapfd.
* @wrapfd: The wrapfd to query.
* @state_out: A pointer to where to store the state of the wrapfd. Values are
* one of the WrapfdState enum values.
*
* Returns:
* 0 on success and the wrapfd state is stored at the memory location pointed to by state_out.
* -EBADF if the fd is not a valid fd in the process' context.
* -ENOTTY if the fd is not a wrapfd.
*/
int32_t wrapfd_get_state(int wrapfd, uint32_t* state_out);
/**
* wrapfd_acquire_ownership() - Get ownership of buffer.
* @wrapfd: The wrapfd.
*
* Returns:
* 0 on success.
* -EBADF if the fd is not a valid fd in the process' context.
* -ENOTTY if the fd is not a wrapfd.
* -EBUSY if the fd is owned by another process or by a kernel driver.
* -EINVAL if the fd is mapped into the address space of a process.
* -ENOENT if the fd is empty (i.e. it is not wrapping an underlying buffer).
*/
int32_t wrapfd_acquire_ownership(int wrapfd);
/**
* wrapfd_release_ownership() - Release ownership of buffer.
* @wrapfd: The wrapfd.
*
* Returns:
* 0 on success.
* -EBADF if the fd is not a valid fd in the process' context.
* -ENOTTY if the fd is not a wrapfd.
* -EBUSY if the fd is not owned by the invoking process.
* -EINVAL if the fd is currently mapped into the address space of a process.
*/
int32_t wrapfd_release_ownership(int wrapfd);
/**
* wrapfd_load() - Loads a segment of a file into the buffer at the given offset.
* @wrapfd: The wrapfd.
* @file: The fd corresponding to the file being loaded.
* @file_offset: Offset into the file.
* @buf_offset: Offset into the buffer we're loading into.
* @len: length of the data.
*
* Returns:
* 0 on success.
* -ENOTTY if the fd is not a wrapfd.
* -EINVAL if the file offset is not page aligned.
* -EINVAL if the buffer offset is not page aligned.
* -EINVAL if the file is not suitable for reading.
* -EINVAL if the file does not support direct I/O.
* -EINVAL if reading len bytes of data from file_offset would go beyond the length of the file.
* -EINVAL if the sum of buf_offset or file_offset and len would cause an overflow.
* -EINVAL if the buffer is not part of system RAM.
* -EINVAL if writing len bytes of data from buf_offset would go beyond the length of the buffer.
* -ENOENT if the fd is empty (i.e. it is not wrapping an underlying buffer).
* -EBUSY if the fd is not owned by the invoking process.
* -EINVAL if the fd is mapped in the address space of a process.
* -EBADF if the fd for the file being loaded is not valid in the process' context.
* -EBADF if the file being loaded was not opened in read mode.
* -EAGAIN if the underlying buffer for the wrapfd is being modified.
*/
int32_t wrapfd_load(int wrapfd, int file, uint64_t file_offset, uint64_t buf_offset,
uint64_t len);
/**
* wrapfd_rewrap() - Move the buffer into a new wrap.
* @wrapfd: The wrapfd. The contents of this wrapfd are empty after this call.
* The caller is still responsible for managing @wrapfd after this call returns.
* @prot: New prot bitmask.
*
* Returns:
* the new wrapfd on success
* -EBADF if the fd is not a valid fd in the process' context.
* -ENOTTY if the fd is not a wrapfd.
* -EINVAL if the prot value is not PROT_READ, PROT_WRITE, a combination of the two, or PROT_NONE.
* -EBUSY if the fd is not owned by the invoking process.
* -EINVAL if the fd is mapped in the address space of a process.
* -ENOENT if the fd is empty (i.e. it is not wrapping an underlying buffer).
* -EAGAIN if the underlying buffer for the wrapfd is being modified.
* -ENOMEM if the kernel cannot allocate memory to satisfy the request.
* -EMFILE if the process reaches the limit of total number of open files.
*/
int wrapfd_rewrap(int wrapfd, int prot);
/**
* wrapfd_empty() - Empty the wrap and closes the internal buffer.
* @wrapfd: The wrapfd. The contents of this wrapfd are empty after this call.
* The caller is responsible for managing @wrapfd after this call returns.
*
* Returns:
* 0 on success
* -EBADF if the fd is not a valid fd in the process' context.
* -ENOTTY if the fd is not a wrapfd.
* -EBUSY if the fd is not owned by the invoking process.
* -EINVAL if the fd is mapped in the address space of a process.
* -ENOENT if the fd is empty (i.e. it is not wrapping an underlying buffer).
* -EAGAIN if the underlying buffer for the wrapfd is being modified.
*/
int32_t wrapfd_empty(int wrapfd);
/**
* wrapfd_allow_guests() - Allow non-owner processes to map the buffer.
* @wrapfd: The wrapfd.
*
* Returns:
* 0 on success
* -EBADF if the fd is not a valid fd in the process' context.
* -ENOTTY if the fd is not a wrapfd.
* -EBUSY if the fd is not owned by the invoking process.
* -EINVAL if the fd is mapped in the address space of a process.
* -ENOENT if the fd is empty (i.e. it is not wrapping an underlying buffer).
*/
int32_t wrapfd_allow_guests(int wrapfd);
/**
* wrapfd_prohibit_guests() - Disallow non-owner processes to map the buffer.
* @wrapfd: The wrapfd.
*
* Returns:
* 0 on success
* -EBADF if the fd is not a valid fd in the process' context.
* -ENOTTY if the fd is not a wrapfd.
* -EBUSY if the fd is not owned by the invoking process.
* -EINVAL if the fd is mapped in the address space of a process.
* -ENOENT if the fd is empty (i.e. it is not wrapping an underlying buffer).
*/
int32_t wrapfd_prohibit_guests(int wrapfd);
__END_DECLS
#endif // LIBWRAPFD_WRAPFD_H