2 .\" Copyright (c) 2010-2022 Hans Petter Selasky
4 .\" All rights reserved.
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
9 .\" 1. Redistributions of source code must retain the above copyright
10 .\" notice, this list of conditions and the following disclaimer.
11 .\" 2. Redistributions in binary form must reproduce the above copyright
12 .\" notice, this list of conditions and the following disclaimer in the
13 .\" documentation and/or other materials provided with the distribution.
15 .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 .Nd "Userland character device library"
36 To load the required kernel module at boot time, place the following line in
38 .Bd -literal -offset indent
46 library contains functions to create a character device in userspace.
49 library is thread safe.
50 .Sh LIBRARY INITIALISATION / DEINITIALISATION
52 .Fn "cuse_init" "void"
53 This function initialises
55 Must be called at the beginning of the program.
56 This function returns 0 on success or a negative value on failure.
59 for known error codes.
60 If the cuse kernel module is not loaded,
61 .Dv CUSE_ERR_NOT_LOADED
65 .Fn "cuse_uninit" "void"
68 Can be called at the end of the application.
69 This function returns 0 on success or a negative value on failure.
72 for known error codes.
75 .Fn "cuse_alloc_unit_number" "int *"
76 This function stores a uniq system unit number at the pointed
78 This function returns 0 on success or a negative value on failure.
81 for known error codes.
84 .Fn "cuse_alloc_unit_number_by_id" "int *" "int id"
85 This function stores a unique system unit number at the pointed
87 The returned unit number is uniq within the given ID.
88 Valid ID values are defined by the cuse include file.
91 macros for more information.
92 This function returns 0 on success or a negative value on failure.
95 for known error codes.
98 .Fn "cuse_free_unit_number" "int"
99 This function frees the given allocated system unit number.
100 This function returns 0 on success or a negative value on failure.
103 for known error codes.
106 .Fn "cuse_free_unit_number_by_id" "int unit" "int id"
107 This function frees the given allocated system unit number belonging
109 If both the unit and id argument is -1, all allocated units will be freed.
110 This function returns 0 on success or a negative value on failure.
113 for known error codes.
116 .Fn "cuse_vmalloc" "unsigned size"
117 This function allocates
120 Only memory allocated by this function can be memory
123 This function returns a valid data pointer on success or
126 The returned pointer is always aligned to the system page size.
127 The number and size of allocations is limited by the
129 offset having to fit into a 32-bit variable typically for 32-bit
133 .Fn "cuse_is_vmalloc_addr" "void *"
134 This function returns non-zero if the passed pointer points to a valid
135 and non-freed allocation, as returned by
137 Else this function returns zero.
140 .Fn "cuse_vmfree" "void *"
141 This function frees memory allocated by
143 This function is NULL safe.
146 .Fn "cuse_vmoffset" "void *"
147 This function returns the mmap offset the client must use to
148 access the allocated memory.
149 The passed pointer must be aligned to the system page size.
151 .Ft "struct cuse_dev *"
152 .Fn "cuse_dev_create" "const struct cuse_methods *mtod" "void *priv0" "void *priv1" "uid_t" "gid_t" "int permission" "const char *fmt" "..."
153 This function creates a new character device according to the given
155 This function returns a valid cuse_dev structure pointer
159 The device name can only contain a-z,
160 A-Z, 0-9, dot, / and underscore characters.
163 .Fn "cuse_dev_destroy" "struct cuse_dev *"
164 This functions destroys a previously created character device.
167 .Fn "cuse_dev_get_priv0" "struct cuse_dev *" ,
169 .Fn "cuse_dev_get_priv1" "struct cuse_dev *" ,
171 .Fn "cuse_dev_set_priv0" "struct cuse_dev *" "void *" ,
173 .Fn "cuse_dev_set_priv1" "struct cuse_dev *" "void *"
174 These functions are used to set and get the private data of the given
178 .Fn "cuse_wait_and_process" "void"
179 This function will block and do event processing.
181 required multiple threads must be created looping on this
183 This function returns 0 on success or a negative value on failure.
186 for known error codes.
189 .Fn "cuse_dev_get_per_file_handle" "struct cuse_dev *" ,
191 .Fn "cuse_dev_set_per_file_handle" "struct cuse_dev *" "void *"
192 These functions are used to set and get the per-file-open specific handle
193 and should only be used inside the cuse file operation callbacks.
196 .Fn "cuse_set_local" "int"
197 This function instructs
202 user pointer is local, if the argument passed to it is non-zero.
203 Else the user pointer is assumed to be at the peer application.
204 This function should only be used inside the cuse file operation callbacks.
205 The value is reset to zero when the given file operation returns, and
206 does not affect any other file operation callbacks.
209 .Fn "cuse_get_local" "void"
210 Returns the current local state.
215 .Fn "cuse_copy_out" "const void *src" "void *peer_dst" "int len" ,
217 .Fn "cuse_copy_in" "const void *peer_src" "void *dst" "int len"
218 These functions are used to transfer data between the local
219 application and the peer application.
220 These functions must be used
221 when operating on the data pointers passed to the
227 These functions return 0 on success or a negative value on failure.
230 for known error codes.
233 .Fn "cuse_got_peer_signal" "void"
234 This function is used to check if a signal has been delivered to the
235 peer application and should only be used inside the cuse file
237 This function returns 0 if a signal has been
238 delivered to the caller.
239 Else it returns a negative value.
242 for known error codes.
244 .Ft "struct cuse_dev *"
245 .Fn "cuse_dev_get_current" "int *pcmd"
246 This function is used to get the current cuse device pointer and the
247 currently executing command, by
255 This function should only be used inside the
256 cuse file operation callbacks.
257 On success a valid cuse device pointer
264 .Fn "cuse_poll_wakeup" "void"
265 This function will wake up any file pollers.
266 .Sh LIBRARY LIMITATIONS
273 should not exceed what can fit into a 32-bit signed integer and is
277 Transfer lengths for ioctls should not exceed what is defined by the
280 .Sh LIBRARY CALLBACK METHODS
281 In general fflags are defined by
283 and errors are defined by
285 .Bd -literal -offset indent
323 .Fn "cuse_open_t" "struct cuse_dev *" "int fflags"
324 This function returns a
329 .Fn "cuse_close_t" "struct cuse_dev *" "int fflags"
330 This function returns a
335 .Fn "cuse_read_t" "struct cuse_dev *" "int fflags" "void *peer_ptr" "int len"
336 This function returns a
338 value in case of failure or the
339 actually transferred length in case of success.
343 must be used to transfer data to and from the
347 .Fn "cuse_write_t" "struct cuse_dev *" "int fflags" "const void *peer_ptr" "int len"
348 This function returns a
350 value in case of failure or the
351 actually transferred length in case of success.
355 must be used to transfer data to and from the
359 .Fn "cuse_ioctl_t" "struct cuse_dev *" "int fflags" "unsigned long cmd" "void *peer_data"
360 This function returns a
362 value in case of failure or zero
368 transfer data to and from the
372 .Fn "cuse_poll_t" "struct cuse_dev *" "int fflags" "int events"
373 This function returns a mask of
375 values in case of failure and success.
376 The events argument is also a mask of
379 .Bd -literal -offset indent
380 struct cuse_methods {
381 cuse_open_t *cm_open;
382 cuse_close_t *cm_close;
383 cuse_read_t *cm_read;
384 cuse_write_t *cm_write;
385 cuse_ioctl_t *cm_ioctl;
386 cuse_poll_t *cm_poll;
391 was written by Hans Petter Selasky.