]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - share/man/man9/sbuf.9
Import ee 1.5.2 into HEAD.
[FreeBSD/FreeBSD.git] / share / man / man9 / sbuf.9
1 .\"-
2 .\" Copyright (c) 2000 Poul-Henning Kamp and Dag-Erling Coïdan Smørgrav
3 .\" All rights reserved.
4 .\"
5 .\" Redistribution and use in source and binary forms, with or without
6 .\" modification, are permitted provided that the following conditions
7 .\" are met:
8 .\" 1. Redistributions of source code must retain the above copyright
9 .\"    notice, this list of conditions and the following disclaimer.
10 .\" 2. Redistributions in binary form must reproduce the above copyright
11 .\"    notice, this list of conditions and the following disclaimer in the
12 .\"    documentation and/or other materials provided with the distribution.
13 .\"
14 .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 .\" SUCH DAMAGE.
25 .\"
26 .\" $FreeBSD$
27 .\"
28 .Dd May 17, 2009
29 .Dt SBUF 9
30 .Os
31 .Sh NAME
32 .Nm sbuf ,
33 .Nm sbuf_new ,
34 .Nm sbuf_new_auto ,
35 .Nm sbuf_clear ,
36 .Nm sbuf_setpos ,
37 .Nm sbuf_bcat ,
38 .Nm sbuf_bcopyin ,
39 .Nm sbuf_bcpy ,
40 .Nm sbuf_cat ,
41 .Nm sbuf_copyin ,
42 .Nm sbuf_cpy ,
43 .Nm sbuf_printf ,
44 .Nm sbuf_vprintf ,
45 .Nm sbuf_putc ,
46 .Nm sbuf_set_drain ,
47 .Nm sbuf_trim ,
48 .Nm sbuf_error ,
49 .Nm sbuf_finish ,
50 .Nm sbuf_data ,
51 .Nm sbuf_len ,
52 .Nm sbuf_done ,
53 .Nm sbuf_delete
54 .Nd safe string formatting
55 .Sh SYNOPSIS
56 .In sys/types.h
57 .In sys/sbuf.h
58 .Ft typedef\ int ( sbuf_drain_func ) ( void\ *arg, const\ char\ *data, int\ len ) ;
59 .Pp
60 .Ft struct sbuf *
61 .Fn sbuf_new "struct sbuf *s" "char *buf" "int length" "int flags"
62 .Ft struct sbuf *
63 .Fn sbuf_new_auto
64 .Ft void
65 .Fn sbuf_clear "struct sbuf *s"
66 .Ft int
67 .Fn sbuf_setpos "struct sbuf *s" "int pos"
68 .Ft int
69 .Fn sbuf_bcat "struct sbuf *s" "const void *buf" "size_t len"
70 .Ft int
71 .Fn sbuf_bcopyin "struct sbuf *s" "const void *uaddr" "size_t len"
72 .Ft int
73 .Fn sbuf_bcpy "struct sbuf *s" "const void *buf" "size_t len"
74 .Ft int
75 .Fn sbuf_cat "struct sbuf *s" "const char *str"
76 .Ft int
77 .Fn sbuf_copyin "struct sbuf *s" "const void *uaddr" "size_t len"
78 .Ft int
79 .Fn sbuf_cpy "struct sbuf *s" "const char *str"
80 .Ft int
81 .Fn sbuf_printf "struct sbuf *s" "const char *fmt" "..."
82 .Ft int
83 .Fn sbuf_vprintf "struct sbuf *s" "const char *fmt" "va_list ap"
84 .Ft int
85 .Fn sbuf_putc "struct sbuf *s" "int c"
86 .Ft void
87 .Fn sbuf_set_drain "struct sbuf *s" "sbuf_drain_func *func" "void *arg"
88 .Ft int
89 .Fn sbuf_trim "struct sbuf *s"
90 .Ft int
91 .Fn sbuf_error "struct sbuf *s"
92 .Ft int
93 .Fn sbuf_finish "struct sbuf *s"
94 .Ft char *
95 .Fn sbuf_data "struct sbuf *s"
96 .Ft int
97 .Fn sbuf_len "struct sbuf *s"
98 .Ft int
99 .Fn sbuf_done "struct sbuf *s"
100 .Ft void
101 .Fn sbuf_delete "struct sbuf *s"
102 .Sh DESCRIPTION
103 The
104 .Nm
105 family of functions allows one to safely allocate, construct and
106 release bounded NUL-terminated strings in kernel space.
107 Instead of arrays of characters, these functions operate on structures
108 called
109 .Fa sbufs ,
110 defined in
111 .In sys/sbuf.h .
112 .Pp
113 The
114 .Fn sbuf_new
115 function initializes the
116 .Fa sbuf
117 pointed to by its first argument.
118 If that pointer is
119 .Dv NULL ,
120 .Fn sbuf_new
121 allocates a
122 .Vt struct sbuf
123 using
124 .Xr malloc 9 .
125 The
126 .Fa buf
127 argument is a pointer to a buffer in which to store the actual string;
128 if it is
129 .Dv NULL ,
130 .Fn sbuf_new
131 will allocate one using
132 .Xr malloc 9 .
133 The
134 .Fa length
135 is the initial size of the storage buffer.
136 The fourth argument,
137 .Fa flags ,
138 may be comprised of the following flags:
139 .Bl -tag -width ".Dv SBUF_AUTOEXTEND"
140 .It Dv SBUF_FIXEDLEN
141 The storage buffer is fixed at its initial size.
142 Attempting to extend the sbuf beyond this size results in an overflow condition.
143 .It Dv SBUF_AUTOEXTEND
144 This indicates that the storage buffer may be extended as necessary, so long
145 as resources allow, to hold additional data.
146 .El
147 .Pp
148 Note that if
149 .Fa buf
150 is not
151 .Dv NULL ,
152 it must point to an array of at least
153 .Fa length
154 characters.
155 The result of accessing that array directly while it is in use by the
156 sbuf is undefined.
157 .Pp
158 The
159 .Fn sbuf_new_auto
160 function is a shortcut for creating a completely dynamic
161 .Nm .
162 It is the equivalent of calling
163 .Fn sbuf_new
164 with values
165 .Dv NULL ,
166 .Dv NULL ,
167 .Dv 0 ,
168 and
169 .Dv SBUF_AUTOEXTEND .
170 .Pp
171 The
172 .Fn sbuf_delete
173 function clears the
174 .Fa sbuf
175 and frees any memory allocated for it.
176 There must be a call to
177 .Fn sbuf_delete
178 for every call to
179 .Fn sbuf_new .
180 Any attempt to access the sbuf after it has been deleted will fail.
181 .Pp
182 The
183 .Fn sbuf_clear
184 function invalidates the contents of the
185 .Fa sbuf
186 and resets its position to zero.
187 .Pp
188 The
189 .Fn sbuf_setpos
190 function sets the
191 .Fa sbuf Ns 's
192 end position to
193 .Fa pos ,
194 which is a value between zero and one less than the size of the
195 storage buffer.
196 This effectively truncates the sbuf at the new position.
197 .Pp
198 The
199 .Fn sbuf_bcat
200 function appends the first
201 .Fa len
202 bytes from the buffer
203 .Fa buf
204 to the
205 .Fa sbuf .
206 .Pp
207 The
208 .Fn sbuf_bcopyin
209 function copies
210 .Fa len
211 bytes from the specified userland address into the
212 .Fa sbuf .
213 .Pp
214 The
215 .Fn sbuf_bcpy
216 function replaces the contents of the
217 .Fa sbuf
218 with the first
219 .Fa len
220 bytes from the buffer
221 .Fa buf .
222 .Pp
223 The
224 .Fn sbuf_cat
225 function appends the NUL-terminated string
226 .Fa str
227 to the
228 .Fa sbuf
229 at the current position.
230 .Pp
231 The
232 .Fn sbuf_set_drain
233 function sets a drain function
234 .Fa func
235 for the
236 .Fa sbuf ,
237 and records a pointer
238 .Fa arg
239 to be passed to the drain on callback.
240 The drain function cannot be changed while
241 .Fa sbuf_len
242 is non-zero.
243 .Pp
244 The registered drain function
245 .Vt sbuf_drain_func
246 will be called with the argument
247 .Fa arg
248 provided to
249 .Fn sbuf_set_drain ,
250 a pointer
251 .Fa data
252 to a byte string that is the contents of the sbuf, and the length
253 .Fa len
254 of the data.
255 If the drain function exists, it will be called when the sbuf internal
256 buffer is full, or on behalf of
257 .Fn sbuf_finish .
258 The drain function may drain some or all of the data, but must drain
259 at least 1 byte.
260 The return value from the drain function, if positive, indicates how
261 many bytes were drained.
262 If negative, the return value indicates the negative error code which
263 will be returned from this or a later call to
264 .Fn sbuf_finish .
265 The returned drained length cannot be zero.
266 To do unbuffered draining, initialize the sbuf with a two-byte buffer.
267 The drain will be called for every byte added to the sbuf.
268 The
269 .Fn sbuf_bcopyin ,
270 .Fn sbuf_copyin ,
271 .Fn sbuf_trim ,
272 and
273 .Fn sbuf_data
274 functions cannot be used on an sbuf with a drain.
275 .Pp
276 The
277 .Fn sbuf_copyin
278 function copies a NUL-terminated string from the specified userland
279 address into the
280 .Fa sbuf .
281 If the
282 .Fa len
283 argument is non-zero, no more than
284 .Fa len
285 characters (not counting the terminating NUL) are copied; otherwise
286 the entire string, or as much of it as can fit in the
287 .Fa sbuf ,
288 is copied.
289 .Pp
290 The
291 .Fn sbuf_cpy
292 function replaces the contents of the
293 .Fa sbuf
294 with those of the NUL-terminated string
295 .Fa str .
296 This is equivalent to calling
297 .Fn sbuf_cat
298 with a fresh
299 .Fa sbuf
300 or one which position has been reset to zero with
301 .Fn sbuf_clear
302 or
303 .Fn sbuf_setpos .
304 .Pp
305 The
306 .Fn sbuf_printf
307 function formats its arguments according to the format string pointed
308 to by
309 .Fa fmt
310 and appends the resulting string to the
311 .Fa sbuf
312 at the current position.
313 .Pp
314 The
315 .Fn sbuf_vprintf
316 function behaves the same as
317 .Fn sbuf_printf
318 except that the arguments are obtained from the variable-length argument list
319 .Fa ap .
320 .Pp
321 The
322 .Fn sbuf_putc
323 function appends the character
324 .Fa c
325 to the
326 .Fa sbuf
327 at the current position.
328 .Pp
329 The
330 .Fn sbuf_trim
331 function removes trailing whitespace from the
332 .Fa sbuf .
333 .Pp
334 The
335 .Fn sbuf_error
336 function returns any error value that the
337 .Fa sbuf
338 may have accumulated, either from the drain function, or ENOMEM if the
339 .Fa sbuf
340 overflowed.
341 This function is generally not needed and instead the error code from
342 .Fn sbuf_finish
343 is the preferred way to discover whether an sbuf had an error.
344 .Pp
345 The
346 .Fn sbuf_finish
347 function will call the attached drain function if one exists until all
348 the data in the
349 .Fa sbuf
350 is flushed.
351 If there is no attached drain,
352 .Fn sbuf_finish
353 NUL-terminates the
354 .Fa sbuf .
355 In either case it marks the
356 .Fa sbuf
357 as finished, which means that it may no longer be modified using
358 .Fn sbuf_setpos ,
359 .Fn sbuf_cat ,
360 .Fn sbuf_cpy ,
361 .Fn sbuf_printf
362 or
363 .Fn sbuf_putc ,
364 until
365 .Fn sbuf_clear
366 is used to reset the sbuf.
367 .Pp
368 The
369 .Fn sbuf_data
370 function returns the actual string;
371 .Fn sbuf_data
372 only works on a finished
373 .Fa sbuf .
374 The
375 .Fn sbuf_len function returns the length of the string.
376 For an
377 .Fa sbuf
378 with an attached drain,
379 .Fn sbuf_len
380 returns the length of the un-drained data.
381 .Fn sbuf_done
382 returns non-zero if the
383 .Fa sbuf
384 is finished.
385 .Fn sbuf_done
386 returns non-zero if the
387 .Fa sbuf
388 is finished.
389 .Sh NOTES
390 If an operation caused an
391 .Fa sbuf
392 to overflow, most subsequent operations on it will fail until the
393 .Fa sbuf
394 is finished using
395 .Fn sbuf_finish
396 or reset using
397 .Fn sbuf_clear ,
398 or its position is reset to a value between 0 and one less than the
399 size of its storage buffer using
400 .Fn sbuf_setpos ,
401 or it is reinitialized to a sufficiently short string using
402 .Fn sbuf_cpy .
403 .Pp
404 Drains in user-space will not always function as indicated.
405 While the drain function will be called immediately on overflow from
406 the
407 .Fa sbuf_putc ,
408 .Fa sbuf_bcat ,
409 .Fa sbuf_cat
410 functions,
411 .Fa sbuf_printf
412 and
413 .Fa sbuf_vprintf
414 currently have no way to determine whether there will be an overflow
415 until after it occurs, and cannot do a partial expansion of the format
416 string.
417 Thus when using libsbuf the buffer may be extended to allow completion
418 of a single printf call, even though a drain is attached.
419 .Sh RETURN VALUES
420 The
421 .Fn sbuf_new
422 function returns
423 .Dv NULL
424 if it failed to allocate a storage buffer, and a pointer to the new
425 .Fa sbuf
426 otherwise.
427 .Pp
428 The
429 .Fn sbuf_setpos
430 function returns \-1 if
431 .Fa pos
432 was invalid, and zero otherwise.
433 .Pp
434 The
435 .Fn sbuf_cat ,
436 .Fn sbuf_cpy ,
437 .Fn sbuf_printf ,
438 .Fn sbuf_putc ,
439 and
440 .Fn sbuf_trim
441 functions
442 all return \-1 if the buffer overflowed, and zero otherwise.
443 .Pp
444 The
445 .Fn sbuf_error
446 function returns a non-zero value if the buffer has an overflow or
447 drain error, and zero otherwise.
448 .Pp
449 The
450 .Fn sbuf_data
451 and
452 .Fn sbuf_len
453 functions return
454 .Dv NULL
455 and \-1, respectively, if the buffer overflowed.
456 .Pp
457 The
458 .Fn sbuf_copyin
459 function
460 returns \-1 if copying string from userland failed, and number of bytes
461 copied otherwise.
462 The
463 .Fn sbuf_finish
464 function returns ENOMEM if the sbuf overflowed before being finished,
465 or returns the error code from the drain if one is attached.
466 When used as
467 .Xr sbuf_finish 3 ,
468 .Fn sbuf_finish
469 will return \-1 and set errno on error instead.
470 .Sh SEE ALSO
471 .Xr printf 3 ,
472 .Xr strcat 3 ,
473 .Xr strcpy 3 ,
474 .Xr copyin 9 ,
475 .Xr copyinstr 9 ,
476 .Xr printf 9
477 .Sh HISTORY
478 The
479 .Nm
480 family of functions first appeared in
481 .Fx 4.4 .
482 .Sh AUTHORS
483 .An -nosplit
484 The
485 .Nm
486 family of functions was designed by
487 .An Poul-Henning Kamp Aq phk@FreeBSD.org
488 and implemented by
489 .An Dag-Erling Sm\(/orgrav Aq des@FreeBSD.org .
490 Additional improvements were suggested by
491 .An Justin T. Gibbs Aq gibbs@FreeBSD.org .
492 Auto-extend support added by
493 .An Kelly Yancey Aq kbyanc@FreeBSD.org .
494 Drain functionality added by
495 .An Matthew Fleming Aq mdf@FreeBSD.org .
496 .Pp
497 This manual page was written by
498 .An Dag-Erling Sm\(/orgrav Aq des@FreeBSD.org .