SBUF(9)
π SBUF(9) β safe string composition π
okay bestie, welcome to the safe-string-composition page, straight from the Kernel Developerβs Manual. this is the sbuf glow-up: build strings without the chaos, and yes weβre keeping every function name exactly as-is. no cap.
π NAME π
Meet the whole sbuf fam: sbuf, sbuf_new, sbuf_new_auto, sbuf_new_for_sysctl, sbuf_clear, sbuf_get_flags, sbuf_set_flags, sbuf_clear_flags, sbuf_setpos, sbuf_bcat, sbuf_bcopyin, sbuf_bcpy, sbuf_cat, sbuf_copyin, sbuf_cpy, sbuf_nl_terminate, sbuf_printf, sbuf_vprintf, sbuf_putc, sbuf_set_drain, sbuf_trim, sbuf_error, sbuf_finish, sbuf_data, sbuf_len, sbuf_done, sbuf_delete, sbuf_start_section, sbuf_end_section, sbuf_hexdump, sbuf_printf_drain, sbuf_putbuf. iconic lineup, honestly.
π LIBRARY π
library βlibsbufβ β yup, thatβs the one, bestie. slay.
β¨ SYNOPSIS β¨
okay so hereβs the C spellbook, copy it exactly β no autocorrect energy:
#include <sys/types.h>
#include <sys/sbuf.h>
typedef int
(sbuf_drain_func)(void *arg, const char *data, int len);
struct sbuf *
sbuf_new(struct sbuf *s, char *buf, int length, int flags);
struct sbuf *
sbuf_new_auto(void);
void
sbuf_clear(struct sbuf *s);
int
sbuf_get_flags(struct sbuf *s);
void
sbuf_set_flags(struct sbuf *s, int flags);
void
sbuf_clear_flags(struct sbuf *s, int flags);
int
sbuf_setpos(struct sbuf *s, int pos);
int
sbuf_bcat(struct sbuf *s, const void *buf, size_t len);
int
sbuf_bcpy(struct sbuf *s, const void *buf, size_t len);
int
sbuf_cat(struct sbuf *s, const char *str);
int
sbuf_cpy(struct sbuf *s, const char *str);
int
sbuf_nl_terminate(struct sbuf *);
int
sbuf_printf(struct sbuf *s, const char *fmt, ...);
int
sbuf_vprintf(struct sbuf *s, const char *fmt, va_list ap);
int
sbuf_putc(struct sbuf *s, int c);
void
sbuf_set_drain(struct sbuf *s, sbuf_drain_func *func, void *arg);
int
sbuf_trim(struct sbuf *s);
int
sbuf_error(struct sbuf *s);
int
sbuf_finish(struct sbuf *s);
char *
sbuf_data(struct sbuf *s);
ssize_t
sbuf_len(struct sbuf *s);
int
sbuf_done(struct sbuf *s);
void
sbuf_delete(struct sbuf *s);
void
sbuf_start_section(struct sbuf *s, ssize_t *old_lenp);
ssize_t
sbuf_end_section(struct sbuf *s, ssize_t old_len, size_t pad, int c);
void
sbuf_hexdump(struct sbuf *sb, void *ptr, int length, const char *hdr,
int flags);
int
sbuf_printf_drain(void *arg, const char *data, int len);
void
sbuf_putbuf(struct sbuf *s);
#ifdef _KERNEL
#include <sys/types.h>
#include <sys/sbuf.h>
int
sbuf_bcopyin(struct sbuf *s, const void *uaddr, size_t len);
int
sbuf_copyin(struct sbuf *s, const void *uaddr, size_t len);
#include <sys/sysctl.h>
struct sbuf *
sbuf_new_for_sysctl(struct sbuf *s, char *buf, int length,
struct sysctl_req *req);
#endif /* _KERNEL */
π DESCRIPTION π
okay so bestie, the whole sbuf family is basically your little string-building bestie that lets you safely allocate, compose and release strings β and it works in kernel space or user space, no cap. π¦
instead of dealing with raw arrays of characters (ugh, so much manual work π), these functions operate on structs called sbufs, and they're defined in <sys/sbuf.h>. cute, right?
and here's the iconic part: any errors that happen while you're allocating or composing the string get latched right into the data structure itself. so you literally just do one error check at the very end and you instantly know if the whole process worked or flopped. main character error handling fr fr. β¨
πΈ sbuf_new() β the beginning of everything
The sbuf_new() function initializes the sbuf that its first argument points to. and if that pointer is NULL, sbuf_new() allocates a struct sbuf for you using malloc(9). generous queen. π
the buf argument is a pointer to a buffer where the actual string goes β and if that's NULL too, sbuf_new() will allocate one for you using malloc(9). the length argument is the initial size of the storage buffer.
the fourth argument, flags, can be made up of these beauties:
SBUF_FIXEDLENβ the storage buffer is locked at its initial size. trying to extend the sbuf past that size gets you an overflow condition, bestie. it's giving "you shall not pass". πSBUF_AUTOEXTENDβ this one says the storage buffer can be extended as necessary, as long as resources allow, to hold extra data. grow bestie grow π±SBUF_INCLUDENULβ this makes the final nulterm byte count in the length of the data. so it's included, hence the name, iykyk πSBUF_DRAINTOEORβ treats top-level sections started withsbuf_start_section()as a record boundary marker that gets used during drain operations to avoid records being split. but listen β if a record grows so big that it fills the sbuf and therefore can't be drained without being split, an error ofEDEADLKis set. πSBUF_NOWAITβ says that attempts to extend the storage buffer should fail under low memory conditions, exactly likemalloc(9)withM_NOWAIT. no waiting around β
β οΈ huge warning bestie: ifbufis notNULL, it must point to an array of at leastlengthcharacters. and the result of accessing that array directly while it's in use by the sbuf is undefined. so likeβ¦ don't. π
π§ the shortcuts
sbuf_new_auto()β a shortcut for creating a completely dynamic sbuf. it's the equivalent of callingsbuf_new()with the valuesNULL,NULL,0, andSBUF_AUTOEXTEND. literally the lazy girl's (affectionate) way in πsbuf_new_for_sysctl()β sets up an sbuf with a drain function so it usesSYSCTL_OUT()when the internal buffer fills up. but heads up: if the various functions that append to an sbuf are used while a non-sleepable lock is held, the user buffer should be wired usingsysctl_wire_old_buffer(). safety first, always π«Ά
π the lifecycle functions
sbuf_delete()β clears the sbuf and frees any memory allocated for it. and this is a strict rule: there must be a call tosbuf_delete()for every call tosbuf_new(). any attempt to access the sbuf after it's been deleted will fail. periodt. πsbuf_clear()β invalidates the contents of the sbuf and resets its position to zero. fresh start energy β¨
π¦ the flags functions
sbuf_get_flags()β returns the current user flags.sbuf_set_flags()andsbuf_clear_flags()β set or clear one or more user flags, respectively.
the user flags are the ones described up in the sbuf_new() section, so scroll up if you forgot π
π the position function
sbuf_setpos()β sets the sbuf's end position topos, which is a value between zero and the current position in the buffer. important: it can only truncate the sbuf to the new position. it does not let you grow, bestie, it only lets you snip. βοΈ
π the appending + copying girlies
sbuf_bcat()β appends the firstlenbytes from the bufferbufto the sbuf.sbuf_bcopyin()β copieslenbytes from the specified userland address into the sbuf.sbuf_bcpy()β replaces the contents of the sbuf with the firstlenbytes from the bufferbuf.sbuf_cat()β appends the NUL-terminated stringstrto the sbuf at the current position.sbuf_copyin()β copies a NUL-terminated string from the specified userland address into the sbuf. if thelenargument is non-zero, no more thanlencharacters are copied (not counting the terminating NUL); otherwise the entire string β or as much of it as can fit in the sbuf β is copied. πsbuf_cpy()β replaces the contents of the sbuf with those of the NUL-terminated stringstr. this is equivalent to callingsbuf_cat()with a fresh sbuf, or one whose position has been reset to zero withsbuf_clear()orsbuf_setpos().
π§ the drain stuff (pay attention bestie)
sbuf_set_drain() sets a drain function func for the sbuf, and records a pointer arg to be passed to the drain on callback. the drain function cannot be changed while sbuf_len is non-zero. nope, not allowed π
the registered drain function sbuf_drain_func gets called with:
- the argument
argthat you provided tosbuf_set_drain(), - a pointer
datato a byte string that is the contents of the sbuf, - and the length
lenof the data.
if the drain function exists, it will be called when the sbuf internal buffer is full, or on behalf of sbuf_finish(). the drain function may drain some or all of the data β but it must drain at least 1 byte. no excuses.
the return value rules:
- if positive, it indicates how many bytes were drained.
- if negative, the return value indicates the negative error code which will be returned from this call or a later call to
sbuf_finish(). - if the returned drained length is 0, an error of
EDEADLKis set. π
want unbuffered draining? initialize the sbuf with a two-byte buffer, and the drain will be called for every single byte added to the sbuf. iconic dedication honestly.
π¨ big one: thesbuf_bcopyin(),sbuf_bcpy(),sbuf_clear(),sbuf_copyin(),sbuf_cpy(),sbuf_trim(),sbuf_data(), andsbuf_len()functions cannot be used on an sbuf that has a drain. like, just don't even try it. π ββοΈ
πΈ the formatting + writing functions
sbuf_nl_terminate()β appends a trailing newline character, if the current line is non-empty and not already terminated by a newline character. polite little touch β¨sbuf_printf()β formats its arguments according to the format string pointed to byfmtand appends the resulting string to the sbuf at the current position.sbuf_vprintf()β behaves the same assbuf_printf()except the arguments are obtained from the variable-length argument listap.sbuf_putc()β appends the charactercto the sbuf at the current position.sbuf_trim()β removes trailing whitespace from the sbuf. bye bye extra spaces π
π errors, finishing, and reading back
sbuf_error()β returns any error value the sbuf may have accumulated, either from the drain function, orENOMEMif the sbuf overflowed. but real talk: this function is generally not needed, and the error code fromsbuf_finish()is the preferred way to discover whether an sbuf had an error. iykyk πsbuf_finish()β will call the attached drain function if one exists, until all the data in the sbuf is flushed. if there's no attached drain,sbuf_finish()NUL-terminates the sbuf. in either case it marks the sbuf as finished, which means it may no longer be modified usingsbuf_setpos(),sbuf_cat(),sbuf_cpy(),sbuf_printf()orsbuf_putc()β untilsbuf_clear()is used to reset the sbuf. the sbuf is officially retired, show's over. π¬sbuf_data()β returns the actual string. but it only works on a finished sbuf.sbuf_len()β returns the length of the string. for an sbuf with an attached drain,sbuf_len()returns the length of the un-drained data.sbuf_done()β returns non-zero if the sbuf is finished.
π sections + hexdumps + special drains
sbuf_start_section() and sbuf_end_section() can be used for automatic section alignment. the arguments pad and c specify the padding size and a character used for padding. the arguments old_lenp and old_len are there to save and restore the current section length when nested sections are used. for the top level section, NULL and -1 can be specified for old_lenp and old_len respectively. nesting slays π
sbuf_hexdump() prints an array of bytes to the supplied sbuf, along with an ASCII representation of the bytes if possible. see the hexdump(3) man page for more details on the interface. π₯
sbuf_printf_drain() is a drain function that will call printf, or log to the console. the argument arg must be either NULL, or a valid pointer to a size_t. if arg is not NULL, the total bytes drained will be added to the value pointed to by arg. cute little counter π«Ά
sbuf_putbuf() printfs the sbuf to stdout if in userland, and to the console and log if in the kernel. the sbuf must be finished before calling sbuf_putbuf(). and heads up: it does not drain the buffer or update any pointers. π
β NOTES β
okay so if an operation caused an sbuf to overflow, most subsequent operations on it will fail β until one of these happens:
- the sbuf is finished using
sbuf_finish(), or - it's reset using
sbuf_clear(), or - its position is reset to a value between 0 and one less than the size of its storage buffer using
sbuf_setpos(), or - it's reinitialized to a sufficiently short string using
sbuf_cpy().
and here's a user-space caveat that lowkey matters: drains in user-space will not always function as indicated. while the drain function will be called immediately on overflow from sbuf_putc, sbuf_bcat, sbuf_cat functions, sbuf_printf and sbuf_vprintf currently have no way to determine whether there will be an overflow until after it occurs β and they cannot do a partial expansion of the format string. so when you're using libsbuf, the buffer may be extended to allow completion of a single printf call, even though a drain is attached. so likeβ¦ it's giving "close enough". π
π RETURN VALUES π
sbuf_new()returnsNULLif it failed to allocate a storage buffer, and a pointer to the new sbuf otherwise.sbuf_setpos()returns-1ifposwas invalid, and zero otherwise.sbuf_bcat(),sbuf_cat(),sbuf_cpy(),sbuf_printf(),sbuf_putc(), andsbuf_trim()all return-1if the buffer overflowed, and zero otherwise.sbuf_error()returns a non-zero value if the buffer has an overflow or drain error, and zero otherwise.sbuf_len()returns-1if the buffer overflowed.sbuf_copyin()returns-1if copying a string from userland failed, and the number of bytes copied otherwise.sbuf_end_section()returns the section length or-1if the buffer has an error.sbuf_finish(9)(the kernel version) returnsENOMEMif the sbuf overflowed before being finished, or returns the error code from the drain if one is attached.sbuf_finish(3)(the userland version) will return zero for success and-1and seterrnoon error.
π¦ EXAMPLES π¦
#include <sys/types.h>
#include <sys/sbuf.h>
struct sbuf *sb;
sb = sbuf_new_auto();
sbuf_cat(sb, "Customers found:\n");
TAILQ_FOREACH(foo, &foolist, list) {
sbuf_printf(sb, " %4d %s\n", foo->index, foo->name);
sbuf_printf(sb, " Address: %s\n", foo->address);
sbuf_printf(sb, " Zip: %s\n", foo->zipcode);
}
if (sbuf_finish(sb) != 0) /* Check for any and all errors */
err(1, "Could not generate message");
transmit_msg(sbuf_data(sb), sbuf_len(sb));
sbuf_delete(sb);
π SEE ALSO π
hexdump(3), printf(3), strcat(3), strcpy(3), copyin(9), copyinstr(9), printf(9)
π HISTORY
okay so storytime bestie π β the sbuf family of functions first appeared in FreeBSD 4.4. that's the origin story, that's the lore, no cap. β¨
π AUTHORS
these are the actual main characters, fr fr π«Ά β give them their flowers:
- the
sbuffamily of functions was designed by Poul-Henning Kamp<phk@FreeBSD.org>π iconic - and implemented by Dag-Erling SmΓΈrgrav
<des@FreeBSD.org>β the way that duo just ate π - Additional improvements were suggested by Justin T. Gibbs
<gibbs@FreeBSD.org>π - Auto-extend support was added by Kelly Yancey
<kbyanc@FreeBSD.org>π₯ - Drain functionality was added by Matthew Fleming
<mdf@FreeBSD.org>π - and this very manual page was written by Dag-Erling SmΓΈrgrav
<des@FreeBSD.org>π
so yeah, a whole squad of legends built this thing. respect on every single name. π
and that's a wrap on SBUF(9), bestie π¦π this one's from the Debian edition, dated October 3, 2023 β take care of it, and go forth and sbuf responsibly πΈβ¨
π¦ where this page comes from
- freebsd 14.5: base 14.5
- freebsd 15.1: base 15.1
- nixpkgs unstable: libsbuf 14.1.0
- nixpkgs unstable: source
- Copyright (c) 2000 Poul-Henning Kamp and Dag-Erling SmΓΈrgrav
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms