xref: /dragonfly/share/man/man9/sbuf.9 (revision 335b9e93)
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: head/share/man/man9/sbuf.9 249379 2013-04-11 19:51:53Z trociny $
27.\"
28.Dd April 11, 2013
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_setpos ,
38.Nm sbuf_bcat ,
39.Nm sbuf_bcopyin ,
40.Nm sbuf_bcpy ,
41.Nm sbuf_cat ,
42.Nm sbuf_copyin ,
43.Nm sbuf_cpy ,
44.Nm sbuf_printf ,
45.Nm sbuf_vprintf ,
46.Nm sbuf_putc ,
47.Nm sbuf_set_drain ,
48.Nm sbuf_trim ,
49.Nm sbuf_error ,
50.Nm sbuf_finish ,
51.Nm sbuf_data ,
52.Nm sbuf_len ,
53.Nm sbuf_done ,
54.Nm sbuf_delete ,
55.Nm sbuf_start_section ,
56.Nm sbuf_end_section
57.Nd safe string composition
58.Sh SYNOPSIS
59.In sys/types.h
60.In sys/sbuf.h
61.Ft typedef\ int ( sbuf_drain_func ) ( void\ *arg, const\ char\ *data, int\ len ) ;
62.Pp
63.Ft struct sbuf *
64.Fn sbuf_new "struct sbuf *s" "char *buf" "int length" "int flags"
65.Ft struct sbuf *
66.Fn sbuf_new_auto "void"
67.Ft void
68.Fn sbuf_clear "struct sbuf *s"
69.Ft int
70.Fn sbuf_setpos "struct sbuf *s" "ssize_t pos"
71.Ft int
72.Fn sbuf_bcat "struct sbuf *s" "const void *buf" "size_t len"
73.Ft int
74.Fn sbuf_bcopyin "struct sbuf *s" "const void *uaddr" "size_t len"
75.Ft int
76.Fn sbuf_bcpy "struct sbuf *s" "const void *buf" "size_t len"
77.Ft int
78.Fn sbuf_cat "struct sbuf *s" "const char *str"
79.Ft int
80.Fn sbuf_copyin "struct sbuf *s" "const void *uaddr" "size_t len"
81.Ft int
82.Fn sbuf_cpy "struct sbuf *s" "const char *str"
83.Ft int
84.Fn sbuf_printf "struct sbuf *s" "const char *fmt" "..."
85.Ft int
86.Fn sbuf_vprintf "struct sbuf *s" "const char *fmt" "__va_list ap"
87.Ft int
88.Fn sbuf_putc "struct sbuf *s" "int c"
89.Ft void
90.Fn sbuf_set_drain "struct sbuf *s" "sbuf_drain_func *func" "void *arg"
91.Ft int
92.Fn sbuf_trim "struct sbuf *s"
93.Ft int
94.Fn sbuf_error "const struct sbuf *s"
95.Ft int
96.Fn sbuf_finish "struct sbuf *s"
97.Ft char *
98.Fn sbuf_data "struct sbuf *s"
99.Ft ssize_t
100.Fn sbuf_len "struct sbuf *s"
101.Ft int
102.Fn sbuf_done "const struct sbuf *s"
103.Ft void
104.Fn sbuf_delete "struct sbuf *s"
105.Ft void
106.Fn sbuf_start_section "struct sbuf *s" "ssize_t *old_lenp"
107.Ft ssize_t
108.Fn sbuf_end_section "struct sbuf *s" "ssize_t old_len" "size_t pad" "int c"
109.In sys/sysctl.h
110.Ft struct sbuf *
111.Fn sbuf_new_for_sysctl "struct sbuf *s" "char *buf" "int length" "struct sysctl_req *req"
112.Sh DESCRIPTION
113The
114.Nm
115family of functions allows one to safely allocate, compose and
116release strings in kernel or user space.
117.Pp
118Instead of arrays of characters, these functions operate on structures
119called
120.Fa sbufs ,
121defined in
122.In sys/sbuf.h .
123.Pp
124Any errors encountered during the allocation or composition of the
125string will be latched in the data structure,
126making a single error test at the end of the composition
127sufficient to determine success or failure of the entire process.
128.Pp
129The
130.Fn sbuf_new
131function initializes the
132.Fa sbuf
133pointed to by its first argument.
134If that pointer is
135.Dv NULL ,
136.Fn sbuf_new
137allocates a
138.Vt struct sbuf
139using
140.Xr kmalloc 9 .
141The
142.Fa buf
143argument is a pointer to a buffer in which to store the actual string;
144if it is
145.Dv NULL ,
146.Fn sbuf_new
147will allocate one using
148.Xr kmalloc 9 .
149The
150.Fa length
151is the initial size of the storage buffer.
152The fourth argument,
153.Fa flags ,
154may be comprised of the following flags:
155.Bl -tag -width ".Dv SBUF_AUTOEXTEND"
156.It Dv SBUF_FIXEDLEN
157The storage buffer is fixed at its initial size.
158Attempting to extend the sbuf beyond this size results in an overflow condition.
159.It Dv SBUF_AUTOEXTEND
160This indicates that the storage buffer may be extended as necessary, so long
161as resources allow, to hold additional data.
162.El
163.Pp
164Note that if
165.Fa buf
166is not
167.Dv NULL ,
168it must point to an array of at least
169.Fa length
170characters.
171The result of accessing that array directly while it is in use by the
172sbuf is undefined.
173.Pp
174The
175.Fn sbuf_new_auto
176function is a shortcut for creating a completely dynamic
177.Nm .
178It is the equivalent of calling
179.Fn sbuf_new
180with values
181.Dv NULL ,
182.Dv NULL ,
183.Dv 0 ,
184and
185.Dv SBUF_AUTOEXTEND .
186.Pp
187The
188.Fn sbuf_new_for_sysctl
189function will set up an sbuf with a drain function to use
190.Fn SYSCTL_OUT
191when the internal buffer fills.
192.\"Note that if the various functions which append to an sbuf are used while
193.\"a non-sleepable lock is held, the user buffer should be wired using
194.\".Fn sysctl_wire_old_buffer .
195.Pp
196The
197.Fn sbuf_delete
198function clears the
199.Fa sbuf
200and frees any memory allocated for it.
201There must be a call to
202.Fn sbuf_delete
203for every call to
204.Fn sbuf_new .
205Any attempt to access the sbuf after it has been deleted will fail.
206.Pp
207The
208.Fn sbuf_clear
209function invalidates the contents of the
210.Fa sbuf
211and resets its position to zero.
212.Pp
213The
214.Fn sbuf_setpos
215function sets the
216.Fa sbuf Ns 's
217end position to
218.Fa pos ,
219which is a value between zero and one less than the size of the
220storage buffer.
221This effectively truncates the sbuf at the new position.
222.Pp
223The
224.Fn sbuf_bcat
225function appends the first
226.Fa len
227bytes from the buffer
228.Fa buf
229to the
230.Fa sbuf .
231.Pp
232The
233.Fn sbuf_bcopyin
234function copies
235.Fa len
236bytes from the specified userland address into the
237.Fa sbuf .
238.Pp
239The
240.Fn sbuf_bcpy
241function replaces the contents of the
242.Fa sbuf
243with the first
244.Fa len
245bytes from the buffer
246.Fa buf .
247.Pp
248The
249.Fn sbuf_cat
250function appends the NUL-terminated string
251.Fa str
252to the
253.Fa sbuf
254at the current position.
255.Pp
256The
257.Fn sbuf_set_drain
258function sets a drain function
259.Fa func
260for the
261.Fa sbuf ,
262and records a pointer
263.Fa arg
264to be passed to the drain on callback.
265The drain function cannot be changed while
266.Fa sbuf_len
267is non-zero.
268.Pp
269The registered drain function
270.Vt sbuf_drain_func
271will be called with the argument
272.Fa arg
273provided to
274.Fn sbuf_set_drain ,
275a pointer
276.Fa data
277to a byte string that is the contents of the sbuf, and the length
278.Fa len
279of the data.
280If the drain function exists, it will be called when the sbuf internal
281buffer is full, or on behalf of
282.Fn sbuf_finish .
283The drain function may drain some or all of the data, but must drain
284at least 1 byte.
285The return value from the drain function, if positive, indicates how
286many bytes were drained.
287If negative, the return value indicates the negative error code which
288will be returned from this or a later call to
289.Fn sbuf_finish .
290The returned drained length cannot be zero.
291To do unbuffered draining, initialize the sbuf with a two-byte buffer.
292The drain will be called for every byte added to the sbuf.
293The
294.Fn sbuf_bcopyin ,
295.Fn sbuf_copyin ,
296.Fn sbuf_trim ,
297and
298.Fn sbuf_data
299functions cannot be used on an sbuf with a drain.
300.Pp
301The
302.Fn sbuf_copyin
303function copies a NUL-terminated string from the specified userland
304address into the
305.Fa sbuf .
306If the
307.Fa len
308argument is non-zero, no more than
309.Fa len
310characters (not counting the terminating NUL) are copied; otherwise
311the entire string, or as much of it as can fit in the
312.Fa sbuf ,
313is copied.
314.Pp
315The
316.Fn sbuf_cpy
317function replaces the contents of the
318.Fa sbuf
319with those of the NUL-terminated string
320.Fa str .
321This is equivalent to calling
322.Fn sbuf_cat
323with a fresh
324.Fa sbuf
325or one which position has been reset to zero with
326.Fn sbuf_clear
327or
328.Fn sbuf_setpos .
329.Pp
330The
331.Fn sbuf_printf
332function formats its arguments according to the format string pointed
333to by
334.Fa fmt
335and appends the resulting string to the
336.Fa sbuf
337at the current position.
338.Pp
339The
340.Fn sbuf_vprintf
341function behaves the same as
342.Fn sbuf_printf
343except that the arguments are obtained from the variable-length argument list
344.Fa ap .
345.Pp
346The
347.Fn sbuf_putc
348function appends the character
349.Fa c
350to the
351.Fa sbuf
352at the current position.
353.Pp
354The
355.Fn sbuf_trim
356function removes trailing whitespace from the
357.Fa sbuf .
358.Pp
359The
360.Fn sbuf_error
361function returns any error value that the
362.Fa sbuf
363may have accumulated, either from the drain function, or
364.Er ENOMEM
365if the
366.Fa sbuf
367overflowed.
368This function is generally not needed and instead the error code from
369.Fn sbuf_finish
370is the preferred way to discover whether an sbuf had an error.
371.Pp
372The
373.Fn sbuf_finish
374function will call the attached drain function if one exists until all
375the data in the
376.Fa sbuf
377is flushed.
378If there is no attached drain,
379.Fn sbuf_finish
380NUL-terminates the
381.Fa sbuf .
382In either case it marks the
383.Fa sbuf
384as finished, which means that it may no longer be modified using
385.Fn sbuf_setpos ,
386.Fn sbuf_cat ,
387.Fn sbuf_cpy ,
388.Fn sbuf_printf
389or
390.Fn sbuf_putc ,
391until
392.Fn sbuf_clear
393is used to reset the sbuf.
394.Pp
395The
396.Fn sbuf_data
397function returns the actual string;
398.Fn sbuf_data
399only works on a finished
400.Fa sbuf .
401The
402.Fn sbuf_len
403function returns the length of the string.
404For an
405.Fa sbuf
406with an attached drain,
407.Fn sbuf_len
408returns the length of the un-drained data.
409.Fn sbuf_done
410returns non-zero if the
411.Fa sbuf
412is finished.
413.Pp
414The
415.Fn sbuf_start_section
416and
417.Fn sbuf_end_section
418functions may be used for automatic section alignment.
419The arguments
420.Fa pad
421and
422.Fa c
423specify the padding size and a character used for padding.
424The arguments
425.Fa old_lenp
426and
427.Fa old_len
428are to save and restore the current section length when nested sections
429are used.
430For the top level section
431.Dv NULL
432and \-1 can be specified for
433.Fa old_lenp
434and
435.Fa old_len
436respectively.
437.Sh NOTES
438If an operation caused an
439.Fa sbuf
440to overflow, most subsequent operations on it will fail until the
441.Fa sbuf
442is finished using
443.Fn sbuf_finish
444or reset using
445.Fn sbuf_clear ,
446or its position is reset to a value between 0 and one less than the
447size of its storage buffer using
448.Fn sbuf_setpos ,
449or it is reinitialized to a sufficiently short string using
450.Fn sbuf_cpy .
451.Pp
452Drains in user-space will not always function as indicated.
453While the drain function will be called immediately on overflow from
454the
455.Fa sbuf_putc ,
456.Fa sbuf_bcat ,
457.Fa sbuf_cat
458functions,
459.Fa sbuf_printf
460and
461.Fa sbuf_vprintf
462currently have no way to determine whether there will be an overflow
463until after it occurs, and cannot do a partial expansion of the format
464string.
465Thus when using libsbuf the buffer may be extended to allow completion
466of a single printf call, even though a drain is attached.
467.Sh RETURN VALUES
468The
469.Fn sbuf_new
470function returns
471.Dv NULL
472if it failed to allocate a storage buffer, and a pointer to the new
473.Fa sbuf
474otherwise.
475.Pp
476The
477.Fn sbuf_setpos
478function returns \-1 if
479.Fa pos
480was invalid, and zero otherwise.
481.Pp
482The
483.Fn sbuf_cat ,
484.Fn sbuf_cpy ,
485.Fn sbuf_printf ,
486.Fn sbuf_putc ,
487and
488.Fn sbuf_trim
489functions
490all return \-1 if the buffer overflowed, and zero otherwise.
491.Pp
492The
493.Fn sbuf_error
494function returns a non-zero value if the buffer has an overflow or
495drain error, and zero otherwise.
496.Pp
497The
498.Fn sbuf_len
499function returns \-1 if the buffer overflowed.
500.Pp
501The
502.Fn sbuf_copyin
503function
504returns \-1 if copying string from userland failed, and number of bytes
505copied otherwise.
506.Pp
507The
508.Fn sbuf_end_section
509function returns the section length or \-1 if the buffer has an error.
510.Pp
511The
512.Fn sbuf_finish 9
513function (the kernel version) returns
514.Er ENOMEM
515if the sbuf overflowed before being finished,
516or returns the error code from the drain if one is attached.
517.Pp
518The
519.Fn sbuf_finish 3
520function (the userland version)
521will return zero for success and \-1 and set errno on error.
522.Sh EXAMPLES
523.Bd -literal -compact
524#include <sys/sbuf.h>
525
526struct sbuf *sb;
527
528sb = sbuf_new_auto();
529sbuf_cat(sb, "Customers found:\en");
530TAILQ_FOREACH(foo, &foolist, list) {
531	sbuf_printf(sb, "   %4d %s\en", foo->index, foo->name);
532	sbuf_printf(sb, "      Address: %s\en", foo->address);
533	sbuf_printf(sb, "      Zip: %s\en", foo->zipcode);
534}
535if (sbuf_finish(sb) != 0) /* Check for any and all errors */
536	err(1, "Could not generate message");
537transmit_msg(sbuf_data(sb), sbuf_len(sb));
538sbuf_delete(sb);
539.Ed
540.Sh SEE ALSO
541.Xr printf 3 ,
542.Xr strcat 3 ,
543.Xr strcpy 3 ,
544.Xr copyin 9 ,
545.Xr copyinstr 9 ,
546.Xr kprintf 9
547.Sh HISTORY
548The
549.Nm
550family of functions first appeared in
551.Fx 4.4 .
552.Sh AUTHORS
553.An -nosplit
554The
555.Nm
556family of functions was designed by
557.An Poul-Henning Kamp Aq Mt phk@FreeBSD.org
558and implemented by
559.An Dag-Erling Sm\(/orgrav Aq Mt des@FreeBSD.org .
560Additional improvements were suggested by
561.An Justin T. Gibbs Aq Mt gibbs@FreeBSD.org .
562Auto-extend support added by
563.An Kelly Yancey Aq Mt kbyanc@FreeBSD.org .
564Drain functionality added by
565.An Matthew Fleming Aq Mt mdf@FreeBSD.org .
566.Pp
567This manual page was written by
568.An Dag-Erling Sm\(/orgrav Aq Mt des@FreeBSD.org .
569