]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libcuse/cuse.3
Merge llvm trunk r321414 to contrib/llvm.
[FreeBSD/FreeBSD.git] / lib / libcuse / cuse.3
1 .\" $FreeBSD$
2 .\"
3 .\" Copyright (c) 2010-2013 Hans Petter Selasky
4 .\"
5 .\" All rights reserved.
6 .\"
7 .\" Redistribution and use in source and binary forms, with or without
8 .\" modification, are permitted provided that the following conditions
9 .\" are met:
10 .\" 1. Redistributions of source code must retain the above copyright
11 .\"    notice, this list of conditions and the following disclaimer.
12 .\" 2. Redistributions in binary form must reproduce the above copyright
13 .\"    notice, this list of conditions and the following disclaimer in the
14 .\"    documentation and/or other materials provided with the distribution.
15 .\"
16 .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 .\" SUCH DAMAGE.
27 .\"
28 .Dd October 5, 2017
29 .Dt CUSE 3
30 .Os
31 .Sh NAME
32 .Nm libcuse
33 .Nd "Userland character device library"
34 .Sh LIBRARY
35 .Lb libcuse
36 .Sh SYNOPSIS
37 To load the required kernel module at boot time, place the following line in
38 .Xr loader.conf 5 :
39 .Bd -literal -offset indent
40 cuse_load="YES"
41 .Ed
42 .Pp
43 .In cuse.h
44 .Sh DESCRIPTION
45 The
46 .Nm
47 library contains functions to create a character device in userspace.
48 The
49 .Nm
50 library is thread safe.
51 .Sh LIBRARY INITIALISATION / DEINITIALISATION
52 .Ft "int"
53 .Fn "cuse_init" "void"
54 This function initialises
55 .Nm .
56 Must be called at the beginning of the program.
57 This function returns 0 on success or a negative value on failure.
58 See
59 .Dv CUSE_ERR_XXX
60 for known error codes.
61 If the cuse kernel module is not loaded,
62 .Dv CUSE_ERR_NOT_LOADED
63 is returned.
64 .Pp
65 .Ft "int"
66 .Fn "cuse_uninit" "void"
67 Deinitialise
68 .Nm .
69 Can be called at the end of the application.
70 This function returns 0 on success or a negative value on failure.
71 See
72 .Dv CUSE_ERR_XXX
73 for known error codes.
74 .Sh UNIT MANAGEMENT
75 .Ft "int"
76 .Fn "cuse_alloc_unit_number" "int *"
77 This function stores a uniq system unit number at the pointed
78 integer loation.
79 This function returns 0 on success or a negative value on failure.
80 See
81 .Dv CUSE_ERR_XXX
82 for known error codes.
83 .Pp
84 .Ft "int"
85 .Fn "cuse_alloc_unit_number_by_id" "int *" "int id"
86 This function stores a unique system unit number at the pointed
87 integer loation.
88 The returned unit number is uniq within the given ID.
89 Valid ID values are defined by the cuse include file.
90 See the
91 .Fn CUSE_ID_XXX
92 macros for more information.
93 This function returns 0 on success or a negative value on failure.
94 See
95 .Dv CUSE_ERR_XXX
96 for known error codes.
97 .Pp
98 .Ft "int"
99 .Fn "cuse_free_unit_number" "int"
100 This function frees the given allocated system unit number.
101 This function returns 0 on success or a negative value on failure.
102 See
103 .Dv CUSE_ERR_XXX
104 for known error codes.
105 .Pp
106 .Ft "int"
107 .Fn "cuse_free_unit_number_by_id" "int unit" "int id"
108 This function frees the given allocated system unit number belonging
109 to the given ID.
110 If both the unit and id argument is -1, all allocated units will be freed.
111 This function returns 0 on success or a negative value on failure.
112 See
113 .Dv CUSE_ERR_XXX
114 for known error codes.
115 .Sh LIBRARY USAGE
116 .Ft "void *"
117 .Fn "cuse_vmalloc" "int size"
118 This function allocates
119 .Ar size
120 bytes of memory.
121 Only memory allocated by this function can be memory
122 mapped by
123 .Xr mmap 2 .
124 This function returns a valid data pointer on success or
125 .Dv NULL
126 on failure.
127 .Pp
128 .Ft "int"
129 .Fn "cuse_is_vmalloc_addr" "void *"
130 This function returns non-zero if the passed pointer points to a valid
131 and non-freed allocation, as returned by
132 .Fn cuse_vmalloc .
133 Else this function returns zero.
134 .Pp
135 .Ft "void"
136 .Fn "cuse_vmfree" "void *"
137 This function frees memory allocated by
138 .Fn cuse_vmalloc .
139 Note that the
140 cuse library will internally not free the memory until the
141 .Fn cuse_uninit
142 function is called and that the number of unique
143 allocations is limited.
144 .Pp
145 .Ft "unsigned long"
146 .Fn "cuse_vmoffset" "void *"
147 This function returns the mmap offset that the client must use to
148 access the allocated memory.
149 .Pp
150 .Ft "struct cuse_dev *"
151 .Fn "cuse_dev_create" "const struct cuse_methods *mtod" "void *priv0" "void *priv1" "uid_t" "gid_t" "int permission" "const char *fmt" "..."
152 This function creates a new character device according to the given
153 parameters.
154 This function returns a valid cuse_dev structure pointer
155 on success or
156 .Dv NULL
157 on failure.
158 The device name can only contain a-z,
159 A-Z, 0-9, dot, / and underscore characters.
160 .Pp
161 .Ft "void"
162 .Fn "cuse_dev_destroy" "struct cuse_dev *"
163 This functions destroys a previously created character device.
164 .Pp
165 .Ft "void *"
166 .Fn "cuse_dev_get_priv0" "struct cuse_dev *" ,
167 .Ft "void *"
168 .Fn "cuse_dev_get_priv1" "struct cuse_dev *" ,
169 .Ft "void"
170 .Fn "cuse_dev_set_priv0" "struct cuse_dev *" "void *" ,
171 .Ft "void"
172 .Fn "cuse_dev_set_priv1" "struct cuse_dev *" "void *"
173 These functions are used to set and get the private data of the given
174 cuse device.
175 .Pp
176 .Ft "int"
177 .Fn "cuse_wait_and_process" "void"
178 This function will block and do event processing.
179 If parallel I/O is
180 required multiple threads must be created looping on this
181 function.
182 This function returns 0 on success or a negative value on failure.
183 See
184 .Dv CUSE_ERR_XXX
185 for known error codes.
186 .Pp
187 .Ft "void *"
188 .Fn "cuse_dev_get_per_file_handle" "struct cuse_dev *" ,
189 .Ft "void"
190 .Fn "cuse_dev_set_per_file_handle" "struct cuse_dev *" "void *"
191 These functions are used to set and get the per-file-open specific handle
192 and should only be used inside the cuse file operation callbacks.
193 .Pp
194 .Ft "void"
195 .Fn "cuse_set_local" "int"
196 This function instructs
197 .Fn cuse_copy_out
198 and
199 .Fn cuse_copy_in
200 that the
201 user pointer is local, if the argument passed to it is non-zero.
202 Else the user pointer is assumed to be at the peer application.
203 This function should only be used inside the cuse file operation callbacks.
204 The value is reset to zero when the given file operation returns, and
205 does not affect any other file operation callbacks.
206 .Pp
207 .Ft "int"
208 .Fn "cuse_get_local" "void"
209 Returns the current local state.
210 See
211 .Fn cuse_set_local .
212 .Pp
213 .Ft "int"
214 .Fn "cuse_copy_out" "const void *src" "void *peer_dst" "int len" ,
215 .Ft "int"
216 .Fn "cuse_copy_in" "const void *peer_src" "void *dst" "int len"
217 These functions are used to transfer data between the local
218 application and the peer application.
219 These functions must be used
220 when operating on the data pointers passed to the
221 .Fn cm_read ,
222 .Fn cm_write ,
223 and
224 .Fn cm_ioctl
225 callback functions.
226 These functions return 0 on success or a negative value on failure.
227 See
228 .Dv CUSE_ERR_XXX
229 for known error codes.
230 .Pp
231 .Ft "int"
232 .Fn "cuse_got_peer_signal" "void"
233 This function is used to check if a signal has been delivered to the
234 peer application and should only be used inside the cuse file
235 operation callbacks.
236 This function returns 0 if a signal has been
237 delivered to the caller.
238 Else it returns a negative value.
239 See
240 .Dv CUSE_ERR_XXX
241 for known error codes.
242 .Pp
243 .Ft "struct cuse_dev *"
244 .Fn "cuse_dev_get_current" "int *pcmd"
245 This function is used to get the current cuse device pointer and the
246 currently executing command, by
247 .Dv CUSE_CMD_XXX
248 value.
249 The
250 .Ar pcmd
251 argument
252 is allowed to be
253 .Dv NULL .
254 This function should only be used inside the
255 cuse file operation callbacks.
256 On success a valid cuse device pointer
257 is returned.
258 On failure
259 .Dv NULL
260 is returned.
261 .Pp
262 .Ft "void"
263 .Fn "cuse_poll_wakeup" "void"
264 This function will wake up any file pollers.
265 .Sh LIBRARY LIMITATIONS
266 Transfer lengths for
267 .Fn read ,
268 .Fn write ,
269 .Fn cuse_copy_in ,
270 and
271 .Fn cuse_copy_out
272 should not exceed what can fit into a 32-bit signed integer and is
273 defined by the
274 .Fn CUSE_LENGTH_MAX
275 macro.
276 Transfer lengths for ioctls should not exceed what is defined by the
277 .Fn CUSE_BUFFER_MAX
278 macro.
279 .Sh LIBRARY CALLBACK METHODS
280 In general fflags are defined by
281 .Dv CUSE_FFLAG_XXX
282 and errors are defined by
283 .Dv CUSE_ERR_XXX .
284 .Bd -literal -offset indent
285 enum {
286   CUSE_ERR_NONE
287   CUSE_ERR_BUSY
288   CUSE_ERR_WOULDBLOCK
289   CUSE_ERR_INVALID
290   CUSE_ERR_NO_MEMORY
291   CUSE_ERR_FAULT
292   CUSE_ERR_SIGNAL
293   CUSE_ERR_OTHER
294   CUSE_ERR_NOT_LOADED
295   CUSE_ERR_NO_DEVICE
296
297   CUSE_POLL_NONE
298   CUSE_POLL_READ
299   CUSE_POLL_WRITE
300   CUSE_POLL_ERROR
301
302   CUSE_FFLAG_NONE
303   CUSE_FFLAG_READ
304   CUSE_FFLAG_WRITE
305   CUSE_FFLAG_NONBLOCK
306
307   CUSE_CMD_NONE
308   CUSE_CMD_OPEN
309   CUSE_CMD_CLOSE
310   CUSE_CMD_READ
311   CUSE_CMD_WRITE
312   CUSE_CMD_IOCTL
313   CUSE_CMD_POLL
314   CUSE_CMD_SIGNAL
315   CUSE_CMD_SYNC
316   CUSE_CMD_MAX
317 };
318 .Ed
319 .Pp
320 .Ft "int"
321 .Fn "cuse_open_t" "struct cuse_dev *" "int fflags"
322 This function returns a
323 .Dv CUSE_ERR_XXX
324 value.
325 .Pp
326 .Ft "int"
327 .Fn "cuse_close_t" "struct cuse_dev *" "int fflags"
328 This function returns a
329 .Dv CUSE_ERR_XXX
330 value.
331 .Pp
332 .Ft "int"
333 .Fn "cuse_read_t" "struct cuse_dev *" "int fflags" "void *peer_ptr" "int len"
334 This function returns a
335 .Dv CUSE_ERR_XXX
336 value in case of failure or the
337 actually transferred length in case of success.
338 .Fn cuse_copy_in
339 and
340 .Fn cuse_copy_out
341 must be used to transfer data to and from the
342 .Ar peer_ptr .
343 .Pp
344 .Ft "int"
345 .Fn "cuse_write_t" "struct cuse_dev *" "int fflags" "const void *peer_ptr" "int len"
346 This function returns a
347 .Dv CUSE_ERR_XXX
348 value in case of failure or the
349 actually transferred length in case of success.
350 .Fn cuse_copy_in
351 and
352 .Fn cuse_copy_out
353 must be used to transfer data to and from the
354 .Ar peer_ptr .
355 .Pp
356 .Ft "int"
357 .Fn "cuse_ioctl_t" "struct cuse_dev *" "int fflags" "unsigned long cmd" "void *peer_data"
358 This function returns a
359 .Dv CUSE_ERR_XXX
360 value in case of failure or zero
361 in case of success.
362 .Fn cuse_copy_in
363 and
364 .Fn cuse_copy_out
365 must be used to
366 transfer data to and from the
367 .Ar peer_data .
368 .Pp
369 .Ft "int"
370 .Fn "cuse_poll_t" "struct cuse_dev *" "int fflags" "int events"
371 This function returns a mask of
372 .Dv CUSE_POLL_XXX
373 values in case of failure and success.
374 The events argument is also a mask of
375 .Dv CUSE_POLL_XXX
376 values.
377 .Bd -literal -offset indent
378 struct cuse_methods {
379   cuse_open_t *cm_open;
380   cuse_close_t *cm_close;
381   cuse_read_t *cm_read;
382   cuse_write_t *cm_write;
383   cuse_ioctl_t *cm_ioctl;
384   cuse_poll_t *cm_poll;
385 };
386 .Ed
387 .Sh HISTORY
388 .Nm
389 was written by Hans Petter Selasky.