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.Dd August 26, 2020 27.Dt SBUF 9 28.Os 29.Sh NAME 30.Nm sbuf , 31.Nm sbuf_new , 32.Nm sbuf_new_auto , 33.Nm sbuf_new_for_sysctl , 34.Nm sbuf_clear , 35.Nm sbuf_get_flags , 36.Nm sbuf_set_flags , 37.Nm sbuf_clear_flags , 38.Nm sbuf_setpos , 39.Nm sbuf_bcat , 40.Nm sbuf_bcopyin , 41.Nm sbuf_bcpy , 42.Nm sbuf_cat , 43.Nm sbuf_copyin , 44.Nm sbuf_cpy , 45.Nm sbuf_nl_terminate , 46.Nm sbuf_printf , 47.Nm sbuf_vprintf , 48.Nm sbuf_putc , 49.Nm sbuf_set_drain , 50.Nm sbuf_trim , 51.Nm sbuf_error , 52.Nm sbuf_finish , 53.Nm sbuf_data , 54.Nm sbuf_len , 55.Nm sbuf_done , 56.Nm sbuf_delete , 57.Nm sbuf_start_section , 58.Nm sbuf_end_section , 59.Nm sbuf_hexdump , 60.Nm sbuf_printf_drain , 61.Nm sbuf_putbuf 62.Nd safe string composition 63.Sh SYNOPSIS 64.In sys/types.h 65.In sys/sbuf.h 66.Ft typedef int 67.Fo (sbuf_drain_func) 68.Fa "void *arg" 69.Fa "const char *data" 70.Fa "int len" 71.Fc 72.Pp 73.Ft struct sbuf * 74.Fo sbuf_new 75.Fa "struct sbuf *s" 76.Fa "char *buf" 77.Fa "int length" 78.Fa "int flags" 79.Fc 80.Ft struct sbuf * 81.Fo sbuf_new_auto 82.Fa "void" 83.Fc 84.Ft void 85.Fo sbuf_clear 86.Fa "struct sbuf *s" 87.Fc 88.Ft int 89.Fo sbuf_get_flags 90.Fa "struct sbuf *s" 91.Fc 92.Ft void 93.Fo sbuf_set_flags 94.Fa "struct sbuf *s" 95.Fa "int flags" 96.Fc 97.Ft void 98.Fo sbuf_clear_flags 99.Fa "struct sbuf *s" 100.Fa "int flags" 101.Fc 102.Ft int 103.Fo sbuf_setpos 104.Fa "struct sbuf *s" 105.Fa "int pos" 106.Fc 107.Ft int 108.Fo sbuf_bcat 109.Fa "struct sbuf *s" 110.Fa "const void *buf" 111.Fa "size_t len" 112.Fc 113.Ft int 114.Fo sbuf_bcpy 115.Fa "struct sbuf *s" 116.Fa "const void *buf" 117.Fa "size_t len" 118.Fc 119.Ft int 120.Fo sbuf_cat 121.Fa "struct sbuf *s" 122.Fa "const char *str" 123.Fc 124.Ft int 125.Fo sbuf_cpy 126.Fa "struct sbuf *s" 127.Fa "const char *str" 128.Fc 129.Ft int 130.Fn sbuf_nl_terminate "struct sbuf *" 131.Ft int 132.Fo sbuf_printf 133.Fa "struct sbuf *s" 134.Fa "const char *fmt" "..." 135.Fc 136.Ft int 137.Fo sbuf_vprintf 138.Fa "struct sbuf *s" 139.Fa "const char *fmt" 140.Fa "va_list ap" 141.Fc 142.Ft int 143.Fo sbuf_putc 144.Fa "struct sbuf *s" 145.Fa "int c" 146.Fc 147.Ft void 148.Fo sbuf_set_drain 149.Fa "struct sbuf *s" 150.Fa "sbuf_drain_func *func" 151.Fa "void *arg" 152.Fc 153.Ft int 154.Fo sbuf_trim 155.Fa "struct sbuf *s" 156.Fc 157.Ft int 158.Fo sbuf_error 159.Fa "struct sbuf *s" 160.Fc 161.Ft int 162.Fo sbuf_finish 163.Fa "struct sbuf *s" 164.Fc 165.Ft char * 166.Fo sbuf_data 167.Fa "struct sbuf *s" 168.Fc 169.Ft ssize_t 170.Fo sbuf_len 171.Fa "struct sbuf *s" 172.Fc 173.Ft int 174.Fo sbuf_done 175.Fa "struct sbuf *s" 176.Fc 177.Ft void 178.Fo sbuf_delete 179.Fa "struct sbuf *s" 180.Fc 181.Ft void 182.Fo sbuf_start_section 183.Fa "struct sbuf *s" 184.Fa "ssize_t *old_lenp" 185.Fc 186.Ft ssize_t 187.Fo sbuf_end_section 188.Fa "struct sbuf *s" 189.Fa "ssize_t old_len" 190.Fa "size_t pad" 191.Fa "int c" 192.Fc 193.Ft void 194.Fo sbuf_hexdump 195.Fa "struct sbuf *sb" 196.Fa "void *ptr" 197.Fa "int length" 198.Fa "const char *hdr" 199.Fa "int flags" 200.Fc 201.Ft int 202.Fo sbuf_printf_drain 203.Fa "void *arg" 204.Fa "const char *data" 205.Fa "int len" 206.Fc 207.Ft void 208.Fo sbuf_putbuf 209.Fa "struct sbuf *s" 210.Fc 211.Fd #ifdef _KERNEL 212.In sys/types.h 213.In sys/sbuf.h 214.Ft int 215.Fo sbuf_bcopyin 216.Fa "struct sbuf *s" 217.Fa "const void *uaddr" 218.Fa "size_t len" 219.Fc 220.Ft int 221.Fo sbuf_copyin 222.Fa "struct sbuf *s" 223.Fa "const void *uaddr" 224.Fa "size_t len" 225.Fc 226.In sys/sysctl.h 227.Ft struct sbuf * 228.Fo sbuf_new_for_sysctl 229.Fa "struct sbuf *s" 230.Fa "char *buf" 231.Fa "int length" 232.Fa "struct sysctl_req *req" 233.Fc 234.Fd #endif /* _KERNEL */ 235.Sh DESCRIPTION 236The 237.Nm 238family of functions allows one to safely allocate, compose and 239release strings in kernel or user space. 240.Pp 241Instead of arrays of characters, these functions operate on structures 242called 243.Fa sbufs , 244defined in 245.In sys/sbuf.h . 246.Pp 247Any errors encountered during the allocation or composition of the 248string will be latched in the data structure, 249making a single error test at the end of the composition 250sufficient to determine success or failure of the entire process. 251.Pp 252The 253.Fn sbuf_new 254function initializes the 255.Fa sbuf 256pointed to by its first argument. 257If that pointer is 258.Dv NULL , 259.Fn sbuf_new 260allocates a 261.Vt struct sbuf 262using 263.Xr malloc 9 . 264The 265.Fa buf 266argument is a pointer to a buffer in which to store the actual string; 267if it is 268.Dv NULL , 269.Fn sbuf_new 270will allocate one using 271.Xr malloc 9 . 272The 273.Fa length 274is the initial size of the storage buffer. 275The fourth argument, 276.Fa flags , 277may be comprised of the following flags: 278.Bl -tag -width ".Dv SBUF_AUTOEXTEND" 279.It Dv SBUF_FIXEDLEN 280The storage buffer is fixed at its initial size. 281Attempting to extend the sbuf beyond this size results in an overflow condition. 282.It Dv SBUF_AUTOEXTEND 283This indicates that the storage buffer may be extended as necessary, so long 284as resources allow, to hold additional data. 285.It Dv SBUF_INCLUDENUL 286This causes the final nulterm byte to be counted in the length of the data. 287.It Dv SBUF_DRAINTOEOR 288Treat top-level sections started with 289.Fn sbuf_start_section 290as a record boundary marker that will be used during drain operations to avoid 291records being split. 292If a record grows sufficiently large such that it fills the 293.Fa sbuf 294and therefore cannot be drained without being split, an error of 295.Er EDEADLK 296is set. 297.It Dv SBUF_NOWAIT 298Indicates that attempts to extend the storage buffer should fail in low memory 299conditions, like 300.Xr malloc 9 301.Dv M_NOWAIT . 302.El 303.Pp 304Note that if 305.Fa buf 306is not 307.Dv NULL , 308it must point to an array of at least 309.Fa length 310characters. 311The result of accessing that array directly while it is in use by the 312sbuf is undefined. 313.Pp 314The 315.Fn sbuf_new_auto 316function is a shortcut for creating a completely dynamic 317.Nm . 318It is the equivalent of calling 319.Fn sbuf_new 320with values 321.Dv NULL , 322.Dv NULL , 323.Dv 0 , 324and 325.Dv SBUF_AUTOEXTEND . 326.Pp 327The 328.Fn sbuf_new_for_sysctl 329function will set up an sbuf with a drain function to use 330.Fn SYSCTL_OUT 331when the internal buffer fills. 332Note that if the various functions which append to an sbuf are used while 333a non-sleepable lock is held, the user buffer should be wired using 334.Fn sysctl_wire_old_buffer . 335.Pp 336The 337.Fn sbuf_delete 338function clears the 339.Fa sbuf 340and frees any memory allocated for it. 341There must be a call to 342.Fn sbuf_delete 343for every call to 344.Fn sbuf_new . 345Any attempt to access the sbuf after it has been deleted will fail. 346.Pp 347The 348.Fn sbuf_clear 349function invalidates the contents of the 350.Fa sbuf 351and resets its position to zero. 352.Pp 353The 354.Fn sbuf_get_flags 355function returns the current user flags. 356The 357.Fn sbuf_set_flags 358and 359.Fn sbuf_clear_flags 360functions set or clear one or more user flags, respectively. 361The user flags are described under the 362.Fn sbuf_new 363function. 364.Pp 365The 366.Fn sbuf_setpos 367function sets the 368.Fa sbuf Ns 's 369end position to 370.Fa pos , 371which is a value between zero and the current position in the buffer. 372It can only truncate the sbuf to the new position. 373.Pp 374The 375.Fn sbuf_bcat 376function appends the first 377.Fa len 378bytes from the buffer 379.Fa buf 380to the 381.Fa sbuf . 382.Pp 383The 384.Fn sbuf_bcopyin 385function copies 386.Fa len 387bytes from the specified userland address into the 388.Fa sbuf . 389.Pp 390The 391.Fn sbuf_bcpy 392function replaces the contents of the 393.Fa sbuf 394with the first 395.Fa len 396bytes from the buffer 397.Fa buf . 398.Pp 399The 400.Fn sbuf_cat 401function appends the NUL-terminated string 402.Fa str 403to the 404.Fa sbuf 405at the current position. 406.Pp 407The 408.Fn sbuf_set_drain 409function sets a drain function 410.Fa func 411for the 412.Fa sbuf , 413and records a pointer 414.Fa arg 415to be passed to the drain on callback. 416The drain function cannot be changed while 417.Fa sbuf_len 418is non-zero. 419.Pp 420The registered drain function 421.Vt sbuf_drain_func 422will be called with the argument 423.Fa arg 424provided to 425.Fn sbuf_set_drain , 426a pointer 427.Fa data 428to a byte string that is the contents of the sbuf, and the length 429.Fa len 430of the data. 431If the drain function exists, it will be called when the sbuf internal 432buffer is full, or on behalf of 433.Fn sbuf_finish . 434The drain function may drain some or all of the data, but must drain 435at least 1 byte. 436The return value from the drain function, if positive, indicates how 437many bytes were drained. 438If negative, the return value indicates the negative error code which 439will be returned from this or a later call to 440.Fn sbuf_finish . 441If the returned drained length is 0, an error of 442.Er EDEADLK 443is set. 444To do unbuffered draining, initialize the sbuf with a two-byte buffer. 445The drain will be called for every byte added to the sbuf. 446The 447.Fn sbuf_bcopyin , 448.Fn sbuf_bcpy , 449.Fn sbuf_clear , 450.Fn sbuf_copyin , 451.Fn sbuf_cpy , 452.Fn sbuf_trim , 453.Fn sbuf_data , 454and 455.Fn sbuf_len 456functions cannot be used on an sbuf with a drain. 457.Pp 458The 459.Fn sbuf_copyin 460function copies a NUL-terminated string from the specified userland 461address into the 462.Fa sbuf . 463If the 464.Fa len 465argument is non-zero, no more than 466.Fa len 467characters (not counting the terminating NUL) are copied; otherwise 468the entire string, or as much of it as can fit in the 469.Fa sbuf , 470is copied. 471.Pp 472The 473.Fn sbuf_cpy 474function replaces the contents of the 475.Fa sbuf 476with those of the NUL-terminated string 477.Fa str . 478This is equivalent to calling 479.Fn sbuf_cat 480with a fresh 481.Fa sbuf 482or one which position has been reset to zero with 483.Fn sbuf_clear 484or 485.Fn sbuf_setpos . 486.Pp 487The 488.Fn sbuf_nl_terminate 489function appends a trailing newline character, if the current line is non-empty 490and not already terminated by a newline character. 491.Pp 492The 493.Fn sbuf_printf 494function formats its arguments according to the format string pointed 495to by 496.Fa fmt 497and appends the resulting string to the 498.Fa sbuf 499at the current position. 500.Pp 501The 502.Fn sbuf_vprintf 503function behaves the same as 504.Fn sbuf_printf 505except that the arguments are obtained from the variable-length argument list 506.Fa ap . 507.Pp 508The 509.Fn sbuf_putc 510function appends the character 511.Fa c 512to the 513.Fa sbuf 514at the current position. 515.Pp 516The 517.Fn sbuf_trim 518function removes trailing whitespace from the 519.Fa sbuf . 520.Pp 521The 522.Fn sbuf_error 523function returns any error value that the 524.Fa sbuf 525may have accumulated, either from the drain function, or 526.Er ENOMEM 527if the 528.Fa sbuf 529overflowed. 530This function is generally not needed and instead the error code from 531.Fn sbuf_finish 532is the preferred way to discover whether an sbuf had an error. 533.Pp 534The 535.Fn sbuf_finish 536function will call the attached drain function if one exists until all 537the data in the 538.Fa sbuf 539is flushed. 540If there is no attached drain, 541.Fn sbuf_finish 542NUL-terminates the 543.Fa sbuf . 544In either case it marks the 545.Fa sbuf 546as finished, which means that it may no longer be modified using 547.Fn sbuf_setpos , 548.Fn sbuf_cat , 549.Fn sbuf_cpy , 550.Fn sbuf_printf 551or 552.Fn sbuf_putc , 553until 554.Fn sbuf_clear 555is used to reset the sbuf. 556.Pp 557The 558.Fn sbuf_data 559function returns the actual string; 560.Fn sbuf_data 561only works on a finished 562.Fa sbuf . 563The 564.Fn sbuf_len 565function returns the length of the string. 566For an 567.Fa sbuf 568with an attached drain, 569.Fn sbuf_len 570returns the length of the un-drained data. 571.Fn sbuf_done 572returns non-zero if the 573.Fa sbuf 574is finished. 575.Pp 576The 577.Fn sbuf_start_section 578and 579.Fn sbuf_end_section 580functions may be used for automatic section alignment. 581The arguments 582.Fa pad 583and 584.Fa c 585specify the padding size and a character used for padding. 586The arguments 587.Fa old_lenp 588and 589.Fa old_len 590are to save and restore the current section length when nested sections 591are used. 592For the top level section 593.Dv NULL 594and \-1 can be specified for 595.Fa old_lenp 596and 597.Fa old_len 598respectively. 599.Pp 600The 601.Fn sbuf_hexdump 602function prints an array of bytes to the supplied sbuf, along with an ASCII 603representation of the bytes if possible. 604See the 605.Xr hexdump 3 606man page for more details on the interface. 607.Pp 608The 609.Fn sbuf_printf_drain 610function is a drain function that will call printf, or log to the console. 611The argument 612.Fa arg 613must be either 614.Dv NULL , 615or a valid pointer to a 616.Vt size_t . 617If 618.Fa arg 619is not 620.Dv NULL , 621the total bytes drained will be added to the value pointed to by 622.Fa arg . 623.Pp 624The 625.Fn sbuf_putbuf 626function printfs the sbuf to stdout if in userland, and to the console 627and log if in the kernel. 628The 629.Fa sbuf 630must be finished before calling 631.Fn sbuf_putbuf . 632It does not drain the buffer or update any pointers. 633.Sh NOTES 634If an operation caused an 635.Fa sbuf 636to overflow, most subsequent operations on it will fail until the 637.Fa sbuf 638is finished using 639.Fn sbuf_finish 640or reset using 641.Fn sbuf_clear , 642or its position is reset to a value between 0 and one less than the 643size of its storage buffer using 644.Fn sbuf_setpos , 645or it is reinitialized to a sufficiently short string using 646.Fn sbuf_cpy . 647.Pp 648Drains in user-space will not always function as indicated. 649While the drain function will be called immediately on overflow from 650the 651.Fa sbuf_putc , 652.Fa sbuf_bcat , 653.Fa sbuf_cat 654functions, 655.Fa sbuf_printf 656and 657.Fa sbuf_vprintf 658currently have no way to determine whether there will be an overflow 659until after it occurs, and cannot do a partial expansion of the format 660string. 661Thus when using libsbuf the buffer may be extended to allow completion 662of a single printf call, even though a drain is attached. 663.Sh RETURN VALUES 664The 665.Fn sbuf_new 666function returns 667.Dv NULL 668if it failed to allocate a storage buffer, and a pointer to the new 669.Fa sbuf 670otherwise. 671.Pp 672The 673.Fn sbuf_setpos 674function returns \-1 if 675.Fa pos 676was invalid, and zero otherwise. 677.Pp 678The 679.Fn sbuf_bcat , 680.Fn sbuf_cat , 681.Fn sbuf_cpy , 682.Fn sbuf_printf , 683.Fn sbuf_putc , 684and 685.Fn sbuf_trim 686functions 687all return \-1 if the buffer overflowed, and zero otherwise. 688.Pp 689The 690.Fn sbuf_error 691function returns a non-zero value if the buffer has an overflow or 692drain error, and zero otherwise. 693.Pp 694The 695.Fn sbuf_len 696function returns \-1 if the buffer overflowed. 697.Pp 698The 699.Fn sbuf_copyin 700function 701returns \-1 if copying string from userland failed, and number of bytes 702copied otherwise. 703.Pp 704The 705.Fn sbuf_end_section 706function returns the section length or \-1 if the buffer has an error. 707.Pp 708The 709.Fn sbuf_finish 9 710function (the kernel version) returns 711.Er ENOMEM 712if the sbuf overflowed before being finished, 713or returns the error code from the drain if one is attached. 714.Pp 715The 716.Fn sbuf_finish 3 717function (the userland version) 718will return zero for success and \-1 and set errno on error. 719.Sh EXAMPLES 720.Bd -literal -compact 721#include <sys/types.h> 722#include <sys/sbuf.h> 723 724struct sbuf *sb; 725 726sb = sbuf_new_auto(); 727sbuf_cat(sb, "Customers found:\en"); 728TAILQ_FOREACH(foo, &foolist, list) { 729 sbuf_printf(sb, " %4d %s\en", foo->index, foo->name); 730 sbuf_printf(sb, " Address: %s\en", foo->address); 731 sbuf_printf(sb, " Zip: %s\en", foo->zipcode); 732} 733if (sbuf_finish(sb) != 0) /* Check for any and all errors */ 734 err(1, "Could not generate message"); 735transmit_msg(sbuf_data(sb), sbuf_len(sb)); 736sbuf_delete(sb); 737.Ed 738.Sh SEE ALSO 739.Xr hexdump 3 , 740.Xr printf 3 , 741.Xr strcat 3 , 742.Xr strcpy 3 , 743.Xr copyin 9 , 744.Xr copyinstr 9 , 745.Xr printf 9 746.Sh HISTORY 747The 748.Nm 749family of functions first appeared in 750.Fx 4.4 . 751.Sh AUTHORS 752.An -nosplit 753The 754.Nm 755family of functions was designed by 756.An Poul-Henning Kamp Aq Mt phk@FreeBSD.org 757and implemented by 758.An Dag-Erling Sm\(/orgrav Aq Mt des@FreeBSD.org . 759Additional improvements were suggested by 760.An Justin T. Gibbs Aq Mt gibbs@FreeBSD.org . 761Auto-extend support added by 762.An Kelly Yancey Aq Mt kbyanc@FreeBSD.org . 763Drain functionality added by 764.An Matthew Fleming Aq Mt mdf@FreeBSD.org . 765.Pp 766This manual page was written by 767.An Dag-Erling Sm\(/orgrav Aq Mt des@FreeBSD.org . 768