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