SBUF(9)

πŸ¦‹ SBUF(9) πŸ¦‹ β€” safe string composition

okay bestie, listen β€” this is the Kernel Developer's Manual page for the entire sbuf girl gang and honestly? they ate. πŸ’… Let's get into it.


πŸ’– NAME πŸ’–

Meet the whole squad: 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_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_putbuf β€” safe string composition πŸŽ€


✨ SYNOPSIS ✨

#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_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);

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 πŸ’…

The sbuf family of functions lets you safely allocate, compose and release strings in kernel or user space. ✨

Instead of raw arrays of characters, these functions operate on structures called sbufs, which live in <sys/sbuf.h>.

Here's the slay part: any errors that pop up while you're allocating or composing the string get latched right into the data structure. So you can do one single error check at the very end and know whether the whole thing worked or flopped. one test, whole story. πŸ‘‘


🌸 sbuf_new()

The sbuf_new() function initializes the sbuf pointed to by its first argument. If that pointer is NULL, sbuf_new() allocates a struct sbuf using malloc(9).

The buf argument is a pointer to a buffer where the actual string gets stored β€” and if it's NULL, sbuf_new() will allocate one for you using malloc(9).

The length is the initial size of the storage buffer. 🫢

The fourth argument, flags, may be made up of these flags:

  • SBUF_FIXEDLEN β€” the storage buffer is locked in at its initial size. Try to extend the sbuf past that and you get an overflow condition. no growing, bestie. 🚫
  • SBUF_AUTOEXTEND β€” this says the storage buffer can grow as needed, as long as resources allow, to hold extra data. πŸ’–
  • SBUF_INCLUDENUL β€” this makes the final nulterm byte get counted in the length of the data.
  • SBUF_DRAINTOEOR β€” treat top-level sections started with sbuf_start_section() as a record boundary marker that will be used during drain operations so records don't get split. If a record grows big enough to fill the sbuf and therefore can't be drained without being split, an error of EDEADLK is set. 😭
careful though: if buf is not NULL, it must point to an array of at least length characters. Accessing that array directly while it's in use by the sbuf? the result is undefined. don't do it. πŸ™ˆ

🌈 sbuf_new_auto()

sbuf_new_auto() is the shortcut for creating a completely dynamic sbuf. It's literally the equivalent of calling sbuf_new() with the values NULL, NULL, 0, and SBUF_AUTOEXTEND. ✨


πŸ¦‹ sbuf_new_for_sysctl()

sbuf_new_for_sysctl() sets up an sbuf with a drain function so it uses SYSCTL_OUT() when the internal buffer fills up.

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 using sysctl_wire_old_buffer().

πŸ’œ sbuf_delete()

sbuf_delete() clears the sbuf and frees any memory allocated for it.

the rule: there must be a call to sbuf_delete() for every call to sbuf_new(). And any attempt to access the sbuf after it's been deleted will fail. it's gone, bestie. πŸ’€

πŸŽ€ sbuf_clear()

sbuf_clear() invalidates the contents of the sbuf and resets its position to zero. fresh start energy. ✨


⭐ sbuf_get_flags(), sbuf_set_flags(), sbuf_clear_flags()

sbuf_get_flags() returns the current user flags. The sbuf_set_flags() and sbuf_get_flags() functions set or clear one or more user flags, respectively. The user flags are the ones described up under the sbuf_new() function. πŸ‘†


πŸ“ sbuf_setpos()

sbuf_setpos() sets the sbuf's end position to pos, which is a value between zero and the current position in the buffer.

important: it can only truncate the sbuf to the new position. no sneaky extending. πŸ’…


🌸 sbuf_bcat()

sbuf_bcat() appends the first len bytes from the buffer buf to the sbuf.


🧚 sbuf_bcopyin()

sbuf_bcopyin() copies len bytes from the specified userland address into the sbuf. (kernel-side girlie)


πŸ¦‹ sbuf_bcpy()

sbuf_bcpy() replaces the contents of the sbuf with the first len bytes from the buffer buf. out with the old, in with the new. πŸ’…


πŸ’– sbuf_cat()

sbuf_cat() appends the NUL-terminated string str to the sbuf at the current position.


πŸ”₯ sbuf_set_drain()

sbuf_set_drain() sets a drain function func for the sbuf, and records a pointer arg to be passed to the drain on callback.

note: the drain function cannot be changed while sbuf_len is non-zero.

Now, the registered drain function sbuf_drain_func will be called with:

  • the argument arg that you provided to sbuf_set_drain(),
  • a pointer data to a byte string that is the contents of the sbuf,
  • and the length len of 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. 😀

The return value from the drain function, if positive, indicates how many bytes were drained. If negative, the return value indicates the negative error code which will be returned from this or a later call to sbuf_finish().

no cap: the returned drained length cannot be zero.

To do unbuffered draining, initialize the sbuf with a two-byte buffer β€” then the drain will be called for every single byte added to the sbuf. ✨

and also: sbuf_bcopyin(), sbuf_copyin(), sbuf_trim(), and sbuf_data() cannot be used on an sbuf with a drain. that's just the rules. πŸ™ˆ

πŸŽ€ sbuf_copyin()

sbuf_copyin() copies a NUL-terminated string from the specified userland address into the sbuf. (kernel-side girlie)

If the len argument is non-zero, no more than len characters (not counting the terminating NUL) are copied; otherwise the entire string, or as much of it as can fit in the sbuf, is copied. 🫢


πŸ‘‘ sbuf_cpy()

sbuf_cpy() replaces the contents of the sbuf with those of the NUL-terminated string str.

This is equivalent to calling sbuf_cat() with a fresh sbuf, or one whose position has been reset to zero with sbuf_clear() or sbuf_setpos(). same vibe, different entry point. πŸ’…


✨ sbuf_printf()

sbuf_printf() formats its arguments according to the format string pointed to by fmt and appends the resulting string to the sbuf at the current position.


🌈 sbuf_vprintf()

sbuf_vprintf() behaves the same as sbuf_printf() except that the arguments are obtained from the variable-length argument list ap.


πŸ“ sbuf_putc()

sbuf_putc() appends the character c to the sbuf at the current position.


🌸 sbuf_trim()

sbuf_trim() removes trailing whitespace from the sbuf. bye bye, extra spaces. 🧚


πŸ’œ sbuf_error()

sbuf_error() returns any error value that the sbuf may have accumulated β€” either from the drain function, or ENOMEM if the sbuf overflowed.

ngl though: this function is generally not needed, and instead the error code from sbuf_finish() is the preferred way to discover whether an sbuf had an error. iykyk. ✨


πŸ”₯ sbuf_finish()

sbuf_finish() will call the attached drain function if one exists, until all the data in the sbuf is flushed. If there is no attached drain, sbuf_finish() NUL-terminates the sbuf.

Either way, it marks the sbuf as finished β€” which means it may no longer be modified using sbuf_setpos(), sbuf_cat(), sbuf_cpy(), sbuf_printf() or sbuf_putc(), until sbuf_clear() is used to reset the sbuf. πŸš«πŸ’…


⭐ sbuf_data(), sbuf_len(), sbuf_done()

sbuf_data() returns the actual string β€” and 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. πŸ‘‘


🧚 sbuf_start_section() and sbuf_end_section()

These two may 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. πŸŽ€


πŸ¦‹ sbuf_hexdump()

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_putbuf()

sbuf_putbuf() printfs the sbuf to stdout if in userland, and to the console and log if in the kernel.

heads up: it does not drain the buffer or update any pointers. 🫢

πŸ’€ NOTES πŸ’€

Okay so this is the part where things get a lil dramatic, bestie. If an operation caused an sbuf to overflow, most subsequent operations on it will fail until one of these things 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().

Like… it's in timeout until you fix it. No cap.


Now for the messy part πŸ‘€ Drains in user-space will not always function as indicated. While the drain function will be called immediately on overflow from the 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 cannot do a partial expansion of the format string.

So when using libsbuf the buffer may be extended to allow completion of a single printf call, even though a drain is attached. It's giving "surprise, the drain didn't catch it in time" 😭

✨ RETURN VALUES ✨

Every function's lil report card, let's go:

  • sbuf_new() β€” returns NULL if it failed to allocate a storage buffer, and a pointer to the new sbuf otherwise. Iconic when it works.
  • sbuf_setpos() β€” returns -1 if pos was invalid, and zero otherwise.
  • sbuf_bcat(), sbuf_cat(), sbuf_cpy(), sbuf_printf(), sbuf_putc(), and sbuf_trim() β€” they all return -1 if the buffer overflowed, and zero otherwise. Bestie squad with the same vibe check fr fr.
  • sbuf_error() β€” returns a non-zero value if the buffer has an overflow or drain error, and zero otherwise.
  • sbuf_len() β€” returns -1 if the buffer overflowed. oof
  • sbuf_copyin() β€” returns -1 if copying string from userland failed, and number of bytes copied otherwise.
  • sbuf_end_section() β€” returns the section length or -1 if the buffer has an error.
  • sbuf_finish(9) (the kernel version) β€” returns ENOMEM if 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 -1 and set errno on error.

🌈 EXAMPLES 🌈

Okay this is the moment, the main character example, I'm obsessed:

#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);

Look at her. Building a whole message, checking her work, sending it, cleaning up after herself. That's growth. That's maturity. That's slay. 🫢


πŸ‘€ SEE ALSO πŸ‘€

Go read these next, bestie, they're all part of the same friend group:


πŸŽ€ HISTORY πŸŽ€

The sbuf family of functions first appeared in FreeBSD 4.4. A classic. A legend. An era. πŸ’…


πŸ’– AUTHORS πŸ’–

Giving credit where it's due, because we always do:

  • The sbuf family of functions was designed by Poul-Henning Kamp <phk@FreeBSD.org> and implemented by Dag-Erling SmΓΈrgrav <des@FreeBSD.org>.
  • Additional improvements were suggested by Justin T. Gibbs <gibbs@FreeBSD.org>.
  • Auto-extend support added by Kelly Yancey <kbyanc@FreeBSD.org>.
  • Drain functionality added by Matthew Fleming <mdf@FreeBSD.org>.

And this manual page was written by Dag-Erling SmΓΈrgrav <des@FreeBSD.org>. Author of the code AND the docs?? Multi-hyphenate icon behavior, we love to see it. πŸ‘‘


And that's the whole page, bestie!! 🌸 This came out of the Debian manual page collection, dated August 26, 2020 β€” so yeah, this version is a lil vintage but she still ate. πŸ’œ πŸ¦‹ Go forth and sbuf_finish() responsibly, mwah 😘

πŸ“¦ where this page comes from