CUSE(3)
π CUSE(3) β libcuse π
From the Library Functions Manual, bestie: this is the whole userland character-device situation, and itβs lowkey iconic.
πΈ NAME πΈ
libcuse β the userland character device library. No cap, thatβs the vibe: character devices, but in userspace. π
π LIBRARY π
library βlibcuseβ
β¨ SYNOPSIS β¨
To load the required kernel module at boot time, place the following line in loader.conf(5):
cuse_load="YES"
Then include this header:
#include <cuse.h>
π DESCRIPTION π
The libcuse library contains functions to create a character device in userspace. It is thread safe. Periodt. π
π LIBRARY INITIALISATION / DEINITIALISATION π
int cuse_init(void)β initialiseslibcuse. Must be called at the beginning of the program. Returns0on success or a negative value on failure. SeeCUSE_ERR_XXXfor known error codes. If thecusekernel module is not loaded,CUSE_ERR_NOT_LOADEDis returned.int cuse_uninit(void)β deinitialiseslibcuse. Can be called at the end of the application. Returns0on success or a negative value on failure. SeeCUSE_ERR_XXXfor known error codes.
π¦ UNIT MANAGEMENT π¦
int cuse_alloc_unit_number(int *)β stores a unique system unit number at the pointed integer location. Returns0on success or a negative value on failure. SeeCUSE_ERR_XXXfor known error codes.int cuse_alloc_unit_number_by_id(int *, int id)β stores a unique system unit number at the pointed integer location. The returned unit number is unique within the given ID. Valid ID values are defined by thecuseinclude file. See theCUSE_ID_XXX()macros for more information. Returns0on success or a negative value on failure. SeeCUSE_ERR_XXXfor known error codes.int cuse_free_unit_number(int)β frees the given allocated system unit number. Returns0on success or a negative value on failure. SeeCUSE_ERR_XXXfor known error codes.int cuse_free_unit_number_by_id(int unit, int id)β frees the given allocated system unit number belonging to the given ID. If both theunitandidargument is-1, all allocated units will be freed. Returns0on success or a negative value on failure. SeeCUSE_ERR_XXXfor known error codes.
π LIBRARY USAGE π
void * cuse_vmalloc(unsigned size)β allocatessizebytes of memory. Only memory allocated by this function can be memory mapped bymmap(2). Returns a valid data pointer on success orNULLon failure. The returned pointer is always aligned to the system page size. The number and size of allocations is limited by themmap(2)offset having to fit into a 32-bit variable, typically for 32-bit applications.int cuse_is_vmalloc_addr(void *)β returns non-zero if the passed pointer points to a valid and non-freed allocation, as returned bycuse_vmalloc(). Else this function returns zero.void cuse_vmfree(void *)β frees memory allocated bycuse_vmalloc(). This function isNULLsafe.unsigned long cuse_vmoffset(void *)β returns themmapoffset the client must use to access the allocated memory. The passed pointer must be aligned to the system page size.struct cuse_dev * cuse_dev_create(const struct cuse_methods *mtod, void *priv0, void *priv1, uid_t, gid_t, int permission, const char *fmt, ...)β creates a new character device according to the given parameters. Returns a validcuse_devstructure pointer on success orNULLon failure.
The device name can only containa-z,A-Z,0-9, dot,/and underscore characters. Thatβs the rule, bestie. π
void cuse_dev_destroy(struct cuse_dev *)β destroys a previously created character device.void * cuse_dev_get_priv0(struct cuse_dev *)β gets private data of the given cuse device.void * cuse_dev_get_priv1(struct cuse_dev *)β gets private data of the given cuse device.void cuse_dev_set_priv0(struct cuse_dev *, void *)β sets private data of the given cuse device.void cuse_dev_set_priv1(struct cuse_dev *, void *)β sets private data of the given cuse device.
These functions are used to set and get the private data of the given cuse device.
int cuse_wait_and_process(void)β blocks and does event processing. If parallel I/O is required, multiple threads must be created looping on this function. Returns0on success or a negative value on failure. SeeCUSE_ERR_XXXfor known error codes.void * cuse_dev_get_per_file_handle(struct cuse_dev *)β gets the per-file-open specific handle. Should only be used inside the cuse file operation callbacks.void cuse_dev_set_per_file_handle(struct cuse_dev *, void *)β sets the per-file-open specific handle. Should only be used inside the cuse file operation callbacks.void cuse_set_local(int)β instructscuse_copy_out()andcuse_copy_in()that the user pointer is local, if the argument passed to it is non-zero. Else the user pointer is assumed to be at the peer application. Should only be used inside the cuse file operation callbacks. The value is reset to zero when the given file operation returns, and does not affect any other file operation callbacks.int cuse_get_local(void)β returns the current local state. Seecuse_set_local().int cuse_copy_out(const void *src, void *peer_dst, int len)β transfers data between the local application and the peer application. Must be used when operating on the data pointers passed to thecm_read(),cm_write(), andcm_ioctl()callback functions. Returns0on success or a negative value on failure. SeeCUSE_ERR_XXXfor known error codes.int cuse_copy_in(const void *peer_src, void *dst, int len)β transfers data between the local application and the peer application. Must be used when operating on the data pointers passed to thecm_read(),cm_write(), andcm_ioctl()callback functions. Returns0on success or a negative value on failure. SeeCUSE_ERR_XXXfor known error codes.int cuse_got_peer_signal(void)β used to check if a signal has been delivered to the peer application. Should only be used inside the cuse file operation callbacks. Returns0if a signal has been delivered to the caller. Else it returns a negative value. SeeCUSE_ERR_XXXfor known error codes.struct cuse_dev * cuse_dev_get_current(int *pcmd)β used to get the current cuse device pointer and the currently executing command, byCUSE_CMD_XXXvalue. Thepcmdargument is allowed to beNULL. Should only be used inside the cuse file operation callbacks. On success a valid cuse device pointer is returned. On failureNULLis returned.void cuse_poll_wakeup(void)β wakes up any file pollers.
β LIBRARY LIMITATIONS β
Transfer lengths for read(), write(), cuse_copy_in(), and cuse_copy_out() should not exceed what can fit into a 32-bit signed integer and is defined by the CUSE_LENGTH_MAX() macro. Transfer lengths for ioctls should not exceed what is defined by the CUSE_BUFFER_MAX() macro.
This is one of those βrespect the limitsβ situations, bestie. Donβt test it. π
π₯ LIBRARY CALLBACK METHODS π₯
In general fflags are defined by CUSE_FFLAG_XXX and errors are defined by CUSE_ERR_XXX.
enum {
CUSE_ERR_NONE
CUSE_ERR_BUSY
CUSE_ERR_WOULDBLOCK
CUSE_ERR_INVALID
CUSE_ERR_NO_MEMORY
CUSE_ERR_FAULT
CUSE_ERR_SIGNAL
CUSE_ERR_OTHER
CUSE_ERR_NOT_LOADED
CUSE_ERR_NO_DEVICE
CUSE_POLL_NONE
CUSE_POLL_READ
CUSE_POLL_WRITE
CUSE_POLL_ERROR
CUSE_FFLAG_NONE
CUSE_FFLAG_READ
CUSE_FFLAG_WRITE
CUSE_FFLAG_NONBLOCK
CUSE_FFLAG_COMPAT32
CUSE_CMD_NONE
CUSE_CMD_OPEN
CUSE_CMD_CLOSE
CUSE_CMD_READ
CUSE_CMD_WRITE
CUSE_CMD_IOCTL
CUSE_CMD_POLL
CUSE_CMD_SIGNAL
CUSE_CMD_SYNC
CUSE_CMD_MAX
};
int cuse_open_t(struct cuse_dev *, int fflags)β returns aCUSE_ERR_XXXvalue.int cuse_close_t(struct cuse_dev *, int fflags)β returns aCUSE_ERR_XXXvalue.int cuse_read_t(struct cuse_dev *, int fflags, void *peer_ptr, int len)β returns aCUSE_ERR_XXXvalue in case of failure or the actually transferred length in case of success.cuse_copy_in()andcuse_copy_out()must be used to transfer data to and from thepeer_ptr.int cuse_write_t(struct cuse_dev *, int fflags, const void *peer_ptr, int len)β returns aCUSE_ERR_XXXvalue in case of failure or the actually transferred length in case of success.cuse_copy_in()andcuse_copy_out()must be used to transfer data to and from thepeer_ptr.int cuse_ioctl_t(struct cuse_dev *, int fflags, unsigned long cmd, void *peer_data)β returns aCUSE_ERR_XXXvalue in case of failure or zero in case of success.cuse_copy_in()andcuse_copy_out()must be used to transfer data to and from thepeer_data.int cuse_poll_t(struct cuse_dev *, int fflags, int events)β returns a mask ofCUSE_POLL_XXXvalues in case of failure and success. Theeventsargument is also a mask ofCUSE_POLL_XXXvalues.
struct cuse_methods {
cuse_open_t *cm_open;
cuse_close_t *cm_close;
cuse_read_t *cm_read;
cuse_write_t *cm_write;
cuse_ioctl_t *cm_ioctl;
cuse_poll_t *cm_poll;
};
π HISTORY π
libcuse was written by Hans Petter Selasky.
Thatβs the page, bestie. Source/version: Debian, dated July 18, 2022 β CUSE(3). Slay. πβ¨
π¦ where this page comes from
- freebsd 14.5: base 14.5
- freebsd 15.1: base 15.1
- Copyright (c) 2010-2022 Hans Petter Selasky
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms