]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - share/man/man9/sbuf.9
MFV r350080:
[FreeBSD/FreeBSD.git] / share / man / man9 / sbuf.9
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
230 The
231 .Nm
232 family of functions allows one to safely allocate, compose and
233 release strings in kernel or user space.
234 .Pp
235 Instead of arrays of characters, these functions operate on structures
236 called
237 .Fa sbufs ,
238 defined in
239 .In sys/sbuf.h .
240 .Pp
241 Any errors encountered during the allocation or composition of the
242 string will be latched in the data structure,
243 making a single error test at the end of the composition
244 sufficient to determine success or failure of the entire process.
245 .Pp
246 The
247 .Fn sbuf_new
248 function initializes the
249 .Fa sbuf
250 pointed to by its first argument.
251 If that pointer is
252 .Dv NULL ,
253 .Fn sbuf_new
254 allocates a
255 .Vt struct sbuf
256 using
257 .Xr malloc 9 .
258 The
259 .Fa buf
260 argument is a pointer to a buffer in which to store the actual string;
261 if it is
262 .Dv NULL ,
263 .Fn sbuf_new
264 will allocate one using
265 .Xr malloc 9 .
266 The
267 .Fa length
268 is the initial size of the storage buffer.
269 The fourth argument,
270 .Fa flags ,
271 may be comprised of the following flags:
272 .Bl -tag -width ".Dv SBUF_AUTOEXTEND"
273 .It Dv SBUF_FIXEDLEN
274 The storage buffer is fixed at its initial size.
275 Attempting to extend the sbuf beyond this size results in an overflow condition.
276 .It Dv SBUF_AUTOEXTEND
277 This indicates that the storage buffer may be extended as necessary, so long
278 as resources allow, to hold additional data.
279 .It Dv SBUF_INCLUDENUL
280 This causes the final nulterm byte to be counted in the length of the data.
281 .It Dv SBUF_DRAINTOEOR
282 Treat top-level sections started with
283 .Fn sbuf_start_section
284 as a record boundary marker that will be used during drain operations to avoid
285 records being split.
286 If a record grows sufficiently large such that it fills the
287 .Fa sbuf
288 and therefore cannot be drained without being split, an error of
289 .Er EDEADLK
290 is set.
291 .El
292 .Pp
293 Note that if
294 .Fa buf
295 is not
296 .Dv NULL ,
297 it must point to an array of at least
298 .Fa length
299 characters.
300 The result of accessing that array directly while it is in use by the
301 sbuf is undefined.
302 .Pp
303 The
304 .Fn sbuf_new_auto
305 function is a shortcut for creating a completely dynamic
306 .Nm .
307 It is the equivalent of calling
308 .Fn sbuf_new
309 with values
310 .Dv NULL ,
311 .Dv NULL ,
312 .Dv 0 ,
313 and
314 .Dv SBUF_AUTOEXTEND .
315 .Pp
316 The
317 .Fn sbuf_new_for_sysctl
318 function will set up an sbuf with a drain function to use
319 .Fn SYSCTL_OUT
320 when the internal buffer fills.
321 Note that if the various functions which append to an sbuf are used while
322 a non-sleepable lock is held, the user buffer should be wired using
323 .Fn sysctl_wire_old_buffer .
324 .Pp
325 The
326 .Fn sbuf_delete
327 function clears the
328 .Fa sbuf
329 and frees any memory allocated for it.
330 There must be a call to
331 .Fn sbuf_delete
332 for every call to
333 .Fn sbuf_new .
334 Any attempt to access the sbuf after it has been deleted will fail.
335 .Pp
336 The
337 .Fn sbuf_clear
338 function invalidates the contents of the
339 .Fa sbuf
340 and resets its position to zero.
341 .Pp
342 The
343 .Fn sbuf_get_flags
344 function returns the current user flags.
345 The
346 .Fn sbuf_set_flags
347 and
348 .Fn sbuf_get_flags
349 functions set or clear one or more user flags, respectively.
350 The user flags are described under the
351 .Fn sbuf_new
352 function.
353 .Pp
354 The
355 .Fn sbuf_setpos
356 function sets the
357 .Fa sbuf Ns 's
358 end position to
359 .Fa pos ,
360 which is a value between zero and one less than the size of the
361 storage buffer.
362 This effectively truncates the sbuf at the new position.
363 .Pp
364 The
365 .Fn sbuf_bcat
366 function appends the first
367 .Fa len
368 bytes from the buffer
369 .Fa buf
370 to the
371 .Fa sbuf .
372 .Pp
373 The
374 .Fn sbuf_bcopyin
375 function copies
376 .Fa len
377 bytes from the specified userland address into the
378 .Fa sbuf .
379 .Pp
380 The
381 .Fn sbuf_bcpy
382 function replaces the contents of the
383 .Fa sbuf
384 with the first
385 .Fa len
386 bytes from the buffer
387 .Fa buf .
388 .Pp
389 The
390 .Fn sbuf_cat
391 function appends the NUL-terminated string
392 .Fa str
393 to the
394 .Fa sbuf
395 at the current position.
396 .Pp
397 The
398 .Fn sbuf_set_drain
399 function sets a drain function
400 .Fa func
401 for the
402 .Fa sbuf ,
403 and records a pointer
404 .Fa arg
405 to be passed to the drain on callback.
406 The drain function cannot be changed while
407 .Fa sbuf_len
408 is non-zero.
409 .Pp
410 The registered drain function
411 .Vt sbuf_drain_func
412 will be called with the argument
413 .Fa arg
414 provided to
415 .Fn sbuf_set_drain ,
416 a pointer
417 .Fa data
418 to a byte string that is the contents of the sbuf, and the length
419 .Fa len
420 of the data.
421 If the drain function exists, it will be called when the sbuf internal
422 buffer is full, or on behalf of
423 .Fn sbuf_finish .
424 The drain function may drain some or all of the data, but must drain
425 at least 1 byte.
426 The return value from the drain function, if positive, indicates how
427 many bytes were drained.
428 If negative, the return value indicates the negative error code which
429 will be returned from this or a later call to
430 .Fn sbuf_finish .
431 If the returned drained length is 0, an error of
432 .Er EDEADLK
433 is set.
434 To do unbuffered draining, initialize the sbuf with a two-byte buffer.
435 The drain will be called for every byte added to the sbuf.
436 The
437 .Fn sbuf_bcopyin ,
438 .Fn sbuf_copyin ,
439 .Fn sbuf_trim ,
440 and
441 .Fn sbuf_data
442 functions cannot be used on an sbuf with a drain.
443 .Pp
444 The
445 .Fn sbuf_copyin
446 function copies a NUL-terminated string from the specified userland
447 address into the
448 .Fa sbuf .
449 If the
450 .Fa len
451 argument is non-zero, no more than
452 .Fa len
453 characters (not counting the terminating NUL) are copied; otherwise
454 the entire string, or as much of it as can fit in the
455 .Fa sbuf ,
456 is copied.
457 .Pp
458 The
459 .Fn sbuf_cpy
460 function replaces the contents of the
461 .Fa sbuf
462 with those of the NUL-terminated string
463 .Fa str .
464 This is equivalent to calling
465 .Fn sbuf_cat
466 with a fresh
467 .Fa sbuf
468 or one which position has been reset to zero with
469 .Fn sbuf_clear
470 or
471 .Fn sbuf_setpos .
472 .Pp
473 The
474 .Fn sbuf_printf
475 function formats its arguments according to the format string pointed
476 to by
477 .Fa fmt
478 and appends the resulting string to the
479 .Fa sbuf
480 at the current position.
481 .Pp
482 The
483 .Fn sbuf_vprintf
484 function behaves the same as
485 .Fn sbuf_printf
486 except that the arguments are obtained from the variable-length argument list
487 .Fa ap .
488 .Pp
489 The
490 .Fn sbuf_putc
491 function appends the character
492 .Fa c
493 to the
494 .Fa sbuf
495 at the current position.
496 .Pp
497 The
498 .Fn sbuf_trim
499 function removes trailing whitespace from the
500 .Fa sbuf .
501 .Pp
502 The
503 .Fn sbuf_error
504 function returns any error value that the
505 .Fa sbuf
506 may have accumulated, either from the drain function, or
507 .Er ENOMEM
508 if the
509 .Fa sbuf
510 overflowed.
511 This function is generally not needed and instead the error code from
512 .Fn sbuf_finish
513 is the preferred way to discover whether an sbuf had an error.
514 .Pp
515 The
516 .Fn sbuf_finish
517 function will call the attached drain function if one exists until all
518 the data in the
519 .Fa sbuf
520 is flushed.
521 If there is no attached drain,
522 .Fn sbuf_finish
523 NUL-terminates the
524 .Fa sbuf .
525 In either case it marks the
526 .Fa sbuf
527 as 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
532 or
533 .Fn sbuf_putc ,
534 until
535 .Fn sbuf_clear
536 is used to reset the sbuf.
537 .Pp
538 The
539 .Fn sbuf_data
540 function returns the actual string;
541 .Fn sbuf_data
542 only works on a finished
543 .Fa sbuf .
544 The
545 .Fn sbuf_len
546 function returns the length of the string.
547 For an
548 .Fa sbuf
549 with an attached drain,
550 .Fn sbuf_len
551 returns the length of the un-drained data.
552 .Fn sbuf_done
553 returns non-zero if the
554 .Fa sbuf
555 is finished.
556 .Pp
557 The
558 .Fn sbuf_start_section
559 and
560 .Fn sbuf_end_section
561 functions may be used for automatic section alignment.
562 The arguments
563 .Fa pad
564 and
565 .Fa c
566 specify the padding size and a character used for padding.
567 The arguments
568 .Fa old_lenp
569 and
570 .Fa old_len
571 are to save and restore the current section length when nested sections
572 are used.
573 For the top level section
574 .Dv NULL
575 and \-1 can be specified for
576 .Fa old_lenp
577 and
578 .Fa old_len
579 respectively.
580 .Pp
581 The
582 .Fn sbuf_hexdump
583 function prints an array of bytes to the supplied sbuf, along with an ASCII
584 representation of the bytes if possible.
585 See the
586 .Xr hexdump 3
587 man page for more details on the interface.
588 .Pp
589 The
590 .Fn sbuf_printf_drain
591 function is a drain function that will call printf, or log to the console.
592 The argument
593 .Fa arg
594 must be either
595 .Dv NULL ,
596 or a valid pointer to a
597 .Vt size_t .
598 If
599 .Fa arg
600 is not
601 .Dv NULL ,
602 the total bytes drained will be added to the value pointed to by
603 .Fa arg .
604 .Pp
605 The
606 .Fn sbuf_putbuf
607 function printfs the sbuf to stdout if in userland, and to the console
608 and log if in the kernel.
609 The
610 .Fa sbuf
611 must be finished before calling
612 .Fn sbuf_putbuf .
613 It does not drain the buffer or update any pointers.
614 .Sh NOTES
615 If an operation caused an
616 .Fa sbuf
617 to overflow, most subsequent operations on it will fail until the
618 .Fa sbuf
619 is finished using
620 .Fn sbuf_finish
621 or reset using
622 .Fn sbuf_clear ,
623 or its position is reset to a value between 0 and one less than the
624 size of its storage buffer using
625 .Fn sbuf_setpos ,
626 or it is reinitialized to a sufficiently short string using
627 .Fn sbuf_cpy .
628 .Pp
629 Drains in user-space will not always function as indicated.
630 While the drain function will be called immediately on overflow from
631 the
632 .Fa sbuf_putc ,
633 .Fa sbuf_bcat ,
634 .Fa sbuf_cat
635 functions,
636 .Fa sbuf_printf
637 and
638 .Fa sbuf_vprintf
639 currently have no way to determine whether there will be an overflow
640 until after it occurs, and cannot do a partial expansion of the format
641 string.
642 Thus when using libsbuf the buffer may be extended to allow completion
643 of a single printf call, even though a drain is attached.
644 .Sh RETURN VALUES
645 The
646 .Fn sbuf_new
647 function returns
648 .Dv NULL
649 if it failed to allocate a storage buffer, and a pointer to the new
650 .Fa sbuf
651 otherwise.
652 .Pp
653 The
654 .Fn sbuf_setpos
655 function returns \-1 if
656 .Fa pos
657 was invalid, and zero otherwise.
658 .Pp
659 The
660 .Fn sbuf_bcat ,
661 .Fn sbuf_cat ,
662 .Fn sbuf_cpy ,
663 .Fn sbuf_printf ,
664 .Fn sbuf_putc ,
665 and
666 .Fn sbuf_trim
667 functions
668 all return \-1 if the buffer overflowed, and zero otherwise.
669 .Pp
670 The
671 .Fn sbuf_error
672 function returns a non-zero value if the buffer has an overflow or
673 drain error, and zero otherwise.
674 .Pp
675 The
676 .Fn sbuf_len
677 function returns \-1 if the buffer overflowed.
678 .Pp
679 The
680 .Fn sbuf_copyin
681 function
682 returns \-1 if copying string from userland failed, and number of bytes
683 copied otherwise.
684 .Pp
685 The
686 .Fn sbuf_end_section
687 function returns the section length or \-1 if the buffer has an error.
688 .Pp
689 The
690 .Fn sbuf_finish 9
691 function (the kernel version) returns
692 .Er ENOMEM
693 if the sbuf overflowed before being finished,
694 or returns the error code from the drain if one is attached.
695 .Pp
696 The
697 .Fn sbuf_finish 3
698 function (the userland version)
699 will 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
705 struct sbuf *sb;
706
707 sb = sbuf_new_auto();
708 sbuf_cat(sb, "Customers found:\en");
709 TAILQ_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 }
714 if (sbuf_finish(sb) != 0) /* Check for any and all errors */
715         err(1, "Could not generate message");
716 transmit_msg(sbuf_data(sb), sbuf_len(sb));
717 sbuf_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
728 The
729 .Nm
730 family of functions first appeared in
731 .Fx 4.4 .
732 .Sh AUTHORS
733 .An -nosplit
734 The
735 .Nm
736 family of functions was designed by
737 .An Poul-Henning Kamp Aq Mt phk@FreeBSD.org
738 and implemented by
739 .An Dag-Erling Sm\(/orgrav Aq Mt des@FreeBSD.org .
740 Additional improvements were suggested by
741 .An Justin T. Gibbs Aq Mt gibbs@FreeBSD.org .
742 Auto-extend support added by
743 .An Kelly Yancey Aq Mt kbyanc@FreeBSD.org .
744 Drain functionality added by
745 .An Matthew Fleming Aq Mt mdf@FreeBSD.org .
746 .Pp
747 This manual page was written by
748 .An Dag-Erling Sm\(/orgrav Aq Mt des@FreeBSD.org .