2 .\" Copyright (c) 2000 Poul-Henning Kamp and Dag-Erling Coïdan Smørgrav
3 .\" All rights reserved.
5 .\" Redistribution and use in source and binary forms, with or without
6 .\" modification, are permitted provided that the following conditions
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.
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
35 .Nm sbuf_new_for_sysctl ,
55 .Nm sbuf_start_section ,
56 .Nm sbuf_end_section ,
59 .Nd safe string composition
63 .Ft typedef\ int ( sbuf_drain_func ) ( void\ *arg, const\ char\ *data, int\ len ) ;
66 .Fn sbuf_new "struct sbuf *s" "char *buf" "int length" "int flags"
70 .Fn sbuf_clear "struct sbuf *s"
72 .Fn sbuf_setpos "struct sbuf *s" "int pos"
74 .Fn sbuf_bcat "struct sbuf *s" "const void *buf" "size_t len"
76 .Fn sbuf_bcopyin "struct sbuf *s" "const void *uaddr" "size_t len"
78 .Fn sbuf_bcpy "struct sbuf *s" "const void *buf" "size_t len"
80 .Fn sbuf_cat "struct sbuf *s" "const char *str"
82 .Fn sbuf_copyin "struct sbuf *s" "const void *uaddr" "size_t len"
84 .Fn sbuf_cpy "struct sbuf *s" "const char *str"
86 .Fn sbuf_printf "struct sbuf *s" "const char *fmt" "..."
88 .Fn sbuf_vprintf "struct sbuf *s" "const char *fmt" "va_list ap"
90 .Fn sbuf_putc "struct sbuf *s" "int c"
92 .Fn sbuf_set_drain "struct sbuf *s" "sbuf_drain_func *func" "void *arg"
94 .Fn sbuf_trim "struct sbuf *s"
96 .Fn sbuf_error "struct sbuf *s"
98 .Fn sbuf_finish "struct sbuf *s"
100 .Fn sbuf_data "struct sbuf *s"
102 .Fn sbuf_len "struct sbuf *s"
104 .Fn sbuf_done "struct sbuf *s"
106 .Fn sbuf_delete "struct sbuf *s"
108 .Fn sbuf_start_section "struct sbuf *s" "ssize_t *old_lenp"
110 .Fn sbuf_end_section "struct sbuf *s" "ssize_t old_len" "size_t pad" "int c"
113 .Fa "struct sbuf *sb"
116 .Fa "const char *hdr"
120 .Fn sbuf_putbuf "struct sbuf *s"
123 .Fn sbuf_new_for_sysctl "struct sbuf *s" "char *buf" "int length" "struct sysctl_req *req"
127 family of functions allows one to safely allocate, compose and
128 release strings in kernel or user space.
130 Instead of arrays of characters, these functions operate on structures
136 Any errors encountered during the allocation or composition of the
137 string will be latched in the data structure,
138 making a single error test at the end of the composition
139 sufficient to determine success or failure of the entire process.
143 function initializes the
145 pointed to by its first argument.
155 argument is a pointer to a buffer in which to store the actual string;
159 will allocate one using
163 is the initial size of the storage buffer.
166 may be comprised of the following flags:
167 .Bl -tag -width ".Dv SBUF_AUTOEXTEND"
169 The storage buffer is fixed at its initial size.
170 Attempting to extend the sbuf beyond this size results in an overflow condition.
171 .It Dv SBUF_AUTOEXTEND
172 This indicates that the storage buffer may be extended as necessary, so long
173 as resources allow, to hold additional data.
180 it must point to an array of at least
183 The result of accessing that array directly while it is in use by the
188 function is a shortcut for creating a completely dynamic
190 It is the equivalent of calling
197 .Dv SBUF_AUTOEXTEND .
200 .Fn sbuf_new_for_sysctl
201 function will set up an sbuf with a drain function to use
203 when the internal buffer fills.
204 Note that if the various functions which append to an sbuf are used while
205 a non-sleepable lock is held, the user buffer should be wired using
206 .Fn sysctl_wire_old_buffer .
212 and frees any memory allocated for it.
213 There must be a call to
217 Any attempt to access the sbuf after it has been deleted will fail.
221 function invalidates the contents of the
223 and resets its position to zero.
231 which is a value between zero and one less than the size of the
233 This effectively truncates the sbuf at the new position.
237 function appends the first
239 bytes from the buffer
248 bytes from the specified userland address into the
253 function replaces the contents of the
257 bytes from the buffer
262 function appends the NUL-terminated string
266 at the current position.
270 function sets a drain function
274 and records a pointer
276 to be passed to the drain on callback.
277 The drain function cannot be changed while
281 The registered drain function
283 will be called with the argument
289 to a byte string that is the contents of the sbuf, and the length
292 If the drain function exists, it will be called when the sbuf internal
293 buffer is full, or on behalf of
295 The drain function may drain some or all of the data, but must drain
297 The return value from the drain function, if positive, indicates how
298 many bytes were drained.
299 If negative, the return value indicates the negative error code which
300 will be returned from this or a later call to
302 The returned drained length cannot be zero.
303 To do unbuffered draining, initialize the sbuf with a two-byte buffer.
304 The drain will be called for every byte added to the sbuf.
311 functions cannot be used on an sbuf with a drain.
315 function copies a NUL-terminated string from the specified userland
320 argument is non-zero, no more than
322 characters (not counting the terminating NUL) are copied; otherwise
323 the entire string, or as much of it as can fit in the
329 function replaces the contents of the
331 with those of the NUL-terminated string
333 This is equivalent to calling
337 or one which position has been reset to zero with
344 function formats its arguments according to the format string pointed
347 and appends the resulting string to the
349 at the current position.
353 function behaves the same as
355 except that the arguments are obtained from the variable-length argument list
360 function appends the character
364 at the current position.
368 function removes trailing whitespace from the
373 function returns any error value that the
375 may have accumulated, either from the drain function, or ENOMEM if the
378 This function is generally not needed and instead the error code from
380 is the preferred way to discover whether an sbuf had an error.
384 function will call the attached drain function if one exists until all
388 If there is no attached drain,
392 In either case it marks the
394 as finished, which means that it may no longer be modified using
403 is used to reset the sbuf.
407 function returns the actual string;
409 only works on a finished
413 function returns the length of the string.
416 with an attached drain,
418 returns the length of the un-drained data.
420 returns non-zero if the
425 .Fn sbuf_start_section
428 functions may be used for automatic section alignment.
433 specify the padding size and a character used for padding.
438 are to save and restore the current section length when nested sections
440 For the top level section
442 and \-1 can be specified for
450 function prints an array of bytes to the supplied sbuf, along with an ASCII
451 representation of the bytes if possible.
454 man page for more details on the interface.
458 function printfs the sbuf to stdout if in userland, and to the console
459 and log if in the kernel.
460 It does not drain the buffer or update any pointers.
462 If an operation caused an
464 to overflow, most subsequent operations on it will fail until the
470 or its position is reset to a value between 0 and one less than the
471 size of its storage buffer using
473 or it is reinitialized to a sufficiently short string using
476 Drains in user-space will not always function as indicated.
477 While the drain function will be called immediately on overflow from
486 currently have no way to determine whether there will be an overflow
487 until after it occurs, and cannot do a partial expansion of the format
489 Thus when using libsbuf the buffer may be extended to allow completion
490 of a single printf call, even though a drain is attached.
496 if it failed to allocate a storage buffer, and a pointer to the new
502 function returns \-1 if
504 was invalid, and zero otherwise.
514 all return \-1 if the buffer overflowed, and zero otherwise.
518 function returns a non-zero value if the buffer has an overflow or
519 drain error, and zero otherwise.
523 function returns \-1 if the buffer overflowed.
528 returns \-1 if copying string from userland failed, and number of bytes
533 function returns the section length or \-1 if the buffer has an error.
537 function (the kernel version) returns ENOMEM if the sbuf overflowed before
539 or returns the error code from the drain if one is attached.
543 function (the userland version)
544 will return zero for success and \-1 and set errno on error.
546 .Bd -literal -compact
547 #include <sys/sbuf.h>
551 sb = sbuf_new_auto();
552 sbuf_cat(sb, "Customers found:\en");
553 TAILQ_FOREACH(foo, &foolist, list) {
554 sbuf_printf(sb, " %4d %s\en", foo->index, foo->name);
555 sbuf_printf(sb, " Address: %s\en", foo->address);
556 sbuf_printf(sb, " Zip: %s\en", foo->zipcode);
558 if (sbuf_finish(sb) != 0) /* Check for any and all errors */
559 err(1, "Could not generate message");
560 transmit_msg(sbuf_data(sb), sbuf_len(sb));
574 family of functions first appeared in
580 family of functions was designed by
581 .An Poul-Henning Kamp Aq phk@FreeBSD.org
583 .An Dag-Erling Sm\(/orgrav Aq des@FreeBSD.org .
584 Additional improvements were suggested by
585 .An Justin T. Gibbs Aq gibbs@FreeBSD.org .
586 Auto-extend support added by
587 .An Kelly Yancey Aq kbyanc@FreeBSD.org .
588 Drain functionality added by
589 .An Matthew Fleming Aq mdf@FreeBSD.org .
591 This manual page was written by
592 .An Dag-Erling Sm\(/orgrav Aq des@FreeBSD.org .