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