]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - share/man/man9/sbuf.9
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.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 April 11, 2013
29 .Dt SBUF 9
30 .Os
31 .Sh NAME
32 .Nm sbuf ,
33 .Nm sbuf_new ,
34 .Nm sbuf_new_auto ,
35 .Nm sbuf_new_for_sysctl ,
36 .Nm sbuf_clear ,
37 .Nm sbuf_setpos ,
38 .Nm sbuf_bcat ,
39 .Nm sbuf_bcopyin ,
40 .Nm sbuf_bcpy ,
41 .Nm sbuf_cat ,
42 .Nm sbuf_copyin ,
43 .Nm sbuf_cpy ,
44 .Nm sbuf_printf ,
45 .Nm sbuf_vprintf ,
46 .Nm sbuf_putc ,
47 .Nm sbuf_set_drain ,
48 .Nm sbuf_trim ,
49 .Nm sbuf_error ,
50 .Nm sbuf_finish ,
51 .Nm sbuf_data ,
52 .Nm sbuf_len ,
53 .Nm sbuf_done ,
54 .Nm sbuf_delete ,
55 .Nm sbuf_start_section ,
56 .Nm sbuf_end_section ,
57 .Nm sbuf_hexdump
58 .Nd safe string composition
59 .Sh SYNOPSIS
60 .In sys/types.h
61 .In sys/sbuf.h
62 .Ft typedef\ int ( sbuf_drain_func ) ( void\ *arg, const\ char\ *data, int\ len ) ;
63 .Pp
64 .Ft struct sbuf *
65 .Fn sbuf_new "struct sbuf *s" "char *buf" "int length" "int flags"
66 .Ft struct sbuf *
67 .Fn sbuf_new_auto
68 .Ft void
69 .Fn sbuf_clear "struct sbuf *s"
70 .Ft int
71 .Fn sbuf_setpos "struct sbuf *s" "int pos"
72 .Ft int
73 .Fn sbuf_bcat "struct sbuf *s" "const void *buf" "size_t len"
74 .Ft int
75 .Fn sbuf_bcopyin "struct sbuf *s" "const void *uaddr" "size_t len"
76 .Ft int
77 .Fn sbuf_bcpy "struct sbuf *s" "const void *buf" "size_t len"
78 .Ft int
79 .Fn sbuf_cat "struct sbuf *s" "const char *str"
80 .Ft int
81 .Fn sbuf_copyin "struct sbuf *s" "const void *uaddr" "size_t len"
82 .Ft int
83 .Fn sbuf_cpy "struct sbuf *s" "const char *str"
84 .Ft int
85 .Fn sbuf_printf "struct sbuf *s" "const char *fmt" "..."
86 .Ft int
87 .Fn sbuf_vprintf "struct sbuf *s" "const char *fmt" "va_list ap"
88 .Ft int
89 .Fn sbuf_putc "struct sbuf *s" "int c"
90 .Ft void
91 .Fn sbuf_set_drain "struct sbuf *s" "sbuf_drain_func *func" "void *arg"
92 .Ft int
93 .Fn sbuf_trim "struct sbuf *s"
94 .Ft int
95 .Fn sbuf_error "struct sbuf *s"
96 .Ft int
97 .Fn sbuf_finish "struct sbuf *s"
98 .Ft char *
99 .Fn sbuf_data "struct sbuf *s"
100 .Ft ssize_t
101 .Fn sbuf_len "struct sbuf *s"
102 .Ft int
103 .Fn sbuf_done "struct sbuf *s"
104 .Ft void
105 .Fn sbuf_delete "struct sbuf *s"
106 .Ft void
107 .Fn sbuf_start_section "struct sbuf *s" "ssize_t *old_lenp"
108 .Ft ssize_t
109 .Fn sbuf_end_section "struct sbuf *s" "ssize_t old_len" "size_t pad" "int c"
110 .Ft void
111 .Fo sbuf_hexdump 
112 .Fa "struct sbuf *sb"
113 .Fa "void *ptr"
114 .Fa "int length"
115 .Fa "const char *hdr"
116 .Fa "int flags"
117 .Fc
118 .In sys/sysctl.h
119 .Ft struct sbuf *
120 .Fn sbuf_new_for_sysctl "struct sbuf *s" "char *buf" "int length" "struct sysctl_req *req"
121 .Sh DESCRIPTION
122 The
123 .Nm
124 family of functions allows one to safely allocate, compose and
125 release strings in kernel or user space.
126 .Pp
127 Instead of arrays of characters, these functions operate on structures
128 called
129 .Fa sbufs ,
130 defined in
131 .In sys/sbuf.h .
132 .Pp
133 Any errors encountered during the allocation or composition of the
134 string will be latched in the data structure,
135 making a single error test at the end of the composition
136 sufficient to determine success or failure of the entire process.
137 .Pp
138 The
139 .Fn sbuf_new
140 function initializes the
141 .Fa sbuf
142 pointed to by its first argument.
143 If that pointer is
144 .Dv NULL ,
145 .Fn sbuf_new
146 allocates a
147 .Vt struct sbuf
148 using
149 .Xr malloc 9 .
150 The
151 .Fa buf
152 argument is a pointer to a buffer in which to store the actual string;
153 if it is
154 .Dv NULL ,
155 .Fn sbuf_new
156 will allocate one using
157 .Xr malloc 9 .
158 The
159 .Fa length
160 is the initial size of the storage buffer.
161 The fourth argument,
162 .Fa flags ,
163 may be comprised of the following flags:
164 .Bl -tag -width ".Dv SBUF_AUTOEXTEND"
165 .It Dv SBUF_FIXEDLEN
166 The storage buffer is fixed at its initial size.
167 Attempting to extend the sbuf beyond this size results in an overflow condition.
168 .It Dv SBUF_AUTOEXTEND
169 This indicates that the storage buffer may be extended as necessary, so long
170 as resources allow, to hold additional data.
171 .El
172 .Pp
173 Note that if
174 .Fa buf
175 is not
176 .Dv NULL ,
177 it must point to an array of at least
178 .Fa length
179 characters.
180 The result of accessing that array directly while it is in use by the
181 sbuf is undefined.
182 .Pp
183 The
184 .Fn sbuf_new_auto
185 function is a shortcut for creating a completely dynamic
186 .Nm .
187 It is the equivalent of calling
188 .Fn sbuf_new
189 with values
190 .Dv NULL ,
191 .Dv NULL ,
192 .Dv 0 ,
193 and
194 .Dv SBUF_AUTOEXTEND .
195 .Pp
196 The
197 .Fn sbuf_new_for_sysctl
198 function will set up an sbuf with a drain function to use
199 .Fn SYSCTL_OUT
200 when the internal buffer fills.
201 Note that if the various functions which append to an sbuf are used while
202 a non-sleepable lock is held, the user buffer should be wired using
203 .Fn sysctl_wire_old_buffer .
204 .Pp
205 The
206 .Fn sbuf_delete
207 function clears the
208 .Fa sbuf
209 and frees any memory allocated for it.
210 There must be a call to
211 .Fn sbuf_delete
212 for every call to
213 .Fn sbuf_new .
214 Any attempt to access the sbuf after it has been deleted will fail.
215 .Pp
216 The
217 .Fn sbuf_clear
218 function invalidates the contents of the
219 .Fa sbuf
220 and resets its position to zero.
221 .Pp
222 The
223 .Fn sbuf_setpos
224 function sets the
225 .Fa sbuf Ns 's
226 end position to
227 .Fa pos ,
228 which is a value between zero and one less than the size of the
229 storage buffer.
230 This effectively truncates the sbuf at the new position.
231 .Pp
232 The
233 .Fn sbuf_bcat
234 function appends the first
235 .Fa len
236 bytes from the buffer
237 .Fa buf
238 to the
239 .Fa sbuf .
240 .Pp
241 The
242 .Fn sbuf_bcopyin
243 function copies
244 .Fa len
245 bytes from the specified userland address into the
246 .Fa sbuf .
247 .Pp
248 The
249 .Fn sbuf_bcpy
250 function replaces the contents of the
251 .Fa sbuf
252 with the first
253 .Fa len
254 bytes from the buffer
255 .Fa buf .
256 .Pp
257 The
258 .Fn sbuf_cat
259 function appends the NUL-terminated string
260 .Fa str
261 to the
262 .Fa sbuf
263 at the current position.
264 .Pp
265 The
266 .Fn sbuf_set_drain
267 function sets a drain function
268 .Fa func
269 for the
270 .Fa sbuf ,
271 and records a pointer
272 .Fa arg
273 to be passed to the drain on callback.
274 The drain function cannot be changed while
275 .Fa sbuf_len
276 is non-zero.
277 .Pp
278 The registered drain function
279 .Vt sbuf_drain_func
280 will be called with the argument
281 .Fa arg
282 provided to
283 .Fn sbuf_set_drain ,
284 a pointer
285 .Fa data
286 to a byte string that is the contents of the sbuf, and the length
287 .Fa len
288 of the data.
289 If the drain function exists, it will be called when the sbuf internal
290 buffer is full, or on behalf of
291 .Fn sbuf_finish .
292 The drain function may drain some or all of the data, but must drain
293 at least 1 byte.
294 The return value from the drain function, if positive, indicates how
295 many bytes were drained.
296 If negative, the return value indicates the negative error code which
297 will be returned from this or a later call to
298 .Fn sbuf_finish .
299 The returned drained length cannot be zero.
300 To do unbuffered draining, initialize the sbuf with a two-byte buffer.
301 The drain will be called for every byte added to the sbuf.
302 The
303 .Fn sbuf_bcopyin ,
304 .Fn sbuf_copyin ,
305 .Fn sbuf_trim ,
306 and
307 .Fn sbuf_data
308 functions cannot be used on an sbuf with a drain.
309 .Pp
310 The
311 .Fn sbuf_copyin
312 function copies a NUL-terminated string from the specified userland
313 address into the
314 .Fa sbuf .
315 If the
316 .Fa len
317 argument is non-zero, no more than
318 .Fa len
319 characters (not counting the terminating NUL) are copied; otherwise
320 the entire string, or as much of it as can fit in the
321 .Fa sbuf ,
322 is copied.
323 .Pp
324 The
325 .Fn sbuf_cpy
326 function replaces the contents of the
327 .Fa sbuf
328 with those of the NUL-terminated string
329 .Fa str .
330 This is equivalent to calling
331 .Fn sbuf_cat
332 with a fresh
333 .Fa sbuf
334 or one which position has been reset to zero with
335 .Fn sbuf_clear
336 or
337 .Fn sbuf_setpos .
338 .Pp
339 The
340 .Fn sbuf_printf
341 function formats its arguments according to the format string pointed
342 to by
343 .Fa fmt
344 and appends the resulting string to the
345 .Fa sbuf
346 at the current position.
347 .Pp
348 The
349 .Fn sbuf_vprintf
350 function behaves the same as
351 .Fn sbuf_printf
352 except that the arguments are obtained from the variable-length argument list
353 .Fa ap .
354 .Pp
355 The
356 .Fn sbuf_putc
357 function appends the character
358 .Fa c
359 to the
360 .Fa sbuf
361 at the current position.
362 .Pp
363 The
364 .Fn sbuf_trim
365 function removes trailing whitespace from the
366 .Fa sbuf .
367 .Pp
368 The
369 .Fn sbuf_error
370 function returns any error value that the
371 .Fa sbuf
372 may have accumulated, either from the drain function, or ENOMEM if the
373 .Fa sbuf
374 overflowed.
375 This function is generally not needed and instead the error code from
376 .Fn sbuf_finish
377 is the preferred way to discover whether an sbuf had an error.
378 .Pp
379 The
380 .Fn sbuf_finish
381 function will call the attached drain function if one exists until all
382 the data in the
383 .Fa sbuf
384 is flushed.
385 If there is no attached drain,
386 .Fn sbuf_finish
387 NUL-terminates the
388 .Fa sbuf .
389 In either case it marks the
390 .Fa sbuf
391 as finished, which means that it may no longer be modified using
392 .Fn sbuf_setpos ,
393 .Fn sbuf_cat ,
394 .Fn sbuf_cpy ,
395 .Fn sbuf_printf
396 or
397 .Fn sbuf_putc ,
398 until
399 .Fn sbuf_clear
400 is used to reset the sbuf.
401 .Pp
402 The
403 .Fn sbuf_data
404 function returns the actual string;
405 .Fn sbuf_data
406 only works on a finished
407 .Fa sbuf .
408 The
409 .Fn sbuf_len
410 function returns the length of the string.
411 For an
412 .Fa sbuf
413 with an attached drain,
414 .Fn sbuf_len
415 returns the length of the un-drained data.
416 .Fn sbuf_done
417 returns non-zero if the
418 .Fa sbuf
419 is finished.
420 .Pp
421 The
422 .Fn sbuf_start_section
423 and
424 .Fn sbuf_end_section
425 functions may be used for automatic section alignment.
426 The arguments
427 .Fa pad
428 and
429 .Fa c
430 specify the padding size and a character used for padding.
431 The arguments
432 .Fa old_lenp
433 and
434 .Fa old_len
435 are to save and restore the current section length when nested sections
436 are used.
437 For the top level section
438 .Dv NULL
439 and \-1 can be specified for
440 .Fa old_lenp
441 and
442 .Fa old_len
443 respectively.
444 .Pp
445 The
446 .Fn sbuf_hexdump
447 function prints an array of bytes to the supplied sbuf, along with an ASCII
448 representation of the bytes if possible.
449 See the
450 .Xr hexdump 3
451 man page for more details on the interface.
452 .Sh NOTES
453 If an operation caused an
454 .Fa sbuf
455 to overflow, most subsequent operations on it will fail until the
456 .Fa sbuf
457 is finished using
458 .Fn sbuf_finish
459 or reset using
460 .Fn sbuf_clear ,
461 or its position is reset to a value between 0 and one less than the
462 size of its storage buffer using
463 .Fn sbuf_setpos ,
464 or it is reinitialized to a sufficiently short string using
465 .Fn sbuf_cpy .
466 .Pp
467 Drains in user-space will not always function as indicated.
468 While the drain function will be called immediately on overflow from
469 the
470 .Fa sbuf_putc ,
471 .Fa sbuf_bcat ,
472 .Fa sbuf_cat
473 functions,
474 .Fa sbuf_printf
475 and
476 .Fa sbuf_vprintf
477 currently have no way to determine whether there will be an overflow
478 until after it occurs, and cannot do a partial expansion of the format
479 string.
480 Thus when using libsbuf the buffer may be extended to allow completion
481 of a single printf call, even though a drain is attached.
482 .Sh RETURN VALUES
483 The
484 .Fn sbuf_new
485 function returns
486 .Dv NULL
487 if it failed to allocate a storage buffer, and a pointer to the new
488 .Fa sbuf
489 otherwise.
490 .Pp
491 The
492 .Fn sbuf_setpos
493 function returns \-1 if
494 .Fa pos
495 was invalid, and zero otherwise.
496 .Pp
497 The
498 .Fn sbuf_cat ,
499 .Fn sbuf_cpy ,
500 .Fn sbuf_printf ,
501 .Fn sbuf_putc ,
502 and
503 .Fn sbuf_trim
504 functions
505 all return \-1 if the buffer overflowed, and zero otherwise.
506 .Pp
507 The
508 .Fn sbuf_error
509 function returns a non-zero value if the buffer has an overflow or
510 drain error, and zero otherwise.
511 .Pp
512 The
513 .Fn sbuf_len
514 function returns \-1 if the buffer overflowed.
515 .Pp
516 The
517 .Fn sbuf_copyin
518 function
519 returns \-1 if copying string from userland failed, and number of bytes
520 copied otherwise.
521 .Pp
522 The
523 .Fn sbuf_end_section
524 function returns the section length or \-1 if the buffer has an error.
525 .Pp
526 The
527 .Fn sbuf_finish 9
528 function (the kernel version) returns ENOMEM if the sbuf overflowed before
529 being finished,
530 or returns the error code from the drain if one is attached.
531 .Pp
532 The
533 .Fn sbuf_finish 3
534 function (the userland version)
535 will return zero for success and \-1 and set errno on error.
536 .Sh EXAMPLES
537 .Bd -literal -compact
538 #include <sys/sbuf.h>
539
540 struct sbuf *sb;
541
542 sb = sbuf_new_auto();
543 sbuf_cat(sb, "Customers found:\en");
544 TAILQ_FOREACH(foo, &foolist, list) {
545         sbuf_printf(sb, "   %4d %s\en", foo->index, foo->name);
546         sbuf_printf(sb, "      Address: %s\en", foo->address);
547         sbuf_printf(sb, "      Zip: %s\en", foo->zipcode);
548 }
549 if (sbuf_finish(sb) != 0) /* Check for any and all errors */
550         err(1, "Could not generate message");
551 transmit_msg(sbuf_data(sb), sbuf_len(sb));
552 sbuf_delete(sb);
553 .Ed
554 .Sh SEE ALSO
555 .Xr hexdump 3 ,
556 .Xr printf 3 ,
557 .Xr strcat 3 ,
558 .Xr strcpy 3 ,
559 .Xr copyin 9 ,
560 .Xr copyinstr 9 ,
561 .Xr printf 9
562 .Sh HISTORY
563 The
564 .Nm
565 family of functions first appeared in
566 .Fx 4.4 .
567 .Sh AUTHORS
568 .An -nosplit
569 The
570 .Nm
571 family of functions was designed by
572 .An Poul-Henning Kamp Aq phk@FreeBSD.org
573 and implemented by
574 .An Dag-Erling Sm\(/orgrav Aq des@FreeBSD.org .
575 Additional improvements were suggested by
576 .An Justin T. Gibbs Aq gibbs@FreeBSD.org .
577 Auto-extend support added by
578 .An Kelly Yancey Aq kbyanc@FreeBSD.org .
579 Drain functionality added by
580 .An Matthew Fleming Aq mdf@FreeBSD.org .
581 .Pp
582 This manual page was written by
583 .An Dag-Erling Sm\(/orgrav Aq des@FreeBSD.org .