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