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 ,
39 .Nm sbuf_clear_flags ,
58 .Nm sbuf_start_section ,
59 .Nm sbuf_end_section ,
62 .Nd safe string composition
66 .Ft typedef\ int ( sbuf_drain_func ) ( void\ *arg, const\ char\ *data, int\ len ) ;
105 .Fa "const void *buf"
111 .Fa "const void *buf"
117 .Fa "const char *str"
122 .Fa "const char *str"
127 .Fa "const char *fmt" "..."
132 .Fa "const char *fmt"
143 .Fa "sbuf_drain_func *func"
175 .Fo sbuf_start_section
177 .Fa "ssize_t *old_lenp"
182 .Fa "ssize_t old_len"
188 .Fa "struct sbuf *sb"
191 .Fa "const char *hdr"
204 .Fa "const void *uaddr"
210 .Fa "const void *uaddr"
215 .Fo sbuf_new_for_sysctl
219 .Fa "struct sysctl_req *req"
221 .Fd #endif /* _KERNEL */
225 family of functions allows one to safely allocate, compose and
226 release strings in kernel or user space.
228 Instead of arrays of characters, these functions operate on structures
234 Any errors encountered during the allocation or composition of the
235 string will be latched in the data structure,
236 making a single error test at the end of the composition
237 sufficient to determine success or failure of the entire process.
241 function initializes the
243 pointed to by its first argument.
253 argument is a pointer to a buffer in which to store the actual string;
257 will allocate one using
261 is the initial size of the storage buffer.
264 may be comprised of the following flags:
265 .Bl -tag -width ".Dv SBUF_AUTOEXTEND"
267 The storage buffer is fixed at its initial size.
268 Attempting to extend the sbuf beyond this size results in an overflow condition.
269 .It Dv SBUF_AUTOEXTEND
270 This indicates that the storage buffer may be extended as necessary, so long
271 as resources allow, to hold additional data.
272 .It Dv SBUF_INCLUDENUL
273 This causes the final nulterm byte to be counted in the length of the data.
280 it must point to an array of at least
283 The result of accessing that array directly while it is in use by the
288 function is a shortcut for creating a completely dynamic
290 It is the equivalent of calling
297 .Dv SBUF_AUTOEXTEND .
300 .Fn sbuf_new_for_sysctl
301 function will set up an sbuf with a drain function to use
303 when the internal buffer fills.
304 Note that if the various functions which append to an sbuf are used while
305 a non-sleepable lock is held, the user buffer should be wired using
306 .Fn sysctl_wire_old_buffer .
312 and frees any memory allocated for it.
313 There must be a call to
317 Any attempt to access the sbuf after it has been deleted will fail.
321 function invalidates the contents of the
323 and resets its position to zero.
327 function returns the current user flags.
332 functions set or clear one or more user flags, respectively.
333 The user flags are described under the
343 which is a value between zero and one less than the size of the
345 This effectively truncates the sbuf at the new position.
349 function appends the first
351 bytes from the buffer
360 bytes from the specified userland address into the
365 function replaces the contents of the
369 bytes from the buffer
374 function appends the NUL-terminated string
378 at the current position.
382 function sets a drain function
386 and records a pointer
388 to be passed to the drain on callback.
389 The drain function cannot be changed while
393 The registered drain function
395 will be called with the argument
401 to a byte string that is the contents of the sbuf, and the length
404 If the drain function exists, it will be called when the sbuf internal
405 buffer is full, or on behalf of
407 The drain function may drain some or all of the data, but must drain
409 The return value from the drain function, if positive, indicates how
410 many bytes were drained.
411 If negative, the return value indicates the negative error code which
412 will be returned from this or a later call to
414 The returned drained length cannot be zero.
415 To do unbuffered draining, initialize the sbuf with a two-byte buffer.
416 The drain will be called for every byte added to the sbuf.
423 functions cannot be used on an sbuf with a drain.
427 function copies a NUL-terminated string from the specified userland
432 argument is non-zero, no more than
434 characters (not counting the terminating NUL) are copied; otherwise
435 the entire string, or as much of it as can fit in the
441 function replaces the contents of the
443 with those of the NUL-terminated string
445 This is equivalent to calling
449 or one which position has been reset to zero with
456 function formats its arguments according to the format string pointed
459 and appends the resulting string to the
461 at the current position.
465 function behaves the same as
467 except that the arguments are obtained from the variable-length argument list
472 function appends the character
476 at the current position.
480 function removes trailing whitespace from the
485 function returns any error value that the
487 may have accumulated, either from the drain function, or ENOMEM if the
490 This function is generally not needed and instead the error code from
492 is the preferred way to discover whether an sbuf had an error.
496 function will call the attached drain function if one exists until all
500 If there is no attached drain,
504 In either case it marks the
506 as finished, which means that it may no longer be modified using
515 is used to reset the sbuf.
519 function returns the actual string;
521 only works on a finished
525 function returns the length of the string.
528 with an attached drain,
530 returns the length of the un-drained data.
532 returns non-zero if the
537 .Fn sbuf_start_section
540 functions may be used for automatic section alignment.
545 specify the padding size and a character used for padding.
550 are to save and restore the current section length when nested sections
552 For the top level section
554 and \-1 can be specified for
562 function prints an array of bytes to the supplied sbuf, along with an ASCII
563 representation of the bytes if possible.
566 man page for more details on the interface.
570 function printfs the sbuf to stdout if in userland, and to the console
571 and log if in the kernel.
572 It does not drain the buffer or update any pointers.
574 If an operation caused an
576 to overflow, most subsequent operations on it will fail until the
582 or its position is reset to a value between 0 and one less than the
583 size of its storage buffer using
585 or it is reinitialized to a sufficiently short string using
588 Drains in user-space will not always function as indicated.
589 While the drain function will be called immediately on overflow from
598 currently have no way to determine whether there will be an overflow
599 until after it occurs, and cannot do a partial expansion of the format
601 Thus when using libsbuf the buffer may be extended to allow completion
602 of a single printf call, even though a drain is attached.
608 if it failed to allocate a storage buffer, and a pointer to the new
614 function returns \-1 if
616 was invalid, and zero otherwise.
626 all return \-1 if the buffer overflowed, and zero otherwise.
630 function returns a non-zero value if the buffer has an overflow or
631 drain error, and zero otherwise.
635 function returns \-1 if the buffer overflowed.
640 returns \-1 if copying string from userland failed, and number of bytes
645 function returns the section length or \-1 if the buffer has an error.
649 function (the kernel version) returns ENOMEM if the sbuf overflowed before
651 or returns the error code from the drain if one is attached.
655 function (the userland version)
656 will return zero for success and \-1 and set errno on error.
658 .Bd -literal -compact
659 #include <sys/types.h>
660 #include <sys/sbuf.h>
664 sb = sbuf_new_auto();
665 sbuf_cat(sb, "Customers found:\en");
666 TAILQ_FOREACH(foo, &foolist, list) {
667 sbuf_printf(sb, " %4d %s\en", foo->index, foo->name);
668 sbuf_printf(sb, " Address: %s\en", foo->address);
669 sbuf_printf(sb, " Zip: %s\en", foo->zipcode);
671 if (sbuf_finish(sb) != 0) /* Check for any and all errors */
672 err(1, "Could not generate message");
673 transmit_msg(sbuf_data(sb), sbuf_len(sb));
687 family of functions first appeared in
693 family of functions was designed by
694 .An Poul-Henning Kamp Aq phk@FreeBSD.org
696 .An Dag-Erling Sm\(/orgrav Aq des@FreeBSD.org .
697 Additional improvements were suggested by
698 .An Justin T. Gibbs Aq gibbs@FreeBSD.org .
699 Auto-extend support added by
700 .An Kelly Yancey Aq kbyanc@FreeBSD.org .
701 Drain functionality added by
702 .An Matthew Fleming Aq mdf@FreeBSD.org .
704 This manual page was written by
705 .An Dag-Erling Sm\(/orgrav Aq des@FreeBSD.org .