]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/subr_sbuf.c
nfsd: Reduce the callback timeout to 800msec
[FreeBSD/FreeBSD.git] / sys / kern / subr_sbuf.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2000-2008 Poul-Henning Kamp
5  * Copyright (c) 2000-2008 Dag-Erling Coïdan Smørgrav
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer
13  *    in this position and unchanged.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33
34 #include <sys/param.h>
35
36 #ifdef _KERNEL
37 #include <sys/ctype.h>
38 #include <sys/errno.h>
39 #include <sys/kernel.h>
40 #include <sys/limits.h>
41 #include <sys/malloc.h>
42 #include <sys/systm.h>
43 #include <sys/uio.h>
44 #include <machine/stdarg.h>
45 #else /* _KERNEL */
46 #include <ctype.h>
47 #include <errno.h>
48 #include <limits.h>
49 #include <stdarg.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #endif /* _KERNEL */
54
55 #include <sys/sbuf.h>
56
57 #ifdef _KERNEL
58 static MALLOC_DEFINE(M_SBUF, "sbuf", "string buffers");
59 #define SBMALLOC(size)          malloc(size, M_SBUF, M_WAITOK|M_ZERO)
60 #define SBFREE(buf)             free(buf, M_SBUF)
61 #else /* _KERNEL */
62 #define KASSERT(e, m)
63 #define SBMALLOC(size)          calloc(1, size)
64 #define SBFREE(buf)             free(buf)
65 #endif /* _KERNEL */
66
67 /*
68  * Predicates
69  */
70 #define SBUF_ISDYNAMIC(s)       ((s)->s_flags & SBUF_DYNAMIC)
71 #define SBUF_ISDYNSTRUCT(s)     ((s)->s_flags & SBUF_DYNSTRUCT)
72 #define SBUF_ISFINISHED(s)      ((s)->s_flags & SBUF_FINISHED)
73 #define SBUF_HASROOM(s)         ((s)->s_len < (s)->s_size - 1)
74 #define SBUF_FREESPACE(s)       ((s)->s_size - ((s)->s_len + 1))
75 #define SBUF_CANEXTEND(s)       ((s)->s_flags & SBUF_AUTOEXTEND)
76 #define SBUF_ISSECTION(s)       ((s)->s_flags & SBUF_INSECTION)
77 #define SBUF_NULINCLUDED(s)     ((s)->s_flags & SBUF_INCLUDENUL)
78 #define SBUF_ISDRAINTOEOR(s)    ((s)->s_flags & SBUF_DRAINTOEOR)
79 #define SBUF_DODRAINTOEOR(s)    (SBUF_ISSECTION(s) && SBUF_ISDRAINTOEOR(s))
80
81 /*
82  * Set / clear flags
83  */
84 #define SBUF_SETFLAG(s, f)      do { (s)->s_flags |= (f); } while (0)
85 #define SBUF_CLEARFLAG(s, f)    do { (s)->s_flags &= ~(f); } while (0)
86
87 #define SBUF_MINSIZE             2              /* Min is 1 byte + nulterm. */
88 #define SBUF_MINEXTENDSIZE      16              /* Should be power of 2. */
89
90 #ifdef PAGE_SIZE
91 #define SBUF_MAXEXTENDSIZE      PAGE_SIZE
92 #define SBUF_MAXEXTENDINCR      PAGE_SIZE
93 #else
94 #define SBUF_MAXEXTENDSIZE      4096
95 #define SBUF_MAXEXTENDINCR      4096
96 #endif
97
98 /*
99  * Debugging support
100  */
101 #if defined(_KERNEL) && defined(INVARIANTS)
102
103 static void
104 _assert_sbuf_integrity(const char *fun, struct sbuf *s)
105 {
106
107         KASSERT(s != NULL,
108             ("%s called with a NULL sbuf pointer", fun));
109         KASSERT(s->s_buf != NULL,
110             ("%s called with uninitialized or corrupt sbuf", fun));
111         if (SBUF_ISFINISHED(s) && SBUF_NULINCLUDED(s)) {
112                 KASSERT(s->s_len <= s->s_size,
113                     ("wrote past end of sbuf (%jd >= %jd)",
114                     (intmax_t)s->s_len, (intmax_t)s->s_size));
115         } else {
116                 KASSERT(s->s_len < s->s_size,
117                     ("wrote past end of sbuf (%jd >= %jd)",
118                     (intmax_t)s->s_len, (intmax_t)s->s_size));
119         }
120 }
121
122 static void
123 _assert_sbuf_state(const char *fun, struct sbuf *s, int state)
124 {
125
126         KASSERT((s->s_flags & SBUF_FINISHED) == state,
127             ("%s called with %sfinished or corrupt sbuf", fun,
128             (state ? "un" : "")));
129 }
130
131 #define assert_sbuf_integrity(s) _assert_sbuf_integrity(__func__, (s))
132 #define assert_sbuf_state(s, i)  _assert_sbuf_state(__func__, (s), (i))
133
134 #else /* _KERNEL && INVARIANTS */
135
136 #define assert_sbuf_integrity(s) do { } while (0)
137 #define assert_sbuf_state(s, i)  do { } while (0)
138
139 #endif /* _KERNEL && INVARIANTS */
140
141 #ifdef CTASSERT
142 CTASSERT(powerof2(SBUF_MAXEXTENDSIZE));
143 CTASSERT(powerof2(SBUF_MAXEXTENDINCR));
144 #endif
145
146 static int
147 sbuf_extendsize(int size)
148 {
149         int newsize;
150
151         if (size < (int)SBUF_MAXEXTENDSIZE) {
152                 newsize = SBUF_MINEXTENDSIZE;
153                 while (newsize < size)
154                         newsize *= 2;
155         } else {
156                 newsize = roundup2(size, SBUF_MAXEXTENDINCR);
157         }
158         KASSERT(newsize >= size, ("%s: %d < %d\n", __func__, newsize, size));
159         return (newsize);
160 }
161
162 /*
163  * Extend an sbuf.
164  */
165 static int
166 sbuf_extend(struct sbuf *s, int addlen)
167 {
168         char *newbuf;
169         int newsize;
170
171         if (!SBUF_CANEXTEND(s))
172                 return (-1);
173         newsize = sbuf_extendsize(s->s_size + addlen);
174         newbuf = SBMALLOC(newsize);
175         if (newbuf == NULL)
176                 return (-1);
177         memcpy(newbuf, s->s_buf, s->s_size);
178         if (SBUF_ISDYNAMIC(s))
179                 SBFREE(s->s_buf);
180         else
181                 SBUF_SETFLAG(s, SBUF_DYNAMIC);
182         s->s_buf = newbuf;
183         s->s_size = newsize;
184         return (0);
185 }
186
187 /*
188  * Initialize the internals of an sbuf.
189  * If buf is non-NULL, it points to a static or already-allocated string
190  * big enough to hold at least length characters.
191  */
192 static struct sbuf *
193 sbuf_newbuf(struct sbuf *s, char *buf, int length, int flags)
194 {
195
196         memset(s, 0, sizeof(*s));
197         s->s_flags = flags;
198         s->s_size = length;
199         s->s_buf = buf;
200
201         if ((s->s_flags & SBUF_AUTOEXTEND) == 0) {
202                 KASSERT(s->s_size >= SBUF_MINSIZE,
203                     ("attempt to create an sbuf smaller than %d bytes",
204                     SBUF_MINSIZE));
205         }
206
207         if (s->s_buf != NULL)
208                 return (s);
209
210         if ((flags & SBUF_AUTOEXTEND) != 0)
211                 s->s_size = sbuf_extendsize(s->s_size);
212
213         s->s_buf = SBMALLOC(s->s_size);
214         if (s->s_buf == NULL)
215                 return (NULL);
216         SBUF_SETFLAG(s, SBUF_DYNAMIC);
217         return (s);
218 }
219
220 /*
221  * Initialize an sbuf.
222  * If buf is non-NULL, it points to a static or already-allocated string
223  * big enough to hold at least length characters.
224  */
225 struct sbuf *
226 sbuf_new(struct sbuf *s, char *buf, int length, int flags)
227 {
228
229         KASSERT(length >= 0,
230             ("attempt to create an sbuf of negative length (%d)", length));
231         KASSERT((flags & ~SBUF_USRFLAGMSK) == 0,
232             ("%s called with invalid flags", __func__));
233
234         flags &= SBUF_USRFLAGMSK;
235         if (s != NULL)
236                 return (sbuf_newbuf(s, buf, length, flags));
237
238         s = SBMALLOC(sizeof(*s));
239         if (s == NULL)
240                 return (NULL);
241         if (sbuf_newbuf(s, buf, length, flags) == NULL) {
242                 SBFREE(s);
243                 return (NULL);
244         }
245         SBUF_SETFLAG(s, SBUF_DYNSTRUCT);
246         return (s);
247 }
248
249 #ifdef _KERNEL
250 /*
251  * Create an sbuf with uio data
252  */
253 struct sbuf *
254 sbuf_uionew(struct sbuf *s, struct uio *uio, int *error)
255 {
256
257         KASSERT(uio != NULL,
258             ("%s called with NULL uio pointer", __func__));
259         KASSERT(error != NULL,
260             ("%s called with NULL error pointer", __func__));
261
262         if (uio->uio_resid >= INT_MAX || uio->uio_resid < SBUF_MINSIZE - 1) {
263                 *error = EINVAL;
264                 return (NULL);
265         }
266         s = sbuf_new(s, NULL, uio->uio_resid + 1, 0);
267         if (s == NULL) {
268                 *error = ENOMEM;
269                 return (NULL);
270         }
271         *error = uiomove(s->s_buf, uio->uio_resid, uio);
272         if (*error != 0) {
273                 sbuf_delete(s);
274                 return (NULL);
275         }
276         s->s_len = s->s_size - 1;
277         if (SBUF_ISSECTION(s))
278                 s->s_sect_len = s->s_size - 1;
279         *error = 0;
280         return (s);
281 }
282 #endif
283
284 int
285 sbuf_get_flags(struct sbuf *s)
286 {
287
288         return (s->s_flags & SBUF_USRFLAGMSK);
289 }
290
291 void
292 sbuf_clear_flags(struct sbuf *s, int flags)
293 {
294
295         s->s_flags &= ~(flags & SBUF_USRFLAGMSK);
296 }
297
298 void
299 sbuf_set_flags(struct sbuf *s, int flags)
300 {
301
302
303         s->s_flags |= (flags & SBUF_USRFLAGMSK);
304 }
305
306 /*
307  * Clear an sbuf and reset its position.
308  */
309 void
310 sbuf_clear(struct sbuf *s)
311 {
312
313         assert_sbuf_integrity(s);
314         /* don't care if it's finished or not */
315
316         SBUF_CLEARFLAG(s, SBUF_FINISHED);
317         s->s_error = 0;
318         s->s_len = 0;
319         s->s_rec_off = 0;
320         s->s_sect_len = 0;
321 }
322
323 /*
324  * Set the sbuf's end position to an arbitrary value.
325  * Effectively truncates the sbuf at the new position.
326  */
327 int
328 sbuf_setpos(struct sbuf *s, ssize_t pos)
329 {
330
331         assert_sbuf_integrity(s);
332         assert_sbuf_state(s, 0);
333
334         KASSERT(pos >= 0,
335             ("attempt to seek to a negative position (%jd)", (intmax_t)pos));
336         KASSERT(pos < s->s_size,
337             ("attempt to seek past end of sbuf (%jd >= %jd)",
338             (intmax_t)pos, (intmax_t)s->s_size));
339         KASSERT(!SBUF_ISSECTION(s),
340             ("attempt to seek when in a section"));
341
342         if (pos < 0 || pos > s->s_len)
343                 return (-1);
344         s->s_len = pos;
345         return (0);
346 }
347
348 /*
349  * Drain into a counter.  Counts amount of data without producing output.
350  * Useful for cases like sysctl, where user may first request only size.
351  * This allows to avoid pointless allocation/freeing of large buffers.
352  */
353 int
354 sbuf_count_drain(void *arg, const char *data __unused, int len)
355 {
356         size_t *sizep;
357
358         sizep = (size_t *)arg;
359         *sizep += len;
360         return (len);
361 }
362
363 /*
364  * Set up a drain function and argument on an sbuf to flush data to
365  * when the sbuf buffer overflows.
366  */
367 void
368 sbuf_set_drain(struct sbuf *s, sbuf_drain_func *func, void *ctx)
369 {
370
371         assert_sbuf_state(s, 0);
372         assert_sbuf_integrity(s);
373         KASSERT(func == s->s_drain_func || s->s_len == 0,
374             ("Cannot change drain to %p on non-empty sbuf %p", func, s));
375         s->s_drain_func = func;
376         s->s_drain_arg = ctx;
377 }
378
379 /*
380  * Call the drain and process the return.
381  */
382 int
383 sbuf_drain(struct sbuf *s)
384 {
385         int len;
386
387         /*
388          * Immediately return when no work to do,
389          * or an error has already been accumulated.
390          */
391         if ((s->s_len == 0) || (s->s_error != 0))
392                 return(s->s_error);
393
394         if (SBUF_DODRAINTOEOR(s) && s->s_rec_off == 0)
395                 return (s->s_error = EDEADLK);
396         len = s->s_drain_func(s->s_drain_arg, s->s_buf,
397             SBUF_DODRAINTOEOR(s) ? s->s_rec_off : s->s_len);
398         if (len <= 0) {
399                 s->s_error = len ? -len : EDEADLK;
400                 return (s->s_error);
401         }
402         KASSERT(len > 0 && len <= s->s_len,
403             ("Bad drain amount %d for sbuf %p", len, s));
404         s->s_len -= len;
405         s->s_rec_off -= len;
406         /*
407          * Fast path for the expected case where all the data was
408          * drained.
409          */
410         if (s->s_len == 0)
411                 return (0);
412         /*
413          * Move the remaining characters to the beginning of the
414          * string.
415          */
416         memmove(s->s_buf, s->s_buf + len, s->s_len);
417         return (0);
418 }
419
420 /*
421  * Append bytes to an sbuf.  This is the core function for appending
422  * to an sbuf and is the main place that deals with extending the
423  * buffer and marking overflow.
424  */
425 static void
426 sbuf_put_bytes(struct sbuf *s, const char *buf, size_t len)
427 {
428         size_t n;
429
430         assert_sbuf_integrity(s);
431         assert_sbuf_state(s, 0);
432
433         if (s->s_error != 0)
434                 return;
435         while (len > 0) {
436                 if (SBUF_FREESPACE(s) <= 0) {
437                         /*
438                          * If there is a drain, use it, otherwise extend the
439                          * buffer.
440                          */
441                         if (s->s_drain_func != NULL)
442                                 (void)sbuf_drain(s);
443                         else if (sbuf_extend(s, len > INT_MAX ? INT_MAX : len)
444                             < 0)
445                                 s->s_error = ENOMEM;
446                         if (s->s_error != 0)
447                                 return;
448                 }
449                 n = SBUF_FREESPACE(s);
450                 if (len < n)
451                         n = len;
452                 memcpy(&s->s_buf[s->s_len], buf, n);
453                 s->s_len += n;
454                 if (SBUF_ISSECTION(s))
455                         s->s_sect_len += n;
456                 len -= n;
457                 buf += n;
458         }
459 }
460
461 static void
462 sbuf_put_byte(struct sbuf *s, char c)
463 {
464
465         sbuf_put_bytes(s, &c, 1);
466 }
467
468 /*
469  * Append a byte string to an sbuf.
470  */
471 int
472 sbuf_bcat(struct sbuf *s, const void *buf, size_t len)
473 {
474
475         sbuf_put_bytes(s, buf, len);
476         if (s->s_error != 0)
477                 return (-1);
478         return (0);
479 }
480
481 #ifdef _KERNEL
482 /*
483  * Copy a byte string from userland into an sbuf.
484  */
485 int
486 sbuf_bcopyin(struct sbuf *s, const void *uaddr, size_t len)
487 {
488
489         assert_sbuf_integrity(s);
490         assert_sbuf_state(s, 0);
491         KASSERT(s->s_drain_func == NULL,
492             ("Nonsensical copyin to sbuf %p with a drain", s));
493
494         if (s->s_error != 0)
495                 return (-1);
496         if (len == 0)
497                 return (0);
498         if (len > SBUF_FREESPACE(s)) {
499                 sbuf_extend(s, len - SBUF_FREESPACE(s));
500                 if (SBUF_FREESPACE(s) < len)
501                         len = SBUF_FREESPACE(s);
502         }
503         if (copyin(uaddr, s->s_buf + s->s_len, len) != 0)
504                 return (-1);
505         s->s_len += len;
506
507         return (0);
508 }
509 #endif
510
511 /*
512  * Copy a byte string into an sbuf.
513  */
514 int
515 sbuf_bcpy(struct sbuf *s, const void *buf, size_t len)
516 {
517
518         assert_sbuf_integrity(s);
519         assert_sbuf_state(s, 0);
520
521         sbuf_clear(s);
522         return (sbuf_bcat(s, buf, len));
523 }
524
525 /*
526  * Append a string to an sbuf.
527  */
528 int
529 sbuf_cat(struct sbuf *s, const char *str)
530 {
531         size_t n;
532
533         n = strlen(str);
534         sbuf_put_bytes(s, str, n);
535         if (s->s_error != 0)
536                 return (-1);
537         return (0);
538 }
539
540 #ifdef _KERNEL
541 /*
542  * Append a string from userland to an sbuf.
543  */
544 int
545 sbuf_copyin(struct sbuf *s, const void *uaddr, size_t len)
546 {
547         size_t done;
548
549         assert_sbuf_integrity(s);
550         assert_sbuf_state(s, 0);
551         KASSERT(s->s_drain_func == NULL,
552             ("Nonsensical copyin to sbuf %p with a drain", s));
553
554         if (s->s_error != 0)
555                 return (-1);
556
557         if (len == 0)
558                 len = SBUF_FREESPACE(s);        /* XXX return 0? */
559         if (len > SBUF_FREESPACE(s)) {
560                 sbuf_extend(s, len);
561                 if (SBUF_FREESPACE(s) < len)
562                         len = SBUF_FREESPACE(s);
563         }
564         switch (copyinstr(uaddr, s->s_buf + s->s_len, len + 1, &done)) {
565         case ENAMETOOLONG:
566                 s->s_error = ENOMEM;
567                 /* fall through */
568         case 0:
569                 s->s_len += done - 1;
570                 if (SBUF_ISSECTION(s))
571                         s->s_sect_len += done - 1;
572                 break;
573         default:
574                 return (-1);    /* XXX */
575         }
576
577         return (done);
578 }
579 #endif
580
581 /*
582  * Copy a string into an sbuf.
583  */
584 int
585 sbuf_cpy(struct sbuf *s, const char *str)
586 {
587
588         assert_sbuf_integrity(s);
589         assert_sbuf_state(s, 0);
590
591         sbuf_clear(s);
592         return (sbuf_cat(s, str));
593 }
594
595 /*
596  * Format the given argument list and append the resulting string to an sbuf.
597  */
598 #ifdef _KERNEL
599
600 /*
601  * Append a non-NUL character to an sbuf.  This prototype signature is
602  * suitable for use with kvprintf(9).
603  */
604 static void
605 sbuf_putc_func(int c, void *arg)
606 {
607
608         if (c != '\0')
609                 sbuf_put_byte(arg, c);
610 }
611
612 int
613 sbuf_vprintf(struct sbuf *s, const char *fmt, va_list ap)
614 {
615
616         assert_sbuf_integrity(s);
617         assert_sbuf_state(s, 0);
618
619         KASSERT(fmt != NULL,
620             ("%s called with a NULL format string", __func__));
621
622         (void)kvprintf(fmt, sbuf_putc_func, s, 10, ap);
623         if (s->s_error != 0)
624                 return (-1);
625         return (0);
626 }
627 #else /* !_KERNEL */
628 int
629 sbuf_vprintf(struct sbuf *s, const char *fmt, va_list ap)
630 {
631         va_list ap_copy;
632         int error, len;
633
634         assert_sbuf_integrity(s);
635         assert_sbuf_state(s, 0);
636
637         KASSERT(fmt != NULL,
638             ("%s called with a NULL format string", __func__));
639
640         if (s->s_error != 0)
641                 return (-1);
642
643         /*
644          * For the moment, there is no way to get vsnprintf(3) to hand
645          * back a character at a time, to push everything into
646          * sbuf_putc_func() as was done for the kernel.
647          *
648          * In userspace, while drains are useful, there's generally
649          * not a problem attempting to malloc(3) on out of space.  So
650          * expand a userland sbuf if there is not enough room for the
651          * data produced by sbuf_[v]printf(3).
652          */
653
654         error = 0;
655         do {
656                 va_copy(ap_copy, ap);
657                 len = vsnprintf(&s->s_buf[s->s_len], SBUF_FREESPACE(s) + 1,
658                     fmt, ap_copy);
659                 if (len < 0) {
660                         s->s_error = errno;
661                         return (-1);
662                 }
663                 va_end(ap_copy);
664
665                 if (SBUF_FREESPACE(s) >= len)
666                         break;
667                 /* Cannot print with the current available space. */
668                 if (s->s_drain_func != NULL && s->s_len > 0)
669                         error = sbuf_drain(s); /* sbuf_drain() sets s_error. */
670                 else if (sbuf_extend(s, len - SBUF_FREESPACE(s)) != 0)
671                         s->s_error = error = ENOMEM;
672         } while (error == 0);
673
674         /*
675          * s->s_len is the length of the string, without the terminating nul.
676          * When updating s->s_len, we must subtract 1 from the length that
677          * we passed into vsnprintf() because that length includes the
678          * terminating nul.
679          *
680          * vsnprintf() returns the amount that would have been copied,
681          * given sufficient space, so don't over-increment s_len.
682          */
683         if (SBUF_FREESPACE(s) < len)
684                 len = SBUF_FREESPACE(s);
685         s->s_len += len;
686         if (SBUF_ISSECTION(s))
687                 s->s_sect_len += len;
688
689         KASSERT(s->s_len < s->s_size,
690             ("wrote past end of sbuf (%d >= %d)", s->s_len, s->s_size));
691
692         if (s->s_error != 0)
693                 return (-1);
694         return (0);
695 }
696 #endif /* _KERNEL */
697
698 /*
699  * Format the given arguments and append the resulting string to an sbuf.
700  */
701 int
702 sbuf_printf(struct sbuf *s, const char *fmt, ...)
703 {
704         va_list ap;
705         int result;
706
707         va_start(ap, fmt);
708         result = sbuf_vprintf(s, fmt, ap);
709         va_end(ap);
710         return (result);
711 }
712
713 /*
714  * Append a character to an sbuf.
715  */
716 int
717 sbuf_putc(struct sbuf *s, int c)
718 {
719
720         sbuf_put_byte(s, c);
721         if (s->s_error != 0)
722                 return (-1);
723         return (0);
724 }
725
726 /*
727  * Trim whitespace characters from end of an sbuf.
728  */
729 int
730 sbuf_trim(struct sbuf *s)
731 {
732
733         assert_sbuf_integrity(s);
734         assert_sbuf_state(s, 0);
735         KASSERT(s->s_drain_func == NULL,
736             ("%s makes no sense on sbuf %p with drain", __func__, s));
737
738         if (s->s_error != 0)
739                 return (-1);
740
741         while (s->s_len > 0 && isspace(s->s_buf[s->s_len-1])) {
742                 --s->s_len;
743                 if (SBUF_ISSECTION(s))
744                         s->s_sect_len--;
745         }
746
747         return (0);
748 }
749
750 /*
751  * Check if an sbuf has an error.
752  */
753 int
754 sbuf_error(const struct sbuf *s)
755 {
756
757         return (s->s_error);
758 }
759
760 /*
761  * Finish off an sbuf.
762  */
763 int
764 sbuf_finish(struct sbuf *s)
765 {
766
767         assert_sbuf_integrity(s);
768         assert_sbuf_state(s, 0);
769
770         s->s_buf[s->s_len] = '\0';
771         if (SBUF_NULINCLUDED(s))
772                 s->s_len++;
773         if (s->s_drain_func != NULL) {
774                 while (s->s_len > 0 && s->s_error == 0)
775                         s->s_error = sbuf_drain(s);
776         }
777         SBUF_SETFLAG(s, SBUF_FINISHED);
778 #ifdef _KERNEL
779         return (s->s_error);
780 #else
781         if (s->s_error != 0) {
782                 errno = s->s_error;
783                 return (-1);
784         }
785         return (0);
786 #endif
787 }
788
789 /*
790  * Return a pointer to the sbuf data.
791  */
792 char *
793 sbuf_data(struct sbuf *s)
794 {
795
796         assert_sbuf_integrity(s);
797         assert_sbuf_state(s, SBUF_FINISHED);
798         KASSERT(s->s_drain_func == NULL,
799             ("%s makes no sense on sbuf %p with drain", __func__, s));
800
801         return (s->s_buf);
802 }
803
804 /*
805  * Return the length of the sbuf data.
806  */
807 ssize_t
808 sbuf_len(struct sbuf *s)
809 {
810
811         assert_sbuf_integrity(s);
812         /* don't care if it's finished or not */
813         KASSERT(s->s_drain_func == NULL,
814             ("%s makes no sense on sbuf %p with drain", __func__, s));
815
816         if (s->s_error != 0)
817                 return (-1);
818
819         /* If finished, nulterm is already in len, else add one. */
820         if (SBUF_NULINCLUDED(s) && !SBUF_ISFINISHED(s))
821                 return (s->s_len + 1);
822         return (s->s_len);
823 }
824
825 /*
826  * Clear an sbuf, free its buffer if necessary.
827  */
828 void
829 sbuf_delete(struct sbuf *s)
830 {
831         int isdyn;
832
833         assert_sbuf_integrity(s);
834         /* don't care if it's finished or not */
835
836         if (SBUF_ISDYNAMIC(s))
837                 SBFREE(s->s_buf);
838         isdyn = SBUF_ISDYNSTRUCT(s);
839         memset(s, 0, sizeof(*s));
840         if (isdyn)
841                 SBFREE(s);
842 }
843
844 /*
845  * Check if an sbuf has been finished.
846  */
847 int
848 sbuf_done(const struct sbuf *s)
849 {
850
851         return (SBUF_ISFINISHED(s));
852 }
853
854 /*
855  * Start a section.
856  */
857 void
858 sbuf_start_section(struct sbuf *s, ssize_t *old_lenp)
859 {
860
861         assert_sbuf_integrity(s);
862         assert_sbuf_state(s, 0);
863
864         if (!SBUF_ISSECTION(s)) {
865                 KASSERT(s->s_sect_len == 0,
866                     ("s_sect_len != 0 when starting a section"));
867                 if (old_lenp != NULL)
868                         *old_lenp = -1;
869                 s->s_rec_off = s->s_len;
870                 SBUF_SETFLAG(s, SBUF_INSECTION);
871         } else {
872                 KASSERT(old_lenp != NULL,
873                     ("s_sect_len should be saved when starting a subsection"));
874                 *old_lenp = s->s_sect_len;
875                 s->s_sect_len = 0;
876         }
877 }
878
879 /*
880  * End the section padding to the specified length with the specified
881  * character.
882  */
883 ssize_t
884 sbuf_end_section(struct sbuf *s, ssize_t old_len, size_t pad, int c)
885 {
886         ssize_t len;
887
888         assert_sbuf_integrity(s);
889         assert_sbuf_state(s, 0);
890         KASSERT(SBUF_ISSECTION(s),
891             ("attempt to end a section when not in a section"));
892
893         if (pad > 1) {
894                 len = roundup(s->s_sect_len, pad) - s->s_sect_len;
895                 for (; s->s_error == 0 && len > 0; len--)
896                         sbuf_put_byte(s, c);
897         }
898         len = s->s_sect_len;
899         if (old_len == -1) {
900                 s->s_rec_off = s->s_sect_len = 0;
901                 SBUF_CLEARFLAG(s, SBUF_INSECTION);
902         } else {
903                 s->s_sect_len += old_len;
904         }
905         if (s->s_error != 0)
906                 return (-1);
907         return (len);
908 }