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