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