1863edaecSDag-Erling Smørgrav.\"- 26e18fca1SUlrich Spörlein.\" Copyright (c) 2000 Poul-Henning Kamp and Dag-Erling Coïdan Smørgrav 3863edaecSDag-Erling Smørgrav.\" All rights reserved. 4863edaecSDag-Erling Smørgrav.\" 5863edaecSDag-Erling Smørgrav.\" Redistribution and use in source and binary forms, with or without 6863edaecSDag-Erling Smørgrav.\" modification, are permitted provided that the following conditions 7863edaecSDag-Erling Smørgrav.\" are met: 8863edaecSDag-Erling Smørgrav.\" 1. Redistributions of source code must retain the above copyright 9863edaecSDag-Erling Smørgrav.\" notice, this list of conditions and the following disclaimer. 10863edaecSDag-Erling Smørgrav.\" 2. Redistributions in binary form must reproduce the above copyright 11863edaecSDag-Erling Smørgrav.\" notice, this list of conditions and the following disclaimer in the 12863edaecSDag-Erling Smørgrav.\" documentation and/or other materials provided with the distribution. 13863edaecSDag-Erling Smørgrav.\" 14863edaecSDag-Erling Smørgrav.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15863edaecSDag-Erling Smørgrav.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16863edaecSDag-Erling Smørgrav.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17863edaecSDag-Erling Smørgrav.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18863edaecSDag-Erling Smørgrav.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19863edaecSDag-Erling Smørgrav.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20863edaecSDag-Erling Smørgrav.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21863edaecSDag-Erling Smørgrav.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22863edaecSDag-Erling Smørgrav.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23863edaecSDag-Erling Smørgrav.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24863edaecSDag-Erling Smørgrav.\" SUCH DAMAGE. 25863edaecSDag-Erling Smørgrav.\" 26863edaecSDag-Erling Smørgrav.\" $FreeBSD$ 27863edaecSDag-Erling Smørgrav.\" 287beb104dSEnji Cooper.Dd April 5, 2017 295f95f24bSRuslan Ermilov.Dt SBUF 9 303d45e180SRuslan Ermilov.Os 31863edaecSDag-Erling Smørgrav.Sh NAME 3281ae4b8dSRuslan Ermilov.Nm sbuf , 33863edaecSDag-Erling Smørgrav.Nm sbuf_new , 3472fba9d7SChristian Brueffer.Nm sbuf_new_auto , 357c2dd57bSMatthew D Fleming.Nm sbuf_new_for_sysctl , 36e499b74dSDag-Erling Smørgrav.Nm sbuf_clear , 37f4d28142SIan Lepore.Nm sbuf_get_flags , 38f4d28142SIan Lepore.Nm sbuf_set_flags , 39f4d28142SIan Lepore.Nm sbuf_clear_flags , 40863edaecSDag-Erling Smørgrav.Nm sbuf_setpos , 41b0def2b5SDag-Erling Smørgrav.Nm sbuf_bcat , 42b0def2b5SDag-Erling Smørgrav.Nm sbuf_bcopyin , 43b0def2b5SDag-Erling Smørgrav.Nm sbuf_bcpy , 44863edaecSDag-Erling Smørgrav.Nm sbuf_cat , 45b0def2b5SDag-Erling Smørgrav.Nm sbuf_copyin , 46863edaecSDag-Erling Smørgrav.Nm sbuf_cpy , 47863edaecSDag-Erling Smørgrav.Nm sbuf_printf , 48371c0c84SKelly Yancey.Nm sbuf_vprintf , 49863edaecSDag-Erling Smørgrav.Nm sbuf_putc , 504351ba27SMatthew D Fleming.Nm sbuf_set_drain , 51371c0c84SKelly Yancey.Nm sbuf_trim , 524d369413SMatthew D Fleming.Nm sbuf_error , 53863edaecSDag-Erling Smørgrav.Nm sbuf_finish , 54863edaecSDag-Erling Smørgrav.Nm sbuf_data , 55863edaecSDag-Erling Smørgrav.Nm sbuf_len , 56c5f9218bSPoul-Henning Kamp.Nm sbuf_done , 57f02db7b4SMikolaj Golub.Nm sbuf_delete , 58f02db7b4SMikolaj Golub.Nm sbuf_start_section , 595672fac9SKenneth D. Merry.Nm sbuf_end_section , 60388f3ce6SScott Long.Nm sbuf_hexdump , 61388f3ce6SScott Long.Nm sbuf_putbuf 626ab7244aSPoul-Henning Kamp.Nd safe string composition 63863edaecSDag-Erling Smørgrav.Sh SYNOPSIS 6432eef9aeSRuslan Ermilov.In sys/types.h 6532eef9aeSRuslan Ermilov.In sys/sbuf.h 664351ba27SMatthew D Fleming.Ft typedef\ int ( sbuf_drain_func ) ( void\ *arg, const\ char\ *data, int\ len ) ; 674351ba27SMatthew D Fleming.Pp 688a5bc771SBruce Evans.Ft struct sbuf * 69*a6848fd5SEnji Cooper.Fo sbuf_new 70*a6848fd5SEnji Cooper.Fa "struct sbuf *s" 71*a6848fd5SEnji Cooper.Fa "char *buf" 72*a6848fd5SEnji Cooper.Fa "int length" 73*a6848fd5SEnji Cooper.Fa "int flags" 74*a6848fd5SEnji Cooper.Fc 7572fba9d7SChristian Brueffer.Ft struct sbuf * 76*a6848fd5SEnji Cooper.Fo sbuf_new_auto 77*a6848fd5SEnji Cooper.Fa "void" 78*a6848fd5SEnji Cooper.Fc 79e499b74dSDag-Erling Smørgrav.Ft void 80*a6848fd5SEnji Cooper.Fo sbuf_clear 81*a6848fd5SEnji Cooper.Fa "struct sbuf *s" 82*a6848fd5SEnji Cooper.Fc 83863edaecSDag-Erling Smørgrav.Ft int 84*a6848fd5SEnji Cooper.Fo sbuf_get_flags 85*a6848fd5SEnji Cooper.Fa "struct sbuf *s" 86*a6848fd5SEnji Cooper.Fc 87f4d28142SIan Lepore.Ft void 88*a6848fd5SEnji Cooper.Fo sbuf_set_flags 89*a6848fd5SEnji Cooper.Fa "struct sbuf *s" 90*a6848fd5SEnji Cooper.Fa "int flags" 91*a6848fd5SEnji Cooper.Fc 92f4d28142SIan Lepore.Ft void 93*a6848fd5SEnji Cooper.Fo sbuf_clear_flags 94*a6848fd5SEnji Cooper.Fa "struct sbuf *s" 95*a6848fd5SEnji Cooper.Fa "int flags" 96*a6848fd5SEnji Cooper.Fc 97f4d28142SIan Lepore.Ft int 98*a6848fd5SEnji Cooper.Fo sbuf_setpos 99*a6848fd5SEnji Cooper.Fa "struct sbuf *s" 100*a6848fd5SEnji Cooper.Fa "int pos" 101*a6848fd5SEnji Cooper.Fc 102863edaecSDag-Erling Smørgrav.Ft int 103*a6848fd5SEnji Cooper.Fo sbuf_bcat 104*a6848fd5SEnji Cooper.Fa "struct sbuf *s" 105*a6848fd5SEnji Cooper.Fa "const void *buf" 106*a6848fd5SEnji Cooper.Fa "size_t len" 107*a6848fd5SEnji Cooper.Fc 108b0def2b5SDag-Erling Smørgrav.Ft int 109*a6848fd5SEnji Cooper.Fo sbuf_bcopyin 110*a6848fd5SEnji Cooper.Fa "struct sbuf *s" 111*a6848fd5SEnji Cooper.Fa "const void *uaddr" 112*a6848fd5SEnji Cooper.Fa "size_t len" 113*a6848fd5SEnji Cooper.Fc 114b0def2b5SDag-Erling Smørgrav.Ft int 115*a6848fd5SEnji Cooper.Fo sbuf_bcpy 116*a6848fd5SEnji Cooper.Fa "struct sbuf *s" 117*a6848fd5SEnji Cooper.Fa "const void *buf" 118*a6848fd5SEnji Cooper.Fa "size_t len" 119*a6848fd5SEnji Cooper.Fc 120b0def2b5SDag-Erling Smørgrav.Ft int 121*a6848fd5SEnji Cooper.Fo sbuf_cat 122*a6848fd5SEnji Cooper.Fa "struct sbuf *s" 123*a6848fd5SEnji Cooper.Fa "const char *str" 124*a6848fd5SEnji Cooper.Fc 125863edaecSDag-Erling Smørgrav.Ft int 126*a6848fd5SEnji Cooper.Fo sbuf_cpy 127*a6848fd5SEnji Cooper.Fa "struct sbuf *s" 128*a6848fd5SEnji Cooper.Fa "const char *str" 129*a6848fd5SEnji Cooper.Fc 130b0def2b5SDag-Erling Smørgrav.Ft int 131*a6848fd5SEnji Cooper.Fo sbuf_copyin 132*a6848fd5SEnji Cooper.Fa "struct sbuf *s" 133*a6848fd5SEnji Cooper.Fa "const void *uaddr" 134*a6848fd5SEnji Cooper.Fa "size_t len" 135*a6848fd5SEnji Cooper.Fc 136863edaecSDag-Erling Smørgrav.Ft int 137*a6848fd5SEnji Cooper.Fo sbuf_printf 138*a6848fd5SEnji Cooper.Fa "struct sbuf *s" 139*a6848fd5SEnji Cooper.Fa "const char *fmt" "..." 140*a6848fd5SEnji Cooper.Fc 141863edaecSDag-Erling Smørgrav.Ft int 142*a6848fd5SEnji Cooper.Fo sbuf_vprintf 143*a6848fd5SEnji Cooper.Fa "struct sbuf *s" 144*a6848fd5SEnji Cooper.Fa "const char *fmt" 145*a6848fd5SEnji Cooper.Fa "va_list ap" 146*a6848fd5SEnji Cooper.Fc 147371c0c84SKelly Yancey.Ft int 148*a6848fd5SEnji Cooper.Fo sbuf_putc 149*a6848fd5SEnji Cooper.Fa "struct sbuf *s" 150*a6848fd5SEnji Cooper.Fa "int c" 151*a6848fd5SEnji Cooper.Fc 1524351ba27SMatthew D Fleming.Ft void 153*a6848fd5SEnji Cooper.Fo sbuf_set_drain 154*a6848fd5SEnji Cooper.Fa "struct sbuf *s" 155*a6848fd5SEnji Cooper.Fa "sbuf_drain_func *func" 156*a6848fd5SEnji Cooper.Fa "void *arg" 157*a6848fd5SEnji Cooper.Fc 158863edaecSDag-Erling Smørgrav.Ft int 159*a6848fd5SEnji Cooper.Fo sbuf_trim 160*a6848fd5SEnji Cooper.Fa "struct sbuf *s" 161*a6848fd5SEnji Cooper.Fc 162371c0c84SKelly Yancey.Ft int 163*a6848fd5SEnji Cooper.Fo sbuf_error 164*a6848fd5SEnji Cooper.Fa "struct sbuf *s" 165*a6848fd5SEnji Cooper.Fc 1664351ba27SMatthew D Fleming.Ft int 167*a6848fd5SEnji Cooper.Fo sbuf_finish 168*a6848fd5SEnji Cooper.Fa "struct sbuf *s" 169*a6848fd5SEnji Cooper.Fc 170863edaecSDag-Erling Smørgrav.Ft char * 171*a6848fd5SEnji Cooper.Fo sbuf_data 172*a6848fd5SEnji Cooper.Fa "struct sbuf *s" 173*a6848fd5SEnji Cooper.Fc 174b397b9b7SMikolaj Golub.Ft ssize_t 175*a6848fd5SEnji Cooper.Fo sbuf_len 176*a6848fd5SEnji Cooper.Fa "struct sbuf *s" 177*a6848fd5SEnji Cooper.Fc 178c5f9218bSPoul-Henning Kamp.Ft int 179*a6848fd5SEnji Cooper.Fo sbuf_done 180*a6848fd5SEnji Cooper.Fa "struct sbuf *s" 181*a6848fd5SEnji Cooper.Fc 182863edaecSDag-Erling Smørgrav.Ft void 183*a6848fd5SEnji Cooper.Fo sbuf_delete 184*a6848fd5SEnji Cooper.Fa "struct sbuf *s" 185*a6848fd5SEnji Cooper.Fc 186f02db7b4SMikolaj Golub.Ft void 187*a6848fd5SEnji Cooper.Fo sbuf_start_section 188*a6848fd5SEnji Cooper.Fa "struct sbuf *s" 189*a6848fd5SEnji Cooper.Fa "ssize_t *old_lenp" 190*a6848fd5SEnji Cooper.Fc 191f02db7b4SMikolaj Golub.Ft ssize_t 192*a6848fd5SEnji Cooper.Fo sbuf_end_section 193*a6848fd5SEnji Cooper.Fa "struct sbuf *s" 194*a6848fd5SEnji Cooper.Fa "ssize_t old_len" 195*a6848fd5SEnji Cooper.Fa "size_t pad" 196*a6848fd5SEnji Cooper.Fa "int c" 197*a6848fd5SEnji Cooper.Fc 1985672fac9SKenneth D. Merry.Ft void 1995672fac9SKenneth D. Merry.Fo sbuf_hexdump 2005672fac9SKenneth D. Merry.Fa "struct sbuf *sb" 2015672fac9SKenneth D. Merry.Fa "void *ptr" 2025672fac9SKenneth D. Merry.Fa "int length" 2035672fac9SKenneth D. Merry.Fa "const char *hdr" 2045672fac9SKenneth D. Merry.Fa "int flags" 2055672fac9SKenneth D. Merry.Fc 206388f3ce6SScott Long.Ft void 207*a6848fd5SEnji Cooper.Fo sbuf_putbuf 208*a6848fd5SEnji Cooper.Fa "struct sbuf *s" 209*a6848fd5SEnji Cooper.Fc 2107c2dd57bSMatthew D Fleming.In sys/sysctl.h 2117c2dd57bSMatthew D Fleming.Ft struct sbuf * 212*a6848fd5SEnji Cooper.Fo sbuf_new_for_sysctl 213*a6848fd5SEnji Cooper.Fa "struct sbuf *s" 214*a6848fd5SEnji Cooper.Fa "char *buf" 215*a6848fd5SEnji Cooper.Fa "int length" 216*a6848fd5SEnji Cooper.Fa "struct sysctl_req *req" 217*a6848fd5SEnji Cooper.Fc 218863edaecSDag-Erling Smørgrav.Sh DESCRIPTION 219863edaecSDag-Erling SmørgravThe 22081ae4b8dSRuslan Ermilov.Nm 2216ab7244aSPoul-Henning Kampfamily of functions allows one to safely allocate, compose and 2226ab7244aSPoul-Henning Kamprelease strings in kernel or user space. 2236ab7244aSPoul-Henning Kamp.Pp 224863edaecSDag-Erling SmørgravInstead of arrays of characters, these functions operate on structures 225863edaecSDag-Erling Smørgravcalled 226863edaecSDag-Erling Smørgrav.Fa sbufs , 227863edaecSDag-Erling Smørgravdefined in 228fe08efe6SRuslan Ermilov.In sys/sbuf.h . 229863edaecSDag-Erling Smørgrav.Pp 2306ab7244aSPoul-Henning KampAny errors encountered during the allocation or composition of the 2316ab7244aSPoul-Henning Kampstring will be latched in the data structure, 2326ab7244aSPoul-Henning Kampmaking a single error test at the end of the composition 2336ab7244aSPoul-Henning Kampsufficient to determine success or failure of the entire process. 2346ab7244aSPoul-Henning Kamp.Pp 235863edaecSDag-Erling SmørgravThe 236863edaecSDag-Erling Smørgrav.Fn sbuf_new 237863edaecSDag-Erling Smørgravfunction initializes the 238863edaecSDag-Erling Smørgrav.Fa sbuf 239863edaecSDag-Erling Smørgravpointed to by its first argument. 240d6479358SDag-Erling SmørgravIf that pointer is 241d6479358SDag-Erling Smørgrav.Dv NULL , 242d6479358SDag-Erling Smørgrav.Fn sbuf_new 243d6479358SDag-Erling Smørgravallocates a 244d68b700aSRuslan Ermilov.Vt struct sbuf 245d6479358SDag-Erling Smørgravusing 246d6479358SDag-Erling Smørgrav.Xr malloc 9 . 247863edaecSDag-Erling SmørgravThe 248863edaecSDag-Erling Smørgrav.Fa buf 249863edaecSDag-Erling Smørgravargument is a pointer to a buffer in which to store the actual string; 250863edaecSDag-Erling Smørgravif it is 251863edaecSDag-Erling Smørgrav.Dv NULL , 252863edaecSDag-Erling Smørgrav.Fn sbuf_new 253863edaecSDag-Erling Smørgravwill allocate one using 254863edaecSDag-Erling Smørgrav.Xr malloc 9 . 255863edaecSDag-Erling SmørgravThe 256863edaecSDag-Erling Smørgrav.Fa length 257371c0c84SKelly Yanceyis the initial size of the storage buffer. 258863edaecSDag-Erling SmørgravThe fourth argument, 259863edaecSDag-Erling Smørgrav.Fa flags , 260371c0c84SKelly Yanceymay be comprised of the following flags: 2613b0536faSRuslan Ermilov.Bl -tag -width ".Dv SBUF_AUTOEXTEND" 262371c0c84SKelly Yancey.It Dv SBUF_FIXEDLEN 2633b0536faSRuslan ErmilovThe storage buffer is fixed at its initial size. 264371c0c84SKelly YanceyAttempting to extend the sbuf beyond this size results in an overflow condition. 265371c0c84SKelly Yancey.It Dv SBUF_AUTOEXTEND 266371c0c84SKelly YanceyThis indicates that the storage buffer may be extended as necessary, so long 267371c0c84SKelly Yanceyas resources allow, to hold additional data. 268f4d28142SIan Lepore.It Dv SBUF_INCLUDENUL 269f4d28142SIan LeporeThis causes the final nulterm byte to be counted in the length of the data. 270371c0c84SKelly Yancey.El 271863edaecSDag-Erling Smørgrav.Pp 272863edaecSDag-Erling SmørgravNote that if 273863edaecSDag-Erling Smørgrav.Fa buf 274863edaecSDag-Erling Smørgravis not 275863edaecSDag-Erling Smørgrav.Dv NULL , 276863edaecSDag-Erling Smørgravit must point to an array of at least 277863edaecSDag-Erling Smørgrav.Fa length 278863edaecSDag-Erling Smørgravcharacters. 27991c9d24eSDag-Erling SmørgravThe result of accessing that array directly while it is in use by the 28091c9d24eSDag-Erling Smørgravsbuf is undefined. 28191c9d24eSDag-Erling Smørgrav.Pp 28291c9d24eSDag-Erling SmørgravThe 28372fba9d7SChristian Brueffer.Fn sbuf_new_auto 28472fba9d7SChristian Bruefferfunction is a shortcut for creating a completely dynamic 28572fba9d7SChristian Brueffer.Nm . 28672fba9d7SChristian BruefferIt is the equivalent of calling 28772fba9d7SChristian Brueffer.Fn sbuf_new 28872fba9d7SChristian Bruefferwith values 28972fba9d7SChristian Brueffer.Dv NULL , 29072fba9d7SChristian Brueffer.Dv NULL , 29172fba9d7SChristian Brueffer.Dv 0 , 29272fba9d7SChristian Bruefferand 29372fba9d7SChristian Brueffer.Dv SBUF_AUTOEXTEND . 29472fba9d7SChristian Brueffer.Pp 29572fba9d7SChristian BruefferThe 2967c2dd57bSMatthew D Fleming.Fn sbuf_new_for_sysctl 2977c2dd57bSMatthew D Flemingfunction will set up an sbuf with a drain function to use 2987c2dd57bSMatthew D Fleming.Fn SYSCTL_OUT 2997c2dd57bSMatthew D Flemingwhen the internal buffer fills. 30000f0e671SMatthew D FlemingNote that if the various functions which append to an sbuf are used while 30100f0e671SMatthew D Fleminga non-sleepable lock is held, the user buffer should be wired using 30200f0e671SMatthew D Fleming.Fn sysctl_wire_old_buffer . 3037c2dd57bSMatthew D Fleming.Pp 3047c2dd57bSMatthew D FlemingThe 30591c9d24eSDag-Erling Smørgrav.Fn sbuf_delete 30691c9d24eSDag-Erling Smørgravfunction clears the 30791c9d24eSDag-Erling Smørgrav.Fa sbuf 30891c9d24eSDag-Erling Smørgravand frees any memory allocated for it. 30991c9d24eSDag-Erling SmørgravThere must be a call to 31091c9d24eSDag-Erling Smørgrav.Fn sbuf_delete 31191c9d24eSDag-Erling Smørgravfor every call to 31291c9d24eSDag-Erling Smørgrav.Fn sbuf_new . 31391c9d24eSDag-Erling SmørgravAny attempt to access the sbuf after it has been deleted will fail. 314863edaecSDag-Erling Smørgrav.Pp 315863edaecSDag-Erling SmørgravThe 316e499b74dSDag-Erling Smørgrav.Fn sbuf_clear 317e499b74dSDag-Erling Smørgravfunction invalidates the contents of the 318e499b74dSDag-Erling Smørgrav.Fa sbuf 319e499b74dSDag-Erling Smørgravand resets its position to zero. 320e499b74dSDag-Erling Smørgrav.Pp 321e499b74dSDag-Erling SmørgravThe 322f4d28142SIan Lepore.Fn sbuf_get_flags 323f4d28142SIan Leporefunction returns the current user flags. 324f4d28142SIan LeporeThe 325f4d28142SIan Lepore.Fn sbuf_set_flags 326f4d28142SIan Leporeand 327f4d28142SIan Lepore.Fn sbuf_get_flags 328f4d28142SIan Leporefunctions set or clear one or more user flags, respectively. 329f4d28142SIan LeporeThe user flags are described under the 330f4d28142SIan Lepore.Fn sbuf_new 331f4d28142SIan Leporefunction. 332f4d28142SIan Lepore.Pp 333f4d28142SIan LeporeThe 334863edaecSDag-Erling Smørgrav.Fn sbuf_setpos 335863edaecSDag-Erling Smørgravfunction sets the 336863edaecSDag-Erling Smørgrav.Fa sbuf Ns 's 337371c0c84SKelly Yanceyend position to 338863edaecSDag-Erling Smørgrav.Fa pos , 339863edaecSDag-Erling Smørgravwhich is a value between zero and one less than the size of the 340863edaecSDag-Erling Smørgravstorage buffer. 341371c0c84SKelly YanceyThis effectively truncates the sbuf at the new position. 342863edaecSDag-Erling Smørgrav.Pp 343863edaecSDag-Erling SmørgravThe 344b0def2b5SDag-Erling Smørgrav.Fn sbuf_bcat 345b0def2b5SDag-Erling Smørgravfunction appends the first 346b0def2b5SDag-Erling Smørgrav.Fa len 3474aa90de3SDag-Erling Smørgravbytes from the buffer 3484aa90de3SDag-Erling Smørgrav.Fa buf 349b0def2b5SDag-Erling Smørgravto the 350b0def2b5SDag-Erling Smørgrav.Fa sbuf . 351b0def2b5SDag-Erling Smørgrav.Pp 352b0def2b5SDag-Erling SmørgravThe 353b0def2b5SDag-Erling Smørgrav.Fn sbuf_bcopyin 354b0def2b5SDag-Erling Smørgravfunction copies 355b0def2b5SDag-Erling Smørgrav.Fa len 356b0def2b5SDag-Erling Smørgravbytes from the specified userland address into the 357b0def2b5SDag-Erling Smørgrav.Fa sbuf . 358b0def2b5SDag-Erling Smørgrav.Pp 359b0def2b5SDag-Erling SmørgravThe 360b0def2b5SDag-Erling Smørgrav.Fn sbuf_bcpy 361b0def2b5SDag-Erling Smørgravfunction replaces the contents of the 362b0def2b5SDag-Erling Smørgrav.Fa sbuf 363b0def2b5SDag-Erling Smørgravwith the first 364b0def2b5SDag-Erling Smørgrav.Fa len 3654aa90de3SDag-Erling Smørgravbytes from the buffer 3664aa90de3SDag-Erling Smørgrav.Fa buf . 367b0def2b5SDag-Erling Smørgrav.Pp 368b0def2b5SDag-Erling SmørgravThe 369863edaecSDag-Erling Smørgrav.Fn sbuf_cat 370b0def2b5SDag-Erling Smørgravfunction appends the NUL-terminated string 371863edaecSDag-Erling Smørgrav.Fa str 372863edaecSDag-Erling Smørgravto the 373863edaecSDag-Erling Smørgrav.Fa sbuf 374863edaecSDag-Erling Smørgravat the current position. 375863edaecSDag-Erling Smørgrav.Pp 376863edaecSDag-Erling SmørgravThe 3774351ba27SMatthew D Fleming.Fn sbuf_set_drain 3784351ba27SMatthew D Flemingfunction sets a drain function 3794351ba27SMatthew D Fleming.Fa func 3804351ba27SMatthew D Flemingfor the 3814351ba27SMatthew D Fleming.Fa sbuf , 3824351ba27SMatthew D Flemingand records a pointer 3834351ba27SMatthew D Fleming.Fa arg 3844351ba27SMatthew D Flemingto be passed to the drain on callback. 3854351ba27SMatthew D FlemingThe drain function cannot be changed while 3864351ba27SMatthew D Fleming.Fa sbuf_len 3874351ba27SMatthew D Flemingis non-zero. 3884351ba27SMatthew D Fleming.Pp 3894351ba27SMatthew D FlemingThe registered drain function 3904351ba27SMatthew D Fleming.Vt sbuf_drain_func 3914351ba27SMatthew D Flemingwill be called with the argument 3924351ba27SMatthew D Fleming.Fa arg 3934351ba27SMatthew D Flemingprovided to 3944351ba27SMatthew D Fleming.Fn sbuf_set_drain , 3954351ba27SMatthew D Fleminga pointer 3964351ba27SMatthew D Fleming.Fa data 3974351ba27SMatthew D Flemingto a byte string that is the contents of the sbuf, and the length 3984351ba27SMatthew D Fleming.Fa len 3994351ba27SMatthew D Flemingof the data. 4004351ba27SMatthew D FlemingIf the drain function exists, it will be called when the sbuf internal 4014351ba27SMatthew D Flemingbuffer is full, or on behalf of 4024351ba27SMatthew D Fleming.Fn sbuf_finish . 4034351ba27SMatthew D FlemingThe drain function may drain some or all of the data, but must drain 4044351ba27SMatthew D Flemingat least 1 byte. 4054351ba27SMatthew D FlemingThe return value from the drain function, if positive, indicates how 4064351ba27SMatthew D Flemingmany bytes were drained. 4074351ba27SMatthew D FlemingIf negative, the return value indicates the negative error code which 4084351ba27SMatthew D Flemingwill be returned from this or a later call to 4094351ba27SMatthew D Fleming.Fn sbuf_finish . 4104351ba27SMatthew D FlemingThe returned drained length cannot be zero. 4114351ba27SMatthew D FlemingTo do unbuffered draining, initialize the sbuf with a two-byte buffer. 4124351ba27SMatthew D FlemingThe drain will be called for every byte added to the sbuf. 4134351ba27SMatthew D FlemingThe 4144351ba27SMatthew D Fleming.Fn sbuf_bcopyin , 4154351ba27SMatthew D Fleming.Fn sbuf_copyin , 4164351ba27SMatthew D Fleming.Fn sbuf_trim , 4174351ba27SMatthew D Flemingand 4184351ba27SMatthew D Fleming.Fn sbuf_data 4194351ba27SMatthew D Flemingfunctions cannot be used on an sbuf with a drain. 4204351ba27SMatthew D Fleming.Pp 4214351ba27SMatthew D FlemingThe 422b0def2b5SDag-Erling Smørgrav.Fn sbuf_copyin 423b0def2b5SDag-Erling Smørgravfunction copies a NUL-terminated string from the specified userland 424b0def2b5SDag-Erling Smørgravaddress into the 425b0def2b5SDag-Erling Smørgrav.Fa sbuf . 426b0def2b5SDag-Erling SmørgravIf the 427b0def2b5SDag-Erling Smørgrav.Fa len 428b0def2b5SDag-Erling Smørgravargument is non-zero, no more than 429b0def2b5SDag-Erling Smørgrav.Fa len 430b0def2b5SDag-Erling Smørgravcharacters (not counting the terminating NUL) are copied; otherwise 431b0def2b5SDag-Erling Smørgravthe entire string, or as much of it as can fit in the 432b0def2b5SDag-Erling Smørgrav.Fa sbuf , 433b0def2b5SDag-Erling Smørgravis copied. 434b0def2b5SDag-Erling Smørgrav.Pp 435b0def2b5SDag-Erling SmørgravThe 436863edaecSDag-Erling Smørgrav.Fn sbuf_cpy 437863edaecSDag-Erling Smørgravfunction replaces the contents of the 438863edaecSDag-Erling Smørgrav.Fa sbuf 439b0def2b5SDag-Erling Smørgravwith those of the NUL-terminated string 440863edaecSDag-Erling Smørgrav.Fa str . 441863edaecSDag-Erling SmørgravThis is equivalent to calling 442863edaecSDag-Erling Smørgrav.Fn sbuf_cat 443863edaecSDag-Erling Smørgravwith a fresh 444863edaecSDag-Erling Smørgrav.Fa sbuf 445863edaecSDag-Erling Smørgravor one which position has been reset to zero with 446e499b74dSDag-Erling Smørgrav.Fn sbuf_clear 447e499b74dSDag-Erling Smørgravor 448863edaecSDag-Erling Smørgrav.Fn sbuf_setpos . 449863edaecSDag-Erling Smørgrav.Pp 450863edaecSDag-Erling SmørgravThe 451863edaecSDag-Erling Smørgrav.Fn sbuf_printf 452863edaecSDag-Erling Smørgravfunction formats its arguments according to the format string pointed 453863edaecSDag-Erling Smørgravto by 454863edaecSDag-Erling Smørgrav.Fa fmt 455863edaecSDag-Erling Smørgravand appends the resulting string to the 456863edaecSDag-Erling Smørgrav.Fa sbuf 457863edaecSDag-Erling Smørgravat the current position. 458863edaecSDag-Erling Smørgrav.Pp 459863edaecSDag-Erling SmørgravThe 460371c0c84SKelly Yancey.Fn sbuf_vprintf 461371c0c84SKelly Yanceyfunction behaves the same as 462371c0c84SKelly Yancey.Fn sbuf_printf 463371c0c84SKelly Yanceyexcept that the arguments are obtained from the variable-length argument list 464371c0c84SKelly Yancey.Fa ap . 465371c0c84SKelly Yancey.Pp 466371c0c84SKelly YanceyThe 467863edaecSDag-Erling Smørgrav.Fn sbuf_putc 468863edaecSDag-Erling Smørgravfunction appends the character 469863edaecSDag-Erling Smørgrav.Fa c 470863edaecSDag-Erling Smørgravto the 471863edaecSDag-Erling Smørgrav.Fa sbuf 472863edaecSDag-Erling Smørgravat the current position. 473863edaecSDag-Erling Smørgrav.Pp 474863edaecSDag-Erling SmørgravThe 475371c0c84SKelly Yancey.Fn sbuf_trim 476371c0c84SKelly Yanceyfunction removes trailing whitespace from the 477371c0c84SKelly Yancey.Fa sbuf . 478371c0c84SKelly Yancey.Pp 479371c0c84SKelly YanceyThe 4804d369413SMatthew D Fleming.Fn sbuf_error 4814d369413SMatthew D Flemingfunction returns any error value that the 4824d369413SMatthew D Fleming.Fa sbuf 4834d369413SMatthew D Flemingmay have accumulated, either from the drain function, or ENOMEM if the 484e499b74dSDag-Erling Smørgrav.Fa sbuf 485e499b74dSDag-Erling Smørgravoverflowed. 4864d369413SMatthew D FlemingThis function is generally not needed and instead the error code from 4874d369413SMatthew D Fleming.Fn sbuf_finish 4884d369413SMatthew D Flemingis the preferred way to discover whether an sbuf had an error. 489e499b74dSDag-Erling Smørgrav.Pp 490e499b74dSDag-Erling SmørgravThe 491863edaecSDag-Erling Smørgrav.Fn sbuf_finish 4924351ba27SMatthew D Flemingfunction will call the attached drain function if one exists until all 4934351ba27SMatthew D Flemingthe data in the 494863edaecSDag-Erling Smørgrav.Fa sbuf 4954351ba27SMatthew D Flemingis flushed. 4964351ba27SMatthew D FlemingIf there is no attached drain, 4974351ba27SMatthew D Fleming.Fn sbuf_finish 4984351ba27SMatthew D FlemingNUL-terminates the 4994351ba27SMatthew D Fleming.Fa sbuf . 5004351ba27SMatthew D FlemingIn either case it marks the 5014351ba27SMatthew D Fleming.Fa sbuf 5024351ba27SMatthew D Flemingas finished, which means that it may no longer be modified using 503863edaecSDag-Erling Smørgrav.Fn sbuf_setpos , 504863edaecSDag-Erling Smørgrav.Fn sbuf_cat , 5053393f8daSKenneth D. Merry.Fn sbuf_cpy , 506863edaecSDag-Erling Smørgrav.Fn sbuf_printf 507863edaecSDag-Erling Smørgravor 508db960cf0SMatthew D Fleming.Fn sbuf_putc , 509db960cf0SMatthew D Fleminguntil 510db960cf0SMatthew D Fleming.Fn sbuf_clear 511db960cf0SMatthew D Flemingis used to reset the sbuf. 512863edaecSDag-Erling Smørgrav.Pp 513863edaecSDag-Erling SmørgravThe 514863edaecSDag-Erling Smørgrav.Fn sbuf_data 5154351ba27SMatthew D Flemingfunction returns the actual string; 516e499b74dSDag-Erling Smørgrav.Fn sbuf_data 517e499b74dSDag-Erling Smørgravonly works on a finished 518863edaecSDag-Erling Smørgrav.Fa sbuf . 5194351ba27SMatthew D FlemingThe 520c0825cd5SJaakko Heinonen.Fn sbuf_len 521c0825cd5SJaakko Heinonenfunction returns the length of the string. 5224351ba27SMatthew D FlemingFor an 5234351ba27SMatthew D Fleming.Fa sbuf 5244351ba27SMatthew D Flemingwith an attached drain, 5254351ba27SMatthew D Fleming.Fn sbuf_len 5264351ba27SMatthew D Flemingreturns the length of the un-drained data. 5274351ba27SMatthew D Fleming.Fn sbuf_done 5284351ba27SMatthew D Flemingreturns non-zero if the 5294351ba27SMatthew D Fleming.Fa sbuf 5304351ba27SMatthew D Flemingis finished. 531f02db7b4SMikolaj Golub.Pp 532f02db7b4SMikolaj GolubThe 533f02db7b4SMikolaj Golub.Fn sbuf_start_section 534f02db7b4SMikolaj Goluband 535f02db7b4SMikolaj Golub.Fn sbuf_end_section 536f02db7b4SMikolaj Golubfunctions may be used for automatic section alignment. 537f02db7b4SMikolaj GolubThe arguments 538f02db7b4SMikolaj Golub.Fa pad 539f02db7b4SMikolaj Goluband 540f02db7b4SMikolaj Golub.Fa c 541f02db7b4SMikolaj Golubspecify the padding size and a character used for padding. 542f02db7b4SMikolaj GolubThe arguments 543f02db7b4SMikolaj Golub.Fa old_lenp 544f02db7b4SMikolaj Goluband 545f02db7b4SMikolaj Golub.Fa old_len 546f02db7b4SMikolaj Golubare to save and restore the current section length when nested sections 547f02db7b4SMikolaj Golubare used. 548f02db7b4SMikolaj GolubFor the top level section 549f02db7b4SMikolaj Golub.Dv NULL 550f02db7b4SMikolaj Goluband \-1 can be specified for 551f02db7b4SMikolaj Golub.Fa old_lenp 552f02db7b4SMikolaj Goluband 553f02db7b4SMikolaj Golub.Fa old_len 554f02db7b4SMikolaj Golubrespectively. 5555672fac9SKenneth D. Merry.Pp 5565672fac9SKenneth D. MerryThe 5575672fac9SKenneth D. Merry.Fn sbuf_hexdump 5585672fac9SKenneth D. Merryfunction prints an array of bytes to the supplied sbuf, along with an ASCII 5595672fac9SKenneth D. Merryrepresentation of the bytes if possible. 5605672fac9SKenneth D. MerrySee the 5615672fac9SKenneth D. Merry.Xr hexdump 3 5625672fac9SKenneth D. Merryman page for more details on the interface. 563388f3ce6SScott Long.Pp 564388f3ce6SScott LongThe 565388f3ce6SScott Long.Fn sbuf_putbuf 566388f3ce6SScott Longfunction printfs the sbuf to stdout if in userland, and to the console 567388f3ce6SScott Longand log if in the kernel. 568388f3ce6SScott LongIt does not drain the buffer or update any pointers. 569863edaecSDag-Erling Smørgrav.Sh NOTES 570863edaecSDag-Erling SmørgravIf an operation caused an 571863edaecSDag-Erling Smørgrav.Fa sbuf 572e499b74dSDag-Erling Smørgravto overflow, most subsequent operations on it will fail until the 573e499b74dSDag-Erling Smørgrav.Fa sbuf 574e499b74dSDag-Erling Smørgravis finished using 575e499b74dSDag-Erling Smørgrav.Fn sbuf_finish 576e499b74dSDag-Erling Smørgravor reset using 577e499b74dSDag-Erling Smørgrav.Fn sbuf_clear , 578e499b74dSDag-Erling Smørgravor its position is reset to a value between 0 and one less than the 579e499b74dSDag-Erling Smørgravsize of its storage buffer using 580863edaecSDag-Erling Smørgrav.Fn sbuf_setpos , 581863edaecSDag-Erling Smørgravor it is reinitialized to a sufficiently short string using 582863edaecSDag-Erling Smørgrav.Fn sbuf_cpy . 5834351ba27SMatthew D Fleming.Pp 5844351ba27SMatthew D FlemingDrains in user-space will not always function as indicated. 5854351ba27SMatthew D FlemingWhile the drain function will be called immediately on overflow from 5864351ba27SMatthew D Flemingthe 5874351ba27SMatthew D Fleming.Fa sbuf_putc , 5884351ba27SMatthew D Fleming.Fa sbuf_bcat , 5894351ba27SMatthew D Fleming.Fa sbuf_cat 5904351ba27SMatthew D Flemingfunctions, 5914351ba27SMatthew D Fleming.Fa sbuf_printf 5924351ba27SMatthew D Flemingand 5934351ba27SMatthew D Fleming.Fa sbuf_vprintf 5944351ba27SMatthew D Flemingcurrently have no way to determine whether there will be an overflow 5954351ba27SMatthew D Fleminguntil after it occurs, and cannot do a partial expansion of the format 5964351ba27SMatthew D Flemingstring. 5974351ba27SMatthew D FlemingThus when using libsbuf the buffer may be extended to allow completion 5984351ba27SMatthew D Flemingof a single printf call, even though a drain is attached. 599863edaecSDag-Erling Smørgrav.Sh RETURN VALUES 60081ae4b8dSRuslan ErmilovThe 601863edaecSDag-Erling Smørgrav.Fn sbuf_new 60281ae4b8dSRuslan Ermilovfunction returns 603d6479358SDag-Erling Smørgrav.Dv NULL 604d6479358SDag-Erling Smørgravif it failed to allocate a storage buffer, and a pointer to the new 605d6479358SDag-Erling Smørgrav.Fa sbuf 606863edaecSDag-Erling Smørgravotherwise. 607863edaecSDag-Erling Smørgrav.Pp 60881ae4b8dSRuslan ErmilovThe 609863edaecSDag-Erling Smørgrav.Fn sbuf_setpos 61081ae4b8dSRuslan Ermilovfunction returns \-1 if 611863edaecSDag-Erling Smørgrav.Fa pos 612863edaecSDag-Erling Smørgravwas invalid, and zero otherwise. 613863edaecSDag-Erling Smørgrav.Pp 61481ae4b8dSRuslan ErmilovThe 615863edaecSDag-Erling Smørgrav.Fn sbuf_cat , 616863edaecSDag-Erling Smørgrav.Fn sbuf_cpy , 617863edaecSDag-Erling Smørgrav.Fn sbuf_printf , 618371c0c84SKelly Yancey.Fn sbuf_putc , 619863edaecSDag-Erling Smørgravand 620371c0c84SKelly Yancey.Fn sbuf_trim 62181ae4b8dSRuslan Ermilovfunctions 622863edaecSDag-Erling Smørgravall return \-1 if the buffer overflowed, and zero otherwise. 623863edaecSDag-Erling Smørgrav.Pp 62481ae4b8dSRuslan ErmilovThe 6254d369413SMatthew D Fleming.Fn sbuf_error 6264d369413SMatthew D Flemingfunction returns a non-zero value if the buffer has an overflow or 6274d369413SMatthew D Flemingdrain error, and zero otherwise. 628e499b74dSDag-Erling Smørgrav.Pp 62981ae4b8dSRuslan ErmilovThe 630863edaecSDag-Erling Smørgrav.Fn sbuf_len 631569dd0f3SJaakko Heinonenfunction returns \-1 if the buffer overflowed. 63249091c48SPoul-Henning Kamp.Pp 63381ae4b8dSRuslan ErmilovThe 63449091c48SPoul-Henning Kamp.Fn sbuf_copyin 63581ae4b8dSRuslan Ermilovfunction 63649091c48SPoul-Henning Kampreturns \-1 if copying string from userland failed, and number of bytes 63749091c48SPoul-Henning Kampcopied otherwise. 6386ab7244aSPoul-Henning Kamp.Pp 6394351ba27SMatthew D FlemingThe 640f02db7b4SMikolaj Golub.Fn sbuf_end_section 641f02db7b4SMikolaj Golubfunction returns the section length or \-1 if the buffer has an error. 642f02db7b4SMikolaj Golub.Pp 643f02db7b4SMikolaj GolubThe 6446ab7244aSPoul-Henning Kamp.Fn sbuf_finish 9 6456ab7244aSPoul-Henning Kampfunction (the kernel version) returns ENOMEM if the sbuf overflowed before 6466ab7244aSPoul-Henning Kampbeing finished, 6474351ba27SMatthew D Flemingor returns the error code from the drain if one is attached. 6486ab7244aSPoul-Henning Kamp.Pp 6496ab7244aSPoul-Henning KampThe 6506ab7244aSPoul-Henning Kamp.Fn sbuf_finish 3 6516ab7244aSPoul-Henning Kampfunction (the userland version) 6526ab7244aSPoul-Henning Kampwill return zero for success and \-1 and set errno on error. 6536ab7244aSPoul-Henning Kamp.Sh EXAMPLES 6546ab7244aSPoul-Henning Kamp.Bd -literal -compact 6557beb104dSEnji Cooper#include <sys/types.h> 6566ab7244aSPoul-Henning Kamp#include <sys/sbuf.h> 6576ab7244aSPoul-Henning Kamp 6586ab7244aSPoul-Henning Kampstruct sbuf *sb; 6596ab7244aSPoul-Henning Kamp 6606ab7244aSPoul-Henning Kampsb = sbuf_new_auto(); 661c920aa7aSPoul-Henning Kampsbuf_cat(sb, "Customers found:\en"); 6626ab7244aSPoul-Henning KampTAILQ_FOREACH(foo, &foolist, list) { 663c920aa7aSPoul-Henning Kamp sbuf_printf(sb, " %4d %s\en", foo->index, foo->name); 664c920aa7aSPoul-Henning Kamp sbuf_printf(sb, " Address: %s\en", foo->address); 665c920aa7aSPoul-Henning Kamp sbuf_printf(sb, " Zip: %s\en", foo->zipcode); 6666ab7244aSPoul-Henning Kamp} 667c5a0813dSJaakko Heinonenif (sbuf_finish(sb) != 0) /* Check for any and all errors */ 6686ab7244aSPoul-Henning Kamp err(1, "Could not generate message"); 6696ab7244aSPoul-Henning Kamptransmit_msg(sbuf_data(sb), sbuf_len(sb)); 6706ab7244aSPoul-Henning Kampsbuf_delete(sb); 6716ab7244aSPoul-Henning Kamp.Ed 672863edaecSDag-Erling Smørgrav.Sh SEE ALSO 6735672fac9SKenneth D. Merry.Xr hexdump 3 , 674863edaecSDag-Erling Smørgrav.Xr printf 3 , 675863edaecSDag-Erling Smørgrav.Xr strcat 3 , 676d68b700aSRuslan Ermilov.Xr strcpy 3 , 677d68b700aSRuslan Ermilov.Xr copyin 9 , 678e05f3134SAndrew R. Reiter.Xr copyinstr 9 , 679e05f3134SAndrew R. Reiter.Xr printf 9 680863edaecSDag-Erling Smørgrav.Sh HISTORY 681863edaecSDag-Erling SmørgravThe 68281ae4b8dSRuslan Ermilov.Nm 683863edaecSDag-Erling Smørgravfamily of functions first appeared in 68422cbe5e3SDag-Erling Smørgrav.Fx 4.4 . 685863edaecSDag-Erling Smørgrav.Sh AUTHORS 686863edaecSDag-Erling Smørgrav.An -nosplit 687863edaecSDag-Erling SmørgravThe 68881ae4b8dSRuslan Ermilov.Nm 689863edaecSDag-Erling Smørgravfamily of functions was designed by 6908a7314fcSBaptiste Daroussin.An Poul-Henning Kamp Aq Mt phk@FreeBSD.org 691863edaecSDag-Erling Smørgravand implemented by 6928a7314fcSBaptiste Daroussin.An Dag-Erling Sm\(/orgrav Aq Mt des@FreeBSD.org . 693e499b74dSDag-Erling SmørgravAdditional improvements were suggested by 6948a7314fcSBaptiste Daroussin.An Justin T. Gibbs Aq Mt gibbs@FreeBSD.org . 695371c0c84SKelly YanceyAuto-extend support added by 6968a7314fcSBaptiste Daroussin.An Kelly Yancey Aq Mt kbyanc@FreeBSD.org . 6974351ba27SMatthew D FlemingDrain functionality added by 6988a7314fcSBaptiste Daroussin.An Matthew Fleming Aq Mt mdf@FreeBSD.org . 699863edaecSDag-Erling Smørgrav.Pp 700863edaecSDag-Erling SmørgravThis manual page was written by 7018a7314fcSBaptiste Daroussin.An Dag-Erling Sm\(/orgrav Aq Mt des@FreeBSD.org . 702