xref: /freebsd/share/man/man9/sbuf.9 (revision 64223daed4554d08cbfb17b361514ef419bda87b)
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.\"
28*64223daeSMark Johnston.Dd May 23, 2018
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 *
69a6848fd5SEnji Cooper.Fo sbuf_new
70a6848fd5SEnji Cooper.Fa "struct sbuf *s"
71a6848fd5SEnji Cooper.Fa "char *buf"
72a6848fd5SEnji Cooper.Fa "int length"
73a6848fd5SEnji Cooper.Fa "int flags"
74a6848fd5SEnji Cooper.Fc
7572fba9d7SChristian Brueffer.Ft struct sbuf *
76a6848fd5SEnji Cooper.Fo sbuf_new_auto
77a6848fd5SEnji Cooper.Fa "void"
78a6848fd5SEnji Cooper.Fc
79e499b74dSDag-Erling Smørgrav.Ft void
80a6848fd5SEnji Cooper.Fo sbuf_clear
81a6848fd5SEnji Cooper.Fa "struct sbuf *s"
82a6848fd5SEnji Cooper.Fc
83863edaecSDag-Erling Smørgrav.Ft int
84a6848fd5SEnji Cooper.Fo sbuf_get_flags
85a6848fd5SEnji Cooper.Fa "struct sbuf *s"
86a6848fd5SEnji Cooper.Fc
87f4d28142SIan Lepore.Ft void
88a6848fd5SEnji Cooper.Fo sbuf_set_flags
89a6848fd5SEnji Cooper.Fa "struct sbuf *s"
90a6848fd5SEnji Cooper.Fa "int flags"
91a6848fd5SEnji Cooper.Fc
92f4d28142SIan Lepore.Ft void
93a6848fd5SEnji Cooper.Fo sbuf_clear_flags
94a6848fd5SEnji Cooper.Fa "struct sbuf *s"
95a6848fd5SEnji Cooper.Fa "int flags"
96a6848fd5SEnji Cooper.Fc
97f4d28142SIan Lepore.Ft int
98a6848fd5SEnji Cooper.Fo sbuf_setpos
99a6848fd5SEnji Cooper.Fa "struct sbuf *s"
100a6848fd5SEnji Cooper.Fa "int pos"
101a6848fd5SEnji Cooper.Fc
102863edaecSDag-Erling Smørgrav.Ft int
103a6848fd5SEnji Cooper.Fo sbuf_bcat
104a6848fd5SEnji Cooper.Fa "struct sbuf *s"
105a6848fd5SEnji Cooper.Fa "const void *buf"
106a6848fd5SEnji Cooper.Fa "size_t len"
107a6848fd5SEnji Cooper.Fc
108b0def2b5SDag-Erling Smørgrav.Ft int
109a6848fd5SEnji Cooper.Fo sbuf_bcpy
110a6848fd5SEnji Cooper.Fa "struct sbuf *s"
111a6848fd5SEnji Cooper.Fa "const void *buf"
112a6848fd5SEnji Cooper.Fa "size_t len"
113a6848fd5SEnji Cooper.Fc
114b0def2b5SDag-Erling Smørgrav.Ft int
115a6848fd5SEnji Cooper.Fo sbuf_cat
116a6848fd5SEnji Cooper.Fa "struct sbuf *s"
117a6848fd5SEnji Cooper.Fa "const char *str"
118a6848fd5SEnji Cooper.Fc
119863edaecSDag-Erling Smørgrav.Ft int
120a6848fd5SEnji Cooper.Fo sbuf_cpy
121a6848fd5SEnji Cooper.Fa "struct sbuf *s"
122a6848fd5SEnji Cooper.Fa "const char *str"
123a6848fd5SEnji Cooper.Fc
124b0def2b5SDag-Erling Smørgrav.Ft int
125a6848fd5SEnji Cooper.Fo sbuf_printf
126a6848fd5SEnji Cooper.Fa "struct sbuf *s"
127a6848fd5SEnji Cooper.Fa "const char *fmt" "..."
128a6848fd5SEnji Cooper.Fc
129863edaecSDag-Erling Smørgrav.Ft int
130a6848fd5SEnji Cooper.Fo sbuf_vprintf
131a6848fd5SEnji Cooper.Fa "struct sbuf *s"
132a6848fd5SEnji Cooper.Fa "const char *fmt"
133a6848fd5SEnji Cooper.Fa "va_list ap"
134a6848fd5SEnji Cooper.Fc
135371c0c84SKelly Yancey.Ft int
136a6848fd5SEnji Cooper.Fo sbuf_putc
137a6848fd5SEnji Cooper.Fa "struct sbuf *s"
138a6848fd5SEnji Cooper.Fa "int c"
139a6848fd5SEnji Cooper.Fc
1404351ba27SMatthew D Fleming.Ft void
141a6848fd5SEnji Cooper.Fo sbuf_set_drain
142a6848fd5SEnji Cooper.Fa "struct sbuf *s"
143a6848fd5SEnji Cooper.Fa "sbuf_drain_func *func"
144a6848fd5SEnji Cooper.Fa "void *arg"
145a6848fd5SEnji Cooper.Fc
146863edaecSDag-Erling Smørgrav.Ft int
147a6848fd5SEnji Cooper.Fo sbuf_trim
148a6848fd5SEnji Cooper.Fa "struct sbuf *s"
149a6848fd5SEnji Cooper.Fc
150371c0c84SKelly Yancey.Ft int
151a6848fd5SEnji Cooper.Fo sbuf_error
152a6848fd5SEnji Cooper.Fa "struct sbuf *s"
153a6848fd5SEnji Cooper.Fc
1544351ba27SMatthew D Fleming.Ft int
155a6848fd5SEnji Cooper.Fo sbuf_finish
156a6848fd5SEnji Cooper.Fa "struct sbuf *s"
157a6848fd5SEnji Cooper.Fc
158863edaecSDag-Erling Smørgrav.Ft char *
159a6848fd5SEnji Cooper.Fo sbuf_data
160a6848fd5SEnji Cooper.Fa "struct sbuf *s"
161a6848fd5SEnji Cooper.Fc
162b397b9b7SMikolaj Golub.Ft ssize_t
163a6848fd5SEnji Cooper.Fo sbuf_len
164a6848fd5SEnji Cooper.Fa "struct sbuf *s"
165a6848fd5SEnji Cooper.Fc
166c5f9218bSPoul-Henning Kamp.Ft int
167a6848fd5SEnji Cooper.Fo sbuf_done
168a6848fd5SEnji Cooper.Fa "struct sbuf *s"
169a6848fd5SEnji Cooper.Fc
170863edaecSDag-Erling Smørgrav.Ft void
171a6848fd5SEnji Cooper.Fo sbuf_delete
172a6848fd5SEnji Cooper.Fa "struct sbuf *s"
173a6848fd5SEnji Cooper.Fc
174f02db7b4SMikolaj Golub.Ft void
175a6848fd5SEnji Cooper.Fo sbuf_start_section
176a6848fd5SEnji Cooper.Fa "struct sbuf *s"
177a6848fd5SEnji Cooper.Fa "ssize_t *old_lenp"
178a6848fd5SEnji Cooper.Fc
179f02db7b4SMikolaj Golub.Ft ssize_t
180a6848fd5SEnji Cooper.Fo sbuf_end_section
181a6848fd5SEnji Cooper.Fa "struct sbuf *s"
182a6848fd5SEnji Cooper.Fa "ssize_t old_len"
183a6848fd5SEnji Cooper.Fa "size_t pad"
184a6848fd5SEnji Cooper.Fa "int c"
185a6848fd5SEnji Cooper.Fc
1865672fac9SKenneth D. Merry.Ft void
1875672fac9SKenneth D. Merry.Fo sbuf_hexdump
1885672fac9SKenneth D. Merry.Fa "struct sbuf *sb"
1895672fac9SKenneth D. Merry.Fa "void *ptr"
1905672fac9SKenneth D. Merry.Fa "int length"
1915672fac9SKenneth D. Merry.Fa "const char *hdr"
1925672fac9SKenneth D. Merry.Fa "int flags"
1935672fac9SKenneth D. Merry.Fc
194388f3ce6SScott Long.Ft void
195a6848fd5SEnji Cooper.Fo sbuf_putbuf
196a6848fd5SEnji Cooper.Fa "struct sbuf *s"
197a6848fd5SEnji Cooper.Fc
198e90ede1cSEnji Cooper.Fd #ifdef _KERNEL
199e90ede1cSEnji Cooper.In sys/types.h
200e90ede1cSEnji Cooper.In sys/sbuf.h
201e90ede1cSEnji Cooper.Ft int
202e90ede1cSEnji Cooper.Fo sbuf_bcopyin
203e90ede1cSEnji Cooper.Fa "struct sbuf *s"
204e90ede1cSEnji Cooper.Fa "const void *uaddr"
205e90ede1cSEnji Cooper.Fa "size_t len"
206e90ede1cSEnji Cooper.Fc
207e90ede1cSEnji Cooper.Ft int
208e90ede1cSEnji Cooper.Fo sbuf_copyin
209e90ede1cSEnji Cooper.Fa "struct sbuf *s"
210e90ede1cSEnji Cooper.Fa "const void *uaddr"
211e90ede1cSEnji Cooper.Fa "size_t len"
212e90ede1cSEnji Cooper.Fc
2137c2dd57bSMatthew D Fleming.In sys/sysctl.h
2147c2dd57bSMatthew D Fleming.Ft struct sbuf *
215a6848fd5SEnji Cooper.Fo sbuf_new_for_sysctl
216a6848fd5SEnji Cooper.Fa "struct sbuf *s"
217a6848fd5SEnji Cooper.Fa "char *buf"
218a6848fd5SEnji Cooper.Fa "int length"
219a6848fd5SEnji Cooper.Fa "struct sysctl_req *req"
220a6848fd5SEnji Cooper.Fc
221e90ede1cSEnji Cooper.Fd #endif	/* _KERNEL */
222863edaecSDag-Erling Smørgrav.Sh DESCRIPTION
223863edaecSDag-Erling SmørgravThe
22481ae4b8dSRuslan Ermilov.Nm
2256ab7244aSPoul-Henning Kampfamily of functions allows one to safely allocate, compose and
2266ab7244aSPoul-Henning Kamprelease strings in kernel or user space.
2276ab7244aSPoul-Henning Kamp.Pp
228863edaecSDag-Erling SmørgravInstead of arrays of characters, these functions operate on structures
229863edaecSDag-Erling Smørgravcalled
230863edaecSDag-Erling Smørgrav.Fa sbufs ,
231863edaecSDag-Erling Smørgravdefined in
232fe08efe6SRuslan Ermilov.In sys/sbuf.h .
233863edaecSDag-Erling Smørgrav.Pp
2346ab7244aSPoul-Henning KampAny errors encountered during the allocation or composition of the
2356ab7244aSPoul-Henning Kampstring will be latched in the data structure,
2366ab7244aSPoul-Henning Kampmaking a single error test at the end of the composition
2376ab7244aSPoul-Henning Kampsufficient to determine success or failure of the entire process.
2386ab7244aSPoul-Henning Kamp.Pp
239863edaecSDag-Erling SmørgravThe
240863edaecSDag-Erling Smørgrav.Fn sbuf_new
241863edaecSDag-Erling Smørgravfunction initializes the
242863edaecSDag-Erling Smørgrav.Fa sbuf
243863edaecSDag-Erling Smørgravpointed to by its first argument.
244d6479358SDag-Erling SmørgravIf that pointer is
245d6479358SDag-Erling Smørgrav.Dv NULL ,
246d6479358SDag-Erling Smørgrav.Fn sbuf_new
247d6479358SDag-Erling Smørgravallocates a
248d68b700aSRuslan Ermilov.Vt struct sbuf
249d6479358SDag-Erling Smørgravusing
250d6479358SDag-Erling Smørgrav.Xr malloc 9 .
251863edaecSDag-Erling SmørgravThe
252863edaecSDag-Erling Smørgrav.Fa buf
253863edaecSDag-Erling Smørgravargument is a pointer to a buffer in which to store the actual string;
254863edaecSDag-Erling Smørgravif it is
255863edaecSDag-Erling Smørgrav.Dv NULL ,
256863edaecSDag-Erling Smørgrav.Fn sbuf_new
257863edaecSDag-Erling Smørgravwill allocate one using
258863edaecSDag-Erling Smørgrav.Xr malloc 9 .
259863edaecSDag-Erling SmørgravThe
260863edaecSDag-Erling Smørgrav.Fa length
261371c0c84SKelly Yanceyis the initial size of the storage buffer.
262863edaecSDag-Erling SmørgravThe fourth argument,
263863edaecSDag-Erling Smørgrav.Fa flags ,
264371c0c84SKelly Yanceymay be comprised of the following flags:
2653b0536faSRuslan Ermilov.Bl -tag -width ".Dv SBUF_AUTOEXTEND"
266371c0c84SKelly Yancey.It Dv SBUF_FIXEDLEN
2673b0536faSRuslan ErmilovThe storage buffer is fixed at its initial size.
268371c0c84SKelly YanceyAttempting to extend the sbuf beyond this size results in an overflow condition.
269371c0c84SKelly Yancey.It Dv SBUF_AUTOEXTEND
270371c0c84SKelly YanceyThis indicates that the storage buffer may be extended as necessary, so long
271371c0c84SKelly Yanceyas resources allow, to hold additional data.
272f4d28142SIan Lepore.It Dv SBUF_INCLUDENUL
273f4d28142SIan LeporeThis causes the final nulterm byte to be counted in the length of the data.
274a8ec96afSLawrence Stewart.It Dv SBUF_DRAINTOEOR
275a8ec96afSLawrence StewartTreat top-level sections started with
276a8ec96afSLawrence Stewart.Fn sbuf_start_section
277a8ec96afSLawrence Stewartas a record boundary marker that will be used during drain operations to avoid
278a8ec96afSLawrence Stewartrecords being split.
279a8ec96afSLawrence StewartIf a record grows sufficiently large such that it fills the
280a8ec96afSLawrence Stewart.Fa sbuf
281a8ec96afSLawrence Stewartand therefore cannot be drained without being split, an error of EDEADLK is set.
282371c0c84SKelly Yancey.El
283863edaecSDag-Erling Smørgrav.Pp
284863edaecSDag-Erling SmørgravNote that if
285863edaecSDag-Erling Smørgrav.Fa buf
286863edaecSDag-Erling Smørgravis not
287863edaecSDag-Erling Smørgrav.Dv NULL ,
288863edaecSDag-Erling Smørgravit must point to an array of at least
289863edaecSDag-Erling Smørgrav.Fa length
290863edaecSDag-Erling Smørgravcharacters.
29191c9d24eSDag-Erling SmørgravThe result of accessing that array directly while it is in use by the
29291c9d24eSDag-Erling Smørgravsbuf is undefined.
29391c9d24eSDag-Erling Smørgrav.Pp
29491c9d24eSDag-Erling SmørgravThe
29572fba9d7SChristian Brueffer.Fn sbuf_new_auto
29672fba9d7SChristian Bruefferfunction is a shortcut for creating a completely dynamic
29772fba9d7SChristian Brueffer.Nm .
29872fba9d7SChristian BruefferIt is the equivalent of calling
29972fba9d7SChristian Brueffer.Fn sbuf_new
30072fba9d7SChristian Bruefferwith values
30172fba9d7SChristian Brueffer.Dv NULL ,
30272fba9d7SChristian Brueffer.Dv NULL ,
30372fba9d7SChristian Brueffer.Dv 0 ,
30472fba9d7SChristian Bruefferand
30572fba9d7SChristian Brueffer.Dv SBUF_AUTOEXTEND .
30672fba9d7SChristian Brueffer.Pp
30772fba9d7SChristian BruefferThe
3087c2dd57bSMatthew D Fleming.Fn sbuf_new_for_sysctl
3097c2dd57bSMatthew D Flemingfunction will set up an sbuf with a drain function to use
3107c2dd57bSMatthew D Fleming.Fn SYSCTL_OUT
3117c2dd57bSMatthew D Flemingwhen the internal buffer fills.
31200f0e671SMatthew D FlemingNote that if the various functions which append to an sbuf are used while
31300f0e671SMatthew D Fleminga non-sleepable lock is held, the user buffer should be wired using
31400f0e671SMatthew D Fleming.Fn sysctl_wire_old_buffer .
3157c2dd57bSMatthew D Fleming.Pp
3167c2dd57bSMatthew D FlemingThe
31791c9d24eSDag-Erling Smørgrav.Fn sbuf_delete
31891c9d24eSDag-Erling Smørgravfunction clears the
31991c9d24eSDag-Erling Smørgrav.Fa sbuf
32091c9d24eSDag-Erling Smørgravand frees any memory allocated for it.
32191c9d24eSDag-Erling SmørgravThere must be a call to
32291c9d24eSDag-Erling Smørgrav.Fn sbuf_delete
32391c9d24eSDag-Erling Smørgravfor every call to
32491c9d24eSDag-Erling Smørgrav.Fn sbuf_new .
32591c9d24eSDag-Erling SmørgravAny attempt to access the sbuf after it has been deleted will fail.
326863edaecSDag-Erling Smørgrav.Pp
327863edaecSDag-Erling SmørgravThe
328e499b74dSDag-Erling Smørgrav.Fn sbuf_clear
329e499b74dSDag-Erling Smørgravfunction invalidates the contents of the
330e499b74dSDag-Erling Smørgrav.Fa sbuf
331e499b74dSDag-Erling Smørgravand resets its position to zero.
332e499b74dSDag-Erling Smørgrav.Pp
333e499b74dSDag-Erling SmørgravThe
334f4d28142SIan Lepore.Fn sbuf_get_flags
335f4d28142SIan Leporefunction returns the current user flags.
336f4d28142SIan LeporeThe
337f4d28142SIan Lepore.Fn sbuf_set_flags
338f4d28142SIan Leporeand
339f4d28142SIan Lepore.Fn sbuf_get_flags
340f4d28142SIan Leporefunctions set or clear one or more user flags, respectively.
341f4d28142SIan LeporeThe user flags are described under the
342f4d28142SIan Lepore.Fn sbuf_new
343f4d28142SIan Leporefunction.
344f4d28142SIan Lepore.Pp
345f4d28142SIan LeporeThe
346863edaecSDag-Erling Smørgrav.Fn sbuf_setpos
347863edaecSDag-Erling Smørgravfunction sets the
348863edaecSDag-Erling Smørgrav.Fa sbuf Ns 's
349371c0c84SKelly Yanceyend position to
350863edaecSDag-Erling Smørgrav.Fa pos ,
351863edaecSDag-Erling Smørgravwhich is a value between zero and one less than the size of the
352863edaecSDag-Erling Smørgravstorage buffer.
353371c0c84SKelly YanceyThis effectively truncates the sbuf at the new position.
354863edaecSDag-Erling Smørgrav.Pp
355863edaecSDag-Erling SmørgravThe
356b0def2b5SDag-Erling Smørgrav.Fn sbuf_bcat
357b0def2b5SDag-Erling Smørgravfunction appends the first
358b0def2b5SDag-Erling Smørgrav.Fa len
3594aa90de3SDag-Erling Smørgravbytes from the buffer
3604aa90de3SDag-Erling Smørgrav.Fa buf
361b0def2b5SDag-Erling Smørgravto the
362b0def2b5SDag-Erling Smørgrav.Fa sbuf .
363b0def2b5SDag-Erling Smørgrav.Pp
364b0def2b5SDag-Erling SmørgravThe
365b0def2b5SDag-Erling Smørgrav.Fn sbuf_bcopyin
366b0def2b5SDag-Erling Smørgravfunction copies
367b0def2b5SDag-Erling Smørgrav.Fa len
368b0def2b5SDag-Erling Smørgravbytes from the specified userland address into the
369b0def2b5SDag-Erling Smørgrav.Fa sbuf .
370b0def2b5SDag-Erling Smørgrav.Pp
371b0def2b5SDag-Erling SmørgravThe
372b0def2b5SDag-Erling Smørgrav.Fn sbuf_bcpy
373b0def2b5SDag-Erling Smørgravfunction replaces the contents of the
374b0def2b5SDag-Erling Smørgrav.Fa sbuf
375b0def2b5SDag-Erling Smørgravwith the first
376b0def2b5SDag-Erling Smørgrav.Fa len
3774aa90de3SDag-Erling Smørgravbytes from the buffer
3784aa90de3SDag-Erling Smørgrav.Fa buf .
379b0def2b5SDag-Erling Smørgrav.Pp
380b0def2b5SDag-Erling SmørgravThe
381863edaecSDag-Erling Smørgrav.Fn sbuf_cat
382b0def2b5SDag-Erling Smørgravfunction appends the NUL-terminated string
383863edaecSDag-Erling Smørgrav.Fa str
384863edaecSDag-Erling Smørgravto the
385863edaecSDag-Erling Smørgrav.Fa sbuf
386863edaecSDag-Erling Smørgravat the current position.
387863edaecSDag-Erling Smørgrav.Pp
388863edaecSDag-Erling SmørgravThe
3894351ba27SMatthew D Fleming.Fn sbuf_set_drain
3904351ba27SMatthew D Flemingfunction sets a drain function
3914351ba27SMatthew D Fleming.Fa func
3924351ba27SMatthew D Flemingfor the
3934351ba27SMatthew D Fleming.Fa sbuf ,
3944351ba27SMatthew D Flemingand records a pointer
3954351ba27SMatthew D Fleming.Fa arg
3964351ba27SMatthew D Flemingto be passed to the drain on callback.
3974351ba27SMatthew D FlemingThe drain function cannot be changed while
3984351ba27SMatthew D Fleming.Fa sbuf_len
3994351ba27SMatthew D Flemingis non-zero.
4004351ba27SMatthew D Fleming.Pp
4014351ba27SMatthew D FlemingThe registered drain function
4024351ba27SMatthew D Fleming.Vt sbuf_drain_func
4034351ba27SMatthew D Flemingwill be called with the argument
4044351ba27SMatthew D Fleming.Fa arg
4054351ba27SMatthew D Flemingprovided to
4064351ba27SMatthew D Fleming.Fn sbuf_set_drain ,
4074351ba27SMatthew D Fleminga pointer
4084351ba27SMatthew D Fleming.Fa data
4094351ba27SMatthew D Flemingto a byte string that is the contents of the sbuf, and the length
4104351ba27SMatthew D Fleming.Fa len
4114351ba27SMatthew D Flemingof the data.
4124351ba27SMatthew D FlemingIf the drain function exists, it will be called when the sbuf internal
4134351ba27SMatthew D Flemingbuffer is full, or on behalf of
4144351ba27SMatthew D Fleming.Fn sbuf_finish .
4154351ba27SMatthew D FlemingThe drain function may drain some or all of the data, but must drain
4164351ba27SMatthew D Flemingat least 1 byte.
4174351ba27SMatthew D FlemingThe return value from the drain function, if positive, indicates how
4184351ba27SMatthew D Flemingmany bytes were drained.
4194351ba27SMatthew D FlemingIf negative, the return value indicates the negative error code which
4204351ba27SMatthew D Flemingwill be returned from this or a later call to
4214351ba27SMatthew D Fleming.Fn sbuf_finish .
4224351ba27SMatthew D FlemingThe returned drained length cannot be zero.
4234351ba27SMatthew D FlemingTo do unbuffered draining, initialize the sbuf with a two-byte buffer.
4244351ba27SMatthew D FlemingThe drain will be called for every byte added to the sbuf.
4254351ba27SMatthew D FlemingThe
4264351ba27SMatthew D Fleming.Fn sbuf_bcopyin ,
4274351ba27SMatthew D Fleming.Fn sbuf_copyin ,
4284351ba27SMatthew D Fleming.Fn sbuf_trim ,
4294351ba27SMatthew D Flemingand
4304351ba27SMatthew D Fleming.Fn sbuf_data
4314351ba27SMatthew D Flemingfunctions cannot be used on an sbuf with a drain.
4324351ba27SMatthew D Fleming.Pp
4334351ba27SMatthew D FlemingThe
434b0def2b5SDag-Erling Smørgrav.Fn sbuf_copyin
435b0def2b5SDag-Erling Smørgravfunction copies a NUL-terminated string from the specified userland
436b0def2b5SDag-Erling Smørgravaddress into the
437b0def2b5SDag-Erling Smørgrav.Fa sbuf .
438b0def2b5SDag-Erling SmørgravIf the
439b0def2b5SDag-Erling Smørgrav.Fa len
440b0def2b5SDag-Erling Smørgravargument is non-zero, no more than
441b0def2b5SDag-Erling Smørgrav.Fa len
442b0def2b5SDag-Erling Smørgravcharacters (not counting the terminating NUL) are copied; otherwise
443b0def2b5SDag-Erling Smørgravthe entire string, or as much of it as can fit in the
444b0def2b5SDag-Erling Smørgrav.Fa sbuf ,
445b0def2b5SDag-Erling Smørgravis copied.
446b0def2b5SDag-Erling Smørgrav.Pp
447b0def2b5SDag-Erling SmørgravThe
448863edaecSDag-Erling Smørgrav.Fn sbuf_cpy
449863edaecSDag-Erling Smørgravfunction replaces the contents of the
450863edaecSDag-Erling Smørgrav.Fa sbuf
451b0def2b5SDag-Erling Smørgravwith those of the NUL-terminated string
452863edaecSDag-Erling Smørgrav.Fa str .
453863edaecSDag-Erling SmørgravThis is equivalent to calling
454863edaecSDag-Erling Smørgrav.Fn sbuf_cat
455863edaecSDag-Erling Smørgravwith a fresh
456863edaecSDag-Erling Smørgrav.Fa sbuf
457863edaecSDag-Erling Smørgravor one which position has been reset to zero with
458e499b74dSDag-Erling Smørgrav.Fn sbuf_clear
459e499b74dSDag-Erling Smørgravor
460863edaecSDag-Erling Smørgrav.Fn sbuf_setpos .
461863edaecSDag-Erling Smørgrav.Pp
462863edaecSDag-Erling SmørgravThe
463863edaecSDag-Erling Smørgrav.Fn sbuf_printf
464863edaecSDag-Erling Smørgravfunction formats its arguments according to the format string pointed
465863edaecSDag-Erling Smørgravto by
466863edaecSDag-Erling Smørgrav.Fa fmt
467863edaecSDag-Erling Smørgravand appends the resulting string to the
468863edaecSDag-Erling Smørgrav.Fa sbuf
469863edaecSDag-Erling Smørgravat the current position.
470863edaecSDag-Erling Smørgrav.Pp
471863edaecSDag-Erling SmørgravThe
472371c0c84SKelly Yancey.Fn sbuf_vprintf
473371c0c84SKelly Yanceyfunction behaves the same as
474371c0c84SKelly Yancey.Fn sbuf_printf
475371c0c84SKelly Yanceyexcept that the arguments are obtained from the variable-length argument list
476371c0c84SKelly Yancey.Fa ap .
477371c0c84SKelly Yancey.Pp
478371c0c84SKelly YanceyThe
479863edaecSDag-Erling Smørgrav.Fn sbuf_putc
480863edaecSDag-Erling Smørgravfunction appends the character
481863edaecSDag-Erling Smørgrav.Fa c
482863edaecSDag-Erling Smørgravto the
483863edaecSDag-Erling Smørgrav.Fa sbuf
484863edaecSDag-Erling Smørgravat the current position.
485863edaecSDag-Erling Smørgrav.Pp
486863edaecSDag-Erling SmørgravThe
487371c0c84SKelly Yancey.Fn sbuf_trim
488371c0c84SKelly Yanceyfunction removes trailing whitespace from the
489371c0c84SKelly Yancey.Fa sbuf .
490371c0c84SKelly Yancey.Pp
491371c0c84SKelly YanceyThe
4924d369413SMatthew D Fleming.Fn sbuf_error
4934d369413SMatthew D Flemingfunction returns any error value that the
4944d369413SMatthew D Fleming.Fa sbuf
4954d369413SMatthew D Flemingmay have accumulated, either from the drain function, or ENOMEM if the
496e499b74dSDag-Erling Smørgrav.Fa sbuf
497e499b74dSDag-Erling Smørgravoverflowed.
4984d369413SMatthew D FlemingThis function is generally not needed and instead the error code from
4994d369413SMatthew D Fleming.Fn sbuf_finish
5004d369413SMatthew D Flemingis the preferred way to discover whether an sbuf had an error.
501e499b74dSDag-Erling Smørgrav.Pp
502e499b74dSDag-Erling SmørgravThe
503863edaecSDag-Erling Smørgrav.Fn sbuf_finish
5044351ba27SMatthew D Flemingfunction will call the attached drain function if one exists until all
5054351ba27SMatthew D Flemingthe data in the
506863edaecSDag-Erling Smørgrav.Fa sbuf
5074351ba27SMatthew D Flemingis flushed.
5084351ba27SMatthew D FlemingIf there is no attached drain,
5094351ba27SMatthew D Fleming.Fn sbuf_finish
5104351ba27SMatthew D FlemingNUL-terminates the
5114351ba27SMatthew D Fleming.Fa sbuf .
5124351ba27SMatthew D FlemingIn either case it marks the
5134351ba27SMatthew D Fleming.Fa sbuf
5144351ba27SMatthew D Flemingas finished, which means that it may no longer be modified using
515863edaecSDag-Erling Smørgrav.Fn sbuf_setpos ,
516863edaecSDag-Erling Smørgrav.Fn sbuf_cat ,
5173393f8daSKenneth D. Merry.Fn sbuf_cpy ,
518863edaecSDag-Erling Smørgrav.Fn sbuf_printf
519863edaecSDag-Erling Smørgravor
520db960cf0SMatthew D Fleming.Fn sbuf_putc ,
521db960cf0SMatthew D Fleminguntil
522db960cf0SMatthew D Fleming.Fn sbuf_clear
523db960cf0SMatthew D Flemingis used to reset the sbuf.
524863edaecSDag-Erling Smørgrav.Pp
525863edaecSDag-Erling SmørgravThe
526863edaecSDag-Erling Smørgrav.Fn sbuf_data
5274351ba27SMatthew D Flemingfunction returns the actual string;
528e499b74dSDag-Erling Smørgrav.Fn sbuf_data
529e499b74dSDag-Erling Smørgravonly works on a finished
530863edaecSDag-Erling Smørgrav.Fa sbuf .
5314351ba27SMatthew D FlemingThe
532c0825cd5SJaakko Heinonen.Fn sbuf_len
533c0825cd5SJaakko Heinonenfunction returns the length of the string.
5344351ba27SMatthew D FlemingFor an
5354351ba27SMatthew D Fleming.Fa sbuf
5364351ba27SMatthew D Flemingwith an attached drain,
5374351ba27SMatthew D Fleming.Fn sbuf_len
5384351ba27SMatthew D Flemingreturns the length of the un-drained data.
5394351ba27SMatthew D Fleming.Fn sbuf_done
5404351ba27SMatthew D Flemingreturns non-zero if the
5414351ba27SMatthew D Fleming.Fa sbuf
5424351ba27SMatthew D Flemingis finished.
543f02db7b4SMikolaj Golub.Pp
544f02db7b4SMikolaj GolubThe
545f02db7b4SMikolaj Golub.Fn sbuf_start_section
546f02db7b4SMikolaj Goluband
547f02db7b4SMikolaj Golub.Fn sbuf_end_section
548f02db7b4SMikolaj Golubfunctions may be used for automatic section alignment.
549f02db7b4SMikolaj GolubThe arguments
550f02db7b4SMikolaj Golub.Fa pad
551f02db7b4SMikolaj Goluband
552f02db7b4SMikolaj Golub.Fa c
553f02db7b4SMikolaj Golubspecify the padding size and a character used for padding.
554f02db7b4SMikolaj GolubThe arguments
555f02db7b4SMikolaj Golub.Fa old_lenp
556f02db7b4SMikolaj Goluband
557f02db7b4SMikolaj Golub.Fa old_len
558f02db7b4SMikolaj Golubare to save and restore the current section length when nested sections
559f02db7b4SMikolaj Golubare used.
560f02db7b4SMikolaj GolubFor the top level section
561f02db7b4SMikolaj Golub.Dv NULL
562f02db7b4SMikolaj Goluband \-1 can be specified for
563f02db7b4SMikolaj Golub.Fa old_lenp
564f02db7b4SMikolaj Goluband
565f02db7b4SMikolaj Golub.Fa old_len
566f02db7b4SMikolaj Golubrespectively.
5675672fac9SKenneth D. Merry.Pp
5685672fac9SKenneth D. MerryThe
5695672fac9SKenneth D. Merry.Fn sbuf_hexdump
5705672fac9SKenneth D. Merryfunction prints an array of bytes to the supplied sbuf, along with an ASCII
5715672fac9SKenneth D. Merryrepresentation of the bytes if possible.
5725672fac9SKenneth D. MerrySee the
5735672fac9SKenneth D. Merry.Xr hexdump 3
5745672fac9SKenneth D. Merryman page for more details on the interface.
575388f3ce6SScott Long.Pp
576388f3ce6SScott LongThe
577388f3ce6SScott Long.Fn sbuf_putbuf
578388f3ce6SScott Longfunction printfs the sbuf to stdout if in userland, and to the console
579388f3ce6SScott Longand log if in the kernel.
580388f3ce6SScott LongIt does not drain the buffer or update any pointers.
581863edaecSDag-Erling Smørgrav.Sh NOTES
582863edaecSDag-Erling SmørgravIf an operation caused an
583863edaecSDag-Erling Smørgrav.Fa sbuf
584e499b74dSDag-Erling Smørgravto overflow, most subsequent operations on it will fail until the
585e499b74dSDag-Erling Smørgrav.Fa sbuf
586e499b74dSDag-Erling Smørgravis finished using
587e499b74dSDag-Erling Smørgrav.Fn sbuf_finish
588e499b74dSDag-Erling Smørgravor reset using
589e499b74dSDag-Erling Smørgrav.Fn sbuf_clear ,
590e499b74dSDag-Erling Smørgravor its position is reset to a value between 0 and one less than the
591e499b74dSDag-Erling Smørgravsize of its storage buffer using
592863edaecSDag-Erling Smørgrav.Fn sbuf_setpos ,
593863edaecSDag-Erling Smørgravor it is reinitialized to a sufficiently short string using
594863edaecSDag-Erling Smørgrav.Fn sbuf_cpy .
5954351ba27SMatthew D Fleming.Pp
5964351ba27SMatthew D FlemingDrains in user-space will not always function as indicated.
5974351ba27SMatthew D FlemingWhile the drain function will be called immediately on overflow from
5984351ba27SMatthew D Flemingthe
5994351ba27SMatthew D Fleming.Fa sbuf_putc ,
6004351ba27SMatthew D Fleming.Fa sbuf_bcat ,
6014351ba27SMatthew D Fleming.Fa sbuf_cat
6024351ba27SMatthew D Flemingfunctions,
6034351ba27SMatthew D Fleming.Fa sbuf_printf
6044351ba27SMatthew D Flemingand
6054351ba27SMatthew D Fleming.Fa sbuf_vprintf
6064351ba27SMatthew D Flemingcurrently have no way to determine whether there will be an overflow
6074351ba27SMatthew D Fleminguntil after it occurs, and cannot do a partial expansion of the format
6084351ba27SMatthew D Flemingstring.
6094351ba27SMatthew D FlemingThus when using libsbuf the buffer may be extended to allow completion
6104351ba27SMatthew D Flemingof a single printf call, even though a drain is attached.
611863edaecSDag-Erling Smørgrav.Sh RETURN VALUES
61281ae4b8dSRuslan ErmilovThe
613863edaecSDag-Erling Smørgrav.Fn sbuf_new
61481ae4b8dSRuslan Ermilovfunction returns
615d6479358SDag-Erling Smørgrav.Dv NULL
616d6479358SDag-Erling Smørgravif it failed to allocate a storage buffer, and a pointer to the new
617d6479358SDag-Erling Smørgrav.Fa sbuf
618863edaecSDag-Erling Smørgravotherwise.
619863edaecSDag-Erling Smørgrav.Pp
62081ae4b8dSRuslan ErmilovThe
621863edaecSDag-Erling Smørgrav.Fn sbuf_setpos
62281ae4b8dSRuslan Ermilovfunction returns \-1 if
623863edaecSDag-Erling Smørgrav.Fa pos
624863edaecSDag-Erling Smørgravwas invalid, and zero otherwise.
625863edaecSDag-Erling Smørgrav.Pp
62681ae4b8dSRuslan ErmilovThe
627*64223daeSMark Johnston.Fn sbuf_bcat ,
628863edaecSDag-Erling Smørgrav.Fn sbuf_cat ,
629863edaecSDag-Erling Smørgrav.Fn sbuf_cpy ,
630863edaecSDag-Erling Smørgrav.Fn sbuf_printf ,
631371c0c84SKelly Yancey.Fn sbuf_putc ,
632863edaecSDag-Erling Smørgravand
633371c0c84SKelly Yancey.Fn sbuf_trim
63481ae4b8dSRuslan Ermilovfunctions
635863edaecSDag-Erling Smørgravall return \-1 if the buffer overflowed, and zero otherwise.
636863edaecSDag-Erling Smørgrav.Pp
63781ae4b8dSRuslan ErmilovThe
6384d369413SMatthew D Fleming.Fn sbuf_error
6394d369413SMatthew D Flemingfunction returns a non-zero value if the buffer has an overflow or
6404d369413SMatthew D Flemingdrain error, and zero otherwise.
641e499b74dSDag-Erling Smørgrav.Pp
64281ae4b8dSRuslan ErmilovThe
643863edaecSDag-Erling Smørgrav.Fn sbuf_len
644569dd0f3SJaakko Heinonenfunction returns \-1 if the buffer overflowed.
64549091c48SPoul-Henning Kamp.Pp
64681ae4b8dSRuslan ErmilovThe
64749091c48SPoul-Henning Kamp.Fn sbuf_copyin
64881ae4b8dSRuslan Ermilovfunction
64949091c48SPoul-Henning Kampreturns \-1 if copying string from userland failed, and number of bytes
65049091c48SPoul-Henning Kampcopied otherwise.
6516ab7244aSPoul-Henning Kamp.Pp
6524351ba27SMatthew D FlemingThe
653f02db7b4SMikolaj Golub.Fn sbuf_end_section
654f02db7b4SMikolaj Golubfunction returns the section length or \-1 if the buffer has an error.
655f02db7b4SMikolaj Golub.Pp
656f02db7b4SMikolaj GolubThe
6576ab7244aSPoul-Henning Kamp.Fn sbuf_finish 9
6586ab7244aSPoul-Henning Kampfunction (the kernel version) returns ENOMEM if the sbuf overflowed before
6596ab7244aSPoul-Henning Kampbeing finished,
6604351ba27SMatthew D Flemingor returns the error code from the drain if one is attached.
6616ab7244aSPoul-Henning Kamp.Pp
6626ab7244aSPoul-Henning KampThe
6636ab7244aSPoul-Henning Kamp.Fn sbuf_finish 3
6646ab7244aSPoul-Henning Kampfunction (the userland version)
6656ab7244aSPoul-Henning Kampwill return zero for success and \-1 and set errno on error.
6666ab7244aSPoul-Henning Kamp.Sh EXAMPLES
6676ab7244aSPoul-Henning Kamp.Bd -literal -compact
6687beb104dSEnji Cooper#include <sys/types.h>
6696ab7244aSPoul-Henning Kamp#include <sys/sbuf.h>
6706ab7244aSPoul-Henning Kamp
6716ab7244aSPoul-Henning Kampstruct sbuf *sb;
6726ab7244aSPoul-Henning Kamp
6736ab7244aSPoul-Henning Kampsb = sbuf_new_auto();
674c920aa7aSPoul-Henning Kampsbuf_cat(sb, "Customers found:\en");
6756ab7244aSPoul-Henning KampTAILQ_FOREACH(foo, &foolist, list) {
676c920aa7aSPoul-Henning Kamp	sbuf_printf(sb, "   %4d %s\en", foo->index, foo->name);
677c920aa7aSPoul-Henning Kamp	sbuf_printf(sb, "      Address: %s\en", foo->address);
678c920aa7aSPoul-Henning Kamp	sbuf_printf(sb, "      Zip: %s\en", foo->zipcode);
6796ab7244aSPoul-Henning Kamp}
680c5a0813dSJaakko Heinonenif (sbuf_finish(sb) != 0) /* Check for any and all errors */
6816ab7244aSPoul-Henning Kamp	err(1, "Could not generate message");
6826ab7244aSPoul-Henning Kamptransmit_msg(sbuf_data(sb), sbuf_len(sb));
6836ab7244aSPoul-Henning Kampsbuf_delete(sb);
6846ab7244aSPoul-Henning Kamp.Ed
685863edaecSDag-Erling Smørgrav.Sh SEE ALSO
6865672fac9SKenneth D. Merry.Xr hexdump 3 ,
687863edaecSDag-Erling Smørgrav.Xr printf 3 ,
688863edaecSDag-Erling Smørgrav.Xr strcat 3 ,
689d68b700aSRuslan Ermilov.Xr strcpy 3 ,
690d68b700aSRuslan Ermilov.Xr copyin 9 ,
691e05f3134SAndrew R. Reiter.Xr copyinstr 9 ,
692e05f3134SAndrew R. Reiter.Xr printf 9
693863edaecSDag-Erling Smørgrav.Sh HISTORY
694863edaecSDag-Erling SmørgravThe
69581ae4b8dSRuslan Ermilov.Nm
696863edaecSDag-Erling Smørgravfamily of functions first appeared in
69722cbe5e3SDag-Erling Smørgrav.Fx 4.4 .
698863edaecSDag-Erling Smørgrav.Sh AUTHORS
699863edaecSDag-Erling Smørgrav.An -nosplit
700863edaecSDag-Erling SmørgravThe
70181ae4b8dSRuslan Ermilov.Nm
702863edaecSDag-Erling Smørgravfamily of functions was designed by
7038a7314fcSBaptiste Daroussin.An Poul-Henning Kamp Aq Mt phk@FreeBSD.org
704863edaecSDag-Erling Smørgravand implemented by
7058a7314fcSBaptiste Daroussin.An Dag-Erling Sm\(/orgrav Aq Mt des@FreeBSD.org .
706e499b74dSDag-Erling SmørgravAdditional improvements were suggested by
7078a7314fcSBaptiste Daroussin.An Justin T. Gibbs Aq Mt gibbs@FreeBSD.org .
708371c0c84SKelly YanceyAuto-extend support added by
7098a7314fcSBaptiste Daroussin.An Kelly Yancey Aq Mt kbyanc@FreeBSD.org .
7104351ba27SMatthew D FlemingDrain functionality added by
7118a7314fcSBaptiste Daroussin.An Matthew Fleming Aq Mt mdf@FreeBSD.org .
712863edaecSDag-Erling Smørgrav.Pp
713863edaecSDag-Erling SmørgravThis manual page was written by
7148a7314fcSBaptiste Daroussin.An Dag-Erling Sm\(/orgrav Aq Mt des@FreeBSD.org .
715